Perl obtains cpu usage. 1. First, the/proc/stat file information is displayed in Linux/Unix. The CPU usage is divided into user, system, and idle, the time when the CPU is in the user State, the time when the system kernel is executed, and the time when idle system processes are executed. 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 1144 0cpu0 2175 501 15724 1114163 7094 2153 1144 0 intr 11576005 11430258 11 0 3 0 5 0 1 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 530531 btime 1228361375 processes 6764procs_running 1procs_blocked 0 the output explains the meaning of each parameter in each line of CPU, CPU 0, and CPU 1 (in the first example) description: parameter description: user (432661) indicates the CPU time (unit: jiffie) of the user State from the start of the system to the current time. S), does not contain the nice value as a negative process. 1 jiffies = 0.01 seconds nice (13295) the CPU time occupied by processes with negative nice values (unit: jiffies) from the system startup to the current time. system (86656) from system start to current time, the core time (unit: jiffies) idle (422145968) accumulates from system start 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) accumulates from the system start to the current time, hard interrupt time (unit: jiffies) softirq (5346) accumulates from the system start to the current time, Soft Interrupt time (unit: jiffies) CPU time = user + system + nice + idle + iowait + irq + softirq "intr" indicates the interruption information. The first one is all The number of interruptions. Each number corresponds to the number of interruptions that have 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: 1. First, understand the/proc/stat file information in Linux/Unix. The CPU utilization is divided into user, system, and idle, the time when the CPU is in the user State, the time when the system kernel is executed, and the time when idle system processes are executed. 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 1144 0cpu0 2175 501 15724 1114163 7094 2153 1144 0 intr 11576005 11430258 11 0 3 0 5 0 1 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 530531 btime 1228361375 processes 6764procs_running 1procs_blocked 0 the output explains the meaning of each parameter in each line of CPU, CPU 0, and CPU 1 (in the first example) description: parameter description: user (432661) indicates the CPU time (unit: jiffie) of the user State from the start of the system to the current time. S), does not contain the nice value as a negative process. 1 jiffies = 0.01 seconds nice (13295) the CPU time occupied by processes with negative nice values (unit: jiffies) from the system startup to the current time. system (86656) from system start to current time, the core time (unit: jiffies) idle (422145968) accumulates from system start 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) accumulates from the system start to the current time, hard interrupt time (unit: jiffies) softirq (5346) accumulates from the system start to the current time, Soft Interrupt time (unit: jiffies) CPU time = user + system + nice + idle + iowait + irq + softirq "intr" indicates the interruption information. The first one is all The number of interruptions. Each number corresponds to the number of interruptions that have 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. The instance code [plain] #! /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 );