|
2 | 2 | #include "CPUUsage.h" |
3 | 3 | #include "Common.h" |
4 | 4 | #include "TrafficMonitor.h" |
| 5 | +#include <powerbase.h> |
| 6 | +#include <sysinfoapi.h> |
5 | 7 |
|
6 | 8 | void CCPUUsage::SetUseCPUTimes(bool use_get_system_times) |
7 | 9 | { |
8 | | - if (m_use_get_system_times != use_get_system_times) |
9 | | - { |
10 | | - m_use_get_system_times = use_get_system_times; |
11 | | - m_first_get_CPU_utility = true; |
12 | | - } |
| 10 | + if (m_use_get_system_times != use_get_system_times) |
| 11 | + { |
| 12 | + m_use_get_system_times = use_get_system_times; |
| 13 | + m_first_get_CPU_utility = true; |
| 14 | + } |
13 | 15 | } |
14 | 16 |
|
15 | 17 | int CCPUUsage::GetCPUUsage() |
16 | 18 | { |
17 | | - if (m_use_get_system_times) |
18 | | - return GetCPUUsageByGetSystemTimes(); |
19 | | - else |
20 | | - return GetCPUUsageByPdh(); |
| 19 | + if (m_use_get_system_times) |
| 20 | + return GetCPUUsageByGetSystemTimes(); |
| 21 | + else |
| 22 | + return GetCPUUsageByPdh(); |
21 | 23 | } |
22 | 24 |
|
23 | 25 | int CCPUUsage::GetCPUUsageByGetSystemTimes() |
24 | 26 | { |
25 | | - int cpu_usage{}; |
26 | | - FILETIME idleTime; |
27 | | - FILETIME kernelTime; |
28 | | - FILETIME userTime; |
29 | | - GetSystemTimes(&idleTime, &kernelTime, &userTime); |
| 27 | + int cpu_usage{}; |
| 28 | + FILETIME idleTime; |
| 29 | + FILETIME kernelTime; |
| 30 | + FILETIME userTime; |
| 31 | + GetSystemTimes(&idleTime, &kernelTime, &userTime); |
30 | 32 |
|
31 | | - __int64 idle = CCommon::CompareFileTime2(m_preidleTime, idleTime); |
32 | | - __int64 kernel = CCommon::CompareFileTime2(m_prekernelTime, kernelTime); |
33 | | - __int64 user = CCommon::CompareFileTime2(m_preuserTime, userTime); |
| 33 | + __int64 idle = CCommon::CompareFileTime2(m_preidleTime, idleTime); |
| 34 | + __int64 kernel = CCommon::CompareFileTime2(m_prekernelTime, kernelTime); |
| 35 | + __int64 user = CCommon::CompareFileTime2(m_preuserTime, userTime); |
34 | 36 |
|
35 | | - if (kernel + user == 0) |
36 | | - { |
37 | | - cpu_usage = 0; |
38 | | - } |
39 | | - else |
40 | | - { |
41 | | - //(总的时间-空闲时间)/总的时间=占用cpu的时间就是使用率 |
42 | | - cpu_usage = static_cast<int>(abs((kernel + user - idle) * 100 / (kernel + user))); |
43 | | - } |
44 | | - m_preidleTime = idleTime; |
45 | | - m_prekernelTime = kernelTime; |
46 | | - m_preuserTime = userTime; |
| 37 | + if (kernel + user == 0) |
| 38 | + { |
| 39 | + cpu_usage = 0; |
| 40 | + } |
| 41 | + else |
| 42 | + { |
| 43 | + //(总的时间-空闲时间)/总的时间=占用cpu的时间就是使用率 |
| 44 | + cpu_usage = static_cast<int>(abs((kernel + user - idle) * 100 / (kernel + user))); |
| 45 | + } |
| 46 | + m_preidleTime = idleTime; |
| 47 | + m_prekernelTime = kernelTime; |
| 48 | + m_preuserTime = userTime; |
47 | 49 |
|
48 | | - return cpu_usage; |
| 50 | + return cpu_usage; |
49 | 51 | } |
50 | 52 |
|
51 | 53 | int CCPUUsage::GetCPUUsageByPdh() |
52 | 54 | { |
53 | | - int cpu_usage{}; |
54 | | - HQUERY hQuery; |
55 | | - HCOUNTER hCounter; |
56 | | - DWORD counterType; |
57 | | - PDH_RAW_COUNTER rawData; |
| 55 | + int cpu_usage{}; |
| 56 | + HQUERY hQuery; |
| 57 | + HCOUNTER hCounter; |
| 58 | + DWORD counterType; |
| 59 | + PDH_RAW_COUNTER rawData; |
58 | 60 |
|
59 | | - PdhOpenQuery(NULL, 0, &hQuery);//开始查询 |
60 | | - const wchar_t* query_str{}; |
61 | | - if (theApp.m_win_version.GetMajorVersion() >= 10) |
62 | | - query_str = L"\\Processor Information(_Total)\\% Processor Utility"; |
63 | | - else |
64 | | - query_str = L"\\Processor Information(_Total)\\% Processor Time"; |
65 | | - PdhAddCounter(hQuery, query_str, NULL, &hCounter); |
66 | | - PdhCollectQueryData(hQuery); |
67 | | - PdhGetRawCounterValue(hCounter, &counterType, &rawData); |
| 61 | + PdhOpenQuery(NULL, 0, &hQuery);//开始查询 |
| 62 | + const wchar_t* query_str{}; |
| 63 | + if (theApp.m_win_version.GetMajorVersion() >= 10) |
| 64 | + query_str = L"\\Processor Information(_Total)\\% Processor Utility"; |
| 65 | + else |
| 66 | + query_str = L"\\Processor Information(_Total)\\% Processor Time"; |
| 67 | + PdhAddCounter(hQuery, query_str, NULL, &hCounter); |
| 68 | + PdhCollectQueryData(hQuery); |
| 69 | + PdhGetRawCounterValue(hCounter, &counterType, &rawData); |
68 | 70 |
|
69 | | - if (m_first_get_CPU_utility) {//需要获得两次数据才能计算CPU使用率 |
70 | | - cpu_usage = 0; |
71 | | - m_first_get_CPU_utility = false; |
72 | | - } |
73 | | - else { |
74 | | - PDH_FMT_COUNTERVALUE fmtValue; |
75 | | - PdhCalculateCounterFromRawValue(hCounter, PDH_FMT_DOUBLE, &rawData, &m_last_rawData, &fmtValue);//计算使用率 |
76 | | - cpu_usage = fmtValue.doubleValue;//传出数据 |
77 | | - if (cpu_usage > 100) |
78 | | - cpu_usage = 100; |
79 | | - } |
80 | | - m_last_rawData = rawData;//保存上一次数据 |
81 | | - PdhCloseQuery(hQuery);//关闭查询 |
82 | | - return cpu_usage; |
| 71 | + if (m_first_get_CPU_utility) {//需要获得两次数据才能计算CPU使用率 |
| 72 | + cpu_usage = 0; |
| 73 | + m_first_get_CPU_utility = false; |
| 74 | + } |
| 75 | + else { |
| 76 | + PDH_FMT_COUNTERVALUE fmtValue; |
| 77 | + PdhCalculateCounterFromRawValue(hCounter, PDH_FMT_DOUBLE, &rawData, &m_last_rawData, &fmtValue);//计算使用率 |
| 78 | + cpu_usage = fmtValue.doubleValue;//传出数据 |
| 79 | + if (cpu_usage > 100) |
| 80 | + cpu_usage = 100; |
| 81 | + } |
| 82 | + m_last_rawData = rawData;//保存上一次数据 |
| 83 | + PdhCloseQuery(hQuery);//关闭查询 |
| 84 | + return cpu_usage; |
| 85 | +} |
| 86 | + |
| 87 | +/////////////////////////////////////////////////////////////////////////////////////////// |
| 88 | +/////////////////////////////////////////////////////////////////////////////////////////// |
| 89 | + |
| 90 | +typedef struct _PROCESSOR_POWER_INFORMATION { |
| 91 | + ULONG Number; |
| 92 | + ULONG MaxMhz; |
| 93 | + ULONG CurrentMhz; |
| 94 | + ULONG MhzLimit; |
| 95 | + ULONG MaxIdleState; |
| 96 | + ULONG CurrentIdleState; |
| 97 | +} PROCESSOR_POWER_INFORMATION, * PPROCESSOR_POWER_INFORMATION; |
| 98 | + |
| 99 | +CCpuFreq::CCpuFreq() |
| 100 | +{ |
| 101 | + SYSTEM_INFO si; |
| 102 | + GetSystemInfo(&si); |
| 103 | + auto ppInfo = std::vector<PROCESSOR_POWER_INFORMATION>(si.dwNumberOfProcessors); |
| 104 | + auto status = CallNtPowerInformation(POWER_INFORMATION_LEVEL::ProcessorInformation, |
| 105 | + NULL, 0, &ppInfo[0], sizeof(PROCESSOR_POWER_INFORMATION) * ppInfo.size()); |
| 106 | + for (size_t i = 0; i < ppInfo.size(); i++) |
| 107 | + { |
| 108 | + max_cpu_freq = max(max_cpu_freq, ppInfo[i].MaxMhz / 1000.f); |
| 109 | + } |
| 110 | + |
| 111 | +} |
| 112 | + |
| 113 | +float CCpuFreq::GetCpuFreq() |
| 114 | +{ |
| 115 | + float freq{}; |
| 116 | + HQUERY query; |
| 117 | + PDH_STATUS status = PdhOpenQuery(NULL, NULL, &query); |
| 118 | + if (status != ERROR_SUCCESS) |
| 119 | + { |
| 120 | + freq = 0.1; |
| 121 | + return true; |
| 122 | + } |
| 123 | + HCOUNTER counter; |
| 124 | + status = PdhAddCounterA(query, LPCSTR("\\Processor Information(_Total)\\% Processor Performance"), NULL, &counter); |
| 125 | + if (status != ERROR_SUCCESS) |
| 126 | + { |
| 127 | + freq = 0.2; |
| 128 | + return true; |
| 129 | + } |
| 130 | + PdhCollectQueryData(query); |
| 131 | + Sleep(200); |
| 132 | + PdhCollectQueryData(query); |
| 133 | + PDH_FMT_COUNTERVALUE pdhValue; |
| 134 | + DWORD dwValue; |
| 135 | + status = PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE, &dwValue, &pdhValue); |
| 136 | + if (status != ERROR_SUCCESS) |
| 137 | + { |
| 138 | + freq = 0.3; |
| 139 | + return true; |
| 140 | + } |
| 141 | + PdhCloseQuery(query); |
| 142 | + freq = pdhValue.doubleValue / 100 * max_cpu_freq; |
| 143 | + return freq; |
83 | 144 | } |
0 commit comments