Pages

Wednesday, February 1, 2017

How to Calculate CPU Usage and What are the States of CPU

List of Topics

1) How to Calculate CPU Usage
2) What is CPU Time
3) States of CPU

1) How to Calculate CPU Usage

Normally in Operation System CPU usage is calculated based on CPU time. 

2) What is CPU time

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 5 of the 10 CPU slices. 5/10 = .50 = 50% of busy time (and there would therefore be 40% idle time).


enter image description here

Will explain in breif with the help sample top command output

Top Output  : Cpu(s):  0.7%us,  0.7%sy,  0.0%ni, 98.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st

In the above line 0.7% is the CPU time in user space. CPU time is calculated based on ticks. For eg: 1 sec = 100 ticks then out of 100 ticks 0.7 is used for user programs. similarly 0.7 for sytem process and 98%7 for idle 

In case if a process is showing usage more than 100 then we need to check the number of cores and based on that we need to calculate it.

Since CPUs operate in GHz (billionths of cycles a second). The operating system slices that time in smaller units called ticks. They are not really 1/10 of a second. The tick rate in windows is 10 million ticks in a second and in Linux it is sysconf(_SC_CLK_TCK) (usually 100 ticks per second).

In something like top, the busy cpu cycles are then further broken down into percentages of things like user time and system time. In top on Linux and perfmon in Windows, you will
often get a display that goes over 100%, that is because the total is 100% * the_number_of_cpu_cores.

In an operating system, it is the scheduler's job to allocate these precious slices to processes, so the scheduler is what reports this.

3) States of CPU

CPU time is occupied  by one of the below stated of CPU, Below are the states of CPU with some explanation.

a) idIdle, which means it has nothing to do
b) usRunning a user space program, like a command shell, an email server, or a 
c) sy - Running the kernel, servicing interrupts or managing resources.
d) ni - shows the amount of CPU spent running user space processes that have been niced. When no processes have been niced then the number will be 0.
e) wa - Input and output operations, like reading or writing to a disk, are slow compared to the speed of a CPU. Although this operations happen very fast compared to everyday human activities, they are still slow when compared to the performance of a CPU. There are times when the processor has initiated a read or write operation and then it has to wait for the result, but has nothing else to do. In other words it is idle while waiting for an I/O operation to complete. The time the CPU spends in this state is shown by the wa statistic.
f) hi & si - These two statistics show how much time the processor has spent servicing interrupts. hi is for hardware interrupts, and si is for software interrupts. Hardware interrupts are physical interrupts sent to the CPU from various peripherals like disks and network interfaces. Software interrupts come from processes running on the system. A hardware interrupt will actually cause the CPU to stop what it is doing and go handle the interrupt. A software interrupt doesn't occur at the CPU level, but rather at the kernel level.
g) st - This last number only applies to virtual machines. When Linux is running as a virtual machine on a hypervisor, the st (short for stolen) statistic shows how long the virtual CPU has spent waiting for the hypervisor to service another virtual CPU running on a different virtual machine. Since in the real-world these virtual processors are sharing the same physical processor(s) then there will be times when the virtual machine wanted to run but the hypervisor scheduled another virtual machine instead.

No comments:

Post a Comment