top は vmstat に比べて負荷がかかると言いますが、具体的に何が違うのかちょっと調べてみました。
オープンするプロセスファイルシステム(/proc)のファイルが違う気がするので、strace で調べてみました。
- vmstat がオープンしているファイルを調べる。
% strace -e open vmstat 1 1 open("/etc/ld.so.cache", O_RDONLY) = 3 open("/lib/libproc-3.2.7.so", O_RDONLY) = 3 open("/lib/libc.so.6", O_RDONLY) = 3 open("/proc/stat", O_RDONLY) = 3 procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ r b swpd free buff cache si so bi bo in cs us sy id wa st open("/proc/meminfo", O_RDONLY) = 3 open("/proc/stat", O_RDONLY) = 4 open("/proc/vmstat", O_RDONLY) = 5 0 0 0 806304 16788 162936 0 0 194 26 351 78 1 4 91 4 0
- top がオープンしているファイルを調べる。
% strace -e open top -b -n 1 open("/etc/ld.so.cache", O_RDONLY) = 3 open("/lib/libproc-3.2.7.so", O_RDONLY) = 3 open("/usr/lib/libncurses.so.5", O_RDONLY) = 3 open("/lib/libc.so.6", O_RDONLY) = 3 open("/lib/libdl.so.2", O_RDONLY) = 3 open("/proc/stat", O_RDONLY) = 3 open("/proc/sys/kernel/pid_max", O_RDONLY) = 3 open("/etc/toprc", O_RDONLY) = -1 ENOENT (No such file or directory) open("/root/.toprc", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/share/terminfo/d/dumb", O_RDONLY|O_LARGEFILE) = 3 open("/proc", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY) = 3 open("/proc/1/stat", O_RDONLY) = 4 open("/proc/1/statm", O_RDONLY) = 4 open("/etc/nsswitch.conf", O_RDONLY) = 4 open("/etc/ld.so.cache", O_RDONLY) = 4 open("/lib/libnss_files.so.2", O_RDONLY) = 4 open("/etc/passwd", O_RDONLY) = 4 open("/proc/2/stat", O_RDONLY) = 4 open("/proc/2/statm", O_RDONLY) = 4 open("/proc/3/stat", O_RDONLY) = 4 open("/proc/3/statm", O_RDONLY) = 4 open("/proc/4/stat", O_RDONLY) = 4 open("/proc/4/statm", O_RDONLY) = 4 open("/proc/5/stat", O_RDONLY) = 4 open("/proc/5/statm", O_RDONLY) = 4 ... open("/proc/4660/stat", O_RDONLY) = 4 open("/proc/4660/statm", O_RDONLY) = 4 open("/proc/4661/stat", O_RDONLY) = 4 open("/proc/4661/statm", O_RDONLY) = 4 open("/etc/localtime", O_RDONLY) = 3 open("/proc/uptime", O_RDONLY) = 3 open("/var/run/utmp", O_RDONLY|O_LARGEFILE) = 4 open("/proc/loadavg", O_RDONLY) = 4 open("/proc/stat", O_RDONLY) = 5 open("/proc/meminfo", O_RDONLY) = 6 top - 20:45:54 up 7 min, 1 user, load average: 0.06, 0.48, 0.28 Tasks: 93 total, 1 running, 92 sleeping, 0 stopped, 0 zombie Cpu(s): 0.5%us, 4.4%sy, 0.0%ni, 91.1%id, 3.8%wa, 0.0%hi, 0.1%si, 0.0%st Mem: 1035064k total, 228884k used, 806180k free, 16772k buffers Swap: 2096472k total, 0k used, 2096472k free, 162920k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 4661 root 15 0 2200 888 692 R 7.0 0.1 0:00.12 top 4660 root 15 0 1904 564 476 S 4.7 0.1 0:00.07 strace 1 root 18 0 2072 632 544 S 0.0 0.1 0:01.68 init 2 root RT -5 0 0 0 S 0.0 0.0 0:00.08 migration/0 3 root 34 19 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0 4 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/0 5 root RT -5 0 0 0 S 0.0 0.0 0:00.05 migration/1 6 root 34 19 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/1 7 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/1 8 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 events/0 9 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 events/1 10 root 10 -5 0 0 0 S 0.0 0.0 0:00.02 khelper 11 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 kthread ...
vmstat に比べて top はかなりのファイルにアクセスしてますね。