首页 > 作文

Linux中计算特定CPU使用率案例详解

更新时间:2023-04-05 20:33:05 阅读: 评论:0

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

需求

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

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

解决方案

1. 背景知识

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

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

/proc/statkernel/system statistics.  varies with architecture.  common entries include:     ur 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 0   the amount of time, measured in units of ur_hz   (1/100ths of a cond on most architectures, u   sysconf(_sc_clk_tck) to obtain the right value), that   the system ("cpu" line) or the specific cpu ("cpun"   line) spent in various states:   ur   (1) time spent in ur mode.   nice   (2) time spent in ur mode with low priority          (nice).   system (3) time spent in system mode.   idle   (4) time spent in the idle task.  this value          should be ur_hz times the cond entry in the          /proc/uptime pudo-file.   iowait (since linux 2.5.41)          (5) time waiting for i/o to complete.  this          value 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 for             i/o to complete.  when a cpu goes into idle             state for outstanding task i/o, another task             will be scheduled on this cpu.          2. on a multi-core cpu, the task waiting for i/o             to complete is not running on any cpu, so the             iowait of each cpu is difficult to calculate.          3. the value in this field may decrea in c5 1劳动节作文er‐             tain conditions.   irq (since linux 2.6.0-test4)          (6) time rvicing interrupts.   softirq (since linux 2.6.0-test4)          (7) time rvicing softirqs.   steal (since linux 2.6.11)          (8) stolen time, which is the time spent in          other operating systems when running in a virtu‐          alized environment   guest (since linux 2.6.24)          (9) time spent running a virtual cpu for guest          operating systems under the control of the linux          kernel.   guest_nice (since linux 2.6.33)          (10) time spent running a niced guest (virtual          cpu for guest operating systems under the con‐          trol of the linux kernel).

2.计算具体cpu使用率

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

total cpu time since boot = ur+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时刻系统总体的ur、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 rerved ;)prev_total=0prev_idle=0while true; do  # get the total cpu statistics, discarding the 'cpu ' prefix.  cpu=(`d -n 's/^cpu\s//p' /proc/stat`)  idle=${cpu[3]} # just the idle cpu time.  # calculate the total cpu time.  total=0  for value in "${cpu[@]}"; do    let "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/q_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;}#elstatic 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];elidle = ucs_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];eliowait = ucs_to_cputime64(iowait_time);return iowait;}#endifstatic int show_stat(struct q_file *p, void *v){int i, j;unsigned long jif;u64 ur, 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;ur = nice = system = idle = iowait =irq = softirq = steal = 0;guest = guest_nice = 0;getboottime(&boottime);jif = boottime.tv_c;for_each_possible_cpu(i) {ur += kcpustat_cpu(i).cpustat[cputime_ur];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();q_puts(p, "cpu ");q_put_decimal_ull(p, ' ', cputime64_to_clock_t(ur));q_put_decimal_ull(p, ' ', cputime64_to_clock_t(nice));q_put_decimal_ull(p, ' ', cputime64_to_clock_t(system));q_put_decimal_ull(p, ' ', cputime64_to_clock_t(idle));q_put_decimal_ull(p, ' ', cputime64_to_clock_t(iowait));q_put_decimal_ull(p, ' ', cputime64_to_clock_t(irq));q_put_decimal_ull(p, ' ', cputime64_to_clock_t(softirq));q_put_decimal_ull(p, ' ', cputime64_to_clock_t(steal));q_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest));q_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest_nice));q_putc(p, '\n');for_each_online_cpu(i) {/* copy values here to work around gcc-2.95.3, gcc-2.96 */ur = kcpustat_cpu(i).cpustat[cputime_ur];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];q_printf(p, "cpu%d", i);q_put_decimal_ull(p, ' ', cputime64_to_白百合花clock_t(ur));q_put_decimal_ull(p, ' ', cputime64_to_clonortheastck_t(nice));q_put_decimal_ull(p, ' ', cputime64_to_clock_t(system));q_put_decimal_ull(p, ' ', cputime64_to_clock_t(idle));q_put_decimal_ull(p, ' ', cputime64_to_clock_t(iowait));q_put_decimal_ull(p, ' ', cputime64_to_clock_t(irq));q_put_decimal_ull(p, ' ', cputime64_to_clock_t(softirq));q_put_decimal_ull(p, ' ', cputime64_to_clock_t(steal));q_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest));q_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest_nice));q_putc(p, '\n');}q_printf(p, "intr %llu", (unsigned long long)sum);/* sum again ? it could be updated? */for_each_irq_nr(j)q_put_decimal_ull(p, ' ', kstat_irqs_usr(j));q_printf(p,"\nctxt %llu\n""btime %lu\n""process %lu\n""procs_running %lu\n""procs_blocked %lu\n",nr_context_switches(),(unsigned long)jif,total_forks,nr_running(),nr_iowait());q_printf(p, "softirq %llu", (unsigned long long)sum_softirq);for (i = 0; i < nr_softirqs; i++)q_put_decimal_ull(p, ' ', per_softirq_sums[i]);q_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 q_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);} elkfree(buf);return res;}static const struct file_operations proc_stat_operations = {.open= stat_open,.read= q_read,.llek= q_lek,.relea= single_relea,};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使用率内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章,希望大家以后多多支持www.887551.com!

本文发布于:2023-04-05 20:33:00,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/ad9d6ed40b704e935f6e10379089f79b.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:Linux中计算特定CPU使用率案例详解.doc

本文 PDF 下载地址:Linux中计算特定CPU使用率案例详解.pdf

标签:使用率   情况   系统   时刻
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图