How to get percentage of processor use with bash? Read cpu user nice system idle iowait irq softirq steal guest. I always use user + system + nice + idle + iowait == 100%. On some systems, there's also 'steal' which is typically if the OS is running in a VM. Share improve this answer.

Get CPU usage (self.archlinux) submitted 1 year ago by caffeinated-fox. TotalWork = user + nice + system totalTime = totalWork + idle + iowait + irq + softirq permalink; embed; save; give award. CREATE TEMPORARY TABLE test.proc_stat ( seq tinyint auto_increment primary key, the_key char(25) NOT NULL, user bigint, nice bigint, system bigint, idle bigint, iowait bigint, irq bigint, softirq bigint, steal bigint, guest bigint, other bigint ); /* MySQL treats consecutive delimiters as separate fields, so some fancy footwork is required to.

Overview

CPU time is allocated in discrete time slices (ticks). For a certain number of time slices, the cpu is busy, other times it is not (which is represented by the idle process). In the picture below the CPU is busy for 6 of the 10 CPU slices. 6/10 = .60 = 60% of busy time (and there would therefore be 40% idle time).

Note: A tick(cycle) is the time it takes to send a single pulse. A pulse consists of a high voltage followed by a low voltage. There can be billions of ticks per second depending on the frequency(GHz) of the CPU clock.

You can get the number of CPU ticks since boot from /proc/stat

  • user: normal processes executing in user mode
  • nice: niced processes executing in user mode
  • system: processes executing in kernel mode
  • idle: twiddling thumbs
  • iowait: waiting for I/O to complete
  • irq: servicing interrupts
  • softirq: servicing softirqs
  • steal: involuntary wait
  • guest: running a normal guest
  • guest_nice: running a niced guest

Formula

To calculate Linux CPU usage time subtract the idle CPU time from the total CPU time as follows:

Total CPU time since boot = user+nice+system+idle+iowait+irq+softirq+steal

User Nice System Idle Iowait Irq Softirq Steals

Total CPU Idle time since boot = idle + iowait

User Nice System Idle Iowait Irq Softirq Stealer

Total CPU usage time since boot = Total CPU time since boot - Total CPU Idle time since boot

User Nice System Idle Iowait Irq Softirq Stealth

Total CPU percentage = Total CPU usage time since boot/Total CPU time since boot X 100

If you use the formula on the example data above you should get a Linux CPU usage Percentage of 60%.

Note: Guest and Guest_nice are already accounted in user and nice, hence they are not included in the total calculation

Download aaron copland at the river pdf to word. For real time CPU usage, you will need to calculate the time between two intervals.

Below is an example of a Bash Script by Paul Colby that does this

Refhttps://www.kernel.org/doc/Documentation/filesystems/proc.txt section 1.8

Coments are closed
© 2020 - an1mal.netlify.app
Scroll to top