成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Linux中計(jì)算特定CPU使用率案例詳解

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

Linux中計(jì)算特定CPU使用率 需求解決方案拓展參考

需求

在Linux中可以通過(guò)top指令查看某一進(jìn)程占用的CPU情況,也可以查看某一個(gè)CPU使用率情況(先top指令,然后按數(shù)字“1”鍵即可顯示每一個(gè)CPU的使用情況),如下圖:

而我們的需求是:如何得到一個(gè)CPU的占用率呢?

解決方案

1. 背景知識(shí)

在/proc/stat中可以查看每一個(gè)CPU的使用情況的,如下圖:

其中cpu(0/1/2/…)后面的那十個(gè)數(shù)字含義如下:

/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.計(jì)算具體CPU使用率

有了上面的背景知識(shí),接下來(lái)我們就可以計(jì)算具體CPU的使用情況了。具體計(jì)算方式如下:

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%

有了上面的計(jì)算公式,計(jì)算某一CPU使用率或者系統(tǒng)總的CPU占用率也就是不難了。
示例:計(jì)算系統(tǒng)整體CPU占用情況
首先從/proc/stat中獲取 t1時(shí)刻系統(tǒng)總體的user、nice、system、idle、iowait、irq、softirq、steal、guest、guest_nice的值,得到此時(shí)Total CPU time since boot(記為total1)和 Total CPU idle time since boot(記為idle1)。
其次,從/proc/stat中獲取t2時(shí)刻系統(tǒng)總的Total CPU time since boot(記為total2)和Total CPU idle time since boot(記為idle2)。(方法同上一步)
最后,計(jì)算t2t1之間系統(tǒng)總的CPU使用情況。也就是:
CPU percentage between t1 and t2 = ((total2-total1)-(idle2-idle1))/(total2-total1)* 100%
其中, ((total2-total1)-(idle2-idle1))實(shí)際上就是t1與t2時(shí)刻之間系統(tǒng)CPU被占用的時(shí)間(總時(shí)間 - 空閑時(shí)間)。
下面是一段計(jì)算時(shí)間段內(nèi)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

拓展

在內(nèi)核中,關(guān)于/proc/stat中文件的實(shí)現(xiàn)函數(shù)如下:

附注:內(nèi)核版本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);

參考

到此這篇關(guān)于Linux中計(jì)算特定CPU使用率案例詳解的文章就介紹到這了,更多相關(guān)Linux中計(jì)算特定CPU使用率內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持!

