Logo ROOT  
Reference Guide
TMemStat.cxx
Go to the documentation of this file.
1// @(#)root/memstat:$Id$
2// Author: Anar Manafov (A.Manafov@gsi.de) and Rene Brun 23/09/2010
3
4/*************************************************************************
5* Copyright (C) 1995-2010, Rene Brun and Fons Rademakers. *
6* All rights reserved. *
7* *
8* For the licensing terms see $ROOTSYS/LICENSE. *
9* For the list of contributors see $ROOTSYS/README/CREDITS. *
10*************************************************************************/
11
12//___________________________________________________________________________
13// TMemStat records all the calls to malloc and free and write a TTree
14// with the position where the memory is allocated/freed , as well as
15// the number of bytes.
16//
17// To use the class TMemStat, add the following statement at the beginning
18// of your script or program
19// TMemStat mm("gnubuiltin");
20// or in an interactive session do something like:
21// root > TMemStat mm("gnubuiltin");
22// root > .x somescript.C
23// root > .q
24//
25// another (may be more practical way) is to modify $ROOTSYS/etc/system.rootrc
26// and activate the variable
27// Root.TMemStat: 1
28//
29// The file collected by TMemStat is named memstat_ProcessID and can be analyzed and results shown
30// by executing the static function Show.
31// When TMemStat is active it recors every call to malloc/free in a ROOT Tree.
32// You must be careful when running jobs with many millions (or more) of calls
33// to malloc/free because the generated Tree may become very large.
34// The TMemStat constructor TMemStat(const char* system, Int_t buffersize, Int_t maxcalls)
35// has its 3 arguments optional:
36// -system refers to the internal algorithm to compute the back traces.
37// the recommended value is "gnubuiltin"
38// -buffersize is the number of calls to malloc or free that can be stored in one memory buffer.
39// when the buffer is full, the calls to malloc/free pointing to the same location
40// are eliminated and not written to the final Tree. The default value 100000
41// is such that between 50 and 90% of the calls are eliminated depending on the application.
42// You can set buffersize <=1 to keep every single call to malloc/free.
43// -maxcalls can set a limit for the maximum number of calls to be registered in the Tree.
44// The default value is 5000000.
45// The 3 arguments can be set in $ROOTSYS/etc/system.rootrc
46// Root.TMemStat.system gnubuiltin
47// Root.TMemStat.buffersize 100000
48// Root.TMemStat.maxcalls 5000000
49//
50// TMemStat::Show creates 3 canvases.
51// -In canvas1 it displays a dynamic histogram showing for pages (10 kbytes by default)
52// the percentage of the page used.
53// A summary pave shows the total memory still in use when the TMemStat object
54// goes out of scope and the average occupancy of the pages.
55// The average occupancy gives a good indication of the memory fragmentation.
56//
57// -In canvas2 it displays the histogram of memory leaks in decreasing order.
58// when moving the mouse on this canvas, a tooltip shows the backtrace for the leak
59// in the bin below the mouse.
60//
61// -In canvas3 it displays the histogram of the nbigleaks largest leaks (default is 20)
62// for each leak, the number of allocs and average alloc size is shown.
63//
64//
65// Simply do:
66// root > TMemStat::Show()
67// or specifying arguments
68// root > TMemStat::Show(0.1,20,"mydir/mymemstat.root");
69//
70// The first argument to Show is the percentage of the time of the original job
71// that produced the file after which the display is updated. By default update=0.1,
72// ie 10 time intervals will be shown.
73// The second argument is nbigleaks.
74// The third argument is the imput file name (result of TMemStat).
75// If this argument is omitted, Show will take the most recent file
76// generated by TMemStat.
77//
78// You can restrict the address range to be analyzed via TMemStatShow::SetAddressRange
79// You can restrict the entry range to be analyzed via TMemStatShow::SetEntryRange
80//
81//___________________________________________________________________________
82
83#include "TROOT.h"
84#include "TDirectory.h"
85#include "TMemStat.h"
86#include "TMemStatBacktrace.h"
87#include "TMemStatMng.h"
88#include "TMemStatHelpers.h"
89
90#if defined(__GNUC__) && !defined(__clang__)
91#if __GNUC__ > 5
92#pragma GCC diagnostic ignored "-Wframe-address"
93#endif
94#endif
95
97
98using namespace std;
99using namespace Memstat;
100
102
103////////////////////////////////////////////////////////////////////////////////
104/// Supported options:
105/// "gnubuiltin" - if declared, then MemStat will use gcc build-in function,
106/// otherwise glibc backtrace will be used
107///
108/// Note: Currently MemStat uses a hard-coded output file name (for writing) = "memstat.root";
109
110TMemStat::TMemStat(Option_t* option, Int_t buffersize, Int_t maxcalls): fIsActive(kFALSE)
111{
112 // It marks the highest used stack address.
114
115 //preserve context. When exiting will restore the current directory
116 TDirectory::TContext context;
117
118 Bool_t useBuiltin = kTRUE;
119 // Define string in a scope, so that the deletion of it will be not recorded by YAMS
120 {
121 string opt(option);
122 transform(opt.begin(), opt.end(), opt.begin(),
124
125 useBuiltin = (opt.find("gnubuiltin") != string::npos) ? kTRUE : kFALSE;
126 }
127
128 TMemStatMng::GetInstance()->SetUseGNUBuiltinBacktrace(useBuiltin);
129 TMemStatMng::GetInstance()->SetBufferSize(buffersize);
130 TMemStatMng::GetInstance()->SetMaxCalls(maxcalls);
131 TMemStatMng::GetInstance()->Enable();
132 // set this variable only if "NEW" mode is active
134
135}
136
137////////////////////////////////////////////////////////////////////////////////
138///destructor
139
141{
142 if (fIsActive) {
143 TMemStatMng::GetInstance()->Disable();
144 TMemStatMng::GetInstance()->Close();
145 }
146}
147
148////////////////////////////////////////////////////////////////////////////////
149///close the TMemStat manager
150
152{
153 TMemStatMng::Close();
154}
155
156////////////////////////////////////////////////////////////////////////////////
157///Disable memory statistics
158
160{
161 TMemStatMng::GetInstance()->Disable();
162}
163
164////////////////////////////////////////////////////////////////////////////////
165///Enable memory statistics
166
168{
169 TMemStatMng::GetInstance()->Enable();
170}
171
172////////////////////////////////////////////////////////////////////////////////
173///Show results
174
175void TMemStat::Show(Double_t update, Int_t nbigleaks, const char* fname)
176{
177 TString action = TString::Format("TMemStatShow::Show(%g,%d,\"%s\");",update,nbigleaks,fname);
178 gROOT->ProcessLine(action);
179}
static void update(gsl_integration_workspace *workspace, double a1, double b1, double area1, double error1, double a2, double b2, double area2, double error2)
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
const Bool_t kTRUE
Definition: RtypesCore.h:87
const char Option_t
Definition: RtypesCore.h:62
#define ClassImp(name)
Definition: Rtypes.h:365
#define _GET_CALLER_FRAME_ADDR
_INIT_TOP_STACK
Definition: TMemStat.cxx:101
#define gROOT
Definition: TROOT.h:415
Small helper to keep current directory context.
Definition: TDirectory.h:41
Bool_t fIsActive
Definition: TMemStat.h:18
virtual void Disable()
Disable memory statistics.
Definition: TMemStat.cxx:159
virtual ~TMemStat()
destructor
Definition: TMemStat.cxx:140
static void Close()
close the TMemStat manager
Definition: TMemStat.cxx:151
TMemStat(Option_t *option="read", Int_t buffersize=10000, Int_t maxcalls=5000000)
Supported options: "gnubuiltin" - if declared, then MemStat will use gcc build-in function,...
Definition: TMemStat.cxx:110
virtual void Enable()
Enable memory statistics.
Definition: TMemStat.cxx:167
static void Show(Double_t update=0.1, Int_t nbigleaks=20, const char *fname="*")
Show results.
Definition: TMemStat.cxx:175
Basic string class.
Definition: TString.h:131
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition: TString.cxx:2311