Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Calculate CPU utilization     - CentOS 6.6 running level (Linux)

- Java object initialization (Programming)

- To install the Git and Github under Ubuntu (Linux)

- How Linux Log Analysis (Linux)

- Linux for enterprises to build a firewall (Linux)

- A new method for Linux hidden files (Linux)

- RHEL7 system making use of OpenStack mirror (Linux)

- Quickly locate the mistakes by gdb location (Programming)

- How to Set Free SSH password on CentOS / RHEL (Linux)

- Everyone should know something about TCP (Linux)

- PostgreSQL vacuum principle of a function and parameters (Database)

- OpenGL Programming Guide (8th edition of the original book) - compute shader (Programming)

- Java Concurrency - processes and threads (Programming)

- Mac OS X system setup Google Go language development environment configuration tool Sublime Text 2 (Linux)

- Linux C source code (Ascii HexToBinary: Converts hexadecimal string format ASCII codes) (Programming)

- Spring classic face questions Share (Programming)

- Linux Creating a new user error Creating mailbox file: File exists (Linux)

- Linux initialization init system - UpStart (Linux)

- Nonstandard IMP-00010 error processing one case (Database)

- CentOS iptables firewall configuration (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:      
 
- Github Getting Started Basic Course (Linux)
- Linux kernel compilation, the configuration of the motor drive (Programming)
- MySQL Study of --Percona Server 5.5 Upgrade 5.6 (Database)
- Getting Started with Linux system to learn: how to check the version of SSH on Linux (Linux)
- Installation in lxml Python module (Linux)
- Use Linux firewall camouflage defense hacked (Linux)
- Android start automatically and add and delete a desktop shortcut (Programming)
- Use Bash script write CVS version control (Server)
- Struts2 dynamic call DMI and error Solution (Programming)
- CentOS modify yum update source (Linux)
- How to make a U disk to install Ubuntu (Linux)
- Ubuntu cut screen method (Linux)
- Large computer network security policy Experience (Linux)
- Caffe + Ubuntu 14.04 64bit + CUDA 6.5 configuration instructions (Linux)
- 10 practical Java programming technology (Programming)
- Linux, Firefox unloading and installation and upgrade (Linux)
- Installation on Ubuntu class Winamp audio player Qmmp 0.9.0 (Linux)
- Vagrant failed to start, stuck in Waiting for VM to boot solution (Linux)
- GitLab upgrade to 8.2.0 (Linux)
- Python type way of comparison (Programming)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.