ROOT logo
// @(#)root/memstat:$Name$:$Id: TMemStatInfo.h 30815 2009-10-20 13:49:22Z rdm $
// Author: D.Bertini and M.Ivanov   18/06/2007 -- Anar Manafov (A.Manafov@gsi.de) 28/04/2008

/*************************************************************************
 * Copyright (C) 1995-2008, 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_TMemStatInfo
#define ROOT_TMemStatInfo


//****************************************************************************//
//
//
//  Memory statistic information
//                   TMemStatInfoStamp
//                   TMemStatCodeInfo
//                   TMemStatStackInfo
//****************************************************************************//

// STD
#include <iosfwd>
#include <iomanip>
#include <sstream>
//ROOT
#ifndef ROOT_TROOT
#include "TROOT.h"
#endif
// Memstat
#ifndef ROOT_TMemStatHelpers
#include "TMemStatHelpers.h"
#endif

class TMemStatStackInfo;
class TMemStatManager;



const int fields_length[] = {18, 15, 19, 12, 8};


class TMemStatInfoStamp: public TObject
{
public:
   enum EStampType { kCode, kStack };
   TMemStatInfoStamp();              //stamp of memory usage information
   virtual ~TMemStatInfoStamp();
   void        Print(Option_t* option = "") const;
   Bool_t      Equal(TMemStatInfoStamp&stamp);
   void     Inc(Int_t memSize);  //increment counters -when memory allocated
   void     Dec(Int_t memSize);  //decrement counters -when memory deallocated
   friend std::ostream& operator<< (std::ostream &_ostream, const TMemStatInfoStamp &_this);

   Long64_t    fTotalAllocCount;  //total number of allocation for stack sequence
   Long64_t    fTotalAllocSize;   //total size of allocated memory
   Int_t       fAllocCount;       //current number of allocation-deallocation
   Int_t       fAllocSize;        //current allocated size
   Int_t       fStampNumber;      //stamp number
   Int_t       fID;               //code ID number
   Short_t     fStampType;        //stamp Type

   ClassDef(TMemStatInfoStamp, 1) // information about stamps
};


class TMemStatCodeInfo: public TObject
{
public:
   TMemStatCodeInfo();              // store information about line of code
   void SetInfo(void *info);
   virtual ~TMemStatCodeInfo() {
   }
   void MakeStamp(Int_t stampNumber);
   void Print(Option_t* option = "") const;
   void Inc(Int_t memSize);  //increment counters -when memory allocated
   void Dec(Int_t memSize);  //decrement counters -when memory deallocated
   friend std::ostream& operator<< (std::ostream &_ostream, const TMemStatCodeInfo &_this);

   TMemStatInfoStamp  fLastStamp;     // last time stamp info
   TMemStatInfoStamp  fCurrentStamp;  // current  time stamp info
   TMemStatInfoStamp  fMaxStampSize;  // max current size stamp
   TMemStatInfoStamp  fMaxStamp;      // max current size stamp
   Long64_t    fCode;          //pointer to the code
   TString     fInfo;          //pointer description
   TString     fFunction;      //function
   TString     fLib;           //library
   UInt_t      fCodeID;        //ID number

   ClassDef(TMemStatCodeInfo, 1) // a code information structure
};


class TMemStatStackInfo: public TObject
{
public:
   enum {kStackHistorySize = 50};
   UInt_t      fSize;               // size of the stack
   TMemStatInfoStamp  fLastStamp;          // last time stamp info
   TMemStatInfoStamp  fCurrentStamp;       // current  time stamp info
   TMemStatInfoStamp  fMaxStampSize;       // max current size stamp
   TMemStatInfoStamp  fMaxStamp;           // max current size stamp
   Int_t       fNextHash;           // index  to the next info for given hash value
   void      **fStackSymbols;       //!Stack Symbols
   UInt_t     *fSymbolIndexes;      //[fSize]symbol indexes
   UInt_t      fStackID;            //ID number

   TMemStatStackInfo();
   virtual ~TMemStatStackInfo() {}
   void     Init(Int_t stacksize, void **stackptrs,  TMemStatManager *manager, Int_t ID); //initialization
   void     Inc(Int_t memSize, TMemStatManager *manager);  //increment counters -when memory allocated
   void     Dec(Int_t memSize, TMemStatManager *manager);  //decrement counters -when memory deallocated
   ULong_t  Hash() const;
   Int_t    Equal(UInt_t size, void **ptr);
   void    *StackAt(UInt_t i);
   //   TMemStatStackInfo *Next();    //index of the next entries
   void     MakeStamp(Int_t stampNumber);
   static inline ULong_t HashStack(UInt_t size, void **ptr) {
      return  TString::Hash(ptr, size*sizeof(void*));
   }
   friend std::ostream& operator << (std::ostream &_ostream, const TMemStatStackInfo &_this);

   ClassDef(TMemStatStackInfo, 1) // a stack information structure
};


