2011-02-23 00:28:52 +00:00
|
|
|
// This file is part of BOINC.
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2011 University of California
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// platform-independent process-enumeration functions
|
|
|
|
|
2020-06-04 07:48:21 +00:00
|
|
|
#if defined(_WIN32)
|
2011-04-01 21:21:11 +00:00
|
|
|
#include "boinc_win.h"
|
2011-04-14 22:48:04 +00:00
|
|
|
#include "win_util.h"
|
2011-04-01 21:21:11 +00:00
|
|
|
#else
|
|
|
|
#include "config.h"
|
2011-02-23 00:28:52 +00:00
|
|
|
#include <sys/types.h>
|
2011-04-14 22:25:38 +00:00
|
|
|
#include <unistd.h>
|
2011-02-23 00:28:52 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <string.h>
|
2011-02-23 01:08:57 +00:00
|
|
|
#include <signal.h>
|
2011-04-01 21:21:11 +00:00
|
|
|
#endif
|
2011-02-23 01:08:57 +00:00
|
|
|
|
2012-12-12 00:05:40 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2012-08-11 05:47:18 +00:00
|
|
|
#include "procinfo.h"
|
|
|
|
|
2011-02-23 00:28:52 +00:00
|
|
|
using std::vector;
|
|
|
|
|
2011-09-02 17:45:29 +00:00
|
|
|
// Scan the process table adding in CPU time and mem usage.
|
|
|
|
//
|
2013-07-29 08:31:09 +00:00
|
|
|
void add_child_totals(PROCINFO& procinfo, PROC_MAP& pm, PROC_MAP::iterator i) {
|
2011-09-02 17:45:29 +00:00
|
|
|
PROCINFO parent = i->second;
|
|
|
|
for (unsigned int j=0; j<parent.children.size(); j++) {
|
|
|
|
int child_pid = parent.children[j];
|
|
|
|
PROC_MAP::iterator i2 = pm.find(child_pid);
|
|
|
|
if (i2 == pm.end()) continue;
|
|
|
|
PROCINFO& p = i2->second;
|
2011-09-16 22:22:45 +00:00
|
|
|
if (p.scanned) {
|
|
|
|
return; // cycle in graph - shouldn't happen
|
|
|
|
}
|
2013-07-29 08:31:09 +00:00
|
|
|
procinfo.kernel_time += p.kernel_time;
|
|
|
|
procinfo.user_time += p.user_time;
|
2011-09-16 22:22:45 +00:00
|
|
|
p.scanned = true;
|
2011-09-02 17:45:29 +00:00
|
|
|
|
|
|
|
// only count process with most swap and memory
|
2013-07-29 08:31:09 +00:00
|
|
|
if (p.swap_size > procinfo.swap_size) {
|
|
|
|
procinfo.swap_size = p.swap_size;
|
2011-09-02 17:45:29 +00:00
|
|
|
}
|
2013-07-29 08:31:09 +00:00
|
|
|
if (p.working_set_size > procinfo.working_set_size) {
|
|
|
|
procinfo.working_set_size = p.working_set_size;
|
2011-02-23 00:28:52 +00:00
|
|
|
}
|
2011-09-02 17:45:29 +00:00
|
|
|
|
|
|
|
p.is_boinc_app = true;
|
2013-07-29 08:31:09 +00:00
|
|
|
add_child_totals(procinfo, pm, i2); // recursion - woo hoo!
|
2011-09-02 17:45:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-02 20:47:05 +00:00
|
|
|
// Fill in the given PROCINFO (initially zero except for id)
|
|
|
|
// with totals from that process and all its descendants.
|
|
|
|
// Set PROCINFO.is_boinc_app for all of them.
|
2011-09-02 17:45:29 +00:00
|
|
|
//
|
|
|
|
void procinfo_app(
|
2013-07-29 08:31:09 +00:00
|
|
|
PROCINFO& procinfo, vector<int>* other_pids, PROC_MAP& pm, char* graphics_exec_file
|
2011-09-02 17:45:29 +00:00
|
|
|
) {
|
|
|
|
PROC_MAP::iterator i;
|
2015-01-14 20:18:29 +00:00
|
|
|
for (i=pm.begin(); i!=pm.end(); ++i) {
|
2011-09-02 17:45:29 +00:00
|
|
|
PROCINFO& p = i->second;
|
2013-07-29 08:31:09 +00:00
|
|
|
if (p.id == procinfo.id
|
2011-09-02 20:47:05 +00:00
|
|
|
|| (other_pids && in_vector(p.id, *other_pids))
|
|
|
|
) {
|
2013-07-29 08:31:09 +00:00
|
|
|
procinfo.kernel_time += p.kernel_time;
|
|
|
|
procinfo.user_time += p.user_time;
|
|
|
|
procinfo.swap_size += p.swap_size;
|
|
|
|
procinfo.working_set_size += p.working_set_size;
|
2011-09-02 17:45:29 +00:00
|
|
|
p.is_boinc_app = true;
|
2011-09-16 22:22:45 +00:00
|
|
|
p.scanned = true;
|
2011-09-02 17:45:29 +00:00
|
|
|
|
|
|
|
// look for child processes
|
|
|
|
//
|
2013-07-29 08:31:09 +00:00
|
|
|
add_child_totals(procinfo, pm, i);
|
2011-09-02 17:45:29 +00:00
|
|
|
}
|
2011-11-29 15:06:08 +00:00
|
|
|
if (graphics_exec_file && !strcmp(p.command, graphics_exec_file)) {
|
2011-09-02 17:45:29 +00:00
|
|
|
p.is_boinc_app = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void find_children(PROC_MAP& pm) {
|
|
|
|
PROC_MAP::iterator i;
|
2015-01-14 20:18:29 +00:00
|
|
|
for (i=pm.begin(); i!=pm.end(); ++i) {
|
2011-09-02 17:45:29 +00:00
|
|
|
int parentid = i->second.parentid;
|
|
|
|
PROC_MAP::iterator j = pm.find(parentid);
|
|
|
|
if (j == pm.end()) continue; // should never happen
|
2023-03-17 09:25:53 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
// In Windows:
|
|
|
|
// 1) PIDs are reused, possibly quickly
|
|
|
|
// 2) if a process creates children and then exits,
|
|
|
|
// the parentID of the children are not cleared,
|
|
|
|
// so they may soon refer to an unrelated process.
|
|
|
|
// (this is horrible design, BTW)
|
|
|
|
// This can cause problems:
|
|
|
|
// - when we abort a BOINC app we kill the process and its descendants
|
|
|
|
// (based on parent ID). These could be unrelated processes.
|
|
|
|
// - If a BOINC app gets a process ID that is the parentID of
|
|
|
|
// orphan processes, its CPU time will be computed incorrectly.
|
|
|
|
// To fix this, don't create a parent/child link
|
|
|
|
// if the parent was created after the child.
|
|
|
|
//
|
|
|
|
if (j->second.create_time.QuadPart > i->second.create_time.QuadPart) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2011-09-02 17:45:29 +00:00
|
|
|
j->second.children.push_back(i->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-23 00:28:52 +00:00
|
|
|
// get resource usage of non-BOINC apps
|
2023-03-29 02:22:35 +00:00
|
|
|
// NOTE: this is flawed because it doesn't account for short-lived processes.
|
|
|
|
// It's not used on Win, Mac, or Linux, which have better ways of getting total CPU usage.
|
2011-02-23 00:28:52 +00:00
|
|
|
//
|
2013-07-29 08:31:09 +00:00
|
|
|
void procinfo_non_boinc(PROCINFO& procinfo, PROC_MAP& pm) {
|
|
|
|
procinfo.clear();
|
2011-09-02 17:45:29 +00:00
|
|
|
PROC_MAP::iterator i;
|
2015-01-14 20:18:29 +00:00
|
|
|
for (i=pm.begin(); i!=pm.end(); ++i) {
|
2011-09-02 17:45:29 +00:00
|
|
|
PROCINFO& p = i->second;
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (p.id == 0) continue; // idle process
|
|
|
|
#endif
|
2011-02-23 00:28:52 +00:00
|
|
|
if (p.is_boinc_app) continue;
|
|
|
|
if (p.is_low_priority) continue;
|
|
|
|
|
2012-03-19 20:13:28 +00:00
|
|
|
// count VirtualBox process as BOINC;
|
|
|
|
// on some systems they use nontrivial CPU time
|
|
|
|
// TODO: do this only if we're running a vbox app
|
|
|
|
//
|
2012-03-19 20:26:48 +00:00
|
|
|
if (strstr(p.command, "VBoxSVC")) continue;
|
|
|
|
if (strstr(p.command, "VBoxXPCOMIPCD")) continue;
|
2012-03-19 20:13:28 +00:00
|
|
|
|
2012-02-09 00:54:05 +00:00
|
|
|
#if 0
|
|
|
|
if (p.user_time > .1) {
|
|
|
|
fprintf(stderr, "non-boinc: %s (%d) %f %f\n", p.command, p.id, p.user_time, p.kernel_time);
|
|
|
|
}
|
|
|
|
#endif
|
2013-07-29 08:31:09 +00:00
|
|
|
procinfo.kernel_time += p.kernel_time;
|
|
|
|
procinfo.user_time += p.user_time;
|
|
|
|
procinfo.swap_size += p.swap_size;
|
|
|
|
procinfo.working_set_size += p.working_set_size;
|
2011-02-23 00:28:52 +00:00
|
|
|
}
|
2012-02-09 00:54:05 +00:00
|
|
|
#if 0
|
2022-07-29 19:42:42 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"total non-boinc: %f %f\n", procinfo.user_time, procinfo.kernel_time
|
|
|
|
);
|
2012-02-09 00:54:05 +00:00
|
|
|
#endif
|
2011-02-23 00:28:52 +00:00
|
|
|
}
|
2011-11-29 15:06:08 +00:00
|
|
|
|
2022-07-29 19:42:42 +00:00
|
|
|
// get CPU time of BOINC-related processes, low-priority processes,
|
|
|
|
// and (if we're using Vbox) the Vbox daemon.
|
|
|
|
//
|
|
|
|
double boinc_related_cpu_time(PROC_MAP& pm, bool using_vbox) {
|
|
|
|
double sum = 0;
|
|
|
|
PROC_MAP::iterator i;
|
|
|
|
for (i=pm.begin(); i!=pm.end(); ++i) {
|
|
|
|
PROCINFO& p = i->second;
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (p.id == 0) continue; // idle process
|
|
|
|
#endif
|
|
|
|
if (
|
|
|
|
p.is_boinc_app
|
|
|
|
|| p.is_low_priority
|
|
|
|
|| (using_vbox && strstr(p.command, "VBoxSVC"))
|
|
|
|
) {
|
|
|
|
sum += p.user_time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2011-11-29 15:06:08 +00:00
|
|
|
double process_tree_cpu_time(int pid) {
|
|
|
|
PROC_MAP pm;
|
2013-07-29 08:31:09 +00:00
|
|
|
PROCINFO procinfo;
|
2011-11-29 15:06:08 +00:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = procinfo_setup(pm);
|
|
|
|
if (retval) return 0;
|
|
|
|
|
2013-07-29 08:31:09 +00:00
|
|
|
procinfo.clear();
|
|
|
|
procinfo.id = pid;
|
|
|
|
procinfo_app(procinfo, NULL, pm, NULL);
|
|
|
|
return procinfo.user_time + procinfo.kernel_time;
|
2012-12-08 18:16:28 +00:00
|
|
|
}
|