]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliMemoryWatcher.cxx
Realisation of VEventHandler for input events.
[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$ */
cc124d17 16
d60522e4 17//_________________________________________________________________________
00399209 18//Basic Memory Leak utility.
19// You can use this tiny class to *see* if your program is leaking.
20// Usage:
ac8cd48d 21// AliMemoryWatcher memwatcher;
00399209 22// some program loop on events here {
23// if ( nevents % x == 0 )
24// {
25// // take a sample every x events
7c8a93e6 26// memwatcher.Watch(nevents);
00399209 27// }
28// }
29// TFile f("out.root","RECREATE");
7c8a93e6 30// memwatcher.Write();
00399209 31// f.Close();
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)
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// But by fitting the VSIZE by a pol1 under ROOT, you'll see right away
44// by how much your program is leaking.
d60522e4 45//*-- Author: Laurent Aphecetche(SUBATECH)
cc124d17 46
d60522e4 47// --- std system ---
54ee927c 48#include <cassert>
65f03c13 49#ifdef __APPLE__
50#include <stdlib.h>
51#else
7406fd3c 52#include <malloc.h>
65f03c13 53#endif
d60522e4 54// --- AliRoot header files ---
21bf7095 55#include "AliLog.h"
ac8cd48d 56#include "AliMemoryWatcher.h"
d60522e4 57// --- ROOT system ---
58#include "TSystem.h"
59#include "TGraph.h"
60#include "TH2.h"
61#include "TStopwatch.h"
7c8a93e6 62#include "TError.h"
dfe7f75b 63
ac8cd48d 64ClassImp(AliMemoryWatcher)
dfe7f75b 65
d60522e4 66//_____________________________________________________________________________
90e48c0c 67AliMemoryWatcher::AliMemoryWatcher(UInt_t maxsize) :
68 TObject(),
69 fUseMallinfo(kTRUE),
70 fPID(gSystem->GetPid()),
71 fMAXSIZE(maxsize),
72 fSize(0),
73 fX(new Int_t[fMAXSIZE]),
74 fVSIZE(new Int_t[fMAXSIZE]),
75 fRSSIZE(new Int_t[fMAXSIZE]),
76 fTIME(new Double_t[fMAXSIZE]),
77 fTimer(0),
78 fDisabled(kFALSE)
d60522e4 79{
90e48c0c 80 //
b01af8c2 81 //ctor
90e48c0c 82 //
9f50efc4 83 sprintf(fCmd,"ps -h -p %d -o vsz,rss | grep -v VSZ",fPID);
d60522e4 84}
90e48c0c 85
d60522e4 86//_____________________________________________________________________________
d1898505 87AliMemoryWatcher::AliMemoryWatcher(const AliMemoryWatcher& mw):
90e48c0c 88 TObject(mw),
89 fUseMallinfo(mw.fUseMallinfo),
90 fPID(mw.fPID),
91 fMAXSIZE(mw.fMAXSIZE),
92 fSize(0),
93 fX(new Int_t[fMAXSIZE]),
94 fVSIZE(new Int_t[fMAXSIZE]),
95 fRSSIZE(new Int_t[fMAXSIZE]),
96 fTIME(new Double_t[fMAXSIZE]),
97 fTimer(0),
98 fDisabled(kFALSE)
00399209 99{
100 //copy ctor
00399209 101 strcpy(fCmd, mw.fCmd) ;
00399209 102}
90e48c0c 103
00399209 104//_____________________________________________________________________________
ac8cd48d 105AliMemoryWatcher::~AliMemoryWatcher()
d60522e4 106{
2a6e66b9 107 // dtor
d60522e4 108 delete[] fVSIZE;
109 delete[] fRSSIZE;
110 delete[] fX;
111 delete[] fTIME;
112 delete fTimer;
113}
d60522e4 114//_____________________________________________________________________________
ac8cd48d 115void AliMemoryWatcher::Watch(Int_t x)
d60522e4 116{
b01af8c2 117 // Sets the point where CPU parameters have to be monitored
d60522e4 118 if ( !fDisabled && fSize < fMAXSIZE ) {
d60522e4 119 if ( fSize==0 ) {
120 assert(fTimer==0);
121 fTimer = new TStopwatch;
122 fTimer->Start(true);
123 fTimer->Stop();
124 }
7406fd3c 125 if ( fUseMallinfo ) {
1844c927 126#ifdef __linux
7406fd3c 127 static struct mallinfo meminfo;
128 meminfo = mallinfo();
d60522e4 129 fX[fSize] = x ;
7406fd3c 130 fVSIZE[fSize] = (meminfo.hblkhd + meminfo.uordblks) / 1024;
131 fRSSIZE[fSize] = meminfo.uordblks / 1024;
d60522e4 132 fTIME[fSize] = fTimer->CpuTime();
133 fSize++;
1844c927 134#else
21bf7095 135 AliFatal("Please SetUseMallinfo to kFALSE on this system");
1844c927 136#endif
7406fd3c 137 } else {
138 static Int_t vsize, rssize;
139 static FILE* pipe = 0;
140 pipe = popen(fCmd,"r");
141 if ( pipe ) {
142
143 fscanf(pipe,"%d %d",&vsize,&rssize);
144
145 fX[fSize] = x ;
146 fVSIZE[fSize] = vsize ;
147 fRSSIZE[fSize] = rssize ;
148 fTIME[fSize] = fTimer->CpuTime();
149 fSize++;
150 }
151 assert(pclose(pipe)!=-1);
d60522e4 152 }
d60522e4 153 fTimer->Start(true);
d60522e4 154 }
155 else {
156 fDisabled=true;
21bf7095 157 AliError("I'm full !" ) ;
d60522e4 158 }
159}
d60522e4 160//_____________________________________________________________________________
161TGraph*
ac8cd48d 162AliMemoryWatcher::GraphVSIZE(void)
d60522e4 163{
b01af8c2 164 // Fills the graph with the virtual memory sized used
d60522e4 165 TGraph* g = 0;
00399209 166 if ( Size() )
d60522e4 167 {
00399209 168 g = new TGraph(Size());
b01af8c2 169 Int_t i ;
170 for (i=0; i < g->GetN(); i++ ) {
d60522e4 171 g->SetPoint(i,X(i),VSIZE(i));
172 }
173 }
174 return g;
175}
d60522e4 176//_____________________________________________________________________________
177TGraph*
ac8cd48d 178AliMemoryWatcher::GraphRSSIZE(void)
d60522e4 179{
b01af8c2 180 // Fills the graph with the real memory sized used
d60522e4 181 TGraph* g = 0;
00399209 182 if ( Size() )
d60522e4 183 {
00399209 184 g = new TGraph(Size());
b01af8c2 185 Int_t i ;
186 for (i=0; i < g->GetN(); i++ ) {
d60522e4 187 g->SetPoint(i,X(i),RSSIZE(i));
188 }
189 }
190 return g;
191}
d60522e4 192//_____________________________________________________________________________
193TGraph*
ac8cd48d 194AliMemoryWatcher::GraphTIME(void)
d60522e4 195{
b01af8c2 196 // Fills the raph with the used CPU time
d60522e4 197 TGraph* g = 0;
00399209 198 if ( Size() )
d60522e4 199 {
00399209 200 g = new TGraph(Size());
b01af8c2 201 Int_t i ;
202 for (i=0; i < g->GetN(); i++ ) {
d60522e4 203 g->SetPoint(i,X(i),TIME(i));
204 }
205 }
206 return g;
207}
d60522e4 208//_____________________________________________________________________________
209TH2*
ac8cd48d 210AliMemoryWatcher::Frame(void) const
d60522e4 211{
b01af8c2 212 //creates the frame histo in which the graphs will be plotted
213 Double_t xmin=1E30;
214 Double_t xmax=0;
215 Double_t ymin=1;
216 Double_t ymax=0;
217 UInt_t i ;
00399209 218 for (i=0; i < Size() ; i++ ) {
d60522e4 219 if ( X(i) < xmin ) xmin = X(i);
220 if ( X(i) > xmax ) xmax = X(i);
b01af8c2 221 Double_t y = VSIZE(i)+RSSIZE(i);
d60522e4 222 if ( y > ymax ) ymax = y;
d60522e4 223 if ( VSIZE(i) < ymin ) ymin = VSIZE(i);
224 if ( RSSIZE(i) < ymin ) ymin = RSSIZE(i);
225 }
d60522e4 226 TH2F* h = new TH2F("frame","",10,xmin,xmax,10,ymin*0.8,ymax*1.2);
d60522e4 227 return h;
228}
d60522e4 229//_____________________________________________________________________________
6c4904c2 230Int_t
c4cb6153 231AliMemoryWatcher::WriteToFile()
d60522e4 232{
b01af8c2 233 // Stores the graphs in a file
234 if ( GraphVSIZE() ) GraphVSIZE()->Write("VSIZE",TObject::kOverwrite);
00399209 235 if ( GraphRSSIZE() ) GraphRSSIZE() ->Write("RSSIZE",TObject::kOverwrite);
b01af8c2 236 if ( GraphTIME() ) GraphTIME()->Write("TIME",TObject::kOverwrite);
6c4904c2 237 return 0;
4ed9ed00 238}