TMemStatManager.h

Go to the documentation of this file.
00001 // @(#)root/memstat:$Name$:$Id: TMemStatManager.h 30815 2009-10-20 13:49:22Z rdm $
00002 // Author: D.Bertini and M.Ivanov   18/06/2007 -- Anar Manafov (A.Manafov@gsi.de) 28/04/2008
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2008, 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_TMemStatManager
00013 #define ROOT_TMemStatManager
00014 
00015 //****************************************************************************//
00016 //
00017 //  TMemStatManager
00018 //  Memory statistic manager class
00019 //
00020 //****************************************************************************//
00021 // STD
00022 #include <map>
00023 #include <vector>
00024 #include <memory>
00025 #include <cstdlib>
00026 // ROOT
00027 #ifndef ROOT_TObject
00028 #include "TObject.h"
00029 #endif
00030 #ifndef ROOT_TTimeStamp
00031 #include "TTimeStamp.h"
00032 #endif
00033 // Memstat
00034 #ifndef ROOT_TMemStatDepend
00035 #include "TMemStatDepend.h"
00036 #endif
00037 #ifndef ROOT_TmemStatInfo
00038 #include "TMemStatInfo.h"
00039 #endif
00040 
00041 
00042 class TTree;
00043 class TMemStatStackInfo;
00044 
00045 typedef std::vector<Int_t> IntVector_t;
00046 typedef std::auto_ptr<TFile> TFilePtr_t;
00047 
00048 class TMemStatManager: public TObject
00049 {
00050    struct TMemInfo_t {
00051       void   *fAddress;    //mem address
00052       size_t  fSize;       //size of the allocated memory
00053       Int_t   fStackIndex; //index of the stack info
00054    };
00055 
00056    struct TMemTable_t {
00057       Int_t     fAllocCount;    //number of memory allocation blocks
00058       Int_t     fMemSize;       //total memory allocated size
00059       Int_t     fTableSize;     //amount of entries in the below array
00060       Int_t     fFirstFreeSpot; //where is the first free spot in the leaks array?
00061       TMemInfo_t *fLeaks;         //leak table
00062    };
00063 
00064    struct TDeleteTable_t {
00065       Int_t     fAllocCount;    //how many memory blocks do we have
00066       Int_t     fTableSize;     //amount of entries in the below array
00067       TMemInfo_t *fLeaks;         //leak table
00068    };
00069 
00070 public:
00071    typedef std::vector<TMemStatCodeInfo> CodeInfoContainer_t;
00072 
00073    enum EStatusBits {
00074       kUserDisable = BIT(18),       // user disable-enable switch  switch
00075       kStatDisable = BIT(16),       // true if disable statistic
00076       kStatRoutine = BIT(17)        // indicator inside of stat routine  (AddPointer or FreePointer)
00077    };
00078    enum EDumpTo { kTree, kSysTree };
00079 
00080    TMemStatManager();
00081    virtual ~TMemStatManager();
00082 
00083    void Enable();                              //enable memory statistic
00084    void Disable();                             //Disable memory statistic
00085    void SetAutoStamp(UInt_t sizeMem, UInt_t n, UInt_t max) {
00086       fAutoStampSize = sizeMem;
00087       fAutoStampN = n;
00088       fAutoStampDumpSize = max;
00089    }
00090    void AddStamps(const char * stampname = 0);           //add  stamps to the list of stamps for changed stacks
00091    static void SAddStamps(const Char_t * stampname);             // static version add  stamps to the list of stamps for changed stacks
00092 
00093    static TMemStatManager* GetInstance();       //get instance of class - ONLY ONE INSTANCE
00094    static void Close();                         //close MemStatManager
00095    TMemStatInfoStamp &AddStamp();                   //add one stamp to the list of stamps
00096    TMemStatCodeInfo &GetCodeInfo(void *address);
00097    UInt_t GetCodeInfoIndex(void *address) {
00098       return fCodeInfoMap[address];
00099    }
00100    void DumpTo(EDumpTo _DumpTo, Bool_t _clearStamps = kTRUE, const char * _stampName = 0);  //write current status to file
00101 
00102 public:
00103    typedef void (*StampCallback_t)(const Char_t * desription);
00104    //stack data members
00105    IntVector_t fSTHashTable; //!pointer to the hash table
00106    Int_t fCount;        //!number of entries in table
00107    Int_t fStampNumber;  //current stamp number
00108    std::vector<TMemStatStackInfo> fStackVector;            // vector with stack symbols
00109    std::vector<TMemStatInfoStamp> fStampVector;            // vector of stamp information
00110    std::vector<TTimeStamp> fStampTime;              // vector of stamp information
00111    CodeInfoContainer_t  fCodeInfoArray;          // vector with code info
00112    std::map<const void*, UInt_t> fCodeInfoMap;      //! map of code information
00113    Int_t fDebugLevel;                               //!debug level
00114    TMemStatManager::StampCallback_t fStampCallBack; //!call back function
00115    void SetUseGNUBuildinBacktrace(Bool_t _NewVal) {
00116       fUseGNUBuildinBacktrace = _NewVal;
00117    }
00118 
00119 protected:
00120    TMemStatDepend::MallocHookFunc_t fPreviousMallocHook;    //!old malloc function
00121    TMemStatDepend::FreeHookFunc_t fPreviousFreeHook;        //!old free function
00122    void Init();
00123    TMemStatStackInfo *STAddInfo(Int_t size, void **stackptrs);
00124    TMemStatStackInfo *STFindInfo(Int_t size, void **stackptrs);
00125    void RehashLeak(Int_t newSize);                  //rehash leak pointers
00126    void *AddPointer(size_t size, void *ptr = 0);    //add pointer to the table
00127    void FreePointer(void *p);                       //free pointer
00128    static void *AllocHook(size_t size, const void* /*caller*/);
00129    static void FreeHook(void* ptr, const void* /*caller*/);
00130    TMemStatInfoStamp fLastStamp;           //last written stamp
00131    TMemStatInfoStamp fCurrentStamp;        //current stamp
00132    UInt_t fAutoStampSize;           //change of size invoking STAMP
00133    UInt_t fAutoStampN;              //change of number of allocation  STAMP
00134    UInt_t fAutoStampDumpSize;       //
00135    Int_t fMinStampSize;             // the minimal size to be dumped to tree
00136    //  memory information
00137    Int_t fSize;                     //!size of hash table
00138    TMemTable_t **fLeak;               //!pointer to the hash table
00139    Int_t fAllocCount;               //!number of memory allocation blocks
00140    TDeleteTable_t fMultDeleteTable;   //!pointer to the table
00141    TFilePtr_t fDumpFile;              //!file to dump current information
00142    TTree *fDumpTree;                //!tree to dump information
00143    TTree *fDumpSysTree;             //!tree to dump information
00144    static TMemStatManager *fgInstance; // pointer to instance
00145    static void *fgStackTop;             // stack top pointer
00146 
00147    void FreeHashtable() {
00148       if (!fLeak)
00149          return;
00150 
00151       for (Int_t i = 0; i < fSize; ++i)
00152          free(fLeak[i]);
00153       free(fLeak);
00154    }
00155 
00156    Bool_t fUseGNUBuildinBacktrace;
00157 
00158    ClassDef(TMemStatManager, 1) // a manager of memstat sessions.
00159 };
00160 
00161 #endif

Generated on Tue Jul 5 14:43:47 2011 for ROOT_528-00b_version by  doxygen 1.5.1