[root@node01:/var/log/sa]$ cat /home/sa.haibo.he/memory.c
#include
#include
#include
#define PAGE_SZ (1<<12)
int main() {
int i;
int gb = 2; //以GB为单位分配内存大小
for (i = 0; i < ((unsigned long)gb<<30)/PAGE_SZ ; ++i) {
void *m = malloc(PAGE_SZ);
if (!m)
break;
memset(m, 0, 1);
}
printf("allocated %lu MB/n", ((unsigned long)i*PAGE_SZ)>>20);
getchar();
return 0;
}
[root@node01:/]$ gcc -o memory memory.c
[root@node01:/]$ ./memory
每秒1次,共5次。
[root@node01:/var/log/sa]$ sar -r 1 5
Linux 3.10.0-1160.el7.x86_64 (node01) 09/22/2021 _x86_64_ (4 CPU)
10:25:00 AM kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty
10:25:01 AM 144668 7864308 98.19 0 4174484 9607004 58.60 5162884 1966344 0
10:25:02 AM 144668 7864308 98.19 0 4174484 9607004 58.60 5162900 1966344 0
10:25:03 AM 144668 7864308 98.19 0 4174484 9607004 58.60 5162912 1966344 0
10:25:04 AM 144668 7864308 98.19 0 4174484 9607004 58.60 5162912 1966344 0
10:25:05 AM 144668 7864308 98.19 0 4174488 9607004 58.60 5162916 1966344 32
Average: 144668 7864308 98.19 0 4174485 9607004 58.60 5162905 1966344 6
查看22号内存使用情况
[root@node01:/var/log/sa]$ sar -r -f /var/log/sa/sa22
Linux 3.10.0-1160.el7.x86_64 (node01) 09/22/2021 _x86_64_ (4 CPU)
12:00:01 AM kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty
12:10:01 AM 148016 7860960 98.15 0 6224336 7512448 45.83 4021888 3061428 24
12:20:01 AM 148576 7860400 98.14 0 6224352 7512164 45.82 4022120 3061216 40
12:30:01 AM 148036 7860940 98.15 0 6224364 7521640 45.88 4023080 3061156 24
12:40:01 AM 149892 7859084 98.13 0 6224388 7510960 45.82 4021780 3061124 16
12:50:01 AM 162972 7846004 97.97 0 6224404 7497012 45.73 4015204 3056840 40
01:00:01 AM 162588 7846388 97.97 0 6224436 7497448 45.73 4015736 3056700 16
01:10:01 AM 160776 7848200 97.99 0 6224484 7496868 45.73 4020144 3053580 32
01:20:01 AM 160712 7848264 97.99 0 6224504 7505956 45.79 4020564 3053552 36
01:30:01 AM 159744 7849232 98.01 0 6224524 7497904 45.74 4020544 3053548 0
01:40:01 AM 155704 7853272 98.06 0 6224548 7504464 45.78 4023876 3053552 12
01:50:01 AM 155800 7853176 98.05 0 6224572 7503940 45.77 4023820 3053508 0
02:00:01 AM 155564 7853412 98.06 0 6224592 7503940 45.77 4023860 3053508 24
02:10:01 AM 154056 7854920 98.08 0 6224672 7506672 45.79 4030056 3049676 8
02:20:01 AM 151892 7857084 98.10 0 6224696 7517520 45.86 4031944 3049440 8
02:30:01 AM 150104 7858872 98.13 0 6224680 7517840 45.86 4032964 3049392 8
02:40:01 AM 150468 7858508 98.12 0 6224732 7517840 45.86 4032920 3049396 8
02:50:01 AM 150164 7858812 98.13 0 6224748 7508752 45.80 4032500 3049340 8
03:00:01 AM 149892 7859084 98.13 0 6224772 7509468 45.81 4032948 3049076 12
03:10:01 AM 144452 7864524 98.20 0 6224836 7513428 45.83 4040668 3046208 8
03:20:01 AM 160928 7848048 97.99 0 6224832 7496652 45.73 4032448 3041880 8
03:30:01 AM 160392 7848584 98.00 0 6224788 7566896 46.16 4032444 3041856 32
03:40:01 AM 160752 7848224 97.99 0 6224812 7497032 45.73 4032900 3041724 36
03:50:01 AM 160864 7848112 97.99 0 6224792 7496484 45.73 4032412 3041724 32
04:00:01 AM 160696 7848280 97.99 0 6224848 7496900 45.73 4032836 3041688 32
04:10:01 AM 153284 7855692 98.09 0 6224884 7508652 45.80 4042292 3037744 32
04:20:01 AM 154668 7854308 98.07 0 6224900 7504788 45.78 4041232 3037612 32
04:30:01 AM 153660 7855316 98.08 0 6224912 7514216 45.84 4042252 3037564 24
04:40:01 AM 154088 7854888 98.08 0 6224948 7505908 45.79 4042376 3037560 24
04:50:01 AM 154252 7854724 98.07 0 6224984 7514280 45.84 4042508 3037524 36
05:00:01 AM 152604 7856372 98.09 0 6225004 7515432 45.84 4043256 3037464 32
05:10:01 AM 147848 7861128 98.15 0 6225040 7521320 45.88 4052036 3032708 36
05:10:01 AM kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty
05:20:01 AM 147380 7861596 98.16 0 6225056 7583756 46.26 4051524 3032552 32
05:30:01 AM 147920 7861056 98.15 0 6225068 7512548 45.83 4052080 3032416 32
05:40:01 AM 163192 7845784 97.96 0 6225088 7503316 45.77 4042264 3029976 8
05:50:01 AM 163596 7845380 97.96 0 6225108 7494772 45.72 4042140 3029944 8
06:00:01 AM 164296 7844680 97.95 0 6225128 7494636 45.72 4041692 3029884 12
06:10:01 AM 162428 7846548 97.97 0 6225168 7495992 45.73 4044876 3028388 8
06:20:01 AM 162192 7846784 97.97 0 6225188 7495448 45.72 4045176 3028260 8
06:30:01 AM 157340 7851636 98.04 0 6225212 7574044 46.20 4048932 3028220 8
06:40:01 AM 164116 7844860 97.95 0 6225220 7493944 45.71 4044168 3028184 8
06:50:01 AM 163672 7845304 97.96 0 6225248 7564224 46.14 4044328 3028156 8
07:00:01 AM 162764 7846212 97.97 0 6225272 7495348 45.72 4045112 3028056 8
07:10:01 AM 141868 7867108 98.23 0 6235540 7504920 45.78 4059672 3032828 32
07:20:01 AM 142276 7866700 98.22 0 6235528 7575524 46.21 4059324 3032684 32
07:30:01 AM 141704 7867272 98.23 0 6235568 7505064 45.78 4059924 3032668 40
07:40:01 AM 140960 7868016 98.24 0 6235592 7504892 45.78 4059420 3032644 24
07:50:01 AM 145576 7863400 98.18 0 6235616 7499744 45.75 4055592 3032616 32
08:00:01 AM 145904 7863072 98.18 0 6235628 7500024 45.75 4055424 3032484 32
08:10:01 AM 145088 7863888 98.19 0 6235660 7509024 45.80 4058316 3030264 32
08:20:01 AM 145376 7863600 98.18 0 6235684 7500348 45.75 4058292 3030244 24
08:30:01 AM 145424 7863552 98.18 0 6235692 7500668 45.75 4058616 3030236 24
08:40:01 AM 145252 7863724 98.19 0 6235720 7509344 45.81 4059184 3030236 36
08:50:01 AM 124744 7884232 98.44 0 4198212 9609716 58.62 5150956 1998288 36
09:00:01 AM 141172 7867804 98.24 0 4181996 9609716 58.62 5153980 1979432 32
09:10:02 AM 126268 7882708 98.42 0 4196624 9601100 58.57 5160748 1987192 32
09:20:01 AM 154672 7854304 98.07 0 4168992 9600952 58.57 5151024 1968924 36
09:30:01 AM 154084 7854892 98.08 0 4169160 9610484 58.62 5152612 1968940 32
09:40:01 AM 159396 7849580 98.01 0 4169152 9667992 58.97 5147316 1968808 32
09:50:01 AM 158708 7850268 98.02 0 4169204 9596596 58.54 5148444 1967752 36
10:00:01 AM 154276 7854700 98.07 0 4169240 9612304 58.63 5152620 1967660 8
10:10:01 AM 141828 7867148 98.23 0 4174280 9611828 58.63 5164256 1966300 8
10:20:01 AM 143100 7865876 98.21 0 4174512 9617720 58.67 5163600 1966360 20
Average: kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty
Average: 152420 7856556 98.10 0 5896286 7850820 47.89 4219149 2868869 23
sar 的常用参数说明:
-r 查看系统内存资源
-n 查看系统网络资源
-u 查看系统CPU资源
-d 查看系统磁盘资源
-b 查看系统的IO速率
-f 从历史数据文件中获取数据,与上面的参数连用
sar 应用举例:
# sar -r 10 5 查看系统当前内存使用情况,每隔10秒报告一次,连续报告5次
# sar -n DEV 1 5 查看系统当前网络资源使用情况,每个一秒报告一次,连续报告5次
# sar -d -f /var/log/sa/sa22 查看系统历史(22号)磁盘资源使用情况
# sar -u -f /var/log/sa/sa22 查看系统历史(22号)CPU资源使用情况
# sar -h 查看sar命令的用法
注意:其它的参数可以套用上面的例子