/
CpuData.cpp
119 lines (96 loc) · 2.64 KB
/
CpuData.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
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
#pragma comment(lib, "pdh.lib")
#include<iostream>
#include<string>
#include<windows.h>
#include "TCHAR.h"
#include "pdh.h"
#include "atlstr.h"
#include<time.h>
using namespace std;
static PDH_HQUERY cpuQuery;
static PDH_HCOUNTER cpuTotal;
using namespace std;
class CpuData {
private:
SYSTEM_INFO SysInfo;
wchar_t CpuName[255];//cpu 이름 O
double CpuUsage;//cpu 사용량 O
DWORD CpuSpeed; //cpu 속도 O 기본속도
int ProcessNum; //process 갯수 O
int ThreadNum; //thread 갯수 O
int HandleNum; //핸들러 개수(?)
time_t BeginTime, WorkTime; // 작동된 시간 O
int SocketNum; //소켓 갯수(?)
int CoreNum; //코어 갯수(?)
int logicProc; //논리 프로세서(O)
bool isVirt; //가상화 사용여부(?)
double L1Cach; //L1 캐시 용량(?)
double L2Cach; //L2 캐시 용량(?)
double L3Cach; //L3 캐시 용량(?)
HKEY hKey;
DWORD cname_size;
DWORD cspeed_size;
public:
CpuData() {
BeginTime = clock();
//init 생성자
GetSystemInfo(&SysInfo);
//cpu 타입,
InitReg();
InitCPUName();
InitThreadNum();
InitCPUUsage();
}
void InitThreadNum() {
ThreadNum = SysInfo.dwNumberOfProcessors; // cpu의 스레드 개수임.
}
void InitCPUUsage() {
PdhOpenQuery(NULL, NULL, &cpuQuery);
PdhAddCounter(cpuQuery, L"\\Processor(_Total)\\% Processor Time", NULL, &cpuTotal);
PdhCollectQueryData(cpuQuery);
}
void InitReg() {
cname_size = sizeof(CpuName);
cspeed_size = sizeof(CpuSpeed);
RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Hardware\\Description\\System\\CentralProcessor\\0", 0, KEY_QUERY_VALUE, &hKey);
RegQueryValueEx(hKey, L"ProcessorNameString", NULL, NULL, (LPBYTE)CpuName, &cname_size);
RegQueryValueEx(hKey, L"~MHz", NULL, NULL, (LPBYTE)&CpuSpeed, &cspeed_size); // CPU Speed
RegCloseKey(hKey);
}
void InitCPUName() {
//GetSystemInfo 함수를 이용해 논리적 코어 개수를 얻어냅니다.
wchar_t num[8];
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
swprintf(num, 8, L" * %d", systemInfo.dwNumberOfProcessors);
wcscat_s(CpuName, 100, num);
}
double GetCpuUsage() {
PDH_FMT_COUNTERVALUE counterVal;
PdhCollectQueryData(cpuQuery);
PdhGetFormattedCounterValue(cpuTotal, PDH_FMT_DOUBLE, NULL, &counterVal);
return counterVal.doubleValue;
}
wstring GetCpuName() {
return CpuName;
}
DWORD GetCpuSpeed() {
return CpuSpeed / 1000.0;
}
time_t GetWorkTime() {
WorkTime = clock();
return (WorkTime - BeginTime) / 1000.0;
}
};
int main() {
CpuData c;
wcout << "CPU Name : " << c.GetCpuName() << "\n";
while (true) {
cout << "CPU Usage : " << c.GetCpuUsage() << "% \n";
cout << "CPU Speed : " << c.GetCpuSpeed() << "GHz \n";
cout << "CPU WorkTime : " << c.GetWorkTime() << "Sec \n";
cout << "Sleep !\n\n";
Sleep(1000);
}
return 0;
}