All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ProcStats.cpp
Go to the documentation of this file.
1 // Class: ProcStats
2 // Purpose: To keep statistics on memory use
3 // Warning: Only Linux implementation at the present time...
4 #ifdef __ICC
5 // disable icc remark #2259: non-pointer conversion from "X" to "Y" may lose significant bits
6 // meant
7 #pragma warning(disable:2259)
8 #endif
9 
10 #include "ProcStats.h"
11 
12 #ifdef __linux
13 #include <unistd.h>
14 #include <iostream>
15 #include <sstream>
16 #include <fcntl.h>
17 #include <sys/types.h>
18 #include <sys/signal.h>
19 #include <sys/syscall.h>
20 #include <sys/procfs.h>
21 #include <cstdio>
22 
23 using std::cerr;
24 using std::cout;
25 using std::endl;
26 
27 /* Format of the Linux proc/stat (man 5 proc, kernel 2.6.35):
28  pid %d The process ID.
29 
30  comm %s The filename of the executable, in parentheses. This is visible
31  whether or not the executable is swapped out.
32 
33  state %c One character from the string "RSDZTW" where R is running, S is
34  sleeping in an interruptible wait, D is waiting in uninterruptible
35  disk sleep, Z is zombie, T is traced or stopped (on a signal), and
36  W is paging.
37 
38  ppid %d The PID of the parent.
39 
40  pgrp %d The process group ID of the process.
41 
42  session %d The session ID of the process.
43 
44  tty_nr %d The controlling terminal of the process. (The minor device number
45  is contained in the combination of bits 31 to 20 and 7 to 0; the
46  major device number is in bits 15 t0 8.)
47 
48  tpgid %d The ID of the foreground process group of the controlling terminal
49  of the process.
50 
51  flags %u (%lu before Linux 2.6.22)
52  The kernel flags word of the process. For bit meanings, see the
53  PF_* defines in <linux/sched.h>. Details depend on the kernel
54  version.
55 
56  minflt %lu The number of minor faults the process has made which have not
57  required loading a memory page from disk.
58 
59  cminflt %lu The number of minor faults that the process's waited-for children
60  have made.
61 
62  majflt %lu The number of major faults the process has made which have
63  required loading a memory page from disk.
64 
65  cmajflt %lu The number of major faults that the process's waited-for children
66  have made.
67 
68  utime %lu Amount of time that this process has been scheduled in user mode,
69  measured in clock ticks (divide by sysconf(_SC_CLK_TCK). This
70  includes guest time, guest_time (time spent running a virtual CPU,
71  see below), so that applications that are not aware of the guest
72  time field do not lose that time from their calculations.
73 
74  stime %lu Amount of time that this process has been scheduled in kernel
75  mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK).
76 
77  cutime %ld Amount of time that this process's waited-for children have been
78  scheduled in user mode, measured in clock ticks (divide by
79  sysconf(_SC_CLK_TCK). (See also times(2).) This includes guest
80  time, cguest_time (time spent running a virtual CPU, see below).
81 
82  cstime %ld Amount of time that this process's waited-for children have been
83  scheduled in kernel mode, measured in clock ticks (divide by
84  sysconf(_SC_CLK_TCK).
85 
86  priority %ld
87  (Explanation for Linux 2.6) For processes running a real-time
88  scheduling policy (policy below; see sched_setscheduler(2)), this
89  is the negated scheduling priority, minus one; that is, a number
90  in the range -2 to -100, corresponding to real-time priorities 1
91  to 99. For processes running under a non-real-time scheduling
92  policy, this is the raw nice value (setpriority(2)) as represented
93  in the kernel. The kernel stores nice values as numbers in the
94  range 0 (high) to 39 (low), corresponding to the user-visible nice
95  range of -20 to 19.
96 
97  Before Linux 2.6, this was a scaled value based on the scheduler
98  weighting given to this process.
99 
100  nice %ld The nice value (see setpriority(2)), a value in the range 19 (low
101  priority) to -20 (high priority).
102 
103  num_threads %ld
104  Number of threads in this process (since Linux 2.6). Before ker‐
105  nel 2.6, this field was hard coded to 0 as a placeholder for an
106  earlier removed field.
107 
108  itrealvalue %ld
109  The time in jiffies before the next SIGALRM is sent to the process
110  due to an interval timer. Since kernel 2.6.17, this field is no
111  longer maintained, and is hard coded as 0.
112 
113  starttime %llu (was %lu before Linux 2.6)
114  The time in jiffies the process started after system boot.
115 
116  vsize %lu Virtual memory size in bytes.
117 
118  rss %ld Resident Set Size: number of pages the process has in real memory.
119  This is just the pages which count towards text, data, or stack
120  space. This does not include pages which have not been demand-
121  loaded in, or which are swapped out.
122 
123  rsslim %lu Current soft limit in bytes on the rss of the process; see the
124  description of RLIMIT_RSS in getpriority(2).
125 
126  startcode %lu
127  The address above which program text can run.
128 
129  endcode %lu The address below which program text can run.
130 
131  startstack %lu
132  The address of the start (i.e., bottom) of the stack.
133 
134  kstkesp %lu The current value of ESP (stack pointer), as found in the kernel
135  stack page for the process.
136 
137  kstkeip %lu The current EIP (instruction pointer).
138 
139  signal %lu The bitmap of pending signals, displayed as a decimal number.
140  Obsolete, because it does not provide information on real-time
141  signals; use /proc/[pid]/status instead.
142 
143  blocked %lu The bitmap of blocked signals, displayed as a decimal number.
144  Obsolete, because it does not provide information on real-time
145  signals; use /proc/[pid]/status instead.
146 
147  sigignore %lu
148  The bitmap of ignored signals, displayed as a decimal number.
149  Obsolete, because it does not provide information on real-time
150  signals; use /proc/[pid]/status instead.
151 
152  sigcatch %lu
153  The bitmap of caught signals, displayed as a decimal number.
154  Obsolete, because it does not provide information on real-time
155  signals; use /proc/[pid]/status instead.
156 
157  wchan %lu This is the "channel" in which the process is waiting. It is the
158  address of a system call, and can be looked up in a namelist if
159  you need a textual name. (If you have an up-to-date
160  /etc/psdatabase, then try ps -l to see the WCHAN field in action.)
161 
162  nswap %lu Number of pages swapped (not maintained).
163 
164  cnswap %lu Cumulative nswap for child processes (not maintained).
165 
166  exit_signal %d (since Linux 2.1.22)
167  Signal to be sent to parent when we die.
168 
169  processor %d (since Linux 2.2.8)
170  CPU number last executed on.
171 
172  rt_priority %u (since Linux 2.5.19; was %lu before Linux 2.6.22)
173  Real-time scheduling priority, a number in the range 1 to 99 for
174  processes scheduled under a real-time policy, or 0, for non-real-
175  time processes (see sched_setscheduler(2)).
176 
177  policy %u (since Linux 2.5.19; was %lu before Linux 2.6.22)
178  Scheduling policy (see sched_setscheduler(2)). Decode using the
179  SCHED_* constants in linux/sched.h.
180 
181  delayacct_blkio_ticks %llu (since Linux 2.6.18)
182  Aggregated block I/O delays, measured in clock ticks (centisec‐
183  onds).
184 
185  guest_time %lu (since Linux 2.6.24)
186  Guest time of the process (time spent running a virtual CPU for a
187  guest operating system), measured in clock ticks (divide by
188  sysconf(_SC_CLK_TCK).
189 
190  cguest_time %ld (since Linux 2.6.24)
191  Guest time of the process's children, measured in clock ticks
192  (divide by sysconf(_SC_CLK_TCK).
193 */
194 struct linux_proc {
195  int pid;
196  char comm[400];
197  char state;
198  int ppid;
199  int pgrp;
200  int session;
201  int tty;
202  int tpgid;
203  unsigned long flags;
204  unsigned long minflt;
205  unsigned long cminflt;
206  unsigned long majflt;
207  unsigned long cmajflt;
208  unsigned long utime;
209  unsigned long stime;
210  long cutime;
211  long cstime;
212  long priority;
213  long nice;
214  long num_threads;
215  long itrealvalue;
216  unsigned long long starttime;
217  unsigned long vsize;
218  long rss;
219  unsigned long rlim;
220  unsigned long startcode;
221  unsigned long endcode;
222  unsigned long startstack;
223  unsigned long kstkesp;
224  unsigned long kstkeip;
225  unsigned long signal;
226  unsigned long blocked;
227  unsigned long sigignore;
228  unsigned long sigcatch;
229  unsigned long wchan;
230 };
231 #endif // __linux
232 
234  if(ProcStats::inst!=0) {
235  delete ProcStats::inst;
236  ProcStats::inst=0;
237  }
238 }
239 
241  static cleanup c;
242  if(inst==0)
243  inst = new ProcStats;
244  return inst;
245 }
246 
248 
250 {
251 #ifdef __linux
252  pg_size = sysconf(_SC_PAGESIZE); // getpagesize();
253  std::ostringstream ost;
254 
255  ost << "/proc/" << getpid() << "/stat";
256  fname = ost.str();
257  if((fd=open(fname.c_str(),O_RDONLY))<0)
258  {
259  cerr << "Failed to open " << ost.str() << endl;
260  return;
261  }
262 #endif
263  valid=true;
264 }
265 
267 {
268 #ifdef __linux
269  close(fd);
270 #endif
271 }
272 
274 {
275  if( valid == false ) return false;
276 
277 #ifdef __linux
278  double pr_size, pr_rssize;
279  linux_proc pinfo;
280  int cnt;
281 
282  lseek(fd,0,SEEK_SET);
283 
284  if((cnt=read(fd,buf,sizeof(buf)))<0)
285  {
286  cout << "LINUX Read of Proc file failed:" << endl;
287  return false;
288  }
289 
290  if(cnt>0)
291  {
292  buf[cnt]='\0';
293 
294  sscanf(buf,
295  //1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 20 1 2 3 4 5 6 7 8 9 30 1 2 3 4 5
296  "%d %s %c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %llu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
297  &pinfo.pid,
298  pinfo.comm,
299  &pinfo.state,
300  &pinfo.ppid,
301  &pinfo.pgrp,
302  &pinfo.session,
303  &pinfo.tty,
304  &pinfo.tpgid,
305  &pinfo.flags,
306  &pinfo.minflt,
307  &pinfo.cminflt,
308  &pinfo.majflt,
309  &pinfo.cmajflt,
310  &pinfo.utime,
311  &pinfo.stime,
312  &pinfo.cutime,
313  &pinfo.cstime,
314  &pinfo.priority,
315  &pinfo.nice,
316  &pinfo.num_threads,
317  &pinfo.itrealvalue,
318  &pinfo.starttime,
319  &pinfo.vsize,
320  &pinfo.rss,
321  &pinfo.rlim,
322  &pinfo.startcode,
323  &pinfo.endcode,
324  &pinfo.startstack,
325  &pinfo.kstkesp,
326  &pinfo.kstkeip,
327  &pinfo.signal,
328  &pinfo.blocked,
329  &pinfo.sigignore,
330  &pinfo.sigcatch,
331  &pinfo.wchan
332  );
333 
334  // resident set size in pages
335  pr_size = (double)pinfo.vsize;
336  pr_rssize = (double)pinfo.rss;
337 
338  f.vsize = pr_size / (1024*1024);
339  f.rss = pr_rssize * pg_size / (1024*1024);
340  }
341 
342 #else
343  f.vsize = 0;
344  f.rss = 0;
345 #endif
346 
347  bool rc = (curr==f)?false:true;
348 
349  curr.rss=f.rss;
350  curr.vsize=f.vsize;
351 
352  return rc;
353 }
354 
unsigned long minflt
unsigned long kstkesp
static ProcStats * inst
Definition: ProcStats.h:64
unsigned long signal
unsigned long wchan
unsigned long sigcatch
static ProcStats * instance()
Definition: ProcStats.cpp:240
unsigned long flags
tuple c
Definition: gaudirun.py:341
double pg_size
Definition: ProcStats.h:58
procInfo curr
Definition: ProcStats.h:59
std::string fname
Definition: ProcStats.h:60
int fd
Definition: ProcStats.h:57
double rss
Definition: ProcStats.h:34
unsigned long vsize
unsigned long startstack
tuple rc
Definition: IOTest.py:92
unsigned long cminflt
unsigned long majflt
unsigned long long starttime
unsigned long cmajflt
unsigned long kstkeip
unsigned long startcode
bool valid
Definition: ProcStats.h:62
unsigned long utime
char buf[500]
Definition: ProcStats.h:61
unsigned long blocked
double vsize
Definition: ProcStats.h:33
bool fetch(procInfo &fill_me)
Definition: ProcStats.cpp:273
unsigned long endcode
unsigned long stime
unsigned long sigignore
unsigned long rlim