-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPU.cpp
196 lines (169 loc) · 5.39 KB
/
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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#pragma comment(lib,"Pdh.lib")
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <locale.h>
#include <Pdh.h>
#include <string>
#include <pdhmsg.h>
#include <tchar.h>
#include <windows.h>
#include <iostream>
using namespace std;
int get_main()
{
HQUERY m_hQuery;
HCOUNTER hCounter;
PDH_FMT_COUNTERVALUE* m_pDisplayValue;
PDH_STATUS status = ERROR_SUCCESS;
m_pDisplayValue = new PDH_FMT_COUNTERVALUE();
status = PdhOpenQuery(NULL, 0, &m_hQuery);
//if (ERROR_SUCCESS == status)
//{
status = PdhAddCounter(m_hQuery, _T("\\Processor Information(_Total)\\% Processor Utility"), 0, &hCounter);
//WIN10以前的路径换成:\\Processor(_Total)\\% Processor Time
if (ERROR_SUCCESS != status)
{
return status;
}
//}
while (1)
{
PDH_STATUS status = ERROR_SUCCESS;
DWORD dwCounterType = 0;
PdhCollectQueryData(m_hQuery);
status = PdhGetFormattedCounterValue(hCounter, PDH_FMT_DOUBLE, &dwCounterType, m_pDisplayValue);
double dCpuLoad = *(double*)((char*)m_pDisplayValue + 8);
printf("CPULoad:%f\n", dCpuLoad);
Sleep(500);
}
return 0;
}
int getCPU()
{
HQUERY query;
PDH_STATUS status = PdhOpenQuery(NULL, NULL, &query);
PDH_STATUS AvilableMemorystatus = PdhOpenQuery(NULL, NULL, &query);
if (status != ERROR_SUCCESS)
cout << "Open Query Error" << endl;
HCOUNTER counter;
//counter = (HCOUNTER*)GlobalAlloc(GPTR, sizeof(HCOUNTER));
HCOUNTER cntProcessCPU, cntAvilableMemory;
status = PdhAddCounter(query, TEXT("\\Processor Information(_Total)\\% Processor Utility"), NULL, &counter);
PdhCollectQueryData(query);
Sleep(1000);
PdhCollectQueryData(query);
PDH_FMT_COUNTERVALUE pdhValue;
DWORD dwValue;
status = PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE, &dwValue, &pdhValue);
if (status != ERROR_SUCCESS)
cout << "Get Value Error" << endl;
cout << "processCPU:" << pdhValue.doubleValue << endl;
return 0;
}
//
/*
通过性能计数器获取某个进程的CPU使用率、内存使用量、磁盘读写速率
使用性能计数器中的Process实现
*/
int nCPU;
using std::string;
using namespace std;
int GetData()
{
HQUERY query;
double dbVal;
long iVal;
//PDH_STATUS status = PdhOpenQuery(NULL, NULL, &query);
PDH_STATUS status = PdhOpenQuery(0, 0, &query);
if (ERROR_SUCCESS != status)
{
MessageBox(NULL, TEXT("打开失败"), TEXT(""), MB_OK);
return -1;
}
HCOUNTER cntProcessCPU, cntProcessMemory;
HCOUNTER cntProcessDiskRead, cntProcessDiskWrite;
status = PdhAddCounter(query, TEXT("\\Processor Information(_Total)\\% Processor Utility"), NULL, &cntProcessCPU);
status = PdhAddCounter(query, TEXT("\\Process(_Total)\\Working Set - Private"), NULL, &cntProcessMemory);
status = PdhAddCounter(query, TEXT("\\Process(_Total)\\IO Read Bytes/sec"), NULL, &cntProcessDiskRead);
status = PdhAddCounter(query, TEXT("\\Process(_Total)\\IO Write Bytes/sec"), NULL, &cntProcessDiskWrite);
if (ERROR_SUCCESS != status)
{
MessageBox(NULL, TEXT("添加失败"), TEXT(""), MB_OK);
return -1;
}
status = PdhCollectQueryData(query);
Sleep(1000); //这里要有延时不然结果相当不准确
status = PdhCollectQueryData(query);
if (ERROR_SUCCESS != status)
{
MessageBox(NULL, TEXT("数据请求失败"), TEXT(""), MB_OK);
return -1;
}
PDH_FMT_COUNTERVALUE pdhValue;
DWORD dwValue;
status = PdhGetFormattedCounterValue(cntProcessCPU, PDH_FMT_DOUBLE, &dwValue, &pdhValue);
if (ERROR_SUCCESS != status)
{
MessageBox(NULL, TEXT("得到数据失败"), TEXT(""), MB_OK);
return -1;
}
else
{
dbVal = pdhValue.doubleValue;
printf("Process-CPU: %.3f%% ", (dbVal));
}
status = PdhGetFormattedCounterValue(cntProcessMemory, PDH_FMT_DOUBLE, &dwValue, &pdhValue);
if (ERROR_SUCCESS != status)
{
MessageBox(NULL, TEXT("得到数据失败"), TEXT(""), MB_OK);
return -1;
}
else
{
dbVal = pdhValue.doubleValue;
printf("Process-Memory: %8dK \n", (int)(dbVal / 1024));
}
status = PdhGetFormattedCounterValue(cntProcessDiskRead, PDH_FMT_DOUBLE, &dwValue, &pdhValue);
if (ERROR_SUCCESS != status)
{
MessageBox(NULL, TEXT("得到数据失败"), TEXT(""), MB_OK);
return -1;
}
else
{
dbVal = pdhValue.doubleValue;
printf("Process-DiskRead:%8dK/s ", (int)(dbVal / 1024));
}
status = PdhGetFormattedCounterValue(cntProcessDiskWrite, PDH_FMT_DOUBLE, &dwValue, &pdhValue);
if (ERROR_SUCCESS != status)
{
MessageBox(NULL, TEXT("得到数据失败"), TEXT(""), MB_OK);
return -1;
}
else
{
dbVal = pdhValue.doubleValue;
printf("Process-DiskWrite:%8dK/s \n*****************************************************************************\n", (int)(dbVal / 1024));
}
PdhRemoveCounter(cntProcessCPU);
PdhRemoveCounter(cntProcessMemory);
PdhRemoveCounter(cntProcessDiskRead);
PdhRemoveCounter(cntProcessDiskWrite);
PdhCloseQuery(query);
}
int main() {
setlocale(LC_ALL, "chs");
SYSTEM_INFO si;
GetSystemInfo(&si);
nCPU = si.dwNumberOfProcessors;
printf("hello\n");
while (1)
{
//getCPU();
GetData();
//get_main();
}
system("pause");
return 0;
}