Skip to content

Commit 655bc9c

Browse files
committed
新增Lite版显示频率的功能,修正ARM64EC显示CPU频率不正常的问题
1 parent 04f4bb3 commit 655bc9c

File tree

9 files changed

+210
-110
lines changed

9 files changed

+210
-110
lines changed

TrafficMonitor/CPUUsage.cpp

Lines changed: 119 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,143 @@
22
#include "CPUUsage.h"
33
#include "Common.h"
44
#include "TrafficMonitor.h"
5+
#include <powerbase.h>
6+
#include <sysinfoapi.h>
57

68
void CCPUUsage::SetUseCPUTimes(bool use_get_system_times)
79
{
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+
}
1315
}
1416

1517
int CCPUUsage::GetCPUUsage()
1618
{
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();
2123
}
2224

2325
int CCPUUsage::GetCPUUsageByGetSystemTimes()
2426
{
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);
3032

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);
3436

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;
4749

48-
return cpu_usage;
50+
return cpu_usage;
4951
}
5052

5153
int CCPUUsage::GetCPUUsageByPdh()
5254
{
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;
5860

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);
6870

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;
83144
}

TrafficMonitor/CPUUsage.h

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
#pragma once
22
#include <Pdh.h>
33
#include <PdhMsg.h>
4-
#pragma comment(lib,"pdh.lib")
54

65
class CCPUUsage
76
{
87
public:
9-
CCPUUsage()
10-
{}
8+
CCPUUsage()
9+
{}
1110

12-
~CCPUUsage()
13-
{}
11+
~CCPUUsage()
12+
{}
1413

15-
void SetUseCPUTimes(bool use_get_system_times); //设置获取CPU利用率的方式,是通过GetSystemTimes还是Pdh
16-
int GetCPUUsage();
14+
void SetUseCPUTimes(bool use_get_system_times); //设置获取CPU利用率的方式,是通过GetSystemTimes还是Pdh
15+
int GetCPUUsage();
1716

1817
private:
19-
int GetCPUUsageByGetSystemTimes();
20-
int GetCPUUsageByPdh();
18+
int GetCPUUsageByGetSystemTimes();
19+
int GetCPUUsageByPdh();
2120

2221
private:
23-
bool m_use_get_system_times{ true }; //是否使用GetSysTime这个API来获取CPU利用率
22+
bool m_use_get_system_times{ true }; //是否使用GetSysTime这个API来获取CPU利用率
2423

25-
PDH_RAW_COUNTER m_last_rawData;//保存计算CPU使用率的上一次数据
26-
bool m_first_get_CPU_utility{ true };
24+
PDH_RAW_COUNTER m_last_rawData{};//保存计算CPU使用率的上一次数据
25+
bool m_first_get_CPU_utility{ true };
2726

28-
FILETIME m_preidleTime{};
29-
FILETIME m_prekernelTime{};
30-
FILETIME m_preuserTime{};
27+
FILETIME m_preidleTime{};
28+
FILETIME m_prekernelTime{};
29+
FILETIME m_preuserTime{};
3130

3231
};
3332

33+
class CCpuFreq
34+
{
35+
public:
36+
CCpuFreq();
37+
float GetCpuFreq();
38+
39+
private:
40+
float max_cpu_freq = 0;
41+
};

TrafficMonitor/GeneralSettingsDlg.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ void CGeneralSettingsDlg::CheckTaskbarDisplayItem()
3131
if (!theApp.m_general_data.IsHardwareEnable(HI_CPU))
3232
{
3333
theApp.m_taskbar_data.m_tbar_display_item &= ~TDI_CPU_TEMP;
34-
theApp.m_taskbar_data.m_tbar_display_item &= ~TDI_CPU_FREQ;
3534
}
3635
if (!theApp.m_general_data.IsHardwareEnable(HI_GPU))
3736
{

TrafficMonitor/TaskBarDlg.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,12 @@ CString CTaskBarDlg::GetMouseTipsInfo()
927927
CCommon::KBytesToString(theApp.m_total_memory));
928928
tip_info += temp;
929929
}
930+
if (!IsItemShow(TDI_CPU_FREQ) && theApp.m_cpu_freq > 0)
931+
{
932+
temp.Format(_T("\r\n%s: %s"), CCommon::LoadText(IDS_CPU_FREQ), CCommon::FreqToString(theApp.m_cpu_freq, theApp.m_taskbar_data));
933+
tip_info += temp;
934+
}
935+
930936
#ifndef WITHOUT_TEMPERATURE
931937
CTrafficMonitorDlg* pMainWnd = dynamic_cast<CTrafficMonitorDlg*>(theApp.m_pMainWnd);
932938
if (pMainWnd->IsTemperatureNeeded())
@@ -941,11 +947,6 @@ CString CTaskBarDlg::GetMouseTipsInfo()
941947
temp.Format(_T("\r\n%s: %s"), CCommon::LoadText(IDS_CPU_TEMPERATURE), CCommon::TemperatureToString(theApp.m_cpu_temperature, theApp.m_taskbar_data));
942948
tip_info += temp;
943949
}
944-
if (!IsItemShow(TDI_CPU_FREQ) && theApp.m_cpu_freq > 0)
945-
{
946-
temp.Format(_T("\r\n%s: %s"), CCommon::LoadText(IDS_CPU_FREQ), CCommon::FreqToString(theApp.m_cpu_freq, theApp.m_taskbar_data));
947-
tip_info += temp;
948-
}
949950
if (!IsItemShow(TDI_GPU_TEMP) && theApp.m_gpu_temperature > 0)
950951
{
951952
temp.Format(_T("\r\n%s: %s"), CCommon::LoadText(IDS_GPU_TEMPERATURE), CCommon::TemperatureToString(theApp.m_gpu_temperature, theApp.m_taskbar_data));

TrafficMonitor/TaskbarItemOrderHelper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ bool CTaskbarItemOrderHelper::IsItemDisplayed(CommonDisplayItem item)
225225
bool displayed = true;
226226
if (!item.is_plugin)
227227
{
228-
if ((item == TDI_CPU_TEMP || item == TDI_CPU_FREQ) && !theApp.m_general_data.IsHardwareEnable(HI_CPU))
228+
if ((item == TDI_CPU_TEMP) && !theApp.m_general_data.IsHardwareEnable(HI_CPU))
229229
displayed = false;
230230
if ((item == TDI_GPU_TEMP || item == TDI_GPU_USAGE) && !theApp.m_general_data.IsHardwareEnable(HI_GPU))
231231
displayed = false;

TrafficMonitor/TaskbarItemOrderHelper.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ const std::set<DisplayItem> AllDisplayItems
4141
{
4242
TDI_UP, TDI_DOWN, TDI_CPU, TDI_MEMORY
4343
#ifndef WITHOUT_TEMPERATURE
44-
, TDI_GPU_USAGE, TDI_CPU_TEMP, TDI_GPU_TEMP, TDI_HDD_TEMP, TDI_MAIN_BOARD_TEMP, TDI_HDD_USAGE,TDI_CPU_FREQ
44+
, TDI_GPU_USAGE, TDI_CPU_TEMP, TDI_GPU_TEMP, TDI_HDD_TEMP, TDI_MAIN_BOARD_TEMP, TDI_HDD_USAGE
4545
#endif
46-
, TDI_TOTAL_SPEED, TDI_TODAY_TRAFFIC
46+
,TDI_CPU_FREQ, TDI_TOTAL_SPEED, TDI_TODAY_TRAFFIC
4747
};
4848

4949

0 commit comments

Comments
 (0)