我是靠谱客的博主 爱笑鼠标,这篇文章主要介绍linux系统下CPU利用率的计算一、设计思路二、参考代码,现在分享给大家,希望可以做个参考。

目录

一、设计思路

二、参考代码


一、设计思路

        思路很简单:主要通过解析 /proc/stat 文件中记录的 CPU 使用详情信息。内容如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ cat /proc/stat cpu 500 0 1148 37091 0 21 0 0 0 0 cpu0 46 0 193 4603 0 17 0 0 0 0 cpu1 17 0 34 4792 0 0 0 0 0 0 cpu2 198 0 171 4473 0 3 0 0 0 0 cpu3 21 0 20 4801 0 0 0 0 0 0 cpu4 96 0 331 4415 0 0 0 0 0 0 cpu5 34 0 82 4726 0 0 0 0 0 0 cpu6 62 0 128 4653 0 0 0 0 0 0 cpu7 26 0 189 4628 0 1 0 0 0 0 intr 894498 377553 332 0 0 0 0 0 0 0 287178 0 0 1078 0 8572 189 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 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 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 ctxt 494453 btime 1658673908 processes 21 procs_running 1 procs_blocked 0 softirq 531871 0 166655 5588 285920 8660 0 272 0 791 63985

        该文件的第一行记录了 CPU 时间片的分布情况。字段依次为:

user 

从系统启动开始累计到当前时刻,用户态的CPU时间,不包含nice值为负进程。

nice 

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

system 

从系统启动开始累计到当前时刻,核心时间

idle 

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

iowait 

从系统启动开始累计到当前时刻,IO等待时间

irq 

从系统启动开始累计到当前时刻,硬中断时间

softirq 

从系统启动开始累计到当前时刻,软中断时间

    CPU利用率 = 1 - (CPU空闲时间片 / CPU时间片总和)。

    CPU时间片总和 = user + nice + system + idle + iowait + irq + softirq

    CPU 空闲时间片 = idle

         需要注意的是,/proc/stat 文件中记录的时间都是自系统启动开始的累计时间,所以计算CPU的使用率需要依据一个时间间隔内的CPU时间片分布情况。

二、参考代码

        只是参考,肯定跑不起来的,缺了一些头文件。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
typedef struct cpu_info_ { long time_user; long time_nice; long time_system; long time_idle; long time_iowait; long time_irq; long time_sirq; }cpu_info; cpu_info *cpu_info_snap() { char cmd[CMD_LEN_GER] = "cat /proc/stat", cpu_info_tmp[CMD_LEN_RES] = "", str_tmp[10] = ""; FILE *file = popen(cmd, "r"); if (file == NULL) { LOG_FATAL("[CPU]-->cmd(%s) execute failedn", cmd); return NULL; } cpu_info *cpu_time = (cpu_info *)malloc(sizeof(cpu_info)); fgets(cpu_info_tmp, CMD_LEN_RES, file); sscanf(cpu_info_tmp, "%s %ld %ld %ld %ld %ld %ld %ld", str_tmp, &cpu_time->time_user, &cpu_time->time_nice, &cpu_time->time_system, &cpu_time->time_idle, &cpu_time->time_iowait, &cpu_time->time_irq, &cpu_time->time_sirq); return cpu_time; } long cpu_time_total(cpu_info *info) { if (info == NULL) { return ERROR; } return info->time_user + info->time_nice + info->time_system + info->time_idle + info->time_iowait + info->time_irq + info->time_sirq; } int cpu_gen_info(char *cpu_info_str, int len, char *err) { cpu_info *cpu_time_pre = cpu_info_snap(); usleep(100*1000); cpu_info *cpu_time_back = cpu_info_snap(); long ct_total_pre = cpu_time_total(cpu_time_pre), ct_total_back = cpu_time_total(cpu_time_back); if (ct_total_pre < 0 || ct_total_back < 0 || ct_total_pre >= ct_total_back) { LOG_ERROR("[CPU]->cpu time format error! pre(%ld)--back(%ld)n", ct_total_pre, ct_total_back); return ERROR; } long ct_idle_pre = cpu_time_pre->time_idle, ct_idle_back = cpu_time_back->time_idle; if(ct_idle_back <= ct_idle_pre) { LOG_ERROR("[CPU]->cpu idle time fotmat errot! pre(%ld)--back(%ld)n", ct_idle_pre, ct_idle_back); return ERROR; } int rate = (1 - (ct_idle_back * 1.0 - ct_idle_pre * 1.0) / (ct_total_back * 1.0 - ct_total_pre * 1.0)) * 100; printf("%d n", rate); free(cpu_time_pre); free(cpu_time_back); return OK; }

        有什么不足欢迎提出来。

最后

以上就是爱笑鼠标最近收集整理的关于linux系统下CPU利用率的计算一、设计思路二、参考代码的全部内容,更多相关linux系统下CPU利用率内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(73)

评论列表共有 0 条评论

立即
投稿
返回
顶部