20 #include "StMemStat.h"
27 Double_t StMemStat::fgUsed=0;
28 TList *StMemStat::fgList=0;
31 #define LOWEST_VAL 0.0000001
34 //______________________________________________________________________________
35 StMemStat::StMemStat(
const char *name):TNamed(name,
"")
37 int n = (
char*)&fTally - (
char*)&fLast +
sizeof(fTally);
41 if (!fgList) fgList=
new TList;
45 StMemStat::~StMemStat()
48 if (!fgList->First()) {
delete fgList; fgList=0;}
52 void StMemStat::Start()
58 void StMemStat::Stop()
61 Double_t dif = Used() - fLast;
64 if ( fabs(dif) < LOWEST_VAL ) dif = 0.0;
65 if ( dif < fMin ) fMin = dif;
66 if ( dif > fMax ) fMax = dif;
73 void StMemStat::Print(
const char *)
const
76 Double_t aver = fAver/fTally;
77 Double_t rms = ::sqrt(fabs(fRms/fTally - aver*aver));
80 if ( fabs(aver) < LOWEST_VAL ) aver = 0.0;
81 if ( rms < LOWEST_VAL ) rms = 0.0;
83 printf(
"%40.40s(%d)%12.6f%12.6f%12.6f%12.6f\n",
84 GetName(),fTally,fMin,aver,fMax,rms);
89 #define NUMTICKS (40+4*12+5)
91 Double_t dmin=1.e+33, daver=0, dmax=-1.e+33, drms=0, dtally=0, dmp;
96 printf(
"%40.40s%12s%12s%12s%12s\n",
97 "StMemStat::Summary(calls)",
"Min ",
"Aver ",
"Max ",
"RMS ");
99 for( i=0 ; i < NUMTICKS ; i++) printf(
"=");
102 TListIter next(fgList);
105 if(!m->fTally)
continue;
108 if (m->fMin < dmin) dmin=m->fMin;
109 if (m->fMax > dmax) dmax=m->fMax;
110 dmp = m->fAver/m->fTally;
112 drms += fabs(m->fRms/m->fTally-dmp*dmp);
117 for( i=0 ; i < NUMTICKS ; i++) printf(
"-");
121 drms = ::sqrt(fabs(drms));
122 printf(
"%40.40s(%d)%12.6f%12.6f%12.6f%12.6f\n",
123 "Total", (
int)dtally, dmin, daver, dmax, drms);
125 for( i=0 ; i < NUMTICKS ; i++) printf(
"=");
131 void StMemStat::doPs(std::string who, std::string where)
133 if (!gSystem->Getenv(
"StarEndMakerShell"))
136 PrintMem(
FormString(
"QAInfo: doPs for %20s:%12s \t", who.c_str(), where.c_str()));
137 SaveProcStatus(where +
':' + who);
144 struct mallinfo info;
146 return double(info.uordblks + info.usmblks)/1024/1024;
151 struct mallinfo info;
153 return double(info.fordblks + info.fsmblks)/1024/1024;
160 int pid = ::getpid();
162 sprintf(line,
"/proc/%d/status",pid);
164 FILE *proc = fopen(line,
"r");
166 while (fgets(line,100,proc)) {
167 if (strncmp(
"VmSize:",line,7)==0) {
170 res = (strtod(line+7,&aft));
171 while ((++aft)[0]==
' '){}
173 if (strncmp(
"kB",aft,2)==0) b = 1024;
174 if (strncmp(
"mB",aft,2)==0) b = 1024*1024;
175 if (strncmp(
"gB",aft,2)==0) b = 1024*1024*1024;
176 res = (res*b)/(1024*1024);
185 ps = (
char*)malloc(25);
186 sprintf(ps,
"/bin/ps -l -p %d",pid);
188 FILE *pipe = ::popen(ps,
"r");
189 if (!pipe)
return 0.;
193 while( !feof( pipe ) ) {
195 if(!fgets( psBuf+1, 128, pipe))
continue;
199 for (c=psBuf; c[0]; c++) {
200 if (c[0]==
' ' && c[1]!=
' ') ifild++;
201 if (ifild == 10)
break;
203 res = (Double_t) atoi(c+1);
207 res *=::getpagesize()/(1024.*1024.);
214 printf(
"%s\n", AsString(prefix).c_str());
220 double alloc_used = Used();
221 double alloc_used_prev = fgUsed;
222 double alloc_free = Free();
223 double total = ProgSize();
226 return FormString(
"%s\t total =%10.6f heap =%10.6f and %10.6f(%+10.6f)",
227 prefix.c_str(), total, alloc_used, alloc_free, alloc_used - alloc_used_prev);
233 Double_t used = Used();
234 printf(
"\nStMemStat: ");
235 printf(
"StMemStat::heap =%10.6f(%+10.6f)\n",used,used-fgUsed);
239 void StMemStat::Streamer(TBuffer&)
245 StMemStat::ProcStatusMap_t StMemStat::ReadProcStatus()
247 ProcStatusMap_t tokens{
257 std::ifstream procfile(
"/proc/self/status");
260 while (std::getline(procfile, line))
262 std::istringstream iss(line);
267 for (ProcStatus_t& token : tokens)
269 if (label.find(token.first) != std::string::npos) {
272 token.second /= 1024;
283 static std::ofstream outfile(
"proc_status.csv");
284 static bool firstCall =
true;
286 const ProcStatusMap_t& tokens = ReadProcStatus();
289 outfile <<
"callerId";
290 for (
const ProcStatus_t& token : tokens)
291 outfile <<
", " << token.first;
296 for (
const ProcStatus_t& token : tokens)
297 outfile <<
", " << token.second;
static std::string AsString(std::string prefix="")
Returns a string with memory utilization estimates for the current process.
static void PrintMem(std::string prefix="")
std::string FormString(const std::string &format, Args...args)
static void SaveProcStatus(std::string callerId)
static Double_t ProgSize()