19
21
#include <w32api/windows.h>
20
22
#include <w32api/windef.h>
21
23
#include <w32api/winreg.h>
24
DWORD dwType = REG_DWORD;
32
ret = RegOpenKeyEx(HKEY_DYN_DATA, "PerfStats\\StartStat", 0, KEY_READ, &hkey);
34
ret = RegQueryValueEx(hkey, "KERNEL\\CPUUsage", 0, &dwType,
35
(unsigned char *) &dwDust, &dw);
36
ret = RegCloseKey(hkey);
44
/* returns current CPU usage in percent */
45
int cpu_get_usage(struct cpu_options *opts)
49
RegOpenKeyEx(HKEY_DYN_DATA, "PerfStats\\StatData", 0, KEY_READ, &hkey);
50
RegQueryValueEx(hkey, "KERNEL\\CPUUsage", 0, &dwType,
51
(unsigned char *) &result, &dw);
24
#include <w32api/winbase.h>
26
#define WIN32_9x 1 /* 95, 98, Me */
27
#define WIN32_NT 2 /* NT, 2000, XP */
30
#define LONGINT2DOUBLE(x) ((double)((x).HighPart) * 4.294967296E9 + (double)((x).LowPart))
32
* The following both data structures aren't defined anywhere in the Microsoft
33
* header files. Taken from DNA's libraly licensed under GPL.
34
* (http://estu.nit.ac.jp/~e982457/freesoft/freesoft.html)
36
typedef struct _SYSTEM_BASIC_INFORMATION { /* Info Class 0 */
41
BYTE bKeNumberProcessors;
44
} SYSTEM_BASIC_INFORMATION;
46
typedef struct _SYSTEM_PERFORMANCE_INFORMATION { /* Info Class 2 */
47
LARGE_INTEGER IdleTime;
49
} SYSTEM_PERFORMANCE_INFORMATION;
51
typedef struct _SYSTEM_TIME_INFORMATION { /* Info Class 3 */
52
LARGE_INTEGER liKeBootTime;
53
LARGE_INTEGER liKeSystemTime;
54
LARGE_INTEGER liExpTimeZoneBias;
55
ULONG uCurrentTimeZoneId;
57
} SYSTEM_TIME_INFORMATION;
59
#define SystemBasicInformation 0
60
#define SystemPerformanceInformation 2
61
#define SystemTimeInformation 3
62
/* end of NT, 2000, XP */
64
static int platform = 0;
69
OSVERSIONINFO os_ver_info;
72
os_ver_info.dwOSVersionInfoSize = sizeof(os_ver_info);
73
GetVersionEx(&os_ver_info);
74
platform = os_ver_info.dwPlatformId;
76
if ((platform != WIN32_9x) && (platform != WIN32_NT)) {
77
fprintf(stderr, "%s: unknown platform\n", PACKAGE);
83
* cpu_get_usage_9x(): get cpu usage in percent via registry
86
* 1. query 'PerfStats.StartStat.KERNEL.CPUUsage' to start monitoring
87
* 2. get usage from 'PerfStats.StatData.KERNEL.CPUUsage'
88
* 3. query 'PerfStats.StopStat.KERNEL.CPUUsage' to stop monitoring
90
* If cpu usage is 100% evry time, please reboot.;(
93
cpu_get_usage_9x(cpu_options *opts)
97
HKEY hkeys; /* for monitoring (start, stop) */
98
HKEY hkeyr; /* for reading usage */
100
DWORD dwsize = sizeof(DWORD);
102
if (RegOpenKeyEx(HKEY_DYN_DATA, "PerfStats\\StatData",
103
0, KEY_READ, &hkeyr) != ERROR_SUCCESS) {
104
fprintf(stderr, "%s: can't open registry 'PerfStats\\StatData'\n", PACKAGE);
108
/* start monitoring */
109
RegOpenKeyEx(HKEY_DYN_DATA, "PerfStats\\StartStat", 0, KEY_READ, &hkeys);
110
RegQueryValueEx(hkeys, "KERNEL\\CPUUsage", 0, NULL, (LPBYTE)&dummy,
115
RegQueryValueEx(hkeyr, "KERNEL\\CPUUsage", 0, NULL, (LPBYTE)&usage,
119
/* stop monitoring */
120
RegOpenKeyEx(HKEY_DYN_DATA, "PerfStats\\StopStat", 0, KEY_READ, &hkeys);
121
RegQueryValueEx(hkeys, "KERNEL\\CPUUsage", 0, NULL, (LPBYTE)&dummy,
132
* 1. Load NTDLL.DLL (should use dlopen?)
133
* 2. Get addresses of NtQuerySystemInformation (should use dlsym?)
134
* 3. Get system time and idle time
135
* 4. Calculate cpu usage
136
* 5. Unload NTDLL.DLL (should use dlclose?)
138
* I do not test this function with SMP system, since I do not have SMP system.
141
cpu_get_usage_NT(cpu_options *opts)
145
static double pre_total = 0, pre_used = 0;
148
FARPROC NtQuerySystemInformation = NULL;
150
SYSTEM_BASIC_INFORMATION sbi;
151
SYSTEM_TIME_INFORMATION sti;
152
SYSTEM_PERFORMANCE_INFORMATION spi;
154
if ((h_ntdll = LoadLibraryEx("NTDLL.DLL", NULL, 0)) == NULL) {
155
fprintf(stderr, "%s: can't load NTDLL.DLL\n", PACKAGE);
159
NtQuerySystemInformation = GetProcAddress(h_ntdll,
160
"NtQuerySystemInformation");
161
if (!NtQuerySystemInformation) {
162
fprintf(stderr, "%s: can't find NtQuerySystemInformation()\n", PACKAGE);
163
FreeLibrary(h_ntdll);
167
if ((NtQuerySystemInformation(SystemBasicInformation,
169
sizeof(SYSTEM_BASIC_INFORMATION),
173
if ((NtQuerySystemInformation(SystemTimeInformation,
175
sizeof(SYSTEM_TIME_INFORMATION),
179
if ((NtQuerySystemInformation(SystemPerformanceInformation,
181
sizeof(SYSTEM_PERFORMANCE_INFORMATION),
185
total = LONGINT2DOUBLE(sti.liKeSystemTime);
186
used = total - LONGINT2DOUBLE(spi.IdleTime);
188
if ((pre_total == 0) || !(total - pre_total > 0)) {
191
usage = (100 * (used - pre_used)) / (total - pre_total);
194
if (sbi.bKeNumberProcessors > 1) {
195
usage = usage / sbi.bKeNumberProcessors;
201
FreeLibrary(h_ntdll);
206
/* return current cpu usage in percent */
208
cpu_get_usage(cpu_options *opts)
212
return cpu_get_usage_9x(opts);
214
return cpu_get_usage_NT(opts);
215
default: /* make gcc happy */