Linux CPU使用率采集程序编写

引言(Introduction):从基础概念出发,手把手抒写Linux CPU采集程序,并在后面的一个章节里简述经常出现的问题。本文主要环境以centos 7 上进行的一系列研究,其他版本Linux可能会有些出路,但总体思路不变。

1 基础概念(Basic Concept)

1.1 Linux /proc/stat文件介绍 (Linux /proc/stat File Introduce)

        在Linux系统中,可以用/proc/stat文件来计算cpu的利用率。这个文件包含了所有CPU活动的信息,该文件中的所有值都是从系统启动开始累计到当前时刻(换言之,当系统重启,计数器将重置为0)。

        通过"file"命令可知,该文件并非普通文件,该文件为内核缓冲区文件。而该文件的数据是实时变化的,每次打开,内容都存在差异,下面我将带领大家看该文件内容结合http://www.linuxhowtos.org/manpages/5/proc.htm,并给予对应说明。

        前面几列以cpu开头的每个逻辑cpu的单独情况,从序号0开始,往后递增,而第一行,无序号的,为总情况,这些数据都是以clock_tick为单位记录的,这里的clock_tick在绝大多数机器上是1/100秒即10ms。对于特定的机器可以通过sysconf(_SC_CLK_TCK)函数来获得clock_tick(ps: 这个函数属于linux内核中unistd.h文件中提供的方法,运行前需包含该库文件);即前五行所表示的是:总CPU使用情况,和该机器的4颗逻辑CPU的分别情况。其他的具体的每一列信息为:

    cpu user nice system idle iowait irq softirq steal guest guest_nice

前五行具体说明(有时候未必是前五行,根据具体CPU逻辑数而定):

        user: 用户态的CPU时间。(Time spent in user mode)

        nice: 低优先级程序所占用的用户态的cpu时间。(Time spent in user mode with low priority(nice))

        system: 系统态的CPU时间。(Time spent in system mode)

        idle: CPU空闲的时间,不包含IO等待。(Time spent in the idle task. This value should be USER_HZ times the second entry in the/proc/uptime pseudo-file.)

        iowait: 等待IO响应的时间。(Time waiting for I/O to complete. This value is not reliable.)

        irq: 处理硬件中断的时间。(Time servicing interrupts)

        softirq: 处理软中断的时间。(Time servicing softirqs)

        steal: 其他系统所花的时间。(Stolen time, which is the time spent in other operating systems when running in a virtualized environment)

        guest: 运行时间为客户操作系统下的虚拟CPU控制。(Time spent running a virtual CPU for guest operating systems under the control of the Linux kernel)

        guest_nice: 低优先级程序所占用的用户态的cpu时间。(Time spent running a niced guest ,virtual CPU for guest operating systems under the control of the Linux kernel)

后面七行的具体说明:

        intr:系统启动以来的所有interrupts的次数情况,每个数对应一个特定的中断自系统启动以来所发生的次数,可参考/proc/interrupt。(This line shows counts of interrupts serviced since boot time, for each of the possible system interrupts. The first column is the total of all interrupts serviced including unnumbered architecture specific interrupts; each subsequent column is the total for that particular numbered interrupt. Unnumbered interrupts are not shown, only summed into the total.)

        ctxt: 系统上下文切换次数。(The number of context switches that the system underwent.)

        btime:启动时长(单位:秒),从Epoch(即1970零时)开始到系统启动所经过的时长,每次启动会改变。(boot time, in seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).)

        processes:系统启动后所创建过的进程数量。(Number of forks since boot.)

        procs_running:处于Runnable状态的进程个数.(Number of processes in runnable state. (Linux 2.5.45 onward.))

        procs_blocked:处于等待I/O完成的进程个数。(Number of processes blocked waiting for I/O to complete.)

        softirq:软中断情况,第一列表示所有CPU的软中断总和,其他的几列表示特定的软中断情况,具体参考/proc/softirq(This line shows the number of softirq for all CPUs. The first column is the total of all softirqs and each subsequent column is the total for particular softirq.)

1.2 CPU计算公式 (CPU Compute Formula)

        

      

说明:

        /proc/stat时间一直在累积,所以可以在该秒获取该文件内容后,sleep 1000 下一秒再获取该文件内容,按照对应公式,即可得出该秒内CPU的平均时间情况。

2 实现说明 (Implement)

  按照第一章节说明,实际上我们只需要读取第一行数据即可。

按照公式,拼接redis命令,单纯存储总时间和空闲时间,key为对应的时间,value为总时间_空闲时间。

获取前一秒的数据和当前秒的数据,带入该函数,获得前一秒到该秒时间段的平均CPU利用率。

3 可能存在的问题说明 (Possible Problems)

3.1 重启后导致CPU利用率不准确 (Reboot Make CPU Percent Incorrect)

    说明:当linux重启后,CPU计数器置0,导致正常的公式无法被使用。

    解决方法:判断时间更早的点总CPU时间是否小于现在的点的总CPU时间,如果不是,则剔除该点数据。

3.2 快照恢复后导致CPU利用率不准确 (Snapshot Make CPU Percent Incorrect)

    说明:快照恢复后实际做了两件事:重启+时间同步宿主机,前者按照3.1的处理,时间同步必须保证整个集群时间一致,否作会出现存入了01:00、01:02、01:03....02:00的数据,快照执行完又回到了01:00的点开始采集数据,且数据CPU时间无限小(因为重启了),导致带入公式计算01:00到02:00利用率存在问题。

    解决方法:计算01:00到02:00该分钟的利用率,将总CPU时间相减,是否约等于1分钟,如果不约等于1分钟,则该时间段数据弃用。

4 展示图