2008-08-05 22:52:17 +00:00
|
|
|
// much of this code is public-domain
|
|
|
|
//
|
2008-01-31 18:34:51 +00:00
|
|
|
#include "boinc_win.h"
|
2006-11-01 22:40:30 +00:00
|
|
|
#include "error_numbers.h"
|
2006-08-28 18:22:07 +00:00
|
|
|
#include "diagnostics_win.h"
|
2008-09-29 03:04:39 +00:00
|
|
|
#include "str_util.h"
|
2010-03-16 00:07:58 +00:00
|
|
|
#include "str_replace.h"
|
2006-08-25 21:31:05 +00:00
|
|
|
#include "procinfo.h"
|
2006-08-25 21:31:04 +00:00
|
|
|
|
2006-08-28 18:22:07 +00:00
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
// NtQuerySystemInformation
|
|
|
|
typedef NTSTATUS (WINAPI *tNTQSI)(
|
|
|
|
ULONG SystemInformationClass,
|
|
|
|
PVOID SystemInformation,
|
|
|
|
ULONG SystemInformationLength,
|
|
|
|
PULONG ReturnLength
|
|
|
|
);
|
|
|
|
|
2006-08-25 21:31:04 +00:00
|
|
|
static int get_process_information(PVOID* ppBuffer, PULONG pcbBuffer) {
|
|
|
|
NTSTATUS Status = STATUS_INFO_LENGTH_MISMATCH;
|
|
|
|
HANDLE hHeap = GetProcessHeap();
|
2010-08-11 16:35:06 +00:00
|
|
|
HMODULE hNTDllLib = GetModuleHandle(_T("ntdll.dll"));
|
2006-11-02 00:52:55 +00:00
|
|
|
tNTQSI pNTQSI = (tNTQSI)GetProcAddress(hNTDllLib, "NtQuerySystemInformation");
|
|
|
|
ULONG cbBuffer = 0;
|
2006-08-25 21:31:04 +00:00
|
|
|
|
2006-11-01 22:40:30 +00:00
|
|
|
while (1) {
|
2006-11-02 00:52:55 +00:00
|
|
|
// Store the buffer size since it appears that somebody is monkeying around
|
|
|
|
// with the return values on some systems.
|
|
|
|
cbBuffer = *pcbBuffer;
|
|
|
|
|
2006-08-25 21:31:04 +00:00
|
|
|
*ppBuffer = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, *pcbBuffer);
|
|
|
|
if (ppBuffer == NULL) {
|
2006-11-01 22:40:30 +00:00
|
|
|
return ERR_MALLOC;
|
2006-08-25 21:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Status = pNTQSI(
|
|
|
|
SystemProcessAndThreadInformation,
|
|
|
|
*ppBuffer,
|
|
|
|
*pcbBuffer,
|
|
|
|
pcbBuffer
|
|
|
|
);
|
|
|
|
|
2006-11-02 00:52:55 +00:00
|
|
|
if (*pcbBuffer < cbBuffer) {
|
2008-09-29 01:55:58 +00:00
|
|
|
// Somebody is trying to screw us up,
|
|
|
|
// so set the value back to the cached size
|
|
|
|
// so we can do something smart like increase the buffer size.
|
2006-11-02 00:52:55 +00:00
|
|
|
*pcbBuffer = cbBuffer;
|
|
|
|
}
|
|
|
|
|
2006-08-25 21:31:04 +00:00
|
|
|
if (Status == STATUS_INFO_LENGTH_MISMATCH) {
|
|
|
|
HeapFree(hHeap, NULL, *ppBuffer);
|
|
|
|
*pcbBuffer *= 2;
|
|
|
|
} else if (!NT_SUCCESS(Status)) {
|
|
|
|
HeapFree(hHeap, NULL, *ppBuffer);
|
2006-11-01 22:40:30 +00:00
|
|
|
return ERR_GETRUSAGE;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0; // never reached
|
2006-08-25 21:31:04 +00:00
|
|
|
}
|
|
|
|
|
2006-09-11 17:32:18 +00:00
|
|
|
// Note: the following will work on both NT and XP,
|
|
|
|
// because the NT process structure differs only at the end
|
|
|
|
//
|
2006-08-25 21:31:04 +00:00
|
|
|
int get_procinfo_XP(vector<PROCINFO>& pi) {
|
2006-11-02 00:52:55 +00:00
|
|
|
ULONG cbBuffer = 128*1024; // 128k initial buffer
|
2006-08-25 21:31:04 +00:00
|
|
|
PVOID pBuffer = NULL;
|
|
|
|
PSYSTEM_PROCESSES pProcesses = NULL;
|
2010-04-13 08:52:50 +00:00
|
|
|
static DWORD pid = 0;
|
2010-02-15 01:00:32 +00:00
|
|
|
|
|
|
|
if (!pid) {
|
|
|
|
pid = GetCurrentProcessId();
|
|
|
|
}
|
2007-06-01 22:34:47 +00:00
|
|
|
#if 0
|
|
|
|
printf("FILETIME: %d\n", sizeof(FILETIME));
|
|
|
|
printf("LARGE_INTEGER: %d\n", sizeof(LARGE_INTEGER));
|
|
|
|
printf("DWORD: %d\n", sizeof(DWORD));
|
|
|
|
printf("UNICODE_STRING: %d\n", sizeof(UNICODE_STRING));
|
|
|
|
printf("KPRIORITY: %d\n", sizeof(KPRIORITY));
|
|
|
|
printf("ULONG: %d\n", sizeof(ULONG));
|
|
|
|
printf("SIZE_T: %d\n", sizeof(SIZE_T));
|
|
|
|
#endif
|
2006-08-25 21:31:04 +00:00
|
|
|
|
|
|
|
get_process_information(&pBuffer, &cbBuffer);
|
|
|
|
pProcesses = (PSYSTEM_PROCESSES)pBuffer;
|
|
|
|
while (pProcesses) {
|
|
|
|
PROCINFO p;
|
2006-08-28 18:22:07 +00:00
|
|
|
p.id = pProcesses->ProcessId;
|
|
|
|
p.parentid = pProcesses->InheritedFromProcessId;
|
|
|
|
p.swap_size = pProcesses->VmCounters.PagefileUsage;
|
2006-08-25 21:31:04 +00:00
|
|
|
p.working_set_size = pProcesses->VmCounters.WorkingSetSize;
|
2006-09-08 19:27:42 +00:00
|
|
|
p.page_fault_count = pProcesses->VmCounters.PageFaultCount;
|
2006-08-28 18:22:07 +00:00
|
|
|
p.user_time = ((double) pProcesses->UserTime.QuadPart)/1e7;
|
|
|
|
p.kernel_time = ((double) pProcesses->KernelTime.QuadPart)/1e7;
|
|
|
|
p.id = pProcesses->ProcessId;
|
|
|
|
p.parentid = pProcesses->InheritedFromProcessId;
|
2008-09-29 03:04:39 +00:00
|
|
|
WideCharToMultiByte(CP_ACP, 0,
|
|
|
|
pProcesses->ProcessName.Buffer,
|
|
|
|
pProcesses->ProcessName.Length,
|
|
|
|
p.command,
|
|
|
|
sizeof(p.command),
|
|
|
|
NULL, NULL
|
|
|
|
);
|
2010-05-18 20:16:46 +00:00
|
|
|
p.is_boinc_app = (p.id == (int)pid) || (strcasestr(p.command, "boinc") != NULL);
|
2010-04-13 08:52:50 +00:00
|
|
|
|
2010-04-13 09:40:22 +00:00
|
|
|
#ifdef _GRIDREPUBLIC
|
2010-04-15 23:03:22 +00:00
|
|
|
if (!strcmp(p.command, "gridrepublic.exe")) {
|
2010-04-13 09:40:22 +00:00
|
|
|
p.is_boinc_app = true;
|
2010-04-13 08:52:50 +00:00
|
|
|
}
|
2010-04-13 09:40:22 +00:00
|
|
|
#endif
|
2010-04-13 09:42:19 +00:00
|
|
|
#ifdef _PROGRESSTHRUPROCESSORS
|
2010-04-15 23:03:22 +00:00
|
|
|
if (!strcmp(p.command, "progressthruprocessors.exe")) {
|
2010-04-13 09:40:22 +00:00
|
|
|
p.is_boinc_app = true;
|
|
|
|
}
|
|
|
|
#endif
|
2006-08-25 21:31:04 +00:00
|
|
|
pi.push_back(p);
|
|
|
|
if (!pProcesses->NextEntryDelta) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pProcesses = (PSYSTEM_PROCESSES)(((LPBYTE)pProcesses) + pProcesses->NextEntryDelta);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pBuffer) HeapFree(GetProcessHeap(), NULL, pBuffer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-28 18:22:07 +00:00
|
|
|
// get a list of all running processes.
|
|
|
|
//
|
|
|
|
int procinfo_setup(vector<PROCINFO>& pi) {
|
2006-08-25 21:31:04 +00:00
|
|
|
OSVERSIONINFO osvi;
|
|
|
|
osvi.dwOSVersionInfoSize = sizeof(osvi);
|
|
|
|
GetVersionEx(&osvi);
|
|
|
|
|
|
|
|
pi.clear();
|
|
|
|
|
|
|
|
switch(osvi.dwPlatformId) {
|
|
|
|
case VER_PLATFORM_WIN32_WINDOWS:
|
|
|
|
// Win95, Win98, WinME
|
2006-09-11 17:32:18 +00:00
|
|
|
return 0; // not supported
|
2006-08-25 21:31:04 +00:00
|
|
|
case VER_PLATFORM_WIN32_NT:
|
2006-09-11 17:32:18 +00:00
|
|
|
return get_procinfo_XP(pi);
|
2006-08-25 21:31:04 +00:00
|
|
|
}
|
2006-09-11 17:32:18 +00:00
|
|
|
return 0;
|
2006-08-28 18:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// scan the process table from the given point,
|
|
|
|
// adding in CPU time and mem usage
|
|
|
|
//
|
2010-04-07 16:04:36 +00:00
|
|
|
void add_proc_totals(
|
|
|
|
PROCINFO& pi, vector<PROCINFO>& piv, int pid,
|
|
|
|
char* graphics_exec_file, int start
|
|
|
|
) {
|
2006-08-28 18:22:07 +00:00
|
|
|
unsigned int i;
|
|
|
|
for (i=start; i<piv.size(); i++) {
|
|
|
|
PROCINFO& p = piv[i];
|
|
|
|
if (p.id == pid || p.parentid == pid) {
|
|
|
|
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-09-08 19:27:42 +00:00
|
|
|
pi.page_fault_count += p.page_fault_count;
|
2006-08-28 18:22:07 +00:00
|
|
|
p.is_boinc_app = true;
|
2010-04-07 05:54:20 +00:00
|
|
|
}
|
|
|
|
if (!strcmp(p.command, graphics_exec_file)) {
|
|
|
|
p.is_boinc_app = true;
|
|
|
|
}
|
2006-08-28 18:22:07 +00:00
|
|
|
if (p.parentid == pid) {
|
2010-04-07 05:54:20 +00:00
|
|
|
add_proc_totals(pi, piv, p.id, graphics_exec_file, i+1); // recursion - woo hoo!
|
2006-08-28 18:22:07 +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 05:54:20 +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);
|
2006-08-25 21:31:04 +00:00
|
|
|
}
|
2006-08-28 18:22:07 +00:00
|
|
|
|
2006-10-02 23:42:38 +00:00
|
|
|
// get totals of all non-BOINC processes
|
|
|
|
//
|
2006-08-28 18:22:07 +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-01-19 00:03:38 +00:00
|
|
|
if (!p.is_boinc_app && p.id != 0) { // PID 0 is idle process
|
2006-08-28 18:22:07 +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;
|
|
|
|
}
|
|
|
|
}
|
2006-09-11 17:32:18 +00:00
|
|
|
}
|