TMemStatMng.h

Go to the documentation of this file.
00001 // @(#)root/memstat:$Id: TMemStatMng.h 36382 2010-10-20 12:27:40Z brun $
00002 // Author: Anar Manafov (A.Manafov@gsi.de) 2008-03-02
00003 
00004 /*************************************************************************
00005 * Copyright (C) 1995-2010, 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 #ifndef ROOT_TMemStatMng
00012 #define ROOT_TMemStatMng
00013 
00014 // STD
00015 #include <map>
00016 // ROOT
00017 #include "TTimeStamp.h"
00018 // Memstat
00019 #include "TMemStatHook.h"
00020 #include "TMemStatDef.h"
00021 
00022 
00023 class TTree;
00024 class TFile;
00025 class TH1I;
00026 class TObjArray;
00027 
00028 namespace memstat {
00029 
00030    class TMemStatFAddrContainer {
00031       typedef std::map<ULong_t, Int_t> Container_t;
00032       typedef Container_t::iterator pos_type;
00033       typedef Container_t::value_type value_type;
00034 
00035    public:
00036       bool add(ULong_t addr, Int_t idx) {
00037          std::pair<pos_type, bool> ret = fContainer.insert(value_type(addr, idx));
00038          return (ret.second);
00039       }
00040 
00041       Int_t find(ULong_t addr) {
00042          pos_type iter = fContainer.find(addr);
00043          if(fContainer.end() == iter)
00044             return -1;
00045 
00046          return iter->second;
00047       }
00048 
00049    private:
00050       Container_t fContainer;
00051    };
00052 
00053    const UShort_t g_digestSize = 16;
00054    struct SCustomDigest {
00055       SCustomDigest() {
00056          memset(fValue, 0, g_digestSize);
00057       }
00058       SCustomDigest(UChar_t _val[g_digestSize]) {
00059          memcpy(fValue, _val, g_digestSize);
00060       }
00061 
00062       UChar_t fValue[g_digestSize];
00063    };
00064    inline bool operator< (const SCustomDigest &a, const SCustomDigest &b)
00065    {
00066       for(int i = 0; i < g_digestSize; ++i) {
00067          if(a.fValue[i] != b.fValue[i])
00068             return (a.fValue[i] < b.fValue[i]);
00069       }
00070       return false;
00071    }
00072 
00073 
00074    class TMemStatMng: public TObject {
00075       typedef std::map<SCustomDigest, Int_t> CRCSet_t;
00076 
00077    private:
00078       TMemStatMng();
00079       virtual ~TMemStatMng();
00080 
00081    public:
00082       void Enable();                       //enable memory statistic
00083       void Disable();                      //Disable memory statistic
00084       static TMemStatMng* GetInstance();   //get instance of class - ONLY ONE INSTANCE
00085       static void Close();                 //close MemStatManager
00086       void SetBufferSize(Int_t buffersize);
00087       void SetMaxCalls(Int_t maxcalls);
00088       
00089    public:
00090       //stack data members
00091       void SetUseGNUBuiltinBacktrace(Bool_t newVal) {
00092          fUseGNUBuiltinBacktrace = newVal;
00093       }
00094 
00095    protected:
00096 #if !defined(__APPLE__)
00097       TMemStatHook::MallocHookFunc_t fPreviousMallocHook;    //!old malloc function
00098       TMemStatHook::FreeHookFunc_t fPreviousFreeHook;        //!old free function
00099 #endif
00100       void Init();
00101       void AddPointer(void *ptr, Int_t size);    //add pointer to the table
00102       void FillTree();
00103       static void *AllocHook(size_t size, const void* /*caller*/);
00104       static void FreeHook(void* ptr, const void* /*caller*/);
00105       static void MacAllocHook(void *ptr, size_t size);
00106       static void MacFreeHook(void *ptr);
00107       Int_t generateBTID(UChar_t *CRCdigest, Int_t stackEntries,
00108                          void **stackPointers);
00109 
00110 
00111       //  memory information
00112       TFile* fDumpFile;               //!file to dump current information
00113       TTree *fDumpTree;               //!tree to dump information
00114       static TMemStatMng *fgInstance; // pointer to instance
00115       static void *fgStackTop;        // stack top pointer
00116 
00117       Bool_t fUseGNUBuiltinBacktrace;
00118       TTimeStamp fTimeStamp;
00119       Double_t  fBeginTime;   //time when monitoring starts
00120       ULong64_t fPos;         //position in memory where alloc/free happens
00121       Int_t     fTimems;      //10000*(current time - begin time)
00122       Int_t     fNBytes;      //number of bytes allocated/freed
00123       Int_t     fBtID;        //back trace identifier
00124       Int_t     fMaxCalls;    //max number of malloc/frees to register in the output Tree
00125       Int_t     fBufferSize;  //max number of malloc/free to keep in the buffer
00126       Int_t     fBufN;        //current number of alloc or free in the buffer
00127       ULong64_t *fBufPos;     //position in memory where alloc/free happens
00128       Int_t     *fBufTimems;  //10000*(current time - begin time)
00129       Int_t     *fBufNBytes;  //number of bytes allocated/freed
00130       Int_t     *fBufBtID;    //back trace identifier
00131       Int_t     *fIndex;      //array to sort fBufPos
00132       Bool_t    *fMustWrite;  //flag to write or not the entry
00133             
00134    private:
00135       TMemStatFAddrContainer fFAddrs;
00136       TObjArray *fFAddrsList;
00137       TH1I *fHbtids;
00138       CRCSet_t fBTChecksums;
00139       Int_t fBTCount;
00140       // for Debug. A counter of all (de)allacations.
00141       UInt_t  fBTIDCount;
00142       TNamed *fSysInfo;
00143 
00144       ClassDef(TMemStatMng, 0)   // a manager of memstat sessions.
00145    };
00146 
00147 }
00148 
00149 #endif

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