BSDwiki/ Determine which process are consuming the most CPU

Determine which processes are consuming the most CPU

TODO: is this section header okay? "process is" versus "processes are"

I concur, and had the gall to make the change ;-). Now, do we not also need "cycles" at the end? ---KevinDKinsey

Author: hubertf contact BSD flavour

Reviewer: ceri ceri@FreeBSD.org FreeBSD|OpenBSD

Reviewer: name contact BSD flavour


Concept

Be able to view active processes and recognize inordinate CPU(((CPU))) usage. In addition, know how to end a process or change its priority.

Introduction

There are several programs that allow showing CPU utilization on a Unix system. Some of them can be found on every kind of system, some are specific to others. Here's a list:

Now that we know how to determine general process stats, managing them should be discussed. For that, processes need to be identified, which is done via a process ID (PID)(((PID))) that is unique for each running process on a system. The above programs, with the exception of systat(1)(((systat))), can be used to determine the PID of a running process.

Operations that can be done on processes include:

Examples

Determine the process that takes most CPU:

% ps -aux | head -3
USER      PID %CPU %MEM    VSZ   RSS TTY   STAT STARTED     TIME COMMAND
feyrer   5924 50.0  5.9 104588 30900 ttyp6 R+   12:17AM  0:03.31 qemu -m 64 -bo
root    25528 13.7  0.2    468  1104 ttyp4 R+   12:17AM  0:00.85 /usr/bin/find

Now that we know qemu hogs the CPU, nice it down a bit. Running 'renice' without arguments will show its usage:

% renice
Usage: renice [<priority> | -n <incr>] [[-p] <pids>...] [-g <pgrp>...] [-u <user>...]

Let's say we want to change the nice level of process 5924 (qemu) from the default of 0 to 10:

% renice 10 -p 5924
5924: old priority 0, new priority 10

Upon observation we will still see that the process takes the most CPU:

% ps -aux | head -3
USER      PID %CPU %MEM    VSZ   RSS TTY   STAT STARTED     TIME COMMAND
feyrer   5924 27.1 11.6 104704 60636 ttyp6 RN+  12:17AM  0:31.38 qemu -m 64 -bo
feyrer   1206  1.9 10.8  73896 56304 ?     Ra   11:48AM 75:11.33 /usr/pkg/lib/f

This is because no other process claims the CPU. If another process (e.g. your windowing system, or a compile job) would claim the CPU, the qemu process would relinquish the CPU until no other job needs it.

If the command still uses too much CPU and you are very certain that there is no other way to end it (e.g. by properly ending it; in the case of Qemu by shutting down the system being emulated), it can be killed using the kill(1) command:

% kill 5924

If, for some reason, a process catches the default signal (SIGTERM, 15), a different signal number can be given to the kill(1) command either by name or by signal number that is known to terminate the process unconditionally - be very careful with this:

% kill -9 5924
% kill -KILL 5924

Both of the preceding commands have the same effect; they send the SIGKILL signal to the process with process ID 5924.

Practice Exercises

More information

top(1), systat(1), ps(1), nice(1), renice(1), kill(1), signal(7)