Logo ROOT   6.12/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 #include "TFile.h"
16 #include "TThread.h"
17 #include "TFPBlock.h"
18 #include "TSemaphore.h"
19 #include "TMD5.h"
20 #include "TObject.h"
21 #include "TString.h"
22 #include "TObjString.h"
23 #include "TObjArray.h"
24 #include "TStopwatch.h"
25 
26 #include <atomic>
27 #include <condition_variable>
28 #include <mutex>
29 
30 
31 class TFilePrefetch : public TObject {
32 
33 private:
34  TFile *fFile; // reference to the file
35  TList *fPendingBlocks; // list of pending blocks to be read
36  TList *fReadBlocks; // list of blocks read
37  TThread *fConsumer; // consumer thread
38  std::mutex fMutexPendingList; // mutex for the pending list
39  std::mutex fMutexReadList; // mutex for the list of read blocks
40  std::condition_variable fNewBlockAdded; // signal the addition of a new pending block
41  std::condition_variable fReadBlockAdded; // signal the addition of a new red block
42  TSemaphore *fSemChangeFile; // semaphore used when changin a file in TChain
43  TString fPathCache; // path to the cache directory
44  TStopwatch fWaitTime; // time wating to prefetch a buffer (in usec)
45  Bool_t fThreadJoined; // mark if async thread was joined
46  std::atomic<Bool_t> fPrefetchFinished; // true if prefetching is over
47 
48  static TThread::VoidRtnFunc_t ThreadProc(void*); //create a joinable worker thread
49 
50 public:
52  virtual ~TFilePrefetch();
53 
54  void ReadAsync(TFPBlock*, Bool_t&);
55  void ReadListOfBlocks();
56 
59 
60  void AddReadBlock(TFPBlock*);
62  void ReadBlock(Long64_t*, Int_t*, Int_t);
64 
65  TThread *GetThread() const;
67 
68  Bool_t SetCache(const char*);
70  char *GetBlockFromCache(const char*, Int_t);
72 
73  Int_t SumHex(const char*);
76 
78  std::condition_variable &GetCondNewBlock() { return fNewBlockAdded; };
79  void WaitFinishPrefetch();
81 
82  ClassDef(TFilePrefetch, 0); // File block prefetcher
83 };
84 
85 #endif
void AddPendingBlock(TFPBlock *)
Safe method to add a block to the pendingList.
std::mutex fMutexReadList
Definition: TFilePrefetch.h:39
void *(* VoidRtnFunc_t)(void *)
Definition: TThread.h:49
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
Definition: TFilePrefetch.h:80
static TThread::VoidRtnFunc_t ThreadProc(void *)
Execution loop of the consumer thread.
std::condition_variable fNewBlockAdded
Definition: TFilePrefetch.h:40
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
Bool_t fThreadJoined
Definition: TFilePrefetch.h:45
Long64_t GetWaitTime()
Return the time spent wating for buffer to be read in microseconds.
Basic string class.
Definition: TString.h:125
TStopwatch fWaitTime
Definition: TFilePrefetch.h:44
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
std::mutex fMutexPendingList
Definition: TFilePrefetch.h:38
TThread * fConsumer
Definition: TFilePrefetch.h:37
Int_t ThreadStart()
Used to start the consumer thread.
void SaveBlockInCache(TFPBlock *)
Save the block content in cache.
TSemaphore * fSemChangeFile
Definition: TFilePrefetch.h:42
#define ClassDef(name, id)
Definition: Rtypes.h:320
TString fPathCache
Definition: TFilePrefetch.h:43
std::condition_variable & GetCondNewBlock()
Definition: TFilePrefetch.h:78
This class represents the encapsulation of a block request.
Definition: TFPBlock.h:22
A doubly linked list.
Definition: TList.h:44
void AddReadBlock(TFPBlock *)
Safe method to add a block to the readList.
void SetFile(TFile *file, TFile::ECacheAction action=TFile::kDisconnect)
Change the file.
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:41
The prefetching mechanism uses two classes (TFilePrefetch and TFPBlock) to prefetch in advance a bloc...
Definition: TFilePrefetch.h:31
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:46
Definition: file.py:1
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:36
TFilePrefetch(TFile *)
Constructor.
TList * fPendingBlocks
Definition: TFilePrefetch.h:35
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.
ECacheAction
TTreeCache flushing semantics.
Definition: TFile.h:63
Stopwatch class.
Definition: TStopwatch.h:28