Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TFileCacheRead.cxx
Go to the documentation of this file.
1// @(#)root/io:$Id$
2// Author: Rene Brun 18/05/2006
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12/**
13 \class TFileCacheRead
14 \ingroup io_files
15
16 A cache when reading files over the network.
17
18 A caching system to speed up network I/O, i.e. when there is
19 no operating system caching support (like the buffer cache for
20 local disk I/O). The cache makes sure that every I/O is done with
21 a (large) fixed length buffer thereby avoiding many small I/O's.
22 Currently the read cache system is used by the classes like
23 TNetXNGFile (via TFile::ReadBuffers()).
24 When processing TTree, TChain, a specialized class TTreeCache that
25 derives from this class is automatically created.
26*/
27
28#include "TEnv.h"
29#include "TFile.h"
30#include "TFileCacheRead.h"
31#include "TFileCacheWrite.h"
32#include "TFilePrefetch.h"
33#include "TMathBase.h"
34#include <limits>
35
36
37////////////////////////////////////////////////////////////////////////////////
38/// Default Constructor.
39
41{
43 fBufferSize = 0;
44 fBufferLen = 0;
45 fBytesRead = 0;
48 fReadCalls = 0;
50 fNseek = 0;
51 fNtot = 0;
52 fNb = 0;
53 fSeekSize = 0;
54 fSeek = 0;
55 fSeekIndex = 0;
56 fSeekSort = 0;
57 fPos = 0;
58 fSeekLen = 0;
59 fSeekSortLen = 0;
60 fSeekPos = 0;
61 fLen = 0;
62 fFile = 0;
63 fBuffer = 0;
66
67 //values for the second prefetched block
68 fBNseek = 0;
69 fBNtot = 0;
70 fBNb = 0;
71 fBSeekSize = 0;
72 fBSeek = 0;
73 fBSeekSort = 0;
74 fBSeekIndex = 0;
75 fBPos = 0;
76 fBSeekLen = 0;
77 fBSeekSortLen = 0;
78 fBSeekPos = 0;
79 fBLen = 0;
82
85 fPrefetch = 0;
87}
88
89////////////////////////////////////////////////////////////////////////////////
90/// Creates a TFileCacheRead data structure.
91
93 : TObject()
94{
95 if (bufsize <=10000) fBufferSize = 100000;
96 else fBufferSize = bufsize;
97
99 fBufferLen = 0;
100 fBytesRead = 0;
102 fBytesReadExtra = 0;
103 fReadCalls = 0;
105 fNseek = 0;
106 fNtot = 0;
107 fNb = 0;
108 fSeekSize = 10000;
109 fSeek = new Long64_t[fSeekSize];
112 fPos = new Long64_t[fSeekSize];
113 fSeekLen = new Int_t[fSeekSize];
115 fSeekPos = new Int_t[fSeekSize];
116 fLen = new Int_t[fSeekSize];
117 fFile = file;
118
119 //initialisation for the second block
120 fBNseek = 0;
121 fBNtot = 0;
122 fBNb = 0;
123 fBSeekSize = 10000;
131 fBLen = new Int_t[fBSeekSize];
132
133 fBuffer = 0;
134 fPrefetch = 0;
136
137 //initialise the prefetch object and set the cache directory
138 // start the thread only if the file is not local
139 fEnablePrefetching = gEnv->GetValue("TFile.AsyncPrefetching", 0);
140
141 if (fEnablePrefetching && file && strcmp(file->GetEndpointUrl()->GetProtocol(), "file")){
143 }
144 else { //disable the async pref for local files
146 }
147
152
153 if (file) file->SetCacheRead(this, tree);
154}
155
156////////////////////////////////////////////////////////////////////////////////
157/// Destructor.
158
160{
162 delete [] fSeek;
163 delete [] fSeekIndex;
164 delete [] fSeekSort;
165 delete [] fPos;
166 delete [] fSeekLen;
167 delete [] fSeekSortLen;
168 delete [] fSeekPos;
169 delete [] fLen;
170 if (fBuffer)
171 delete [] fBuffer;
172 delete [] fBSeek;
173 delete [] fBSeekIndex;
174 delete [] fBSeekSort;
175 delete [] fBPos;
176 delete [] fBSeekLen;
177 delete [] fBSeekSortLen;
178 delete [] fBSeekPos;
179 delete [] fBLen;
180}
181
182////////////////////////////////////////////////////////////////////////////////
183/// Close out any threads or asynchronous fetches used by the underlying
184/// implementation.
185/// This is called by TFile::Close to prevent usage of the file handles
186/// after the closing of the file.
187
188void TFileCacheRead::Close(Option_t * /* opt = "" */)
189{
190 if (fPrefetch) {
191 delete fPrefetch;
192 fPrefetch = 0;
193 }
194
195}
196
197////////////////////////////////////////////////////////////////////////////////
198/// Add block of length len at position pos in the list of blocks to
199/// be prefetched. If pos <= 0 the current blocks (if any) are reset.
200
202{
205 if (pos <= 0) {
206 fNseek = 0;
207 fNtot = 0;
208 return;
209 }
210 if (fNseek >= fSeekSize) {
211 //reallocate buffers
212 fSeekSize *= 2;
213 Long64_t *aSeek = new Long64_t[fSeekSize];
214 Int_t *aSeekIndex = new Int_t[fSeekSize];
215 Long64_t *aSeekSort = new Long64_t[fSeekSize];
216 Long64_t *aPos = new Long64_t[fSeekSize];
217 Int_t *aSeekLen = new Int_t[fSeekSize];
218 Int_t *aSeekSortLen = new Int_t[fSeekSize];
219 Int_t *aSeekPos = new Int_t[fSeekSize];
220 Int_t *aLen = new Int_t[fSeekSize];
221 for (Int_t i=0;i<fNseek;i++) {
222 aSeek[i] = fSeek[i];
223 aSeekIndex[i] = fSeekIndex[i];
224 aSeekSort[i] = fSeekSort[i];
225 aPos[i] = fPos[i];
226 aSeekLen[i] = fSeekLen[i];
227 aSeekSortLen[i] = fSeekSortLen[i];
228 aSeekPos[i] = fSeekPos[i];
229 aLen[i] = fLen[i];
230 }
231 delete [] fSeek;
232 delete [] fSeekIndex;
233 delete [] fSeekSort;
234 delete [] fPos;
235 delete [] fSeekLen;
236 delete [] fSeekSortLen;
237 delete [] fSeekPos;
238 delete [] fLen;
239 fSeek = aSeek;
240 fSeekIndex = aSeekIndex;
241 fSeekSort = aSeekSort;
242 fPos = aPos;
243 fSeekLen = aSeekLen;
244 fSeekSortLen = aSeekSortLen;
245 fSeekPos = aSeekPos;
246 fLen = aLen;
247 }
248
249 fSeek[fNseek] = pos;
251 fNseek++;
252 fNtot += len;
253}
254
255
256////////////////////////////////////////////////////////////////////////////////
257
259 //add a new element and increase the size if necessary
261 if (pos <= 0) {
262 fBNseek = 0;
263 fBNtot = 0;
264 return;
265 }
266 if (fBNseek >= fBSeekSize) {
267 //reallocate buffers
268 fBSeekSize *= 2;
269 Long64_t *aSeek = new Long64_t[fBSeekSize];
270 Int_t *aSeekIndex = new Int_t[fBSeekSize];
271 Long64_t *aSeekSort = new Long64_t[fBSeekSize];
272 Long64_t *aPos = new Long64_t[fBSeekSize];
273 Int_t *aSeekLen = new Int_t[fBSeekSize];
274 Int_t *aSeekSortLen = new Int_t[fBSeekSize];
275 Int_t *aSeekPos = new Int_t[fBSeekSize];
276 Int_t *aLen = new Int_t[fBSeekSize];
277 for (Int_t i=0;i<fBNseek;i++) {
278 aSeek[i] = fBSeek[i];
279 aSeekIndex[i] = fBSeekIndex[i];
280 aSeekSort[i] = fBSeekSort[i];
281 aPos[i] = fBPos[i];
282 aSeekLen[i] = fBSeekLen[i];
283 aSeekSortLen[i] = fBSeekSortLen[i];
284 aSeekPos[i] = fBSeekPos[i];
285 aLen[i] = fBLen[i];
286 }
287 delete [] fBSeek;
288 delete [] fBSeekIndex;
289 delete [] fBSeekSort;
290 delete [] fBPos;
291 delete [] fBSeekLen;
292 delete [] fBSeekSortLen;
293 delete [] fBSeekPos;
294 delete [] fBLen;
295 fBSeek = aSeek;
296 fBSeekIndex = aSeekIndex;
297 fBSeekSort = aSeekSort;
298 fBPos = aPos;
299 fBSeekLen = aSeekLen;
300 fBSeekSortLen = aSeekSortLen;
301 fBSeekPos = aSeekPos;
302 fBLen = aLen;
303 }
304
305 fBSeek[fBNseek] = pos;
307 fBNseek++;
308 fBNtot += len;
309}
310
311
312////////////////////////////////////////////////////////////////////////////////
313/// Print cache statistics.
314///
315/// The format is:
316/// ******TreeCache statistics for file: cms2.root ******
317/// Reading............................: 72761843 bytes in 7 transactions
318/// Readahead..........................: 256000 bytes with overhead = 0 bytes
319/// Average transaction................: 10394.549000 Kbytes
320/// Number of blocks in current cache..: 210, total size: 6280352
321///
322/// If option = "a" the list of blocks in the cache is printed
323/// NB: this function is automatically called by TTreeCache::Print
324
326{
327 TString opt = option;
328 opt.ToLower();
329 printf("Cached Reading.....................: %lld bytes in %d transactions\n",this->GetBytesRead(), this->GetReadCalls());
330 printf("Reading............................: %lld bytes in %d uncached transactions\n",this->GetNoCacheBytesRead(), this->GetNoCacheReadCalls());
331 printf("Readahead..........................: %d bytes with overhead = %lld bytes\n",TFile::GetReadaheadSize(),this->GetBytesReadExtra());
332 if (this->GetReadCalls() > 0)
333 printf("Average transaction................: %f Kbytes\n",0.001*Double_t(this->GetBytesRead())/Double_t(this->GetReadCalls()));
334 else
335 printf("Average transaction................: No read calls yet\n");
336 printf("Number of blocks in current cache..: %d, total size: %d\n",fNseek,fNtot);
337 if (fPrefetch){
338 printf("Prefetching .......................: %lli blocks\n", fPrefetchedBlocks);
339 printf("Prefetching Wait Time..............: %f seconds\n", fPrefetch->GetWaitTime() / 1e+6);
340 }
341
342 if (!opt.Contains("a")) return;
343 for (Int_t i=0;i<fNseek;i++) {
344 if (fIsSorted && !opt.Contains("s")) {
345 printf("block: %5d, from: %lld to %lld, len = %d bytes\n",i,fSeekSort[i],fSeekSort[i]+fSeekSortLen[i],fSeekSortLen[i]);
346 } else {
347 printf("block: %5d, from: %lld to %lld, len = %d bytes\n",i,fSeek[i],fSeek[i]+fSeekLen[i],fSeekLen[i]);
348 }
349 }
350 printf ("Number of long buffers = %d\n",fNb);
351 for (Int_t j=0;j<fNb;j++) {
352 printf("fPos[%d] = %lld, fLen = %d\n",j,fPos[j],fLen[j]);
353 }
354}
355
356////////////////////////////////////////////////////////////////////////////////
357/// Read buffer at position pos.
358///
359/// If pos is in the list of prefetched blocks read from fBuffer,
360/// otherwise need to make a normal read from file. Returns -1 in case of
361/// read error, 0 in case not in cache, 1 in case read from cache.
362
364{
365 Long64_t fileBytesRead0 = fFile->GetBytesRead();
366 Long64_t fileBytesReadExtra0 = fFile->GetBytesReadExtra();
367 Int_t fileReadCalls0 = fFile->GetReadCalls();
368
369 Int_t loc = -1;
370 Int_t rc = ReadBufferExt(buf, pos, len, loc);
371
372 fBytesRead += fFile->GetBytesRead() - fileBytesRead0;
373 fBytesReadExtra += fFile->GetBytesReadExtra() - fileBytesReadExtra0;
374 fReadCalls += fFile->GetReadCalls() - fileReadCalls0;
375
376 return rc;
377}
378
379////////////////////////////////////////////////////////////////////////////////
380
382{
384 return ReadBufferExtPrefetch(buf, pos, len, loc);
385 else
386 return ReadBufferExtNormal(buf, pos, len, loc);
387}
388
389
390////////////////////////////////////////////////////////////////////////////////
391///prefetch the first block
392
394{
395 if (fNseek > 0 && !fIsSorted) {
396 Sort();
397 loc = -1;
401 }
402
403 //try to prefetch the second block
404 if (fBNseek > 0 && !fBIsSorted) {
405 SecondSort();
406 loc = -1;
409 }
410
411 // in case we are writing and reading to/from this file, we must check
412 // if this buffer is in the write cache (not yet written to the file)
413 if (TFileCacheWrite *cachew = fFile->GetCacheWrite()) {
414 if (cachew->ReadBuffer(buf,pos,len) == 0) {
415 fFile->SetOffset(pos+len);
416 return 1;
417 }
418 }
419
420 // try to prefetch from the first block
421 if (loc < 0) {
423 }
424
425 if (loc >= 0 && loc < fNseek && pos == fSeekSort[loc]) {
426 if (buf && fPrefetch){
427 // prefetch with the new method
428 fPrefetch->ReadBuffer(buf, pos, len);
429 return 1;
430 }
431 }
432 else if (buf && fPrefetch){
433 // try to preferch from the second block
435
436 if (loc >= 0 && loc < fBNseek && pos == fBSeekSort[loc]){
437 if (fPrefetch->ReadBuffer(buf, pos, len)) {
438 return 1;
439 }
440 }
441 }
442
443 return 0;
444}
445
446
447////////////////////////////////////////////////////////////////////////////////
448/// Base function for ReadBuffer.
449///
450/// Also gives out the position of the block in the internal buffer.
451/// This helps TTreeCacheUnzip to avoid doing twice the binary search.
452
454{
455 if (fNseek > 0 && !fIsSorted) {
456 Sort();
457 loc = -1;
458
459 // If ReadBufferAsync is not supported by this implementation...
460 if (!fAsyncReading) {
461 // Then we use the vectored read to read everything now
463 // The vector read failed. fBuffer does not hold all the blocks: forget we even asked for them, so that
464 // they can be read individually from the file instead.
466 return -1;
467 }
469 } else {
470 // In any case, we'll start to request the chunks.
471 // This implementation simply reads all the chunks in advance
472 // in the async way.
473
474 // Use the async readv instead of single reads
475 fFile->ReadBuffers(0, 0, 0, 0); //Clear the XrdClient cache
476 if (fFile->ReadBuffers(0,fPos,fLen,fNb)) {
477 return -1;
478 }
480 }
481 }
482
483 // in case we are writing and reading to/from this file, we much check
484 // if this buffer is in the write cache (not yet written to the file)
485 if (TFileCacheWrite *cachew = fFile->GetCacheWrite()) {
486 if (cachew->ReadBuffer(buf,pos,len) == 0) {
487 fFile->SetOffset(pos+len);
488 return 1;
489 }
490 }
491
492 // If asynchronous reading is supported by this implementation...
493 if (fAsyncReading) {
494
495 // Now we dont have to look for it in the local buffer
496 // if it's async, we expect that the communication library
497 // will handle it more efficiently than we can do here
498
499 Int_t retval;
500 if (loc < 0)
502
503 // We use the internal list just to notify if the list is to be reconstructed
504 if (loc >= 0 && loc < fNseek && pos == fSeekSort[loc]) {
505 // Block found, the caller will get it
506
507 if (buf) {
508 // disable cache to avoid infinite recursion
509 if (fFile->ReadBuffer(buf, pos, len)) {
510 return -1;
511 }
512 fFile->SetOffset(pos+len);
513 }
514
515 retval = 1;
516 } else {
517 // Block not found in the list, we report it as a miss
518 retval = 0;
519 }
520
521 if (gDebug > 0)
522 Info("ReadBuffer","pos=%lld, len=%d, retval=%d, loc=%d, "
523 "fseekSort[loc]=%lld, fSeekLen[loc]=%d",
524 pos, len, retval, loc, fSeekSort[loc], fSeekLen[loc]);
525
526 return retval;
527 } else {
528
529 if (loc < 0)
531
532 if (loc >= 0 && loc <fNseek && pos == fSeekSort[loc]) {
533 if (buf) {
534 memcpy(buf,&fBuffer[fSeekPos[loc]],len);
535 fFile->SetOffset(pos+len);
536 }
537 return 1;
538 }
539 }
540
541 return 0;
542}
543
544////////////////////////////////////////////////////////////////////////////////
545/// Set the file using this cache and reset the current blocks (if any).
546
548{
549 fFile = file;
550
551 if (fAsyncReading) {
552 // If asynchronous reading is not supported by this TFile specialization
553 // we use sync primitives, hence we need the local buffer
554 if (file && file->ReadBufferAsync(0, 0)) {
556 fBuffer = new char[fBufferSize];
557 }
558 }
559
560 if (action == TFile::kDisconnect)
561 Prefetch(0,0);
562
563 if (fPrefetch) {
564 if (action == TFile::kDisconnect)
565 SecondPrefetch(0, 0);
566 fPrefetch->SetFile(file, action);
567 }
568}
569
570////////////////////////////////////////////////////////////////////////////////
571/// Sort buffers to be prefetched in increasing order of positions.
572/// Merge consecutive blocks if necessary.
573
575{
576 if (!fNseek) return;
578 Int_t i;
579 Int_t nb = 0;
580 Int_t effectiveNseek = 0;
581 for (i=0;i<fNseek;i++) {
582 // Skip duplicates
583 Int_t ind = fSeekIndex[i];
584 if (effectiveNseek!=0 && fSeek[ind]==fSeekSort[effectiveNseek-1])
585 {
586 if (fSeekSortLen[effectiveNseek-1] < fSeekLen[ind]) {
587 fSeekSortLen[effectiveNseek-1] = fSeekLen[ind];
588 }
589 continue;
590 }
591 fSeekSort[effectiveNseek] = fSeek[ind];
592 fSeekSortLen[effectiveNseek] = fSeekLen[ind];
593 ++effectiveNseek;
594 }
595 fNseek = effectiveNseek;
596 if (fNtot > fBufferSizeMin) {
597 fBufferSize = fNtot + 100;
598 delete [] fBuffer;
599 fBuffer = 0;
600 // If ReadBufferAsync is not supported by this implementation
601 // it means that we are using sync primitives, hence we need the local buffer
602 if (!fAsyncReading)
603 fBuffer = new char[fBufferSize];
604 }
605 fPos[0] = fSeekSort[0];
606 fLen[0] = fSeekSortLen[0];
607 fSeekPos[0] = 0;
608 for (i=1;i<fNseek;i++) {
609 fSeekPos[i] = fSeekPos[i-1] + fSeekSortLen[i-1];
610 //in the test below 16 MBytes is pure empirirical and may depend on the file system.
611 //increasing this number must be done with care, as it may increase
612 //the job real time (mismatch with OS buffers)
613 if ((fSeekSort[i] != fSeekSort[i-1]+fSeekSortLen[i-1]) ||
614 (fLen[nb] > 16000000)) {
615 nb++;
616 fPos[nb] = fSeekSort[i];
617 fLen[nb] = fSeekSortLen[i];
618 } else {
619 fLen[nb] += fSeekSortLen[i];
620 }
621 }
622 fNb = nb+1;
624}
625
626
627////////////////////////////////////////////////////////////////////////////////
628/// Sort buffers to be prefetched in increasing order of positions.
629///
630/// Merge consecutive blocks if necessary.
631
633{
634 if (!fBNseek) return;
636 Int_t i;
637 Int_t nb = 0;
638 Int_t effectiveNseek = 0;
639 for (i=0;i<fBNseek;i++) {
640 // Skip duplicates
641 Int_t ind = fBSeekIndex[i];
642 if (effectiveNseek!=0 && fBSeek[ind]==fBSeekSort[effectiveNseek-1])
643 {
644 if (fBSeekSortLen[effectiveNseek-1] < fBSeekLen[ind]) {
645 fBSeekSortLen[effectiveNseek-1] = fBSeekLen[ind];
646 }
647 continue;
648 }
649 fBSeekSort[effectiveNseek] = fBSeek[ind];
650 fBSeekSortLen[effectiveNseek] = fBSeekLen[ind];
651 ++effectiveNseek;
652 }
653 fBNseek = effectiveNseek;
654 if (fBNtot > fBufferSizeMin) {
655 fBufferSize = fBNtot + 100;
656 delete [] fBuffer;
657 fBuffer = 0;
658 // If ReadBufferAsync is not supported by this implementation
659 // it means that we are using sync primitives, hence we need the local buffer
660 if (!fAsyncReading)
661 fBuffer = new char[fBufferSize];
662 }
663 fBPos[0] = fBSeekSort[0];
664 fBLen[0] = fBSeekSortLen[0];
665 fBSeekPos[0] = 0;
666 for (i=1;i<fBNseek;i++) {
667 fBSeekPos[i] = fBSeekPos[i-1] + fBSeekSortLen[i-1];
668 //in the test below 16 MBytes is pure empirirical and may depend on the file system.
669 //increasing this number must be done with care, as it may increase
670 //the job real time (mismatch with OS buffers)
671 if ((fBSeekSort[i] != fBSeekSort[i-1]+fBSeekSortLen[i-1]) ||
672 (fBLen[nb] > 16000000)) {
673 nb++;
674 fBPos[nb] = fBSeekSort[i];
675 fBLen[nb] = fBSeekSortLen[i];
676 } else {
677 fBLen[nb] += fBSeekSortLen[i];
678 }
679 }
680 fBNb = nb+1;
682}
683
684////////////////////////////////////////////////////////////////////////////////
685
689
690
691////////////////////////////////////////////////////////////////////////////////
692
699
700
701////////////////////////////////////////////////////////////////////////////////
702/// Sets the buffer size.
703///
704/// If the current prefetch list is too large to fit in
705/// the new buffer some or all of the prefetch blocks are dropped. The
706/// requested buffersize must be greater than zero.
707/// It will be automatically clamped to minimum 100000 (if <= 10000) and maximum INT_MAX
708/// Return values:
709/// - 0 if the prefetch block lists remain unchanged
710/// - 1 if some or all blocks have been removed from the prefetch list
711/// - -1 on error
712
714{
715 if (bufsize <= 0) return -1;
716 if (bufsize <=10000) bufsize = 100000;
717 if (bufsize > std::numeric_limits<Int_t>::max()) bufsize = std::numeric_limits<Int_t>::max();
718
719 if (bufsize == fBufferSize) {
720 fBufferSizeMin = bufsize;
721 return 0;
722 }
723
724 Bool_t inval = kFALSE;
725
726 // the cached data is too large to fit in the new buffer size mark data unavailable
727 if (fNtot > bufsize) {
728 Prefetch(0, 0);
729 inval = kTRUE;
730 }
731 if (fBNtot > bufsize) {
732 SecondPrefetch(0, 0);
733 inval = kTRUE;
734 }
735
736 char *np = 0;
738 char *pres = 0;
739 if (fIsTransferred) {
740 // will need to preserve buffer data
741 pres = fBuffer;
742 fBuffer = 0;
743 }
744 delete [] fBuffer;
745 fBuffer = 0;
746 np = new char[bufsize];
747 if (pres) {
748 memcpy(np, pres, fNtot);
749 }
750 delete [] pres;
751 }
752
753 delete [] fBuffer;
754 fBuffer = np;
755 fBufferSizeMin = bufsize;
756 fBufferSize = bufsize;
757
758 if (inval) {
759 return 1;
760 }
761
762 return 0;
763}
764
765
766////////////////////////////////////////////////////////////////////////////////
767/// Set the prefetching mode of this file.
768///
769/// If 'setPrefetching', enable the asynchronous prefetching
770/// (using TFilePrefetch) and if the gEnv and rootrc
771/// variable Cache.Directory is set, also enable the local
772/// caching of the prefetched blocks.
773/// if 'setPrefetching', the old prefetcher is enabled is
774/// the gEnv and rootrc variable is TFile.AsyncReading
775
777{
778 SetEnablePrefetchingImpl(setPrefetching);
779}
780
781////////////////////////////////////////////////////////////////////////////////
782/// TFileCacheRead implementation of SetEnablePrefetching.
783///
784/// This function is called from the constructor and should not be virtual.
785
787{
788 fEnablePrefetching = setPrefetching;
789
792 const char* cacheDir = gEnv->GetValue("Cache.Directory", "");
793 if (strcmp(cacheDir, ""))
794 if (!fPrefetch->SetCache((char*) cacheDir))
795 fprintf(stderr, "Error while trying to set the cache directory: %s.\n", cacheDir);
796 if (fPrefetch->ThreadStart()){
797 fprintf(stderr,"Error stating prefetching thread. Disabling prefetching.\n");
799 }
800 } else if (fPrefetch && !fEnablePrefetching) {
802 fPrefetch = nullptr;
803 }
804
805 //environment variable used to switch to the new method of reading asynchronously
806 if (fEnablePrefetching) {
808 }
809 else {
810 fAsyncReading = gEnv->GetValue("TFile.AsyncReading", 0);
811 if (fAsyncReading) {
812 // Check if asynchronous reading is supported by this TFile specialization
814 if (fFile && !(fFile->ReadBufferAsync(0, 0)))
816 }
817 if (!fAsyncReading && fBuffer == 0) {
818 // we use sync primitives, hence we need the local buffer
819 fBuffer = new char[fBufferSize];
820 }
821 }
822}
823
#define SafeDelete(p)
Definition RConfig.hxx:507
#define e(i)
Definition RSha256.hxx:103
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
constexpr Bool_t kFALSE
Definition RtypesCore.h:109
double Double_t
Double 8 bytes.
Definition RtypesCore.h:74
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:84
constexpr Bool_t kTRUE
Definition RtypesCore.h:108
const char Option_t
Option string (const char)
Definition RtypesCore.h:81
R__EXTERN TEnv * gEnv
Definition TEnv.h:126
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t np
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Int_t gDebug
Global variable setting the debug level. Set to 0 to disable, increase it in steps of 1 to increase t...
Definition TROOT.cxx:792
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition TEnv.cxx:511
virtual Long64_t GetBytesRead() const
Long64_t fBytesReadExtra
Number of extra bytes (overhead) read by the readahead buffer.
virtual Long64_t GetBytesReadExtra() const
virtual Long64_t GetNoCacheBytesRead() const
Int_t fBufferSize
Allocated size of fBuffer (at a given time)
char * fBuffer
[fBufferSize] buffer of contiguous prefetched blocks
Long64_t * fBSeekSort
[fBNseek]
Long64_t * fPos
[fNb] start of long buffers
Bool_t fIsSorted
True if fSeek array is sorted.
virtual Int_t GetReadCalls() const
Int_t * fSeekIndex
[fNseek] sorted index table of fSeek
virtual Int_t ReadBufferExt(char *buf, Long64_t pos, Int_t len, Int_t &loc)
~TFileCacheRead() override
Destructor.
Long64_t * fSeekSort
[fNseek] Position on file of buffers to be prefetched (sorted)
virtual Int_t ReadBuffer(char *buf, Long64_t pos, Int_t len)
Read buffer at position pos.
TFilePrefetch * fPrefetch
! Object that does the asynchronous reading in another thread
Int_t * fBSeekPos
[fBNseek]
virtual void SecondPrefetch(Long64_t, Int_t)
Long64_t fBytesRead
Number of bytes read for this cache.
virtual void SecondSort()
Sort buffers to be prefetched in increasing order of positions.
virtual TFilePrefetch * GetPrefetchObj()
virtual Int_t ReadBufferExtPrefetch(char *buf, Long64_t pos, Int_t len, Int_t &loc)
prefetch the first block
virtual void Sort()
Sort buffers to be prefetched in increasing order of positions.
virtual Int_t ReadBufferExtNormal(char *buf, Long64_t pos, Int_t len, Int_t &loc)
Base function for ReadBuffer.
TFileCacheRead()
Default Constructor.
void SetEnablePrefetchingImpl(Bool_t setPrefetching=kFALSE)
TFileCacheRead implementation of SetEnablePrefetching.
Bool_t fEnablePrefetching
reading by prefetching asynchronously
Int_t * fSeekLen
[fNseek] Length of buffers to be prefetched
Int_t fNtot
Total size of prefetched blocks.
virtual Int_t SetBufferSize(Long64_t buffersize)
Sets the buffer size.
virtual void Prefetch(Long64_t pos, Int_t len)
Add block of length len at position pos in the list of blocks to be prefetched.
Int_t * fSeekSortLen
[fNseek] Length of buffers to be prefetched (sorted)
virtual void SetEnablePrefetching(Bool_t setPrefetching=kFALSE)
Set the prefetching mode of this file.
Int_t fBufferSizeMin
Original size of fBuffer.
Int_t * fBSeekIndex
[fBNseek]
virtual void Close(Option_t *option="")
Close out any threads or asynchronous fetches used by the underlying implementation.
Int_t fNb
Number of long buffers.
Int_t fReadCalls
Number of read calls for this cache.
Int_t fBufferLen
Current buffer length (<= fBufferSize)
Long64_t fPrefetchedBlocks
Number of blocks prefetched.
Long64_t fNoCacheBytesRead
Number of bytes read by basket to fill cached tree.
Long64_t * fSeek
[fNseek] Position on file of buffers to be prefetched
virtual Int_t GetNoCacheReadCalls() const
Bool_t fIsTransferred
True when fBuffer contains something valid.
TFile * fFile
Pointer to file.
virtual void WaitFinishPrefetch()
Int_t * fBLen
[fBNb]
Int_t * fBSeekLen
[fBNseek]
Int_t fNseek
Number of blocks to be prefetched.
Int_t * fLen
[fNb] Length of long buffers
Int_t * fBSeekSortLen
[fBNseek]
void Print(Option_t *option="") const override
Print cache statistics.
Int_t fNoCacheReadCalls
Number of read calls by basket to fill cached tree.
Int_t * fSeekPos
[fNseek] Position of sorted blocks in fBuffer
Long64_t * fBPos
[fBNb]
Int_t fSeekSize
Allocated size of fSeek.
Long64_t * fBSeek
[fBNseek]
virtual void SetFile(TFile *file, TFile::ECacheAction action=TFile::kDisconnect)
Set the file using this cache and reset the current blocks (if any).
A cache when writing files over the network.
The prefetching mechanism uses two classes (TFilePrefetch and TFPBlock) to prefetch in advance a bloc...
void ReadBlock(Long64_t *, Int_t *, Int_t)
Create a TFPBlock object or recycle one and add it to the prefetchBlocks list.
Long64_t GetWaitTime()
Return the time spent wating for buffer to be read in microseconds.
Bool_t SetCache(const char *)
Set the path of the cache directory.
Int_t ThreadStart()
Used to start the consumer thread.
Bool_t ReadBuffer(char *, Long64_t, Int_t)
Return a prefetched element.
void WaitFinishPrefetch()
Killing the async prefetching thread.
void SetFile(TFile *file, TFile::ECacheAction action=TFile::kDisconnect)
Change the file.
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
virtual void SetCacheRead(TFileCacheRead *cache, TObject *tree=nullptr, ECacheAction action=kDisconnect)
Set a pointer to the read cache.
Definition TFile.cxx:2432
TFileCacheWrite * GetCacheWrite() const
Return a pointer to the current write cache.
Definition TFile.cxx:1304
static Int_t GetReadaheadSize()
Static function returning the readahead buffer size.
Definition TFile.cxx:4304
virtual Bool_t ReadBuffers(char *buf, Long64_t *pos, Int_t *len, Int_t nbuf)
Read the nbuf blocks described in arrays pos and len.
Definition TFile.cxx:1887
virtual Int_t GetReadCalls() const
Definition TFile.h:334
virtual Long64_t GetBytesRead() const
Definition TFile.h:331
virtual const TUrl * GetEndpointUrl() const
Definition TFile.h:325
virtual Long64_t GetBytesReadExtra() const
Definition TFile.h:332
ECacheAction
TTreeCache flushing semantics.
Definition TFile.h:148
@ kDisconnect
Definition TFile.h:148
virtual Bool_t ReadBufferAsync(Long64_t offs, Int_t len)
Definition TFile.cxx:4895
virtual void SetOffset(Long64_t offset, ERelativeTo pos=kBeg)
Set position from where to start reading.
Definition TFile.cxx:2320
virtual Bool_t ReadBuffer(char *buf, Int_t len)
Read a buffer from the file.
Definition TFile.cxx:1837
Mother of all ROOT objects.
Definition TObject.h:42
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:1070
Basic string class.
Definition TString.h:137
void ToLower()
Change string to lower-case.
Definition TString.cxx:1190
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:642
const char * GetProtocol() const
Definition TUrl.h:64
void Sort(Index n, const Element *a, Index *index, Bool_t down=kTRUE)
Sort the n elements of the array a of generic templated type Element.
Definition TMathBase.h:413
Long64_t BinarySearch(Long64_t n, const T *array, T value)
Binary search in an array of n values to locate value.
Definition TMathBase.h:329