Книга: Embedded Linux Primer: A Practical, Real-World Approach
13.4.4. ps
13.4.4. ps
With the possible exception of strace and ltrace, no tools are more often neglected by the embedded systems developer than top and ps. Given the myriad options available for each utility, we could easily devote an entire chapter to these useful system-profiling tools. They are almost universally available in embedded Linux distributions.
Both of these utilities make use of the /proc file system, as described in Chapter 9, "File Systems." Much of the information they convey can be learned from the /proc file system if you know what to look for and how to parse the resulting information. These tools present that information in a convenient human-readable form.
The ps utility lists all the running processes on a machine. However, it is very flexible and can be tailored to provide much useful data on the state of a running machine and the processes running on it. For example, ps can display the scheduling policy of each process. This is particularly useful for systems that employ real-time processes.
Without any options, ps displays all processes with the same user ID as the user who invoked the command, and only those processes associated with the terminal on which the command was issued. This is useful when many jobs have been spawned by that user and terminal.
Passing options to ps can be confusing because ps supports a wide variety of standards (as in POSIX versus UNIX) and three distinct options styles: BSD, UNIX, and GNU. In general, BSD options are single or multiple letters, with no dash. UNIX options are the familiar dash-letter combinations, and GNU uses long argument formats preceded by double dashes. Refer to the man page for details of your ps implementation.
Everyone who uses ps likely has a favorite invocation. One particularly useful general-purpose invocation is ps aux. This displays every process on the system. Listing 13-9 is an example from a running embedded target board.
Listing 13-9. Process Listing
$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.8 1416 508 ? S 00:00 0:00 init [3]
root 2 0.0 0.0 0 0 ? S< 00:00 0:00 [ksoftirqd/0]
root 3 0.0 0.0 0 0 ? S< 00:00 0:00 [desched/0]
root 4 0.0 0.0 0 0 ? S< 00:00 0:00 [events/0]
root 5 0.0 0.0 0 0 ? S< 00:00 0:00 [khelper]
root 10 0.0 0.0 0 0 ? S< 00:00 0:00 [kthread]
root 21 0.0 0.0 0 0 ? S< 00:00 0:00 [kblockd/0]
root 62 0.0 0.0 0 0 ? S 00:00 0:00 [pdflush]
root 63 0.0 0.0 0 0 ? S 00:00 0:00 [pdflush]
root 65 0.0 0.0 0 0 ? S< 00:00 0:00 [aio/0]
root 36 0.0 0.0 0 0 ? S 00:00 0:00 [kapmd]
root 64 0.0 0.0 0 0 ? S 00:00 0:00 [kswapd0]
root 617 0.0 0.0 0 0 ? S 00:00 0:00 [mtdblockd]
root 638 0.0 0.0 0 0 ? S 00:00 0:00 [rpciod]
bin 834 0.0 0.7 1568 444 ? Ss 00:00 0:00 /sbin/portmap
root 861 0.0 0.0 0 0 ? S 00:00 0:00 [lockd]
root 868 0.0 0.9 1488 596 ? Ss 00:00 0:00 /sbin/syslogd -r
root 876 0.0 0.7 1416 456 ? Ss 00:00 0:00 /sbin/klogd -x
root 884 0.0 1.1 1660 700 ? Ss 00:00 0:00 /usr/sbin/rpc.statd
root 896 0.0 0.9 1668 584 ? Ss 00:00 0:00 /usr/sbin/inetd
root 909 0.0 2.2 2412 1372 ? Ss+ 00:00 0:00 -bash
telnetd 953 0.3 1.1 1736 732 ? S 05:58 0:00 in.telnetd
root 954 0.2 2.1 2384 1348 pts/0 Ss 05:58 0:00 -bash
root 960 0.0 1.2 2312 772 pts/0 R+ 05:59 0:00 ps aux
This is but one of the many ways to view output data using ps. The columns are explained in the following text.
• The USER and process ID (PID) fields should be self-explanatory.
• The %CPU field expresses the percent of CPU utilization since the beginning of the process's lifetime; thus, CPU usage will virtually never add up to 100 percent.
• The %MEM field indicates the ratio of the process's resident memory footprint to the total available physical memory.
• The VSZ field is the virtual memory size of the process in kilobytes.
• RSS is resident set size and indicates the nonswapped physical memory that a process has used, also in kilobytes.
• TTY is the controlling terminal of the process.
Most of the processes in this example are not associated with a controlling terminal. The ps command that generated Listing 13-9 was issued from a Telnet session, which is indicated by the pts/0 terminal device.
The STAT field describes the state of the process at the time this snapshot was produced. Here, S means that the process is sleeping, waiting on an event of some type, often I/O. R means that the process is in a runnable state (that is, the scheduler is free to give it control of the CPU if nothing of a higher priority is waiting). The left bracket next to the state letter is an indication that this process has a higher priority.
The final column is the command name. Those listed in brackets are kernel threads. Many more symbols and options are available; refer to the man page for ps for complete details.