1. First understand the/proc/STAT File Information
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.
This information is stored in the/proc/STAT file,
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. 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. Example:
[Root @ bogon TMP] # Cat/proc/STAT
CPU 2175 501 15724 1114163 7094 2153 0
Cpu0 2175 501 15724 1114163 7094 2153 0
Intr 11576005 11430258 11 0 3 0 5 0 1 0 0 0 0 0 0 107 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 5461 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, 530531
(Btime 1228361375)
Processes 6764
Procs_running 1
Procs_blocked 0
Output description
The meanings of each parameter in each line of CPU, CPU 0, and CPU 1 (in the first behavior example) are as follows:
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:
1. First understand the/proc/STAT File Information
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.
This information is stored in the/proc/STAT file,
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. 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. Example:
[Root @ bogon TMP] # Cat/proc/STAT
CPU 2175 501 15724 1114163 7094 2153 0
Cpu0 2175 501 15724 1114163 7094 2153 0
Intr 11576005 11430258 11 0 3 0 5 0 1 0 0 0 0 0 0 107 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 5461 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, 530531
(Btime 1228361375)
Processes 6764
Procs_running 1
Procs_blocked 0
Output description
The meanings of each parameter in each line of CPU, CPU 0, and CPU 1 (in the first behavior example) are as follows:
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:
2. instance code
#!/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);