/
XrdFileCache.cc
463 lines (401 loc) · 12.6 KB
/
XrdFileCache.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//----------------------------------------------------------------------------------
// Copyright (c) 2014 by Board of Trustees of the Leland Stanford, Jr., University
// Author: Alja Mrak-Tadel, Matevz Tadel, Brian Bockelman
//----------------------------------------------------------------------------------
// XRootD is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// XRootD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
//----------------------------------------------------------------------------------
#include <fcntl.h>
#include <sstream>
#include <algorithm>
#include <sys/statvfs.h>
#include "XrdCl/XrdClConstants.hh"
#include "XrdCl/XrdClURL.hh"
#include "XrdSys/XrdSysPthread.hh"
#include "XrdSys/XrdSysTimer.hh"
#include "XrdOss/XrdOss.hh"
#include "XrdOuc/XrdOucEnv.hh"
#include "XrdOuc/XrdOucUtils.hh"
#include "XrdOuc/XrdOucTrace.hh"
#include "XrdFileCache.hh"
#include "XrdFileCacheTrace.hh"
#include "XrdFileCacheInfo.hh"
#include "XrdFileCacheIOEntireFile.hh"
#include "XrdFileCacheIOFileBlock.hh"
using namespace XrdFileCache;
Cache * Cache::m_factory = NULL;
void *CacheDirCleanupThread(void* cache_void)
{
Cache::GetInstance().CacheDirCleanup();
return NULL;
}
void *ProcessWriteTaskThread(void* c)
{
Cache *cache = static_cast<Cache*>(c);
cache->ProcessWriteTasks();
return NULL;
}
void *PrefetchThread(void* ptr)
{
Cache* cache = static_cast<Cache*>(ptr);
cache->Prefetch();
return NULL;
}
extern "C"
{
XrdOucCache2 *XrdOucGetCache2(XrdSysLogger *logger,
const char *config_filename,
const char *parameters)
{
XrdSysError err(0, "");
err.logger(logger);
err.Emsg("Retrieve", "Retrieving a caching proxy factory.");
Cache &factory = Cache::GetInstance();
if (!factory.Config(logger, config_filename, parameters))
{
err.Emsg("Retrieve", "Error - unable to create a factory.");
return NULL;
}
err.Emsg("Retrieve", "Success - returning a factory.");
pthread_t tid1;
XrdSysThread::Run(&tid1, ProcessWriteTaskThread, (void*)(&factory), 0, "XrdFileCache WriteTasks ");
pthread_t tid2;
XrdSysThread::Run(&tid2, PrefetchThread, (void*)(&factory), 0, "XrdFileCache Prefetch ");
pthread_t tid;
XrdSysThread::Run(&tid, CacheDirCleanupThread, NULL, 0, "XrdFileCache CacheDirCleanup");
return &factory;
}
}
Cache &Cache::GetInstance()
{
if (m_factory == NULL)
m_factory = new Cache();
return *m_factory;
}
// !AMT will be obsolete in future
XrdOucCache *Cache::Create(Parms & parms, XrdOucCacheIO::aprParms * prParms)
{
return this;
}
//______________________________________________________________________________
bool Cache::Decide(XrdOucCacheIO* io)
{
if (!m_decisionpoints.empty())
{
std::string filename = io->Path();
std::vector<Decision*>::const_iterator it;
for (it = m_decisionpoints.begin(); it != m_decisionpoints.end(); ++it)
{
XrdFileCache::Decision *d = *it;
if (!d) continue;
if (!d->Decide(filename, *m_output_fs))
{
return false;
}
}
}
return true;
}
//______________________________________________________________________________
Cache::Cache() : XrdOucCache(),
m_log(0, "XrdFileCache_"),
m_trace(0),
m_traceID("Manager"),
m_prefetch_condVar(0),
m_RAMblocks_used(0)
{
m_trace = new XrdOucTrace(&m_log);
// default log level is Warning
m_trace->What = 2;
}
//______________________________________________________________________________
XrdOucCacheIO2 *Cache::Attach(XrdOucCacheIO2 *io, int Options)
{
if (Cache::GetInstance().Decide(io))
{
TRACE(Debug, "Cache::Attach() " << io->Path());
IO* cio;
if (Cache::GetInstance().RefConfiguration().m_hdfsmode)
cio = new IOFileBlock(io, m_stats, *this);
else
cio = new IOEntireFile(io, m_stats, *this);
TRACE_PC(Info, const char* loc = io->Location(),
"Cache::Attach() " << io->Path() << " location: " <<
((loc && loc[0] != 0) ? loc : "<deferred open>"));
return cio;
}
else
{
TRACE(Info, "Cache::Attach() decision decline " << io->Path());
}
return io;
}
//______________________________________________________________________________
int Cache::isAttached()
{
// virutal function of XrdOucCache, don't see it used in pfc or posix layer
return true;
}
void Cache::Detach(XrdOucCacheIO* io)
{
TRACE(Info, "Cache::Detach() " << io->Path());
// Cache owns File objects
XrdSysMutexHelper lock(&m_active_mutex);
std::vector<DiskNetIO>::iterator it = m_active.begin();
while ( it != m_active.end() )
{
if (it->io == io) {
if (it->file) {
it->io->RelinquishFile(it->file);
delete it->file;
}
m_active.erase(it);
if (m_configuration.m_hdfsmode == false) break;
}
else
++it;
}
delete io;
}
//______________________________________________________________________________
void
Cache::AddWriteTask(Block* b, bool fromRead)
{
TRACE(Dump, "Cache::AddWriteTask() bOff=%ld " << b->m_offset);
m_writeQ.condVar.Lock();
if (fromRead)
m_writeQ.queue.push_back(b);
else
m_writeQ.queue.push_front(b); // AMT should this not be the opposite
m_writeQ.size++;
m_writeQ.condVar.Signal();
m_writeQ.condVar.UnLock();
}
//______________________________________________________________________________
void Cache::RemoveWriteQEntriesFor(File *iFile)
{
m_writeQ.condVar.Lock();
std::list<Block*>::iterator i = m_writeQ.queue.begin();
while (i != m_writeQ.queue.end())
{
if ((*i)->m_file == iFile)
{
TRACE(Dump, "Cache::Remove entries for " << (void*)(*i) << " path " << iFile->lPath());
std::list<Block*>::iterator j = i++;
iFile->BlockRemovedFromWriteQ(*j);
m_writeQ.queue.erase(j);
--m_writeQ.size;
}
else
{
++i;
}
}
m_writeQ.condVar.UnLock();
}
//______________________________________________________________________________
void
Cache::ProcessWriteTasks()
{
while (true)
{
m_writeQ.condVar.Lock();
while (m_writeQ.queue.empty())
{
m_writeQ.condVar.Wait();
}
Block* block = m_writeQ.queue.front(); // AMT should not be back ???
m_writeQ.queue.pop_front();
m_writeQ.size--;
TRACE(Dump, "Cache::ProcessWriteTasks for %p " << (void*)(block) << " path " << block->m_file->lPath());
m_writeQ.condVar.UnLock();
block->m_file->WriteBlockToDisk(block);
}
}
//______________________________________________________________________________
bool
Cache::RequestRAMBlock()
{
XrdSysMutexHelper lock(&m_RAMblock_mutex);
if ( m_RAMblocks_used < Cache::GetInstance().RefConfiguration().m_NRamBuffers )
{
m_RAMblocks_used++;
return true;
}
return false;
}
void
Cache::RAMBlockReleased()
{
XrdSysMutexHelper lock(&m_RAMblock_mutex);
m_RAMblocks_used--;
}
void
Cache::AddActive(IO* io, File* file)
{
XrdSysMutexHelper lock(&m_active_mutex);
m_active.push_back(DiskNetIO(io, file));
}
//==============================================================================
//======================= File relinquish at process of dying ===================
//======================================================================
File* Cache::GetFileWithLocalPath(std::string path, IO* iIo)
{
XrdSysMutexHelper lock(&m_active_mutex);
for ( std::vector<DiskNetIO>::iterator it = m_active.begin(); it != m_active.end(); ++it)
{
if (it->file && (!strcmp(path.c_str(), it->file->lPath())))
{
File *ff = it->file;
it->io->RelinquishFile(ff);
it->file = 0;
return ff;
}
}
return 0;
}
//==============================================================================
//======================= PREFETCH ===================================
//==============================================================================
/*
namespace {
struct prefetch_less_than
{
inline bool operator() (const File* struct1, const File* struct2)
{
return (struct1->GetPrefetchScore() < struct2->GetPrefetchScore());
}
}myobject;
}*/
//______________________________________________________________________________
void
Cache::RegisterPrefetchFile(File* file)
{
// called from File::Open()
if (Cache::GetInstance().RefConfiguration().m_prefetch_max_blocks)
{
m_prefetch_condVar.Lock();
m_prefetchList.push_back(file);
m_prefetch_condVar.Signal();
m_prefetch_condVar.UnLock();
}
}
//______________________________________________________________________________
void
Cache::DeRegisterPrefetchFile(File* file)
{
// called from last line File::InitiateClose()
m_prefetch_condVar.Lock();
for (PrefetchList::iterator it = m_prefetchList.begin(); it != m_prefetchList.end(); ++it) {
if (*it == file) {
m_prefetchList.erase(it);
break;
}
}
m_prefetch_condVar.UnLock();
}
//______________________________________________________________________________
File*
Cache::GetNextFileToPrefetch()
{
m_prefetch_condVar.Lock();
while (m_prefetchList.empty()) {
m_prefetch_condVar.Wait();
}
// std::sort(m_prefetchList.begin(), m_prefetchList.end(), myobject);
size_t l = m_prefetchList.size();
int idx = rand() % l;
File* f = m_prefetchList[idx];
m_prefetch_condVar.UnLock();
return f;
}
//______________________________________________________________________________
//! Preapare the cache for a file open request. This method is called prior to
//! actually opening a file. This method is meant to allow defering an open
//! request or implementing the full I/O stack in the cache layer.
//! @return <0 Error has occurred, return value is -errno; fail open request.
//! =0 Continue with open() request.
//! >0 Defer open but treat the file as actually being open. Use the
//! XrdOucCacheIO2::Open() method to open the file at a later time.
//------------------------------------------------------------------------------
int
Cache::Prepare(const char *url, int oflags, mode_t mode)
{
std::string curl(url);
XrdCl::URL xx(curl);
std::string spath = xx.GetPath();
spath += ".cinfo";
struct stat buf;
int res = m_output_fs->Stat(spath.c_str(), &buf);
if (res == 0) {
TRACE( Dump, "Cache::Prefetch defer open " << spath);
return 1;
}
else {
return 0;
}
}
//______________________________________________________________________________
// virtual method of XrdOucCache2::Stat()
//!
//! @return <0 - Stat failed, value is -errno.
//! =0 - Stat succeeded, sbuff holds stat information.
//! >0 - Stat could not be done, forward operation to next level.
//------------------------------------------------------------------------------
int Cache::Stat(const char *curl, struct stat &sbuff)
{
XrdCl::URL url(curl);
std::string name = url.GetPath();
name += ".cinfo";
if (m_output_fs->Stat(name.c_str(), &sbuff) == XrdOssOK) {
if ( S_ISDIR(sbuff.st_mode)) {
return 0;
}
else {
bool success = false;
XrdOssDF* infoFile = m_output_fs->newFile(m_configuration.m_username.c_str());
XrdOucEnv myEnv;
int res = infoFile->Open(name.c_str(), O_RDONLY, 0600, myEnv);
if (res >= 0) {
Info info(m_trace, 0);
if (info.Read(infoFile, name))
{
sbuff.st_size = info.GetFileSize();
success = true;
}
}
infoFile->Close();
delete infoFile;
return success ? 0 : 1;
}
}
return 1;
}
//______________________________________________________________________________
void
Cache::Prefetch()
{
int limitRAM= Cache::GetInstance().RefConfiguration().m_NRamBuffers * 0.7;
while (true) {
m_RAMblock_mutex.Lock();
bool doPrefetch = (m_RAMblocks_used < limitRAM);
m_RAMblock_mutex.UnLock();
if (doPrefetch) {
File* f = GetNextFileToPrefetch();
f->Prefetch();
}
else {
XrdSysTimer::Wait(5);
}
}
}