windows 中准确的系统 cpu 使用情况

accurate system cpu usage in windows

我正在努力将 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;
}

工作代码:

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