Use/proc/STAT in Linux to calculate CPU usage

Source: Internet
Author: User

Generally, for processes that require a large amount of CPU computing, the higher the front-end pressure, the higher the CPU utilization. However, for I/O network-intensive processes, even if there are many requests, the CPU of the server may not be very good. At this time, the Service bottleneck is generally on the disk I/O. In a long view, the CPU overhead for frequent reading and writing of large files is much smaller than that for frequent reading and writing of small files. When the I/O throughput is certain, the reading and writing of small files is more frequent, and more CPUs are needed to handle I/O interruptions.

In Linux/Unix, CPU utilization is divided into user-state, system-state, and idle state, indicating the time when the CPU is in user-state, and the time when the system kernel is executed, and the execution time of idle system processes. CPU utilization usually refers to the time when the CPU executes non-system idle processes/The total execution time of the CPU.

In the Linux kernel, there is a global variable: jiffies. Jiffies indicates the time. Its unit varies with the hardware platform. A constant Hz is defined in the system, representing the number of minimum time intervals per second. In this way, the unit of jiffies is 1/Hz. Jiffies of Intel Platform is measured in 1/100 seconds, which is the minimum time interval that the system can distinguish. For each CPU time slice, jiffies must be added with 1. CPU utilization is expressed by dividing the execution user State + system state jiffies by the total jifffies.

In Linux, you can use the/proc/STAT file to calculate the CPU usage (for more information, see: http://www.linuxhowtos.org/System/procstat.htm ). This file contains information about all CPU activities. All values in this file are accumulated from the start of the system to the current time.

For example:

Quote:
[Sailorhzr @ builder ~] $ CAT/proc/STAT
CPU 432661 13295 86656 422145968 171474 233
Cpu0 123075 2462 23494 105543694 0 16586
Cpu1 111917 4124 23858 105503820 69697 123
Cpu2 103164 3554 21530 105521167 64032 106 334
Cpu3 94504 3153 17772 105577285 4 24
Intr 1065711094 1057275779 92 0 6 0 4 0 0 0 0 0 0 70 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0
Ctxt 1, 19067887
(Btime 1139187531)
Processes 270014
Procs_running 1
Procs_blocked 0



Output description
The meanings of each parameter in each line of CPU and cpu0, cpu1, cpu2, and cpu3 (in the first behavior example) are:

Parameter description
User (432661) indicates the CPU time (unit: jiffies) of the user State from the start of the system to the current time. The nice value is not a negative process. 1 jiffies = 0.01 seconds
Nice (13295) indicates the CPU time occupied by processes whose nice value is negative (unit: jiffies) from the time the system starts to the current time)
System (86656) accumulates from system startup to current time, core time (unit: jiffies)
Idle (422145968) accumulates from system startup to current time, except hard disk Io wait time (unit: jiffies)
Iowait (171474) accumulates from system startup to current time, hard disk Io wait time (unit: jiffies ),
IRQ (233) indicates the hard interrupt time (unit: jiffies) from the start of the system to the current time)
Softirq (5346) indicates the Soft Interrupt time (unit: jiffies) from the start of the system to the current time)

CPU time = user + system + nice + idle + iowait + IRQ + softirq

The "intr" line shows the number of interruptions. The first line is the number of all interruptions that have occurred since the system was started; then each number corresponds to the number of times a specific interruption has occurred since the system was started.
"Ctxt" indicates the number of times the CPU context has been exchanged since the system was started.
"Btime" indicates the time (in seconds) that has elapsed since the system was started.
The number of tasks created since the system started "processes (total_forks.
"Procs_running": number of tasks in the current queue.
"Procs_blocked": Number of blocked tasks.

The following two methods can be used for CPU utilization. Take two sampling points and calculate the difference value:

Quote:
CPU usage = (idle2-idle1)/(cpu2-cpu1) * 100
CPU usage = [(user_2 + sys_2 + nice_2)-(user_1 + sys_1 + nice_1)]/(total_2-total_1) * 100



The following uses bash and Perl to calculate the CPU usage:

Note: The following code uses the formula:

Quote:
Total_0user [0] + nice [0] + system [0] + idle [0] + iowait [0] + IRQ [0] + softirq [0]
Total_1 = user [1] + nice [1] + system [1] + idle [1] + iowait [1] + IRQ [1] + softirq [1]
CPU usage = (idle [0]-idle [1])/(total_0-total_1) * 100



### Bash code (provided by myself. available)

Code :#! /Bin/sh

# Echo user nice system idle iowait IRQ softirq
Cpulog_1 = $ (CAT/proc/STAT | grep 'cpu '| awk' {print $2 "" $3 "" $4 "" $5 "" $6 "" $7 "" $8 }')
Sys_idle_1 = $ (echo $ cpulog_1 | awk '{print $4 }')
Total_1 = $ (echo $ cpulog_1 | awk '{print $1 + $2 + $3 + $4 + $5 + $6 + $7 }')

Sleep 5

Cpulog_2 = $ (CAT/proc/STAT | grep 'cpu '| awk' {print $2 "" $3 "" $4 "" $5 "" $6 "" $7 "" $8 }')
Sys_idle_2 =$ (echo $ cpulog_2 | awk '{print $4 }')
Total_2 =$ (echo $ cpulog_2 | awk '{print $1 + $2 + $3 + $4 + $5 + $6 + $7 }')

Sys_idle = 'expr $ sys_idle_2-$ sys_idle_1'

Total = 'expr $ total_2-$ total_1'
Sys_usage = 'expr $ sys_idle/$ total * 100 | BC-l'

Sys_rate = 'expr 100-$ sys_usage | BC-l'

Disp_sys_rate = 'expr "scale = 3; $ sys_rate/1" | bc'
Echo $ disp_sys_rate %

### Perl code (available)

#! /Usr/bin/perl

Use warnings;

$ Sleeptime = 5;

If (-e "/tmp/STAT "){
Unlink "/tmp/STAT ";
}
Open (jiff_tmp, ">/tmp/STAT") | die "can't open/proc/STAT file! \ N ";
Open (jiff, "/proc/STAT") | die "can't open/proc/STAT file! \ N ";
@ Jiff_0 = <jiff>;
Print jiff_tmp $ jiff_0 [0];
Close (jiff );

Sleep $ sleeptime;

Open (jiff, "/proc/STAT") | die "can't open/proc/STAT file! \ N ";
@ Jiff_1 = <jiff>;
Print jiff_tmp $ jiff_1 [0];
Close (jiff );
Close (jiff_tmp );

@ User = 'awk' {print \ $2} '"/tmp/STAT "';
@ Nice = 'awk' {print \ $3} '"/tmp/STAT "';
@ System = 'awk' {print \ $4} '"/tmp/STAT "';
@ Idle = 'awk' {print \ $5} '"/tmp/STAT "';
@ Iowait = 'awk' {print \ $6} '"/tmp/STAT "';
@ IRQ = 'awk' {print \ $7} '"/tmp/STAT "';
@ Softirq = 'awk' {print \ $8} '"/tmp/STAT "';

$ Jiff_0 = $ user [0] + $ nice [0] + $ system [0] + $ idle [0] + $ iowait [0] + $ IRQ [0] + $ softirq [0];
$ Jiff_1 = $ user [1] + $ nice [1] + $ system [1] + $ idle [1] + $ iowait [1] + $ IRQ [1] + $ softirq [1];

$ Sys_idle = ($ idle [0]-$ idle [1]) // ($ jiff_0-$ jiff_1) * 100;
$ Sys_usage = 100-$ sys_idle;

Printf ("the CPU usage is % 1.2f % \ n", $ sys_usage );


Original article address
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.