Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:12:30

0001 // Authors: Rene Brun   04/06/2006
0002 //          Leandro Franco   10/04/2008
0003 //          Fabrizio Furano (CERN) Aug 2009
0004 
0005 /*************************************************************************
0006  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
0007  * All rights reserved.                                                  *
0008  *                                                                       *
0009  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0010  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0011  *************************************************************************/
0012 
0013 #ifndef ROOT_TTreeCacheUnzip
0014 #define ROOT_TTreeCacheUnzip
0015 
0016 #include "Bytes.h"
0017 #include "TTreeCache.h"
0018 #include <atomic>
0019 #include <memory>
0020 #include <vector>
0021 
0022 class TBasket;
0023 class TBranch;
0024 class TMutex;
0025 class TTree;
0026 
0027 #ifdef R__USE_IMT
0028 namespace ROOT {
0029 namespace Experimental {
0030 class TTaskGroup;
0031 }
0032 }
0033 #endif
0034 
0035 class TTreeCacheUnzip : public TTreeCache {
0036 
0037 public:
0038    // We have three possibilities for the unzipping mode:
0039    // enable, disable and force
0040    enum EParUnzipMode { kEnable, kDisable, kForce };
0041 
0042    // Unzipping states for a basket:
0043    enum EUnzipState { kUntouched, kProgress, kFinished };
0044 
0045 protected:
0046    // Unzipping state for baskets
0047    struct UnzipState {
0048       // Note: we cannot use std::unique_ptr<std::unique_ptr<char[]>[]> or vector of unique_ptr
0049       // for fUnzipChunks since std::unique_ptr is not copy constructable.
0050       // However, in future upgrade we cannot use make_vector in C++14.
0051       std::unique_ptr<char[]> *fUnzipChunks;     ///<! [fNseek] Individual unzipped chunks. Their summed size is kept under control.
0052       std::vector<Int_t>       fUnzipLen;        ///<! [fNseek] Length of the unzipped buffers
0053       std::atomic<Byte_t>     *fUnzipStatus;     ///<! [fNSeek]
0054 
0055       UnzipState() {
0056          fUnzipChunks = nullptr;
0057          fUnzipStatus = nullptr;
0058       }
0059       ~UnzipState() {
0060          if (fUnzipChunks) delete [] fUnzipChunks;
0061          if (fUnzipStatus) delete [] fUnzipStatus;
0062       }
0063       void   Clear(Int_t size);
0064       bool   IsUntouched(Int_t index) const;
0065       bool   IsProgress(Int_t index) const;
0066       bool   IsFinished(Int_t index) const;
0067       bool   IsUnzipped(Int_t index) const;
0068       void   Reset(Int_t oldSize, Int_t newSize);
0069       void   SetUntouched(Int_t index);
0070       void   SetProgress(Int_t index);
0071       void   SetFinished(Int_t index);
0072       void   SetMissed(Int_t index);
0073       void   SetUnzipped(Int_t index, char* buf, Int_t len);
0074       bool TryUnzipping(Int_t index);
0075    };
0076 
0077    typedef struct UnzipState UnzipState_t;
0078    UnzipState_t fUnzipState;
0079 
0080    // Members for paral. managing
0081    bool        fAsyncReading;
0082    bool        fEmpty;
0083    Int_t       fCycle;
0084    bool        fParallel; ///< Indicate if we want to activate the parallelism (for this instance)
0085 
0086    std::unique_ptr<TMutex> fIOMutex;
0087 
0088    static TTreeCacheUnzip::EParUnzipMode fgParallel;  ///< Indicate if we want to activate the parallelism
0089 
0090    // IMT TTaskGroup Manager
0091 #ifdef R__USE_IMT
0092    std::unique_ptr<ROOT::Experimental::TTaskGroup> fUnzipTaskGroup;
0093 #endif
0094 
0095    // Unzipping related members
0096    Int_t       fNseekMax;         ///<!  fNseek can change so we need to know its max size
0097    Int_t       fUnzipGroupSize;   ///<!  Min accumulated size of a group of baskets ready to be unzipped by a IMT task
0098    Long64_t    fUnzipBufferSize;  ///<!  Max Size for the ready unzipped blocks (default is 2*fBufferSize)
0099 
0100    static Double_t fgRelBuffSize; ///< This is the percentage of the TTreeCacheUnzip that will be used
0101 
0102    // Members use to keep statistics
0103    Int_t       fNFound;           ///<! number of blocks that were found in the cache
0104    Int_t       fNMissed;          ///<! number of blocks that were not found in the cache and were unzipped
0105    Int_t       fNStalls;          ///<! number of hits which caused a stall
0106    Int_t       fNUnzip;           ///<! number of blocks that were unzipped
0107 
0108 private:
0109    TTreeCacheUnzip(const TTreeCacheUnzip &) = delete;
0110    TTreeCacheUnzip& operator=(const TTreeCacheUnzip &) = delete;
0111 
0112    char *fCompBuffer;
0113    Int_t fCompBufferSize;
0114 
0115    // Private methods
0116    void  Init();
0117 
0118 public:
0119    TTreeCacheUnzip();
0120    TTreeCacheUnzip(TTree *tree, Int_t buffersize=0);
0121    ~TTreeCacheUnzip() override;
0122 
0123    Int_t               AddBranch(TBranch *b, bool subbranches = false) override;
0124    Int_t               AddBranch(const char *branch, bool subbranches = false) override;
0125    bool                FillBuffer() override;
0126    Int_t               ReadBufferExt(char *buf, Long64_t pos, Int_t len, Int_t &loc) override;
0127    void                SetEntryRange(Long64_t emin,   Long64_t emax) override;
0128    void                StopLearningPhase() override;
0129    void                UpdateBranches(TTree *tree) override;
0130 
0131    // Methods related to the thread
0132    static EParUnzipMode GetParallelUnzip();
0133    static bool          IsParallelUnzip();
0134    static Int_t         SetParallelUnzip(TTreeCacheUnzip::EParUnzipMode option = TTreeCacheUnzip::kEnable);
0135 
0136    // Unzipping related methods
0137 #ifdef R__USE_IMT
0138    Int_t          CreateTasks();
0139 #endif
0140    Int_t          GetRecordHeader(char *buf, Int_t maxbytes, Int_t &nbytes, Int_t &objlen, Int_t &keylen);
0141    Int_t          GetUnzipBuffer(char **buf, Long64_t pos, Int_t len, bool *free) override;
0142    Int_t          GetUnzipGroupSize() { return fUnzipGroupSize; }
0143    void           ResetCache() override;
0144    Int_t          SetBufferSize(Int_t buffersize) override;
0145    void           SetUnzipBufferSize(Long64_t bufferSize);
0146    void           SetUnzipGroupSize(Int_t groupSize) { fUnzipGroupSize = groupSize; }
0147    static void    SetUnzipRelBufferSize(Float_t relbufferSize);
0148    Int_t          UnzipBuffer(char **dest, char *src);
0149    Int_t          UnzipCache(Int_t index);
0150 
0151    // Methods to get stats
0152    Int_t  GetNUnzip() { return fNUnzip; }
0153    Int_t  GetNMissed(){ return fNMissed; }
0154    Int_t  GetNFound() { return fNFound; }
0155 
0156    void Print(Option_t* option = "") const override;
0157 
0158    // static members
0159    ClassDefOverride(TTreeCacheUnzip,0)  //Specialization of TTreeCache for parallel unzipping
0160 };
0161 
0162 #endif