-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.cpp
31 lines (23 loc) · 815 Bytes
/
cpu.cpp
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
#include <iostream>
#include <windows.h>
#include "cpu.h"
ULONGLONG SubtractTimes(const FILETIME& ftA, const FILETIME& ftB) {
LARGE_INTEGER a, b;
a.LowPart = ftA.dwLowDateTime;
a.HighPart = ftA.dwHighDateTime;
b.LowPart = ftB.dwLowDateTime;
b.HighPart = ftB.dwHighDateTime;
return a.QuadPart - b.QuadPart;
}
void cpu() {
FILETIME idleTimeNow, kernelTimeNow, userTimeNow;
FILETIME idleTimeLast, kernelTimeLast, userTimeLast;
double idle, kernel, user, system;
std::cout << "Current CPU Load : ";
GetSystemTimes(&idleTimeNow, &kernelTimeNow, &userTimeNow);
user = SubtractTimes(userTimeNow, userTimeLast);
kernel = SubtractTimes(kernelTimeNow, kernelTimeLast);
idle = SubtractTimes(idleTimeNow, idleTimeLast);
system = kernel + user;
std::cout << (system - idle) * 100 / system << std::endl;
}