Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Calculate CPU utilization     - Ubuntu How to install Pacman (Linux)

- How to write a new Git protocol (Linux)

- Manage SQL Server services login (start) account and password (Database)

- MySQL enabled SSD storage (Database)

- How to network to share files between Windows, MAC and Linux (Linux)

- Difference LVS three scheduling modes (Server)

- Installation under Linux Mint system guidelines for Gtk (Linux)

- CentOS 7 How to install MySQL Server (Database)

- Through the source code to install MySQL 5.6.26 under CentOS6 (Database)

- Open source backup software installed on Ubuntu Systemback 1.6.201 (Linux)

- Necessity in Java packages (Programming)

- Create, modify, delete users, user groups under linux (Linux)

- OpenVPN offsite interconnecting room availability and load balancing solution (Server)

- Linux (Ubuntu) How iptables port mapping (Server)

- Batch kill processes using awk command (Linux)

- Boot automatically remove Linux operating history records (Linux)

- Ubuntu 14.04 Configuring cuda-convnet (Linux)

- WEB-based Android Remote Tools Python implementation (Programming)

- Linux disk partition batch format mount script (Linux)

- CentOS7 yum install third-party source EPEL (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:      
 
- Java 8 Lambda principle analysis (Programming)
- SQL Server 2012 failover looksalive check and is alive check (Database)
- To setup a ftp server under Linux (Server)
- Oracle lag () and lpad () function (Database)
- Effect MongoDB plan cache (Database)
- Linux set to select the appropriate level of security of the network according to deployment (Linux)
- MySQL EXPLAIN SQL output description (Database)
- Android Studio commonly used shortcuts and how to follow the Eclipse Shortcuts (Linux)
- The Linux disguised as windows to make the system more secure (Linux)
- Python Basics: Search Path (Programming)
- Linux System Getting Started Tutorial: How to Force Change your password at next logon Linux (Linux)
- JavaScript is implemented without new keywords constructor (Programming)
- Linux installation beautify early experience (Linux)
- YUM install desktop environment in CentOS (Linux)
- Linux Oracle delete archived logs (Database)
- Build Python3.4 + PyQt5.3.2 + Eric 6.0 development platform Ubuntu 14.04 (Server)
- Mac OS X 10.9 build Nginx + MySQL + php-fpm environment (Server)
- systemd Power Management (Linux)
- Ubuntu / Fedora / CentOS system how to install Plex Media Server 0.9.9 (Linux)
- Ubuntu disable graphics card (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.