Linux cpu usage Calculation

Source: Internet
Author: User
Article Title: Linux system cpu usage calculation. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

The cpu usage in Linux is not as intuitive as that in windows Task Manager. Top and vmstat are one way to view cpu utilization.

The following describes how to calculate the cpu usage for the monitoring view.

[Root @ Centos5 admin] # more/proc/stat

Cpu 494881706 19 67370877 876689477 17202366 200116 0

Cpu0 96177278 1 13815598 71114795 780840 160297 0

Cpu1 55637645 2 7822250 116484226 2099600 5076 0

Cpu2 55163466 2 7460892 116965069 2441533 6266 0

Cpu3 59323490 2 7755772 112506339 2445624 6001 0

Cpu4 57541559 2 7651880 114524670 2321514 5737 0

Cpu5 56891878 3 7629496 115132164 2386230 5592 0

Cpu6 56887733 3 7617390 115174187 2355772 5791 0

Cpu7 57258654 1 7617597 114788022 2371250 5351 0

This is an official explanation of the cpu's first line of data:

The number of jiffies (1/100ths of a second) that the system spent in user mode, user mode with low priority (nice), system mode, and the idle task, respectively. the last value shocould be 100 times the second entry in the uptime pseudo-file.

Parameters:

The first row is the total cpu usage. Below are two 4-core cpu parameters, so there are 8 rows of data.

User (494881706) indicates the CPU time (unit: jiffies) of the user State from the start of the system to the current time. The nice value is not a negative process. 1 jiffies = 0.01 seconds

Nice (19) CPU time occupied by processes whose nice value is negative from system startup to current time (unit: jiffies)

System (67370877) accumulates from system startup to current time, core time (unit: jiffies)

Idle (876689477) accumulates from system startup to current time, except hard disk IO wait time (unit: jiffies)

Iowait (17202366) accumulates from system startup to current time, hard disk IO wait time (unit: jiffies ),

Irq (200116) indicates the hard interrupt time (unit: jiffies) from the start of the system to the current time)

Softirq (0) accumulates from the system startup to the current time, and the soft interrupt time (unit: jiffies)

Technical script for CPU utilization:

This script continuously shows the cpu usage.

#! /Bin/bash

While (true)

Do

CPU_1 = $ (cat/proc/stat | grep 'cpu '| awk' {print $2 "" $3 "" $4 "" $5 "" $6 "" $7 "" $8 }')

SYS_IDLE_1 = $ (echo $ CPU_1 | awk '{print $4 }')

Total01 = $ (echo $ CPU_1 | awk '{printf "%. f ", $1 + $2 + $3 + $4 + $5 + $6 + $7 }')

Sleep 2

CPU_2 = $ (cat/proc/stat | grep 'cpu '| awk' {print $2 "" $3 "" $4 "" $5 "" $6 "" $7 "" $8 }')

SYS_IDLE_2 =$ (echo $ CPU_2 | awk '{print $4 }')

Total_2 = $ (echo $ CPU_2 | awk '{printf "%. f ", $1 + $2 + $3 + $4 + $5 + $6 + $7 }')

SYS_IDLE = 'expr $ SYS_IDLE_2-$ SYS_IDLE_1'

Total = 'expr $ Total_2-$ Total01'

TT = 'expr $ SYS_IDLE \ * 100'

SYS_USAGE = 'expr $ TT/$ total'

SYS_Rate = 'expr 100-$ SYS_USAGE'

Echo "The CPU Rate: $ SYS_Rate %"

Echo "------------------"

Done

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.