一个top命令不就行了么?顶多再加一些管道什么的过滤一下。我一开始也是这么想得。其实还可以理解的更多。

首先一个问题,是统计某个时间点的CPU负载,还是某个时间段的?

为了画折线图报表,一般横坐标都是某个时间点,也就是希望能够统计某个时间点的CPU负载,但这是很难办得到的。比较容易的做法是通过两个时间点之间的CPU负载,也就是某个时间段。如果要做benchmark,就把时间段变得很小,1秒甚至更小。如果要常规监控, 可以将时间段放大到1分钟,甚至更多。

第二个问题,用什么来判断某个时间段的CPU的负载?

CPU有一个基本时间度量单位叫做jiffy,这是一个很短的时间,具体时常多少取决与硬件。不过关系不大,对于我的计算负载达到百分之多少来讲已经够用了。

下面这篇文章http://www.linuxhowtos.org/System/procstat.htm介绍了介绍了 介绍了/proc/stat文件。里面指的关注的是:

1. 第一行CPU的数值是下面几个CPU数值的总和

2. 一行7个数字的分别解释:

The meanings of the columns are as follows, from left to right:

user: normal processes executing in user mode
nice: niced processes executing in user mode
system: processes executing in kernel mode
idle: twiddling thumbs
iowait: waiting for I/O to complete
irq: servicing interrupts
softirq: servicing softirqs

然后这篇讨论贴给出计算公式,
http://stackoverflow.com/questions/3017162/how-to-get-total-cpu-usage-in-linux-c

e.g. Suppose at 14:00:00 you have

cpu 4698 591 262 8953 916 449 531

total_jiffies_1 = (sum of all values) = 16400

work_jiffies_1 = (sum of user,nice,system = the first 3 values) = 5551

and at 14:00:05 you have

cpu 4739 591 289 9961 936 449 541

total_jiffies_2 = 17506

work_jiffies_2 = 5619

So the %cpu usage over this period is:

work_over_period = work_jiffies_2 - work_jiffies_1 = 68

total_over_period = total_jiffies_2 - total_jiffies_1 = 1106

%cpu = work_over_period / total_over_period * 100 = 6.1%

很容易理解。最后算出来的小数 * 100后就是百分数。

在我的机器上,一共10列。

 cat /proc/stat
cpu 2065552 1692 636745 10842974 59979 16 6860 0 0 0
cpu0 524690 552 158305 2701823 8912 7 4808 0 0 0
cpu1 511203 670 157274 2703792 31404 1 1179 0 0 0
cpu2 519169 441 155591 2720326 11179 0 438 0 0 0
cpu3 510489 27 165574 2717032 8482 7 435 0 0 0

在man 5 proc中回车,输入/proc/stat后再次回车进行查找,看到

       /proc/stat
kernel/system statistics. Varies with architecture. Common entries include: cpu 3357 0 4313 1362393
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 spent in user mode,
user mode with low priority (nice), system mode, and the idle task, respectively. The last value should be USER_HZ times the second entry in the uptime pseudo-file. In Linux 2.6 this line includes three additional columns: iowait - time waiting for I/O to complete (since 2.5.41); irq - time servicing interrupts (since 2.6.0-test4); softirq - time
servicing softirqs (since 2.6.0-test4). Since Linux 2.6.11, there is an eighth column, steal - stolen time, which is the time spent in other operating systems when running in a virtualized environment Since Linux 2.6.24, there is a ninth column, guest, which is the time spent running a virtual CPU for guest operating systems under the control of the Linux kernel.

这里解释了

第8个是虚拟机环境下,其他OS偷走的时间。

第9个是如果是host机器,那么运行的guest VM用去的时间。

这些信息也是很有用的。毕竟现在不少server其实只是VM而已。

