Process utilization in linux

Cpustat – Monitors CPU Utilization by Running Processes in Linux

Cpustat is a powerful system performance measure program for Linux, written using Go programming language. It attempts to reveal CPU utilization and saturation in an effective way, using The Utilization Saturation and Errors (USE) Method (a methodology for analyzing the performance of any system).

It extracts higher frequency samples of every process being executed on the system and then summarizes these samples at a lower frequency. For instance, it can measure every process every 200ms and summarize these samples every 5 seconds, including min/average/max values for certain metrics.

Cpustat outputs data in two possible ways: a pure text list of the summary interval and a colorful scrolling dashboard of each sample.

How to Install Cpustat in Linux

You must have Go (GoLang) installed on your Linux system in order to use cpustat, click on the link below to follow the GoLang installation steps that is if you do not have it installed:

Once you have installed Go, type the go get command below to install it, this command will install the cpustat binary in your GOBIN variable:

How to Use Cpustat in Linux

When the installation process completes, run cpustat as follows with root privileges using the sudo command that is if your controlling the system as a non-root user, otherwise you’ll get the error as shown:

Note: To run cpustat as well as all other Go programs you have installed on your system like any other commands, include GOBIN variable in your PATH environment variable. Open the link below to learn how to set the PATH variable in Linux.

This is how cpustat works; the /proc directory is queried to get the current list of process IDs for every interval, and:

  • for each PID, read /proc/pid/stat, then compute difference from previous sample.
  • in case it’s a new PID, read /proc/pid/cmdline.
  • for each PID, send a netlink message to fetch the taskstats, compute difference from previous sample.
  • fetch /proc/stat to get the overall system stats.

Again, each sleep interval is adjusted to account for the amount of time consumed fetching all of these stats. Furthermore, each sample also records the time it took to scale each measurement by the actual elapsed time between samples. This attempts to account for delays in cpustat itself.

When run without any arguments, cpustat will display the following by default: sampling interval: 200ms, summary interval: 2s (10 samples), showing top 10 procs, user filter: all, pid filter: all as shown in the screenshot below:

Cpustat – Monitor Linux CPU Usage

From the output above, the following are the meanings of the system-wide summary metrics displayed before the fields:

  • usr – min/avg/max user mode run time as a percentage of a CPU.
  • sys – min/avg/max system mode run time as a percentage of a CPU.
  • nice – min/avg/max user mode low priority run time as a percentage of a CPU.
  • idle – min/avg/max user mode run time as a percentage of a CPU.
  • iowait – min/avg/max delay time waiting for disk IO.
  • prun – min/avg/max count of processes in a runnable state (same as load average).
  • pblock – min/avg/max count of processes blocked on disk IO.
  • pstart – number of processes/threads started in this summary interval.
Читайте также:  Windows как изменять язык

Still from the output above, for a given process, the different columns mean:

  • name – common process name from /proc/pid/stat or /proc/pid/cmdline.
  • pid – process id, also referred to as “tgid”.
  • min – lowest sample of user+system time for the pid, measured from /proc/pid/stat. Scale is a percentage of a CPU.
  • max – highest sample of user+system time for this pid, also measured from /proc/pid/stat.
  • usr – average user time for the pid over the summary period, measured from /proc/pid/stat.
  • sys – average system time for the pid over the summary period, measured from /proc/pid/stat.
  • nice – indicates current “nice” value for the process, measured from /proc/pid/stat. Higher means “nicer”.
  • runq – time the process and all of its threads spent runnable but waiting to run, measured from taskstats via netlink. Scale is a percentage of a CPU.
  • iow – time the process and all of its threads spent blocked by disk IO, measured from taskstats via netlink. Scale is a percentage of a CPU, averaged over the summary interval.
  • swap – time the process and all of its threads spent waiting to be swapped in, measured from taskstats via netlink. Scale is a percentage of a CPU, averaged over the summary interval.
  • vcx and icx – total number of voluntary context switches by the process and all of its threads over the summary interval, measured from taskstats via netlink.
  • rss – current RSS value fetched from /proc/pid/stat. It is the amount of memory this process is using.
  • ctime – sum of user+sys CPU time consumed by waited for children that exited during this summary interval, measured from /proc/pid/stat.

