Skip to content

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

License

Notifications You must be signed in to change notification settings

vivaladav/cpu-stat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Development

To build cpu-stat you need CMake or any IDE that can handle CMake files.

simple build

This will build cpu-stat in debug mode:

$ mkdir build
$ cd build
$ cmake ..
$ make

build in release mode

This will build cpu-stat in release mode:

$ mkdir build
$ cd build
$ cmake -DCMAKE_BUILD_TYPE=Release ..
$ make

install release build

This will install cpu-stat in /usr/local/bin/ after a build is completed.

$ cd build
$ sudo make install

clean release build

This will remove all files generated by a build:

$ cd build
$ make clean

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.