Skip to content

Commit 6f7ebcf

Browse files
committed
实现使用PDH获取硬盘利用率,Lite版增加显示硬盘利用率的功能
1 parent 1563301 commit 6f7ebcf

14 files changed

+176
-30
lines changed

TrafficMonitor/DisplayItem.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ const std::set<DisplayItem> AllDisplayItems
2424
{
2525
TDI_UP, TDI_DOWN, TDI_CPU, TDI_MEMORY, TDI_GPU_USAGE
2626
#ifndef WITHOUT_TEMPERATURE
27-
, 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
2828
#endif
29-
,TDI_CPU_FREQ, TDI_TOTAL_SPEED, TDI_TODAY_TRAFFIC
29+
, TDI_HDD_USAGE, TDI_CPU_FREQ, TDI_TOTAL_SPEED, TDI_TODAY_TRAFFIC
3030
};
3131

3232
//显示的项目

TrafficMonitor/GeneralSettingsDlg.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "stdafx.h"
55
#include "TrafficMonitor.h"
6+
#include "TrafficMonitorDlg.h"
67
#include "GeneralSettingsDlg.h"
78
#include "PluginManagerDlg.h"
89
#include "SelectConnectionsDlg.h"
@@ -38,7 +39,6 @@ void CGeneralSettingsDlg::CheckTaskbarDisplayItem()
3839
if (!theApp.m_general_data.IsHardwareEnable(HI_HDD))
3940
{
4041
theApp.m_taskbar_data.display_item.Remove(TDI_HDD_TEMP);
41-
theApp.m_taskbar_data.display_item.Remove(TDI_HDD_USAGE);
4242
}
4343
if (!theApp.m_general_data.IsHardwareEnable(HI_MBD))
4444
theApp.m_taskbar_data.display_item.Remove(TDI_MAIN_BOARD_TEMP);
@@ -199,7 +199,7 @@ void CGeneralSettingsDlg::SetControlEnable()
199199
m_hdd_temp_tip_edit.EnableWindow(m_data.hdd_temp_tip.enable);
200200
m_mbd_temp_tip_edit.EnableWindow(m_data.mainboard_temp_tip.enable);
201201

202-
m_hard_disk_combo.EnableWindow(m_data.IsHardwareEnable(HI_HDD));
202+
//m_hard_disk_combo.EnableWindow(m_data.IsHardwareEnable(HI_HDD));
203203
m_select_cpu_combo.EnableWindow(m_data.IsHardwareEnable(HI_CPU));
204204

205205
EnableDlgCtrl(IDC_SELECT_CONNECTIONS_BUTTON, !m_data.show_all_interface);
@@ -358,6 +358,14 @@ BOOL CGeneralSettingsDlg::OnInitDialog()
358358
m_monitor_time_span_ori = m_data.monitor_time_span;
359359
m_update_source_ori = m_data.update_source;
360360

