The SAR described in this article is a very useful tool for analyzing the operation of the system itself under Linux. We know that the program to run on the operating system, the point to focus on is not only memory, CPU and IO (including disk IO and network IO). Before our application runs in the operating system, we need to understand the current usage of the system's memory, CPU and IO, as well as the memory, CPU and IO resources that our application needs to run itself. Only the remaining memory of the operating system, CPU and IO resources can meet the needs of the application, to ensure that the application in the operating system to run properly. SAR is a very handy tool to help us understand the current usage of resources such as memory, CPU, and Io, and the use of SAR is described below through specific examples.
Before we introduce the example, we first need to say the basic usage of the SAR command: SAR [option] [interval] [count]
- 1. Get system memory usage information via SAR
(1) Memory usage statistics:-r, with this option, we can understand the current memory usage in the system
12345678 |
[Email protected]:~/work/test$ sar-r 1 3Linux 2.6.38-8-generic (Ubuntu) 07/09/2011 _i686_ (2 CPU) 06:33:01 PM kbmemfree kb memused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact06:33:02 PM 458664 566488 55.26 96268 265056 1034780 49.95 262500 25488806:33:03 PM 458664 566488 55.26 96268 265056 1034780 49.95 262468 25488806:33:04 PM 458696 566456 55.26 96268 265056 1034780 49.95 262468 254888Average: 458675 566477 55.26 96268 265056 1034780 49.95 262479 254888 |
Take a look at the above example, Sar-r 1 3 means the interval is 1 seconds, statistics 3 times, the last behavior 3 times the mean value.
(2) Page break statistics:-B, through this option, we can understand the current system, the situation of the page exchange
12345678 |
[Email protected]:~/work/test$ sar-b 1 3Linux 2.6.38-8-generic (Ubuntu) 07/09/2011 _i686_ (2 CPU) 06:56:48 PM pgpgin/s pgpgout/s fault/s majflt/s pgfree/s pgscank/s pgscand/s pgsteal/s %vmeff06:56:49 PM 0.00 0.00 1438.00 0.00 3001.00 0.00 0.00 0.00 0.0006:56:50 PM 0.00 0.00 796.00 0.00 1620.00 0.00 0.00 0.00 0.0006:56:51 PM 0.00 0.00 770.00 0.00 1580.00 0.00 0.00 0.00 0.00Average: 0.00 0.00 1001.33 0.00 2067.00 0.00 0.00 0.00 0.00 |
The above is counted every 1 seconds, statistics 3 times the result, the last behavior 3 times the mean value.
The above is about the memory of the more critical two indicators, through the above two options, the system memory usage related to the situation can be clearly at a glance.
- 2. Obtaining information about the CPU usage of the system via SAR
(1) Total CPU usage Statistics:-u, using sar-u [all] [interval] [count], plus all to list all the statistics about the CPU, if not add all, only a few more commonly used indicators:
12345678 |
[Email protected]:~/work/test$ sar-u all 1 3 Linux 2.6.38-8-generic (Ubuntu) 07/09/2011 _i686_ (2 CPU) 07:21:08 PM CPU %usr %nice %sys %iowait %steal %irq %soft %guest %idle07:21:09 PM All 1.01 0.00 1.01 0.00 0.00 0.00 0.00 0.00 97.9807:21:10 PM All 0.50 0.00 1.00 0.00 0.00 0.00 0.00 0.00 98.5107:21:11 PM All 0.99 0.00 0.99 0.00 0.00 0.00 0.00 0.00 98.02Average: All 0.83 0.00 1.00 0.00 0.00 0.00 0.00 0.00 98.17 |
The above is counted every 1 seconds, statistics 3 times after the result, the last behavior 3 times the average.
(2) Separate statistics per CPU:-p, usage of sar-p {CPU [,...] | all} [Interval] [count], where the second parameter can specify the CPU number to view the specified CPU, or specify all to see statistics for all CPUs:
123456789101112131415161718192021222324252627282930 |
[email protected]:~/work/test$ sar-p 0 1 3Linux 2.6.38-8-generic (Ubuntu) 07/09/2011 _i686_ (2 CPU) 07:44:42 PM CPU%user%nice%system%iowait%steal%idle07:44:43 PM 0 1.06 0.00 2.13 0.00 0.00 96.8107:44:44 PM 0 0.00 0.00 0.97 0.00 0.00 99.0307:44:45 PM 0 0.97 0.00 1.94 0.00 0.00 97.09average:0 0.67 0.00 1.67 0.00 0.00 97.67[email protected]:~/work/test$ Sar-p All 1 3Linux 2.6.38-8-generic (Ubuntu) 07/09/2011 _i686_ (2 CP U) 07:46:01 pm CPU%user%nice%system%iowait%steal%idle07:46:02 PM all 1.51 0.00 3.52 0.00 0.00 94.9707:46:02 PM 0 3.00 0.00 6.00 0.00 0.00 91.0 007:46:02 PM 1 0.00 0.00 2.00 0.00 0.00 98.00 07:46:02 PM CPU%user%n Ice%system%iowait %steal%idle07:46:03 pm All 1.01 0.00 1.52 0.00 0.00 97.4707:46:03 PM 0 1.04 0.00 2.08 0.00 0.00 96.8807:46:03 PM 1 0.00 0.00 0.00 0.00 0.0 0 100.00 07:46:03 pm CPU%user%nice%system%iowait%steal%idle07:46:04 PM all 0 .0.00 1.50 0.00 0.00 98.5007:46:04 PM 0 1.00 0.00 3.00 0.00 0.00 96.0007:46:04 PM 1 0.00 0.00 0.00 0.00 0.00 100.00 AVERAGE:CPU%us Er%nice%system%iowait%steal%idleaverage:all 0.84 0.00 2.18 0.00 0.00 96.98average:0 1.69 0.00 3.72 0.00 0.00 94.59average:1 0.00 0.00 0.67 0.00) 0.00 99.33 |
The above is counted every 1 seconds, statistics 3 times after the result, the last behavior 3 times the average. As we can see, the situation of each CPU here is very clearly listed.
With the above two options, the basic usage of the CPU is at a glance.
- 3. Obtaining system IO information via SAR
(1) disk device IO Statistics:-B, which is used to count the IO status of the physical device, mainly disk IO
123456789101112131415 |
[e mail protected]:~/work/test$ sar-b 1 10Linux 2.6.38-8-generic (Ubuntu) 07/09/2011 _i686_ (2 CPU) 07:55:30 PM TPS Rtps Wtps bread/s bwrtn/s07:55:31 PM 0.00 0.00 0.00 0.00 0.0007:55:32 PM 0.00 0.00 0.00 0.00 0.0007:55:33 PM 0.00 0.00 0.00 0.00 0.0007:55:34 PM 0.00 0.00 0.00 0.00 0.0007:55:35 PM 6.00 0.00 6.00 0.00 112.0007:55:36 PM 0.00 0.00 0.00 0.00 0.0007:55:37 PM 0.00 0.00 0.00 0.00 0.0007:55:38 PM 0.00 0.00 0.00 0.00 0.0007:55:39 PM 0.00 0.00 0.00 0.00 0.0007:55:40 PM 10.00 0.00 10.00 0.00 80.00average:1.60 0.00 1.60 0.00 19.20 | /tr>
(2) network device IO Status statistics:-N, usage sar-n {keyword [,...] | All}, keyword includes dev, edev, NFS, NFSD, SOCK, IP, EIP, ICMP, eicmp, TCP, Etcp, UDP, SOCK6, IP6, EIP6, ICMP6, EICMP6 and UDP6, no With the keyword statistics of different aspects, the statistics about network io are mainly using dev to see the read and write on each device:
123456789101112131415161718 |
[email protected]:~/work/test$ sar-n DEV 1 3Linux 2.6.38-8-generic (Ubuntu) 07/09/2011 _i686_ (2 CPU) 08:05:33 PM IFACE rxpck/s txpck/s rxkb/s txkb/s rxcmp/s txcmp/s rxmcst/s08:05:34 PM lo 0.00 0.0 0 0.00 0.00 0.00 0.00 0.0008:05:34 PM eth0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 08:05:34 PM IFACE rxpck/s txpck/s rxkb/s txkb/s rxcmp/s txcmp/s rxmcst/s08:05 : PM Lo 0.00 0.00 0.00 0.00 0.00 0.00 0.0008:05:35 PM eth0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 08:05:35 PM IFACE rxpck/s txpck/s rxkb/s txkb/s rxcmp/s txcmp/s rxmcst/s08:05:36 PM Lo 0.00 0.00 0.00 0.00 0.00 0.00 0.000 8:05:36 PM eth0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 average:iface Rx PCK/S txpck/s rxkb/s txkb/s rxcmp/s txcmp/s Rxmcst/saverage:lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00average:eth0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 |
above is counted 1 times every 1 seconds, statistics 3 times results, the last few columns are the mean value.
with these options, you can get a clear picture of disk IO and network IO.
through the contents of this article, about memory, CPU, IO, we can clearly understand, in fact, there are many tools under Linux to complete the above functions, But SAR is a relatively comprehensive tool, so here is the key to introduce a bit. Other tools, including Vmstat, can be used to view memory usage, top can be used to view CPU usage, and iostat can be used to view IO situations. In the final analysis, what tools are not the key to solve the problem is our goal. In addition, it is important to understand how these tools are used, and how to use them to solve real-world problems, and I will follow up on how to optimize applications through these tools to write a few articles and share them with you.