Bug fix. Removed delete statement
[u/mrichter/AliRoot.git] / STEER / 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 //  The typical usage:
23 //  Make a set of stamps in the code in the place of interest
24 //  e.g. 
25 //
26 //  AliSysInfo::AddStamp("Start");
27 //
28 //  loader->UnloadRecPoints();
29 //    AliSysInfo::AddStamp(Form("LRec%s_%d",fgkDetectorName[iDet],eventNr), iDet,1,eventNr);
30 //
31
32 // The log file can be transformed to the tree - to make a visualization
33 // See $ALICE_ROOT/macros/PlotSysInfo.C as an example
34
35
36 #include <Riostream.h>
37 #include "AliLog.h"
38 #include "TStopwatch.h"
39 #include "TSystem.h"
40 #include "TTree.h"
41
42 #include "TTimeStamp.h"
43 #include "AliSysInfo.h"
44
45 //#include "TMemStatManager.h"  //USE IFDEF
46
47
48 ClassImp(AliSysInfo)
49
50 AliSysInfo* AliSysInfo::fInstance=0;
51
52 AliSysInfo::AliSysInfo():
53     TObject(),
54     fSysWatch(0),
55     fTimer(0),
56     fMemStat(0),
57     fCallBackFunc(0),
58     fNCallBack(0)
59 {
60   fTimer = new TStopwatch;
61   fSysWatch  = new fstream("syswatch.log", ios_base::out|ios_base::trunc);
62
63   //hname/C:sname/C:sec/I:mI.fMemUsed/F:mI.fSwapUsed/F:pI.fMemResident/F:pI.fMemVirtual/F:cI.fUser/F:cI.fSys/F:cI.fCpuUser/F:pI.fCpuSys/F
64
65
66   (*fSysWatch) <<"hname"<<"/C:"               // hname - hostname  
67                <<"sname"<<"/C:"              // stamp name
68                <<"id0"<<"/I:"                // 0 id
69                <<"id1"<<"/I:"                // 1 id
70                <<"id2"<<"/I:"                // 1 id
71                <<"first"<<"/I:"              // first stamp
72     //
73                <<"stampSec"<<"/I:"         // time  - time stamp in seconds
74                <<"mi.fMemUsed"<<"/F:"       // system info 
75                <<"mi.fSwapUsed"<<"/F:"      //
76                <<"cI.fUser"<<"/F:"         //
77                <<"cI.fSys"<<"/F:"         //
78     // 
79                <<"pI.fMemResident"<<"/F:"  // process info
80                <<"pI.fMemVirtual"<<"/F:"   //    
81                <<"pI.fCpuUser"<<"/F:"      //
82                <<"pI.fCpuSys"<<"/F:"       //
83     //
84                <<"stampOldSec"<<"/I:"         // time  - time stamp in seconds
85                <<"miOld.fMemUsed"<<"/F:"       // system info - previous
86                <<"miOld.fSwapUsed"<<"/F:"      //
87                <<"cIOld.fUser"<<"/F:"         //
88                <<"cIOld.fSys"<<"/F:"         //
89     // 
90                <<"pIOld.fMemResident"<<"/F:"  // process info -previous
91                <<"pIOld.fMemVirtual"<<"/F:"   //    
92                <<"pIOld.fCpuUser"<<"/F:"      //
93                <<"pIOld.fCpuSys"<<"/F"       //
94                << endl;
95   
96 }
97
98
99
100
101 AliSysInfo * AliSysInfo::Instance(){
102   //
103   //
104   //
105   if (!fInstance){
106     fInstance = new AliSysInfo;
107   }
108   return fInstance;
109 }
110
111
112 void AliSysInfo::AddStamp(const char *sname, Int_t id0, Int_t id1, Int_t id2){
113   //
114   // 
115   //
116   //
117   TTimeStamp stamp;
118   CpuInfo_t  cpuInfo;
119   MemInfo_t  memInfo;
120   ProcInfo_t procInfo;  
121   gSystem->GetCpuInfo(&cpuInfo, 10);
122   gSystem->GetMemInfo(&memInfo);
123   gSystem->GetProcInfo(&procInfo);
124   procInfo.fMemVirtual/=1024;  //size in MBy
125   procInfo.fMemResident/=1024;  //size in MBy
126
127   const char * hname = gSystem->HostName();
128
129   static Int_t entry=0;
130   static Int_t first=stamp.GetSec();
131   //
132   static TTimeStamp stampOld;
133   static CpuInfo_t  cpuInfoOld;
134   static MemInfo_t  memInfoOld;
135   static ProcInfo_t procInfoOld;  
136
137
138   (*(Instance()->fSysWatch)) << hname   <<"\t"               // hname - hostname  
139                << sname    <<"\t"              // stamp name
140                << id0      <<"\t"
141                << id1      <<"\t"
142                << id2      <<"\t"
143                << first    <<"\t"              // first stamp               
144                //
145                << stamp.GetSec()<<"\t"         // time  - time stamp in seconds
146                << memInfo.fMemUsed<<"\t"       // system info 
147                << memInfo.fSwapUsed<<"\t"      //
148                << cpuInfo.fUser <<"\t"         //
149                << cpuInfo.fSys  <<"\t"         //
150                // 
151                << procInfo.fMemResident<<"\t"  // process info
152                << procInfo.fMemVirtual<<"\t"   //    
153                << procInfo.fCpuUser<<"\t"      //
154                << procInfo.fCpuSys<<"\t"       //
155     //
156                << stampOld.GetSec()<<"\t"         // time  - time stamp in seconds
157                << memInfoOld.fMemUsed<<"\t"       // system info - previous
158                << memInfoOld.fSwapUsed<<"\t"      //
159                << cpuInfoOld.fUser <<"\t"         //
160                << cpuInfoOld.fSys  <<"\t"         //
161                // 
162                << procInfoOld.fMemResident<<"\t"  // process info -previous
163                << procInfoOld.fMemVirtual<<"\t"   //    
164                << procInfoOld.fCpuUser<<"\t"      //
165                << procInfoOld.fCpuSys<<"\t"       //
166                << endl;
167   stampOld   = stamp;
168   cpuInfoOld = cpuInfo;
169   memInfoOld = memInfo;
170   procInfoOld= procInfo;
171
172   //  if (fInstance->fMemStat) fInstance->fMemStat->AddStamps(sname);
173   for (Int_t icallback=0; icallback<Instance()->fNCallBack; icallback++){
174     Instance()->fCallBackFunc[icallback](sname);
175   }
176   entry++;
177 }
178
179
180 TTree * AliSysInfo::MakeTree(const char *lname){
181   // char * lname = "syswatch.log"
182   TTree * tree = new TTree;
183   tree->ReadFile(lname);
184   tree->SetAlias("deltaT","stampSec-stampOldSec");
185   tree->SetAlias("T","stampSec-first");
186   tree->SetAlias("deltaVM","(pI.fMemVirtual-pIOld.fMemVirtual)");
187   tree->SetAlias("VM","pI.fMemVirtual");
188   return tree;
189 }
190
191
192 Bool_t AliSysInfo::Contain(const char * str1, const char * str2){
193   //
194   //
195   //
196   TString str(str1);
197   return str.Contains(str2);
198 }
199
200
201
202 void AliSysInfo::OpenMemStat(){
203   //
204   //
205   //
206   //USE IFDEF if MEMSTAT ENABLED  
207   //  Instance()->fMemStat = TMemStatManager::GetInstance();
208   //   Instance()->fMemStat->SetAutoStamp(10000000, 10000000,1000000);
209   //   Instance()->fMemStat->Enable();  
210 }
211
212 void AliSysInfo::CloseMemStat(){
213   //
214   //
215   //
216   //USE IFDEF if MEMSTAT ENABLED
217   //if (Instance()->fMemStat  == TMemStatManager::GetInstance()) Instance()->fMemStat->Close();
218   //Instance()->fMemStat=0;
219 }
220
221
222
223 void AliSysInfo::AddCallBack(StampCallback_t callback){
224   //
225   // add cal back function
226   //
227   AliSysInfo *info =  Instance();
228   if (!info->fCallBackFunc)
229     info->fCallBackFunc = new StampCallback_t[100];
230   info->fCallBackFunc[info->fNCallBack]=callback;
231   info->fNCallBack++;
232 }