Skip to content

Commit 1563301

Browse files
committed
新增通过pdh接口获取显卡利用率的功能,实现Lite版也能显示显卡利用率
1 parent cd46521 commit 1563301

19 files changed

+473
-282
lines changed

TrafficMonitor/CPUUsage.cpp

Lines changed: 0 additions & 188 deletions
This file was deleted.

TrafficMonitor/CPUUsage.h

Lines changed: 0 additions & 57 deletions
This file was deleted.

TrafficMonitor/DisplayItem.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ enum DisplayItem
2222
//所有内置显示项目的集合
2323
const std::set<DisplayItem> AllDisplayItems
2424
{
25-
TDI_UP, TDI_DOWN, TDI_CPU, TDI_MEMORY
25+
TDI_UP, TDI_DOWN, TDI_CPU, TDI_MEMORY, TDI_GPU_USAGE
2626
#ifndef WITHOUT_TEMPERATURE
27-
, TDI_GPU_USAGE, TDI_CPU_TEMP, TDI_GPU_TEMP, TDI_HDD_TEMP, TDI_MAIN_BOARD_TEMP, TDI_HDD_USAGE
27+
, TDI_CPU_TEMP, TDI_GPU_TEMP, TDI_HDD_TEMP, TDI_MAIN_BOARD_TEMP, TDI_HDD_USAGE
2828
#endif
2929
,TDI_CPU_FREQ, TDI_TOTAL_SPEED, TDI_TODAY_TRAFFIC
3030
};

TrafficMonitor/GeneralSettingsDlg.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ void CGeneralSettingsDlg::CheckTaskbarDisplayItem()
3333
}
3434
if (!theApp.m_general_data.IsHardwareEnable(HI_GPU))
3535
{
36-
theApp.m_taskbar_data.display_item.Remove(TDI_GPU_USAGE);
3736
theApp.m_taskbar_data.display_item.Remove(TDI_GPU_TEMP);
3837
}
3938
if (!theApp.m_general_data.IsHardwareEnable(HI_HDD))
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include "stdafx.h"
2+
#include "CPUUsage.h"
3+
#include "Common.h"
4+
#include "TrafficMonitor.h"
5+
#include <powerbase.h>
6+
#include <sysinfoapi.h>
7+
8+
9+
///////////////////////////////////////////////////////////////////////////////////////
10+
///////////////////////////////////////////////////////////////////////////////////////
11+
CPdhCPUUsage::CPdhCPUUsage()
12+
: CPdhQuery(theApp.m_win_version.GetMajorVersion() >= 10 ? _T("\\Processor Information(_Total)\\% Processor Utility") : _T("\\Processor Information(_Total)\\% Processor Time"))
13+
{
14+
}
15+
16+
bool CPdhCPUUsage::GetCPUUsage(int& cpu_usage)
17+
{
18+
double value{};
19+
if (QueryValue(value))
20+
{
21+
cpu_usage = static_cast<int>(value);
22+
if (cpu_usage > 100)
23+
cpu_usage = 100;
24+
return true;
25+
}
26+
return false;
27+
}
28+
29+
///////////////////////////////////////////////////////////////////////////////////////
30+
///////////////////////////////////////////////////////////////////////////////////////
31+
CCPUUsage::CCPUUsage()
32+
{
33+
}
34+
35+
void CCPUUsage::SetUseCPUTimes(bool use_get_system_times)
36+
{
37+
if (m_use_get_system_times != use_get_system_times)
38+
{
39+
m_use_get_system_times = use_get_system_times;
40+
}
41+
}
42+
43+
int CCPUUsage::GetCpuUsage()
44+
{
45+
int cpu_usage{};
46+
if (m_use_get_system_times)
47+
{
48+
cpu_usage = GetCpuUsageByGetSystemTimes();
49+
}
50+
else
51+
{
52+
//如果通过pdh获取CPU利用率失败,采用GetSystemTimes获取
53+
if (!m_pdh_cup_usage_query.GetCPUUsage(cpu_usage))
54+
{
55+
cpu_usage = GetCpuUsageByGetSystemTimes();
56+
//写入日志
57+
//static bool write_log = false;
58+
//if (!write_log)
59+
//{
60+
// CString str_log = CCommon::LoadTextFormat(IDS_GET_CPU_USAGE_BY_PDH_FAILED_LOG, { fullCounterPath });
61+
// CCommon::WriteLog(str_log, theApp.m_log_path.c_str());
62+
// write_log = true;
63+
//}
64+
}
65+
}
66+
return cpu_usage;
67+
}
68+
69+
int CCPUUsage::GetCpuUsageByGetSystemTimes()
70+
{
71+
int cpu_usage{};
72+
FILETIME idleTime;
73+
FILETIME kernelTime;
74+
FILETIME userTime;
75+
GetSystemTimes(&idleTime, &kernelTime, &userTime);
76+
77+
__int64 idle = CCommon::CompareFileTime2(m_preidleTime, idleTime);
78+
__int64 kernel = CCommon::CompareFileTime2(m_prekernelTime, kernelTime);
79+
__int64 user = CCommon::CompareFileTime2(m_preuserTime, userTime);
80+
81+
if (kernel + user == 0)
82+
{
83+
cpu_usage = 0;
84+
}
85+
else
86+
{
87+
//(总的时间-空闲时间)/总的时间=占用cpu的时间就是使用率
88+
cpu_usage = static_cast<int>(abs((kernel + user - idle) * 100 / (kernel + user)));
89+
}
90+
m_preidleTime = idleTime;
91+
m_prekernelTime = kernelTime;
92+
m_preuserTime = userTime;
93+
94+
return cpu_usage;
95+
}

0 commit comments

Comments
 (0)