Linux中计算特定CPU使用率

  • 需求
    • 解决方案
    • 拓展
    • 参考

需求

在Linux中可以通过top指令查看某一进程占用的CPU情况,也可以查看某一个CPU使用率情况(先top指令,然后按数字“1”键即可显示每一个CPU的使用情况),如下图:

而我们的需求是:如何得到一个CPU的占用率呢?

解决方案

1. 背景知识
在/proc/stat中可以查看每一个CPU的使用情况的,如下图:

其中cpu(0/1/2/…)后面的那十个数字含义如下:

/proc/stat
kernel/system statistics.  Varies with architecture.
Common entries include:user nice system idle iowait  irq  softirq steal guest guest_nice
cpu  4705 356  584    3699   23    23     0       0     0        0
cpu0 1393280 32966 572056 13343292 6130 0 17875 0 23933 0The amount of time, measured in units of USER_HZ(1/100ths of a second on most architectures, usesysconf(_SC_CLK_TCK) to obtain the right value), thatthe system ("cpu" line) or the specific CPU ("cpuN"line) spent in various states:user   (1) Time spent in user mode.nice   (2) Time spent in user mode with low priority(nice).system (3) Time spent in system mode.idle   (4) Time spent in the idle task.  This valueshould be USER_HZ times the second entry in the/proc/uptime pseudo-file.iowait (since Linux 2.5.41)(5) Time waiting for I/O to complete.  Thisvalue is not reliable, for the following rea‐sons:1. The CPU will not wait for I/O to complete;iowait is the time that a task is waiting forI/O to complete.  When a CPU goes into idlestate for outstanding task I/O, another taskwill be scheduled on this CPU.2. On a multi-core CPU, the task waiting for I/Oto complete is not running on any CPU, so theiowait of each CPU is difficult to calculate.3. The value in this field may decrease in cer‐tain conditions.irq (since Linux 2.6.0-test4)(6) Time servicing interrupts.softirq (since Linux 2.6.0-test4)(7) Time servicing softirqs.steal (since Linux 2.6.11)(8) Stolen time, which is the time spent inother operating systems when running in a virtu‐alized environmentguest (since Linux 2.6.24)(9) Time spent running a virtual CPU for guestoperating systems under the control of the Linuxkernel.guest_nice (since Linux 2.6.33)(10) Time spent running a niced guest (virtualCPU for guest operating systems under the con‐trol of the Linux kernel).

2.计算具体CPU使用率
有了上面的背景知识,接下来我们就可以计算具体CPU的使用情况了。具体计算方式如下:

Total CPU time since boot = user+nice+system+idle+iowait+irq+softirq+steal
Total CPU Idle time since boot = idle + iowait
Total CPU usage time since boot = Total CPU time since boot - Total CPU Idle time since boot
Total CPU percentage = Total CPU usage time since boot/Total CPU time since boot * 100%

有了上面的计算公式,计算某一CPU使用率或者系统总的CPU占用率也就是不难了。
示例:计算系统整体CPU占用情况
首先从/proc/stat中获取 t1时刻系统总体的user、nice、system、idle、iowait、irq、softirq、steal、guest、guest_nice的值,得到此时Total CPU time since boot(记为total1)和 Total CPU idle time since boot(记为idle1)。
其次,从/proc/stat中获取t2时刻系统总的Total CPU time since boot(记为total2)和Total CPU idle time since boot(记为idle2)。(方法同上一步)
最后,计算t2t1之间系统总的CPU使用情况。也就是:
CPU percentage between t1 and t2 = ((total2-total1)-(idle2-idle1))/(total2-total1)* 100%
其中, ((total2-total1)-(idle2-idle1))实际上就是t1与t2时刻之间系统CPU被占用的时间(总时间 - 空闲时间)。
下面是一段计算时间段内CPU被占用情况的脚本:

