Processes and signals

INPUT · Slides

Who is heavy right now (`top`)

01 / 08

What a photograph cannot tell you

With ps and pstree you can see what is running and how it is related.

And something is missing. You cannot tell what is heavy right now.

~ $ psPID   USER     COMMAND    1 root     init   78 learner -sh

Nothing in that list says which one is eating the CPU.

What ps shows is a photograph of that instant. Heaviness is "how much it ran over a stretch of time", and one photograph cannot measure it.

So you need a tool that shows a film. That is top.

02 / 08

top keeps moving

Type top and it fills the screen, rewriting itself every few seconds.

~ $ topMem: 6880K used, 15904K free, ...CPU:   0% usr   9% sys   0% nic  90% idleLoad average: 0.00 0.00 0.00 2/35 85  PID  PPID USER     STAT   VSZ %VSZ %CPU COMMAND   85    78 learner R     1648   7%   9% top   78     1 learner S     1644   7%   0% -sh

The prompt does not come back. Like less, it is the kind of command that waits until you leave.

There are two ways out.

  • press q
  • press Ctrl-C

Leaving with q is the same as less. A command that takes over the screen leaves with q is a good guess almost every time.

03 / 08

Read the top four lines

The top four lines are the state of the whole machine.

Mem: 6880K used, 15904K free, 1840K shrd, 0K buff, 1840K cachedCPU:   0% usr   9% sys   0% nic  90% idle   0% ioLoad average: 0.00 0.00 0.00 2/35 85  PID  PPID USER     STAT   VSZ %VSZ %CPU COMMAND

Line 1 is memory: how much is in use and how much is free.

Line 2 is how the CPU is being used.

NameMeaning
usrused by your programs
sysused by the kernel
idledoing nothing
iostopped waiting for the disk

The one you look at most is idle. Near 0% means the CPU is full.

Line 4 is the heading of the table below, with eight columns.

04 / 08

Reading the load average

The Load average on line 3 is a slightly unusual number.

Load average: 0.00 0.00 0.00 2/35 85              ^    ^    ^    ^    ^            1min 5min 15min run/all newest PID

The three numbers are the average number of processes waiting their turn, over the last 1, 5 and 15 minutes.

A rough guide to reading them.

  • about the same as the number of CPUs — working nicely
  • larger than the number of CPUs — a queue has formed (heavy)
  • near 0 — idle

This Linux has one CPU, so above 1.00 it is starting to get crowded.

Having three is kind. High for one minute only means it is temporary; high over fifteen means it has been heavy all along. That difference is a clue when you hunt for the cause.

The 2/35 after them is "running / all", and the last number is the newest PID.

05 / 08

Sort to find the culprit

While top is open, keys sort it.

KeySorts by
PCPU use (the default)
Mmemory use
NPID
Ttotal time used
Rreverse the current order

You only need P and M.

A problem is nearly always either "eating CPU" or "eating memory".

slow      -> P (most CPU first)crashing  -> M (most memory first)

P when things are slow, M when things die for want of memory.

Look at the top line or two and you have your culprit. Open top and press M really does solve some problems on its own.

06 / 08

Keep it in a file with -b

top takes over the screen, so as it stands you cannot keep a record. That is what -b (batch) is for.

~ $ top -b -n1 > t.txt~ $ head -4 t.txt

-b says "do not rewrite the screen, just stream the characters". So you can drop it into a file with > and hand it to grep.

-n is the number of rounds. -n1 finishes after one.

top -b -n1        take one round and stoptop -b -n5 -d 2   take five rounds, two seconds apart

-d is the interval in seconds.

You use this shape when investigating a cause afterwards: a server that goes heavy in the night, with -b adding to a file once a minute.

That leaves a record of the hours nobody was watching. When you do crontab in chapter 12, you can make it happen by itself.

07 / 08

free, uptime and /proc

If all you want is top's top four lines, there are small dedicated tools.

~ $ free              total   used   free  shared ...Mem:          22784   4592  16028    1840Swap:             0      0      0~ $ uptime 15:04:31 up 0 min,  load average: 0.08, 0.01, 0.00

free gives only memory, uptime only the running time and the load average. They do not take over the screen, so they are easy to record.

Follow them back and both are only reading /proc.

~ $ cat /proc/loadavg0.08 0.02 0.01 2/35 95~ $ head -3 /proc/meminfoMemTotal:          22784 kBMemFree:           16028 kB

The same place as the /proc/your-number/stat you read last lesson. The kernel makes it on the spot to show you the state right now.

Which means you can get the numbers even without the tools.

08 / 08

Now have a go

This is everything for the lesson.

top              watch it live (q to leave)top -b -n1       stream one roundfree             memory onlyuptime           running time and load averagecat /proc/loadavg

And you practise making it heavy on purpose.

yes > /dev/null &

yes is a command that prints the character y endlessly. Pour it into /dev/null (the bin) and put it in the background with &. That gives you a "culprit" using the CPU flat out.

Once you make one, be sure to stop it. You stop it with kill %1 (properly covered next lesson).

This course tidies up between questions, and on a real computer it keeps spinning until you stop it.