Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

TCache.h

Go to the documentation of this file.
00001 // @(#)root/net:$Name:  $:$Id: TCache.h,v 1.4 2001/04/11 11:10:44 brun Exp $
00002 // Author: Fons Rademakers   13/01/2001
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2001, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TCache
00013 #define ROOT_TCache
00014 
00015 
00017 //                                                                      //
00018 // TCache                                                               //
00019 //                                                                      //
00020 // A caching system to speed up network I/O, i.e. when there is         //
00021 // no operating system caching support (like the buffer cache for       //
00022 // local disk I/O). The cache makes sure that every I/O is done with    //
00023 // a (large) fixed length buffer thereby avoiding many small I/O's.     //
00024 // The default page size is 512KB. The cache size is not very important //
00025 // when writing sequentially a file, since the pages will not be        //
00026 // reused. In that case use a small cache containing 10 to 20 pages.    //
00027 // In case a file is used for random-access the cache size should be    //
00028 // taken much larger to avoid re-reading pages over the network.        //
00029 // Notice that the TTree's have their own caching mechanism (see        //
00030 // TTree::SetMaxVirtualSize()), so when using mainly TTree's with large //
00031 // basket buffers the cache can be kept quite small.                    //
00032 // Currently the TCache system is used by the classes TNetFile,         //
00033 // TRFIOFile and TWebFile.                                              //
00034 //                                                                      //
00035 // Extra improvement would be to run the Free() process in a separate   //
00036 // thread. Possible flush parameters:                                   //
00037 // nfract  25   fraction of dirty buffers above which the flush process //
00038 //              is activated                                            //
00039 // ndirty  500  maximum number of buffer block which may be written     //
00040 //              during a flush                                          //
00041 //                                                                      //
00043 
00044 #ifndef ROOT_THashList
00045 #include "THashList.h"
00046 #endif
00047 
00048 class TSortedList;
00049 class TFile;
00050 
00051 
00052 class TCache : public TObject {
00053 
00054 friend class TFile;
00055 
00056 private:
00057    // The TPage class describes a cache page
00058    class TPage : public TObject {
00059    friend class TCache;
00060    private:
00061       Seek_t    fOffset; // offset of page in file
00062       char     *fData;   // pointer to page data
00063       Int_t     fSize;   // size of page
00064    public:
00065       enum { kDirty = BIT(14), kLocked = BIT(15) };
00066       TPage(Seek_t offset, char *page, Int_t size)
00067          { fOffset = offset; fData = page; fSize = size; }
00068       ~TPage() { delete [] fData; }
00069       ULong_t Hash() const { return fOffset; }
00070       Bool_t  IsEqual(const TObject *obj) const
00071          { return fOffset == ((const TPage*)obj)->fOffset; }
00072       Bool_t  IsSortable() const { return kTRUE; }
00073       Int_t   Compare(const TObject *obj) const
00074          { return fOffset > ((const TPage*)obj)->fOffset ? 1 :
00075                   fOffset < ((const TPage*)obj)->fOffset ? -1 : 0; }
00076       Seek_t  Offset() const { return fOffset; }
00077       char   *Data() const { return fData; }
00078       Int_t   Size() const { return fSize; }
00079    };
00080 
00081    class TCacheList : public THashList {
00082    public:
00083       TCacheList(Int_t capacity = 1000) : THashList(capacity, 3) { }
00084       void PageUsed(TObject *page) { TList::Remove(page); TList::AddLast(page); }
00085    };
00086 
00087    TCacheList  *fCache;         // hash list containing cached pages
00088    TSortedList *fNew;           // list constaining new pages that have to be written to disk
00089    TList       *fFree;          // list containing unused pages
00090    TFile       *fFile;          // file for which pages are being cached
00091    Seek_t       fEOF;           // end of file
00092    ULong_t      fHighWater;     // high water mark (i.e. maximum cache size in bytes)
00093    ULong_t      fLowWater;      // low water mark (free pages till low water mark is reached)
00094    Int_t        fPageSize;      // size of cached pages
00095    Int_t        fLowLevel;      // low water mark is at low level percent of high
00096    Int_t        fDiv;           // page size divider
00097    Bool_t       fRecursive;     // true to prevent recusively calling ReadBuffer()
00098 
00099    void   SetPageSize(Int_t size);
00100    TPage *ReadPage(Seek_t offset);
00101    Int_t  WritePage(TPage *page);
00102    Int_t  FlushList(TList *list);
00103    Int_t  FlushNew();
00104    Int_t  Free(ULong_t upto);
00105 
00106 public:
00107    enum {
00108       kDfltPageSize = 0x80000,    // 512KB
00109       kDfltLowLevel = 70          // 70% of fHighWater
00110    };
00111 
00112    TCache(Int_t maxCacheSize, TFile *file, Int_t pageSize = kDfltPageSize);
00113    virtual ~TCache();
00114 
00115    Int_t GetMaxCacheSize() const { return Int_t(fHighWater / 1024 / 1024); }
00116    Int_t GetActiveCacheSize() const;
00117    Int_t GetPageSize() const { return fPageSize; }
00118    Int_t GetLowLevel() const { return fLowLevel; }
00119    Int_t Resize(Int_t maxCacheSize);
00120    void  SetLowLevel(Int_t percentOfHigh);
00121 
00122    Int_t ReadBuffer(Seek_t offset, char *buf, Int_t len);
00123    Int_t WriteBuffer(Seek_t offset, const char *buf, Int_t len);
00124    Int_t Flush();
00125 
00126    ClassDef(TCache,0)  // Page cache used for remote I/O
00127 };
00128 
00129 #endif

Generated on Thu Dec 18 14:52:16 2003 for ROOT by doxygen1.2.16