当前位置:   article > 正文

Linux系统C语言获取所有CPU核心的利用率“/proc/stat”_linux下c/c 实现cpustat完整代码

linux下c/c 实现cpustat完整代码

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


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

  1. //"/proc/stat"
  2. //---------------------------------
  3. // user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice.
  4. //cpu 195044598 6619 410450970 967314001 64039 0 85058 394458 0 0
  5. //cpu0 48705507 1701 102480874 241499274 14130 0 82302 103069 0 0
  6. //cpu1 48866063 1895 102699462 241657644 15853 0 1245 97930 0 0
  7. //cpu2 48782413 1260 102714102 242031473 18001 0 790 96743 0 0
  8. //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利用率。那我们修改公式为:

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

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

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #define NR_CPU_CORES sysconf(_SC_NPROCESSORS_ONLN) //CPU核心总数
  5. struct __cpu_core_stat {
  6. //"/proc/stat"
  7. //---------------------------------
  8. // user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice.
  9. //cpu 195044598 6619 410450970 967314001 64039 0 85058 394458 0 0
  10. //cpu0 48705507 1701 102480874 241499274 14130 0 82302 103069 0 0
  11. //cpu1 48866063 1895 102699462 241657644 15853 0 1245 97930 0 0
  12. //cpu2 48782413 1260 102714102 242031473 18001 0 790 96743 0 0
  13. //cpu3 48690614 1762 102556531 242125609 16054 0 720 96715 0 0
  14. long double user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice;
  15. };
  16. struct __cpu_core_stat_pair {
  17. struct __cpu_core_stat stat[2]; //0-start, 1-end
  18. struct {
  19. int integer, decimal;
  20. }occupy;
  21. // long double occupy;
  22. };
  23. struct cpu_cores_occupy {
  24. int nr_cpu_core;
  25. struct __cpu_core_stat_pair *cpus_stat;
  26. };
  27. static int cpu_cores_occupy_init(struct cpu_cores_occupy *cco)
  28. {
  29. if(!cco)
  30. return -1;
  31. cco->nr_cpu_core = NR_CPU_CORES;
  32. cco->cpus_stat = malloc(sizeof(struct __cpu_core_stat_pair)*(NR_CPU_CORES+1));
  33. return 0;
  34. }
  35. static int __cpu_cores_occupy_get(struct cpu_cores_occupy *cco, int stat_idx/*0-start, 1-end*/)
  36. {
  37. int icore = 0;
  38. FILE *fp;
  39. struct __cpu_core_stat_pair *cpus_stat = cco->cpus_stat;
  40. fp = fopen("/proc/stat","r");
  41. for(icore=0;icore<=cco->nr_cpu_core;icore++) {
  42. fscanf(fp,"%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf",
  43. &cpus_stat[icore].stat[stat_idx].user,
  44. &cpus_stat[icore].stat[stat_idx].nice,
  45. &cpus_stat[icore].stat[stat_idx].system,
  46. &cpus_stat[icore].stat[stat_idx].idle,
  47. &cpus_stat[icore].stat[stat_idx].iowait,
  48. &cpus_stat[icore].stat[stat_idx].irq,
  49. &cpus_stat[icore].stat[stat_idx].softirq,
  50. &cpus_stat[icore].stat[stat_idx].steal,
  51. &cpus_stat[icore].stat[stat_idx].guest,
  52. &cpus_stat[icore].stat[stat_idx].guest_nice);
  53. }
  54. fclose(fp);
  55. }
  56. static int cpu_cores_occupy_getstart(struct cpu_cores_occupy *cco)
  57. {
  58. return __cpu_cores_occupy_get(cco, 0);
  59. }
  60. static int cpu_cores_occupy_getend(struct cpu_cores_occupy *cco)
  61. {
  62. return __cpu_cores_occupy_get(cco, 1);
  63. }
  64. static int cpu_cores_occupy_call(struct cpu_cores_occupy *cco)
  65. {
  66. int icore = 0, idx;
  67. struct __cpu_core_stat_pair *cpus_stat = cco->cpus_stat;
  68. for(icore=0;icore<=cco->nr_cpu_core;icore++) {
  69. struct __cpu_core_stat *s0 = &cpus_stat[icore].stat[0];
  70. struct __cpu_core_stat *s1 = &cpus_stat[icore].stat[1];
  71. long double s0_0 = (s0->user+s0->nice+s0->system);
  72. long double s1_0 = (s1->user+s1->nice+s1->system);
  73. long double s0_1 = (s0->user+s0->nice+s0->system+s0->idle);
  74. long double s1_1 = (s1->user+s1->nice+s1->system+s1->idle);
  75. long double occupy = (s1_0 - s0_0) / (s1_1 - s0_1);
  76. cpus_stat[icore].occupy.integer = (int)(occupy*100);
  77. cpus_stat[icore].occupy.decimal = (int)(occupy*10000);
  78. // printf("cpu%d %Lf = (%Lf - %Lf) / (%Lf - %Lf)\n", icore-1, cpus_stat[icore].occupy, s1_0, s0_0, s1_1, s0_1);
  79. }
  80. }
  81. static int cpu_cores_occupy_display(struct cpu_cores_occupy *cco)
  82. {
  83. int icore = 0;
  84. struct __cpu_core_stat_pair *cpus_stat = cco->cpus_stat;
  85. system("clear");
  86. for(icore=0;icore<=cco->nr_cpu_core;icore++) {
  87. printf("cpu%d \t%3d.%-4d %%\n", icore-1,cpus_stat[icore].occupy.integer, cpus_stat[icore].occupy.decimal);
  88. }
  89. }
  90. int main()
  91. {
  92. struct cpu_cores_occupy cco;
  93. cpu_cores_occupy_init(&cco);
  94. for(;;)
  95. {
  96. cpu_cores_occupy_getstart(&cco);
  97. sleep(1);
  98. cpu_cores_occupy_getend(&cco);
  99. cpu_cores_occupy_call(&cco);
  100. cpu_cores_occupy_display(&cco);
  101. }
  102. }

开始运行

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

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

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

结果如下:

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

确认无误,下班。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/523131
推荐阅读
相关标签
  

闽ICP备14008679号