Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Calculate CPU utilization     - Bash job control (Linux)

- CentOS 6.5 installation configuration DRBD (Server)

- 5 interesting Linux command line tips (Linux)

- Upgrade Goldengate 11.1.1.1.2 to 11.2.1.0.1 (Database)

- Open SSH remote access service that allows Android phone Ubuntu 14.04 (Linux)

- C ++ Fundamentals study notes (Programming)

- GitLab Installation Guide -Ubuntu 14.04 LTS (Server)

- Laravel 4.2 Laravel5 comprehensive upgrade Raiders (Server)

- Use Android Studio and Gradle packaged multi-version APK (Programming)

- Advanced Linux security settings (Linux)

- Linux user status query, and to unlock locked user (Linux)

- Linux file permissions bit forced bits and adventure Comments (Linux)

- Linux uses the SMTP proxy to send mail (Linux)

- Use Pylint regulate your Python code (Programming)

- 256 with rich colors decorate your terminal (Linux)

- With screenfetch linux logo and basic hardware information display with cool Linux logo (Linux)

- Java NIO2: Buffer (Programming)

- Guide: Trickle restrict application bandwidth usage (Linux)

- How the program is executed (Programming)

- Difference between TCP and UDP protocols (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:      
 
- Android using SVG vector graphics to create cool animation effects (Programming)
- Java inner classes (Programming)
- WordPress blog installation Redis Cache (Server)
- CentOS 5.x install Lua 5.2.3 error (LIBS = -lncurses) (Linux)
- Linux with Windows Explorer as a security system (Linux)
- Install Git on CentOS (Linux)
- CentOS 6.5 can not connect to the network under VMware (Linux)
- Spring multi data source configuration (Programming)
- Linux kernel log --dmesg (Linux)
- Asynchronous communication mechanism between the Android source code analysis thread (Programming)
- Which file system is most suitable for your Linux system (Linux)
- Protect against network attacks using Linux system firewall (Linux)
- xCAT install and update software (Linux)
- How to install Ubuntu strategy game Wesnoth 1.12.0 (Linux)
- Elaborate 10-point difference between the new and malloc (Programming)
- CentOS 6.4 OpenNebula installation (Linux)
- GAMIT 10.50 installed in Ubuntu 12.04 system (Linux)
- How to deploy Icinga client (Server)
- Using LLVM Clang and Blocks under Linux (Programming)
- Nginx version of helloworld (Server)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.