How to Speed up Your Linux PC

Tuesday Jul 28th 2009 by Akkana Peck
Share:

Eliminate the resource hogs and your machine will zip along that much faster.

Hardware is getting amazingly fast. But sometimes it seems like software gets slower faster than hardware speeds up. So why do so many apps feel poky?

In this series, I'll look at some of the ways you can examine your system to see what's taking up resources, and offer some tips on slimming your system down so it runs faster.

I'll start with two of the basic command-line applications for measuring performance: top and ps.

top

When you suspect something is running away with system resources, top should be your first recourse. It gives you a screen that updates every couple of seconds, showing you a list of the processes that are hogging the most CPU.

The fields, in order, are:

PID
process ID -- you can use this for killing the process or finding out more about it
USER
the user who started the process, most often either you or root
PR
The priority of the process. This usually doesn't mean much on Linux
NI
How "nice" the process is -- whether it will step aside when other processes need to do some work. Most normal programs will be at zero; a positive number up to 20 means the process will give way to other processes, while a negative number means an important process (often part of the kernel) which will take priority over normal programs.
VIRT
The process's virtual memory size. This is virtually meaningless -- I'll write more about memory in a future article.
RES
The amount of memory the process is currently using in memory. Again, there are caveats and this may not mean quite what you think it does.
SHR
Shared memory: how many shared libraries the process is using.
S
A single letter code representing the process's status. R means it's actively running. S means sleeping, but you'll also see it for lots of running processes, if top happens to check in between updates. T means it's temporarily stopped (you can stop a process by typing Control-Z in the terminal where you started it). D means it's in uninterruptible sleep, perhaps because it's waiting for a device to respond. Finally, Z means it's a zombie process: one that has exited but its parent process isn't paying attention any more. You can't get rid of zombie processes, but they don't take up any significant system resources.
%CPU
What percentage of the CPU the process is currently taking
%MEM
What percentage of memory the process is using
TIME+
How much CPU time the process has accumulated, in hundredths of a second.
COMMAND
The name of the process

Top has some useful options, too. For instance, if you want to run top just once and save its output into a file, you can do it like this:

top -b -n 1
When you're running top interactively, you can change the way it sorts: type M to sort by memory use rather than CPU, T to sort by total time, or P to go back to sorting by CPU. Those letters are all capitalized.
The next workhorse is ps, Process Status. If you type it with no arguments, you'll probably get something that's not very useful:
  PID TTY          TIME CMD
13978 pts/2    00:00:00 bash
15375 pts/2    00:00:00 ps

Add a u (no dash in front of it) to get a lot more information in "user-oriented format":

(imbrium)- ps u
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
akkana    3848  0.0  0.0   4188  2084 tty1     S    08:46   0:00 -tcsh
akkana    3905  0.0  0.0   3504  1552 tty1     S+   08:46   0:00 /bin/bash /usr/
akkana    3965  0.0  0.0   2904   824 tty1     S+   08:46   0:00 xinit /home/akk
akkana    4022  0.0  0.0   1868   504 tty1     S    08:46   0:00 sh /home/akkana
akkana    4032  0.0  0.1   9232  3664 tty1     S    08:46   0:00 xterm -geometry
akkana    4034  0.0  0.0   5572  2840 tty1     S    08:46   0:00 /usr/bin/python
akkana    4038  0.0  0.2  12356  7364 tty1     S    08:46   0:01 openbox
akkana    4040  0.0  0.0   3976  2000 pts/0    Ss   08:46   0:00 -tcsh
akkana    4057  0.0  0.0   3140   716 tty1     S    08:46   0:00 dbus-launch --a
akkana    4065  0.0  0.0   1868   532 tty1     S    08:46   0:00 /bin/sh /usr/lo
akkana    4070  0.0  0.0   1868   548 tty1     S    08:46   0:00 /bin/sh /usr/lo
akkana    4074  0.7  3.1 261620 98948 tty1     Sl   08:46   1:33 /usr/local/fire
akkana    4132  0.0  0.1   8796  3588 tty1     S    08:49   0:00 xterm -geometry
akkana    4133  0.0  0.0   4456  2576 pts/1    Ss+  08:49   0:00 -tcsh
akkana    4598  0.0  0.1   8684  4952 pts/0    S+   08:51   0:00 mutt
akkana    5221  0.0  0.5  26340 17124 tty1     S    09:03   0:05 emacs
akkana   12067  0.0  0.4  27612 13044 tty1     Sl   11:05   0:00 python /home/ak
akkana   13535  0.0  0.6  33096 18624 tty1     S    11:46   0:01 /usr/local/bin/
akkana   13977  0.0  0.1   8788  5472 pts/1    S    11:57   0:00 xterm -bg white
akkana   13978  0.0  0.0   3964  2048 pts/2    Ss   11:57   0:00 -tcsh
akkana   15400  0.0  0.0   2704   976 pts/2    R+   12:24   0:00 ps u

