-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdFileCache.cc
578 lines (496 loc) · 14.9 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//----------------------------------------------------------------------------------
// 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;
XrdScheduler *Cache::schedP = 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;
}
class DiskSyncer : public XrdJob
{
private:
File *m_file;
public:
DiskSyncer(File *f, const char *desc = "") :
XrdJob(desc),
m_file(f)
{}
void DoIt()
{
m_file->Sync();
Cache::GetInstance().FileSyncDone(m_file);
delete 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_isClient(false)
{
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(Info, "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(Debug, 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::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);
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();
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--;
}
//______________________________________________________________________________
File* Cache::GetFile(const std::string& path, IO* iIO, long long off, long long filesize)
{
// Called from virtual IO::Attach
TRACE(Debug, "Cache::GetFile " << path);
XrdSysMutexHelper lock(&m_active_mutex);
ActiveMap_i it = m_active.find(path);
if (it != m_active.end())
{
IO* prevIO = it->second->SetIO(iIO);
if (prevIO)
{
prevIO->RelinquishFile(it->second);
}
else
{
inc_ref_cnt(it->second, false);
}
return it->second;
}
else
{
if (filesize == 0)
{
struct stat st;
int res = iIO->Fstat(st);
if (res) {
TRACE(Error, "Cache::GetFile, could not get valid stat");
return 0;
}
filesize = st.st_size;
}
File* file = new File(iIO, path, off, filesize);
inc_ref_cnt(file, false);
m_active[file->GetLocalPath()] = file;
return file;
}
}
void Cache::ReleaseFile(File* f)
{
// Called from virtual IO::Detach
TRACE(Debug, "Cache::ReleaseFile " << f->GetLocalPath());
f->ReleaseIO();
dec_ref_cnt(f);
}
//______________________________________________________________________________
namespace
{
void *callDoIt(void *pp)
{
XrdJob *jP = (XrdJob *)pp;
jP->DoIt();
return (void *)0;
}
};
void Cache::schedule_file_sync(File* f, bool ref_cnt_already_set)
{
DiskSyncer* ds = new DiskSyncer(f);
if ( ! ref_cnt_already_set) inc_ref_cnt(f, true);
if (m_isClient) ds->DoIt();
else if (schedP) schedP->Schedule(ds);
else {pthread_t tid;
XrdSysThread::Run(&tid, callDoIt, ds, 0, "DiskSyncer");
}
}
//______________________________________________________________________________
void Cache::FileSyncDone(File* f)
{
dec_ref_cnt(f);
}
//______________________________________________________________________________
void Cache::inc_ref_cnt(File* f, bool lock)
{
// called from GetFile() or SheduleFileSync();
TRACE(Debug, "Cache::inc_ref_cnt " << f->GetLocalPath());
if (lock) m_active_mutex.Lock();
f->inc_ref_cnt();
if (lock) m_active_mutex.UnLock();
}
//______________________________________________________________________________
void Cache::dec_ref_cnt(File* f)
{
// called from ReleaseFile() or DiskSync callback
m_active_mutex.Lock();
int cnt = f->get_ref_cnt();
m_active_mutex.UnLock();
TRACE(Debug, "Cache::dec_ref_cnt " << f->GetLocalPath() << ", cnt at entry = " << cnt);
if (cnt == 1)
{
if (f->FinalizeSyncBeforeExit())
{
TRACE(Debug, "Cache::dec_ref_cnt " << f->GetLocalPath() << ", scheduling final sync");
schedule_file_sync(f, true);
return;
}
}
m_active_mutex.Lock();
cnt = f->dec_ref_cnt();
TRACE(Debug, "Cache::dec_ref_cnt " << f->GetLocalPath() << ", cnt after sync_check and dec_ref_cnt = " << cnt);
if (cnt == 0)
{
ActiveMap_i it = m_active.find(f->GetLocalPath());
m_active.erase(it);
delete f;
}
m_active_mutex.UnLock();
}
//______________________________________________________________________________
bool Cache::HaveActiveFileWithLocalPath(std::string path)
{
XrdSysMutexHelper lock(&m_active_mutex);
ActiveMap_i it = m_active.find(path);
return (it != m_active.end());
}
//==============================================================================
//======================= 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 = int( 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);
}
}
}
//______________________________________________________________________________