linux下用/proc/stat文件来计算cpu的利用率-c语言实现
proc文件系统介绍
/proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间。它以文件系统的方式为内核与进程提供通信的接口。用户和应用程序可以通过/proc得到系统的信息,并可以改变内核的某些参数。由于系统的信息,如进程,是动态改变的,所以用户或应用程序读取/proc目录中的文件时,proc文件系统是动态从系统内核读出所需信息并提交的。
/proc目录中有一些以数字命名的目录,它们是进程目录。系统中当前运行的每一个进程在/proc下都对应一个以进程号为目录名的目录/proc/pid,它们是读取进程信息的接口。此外,在Linux2.6.0-test6以上的版本中/proc/pid目录中有一个task目录,/proc/pid/task目录中也有一些以该进程所拥有的线程的线程号命名的目录/proc/pid/task/tid,它们是读取线程信息的接口。
/proc/stat文件
- [root@root c_study]# cat /proc/stat
- cpu 15579 99 13680 698457 10939 40 651 0 0
- cpu0 1669 7 1974 338065 1396 5 9 0 0
- cpu1 13910 91 11705 360391 9542 35 641 0 0
- intr 957831 163 8 0 1 1 0 5 0 1 0 0 0 101 0 0 3582 0 37804 3657 22410 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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1363495431
- processes 40101
- procs_running 1
- procs_blocked 0
- softirq 1396087 0 693403 12972 15932 35928 3 44577 479 592793
- [root@root c_study]#
第一行的数值表示的是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
/proc/<pid>/stat文件
该文件包含了某一进程所有的活动的信息,该文件中的所有值都是从系统启动开始累计
到当前时刻。以下通过实例数据来说明该文件中各字段的含义。
[zhengangen@buick ~]# cat /proc/6873/stat
6873 (a.out) R 6723 6873 6723 34819 6873 8388608 77 0 0 0 41958 31 0 0 25 0 3 0 5882654 1409024 56 4294967295 134512640 134513720 3215579040 0 2097798 0 0 0 0 0 0 0 17 0 0 0
说明:以下只解释对我们计算Cpu使用率有用相关参数
参数 解释
pid=6873 进程号
utime=1587 该任务在用户态运行的时间,单位为jiffies
stime=41958 该任务在核心态运行的时间,单位为jiffies
cutime=0 所有已死线程在用户态运行的时间,单位为jiffies
cstime=0 所有已死在核心态运行的时间,单位为jiffies
结论:进程的总Cpu时间processCpuTime = utime + stime + cutime + cstime,该值包括其所有线程的cpu时间。
/proc/<pid>/task/<tid>/stat文件
该文件包含了某一进程所有的活动的信息,该文件中的所有值都是从系统启动开始累计到当前时刻。该文件的内容格式以及各字段的含义同/proc/<pid>/stat文件。
注意,该文件中的tid字段表示的不再是进程号,而是linux中的轻量级进程(lwp),即我们通常所说的线程。
结论:线程Cpu时间threadCpuTime = utime + stime
总的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资源
源码
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #define __DEBUG__ 1
- #define CK_TIME 1
- int main(int argc ,char *argv[])
- {
- FILE *fp;
- char buf[128];
- char cpu[5];
- long int user,nice,sys,idle,iowait,irq,softirq;
- long int all1,all2,idle1,idle2;
- float usage;
- while(1)
- {
- 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;
- rewind(fp);
- /*第二次取数据*/
- sleep(CK_TIME);
- memset(buf,0,sizeof(buf));
- cpu[0] = '\0';
- user=nice=sys=idle=iowait=irq=softirq=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
- */
- all2 = user+nice+sys+idle+iowait+irq+softirq;
- idle2 = idle;
- usage = (float)(all2-all1-(idle2-idle1)) / (all2-all1)*100 ;
- printf("all=%d\n",all2-all1);
- printf("ilde=%d\n",all2-all1-(idle2-idle1));
- printf("cpu use = %.2f\%\n",usage);
- printf("=======================\n");
- fclose(fp);
- }
- return 1;
- }
gcc编译
- gcc -o cpu_use -g cpu_use.c
运行
- buf=cpu 15824 100 13772 879622 11014 40 720 0 0
- buf=cpu 15837 100 13790 879731 11014 40 720 0 0
- all=140
- ilde=31
- cpu use = 22.14%
- =======================
- buf=cpu 15837 100 13790 879731 11014 40 720 0 0
- buf=cpu 15857 100 13824 879786 11014 40 721 0 0
- all=110
- ilde=55
- cpu use = 50.00%
- =======================
- buf=cpu 15857 100 13824 879786 11014 40 721 0 0
- buf=cpu 15877 100 13856 879842 11014 41 721 0 0
- all=109
- ilde=53
- cpu use = 48.62%
- =======================
- buf=cpu 15877 100 13857 879842 11014 41 721 0 0
- buf=cpu 15897 100 13889 879901 11014 41 722 0 0
- all=112
- ilde=53
- cpu use = 47.32%
- =======================
转自:https://blog.csdn.net/stormbjm/article/details/19202849
linux下用/proc/stat文件来计算cpu的利用率-c语言实现的更多相关文章
- linux下生成core dump文件方法及设置
linux下生成core dump文件方法及设置 from:http://www.cppblog.com/kongque/archive/2011/03/07/141262.html core ...
- linux下为目录和文件设置权限
摘:linux下为目录和文件设置权限 分类: Linux2012-05-09 03:18 7456人阅读 评论(1) 收藏 举报 linuxwordpressweb数据库serverfile linu ...
- linux下恢复误删除的文件方法(ext2及ext3)
linux下恢复误删除的文件方法(ext2及ext3) 2009-12-19 15:23:47 分类: LINUX 如果是ext2文件系统的,直接用debugfs是可以恢复出来的,但对于ext3,d ...
- linux下分卷tar.bz文件的合并并解压缩
linux下分卷tar.bz文件的合并并解压缩 例: linux.tar.bz2.001;linux.tar.bz2.002;linux.tar.bz2.003; 首先 cat linux.tar.b ...
- linux下php上传文件注意
linux下php上传文件注意1.修改上传目录权限linux 修改某目录下所有所有子目录权限chmod -R 777 html修改某目录为任何用户都用写读执行权限chmod a+rwx html2.设 ...
- Linux下遍历目录及文件,更改权限
Linux下遍历目录及文件,更改权限 引言: 我在Linux下搭建android时,将eclipse及sdk复制到/usr/下时,总会出现无法读,无法写写样的问题. 解决方案: 有两个方案: 一.将复 ...
- 恢复Linux下被误删除的文件(笔记)
恢复Linux下被误删除的文件 [root@xuegod63 ~]# mount /dev/cdrom /mnt/ 分一个区:sda4 查找:extundelete 分一个区:sda4 [root ...
- linux下java调用.so文件的方法1: JNI
摘自http://blog.163.com/squall_smile/blog/static/6034984020129296931793/ https://my.oschina.net/simabe ...
- Linux 下 Vi 配置文件 .vimrc 文件
Linux 下 Vi 配置文件 .vimrc 文件 配置 vim 的方法是在用户主目录下建立个.vimrc文件,我一般使用root帐户,所以就在/root/下建立一个.vimrc文件:vi /root ...
随机推荐
- win10 修改 无线名 无线网络属性 名称 修改
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha win10 修改 管理无线网络 无线网络属性 名称 修改 注册表 修改 ======== ...
- WinForm timer 控件
timer 控件:按用户定义的时间间隔引发的事件 属性: Enabled 是否启用: Interval 事件发生的事件间隔,单位是毫秒 事件只有一个:Tick 事件经过指定的时间间隔发生 打开一个窗口 ...
- Bzoj5332: [Sdoi2018]旧试题
国际惯例的题面首先我们进行一些相对显然的数学变化.解释一下第二行的那个变形,如果一个数是ijk的因数,那么它一定能被分解成三部分分别是i,j,k的因数.我们钦定一个质数只能在三部分的一个中出现.如果一 ...
- 潭州课堂25班:Ph201805201 python 模块 datetime,logging 第七课 (课堂笔记)
datetime 模块 # -*- coding: utf-8 -*-# 斌彬电脑# @Time : 2018/7/9 0009 20:42import datetime d = datetime.d ...
- Java虚拟机详解----常用JVM配置参数
本文主要内容: Trace跟踪参数 堆的分配参数 栈的分配参数 零.在IDE的后台打印GC日志: 既然学习JVM,阅读GC日志是处理Java虚拟机内存问题的基础技能,它只是一些人为确定的规则,没有太多 ...
- 如何实现跨域获取iframe子页面动态的url
有的时候iframe的子页面会动态的切换页面,我们在父页面通过iframe1.contentWindow.window.location只能获取同源的子页面的信息.获取跨域的子页面信息会报错. 这时可 ...
- STL——配接器、常用算法使用
学习STL,必然会用到它里面的适配器和一些常用的算法.它们都是STL中的重要组成部分. 适配器 在STL里可以用一些容器适配得到适配器.例如其中的stack和queue就是由双端队列deque容器适配 ...
- jdk9+版本的bug
今天从jvm大神"你假笨"的公众号上,看到一个jdk 9+版本的编译bug,记录一下: public class JavacEvalBug{ private static Stri ...
- Windows如何安装pip
下载这个文件: https://bootstrap.pypa.io/get-pip.py 然后到下载目录执行Python命令: (管理员权限执行) python get-pip.py
- 微信小程序富文本渲染组件html2wxml及html2wxml代码块格式化在ios下字体过大问题
1.组件使用: 之前微信小程序的富文本渲染组件用的wxParse,对普通富文本确实可以,但是对于代码格式pre标签则无法使用. 下面这个html2wxml很不错,可以支持代码高亮. 详细文档:http ...