A command line program to get CPU usage statistics on Linux systems.
C++ Python
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
data/imgs Added project logo, first version. Dec 27, 2016
src
.gitignore
LICENSE
README.md
SConstruct

README.md

cpu-stat

A command line program to get CPU usage statistics on Linux systems.

cpu-stat

I created cpu-stat to have a quick and simple answer to the question "what's the CPU usage of my system right now?" and that's what you get when you run the program with no options.

The intended use is for scripts or system monitoring tools which do not want to parse /proc/stat directly to get CPU usage stats.

For people interested in understanding more about how things work under the hood, I wrote a tutorial describing a much simpler version of this program and providing more information on the different CPU states.

NOTE: cpu-stat requires Linux kernel 2.6.33 or newer.

Installation

To build cpu-stat you will need SCons.

simple build

This will build cpu-stat in debug mode. Executable file will be in bin/debug.

scons

build in release mode

This will build cpu-stat in release mode. Executable file will be in bin/release.

scons mode=release

install release build

This will install cpu-stat in /usr/bin/.

sudo scons mode=release install

clean release build

This will remove all files generated by a release build.

sudo scons mode=release -c install

You can omit install at the end to remove only build files and keep the installed executable.

Usage

Print options

flag description default
-a | --all print active time percentage for all CPUs, starting with total false
-c <cpu> print stats only for selected CPU. -
-f | --full-state print time percentage of all states false
-p <precision> set the decimal precision of printed numbers 2
-s <state> print time percentage for specific state [0-9] -
-v | --verbose enable verbose mode false

Other options

flag description default
-d <time> set delay time (in ms) between 2 snapshots of CPU data 100
-h | --help print help -
--version print the version number -

Examples

Print percentage of active time for total CPU:

$ cpu-stat
2.50

Print percentage of active time for all CPUs in verbose mode (with labels):

$ cpu-stat -a -v
tot] 5.00%
  0] 0.00%
  1] 0.00%
  2] 20.00%
  3] 0.00%

Print percentage of idle time (state 3) for CPU 2 using 4 digits as decimal precision:

$ cpu-stat -s 3 -c 2 -p 4
100.0000

Print percentage of all states for all CPUs in verbose mode (with labels):

$ cpu-stat -a -v -f
tot] usr:   0.00% | sys:   0.00% | nic:   0.00% | idl: 100.00% | iow:   0.00% | hir:   0.00% | sir:   0.00% | ste:   0.00% | gue:   0.00% | gun:   0.00% | 
  0] usr:   0.00% | sys:   0.00% | nic:   0.00% | idl: 100.00% | iow:   0.00% | hir:   0.00% | sir:   0.00% | ste:   0.00% | gue:   0.00% | gun:   0.00% | 
  1] usr:   0.00% | sys:   0.00% | nic:   0.00% | idl: 100.00% | iow:   0.00% | hir:   0.00% | sir:   0.00% | ste:   0.00% | gue:   0.00% | gun:   0.00% | 
  2] usr:   0.00% | sys:   0.00% | nic:   0.00% | idl: 100.00% | iow:   0.00% | hir:   0.00% | sir:   0.00% | ste:   0.00% | gue:   0.00% | gun:   0.00% | 
  3] usr:   0.00% | sys:   0.00% | nic:   0.00% | idl:  90.91% | iow:   9.09% | hir:   0.00% | sir:   0.00% | ste:   0.00% | gue:   0.00% | gun:   0.00% |

Contribute

If you want to contribute to cpu-stat check out this guide about how to contribute to an open source project on GitHub first.