-
Notifications
You must be signed in to change notification settings - Fork 464
Expand file tree
/
Copy pathCpuUsage.cpp
More file actions
57 lines (47 loc) · 1.15 KB
/
Copy pathCpuUsage.cpp
File metadata and controls
57 lines (47 loc) · 1.15 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
#include "CpuUsage.h"
static DWORD Quota;
static DWORD CpuUsageThreadCount;
static HANDLE *CpuUsageThreads;
VOID InitializeCpuUsage()
{
Quota = 0;
SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
CpuUsageThreadCount = systemInfo.dwNumberOfProcessors;
CpuUsageThreads = new HANDLE[CpuUsageThreadCount];
for (DWORD i = 0; i < CpuUsageThreadCount; i++)
{
CpuUsageThreads[i] = CreateThread(NULL, 0, CpuUsageThreadFunc, NULL, 0, NULL);
SetThreadPriority(CpuUsageThreads[i], THREAD_PRIORITY_IDLE);
}
}
VOID UninitializeCpuUsage()
{
for (DWORD i = 0; i < CpuUsageThreadCount; i++)
{
TerminateThread(CpuUsageThreads[i], 0);
CloseHandle(CpuUsageThreads[i]);
}
delete[] CpuUsageThreads;
}
static DWORD WINAPI CpuUsageThreadFunc(LPVOID parameter)
{
while (TRUE)
{
DWORD busyTime = Quota;
DWORD idleTime = 100 - Quota;
for (ULONGLONG startTime = GetTickCount64(); GetTickCount64() - startTime < busyTime;)
{
}
if (idleTime > 0)
{
Sleep(idleTime);
}
}
return 0;
}
VOID SetCpuUsage(DWORD quota)
{
Quota = quota;
}