Home >Operation and Maintenance >Linux Operation and Maintenance >How to calculate specific CPU usage in Linux

How to calculate specific CPU usage in Linux

coldplay.xixi
coldplay.xixiOriginal
2020-08-22 11:26:393649browse

The method of calculating specific CPU usage in Linux: first obtain the overall value of the system at time t1 from [/proc/stat]; then obtain the total value of the system at time t2 from [/proc/stat]; Finally, calculate the total CPU usage of the system between t2 and t1.

How to calculate specific CPU usage in Linux

Related learning recommendations: linux video tutorial

Method to calculate specific CPU usage in Linux:

1. Background knowledge

You can view each CPU in /proc/stat The usage is as shown below:

How to calculate specific CPU usage in Linux

The ten numbers after cpu (0/1/2/…) have the following meanings:

/proc/stat
kernel/system statistics.  Varies with architecture.  
Common entries include:
     user nice system idle iowait  irq  softirq steal guest guest_nice
cpu  4705 356  584    3699   23    23     0       0     0        0
cpu0 1393280 32966 572056 13343292 6130 0 17875 0 23933 0
   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 ("cpu" line) or the specific CPU ("cpuN"
   line) spent in various states:
   user   (1) Time spent in user mode.
   nice   (2) Time spent in user mode with low priority
          (nice).
   system (3) Time spent in system mode.
   idle   (4) Time spent in the idle task.  This value
          should be USER_HZ times the second entry in the
          /proc/uptime pseudo-file.
   iowait (since Linux 2.5.41)
          (5) Time waiting for I/O to complete.  This
          value is not reliable, for the following rea‐
          sons:
          1. The CPU will not wait for I/O to complete;
             iowait is the time that a task is waiting for
             I/O to complete.  When a CPU goes into idle
             state for outstanding task I/O, another task
             will be scheduled on this CPU.
          2. On a multi-core CPU, the task waiting for I/O
             to complete is not running on any CPU, so the
             iowait of each CPU is difficult to calculate.
          3. The value in this field may decrease in cer‐
             tain conditions.
   irq (since Linux 2.6.0-test4)
          (6) Time servicing interrupts.
   softirq (since Linux 2.6.0-test4)
          (7) Time servicing softirqs.
   steal (since Linux 2.6.11)
          (8) Stolen time, which is the time spent in
          other operating systems when running in a virtu‐
          alized environment
   guest (since Linux 2.6.24)
          (9) Time spent running a virtual CPU for guest
          operating systems under the control of the Linux
          kernel.
   guest_nice (since Linux 2.6.33)
          (10) Time spent running a niced guest (virtual
          CPU for guest operating systems under the con‐
          trol of the Linux kernel).

2. Calculate the specific CPU usage

With the above background knowledge, we can then calculate the specific CPU usage. The specific calculation method is as follows:

Total CPU time since boot = user+nice+system+idle+iowait+irq+softirq+steal
Total CPU Idle time since boot = idle + iowait
Total CPU usage time since boot = Total CPU time since boot - Total CPU Idle time since boot
Total CPU percentage = Total CPU usage time since boot/Total CPU time since boot * 100%

With the above calculation formula, it is not difficult to calculate a certain CPU usage or the total CPU usage of the system.

Example: Calculate the overall CPU usage of the system

First obtain the overall user, nice, system, idle, iowait, irq, softirq, steal, guest of the system at time t1 from /proc/stat , the value of guest_nice, and the Total CPU time since boot (recorded as total1) and Total CPU idle time since boot (recorded as idle1) are obtained.

Secondly, obtain the total Total CPU time since boot (recorded as total2) and Total CPU idle time since boot (recorded as idle2) of the system at time t2 from /proc/stat. (The method is the same as the previous step)

Finally, calculate the total CPU usage of the system between t2 and t1. That is:

CPU percentage between t1 and t2 = ((total2-total1)-(idle2-idle1))/(total2-total1)* 100%

Among them, ((total2-total1)-(idle2-idle1)) is actually the time the system CPU is occupied between t1 and t2 (total time - idle time).

The following is a script that calculates the CPU usage within a period of time:

#!/bin/bash
# by Paul Colby (http://colby.id.au), no rights reserved ;)
PREV_TOTAL=0
PREV_IDLE=0
while true; do
  # Get the total CPU statistics, discarding the 'cpu ' prefix.
  CPU=(`sed -n 's/^cpu\s//p' /proc/stat`)
  IDLE=${CPU[3]} # Just the idle CPU time.
  # Calculate the total CPU time.
  TOTAL=0
  for VALUE in "${CPU[@]}"; do
    let "TOTAL=$TOTAL+$VALUE"
  done
  # Calculate the CPU usage since we last checked.
  let "DIFF_IDLE=$IDLE-$PREV_IDLE"
  let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL"
  let "DIFF_USAGE=(1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10"
  echo -en "\rCPU: $DIFF_USAGE%  \b\b"
  # Remember the total and idle CPU times for the next check.
  PREV_TOTAL="$TOTAL"
  PREV_IDLE="$IDLE"
  # Wait before checking again.
  sleep 1
done

The above is the detailed content of How to calculate specific CPU usage in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more