Professional Documents
Culture Documents
Linux Performance Tuning
Linux Performance Tuning
and delivered to clients, it is designed to be fully compatible with most of the computers available in the market. This is a very heterogeneous set of hardware (hard disks, video cards, network cards, etc.). So distribution vendors like Red Hat, SuSe, Mandriva and the rest of them choose some conservative configuration options to assure a successful installation."
ps
This tool allows you to view what processes are running in your system. The range of the process you can see goes from your own ones to all-wide system processes. This command shows the PID for the process, the terminal associated, the accumulated time and the name of the process. You can use ps to monitor what processes are running on your system and which are extensively using the CPU.
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND fernape fernape fernape fernape 2474 2577 2609 2801 0.0 0.0 0.0 0.0 0.1 0.1 0.0 1.1 53944 1648 tty1 Ss 13:02 0:00 -bash 52732 1180 tty1 S+ 13:02 0:00 /bin/sh /usr/X11R 5476 720 tty1 S+ 13:02 0:00 xinit /etc/X11/xi 134360 11768 tty1 S 13:03 0:00 /usr/bin/gnome-se
8796 812 tty1 S 13:03 0:00 /usr/bin/dbus-lau 73576 10112 tty1 S 13:03 0:01 /usr/libexec/gcon 5244 972 tty1 S 13:03 0:00 /usr/bin/gnome-ke 53952 1652 pts/0 Ss 13:07 0:00 bash 53952 1628 pts/1 Ss 13:09 0:00 bash 5440 828 pts/0 R+ 13:18 0:00 ps u
top
If you liked ps, you will like top. Top is a very efficient tool to keep track of processes continuously. The tool is invoked by running top at the prompt. With top, you can list processes ordered by CPU or memory usage. To know more about top, please read this article: Using Top More Efficiently.
vmstat
vmstat provides information about virtual memory, processes, cpu and more. It can be useful to monitor your global system statistics. This tool is a compilation of information that can be gathered from other well-known UNIX commands
[fernape@Hammer ~]$ vmstat procs -----------memory---------- ---swap-- -----io---- --system-----cpu---r b swpd free buff cache si so bi bo in cs us sy id wa 0 0 0 598412 17444 222484 0 0 258 39 1040 211 6 2 86 7
time
This is a little but useful tool to measure execution times. It takes a command as an argument. Then executes it and shows a brief statistic about time spent by the process. This can help you to find out if a command takes more time than expected. Of course, this is specially useful if you are dealing with a program developed by you, so you can modify your sources and compile again to compare results.
[fernape@Hammer ~]$ time ls -R >/dev/null real 0m0.117s user 0m0.057s sys 0m0.046s
x11perf
x11perf performs a benchmark over the X server. This command is invoked running x11perf from prompt. It can take a lot of parameters but running x11perf with -all parameter reveals a lot of information (but it will take a while...). Obviously this information is not very useful if we can not compare it with other. Here is when x11perfcomp is in order. x11perfcomp is a shell script that compares two outputs
generated by x11perf. Now, the way to analyze your X server performance is clear: take a snapshot of your server performance, make some changes in the configuration file and other system settings (remember that X window system uses shared memory and other IPC mechanisms that can affect performance), run x11perf again and finally, compare the results with x11perfcomp. Below is a truncated output of x11perf. It is a very configurable tool, you can specify certain performance tests or tell him to run all available tests.
[fernape@Hammer ~]$ x11perf -all x11perf - X11 performance program, version 1.5 The X.Org Foundation server version 60801000 on :0.0 from Hammer Sun Apr 2 13:21:41 2006 Sync time adjustment is 0.0895 msecs. 40000000 reps @ 0.0001 msec (6890000.0/sec): Dot 40000000 reps @ 0.0001 msec (7910000.0/sec): Dot 40000000 reps @ 0.0001 msec (7890000.0/sec): Dot 40000000 reps @ 0.0001 msec (7790000.0/sec): Dot 40000000 reps @ 0.0001 msec (8290000.0/sec): Dot 200000000 trep @ 0.0001 msec (7720000.0/sec): Dot 50000000 reps @ 0.0001 msec (10100000.0/sec): 1x1 rectangle
Results can be saved by redirecting the output of x11perf to a file. I executed x11perf once and saved the results in test1 file, then I changed some settings in my xorg.conf file and executed the benchmark again saving results in test2 file. Then I compared them:
[fernape@Hammer ~]$ x11perfcomp test1 test2 1: test1 2: test2 1 2 Operation -------- -------- --------8310000.0 8140000.0 Dot
It appears that my changes were not very appropriate. When x11perf is used, a white window is showed in the screen as you can see in the picture below. To avoid benchmarking result modification, don't touch that window nor move the mouse over it. The window will disappear when the tests are finished.
hdparm
One of the most important devices in your machine is the hard drive. It is used whenever a new process is loaded, when a memory region is swapped from memory to hard disk and when it is restored from the swap partition to memory again. And because UNIX like systems makes an extensively use of files for almost everything, it is very important to set up the hard disk properly. Hdparm helps you in benchmarking and setting your hard disk. For now I only show you how to perform a test for your hard disk. If you are impatient and want to know what parameters you can set, read the next section: Tuning your system To perform the test, type (change /dev/hda to match your hard disk.):
[root@Hammer ~]# hdparm -tT /dev/hda /dev/hda: Timing cached reads: 1656 MB in 2.00 seconds = 828.13 MB/sec Timing buffered disk reads: 102 MB in 3.03 seconds = 33.71 MB/sec
I executed hdparm from root account since it is a restricted tool. We can see transfer
rates for cached reads and buffered disk reads. If you want to see your current settings, try:
[root@Hammer ~]# hdparm /dev/hda /dev/hda: multcount = 16 (on) IO_support = 1 (32-bit) unmaskirq = 1 (on) using_dma = 1 (on) keepsettings = 0 (off) readonly = 0 (off) readahead = 256 (on) geometry = 65535/16/63, sectors = 80026361856, start = 0
I recommend you to run the test at least five times to take the mean value. And it is important not to use the rest of the system if it is possible, so the test will be not affected by other block disk requests.
Network Manager: chooses the best available connection (are you always connected and permanently moving?) bluetooth: if you usually don't work with bluetooth devices you can disable it. cron: disable it if you have not programmed tasks (maybe you would like to run these tasks for yourself instead of schedule them). cups: printer daemon (I have it disabled in my laptop since I don't print from it) iptables: the linux firewall. At your own risk. irda: Same as bluetooth for Infra Red communications irqbalance: disable if you are not working with an SMP system. nfs: you can disable it if you are not sharing your files through NFS. misc. servers (ssh, sftpd...): A lot of servers are launched by default. Do you really want your system becomes a server?
vnc: Virtual Network Console. Disable if you don't need remote graphical access to your system.
In Red Hat systems, you can add and remove system services using the chkconfig command (for non graphical sessions) in this way:
chkconfig service on/off
Other systems have their own mechanisms for managing services. See your handbook.
Disabling X at booting.
You would like to disable X or not, depending of the type of user you are. Im a fanatical of the command line, and I have disable X at boot time. When I start my system I log in a console do my work and shut it down. This can save a lot of RAM if you dont mind to deal with bash (or the shell you use). If you want to disable X, go to the /etc/inittab file and change the line:
id:5:initdefault:
The next time you boot your system, you will be in pure command line. This not only saves memory but it is faster for simply operations, e.g. If I want to insert a line in my movie list file I waste less time in console mode than in graphical mode, waiting for GNOME, Metacity and X server to be loaded.
Removing consoles
One of the cool features of a linux distribution is the availability of several terminals. These terminals are accessed with Ctrl+Fn keys and allows to log in more than once in the system. But sincerely, I have been using Linux since several years ago and I never needed more than three or maybe four terminals at a time. So you can save some memory by disabling some of them. Even more, if you usually use a graphical desktop, you can use one or more gnome-terminals or konsoles, so you can safely left only one additional terminal in your system and free the other ones. To achieve this, the /etc/inittab file must be modified again. Just comment the lines that set the terminals:
# Run gettys in standard runlevels 1:2345:respawn:/sbin/mingetty 2:2345:respawn:/sbin/mingetty 3:2345:respawn:/sbin/mingetty 4:2345:respawn:/sbin/mingetty 5:2345:respawn:/sbin/mingetty 6:2345:respawn:/sbin/mingetty tty1 tty2 tty3 tty4 tty5 tty6
Hdparm
At this moment we revisited hdparm. We have seen that it can show disk information but it can set some properties too. The most important options you can try are: -d: enables/disables DMA transfers. -X: fine tuning for the DMA transfers. -A: for look ahead when reading disk blocks. -c: enable IDE 32 bit support. -C: very important for laptops (power management features) As an example, I disable my DMA transfers, look ahead and 32 bit support and run a test:
[root@Hammer ~]# hdparm -A0 -d0 /dev/hda /dev/hda: setting using_dma to 0 (off) setting drive read-lookahead to 0 (off) using_dma = 0 (off) [root@Hammer ~]# hdparm -tT /dev/hda /dev/hda: Timing cached reads: 1580 MB in 2.00 seconds = 789.33 MB/sec Timing buffered disk reads: 8 MB in 3.20 seconds = 2.50 MB/sec
Notice that buffered disk reads have decreased dramatically! It is more than 15x worse (compared to the test performed above...).
Sysctl
Probably this is the best tool for tuning linux systems. It can configure a lot of system parameters while the linux kernel is running. It handles this, by reading and writing to kernel variables through procfs files. The complete list of available variables is shown by typing:
[root@Hammer ~]# sysctl -a
It would be a waste of time to explain every variable in this long list. Instead of that, if you are curious, you can play with them (most of them are self-explanatory). My advice: look at networking options.
Nvidia, ATI or Intel provides their own proprietary drivers for linux. This license issue is the reason by which they are not included in most of the distributions. Proprietary drivers can improve your video card performance very much. Lets use x11perf again. I run a deepcopyplane500 test with the Standard VGA driver chosen by my stock installation. I saved the result in test1 file. Then I change to the ATI proprietary drivers for my ATI Radeon Mobility 9700 and run the test again saving the result in test2 file. The use of x11perfcomp will reveal the big difference between the two drivers.
[fernape@Hammer ~]$ x11perfcomp test1 test2 1: test1 2: test2 1 2 Operation -------- -------- --------7.7 54.2 Copy 500x500 n-bit deep plane
Almost 8x better performance with the ATI drivers! As you can see, it is really a good idea to install the proper optimised driver for your video card. Going further, you can even improve the performance of the driver. Nvidia and ATI, allows you to install a precompiled package or to generate a new one. The last option allows you to compile the driver for your specific machine instead of use a generic one.
If you choose the recompile option, you will need your kernel sources, but fortunately the installers make a good job and generate the package automatically without additional work.
See documentation for additional information. You can install the cpufreq applet to easily configure your driver.
We could start to configure from scratch but this is not our objective. We could forget an important feature and then our kernel will be possibly broken. So, load a configuration file from configs subdirectory:
These configuration files ensure that you will obtain the same result you had with a stock kernel but with your specific processor optimizations. You can safely load this file and don't worry about anything. The picture below shows the configuration file loaded.
Even more, in my case, I'm sure my processor is an Athlon64 so I can set it in the options and deselect the Generic-x86-64 option. Then save your settings and continue with the process: make; make modules; make modules_install; And if needed, create your initrd file. If you have compiled and installed your kernel successfully, you will take advantage of your system architecture without a big effort.
Conclusions
Linux vendors make a great effort to build optimized versions of linux systems. However the success in installation is very important too. So, the vendors should estimate both factors. Due to this, some parts of the system are not ready to work at maximum performance. Our mission is to find these points and lead them to a better performance state. Moreover, since vendors want linux become a wide-used operating system, sometimes they start a lot of services that are not usually used. As an advanced user, it is important to disable these services and features to save memory and other system resources. The best strategy: if you are not using that feature you can safely disable it. You will save system resources.