218
225
/* Insert data from /proc directory */
219
226
for (i = 0; i < treesize; i++) {
221
pt[i].pid = atoi(globbuf.gl_pathv[i] + strlen("/proc/"));
223
if (!read_proc_file(buf, sizeof(buf), "stat", pt[i].pid, NULL)) {
224
DEBUG("system statistic error -- cannot read /proc/%d/stat\n", pt[i].pid);
227
stat_pid = atoi(globbuf.gl_pathv[i] + strlen("/proc/"));
229
/********** /proc/PID/stat **********/
230
if (!read_proc_file(buf, sizeof(buf), "stat", stat_pid, NULL)) {
231
DEBUG("system statistic error -- cannot read /proc/%d/stat\n", stat_pid);
228
pt[i].time = get_float_time();
230
234
if (!(tmp = strrchr(buf, ')'))) {
231
DEBUG("system statistic error -- file /proc/%d/stat parse error\n", pt[i].pid);
235
DEBUG("system statistic error -- file /proc/%d/stat parse error\n", stat_pid);
235
239
if (sscanf(buf, "%*d (%256s", procname) != 1) {
236
DEBUG("system statistic error -- file /proc/%d/stat process name parse error\n", pt[i].pid);
240
DEBUG("system statistic error -- file /proc/%d/stat process name parse error\n", stat_pid);
242
/* This implementation is done by using fs/procfs/array.c as a basis
243
* it is also worth looking into the source of the procps utils */
245
245
"%c %d %*d %*d %*d %*d %*u %*u"
246
246
"%*u %*u %*u %lu %lu %ld %ld %*d %*d %*d "
254
254
&stat_item_cstime,
255
255
&stat_item_starttime,
256
256
&stat_item_rss) != 8) {
257
DEBUG("system statistic error -- file /proc/%d/stat parse error\n", pt[i].pid);
261
pt[i].ppid = stat_ppid;
262
pt[i].starttime = get_starttime() + (time_t)(stat_item_starttime / HZ);
264
/* jiffies -> seconds = 1 / HZ
265
* HZ is defined in "asm/param.h" and it is usually 1/100s but on
266
* alpha system it is 1/1024s */
267
pt[i].cputime = ((float)(stat_item_utime + stat_item_stime) * 10.0) / HZ;
268
pt[i].cpu_percent = 0;
270
/* State is Zombie -> then we are a Zombie ... clear or? (-: */
271
if (stat_item_state == 'Z')
272
pt[i].status_flag |= PROCESS_ZOMBIE;
274
if (page_shift_to_kb < 0)
275
pt[i].mem_kbyte = (stat_item_rss >> abs(page_shift_to_kb));
277
pt[i].mem_kbyte = (stat_item_rss << abs(page_shift_to_kb));
279
if (! read_proc_file(buf, sizeof(buf), "status", pt[i].pid, NULL))
257
DEBUG("system statistic error -- file /proc/%d/stat parse error\n", stat_pid);
261
/********** /proc/PID/status **********/
262
if (! read_proc_file(buf, sizeof(buf), "status", stat_pid, NULL)) {
263
DEBUG("system statistic error -- cannot read /proc/%d/status\n", stat_pid);
282
266
if (! (tmp = strstr(buf, UID))) {
283
267
DEBUG("system statistic error -- cannot find process uid\n");
286
if (sscanf(tmp+strlen(UID), "\t%d\t%d", &(pt[i].uid), &(pt[i].euid)) != 2) {
270
if (sscanf(tmp+strlen(UID), "\t%d\t%d", &stat_uid, &stat_euid) != 2) {
287
271
DEBUG("system statistic error -- cannot read process uid\n");
291
274
if (! (tmp = strstr(buf, GID))) {
292
275
DEBUG("system statistic error -- cannot find process gid\n");
295
if (sscanf(tmp+strlen(GID), "\t%d", &(pt[i].gid)) != 1) {
278
if (sscanf(tmp+strlen(GID), "\t%d", &stat_gid) != 1) {
296
279
DEBUG("system statistic error -- cannot read process gid\n");
300
if (! read_proc_file(buf, sizeof(buf), "cmdline", pt[i].pid, &bytes)) {
301
DEBUG("system statistic error -- cannot read /proc/%d/cmdline\n", pt[i].pid);
283
/********** /proc/PID/cmdline **********/
284
if (! read_proc_file(buf, sizeof(buf), "cmdline", stat_pid, &bytes)) {
285
DEBUG("system statistic error -- cannot read /proc/%d/cmdline\n", stat_pid);
304
/* The cmdline file contains argv elements/strings terminated separated by '\0' => join the string: */
305
for (j = 0; j < (bytes - 1); j++)
288
for (j = 0; j < (bytes - 1); j++) // The cmdline file contains argv elements/strings terminated separated by '\0' => join the string
308
pt[i].cmdline = *buf ? Str_dup(buf) : Str_dup(procname);
292
/* Set the data in ptree only if all process related reads succeeded (prevent partial data in the case that continue was called during data gathering) */
293
pt[i].time = get_float_time();
294
pt[i].pid = stat_pid;
295
pt[i].ppid = stat_ppid;
296
pt[i].uid = stat_uid;
297
pt[i].euid = stat_euid;
298
pt[i].gid = stat_gid;
299
pt[i].starttime = get_starttime() + (time_t)(stat_item_starttime / HZ);
300
pt[i].cmdline = Str_dup(*buf ? buf : procname);
301
pt[i].cputime = ((float)(stat_item_utime + stat_item_stime) * 10.0) / HZ; // jiffies -> seconds = 1 / HZ. HZ is defined in "asm/param.h" and it is usually 1/100s but on alpha system it is 1/1024s
302
pt[i].cpu_percent = 0;
303
pt[i].mem_kbyte = (page_shift_to_kb < 0) ? (stat_item_rss >> abs(page_shift_to_kb)) : (stat_item_rss << abs(page_shift_to_kb));
304
if (stat_item_state == 'Z') // State is Zombie -> then we are a Zombie ... clear or? (-:
305
pt[i].status_flag |= PROCESS_ZOMBIE;