NEXT UP previous
Next: Questions

Space

In addition to processes, there are several system resources that it is useful to be able to monitor, the most common being disk space and memory space. In order to see on a per filesystem basis the amount of disk space available and in use, you use the df command:

	$ df
	Filesystem	1024-blocks  Used       Available   Capacity   Mounted on
	/dev/hda3       199270	     182354	6625	     96%        /
	/dev/hda1       610608	     209675	369394	     36%	/usr
	/dev/hda4       199271       147953	41027	     78%        /home/pc

The numbers and sizes of partitions on your system will obviously differ from this example, but the principle is the same. The first column in the df listing specifies the pathname of the special file that represents the disk partition. The second column gives the number of 1024-byte data blocks contained in the partition, with third and fourth columns being the number of these blocks in use and available respectively. You may be surprised to discover that the values in the fourth column when added to those in the third column do not add up the numbers in the second column. This is because, by default, a small percentage of the space on each partition is reserved for use by the system administrators so that they can login and still have some working space for sorting out problems, even when the filesystems are full as far as ordinary users are concerned.

The Capacity column shows the percentage of the ordinary users' space that is in use. This means that when this figure reaches 100% there is still the system administrator's percentage of the partition left to go. Finally, the Mounted on column shows where in the directory hierarchy the root directory of each partition is attached to the overall directory tree structure, see Figure 2.1.

The df command is useful when you want to see how much disk space is in use on a particular partition and how much is left. However, it will not help you to find out how much disk space is used up by your home directory and its subdirectories and files. To find out this kind of disk usage information, you use the du command:

	$ cd /etc
	$ du
	24	./rc.d
	1	./fs
	4	./skel/.tern
	8	./skel
	1	./lilo
	1	./default
        5	./msgs
	389	.

By default, the first column in this output listing is the amount of disk space, measured in kilo-bytes, allocated to all of the files in each of the directories listed in the second column.

Notice, that without any command line switches specified, du systematically works its way down the directory hierarchy from the current directory until the sizes of all subdirectories have been reported. This can be rather a lengthy listing, when very often all you want is just a single number giving the total. In this case it is possible to get du to print a summary of what it finds, using the -s command line switch:

	$ du -s /etc
	389	/etc

As you can see the name of the directory to use as the start of the disk usage search can be specified as a parameter. In fact, the du command can have a list of directory names specified as command line parameters and it will size each of them separately and generate a list of the results.

Now, we'll move on from disk usage to memory usage. Your machine will have a fixed amount of random access memory (RAM) installed in it. This memory is used to hold the running operating system kernel itself, all the processes that are executing on the machine, and all the data on which these processes are operating. In fact, Linux can actually arrange to be dealing with more processes than will fit simultaneously into the memory. It does this by having available a memory overflow area on disk, traditionally called the swap space.

In general, all memory starts off as unused (i.e. free). In order to speed up disk access, Linux arranges that once a block of data has been read from a disk file it is stored in memory in a buffer for as long as possible, so that if the contents of the same disk block are required again, the data can be taken from the memory buffer rather than re-reading the disk. Reading disk data from memory like this is orders of magnitude faster than getting the data from disk every time it is required. A similar buffering trick is also done when data is written to the disk.

Another way to use up free memory is to load programs into it for execution as processes. If the number of processes in the system continues to increase, then after a while the quantity of free memory will dwindle down to a small amount that is insufficient to run any new programs. What happens then is that Linux will start to take some of the memory back from the disk buffers in order to run more processes. In this way, as more processes are added to the system, the amount of memory dedicated to disk buffers falls until some minimum required value is reached. When this happens, no more free space or buffer space can be taken up, so Linux now needs to adopt a new strategy. What it does is to look for blocks of memory that are in use but haven't been needed for a while and writes them away to the swap space. This effectively makes those swapped memory blocks free again and available for reuse. At times when the process load on the system lightens, swapped out memory blocks can be brought back into main memory, eventually to rejoin the free memory list as the processes terminate.