inline void TMemStatInfoStamp::Inc(int memSize)
{
   fTotalAllocCount += 1;
   fTotalAllocSize  += memSize;
   fAllocCount += 1;
   fAllocSize  += memSize;
}
inline void TMemStatInfoStamp::Dec(int memSize)
{
   fAllocCount -= 1;
   fAllocSize  -= memSize;
}
inline void TMemStatCodeInfo::Dec(int memSize)
{
   fCurrentStamp.Dec(memSize);
}

inline ULong_t TMemStatStackInfo::Hash() const
{
   return HashStack(fSize, fStackSymbols);
}

inline void *TMemStatStackInfo::StackAt(UInt_t i)
{
   return i < fSize ? fStackSymbols[i] : 0;
}


//______________________________________________________________________________
template<class T>
std::ostream& StreemCurrAndMax(std::ostream &_ostream, const T &_this)
{
   std::ios::fmtflags old_flags(_ostream.flags(std::ios::left));

   _ostream << "\n"
   << std::setw(fields_length[0]) << ""
   << std::setw(fields_length[1]) << "TotalCount"
   << std::setw(fields_length[2]) << "TotalSize"
   << std::setw(fields_length[3]) << "Count"
   << std::setw(fields_length[4]) << "Size" << std::endl;

   // Setting a bit nicer formating
   // example: instead of 20600000 print 20,600,000
   std::locale loc("");
   std::locale loc_previous = _ostream.imbue(loc);
   _ostream.precision(2);
   _ostream << std::fixed;

   _ostream << std::setw(fields_length[0]) << "Current stamp";
   _ostream
   << std::setw(fields_length[1]) << _this.fCurrentStamp.fTotalAllocCount
   << std::setw(fields_length[2]) << Memstat::dig2bytes(_this.fCurrentStamp.fTotalAllocSize)
   << std::setw(fields_length[3]) << _this.fCurrentStamp.fAllocCount
   << std::setw(fields_length[4]) << Memstat::dig2bytes(_this.fCurrentStamp.fAllocSize) << std::endl;

   _ostream << std::setw(fields_length[0]) << "Max Alloc stamp";
   _ostream
   << std::setw(fields_length[1]) << _this.fMaxStamp.fTotalAllocCount
   << std::setw(fields_length[2]) << Memstat::dig2bytes(_this.fMaxStamp.fTotalAllocSize)
   << std::setw(fields_length[3]) << _this.fMaxStamp.fAllocCount
   << std::setw(fields_length[4]) << Memstat::dig2bytes(_this.fMaxStamp.fAllocSize) << std::endl;

   _ostream << std::setw(fields_length[0]) << "Max Size stamp";
   _ostream
   << std::setw(fields_length[1]) << _this.fMaxStampSize.fTotalAllocCount
   << std::setw(fields_length[2]) << Memstat::dig2bytes(_this.fMaxStampSize.fTotalAllocSize)
   << std::setw(fields_length[3]) << _this.fMaxStampSize.fAllocCount
   << std::setw(fields_length[4]) << Memstat::dig2bytes(_this.fMaxStampSize.fAllocSize);

   _ostream.imbue(loc_previous);
   _ostream.flags(old_flags);

   return _ostream;
}


