2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2006-08-31 18:52:26 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2006-08-31 18:52:26 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
2006-08-31 18:52:26 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2006-08-31 18:52:26 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2006-08-31 18:52:26 +00:00
|
|
|
|
|
|
|
|
2006-08-31 18:41:13 +00:00
|
|
|
#include "config.h"
|
2007-01-18 22:49:38 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_PROCFS_H
|
|
|
|
// Can't use large file calls with solaris procfs.
|
|
|
|
#if defined(_FILE_OFFSET_BITS) && ( _FILE_OFFSET_BITS == 64 )
|
|
|
|
#undef _FILE_OFFSET_BITS
|
2008-07-01 21:27:25 +00:00
|
|
|
#undef _LARGE_FILES
|
|
|
|
#undef _LARGEFILE_SOURCE
|
|
|
|
#undef _LARGEFILE64_SOURCE
|
2007-01-18 22:49:38 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2009-02-26 00:23:23 +00:00
|
|
|
#include <cstdio>
|
2008-04-06 03:15:36 +00:00
|
|
|
#include <string.h>
|
2006-09-01 21:06:30 +00:00
|
|
|
#include <ctype.h>
|
2006-08-31 18:41:13 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
2007-01-18 22:49:38 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_PROCFS_H
|
|
|
|
#include <procfs.h> // definitions for solaris /proc structs
|
|
|
|
#endif
|
|
|
|
|
2006-08-31 18:41:13 +00:00
|
|
|
#include "procinfo.h"
|
2008-09-29 16:47:10 +00:00
|
|
|
#include "str_util.h"
|
2009-06-16 20:54:44 +00:00
|
|
|
#include "str_replace.h"
|
2006-08-31 18:41:13 +00:00
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
// see:
|
|
|
|
// man 5 proc
|
|
|
|
// /usr/src/linux/fs/proc/array.C
|
|
|
|
|
|
|
|
struct PROC_STAT {
|
|
|
|
int pid;
|
|
|
|
char comm[256];
|
|
|
|
char state;
|
|
|
|
int ppid;
|
|
|
|
int pgrp;
|
|
|
|
int session;
|
|
|
|
int tty_nr;
|
|
|
|
int tpgid;
|
|
|
|
unsigned long flags;
|
|
|
|
unsigned long minflt;
|
|
|
|
unsigned long cminflt;
|
|
|
|
unsigned long majflt;
|
|
|
|
unsigned long cmajflt;
|
|
|
|
unsigned long utime;
|
|
|
|
unsigned long stime;
|
|
|
|
int cutime;
|
|
|
|
int cstime;
|
|
|
|
int priority;
|
|
|
|
int nice;
|
|
|
|
int zero;
|
|
|
|
int itrealvalue;
|
|
|
|
unsigned long starttime;
|
|
|
|
unsigned long vsize;
|
|
|
|
int rss;
|
|
|
|
unsigned long rlim;
|
|
|
|
unsigned long startcode;
|
|
|
|
unsigned long endcode;
|
|
|
|
unsigned long startstack;
|
|
|
|
unsigned long kstkesp;
|
|
|
|
unsigned long kstkeip;
|
|
|
|
unsigned long signal;
|
|
|
|
unsigned long blocked;
|
|
|
|
unsigned long sigignore;
|
|
|
|
unsigned long sigcatch;
|
|
|
|
unsigned long wchan;
|
|
|
|
unsigned long nswap;
|
|
|
|
unsigned long cnswap;
|
|
|
|
int exit_signal;
|
|
|
|
int processor;
|
|
|
|
|
|
|
|
int parse(char*);
|
|
|
|
};
|
|
|
|
|
|
|
|
int PROC_STAT::parse(char* buf) {
|
2007-03-20 23:12:22 +00:00
|
|
|
int n = sscanf(buf,
|
2008-10-29 16:05:26 +00:00
|
|
|
"%d (%s %c %d %d %d %d %d "
|
2007-03-20 23:12:22 +00:00
|
|
|
"%lu %lu %lu %lu %lu %lu %lu "
|
|
|
|
"%d %d %d %d %d %d "
|
|
|
|
"%lu %lu "
|
|
|
|
"%d "
|
|
|
|
"%lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu "
|
|
|
|
"%d %d",
|
|
|
|
&pid,
|
|
|
|
comm,
|
|
|
|
&state,
|
|
|
|
&ppid,
|
|
|
|
&pgrp,
|
|
|
|
&session,
|
|
|
|
&tty_nr,
|
|
|
|
&tpgid,
|
|
|
|
&flags,
|
|
|
|
&minflt,
|
|
|
|
&cminflt,
|
|
|
|
&majflt,
|
|
|
|
&cmajflt,
|
|
|
|
&utime,
|
|
|
|
&stime,
|
|
|
|
&cutime,
|
|
|
|
&cstime,
|
|
|
|
&priority,
|
|
|
|
&nice,
|
|
|
|
&zero,
|
|
|
|
&itrealvalue,
|
|
|
|
&starttime,
|
|
|
|
&vsize,
|
|
|
|
&rss,
|
|
|
|
&rlim,
|
|
|
|
&startcode,
|
|
|
|
&endcode,
|
|
|
|
&startstack,
|
|
|
|
&kstkesp,
|
|
|
|
&kstkeip,
|
|
|
|
&signal,
|
|
|
|
&blocked,
|
|
|
|
&sigignore,
|
|
|
|
&sigcatch,
|
|
|
|
&wchan,
|
|
|
|
&nswap,
|
|
|
|
&cnswap,
|
|
|
|
&exit_signal,
|
|
|
|
&processor
|
|
|
|
);
|
2008-10-29 16:05:26 +00:00
|
|
|
if (n == 39) {
|
|
|
|
char* p = strchr(comm, ')');
|
|
|
|
if (p) *p = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2007-02-22 09:33:08 +00:00
|
|
|
|
|
|
|
// I don't see a good choice of ERR_ for this...
|
|
|
|
//
|
|
|
|
return 1;
|
2006-08-31 18:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// build table of all processes in system
|
|
|
|
//
|
|
|
|
int procinfo_setup(vector<PROCINFO>& pi) {
|
|
|
|
|
2006-08-31 18:52:26 +00:00
|
|
|
#ifdef HAVE_DIRENT_H
|
2006-08-31 18:41:13 +00:00
|
|
|
DIR *dir;
|
|
|
|
dirent *piddir;
|
|
|
|
FILE* fd;
|
|
|
|
PROC_STAT ps;
|
|
|
|
PROCINFO p;
|
|
|
|
char pidpath[1024];
|
|
|
|
char buf[1024];
|
2010-02-15 01:00:32 +00:00
|
|
|
int pid = getpid();
|
2010-05-20 17:50:00 +00:00
|
|
|
int retval, final_retval = 0;
|
2006-08-31 18:41:13 +00:00
|
|
|
|
|
|
|
dir = opendir("/proc");
|
|
|
|
if (!dir) return 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
piddir = readdir(dir);
|
2010-05-20 17:50:00 +00:00
|
|
|
if (!piddir) break;
|
|
|
|
if (!isdigit(piddir->d_name[0])) continue;
|
2006-08-31 18:41:13 +00:00
|
|
|
|
2007-01-18 22:49:38 +00:00
|
|
|
#if defined(HAVE_PROCFS_H) && defined(HAVE__PROC_SELF_PSINFO) // solaris
|
2010-05-20 17:50:00 +00:00
|
|
|
psinfo_t psinfo;
|
|
|
|
sprintf(pidpath, "/proc/%s/psinfo", piddir->d_name);
|
|
|
|
fd = fopen(pidpath, "r");
|
|
|
|
if (fd) {
|
|
|
|
memset(&p, 0, sizeof(p));
|
|
|
|
if (fread(&psinfo, sizeof(psinfo_t), 1, fd) == 1) {
|
|
|
|
p.id = psinfo.pr_pid;
|
|
|
|
p.parentid = psinfo.pr_ppid;
|
|
|
|
p.swap_size = psinfo.pr_size*1024.;
|
|
|
|
p.working_set_size = psinfo.pr_rssize * 1024.;
|
|
|
|
strlcpy(p.command, psinfo.pr_fname, sizeof(p.command));
|
|
|
|
}
|
|
|
|
fclose(fd);
|
|
|
|
sprintf(pidpath, "/proc/%s/usage", piddir->d_name);
|
|
|
|
prusage_t prusage;
|
|
|
|
fd = fopen(pidpath, "r");
|
|
|
|
if (fd) {
|
|
|
|
if (fread(&prusage, sizeof(prusage_t), 1, fd) == 1) {
|
|
|
|
p.user_time = (float)prusage.pr_utime.tv_sec +
|
|
|
|
((float)prusage.pr_utime.tv_nsec)/1e+9;
|
|
|
|
p.kernel_time = (float)prusage.pr_stime.tv_sec +
|
|
|
|
((float)prusage.pr_utime.tv_nsec)/1e+9;
|
2007-01-18 22:49:38 +00:00
|
|
|
// page faults: I/O + non I/O
|
2010-05-20 17:50:00 +00:00
|
|
|
p.page_fault_count = prusage.pr_majf + prusage.pr_minf;
|
2006-08-31 18:41:13 +00:00
|
|
|
}
|
2010-05-20 17:50:00 +00:00
|
|
|
fclose(fd);
|
|
|
|
p.is_boinc_app = (p.id == pid || strcasestr(p.command, "boinc"));
|
|
|
|
pi.push_back(p);
|
2006-08-31 18:41:13 +00:00
|
|
|
}
|
2006-09-01 02:34:01 +00:00
|
|
|
}
|
2010-05-20 17:50:00 +00:00
|
|
|
#else // linux
|
|
|
|
sprintf(pidpath, "/proc/%s/stat", piddir->d_name);
|
|
|
|
fd = fopen(pidpath, "r");
|
|
|
|
if (fd) {
|
|
|
|
fgets(buf, sizeof(buf), fd);
|
|
|
|
retval = ps.parse(buf);
|
|
|
|
fclose(fd);
|
|
|
|
|
|
|
|
if (retval) {
|
|
|
|
final_retval = retval;
|
|
|
|
} else {
|
|
|
|
memset(&p, 0, sizeof(p));
|
|
|
|
p.id = ps.pid;
|
|
|
|
p.parentid = ps.ppid;
|
|
|
|
p.swap_size = ps.vsize;
|
|
|
|
// rss = pages, need bytes
|
|
|
|
// assumes page size = 4k
|
|
|
|
p.working_set_size = ps.rss * (float)getpagesize();
|
|
|
|
// page faults: I/O + non I/O
|
|
|
|
p.page_fault_count = ps.majflt + ps.minflt;
|
|
|
|
// times are in jiffies, need seconds
|
|
|
|
// assumes 100 jiffies per second
|
|
|
|
p.user_time = ps.utime / 100.;
|
|
|
|
p.kernel_time = ps.stime / 100.;
|
|
|
|
strlcpy(p.command, ps.comm, sizeof(p.command));
|
|
|
|
p.is_boinc_app = (p.id == pid || strcasestr(p.command, "boinc"));
|
2010-09-29 20:12:41 +00:00
|
|
|
p.is_low_priority = (ps.priority == 39);
|
|
|
|
// Linux seems to add 20 here,
|
|
|
|
// but this isn't documented anywhere
|
2010-05-20 17:50:00 +00:00
|
|
|
pi.push_back(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2006-08-31 18:41:13 +00:00
|
|
|
}
|
2010-05-20 17:50:00 +00:00
|
|
|
closedir(dir);
|
2007-01-18 22:49:38 +00:00
|
|
|
#endif
|
2010-05-20 17:50:00 +00:00
|
|
|
return final_retval;
|
2006-08-31 18:41:13 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-10-03 15:43:38 +00:00
|
|
|
// Scan the process table adding in CPU time and mem usage.
|
|
|
|
// Loop thru entire table as the entries aren't in order.
|
2007-01-18 22:49:38 +00:00
|
|
|
// Recurse at most 4 times to get additional child processes
|
2006-08-31 18:41:13 +00:00
|
|
|
//
|
|
|
|
void add_child_totals(PROCINFO& pi, vector<PROCINFO>& piv, int pid, int rlvl) {
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (rlvl > 3) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (i=0; i<piv.size(); i++) {
|
|
|
|
PROCINFO& p = piv[i];
|
|
|
|
if (p.parentid == pid) {
|
|
|
|
pi.kernel_time += p.kernel_time;
|
|
|
|
pi.user_time += p.user_time;
|
2007-01-10 01:28:12 +00:00
|
|
|
|
|
|
|
// only count process with most swap and memory
|
2007-01-10 01:43:50 +00:00
|
|
|
if (p.swap_size > pi.swap_size) {
|
2007-01-10 01:28:12 +00:00
|
|
|
pi.swap_size = p.swap_size;
|
|
|
|
}
|
2007-01-09 20:09:20 +00:00
|
|
|
if (p.working_set_size > pi.working_set_size) {
|
|
|
|
pi.working_set_size = p.working_set_size;
|
|
|
|
}
|
2007-01-10 01:28:12 +00:00
|
|
|
|
2006-08-31 18:41:13 +00:00
|
|
|
p.is_boinc_app = true;
|
|
|
|
// look for child process of this one
|
|
|
|
add_child_totals(pi, piv, p.id, rlvl+1); // recursion - woo hoo!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill in the given PROCINFO (which initially is zero except for id)
|
|
|
|
// with totals from that process and all its descendants
|
|
|
|
//
|
2010-04-07 16:04:36 +00:00
|
|
|
void procinfo_app(
|
|
|
|
PROCINFO& pi, vector<PROCINFO>& piv, char* graphics_exec_file
|
|
|
|
) {
|
2006-08-31 18:41:13 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i=0; i<piv.size(); i++) {
|
|
|
|
PROCINFO& p = piv[i];
|
|
|
|
if (p.id == pi.id) {
|
|
|
|
pi.kernel_time += p.kernel_time;
|
|
|
|
pi.user_time += p.user_time;
|
|
|
|
pi.swap_size += p.swap_size;
|
|
|
|
pi.working_set_size += p.working_set_size;
|
|
|
|
p.is_boinc_app = true;
|
2006-10-03 15:43:38 +00:00
|
|
|
|
2006-08-31 18:41:13 +00:00
|
|
|
// look for child processes
|
2006-10-03 15:43:38 +00:00
|
|
|
//
|
|
|
|
add_child_totals(pi, piv, pi.id, 0);
|
2006-08-31 18:41:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-04-07 16:04:36 +00:00
|
|
|
if (!strcmp(p.command, graphics_exec_file)) {
|
|
|
|
p.is_boinc_app = true;
|
|
|
|
}
|
2006-08-31 18:41:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void procinfo_other(PROCINFO& pi, vector<PROCINFO>& piv) {
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
memset(&pi, 0, sizeof(pi));
|
|
|
|
for (i=0; i<piv.size(); i++) {
|
|
|
|
PROCINFO& p = piv[i];
|
2010-09-29 20:12:41 +00:00
|
|
|
if (p.is_boinc_app) continue;
|
|
|
|
if (p.is_low_priority) continue;
|
|
|
|
|
|
|
|
pi.kernel_time += p.kernel_time;
|
|
|
|
pi.user_time += p.user_time;
|
|
|
|
pi.swap_size += p.swap_size;
|
|
|
|
pi.working_set_size += p.working_set_size;
|
2006-08-31 18:41:13 +00:00
|
|
|
}
|
|
|
|
}
|