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