ROOT logo
// @(#)root/tree:$Id: TTreeCacheUnzip.h 23685 2008-05-07 15:18:39Z brun $
// Author: Rene Brun   04/06/2006

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TTreeCacheUnzip
#define ROOT_TTreeCacheUnzip


//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TTreeCacheUnzip                                                      //
//                                                                      //
// Specialization of TTreeCache for parallel Unzipping                  //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TTreeCache
#include "TTreeCache.h"
#endif

class TTree;
class TBranch;
class TThread;
class TCondition;
class TBasket;
class TMutex;

class TTreeCacheUnzip : public TTreeCache {
public:
   // We have three possibilities for the unzipping mode:
   // enable, disable and force
   enum EParUnzipMode { kEnable, kDisable, kForce };

protected:
   TMutex     *fMutexCache;

   // Members for paral. managing
   TThread    *fUnzipThread;
   Bool_t      fActiveThread;
   TCondition *fUnzipCondition;
   Bool_t      fNewTransfer;      // Used to indicate the second thread taht a new transfer is in progress
   Bool_t      fParallel;         // Indicate if we want to activate the parallelism (for this instance)

   TMutex     *fMutexBuffer;      // Mutex to protect the unzipping buffer 'fUnzipBuffer'
   TMutex     *fMutexList;        // Mutex to protect the list of inflated buffer

   Int_t       fTmpBufferSz;      //!  Size for the fTmpBuffer (default is 10KB... used to unzip a buffer)
   char       *fTmpBuffer;        //! [fTmpBufferSz] buffer of contiguous unzipped blocks

   static TTreeCacheUnzip::EParUnzipMode fgParallel;  // Indicate if we want to activate the parallelism

   // Members to keep track of the unzipping buffer
   Long64_t    fPosWrite;

   // Unzipping related member
   Int_t      *fUnzipLen;         //! [fNseek] Length of buffers to be unzipped
   Int_t      *fUnzipPos;         //! [fNseek] Position of sorted blocks in fUnzipBuffer
   Int_t       fNseekMax;         //!  fNseek can change so we need to know its max size
   Long64_t    fUnzipBufferSize;  //!  Size for the fUnzipBuffer (default is 2*fBufferSize)
   char       *fUnzipBuffer;      //! [fTotBytes] buffer of contiguous unzipped blocks
   Bool_t      fSkipZip;          //  say if we should skip the uncompression of all buffers
   static Double_t fgRelBuffSize; // This is the percentage of the TTreeCacheUnzip that will be used

   //! keep track of the buffer we are currently unzipping
   Int_t       fUnzipStart;       //! This will give uf the start index (fSeekSort)
   Int_t       fUnzipEnd;         //! Unzipped buffers go from fUnzipStart to fUnzipEnd
   Int_t       fUnzipNext;        //! From fUnzipEnd to fUnzipNext we have to buffer that will be unzipped soon

   // Members use to keep statistics
   Int_t       fNUnzip;           //! number of blocks that were unzipped
   Int_t       fNFound;           //! number of blocks that were found in the cache
   Int_t       fNMissed;          //! number of blocks that were not found in the cache and were unzipped


private:
   TTreeCacheUnzip(const TTreeCacheUnzip &);            //this class cannot be copied
   TTreeCacheUnzip& operator=(const TTreeCacheUnzip &);

   // Private methods
   void  Init();
   Int_t StartThreadUnzip();
   Int_t StopThreadUnzip();

public:
   TTreeCacheUnzip();
   TTreeCacheUnzip(TTree *tree, Int_t buffersize=0);
   virtual ~TTreeCacheUnzip();
   virtual void        AddBranch(TBranch *b, Bool_t subbranches = kFALSE);
   virtual void        AddBranch(const char *branch, Bool_t subbranches = kFALSE);
   Bool_t              FillBuffer();
   void                SetEntryRange(Long64_t emin,   Long64_t emax);
   virtual void        StopLearningPhase();
   void                UpdateBranches(TTree *tree, Bool_t owner = kFALSE);

   // Methods related to the thread
   static EParUnzipMode GetParallelUnzip();
   static Bool_t        IsParallelUnzip();
   Bool_t               IsActiveThread();
   Bool_t               IsQueueEmpty();
   Int_t                ProcessQueue();
   void                 SendSignal();
   static Int_t         SetParallelUnzip(TTreeCacheUnzip::EParUnzipMode option = TTreeCacheUnzip::kEnable);
   void                 WaitForSignal();

   // Unzipping related methods
   Int_t          GetRecordHeader(char *buf, Int_t maxbytes, Int_t &nbytes, Int_t &objlen, Int_t &keylen);
   virtual Bool_t GetSkipZip() { return fSkipZip; }
   virtual void   ResetCache();
   Int_t          GetUnzipBuffer(char **buf, Long64_t pos, Int_t len, Bool_t *free);
   void           SetUnzipBufferSize(Long64_t bufferSize);
   virtual void   SetSkipZip(Bool_t skip = kTRUE) { fSkipZip = skip; }
   Int_t          UnzipBuffer(char **dest, char *src);
   Int_t          UnzipCache();

   // Methods to get stats
   Int_t  GetNUnzip() { return fNUnzip; }
   Int_t  GetNFound() { return fNFound; }
   Int_t  GetNMissed(){ return fNMissed; }

   // static members
   static void* UnzipLoop(void *arg);
   ClassDef(TTreeCacheUnzip,0)  //Specialization of TTreeCache for parallel unzipping
};

