Skip to content

Commit

Permalink
CPU利用率获取方式新增通过硬件监控模块获取,选项设置-常规设置-CPU利用率中增加相应的选项
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongyang219 committed Jan 16, 2025
1 parent e28f24c commit 4ee5fc3
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 12 deletions.
27 changes: 27 additions & 0 deletions OpenHardwareMonitorApi/OpenHardwareMonitorImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ namespace OpenHardwareMonitorApi
return m_cpu_freq;
}

float COpenHardwareMonitor::CpuUsage()
{
return m_cpu_usage;
}

const std::map<std::wstring, float>& COpenHardwareMonitor::AllHDDTemperature()
{
return m_all_hdd_temperature;
Expand Down Expand Up @@ -120,6 +125,7 @@ namespace OpenHardwareMonitorApi
{
MonitorGlobal::Instance()->computer->IsMotherboardEnabled = enable;
}

bool COpenHardwareMonitor::GetCPUFreq(IHardware^ hardware, float& freq) {
for (int i = 0; i < hardware->Sensors->Length; i++)
{
Expand All @@ -136,6 +142,24 @@ namespace OpenHardwareMonitorApi
freq = sum / m_all_cpu_clock.size() / 1000.0;
return true;
}

bool COpenHardwareMonitor::GetCpuUsage(IHardware^ hardware, float& cpu_usage)
{
for (int i = 0; i < hardware->Sensors->Length; i++)
{
if (hardware->Sensors[i]->SensorType == SensorType::Load)
{
String^ name = hardware->Sensors[i]->Name;
if (name != L"CPU Total")
{
cpu_usage = Convert::ToDouble(hardware->Sensors[i]->Value);
return true;
}
}
}
return false;
}

bool COpenHardwareMonitor::GetHardwareTemperature(IHardware^ hardware, float& temperature)
{
temperature = -1;
Expand Down Expand Up @@ -277,6 +301,7 @@ namespace OpenHardwareMonitorApi
m_all_hdd_temperature.clear();
m_all_hdd_usage.clear();
m_cpu_freq = -1;
m_cpu_usage = -1;
}

void COpenHardwareMonitor::InsertValueToMap(std::map<std::wstring, float>& value_map, const std::wstring& key, float value)
Expand Down Expand Up @@ -324,6 +349,8 @@ namespace OpenHardwareMonitorApi
GetCpuTemperature(computer->Hardware[i], m_cpu_temperature);
if (m_cpu_freq < 0)
GetCPUFreq(computer->Hardware[i], m_cpu_freq);
if (m_cpu_usage < 0)
GetCpuUsage(computer->Hardware[i], m_cpu_usage);
break;
case HardwareType::GpuNvidia:
if (m_gpu_nvidia_temperature < 0)
Expand Down
3 changes: 3 additions & 0 deletions OpenHardwareMonitorApi/OpenHardwareMonitorImp.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace OpenHardwareMonitorApi {
virtual float MainboardTemperature() override;
virtual float GpuUsage() override;
virtual float CpuFreq() override;
virtual float CpuUsage() override;
virtual const std::map<std::wstring, float>& AllHDDTemperature() override;
virtual const std::map<std::wstring, float>& AllCpuTemperature() override;
virtual const std::map<std::wstring, float>& AllHDDUsage() override;
Expand All @@ -39,6 +40,7 @@ namespace OpenHardwareMonitorApi {
bool GetGpuUsage(IHardware^ hardware, float& gpu_usage);
bool GetHddUsage(IHardware^ hardware, float& hdd_usage);
bool GetCPUFreq(IHardware^ hardware, float& freq);
bool GetCpuUsage(IHardware^ hardware, float& cpu_usage);
void ResetAllValues();
//向map中插入一个数值,如果key已经存在,则自动对新插入的key重命名
static void InsertValueToMap(std::map<std::wstring, float>& value_map, const std::wstring& key, float value);
Expand All @@ -54,6 +56,7 @@ namespace OpenHardwareMonitorApi {
float m_gpu_ati_usage{};
float m_gpu_intel_usage{};
float m_cpu_freq{};
float m_cpu_usage{};
std::map<std::wstring, float> m_all_hdd_temperature;
std::map<std::wstring, float> m_all_cpu_temperature;
std::map<std::wstring, float> m_all_cpu_clock;
Expand Down
10 changes: 9 additions & 1 deletion TrafficMonitor/CommonData.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,15 @@ struct GeneralSettingData
Language language;

bool show_all_interface{ true };
bool m_get_cpu_usage_by_cpu_times{ true }; //获取CPU利用率的方式,如果为true则是使用GetSystemTimes,否则使用Pdh(性能计数器)

//CPU利用率获取方式
enum CpuUsageAcquireMethod
{
CA_CPU_TIME, //使用时间
CA_PDH, //性能计数器
CA_HARDWARE_MONITOR //来自硬件监控
};
CpuUsageAcquireMethod cpu_usage_acquire_method{}; //获取CPU利用率的方式

bool portable_mode{ false }; //便携模式,如果为true,则程序所有数据都保存到exe所在目录下,否则保存到Appdata\Romaing目录下
int monitor_time_span{ 1000 }; //监控的时间间隔
Expand Down
23 changes: 17 additions & 6 deletions TrafficMonitor/GeneralSettingsDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ BEGIN_MESSAGE_MAP(CGeneralSettingsDlg, CTabDlg)
ON_BN_CLICKED(IDC_SHOW_NOTIFY_ICON_CHECK, &CGeneralSettingsDlg::OnBnClickedShowNotifyIconCheck)
ON_BN_CLICKED(IDC_SELECT_CONNECTIONS_BUTTON, &CGeneralSettingsDlg::OnBnClickedSelectConnectionsButton)
ON_BN_CLICKED(IDC_RESET_AUTO_RUN_BUTTON, &CGeneralSettingsDlg::OnBnClickedResetAutoRunButton)
ON_BN_CLICKED(IDC_USE_HARDWARE_MONITOR_RADIO, &CGeneralSettingsDlg::OnBnClickedUseHardwareMonitorRadio)
END_MESSAGE_MAP()


Expand Down Expand Up @@ -254,8 +255,14 @@ BOOL CGeneralSettingsDlg::OnInitDialog()
m_toolTip.AddTool(GetDlgItem(IDC_SAVE_TO_PROGRAM_DIR_RADIO), theApp.m_module_dir.c_str());
AddOrUpdateAutoRunTooltip(true);

((CButton*)GetDlgItem(IDC_USE_CPU_TIME_RADIO))->SetCheck(m_data.m_get_cpu_usage_by_cpu_times);
((CButton*)GetDlgItem(IDC_USE_PDH_RADIO))->SetCheck(!m_data.m_get_cpu_usage_by_cpu_times);
if (m_data.cpu_usage_acquire_method == GeneralSettingData::CA_CPU_TIME || !m_data.IsHardwareEnable(HI_CPU))
CheckDlgButton(IDC_USE_CPU_TIME_RADIO, TRUE);
else if (m_data.cpu_usage_acquire_method == GeneralSettingData::CA_PDH)
CheckDlgButton(IDC_USE_PDH_RADIO, TRUE);
else if (m_data.cpu_usage_acquire_method == GeneralSettingData::CA_HARDWARE_MONITOR)
CheckDlgButton(IDC_USE_HARDWARE_MONITOR_RADIO, TRUE);

EnableDlgCtrl(IDC_USE_HARDWARE_MONITOR_RADIO, m_data.IsHardwareEnable(HI_CPU));

m_monitor_span_edit.SetRange(MONITOR_TIME_SPAN_MIN, MONITOR_TIME_SPAN_MAX);
m_monitor_span_edit.SetValue(m_data.monitor_time_span);
Expand Down Expand Up @@ -452,15 +459,18 @@ BOOL CGeneralSettingsDlg::PreTranslateMessage(MSG* pMsg)

void CGeneralSettingsDlg::OnBnClickedUseCpuTimeRadio()
{
// TODO: 在此添加控件通知处理程序代码
m_data.m_get_cpu_usage_by_cpu_times = true;
m_data.cpu_usage_acquire_method = GeneralSettingData::CA_CPU_TIME;
}


void CGeneralSettingsDlg::OnBnClickedUsePdhRadio()
{
// TODO: 在此添加控件通知处理程序代码
m_data.m_get_cpu_usage_by_cpu_times = false;
m_data.cpu_usage_acquire_method = GeneralSettingData::CA_PDH;
}

void CGeneralSettingsDlg::OnBnClickedUseHardwareMonitorRadio()
{
m_data.cpu_usage_acquire_method = GeneralSettingData::CA_HARDWARE_MONITOR;
}

void CGeneralSettingsDlg::OnDeltaposSpin(NMHDR* pNMHDR, LRESULT* pResult)
Expand Down Expand Up @@ -608,6 +618,7 @@ void CGeneralSettingsDlg::OnBnClickedCpuCheck()
CheckDlgButton(IDC_CPU_CHECK, FALSE);
}
m_data.SetHardwareEnable(HI_CPU, checked);
EnableDlgCtrl(IDC_USE_HARDWARE_MONITOR_RADIO, checked);
}


Expand Down
1 change: 1 addition & 0 deletions TrafficMonitor/GeneralSettingsDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class CGeneralSettingsDlg : public CTabDlg
virtual BOOL PreTranslateMessage(MSG* pMsg);
afx_msg void OnBnClickedUseCpuTimeRadio();
afx_msg void OnBnClickedUsePdhRadio();
afx_msg void OnBnClickedUseHardwareMonitorRadio();
afx_msg void OnDeltaposSpin(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnEnKillfocusMonitorSpanEdit();
afx_msg void OnBnClickedCpuTempTipCheck();
Expand Down
10 changes: 8 additions & 2 deletions TrafficMonitor/TrafficMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ void CTrafficMonitorApp::LoadConfig()
is_chinese_language = (m_general_data.language == Language::SIMPLIFIED_CHINESE);
m_general_data.update_source = ini.GetInt(L"general", L"update_source", is_chinese_language ? 1 : 0); //如果当前语言为简体,则默认更新源为Gitee,否则为GitHub
//载入获取CPU利用率的方式,默认使用GetSystemTimes获取
m_general_data.m_get_cpu_usage_by_cpu_times = ini.GetBool(L"general", L"get_cpu_usage_by_cpu_times", /*m_win_version.GetMajorVersion() < 10*/ true);
m_general_data.cpu_usage_acquire_method = static_cast<GeneralSettingData::CpuUsageAcquireMethod>(ini.GetInt(L"general", L"cpu_usage_acquire_method", GeneralSettingData::CA_CPU_TIME));
//Lite版获取CPU利用率的方式不能为“通过硬件监控”
#ifdef WITHOUT_TEMPERATURE
if (m_general_data.cpu_usage_acquire_method == GeneralSettingData::CA_HARDWARE_MONITOR)
m_general_data.cpu_usage_acquire_method = GeneralSettingData::CA_CPU_TIME;
#endif

m_general_data.monitor_time_span = ini.GetInt(L"general", L"monitor_time_span", 1000);
if (m_general_data.monitor_time_span < MONITOR_TIME_SPAN_MIN || m_general_data.monitor_time_span > MONITOR_TIME_SPAN_MAX)
m_general_data.monitor_time_span = 1000;
Expand Down Expand Up @@ -316,7 +322,7 @@ void CTrafficMonitorApp::SaveConfig()
ini.WriteBool(_T("general"), _T("allow_skin_cover_text"), m_general_data.allow_skin_cover_text);
ini.WriteInt(_T("general"), _T("language"), static_cast<int>(m_general_data.language));
ini.WriteBool(L"general", L"show_all_interface", m_general_data.show_all_interface);
ini.WriteBool(L"general", L"get_cpu_usage_by_cpu_times", m_general_data.m_get_cpu_usage_by_cpu_times);
ini.WriteInt(L"general", L"cpu_usage_acquire_method", m_general_data.cpu_usage_acquire_method);
ini.WriteInt(L"general", L"monitor_time_span", m_general_data.monitor_time_span);
ini.WriteString(L"general", L"hard_disk_name", m_general_data.hard_disk_name);
ini.WriteString(L"general", L"cpu_core_name", m_general_data.cpu_core_name);
Expand Down
Binary file modified TrafficMonitor/TrafficMonitor.rc
Binary file not shown.
11 changes: 8 additions & 3 deletions TrafficMonitor/TrafficMonitorDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ void CTrafficMonitorDlg::ApplySettings(COptionsDlg& optionsDlg)
}

//设置获取CPU利用率的方式
m_cpu_usage.SetUseCPUTimes(theApp.m_general_data.m_get_cpu_usage_by_cpu_times);
m_cpu_usage.SetUseCPUTimes(theApp.m_general_data.cpu_usage_acquire_method != GeneralSettingData::CA_PDH);

#ifndef WITHOUT_TEMPERATURE
if (is_hardware_monitor_item_changed)
Expand Down Expand Up @@ -1123,7 +1123,7 @@ BOOL CTrafficMonitorDlg::OnInitDialog()
m_tool_tips.AddTool(this, _T(""));

//设置获取CPU利用率的方式
m_cpu_usage.SetUseCPUTimes(theApp.m_general_data.m_get_cpu_usage_by_cpu_times);
m_cpu_usage.SetUseCPUTimes(theApp.m_general_data.cpu_usage_acquire_method != GeneralSettingData::CA_PDH);

//如果程序启动时设置了隐藏主窗口,或窗口的位置在左上角,则先将其不透明度设为0
if (theApp.m_cfg_data.m_hide_main_window || (theApp.m_cfg_data.m_position_x == 0 && theApp.m_cfg_data.m_position_y == 0))
Expand Down Expand Up @@ -1334,7 +1334,10 @@ UINT CTrafficMonitorDlg::MonitorThreadCallback(LPVOID dwUser)
//if (!theApp.m_cfg_data.m_hide_main_window || theApp.m_cfg_data.m_show_task_bar_wnd)
//{
//获取CPU使用率
theApp.m_cpu_usage = pThis->m_cpu_usage.GetCPUUsage();
if (theApp.m_general_data.cpu_usage_acquire_method != GeneralSettingData::CA_HARDWARE_MONITOR || !theApp.m_general_data.IsHardwareEnable(HI_CPU))
{
theApp.m_cpu_usage = pThis->m_cpu_usage.GetCPUUsage();
}

//获取内存利用率
MEMORYSTATUSEX statex;
Expand Down Expand Up @@ -1374,6 +1377,8 @@ UINT CTrafficMonitorDlg::MonitorThreadCallback(LPVOID dwUser)
theApp.m_main_board_temperature = theApp.m_pMonitor->MainboardTemperature();
theApp.m_gpu_usage = theApp.m_pMonitor->GpuUsage();
theApp.m_cpu_freq = theApp.m_pMonitor->CpuFreq();
if (theApp.m_general_data.cpu_usage_acquire_method == GeneralSettingData::CA_HARDWARE_MONITOR && theApp.m_general_data.IsHardwareEnable(HI_CPU))
theApp.m_cpu_usage = theApp.m_pMonitor->CpuUsage();
//获取CPU温度
if (!theApp.m_pMonitor->AllCpuTemperature().empty())
{
Expand Down
1 change: 1 addition & 0 deletions TrafficMonitor/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@
#define IDC_USE_CPU_TIME_RADIO 1106
#define IDC_USE_PDH_RADIO 1107
#define IDC_BACKGROUND_TRANSPARENT_CHECK 1108
#define IDC_USE_HARDWARE_MONITOR_RADIO 1108
#define IDC_AUTO_ADAPT_LIGHT_THEME_CHECK 1109
#define IDC_AUTO_ADAPT_SETTINGS_BUTTON 1110
#define IDC_DARK_MODE_DEFAULT_STYLE_COMBO 1111
Expand Down
1 change: 1 addition & 0 deletions include/OpenHardwareMonitor/OpenHardwareMonitorApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace OpenHardwareMonitorApi
virtual float MainboardTemperature() = 0; //返回获取到的主板温度
virtual float GpuUsage() = 0; //返回获取到的GPU利用率
virtual float CpuFreq() = 0; //返回获取到的CPU频率
virtual float CpuUsage() = 0; //返回获取的CPU利用率
virtual const std::map<std::wstring, float>& AllHDDTemperature() = 0; //返回所有硬盘的温度。map的key是硬盘的名称,value是硬盘的温度
virtual const std::map<std::wstring, float>& AllCpuTemperature() = 0; //返回所有CPU(核心)的温度。map的key是CPU的名称,value是硬盘的温度
virtual const std::map<std::wstring, float>& AllHDDUsage() = 0; //返回所有硬盘的使用率
Expand Down

0 comments on commit 4ee5fc3

Please sign in to comment.