Exercise: Interrupt Counters
Moderate
Interrupts have a significant impact on system performance and correct operation. Modern operating systems typically offer detailed “interrupt accounting” functionality for this reason. In this exercise, we will first explore how to gain insight into interrupt behavior on a modern Linux system. Then, we will add this basic functionality to xv6.
Interrupt Accounting in Linux
Run the following command on your own machine if you are running Linux (you can exit this command with CTRL+C):
$ watch -n 1 cat /proc/interrupts
If you do not have access to a Linux machine, you can see an example below:
What do you see? What do these numbers mean to you? Why do these numbers vary over time?
It is not important to understand the exact meaning of each interrupt source listed here (this depends on the underlying computer architecture and can quickly become very technical!). The purpose of this step is just to give you an idea of interrupt frequency and accounting in a real-world operating system.
Unfortunately, this command does not work in the online VS Code environment because it runs in a container.
Interrupt Accounting in xv6
Currently, xv6 does not track how often interrupts occur. Now, we will add a syscall that provides information about the number of interrupts that have occurred per CPU since xv6 started.
Your task is to replicate the output of Linux’s /proc/interrupts as closely as possible. Specifically, there are three types of interrupts handled by xv6: timer, UART, and disk (VIRTIO). We want to obtain separate information for each type and for each individual CPU. Example output is as follows:
$ printirq
CPU0 CPU1 CPU2
TMR: 26 26 26
UART: 11 7 10
DISK: 8 7 14
-
First, we need to ensure that interrupt counters can be maintained for each individual CPU.
You will learn more about multitasking later. For now, it is enough to know that xv6 supports a maximum of NCPU = 8processors, for which important metadata is stored in a global array of the typestruct cpu cpus[NCPU].- Extend the
struct cpudatatype in proc.h with a separate counter for each interrupt source (timer, UART, disk) that we will count for each CPU. - Initialize the added counters to zero when the CPUs start executing, i.e., at the beginning of the scheduler() function.
- Extend the
-
Currently, xv6 does not explicitly register during startup which of the maximum 8 supported CPUs are actually present and which are not. However, we only want to print interrupts for CPUs that actually exist!
By default, 3 (of the maximum 8) processors are emulated in QEMU. You can optionally override this by starting xv6 as follows:student@code:~/xv6$ CPUS=4 make qemu- Extend the
struct cpudatatype in proc.h with an explicitinitfield indicating whether this CPU exists, i.e., has started. - Initialize the
initfield when the CPUs start executing, i.e., at the beginning of the scheduler() function.
- Extend the
-
Now, we need to add logic to the
devintr()function to actually count the different types of interrupts. Use the functionmycpu()as follows:struct cpu *cpu = mycpu();. With this pointer, you can then increment the appropriate counter incpudepending on which interruptdevintr()is handling.
Timer interrupts are sent to all CPUs, but only one CPU keeps track of the clock. Where does this happen? Ensure that each timer interrupt is counted once per CPU. -
Add a new syscall
printinterrupts()to print these counters. Inprintinterrupts(), you can iterate over the globalstruct cpu cpus[NCPU]array, but make sure to print counters only for CPUs that actually exist (i.e., for which theinitfield is set).
You can use tabs to align the columns. -
Finally, write a simple user-space program
printirqthat uses the new syscall.
Run printirq a few times and try to interpret the results, for example, as follows:
$ printirq
CPU0 CPU1 CPU2
TMR: 26 26 26
UART: 11 7 10
DISK: 8 7 14
$ printirq
CPU0 CPU1 CPU2
TMR: 117 117 117
UART: 16 9 28
DISK: 8 7 14