-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.cpp
More file actions
139 lines (113 loc) · 3.51 KB
/
Copy pathcpu.cpp
File metadata and controls
139 lines (113 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* CPU.cpp
* CSCE 463 Sample Code
* by Dmitri Loguinov
*
* Obtains the current CPU utilization
*/
#pragma comment (lib,"Psapi.lib")
#include "stdafx.h"
#include "common.h"
#include "cpu.h"
// NOTE: link with Psapi.lib; this class uses the same functions as Task Manager
CPU::CPU (void)
{
// NOTE: do not use unicode chars in the project; this will not compile!
// project->properties->general->character set = Not Set
hDll = GetModuleHandle("ntdll.dll");
if (hDll != NULL)
{
NtQuerySystemInformation = (NTSTATUS (__stdcall *)(SYSTEM_INFORMATION_CLASS
SystemInformationClass,
PVOID SystemInformation,
ULONG SystemInformationLength,
PULONG ReturnLength)) GetProcAddress(hDll, "NtQuerySystemInformation");
if (NtQuerySystemInformation != NULL)
{
SYSTEM_INFORMATION_CLASS query = SystemProcessorPerformanceInformation;
NTSTATUS code = (NtQuerySystemInformation)(query, info,
sizeof (SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)*MAX_CPU, &len);
// how many CPUs
this->cpus = len / sizeof (SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
printf ("found %d CPUs, total RAM %d MB\n", cpus, GetSystemRAM ());
for (int i=0; i<cpus; i++)
{
this->idle[i] = info[i].IdleTime.QuadPart;
this->kernel[i] = info[i].KernelTime.QuadPart;
this->user[i] = info[i].UserTime.QuadPart;
}
}
}
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, GetCurrentProcessId());
}
CPU::~CPU()
{
if (hProcess != NULL)
CloseHandle (hProcess);
}
// returns utilization * 100
double CPU::GetCpuUtilization (double *array)
{
SYSTEM_INFORMATION_CLASS query = SystemProcessorPerformanceInformation;
if (hDll == NULL || NtQuerySystemInformation == NULL)
return -1;
NTSTATUS code = (NtQuerySystemInformation)(query, info,
sizeof (SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)*MAX_CPU, &len);
double average = 0;
for (int i = 0; i < cpus; i++)
{
__int64 idle_dur = info[i].IdleTime.QuadPart - idle[i];
__int64 kernel_dur = info[i].KernelTime.QuadPart - kernel[i];
__int64 user_dur = info[i].UserTime.QuadPart - user[i];
__int64 sys_time = kernel_dur + user_dur;
if (sys_time == 0) // called too often; need to wait between calls
return -2;
double val = 100.0 * (sys_time - idle_dur) / sys_time;
// store individual CPU utilization if desired by caller
if (array != NULL)
array[i] = val;
average += val;
idle[i] = info[i].IdleTime.QuadPart;
kernel[i] = info[i].KernelTime.QuadPart;
user[i] = info[i].UserTime.QuadPart;
}
return average/cpus;
}
// return in megabytes
int CPU::GetProcessRAMUsage(void)
{
if (hProcess != NULL)
{
PROCESS_MEMORY_COUNTERS pp;
if (GetProcessMemoryInfo (hProcess, &pp, sizeof (PROCESS_MEMORY_COUNTERS)))
// physical memory usage
//return (int)floor(pp.WorkingSetSize/MEGABYTE + 0.5);
// virtual memory usage
return (int)floor(pp.PagefileUsage/MEGABYTE + 0.5);
else
return 0;
}
return 0;
}
int CPU::GetSystemRAM (void)
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
// total virtual memory
//return (int) (statex.ullTotalPageFile/MEGABYTE + 0.5); //(statex.ullTotalPhys/1e6);
// total physical memory
return (int) (statex.ullTotalPhys/MEGABYTE + 0.5);
}
int CPU::GetSystemRAMUsage (void)
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
// physical memory
//return ((statex.ullTotalPhys-statex.ullAvailPhys)/1e6);
// virtual memory
return (int) ((statex.ullTotalPageFile -
statex.ullAvailPageFile)/MEGABYTE + 0.5);
}