位置: IT常识 - 正文

Linux中计算特定CPU使用率案例详解(linux 计算字符数)

编辑:rootadmin
这篇文章主要介绍了Linux中计算特定CPU使用率案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下... 21-09-02

推荐整理分享Linux中计算特定CPU使用率案例详解(linux 计算字符数),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:linux怎么计算,linux计算pi,linux运算,linux运算,linux 计算命令,linux 算数,linux计算pi,linux计算sha1,内容如对您有帮助,希望把文章链接给更多的朋友!

Linux中计算特定CPU使用率 需求解决方案拓展参考

需求

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

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

解决方案

1. 背景知识

在/proc/stat中可以查看每一个CPU的使用情况的,如下图:

Linux中计算特定CPU使用率案例详解(linux 计算字符数)

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

/proc/statkernel/system statistics. Varies with architecture. Common entries include:user nice system idle iowait irq softirq steal guest guest_nicecpu 4705 356 584 3699 23 23 0 0 0 0cpu0 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+stealTotal CPU Idle time since boot = idle + iowaitTotal CPU usage time since boot = Total CPU time since boot-Total CPU Idle time since bootTotal 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)。(方法同上一步)最后,计算t2与t1之间系统总的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=0PREV_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 1done拓展

在内核中,关于/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/pcolby/scripts/blob/master/cpu.sh

https://elixir.bootlin.com/linux/v3.14.69/source/fs/proc/stat.c

到此这篇关于Linux中计算特定CPU使用率案例详解的文章就介绍到这了,更多相关Linux中计算特定CPU使用率内容请搜索电脑学习网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持电脑学习网!

本文链接地址:https://www.jiuchutong.com/zhishi/278044.html 转载请保留说明!

上一篇:爱荷华州中部的高架栈桥 (© Kelly van Dellen/Getty Images Plus)(爱荷华州的首府)

