日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

Linux中計算特定CPU使用率案例詳解

瀏覽:355日期:2022-06-03 11:15:20

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:     user nice system idle iowait  irq  softirq steal guest guest_nicecpu  4705 356  584    3699   23    23     0       0     00cpu0 1393280 32966 572056 13343292 6130 0 17875 0 23933 0   The amount of time, measured in units of USER_HZ   (1/100ths of a second on most architectures, use   sysconf(_SC_CLK_TCK) to obtain the right value), that   the 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 value  should 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.  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 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 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 = 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)。(方法同上一步)
最后,計算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=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=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/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];	else		idle = 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];	else		iowait = 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);	} else		kfree(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);

參考

到此這篇關于Linux中計算特定CPU使用率案例詳解的文章就介紹到這了,更多相關Linux中計算特定CPU使用率內容請搜索以前的文章或繼續瀏覽下面的相關文章,希望大家以后多多支持!

日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
国产精区一区二区| 日韩精品一区第一页| 免费成人在线观看| 国产日韩电影| 国产成人免费av一区二区午夜| 欧美日韩亚洲一区二区三区在线 | 日韩av网站在线免费观看| 日本高清久久| 久久国产尿小便嘘嘘| 久久精品日韩欧美| 群体交乱之放荡娇妻一区二区| 精品视频在线你懂得| 日本久久精品| 99综合视频| 青青草精品视频| 久久只有精品| 色婷婷亚洲mv天堂mv在影片| 久久亚洲专区| 日韩国产欧美三级| 国产精品13p| 男人操女人的视频在线观看欧美| 日本aⅴ亚洲精品中文乱码| 日韩综合一区| 特黄特色欧美大片| 婷婷亚洲成人| 久久国产亚洲精品| 欧美午夜三级| 欧美精品一区二区三区精品| 亚洲精品乱码日韩| 国产精品久久久久久久免费观看 | 精品三区视频| 蘑菇福利视频一区播放| 日本午夜精品| 九色porny丨国产首页在线| 中文字幕亚洲影视| 私拍精品福利视频在线一区| 天堂俺去俺来也www久久婷婷| 日韩在线观看一区二区| 蜜臀91精品一区二区三区| 日韩亚洲精品在线| 亚洲一区欧美激情| 亚洲精品一二| 久久国产日韩欧美精品| 国产欧美日韩视频在线| 久久wwww| 精品三级av| 日韩欧美另类一区二区| 亚洲二区在线| 性一交一乱一区二区洋洋av| 亚洲资源在线| 国产日韩精品视频一区二区三区| 国产亚洲欧美日韩精品一区二区三区 | 亚洲激情五月| 日韩欧美一区二区三区在线观看| 亚洲a一区二区三区| 欧美网站在线| 日本中文字幕一区二区视频| 欧美黑人巨大videos精品| 中文字幕在线免费观看视频| 秋霞影视一区二区三区| 欧美网站在线| 国产欧美视频在线| 欧美日韩一区二区综合| 亚洲精品三级| 午夜精品成人av| 亚洲小说春色综合另类电影| 色天使综合视频| 亚欧洲精品视频在线观看| 国产va免费精品观看精品视频| 国产精品7m凸凹视频分类| 97久久亚洲| 亚洲精品123区| 国产麻豆精品久久| 国产亚洲欧洲| 激情国产在线| 91成人在线| 国产精品视区| 日韩欧美国产精品综合嫩v| 欧美一区网站| 欧美午夜不卡| 精品久久视频| 久久最新视频| 国模 一区 二区 三区| 精品日产乱码久久久久久仙踪林| 激情91久久| 久久视频一区| 中国字幕a在线看韩国电影| 欧美亚洲三区| 色综合视频一区二区三区日韩| 激情六月综合| 日本欧美不卡| 日韩在线观看| av中文资源在线资源免费观看| 蜜桃91丨九色丨蝌蚪91桃色| 欧洲在线一区| 久久精品国语| 国产成人免费av一区二区午夜| 国产麻豆精品| 欧美偷窥清纯综合图区| 在线看片日韩| 日韩三区四区| 国产日本久久| 国产精东传媒成人av电影| 午夜精品影视国产一区在线麻豆| 国产亚洲在线| 亚洲伊人精品酒店| 日本在线观看不卡视频| 亚洲欧美久久久| 91成人精品视频| 五月天久久网站| 亚洲欧美日韩国产一区二区| 激情欧美一区| 首页国产欧美久久| 久久亚洲美女| 欧美日一区二区三区在线观看国产免| 日本亚洲欧美天堂免费| 国产精品最新自拍| 国产不卡人人| 激情综合网站| 人人精品人人爱| 国产精品久久久网站| а√天堂8资源中文在线| 久久中文在线| 久久精品国语| 天堂久久av| 国产精品久久久久久久久久10秀| 美女免费视频一区| 国产精品久久久久av电视剧| 国产综合激情| 97久久超碰| 日韩精品网站| 日韩av午夜在线观看| 日韩国产专区| 亚洲理论在线| 日韩影院二区| 久久国际精品| 亚洲综合不卡| 国产精品一区二区三区www| 亚洲精品一区三区三区在线观看| 日韩免费精品| 欧美日韩在线二区| 日本不卡的三区四区五区| 亚洲深夜视频| 国产精品久久久久久久久久齐齐| 美女毛片一区二区三区四区| 国产毛片精品久久| 免费观看久久av| 欧美国产另类| 日韩一二三区在线观看| www成人在线视频| 国产精品一区二区99| 国产亚洲精品久久久久婷婷瑜伽| 久久国产精品美女| 午夜久久美女| 欧美日韩国产观看视频| 国产日韩欧美三区| 欧美日韩日本国产亚洲在线| 精品三区视频| 88久久精品| 亚洲一区有码| 亚洲免费成人| 欧美日韩精品免费观看视欧美高清免费大片 | 国产精品一区二区中文字幕| 一区免费视频| 激情欧美丁香| 久久久精品久久久久久96| 久久久久观看| 国产精品免费不| 国产欧美一区二区三区精品观看| 在线亚洲观看| 国产高清久久| 中国女人久久久| 亚洲作爱视频| 免费在线观看日韩欧美| 欧美在线综合| 欧美日韩精品一区二区视频| 国产欧美日韩视频在线| 欧美日韩黄网站| 麻豆视频观看网址久久| 国产一区二区三区久久久久久久久| 麻豆精品99| 亚洲免费福利| 亚洲欧洲另类| 最近国产精品视频| 国产日韩一区| 粉嫩av一区二区三区四区五区| 一区二区精品伦理...| 蜜臀久久99精品久久一区二区| 黄色欧美日韩| 五月国产精品| 久久精品福利| 91精品国产乱码久久久久久久| 久久不见久久见免费视频7 | 免费在线观看一区| 精品理论电影在线| 99视频精品全国免费| 蜜臀av在线播放一区二区三区| 91成人在线精品视频| 91日韩在线| 亚州欧美在线|