Best Linux Performance Monitoring and Debugging Tools
Best Linux Performance Monitoring and Debugging Tools
1. SAR
Using sar utility you can do two things: 1) Monitor system real time performance (CPU, Memory, I/O,
etc) 2) Collect performance data in the background on an on-going basis and do analysis on the historical
data to identify bottlenecks.
Sar is part of the sysstat package. The following are some of the things you can do using sar utility.
Network statistics
The following sar command will display the system CPU statistics 3 times (with 1 second interval).
The following “sar -b” command reports I/O statistics. “1 3” indicates that the sar -b will be executed for
every 1 second for a total of 3 times.
$ sar -b 1 3
Linux 2.6.18-194.el5PAE (dev-db) 03/26/2011 _i686_ (8 CPU)
Iostat without any argument displays information about the CPU usage, and I/O statistics about all the
partitions on the system as shown below.
$ iostat
Linux 2.6.32-100.28.5.el6.x86_64 (dev-db) 07/09/2011
$ iostat -p sda
Linux 2.6.32-100.28.5.el6.x86_64 (dev-db) 07/09/2011
Option -A, displays all the information that can be displayed by the mpstat command as shown below.
This is really equivalent to “mpstat -I ALL -u -P ALL” command.
$ mpstat -A
Linux 2.6.32-100.28.5.el6.x86_64 (dev-db) 07/09/2011 _x86_64_
(4 CPU)
mpstat Option -P ALL, displays all the individual CPUs (or Cores) along with its statistics as shown below.
$ mpstat -P ALL
Linux 2.6.32-100.28.5.el6.x86_64 (dev-db) 07/09/2011 _x86_64_
(4 CPU)
Vmstat
vmstat reports virtual memory statistics. The following are some of vmstat command examples.
vmstat by default will display the memory usage (including swap) as shown below.
$ vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu--
----
r b swpd free buff cache si so bi bo in cs us sy id wa
st
0 0 305416 260688 29160 2356920 2 2 4 1 0 0 6 1 92
2 0
To execute vmstat every 2 seconds for 10 times, do the following. After
executing 10 times, it will stop automatically.
$ vmstat 2 10
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu--
---
r b swpd free buff cache si so bi bo in cs us sy id wa
st
1 0 0 537144 182736 6789320 0 0 0 0 1 1 0 0 100
0 0
0 0 0 537004 182736 6789320 0 0 0 0 50 32 0 0 100
0 0
..
iostat and vmstat are part of the sar utility. You should install sysstat package to get iostat and vmstat
working.
PS Command
Process is a running instance of a program. Linux is a multitasking operating system, which means that
more than one process can be active at once. Use ps command to find out what processes are running
on your system.
ps command also give you lot of additional information about the running process which will help you
identify any performance bottlenecks on your system.
Use -u option to display the process that belongs to a specific username. When you have multiple
username, separate them using a comma. The example below displays all the process that are owned by
user oracle.
eq)))
eq)))
eq)))
eq)))
eq)))
ic
ss 7 --session
ets
Free
Free command displays information about the physical (RAM) and swap memory of your system.
In the example below, the total physical memory on this system is 1GB. The values displayed below are
in KB.
# free
total used free shared buffers cached
Mem: 1034624 1006696 27928 0 174136 615892
-/+ buffers/cache: 216668 817956
Swap: 2031608 0 2031608
The following example will display the total memory on your system including RAM and Swap.
option t displays the “Total” line, which is sum of physical and swap memory values
# free -mto
total used free shared buffers cached
Mem: 1010 983 27 0 170
601
Swap: 1983 0 1983
Total: 2994 983 2011
TOP
Top command displays all the running process in the system ordered by certain columns. This displays
the information real-time.
You can kill a process without exiting from top. Once you’ve located a process that needs to be killed,
press “k” which will ask for the process id, and signal to send. If you have the privilege to kill that
particular PID, it will get killed successfully.
PID to kill: 1309
Kill PID 1309 with signal [15]:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1309 geek 23 0 2483m 1.7g 27m S 0 21.8 45:31.32 gagent
1882 geek 25 0 2485m 1.7g 26m S 0 21.7 22:38.97 gagent
5136 root 16 0 38040 14m 9836 S 0 0.2 0:00.39 nautilus
Use top -u to display a specific user processes only in the top command output.
$ top -u geek
While unix top command is running, press u which will ask for username as shown below.
Pmap
pmap command displays the memory map of a given process. You need to pass the pid as an argument
to the pmap command.
The following example displays the memory map of the current bash shell. In this example, 5732 is the
PID of the bash shell.
$ pmap 5732
5732: -bash
00393000 104K r-x-- /lib/ld-2.5.so
003b1000 1272K r-x-- /lib/libc-2.5.so
00520000 8K r-x-- /lib/libdl-2.5.so
0053f000 12K r-x-- /lib/libtermcap.so.2.0.8
0084d000 76K r-x-- /lib/libnsl-2.5.so
00c57000 32K r-x-- /lib/libnss_nis-2.5.so
00c8d000 36K r-x-- /lib/libnss_files-2.5.so
b7d6c000 2048K r---- /usr/lib/locale/locale-archive
bfd10000 84K rw--- [ stack ]
total 4796K
pmap -x gives some additional information about the memory maps.
$ pmap -x 5732
5732: -bash
Address Kbytes RSS Anon Locked Mode Mapping
00393000 104 - - - r-x-- ld-2.5.so
003b1000 1272 - - - r-x-- libc-2.5.so
00520000 8 - - - r-x-- libdl-2.5.so
0053f000 12 - - - r-x-- libtermcap.so.2.0.8
0084d000 76 - - - r-x-- libnsl-2.5.so
00c57000 32 - - - r-x-- libnss_nis-2.5.so
00c8d000 36 - - - r-x-- libnss_files-2.5.so
b7d6c000 2048 - - - r---- locale-archive
bfd10000 84 - - - rw--- [ stack ]
-------- ------- ------- ------- -------
total kB 4796 - - -
To display the device information of the process maps use ‘pamp -d pid’.
Netstat
Netstat command displays various network related information such as network connections, routing
tables, interface statistics, masquerade connections, multicast memberships etc.,
List all ports (both listening and non listening) using netstat -a as shown below.
# netstat -a | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 localhost:30037 *:* LISTEN
udp 0 0 *:bootpc *:*
Use the following netstat command to find out on which port a program is running.
Use the following netstat command to find out which process is using a particular port.
Lsof
Lsof stands for ls open files, which will list all the open files in the system. The open files include network
connection, devices and directories. The output of the lsof command will have the following columns:
PID process ID
USER Username
FD file descriptor
To view all open files of the system, execute the lsof command without any parameter as shown below.
# lsof | more
COMMAND PID USER FD TYPE DEVICE SIZE NODE
NAME
init 1 root cwd DIR 8,1 4096 2 /
init 1 root rtd DIR 8,1 4096 2 /
init 1 root txt REG 8,1 32684 983101
/sbin/init
init 1 root mem REG 8,1 106397 166798
/lib/ld-2.3.4.so
init 1 root mem REG 8,1 1454802 166799
/lib/tls/libc-2.3.4.so
init 1 root mem REG 8,1 53736 163964
/lib/libsepol.so.1
init 1 root mem REG 8,1 56328 166811
/lib/libselinux.so.1
init 1 root 10u FIFO 0,13 972
/dev/initctl
migration 2 root cwd DIR 8,1 4096 2 /
skipped..
To view open files by a specific user, use lsof -u option to display all the files opened by a specific user.
# lsof -u ramesh
vi 7190 ramesh txt REG 8,1 474608 475196 /bin/vi
sshd 7163 ramesh 3u IPv6 15088263 TCP dev-db:ssh->abc-
12-12-12-12.
To list users of a particular file, use lsof as shown below. In this example, it displays all users who are
currently using vi.
# lsof /bin/vi
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
vi 7258 root txt REG 8,1 474608 475196 /bin/vi
vi 7300 ramesh txt REG 8,1 474608 475196 /bin/vi
w and uptime
While monitoring system performance, w command will hlep to know who is logged on to the system.
$ w
09:35:06 up 21 days, 23:28, 2 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 :0 24Oct11 21days 1:05 1:05 /usr/bin/Xorg :0
-nr -verbose
ramesh pts/0 192.168.1.10 Mon14 0.00s 15.55s 0.26s sshd: localuser
[priv]
john pts/0 192.168.1.11 Mon07 0.00s 19.05s 0.20s sshd: localuser
[priv]
jason pts/0 192.168.1.12 Mon07 0.00s 21.15s 0.16s sshd: localuser
[priv]
For each and every user who is logged on, it displays the following info:
Username
tty info
Line 1 of the w command output is similar to the uptime command output. It displays the following:
Current time
If you want only the uptime information, use the uptime command.
$ uptime
09:35:02 up 106 days, 28 min, 2 users, load average: 0.08, 0.11, 0.05
Please note that both w and uptime command gets the information from the /var/run/utmp data file.
/proc
/proc is a virtual file system. For example, if you do ls -l /proc/stat, you’ll notice that it has a size of 0
bytes, but if you do “cat /proc/stat”, you’ll see some content inside the file.
Do a ls -l /proc, and you’ll see lot of directories with just numbers. These numbers represents the
process ids, the files inside this numbered directory corresponds to the process with that particular PID.
The following are the important files located under each numbered directory (for each process):
cmdline – command line of the command.
limits – Contains the information about the specific limits to the process.
The following are the important links under each numbered directory (for each process):
Socket Statistics – SS
ss stands for socket statistics. This displays information that are similar to netstat command.
$ ss -l
Recv-Q Send-Q Local Address:Port Peer Address:Port
0 100 :::8009 :::*
0 128 :::sunrpc :::*
0 100 :::webcache :::*
0 128 :::ssh :::*
0 64 :::nrpe :::*
$ ss -o state established
Recv-Q Send-Q Local Address:Port Peer Address:Port
0 52 192.168.1.10:ssh 192.168.2.11:55969 timer:(on,414ms,0)
The following displays socket summary statistics. This displays the total number of sockets broken down
by the type.
$ ss -s
Total: 688 (kernel 721)
TCP: 16 (estab 1, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 11
Transport Total IP IPv6
* 721 - -
RAW 0 0 0
UDP 13 10 3
TCP 16 7 9
INET 29 17 12
FRAG 0 0 0