]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliMemoryWatcher.cxx
added function returning number in truncated gaussian (user passes mean, sigma and...
[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>
7f602369 49#ifdef NEVER
65f03c13 50#include <stdlib.h>
65f03c13 51#endif
d60522e4 52// --- AliRoot header files ---
21bf7095 53#include "AliLog.h"
ac8cd48d 54#include "AliMemoryWatcher.h"
d60522e4 55// --- ROOT system ---
56#include "TSystem.h"
57#include "TGraph.h"
58#include "TH2.h"
59#include "TStopwatch.h"
7c8a93e6 60#include "TError.h"
dfe7f75b 61
ac8cd48d 62ClassImp(AliMemoryWatcher)
dfe7f75b 63
d60522e4 64//_____________________________________________________________________________
90e48c0c 65AliMemoryWatcher::AliMemoryWatcher(UInt_t maxsize) :
66 TObject(),
90e48c0c 67 fMAXSIZE(maxsize),
68 fSize(0),
69 fX(new Int_t[fMAXSIZE]),
70 fVSIZE(new Int_t[fMAXSIZE]),
71 fRSSIZE(new Int_t[fMAXSIZE]),
72 fTIME(new Double_t[fMAXSIZE]),
73 fTimer(0),
74 fDisabled(kFALSE)
d60522e4 75{
90e48c0c 76 //
b01af8c2 77 //ctor
90e48c0c 78 //
d60522e4 79}
90e48c0c 80
d60522e4 81//_____________________________________________________________________________
d1898505 82AliMemoryWatcher::AliMemoryWatcher(const AliMemoryWatcher& mw):
90e48c0c 83 TObject(mw),
90e48c0c 84 fMAXSIZE(mw.fMAXSIZE),
85 fSize(0),
86 fX(new Int_t[fMAXSIZE]),
87 fVSIZE(new Int_t[fMAXSIZE]),
88 fRSSIZE(new Int_t[fMAXSIZE]),
89 fTIME(new Double_t[fMAXSIZE]),
90 fTimer(0),
91 fDisabled(kFALSE)
00399209 92{
93 //copy ctor
00399209 94}
90e48c0c 95
00399209 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{
7f602369 109 static ProcInfo_t meminfo;
110#ifdef NEVER
111 static Char_t cmd[1024]="";
112#endif
b01af8c2 113 // Sets the point where CPU parameters have to be monitored
d60522e4 114 if ( !fDisabled && fSize < fMAXSIZE ) {
d60522e4 115 if ( fSize==0 ) {
d60522e4 116 fTimer = new TStopwatch;
117 fTimer->Start(true);
118 fTimer->Stop();
7f602369 119#ifdef NEVER
120 if(!cmd[0])
121 sprintf(cmd,"ps -h -p %d -o vsz,rss | grep -v VSZ",gSystem->GetPid());
122#endif
d60522e4 123 }
7f602369 124 gSystem->GetProcInfo(&meminfo);
125 fX[fSize] = x ;
126 fVSIZE[fSize] = meminfo.fMemVirtual / 1024;
127 fRSSIZE[fSize] = meminfo.fMemResident / 1024;
128 fTIME[fSize] = fTimer->CpuTime();
129 fSize++;
130#ifdef NEVER
131 Int_t vsize, rssize;
132 FILE* pipe = 0;
133 pipe = popen(cmd,"r");
134 if ( pipe ) {
135
136 fscanf(pipe,"%d %d",&vsize,&rssize);
137
d60522e4 138 fX[fSize] = x ;
7f602369 139 fVSIZE[fSize] = vsize ;
140 fRSSIZE[fSize] = rssize ;
d60522e4 141 fTIME[fSize] = fTimer->CpuTime();
142 fSize++;
143 }
7f602369 144 Int_t iclose=pclose(pipe);
145 assert(iclose!=-1);
146#endif
d60522e4 147 fTimer->Start(true);
7f602369 148 } else {
d60522e4 149 fDisabled=true;
21bf7095 150 AliError("I'm full !" ) ;
d60522e4 151 }
152}
d60522e4 153//_____________________________________________________________________________
154TGraph*
ac8cd48d 155AliMemoryWatcher::GraphVSIZE(void)
d60522e4 156{
b01af8c2 157 // Fills the graph with the virtual memory sized used
d60522e4 158 TGraph* g = 0;
00399209 159 if ( Size() )
d60522e4 160 {
00399209 161 g = new TGraph(Size());
b01af8c2 162 Int_t i ;
163 for (i=0; i < g->GetN(); i++ ) {
d60522e4 164 g->SetPoint(i,X(i),VSIZE(i));
165 }
166 }
167 return g;
168}
d60522e4 169//_____________________________________________________________________________
170TGraph*
ac8cd48d 171AliMemoryWatcher::GraphRSSIZE(void)
d60522e4 172{
b01af8c2 173 // Fills the graph with the real memory sized used
d60522e4 174 TGraph* g = 0;
00399209 175 if ( Size() )
d60522e4 176 {
00399209 177 g = new TGraph(Size());
b01af8c2 178 Int_t i ;
179 for (i=0; i < g->GetN(); i++ ) {
d60522e4 180 g->SetPoint(i,X(i),RSSIZE(i));
181 }
182 }
183 return g;
184}
d60522e4 185//_____________________________________________________________________________
186TGraph*
ac8cd48d 187AliMemoryWatcher::GraphTIME(void)
d60522e4 188{
b01af8c2 189 // Fills the raph with the used CPU time
d60522e4 190 TGraph* g = 0;
00399209 191 if ( Size() )
d60522e4 192 {
00399209 193 g = new TGraph(Size());
b01af8c2 194 Int_t i ;
195 for (i=0; i < g->GetN(); i++ ) {
d60522e4 196 g->SetPoint(i,X(i),TIME(i));
197 }
198 }
199 return g;
200}
d60522e4 201//_____________________________________________________________________________
202TH2*
ac8cd48d 203AliMemoryWatcher::Frame(void) const
d60522e4 204{
b01af8c2 205 //creates the frame histo in which the graphs will be plotted
206 Double_t xmin=1E30;
207 Double_t xmax=0;
208 Double_t ymin=1;
209 Double_t ymax=0;
210 UInt_t i ;
00399209 211 for (i=0; i < Size() ; i++ ) {
d60522e4 212 if ( X(i) < xmin ) xmin = X(i);
213 if ( X(i) > xmax ) xmax = X(i);
b01af8c2 214 Double_t y = VSIZE(i)+RSSIZE(i);
d60522e4 215 if ( y > ymax ) ymax = y;
d60522e4 216 if ( VSIZE(i) < ymin ) ymin = VSIZE(i);
217 if ( RSSIZE(i) < ymin ) ymin = RSSIZE(i);
218 }
d60522e4 219 TH2F* h = new TH2F("frame","",10,xmin,xmax,10,ymin*0.8,ymax*1.2);
d60522e4 220 return h;
221}
d60522e4 222//_____________________________________________________________________________
6c4904c2 223Int_t
c4cb6153 224AliMemoryWatcher::WriteToFile()
d60522e4 225{
b01af8c2 226 // Stores the graphs in a file
227 if ( GraphVSIZE() ) GraphVSIZE()->Write("VSIZE",TObject::kOverwrite);
00399209 228 if ( GraphRSSIZE() ) GraphRSSIZE() ->Write("RSSIZE",TObject::kOverwrite);
b01af8c2 229 if ( GraphTIME() ) GraphTIME()->Write("TIME",TObject::kOverwrite);
6c4904c2 230 return 0;
4ed9ed00 231}