Note that long running child processes can often confuse this measurement, because the time is reported only when the child process exits. However, this is useful for measuring the impact of frequent cron jobs and health checks where the CPU time is often consumed by many child processes.

  • thrd – number of threads at the end of the summary interval, measured from /proc/pid/stat.
  • sam – number of samples for this process included in the summary interval. Processes that have recently started or exited may have been visible for fewer samples than the summary interval.

The following command displays the top 10 root user processes running on the system:

Find Root User Running Processes

To display output in a fancy terminal mode, use the -t flag as follows:

Running Process Usage of Root User

To view the top x number of processes (the default is 10), you can use the -n flag, the following command shows the top 20 Linux processes running on the system:

You can also write CPU profile to a file using the -cpuprofile option as follows and then use the cat command to view the file:

To display help info, use the -h flag as follows:

Find additional info from the cpustat Github Repository: https://github.com/uber-common/cpustat

That’s all! In this article, we showed you how to install and use cpustat, a useful system performance measure tool for Linux. Share your thoughts with us via the comment section below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

How Do I Find Out Linux CPU Utilization?

W henever a Linux system CPU is occupied by a process, it is unavailable for processing other requests. Rest of pending requests must wait until the CPU is free. This becomes a bottleneck in the system. Following command will help you to identify CPU utilization, so that you can troubleshoot CPU-related performance problems on a Linux-based system. This page explains how to check Linux CPU utilization and usage using various tools.

Читайте также:  Windows update driver fix

Finding CPU utilization is one of the important tasks. Linux comes with various utilities to report CPU utilization. With these commands, you will be able to find out:

  1. CPU utilization
  2. Display the utilization of each CPU individually (SMP cpu)
  3. Find out your system’s average CPU utilization since the last system reboot
  4. Determine which process is eating the CPU(s)

The old good top command to find out Linux CPU Utilization

The top program provides a dynamic real-time view of a running system. It can display system summary information as well as a list of tasks currently being managed by the Linux kernel. The top command monitors CPU utilization, process statistics, and memory utilization. The top section contains information related to overall system status – uptime, load average, process counts, CPU status, and utilization statistics for both memory and swap space.

Top command to find out Linux cpu usage

Type the top command:
$ top

Say hello to htop

htop is similar to top command but allows you to scroll vertically and horizontally and much more.
htop

Find Linux CPU utilization using mpstat and other tools

Please note that you need to install a special package called sysstat to take advantage of following commands. This package includes system performance tools for Linux (Red Hat Linux / RHEL includes these tools by default). Install it on a Debian or Ubuntu Linux using apt-get command/apt command:
# apt-get install sysstat
Use up2date command if you are using RHEL/CentOS Linux v4.x or older:
# up2date install sysstat
Run yum command command if you are using a CentOS/RHEL/Oracle Linux v5.x+ or newer:
# yum install sysstat
Fedora users should run the dnf command:
# dnf install sysstat

Display the utilization of each CPU individually using mpstat

If you are using SMP (Multiple CPU) system, use mpstat command to display the utilization of each CPU individually. It report processors related statistics. For example, type command:
# mpstat
Sample outputs:

The mpstat command display activities for each available processor, processor 0 being the first one. Global average activities among all processors are also reported. The mpstat command can be used both on SMP and UP machines, but in the latter, only global average activities will be printed.:
# mpstat -P ALL
Sample outputs:

Another output from my Ubuntu 18.04 LTS server:

Report CPU utilization using the sar command

You can display today’s CPU activity, with the help of sar command:
# sar
Output:

