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[PATH_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 procStat(std::ostringstream *out) {
158  tickspersec = sysconf(_SC_CLK_TCK);
159  input = NULL;
160 
161  std::ostringstream ost;
162  ost << "/proc/" << getpid() << "/stat";
163  input = fopen(ost.str().c_str(), "r");
164 
165 
166  readone(&pid);
167  readstr(tcomm);
168  readchar(&state);
169  readone(&ppid);
170  readone(&pgid);
171  readone(&sid);
172  readone(&tty_nr);
173  readone(&tty_pgrp);
174  readone(&flags);
175  readone(&min_flt);
176  readone(&cmin_flt);
177  readone(&maj_flt);
178  readone(&cmaj_flt);
179  readone(&utime);
180  readone(&stimev);
181  readone(&cutime);
182  readone(&cstime);
183  readone(&priority);
184  readone(&nicev);
188  readone(&vsize);
189  readone(&rss);
190  readone(&rsslim);
192  readone(&end_code);
194  readone(&esp);
195  readone(&eip);
196  readone(&pending);
197  readone(&blocked);
198  readone(&sigign);
199  readone(&sigcatch);
200  readone(&wchan);
201  readone(&zero1);
202  readone(&zero2);
204  readone(&cpu);
206  readone(&policy);
207 
208  {
209  printone("pid", pid); *out << obuf;
210  printstr("tcomm", tcomm); *out << obuf;
211  printchar("state", state); *out << obuf;
212  printone("ppid", ppid); *out << obuf;
213  printone("pgid", pgid); *out << obuf;
214  printone("sid", sid); *out << obuf;
215  printone("tty_nr", tty_nr); *out << obuf;
216  printone("tty_pgrp", tty_pgrp); *out << obuf;
217  printone("flags", flags); *out << obuf;
218  printone("min_flt", min_flt); *out << obuf;
219  printone("cmin_flt", cmin_flt); *out << obuf;
220  printone("maj_flt", maj_flt); *out << obuf;
221  printone("cmaj_flt", cmaj_flt); *out << obuf;
222  printtime("utime", utime); *out << obuf;
223  printtime("stime", stimev); *out << obuf;
224  printtime("cutime", cutime); *out << obuf;
225  printtime("cstime", cstime); *out << obuf;
226  printone("priority", priority); *out << obuf;
227  printone("nice", nicev); *out << obuf;
228  printone("num_threads", num_threads); *out << obuf;
229  printtime("it_real_value", it_real_value); *out << obuf;
230  printtimediff("start_time", start_time); *out << obuf;
231  printone("vsize", vsize); *out << obuf;
232  printone("rss", rss); *out << obuf;
233  printone("rsslim", rsslim); *out << obuf;
234  printone("start_code", start_code); *out << obuf;
235  printone("end_code", end_code); *out << obuf;
236  printone("start_stack", start_stack); *out << obuf;
237  printone("esp", esp); *out << obuf;
238  printone("eip", eip); *out << obuf;
239  printonex("pending", pending); *out << obuf;
240  printonex("blocked", blocked); *out << obuf;
241  printonex("sigign", sigign); *out << obuf;
242  printonex("sigcatch", sigcatch); *out << obuf;
243  printone("wchan", wchan); *out << obuf;
244  printone("zero1", zero1); *out << obuf;
245  printone("zero2", zero2); *out << obuf;
246  printonex("exit_signal", exit_signal); *out << obuf;
247  printone("cpu", cpu); *out << obuf;
248  printone("rt_priority", rt_priority); *out << obuf;
249  printone("policy", policy); *out << obuf;
250  }
251  fclose(input);
252  }
253 
254 
255 
256 
257 
258  //Derived from:
259  /* vi: set sw=4 ts=4: */
260  /*
261  * Mini uptime implementation for busybox
262  *
263  * Copyright (C) 1999,2000 by Lineo, inc.
264  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
265  *
266  * This program is free software; you can redistribute it and/or modify
267  * it under the terms of the GNU General Public License as published by
268  * the Free Software Foundation; either version 2 of the License, or
269  * (at your option) any later version.
270  *
271  * This program is distributed in the hope that it will be useful,
272  * but WITHOUT ANY WARRANTY; without even the implied warranty of
273  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
274  * General Public License for more details.
275  *
276  * You should have received a copy of the GNU General Public License
277  * along with this program; if not, write to the Free Software
278  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
279  *
280  */
281 
282  /* This version of uptime doesn't display the number of users on the system,
283  * since busybox init doesn't mess with utmp. For folks using utmp that are
284  * just dying to have # of users reported, feel free to write it as some type
285  * of BB_FEATURE_UMTP_SUPPORT #define
286  */
287 
288 
289 
290  void uptime(std::ostringstream *out)
291  {
292 #ifdef linux
293  int updays, uphours, upminutes;
294  struct sysinfo info;
295  struct tm *current_time;
296  time_t current_secs;
297 
298  time(&current_secs);
299  current_time = localtime(&current_secs);
300 
301  sysinfo(&info);
302 
303  *out << std::setw(2)
304  << (current_time->tm_hour%12 ? current_time->tm_hour%12 : 12)
305  << ":"
306  << current_time->tm_min
307  << (current_time->tm_hour > 11 ? " pm, " : " am, ")
308  << " up ";
309  updays = (int) info.uptime / (60*60*24);
310  if (updays)
311  *out << updays << " day" << ((updays != 1) ? "s " : " ");
312  upminutes = (int) info.uptime / 60;
313  uphours = (upminutes / 60) % 24;
314  upminutes %= 60;
315  if(uphours)
316  *out << std::setw(2) << uphours << ":" << upminutes;
317  else
318  *out << upminutes << " minutes ";
319 
320  *out << " - load average "
321  << LOAD_INT(info.loads[0]) << " "
322  << LOAD_FRAC(info.loads[0]) << " "
323  << LOAD_INT(info.loads[1]) << " "
324  << LOAD_FRAC(info.loads[1]) << " "
325  << LOAD_INT(info.loads[2]) << " "
326  << LOAD_FRAC(info.loads[2]) << " ";
327  *out << " used memory " << std::setw(3)
328  << (float(info.totalram-info.freeram)/float(info.totalram))*100 << "%";
329 #else
330  // FIXME: one could probably use `clock_get_uptime` and similar on
331  // macosx to obtain at least part of the information.
332  *out << "Unable to retrieve uptime information on this platform.";
333 #endif
334  }
335  void mDiv(std::ostringstream *out, std::string name){
336  *out << "<div id=\"" << name << "\">";
337  }
338  void cDiv(std::ostringstream *out){
339  *out << "</div>";
340  }
341  void mDiv(std::ostringstream *out, std::string name, std::string value){
342  mDiv(out,name);
343  *out << value;
344  cDiv(out);
345  }
346  void mDiv(std::ostringstream *out, std::string name, unsigned int value){
347  mDiv(out,name);
348  *out << value;
349  cDiv(out);
350  }
351  } // namespace utils
352 } //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:157
FILE * input
Definition: procUtils.cc:123
void cDiv(std::ostringstream *out)
Definition: procUtils.cc:338
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
char tcomm[PATH_MAX]
Definition: procUtils.cc:74
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
void printonex(const char *name, num x)
Definition: procUtils.cc:131
#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:335
void readstr(char *x)
Definition: procUtils.cc:127
num cmaj_flt
Definition: procUtils.cc:87
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:290