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