1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
16 //_________________________________________________________________________
17 //Basic Memory Leak utility.
18 // You can use this tiny class to *see* if your program is leaking.
20 // AliPHOSMemoryWatcher memwatcher;
21 // some program loop on events here {
22 // if ( nevents % x == 0 )
24 // // take a sample every x events
25 // memwatcher.watch(nevents);
28 // TFile f("out.root","RECREATE");
29 // memwatcher.write();
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.
44 //*-- Author: Laurent Aphecetche(SUBATECH)
47 // --- AliRoot header files ---
48 #include "AliPHOSMemoryWatcher.h"
49 // --- ROOT system ---
53 #include "TStopwatch.h"
55 ClassImp(AliPHOSMemoryWatcher)
57 //_____________________________________________________________________________
58 AliPHOSMemoryWatcher::AliPHOSMemoryWatcher(UInt_t maxsize)
62 fPID = gSystem->GetPid();
63 sprintf(fCmd,"ps -h -p %d -o vsize,rssize",fPID);
64 fX = new Int_t[fMAXSIZE];
65 fVSIZE = new Int_t[fMAXSIZE];
66 fRSSIZE = new Int_t[fMAXSIZE];
67 fTIME = new Double_t[fMAXSIZE];
72 //_____________________________________________________________________________
73 AliPHOSMemoryWatcher::AliPHOSMemoryWatcher(AliPHOSMemoryWatcher& mw)
76 fMAXSIZE = mw.fMAXSIZE ;
78 strcpy(fCmd, mw.fCmd) ;
79 fX = new Int_t[fMAXSIZE];
80 fVSIZE = new Int_t[fMAXSIZE];
81 fRSSIZE = new Int_t[fMAXSIZE];
82 fTIME = new Double_t[fMAXSIZE];
87 //_____________________________________________________________________________
88 AliPHOSMemoryWatcher::~AliPHOSMemoryWatcher()
97 //_____________________________________________________________________________
98 void AliPHOSMemoryWatcher::Watch(Int_t x)
100 // Sets the point where CPU parameters have to be monitored
101 if ( !fDisabled && fSize < fMAXSIZE ) {
104 fTimer = new TStopwatch;
108 static Int_t vsize, rssize;
109 static FILE* pipe = 0;
110 pipe = popen(fCmd,"r");
113 fscanf(pipe,"%d %d",&vsize,&rssize);
116 fVSIZE[fSize] = vsize ;
117 fRSSIZE[fSize] = rssize ;
118 fTIME[fSize] = fTimer->CpuTime();
121 assert(pclose(pipe)!=-1);
126 Error("watch", "I'm full !" ) ;
129 //_____________________________________________________________________________
131 AliPHOSMemoryWatcher::GraphVSIZE(void)
133 // Fills the graph with the virtual memory sized used
137 g = new TGraph(Size());
139 for (i=0; i < g->GetN(); i++ ) {
140 g->SetPoint(i,X(i),VSIZE(i));
145 //_____________________________________________________________________________
147 AliPHOSMemoryWatcher::GraphRSSIZE(void)
149 // Fills the graph with the real memory sized used
153 g = new TGraph(Size());
155 for (i=0; i < g->GetN(); i++ ) {
156 g->SetPoint(i,X(i),RSSIZE(i));
161 //_____________________________________________________________________________
163 AliPHOSMemoryWatcher::GraphTIME(void)
165 // Fills the raph with the used CPU time
169 g = new TGraph(Size());
171 for (i=0; i < g->GetN(); i++ ) {
172 g->SetPoint(i,X(i),TIME(i));
177 //_____________________________________________________________________________
179 AliPHOSMemoryWatcher::Frame(void) const
181 //creates the frame histo in which the graphs will be plotted
187 for (i=0; i < Size() ; i++ ) {
188 if ( X(i) < xmin ) xmin = X(i);
189 if ( X(i) > xmax ) xmax = X(i);
190 Double_t y = VSIZE(i)+RSSIZE(i);
191 if ( y > ymax ) ymax = y;
192 if ( VSIZE(i) < ymin ) ymin = VSIZE(i);
193 if ( RSSIZE(i) < ymin ) ymin = RSSIZE(i);
195 TH2F* h = new TH2F("frame","",10,xmin,xmax,10,ymin*0.8,ymax*1.2);
198 //_____________________________________________________________________________
200 AliPHOSMemoryWatcher::Write(void)
202 // Stores the graphs in a file
203 if ( GraphVSIZE() ) GraphVSIZE()->Write("VSIZE",TObject::kOverwrite);
204 if ( GraphRSSIZE() ) GraphRSSIZE() ->Write("RSSIZE",TObject::kOverwrite);
205 if ( GraphTIME() ) GraphTIME()->Write("TIME",TObject::kOverwrite);