Common Linux Statistics CLI Tools
top
Display Linux tasks
Summary
- Press space or enter to force refresh
- Use
hto open help - Use
l,t,mto collapse summary sections - Use
dto modify refresh interval - Use
zto enable color highlighting - Use
uto list processes for specific user - Use
<>to change sort column - Use
Pto sort by CPU usage - Use
Mto sort by resident memory size - Use
Tto sort by cumulative time
Batch Mode
The -b parameter can be used for batch mode, combined with -n parameter to specify number of batches. The -d parameter can specify interval time between batches.
For example, to get current machine load usage, obtaining three times at 0.1 second intervals, getting CPU summary from the last time:
Output Format
top output is divided into two parts: system summary in the top few lines, process list below, separated by a blank line. Here’s sample output from top command:
Summary Section
Summary consists of three parts by default, totaling five lines:
- System runtime, average load, one line total (
ltoggles content) - Tasks, CPU status, one line each (
ttoggles content) - Memory usage, Swap usage, one line each (
mtoggles content)
System Runtime and Average Load
- Current time:
12:11:01 - System uptime:
up 401 days - Number of currently logged in users:
2 users - Average load for the last 5, 10, and 15 minutes:
load average: 1.12, 1.26, 1.40
Load represents operating system load, i.e., number of currently running tasks. Load average represents average load over a period of time, i.e., how many tasks were running on average over a past period. Note that Load is not the same as CPU utilization.
Tasks
The second line shows task or process summary. Processes can be in different states. This shows total number of processes. Additionally, it shows numbers of running, sleeping, stopped, zombie processes (zombie is a process state).
CPU Status
The next line shows CPU status. This displays percentage of CPU time in different modes:
- us, user: CPU time for running (non-priority-adjusted) user processes
- sy, system: CPU time for running kernel processes
- ni, niced: CPU time for running priority-adjusted user processes
- id, idle: Idle CPU time
- wa, IO wait: CPU time spent waiting for IO completion
- hi: CPU time for handling hardware interrupts
- si: CPU time for handling software interrupts
- st: CPU time stolen by hypervisor from virtual machine (if currently in a virtual machine, CPU processing time consumed by host machine)
Memory Usage
- Memory section: total available memory, used memory, free memory, buffer memory
- SWAP section: total, used, free, and buffer swap space
Process Section
Process section displays some key information by default:
PID: Process ID, unique identifier for the process
USER: Actual username of the process owner
PR: Scheduling priority of the process. Some values in this field are ‘rt’, meaning these processes run in real-time state
NI: Nice value (priority) of the process. Smaller values mean higher priority
VIRT: Virtual memory used by the process
RES: Resident memory size. Resident memory is the non-swap physical memory size used by the task
SHR: Shared memory used by the process
S: Process state. It has different values:
D - Uninterruptible sleep state
R – Running state
S – Sleep state
T – Trace or Stop
Z – Zombie state
%CPU: Percentage of CPU time used by task since last update
%MEM: Percentage of available physical memory used by the process
TIME+: Total CPU time used by task since startup, in hundredths of seconds
COMMAND: Command used to run the process
Linux Process States
R (TASK_RUNNING): Runnable state. Both actually running andReadyare considered Running state in LinuxS (TASK_INTERRUPTIBLE): Interruptible sleep state, process waits for events, located in wait queueD (TASK_UNINTERRUPTIBLE): Uninterruptible sleep state, cannot respond to asynchronous signals, e.g., hardware operations, kernel threadsT (TASK_STOPPED | TASK_TRACED): Stopped state or traced state, triggered by SIGSTOP or breakpointsZ (TASK_DEAD): After child process exits, parent process hasn’t cleaned up yet, leavingtask_structureprocesses in this state
free
Display system memory usage
-b | -k | -m | -g | -hcan control units when displaying sizes (bytes, KB, MB, GB, auto-adapt)-scan specify polling interval,-cspecifies polling count
Sample Output
- Here, total memory is 378GB, used 370GB, free 8GB. The three have relationship
total=used+free. Shared memory occupies 36GB. - buffers and cache are allocated and managed by the operating system to improve I/O performance, where Buffer is write buffer and Cache is read cache. This line shows application used
buffers/cachedand theoretically availablebuffers/cache.-/+ buffers/cache: 30338 357153 - The last line shows SWAP information: total SWAP space, actually used SWAP space, and available SWAP space. As long as SWAP isn’t used (used = 0), memory space is still sufficient.
Data Source
free actually gets information through cat /proc/meminfo.
The correspondence between free and /proc/meminfo indicators:
Clear Cache
You can force clear cache with the following commands:
vmstat
Report virtual memory statistics
Summary
Most commonly used:
For example, vmstat 1 10 samples memory statistics 10 times at 1-second intervals.
Sample Output
Data Source
Extracts information from these three files:
/proc/meminfo/proc/stat/proc/*/stat
iostat
Report IO-related statistics
Summary
By default iostat prints CPU and disk IO information. Use -d parameter to show only IO section, use -x to print more information. Sample output:
Common Options
- Use
-dparameter to show only IO section information, while-cparameter shows only CPU section information - Use
-xto print more detailed extended information - Use
-kto use KB instead of block count as unit for some values,-muses MB
Output Description
Without -x option, defaults to printing 5 columns for each device:
- tps: Transfers per second for this device. (Multiple logical requests may be merged into one IO request, transfer amount unknown)
- kB_read/s: Data read from device per second; kB_wrtn/s: Data written to device per second; kB_read: Total data read; kB_wrtn: Total data written; These units are all Kilobytes when using
-kparameter. Default uses block count as unit.
With -x option, prints more information:
- rrqm/s: How many read requests related to this device were merged per second (when system calls need to read data, VFS sends requests to various FS, if FS finds different read requests reading the same Block data, FS will merge these requests)
- wrqm/s: How many write requests related to this device were merged per second
- r/s and w/s: (After merging) read/write requests per second
- rsec/s and wsec/s: Sectors read/written per second
- avgrq-sz: Average request size (in sectors)
- avgqu-sz: Average request queue length
- await: Average time for each IO request processing (in milliseconds)
- r_await/w_await: Average response time for read/write
- %util: Device bandwidth utilization, IO time percentage. All time processing IO during statistics time. Generally when this parameter reaches 100%, device is close to full load operation.
Common Usage
Collect IO information for /dev/dfa, calculate in kB, once per second, 10 consecutive times:
Data Source
Actually extracts information from these files:
