Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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:
51  TFilePrefetch(TFile*);
52  virtual ~TFilePrefetch();
53 
54  void ReadAsync(TFPBlock*, Bool_t&);
55  void ReadListOfBlocks();
56 
57  void AddPendingBlock(TFPBlock*);
58  TFPBlock *GetPendingBlock();
59 
60  void AddReadBlock(TFPBlock*);
61  Bool_t ReadBuffer(char*, Long64_t, Int_t);
62  void ReadBlock(Long64_t*, Int_t*, Int_t);
63  TFPBlock *CreateBlockObj(Long64_t*, Int_t*, Int_t);
64 
65  TThread *GetThread() const;
66  Int_t ThreadStart();
67 
68  Bool_t SetCache(const char*);
69  Bool_t CheckBlockInCache(char*&, TFPBlock*);
70  char *GetBlockFromCache(const char*, Int_t);
71  void SaveBlockInCache(TFPBlock*);
72 
73  Int_t SumHex(const char*);
74  Bool_t BinarySearchReadList(TFPBlock*, Long64_t, Int_t, Int_t*);
75  Long64_t GetWaitTime();
76 
77  void SetFile(TFile* file, TFile::ECacheAction action = TFile::kDisconnect);
78  std::condition_variable &GetCondNewBlock() { return fNewBlockAdded; };
79  void WaitFinishPrefetch();
80  Bool_t IsPrefetchFinished() const { return fPrefetchFinished; }
81 
82  ClassDef(TFilePrefetch, 0); // File block prefetcher
83 };
84 
85 #endif