Comparison of CPU utilization

The sar command writes to standard output the contents of selected cumulative activity counters in the operating system. The accounting system, based on the values in the count and interval parameters. For example display comparison of CPU utilization; 2 seconds apart; 5 times, use:
# sar -u 2 5
Output (for each 2 seconds. 5 lines are displayed):

  • -u 12 5 : Report CPU utilization. The following values are displayed:
    • %user: Percentage of CPU utilization that occurred while executing at the user level (application).
    • %nice: Percentage of CPU utilization that occurred while executing at the user level with nice priority.
    • %system: Percentage of CPU utilization that occurred while executing at the system level (kernel).
    • %iowait: Percentage of time that the CPU or CPUs were idle during which the system had an outstanding disk I/O request.
    • %idle: Percentage of time that the CPU or CPUs were idle and the system did not have an outstanding disk I/O request.

To get multiple samples and multiple reports set an output file for the sar command. Run the sar command as a background process using.
# sar -o output.file 12 8 >/dev/null 2>&1 &
Better use nohup command so that you can logout and check back report later on:
# nohup sar -o output.file 12 8 >/dev/null 2>&1 &

Читайте также:  What is the windows graphical user interface

All data is captured in binary form and saved to a file (data.file). The data can then be selectively displayed ith the sar command using the -f option.
# sar -f data.file

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Task: Find out who is monopolizing or eating the CPUs

Finally, you need to determine which process is monopolizing or eating the CPUs. Following command will displays the top 10 CPU users on the Linux system.
# ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10
OR
# ps -eo pcpu,pid,user,args | sort -r -k1 | less
Sample outputs:

Now you know vmware-vmx process is eating up lots of CPU power. The ps command command displays every process ( -e ) with a user-defined format ( -o pcpu ). First field is pcpu (cpu utilization). It is sorted in reverse order to display top 10 CPU eating process.

iostat command

You can also use iostat command which report Central Processing Unit (CPU) statistics and input/output statistics for devices and partitions. It can be use to find out your system’s average CPU utilization since the last reboot.
# iostat Output:

You may want to use following command, which gives you three outputs every 5 seconds (as previous command gives information since the last reboot): $ iostat -xtc 5 3

vmstat command

The vmstat command shows information about processes, memory, paging, block IO, traps, disks and cpu activity. Run vmstat as follows:
vmstat
vmstat [options] vmstat [interval] [count]
Sample outputs:

In this example, run vmstat with an interval of one second twenty one times:
vmstat 1 21

How to interpret vmstat CPU section output

These are percentages of total CPU time.

  1. us : Time spent running non-kernel code. (user time, including nice time)
  2. sy : Time spent running kernel code. (system time)
  3. id : Time spent idle.
  4. wa : Time spent waiting for IO.
  5. st : Time stolen from a virtual machine.

turbostat command

The turbostat command shows processor topology, frequency, idle power-state statistics, temperature and power on X86 processors. Simply run as follows:
sudo turbostat
sudo turbostat 5
See turbostat man page for further details.

nmon command

nmon is a systems administrator tool to get information about cpu, top process, memory and much more. One can install it as follows:
sudo apt install nmon ## Debain/ubuntu ##
sudo dnf install nmon ## fedora ##
sudo yum install nmon ## centos/rhel ##
Now start it:
nmon

GUI tools for your laptops/desktops

Above tools/commands are quite useful on remote server. For local system with X GUI installed you can try out gnome-system-monitor. It allows you to view and control the processes running on your system. You can access detailed memory maps, send signals, and terminate the processes.
$ gnome-system-monitor
Sample outputs:

In addition, the gnome-system-monitor provides an overall view of the resource usage on your system, including memory and CPU allocation:

Finding out Linux CPU usage using GUI tool

Conclusion

This page explained various Linux command line tools that we can use to find Linux CPU utilization. For further information, see the following resources:

Источник

Оцените статью