The fields pretty much mirror the ones you see in top, with slightly different names and units.

You may notice that ps u only shows you processes owned by you. When you're trying to speed up your system, don't forget about system processes. Sometimes they can take up a significant amount of time. ps uax will show all processes: the a means "all processes", but despite that, it doesn't really show all processes until you tack on the x to show processes that aren't attached to any terminal -- for instance, processes that were started when your system booted.

One thing you may have noticed about ps output is that it isn't sorted in any useful order. For instance, take this output of ps uax:

(imbrium)- ps uax
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0   3080  1884 ?        Ss   08:45   0:00 /sbin/init
root         2  0.0  0.0      0     0 ?        S<   08:45   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S<   08:45   0:00 [migration/0]
root         4  0.0  0.0      0     0 ?        S<   08:45   0:00 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S<   08:45   0:00 [watchdog/0]
root         9  0.0  0.0      0     0 ?        S<   08:45   0:00 [events/0]
root        11  0.0  0.0      0     0 ?        S<   08:45   0:00 [khelper]
root        17  0.0  0.0      0     0 ?        S<   08:45   0:00 [async/mgr]
root       162  0.0  0.0      0     0 ?        S<   08:45   0:00 [kblockd/0]
root       165  0.0  0.0      0     0 ?        S<   08:45   0:00 [kacpid]
root       166  0.0  0.0      0     0 ?        S<   08:45   0:00 [kacpi_notify]
root       233  0.0  0.0      0     0 ?        S<   08:45   0:00 [ata/0]
root       235  0.0  0.0      0     0 ?        S<   08:45   0:00 [ata_aux]
root       236  0.0  0.0      0     0 ?        S<   08:45   0:00 [ksuspend_usbd]
root       242  0.0  0.0      0     0 ?        S<   08:45   0:00 [khubd]
root       245  0.0  0.0      0     0 ?        S<   08:45   0:00 [kseriod]
[ ... ]
It goes on, but you get the idea: the list is obviously sorted by process ID. It would make a lot more sense to sort by CPU or memory use, especially when you're trying to speed up your system. So you can specify a sort order, like:
ps uax --sort=-rss
to sort by memory use (resident set size). The dash in front of rss tells ps to put the biggest processes first. Other useful sort options are -pcpu, to sort by CPU use and -time to sort by cumulative time the process has ued.

You can also use those specifiers to indicate what fields ps will show, in case you're not interested in details like Status, Start time, Priority and so forth. For the full list of parameters you can use, search for STANDARD FORMAT SPECIFIERS in the ps manual page.

For instance, here's a useful ps command that gives much more useful output:

ps ax --sort=-pcpu o user,pid,pcpu,pmem,vsz,rss,stat,time,comm

Of course, you wouldn't want to type that every time. So choose a set of arguments that make sense to you, and make a shell alias in your .bashrc file:

alias psall "ps ax --sort=-pcpu o user,pid,pcpu,pmem,vsz,rss,stat,time,comm"

Akkana Peck is a freelance programmer and author of the book Beginning GIMP: From Novice to Professional. She'll be discussing some techniques for speeding up Linux in a talk at OSCON this Wednesday. Her website is is shallowsky.com.

Article courtesy of Linux Planet.

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved