-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathTStorageFactoryFile.cc
596 lines (513 loc) · 20.2 KB
/
TStorageFactoryFile.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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#include "IOPool/TFileAdaptor/interface/TStorageFactoryFile.h"
#include "Utilities/StorageFactory/interface/Storage.h"
#include "Utilities/StorageFactory/interface/StorageFactory.h"
#include "Utilities/StorageFactory/interface/StorageAccount.h"
#include "Utilities/StorageFactory/interface/StatisticsSenderService.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Utilities/interface/EDMException.h"
#include "FWCore/Utilities/interface/ExceptionPropagate.h"
#include "ReadRepacker.h"
#include "TFileCacheRead.h"
#include "TSystem.h"
#include "TROOT.h"
#include "TEnv.h"
#include <cerrno>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <iostream>
#include <cassert>
#if 0
#include "TTreeCache.h"
#include "TTree.h"
class TTreeCacheDebug : public TTreeCache {
public:
void dump(const char *label, const char *trailer)
{
Long64_t entry = fOwner->GetReadEntry();
std::cerr
<< label << ": " << entry << " "
<< "{ fEntryMin=" << fEntryMin
<< ", fEntryMax=" << fEntryMax
<< ", fEntryNext=" << fEntryNext
<< ", fZipBytes=" << fZipBytes
<< ", fNbranches=" << fNbranches
<< ", fNReadOk=" << fNReadOk
<< ", fNReadMiss=" << fNReadMiss
<< ", fNReadPref=" << fNReadPref
<< ", fBranches=" << fBranches
<< ", fBrNames=" << fBrNames
<< ", fOwner=" << fOwner
<< ", fTree=" << fTree
<< ", fIsLearning=" << fIsLearning
<< ", fIsManual=" << fIsManual
<< "; fBufferSizeMin=" << fBufferSizeMin
<< ", fBufferSize=" << fBufferSize
<< ", fBufferLen=" << fBufferLen
<< ", fBytesToPrefetch=" << fBytesToPrefetch
<< ", fFirstIndexToPrefetch=" << fFirstIndexToPrefetch
<< ", fAsyncReading=" << fAsyncReading
<< ", fNseek=" << fNseek
<< ", fNtot=" << fNtot
<< ", fNb=" << fNb
<< ", fSeekSize=" << fSeekSize
<< ", fSeek=" << fSeek
<< ", fSeekIndex=" << fSeekIndex
<< ", fSeekSort=" << fSeekSort
<< ", fPos=" << fPos
<< ", fSeekLen=" << fSeekLen
<< ", fSeekSortLen=" << fSeekSortLen
<< ", fSeekPos=" << fSeekPos
<< ", fLen=" << fLen
<< ", fFile=" << fFile
<< ", fBuffer=" << (void *) fBuffer
<< ", fIsSorted=" << fIsSorted
<< " }\n" << trailer;
}
};
#endif
ClassImp(TStorageFactoryFile);
static StorageAccount::Counter *s_statsCtor = nullptr;
static StorageAccount::Counter *s_statsOpen = nullptr;
static StorageAccount::Counter *s_statsClose = nullptr;
static StorageAccount::Counter *s_statsFlush = nullptr;
static StorageAccount::Counter *s_statsStat = nullptr;
static StorageAccount::Counter *s_statsSeek = nullptr;
static StorageAccount::Counter *s_statsRead = nullptr;
static StorageAccount::Counter *s_statsCRead = nullptr;
static StorageAccount::Counter *s_statsCPrefetch = nullptr;
static StorageAccount::Counter *s_statsARead = nullptr;
static StorageAccount::Counter *s_statsXRead = nullptr;
static StorageAccount::Counter *s_statsWrite = nullptr;
static StorageAccount::Counter *s_statsCWrite = nullptr;
static StorageAccount::Counter *s_statsXWrite = nullptr;
static inline StorageAccount::Counter &storageCounter(StorageAccount::Counter *&c,
StorageAccount::Operation operation) {
static const auto token = StorageAccount::tokenForStorageClassName("tstoragefile");
if (!c)
c = &StorageAccount::counter(token, operation);
return *c;
}
TStorageFactoryFile::TStorageFactoryFile(void) : storage_() {
StorageAccount::Stamp stats(storageCounter(s_statsCtor, StorageAccount::Operation::construct));
stats.tick(0);
}
// This constructor must be compatible with *all* the various built-in TFile plugins,
// including TXNetFile. This is why some arguments in the constructor is ignored.
// If there's a future T*File that is incompatible with this constructor, a new
// constructor will have to be added.
TStorageFactoryFile::TStorageFactoryFile(const char *path,
Option_t *option,
const char *ftitle,
Int_t compress,
Int_t netopt,
Bool_t parallelopen /* = kFALSE */)
: TFile(path, "NET", ftitle, compress), // Pass "NET" to prevent local access in base class
storage_() {
try {
Initialize(path, option);
} catch (...) {
edm::threadLocalException::setException(std::current_exception()); // capture
}
}
TStorageFactoryFile::TStorageFactoryFile(const char *path,
Option_t *option /* = "" */,
const char *ftitle /* = "" */,
Int_t compress /* = 1 */)
: TFile(path, "NET", ftitle, compress), // Pass "NET" to prevent local access in base class
storage_() {
try {
Initialize(path, option);
} catch (...) {
edm::threadLocalException::setException(std::current_exception()); // capture
}
}
void TStorageFactoryFile::Initialize(const char *path, Option_t *option /* = "" */) {
StorageAccount::Stamp stats(storageCounter(s_statsCtor, StorageAccount::Operation::construct));
// Enable AsyncReading.
// This was the default for 5.27, but turned off by default for 5.32.
// In our testing, AsyncReading is the fastest mechanism available.
// In 5.32, the AsyncPrefetching mechanism is preferred, but has been a
// performance hit in our "average case" tests.
gEnv->SetValue("TFile.AsyncReading", 1);
// Parse options; at the moment we only accept read!
fOption = option;
fOption.ToUpper();
if (fOption == "NEW")
fOption = "CREATE";
Bool_t create = (fOption == "CREATE");
Bool_t recreate = (fOption == "RECREATE");
Bool_t update = (fOption == "UPDATE");
Bool_t read = (fOption == "READ") || (fOption == "READWRAP");
Bool_t readwrap = (fOption == "READWRAP");
if (!create && !recreate && !update && !read) {
read = true;
fOption = "READ";
}
if (recreate) {
if (!gSystem->AccessPathName(path, kFileExists))
gSystem->Unlink(path);
recreate = false;
create = true;
fOption = "CREATE";
}
assert(!recreate);
if (update && gSystem->AccessPathName(path, kFileExists)) {
update = kFALSE;
create = kTRUE;
}
assert(read || update || create);
int openFlags = IOFlags::OpenRead;
if (!read)
openFlags |= IOFlags::OpenWrite;
if (create)
openFlags |= IOFlags::OpenCreate;
//if (recreate) openFlags |= IOFlags::OpenCreate | IOFlags::OpenTruncate;
if (readwrap)
openFlags |= IOFlags::OpenWrap;
// Open storage
if (!(storage_ = StorageFactory::get()->open(path, openFlags))) {
MakeZombie();
gDirectory = gROOT;
throw cms::Exception("TStorageFactoryFile::TStorageFactoryFile()") << "Cannot open file '" << path << "'";
}
// Record the statistics.
try {
edm::Service<edm::storage::StatisticsSenderService> statsService;
if (statsService.isAvailable()) {
statsService->setSize(storage_->size());
}
} catch (edm::Exception const &e) {
if (e.categoryCode() != edm::errors::NotFound) {
throw;
}
}
fRealName = path;
fD = 0; // sorry, meaningless
fWritable = read ? kFALSE : kTRUE;
Init(create);
stats.tick(0);
}
TStorageFactoryFile::~TStorageFactoryFile(void) { Close(); }
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
Bool_t TStorageFactoryFile::ReadBuffer(char *buf, Long64_t pos, Int_t len) {
// This function needs to be optimized to minimize seeks.
// See TFile::ReadBuffer(char *buf, Long64_t pos, Int_t len) in ROOT 5.27.06.
Seek(pos);
return ReadBuffer(buf, len);
}
Bool_t TStorageFactoryFile::ReadBuffer(char *buf, Int_t len) {
// Check that it's valid to access this file.
if (IsZombie()) {
Error("ReadBuffer", "Cannot read from a zombie file");
return kTRUE;
}
if (!IsOpen()) {
Error("ReadBuffer", "Cannot read from a file that is not open");
return kTRUE;
}
// Read specified byte range from the storage. Returns kTRUE in
// case of error. Note that ROOT uses this function recursively
// to fill the cache; we use a flag to make sure our accounting
// is reflected in a comprehensible manner. The "read" counter
// will include both, "readc" indicates how much read from the
// cache, "readu" indicates how much we failed to read from the
// cache (excluding those recursive reads), and "readx" counts
// the amount actually passed to read from the storage object.
StorageAccount::Stamp stats(storageCounter(s_statsRead, StorageAccount::Operation::read));
// If we have a cache, read from there first. This returns 0
// if the block hasn't been prefetched, 1 if it was in cache,
// and 2 if there was an error.
if (TFileCacheRead *c = GetCacheRead()) {
Long64_t here = GetRelOffset();
Bool_t async = c->IsAsyncReading();
StorageAccount::Stamp cstats(async
? storageCounter(s_statsCPrefetch, StorageAccount::Operation::readPrefetchToCache)
: storageCounter(s_statsCRead, StorageAccount::Operation::readViaCache));
Int_t st = ReadBufferViaCache(async ? nullptr : buf, len);
if (st == 2) {
Error("ReadBuffer",
"ReadBufferViaCache failed. Asked to read nBytes: %d from offset: %lld with file size: %lld",
len,
here,
GetSize());
return kTRUE;
}
if (st == 1) {
if (async) {
cstats.tick(len);
Seek(here);
} else {
cstats.tick(len);
stats.tick(len);
return kFALSE;
}
}
}
// FIXME: Re-enable read-ahead if the data wasn't in cache.
// if (! st) storage_->caching(true, -1, s_readahead);
// A real read
StorageAccount::Stamp xstats(storageCounter(s_statsXRead, StorageAccount::Operation::readActual));
IOSize n = storage_->xread(buf, len);
xstats.tick(n);
stats.tick(n);
if (n < static_cast<IOSize>(len)) {
Error("ReadBuffer",
"read from Storage::xread returned %ld. Asked to read n bytes: %d from offset: %lld with file size: %lld",
n,
len,
GetRelOffset(),
GetSize());
}
return n ? kFALSE : kTRUE;
}
Bool_t TStorageFactoryFile::ReadBufferAsync(Long64_t off, Int_t len) {
// Check that it's valid to access this file.
if (IsZombie()) {
Error("ReadBufferAsync", "Cannot read from a zombie file");
return kTRUE;
}
if (!IsOpen()) {
Error("ReadBufferAsync", "Cannot read from a file that is not open");
return kTRUE;
}
StorageAccount::Stamp stats(storageCounter(s_statsARead, StorageAccount::Operation::readAsync));
// If asynchronous reading is disabled, bail out now, regardless
// whether the underlying storage supports prefetching. If it is
// forced on, pretend it's on, even if the storage doesn't support
// it, as this turns off the caching in ROOT's side.
const StorageFactory *f = StorageFactory::get();
// Verify that we never using async reads in app-only mode
if (f->cacheHint() == StorageFactory::CACHE_HINT_APPLICATION)
return kTRUE;
// Let the I/O method indicate if it can do client-side prefetch.
// If it does, then for example TTreeCache will drop its own cache
// and will use the client-side cache of the actual I/O layer.
// If len is zero ROOT is probing for prefetch support.
if (len) {
// FIXME: Synchronise caching.
// storage_->caching(true, -1, 0);
;
}
IOPosBuffer iov(off, (void *)nullptr, len ? len : PREFETCH_PROBE_LENGTH);
if (storage_->prefetch(&iov, 1)) {
stats.tick(len);
return kFALSE;
}
// Always ask ROOT to use async reads in storage-only mode,
// regardless of whether the storage system supports it.
if (f->cacheHint() == StorageFactory::CACHE_HINT_STORAGE)
return kFALSE;
// Prefetching not available right now.
return kTRUE;
}
Bool_t TStorageFactoryFile::ReadBuffersSync(char *buf, Long64_t *pos, Int_t *len, Int_t nbuf) {
/** Most storage systems are not prepared for the onslaught of small reads
* that ROOT will perform, even if they implement a vectored read interface.
*
* Typically, on the server side, the loop is unrolled and the reads are
* issued sequentially - giving the OS no hint that you're about to read
* a very close-by byte in the near future. Normally, OS read-ahead takes
* care of such situations; because the storage server has so many clients,
* and ROOT reads look random to the OS, the read-ahead becomes disabled.
*
* Hence, this function will repack the application-layer request into an
* optimized storage-layer request. The resulting request to the storage
* layer typically has a slightly larger number of bytes, but far less
* individual reads.
*
* On average, the server's disks see a smaller number of overall reads,
* the number of bytes transferred over the network increases modestly
* (around 10%), and the single application request becomes one-to-two
* I/O transactions. A clear win for all cases except high-latency WAN.
*/
Int_t remaining = nbuf; // Number of read requests left to process.
Int_t pack_count; // Number of read requests processed by this iteration.
IOSize remaining_buffer_size = 0;
// Calculate the remaining buffer size for the ROOT-owned buffer by adding
// the size of the various requests.
for (Int_t i = 0; i < nbuf; i++)
remaining_buffer_size += len[i];
char *current_buffer = buf;
Long64_t *current_pos = pos;
Int_t *current_len = len;
ReadRepacker repacker;
while (remaining > 0) {
pack_count = repacker.pack(
static_cast<long long int *>(current_pos), current_len, remaining, current_buffer, remaining_buffer_size);
int real_bytes_processed = repacker.realBytesProcessed();
IOSize io_buffer_used = repacker.bufferUsed();
// Issue readv, then unpack buffers.
StorageAccount::Stamp xstats(storageCounter(s_statsXRead, StorageAccount::Operation::readActual));
std::vector<IOPosBuffer> &iov = repacker.iov();
IOSize result = storage_->readv(&iov[0], iov.size());
if (result != io_buffer_used) {
Error(
"ReadBuffersSync", "Storage::readv returned different size result=%ld expected=%ld", result, io_buffer_used);
return kTRUE;
}
xstats.tick(io_buffer_used);
repacker.unpack(current_buffer);
// Update the location of the unused part of the input buffer.
remaining_buffer_size -= real_bytes_processed;
current_buffer += real_bytes_processed;
current_pos += pack_count;
current_len += pack_count;
remaining -= pack_count;
}
assert(remaining_buffer_size == 0);
return kFALSE;
}
Bool_t TStorageFactoryFile::ReadBuffers(char *buf, Long64_t *pos, Int_t *len, Int_t nbuf) {
// Check that it's valid to access this file.
if (IsZombie()) {
Error("ReadBuffers", "Cannot read from a zombie file");
return kTRUE;
}
if (!IsOpen()) {
Error("ReadBuffers", "Cannot read from a file that is not open");
return kTRUE;
}
// For synchronous reads, we have special logic to optimize the I/O requests
// from ROOT before handing it to the storage.
if (buf) {
return ReadBuffersSync(buf, pos, len, nbuf);
}
// For an async read, we assume the storage system is smart enough to do the
// optimization itself.
// Read from underlying storage.
void *const nobuf = nullptr;
Int_t total = 0;
std::vector<IOPosBuffer> iov;
iov.reserve(nbuf);
for (Int_t i = 0; i < nbuf; ++i) {
iov.emplace_back(pos[i], nobuf, len[i]);
total += len[i];
}
// Null buffer means asynchronous reads into I/O system's cache.
bool success;
StorageAccount::Stamp astats(storageCounter(s_statsARead, StorageAccount::Operation::readAsync));
// Synchronise low-level cache with the supposed cache in TFile.
// storage_->caching(true, -1, 0);
success = storage_->prefetch(iov.data(), nbuf);
astats.tick(total);
// If it didn't suceeed, pass down to the base class.
if (not success) {
if (TFile::ReadBuffers(buf, pos, len, nbuf)) {
Error("ReadBuffers", "call to TFile::ReadBuffers failed after prefetch already failed.");
return kTRUE;
}
}
return kFALSE;
}
Bool_t TStorageFactoryFile::WriteBuffer(const char *buf, Int_t len) {
// Check that it's valid to access this file.
if (IsZombie()) {
Error("WriteBuffer", "Cannot write to a zombie file");
return kTRUE;
}
if (!IsOpen()) {
Error("WriteBuffer", "Cannot write to a file that is not open");
return kTRUE;
}
if (!fWritable) {
Error("WriteBuffer", "File is not writable");
return kTRUE;
}
StorageAccount::Stamp stats(storageCounter(s_statsWrite, StorageAccount::Operation::write));
StorageAccount::Stamp cstats(storageCounter(s_statsCWrite, StorageAccount::Operation::writeViaCache));
// Try first writing via a cache, and if that's not possible, directly.
Int_t st;
switch ((st = WriteBufferViaCache(buf, len))) {
case 0:
// Actual write.
{
StorageAccount::Stamp xstats(storageCounter(s_statsXWrite, StorageAccount::Operation::writeActual));
IOSize n = storage_->xwrite(buf, len);
xstats.tick(n);
stats.tick(n);
// FIXME: What if it's a short write?
return n > 0 ? kFALSE : kTRUE;
}
case 1:
cstats.tick(len);
stats.tick(len);
return kFALSE;
case 2:
default:
Error("WriteBuffer", "Error writing to cache");
return kTRUE;
}
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// FIXME: Override GetBytesToPrefetch() so XROOTD can suggest how
// large a prefetch cache to use.
// FIXME: Asynchronous open support?
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
Int_t TStorageFactoryFile::SysOpen(const char *pathname, Int_t flags, UInt_t /* mode */) {
StorageAccount::Stamp stats(storageCounter(s_statsOpen, StorageAccount::Operation::open));
if (storage_) {
storage_->close();
}
int openFlags = IOFlags::OpenRead;
if (flags & O_WRONLY)
openFlags = IOFlags::OpenWrite;
else if (flags & O_RDWR)
openFlags |= IOFlags::OpenWrite;
if (flags & O_CREAT)
openFlags |= IOFlags::OpenCreate;
if (flags & O_APPEND)
openFlags |= IOFlags::OpenAppend;
if (flags & O_EXCL)
openFlags |= IOFlags::OpenExclusive;
if (flags & O_TRUNC)
openFlags |= IOFlags::OpenTruncate;
if (flags & O_NONBLOCK)
openFlags |= IOFlags::OpenNonBlock;
if (!(storage_ = StorageFactory::get()->open(pathname, openFlags))) {
MakeZombie();
gDirectory = gROOT;
throw cms::Exception("TStorageFactoryFile::SysOpen()") << "Cannot open file '" << pathname << "'";
}
stats.tick();
return 0;
}
Int_t TStorageFactoryFile::SysClose(Int_t /* fd */) {
StorageAccount::Stamp stats(storageCounter(s_statsClose, StorageAccount::Operation::close));
if (storage_) {
storage_->close();
releaseStorage();
}
stats.tick();
return 0;
}
Long64_t TStorageFactoryFile::SysSeek(Int_t /* fd */, Long64_t offset, Int_t whence) {
StorageAccount::Stamp stats(storageCounter(s_statsSeek, StorageAccount::Operation::seek));
Storage::Relative rel = (whence == SEEK_SET ? Storage::SET : whence == SEEK_CUR ? Storage::CURRENT : Storage::END);
offset = storage_->position(offset, rel);
stats.tick();
return offset;
}
Int_t TStorageFactoryFile::SysSync(Int_t /* fd */) {
StorageAccount::Stamp stats(storageCounter(s_statsFlush, StorageAccount::Operation::flush));
storage_->flush();
stats.tick();
return 0;
}
Int_t TStorageFactoryFile::SysStat(Int_t /* fd */, Long_t *id, Long64_t *size, Long_t *flags, Long_t *modtime) {
StorageAccount::Stamp stats(storageCounter(s_statsStat, StorageAccount::Operation::stat));
// FIXME: Most of this is unsupported or makes no sense with Storage
*id = ::Hash(fRealName);
*size = storage_->size();
*flags = 0;
*modtime = 0;
stats.tick();
return 0;
}
void TStorageFactoryFile::ResetErrno(void) const { TSystem::ResetErrno(); }