How to use the command 'sar' (with examples)
- Linux
- December 25, 2023
The ‘sar’ command is a powerful tool for monitoring and analyzing performance on Linux systems. It provides detailed information about various subsystems such as I/O, network, CPU, memory, and more. With the help of ‘sar’, system administrators can easily identify bottlenecks and optimize system performance.
Use case 1: Report I/O and transfer rate issued to physical devices, one per second
Code:
sar -b 1
Motivation: This example is useful when you want to monitor the I/O activity and transfer rates of physical devices on your system. This information helps in understanding the workload on the system and identifying potential performance issues related to disk I/O.
Explanation:
-b
: Specifies the option to report I/O and transfer rate statistics.1
: Specifies the interval in seconds between each report.
Example output:
Linux 5.4.0-53-generic (hostname) 12/03/21 _x86_64_ (4 CPU)
08:31:20 bread/s bwrd/s bdsk/s sps/s sec/s
08:31:21 10.00 128.00 200.00 24.00 2.00
08:31:22 10.00 256.00 500.00 50.00 4.00
08:31:23 10.00 374.00 564.00 56.00 5.00
...
Use case 2: Report a total of 10 network device statistics, one per 2 seconds
Code:
sar -n DEV 2 10
Motivation: This example is useful when you want to monitor network device statistics on your system. It helps in understanding network utilization, identifying potential network bottlenecks, and analyzing network traffic patterns.
Explanation:
-n DEV
: Specifies the option to report network device statistics.2
: Specifies the interval in seconds between each report.10
: Specifies the total number of reports to be generated.
Example output:
Linux 5.4.0-53-generic (hostname) 12/03/21 _x86_64_ (4 CPU)
08:35:02 IFACE rxpck/s txpck/s rxkbr/s txkbr/s rxcmp/s txcmp/s rxmcst/s %ifutil
08:35:04 loo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
08:35:04 ens33 10.00 20.00 2.00 8.00 0.00 0.00 0.00 0.00
08:35:04 ens35 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
...
Use case 3: Report CPU utilization, one per 2 seconds
Code:
sar -u ALL 2
Motivation: This example is useful when you want to monitor CPU utilization on your system. It provides insights into the system’s workload and helps in identifying CPU bottlenecks or underutilization.
Explanation:
-u ALL
: Specifies the option to report CPU utilization statistics for all CPUs.2
: Specifies the interval in seconds between each report.
Example output:
Linux 5.4.0-53-generic (hostname) 12/03/21 _x86_64_ (4 CPU)
09:40:34 CPU %user %nice %system %iowait %steal %idle
09:40:36 all 0.00 0.00 2.99 0.00 0.00 97.01
09:40:36 0 0.00 0.00 3.00 0.00 0.00 97.00
09:40:36 1 0.00 0.00 3.00 0.00 0.00 97.00
...
Use case 4: Report a total of 20 memory utilization statistics, one per second
Code:
sar -r ALL 1 20
Motivation: This example is useful when you want to monitor memory utilization on your system. It helps in identifying memory usage patterns, potential memory leaks, and overall system performance related to memory.
Explanation:
-r ALL
: Specifies the option to report memory utilization statistics for all memory resources.1
: Specifies the interval in seconds between each report.20
: Specifies the total number of reports to be generated.
Example output:
Linux 5.4.0-53-generic (hostname) 12/03/21 _x86_64_ (4 CPU)
10:15:18 kbmemfree kbavail %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty
10:15:19 8290964 9135416 79.61 577620 14610048 19252064 117.43 13179748 9334272 120
10:15:20 9054324 9898524 75.94 577620 14583776 19252064 117.43 13108176 9080256 120
...
Use case 5: Report the run queue length and load averages, one per second
Code:
sar -q 1
Motivation: This example is useful when you want to monitor the run queue length and load averages on your system. It helps in understanding the level of concurrency and system load, which can be useful for capacity planning and understanding application performance.
Explanation:
-q
: Specifies the option to report run queue length and load averages.1
: Specifies the interval in seconds between each report.
Example output:
Linux 5.4.0-53-generic (hostname) 12/03/21 _x86_64_ (4 CPU)
10:30:17 runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15 blocked
10:30:18 11 267 0.02 0.03 0.00 0
10:30:19 11 267 0.00 0.02 0.00 0
...
Use case 6: Report paging statistics, one per 5 seconds
Code:
sar -B 5
Motivation: This example is useful when you want to monitor paging statistics on your system. It helps in understanding the system’s memory management and analyzing the impact of paging on overall system performance.
Explanation:
-B
: Specifies the option to report paging statistics.5
: Specifies the interval in seconds between each report.
Example output:
Linux 5.4.0-53-generic (hostname) 12/03/21 _x86_64_ (4 CPU)
11:45:11 pgpgin/s pgpgout/s fault/s majflt/s pgfree/s pgscank/s pgscand/s pgsteal/s %vmeff
11:45:16 0.62 5.16 0.22 0.00 4.68 0.00 0.00 0.00 0.00
11:45:21 0.40 1.81 0.00 0.00 0.41 0.00 0.00 0.00 0.00
...
Conclusion:
The ‘sar’ command is a versatile tool for monitoring various subsystems on a Linux system. By providing detailed performance statistics, it enables system administrators to diagnose and optimize system performance. Whether it’s monitoring I/O activity, network statistics, CPU utilization, memory usage, run queue length, or paging behavior, ‘sar’ is a valuable asset in understanding system performance.