33 const char* cpu_stat_file =
"/proc/stat";
35 fStatFp = fopen (cpu_stat_file,
"r");
37 EOUT(
"fopen of %s failed", cpu_stat_file);
44 snprintf(fname,
sizeof(fname),
"/proc/%d/status",
id);
48 EOUT(
"fopen of %s failed", fname);
57 if (fclose (fStatFp) != 0)
58 EOUT(
"fclose of stat file failed");
61 if (fclose (fProcStatFp) != 0)
62 EOUT(
"fclose of proc stat file failed");
67 if (fStatFp==0)
return false;
77 while (fgets(buffer,
sizeof(buffer), fStatFp)) {
79 if (strncmp (buffer,
"cpu", 3)!=0)
break;
81 const char* info = buffer;
83 while ((*info!=
' ') && (*info != 0)) info++;
86 unsigned long curr_user(0), curr_nice(0), curr_sys(0), curr_idle(0);
88 sscanf(info,
"%lu %lu %lu %lu", &curr_user, &curr_nice, &curr_sys, &curr_idle);
94 curr_user += curr_nice;
96 unsigned long user(0), sys(0), idle(0);
98 if (cnt>=fCPUs.size()) {
101 user = curr_user - fCPUs[cnt].last_user;
102 sys = curr_sys - fCPUs[cnt].last_sys;
103 idle = curr_idle - fCPUs[cnt].last_idle;
106 unsigned long total =
user + sys + idle;
108 fCPUs[cnt].user_util = 1. *
user / total;
109 fCPUs[cnt].sys_util = 1. * sys / total;
110 fCPUs[cnt].cpu_util = 1. * (
user + sys) / total;
112 fCPUs[cnt].user_util = 0.;
113 fCPUs[cnt].sys_util = 0.;
114 fCPUs[cnt].cpu_util = 0.;
117 fCPUs[cnt].last_user = curr_user;
118 fCPUs[cnt].last_sys = curr_sys;
119 fCPUs[cnt].last_idle = curr_idle;
124 if (fProcStatFp != 0) {
128 while (fgets(buffer,
sizeof(buffer), fProcStatFp)) {
129 if (strncmp(buffer,
"VmSize:", 7)==0)
130 sscanf(buffer + 7,
"%lu", &fVmSize);
132 if (strncmp(buffer,
"VmPeak:", 7)==0)
133 sscanf(buffer + 7,
"%lu", &fVmPeak);
135 if (strncmp(buffer,
"Threads:", 8)==0) {
136 sscanf(buffer + 8,
"%lu", &fNumThreads);
156 snprintf(fname,
sizeof(fname),
"/proc/%d/status",
id);
158 FILE* f = fopen(fname,
"r");
164 while(fgets(buf,
sizeof(buf), f)) {
165 int rc = sscanf(buf,
"VmSize: %ld", &sz);
179 fMeasureInterval = 0;
200 if (now>=firstoper) {
203 totalpacketsize+=size;
207 if ((fPoints!=0) && (fMeasureInterval>0)) {
208 long npnt = lrint((now - firstoper) / fMeasureInterval);
209 if ((npnt>=0) && (npnt<fMeasurePoints))
210 fPoints[npnt] += size / fMeasureInterval;
224 fMeasureInterval = 0;
233 double totaltm = GetTotalTime();
234 if ((numoper<2) || (totaltm<=0.))
return 0.;
235 return totalpacketsize / totaltm * 1e-6;
240 return lastoper - firstoper;
245 if (numoper<=0)
return 0;
246 return totalpacketsize/numoper;
252 fMeasureInterval = interval_sec;
253 fMeasurePoints = npoints;
254 fPoints =
new double[fMeasurePoints];
255 for (
int n=0;n<fMeasurePoints;n++)
263 if (nrates<=0)
return;
267 for (
int nr=0;nr<nrates;nr++)
273 if ((npoints<=0) || (interval<=0))
return;
275 FILE* f = fopen(fname,
"w");
278 for (
int n=0;n<npoints;n++) {
279 fprintf(f,
"%7.1f", n*interval*1e3);
281 for (
int nr=0;nr<nrates;nr++) {
282 if (rates[nr]==0)
continue;
283 fprintf(f,
"\t%5.1f", rates[nr]->fPoints[n]);
286 if (withsum) fprintf(f,
"\t%5.1f\n", sum);
287 else fprintf(f,
"\n");
296 SaveRatesInFile(fname, &rate, 1,
false);
313 AllocateHist(0, 0., 1.);
318 if (hist) {
delete[] hist; hist = 0; nhist = 0; }
320 if ((nbins>0) && (xmax>xmin)) {
321 hist =
new long[nbins+2];
325 for (
int n=0;n<nhist+2;n++) hist[n] = 0;
338 for (
int n=0;n<nhist+2;n++)
349 if (zn>max) max = zn;
else
350 if (zn<min) min = zn;
358 if (zn<hist_min) hist[0]++;
else
359 if (zn>=hist_max) hist[nhist+1]++;
else {
361 long bin = lrint((zn - hist_min) / (hist_max-hist_min) * nhist + 0.5);
362 if ((bin>0) && (bin<=nhist)) hist[bin]++;
363 else EOUT(
"Bin error bin = %ld nhist = %d", bin, nhist);
370 if (num<=1)
return 0.;
371 return sqrt(sum2/num - Mean()*Mean());
377 DOUT0(
"%s = %f +- %f (min = %f, max = %f)",name,Mean(), Dev(), min, max);
379 DOUT0(
"%s = %f +- %f",name,Mean(), Dev());
384 if ((hist==0) || (nhist==0))
return;
387 for (
int n=0;n<nhist+2;n++) sum0+=hist[n];
390 DOUT1(
"Below %5.2f cnt = %3ld %5.1f", hist_min, hist[0], 100.*hist[0]/sum0);
392 for (
int n=1;n<=nhist;n++) {
394 DOUT1(
"Bin%02d x:%5.2f = %3ld %5.1f", n, (n - 0.5) / nhist * (hist_max-hist_min) + hist_min, hist[n], 100.*sum1/sum0);
396 DOUT1(
"Over %5.2f cnt = %3ld", hist_max, hist[nhist+1]);
void Show(const char *name, bool showextr=false)
void AllocateHist(int nbins, double xmin, double xmax)
CpuStatistic(bool withmem=false)
static long GetProcVirtMem()
Helper class to calculate data rate.
static void SaveRatesInFile(const char *fname, Ratemeter **rates, int nrates, bool withsum=false)
void SaveInFile(const char *fname)
void DoMeasure(double interval_sec, long npoints, double firsttm=0.)
void Packet(int size, double tm=0.)
double AsDouble() const
Return time stamp in form of double (in seconds)