成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久
精品国产人成亚洲区| 亚洲欧美日韩中文字幕一区二区三区 | 亚洲欧美日韩专区| 久久亚洲欧美国产精品乐播| 蜜桃视频一区二区三区| 国产精品推荐精品| 中文字幕在线不卡| 99久久久精品| 欧美videossexotv100| 国产乱一区二区| 欧美亚洲一区三区| 性做久久久久久久免费看| 激情综合久久| 久久久久青草大香线综合精品| 亚洲综合在线视频| 国产在线播放一区二区三区| 六月天综合网| 亚洲福利国产精品| 国产亚洲综合精品| 亚洲人成精品久久久久| 国产精品v欧美精品∨日韩| 色噜噜狠狠色综合中国| 亚洲免费在线播放| 亚洲二区视频| 国产精品天天摸av网| 成人免费看的视频| 欧美岛国在线观看| 成人性视频免费网站| 欧美精品丝袜中出| 一区二区三区日韩| 国产一区二区三区无遮挡| 日本一区二区三区四区| 久久国产精品99久久久久久老狼| 欧美日韩p片| 久久精品一区二区三区不卡牛牛| 日韩和欧美一区二区| 亚洲巨乳在线| 日韩三级伦理片妻子的秘密按摩| 亚洲国产综合人成综合网站| 亚洲国产一区二区三区高清| 欧美大度的电影原声| 国产河南妇女毛片精品久久久| 可以免费看不卡的av网站| 亚洲图片有声小说| 日本乱码高清不卡字幕| 日韩精品91亚洲二区在线观看| 亚洲性图久久| 一色桃子久久精品亚洲| 国产精品国产一区二区| 中文字幕五月欧美| 野花国产精品入口| 亚洲成va人在线观看| 蜜桃久久av| 日本va欧美va精品| 8x8x8国产精品| a亚洲天堂av| 7777精品伊人久久久大香线蕉超级流畅 | 亚洲一区二区成人| 日本美女一区二区三区视频| 色美美综合视频| 韩国欧美国产1区| 精品国精品国产| 国产91精品欧美| 久久中文字幕电影| 亚洲高清毛片| 日产国产欧美视频一区精品 | 洋洋av久久久久久久一区| 亚洲激情精品| 亚洲成人手机在线| 欧美精品高清视频| 欧美sm重口味系列视频在线观看| 精品国产91乱码一区二区三区 | 91福利资源站| 成人禁用看黄a在线| 国产精品无人区| 男人的天堂成人在线| 国产乱人伦精品一区二区在线观看 | 欧美视频亚洲视频| 亚洲综合无码一区二区| 欧美在线观看一区| 欧美在线影院| 爽爽淫人综合网网站| 678五月天丁香亚洲综合网| 91麻豆123| 亚洲第一二三四区| 欧美一级理论片| 国产黑丝在线一区二区三区| 国产蜜臀av在线一区二区三区| 国产精品国产三级欧美二区| 午夜精品免费在线| 久久综合九色综合欧美就去吻| 岛国精品一区二区| 国产精品传媒视频| 亚洲麻豆av| 久久99精品久久久久久国产越南| 欧美老肥妇做.爰bbww视频| 色综合久久综合网97色综合| 悠悠色在线精品| 性娇小13――14欧美| 国产精品一级黄| 亚洲日本va午夜在线电影| 欧美群妇大交群的观看方式| 国产精品mm| 精品一区二区三区免费观看 | 在线电影欧美成精品| 午夜久久tv| 久久国产福利国产秒拍| 国产精品久久久久一区二区三区共 | 欧美亚州韩日在线看免费版国语版| 蜜桃一区二区三区在线观看| 久久亚洲精品小早川怜子| 欧美亚洲一区| 不卡的电视剧免费网站有什么| 中文子幕无线码一区tr| 色噜噜狠狠色综合欧洲selulu| 国产乱码精品一区二区三区忘忧草| 91精品国产色综合久久不卡电影| 成人国产亚洲欧美成人综合网| 久久久亚洲国产美女国产盗摄| 黄色综合网站| 国产一区二区三区美女| 日韩理论电影院| 日韩欧美国产三级| 日本道精品一区二区三区| 国产91精品免费| 亚洲国产视频一区二区| 久久日韩精品一区二区五区| 欧美日韩国产成人精品| 最新日韩在线视频| 8v天堂国产在线一区二区| 中文精品视频| 经典三级在线一区| 中文字幕一区不卡| 欧美xxx久久| 欧洲av一区二区嗯嗯嗯啊| 影音欧美亚洲| 国产91对白在线观看九色| 中文字幕+乱码+中文字幕一区| 国产视频久久| 日韩视频一区二区在线观看| 国产精品久久久对白| 久久久久国产免费免费| 亚洲丝袜精品丝袜在线| 欧美影视一区在线| 91国偷自产一区二区开放时间| 国模精品娜娜一二三区| 欧美日韩国产综合视频在线| 成人av资源站| av中文字幕在线不卡| 欧美影视一区在线| 国产精品免费一区二区三区在线观看 | 久久高清免费观看| 暖暖成人免费视频| 国产高清视频一区| 亚洲成人在线观看视频| 中文字幕色av一区二区三区| 久久久久久久久一| 国产精品久久久久久亚洲伦| 666欧美在线视频| 日韩一级在线| 欧美呦呦网站| 成人黄色电影在线| 福利电影一区二区三区| 精品一区免费av| 午夜电影一区二区三区| 亚洲午夜激情av| 精品国产一区二区精华| 欧美性猛交xxxx黑人交| 久久精品日产第一区二区| 亚洲一区日韩在线| 国产欧美日韩一级| 合欧美一区二区三区| 国内精品**久久毛片app| 琪琪久久久久日韩精品| 中文字幕精品一区二区精品绿巨人| 欧美亚洲国产一区二区三区va| 欧美日韩亚洲在线| 99re免费视频精品全部| 成人性视频免费网站| 丰满亚洲少妇av| 日韩高清一级片| 亚洲电影第三页| 国产日韩欧美精品综合| 欧美亚洲自拍偷拍| 色婷婷久久久久swag精品| 日本道免费精品一区二区三区| 在线免费日韩片| 亚洲国产日韩欧美| 亚洲经典三级| 99久久国产综合精品女不卡| 高清成人免费视频| 成人av电影在线网| 欧美精品免费观看二区| 亚洲国产一区二区三区在线播| 99久久久精品免费观看国产蜜| 日韩精品成人一区二区在线| 日韩福利视频导航| 亚洲三级在线看| 亚洲视频图片小说| 亚洲香肠在线观看|