c++之cpu使用率

如何获取cpu使用率呢?这用到了系统api,也就是几个系统函数来获取系统信息.
首先介绍下GetSystemTimes()这个函数:
BOOL WINAPI GetSystemTimes(
  _Out_opt_ LPFILETIME lpIdleTime,
  _Out_opt_ LPFILETIME lpKernelTime,
  _Out_opt_ LPFILETIME lpUserTime
);

Parameters

lpIdleTime  [out, optional]

A pointer to a FILETIME structure that receives the amount of time that the system has been idle.

lpKernelTime  [out, optional]

A pointer to a FILETIME structure that receives the amount of time that the system has spent executing in Kernel mode (including all threads in all processes, on all processors). This time value also includes the amount of time the system has been idle.

lpUserTime  [out, optional]

A pointer to a FILETIME structure that receives the amount of time that the system has spent executing in User mode (including all threads in all processes, on all processors).


这个函数可以获取cpu内核时间,用户时间和空闲时间.把这些时间信息储存在FILETIME中,FILETIME是一个结构体,来看看他的声明:
typedefstruct _FILETIME {
  DWORD dwLowDateTime;
  DWORD dwHighDateTime;
} FILETIME, *PFILETIME;

Members

dwLowDateTime

The low-order part of the file time.

dwHighDateTime

The high-order part of the file time


结构体有两个成员,分别储存着时间的高位信息和时间的低位信息.
这里面储存的是自开机以来的到现在的时间,是一个无符号长整形数字,DWORD只是把unsigned long改了下而已,因此DWORD占四个字节.
FILETIME分成了两部分储存,因此dwLowDAteTime储存的是时间的低位信息,dwHighTime储存的是时间的高位信息.

现在继续之前的话题,GetSystemTimes分别把空闲时间,内核时间和用户时间储存在lpIdleTime,lpKernelTime和lpIpUserTime中,注意,这是三个指向FILETIME的指针.因此我们可以得出一个计算cpu使用率的公式:
CPU使用率 = (内核时间 + 用户时间 - 空闲时间)/(内核时间 + 用户时间)

理解这个公式的重要因素是我们必须知道,我们通过获得的FILETIME结构体只是从开机到目前的cpu使用进度.我们要有两组FILETIME数据相减才能得到这段时间那之内cpu被内核代码,用户代码和空闲的时间.

好了,有了这些知识就可以获得cpu使用率了,看如下的一堆完整的代码:

#include <iostream>
#include <windows.h>        //使用系统函数所要包含的头文件
#include <iomanip>

using std::cout;
using std::endl;

class Cpu            //定义cpu类
{
    public:
    Cpu()
    {
        GetSystemTimes( &preidleTime, &prekernelTime, &preuserTime );    //首先得到目前的cpu使用进度
        Sleep(1000);            //睡眠一段时间来获得
        while(1)
        {
            GetSystemTimes( &idleTime, &kernelTime, &userTime );        //得到另一组数据

            __int64 idle = CompareFileTime( preidleTime,idleTime);        //通过自定义的函数来比较这段时间内cpu空闲的时间
            __int64 kernel = CompareFileTime( prekernelTime, kernelTime);    //通过自定义的函数来比较这段时间内cpu内核使用的时间
            __int64 user = CompareFileTime(preuserTime, userTime);        //通过自定义的函数来比较这段时间内cpu用户代码使用的时间

            long double cpu = (kernel +user - idle) *100.0/(kernel+user);    //计算cpu使用率

            preidleTime = idleTime;                //更新原有数据
            prekernelTime = kernelTime;
            preuserTime = userTime ;

            cout<<"CPU:"<<std::setprecision(2)<<cpu<<"%"<<endl;    
            Sleep(1000);        //睡眠一下
            //system("cls");     //因为是控制台,所以想要好看点,可以清空缓存区
        }
    }
    __int64 CompareFileTime(FILETIME time1,FILETIME time2)    //得到两个结构体中的时间只差
    {
        __int64 a = time1.dwHighDateTime << 32 | time1.dwLowDateTime ;    //<<32这步是为了把这高位时间移动到高位去,因为原有的数据是分两步份储存的
        __int64 b = time2.dwHighDateTime << 32 | time2.dwLowDateTime ;

        return   (b - a);       //返回时间之差
    }
    private:
    FILETIME preidleTime;                //储存以前的时间信息
    FILETIME prekernelTime;
    FILETIME preuserTime;

    FILETIME idleTime;                //储存现在的时间信息
    FILETIME kernelTime;
    FILETIME userTime;
};
int main()
{
    Cpu cpu;            //创建cpu对象来执行
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值