htop
What it's really useful for
htop is top with a fancy hat on. That's a bit dismissive; it's a lot more than that. It's also quite cross-platform these days. The official site is here. A simple, command-line tool that enables you to identify:
- More or less everything top did
and also adds some nice new features, including:
- Easy to understand CPU usage visualisation
- Filter the list of observed processes
As with top, the best way to understand what this tool can do for you is to start seeing it in action.
Invocation
Invoke htop at your command line:
htop -d 5
This invokes htop, with an update period of five tenths of a second (i.e. the display will be refreshed every half second). It will look something like this:
For a really thorough explanation of the meanings of everything shown, PÄ“teris Å…ikiforovs goes into fantastic detail on it (and everything else visible in htop) here. He's really helpful.
There are three principal parts to this:
- Meters (top left):
The top bars of these (one for each CPU being monitored) show how busy a given CPU is, and of that work being done, where it's being spent. As with top, key parts of this are the amount being spent in user space (shown by default in green) and in kernel space (shown in default in red). There are other colours for other tasks, which we'll get to shortly. As you can see in this image, the length of the bar matches the percentage value on the right of the bar; at a glance, you can get an understanding of how busy the CPU and you can watch trends over time. A changing bar is much easier to divine trends from than a simple value, so even though this is providing the same data as top, you can draw extra information from the presentation.
The lowest two meters, labelled "Mem" and "Swp" show usage of memory and swap space. The memory colours indicate used pages (green), buffered pages (blue) and cache pages (yellow). If you're running out of memory, you'll be able to see it here; be wary, though - memory is often not so simple as "used" and "free", especially in modern operating systems. Again, there is a helpful value on the far right; at first glance, this is the value to look at. As can be seen here, about a fifth of the memory appears to be in use according to that value.
"Swp" is a measure of how much the operating system is having to store data that it would like to keep in the memory in "swap space"; which generally means on your hard drive. This happens when so much memory has been requested that there simply isn't enough to go round. The operating system will copy some memory to disk, use the newly free memory for something else, and when data in the swap space is needed again, try to copy it back into memory (and, in the process, find something else in memory that can be put into swap space for the time being). This can be a huge performance killer. If you can hear your hard-drive thrashing, and everything runs as if it's knee-deep in treacle, and even the OS stops responding, take a look at this. You've either got a memory leak, or you simply need to rewrite your code to use less memory. If this is the problem, sharpening your code in other ways won't help.
- Task information (top right):
- Process table (bottom half):
"Tasks" is the number of processes, and how many of them are running. For our purposes here (we're trying to figure out what the bottleneck on a process of interest is) it's not so relevant.
"Load average" is a set of average values representing how busy the system's CPUs (or cores) have been over the timespan indicated, but not only over that timeframe. For a simple examination of where the bottleneck in your process of interest is, it's not so useful.
"Uptime" is simply how long the system has been running. It's taken from the system itself (at least it is on Linux).