]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/STEERBase/AliSysInfo.cxx
Updates for the pid caching. By default it is still switched off
[u/mrichter/AliRoot.git] / STEER / STEERBase / AliSysInfo.cxx
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
16
17 //
18 // Origin:  marian.ivanov@cern.ch
19 //
20 //  Make   a log file for the CPU and Memory usage
21 //
22 //  Principles:
23 //  Snapshots of the system information are writen to the text log files.
24 //  Text files were chosen in order to get the ouptu also in case code 
25 //  is crashing. 
26 //  Following information is stored in the log file:
27 //  TTimeStamp stamp;
28 //  CpuInfo_t  cpuInfo;
29 //  MemInfo_t  memInfo;
30 //  ProcInfo_t procInfo;
31 // 
32 //  Root TSystem is used to retrieve this information:
33 //  gSystem->GetCpuInfo(&cpuInfo, 10);
34 //  gSystem->GetMemInfo(&memInfo);
35 //  gSystem->GetProcInfo(&procInfo);
36 //  for details see:
37 //  http://root.cern.ch/root/html/TUnixSystem.html
38 //  http://root.cern.ch/root/html/ProcInfo_t.html
39 //  http://root.cern.ch/root/html/MemInfo_t.html
40 //  http://root.cern.ch/root/html/CpuInfo_t.html
41 //  -------------------------------------------------------------------
42 //  class CpuInfo_t
43 //   Float_t    fIdle   cpu idle percentage
44 //   Float_t    fLoad15m        cpu load average over 15 m
45 //   Float_t    fLoad1m cpu load average over 1 m
46 //   Float_t    fLoad5m cpu load average over 5 m
47 //   Float_t    fSys    cpu sys load in percentage
48 //   Float_t    fTotal  cpu user+sys load in percentage
49 //   Float_t    fUser   cpu user load in percentage
50
51 //  -------------------------------------------------------------------
52 //  class ProcInfo_t:
53 //  Float_t     fCpuSys system time used by this process in seconds
54 //  Float_t     fCpuUser        user time used by this process in seconds
55 //  Long_t      fMemResident    resident memory used by this process in KB
56 //  Long_t      fMemVirtual     virtual memory used by this process in KB
57 //  -------------------------------------------------------------------
58
59
60 //  The information from the AliSysInfo can be used as measurement
61 //  of the code quality. Be aware of the limitation induced by
62 //  using only system info described in the  AliSysInfo::Test() function
63 // 
64 //  The example usage of the AliSysInfo is shown in the
65 //  AliSysInfo::Test() example.
66 //  
67 //  
68 //
69 //  
70 //  The typical usage in the AliRoot code:
71 //  Make a set of stamps in the code in the place of interest
72 //  e.g. 
73 //
74 //  AliSysInfo::AddStamp("Start");
75 //
76 //  loader->UnloadRecPoints();
77 //    AliSysInfo::AddStamp(Form("LRec%s_%d",fgkDetectorName[iDet],eventNr), iDet,1,eventNr);
78 //
79
80 // The log file can be transformed to the tree - to make a visualization
81 // See $ALICE_ROOT/macros/PlotSysInfo.C as an example
82
83
84 #include <Riostream.h>
85 //#include "AliLog.h"
86 #include "TStopwatch.h"
87 #include "TSystem.h"
88 #include "TTree.h"
89 #include "TFile.h"
90
91 #include "TTimeStamp.h"
92 #include "AliSysInfo.h"
93 #include "TBufferFile.h"
94
95 //#include "TMemStatManager.h"  //USE IFDEF
96
97
98 using std::endl;
99 using std::cout;
100 using std::ios_base;
101 using std::setprecision;
102 ClassImp(AliSysInfo)
103
104 AliSysInfo* AliSysInfo::fInstance=0;
105
106 AliSysInfo::AliSysInfo():
107     TObject(),
108     fSysWatch(0),
109     fTimer(0),
110     fMemStat(0),
111     fCallBackFunc(0),
112     fNCallBack(0)
113 {
114   fTimer = new TStopwatch;
115   fSysWatch  = new fstream("syswatch.log", ios_base::out|ios_base::trunc);
116
117   //hname/C:sname/C:sec/D:mI.fMemUsed/F:mI.fSwapUsed/F:pI.fMemResident/F:pI.fMemVirtual/F:cI.fUser/F:cI.fSys/F:cI.fCpuUser/F:pI.fCpuSys/F
118
119
120   (*fSysWatch) <<"hname"<<"/C:"               // hname - hostname  
121                <<"sname"<<"/C:"              // stamp name
122                <<"id0"<<"/I:"                // 0 id
123                <<"id1"<<"/I:"                // 1 id
124                <<"id2"<<"/I:"                // 1 id
125                <<"first"<<"/D:"              // first stamp
126     //
127                <<"stampSec"<<"/D:"         // time  - time stamp in seconds
128                <<"mi.fMemUsed"<<"/D:"       // system info 
129                <<"mi.fSwapUsed"<<"/D:"      //
130                <<"cI.fUser"<<"/D:"         //
131                <<"cI.fSys"<<"/D:"         //
132     // 
133                <<"pI.fMemResident"<<"/D:"  // process info
134                <<"pI.fMemVirtual"<<"/D:"   //    
135                <<"pI.fCpuUser"<<"/D:"      //
136                <<"pI.fCpuSys"<<"/D:"       //
137     //
138                <<"stampOldSec"<<"/D:"         // time  - time stamp in seconds
139                <<"miOld.fMemUsed"<<"/D:"       // system info - previous
140                <<"miOld.fSwapUsed"<<"/D:"      //
141                <<"cIOld.fUser"<<"/D:"         //
142                <<"cIOld.fSys"<<"/D:"         //
143     // 
144                <<"pIOld.fMemResident"<<"/D:"  // process info -previous
145                <<"pIOld.fMemVirtual"<<"/D:"   //    
146                <<"pIOld.fCpuUser"<<"/D:"      //
147                <<"pIOld.fCpuSys"<<"/D:"       //
148     // 
149                <<"fileBytesRead"<<"/D:"       // file IO information
150                <<"fileBytesWritten"<<"/D:"    //    
151                <<"fileCounter"<<"/D:"         //
152                <<"fileReadCalls"<<"/D"        //
153                << endl;
154   
155 }
156
157
158
159
160 AliSysInfo * AliSysInfo::Instance(){
161   //
162   //
163   //
164   if (!fInstance){
165     fInstance = new AliSysInfo;
166   }
167   return fInstance;
168 }
169
170
171 void AliSysInfo::AddStamp(const char *sname, Int_t id0, Int_t id1, Int_t id2){
172   //
173   // 
174   //
175   //
176   TTimeStamp stamp;
177   CpuInfo_t  cpuInfo;
178   MemInfo_t  memInfo;
179   ProcInfo_t procInfo;  
180   gSystem->GetCpuInfo(&cpuInfo, 10);
181   gSystem->GetMemInfo(&memInfo);
182   gSystem->GetProcInfo(&procInfo);
183   //  procInfo.fMemVirtual/=1024;  //size in MBy
184   //procInfo.fMemResident/=1024;  //size in MBy
185
186   const char * hname = gSystem->HostName();
187
188   static Int_t entry=0;
189   static Double_t  first=stamp.GetSec()+stamp.GetNanoSec()/1000000000.;
190   //
191   static TTimeStamp stampOld;
192   static CpuInfo_t  cpuInfoOld;
193   static MemInfo_t  memInfoOld;
194   static ProcInfo_t procInfoOld;  
195   Double_t fileBytesRead    = TFile::GetFileBytesRead();
196   Double_t fileBytesWritten = TFile::GetFileBytesWritten();
197   Double_t fileCounter      = TFile::GetFileCounter();
198   Double_t fileReadCalls    = TFile::GetFileReadCalls();
199
200
201   (*(Instance()->fSysWatch)) 
202     << hname   <<"\t"               // hname - hostname  
203     << sname    <<"\t"              // stamp name
204     << id0      <<"\t"
205     << id1      <<"\t"
206     << id2      <<"\t"
207     <<setprecision(15)<< first    <<"\t"              // first stamp               
208     //
209     <<setprecision(15)<< stamp.GetSec()+stamp.GetNanoSec()/1000000000.<<"\t"         // time  - time stamp in seconds
210     << memInfo.fMemUsed<<"\t"       // system info 
211     << memInfo.fSwapUsed<<"\t"      //
212     << cpuInfo.fUser <<"\t"         //
213     << cpuInfo.fSys  <<"\t"         //
214     // 
215     <<setprecision(15)<< procInfo.fMemResident/1024.<<"\t"  // process info
216     <<setprecision(15)<< procInfo.fMemVirtual/1024.<<"\t"   //    
217     << procInfo.fCpuUser<<"\t"      //
218     << procInfo.fCpuSys<<"\t"       //
219     //
220     <<setprecision(15)<< stampOld.GetSec()+stampOld.GetNanoSec()/1000000000.<<"\t"         // time  - time stamp in seconds
221     << memInfoOld.fMemUsed<<"\t"       // system info - previous
222     << memInfoOld.fSwapUsed<<"\t"      //
223     << cpuInfoOld.fUser <<"\t"         //
224     << cpuInfoOld.fSys  <<"\t"         //
225     // 
226     <<setprecision(15)<< procInfoOld.fMemResident/1024.<<"\t"  // process info -previous
227     <<setprecision(15)<< procInfoOld.fMemVirtual/1024.<<"\t"   //    
228     << procInfoOld.fCpuUser<<"\t"      //
229     << procInfoOld.fCpuSys<<"\t"       //
230     //
231     <<fileBytesRead<<"\t"           // file IO information
232     <<fileBytesWritten<<"\t"        //    
233     <<fileCounter<<"\t"             //
234     <<fileReadCalls<<"\t"            //
235     << endl;
236
237   stampOld   = stamp;
238   cpuInfoOld = cpuInfo;
239   memInfoOld = memInfo;
240   procInfoOld= procInfo;
241
242   //  if (fInstance->fMemStat) fInstance->fMemStat->AddStamps(sname);
243   for (Int_t icallback=0; icallback<Instance()->fNCallBack; icallback++){
244     Instance()->fCallBackFunc[icallback](sname);
245   }
246   entry++;
247 }
248
249
250 TTree * AliSysInfo::MakeTree(const char *lname){
251   // char * lname = "syswatch.log"
252   TTree * tree = new TTree;
253   tree->ReadFile(lname);
254   tree->SetAlias("deltaT","stampSec-stampOldSec");
255   tree->SetAlias("T","stampSec-first");
256   tree->SetAlias("deltaVM","(pI.fMemVirtual-pIOld.fMemVirtual)");
257   tree->SetAlias("VM","pI.fMemVirtual");
258   return tree;
259 }
260
261
262 Bool_t AliSysInfo::Contain(const char * str1, const char * str2){
263   //
264   //
265   //
266   TString str(str1);
267   return str.Contains(str2);
268 }
269
270
271
272 void AliSysInfo::OpenMemStat(){
273   //
274   //
275   //
276   //USE IFDEF if MEMSTAT ENABLED  
277   //  Instance()->fMemStat = TMemStatManager::GetInstance();
278   //   Instance()->fMemStat->SetAutoStamp(10000000, 10000000,1000000);
279   //   Instance()->fMemStat->Enable();  
280 }
281
282 void AliSysInfo::CloseMemStat(){
283   //
284   //
285   //
286   //USE IFDEF if MEMSTAT ENABLED
287   //if (Instance()->fMemStat  == TMemStatManager::GetInstance()) Instance()->fMemStat->Close();
288   //Instance()->fMemStat=0;
289 }
290
291
292
293 void AliSysInfo::AddCallBack(StampCallback_t callback){
294   //
295   // add cal back function
296   //
297   AliSysInfo *info =  Instance();
298   if (!info->fCallBackFunc)
299     info->fCallBackFunc = new StampCallback_t[100];
300   info->fCallBackFunc[info->fNCallBack]=callback;
301   info->fNCallBack++;
302 }
303
304
305
306 TTree*  AliSysInfo::Test(){
307   //
308   // Test example for AliSysInfo:
309   // 1. Make huge memory leak
310   // 2. Slow down execution
311   /*
312     To use the test:
313     TTree * tree = AliSysInfo::Test();
314     // Make alias what we set as input
315     tree->SetAlias("deltaVMIn","(id0*100000+id1*10000+id2*1000)/1000000.")
316     tree->SetAlias("deltaVM","(pI.fMemVirtual-pIOld.fMemVirtual)");
317     tree->SetAlias("deltaTIn","(id1+id0*10)");
318     tree->SetAlias("deltaT","stampSec-stampOldSec");
319     //
320     tree->SetMarkerStyle(23); tree->SetMarkerSize(0.5);
321     // Memory usage
322     tree->Draw("deltaVM:deltaVMIn","Entry$>0");
323     // or alternative
324     tree->Draw("deltaVM:deltaVMIn","Entry$>0","prof");
325     //
326     // draw time usage
327     tree->Draw("deltaT:deltaTIn","Entry$>0"); 
328   */
329   //
330   // The delta of VM as obtained from the AliSysInfo starts to be proportional
331   // to  the input allocation after 0.12 MBy (and it is system dependent) 
332   // Bellow these limit the deltaVM can be used only in mean.
333   // (compare first and  profile histogram)
334   for (Int_t id0=0; id0<5; id0++)
335     for (Int_t id1=1; id1<10; id1++)
336       for (Int_t id2=0; id2<20; id2++){
337         new Char_t[id2*1000+id1*10000+id0*100000];  // emulate memory leak
338         gSystem->Sleep(id1+id0*10);         // emulate CPU usage 
339         AliSysInfo::AddStamp("Leak",id0,id1,id2);
340       }
341   TTree * tree = AliSysInfo::MakeTree("syswatch.log");
342   return tree;  
343 }
344
345 Double_t AliSysInfo::EstimateObjectSize(TObject* object){
346   //
347   // Estimate size of object as represented in the memory size in bytes
348   // Warnings:
349   //  1. Only data memebrs which are persistent are counted
350   //  2. Local copy of the object is temporary created in memory
351   //  3. Do not use it in standard programs, time and memory consument procedure
352   //
353   if (!object) return 0;
354   TBufferFile * file = new TBufferFile(TBuffer::kWrite);
355   file->WriteObject(object);
356   Double_t size=file->Length();
357   delete file;
358   return size;
359 }