]>
Commit | Line | Data |
---|---|---|
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 | //_________________________________________________________________________ | |
00399209 | 17 | //Basic Memory Leak utility. |
18 | // You can use this tiny class to *see* if your program is leaking. | |
19 | // Usage: | |
20 | // AliPHOSMemoryWatcher memwatcher; | |
21 | // some program loop on events here { | |
22 | // if ( nevents % x == 0 ) | |
23 | // { | |
24 | // // take a sample every x events | |
25 | // memwatcher.watch(nevents); | |
26 | // } | |
27 | // } | |
28 | // TFile f("out.root","RECREATE"); | |
29 | // memwatcher.write(); | |
30 | // f.Close(); | |
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. | |
d60522e4 | 44 | //*-- Author: Laurent Aphecetche(SUBATECH) |
d60522e4 | 45 | // --- std system --- |
b01af8c2 | 46 | class assert ; |
d60522e4 | 47 | // --- AliRoot header files --- |
48 | #include "AliPHOSMemoryWatcher.h" | |
d60522e4 | 49 | // --- ROOT system --- |
50 | #include "TSystem.h" | |
51 | #include "TGraph.h" | |
52 | #include "TH2.h" | |
53 | #include "TStopwatch.h" | |
dfe7f75b | 54 | |
55 | ClassImp(AliPHOSMemoryWatcher) | |
56 | ||
d60522e4 | 57 | //_____________________________________________________________________________ |
b01af8c2 | 58 | AliPHOSMemoryWatcher::AliPHOSMemoryWatcher(UInt_t maxsize) |
d60522e4 | 59 | { |
b01af8c2 | 60 | //ctor |
d60522e4 | 61 | fMAXSIZE=maxsize; |
62 | fPID = gSystem->GetPid(); | |
63 | sprintf(fCmd,"ps -h -p %d -o vsize,rssize",fPID); | |
b01af8c2 | 64 | fX = new Int_t[fMAXSIZE]; |
65 | fVSIZE = new Int_t[fMAXSIZE]; | |
66 | fRSSIZE = new Int_t[fMAXSIZE]; | |
67 | fTIME = new Double_t[fMAXSIZE]; | |
d60522e4 | 68 | fSize=0; |
69 | fDisabled=false; | |
70 | fTimer=0; | |
71 | } | |
d60522e4 | 72 | //_____________________________________________________________________________ |
00399209 | 73 | AliPHOSMemoryWatcher::AliPHOSMemoryWatcher(AliPHOSMemoryWatcher& mw) |
74 | { | |
75 | //copy ctor | |
76 | fMAXSIZE = mw.fMAXSIZE ; | |
77 | fPID = mw.fPID ; | |
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]; | |
83 | fSize=0; | |
84 | fDisabled=false; | |
85 | fTimer=0; | |
86 | } | |
87 | //_____________________________________________________________________________ | |
d60522e4 | 88 | AliPHOSMemoryWatcher::~AliPHOSMemoryWatcher() |
89 | { | |
90 | delete[] fVSIZE; | |
91 | delete[] fRSSIZE; | |
92 | delete[] fX; | |
93 | delete[] fTIME; | |
94 | delete fTimer; | |
95 | } | |
d60522e4 | 96 | //_____________________________________________________________________________ |
b01af8c2 | 97 | void AliPHOSMemoryWatcher::Watch(Int_t x) |
d60522e4 | 98 | { |
b01af8c2 | 99 | // Sets the point where CPU parameters have to be monitored |
d60522e4 | 100 | if ( !fDisabled && fSize < fMAXSIZE ) { |
d60522e4 | 101 | if ( fSize==0 ) { |
102 | assert(fTimer==0); | |
103 | fTimer = new TStopwatch; | |
104 | fTimer->Start(true); | |
105 | fTimer->Stop(); | |
106 | } | |
b01af8c2 | 107 | static Int_t vsize, rssize; |
d60522e4 | 108 | static FILE* pipe = 0; |
d60522e4 | 109 | pipe = popen(fCmd,"r"); |
d60522e4 | 110 | if ( pipe ) { |
111 | ||
112 | fscanf(pipe,"%d %d",&vsize,&rssize); | |
113 | ||
114 | fX[fSize] = x ; | |
115 | fVSIZE[fSize] = vsize ; | |
116 | fRSSIZE[fSize] = rssize ; | |
117 | fTIME[fSize] = fTimer->CpuTime(); | |
118 | fSize++; | |
119 | } | |
d60522e4 | 120 | assert(pclose(pipe)!=-1); |
d60522e4 | 121 | fTimer->Start(true); |
d60522e4 | 122 | } |
123 | else { | |
124 | fDisabled=true; | |
21cd0c07 | 125 | Error("watch", "I'm full !" ) ; |
d60522e4 | 126 | } |
127 | } | |
d60522e4 | 128 | //_____________________________________________________________________________ |
129 | TGraph* | |
b01af8c2 | 130 | AliPHOSMemoryWatcher::GraphVSIZE(void) |
d60522e4 | 131 | { |
b01af8c2 | 132 | // Fills the graph with the virtual memory sized used |
d60522e4 | 133 | TGraph* g = 0; |
00399209 | 134 | if ( Size() ) |
d60522e4 | 135 | { |
00399209 | 136 | g = new TGraph(Size()); |
b01af8c2 | 137 | Int_t i ; |
138 | for (i=0; i < g->GetN(); i++ ) { | |
d60522e4 | 139 | g->SetPoint(i,X(i),VSIZE(i)); |
140 | } | |
141 | } | |
142 | return g; | |
143 | } | |
d60522e4 | 144 | //_____________________________________________________________________________ |
145 | TGraph* | |
b01af8c2 | 146 | AliPHOSMemoryWatcher::GraphRSSIZE(void) |
d60522e4 | 147 | { |
b01af8c2 | 148 | // Fills the graph with the real memory sized used |
d60522e4 | 149 | TGraph* g = 0; |
00399209 | 150 | if ( Size() ) |
d60522e4 | 151 | { |
00399209 | 152 | g = new TGraph(Size()); |
b01af8c2 | 153 | Int_t i ; |
154 | for (i=0; i < g->GetN(); i++ ) { | |
d60522e4 | 155 | g->SetPoint(i,X(i),RSSIZE(i)); |
156 | } | |
157 | } | |
158 | return g; | |
159 | } | |
d60522e4 | 160 | //_____________________________________________________________________________ |
161 | TGraph* | |
b01af8c2 | 162 | AliPHOSMemoryWatcher::GraphTIME(void) |
d60522e4 | 163 | { |
b01af8c2 | 164 | // Fills the raph with the used CPU time |
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),TIME(i)); |
172 | } | |
173 | } | |
174 | return g; | |
175 | } | |
d60522e4 | 176 | //_____________________________________________________________________________ |
177 | TH2* | |
b01af8c2 | 178 | AliPHOSMemoryWatcher::Frame(void) const |
d60522e4 | 179 | { |
b01af8c2 | 180 | //creates the frame histo in which the graphs will be plotted |
181 | Double_t xmin=1E30; | |
182 | Double_t xmax=0; | |
183 | Double_t ymin=1; | |
184 | Double_t ymax=0; | |
185 | UInt_t i ; | |
00399209 | 186 | for (i=0; i < Size() ; i++ ) { |
d60522e4 | 187 | if ( X(i) < xmin ) xmin = X(i); |
188 | if ( X(i) > xmax ) xmax = X(i); | |
b01af8c2 | 189 | Double_t y = VSIZE(i)+RSSIZE(i); |
d60522e4 | 190 | if ( y > ymax ) ymax = y; |
d60522e4 | 191 | if ( VSIZE(i) < ymin ) ymin = VSIZE(i); |
192 | if ( RSSIZE(i) < ymin ) ymin = RSSIZE(i); | |
193 | } | |
d60522e4 | 194 | TH2F* h = new TH2F("frame","",10,xmin,xmax,10,ymin*0.8,ymax*1.2); |
d60522e4 | 195 | return h; |
196 | } | |
d60522e4 | 197 | //_____________________________________________________________________________ |
198 | void | |
b01af8c2 | 199 | AliPHOSMemoryWatcher::Write(void) |
d60522e4 | 200 | { |
b01af8c2 | 201 | // Stores the graphs in a file |
202 | if ( GraphVSIZE() ) GraphVSIZE()->Write("VSIZE",TObject::kOverwrite); | |
00399209 | 203 | if ( GraphRSSIZE() ) GraphRSSIZE() ->Write("RSSIZE",TObject::kOverwrite); |
b01af8c2 | 204 | if ( GraphTIME() ) GraphTIME()->Write("TIME",TObject::kOverwrite); |
4ed9ed00 | 205 | } |