test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TStorageFactoryFile.cc
Go to the documentation of this file.
9 #include "ReadRepacker.h"
10 #include "TFileCacheRead.h"
11 #include "TSystem.h"
12 #include "TROOT.h"
13 #include "TEnv.h"
14 #include <errno.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <iostream>
19 #include <cassert>
20 
21 #if 0
22 #include "TTreeCache.h"
23 #include "TTree.h"
24 
25 class TTreeCacheDebug : public TTreeCache {
26 public:
27  void dump(const char *label, const char *trailer)
28  {
29  Long64_t entry = fOwner->GetReadEntry();
30  std::cerr
31  << label << ": " << entry << " "
32  << "{ fEntryMin=" << fEntryMin
33  << ", fEntryMax=" << fEntryMax
34  << ", fEntryNext=" << fEntryNext
35  << ", fZipBytes=" << fZipBytes
36  << ", fNbranches=" << fNbranches
37  << ", fNReadOk=" << fNReadOk
38  << ", fNReadMiss=" << fNReadMiss
39  << ", fNReadPref=" << fNReadPref
40  << ", fBranches=" << fBranches
41  << ", fBrNames=" << fBrNames
42  << ", fOwner=" << fOwner
43  << ", fTree=" << fTree
44  << ", fIsLearning=" << fIsLearning
45  << ", fIsManual=" << fIsManual
46  << "; fBufferSizeMin=" << fBufferSizeMin
47  << ", fBufferSize=" << fBufferSize
48  << ", fBufferLen=" << fBufferLen
49  << ", fBytesToPrefetch=" << fBytesToPrefetch
50  << ", fFirstIndexToPrefetch=" << fFirstIndexToPrefetch
51  << ", fAsyncReading=" << fAsyncReading
52  << ", fNseek=" << fNseek
53  << ", fNtot=" << fNtot
54  << ", fNb=" << fNb
55  << ", fSeekSize=" << fSeekSize
56  << ", fSeek=" << fSeek
57  << ", fSeekIndex=" << fSeekIndex
58  << ", fSeekSort=" << fSeekSort
59  << ", fPos=" << fPos
60  << ", fSeekLen=" << fSeekLen
61  << ", fSeekSortLen=" << fSeekSortLen
62  << ", fSeekPos=" << fSeekPos
63  << ", fLen=" << fLen
64  << ", fFile=" << fFile
65  << ", fBuffer=" << (void *) fBuffer
66  << ", fIsSorted=" << fIsSorted
67  << " }\n" << trailer;
68  }
69 };
70 #endif
71 
72 ClassImp(TStorageFactoryFile)
87 
88 
89 static inline StorageAccount::Counter &
91 {
92  static const auto token = StorageAccount::tokenForStorageClassName("tstoragefile");
93  if (! c) c = &StorageAccount::counter(token, operation);
94  return *c;
95 }
96 
98  : storage_()
99 {
101  stats.tick(0);
102 }
103 
104 // This constructor must be compatible with *all* the various built-in TFile plugins,
105 // including TXNetFile. This is why some arguments in the constructor is ignored.
106 // If there's a future T*File that is incompatible with this constructor, a new
107 // constructor will have to be added.
109  Option_t *option,
110  const char *ftitle,
111  Int_t compress,
112  Int_t netopt,
113  Bool_t parallelopen /* = kFALSE */)
114  : TFile(path, "NET", ftitle, compress), // Pass "NET" to prevent local access in base class
115  storage_()
116 {
117  try {
118  Initialize(path, option);
119  } catch (...) {
120  edm::threadLocalException::setException(std::current_exception()); // capture
121  }
122 }
123 
125  Option_t *option /* = "" */,
126  const char *ftitle /* = "" */,
127  Int_t compress /* = 1 */)
128  : TFile(path, "NET", ftitle, compress), // Pass "NET" to prevent local access in base class
129  storage_()
130 {
131  try {
132  Initialize(path, option);
133  } catch (...) {
134  edm::threadLocalException::setException(std::current_exception()); // capture
135  }
136 }
137 
138 void
140  Option_t *option /* = "" */)
141 {
143 
144  // Enable AsyncReading.
145  // This was the default for 5.27, but turned off by default for 5.32.
146  // In our testing, AsyncReading is the fastest mechanism available.
147  // In 5.32, the AsyncPrefetching mechanism is preferred, but has been a
148  // performance hit in our "average case" tests.
149  gEnv->SetValue("TFile.AsyncReading", 1);
150 
151  // Parse options; at the moment we only accept read!
152  fOption = option;
153  fOption.ToUpper();
154 
155  if (fOption == "NEW")
156  fOption = "CREATE";
157 
158  Bool_t create = (fOption == "CREATE");
159  Bool_t recreate = (fOption == "RECREATE");
160  Bool_t update = (fOption == "UPDATE");
161  Bool_t read = (fOption == "READ") || (fOption == "READWRAP");
162  Bool_t readwrap = (fOption == "READWRAP");
163 
164  if (!create && !recreate && !update && !read)
165  {
166  read = true;
167  fOption = "READ";
168  }
169 
170  if (recreate)
171  {
172  if (!gSystem->AccessPathName(path, kFileExists))
173  gSystem->Unlink(path);
174 
175  recreate = false;
176  create = true;
177  fOption = "CREATE";
178  }
179  assert(!recreate);
180 
181  if (update && gSystem->AccessPathName(path, kFileExists))
182  {
183  update = kFALSE;
184  create = kTRUE;
185  }
186 
187  assert(read || update || create);
188 
189  int openFlags = IOFlags::OpenRead;
190  if (!read) openFlags |= IOFlags::OpenWrite;
191  if (create) openFlags |= IOFlags::OpenCreate;
192  //if (recreate) openFlags |= IOFlags::OpenCreate | IOFlags::OpenTruncate;
193  if (readwrap) openFlags |= IOFlags::OpenWrap;
194 
195  // Open storage
196  if (! (storage_ = StorageFactory::get()->open(path, openFlags)))
197  {
198  MakeZombie();
199  gDirectory = gROOT;
200  throw cms::Exception("TStorageFactoryFile::TStorageFactoryFile()")
201  << "Cannot open file '" << path << "'";
202  }
203 
204  // Record the statistics.
205  try {
207  if (statsService.isAvailable()) {
208  statsService->setSize(storage_->size());
209  }
210  } catch (edm::Exception e) {
211  if (e.categoryCode() != edm::errors::NotFound) {
212  throw;
213  }
214  }
215 
216  fRealName = path;
217  fD = 0; // sorry, meaningless
218  fWritable = read ? kFALSE : kTRUE;
219 
220  Init(create);
221 
222  stats.tick(0);
223 }
224 
226 {
227  Close();
228 }
229 
233 
234 Bool_t
235 TStorageFactoryFile::ReadBuffer(char *buf, Long64_t pos, Int_t len)
236 {
237  // This function needs to be optimized to minimize seeks.
238  // See TFile::ReadBuffer(char *buf, Long64_t pos, Int_t len) in ROOT 5.27.06.
239  Seek(pos);
240  return ReadBuffer(buf, len);
241 }
242 
243 Bool_t
244 TStorageFactoryFile::ReadBuffer(char *buf, Int_t len)
245 {
246  // Check that it's valid to access this file.
247  if (IsZombie())
248  {
249  Error("ReadBuffer", "Cannot read from a zombie file");
250  return kTRUE;
251  }
252 
253  if (! IsOpen())
254  {
255  Error("ReadBuffer", "Cannot read from a file that is not open");
256  return kTRUE;
257  }
258 
259  // Read specified byte range from the storage. Returns kTRUE in
260  // case of error. Note that ROOT uses this function recursively
261  // to fill the cache; we use a flag to make sure our accounting
262  // is reflected in a comprehensible manner. The "read" counter
263  // will include both, "readc" indicates how much read from the
264  // cache, "readu" indicates how much we failed to read from the
265  // cache (excluding those recursive reads), and "readx" counts
266  // the amount actually passed to read from the storage object.
268 
269  // If we have a cache, read from there first. This returns 0
270  // if the block hasn't been prefetched, 1 if it was in cache,
271  // and 2 if there was an error.
272  if (TFileCacheRead *c = GetCacheRead())
273  {
274  Long64_t here = GetRelOffset();
275  Bool_t async = c->IsAsyncReading();
276 
277  StorageAccount::Stamp cstats(async
280 
281  Int_t st = ReadBufferViaCache(async ? 0 : buf, len);
282 
283  if (st == 2) {
284  return kTRUE;
285  }
286 
287  if (st == 1) {
288  if (async) {
289  cstats.tick(len);
290  Seek(here);
291  } else {
292  cstats.tick(len);
293  stats.tick(len);
294  return kFALSE;
295  }
296  }
297  }
298 
299  // FIXME: Re-enable read-ahead if the data wasn't in cache.
300  // if (! st) storage_->caching(true, -1, s_readahead);
301 
302  // A real read
304  IOSize n = storage_->xread(buf, len);
305  xstats.tick(n);
306  stats.tick(n);
307  return n ? kFALSE : kTRUE;
308 }
309 
310 Bool_t
311 TStorageFactoryFile::ReadBufferAsync(Long64_t off, Int_t len)
312 {
313  // Check that it's valid to access this file.
314  if (IsZombie())
315  {
316  Error("ReadBufferAsync", "Cannot read from a zombie file");
317  return kTRUE;
318  }
319 
320  if (! IsOpen())
321  {
322  Error("ReadBufferAsync", "Cannot read from a file that is not open");
323  return kTRUE;
324  }
325 
327 
328  // If asynchronous reading is disabled, bail out now, regardless
329  // whether the underlying storage supports prefetching. If it is
330  // forced on, pretend it's on, even if the storage doesn't support
331  // it, as this turns off the caching in ROOT's side.
333 
334  // Verify that we never using async reads in app-only mode
336  return kTRUE;
337 
338  // Let the I/O method indicate if it can do client-side prefetch.
339  // If it does, then for example TTreeCache will drop its own cache
340  // and will use the client-side cache of the actual I/O layer.
341  // If len is zero ROOT is probing for prefetch support.
342  if (len) {
343  // FIXME: Synchronise caching.
344  // storage_->caching(true, -1, 0);
345  ;
346  }
347 
348  IOPosBuffer iov(off, (void *) 0, len ? len : PREFETCH_PROBE_LENGTH);
349  if (storage_->prefetch(&iov, 1))
350  {
351  stats.tick(len);
352  return kFALSE;
353  }
354 
355  // Always ask ROOT to use async reads in storage-only mode,
356  // regardless of whether the storage system supports it.
358  return kFALSE;
359 
360  // Prefetching not available right now.
361  return kTRUE;
362 }
363 
364 Bool_t
365 TStorageFactoryFile::ReadBuffersSync(char *buf, Long64_t *pos, Int_t *len, Int_t nbuf)
366 {
387  Int_t remaining = nbuf; // Number of read requests left to process.
388  Int_t pack_count; // Number of read requests processed by this iteration.
389 
390  IOSize remaining_buffer_size=0;
391  // Calculate the remaining buffer size for the ROOT-owned buffer by adding
392  // the size of the various requests.
393  for (Int_t i=0; i<nbuf; i++) remaining_buffer_size+=len[i];
394 
395  char *current_buffer = buf;
396  Long64_t *current_pos = pos;
397  Int_t *current_len = len;
398 
399  ReadRepacker repacker;
400 
401  while (remaining > 0) {
402 
403  pack_count = repacker.pack(static_cast<long long int *>(current_pos), current_len, remaining, current_buffer, remaining_buffer_size);
404 
405  int real_bytes_processed = repacker.realBytesProcessed();
406  IOSize io_buffer_used = repacker.bufferUsed();
407 
408  // Issue readv, then unpack buffers.
410  std::vector<IOPosBuffer> &iov = repacker.iov();
411  IOSize result = storage_->readv(&iov[0], iov.size());
412  if (result != io_buffer_used) {
413  return kTRUE;
414  }
415  xstats.tick(io_buffer_used);
416  repacker.unpack(current_buffer);
417 
418  // Update the location of the unused part of the input buffer.
419  remaining_buffer_size -= real_bytes_processed;
420  current_buffer += real_bytes_processed;
421 
422  current_pos += pack_count;
423  current_len += pack_count;
424  remaining -= pack_count;
425 
426  }
427  assert(remaining_buffer_size == 0);
428  return kFALSE;
429 }
430 
431 Bool_t
432 TStorageFactoryFile::ReadBuffers(char *buf, Long64_t *pos, Int_t *len, Int_t nbuf)
433 {
434  // Check that it's valid to access this file.
435  if (IsZombie())
436  {
437  Error("ReadBuffers", "Cannot read from a zombie file");
438  return kTRUE;
439  }
440 
441  if (! IsOpen())
442  {
443  Error("ReadBuffers", "Cannot read from a file that is not open");
444  return kTRUE;
445  }
446 
447  // For synchronous reads, we have special logic to optimize the I/O requests
448  // from ROOT before handing it to the storage.
449  if (buf)
450  {
451  return ReadBuffersSync(buf, pos, len, nbuf);
452  }
453  // For an async read, we assume the storage system is smart enough to do the
454  // optimization itself.
455 
456  // Read from underlying storage.
457  void* const nobuf = 0;
458  Int_t total = 0;
459  std::vector<IOPosBuffer> iov;
460  iov.reserve(nbuf);
461  for (Int_t i = 0; i < nbuf; ++i)
462  {
463  iov.push_back(IOPosBuffer(pos[i], nobuf, len[i]));
464  total += len[i];
465  }
466 
467  // Null buffer means asynchronous reads into I/O system's cache.
468  bool success;
470  // Synchronise low-level cache with the supposed cache in TFile.
471  // storage_->caching(true, -1, 0);
472  success = storage_->prefetch(&iov[0], nbuf);
473  astats.tick(total);
474 
475  // If it didn't suceeed, pass down to the base class.
476  return success ? kFALSE : TFile::ReadBuffers(buf, pos, len, nbuf);
477 }
478 
479 Bool_t
480 TStorageFactoryFile::WriteBuffer(const char *buf, Int_t len)
481 {
482  // Check that it's valid to access this file.
483  if (IsZombie())
484  {
485  Error("WriteBuffer", "Cannot write to a zombie file");
486  return kTRUE;
487  }
488 
489  if (! IsOpen())
490  {
491  Error("WriteBuffer", "Cannot write to a file that is not open");
492  return kTRUE;
493  }
494 
495  if (! fWritable)
496  {
497  Error("WriteBuffer", "File is not writable");
498  return kTRUE;
499  }
500 
503 
504  // Try first writing via a cache, and if that's not possible, directly.
505  Int_t st;
506  switch ((st = WriteBufferViaCache(buf, len)))
507  {
508  case 0:
509  // Actual write.
510  {
512  IOSize n = storage_->xwrite(buf, len);
513  xstats.tick(n);
514  stats.tick(n);
515 
516  // FIXME: What if it's a short write?
517  return n > 0 ? kFALSE : kTRUE;
518  }
519 
520  case 1:
521  cstats.tick(len);
522  stats.tick(len);
523  return kFALSE;
524 
525  case 2:
526  default:
527  Error("WriteBuffer", "Error writing to cache");
528  return kTRUE;
529  }
530 }
531 
535 // FIXME: Override GetBytesToPrefetch() so XROOTD can suggest how
536 // large a prefetch cache to use.
537 // FIXME: Asynchronous open support?
538 
542 Int_t
543 TStorageFactoryFile::SysOpen(const char *pathname, Int_t flags, UInt_t /* mode */)
544 {
546 
547  if (storage_)
548  {
549  storage_->close();
550  }
551 
552  int openFlags = IOFlags::OpenRead;
553  if (flags & O_WRONLY) openFlags = IOFlags::OpenWrite;
554  else if (flags & O_RDWR) openFlags |= IOFlags::OpenWrite;
555  if (flags & O_CREAT) openFlags |= IOFlags::OpenCreate;
556  if (flags & O_APPEND) openFlags |= IOFlags::OpenAppend;
557  if (flags & O_EXCL) openFlags |= IOFlags::OpenExclusive;
558  if (flags & O_TRUNC) openFlags |= IOFlags::OpenTruncate;
559  if (flags & O_NONBLOCK) openFlags |= IOFlags::OpenNonBlock;
560 
561  if (! (storage_ = StorageFactory::get()->open(pathname, openFlags)))
562  {
563  MakeZombie();
564  gDirectory = gROOT;
565  throw cms::Exception("TStorageFactoryFile::SysOpen()")
566  << "Cannot open file '" << pathname << "'";
567  }
568 
569  stats.tick();
570  return 0;
571 }
572 
573 Int_t
575 {
577 
578  if (storage_)
579  {
580  storage_->close();
581  releaseStorage();
582  }
583 
584  stats.tick();
585  return 0;
586 }
587 
588 Long64_t
589 TStorageFactoryFile::SysSeek(Int_t /* fd */, Long64_t offset, Int_t whence)
590 {
592  Storage::Relative rel = (whence == SEEK_SET ? Storage::SET
593  : whence == SEEK_CUR ? Storage::CURRENT
594  : Storage::END);
595 
596  offset = storage_->position(offset, rel);
597  stats.tick();
598  return offset;
599 }
600 
601 Int_t
603 {
605  storage_->flush();
606  stats.tick();
607  return 0;
608 }
609 
610 Int_t
611 TStorageFactoryFile::SysStat(Int_t /* fd */, Long_t *id, Long64_t *size,
612  Long_t *flags, Long_t *modtime)
613 {
615  // FIXME: Most of this is unsupported or makes no sense with Storage
616  *id = ::Hash(fRealName);
617  *size = storage_->size();
618  *flags = 0;
619  *modtime = 0;
620  stats.tick();
621  return 0;
622 }
623 
624 void
626 {
627  TSystem::ResetErrno();
628 }
Code categoryCode() const
Definition: EDMException.h:93
int i
Definition: DBlmapReader.cc:9
CacheHint cacheHint(void) const
static StorageAccount::Counter * s_statsWrite
static StorageAccount::Counter * s_statsStat
#define PREFETCH_PROBE_LENGTH
Definition: Storage.h:18
virtual Long64_t SysSeek(Int_t fd, Long64_t offset, Int_t whence)
virtual Int_t SysOpen(const char *pathname, Int_t flags, UInt_t mode)
static StorageAccount::Counter * s_statsXWrite
static StorageAccount::Counter * s_statsClose
assert(m_qm.get())
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
virtual Bool_t ReadBuffers(char *buf, Long64_t *pos, Int_t *len, Int_t nbuf)
tuple result
Definition: mps_fire.py:83
edm::propagate_const< std::unique_ptr< Storage > > storage_
Relative
Definition: Storage.h:23
static const StorageFactory * get(void)
IOSize realBytesProcessed() const
Definition: ReadRepacker.h:53
static StorageAccount::Counter * s_statsARead
static StorageClassToken tokenForStorageClassName(std::string const &iName)
tuple iov
Definition: o2o.py:307
static StorageAccount::Counter & storageCounter(StorageAccount::Counter *&c, StorageAccount::Operation operation)
virtual Bool_t ReadBufferAsync(Long64_t off, Int_t len)
unsigned int(* Counter)(align::ID, const TrackerTopology *)
Definition: Counters.h:27
static StorageAccount::Counter * s_statsCtor
virtual Bool_t ReadBuffer(char *buf, Int_t len)
bool isAvailable() const
Definition: Service.h:46
XrdSiteStatisticsInformation * statsService
Definition: XrdSource.cc:219
std::string Hash
Definition: Types.h:45
virtual Bool_t WriteBuffer(const char *buf, Int_t len)
double f[11][100]
int pack(long long int *pos, int *len, int nbuf, char *buf, IOSize buffer_size)
Definition: ReadRepacker.cc:22
void unpack(char *buf)
static StorageAccount::Counter * s_statsRead
static StorageAccount::Counter * s_statsXRead
static StorageAccount::Counter * s_statsFlush
static Counter & counter(StorageClassToken token, Operation operation)
static StorageAccount::Counter * s_statsCWrite
IOSize bufferUsed() const
Definition: ReadRepacker.h:50
std::vector< IOPosBuffer > & iov()
Definition: ReadRepacker.h:48
void setException(std::exception_ptr e)
void ResetErrno(void) const
void tick(uint64_t amount=0, int64_t tick=0) const
#define update(a, b)
list entry
Definition: mps_splice.py:62
virtual Int_t SysStat(Int_t fd, Long_t *id, Long64_t *size, Long_t *flags, Long_t *modtime)
#define O_NONBLOCK
Definition: SysFile.h:21
size_t IOSize
Definition: IOTypes.h:14
virtual Int_t SysClose(Int_t fd)
static StorageAccount::Counter * s_statsSeek
static StorageAccount::Counter * s_statsOpen
virtual Int_t SysSync(Int_t fd)
Bool_t ReadBuffersSync(char *buf, Long64_t *pos, Int_t *len, Int_t nbuf)
void Initialize(const char *name, Option_t *option="")
SurfaceDeformation * create(int type, const std::vector< double > &params)
tuple size
Write out results.
static StorageAccount::Counter * s_statsCPrefetch
static StorageAccount::Counter * s_statsCRead