361+
if (CTrafficMonitorDlg::Instance()->IsGetDiskUsageByPdh())
362+
{
363+
const auto& disk_names = CTrafficMonitorDlg::Instance()->GetPdhDiskUsageHelper().GetDiskNames();
364+
for (const auto& hdd_name : disk_names)
365+
m_hard_disk_combo.AddString(hdd_name);
366+
int cur_index = m_hard_disk_combo.FindString(-1, m_data.hard_disk_name.c_str());
367+
m_hard_disk_combo.SetCurSel(cur_index);
368+
}
361369
#ifndef WITHOUT_TEMPERATURE
362370
//初始化硬件监控Check box
363371
CheckDlgButton(IDC_CPU_CHECK, m_data.IsHardwareEnable(HI_CPU));
@@ -369,10 +377,13 @@ BOOL CGeneralSettingsDlg::OnInitDialog()
369377
{
370378
CSingleLock sync(&theApp.m_minitor_lib_critical, TRUE);
371379
//初始化选择硬盘下拉列表
372-
for (const auto& hdd_item : theApp.m_pMonitor->AllHDDTemperature())
373-
m_hard_disk_combo.AddString(hdd_item.first.c_str());
374-
int cur_index = m_hard_disk_combo.FindString(-1, m_data.hard_disk_name.c_str());
375-
m_hard_disk_combo.SetCurSel(cur_index);
380+
if (!CTrafficMonitorDlg::Instance()->IsGetDiskUsageByPdh())
381+
{
382+
for (const auto& hdd_item : theApp.m_pMonitor->AllHDDTemperature())
383+
m_hard_disk_combo.AddString(hdd_item.first.c_str());
384+
int cur_index = m_hard_disk_combo.FindString(-1, m_data.hard_disk_name.c_str());
385+
m_hard_disk_combo.SetCurSel(cur_index);
386+
}
376387
//初始化选择CPU下拉列表
377388
m_select_cpu_combo.AddString(CCommon::LoadText(IDS_AVREAGE_TEMPERATURE));
378389
for (const auto& cpu_item : theApp.m_pMonitor->AllCpuTemperature())
@@ -398,13 +409,13 @@ BOOL CGeneralSettingsDlg::OnInitDialog()
398409
EnableDlgCtrl(IDC_GPU_CHECK, false);
399410
EnableDlgCtrl(IDC_HDD_CHECK, false);
400411
EnableDlgCtrl(IDC_MBD_CHECK, false);
401-
EnableDlgCtrl(IDC_SELECT_HARD_DISK_COMBO, false);
412+
//EnableDlgCtrl(IDC_SELECT_HARD_DISK_COMBO, false);
402413
EnableDlgCtrl(IDC_SELECT_CPU_COMBO, false);
403414
EnableDlgCtrl(IDC_CPU_TEMP_STATIC, false);
404415
EnableDlgCtrl(IDC_GPU_TEMP_STATIC, false);
405416
EnableDlgCtrl(IDC_HDD_STATIC, false);
406417
EnableDlgCtrl(IDC_MBD_TEMP_STATIC, false);
407-
EnableDlgCtrl(IDC_SELECT_HDD_STATIC, false);
418+
//EnableDlgCtrl(IDC_SELECT_HDD_STATIC, false);
408419
EnableDlgCtrl(IDC_SELECT_CPU_STATIC, false);
409420
EnableDlgCtrl(IDC_HARDWARE_MONITOR_STATIC, false);
410421
#endif
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "stdafx.h"
2+
#include "DiskUsage.h"
3+
#include <algorithm>
4+
#include <cmath>
5+
6+
CPdhDiskUsage::CPdhDiskUsage()
7+
: CPdhQuery(_T("\\PhysicalDisk(*)\\% Idle Time"))
8+
{
9+
m_isAvailable = Initialize();
10+
if (m_isAvailable)
11+
{
12+
// 预热:触发PDH内部初始化(首次QueryValues会填充实例列表)
13+
std::vector<CounterValueItem> dummy;
14+
QueryValues(dummy);
15+
ExtractDiskNames();
16+
}
17+
}
18+
19+
CPdhDiskUsage::~CPdhDiskUsage()
20+
{
21+
}
22+
23+
void CPdhDiskUsage::ExtractDiskNames()
24+
{
25+
m_diskNames.clear();
26+
std::vector<CounterValueItem> values;
27+
28+
if (!QueryValues(values))
29+
return;
30+
31+
for (const auto& item : values)
32+
{
33+
CString name(item.name.c_str());
34+
m_diskNames.push_back(name);
35+
}
36+
}
37+
38+
int CPdhDiskUsage::CalculateUtilization(double idleTime) const
39+
{
40+
// 关键处理:NVMe/RAID等多队列磁盘的空闲时间可能 >100%
41+
// 例如: 4队列磁盘空闲时间=400% → 实际空闲=100% → 利用率=0%
42+
if (idleTime > 100.0)
43+
idleTime = 100.0;
44+
45+
// 确保范围在0-100
46+
idleTime = (std::max)(0.0, (std::min)(100.0, idleTime));
47+
48+
// 利用率 = 100% - 空闲时间
49+
double utilization = 100.0 - idleTime;
50+
return static_cast<int>(utilization + 0.5); // 四舍五入
51+
}
52+
53+
bool CPdhDiskUsage::GetDiskUsage(int diskIndex, int& usage)
54+
{
55+
usage = 0;
56+
if (!m_isAvailable)
57+
return false;
58+
59+
std::vector<CounterValueItem> values;
60+
if (!QueryValues(values) || values.empty())
61+
return false;
62+
63+
if (diskIndex >= 0 && diskIndex < static_cast<int>(values.size()))
64+
{
65+
usage = CalculateUtilization(values[diskIndex].value);
66+
return true;
67+
}
68+
return false;
69+
70+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
#include "PdhQuery.h"
3+
4+
class CPdhDiskUsage : public CPdhQuery
5+
{
6+
public:
7+
CPdhDiskUsage();
8+
~CPdhDiskUsage();
9+
10+
// 获取指定物理磁盘的利用率(0-100)
11+
// diskIndex: 0=第一个物理磁盘, 1=第二个...
12+
// 返回值: true=成功, false=失败(首次调用通常失败,需间隔200ms+重试)
13+
bool GetDiskUsage(int diskIndex, /*out*/ int& usage);
14+
15+
// 获取物理磁盘名称列表
16+
const std::vector<CString>& GetDiskNames() const { return m_diskNames; }
17+
18+
// 检查磁盘监控是否可用
19+
bool IsAvailable() const { return m_isAvailable; }
20+
21+
private:
22+
// 从计数器实例提取磁盘名称
23+
void ExtractDiskNames();
24+
25+
// 处理空闲时间
26+
int CalculateUtilization(double idleTime) const;
27+
28+
private:
29+
bool m_isAvailable{ false };
30+
std::vector<CString> m_diskNames; // 磁盘名称列表,按PDH返回顺序
31+
};

TrafficMonitor/PdhHardwareQuery/GpuUsage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ bool CPdhGPUUsage::GetGpuUsage(int& usage)
1717
{
1818
if (isInitialized)
1919
{
20-
std::vector<ValueItem> valueItems;
20+
std::vector<CounterValueItem> valueItems;
2121
if (QueryValues(valueItems))
2222
{
2323
if (!valueItems.empty())

TrafficMonitor/PdhHardwareQuery/PdhQuery.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ bool CPdhQuery::QueryValue(double& value)
6262
return true;
6363
}
6464

65-
bool CPdhQuery::QueryValues(std::vector<ValueItem>& values)
65+
bool CPdhQuery::QueryValues(std::vector<CounterValueItem>& values)
6666
{
67+
values.clear();
6768
if (!isInitialized)
6869
return false;
6970

@@ -84,7 +85,7 @@ bool CPdhQuery::QueryValues(std::vector<ValueItem>& values)
8485
// Loop through the array and print the instance name and counter value.
8586
for (DWORD i = 0; i < dwItemCount; i++)
8687
{
87-
ValueItem value_item;
88+
CounterValueItem value_item;
8889
value_item.name = pItems[i].szName;
8990
value_item.value = pItems[i].FmtValue.doubleValue;
9091
values.push_back(value_item);

TrafficMonitor/PdhHardwareQuery/PdhQuery.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ class CPdhQuery
1111
protected:
1212
bool Initialize();
1313
bool QueryValue(double& value);
14-
struct ValueItem
14+
struct CounterValueItem
1515
{
1616
std::wstring name;
1717
double value{};
1818
};
19-
bool QueryValues(std::vector<ValueItem>& values);
19+
bool QueryValues(std::vector<CounterValueItem>& values);
2020

2121
protected:
2222
HQUERY query = nullptr;

TrafficMonitor/TaskBarDlg.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -778,13 +778,13 @@ CString CTaskBarDlg::GetMouseTipsInfo()
778778
temp.Format(_T("\r\n%s: %s"), CCommon::LoadText(IDS_MAINBOARD_TEMPERATURE), CCommon::TemperatureToString(theApp.m_main_board_temperature, theApp.m_taskbar_data));
779779
tip_info += temp;
780780
}
781-
if (!IsItemShow(TDI_HDD_USAGE) && theApp.m_hdd_usage >= 0)
782-
{
783-
temp.Format(_T("\r\n%s: %d %%"), CCommon::LoadText(IDS_HDD_USAGE), theApp.m_hdd_usage);
784-
tip_info += temp;
785-
}
786781
}
787782
#endif
783+
if (!IsItemShow(TDI_HDD_USAGE) && theApp.m_hdd_usage >= 0)
784+
{
785+
temp.Format(_T("\r\n%s: %d %%"), CCommon::LoadText(IDS_HDD_USAGE), theApp.m_hdd_usage);
786+
tip_info += temp;
787+
}
788788

789789
//添加插件项目的鼠标提示
790790
tip_info += theApp.GetPlauginTooltipInfo().c_str();

TrafficMonitor/TaskbarItemOrderHelper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ bool CTaskbarItemOrderHelper::IsItemDisplayed(CommonDisplayItem item)
9292
displayed = false;
9393
if ((item == TDI_GPU_TEMP) && !theApp.m_general_data.IsHardwareEnable(HI_GPU))
9494
displayed = false;
95-
if ((item == TDI_HDD_TEMP || item == TDI_HDD_USAGE) && !theApp.m_general_data.IsHardwareEnable(HI_HDD))
95+
if ((item == TDI_HDD_TEMP) && !theApp.m_general_data.IsHardwareEnable(HI_HDD))
9696
displayed = false;
9797
if (item == TDI_MAIN_BOARD_TEMP && !theApp.m_general_data.IsHardwareEnable(HI_MBD))
9898
displayed = false;

TrafficMonitor/TrafficMonitor.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ void CTrafficMonitorApp::LoadConfig()
188188
m_taskbar_data.display_item.Remove(TDI_GPU_TEMP);
189189
m_taskbar_data.display_item.Remove(TDI_HDD_TEMP);
190190
m_taskbar_data.display_item.Remove(TDI_MAIN_BOARD_TEMP);
191-
m_taskbar_data.display_item.Remove(TDI_HDD_USAGE);
192191
#endif
193192

194193
//如果选项设置中关闭了某个硬件监控,则不显示对应的温度监控相关项目
@@ -201,7 +200,6 @@ void CTrafficMonitorApp::LoadConfig()
201200
if (!m_general_data.IsHardwareEnable(HI_HDD))
202201
{
203202
m_taskbar_data.display_item.Remove(TDI_HDD_TEMP);
204-
m_taskbar_data.display_item.Remove(TDI_HDD_USAGE);
205203
}
206204
if (!m_general_data.IsHardwareEnable(HI_MBD))
207205
m_taskbar_data.display_item.Remove(TDI_MAIN_BOARD_TEMP);

0 commit comments

Comments
 (0)