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