#endif
 TTreeCacheUnzip.h:1
 TTreeCacheUnzip.h:2
 TTreeCacheUnzip.h:3
 TTreeCacheUnzip.h:4
 TTreeCacheUnzip.h:5
 TTreeCacheUnzip.h:6
 TTreeCacheUnzip.h:7
 TTreeCacheUnzip.h:8
 TTreeCacheUnzip.h:9
 TTreeCacheUnzip.h:10
 TTreeCacheUnzip.h:11
 TTreeCacheUnzip.h:12
 TTreeCacheUnzip.h:13
 TTreeCacheUnzip.h:14
 TTreeCacheUnzip.h:15
 TTreeCacheUnzip.h:16
 TTreeCacheUnzip.h:17
 TTreeCacheUnzip.h:18
 TTreeCacheUnzip.h:19
 TTreeCacheUnzip.h:20
 TTreeCacheUnzip.h:21
 TTreeCacheUnzip.h:22
 TTreeCacheUnzip.h:23
 TTreeCacheUnzip.h:24
 TTreeCacheUnzip.h:25
 TTreeCacheUnzip.h:26
 TTreeCacheUnzip.h:27
 TTreeCacheUnzip.h:28
 TTreeCacheUnzip.h:29
 TTreeCacheUnzip.h:30
 TTreeCacheUnzip.h:31
 TTreeCacheUnzip.h:32
 TTreeCacheUnzip.h:33
 TTreeCacheUnzip.h:34
 TTreeCacheUnzip.h:35
 TTreeCacheUnzip.h:36
 TTreeCacheUnzip.h:37
 TTreeCacheUnzip.h:38
 TTreeCacheUnzip.h:39
 TTreeCacheUnzip.h:40
 TTreeCacheUnzip.h:41
 TTreeCacheUnzip.h:42
 TTreeCacheUnzip.h:43
 TTreeCacheUnzip.h:44
 TTreeCacheUnzip.h:45
 TTreeCacheUnzip.h:46
 TTreeCacheUnzip.h:47
 TTreeCacheUnzip.h:48
 TTreeCacheUnzip.h:49
 TTreeCacheUnzip.h:50
 TTreeCacheUnzip.h:51
 TTreeCacheUnzip.h:52
 TTreeCacheUnzip.h:53
 TTreeCacheUnzip.h:54
 TTreeCacheUnzip.h:55
 TTreeCacheUnzip.h:56
 TTreeCacheUnzip.h:57
 TTreeCacheUnzip.h:58
 TTreeCacheUnzip.h:59
 TTreeCacheUnzip.h:60
 TTreeCacheUnzip.h:61
 TTreeCacheUnzip.h:62
 TTreeCacheUnzip.h:63
 TTreeCacheUnzip.h:64
 TTreeCacheUnzip.h:65
 TTreeCacheUnzip.h:66
 TTreeCacheUnzip.h:67
 TTreeCacheUnzip.h:68
 TTreeCacheUnzip.h:69
 TTreeCacheUnzip.h:70
 TTreeCacheUnzip.h:71
 TTreeCacheUnzip.h:72
 TTreeCacheUnzip.h:73
 TTreeCacheUnzip.h:74
 TTreeCacheUnzip.h:75
 TTreeCacheUnzip.h:76
 TTreeCacheUnzip.h:77
 TTreeCacheUnzip.h:78
 TTreeCacheUnzip.h:79
 TTreeCacheUnzip.h:80
 TTreeCacheUnzip.h:81
 TTreeCacheUnzip.h:82
 TTreeCacheUnzip.h:83
 TTreeCacheUnzip.h:84
 TTreeCacheUnzip.h:85
 TTreeCacheUnzip.h:86
 TTreeCacheUnzip.h:87
 TTreeCacheUnzip.h:88
 TTreeCacheUnzip.h:89
 TTreeCacheUnzip.h:90
 TTreeCacheUnzip.h:91
 TTreeCacheUnzip.h:92
 TTreeCacheUnzip.h:93
 TTreeCacheUnzip.h:94
 TTreeCacheUnzip.h:95
 TTreeCacheUnzip.h:96
 TTreeCacheUnzip.h:97
 TTreeCacheUnzip.h:98
 TTreeCacheUnzip.h:99
 TTreeCacheUnzip.h:100
 TTreeCacheUnzip.h:101
 TTreeCacheUnzip.h:102
 TTreeCacheUnzip.h:103
 TTreeCacheUnzip.h:104
 TTreeCacheUnzip.h:105
 TTreeCacheUnzip.h:106
 TTreeCacheUnzip.h:107
 TTreeCacheUnzip.h:108
 TTreeCacheUnzip.h:109
 TTreeCacheUnzip.h:110
 TTreeCacheUnzip.h:111
 TTreeCacheUnzip.h:112
 TTreeCacheUnzip.h:113
 TTreeCacheUnzip.h:114
 TTreeCacheUnzip.h:115
 TTreeCacheUnzip.h:116
 TTreeCacheUnzip.h:117
 TTreeCacheUnzip.h:118
 TTreeCacheUnzip.h:119
 TTreeCacheUnzip.h:120
 TTreeCacheUnzip.h:121
 TTreeCacheUnzip.h:122
 TTreeCacheUnzip.h:123
 TTreeCacheUnzip.h:124
 TTreeCacheUnzip.h:125
 TTreeCacheUnzip.h:126
 TTreeCacheUnzip.h:127
 TTreeCacheUnzip.h:128
 TTreeCacheUnzip.h:129
 TTreeCacheUnzip.h:130
 TTreeCacheUnzip.h:131
 TTreeCacheUnzip.h:132