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
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 TNetFile,
23 TNetXNGFile and TWebFile (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
37
38////////////////////////////////////////////////////////////////////////////////
39/// Default Constructor.
40
42{
44 fBufferSize = 0;
45 fBufferLen = 0;
46 fBytesRead = 0;
49 fReadCalls = 0;
51 fNseek = 0;
52 fNtot = 0;
53 fNb = 0;
54 fSeekSize = 0;
55 fSeek = 0;
56 fSeekIndex = 0;
57 fSeekSort = 0;
58 fPos = 0;
59 fSeekLen = 0;
60 fSeekSortLen = 0;
61 fSeekPos = 0;
62 fLen = 0;
63 fFile = 0;
64 fBuffer = 0;
67
68 //values for the second prefetched block
69 fBNseek = 0;
70 fBNtot = 0;
71 fBNb = 0;
72 fBSeekSize = 0;
73 fBSeek = 0;
74 fBSeekSort = 0;
75 fBSeekIndex = 0;
76 fBPos = 0;
77 fBSeekLen = 0;
78 fBSeekSortLen = 0;
79 fBSeekPos = 0;
80 fBLen = 0;
83
86 fPrefetch = 0;
88}
89
90////////////////////////////////////////////////////////////////////////////////
91/// Creates a TFileCacheRead data structure.
92
94 : TObject()
95{
96 if (buffersize <=10000) fBufferSize = 100000;
97 else fBufferSize = buffersize;
98
100 fBufferLen = 0;
101 fBytesRead = 0;
103 fBytesReadExtra = 0;
104 fReadCalls = 0;
106 fNseek = 0;
107 fNtot = 0;
108 fNb = 0;
109 fSeekSize = 10000;
110 fSeek = new Long64_t[fSeekSize];
113 fPos = new Long64_t[fSeekSize];
114 fSeekLen = new Int_t[fSeekSize];
116 fSeekPos = new Int_t[fSeekSize];
117 fLen = new Int_t[fSeekSize];
118 fFile = file;
119
120 //initialisation for the second block
121 fBNseek = 0;
122 fBNtot = 0;
123 fBNb = 0;
124 fBSeekSize = 10000;
132 fBLen = new Int_t[fBSeekSize];
133
134 fBuffer = 0;
135 fPrefetch = 0;
137
138 //initialise the prefetch object and set the cache directory
139 // start the thread only if the file is not local
140 fEnablePrefetching = gEnv->GetValue("TFile.AsyncPrefetching", 0);
141
142 if (fEnablePrefetching && file && strcmp(file->GetEndpointUrl()->GetProtocol(), "file")){
144 }
145 else { //disable the async pref for local files
147 }
148
153
154 if (file) file->SetCacheRead(this, tree);
155}
156
157////////////////////////////////////////////////////////////////////////////////
158/// Destructor.
159
161{
163 delete [] fSeek;
164 delete [] fSeekIndex;
165 delete [] fSeekSort;
166 delete [] fPos;
167 delete [] fSeekLen;
168 delete [] fSeekSortLen;
169 delete [] fSeekPos;
170 delete [] fLen;
171 if (fBuffer)
172 delete [] fBuffer;
173 delete [] fBSeek;
174 delete [] fBSeekIndex;
175 delete [] fBSeekSort;
176 delete [] fBPos;
177 delete [] fBSeekLen;
178 delete [] fBSeekSortLen;
179 delete [] fBSeekPos;
180 delete [] fBLen;
181}
182
183////////////////////////////////////////////////////////////////////////////////
184/// Close out any threads or asynchronous fetches used by the underlying
185/// implementation.
186/// This is called by TFile::Close to prevent usage of the file handles
187/// after the closing of the file.
188
189void TFileCacheRead::Close(Option_t * /* opt = "" */)
190{
191 if (fPrefetch) {
192 delete fPrefetch;
193 fPrefetch = 0;
194 }
195
196}
197
198////////////////////////////////////////////////////////////////////////////////
199/// Add block of length len at position pos in the list of blocks to
200/// be prefetched. If pos <= 0 the current blocks (if any) are reset.
201
203{
206 if (pos <= 0) {
207 fNseek = 0;
208 fNtot = 0;
209 return;
210 }
211 if (fNseek >= fSeekSize) {
212 //reallocate buffers
213 fSeekSize *= 2;
214 Long64_t *aSeek = new Long64_t[fSeekSize];
215 Int_t *aSeekIndex = new Int_t[fSeekSize];
216 Long64_t *aSeekSort = new Long64_t[fSeekSize];
217 Long64_t *aPos = new Long64_t[fSeekSize];
218 Int_t *aSeekLen = new Int_t[fSeekSize];
219 Int_t *aSeekSortLen = new Int_t[fSeekSize];
220 Int_t *aSeekPos = new Int_t[fSeekSize];
221 Int_t *aLen = new Int_t[fSeekSize];
222 for (Int_t i=0;i<fNseek;i++) {
223 aSeek[i] = fSeek[i];
224 aSeekIndex[i] = fSeekIndex[i];
225 aSeekSort[i] = fSeekSort[i];
226 aPos[i] = fPos[i];
227 aSeekLen[i] = fSeekLen[i];
228 aSeekSortLen[i] = fSeekSortLen[i];
229 aSeekPos[i] = fSeekPos[i];
230 aLen[i] = fLen[i];
231 }
232 delete [] fSeek;
233 delete [] fSeekIndex;
234 delete [] fSeekSort;
235 delete [] fPos;
236 delete [] fSeekLen;
237 delete [] fSeekSortLen;
238 delete [] fSeekPos;
239 delete [] fLen;
240 fSeek = aSeek;
241 fSeekIndex = aSeekIndex;
242 fSeekSort = aSeekSort;
243 fPos = aPos;
244 fSeekLen = aSeekLen;
245 fSeekSortLen = aSeekSortLen;
246 fSeekPos = aSeekPos;
247 fLen = aLen;
248 }
249
250 fSeek[fNseek] = pos;
252 fNseek++;
253 fNtot += len;
254}
255
256
257////////////////////////////////////////////////////////////////////////////////
258
260 //add a new element and increase the size if necessary
262 if (pos <= 0) {
263 fBNseek = 0;
264 fBNtot = 0;
265 return;
266 }
267 if (fBNseek >= fBSeekSize) {
268 //reallocate buffers
269 fBSeekSize *= 2;
270 Long64_t *aSeek = new Long64_t[fBSeekSize];
271 Int_t *aSeekIndex = new Int_t[fBSeekSize];
272 Long64_t *aSeekSort = new Long64_t[fBSeekSize];
273 Long64_t *aPos = new Long64_t[fBSeekSize];
274 Int_t *aSeekLen = new Int_t[fBSeekSize];
275 Int_t *aSeekSortLen = new Int_t[fBSeekSize];
276 Int_t *aSeekPos = new Int_t[fBSeekSize];
277 Int_t *aLen = new Int_t[fBSeekSize];
278 for (Int_t i=0;i<fBNseek;i++) {
279 aSeek[i] = fBSeek[i];
280 aSeekIndex[i] = fBSeekIndex[i];
281 aSeekSort[i] = fBSeekSort[i];
282 aPos[i] = fBPos[i];
283 aSeekLen[i] = fBSeekLen[i];
284 aSeekSortLen[i] = fBSeekSortLen[i];
285 aSeekPos[i] = fBSeekPos[i];
286 aLen[i] = fBLen[i];
287 }
288 delete [] fBSeek;
289 delete [] fBSeekIndex;
290 delete [] fBSeekSort;
291 delete [] fBPos;
292 delete [] fBSeekLen;
293 delete [] fBSeekSortLen;
294 delete [] fBSeekPos;
295 delete [] fBLen;
296 fBSeek = aSeek;
297 fBSeekIndex = aSeekIndex;
298 fBSeekSort = aSeekSort;
299 fBPos = aPos;
300 fBSeekLen = aSeekLen;
301 fBSeekSortLen = aSeekSortLen;
302 fBSeekPos = aSeekPos;
303 fBLen = aLen;
304 }
305
306 fBSeek[fBNseek] = pos;
308 fBNseek++;
309 fBNtot += len;
310}
311
312
313////////////////////////////////////////////////////////////////////////////////
314/// Print cache statistics.
315///
316/// The format is:
317/// ******TreeCache statistics for file: cms2.root ******
318/// Reading............................: 72761843 bytes in 7 transactions
319/// Readahead..........................: 256000 bytes with overhead = 0 bytes
320/// Average transaction................: 10394.549000 Kbytes
321/// Number of blocks in current cache..: 210, total size: 6280352
322///
323/// If option = "a" the list of blocks in the cache is printed
324/// NB: this function is automatically called by TTreeCache::Print
325
327{
328 TString opt = option;
329 opt.ToLower();
330 printf("Cached Reading.....................: %lld bytes in %d transactions\n",this->GetBytesRead(), this->GetReadCalls());
331 printf("Reading............................: %lld bytes in %d uncached transactions\n",this->GetNoCacheBytesRead(), this->GetNoCacheReadCalls());
332 printf("Readahead..........................: %d bytes with overhead = %lld bytes\n",TFile::GetReadaheadSize(),this->GetBytesReadExtra());
333 if (this->GetReadCalls() > 0)
334 printf("Average transaction................: %f Kbytes\n",0.001*Double_t(this->GetBytesRead())/Double_t(this->GetReadCalls()));
335 else
336 printf("Average transaction................: No read calls yet\n");
337 printf("Number of blocks in current cache..: %d, total size: %d\n",fNseek,fNtot);
338 if (fPrefetch){
339 printf("Prefetching .......................: %lli blocks\n", fPrefetchedBlocks);
340 printf("Prefetching Wait Time..............: %f seconds\n", fPrefetch->GetWaitTime() / 1e+6);
341 }
342
343 if (!opt.Contains("a")) return;
344 for (Int_t i=0;i<fNseek;i++) {
345 if (fIsSorted && !opt.Contains("s")) {
346 printf("block: %5d, from: %lld to %lld, len = %d bytes\n",i,fSeekSort[i],fSeekSort[i]+fSeekSortLen[i],fSeekSortLen[i]);
347 } else {
348 printf("block: %5d, from: %lld to %lld, len = %d bytes\n",i,fSeek[i],fSeek[i]+fSeekLen[i],fSeekLen[i]);
349 }
350 }
351 printf ("Number of long buffers = %d\n",fNb);
352 for (Int_t j=0;j<fNb;j++) {
353 printf("fPos[%d] = %lld, fLen = %d\n",j,fPos[j],fLen[j]);
354 }
355}
356
357////////////////////////////////////////////////////////////////////////////////
358/// Read buffer at position pos.
359///
360/// If pos is in the list of prefetched blocks read from fBuffer,
361/// otherwise need to make a normal read from file. Returns -1 in case of
362/// read error, 0 in case not in cache, 1 in case read from cache.
363
365{
366 Long64_t fileBytesRead0 = fFile->GetBytesRead();
367 Long64_t fileBytesReadExtra0 = fFile->GetBytesReadExtra();
368 Int_t fileReadCalls0 = fFile->GetReadCalls();
369
370 Int_t loc = -1;
371 Int_t rc = ReadBufferExt(buf, pos, len, loc);
372
373 fBytesRead += fFile->GetBytesRead() - fileBytesRead0;
374 fBytesReadExtra += fFile->GetBytesReadExtra() - fileBytesReadExtra0;
375 fReadCalls += fFile->GetReadCalls() - fileReadCalls0;
376
377 return rc;
378}
379
380////////////////////////////////////////////////////////////////////////////////
381
383{
385 return ReadBufferExtPrefetch(buf, pos, len, loc);
386 else
387 return ReadBufferExtNormal(buf, pos, len, loc);
388}
389
390
391////////////////////////////////////////////////////////////////////////////////
392///prefetch the first block
393
395{
396 if (fNseek > 0 && !fIsSorted) {
397 Sort();
398 loc = -1;
402 }
403
404 //try to prefetch the second block
405 if (fBNseek > 0 && !fBIsSorted) {
406 SecondSort();
407 loc = -1;
410 }
411
412 // in case we are writing and reading to/from this file, we must check
413 // if this buffer is in the write cache (not yet written to the file)
414 if (TFileCacheWrite *cachew = fFile->GetCacheWrite()) {
415 if (cachew->ReadBuffer(buf,pos,len) == 0) {
416 fFile->SetOffset(pos+len);
417 return 1;
418 }
419 }
420
421 // try to prefetch from the first block
422 if (loc < 0) {
424 }
425
426 if (loc >= 0 && loc < fNseek && pos == fSeekSort[loc]) {
427 if (buf && fPrefetch){
428 // prefetch with the new method
429 fPrefetch->ReadBuffer(buf, pos, len);
430 return 1;
431 }
432 }
433 else if (buf && fPrefetch){
434 // try to preferch from the second block
436
437 if (loc >= 0 && loc < fBNseek && pos == fBSeekSort[loc]){
438 if (fPrefetch->ReadBuffer(buf, pos, len)) {
439 return 1;
440 }
441 }
442 }
443
444 return 0;
445}
446
447
448////////////////////////////////////////////////////////////////////////////////
449/// Base function for ReadBuffer.
450///
451/// Also gives out the position of the block in the internal buffer.
452/// This helps TTreeCacheUnzip to avoid doing twice the binary search.
453
455{
456 if (fNseek > 0 && !fIsSorted) {
457 Sort();
458 loc = -1;
459
460 // If ReadBufferAsync is not supported by this implementation...
461 if (!fAsyncReading) {
462 // Then we use the vectored read to read everything now
464 return -1;
465 }
467 } else {
468 // In any case, we'll start to request the chunks.
469 // This implementation simply reads all the chunks in advance
470 // in the async way.
471
472 // Use the async readv instead of single reads
473 fFile->ReadBuffers(0, 0, 0, 0); //Clear the XrdClient cache
474 if (fFile->ReadBuffers(0,fPos,fLen,fNb)) {
475 return -1;
476 }
478 }
479 }
480
481 // in case we are writing and reading to/from this file, we much check
482 // if this buffer is in the write cache (not yet written to the file)
483 if (TFileCacheWrite *cachew = fFile->GetCacheWrite()) {
484 if (cachew->ReadBuffer(buf,pos,len) == 0) {
485 fFile->SetOffset(pos+len);
486 return 1;
487 }
488 }
489
490 // If asynchronous reading is supported by this implementation...
491 if (fAsyncReading) {
492
493 // Now we dont have to look for it in the local buffer
494 // if it's async, we expect that the communication library
495 // will handle it more efficiently than we can do here
496
497 Int_t retval;
498 if (loc < 0)
500
501 // We use the internal list just to notify if the list is to be reconstructed
502 if (loc >= 0 && loc < fNseek && pos == fSeekSort[loc]) {
503 // Block found, the caller will get it
504
505 if (buf) {
506 // disable cache to avoid infinite recursion
507 if (fFile->ReadBuffer(buf, pos, len)) {
508 return -1;
509 }
510 fFile->SetOffset(pos+len);
511 }
512
513 retval = 1;
514 } else {
515 // Block not found in the list, we report it as a miss
516 retval = 0;
517 }
518
519 if (gDebug > 0)
520 Info("ReadBuffer","pos=%lld, len=%d, retval=%d, loc=%d, "
521 "fseekSort[loc]=%lld, fSeekLen[loc]=%d",
522 pos, len, retval, loc, fSeekSort[loc], fSeekLen[loc]);
523
524 return retval;
525 } else {
526
527 if (loc < 0)
529
530 if (loc >= 0 && loc <fNseek && pos == fSeekSort[loc]) {
531 if (buf) {
532 memcpy(buf,&fBuffer[fSeekPos[loc]],len);
533 fFile->SetOffset(pos+len);
534 }
535 return 1;
536 }
537 }
538
539 return 0;
540}
541
542////////////////////////////////////////////////////////////////////////////////
543/// Set the file using this cache and reset the current blocks (if any).
544
546{
547 fFile = file;
548
549 if (fAsyncReading) {
550 // If asynchronous reading is not supported by this TFile specialization
551 // we use sync primitives, hence we need the local buffer
552 if (file && file->ReadBufferAsync(0, 0)) {
554 fBuffer = new char[fBufferSize];
555 }
556 }
557
558 if (action == TFile::kDisconnect)
559 Prefetch(0,0);
560
561 if (fPrefetch) {
562 if (action == TFile::kDisconnect)
563 SecondPrefetch(0, 0);
564 fPrefetch->SetFile(file, action);
565 }
566}
567
568////////////////////////////////////////////////////////////////////////////////
569/// Sort buffers to be prefetched in increasing order of positions.
570/// Merge consecutive blocks if necessary.
571
573{
574 if (!fNseek) return;
576 Int_t i;
577 Int_t nb = 0;
578 Int_t effectiveNseek = 0;
579 for (i=0;i<fNseek;i++) {
580 // Skip duplicates
581 Int_t ind = fSeekIndex[i];
582 if (effectiveNseek!=0 && fSeek[ind]==fSeekSort[effectiveNseek-1])
583 {
584 if (fSeekSortLen[effectiveNseek-1] < fSeekLen[ind]) {
585 fSeekSortLen[effectiveNseek-1] = fSeekLen[ind];
586 }
587 continue;
588 }
589 fSeekSort[effectiveNseek] = fSeek[ind];
590 fSeekSortLen[effectiveNseek] = fSeekLen[ind];
591 ++effectiveNseek;
592 }
593 fNseek = effectiveNseek;
594 if (fNtot > fBufferSizeMin) {
595 fBufferSize = fNtot + 100;
596 delete [] fBuffer;
597 fBuffer = 0;
598 // If ReadBufferAsync is not supported by this implementation
599 // it means that we are using sync primitives, hence we need the local buffer
600 if (!fAsyncReading)
601 fBuffer = new char[fBufferSize];
602 }
603 fPos[0] = fSeekSort[0];
604 fLen[0] = fSeekSortLen[0];
605 fSeekPos[0] = 0;
606 for (i=1;i<fNseek;i++) {
607 fSeekPos[i] = fSeekPos[i-1] + fSeekSortLen[i-1];
608 //in the test below 16 MBytes is pure empirirical and may depend on the file system.
609 //increasing this number must be done with care, as it may increase
610 //the job real time (mismatch with OS buffers)
611 if ((fSeekSort[i] != fSeekSort[i-1]+fSeekSortLen[i-1]) ||
612 (fLen[nb] > 16000000)) {
613 nb++;
614 fPos[nb] = fSeekSort[i];
615 fLen[nb] = fSeekSortLen[i];
616 } else {
617 fLen[nb] += fSeekSortLen[i];
618 }
619 }
620 fNb = nb+1;
622}
623
624
625////////////////////////////////////////////////////////////////////////////////
626/// Sort buffers to be prefetched in increasing order of positions.
627///
628/// Merge consecutive blocks if necessary.
629
631{
632 if (!fBNseek) return;
634 Int_t i;
635 Int_t nb = 0;
636 Int_t effectiveNseek = 0;
637 for (i=0;i<fBNseek;i++) {
638 // Skip duplicates
639 Int_t ind = fBSeekIndex[i];
640 if (effectiveNseek!=0 && fBSeek[ind]==fBSeekSort[effectiveNseek-1])
641 {
642 if (fBSeekSortLen[effectiveNseek-1] < fBSeekLen[ind]) {
643 fBSeekSortLen[effectiveNseek-1] = fBSeekLen[ind];
644 }
645 continue;
646 }
647 fBSeekSort[effectiveNseek] = fBSeek[ind];
648 fBSeekSortLen[effectiveNseek] = fBSeekLen[ind];
649 ++effectiveNseek;
650 }
651 fBNseek = effectiveNseek;
652 if (fBNtot > fBufferSizeMin) {
653 fBufferSize = fBNtot + 100;
654 delete [] fBuffer;
655 fBuffer = 0;
656 // If ReadBufferAsync is not supported by this implementation
657 // it means that we are using sync primitives, hence we need the local buffer
658 if (!fAsyncReading)
659 fBuffer = new char[fBufferSize];
660 }
661 fBPos[0] = fBSeekSort[0];
662 fBLen[0] = fBSeekSortLen[0];
663 fBSeekPos[0] = 0;
664 for (i=1;i<fBNseek;i++) {
665 fBSeekPos[i] = fBSeekPos[i-1] + fBSeekSortLen[i-1];
666 //in the test below 16 MBytes is pure empirirical and may depend on the file system.
667 //increasing this number must be done with care, as it may increase
668 //the job real time (mismatch with OS buffers)
669 if ((fBSeekSort[i] != fBSeekSort[i-1]+fBSeekSortLen[i-1]) ||
670 (fBLen[nb] > 16000000)) {
671 nb++;
672 fBPos[nb] = fBSeekSort[i];
673 fBLen[nb] = fBSeekSortLen[i];
674 } else {
675 fBLen[nb] += fBSeekSortLen[i];
676 }
677 }
678 fBNb = nb+1;
680}
681
682////////////////////////////////////////////////////////////////////////////////
683
685 return this->fPrefetch;
686}
687
688
689////////////////////////////////////////////////////////////////////////////////
690
692{
693 if ( fEnablePrefetching && fPrefetch ) {
695 }
696}
697
698
699////////////////////////////////////////////////////////////////////////////////
700/// Sets the buffer size.
701///
702/// If the current prefetch list is too large to fit in
703/// the new buffer some or all of the prefetch blocks are dropped. The
704/// requested buffersize must be greater than zero.
705/// It will be automatically clamped to minimum 100000 (if <= 10000) and maximum INT_MAX
706/// Return values:
707/// - 0 if the prefetch block lists remain unchanged
708/// - 1 if some or all blocks have been removed from the prefetch list
709/// - -1 on error
710
712{
713 if (buffersize <= 0) return -1;
714 if (buffersize <=10000) buffersize = 100000;
715 if (buffersize > std::numeric_limits<Int_t>::max()) buffersize = std::numeric_limits<Int_t>::max();
716
717 if (buffersize == fBufferSize) {
718 fBufferSizeMin = buffersize;
719 return 0;
720 }
721
722 Bool_t inval = kFALSE;
723
724 // the cached data is too large to fit in the new buffer size mark data unavailable
725 if (fNtot > buffersize) {
726 Prefetch(0, 0);
727 inval = kTRUE;
728 }
729 if (fBNtot > buffersize) {
730 SecondPrefetch(0, 0);
731 inval = kTRUE;
732 }
733
734 char *np = 0;
736 char *pres = 0;
737 if (fIsTransferred) {
738 // will need to preserve buffer data
739 pres = fBuffer;
740 fBuffer = 0;
741 }
742 delete [] fBuffer;
743 fBuffer = 0;
744 np = new char[buffersize];
745 if (pres) {
746 memcpy(np, pres, fNtot);
747 }
748 delete [] pres;
749 }
750
751 delete [] fBuffer;
752 fBuffer = np;
753 fBufferSizeMin = buffersize;
754 fBufferSize = buffersize;
755
756 if (inval) {
757 return 1;
758 }
759
760 return 0;
761}
762
763
764////////////////////////////////////////////////////////////////////////////////
765/// Set the prefetching mode of this file.
766///
767/// If 'setPrefetching', enable the asynchronous prefetching
768/// (using TFilePrefetch) and if the gEnv and rootrc
769/// variable Cache.Directory is set, also enable the local
770/// caching of the prefetched blocks.
771/// if 'setPrefetching', the old prefetcher is enabled is
772/// the gEnv and rootrc variable is TFile.AsyncReading
773
775{
776 SetEnablePrefetchingImpl(setPrefetching);
777}
778
779////////////////////////////////////////////////////////////////////////////////
780/// TFileCacheRead implementation of SetEnablePrefetching.
781///
782/// This function is called from the constructor and should not be virtual.
783
785{
786 fEnablePrefetching = setPrefetching;
787
790 const char* cacheDir = gEnv->GetValue("Cache.Directory", "");
791 if (strcmp(cacheDir, ""))
792 if (!fPrefetch->SetCache((char*) cacheDir))
793 fprintf(stderr, "Error while trying to set the cache directory: %s.\n", cacheDir);
794 if (fPrefetch->ThreadStart()){
795 fprintf(stderr,"Error stating prefetching thread. Disabling prefetching.\n");
797 }
798 } else if (fPrefetch && !fEnablePrefetching) {
800 fPrefetch = NULL;
801 }
802
803 //environment variable used to switch to the new method of reading asynchronously
804 if (fEnablePrefetching) {
806 }
807 else {
808 fAsyncReading = gEnv->GetValue("TFile.AsyncReading", 0);
809 if (fAsyncReading) {
810 // Check if asynchronous reading is supported by this TFile specialization
812 if (fFile && !(fFile->ReadBufferAsync(0, 0)))
814 }
815 if (!fAsyncReading && fBuffer == 0) {
816 // we use sync primitives, hence we need the local buffer
817 fBuffer = new char[fBufferSize];
818 }
819 }
820}
821
#define SafeDelete(p)
Definition RConfig.hxx:533
#define e(i)
Definition RSha256.hxx:103
int Int_t
Definition RtypesCore.h:45
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
double Double_t
Definition RtypesCore.h:59
long long Long64_t
Definition RtypesCore.h:69
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:382
R__EXTERN TEnv * gEnv
Definition TEnv.h:170
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
Definition TROOT.cxx:597
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition TEnv.cxx:491
A cache when reading files over the network.
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 ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
virtual void SetCacheRead(TFileCacheRead *cache, TObject *tree=nullptr, ECacheAction action=kDisconnect)
Set a pointer to the read cache.
Definition TFile.cxx:2365
TFileCacheWrite * GetCacheWrite() const
Return a pointer to the current write cache.
Definition TFile.cxx:1279
static Int_t GetReadaheadSize()
Static function returning the readahead buffer size.
Definition TFile.cxx:4599
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:1821
virtual Int_t GetReadCalls() const
Definition TFile.h:244
virtual Long64_t GetBytesRead() const
Definition TFile.h:241
virtual const TUrl * GetEndpointUrl() const
Definition TFile.h:235
virtual Long64_t GetBytesReadExtra() const
Definition TFile.h:242
ECacheAction
TTreeCache flushing semantics.
Definition TFile.h:70
@ kDisconnect
Definition TFile.h:70
virtual Bool_t ReadBufferAsync(Long64_t offs, Int_t len)
Definition TFile.cxx:5208
virtual void SetOffset(Long64_t offset, ERelativeTo pos=kBeg)
Set position from where to start reading.
Definition TFile.cxx:2253
virtual Bool_t ReadBuffer(char *buf, Int_t len)
Read a buffer from the file.
Definition TFile.cxx:1770
Mother of all ROOT objects.
Definition TObject.h:41
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:967
Basic string class.
Definition TString.h:139
void ToLower()
Change string to lower-case.
Definition TString.cxx:1182
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:632
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:431
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:347