Logo ROOT  
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 "TObject.h"
16 #include "TString.h"
17 #include "TStopwatch.h"
18 #include "TThread.h"
19 #include "TFile.h"
20 
21 #include <atomic>
22 #include <condition_variable>
23 #include <mutex>
24 
25 #ifdef R__LESS_INCLUDES
26 class TSemaphore;
27 class TFPBlock;
28 #else
29 #include "TSemaphore.h"
30 #include "TFPBlock.h"
31 #endif
32 
33 class TFilePrefetch : public TObject {
34 
35 private:
36  TFile *fFile; // reference to the file
37  TList *fPendingBlocks; // list of pending blocks to be read
38  TList *fReadBlocks; // list of blocks read
39  TThread *fConsumer; // consumer thread
40  std::mutex fMutexPendingList; // mutex for the pending list
41  std::mutex fMutexReadList; // mutex for the list of read blocks
42  std::condition_variable fNewBlockAdded; // signal the addition of a new pending block
43  std::condition_variable fReadBlockAdded; // signal the addition of a new red block
44  TSemaphore *fSemChangeFile; // semaphore used when changin a file in TChain
45  TString fPathCache; // path to the cache directory
46  TStopwatch fWaitTime; // time wating to prefetch a buffer (in usec)
47  Bool_t fThreadJoined; // mark if async thread was joined
48  std::atomic<Bool_t> fPrefetchFinished; // true if prefetching is over
49 
50  static TThread::VoidRtnFunc_t ThreadProc(void*); //create a joinable worker thread
51 
52 public:
54  virtual ~TFilePrefetch();
55 
56  void ReadAsync(TFPBlock*, Bool_t&);
57  void ReadListOfBlocks();
58 
61 
62  void AddReadBlock(TFPBlock*);
64  void ReadBlock(Long64_t*, Int_t*, Int_t);
66 
67  TThread *GetThread() const;
69 
70  Bool_t SetCache(const char*);
72  char *GetBlockFromCache(const char*, Int_t);
74 
75  Int_t SumHex(const char*);
78 
80  std::condition_variable &GetCondNewBlock() { return fNewBlockAdded; };
81  void WaitFinishPrefetch();
83 
84  ClassDef(TFilePrefetch, 0); // File block prefetcher
85 };
86 
87 #endif
TFilePrefetch::GetThread
TThread * GetThread() const
Return reference to the consumer thread.
Definition: TFilePrefetch.cxx:305
TFilePrefetch::fThreadJoined
Bool_t fThreadJoined
Definition: TFilePrefetch.h:47
TThread
Definition: TThread.h:40
TFilePrefetch::fSemChangeFile
TSemaphore * fSemChangeFile
Definition: TFilePrefetch.h:44
TFilePrefetch::fMutexPendingList
std::mutex fMutexPendingList
Definition: TFilePrefetch.h:40
TFile::ECacheAction
ECacheAction
TTreeCache flushing semantics.
Definition: TFile.h:71
TFilePrefetch::fPrefetchFinished
std::atomic< Bool_t > fPrefetchFinished
Definition: TFilePrefetch.h:48
TFilePrefetch::IsPrefetchFinished
Bool_t IsPrefetchFinished() const
Definition: TFilePrefetch.h:82
TStopwatch.h
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
TFilePrefetch::SetFile
void SetFile(TFile *file, TFile::ECacheAction action=TFile::kDisconnect)
Change the file.
Definition: TFilePrefetch.cxx:319
TFilePrefetch::AddReadBlock
void AddReadBlock(TFPBlock *)
Safe method to add a block to the readList.
Definition: TFilePrefetch.cxx:261
TFilePrefetch::fPathCache
TString fPathCache
Definition: TFilePrefetch.h:45
TFilePrefetch::ReadListOfBlocks
void ReadListOfBlocks()
Get blocks specified in prefetchBlocks.
Definition: TFilePrefetch.cxx:130
TFilePrefetch::fMutexReadList
std::mutex fMutexReadList
Definition: TFilePrefetch.h:41
TFilePrefetch::ThreadProc
static TThread::VoidRtnFunc_t ThreadProc(void *)
Execution loop of the consumer thread.
Definition: TFilePrefetch.cxx:367
TFilePrefetch::SetCache
Bool_t SetCache(const char *)
Set the path of the cache directory.
Definition: TFilePrefetch.cxx:528
TString
Basic string class.
Definition: TString.h:136
TFilePrefetch::AddPendingBlock
void AddPendingBlock(TFPBlock *)
Safe method to add a block to the pendingList.
Definition: TFilePrefetch.cxx:225
TFilePrefetch::CreateBlockObj
TFPBlock * CreateBlockObj(Long64_t *, Int_t *, Int_t)
Create a new block or recycle an old one.
Definition: TFilePrefetch.cxx:283
TString.h
TFile.h
bool
TFilePrefetch::ThreadStart
Int_t ThreadStart()
Used to start the consumer thread.
Definition: TFilePrefetch.cxx:351
TFilePrefetch::fReadBlocks
TList * fReadBlocks
Definition: TFilePrefetch.h:38
TFilePrefetch::GetPendingBlock
TFPBlock * GetPendingBlock()
Safe method to remove a block from the pendingList.
Definition: TFilePrefetch.cxx:237
TFile::kDisconnect
@ kDisconnect
Definition: TFile.h:71
TSemaphore.h
TFilePrefetch::ReadAsync
void ReadAsync(TFPBlock *, Bool_t &)
Read one block and insert it in prefetchBuffers list.
Definition: TFilePrefetch.cxx:108
TFilePrefetch::BinarySearchReadList
Bool_t BinarySearchReadList(TFPBlock *, Long64_t, Int_t, Int_t *)
Search for a requested element in a block and return the index.
Definition: TFilePrefetch.cxx:146
TFilePrefetch::TFilePrefetch
TFilePrefetch(TFile *)
Constructor.
Definition: TFilePrefetch.cxx:56
TFilePrefetch::ReadBlock
void ReadBlock(Long64_t *, Int_t *, Int_t)
Create a TFPBlock object or recycle one and add it to the prefetchBlocks list.
Definition: TFilePrefetch.cxx:216
TFilePrefetch::SumHex
Int_t SumHex(const char *)
Sum up individual hex values to obtain a decimal value.
Definition: TFilePrefetch.cxx:383
TFilePrefetch::SaveBlockInCache
void SaveBlockInCache(TFPBlock *)
Save the block content in cache.
Definition: TFilePrefetch.cxx:476
TFile
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
TFilePrefetch::fWaitTime
TStopwatch fWaitTime
Definition: TFilePrefetch.h:46
TFilePrefetch::fReadBlockAdded
std::condition_variable fReadBlockAdded
Definition: TFilePrefetch.h:43
TFilePrefetch
The prefetching mechanism uses two classes (TFilePrefetch and TFPBlock) to prefetch in advance a bloc...
Definition: TFilePrefetch.h:33
TSemaphore
Definition: TSemaphore.h:29
TFPBlock.h
TThread.h
TFilePrefetch::GetWaitTime
Long64_t GetWaitTime()
Return the time spent wating for buffer to be read in microseconds.
Definition: TFilePrefetch.cxx:172
TFilePrefetch::CheckBlockInCache
Bool_t CheckBlockInCache(char *&, TFPBlock *)
Test if the block is in cache.
Definition: TFilePrefetch.cxx:397
TObject.h
TFilePrefetch::fFile
TFile * fFile
Definition: TFilePrefetch.h:36
file
Definition: file.py:1
TStopwatch
Stopwatch class.
Definition: TStopwatch.h:28
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TThread::VoidRtnFunc_t
void *(* VoidRtnFunc_t)(void *)
Definition: TThread.h:52
TFilePrefetch::fPendingBlocks
TList * fPendingBlocks
Definition: TFilePrefetch.h:37
TFilePrefetch::~TFilePrefetch
virtual ~TFilePrefetch()
Destructor.
Definition: TFilePrefetch.cxx:74
TFilePrefetch::ReadBuffer
Bool_t ReadBuffer(char *, Long64_t, Int_t)
Return a prefetched element.
Definition: TFilePrefetch.cxx:180
TFilePrefetch::fNewBlockAdded
std::condition_variable fNewBlockAdded
Definition: TFilePrefetch.h:42
TFPBlock
This class represents the encapsulation of a block request.
Definition: TFPBlock.h:22
TFilePrefetch::GetBlockFromCache
char * GetBlockFromCache(const char *, Int_t)
Return a buffer from cache.
Definition: TFilePrefetch.cxx:443
TFilePrefetch::WaitFinishPrefetch
void WaitFinishPrefetch()
Killing the async prefetching thread.
Definition: TFilePrefetch.cxx:90
TList
A doubly linked list.
Definition: TList.h:44
TFilePrefetch::fConsumer
TThread * fConsumer
Definition: TFilePrefetch.h:39
TFilePrefetch::GetCondNewBlock
std::condition_variable & GetCondNewBlock()
Definition: TFilePrefetch.h:80
int