In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the method of calculating the utilization rate of specific CPU in Linux, which has certain reference value and can be used for reference by friends who need it. I hope you will learn a lot after reading this article. Next, let the editor take you to learn about it.
The method of calculating specific CPU utilization in Linux is as follows: first, the overall value of T1 time system is obtained from [/ proc/stat]; then the overall value of T2 time system is obtained from [/ proc/stat]; finally, the total CPU usage of the system between T2 and T1 is calculated.
The method for calculating specific CPU usage in Linux:
1. Background knowledge
You can view the usage of each CPU in / proc/stat, as shown below:
Among them, cpu (0Compact 1Compact 2 / …) The last ten numbers mean as follows:
/ proc/statkernel/system statistics. Varies with architecture. Common entries include: user nice system idle iowait irq softirq steal guest guest_nicecpu 4705 356 584 3699 23 00 0cpu0 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 Time waiting for 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).
two。 Calculate specific CPU utilization
With the above background, 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+stealTotal CPU Idle time since boot = idle+ iowaitTotal CPU usage time since boot = Total CPU time since boot-Total CPU Idle time since bootTotal CPU percentage = Total CPU usage time since boot/Total CPU time since boot * 100%
With the above formula, it is not difficult to calculate a certain CPU utilization or the total CPU utilization of the system.
Example: calculate the overall CPU usage of the system
First of all, the values of user, nice, system, idle, iowait, irq, softirq, steal, guest and guest_nice of T1 time system are obtained from / proc/stat, and Total CPU time since boot (marked as total1) and Total CPU idle time since boot (marked as idle1) are obtained.
Secondly, the total Total CPU time since boot (marked as total2) and Total CPU idle time since boot (marked as idle2) of the T2 time system are obtained from / proc/stat. (the method is the same as the previous step)
Finally, the total CPU usage of the system between T2 and T1 is calculated. That is:
CPU percentage between T1 and T2 = (total2-total1)-(idle2-idle1) / (total2-total1) * 100%
Where ((total2-total1)-(idle2-idle1)) is actually the time that the system CPU is occupied between T1 and T2 (total time-idle time).
The following is a script that calculates the CPU usage over a period of time:
#! / bin/bash# by Paul Colby (http://colby.id.au), no rights reserved;) PREV_TOTAL=0PREV_IDLE=0while true; do # Get the total CPU statistics, discarding the 'cpu' prefix. CPU= (`CPU-n's / ^ sed\ s) 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" # Remember the total and idle CPU times for the next check. PREV_TOTAL= "$TOTAL" PREV_IDLE= "$IDLE" # Wait before checking again. Sleep 1done thank you for reading this article carefully. I hope it will be helpful for everyone to share the method of calculating the utilization rate of specific CPU in Linux. At the same time, I also hope that you will support it, pay attention to the industry information channel, and find out if you encounter problems. Detailed solutions are waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.