Linux CPU 负载度量公式的更多相关文章

  1. Linux CPU负载状态:%us/%sy/%ni/%id/%wa/%hi/%si/%st含义

    原文 Linux CPU负载状态:%us/%sy/%ni/%id/%wa/%hi/%si/%st含义 缙哥哥发现用了雅黑的探针,在 Linux 的 CPU 状态信息中发现,有“%us.%sy.%ni. ...

  2. Linux CPU负载

    昨天查看Nagios警报信息,发现其中一台服务器CPU负载过重,机器为CentOS系统.信息如下: 2011-2-15 (星期二) 17:50 WARNING - load average: 9.73 ...

  3. 理解Linux CPU负载和 CPU使用率

    CPU负载和 CPU使用率 这两个从一定程度上都可以反映一台机器的繁忙程度. cpu使用率反映的是当前cpu的繁忙程度,忽高忽低的原因在于占用cpu处理时间的进程可能处于io等待状态但却还未释放进入w ...

  4. Linux系统排查2——CPU负载篇

    本随笔介绍CPU负载的排查手段. 查看系统负载的工具:uptime,w,都能查看系统负载,系统平均负载是处于运行或不可打扰状态的进程的平均数, 可运行:运行态,占用CPU,或就绪态,等待CPU调度. ...

  5. 浅谈Linux下CPU利用率和CPU负载【转】

    转自:https://blog.csdn.net/Alisa_xf/article/details/71430406 在Linux/Unix下,CPU利用率(CPU utilization)分为用户态 ...

  6. Linux系统排查——CPU负载篇

    本随笔介绍CPU负载的排查手段. 查看系统负载的工具:uptime,w,都能查看系统负载,系统平均负载是处于运行或不可打扰状态的进程的平均数, 可运行:运行态,占用CPU,或就绪态,等待CPU调度. ...

  7. linux里的CPU负载

    昨天查看Nagios警报信息,发现其中一台服务器CPU负载过重,机器为CentOS系统.信息如下: 2011-2-15 (星期二) 17:50 WARNING - load average: 9.73 ...

  8. Linux如何查看CPU负载

    负载(load)是Linux机器的一个重要指标,直观了反应了机器当前的状态.如果机器负载过高,那么对机器的操作将难以进行. linux的负载高,主要是由于CPU使用.内存使用.IO消耗三部分构成.任意 ...

  9. LINUX内核CPU负载均衡机制【转】

    转自:http://oenhan.com/cpu-load-balance 还是神奇的进程调度问题引发的,参看Linux进程组调度机制分析,组调度机制是看清楚了,发现在重启过程中,很多内核调用栈阻塞在 ...

随机推荐

  1. js页面跳转 和 js打开新窗口 方法

    js页面跳转 和 js打开新窗口 方法 第一种: 第二种: 第三种: 第四种: 第五种: 1.在原来的窗体中直接跳转用 window.location.href="你所要跳转的页面" ...

  2. BZOJ 1189: [HNOI2007]紧急疏散evacuate( BFS + 二分答案 + 匈牙利 )

    我们可以BFS出每个出口到每个人的最短距离, 然后二分答案, 假设当前答案为m, 把一个出口拆成m个表示m个时间, 点u到出口v的距离为d, 那么u->v的[d, m]所有点连边, 然后跑匈牙利 ...

  3. MVC数据提交

    关于请求方式(form表单) .form的几个属性 <form name="input" action="http://www.baidu.com" me ...

  4. python 中文异常问题记录

    头上加入以下内容试试: # -*- coding:utf-8import sysimport osreload(sys)sys.setdefaultencoding( "utf-8" ...

  5. centos6安装PHP5.4

    安装的命令行很简单 sudo yum --enablerepo=remi install php 不过如果你没有配置源Repository,就需要首先启动REMI源: wget http://rpms ...

  6. 高可用mysql集群搭建

    对web系统来说,瓶颈大多在数据库和磁盘IO上面,而不是服务器的计算能力.对于系统伸缩性我们一般有2种解决方案,scale-up(纵向扩展)和scale-out(横向扩展).前者如扩内存,增加单机性能 ...

  7. 重温 Win32 API ----- 截屏指定窗体并打印

    朋友说在一个VC++6.0开发的项目中要增加打印窗体的功能,让帮忙写个代码供其调用. 这么老的IDE当然不想碰了,并且也不喜欢MFC笨拙不清晰的封装.所以决定採用纯Win32 API,然后用C++类简 ...

  8. 80x86汇编小站站长简单介绍

    [人生格言] 1] 一生都用头脑而不是情绪解决这个问题 2] 仅仅有偏执狂才会成功 3] 在最困难时都要保持一份幽默感 4] 吾生也有涯,而知也无涯,以有涯随无涯,殆已 [简历] 我的生日: 1981 ...

  9. Sql server 事务 存储过程

    事务( Transaction )是并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位. 通过事务,SQL Server能将逻辑相关的一组操作绑定在一起, ...

  10. UIButton 动态改变文本闪烁问题

    当动态改变(比如一秒改变一次)按钮的Title的时候发现按钮每次都要闪烁一下:解决方法如下: self.settleButton.titleLabel.text = title; [self.sett ...