Logo ROOT   6.14/05
Reference Guide
TTreeCacheUnzip.h
Go to the documentation of this file.
1 // @(#)root/tree:$Id$
2 // Author: Rene Brun 04/06/2006
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, 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_TTreeCacheUnzip
13 #define ROOT_TTreeCacheUnzip
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TTreeCacheUnzip //
19 // //
20 // Specialization of TTreeCache for parallel Unzipping //
21 // //
22 // Fabrizio Furano (CERN) Aug 2009 //
23 // Core TTree-related code borrowed from the previous version //
24 // by Leandro Franco and Rene Brun //
25 // //
26 //////////////////////////////////////////////////////////////////////////
27 
28 #include "Bytes.h"
29 #include "TTreeCache.h"
30 #include "ROOT/TTaskGroup.hxx"
31 #include <atomic>
32 #include <queue>
33 #include <memory>
34 #include <vector>
35 
36 class TBasket;
37 class TBranch;
38 class TMutex;
39 class TTree;
40 
41 class TTreeCacheUnzip : public TTreeCache {
42 
43 public:
44  // We have three possibilities for the unzipping mode:
45  // enable, disable and force
47 
48  // Unzipping states for a basket:
50 
51 protected:
52  // Unzipping state for baskets
53  struct UnzipState {
54  // Note: we cannot use std::unique_ptr<std::unique_ptr<char[]>[]> or vector of unique_ptr
55  // for fUnzipChunks since std::unique_ptr is not copy constructable.
56  // However, in future upgrade we cannot use make_vector in C++14.
57  std::unique_ptr<char[]> *fUnzipChunks; ///<! [fNseek] Individual unzipped chunks. Their summed size is kept under control.
58  std::vector<Int_t> fUnzipLen; ///<! [fNseek] Length of the unzipped buffers
59  std::atomic<Byte_t> *fUnzipStatus; ///<! [fNSeek]
60 
62  fUnzipChunks = nullptr;
63  fUnzipStatus = nullptr;
64  }
66  if (fUnzipChunks) delete [] fUnzipChunks;
67  if (fUnzipStatus) delete [] fUnzipStatus;
68  }
69  void Clear(Int_t size);
70  Bool_t IsUntouched(Int_t index) const;
71  Bool_t IsProgress(Int_t index) const;
72  Bool_t IsFinished(Int_t index) const;
73  Bool_t IsUnzipped(Int_t index) const;
74  void Reset(Int_t oldSize, Int_t newSize);
75  void SetUntouched(Int_t index);
76  void SetProgress(Int_t index);
77  void SetFinished(Int_t index);
78  void SetMissed(Int_t index);
79  void SetUnzipped(Int_t index, char* buf, Int_t len);
80  Bool_t TryUnzipping(Int_t index);
81  };
82 
83  typedef struct UnzipState UnzipState_t;
85 
86  // Members for paral. managing
90  Bool_t fParallel; ///< Indicate if we want to activate the parallelism (for this instance)
91 
93 
94  static TTreeCacheUnzip::EParUnzipMode fgParallel; ///< Indicate if we want to activate the parallelism
95 
96  // IMT TTaskGroup Manager
97 #ifdef R__USE_IMT
98  std::unique_ptr<ROOT::Experimental::TTaskGroup> fUnzipTaskGroup;
99 #endif
100 
101  // Unzipping related members
102  Int_t fNseekMax; ///<! fNseek can change so we need to know its max size
103  Int_t fUnzipGroupSize; ///<! Min accumulated size of a group of baskets ready to be unzipped by a IMT task
104  Long64_t fUnzipBufferSize; ///<! Max Size for the ready unzipped blocks (default is 2*fBufferSize)
105 
106  static Double_t fgRelBuffSize; ///< This is the percentage of the TTreeCacheUnzip that will be used
107 
108  // Members use to keep statistics
109  Int_t fNFound; ///<! number of blocks that were found in the cache
110  Int_t fNMissed; ///<! number of blocks that were not found in the cache and were unzipped
111  Int_t fNStalls; ///<! number of hits which caused a stall
112  Int_t fNUnzip; ///<! number of blocks that were unzipped
113 
114 private:
115  TTreeCacheUnzip(const TTreeCacheUnzip &); //this class cannot be copied
117 
118  char *fCompBuffer;
120 
121  // Private methods
122  void Init();
123 
124 public:
125  TTreeCacheUnzip();
126  TTreeCacheUnzip(TTree *tree, Int_t buffersize=0);
127  virtual ~TTreeCacheUnzip();
128 
129  virtual Int_t AddBranch(TBranch *b, Bool_t subbranches = kFALSE);
130  virtual Int_t AddBranch(const char *branch, Bool_t subbranches = kFALSE);
131  Bool_t FillBuffer();
132  virtual Int_t ReadBufferExt(char *buf, Long64_t pos, Int_t len, Int_t &loc);
133  void SetEntryRange(Long64_t emin, Long64_t emax);
134  virtual void StopLearningPhase();
135  void UpdateBranches(TTree *tree);
136 
137  // Methods related to the thread
139  static Bool_t IsParallelUnzip();
141 
142  // Unzipping related methods
143 #ifdef R__USE_IMT
144  Int_t CreateTasks();
145 #endif
146  Int_t GetRecordHeader(char *buf, Int_t maxbytes, Int_t &nbytes, Int_t &objlen, Int_t &keylen);
147  virtual Int_t GetUnzipBuffer(char **buf, Long64_t pos, Int_t len, Bool_t *free);
149  virtual void ResetCache();
150  virtual Int_t SetBufferSize(Int_t buffersize);
151  void SetUnzipBufferSize(Long64_t bufferSize);
152  void SetUnzipGroupSize(Int_t groupSize) { fUnzipGroupSize = groupSize; }
153  static void SetUnzipRelBufferSize(Float_t relbufferSize);
154  Int_t UnzipBuffer(char **dest, char *src);
155  Int_t UnzipCache(Int_t index);
156 
157  // Methods to get stats
158  Int_t GetNUnzip() { return fNUnzip; }
159  Int_t GetNMissed(){ return fNMissed; }
160  Int_t GetNFound() { return fNFound; }
161 
162  void Print(Option_t* option = "") const;
163 
164  // static members
165  ClassDef(TTreeCacheUnzip,0) //Specialization of TTreeCache for parallel unzipping
166 };
167 
168 #endif
std::unique_ptr< char[]> * fUnzipChunks
! [fNseek] Individual unzipped chunks. Their summed size is kept under control.
Definition: TMutex.h:30
Bool_t IsProgress(Int_t index) const
Int_t fNUnzip
! number of blocks that were unzipped
void SetUnzipGroupSize(Int_t groupSize)
long long Long64_t
Definition: RtypesCore.h:69
void UpdateBranches(TTree *tree)
update pointer to current Tree and recompute pointers to the branches in the cache ...
virtual void ResetCache()
This will delete the list of buffers that are in the unzipping cache and will reset certain values in...
float Float_t
Definition: RtypesCore.h:53
Int_t fNStalls
! number of hits which caused a stall
const char Option_t
Definition: RtypesCore.h:62
Int_t GetRecordHeader(char *buf, Int_t maxbytes, Int_t &nbytes, Int_t &objlen, Int_t &keylen)
Read the logical record header from the buffer buf.
Bool_t TryUnzipping(Int_t index)
Start unzipping the basket if it is untouched yet.
virtual void StopLearningPhase()
It&#39;s the same as TTreeCache::StopLearningPhase but we guarantee that we start the unzipping just afte...
A specialized TFileCacheRead object for a TTree.
Definition: TTreeCache.h:35
static Int_t SetParallelUnzip(TTreeCacheUnzip::EParUnzipMode option=TTreeCacheUnzip::kEnable)
Static function that (de)activates multithreading unzipping.
virtual Int_t SetBufferSize(Int_t buffersize)
Change the underlying buffer size of the cache.
Bool_t FillBuffer()
Fill the cache buffer with the branches in the cache.
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void SetProgress(Int_t index)
void Clear(Int_t size)
Clear all baskets&#39; state arrays.
void SetUnzipBufferSize(Long64_t bufferSize)
Sets the size for the unzipping cache...
Int_t fNFound
! number of blocks that were found in the cache
Bool_t IsUnzipped(Int_t index) const
Check if the basket is unzipped already.
static Bool_t IsParallelUnzip()
Static function that tells wether the multithreading unzipping is activated.
Int_t UnzipBuffer(char **dest, char *src)
Unzips a ROOT specific buffer...
virtual Int_t GetUnzipBuffer(char **buf, Long64_t pos, Int_t len, Bool_t *free)
We try to read a buffer that has already been unzipped Returns -1 in case of read failure...
virtual Int_t ReadBufferExt(char *buf, Long64_t pos, Int_t len, Int_t &loc)
std::unique_ptr< ROOT::Experimental::TTaskGroup > fUnzipTaskGroup
#define ClassDef(name, id)
Definition: Rtypes.h:320
void SetUnzipped(Int_t index, char *buf, Int_t len)
Int_t fNMissed
! number of blocks that were not found in the cache and were unzipped
void Print(Option_t *option="") const
Print cache statistics.
Specialization of TTreeCache for parallel Unzipping.
void Reset(Int_t oldSize, Int_t newSize)
Reset all baskets&#39; state arrays.
Bool_t fParallel
Indicate if we want to activate the parallelism (for this instance)
virtual Int_t AddBranch(TBranch *b, Bool_t subbranches=kFALSE)
Add a branch to the list of branches to be stored in the cache this function is called by TBranch::Ge...
Int_t GetUnzipGroupSize()
static Double_t fgRelBuffSize
This is the percentage of the TTreeCacheUnzip that will be used.
virtual ~TTreeCacheUnzip()
Destructor. (in general called by the TFile destructor)
Int_t CreateTasks()
We create a TTaskGroup and asynchronously maps each group of baskets(> 100 kB in total) to a task...
Int_t fNseekMax
! fNseek can change so we need to know its max size
Bool_t IsUntouched(Int_t index) const
Manages buffers for branches of a Tree.
Definition: TBasket.h:34
static EParUnzipMode GetParallelUnzip()
Static function that returns the parallel option (to indicate an additional thread) ...
std::atomic< Byte_t > * fUnzipStatus
! [fNSeek]
const Bool_t kFALSE
Definition: RtypesCore.h:88
TTreeCacheUnzip & operator=(const TTreeCacheUnzip &)
Int_t fUnzipGroupSize
! Min accumulated size of a group of baskets ready to be unzipped by a IMT task
double Double_t
Definition: RtypesCore.h:55
Long64_t fUnzipBufferSize
! Max Size for the ready unzipped blocks (default is 2*fBufferSize)
#define free
Definition: civetweb.c:1350
Bool_t IsFinished(Int_t index) const
void Init()
Initialization procedure common to all the constructors.
static void SetUnzipRelBufferSize(Float_t relbufferSize)
static function: Sets the unzip relatibe buffer size
std::vector< Int_t > fUnzipLen
! [fNseek] Length of the unzipped buffers
static TTreeCacheUnzip::EParUnzipMode fgParallel
Indicate if we want to activate the parallelism.
void SetUntouched(Int_t index)
#define dest(otri, vertexptr)
Definition: triangle.c:1040
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
Definition: tree.py:1
UnzipState_t fUnzipState
A TTree object has a header with a name and a title.
Definition: TTree.h:70
void SetFinished(Int_t index)
Set cache as finished.
A TTree is a list of TBranches.
Definition: TBranch.h:62
void SetEntryRange(Long64_t emin, Long64_t emax)
Set the minimum and maximum entry number to be processed this information helps to optimize the numbe...
Int_t UnzipCache(Int_t index)
This inflates a basket in the cache.