ubuntu 下用QT获取CPU使用率

cpu使用信息在proc系统中的位置如下

/proc/stat

第一行的数值表示的是CPU总的使用情况,所以我们只要用第一行的数字计算就可以了。下表解析第一行各数值的含义:

参数          解析(单位:jiffies)

(jiffies是内核中的一个全局变量,用来记录自系统启动一来产生的节拍数,在linux中,一个节拍大致可理解为操作系统进程调度的最小时间片,不同linux内核可能值有不同,通常在1ms到10ms之间)

user ( 15579 )    从系统启动开始累计到当前时刻,处于用户态的运行时间,不包含 nice值为负进程。

nice (99)      从系统启动开始累计到当前时刻,nice值为负的进程所占用的CPU时间

system (13680)  从系统启动开始累计到当前时刻,处于核心态的运行时间

idle (698457)   从系统启动开始累计到当前时刻,除IO等待时间以外的其它等待时间

iowait (10939) 从系统启动开始累计到当前时刻,IO等待时间(since 2.5.41)

irq (40)           从系统启动开始累计到当前时刻,硬中断时间(since 2.6.0-test4)

softirq (651)      从系统启动开始累计到当前时刻,软中断时间(since 2.6.0-test4)

stealstolen(0)     which is the time spent in other operating systems when running in a virtualized environment(since 2.6.11)

guest(0)        which is the time spent running a virtual  CPU  for  guest operating systems under the control of the Linux kernel(since 2.6.24)

 结论:总的cpu时间totalCpuTime = user + nice + system + idle + iowait + irq + softirq + stealstolen +guest


总的Cpu使用率计算



计算方法:

1、  采样两个足够短的时间间隔的Cpu快照,分别记作t1,t2,其中t1、t2的结构均为:

(user、nice、system、idle、iowait、irq、softirq、stealstolen、guest)的9元组;

2、  计算总的Cpu时间片totalCpuTime

a)         把第一次的所有cpu使用情况求和,得到s1;

b)         把第二次的所有cpu使用情况求和,得到s2;

c)         s2 - s1得到这个时间间隔内的所有时间片,即totalCpuTime = j2 - j1 ;

3、计算空闲时间idle

idle对应第四列的数据,用第二次的idle - 第一次的idle即可

idle=第二次的idle - 第一次的idle

4、计算cpu使用率

pcpu =100* (total-idle)/total


5、同理可以用同样的方法求出其他进程和线程所占cpu资源

源码:

float MainWindow::CPU_usage(){
        FILE *fp;
            char buf[128];
            char cpu[5];
            static long int static_all=0;
            static long int static_idle=0;
            long int user,nice,sys,idle,iowait,irq,softirq;

            long int all1,idle1;
            float usage;

                    fp = fopen("/proc/stat","r");
                    if(fp == NULL)
                    {
                            perror("fopen:");
                            exit (0);
                    }


                    fgets(buf,sizeof(buf),fp);
    #if __DEBUG__
                    printf("buf=%s",buf);
    #endif
                    sscanf(buf,"%s%d%d%d%d%d%d%d",cpu,&user,&nice,&sys,&idle,&iowait,&irq,&softirq);
                    /*
    #if __DEBUG__
    printf("%s,%d,%d,%d,%d,%d,%d,%d\n",cpu,user,nice,sys,idle,iowait,irq,softirq);
    #endif
    */
                    all1 = user+nice+sys+idle+iowait+irq+softirq;
                    idle1 = idle;

                    usage = (float)(all1-static_all-(idle1-static_idle)) / (all1-static_all)*100 ;

                    static_all=all1;
                    static_idle=idle1;

                    fclose(fp);
                    qDebug()<<usage;
             return usage;
}

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值