Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Calculate CPU utilization     - Build and verify MongoDB3.0.7 version (shard + replica) Cluster (Database)

- Linux common network tools: ping host sweep (Linux)

- The AWK use Cygwin tools mysql binlog log viewer (Database)

- Use matplotlib scientific drawing in Linux (Linux)

- Commonly used Linux system camouflage method (Linux)

- How to install Bugzilla 4.4 on Ubuntu / CentOS 6.x (Linux)

- how to quickly find a file on Linux system (Linux)

- How to Install Cantata MPD 1.3.3 for Ubuntu and Derived Version Users (Linux)

- Django template inheritance child (Programming)

- Wireshark basic introduction and learning TCP three-way handshake (Linux)

- Android Studio and Git Git configuration file status (Linux)

- The difference between Linux su and sudo commands (Linux)

- MySQL TIMESTAMP and DATETIME (Database)

- Arronax allows you to easily create desktop startup file (Linux)

- Install Unity 8 preview version of the desktop in Ubuntu (Linux)

- PXE + Kickstart automatically install CentOS 6.5 (Linux)

- BusyBox build root file system (Linux)

- By way of a binary installation innobackupex (Database)

- GlusterFS distributed storage deployment (Server)

- Efficient Linux Shell - Shell special characters Summary (Linux)

 
         
  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:      
 
- WEB-based Android Remote Tools Python implementation (Programming)
- Digital jQuery scrolling effect (Programming)
- Ubuntu 14.04 LTS 64-bit installation and activation Sublime Text 3 can not solve the Chinese input method to solve the problem (Linux)
- U disk to install Windows and Ubuntu 15.04 dual system (Programming)
- VMware 8 installation configuration Win7, CentOS-7 Wizard (Linux)
- GAMIT10.5 under CentOS installation (Linux)
- Preliminary understanding of SELinux security management (Linux)
- Analysis of Java reflection mechanism (Programming)
- How to install Ubuntu applications Device 7 (Linux)
- Java implementation file encryption and decryption (Programming)
- Linux operating system set up to effectively prevent ARP attacks (Linux)
- Git build a team development environment operating drills (Linux)
- Report generation CPU, memory, and input and output ports with the sar command (Linux)
- Nagios (centreon) monitoring Linux Log (Server)
- 256 with rich colors decorate your terminal (Linux)
- Linux, modify the fstab file system can not start causing solve one case (Linux)
- Oracle set and remove columns unavailable (Database)
- Oracle Shared Server Configuration (Database)
- Turning off the interface eth0: error: Disconnect the device 'eth0' (Linux)
- FastDFS installation and deployment (Server)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.