Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Calculate CPU utilization     - Linux network monitoring strategy (Linux)

- CentOS installation Docker series (Linux)

- Docker knowledge base (Server)

- String JavaScript type system (Programming)

- Web database security tips (Linux)

- Install FFmpeg compiling from source in Mac OS X environment (Linux)

- Oracle Incident Packaging Service (Database)

- Java abstract class instantiation (Programming)

- A key installation Gitlab 7 on RHEL6.4 and Setup Mail TX (Linux)

- How to modify the Linux NIC eth1 to eth0 (Linux)

- Depth understanding of C language (Programming)

- Quota for Vsftpd do use disk quotas (Server)

- Free compiler install MySQL-5.6.14 (Database)

- Linux novice common commands (Linux)

- Installation and deployment of Hadoop 2.7.1 on Ubuntu 14.04 LTS (Server)

- Learning how to teach safety system to prevent your own IP leakage (Linux)

- Android WebView use layman (Programming)

- Nginx introduced Dynamic Module Architecture (Server)

- Use XtraBackup be physical standby database MySQL (Database)

- The direct insertion sort algorithm (Programming)

 
         
  Calculate CPU utilization
     
  Add Date : 2018-11-21      
         
         
         
  For general process requires a lot of computing cpu, the greater the pressure at the end, CPU utilization, the higher the current. But for the I / O network-intensive process, even if many requests, CPU server is not necessarily to, when the service is usually the bottleneck in disk I / O on. More common is, cpu overhead frequently read and write large files is much less than the cost of reading and writing small files frequently. Because the I / O throughput is constant, the more frequent reading and writing small files, requiring more cpu to handle I / O interrupts.

In Linux / Unix, CPU utilization is divided into user mode, the system state and the idle state, respectively, CPU time in user mode execution time, the execution time of the system kernel, and system idle process execution. CPU utilization is usually said means:
CPU to perform non-system idle process time / CPU total execution time.

In the Linux kernel, there is a global variable: Jiffies. Jiffies represents time. Its units with different hardware platforms and different. The system defines a constant HZ, the minimum number of time intervals per second representative, this value can be modified in the kernel compile time. Such jiffies unit is 1 / HZ. Intel platform jiffies unit is 1/100 seconds, which is the minimum time interval of the system can distinguish. Jiffies here to 1/100 second example. Each CPU time slice, Jiffies be incremented. CPU utilization is to use user-mode execution Jiffies + system state divided by the total Jifffies to represent.

In the Linux system, you can use / proc / stat file to calculate the cpu utilization. This file contains all the information CPU activity, all the values ​​in the file system is started from the beginning to the current accumulated time.
Such as:
[Test @ pc1 ~] $ cat / proc / stat
cpu 432661 13295 86656 422145968 171474 233 5346
cpu0 123075 2462 23494 105543694 16586 0 4615
cpu1 111917 4124 23858 105503820 69697 123 371
cpu2 103164 3554 21530 105521167 64032 106 334
cpu3 94504 3153 17772 105577285 21158 4 24
intr 1065711094 1057275779 92 0 6 6 0 4 0 3527 0 0 0 70 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0,000,000,000,000,000,000 0,000,000,000,000,000,000 0 0 0 0 0 0 0 0 0 0 0 0 0,000,000,000,000,000,000 0,000,000,000,000,000,000 0 0 0 0 0 0 0 0 0 0 0 0 0,000,000,000,000,000,000 0,000,000,000,000,000,000 0,007,376,958,000,000 0,105,460,200,000,003,000 0,000,000,000,000,000,000
ctxt 19067887
btime 1139187531
processes 270014
procs_running 1
procs_blocked 0

Output Interpreter
CPU and CPU0, CPU1, CPU2, CPU3 meaning of each parameter of each line (the first line as an example) as follows:
Parameter Explanation
user (432661) starting from the current system to start accruing time, user mode CPU time (unit: jiffies), does not contain a nice value is negative process.
nice (13295) start start accruing to the current time, nice value is negative processes from the system CPU time (unit: jiffies)
system (86656) from the start to the current system starts accumulating time, core time (unit: jiffies)
idle (422145968) start from the system to begin accumulating present moment, in addition to other hard disk IO wait time Wait Time (Unit: jiffies)
iowait (171474) starting from the current system to start accruing time, hard disk IO wait time (unit: jiffies)
irq (233) from the system start to the present time to begin accumulating hard interruption time (unit: jiffies)
softirq (5346) starting from the current system to start accruing time, soft interrupt time (unit: jiffies)

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

"Intr" This line gives interruptions, first of all the number of interrupts since the system started happening; then each number corresponds to a specific number of interrupts since the system was started happening.
"Ctxt" given the number of context switching system since the start of the CPU occur.
"Btime" gives the start until the present time from the system in seconds.
"The number of tasks since the system was started to create processes (total_forks).
"Procs_running": the number of tasks currently running queue.
"Procs_blocked": the number of tasks currently being blocked.


Then CPU utilization can use the following two methods. First take two samples and then calculate the difference:
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 calculation using a cpu utilization respectively bash and perl to do:

total_0 = USER [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 achieve

 

 
#! / 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 1
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 achieve

#! / Usr / bin / perl
use warnings;
$ SLEEPTIME = 5;
if (-e "/ tmp / stat") {
    unlink "/ tmp / stat";
}
open (JIFF_TMP, ">> / tmp / stat") || die "Can not open / proc / stat file \ n!";
open (JIFF, "/ proc / stat") || die "! Can not open / proc / stat file \ n";
@ Jiff_0 = ;
print JIFF_TMP $ jiff_0 [0];
close (JIFF);
sleep $ SLEEPTIME;
open (JIFF, "/ proc / stat") || die "! Can not open / proc / stat file \ n";
@ Jiff_1 = ;
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);
     
         
         
         
  More:      
 
- The most common and most effective security settings under linux (Linux)
- Camouflage Nginx Web server version to prevent invasion (Linux)
- Atlassian Linux Shell Scripting the confluence remote backup methods (Linux)
- Five Linux user space debugging tool (Linux)
- CentOS7 virtual machine settings, and bridging problems (Linux)
- SQL Server 2012 failover looksalive check and is alive check (Database)
- Ubuntu dual-card system configuration method (Server)
- Share useful bash aliases and functions (Linux)
- Linux kernel TCP / IP parameters analysis and tuning (Linux)
- Python-- for anomalies and reflection of objects articles (Programming)
- RM Environment Database RMAN Backup Strategy Formulation (Database)
- RedHat Performance Tuning (Server)
- Nginx log cutting and MySQL script regular backup script (Server)
- Summary Linux bond of multi-interface load balancing (Linux)
- Linux operating system boot process analysis (Linux)
- Linux / Raspberry Pi using the following command-line based web browser (Linux)
- Flask installation environment (Linux)
- VirtualBox CentOS is configured as a local disk mirroring software source (Linux)
- Linux program analysis tool: ldd and nm (Linux)
- KVM virtualization nested configuration (Server)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.