下一篇:mhotkey.exe是什么进程 有什么作用 mhotkey进程查询(onekey.exe是什么)

  • 在京东慧采平台怎么入驻(在京东慧采平台采购的单位)

    在京东慧采平台怎么入驻(在京东慧采平台采购的单位)

  • 小白盒子怎么连接打印机(小白盒子怎么连接笔记本)

    小白盒子怎么连接打印机(小白盒子怎么连接笔记本)

  • 抖音视频的下载权限如何设置(抖音视频的下载量怎么看)

    抖音视频的下载权限如何设置(抖音视频的下载量怎么看)

  • p40是曲屏手机吗(p40是曲屏的么)

    p40是曲屏手机吗(p40是曲屏的么)

  • airpods2双击没反应(说airpods2双击不灵的看过来)

    airpods2双击没反应(说airpods2双击不灵的看过来)

  • 京东账号是用户名吗(京东账号是用户名还是手机号)

    京东账号是用户名吗(京东账号是用户名还是手机号)

  • pcam10什么意思(pc10是什么意思)

    pcam10什么意思(pc10是什么意思)

  • 流量卡停机了怎么激活(流量卡停机了怎么办)

    流量卡停机了怎么激活(流量卡停机了怎么办)

  • 显卡dp接口无信号(显卡dp接口无信号hdmi正常)

    显卡dp接口无信号(显卡dp接口无信号hdmi正常)

  • jdn2-w09hn是什么型号(jnd2w09是什么型号)

    jdn2-w09hn是什么型号(jnd2w09是什么型号)

  • 拨打的电话已关机是什么意思(拨打的电话已关机或者无法接通)

    拨打的电话已关机是什么意思(拨打的电话已关机或者无法接通)

  • 微信运动步数怎么显示在屏幕上(微信运动步数怎么刷才能几万步)

    微信运动步数怎么显示在屏幕上(微信运动步数怎么刷才能几万步)

  • 微信限额什么时候可以恢复使用(微信限额什么时候解除)

    微信限额什么时候可以恢复使用(微信限额什么时候解除)

  • 支付宝不支持手机号码充值是怎么回事(支付宝不支持手机版本怎么办)

    支付宝不支持手机号码充值是怎么回事(支付宝不支持手机版本怎么办)

  • 电脑硬盘oem分区什么意思(电脑硬盘oem分区在哪里)

    电脑硬盘oem分区什么意思(电脑硬盘oem分区在哪里)

  • 拉黑了电话还能收到短信吗(拉黑了电话还能打通吗)

    拉黑了电话还能收到短信吗(拉黑了电话还能打通吗)

  • qqpctray.exe损坏文件是什么意思

    qqpctray.exe损坏文件是什么意思

  • iphone7有几个颜色(苹果7有几个颜色)

    iphone7有几个颜色(苹果7有几个颜色)

  • wps如何全部替换文字(wps批量替换内容)

    wps如何全部替换文字(wps批量替换内容)

  • 苹果手机云盘相册在哪(苹果手机相册云盘)

    苹果手机云盘相册在哪(苹果手机相册云盘)

  • 战狼3什么时候上映(战狼3什么时候上映准确时间)

    战狼3什么时候上映(战狼3什么时候上映准确时间)

  • 苹果短信怎么发爱心(苹果短信怎么发不出去信息)

    苹果短信怎么发爱心(苹果短信怎么发不出去信息)

  • 淘宝直播等级有什么用(淘宝直播等级有什么用处)

    淘宝直播等级有什么用(淘宝直播等级有什么用处)

  • 唯品会退款进度在哪看(唯品会退款速度)

    唯品会退款进度在哪看(唯品会退款速度)

  • ps怎么选中文字(ps怎么选中文字中的一个文字)

    ps怎么选中文字(ps怎么选中文字中的一个文字)

  • qq种草是什么意思(种草网络意思)

    qq种草是什么意思(种草网络意思)

  • 小米手机怎么检测电池(小米手机怎么检测各项功能正常)

    小米手机怎么检测电池(小米手机怎么检测各项功能正常)

  • wps文字垂直居中在哪里(wps文字垂直居中怎么设置)

    wps文字垂直居中在哪里(wps文字垂直居中怎么设置)

  • CNN+LSTM+Attention实现时间序列预测(PyTorch版)

    CNN+LSTM+Attention实现时间序列预测(PyTorch版)

  • 金融企业 税
  • 市政道路工程建筑包括什么
  • 退回多报的教育费附加如何做账
  • 分公司以前年度能弥补亏损吗
  • 小规模纳税人每季度报税清盘
  • 支票结算业务流程
  • 本月采购下月付款怎么记账
  • 拆迁补偿款使用范围
  • 房屋征收服务中心是干什么的
  • 实物返利缴纳所得税吗?
  • 环境保护税即将施行 有哪些点需要关注?
  • 劳务派遣公司会扣工资吗
  • 开发转产品好转吗
  • 公司注销无法支付的款项如何处理
  • 收到加盟费尾款怎么做分录?
  • 支付应付账款的会计科目
  • 期望值E(X)怎么算例题
  • 财付通转款会计分录
  • 红字专用发票信息表
  • 企业收到保险公司的赔款
  • 收到财政局专项审计短信
  • 应交增值税与预交增值税报告如何披露
  • 招商银行直播在哪个平台
  • php get post
  • icqlite.exe进程的详细介绍 icqlite进程的查询 作用是什么
  • 如何使用dashboard
  • 高温补贴需要缴纳社会保险费吗
  • 卡特迈国家公园地图
  • php如何实现登录和注册
  • 房地产企业如何计算土地使用税
  • 弗洛雷斯岛
  • 全球矿业巨头力拓集团
  • 珠穆朗玛峰的壮观
  • wamp怎么运行php文件
  • unigui 小程序
  • 建筑会计和会计的区别
  • 从汽车生产到销售有哪些过程
  • 个人完税证明有什么用途
  • 行政事业性收据丢了怎么办
  • 发工资时代扣房贷怎么办
  • 高效刷题app
  • 酒店周转材料怎么摊销
  • 未认证的进项税额会计分录
  • 公司的个人所得税是什么意思
  • 长期待摊费用核销
  • sql server2003
  • 矿泉水发票能否抵税
  • 两个公司开发票
  • 核定征税是什么意思
  • 代开运输发票会不会造成重复征税?
  • 增值税加计扣除最新政策2023
  • 职工福利费相关文件有哪些
  • 应交税费借方是增加还是减少
  • 小规模纳税人销售自己使用过的物品
  • 分公司能
  • 销售费用的增值税是进项还是销项
  • 税务局退回水利基金账务怎么处理
  • 库存商品的出售
  • 基本户打款个人怎么办
  • 商贸公司主营业务成本会计分录
  • 残保金新公司第一年免费吗
  • win7任务管理器快捷键ctrl+alt+
  • window系统怎么复制粘贴
  • sllights.exe - sllights进程是什么意思
  • win8的开始菜单在哪里
  • 怎么重置mysql的密码
  • perl中@_
  • cocos2d schedule
  • bootstrap入门
  • js内存泄漏的原因及解决办法
  • ide通道怎么打开
  • html5过渡的触发机制
  • python文件遍历
  • 江西省国家税务局总局官网
  • 重庆购房退契税
  • 运输发票抵税多少个点
  • 浙江省税务发票
  • 烟草税多少比例
  • 西安市人力资源和社会保障局关于2020年
  • 苏州虎丘区税务局在哪里
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设