#endif
 TMemStatInfo.h:1
 TMemStatInfo.h:2
 TMemStatInfo.h:3
 TMemStatInfo.h:4
 TMemStatInfo.h:5
 TMemStatInfo.h:6
 TMemStatInfo.h:7
 TMemStatInfo.h:8
 TMemStatInfo.h:9
 TMemStatInfo.h:10
 TMemStatInfo.h:11
 TMemStatInfo.h:12
 TMemStatInfo.h:13
 TMemStatInfo.h:14
 TMemStatInfo.h:15
 TMemStatInfo.h:16
 TMemStatInfo.h:17
 TMemStatInfo.h:18
 TMemStatInfo.h:19
 TMemStatInfo.h:20
 TMemStatInfo.h:21
 TMemStatInfo.h:22
 TMemStatInfo.h:23
 TMemStatInfo.h:24
 TMemStatInfo.h:25
 TMemStatInfo.h:26
 TMemStatInfo.h:27
 TMemStatInfo.h:28
 TMemStatInfo.h:29
 TMemStatInfo.h:30
 TMemStatInfo.h:31
 TMemStatInfo.h:32
 TMemStatInfo.h:33
 TMemStatInfo.h:34
 TMemStatInfo.h:35
 TMemStatInfo.h:36
 TMemStatInfo.h:37
 TMemStatInfo.h:38
 TMemStatInfo.h:39
 TMemStatInfo.h:40
 TMemStatInfo.h:41
 TMemStatInfo.h:42
 TMemStatInfo.h:43
 TMemStatInfo.h:44
 TMemStatInfo.h:45
 TMemStatInfo.h:46
 TMemStatInfo.h:47
 TMemStatInfo.h:48
 TMemStatInfo.h:49
 TMemStatInfo.h:50
 TMemStatInfo.h:51
 TMemStatInfo.h:52
 TMemStatInfo.h:53
 TMemStatInfo.h:54
 TMemStatInfo.h:55
 TMemStatInfo.h:56
 TMemStatInfo.h:57
 TMemStatInfo.h:58
 TMemStatInfo.h:59
 TMemStatInfo.h:60
 TMemStatInfo.h:61
 TMemStatInfo.h:62
 TMemStatInfo.h:63
 TMemStatInfo.h:64
 TMemStatInfo.h:65
 TMemStatInfo.h:66
 TMemStatInfo.h:67
 TMemStatInfo.h:68
 TMemStatInfo.h:69
 TMemStatInfo.h:70
 TMemStatInfo.h:71
 TMemStatInfo.h:72
 TMemStatInfo.h:73
 TMemStatInfo.h:74
 TMemStatInfo.h:75
 TMemStatInfo.h:76
 TMemStatInfo.h:77
 TMemStatInfo.h:78
 TMemStatInfo.h:79
 TMemStatInfo.h:80
 TMemStatInfo.h:81
 TMemStatInfo.h:82
 TMemStatInfo.h:83
 TMemStatInfo.h:84
 TMemStatInfo.h:85
 TMemStatInfo.h:86
 TMemStatInfo.h:87
 TMemStatInfo.h:88
 TMemStatInfo.h:89
 TMemStatInfo.h:90
 TMemStatInfo.h:91
 TMemStatInfo.h:92
 TMemStatInfo.h:93
 TMemStatInfo.h:94
 TMemStatInfo.h:95
 TMemStatInfo.h:96
 TMemStatInfo.h:97
 TMemStatInfo.h:98
 TMemStatInfo.h:99
 TMemStatInfo.h:100
 TMemStatInfo.h:101
 TMemStatInfo.h:102
 TMemStatInfo.h:103
 TMemStatInfo.h:104
 TMemStatInfo.h:105
 TMemStatInfo.h:106
 TMemStatInfo.h:107
 TMemStatInfo.h:108
 TMemStatInfo.h:109
 TMemStatInfo.h:110
 TMemStatInfo.h:111
 TMemStatInfo.h:112
 TMemStatInfo.h:113
 TMemStatInfo.h:114
 TMemStatInfo.h:115
 TMemStatInfo.h:116
 TMemStatInfo.h:117
 TMemStatInfo.h:118
 TMemStatInfo.h:119
 TMemStatInfo.h:120
 TMemStatInfo.h:121
 TMemStatInfo.h:122
 TMemStatInfo.h:123
 TMemStatInfo.h:124
 TMemStatInfo.h:125
 TMemStatInfo.h:126
 TMemStatInfo.h:127
 TMemStatInfo.h:128
 TMemStatInfo.h:129
 TMemStatInfo.h:130
 TMemStatInfo.h:131
 TMemStatInfo.h:132
 TMemStatInfo.h:133
 TMemStatInfo.h:134
 TMemStatInfo.h:135
 TMemStatInfo.h:136
 TMemStatInfo.h:137
 TMemStatInfo.h:138
 TMemStatInfo.h:139
 TMemStatInfo.h:140
 TMemStatInfo.h:141
 TMemStatInfo.h:142
 TMemStatInfo.h:143
 TMemStatInfo.h:144
 TMemStatInfo.h:145
 TMemStatInfo.h:146
 TMemStatInfo.h:147
 TMemStatInfo.h:148
 TMemStatInfo.h:149
 TMemStatInfo.h:150
 TMemStatInfo.h:151
 TMemStatInfo.h:152
 TMemStatInfo.h:153
 TMemStatInfo.h:154
 TMemStatInfo.h:155
 TMemStatInfo.h:156
 TMemStatInfo.h:157
 TMemStatInfo.h:158
 TMemStatInfo.h:159
 TMemStatInfo.h:160
 TMemStatInfo.h:161
 TMemStatInfo.h:162
 TMemStatInfo.h:163
 TMemStatInfo.h:164
 TMemStatInfo.h:165
 TMemStatInfo.h:166
 TMemStatInfo.h:167
 TMemStatInfo.h:168
 TMemStatInfo.h:169
 TMemStatInfo.h:170
 TMemStatInfo.h:171
 TMemStatInfo.h:172
 TMemStatInfo.h:173
 TMemStatInfo.h:174
 TMemStatInfo.h:175
 TMemStatInfo.h:176
 TMemStatInfo.h:177
 TMemStatInfo.h:178
 TMemStatInfo.h:179
 TMemStatInfo.h:180
 TMemStatInfo.h:181
 TMemStatInfo.h:182
 TMemStatInfo.h:183
 TMemStatInfo.h:184
 TMemStatInfo.h:185
 TMemStatInfo.h:186
 TMemStatInfo.h:187
 TMemStatInfo.h:188
 TMemStatInfo.h:189
 TMemStatInfo.h:190
 TMemStatInfo.h:191
 TMemStatInfo.h:192
 TMemStatInfo.h:193
 TMemStatInfo.h:194
 TMemStatInfo.h:195
 TMemStatInfo.h:196
 TMemStatInfo.h:197
 TMemStatInfo.h:198
 TMemStatInfo.h:199
 TMemStatInfo.h:200
 TMemStatInfo.h:201
 TMemStatInfo.h:202
 TMemStatInfo.h:203
 TMemStatInfo.h:204
 TMemStatInfo.h:205
 TMemStatInfo.h:206