c++获取windows、mac的cpu利用率

前段时间有一个工作任务是找windows和mac的cpu利用率的方法,并集成到客户端里,最后问题解决,但也还颇费一番功夫。现在在这里纪录一下。

 

windows平台cpu利用率

  windows平台获取cpu利用率的方法我找到的有以下两种:

方法一:查询windows的性能检测器
方法二: 调windows的api获取cpu使用时间并计算
#include 
#include 
#include 
#include 
#pragma comment(lib,"pdh.lib")

using namespace std;

//方法一
void getCpuUsePercentage1()
{
    PDH_HQUERY query;
    PDH_HCOUNTER counter;
    PdhOpenQuery(NULL, NULL, &query);
    PdhAddCounter(query, TEXT("\\Processor(_Total)\\% Processor Time"), NULL, &counter);
    PdhCollectQueryData(query);
    Sleep(1000);
    PdhCollectQueryData(query);
    PDH_FMT_COUNTERVALUE pdhValue;
    DWORD dwValue;
    PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE, &dwValue, &pdhValue);
    cout << float(pdhValue.doubleValue) << endl;
    PdhCloseQuery(query);
}

//方法二
__int64 Filetime2Int64(const FILETIME* ftime)
{
    LARGE_INTEGER li;
    li.LowPart = ftime->dwLowDateTime;
    li.HighPart = ftime->dwHighDateTime;
    return li.QuadPart;
}

__int64 CompareFileTime2(FILETIME preTime,FILETIME nowTime)
{
    return Filetime2Int64(&nowTime) - Filetime2Int64(&preTime);
}

void getCpuUsePercentage2()
{
    FILETIME preIdleTime;
    FILETIME preKernelTime;
    FILETIME preUserTime;
    GetSystemTimes(&preIdleTime, &preKernelTime, &preUserTime);

    Sleep(1000);

    FILETIME idleTime;
    FILETIME kernelTime;
    FILETIME userTime;
    GetSystemTimes(&idleTime, &kernelTime, &userTime);

    int idle = CompareFileTime2(preIdleTime, idleTime);
    int kernel = CompareFileTime2(preKernelTime, kernelTime);
    int user = CompareFileTime2(preUserTime, userTime);

    if (kernel + user == 0)
        return;
    //(总的时间-空闲时间)/总的时间=占用cpu的时间就是使用率
    cout << float(100.0*(kernel + user - idle)/(kernel + user)) << endl;
}

int main()
{
    while (1)
    {
        getCpuUsePercentage1();
        getCpuUsePercentage2();
    }
    return 0;
}

mac平台cpu利用率

注:方法在公司时用mac电脑跑过,在家总结的时候没有mac电脑,所以不确定以下代码会不会有编译错误。

#include 

using namespace std;

#include 
#include 
#include 

#define CP_USER 0
#define CP_SYS  1
#define CP_IDLE 2
#define CP_NICE 3
#define CP_STATES 4

enum BYTE_UNITS
{
  BYTES = 0,
  KILOBYTES = 1,
  MEGABYTES = 2,
  GIGABYTES = 3
};

template <class T>
T convert_unit( T num, int to, int from = BYTES)
{
  for( ; from < to; from++)
  {
    num /= 1024;
  }
  return num;
}

host_cpu_load_info_data_t _get_cpu_percentage()
{
  kern_return_t              error;
  mach_msg_type_number_t     count;
  host_cpu_load_info_data_t  r_load;
  mach_port_t                mach_port;

  count = HOST_CPU_LOAD_INFO_COUNT;
  mach_port = mach_host_self();
  error = host_statistics(mach_port, HOST_CPU_LOAD_INFO,
      ( host_info_t )&r_load, &count );

  if ( error != KERN_SUCCESS )
  {
    return host_cpu_load_info_data_t();
  }

  return r_load;
}

void getCpuUsePercentage()
{
    host_cpu_load_info_data_t load1 = _get_cpu_percentage();

    sleep(1);

    host_cpu_load_info_data_t load2 = _get_cpu_percentage();

    // pre load times
    unsigned long long current_user = load1.cpu_ticks[CP_USER];
    unsigned long long current_system = load1.cpu_ticks[CP_SYS];
    unsigned long long current_nice = load1.cpu_ticks[CP_NICE];
    unsigned long long current_idle = load1.cpu_ticks[CP_IDLE];

    // Current load times
    unsigned long long next_user = load2.cpu_ticks[CP_USER];
    unsigned long long next_system = load2.cpu_ticks[CP_SYS];
    unsigned long long next_nice = load2.cpu_ticks[CP_NICE];
    unsigned long long next_idle = load2.cpu_ticks[CP_IDLE];

    // Difference between the two
    unsigned long long diff_user = next_user - current_user;
    unsigned long long diff_system = next_system - current_system;
    unsigned long long diff_nice = next_nice - current_nice;
    unsigned long long diff_idle = next_idle - current_idle;

    float value = static_cast<float>( diff_user + diff_system + diff_nice ) /
      static_cast<float>( diff_user + diff_system + diff_nice + diff_idle ) *
      100.0;

    cout << value << endl;
}

int main()
{
    while (1)
    {
        getCpuUsePercentage();
    }
    return 0;
}

 

你可能感兴趣的:(c/c++)