]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliMemoryWatcher.cxx
Stand-alone library for ESD. Possibility to use only root and lidESD.so for analysis...
[u/mrichter/AliRoot.git] / STEER / AliMemoryWatcher.cxx
CommitLineData
d60522e4 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15/* $Id$ */
16//_________________________________________________________________________
00399209 17//Basic Memory Leak utility.
18// You can use this tiny class to *see* if your program is leaking.
19// Usage:
ac8cd48d 20// AliMemoryWatcher memwatcher;
00399209 21// some program loop on events here {
22// if ( nevents % x == 0 )
23// {
24// // take a sample every x events
7c8a93e6 25// memwatcher.Watch(nevents);
00399209 26// }
27// }
28// TFile f("out.root","RECREATE");
7c8a93e6 29// memwatcher.Write();
00399209 30// f.Close();
31// In the output root file you'll get 3 graphs representing
32// the evolAliPHOSon, as a function of the number of events, of :
33// - VSIZE is the virtual size (in KBytes) of your program, that is sort of
34// the total memory used
35// - RSSIZE is the resident size (in KBytes), that is, the part of your
36// program which is really in physical memory.
37// - TIME is an estimate of time per event (really it's the time elasped
38// between two calls to watch method)
39// WARNING: this is far from a bulletproof memory report (it's basically
40// using UNIX command ps -h -p [PID] -o vsize,rssize to do its job).
41// It has only been tested on Linux so far.
42// But by fitting the VSIZE by a pol1 under ROOT, you'll see right away
43// by how much your program is leaking.
d60522e4 44//*-- Author: Laurent Aphecetche(SUBATECH)
d60522e4 45// --- std system ---
b01af8c2 46class assert ;
65f03c13 47#ifdef __APPLE__
48#include <stdlib.h>
49#else
7406fd3c 50#include <malloc.h>
65f03c13 51#endif
d60522e4 52// --- AliRoot header files ---
ac8cd48d 53#include "AliMemoryWatcher.h"
d60522e4 54// --- ROOT system ---
55#include "TSystem.h"
56#include "TGraph.h"
57#include "TH2.h"
58#include "TStopwatch.h"
7c8a93e6 59#include "TError.h"
dfe7f75b 60
ac8cd48d 61ClassImp(AliMemoryWatcher)
dfe7f75b 62
d60522e4 63//_____________________________________________________________________________
ac8cd48d 64AliMemoryWatcher::AliMemoryWatcher(UInt_t maxsize)
d60522e4 65{
b01af8c2 66 //ctor
d60522e4 67 fMAXSIZE=maxsize;
7406fd3c 68 fUseMallinfo = true;
d60522e4 69 fPID = gSystem->GetPid();
70 sprintf(fCmd,"ps -h -p %d -o vsize,rssize",fPID);
b01af8c2 71 fX = new Int_t[fMAXSIZE];
72 fVSIZE = new Int_t[fMAXSIZE];
73 fRSSIZE = new Int_t[fMAXSIZE];
74 fTIME = new Double_t[fMAXSIZE];
d60522e4 75 fSize=0;
76 fDisabled=false;
77 fTimer=0;
78}
d60522e4 79//_____________________________________________________________________________
d1898505 80AliMemoryWatcher::AliMemoryWatcher(const AliMemoryWatcher& mw):
81 TObject(mw)
00399209 82{
83 //copy ctor
84 fMAXSIZE = mw.fMAXSIZE ;
7406fd3c 85 fUseMallinfo = mw.fUseMallinfo;
00399209 86 fPID = mw.fPID ;
87 strcpy(fCmd, mw.fCmd) ;
88 fX = new Int_t[fMAXSIZE];
89 fVSIZE = new Int_t[fMAXSIZE];
90 fRSSIZE = new Int_t[fMAXSIZE];
91 fTIME = new Double_t[fMAXSIZE];
92 fSize=0;
93 fDisabled=false;
94 fTimer=0;
95}
96//_____________________________________________________________________________
ac8cd48d 97AliMemoryWatcher::~AliMemoryWatcher()
d60522e4 98{
2a6e66b9 99 // dtor
d60522e4 100 delete[] fVSIZE;
101 delete[] fRSSIZE;
102 delete[] fX;
103 delete[] fTIME;
104 delete fTimer;
105}
d60522e4 106//_____________________________________________________________________________
ac8cd48d 107void AliMemoryWatcher::Watch(Int_t x)
d60522e4 108{
b01af8c2 109 // Sets the point where CPU parameters have to be monitored
d60522e4 110 if ( !fDisabled && fSize < fMAXSIZE ) {
d60522e4 111 if ( fSize==0 ) {
112 assert(fTimer==0);
113 fTimer = new TStopwatch;
114 fTimer->Start(true);
115 fTimer->Stop();
116 }
7406fd3c 117 if ( fUseMallinfo ) {
1844c927 118#ifdef __linux
7406fd3c 119 static struct mallinfo meminfo;
120 meminfo = mallinfo();
d60522e4 121 fX[fSize] = x ;
7406fd3c 122 fVSIZE[fSize] = (meminfo.hblkhd + meminfo.uordblks) / 1024;
123 fRSSIZE[fSize] = meminfo.uordblks / 1024;
d60522e4 124 fTIME[fSize] = fTimer->CpuTime();
125 fSize++;
1844c927 126#else
127 ::Fatal("Watch","Please SetUseMallinfo to kFALSE on this system");
128#endif
7406fd3c 129 } else {
130 static Int_t vsize, rssize;
131 static FILE* pipe = 0;
132 pipe = popen(fCmd,"r");
133 if ( pipe ) {
134
135 fscanf(pipe,"%d %d",&vsize,&rssize);
136
137 fX[fSize] = x ;
138 fVSIZE[fSize] = vsize ;
139 fRSSIZE[fSize] = rssize ;
140 fTIME[fSize] = fTimer->CpuTime();
141 fSize++;
142 }
143 assert(pclose(pipe)!=-1);
d60522e4 144 }
d60522e4 145 fTimer->Start(true);
d60522e4 146 }
147 else {
148 fDisabled=true;
21cd0c07 149 Error("watch", "I'm full !" ) ;
d60522e4 150 }
151}
d60522e4 152//_____________________________________________________________________________
153TGraph*
ac8cd48d 154AliMemoryWatcher::GraphVSIZE(void)
d60522e4 155{
b01af8c2 156 // Fills the graph with the virtual memory sized used
d60522e4 157 TGraph* g = 0;
00399209 158 if ( Size() )
d60522e4 159 {
00399209 160 g = new TGraph(Size());
b01af8c2 161 Int_t i ;
162 for (i=0; i < g->GetN(); i++ ) {
d60522e4 163 g->SetPoint(i,X(i),VSIZE(i));
164 }
165 }
166 return g;
167}
d60522e4 168//_____________________________________________________________________________
169TGraph*
ac8cd48d 170AliMemoryWatcher::GraphRSSIZE(void)
d60522e4 171{
b01af8c2 172 // Fills the graph with the real memory sized used
d60522e4 173 TGraph* g = 0;
00399209 174 if ( Size() )
d60522e4 175 {
00399209 176 g = new TGraph(Size());
b01af8c2 177 Int_t i ;
178 for (i=0; i < g->GetN(); i++ ) {
d60522e4 179 g->SetPoint(i,X(i),RSSIZE(i));
180 }
181 }
182 return g;
183}
d60522e4 184//_____________________________________________________________________________
185TGraph*
ac8cd48d 186AliMemoryWatcher::GraphTIME(void)
d60522e4 187{
b01af8c2 188 // Fills the raph with the used CPU time
d60522e4 189 TGraph* g = 0;
00399209 190 if ( Size() )
d60522e4 191 {
00399209 192 g = new TGraph(Size());
b01af8c2 193 Int_t i ;
194 for (i=0; i < g->GetN(); i++ ) {
d60522e4 195 g->SetPoint(i,X(i),TIME(i));
196 }
197 }
198 return g;
199}
d60522e4 200//_____________________________________________________________________________
201TH2*
ac8cd48d 202AliMemoryWatcher::Frame(void) const
d60522e4 203{
b01af8c2 204 //creates the frame histo in which the graphs will be plotted
205 Double_t xmin=1E30;
206 Double_t xmax=0;
207 Double_t ymin=1;
208 Double_t ymax=0;
209 UInt_t i ;
00399209 210 for (i=0; i < Size() ; i++ ) {
d60522e4 211 if ( X(i) < xmin ) xmin = X(i);
212 if ( X(i) > xmax ) xmax = X(i);
b01af8c2 213 Double_t y = VSIZE(i)+RSSIZE(i);
d60522e4 214 if ( y > ymax ) ymax = y;
d60522e4 215 if ( VSIZE(i) < ymin ) ymin = VSIZE(i);
216 if ( RSSIZE(i) < ymin ) ymin = RSSIZE(i);
217 }
d60522e4 218 TH2F* h = new TH2F("frame","",10,xmin,xmax,10,ymin*0.8,ymax*1.2);
d60522e4 219 return h;
220}
d60522e4 221//_____________________________________________________________________________
6c4904c2 222Int_t
223AliMemoryWatcher::Write(const char *, Int_t, Int_t)
d60522e4 224{
b01af8c2 225 // Stores the graphs in a file
226 if ( GraphVSIZE() ) GraphVSIZE()->Write("VSIZE",TObject::kOverwrite);
00399209 227 if ( GraphRSSIZE() ) GraphRSSIZE() ->Write("RSSIZE",TObject::kOverwrite);
b01af8c2 228 if ( GraphTIME() ) GraphTIME()->Write("TIME",TObject::kOverwrite);
6c4904c2 229 return 0;
4ed9ed00 230}