源码地址:https://github.com/Rtoax/test/blob/master/c/cpu/cpu_occupy-proc-stat.c


首先看一下文件"/proc/stat"

//"/proc/stat"
//---------------------------------
//      user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice.
//cpu  195044598 6619 410450970 967314001 64039 0 85058 394458 0 0
//cpu0 48705507 1701 102480874 241499274 14130 0 82302 103069 0 0
//cpu1 48866063 1895 102699462 241657644 15853 0 1245 97930 0 0
//cpu2 48782413 1260 102714102 242031473 18001 0 790 96743 0 0
//cpu3 48690614 1762 102556531 242125609 16054 0 720 96715 0 0

上面的数值依次为:user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice.

根据计算公式

(user + nice + system)/(user + nice + system + idle + iowait + irq + softirq)*100%

或者省去一些项

(user + nice + system)/(user + nice + system + idle)*100%

需要注意的是,我们需要统计一段时间的值来计算瞬时CPU利用率。那我们修改公式为:

[(user1 + nice1 + system1) - (user0 + nice0 + system0)]/[(user1 + nice1 + system1 + idle1) - (user0 + nice0 + system0 + idle0)]*100%

是不是很简单。下面给出完整的程序:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>#define NR_CPU_CORES    sysconf(_SC_NPROCESSORS_ONLN)   //CPU核心总数struct __cpu_core_stat {        //"/proc/stat"//---------------------------------//      user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice.//cpu  195044598 6619 410450970 967314001 64039 0 85058 394458 0 0//cpu0 48705507 1701 102480874 241499274 14130 0 82302 103069 0 0//cpu1 48866063 1895 102699462 241657644 15853 0 1245 97930 0 0//cpu2 48782413 1260 102714102 242031473 18001 0 790 96743 0 0//cpu3 48690614 1762 102556531 242125609 16054 0 720 96715 0 0long double user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice;
};struct __cpu_core_stat_pair {struct __cpu_core_stat stat[2]; //0-start, 1-endstruct {int integer, decimal;}occupy;
//    long double occupy;
};struct cpu_cores_occupy {int nr_cpu_core;struct __cpu_core_stat_pair *cpus_stat;};static int cpu_cores_occupy_init(struct cpu_cores_occupy *cco)
{if(!cco)return -1;cco->nr_cpu_core = NR_CPU_CORES;cco->cpus_stat = malloc(sizeof(struct __cpu_core_stat_pair)*(NR_CPU_CORES+1));return 0;
}static int __cpu_cores_occupy_get(struct cpu_cores_occupy *cco, int stat_idx/*0-start, 1-end*/)
{int icore = 0;FILE *fp;struct __cpu_core_stat_pair *cpus_stat = cco->cpus_stat;fp = fopen("/proc/stat","r");for(icore=0;icore<=cco->nr_cpu_core;icore++) {fscanf(fp,"%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf",&cpus_stat[icore].stat[stat_idx].user, &cpus_stat[icore].stat[stat_idx].nice, &cpus_stat[icore].stat[stat_idx].system, &cpus_stat[icore].stat[stat_idx].idle, &cpus_stat[icore].stat[stat_idx].iowait, &cpus_stat[icore].stat[stat_idx].irq, &cpus_stat[icore].stat[stat_idx].softirq, &cpus_stat[icore].stat[stat_idx].steal, &cpus_stat[icore].stat[stat_idx].guest, &cpus_stat[icore].stat[stat_idx].guest_nice);}fclose(fp);
}static int cpu_cores_occupy_getstart(struct cpu_cores_occupy *cco)
{return __cpu_cores_occupy_get(cco, 0);
}static int cpu_cores_occupy_getend(struct cpu_cores_occupy *cco)
{return __cpu_cores_occupy_get(cco, 1);
}static int cpu_cores_occupy_call(struct cpu_cores_occupy *cco)
{int icore = 0, idx;struct __cpu_core_stat_pair *cpus_stat = cco->cpus_stat;for(icore=0;icore<=cco->nr_cpu_core;icore++) {struct __cpu_core_stat *s0 = &cpus_stat[icore].stat[0];struct __cpu_core_stat *s1 = &cpus_stat[icore].stat[1];long double s0_0 = (s0->user+s0->nice+s0->system);long double s1_0 = (s1->user+s1->nice+s1->system);long double s0_1 = (s0->user+s0->nice+s0->system+s0->idle);long double s1_1 = (s1->user+s1->nice+s1->system+s1->idle);long double occupy = (s1_0 - s0_0) / (s1_1 - s0_1);cpus_stat[icore].occupy.integer = (int)(occupy*100);cpus_stat[icore].occupy.decimal = (int)(occupy*10000);//        printf("cpu%d %Lf = (%Lf - %Lf) / (%Lf - %Lf)\n", icore-1, cpus_stat[icore].occupy, s1_0, s0_0, s1_1, s0_1);                            }
}static int cpu_cores_occupy_display(struct cpu_cores_occupy *cco)
{int icore = 0;struct __cpu_core_stat_pair *cpus_stat = cco->cpus_stat;system("clear");for(icore=0;icore<=cco->nr_cpu_core;icore++) {printf("cpu%d \t%3d.%-4d %%\n", icore-1,cpus_stat[icore].occupy.integer, cpus_stat[icore].occupy.decimal);}}int main()
{struct cpu_cores_occupy cco;cpu_cores_occupy_init(&cco);for(;;){cpu_cores_occupy_getstart(&cco);sleep(1);cpu_cores_occupy_getend(&cco);cpu_cores_occupy_call(&cco);cpu_cores_occupy_display(&cco);}
}

开始运行

[root@localhost cpu]# ./a.out
cpu-1     0.25   %
cpu0      0.0    %
cpu1      1.101  %
cpu2      0.0    %
cpu3      0.0    %

我们写一个有自旋锁死锁的程序,或者干脆一个while(1);并绑定核心运行,如下:

[root@localhost c]# taskset -c 1 ./a.out &
[1] 182556
[root@localhost c]# taskset -c 3 ./a.out &
[2] 182569

结果如下:

即可看见CPU利用率100%的情况,然后我们用top指令验证一下:

确认无误,下班。

Linux系统C语言获取所有CPU核心的利用率“/proc/stat”相关推荐

  1. linux多cpu运行python脚本,linux系统使用python获取cpu信息脚本分享

    linux系统使用python获取cpu信息脚本分享 代码如下: #!/usr/bin/env Python from __future__ import print_function from co ...

  2. Linux系统查看当前主机CPU、内存、机器型号及主板信息

    Linux系统查看当前主机CPU.内存.机器型号及主板信息: 查看CPU信息(型号) # cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c 查 ...

  3. linux系统gpk-update-icon进程占用CPU资源100%

    1. 症状:linux系统gpk-update-icon进程占用CPU资源100%,监控到用户进程跑到100% 2.原因分析: gpk-update-icon进程在GUI模式下会自动通知rpm软件包更 ...

  4. 利用shell脚本来监控linux系统的负载与CPU占用情况

    这几天在学习研究shell脚本,写的一些系统负载与CPU监控脚本程序.在没有nagios监控软件的情况下,只要服务器能上互联网,就可通过发邮件的方式来提醒管理员系统负载与CPU占用的使用情况. 一.安 ...

  5. 如何查看linux系统版本信息及CPU信息

    捣腾linux系统时,偶尔会需要查看linux系统的版本信息.下面就是几种常用的方法: 1.输入"uname -a ",显示电脑以及操作系统的相关信息: leon@Ubuntu:~ ...

  6. Linux系统【一】CPU+MMU+fork函数创建进程

    切板中的内容输出到文件### 进程相关概念 程序:编译好的二进制文件,在磁盘上,不占用系统资源(不包括磁盘).(剧本) 进程:占用系统资源,是程序的一次运行.(戏剧) 一个程序可以产生多个进程,一个进 ...

  7. 理解Linux系统平均负载和CPU使用率

    CPU 使用率 CPU 使用率就是 CPU 非空闲态运行的时间占比,它反映了 CPU 的繁忙程度.比如,单核 CPU 1s 内非空闲态运行时间为 0.8s,那么它的 CPU 使用率就是 80%:双核 ...

  8. linux编译对cpu要求,Linux系统中使用GCC CPU参数优化代码编译

    使用特定的GCC参数可以使编译出的程序执行效率有较大提升.具体如下: 1.优化原理: 在编译程序时,借助参数传递的方法,使用与系统CPU相匹配的gcc参数,编译出的程序就是为系统CPU而进行特定优化过 ...

  9. linux nginx cpu 高,Linux 系统 Nginx性能优化CPU参数

    Nginx性能优化CPU参数worker_cpu_affinity使用说明 官方说明: worker_cpu_affinity Syntax:worker_cpu_affinity cpumask [ ...

最新文章

  1. 修改文件默认打开方式
  2. base64转码java版
  3. 字符串复制中的while条件
  4. Python实现的导弹跟踪算法,燃!
  5. 搭建eureka注册中心
  6. java有趣项目_有趣的java小项目------猜拳游戏
  7. java 自定义对象 排序,使用自定义排序顺序对对象的ArrayList进行排序
  8. html读取servlet,简单html与servlet交互(HTML利用servlet读取txt)
  9. node express+socket.io实现聊天室
  10. Quartz + Oracle 分布式Job实现
  11. Sql Server 分区之后增加新的分区
  12. 两矩阵相乘的秩的性质_矩阵分析与应用(一,矩阵基础知识)
  13. 一元云购短信-配置修改
  14. 基于Python的作业自动批改系统
  15. android 文件管理 ca,安卓Android手机添加根证书
  16. Linux升级ilo,利用HP iLO4安装系统
  17. 每周分享第 25 期
  18. 使用dockpanel动态添加picturebox并绑定图片
  19. android7.0 8.1 9.0 10.0 去掉屏幕锁屏(屏幕默认锁屏方式改成无)
  20. Linux中使用gzip来压缩/解压 *.gz文件

热门文章

  1. 市场调研策划书_市场调查计划书模板
  2. Cannot convert value of type 'com.sun.proxy.$Proxy10 implementing com.shuai.
  3. Guid.NewGuid().ToString()的几种格式 (转)
  4. Idea创建一个springboot多模块项目
  5. c++如何编写线程安全的DLL
  6. 【Windows phone 8】欢迎引导页面01
  7. web.config forms节点中的属性的含义和用途
  8. java将a对象转换为b对象_Java 对象的深复制五种方式
  9. java easyui 分页_Spring mvc+easyui做列表展示及分页
  10. maven netty 配置_使用Springboot整合开发Netty(一个表白的小案例)