Logo ROOT   6.08/07
Reference Guide
TFilePrefetch.h
Go to the documentation of this file.
1 // @(#)root/io:$Id$
2 // Author: Elvin Sindrilaru 19/05/2011
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2011, 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 #ifndef ROOT_TFilePrefetch
13 #define ROOT_TFilePrefetch
14 
15 #ifndef ROOT_TFile
16 #include "TFile.h"
17 #endif
18 #ifndef ROOT_TThread
19 #include "TThread.h"
20 #endif
21 #ifndef ROOT_TFPBlock
22 #include "TFPBlock.h"
23 #endif
24 #ifndef ROOT_TSemaphore
25 #include "TSemaphore.h"
26 #endif
27 #ifndef ROOT_TMD5
28 #include "TMD5.h"
29 #endif
30 #ifndef ROOT_TObject
31 #include "TObject.h"
32 #endif
33 #ifndef ROOT_TString
34 #include "TString.h"
35 #endif
36 #ifndef ROOT_TObjString
37 #include "TObjString.h"
38 #endif
39 #ifndef ROOT_TObjArray
40 #include "TObjArray.h"
41 #endif
42 #ifndef ROOT_TStopwatch
43 #include "TStopwatch.h"
44 #endif
45 
46 #include <atomic>
47 #include <condition_variable>
48 #include <mutex>
49 
50 
51 class TFilePrefetch : public TObject {
52 
53 private:
54  TFile *fFile; // reference to the file
55  TList *fPendingBlocks; // list of pending blocks to be read
56  TList *fReadBlocks; // list of blocks read
57  TThread *fConsumer; // consumer thread
58  std::mutex fMutexPendingList; // mutex for the pending list
59  std::mutex fMutexReadList; // mutex for the list of read blocks
60  std::condition_variable fNewBlockAdded; // signal the addition of a new pending block
61  std::condition_variable fReadBlockAdded; // signal the addition of a new red block
62  TSemaphore *fSemChangeFile; // semaphore used when changin a file in TChain
63  TString fPathCache; // path to the cache directory
64  TStopwatch fWaitTime; // time wating to prefetch a buffer (in usec)
65  Bool_t fThreadJoined; // mark if async thread was joined
66  std::atomic<Bool_t> fPrefetchFinished; // true if prefetching is over
67 
68  static TThread::VoidRtnFunc_t ThreadProc(void*); //create a joinable worker thread
69 
70 public:
72  virtual ~TFilePrefetch();
73 
74  void ReadAsync(TFPBlock*, Bool_t&);
75  void ReadListOfBlocks();
76 
79 
80  void AddReadBlock(TFPBlock*);
82  void ReadBlock(Long64_t*, Int_t*, Int_t);
84 
85  TThread *GetThread() const;
87 
88  Bool_t SetCache(const char*);
90  char *GetBlockFromCache(const char*, Int_t);
92 
93  Int_t SumHex(const char*);
96 
97  void SetFile(TFile*);
98  std::condition_variable &GetCondNewBlock() { return fNewBlockAdded; };
99  void WaitFinishPrefetch();
101 
102  ClassDef(TFilePrefetch, 0); // File block prefetcher
103 };
104 
105 #endif
void AddPendingBlock(TFPBlock *)
Safe method to add a block to the pendingList.
std::mutex fMutexReadList
Definition: TFilePrefetch.h:59
void *(* VoidRtnFunc_t)(void *)
Definition: TThread.h:59
void ReadAsync(TFPBlock *, Bool_t &)
Read one block and insert it in prefetchBuffers list.
Bool_t BinarySearchReadList(TFPBlock *, Long64_t, Int_t, Int_t *)
Search for a requested element in a block and return the index.
long long Long64_t
Definition: RtypesCore.h:69
Bool_t CheckBlockInCache(char *&, TFPBlock *)
Test if the block is in cache.
Bool_t IsPrefetchFinished() const
static TThread::VoidRtnFunc_t ThreadProc(void *)
Execution loop of the consumer thread.
std::condition_variable fNewBlockAdded
Definition: TFilePrefetch.h:60
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
Bool_t fThreadJoined
Definition: TFilePrefetch.h:65
Long64_t GetWaitTime()
Return the time spent wating for buffer to be read in microseconds.
Basic string class.
Definition: TString.h:137
TStopwatch fWaitTime
Definition: TFilePrefetch.h:64
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
std::mutex fMutexPendingList
Definition: TFilePrefetch.h:58
TThread * fConsumer
Definition: TFilePrefetch.h:57
Int_t ThreadStart()
Used to start the consumer thread.
void SaveBlockInCache(TFPBlock *)
Save the block content in cache.
TSemaphore * fSemChangeFile
Definition: TFilePrefetch.h:62
#define ClassDef(name, id)
Definition: Rtypes.h:254
TString fPathCache
Definition: TFilePrefetch.h:63
void SetFile(TFile *)
Change the file.
std::condition_variable & GetCondNewBlock()
Definition: TFilePrefetch.h:98
This class represents the encapsulation of a block request.
Definition: TFPBlock.h:24
A doubly linked list.
Definition: TList.h:47
void AddReadBlock(TFPBlock *)
Safe method to add a block to the readList.
void WaitFinishPrefetch()
Killing the async prefetching thread.
Bool_t ReadBuffer(char *, Long64_t, Int_t)
Return a prefetched element.
Int_t SumHex(const char *)
Sum up individual hex values to obtain a decimal value.
void ReadListOfBlocks()
Get blocks specified in prefetchBlocks.
virtual ~TFilePrefetch()
Destructor.
TThread * GetThread() const
Return reference to the consumer thread.
std::condition_variable fReadBlockAdded
Definition: TFilePrefetch.h:61
The prefetching mechanism uses two classes (TFilePrefetch and TFPBlock) to prefetch in advance a bloc...
Definition: TFilePrefetch.h:51
Mother of all ROOT objects.
Definition: TObject.h:37
char * GetBlockFromCache(const char *, Int_t)
Return a buffer from cache.
std::atomic< Bool_t > fPrefetchFinished
Definition: TFilePrefetch.h:66
Bool_t SetCache(const char *)
Set the path of the cache directory.
void ReadBlock(Long64_t *, Int_t *, Int_t)
Create a TFPBlock object or recycle one and add it to the prefetchBlocks list.
TList * fReadBlocks
Definition: TFilePrefetch.h:56
TFilePrefetch(TFile *)
Constructor.
TList * fPendingBlocks
Definition: TFilePrefetch.h:55
TFPBlock * GetPendingBlock()
Safe method to remove a block from the pendingList.
TFPBlock * CreateBlockObj(Long64_t *, Int_t *, Int_t)
Create a new block or recycle an old one.
Stopwatch class.
Definition: TStopwatch.h:30