【问题标题】:accurate system cpu usage in windowswindows中准确的系统cpu使用率
【发布时间】:2018-11-14 18:42:15
【问题描述】:

我正在努力将 cpu 使用情况监视器添加到我的进程监视器中,我选择使用 NtQuerySystemInformation,因为它是我可以用来进行更准确计算的最低 api 下面的代码在 cpu 使用率较高但 cpu 几乎空闲(10 : 30)时运行良好,它不会像在任务管理器中那样显示使用百分比 它不会显示低于 26 % 的使用率,但任务管理器会显示 15 % 或接近百分比

这是我的代码:

double accurate_usage() {

  SYSTEM_INFO info = { 0 };
  GetSystemInfo(&info);
  DWORD proc_num = info.dwNumberOfProcessors;
  PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION old_values = new SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[proc_num];
  PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION new_values = new SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[proc_num];
  FILETIME old_time = { 0 }, new_time = { 0 };
  ULARGE_INTEGER uold_time = { 0 }, unew_time = { 0 };

  ULONG size;
  fNtQuerySystemInformation(SystemProcessorPerformanceInformation, old_values, sizeof(old_values[0]) * proc_num, &size);
  GetSystemTimeAsFileTime(&old_time);
  memcpy(&uold_time, &old_time, sizeof(FILETIME));
  Sleep(1000);
  fNtQuerySystemInformation(SystemProcessorPerformanceInformation, new_values, sizeof(old_values[0]) * proc_num, &size);
  GetSystemTimeAsFileTime(&new_time);
  memcpy(&unew_time, &new_time, sizeof(FILETIME));

  double percent = 0.0;
  for (DWORD i = 0; i < proc_num; ++i) {
      double current_percent = (new_values[i].KernelTime.QuadPart - old_values[i].KernelTime.QuadPart) +
      (new_values[i].UserTime.QuadPart - old_values[i].UserTime.QuadPart) - 
      (new_values[i].IdleTime.QuadPart - old_values[i].IdleTime.QuadPart);
      current_percent /= (unew_time.QuadPart - uold_time.QuadPart);
      current_percent /= proc_num;
      current_percent *= 100;
      percent += current_percent;
  }
  return percent;
}

【问题讨论】:

  • 这可能与逻辑处理器与内核数量有关。在我的 2 核 4 线程核心 i3 上运行您的代码,我得到了合理的结果。
  • 但任务管理器在我的 cpu 上显示它不同,我读它使用相同的功能
  • 当然,我的评论仅供参考。
  • 问题是计算公式,它是错误的,我在某个网站上找到了正确的。我将粘贴工作代码
  • 好的,听起来不错。

标签: c++ windows cpu cpu-usage


【解决方案1】:

工作代码:

double accurate_usage() {

  SYSTEM_INFO info = { 0 };
  GetSystemInfo(&info);
  DWORD proc_num = info.dwNumberOfProcessors;
  PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION old_values = new SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[proc_num];
  PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION new_values = new SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[proc_num];
  FILETIME old_time = { 0 }, new_time = { 0 };
  ULARGE_INTEGER uold_time = { 0 }, unew_time = { 0 };

  ULONG size;
  fNtQuerySystemInformation(SystemProcessorPerformanceInformation, old_values, sizeof(old_values[0]) * proc_num, &size);
  GetSystemTimeAsFileTime(&old_time);
  memcpy(&uold_time, &old_time, sizeof(FILETIME));
  Sleep(1000);
  fNtQuerySystemInformation(SystemProcessorPerformanceInformation, new_values, sizeof(old_values[0]) * proc_num, &size);
  GetSystemTimeAsFileTime(&new_time);
  memcpy(&unew_time, &new_time, sizeof(FILETIME));

  double percent = 0.0;
  for (DWORD i = 0; i < proc_num; ++i) {
      double current_percent = (new_values[i].IdleTime.QuadPart - old_values[i].IdleTime.QuadPart) * 100 ;
      current_percent /= ((new_values[i].KernelTime.QuadPart + new_values[i].UserTime.QuadPart) - (old_values[i].KernelTime.QuadPart + old_values[i].UserTime.QuadPart));
      current_percent = 100 - current_percent;
      percent += current_percent;
  }
  return percent / 10;
}

【讨论】:

    猜你喜欢
    • 2017-06-11
    • 1970-01-01
    • 2017-09-07
    • 2011-08-22
    • 2018-01-04
    • 2012-10-28
    • 1970-01-01
    • 2012-06-15
    • 2013-07-12
    相关资源
    最近更新 更多