Corrections to obey the coding conventions
[u/mrichter/AliRoot.git] / PHOS / AliPHOSMemoryWatcher.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//_________________________________________________________________________
17/*Basic Memory Leak utility.
18
19 You can use this tiny class to *see* if your program is leaking.
d60522e4 20 Usage:
d60522e4 21 AliPHOSMemoryWatcher memwatcher;
d60522e4 22 some program loop on events here {
23 if ( nevents % x == 0 )
24 {
25 // take a sample every x events
26 memwatcher.watch(nevents);
27 }
28 }
d60522e4 29 TFile f("out.root","RECREATE");
30 memwatcher.write();
31 f.Close();
d60522e4 32 In the output root file you'll get 3 graphs representing
33 the evolAliPHOSon, as a function of the number of events, of :
34 - VSIZE is the virtual size (in KBytes) of your program, that is sort of
35 the total memory used
36 - RSSIZE is the resident size (in KBytes), that is, the part of your
37 program which is really in physical memory.
38 - TIME is an estimate of time per event (really it's the time elasped
39 between two calls to watch method)
d60522e4 40 WARNING: this is far from a bulletproof memory report (it's basically
41 using UNIX command ps -h -p [PID] -o vsize,rssize to do its job).
42 It has only been tested on Linux so far.
43
44 But by fitting the VSIZE by a pol1 under ROOT, you'll see right away
45 by how much your program is leaking.
46*/
47//*-- Author: Laurent Aphecetche(SUBATECH)
d60522e4 48// --- std system ---
b01af8c2 49class assert ;
d60522e4 50// --- AliRoot header files ---
51#include "AliPHOSMemoryWatcher.h"
d60522e4 52// --- ROOT system ---
53#include "TSystem.h"
54#include "TGraph.h"
55#include "TH2.h"
56#include "TStopwatch.h"
dfe7f75b 57
58ClassImp(AliPHOSMemoryWatcher)
59
d60522e4 60//_____________________________________________________________________________
b01af8c2 61AliPHOSMemoryWatcher::AliPHOSMemoryWatcher(UInt_t maxsize)
d60522e4 62{
b01af8c2 63 //ctor
d60522e4 64 fMAXSIZE=maxsize;
65 fPID = gSystem->GetPid();
66 sprintf(fCmd,"ps -h -p %d -o vsize,rssize",fPID);
b01af8c2 67 fX = new Int_t[fMAXSIZE];
68 fVSIZE = new Int_t[fMAXSIZE];
69 fRSSIZE = new Int_t[fMAXSIZE];
70 fTIME = new Double_t[fMAXSIZE];
d60522e4 71 fSize=0;
72 fDisabled=false;
73 fTimer=0;
74}
d60522e4 75//_____________________________________________________________________________
76AliPHOSMemoryWatcher::~AliPHOSMemoryWatcher()
77{
78 delete[] fVSIZE;
79 delete[] fRSSIZE;
80 delete[] fX;
81 delete[] fTIME;
82 delete fTimer;
83}
d60522e4 84//_____________________________________________________________________________
b01af8c2 85void AliPHOSMemoryWatcher::Watch(Int_t x)
d60522e4 86{
b01af8c2 87 // Sets the point where CPU parameters have to be monitored
d60522e4 88 if ( !fDisabled && fSize < fMAXSIZE ) {
d60522e4 89 if ( fSize==0 ) {
90 assert(fTimer==0);
91 fTimer = new TStopwatch;
92 fTimer->Start(true);
93 fTimer->Stop();
94 }
b01af8c2 95 static Int_t vsize, rssize;
d60522e4 96 static FILE* pipe = 0;
d60522e4 97 pipe = popen(fCmd,"r");
d60522e4 98 if ( pipe ) {
99
100 fscanf(pipe,"%d %d",&vsize,&rssize);
101
102 fX[fSize] = x ;
103 fVSIZE[fSize] = vsize ;
104 fRSSIZE[fSize] = rssize ;
105 fTIME[fSize] = fTimer->CpuTime();
106 fSize++;
107 }
d60522e4 108 assert(pclose(pipe)!=-1);
d60522e4 109 fTimer->Start(true);
d60522e4 110 }
111 else {
112 fDisabled=true;
21cd0c07 113 Error("watch", "I'm full !" ) ;
d60522e4 114 }
115}
d60522e4 116//_____________________________________________________________________________
117TGraph*
b01af8c2 118AliPHOSMemoryWatcher::GraphVSIZE(void)
d60522e4 119{
b01af8c2 120 // Fills the graph with the virtual memory sized used
d60522e4 121 TGraph* g = 0;
d60522e4 122 if ( size() )
123 {
124 g = new TGraph(size());
b01af8c2 125 Int_t i ;
126 for (i=0; i < g->GetN(); i++ ) {
d60522e4 127 g->SetPoint(i,X(i),VSIZE(i));
128 }
129 }
130 return g;
131}
d60522e4 132//_____________________________________________________________________________
133TGraph*
b01af8c2 134AliPHOSMemoryWatcher::GraphRSSIZE(void)
d60522e4 135{
b01af8c2 136 // Fills the graph with the real memory sized used
d60522e4 137 TGraph* g = 0;
138 if ( size() )
139 {
140 g = new TGraph(size());
b01af8c2 141 Int_t i ;
142 for (i=0; i < g->GetN(); i++ ) {
d60522e4 143 g->SetPoint(i,X(i),RSSIZE(i));
144 }
145 }
146 return g;
147}
d60522e4 148//_____________________________________________________________________________
149TGraph*
b01af8c2 150AliPHOSMemoryWatcher::GraphTIME(void)
d60522e4 151{
b01af8c2 152 // Fills the raph with the used CPU time
d60522e4 153 TGraph* g = 0;
154 if ( size() )
155 {
156 g = new TGraph(size());
b01af8c2 157 Int_t i ;
158 for (i=0; i < g->GetN(); i++ ) {
d60522e4 159 g->SetPoint(i,X(i),TIME(i));
160 }
161 }
162 return g;
163}
d60522e4 164//_____________________________________________________________________________
165TH2*
b01af8c2 166AliPHOSMemoryWatcher::Frame(void) const
d60522e4 167{
b01af8c2 168 //creates the frame histo in which the graphs will be plotted
169 Double_t xmin=1E30;
170 Double_t xmax=0;
171 Double_t ymin=1;
172 Double_t ymax=0;
173 UInt_t i ;
174 for (i=0; i < size() ; i++ ) {
d60522e4 175 if ( X(i) < xmin ) xmin = X(i);
176 if ( X(i) > xmax ) xmax = X(i);
b01af8c2 177 Double_t y = VSIZE(i)+RSSIZE(i);
d60522e4 178 if ( y > ymax ) ymax = y;
d60522e4 179 if ( VSIZE(i) < ymin ) ymin = VSIZE(i);
180 if ( RSSIZE(i) < ymin ) ymin = RSSIZE(i);
181 }
d60522e4 182 TH2F* h = new TH2F("frame","",10,xmin,xmax,10,ymin*0.8,ymax*1.2);
d60522e4 183 return h;
184}
d60522e4 185//_____________________________________________________________________________
186void
b01af8c2 187AliPHOSMemoryWatcher::Write(void)
d60522e4 188{
b01af8c2 189 // Stores the graphs in a file
190 if ( GraphVSIZE() ) GraphVSIZE()->Write("VSIZE",TObject::kOverwrite);
191 if ( GraphRSSIZE() ) GraphRSSIZE()->Write("RSSIZE",TObject::kOverwrite);
192 if ( GraphTIME() ) GraphTIME()->Write("TIME",TObject::kOverwrite);
4ed9ed00 193}