However, when the system is used in an embedded mode (sometimes called head-less because it uses no monitor) you need alternative methods for tracking this information. I sometimes will open an SSH connection and use shell utilities to provide this information. Some of the options are: top, mpstat, iostat, and sar.
I really wanted the “lots of blinking lights” effect for my server, so I developed the software below to provide that. The basic concept is to periodically (once per second, or more) read files in the /proc and /sys file systems to collect data and then update banks of LEDs appropriately.
CPU UtilizationThe file /proc/stat provides a lot of information about the system, including CPU utilization. The first line is a total for all CPUs in the system. This is followed by lines for each individual CPU. For the Raspberry Pi, reading the first line is all that is needed. There are four values that we will be interested in:
- User mode CPU time – this is the time the CPU spends running programs that are in user mode at standard execution priority.
- Nice mode CPU time – this is the time the CPU spends running programs that are in user mode at a priority other than standard. See the nice command for more info on this.
- Kernel mode time – this is the time the CPU spends running in kernel mode. This is the protected mode that the operating system uses. This will include things such as device drivers and system API calls as well as the core OS itself.
- Idle mode CPU time – this is the time the CPU spends doing nothing.
These values will be constantly updated, so the utilization program must track the changes over time. It will store the values each time it computes CPU utilization and compute the change for each. The CPU percentage utilization will be 100 x (user + nice + kernel) / (user + nice + kernel + idle)
I/O UtilizationThe file /sys/block/DEVICENAME/stat provides information about the specified DEVICENAME. The device sda is my external hard drive but you may want to use mmcblk0, the SD card. I decided to keep this simple and only track the number of sectors read and written. Like the CPU data, this data is constantly updated, so the change over time is what is tracked. One difficulty with I/O utilization is that the program has no way of knowing what value 100% should be. I provide a function to set this value, but you can simply let the program track it dynamically. 100% percent will always be the maximum usage seen so far.
Displaying the UtilizationThe next challenge is to display this data. If your system has an LCD display, you could just show the percentages directly. Alternatively, you could make this data available via a web interface. I chose to use 10-LED bar graphs controlled via GPIO. You could use a single LED that lights up when a threshold is passed. You could use 10 separate LEDs, but the multi-segment LEDs are easier to connect and take up less space. A good compromise might be three LEDs to indicate low, medium, high and very high utilization. The software below supports 10-segment LED displays. I will leave the other options as an exercise for the reader.
SoftwareThe software I am using can be obtained from google code using the git command.
git clone https://code.google.com/p/raspberrypi-utilization-display/
See the included README file for further instructions.
If you just want to browse the source code, you can do that at this address:
This is still a very rough version of the software but should still prove useful to some. In the future I hope to add a configuration file that will define the update rate, the number of LEDs to use, and what GPIO pins the LEDs are connected to.