CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
procUtils.cc
Go to the documentation of this file.
1  /*
2  * Displays linux /proc/pid/stat in human-readable format
3  *
4  * Build: gcc -o procstat procstat.c
5  * Usage: procstat pid
6  * cat /proc/pid/stat | procstat
7  *
8  * Homepage: http://www.brokestream.com/procstat.html
9  * Version : 2009-03-05
10  *
11  * Ivan Tikhonov, http://www.brokestream.com, kefeer@netangels.ru
12  *
13  * 2007-09-19 changed HZ=100 error to warning
14  *
15  * 2009-03-05 tickspersec are taken from sysconf (Sabuj Pattanayek)
16  *
17  */
18 
19 
20 /* Copyright (C) 2009 Ivan Tikhonov
21 
22  This software is provided 'as-is', without any express or implied
23  warranty. In no event will the authors be held liable for any damages
24  arising from the use of this software.
25 
26  Permission is granted to anyone to use this software for any purpose,
27  including commercial applications, and to alter it and redistribute it
28  freely, subject to the following restrictions:
29 
30  1. The origin of this software must not be misrepresented; you must not
31  claim that you wrote the original software. If you use this software
32  in a product, an acknowledgment in the product documentation would be
33  appreciated but is not required.
34  2. Altered source versions must be plainly marked as such, and must not be
35  misrepresented as being the original software.
36  3. This notice may not be removed or altered from any source distribution.
37 
38  Ivan Tikhonov, kefeer@brokestream.com
39 
40 */
41 
42 
43 #define FSHIFT 16 /* nr of bits of precision */
44 #define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
45 #define LOAD_INT(x) ((x) >> FSHIFT)
46 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
47 
48 #ifdef linux
49 #include <sys/sysinfo.h>
50 #endif
51 #include <errno.h>
52 #include <iostream>
53 #include <iomanip>
54 
55 #include <stdio.h>
56 #include <unistd.h>
57 #include <time.h>
58 #ifdef linux
59 #include <linux/limits.h>
60 #endif
61 #include <sys/times.h>
62 #include <sstream>
63 #include "procUtils.h"
64 
65 namespace evf{
66  namespace utils{
67 
68 
69 
70 
71  typedef long long int num;
72 
74  char tcomm[FILENAME_MAX];
75  char state;
76 
82 
90 
97 
98  unsigned long long start_time;
99 
108 
120 
122  char obuf[4096];
123  FILE *input;
124 
125  void readone(num *x) { fscanf(input, "%lld ", x); }
126  void readunsigned(unsigned long long *x) { fscanf(input, "%llu ", x); }
127  void readstr(char *x) { fscanf(input, "%s ", x);}
128  void readchar(char *x) { fscanf(input, "%c ", x);}
129 
130  void printone(const char *name, num x) { sprintf(obuf,"%20s: %lld\n", name, x);}
131  void printonex(const char *name, num x) { sprintf(obuf,"%20s: %016llx\n", name, x);}
132  void printunsigned(const char *name, unsigned long long x) { sprintf(obuf,"%20s: %llu\n", name, x);}
133  void printchar(const char *name, char x) { sprintf(obuf,"%20s: %c\n", name, x);}
134  void printstr(const char *name, char *x) { sprintf(obuf,"%20s: %s\n", name, x);}
135  void printtime(const char *name, num x) { sprintf(obuf,"%20s: %f\n", name, (((double)x) / tickspersec));}
136 
138  FILE *procuptime;
139  int sec, ssec;
140 
141  procuptime = fopen("/proc/uptime", "r");
142  fscanf(procuptime, "%d.%ds", &sec, &ssec);
143  fclose(procuptime);
144  return (sec*tickspersec)+ssec;
145  }
146 
147  void printtimediff(const char *name, num x) {
148  int sinceboot = gettimesinceboot();
149  int running = sinceboot - x;
150  time_t rt = time(NULL) - (running / tickspersec);
151  char buf[1024];
152 
153  strftime(buf, sizeof(buf), "%m.%d %H:%M", localtime(&rt));
154  sprintf(obuf,"%20s: %s (%lu.%lus)\n", name, buf, running / tickspersec, running % tickspersec);
155  }
156 
157  void procCpuStat(unsigned long long &idleJiffies,unsigned long long &allJiffies) {
158  //read one
159  if (input==NULL)
160  input = fopen("/proc/stat", "r");
161  if (input==NULL) return;
162  char cpu[10];
163  readstr(cpu);
164  int count=0;
165  long long last=0;
166  do {
167  readone(&last);
168  if (count==3) idleJiffies+=last;
169  allJiffies+=last;
170  }
171  while (last && count++<20);
172  fclose(input);
173  input=NULL;
174  }
175 
176  void procStat(std::ostringstream *out) {
177  tickspersec = sysconf(_SC_CLK_TCK);
178  input = NULL;
179 
180  std::ostringstream ost;
181  ost << "/proc/" << getpid() << "/stat";
182  input = fopen(ost.str().c_str(), "r");
183 
184 
185  readone(&pid);
186  readstr(tcomm);
187  readchar(&state);
188  readone(&ppid);
189  readone(&pgid);
190  readone(&sid);
191  readone(&tty_nr);
192  readone(&tty_pgrp);
193  readone(&flags);
194  readone(&min_flt);
195  readone(&cmin_flt);
196  readone(&maj_flt);
197  readone(&cmaj_flt);
198  readone(&utime);
199  readone(&stimev);
200  readone(&cutime);
201  readone(&cstime);
202  readone(&priority);
203  readone(&nicev);
207  readone(&vsize);
208  readone(&rss);
209  readone(&rsslim);
211  readone(&end_code);
213  readone(&esp);
214  readone(&eip);
215  readone(&pending);
216  readone(&blocked);
217  readone(&sigign);
218  readone(&sigcatch);
219  readone(&wchan);
220  readone(&zero1);
221  readone(&zero2);
223  readone(&cpu);
225  readone(&policy);
226 
227  {
228  printone("pid", pid); *out << obuf;
229  printstr("tcomm", tcomm); *out << obuf;
230  printchar("state", state); *out << obuf;
231  printone("ppid", ppid); *out << obuf;
232  printone("pgid", pgid); *out << obuf;
233  printone("sid", sid); *out << obuf;
234  printone("tty_nr", tty_nr); *out << obuf;
235  printone("tty_pgrp", tty_pgrp); *out << obuf;
236  printone("flags", flags); *out << obuf;
237  printone("min_flt", min_flt); *out << obuf;
238  printone("cmin_flt", cmin_flt); *out << obuf;
239  printone("maj_flt", maj_flt); *out << obuf;
240  printone("cmaj_flt", cmaj_flt); *out << obuf;
241  printtime("utime", utime); *out << obuf;
242  printtime("stime", stimev); *out << obuf;
243  printtime("cutime", cutime); *out << obuf;
244  printtime("cstime", cstime); *out << obuf;
245  printone("priority", priority); *out << obuf;
246  printone("nice", nicev); *out << obuf;
247  printone("num_threads", num_threads); *out << obuf;
248  printtime("it_real_value", it_real_value); *out << obuf;
249  printtimediff("start_time", start_time); *out << obuf;
250  printone("vsize", vsize); *out << obuf;
251  printone("rss", rss); *out << obuf;
252  printone("rsslim", rsslim); *out << obuf;
253  printone("start_code", start_code); *out << obuf;
254  printone("end_code", end_code); *out << obuf;
255  printone("start_stack", start_stack); *out << obuf;
256  printone("esp", esp); *out << obuf;
257  printone("eip", eip); *out << obuf;
258  printonex("pending", pending); *out << obuf;
259  printonex("blocked", blocked); *out << obuf;
260  printonex("sigign", sigign); *out << obuf;
261  printonex("sigcatch", sigcatch); *out << obuf;
262  printone("wchan", wchan); *out << obuf;
263  printone("zero1", zero1); *out << obuf;
264  printone("zero2", zero2); *out << obuf;
265  printonex("exit_signal", exit_signal); *out << obuf;
266  printone("cpu", cpu); *out << obuf;
267  printone("rt_priority", rt_priority); *out << obuf;
268  printone("policy", policy); *out << obuf;
269  }
270  fclose(input);
271  }
272 
273 
274 
275 
276 
277  //Derived from:
278  /* vi: set sw=4 ts=4: */
279  /*
280  * Mini uptime implementation for busybox
281  *
282  * Copyright (C) 1999,2000 by Lineo, inc.
283  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
284  *
285  * This program is free software; you can redistribute it and/or modify
286  * it under the terms of the GNU General Public License as published by
287  * the Free Software Foundation; either version 2 of the License, or
288  * (at your option) any later version.
289  *
290  * This program is distributed in the hope that it will be useful,
291  * but WITHOUT ANY WARRANTY; without even the implied warranty of
292  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
293  * General Public License for more details.
294  *
295  * You should have received a copy of the GNU General Public License
296  * along with this program; if not, write to the Free Software
297  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
298  *
299  */
300 
301  /* This version of uptime doesn't display the number of users on the system,
302  * since busybox init doesn't mess with utmp. For folks using utmp that are
303  * just dying to have # of users reported, feel free to write it as some type
304  * of BB_FEATURE_UMTP_SUPPORT #define
305  */
306 
307 
308 
309  void uptime(std::ostringstream *out)
310  {
311 #ifdef linux
312  int updays, uphours, upminutes;
313  struct sysinfo info;
314  struct tm *current_time;
315  time_t current_secs;
316 
317  time(&current_secs);
318  current_time = localtime(&current_secs);
319 
320  sysinfo(&info);
321 
322  *out << std::setw(2)
323  << (current_time->tm_hour%12 ? current_time->tm_hour%12 : 12)
324  << ":"
325  << current_time->tm_min
326  << (current_time->tm_hour > 11 ? " pm, " : " am, ")
327  << " up ";
328  updays = (int) info.uptime / (60*60*24);
329  if (updays)
330  *out << updays << " day" << ((updays != 1) ? "s " : " ");
331  upminutes = (int) info.uptime / 60;
332  uphours = (upminutes / 60) % 24;
333  upminutes %= 60;
334  if(uphours)
335  *out << std::setw(2) << uphours << ":" << upminutes;
336  else
337  *out << upminutes << " minutes ";
338 
339  *out << " - load average "
340  << LOAD_INT(info.loads[0]) << " "
341  << LOAD_FRAC(info.loads[0]) << " "
342  << LOAD_INT(info.loads[1]) << " "
343  << LOAD_FRAC(info.loads[1]) << " "
344  << LOAD_INT(info.loads[2]) << " "
345  << LOAD_FRAC(info.loads[2]) << " ";
346  *out << " used memory " << std::setw(3)
347  << (float(info.totalram-info.freeram)/float(info.totalram))*100 << "%";
348 #else
349  // FIXME: one could probably use `clock_get_uptime` and similar on
350  // macosx to obtain at least part of the information.
351  *out << "Unable to retrieve uptime information on this platform.";
352 #endif
353  }
354  void mDiv(std::ostringstream *out, std::string name){
355  *out << "<div id=\"" << name << "\">";
356  }
357  void cDiv(std::ostringstream *out){
358  *out << "</div>";
359  }
360  void mDiv(std::ostringstream *out, std::string name, std::string value){
361  mDiv(out,name);
362  *out << value;
363  cDiv(out);
364  }
365  void mDiv(std::ostringstream *out, std::string name, unsigned int value){
366  mDiv(out,name);
367  *out << value;
368  cDiv(out);
369  }
370  } // namespace utils
371 } //namespace evf
num priority
Definition: procUtils.cc:93
num min_flt
Definition: procUtils.cc:84
void printstr(const char *name, char *x)
Definition: procUtils.cc:134
num tty_nr
Definition: procUtils.cc:80
void readunsigned(unsigned long long *x)
Definition: procUtils.cc:126
num exit_signal
Definition: procUtils.cc:116
long tickspersec
Definition: procUtils.cc:121
num cmin_flt
Definition: procUtils.cc:85
unsigned long long start_time
Definition: procUtils.cc:98
#define NULL
Definition: scimark2.h:8
num tty_pgrp
Definition: procUtils.cc:81
void procStat(std::ostringstream *out)
Definition: procUtils.cc:176
FILE * input
Definition: procUtils.cc:123
void cDiv(std::ostringstream *out)
Definition: procUtils.cc:357
void printtimediff(const char *name, num x)
Definition: procUtils.cc:147
num stimev
Definition: procUtils.cc:89
void printone(const char *name, num x)
Definition: procUtils.cc:130
void printunsigned(const char *name, unsigned long long x)
Definition: procUtils.cc:132
num cutime
Definition: procUtils.cc:91
void printchar(const char *name, char x)
Definition: procUtils.cc:133
void readchar(char *x)
Definition: procUtils.cc:128
num it_real_value
Definition: procUtils.cc:96
void printtime(const char *name, num x)
Definition: procUtils.cc:135
num end_code
Definition: procUtils.cc:104
char obuf[4096]
Definition: procUtils.cc:122
int gettimesinceboot()
Definition: procUtils.cc:137
num cstime
Definition: procUtils.cc:92
tuple out
Definition: dbtoconf.py:99
#define LOAD_INT(x)
Definition: procUtils.cc:45
void readone(num *x)
Definition: procUtils.cc:125
num start_code
Definition: procUtils.cc:103
long long int num
Definition: procUtils.cc:71
num sigcatch
Definition: procUtils.cc:112
char state
Definition: procUtils.cc:75
char tcomm[FILENAME_MAX]
Definition: procUtils.cc:74
void printonex(const char *name, num x)
Definition: procUtils.cc:131
void procCpuStat(unsigned long long &idleJiffies, unsigned long long &allJiffies)
Definition: procUtils.cc:157
#define LOAD_FRAC(x)
Definition: procUtils.cc:46
num maj_flt
Definition: procUtils.cc:86
void mDiv(std::ostringstream *out, std::string name)
Definition: procUtils.cc:354
void readstr(char *x)
Definition: procUtils.cc:127
num cmaj_flt
Definition: procUtils.cc:87
Definition: DDAxes.h:10
num num_threads
Definition: procUtils.cc:95
num rt_priority
Definition: procUtils.cc:118
num start_stack
Definition: procUtils.cc:105
void uptime(std::ostringstream *out)
Definition: procUtils.cc:309