#!/bin/bash
# by Paul Colby (http://colby.id.au), no rights reserved ;)PREV_TOTAL=0
PREV_IDLE=0while true; do# Get the total CPU statistics, discarding the 'cpu ' prefix.CPU=(`sed -n 's/^cpu\s//p' /proc/stat`)IDLE=${CPU[3]} # Just the idle CPU time.# Calculate the total CPU time.TOTAL=0for VALUE in "${CPU[@]}"; dolet "TOTAL=$TOTAL+$VALUE"done# Calculate the CPU usage since we last checked.let "DIFF_IDLE=$IDLE-$PREV_IDLE"let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL"let "DIFF_USAGE=(1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10"echo -en "\rCPU: $DIFF_USAGE%  \b\b"# Remember the total and idle CPU times for the next check.PREV_TOTAL="$TOTAL"PREV_IDLE="$IDLE"# Wait before checking again.sleep 1
done

拓展

在内核中,关于/proc/stat中文件的实现函数如下:

附注:内核版本3.14.69,文件为 /fs/proc/stat.c#include <linux/cpumask.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel_stat.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
#include <linux/time.h>
#include <linux/irqnr.h>
#include <asm/cputime.h>
#include <linux/tick.h>#ifndef arch_irq_stat_cpu
#define arch_irq_stat_cpu(cpu) 0
#endif
#ifndef arch_irq_stat
#define arch_irq_stat() 0
#endif#ifdef arch_idle_timestatic cputime64_t get_idle_time(int cpu)
{cputime64_t idle;idle = kcpustat_cpu(cpu).cpustat[CPUTIME_IDLE];if (cpu_online(cpu) && !nr_iowait_cpu(cpu))idle += arch_idle_time(cpu);return idle;
}static cputime64_t get_iowait_time(int cpu)
{cputime64_t iowait;iowait = kcpustat_cpu(cpu).cpustat[CPUTIME_IOWAIT];if (cpu_online(cpu) && nr_iowait_cpu(cpu))iowait += arch_idle_time(cpu);return iowait;
}#elsestatic u64 get_idle_time(int cpu)
{u64 idle, idle_time = -1ULL;if (cpu_online(cpu))idle_time = get_cpu_idle_time_us(cpu, NULL);if (idle_time == -1ULL)/* !NO_HZ or cpu offline so we can rely on cpustat.idle */idle = kcpustat_cpu(cpu).cpustat[CPUTIME_IDLE];elseidle = usecs_to_cputime64(idle_time);return idle;
}static u64 get_iowait_time(int cpu)
{u64 iowait, iowait_time = -1ULL;if (cpu_online(cpu))iowait_time = get_cpu_iowait_time_us(cpu, NULL);if (iowait_time == -1ULL)/* !NO_HZ or cpu offline so we can rely on cpustat.iowait */iowait = kcpustat_cpu(cpu).cpustat[CPUTIME_IOWAIT];elseiowait = usecs_to_cputime64(iowait_time);return iowait;
}#endifstatic int show_stat(struct seq_file *p, void *v)
{int i, j;unsigned long jif;u64 user, nice, system, idle, iowait, irq, softirq, steal;u64 guest, guest_nice;u64 sum = 0;u64 sum_softirq = 0;unsigned int per_softirq_sums[NR_SOFTIRQS] = {0};struct timespec boottime;user = nice = system = idle = iowait =irq = softirq = steal = 0;guest = guest_nice = 0;getboottime(&boottime);jif = boottime.tv_sec;for_each_possible_cpu(i) {user += kcpustat_cpu(i).cpustat[CPUTIME_USER];nice += kcpustat_cpu(i).cpustat[CPUTIME_NICE];system += kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM];idle += get_idle_time(i);iowait += get_iowait_time(i);irq += kcpustat_cpu(i).cpustat[CPUTIME_IRQ];softirq += kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ];steal += kcpustat_cpu(i).cpustat[CPUTIME_STEAL];guest += kcpustat_cpu(i).cpustat[CPUTIME_GUEST];guest_nice += kcpustat_cpu(i).cpustat[CPUTIME_GUEST_NICE];sum += kstat_cpu_irqs_sum(i);sum += arch_irq_stat_cpu(i);for (j = 0; j < NR_SOFTIRQS; j++) {unsigned int softirq_stat = kstat_softirqs_cpu(j, i);per_softirq_sums[j] += softirq_stat;sum_softirq += softirq_stat;}}sum += arch_irq_stat();seq_puts(p, "cpu ");seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(user));seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(nice));seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(system));seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(idle));seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(iowait));seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(irq));seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(softirq));seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(steal));seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest));seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest_nice));seq_putc(p, '\n');for_each_online_cpu(i) {/* Copy values here to work around gcc-2.95.3, gcc-2.96 */user = kcpustat_cpu(i).cpustat[CPUTIME_USER];nice = kcpustat_cpu(i).cpustat[CPUTIME_NICE];system = kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM];idle = get_idle_time(i);iowait = get_iowait_time(i);irq = kcpustat_cpu(i).cpustat[CPUTIME_IRQ];softirq = kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ];steal = kcpustat_cpu(i).cpustat[CPUTIME_STEAL];guest = kcpustat_cpu(i).cpustat[CPUTIME_GUEST];guest_nice = kcpustat_cpu(i).cpustat[CPUTIME_GUEST_NICE];seq_printf(p, "cpu%d", i);seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(user));seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(nice));seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(system));seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(idle));seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(iowait));seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(irq));seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(softirq));seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(steal));seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest));seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest_nice));seq_putc(p, '\n');}seq_printf(p, "intr %llu", (unsigned long long)sum);/* sum again ? it could be updated? */for_each_irq_nr(j)seq_put_decimal_ull(p, ' ', kstat_irqs_usr(j));seq_printf(p,"\nctxt %llu\n""btime %lu\n""processes %lu\n""procs_running %lu\n""procs_blocked %lu\n",nr_context_switches(),(unsigned long)jif,total_forks,nr_running(),nr_iowait());seq_printf(p, "softirq %llu", (unsigned long long)sum_softirq);for (i = 0; i < NR_SOFTIRQS; i++)seq_put_decimal_ull(p, ' ', per_softirq_sums[i]);seq_putc(p, '\n');return 0;
}static int stat_open(struct inode *inode, struct file *file)
{size_t size = 1024 + 128 * num_possible_cpus();char *buf;struct seq_file *m;int res;/* minimum size to display an interrupt count : 2 bytes */size += 2 * nr_irqs;/* don't ask for more than the kmalloc() max size */if (size > KMALLOC_MAX_SIZE)size = KMALLOC_MAX_SIZE;buf = kmalloc(size, GFP_KERNEL);if (!buf)return -ENOMEM;res = single_open(file, show_stat, NULL);if (!res) {m = file->private_data;m->buf = buf;m->size = ksize(buf);} elsekfree(buf);return res;
}static const struct file_operations proc_stat_operations = {.open     = stat_open,.read      = seq_read,.llseek     = seq_lseek,.release   = single_release,
};static int __init proc_stat_init(void)
{proc_create("stat", 0, NULL, &proc_stat_operations);return 0;
}
fs_initcall(proc_stat_init);