There is one other technique that Linux uses in order to wring the most use out of the system's main memory sharing executable code. If two different processes are both executing the same program, or the same program libraries, then there is no reason why they should not both share a single copy of the code in memory rather than loading the same code twice into separate memory areas. And in fact Linux does just this.

This whole thing is a rather dynamic situation with blocks of memory migrating between the free list, the disk buffer cache, process memory and the swap space as the operating system balances out the various requirements for this fixed-size resource. Information about all these areas of memory can be displayed using the free command:

	$ free
	            total    used      free      shared    buffers
      Mem:          19208    13444     5746      7448      7784
      -/+ buffers:           5660      13548
      Swap:         32992    0         32992

All of the numbers here are given in l-kilo~byte blocks. Looking at the top row of the output, the total figure is the amount of memory that Linux has available and free to start with. If you know how much memory is really installed into your computer then you will find that the total in the free output is around 1 mega-byte (Mb) less. This is because the amount of memory used by the kernel has already been removed and is not displayed. The next two numbers in the top row of the output show the amount of memory used so far and the amount left unused in the free memory section. Together these two add up to the total. After that, the next number is the amount of the shared memory used by all the processes. And the final figure is the amount of memory in use for disk buffers at this time.

The second row contains two numbers. These are just the same as the numbers directly above them in the first row, but changed by the size of the buffer memory. This gives an idea of the memory situation just with regard to processes, as buffer memory can be reclaimed for process space anyway.

The final row of the output shows the total, free and used space available on disk for swapping.

It can be quite instructive to watch how the pattern of memory usage changes over time and this can be done with the command top. This command is very interesting to see as it provides a large quantity of system statistics in a display which is regularly updated every few seconds. The top command itself is actually a cross between the ps and free commands with a few extras as well. The set of processes that are displayed are listed in the order of how much CPU time they are using, with the heaviest CPU usage at the top. An abbreviated sample output from top appears as follows:

	$ top
	1:44pm up 8 days,2:21, 4 users, load average: 0.11, 0.15, 0.14
	33 processes: 32 sleeping, 1 running, 0 zombie, 0 stopped
	CPU states:	2.1'/, user,  0.0.,. nice,  7.47. system, 90.57. idle
	Mem:	15040K av, 14340K used, 700K free, 6312K shrd, 6620K buff
	Swap:	9208K av, 548K used, 8660K free


	PlD   USER   PRI     NI    SIZE    RES    SHRD    STAT    %CPU    %MEM  TIME   COMMAND
        471   pc     20      0	   100	   308    332     R	  9.0     2.0   2:24   top
        273   root   1       0	   85      280    352     S	  0.5     1.8   0:17   in.tftpd
        1     root   1       0	   48      196    280     S	  0.0     1.3  26:37   init
        295   cew    1	     0	   536     656    508     S	  0.0     4.3   0:05   -bash
        849   mot    1       0     538     652    504     S	  0.0     4.3   0:03   -bash

The first line of output shows the current time, the length of time that the machine has been up and running since it was booted, how many users are currently logged in and three numbers on the end showing how heavily loaded the system has been in the recent past. The three system load values are the load averages taken over the past minute, the past five minutes and the past fifteen minutes respectively. The load average effectively tells you how many processors you need in the machine to run all the processes at full speed. This means that the smaller the value of these numbers, the lighter the load on the system. Obviously, when there is only one CPU to share between all the processes, a load average greater than 1 implies that the processes are not getting as much CPU time as they are requesting and, consequently, they will slow down.

The second line in the output says how many processes there are in total on the system and what states they are all in.

The third line of output gives CPU utilisation information and shows that currently the CPU is idle for over 90% of the time and used to run user and system code for less than 10% of the time. These figures effectively confirm the load average figures given before.

The next two lines contain just the same information that you would get from running the free command. Finally, the rest of the display is ps type output, but ordered on %CPU usage.


NEXT UP previous
Next: Questions