]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PHOS/AliPHOSMemoryWatcher.cxx
cout replaced by printf
[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 ---
25d89c9f 49#include <stdio.h>
50#include <stdlib.h>
51#include <assert.h>
d60522e4 52// --- AliRoot header files ---
53#include "AliPHOSMemoryWatcher.h"
d60522e4 54// --- ROOT system ---
55#include "TSystem.h"
56#include "TGraph.h"
57#include "TH2.h"
58#include "TStopwatch.h"
dfe7f75b 59
60ClassImp(AliPHOSMemoryWatcher)
61
d60522e4 62//_____________________________________________________________________________
63AliPHOSMemoryWatcher::AliPHOSMemoryWatcher(unsigned int maxsize)
64{
65 fMAXSIZE=maxsize;
66 fPID = gSystem->GetPid();
67 sprintf(fCmd,"ps -h -p %d -o vsize,rssize",fPID);
d60522e4 68 fX = new int[fMAXSIZE];
69 fVSIZE = new int[fMAXSIZE];
70 fRSSIZE = new int[fMAXSIZE];
71 fTIME = new double[fMAXSIZE];
72 fSize=0;
73 fDisabled=false;
74 fTimer=0;
75}
d60522e4 76//_____________________________________________________________________________
77AliPHOSMemoryWatcher::~AliPHOSMemoryWatcher()
78{
79 delete[] fVSIZE;
80 delete[] fRSSIZE;
81 delete[] fX;
82 delete[] fTIME;
83 delete fTimer;
84}
d60522e4 85//_____________________________________________________________________________
86void AliPHOSMemoryWatcher::watch(int x)
87{
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 }
d60522e4 95 static int 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*
118AliPHOSMemoryWatcher::graphVSIZE(void)
119{
120 TGraph* g = 0;
d60522e4 121 if ( size() )
122 {
123 g = new TGraph(size());
124 for (int i=0; i < g->GetN(); i++ ) {
125 g->SetPoint(i,X(i),VSIZE(i));
126 }
127 }
128 return g;
129}
d60522e4 130//_____________________________________________________________________________
131TGraph*
132AliPHOSMemoryWatcher::graphRSSIZE(void)
133{
134 TGraph* g = 0;
135 if ( size() )
136 {
137 g = new TGraph(size());
138 for (int i=0; i < g->GetN(); i++ ) {
139 g->SetPoint(i,X(i),RSSIZE(i));
140 }
141 }
142 return g;
143}
d60522e4 144//_____________________________________________________________________________
145TGraph*
146AliPHOSMemoryWatcher::graphTIME(void)
147{
148 TGraph* g = 0;
149 if ( size() )
150 {
151 g = new TGraph(size());
152 for (int i=0; i < g->GetN(); i++ ) {
153 g->SetPoint(i,X(i),TIME(i));
154 }
155 }
156 return g;
157}
d60522e4 158//_____________________________________________________________________________
159TH2*
160AliPHOSMemoryWatcher::frame(void)
161{
162 double xmin=1E30;
163 double xmax=0;
164 double ymin=1;
165 double ymax=0;
d60522e4 166 for (unsigned int i=0; i < size() ; i++ ) {
167 if ( X(i) < xmin ) xmin = X(i);
168 if ( X(i) > xmax ) xmax = X(i);
d60522e4 169 double y = VSIZE(i)+RSSIZE(i);
d60522e4 170 if ( y > ymax ) ymax = y;
d60522e4 171 if ( VSIZE(i) < ymin ) ymin = VSIZE(i);
172 if ( RSSIZE(i) < ymin ) ymin = RSSIZE(i);
173 }
d60522e4 174 TH2F* h = new TH2F("frame","",10,xmin,xmax,10,ymin*0.8,ymax*1.2);
d60522e4 175 return h;
176}
d60522e4 177//_____________________________________________________________________________
178void
179AliPHOSMemoryWatcher::write(void)
180{
181 if ( graphVSIZE() ) graphVSIZE()->Write("VSIZE",TObject::kOverwrite);
182 if ( graphRSSIZE() ) graphRSSIZE()->Write("RSSIZE",TObject::kOverwrite);
183 if ( graphTIME() ) graphTIME()->Write("TIME",TObject::kOverwrite);
4ed9ed00 184}