参考

http://man7.org/linux/man-pages/man5/proc.5.html
https://github.com/Leo-G/DevopsWiki/wiki/How-Linux-CPU-Usage-Time-and-Percentage-is-calculated
https://github.com/pcolby/scripts/blob/master/cpu.sh
https://elixir.bootlin.com/linux/v3.14.69/source/fs/proc/stat.c

Linux中计算特定CPU使用率相关推荐

  1. 查找Linux中内存和CPU使用率最高的进程

    需求:创建一个定时任务,每30分钟输出该时刻(年月日分秒)系统中内存占用最大的四个进程名及进程占用内存 打印当前系统时间(年月日分秒) [root@localhost /]# date "+ ...

  2. android cpu负载 工具,计算Android中进程的CPU使用率

    我正在尝试按以下方式计算Android中进程的CPU使用率,但是由于产生的输出,我不确定它是否正确. 要将吉菲转换为秒:吉菲/赫兹 第一步: 使用/proc/uptime文件的第一个参数获取正常运行时 ...

  3. Linux top命令的cpu使用率和内存使用率

    文章目录 前言 一.cpu使用率 1.1 top简介 1.2 cpu使用率的来源 1.3 内核相关源码 二.内存使用率 2.1 总内存有关的数据 2.2 进程使用内存有关的数据 2.3 内存使用率的来 ...

  4. 【linux性能优化】CPU使用率过高分析

    最常用什么指标来描述系统的 CPU 性能呢? 可能不是平均负载,也不是 CPU上下文切换,而是另一个更直观的指标CPU使用率 CPU使用率是单位时间内CPU使用情况的统计,以百分比的方式展示 那么,作 ...

  5. 【Linux 内核】进程管理 ( Linux 中进程的 CPU 资源调度 | 进程生命周期 | 创建状态 | 就绪状态 | 执行状态 | 阻塞状态 | 终止状态 | 进程生命周期之间的转换 )

    文章目录 一.Linux 中进程的 CPU 资源调度 二.进程生命周期 三.进程生命周期之间的转换 一.Linux 中进程的 CPU 资源调度 Linux 操作系统 是 多任务系统 , 可以 同时运行 ...

  6. linux中进程与cpu核的绑定

    linux中进程与cpu核的绑定 一.cpu亲缘性 二.fork用法 三.设置进程与cpu的亲缘性 一.cpu亲缘性 进程绑定CPU亲缘性,使得进程只能在指定的cpu上进行调度或者运行. 绑定cpu亲 ...

  7. Linux中Python程序CPU占用高排查

    Linux中Python程序CPU占用高排查,Linux中Python程序CPU占用高排查,Linux中Python程序CPU占用高排查 kafka-python==2.0.2和 gevent 新版本 ...

  8. linux 调整 内核空间占用cpu,[Linux性能优化]3.CPU使用率100%,怎么办?

    8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? CPU使用率是单位时间内CPU使用情况的统计,以百分比的方式展示. CPU时间是Linux通过事先定义的节拍率(内核中表 ...

  9. Linux下C语言获取cpu使用率

    一.功能介绍 通过读取/proc/stat文件获取当前系统的CPU占用率. Linux系统上的/proc目录是一种文件系统,即proc文件系统,与其它常见的文件系统不同的是,/proc文件系统是一个伪 ...

最新文章

  1. Android CheckBox 修改选择框
  2. MySQL内存预估_mysql时该如何估算内存的消耗,公式如何计算?
  3. AndroidStudio2.2 Preview3中NDK开发之CMake和传统 JNI在目录结构和配置文件上的区别
  4. 作业: IT 行业博客网站分析和创新
  5. ASP.NET开源框架之HIPPO技术内幕(四)--多语言的实现
  6. 有关为旧版代码创建存根的更多信息–测试技术7
  7. Slog86_使用Sketch 设计ArthurSlog网站主页原型
  8. windows服务初识
  9. Linux服务器安全配置三要点
  10. 计算机二级C语言判断标准,2012年计算机等级考试二级C语言常见问题:标准输入输出库...
  11. 淘宝网或阿里巴巴模拟登陆获取数据
  12. 射极跟随器实验报告数据处理_射极跟随器实验报告 -
  13. 交换机三种端口模式Access、Hybrid和Trunk
  14. 海思35xx移动侦测-修改sdk中sample到嵌入式设备测试成功
  15. 企业网中nextcloud与iRedmail邮件系统的配合
  16. Ubuntu中安装VirtualBox
  17. 『Hadoop』MapReduce 处理 日志log(单机版)
  18. SSM+医保业财一体化管理系统 毕业设计-附源码151023
  19. 阿里巴巴图标库的使用
  20. 你需要了解的群体重测序都在这里(一)

热门文章

  1. 联想微型计算机如何设置u盘启动,联想电脑怎么设置U盘启动
  2. 奔图Pantum M6608 一体机驱动
  3. 【web-攻击用户】(9.4)跨域捕获数据——通过注入HTML捕获数据、注入CSS捕获数据、JavaScript劫持
  4. 调用百度h5活体检测
  5. RocketMQ-Retry
  6. tomcat运行web项目报错:请求的资源[]不可用
  7. mac pro M1(ARM)安装:ubuntu虚拟机(四)
  8. Ensight后处理软件显示网格
  9. 计算机考试ppt应用,职称计算机考试辅导:Powerpoint使用母版
  10. Springboot美食分享系统716y7计算机毕业设计-课程设计-期末作业-毕设程序代做