2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2006-09-19 10:40:24 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2006-09-19 10:40:24 +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-09-19 10:40:24 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2006-09-19 10:40:24 +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-09-19 10:40:24 +00:00
|
|
|
|
2006-09-28 01:30:58 +00:00
|
|
|
#define SHOW_TIMING 0
|
|
|
|
|
2006-09-19 10:40:24 +00:00
|
|
|
#include "config.h"
|
2009-02-26 00:23:23 +00:00
|
|
|
#include <cstdio>
|
2006-09-19 10:40:24 +00:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
2006-09-28 01:30:58 +00:00
|
|
|
|
|
|
|
#if SHOW_TIMING
|
|
|
|
#include <Carbon/Carbon.h>
|
|
|
|
#endif
|
2006-09-19 10:40:24 +00:00
|
|
|
|
|
|
|
#include "procinfo.h"
|
|
|
|
#include "client_msgs.h"
|
2006-09-25 10:49:39 +00:00
|
|
|
#include "client_state.h"
|
2006-09-19 10:40:24 +00:00
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
2006-09-25 10:49:39 +00:00
|
|
|
|
2007-02-20 01:39:26 +00:00
|
|
|
// build table of all processes in system
|
|
|
|
//
|
2006-09-25 10:49:39 +00:00
|
|
|
int procinfo_setup(vector<PROCINFO>& pi) {
|
2010-02-15 01:00:32 +00:00
|
|
|
int pid = getpid();
|
2007-02-20 01:39:26 +00:00
|
|
|
FILE* fd;
|
2006-09-19 10:40:24 +00:00
|
|
|
PROCINFO p;
|
2007-02-20 01:39:26 +00:00
|
|
|
int c, real_mem, virtual_mem, hours;
|
2009-06-12 00:42:09 +00:00
|
|
|
char* lf;
|
2007-02-20 01:39:26 +00:00
|
|
|
|
2006-09-28 01:30:58 +00:00
|
|
|
#if SHOW_TIMING
|
|
|
|
UnsignedWide start, end, elapsed;
|
2006-09-19 10:40:24 +00:00
|
|
|
|
2006-09-28 01:30:58 +00:00
|
|
|
start = UpTime();
|
|
|
|
#endif
|
|
|
|
|
2007-02-20 01:39:26 +00:00
|
|
|
// Some values of possible interest available from 'ps' command:
|
|
|
|
// %cpu percentage cpu usage (alias pcpu)
|
|
|
|
// %mem percentage memory usage (alias pmem)
|
2009-04-09 10:41:11 +00:00
|
|
|
// command command (executable name)
|
2007-02-20 01:39:26 +00:00
|
|
|
// majflt total page faults
|
|
|
|
// minflt total page reclaims
|
|
|
|
// nswap total swaps in/out
|
|
|
|
// pagein pageins (same as majflt)
|
|
|
|
// pid process ID
|
|
|
|
// ppid parent process ID
|
|
|
|
// poip pageouts in progress
|
|
|
|
// rss resident set size in Kbytes
|
|
|
|
// time accumulated cpu time, user + system
|
|
|
|
// vsz virtual size in Kbytes
|
|
|
|
//
|
|
|
|
// Unfortunately, the selectors majflt, minflt, pagein do not work on OS X,
|
|
|
|
// and ps does not return kernel time separately from user time.
|
|
|
|
//
|
|
|
|
// This code doesn't use PROCINFO.is_boinc_app, but we set it to be
|
|
|
|
// consistent with the code for other platforms.
|
|
|
|
//
|
|
|
|
// Earlier versions of procinf_mac.C launched a small helper application
|
|
|
|
// AppStats using a bi-directional pipe. AppStats used mach ports to get
|
|
|
|
// all the information, including page fault counts, kernel and user times.
|
|
|
|
// In order to use mach ports, AppStats must run setuid root.
|
|
|
|
//
|
|
|
|
// But these three items are not actually used (page fault counts aren't
|
|
|
|
// available from Windows either) so we have reverted to using the ps
|
|
|
|
// utility, even though it takes more cpu time than AppStats did.
|
|
|
|
// This eliminates the need to install our own application which runs setuid
|
|
|
|
// root; this was perceived by some users as a security risk.
|
|
|
|
|
|
|
|
|
2009-08-21 22:28:38 +00:00
|
|
|
fd = popen("ps -axcopid,ppid,rss,vsz,pagein,time,command", "r");
|
2007-02-20 01:39:26 +00:00
|
|
|
if (!fd) return 0;
|
|
|
|
|
|
|
|
// Skip over the header line
|
|
|
|
do {
|
|
|
|
c = fgetc(fd);
|
|
|
|
if (c == EOF) {
|
|
|
|
pclose(fd);
|
|
|
|
return 0;
|
2006-09-28 08:13:53 +00:00
|
|
|
}
|
2007-02-20 01:39:26 +00:00
|
|
|
} while (c != '\n');
|
2006-09-28 01:30:58 +00:00
|
|
|
|
2006-09-28 08:13:53 +00:00
|
|
|
while (1) {
|
2006-09-22 02:54:27 +00:00
|
|
|
memset(&p, 0, sizeof(p));
|
2009-04-10 01:09:58 +00:00
|
|
|
c = fscanf(fd, "%d%d%d%d%ld%d:%lf ", &p.id, &p.parentid, &real_mem,
|
|
|
|
&virtual_mem, &p.page_fault_count, &hours, &p.user_time);
|
2007-02-20 01:39:26 +00:00
|
|
|
if (c < 7) break;
|
2009-04-10 01:09:58 +00:00
|
|
|
if (fgets(p.command, sizeof(p.command) , fd) == NULL) break;
|
2009-06-12 00:42:09 +00:00
|
|
|
lf = strchr(p.command, '\n');
|
|
|
|
if (lf) *lf = '\0'; // Strip trailing linefeed
|
2007-02-20 01:39:26 +00:00
|
|
|
p.working_set_size = (double)real_mem * 1024.;
|
|
|
|
p.swap_size = (double)virtual_mem * 1024.;
|
|
|
|
p.user_time += 60. * (float)hours;
|
2010-02-16 01:00:35 +00:00
|
|
|
p.is_boinc_app = (p.id == pid || strcasestr(p.command, "boinc"));
|
2006-09-19 10:40:24 +00:00
|
|
|
pi.push_back(p);
|
|
|
|
}
|
2007-02-20 01:39:26 +00:00
|
|
|
|
|
|
|
pclose(fd);
|
2006-09-28 08:13:53 +00:00
|
|
|
|
2006-09-28 01:30:58 +00:00
|
|
|
#if SHOW_TIMING
|
|
|
|
end = UpTime();
|
|
|
|
elapsed = AbsoluteToNanoseconds(SubAbsoluteFromAbsolute(end, start));
|
2007-02-20 01:39:26 +00:00
|
|
|
msg_printf(NULL, MSG_INFO, "elapsed time = %llu, m_swap = %lf\n", elapsed, gstate.host_info.m_swap);
|
2006-09-28 01:30:58 +00:00
|
|
|
#endif
|
2006-09-25 10:49:39 +00:00
|
|
|
|
|
|
|
return 0;
|
2006-09-19 10:40:24 +00:00
|
|
|
}
|
|
|
|
|
2007-02-20 01:39:26 +00:00
|
|
|
// Scan the process table adding in CPU time and mem usage. Loop
|
|
|
|
// thru entire table as the entries aren't in order. Recurse at
|
|
|
|
// most 4 times to get additional child processes
|
|
|
|
//
|
2010-04-07 11:37:56 +00:00
|
|
|
void add_proc_totals(PROCINFO& pi, vector<PROCINFO>& piv, int pid, char* graphics_exec_file, int start, int rlvl) {
|
2007-02-20 01:39:26 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
2010-04-07 11:37:56 +00:00
|
|
|
if (rlvl > 4) {
|
2007-02-20 01:39:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (i=0; i<piv.size(); i++) {
|
|
|
|
PROCINFO& p = piv[i];
|
2010-04-07 11:37:56 +00:00
|
|
|
if (p.id == pid || p.parentid == pid) {
|
2007-02-20 01:39:26 +00:00
|
|
|
// 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;
|
|
|
|
// look for child process of this one
|
2010-04-07 11:37:56 +00:00
|
|
|
add_proc_totals(pi, piv, p.id, graphics_exec_file, i+1, rlvl+1); // recursion - woo hoo!
|
2007-02-20 01:39:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-19 10:40:24 +00:00
|
|
|
// fill in the given PROCINFO (which initially is zero except for id)
|
|
|
|
// with totals from that process and all its descendants
|
|
|
|
//
|
2010-04-07 11:37:56 +00:00
|
|
|
void procinfo_app(PROCINFO& pi, vector<PROCINFO>& piv, char* graphics_exec_file) {
|
|
|
|
add_proc_totals(pi, piv, pi.id, graphics_exec_file, 0, 0);
|
2006-09-19 10:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void procinfo_other(PROCINFO& pi, vector<PROCINFO>& piv) {
|
2007-02-20 01:39:26 +00:00
|
|
|
unsigned int i;
|
2006-09-28 01:30:58 +00:00
|
|
|
|
2007-02-20 01:39:26 +00:00
|
|
|
memset(&pi, 0, sizeof(pi));
|
|
|
|
for (i=0; i<piv.size(); i++) {
|
|
|
|
PROCINFO& p = piv[i];
|
|
|
|
if (!p.is_boinc_app) {
|
|
|
|
// 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-09-28 01:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|