Tuesday, September 28, 2021

How to Speed up Your Linux PC

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/200:00:00 bash
15375 pts/200: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
USERPID %CPU %MEMVSZRSS TTY  STAT STARTTIME COMMAND
akkana3848  0.0  0.04188  2084 tty1 S08:460:00 -tcsh
akkana3905  0.0  0.03504  1552 tty1 S+08:460:00 /bin/bash /usr/
akkana3965  0.0  0.02904824 tty1 S+08:460:00 xinit /home/akk
akkana4022  0.0  0.01868504 tty1 S08:460:00 sh /home/akkana
akkana4032  0.0  0.19232  3664 tty1 S08:460:00 xterm -geometry
akkana4034  0.0  0.05572  2840 tty1 S08:460:00 /usr/bin/python
akkana4038  0.0  0.2  12356  7364 tty1 S08:460:01 openbox
akkana4040  0.0  0.03976  2000 pts/0Ss08:460:00 -tcsh
akkana4057  0.0  0.03140716 tty1 S08:460:00 dbus-launch --a
akkana4065  0.0  0.01868532 tty1 S08:460:00 /bin/sh /usr/lo
akkana4070  0.0  0.01868548 tty1 S08:460:00 /bin/sh /usr/lo
akkana4074  0.7  3.1 261620 98948 tty1 Sl08:461:33 /usr/local/fire
akkana4132  0.0  0.18796  3588 tty1 S08:490:00 xterm -geometry
akkana4133  0.0  0.04456  2576 pts/1Ss+  08:490:00 -tcsh
akkana4598  0.0  0.18684  4952 pts/0S+08:510:00 mutt
akkana5221  0.0  0.5  26340 17124 tty1 S09:030:05 emacs
akkana12067  0.0  0.4  27612 13044 tty1 Sl11:050:00 python /home/ak
akkana13535  0.0  0.6  33096 18624 tty1 S11:460:01 /usr/local/bin/
akkana13977  0.0  0.18788  5472 pts/1S11:570:00 xterm -bg white
akkana13978  0.0  0.03964  2048 pts/2Ss11:570:00 -tcsh
akkana15400  0.0  0.02704976 pts/2R+12:240: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
USERPID %CPU %MEMVSZRSS TTY  STAT STARTTIME COMMAND
root 1  0.0  0.03080  1884 ?Ss08:450:00 /sbin/init
root 2  0.0  0.0  0 0 ?S
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.

Similar articles

Latest Articles