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?

:information_source: 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.

:cry: 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
  1. First, we need to ensure that interrupt counters can be maintained for each individual CPU.

    :bulb: You will learn more about multitasking later. For now, it is enough to know that xv6 supports a maximum of NCPU = 8 processors, for which important metadata is stored in a global array of the type struct cpu cpus[NCPU].

    1. Extend the struct cpu datatype in proc.h with a separate counter for each interrupt source (timer, UART, disk) that we will count for each CPU.
    2. Initialize the added counters to zero when the CPUs start executing, i.e., at the beginning of the scheduler() function.
  2. 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!

    :bulb: 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
    
    1. Extend the struct cpu datatype in proc.h with an explicit init field indicating whether this CPU exists, i.e., has started.
    2. Initialize the init field when the CPUs start executing, i.e., at the beginning of the scheduler() function.
  3. Now, we need to add logic to the devintr() function to actually count the different types of interrupts. Use the function mycpu() as follows: struct cpu *cpu = mycpu();. With this pointer, you can then increment the appropriate counter in cpu depending on which interrupt devintr() is handling.

    :bulb: 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.

  4. Add a new syscall printinterrupts() to print these counters. In printinterrupts(), you can iterate over the global struct cpu cpus[NCPU] array, but make sure to print counters only for CPUs that actually exist (i.e., for which the init field is set).

    :bulb: You can use tabs to align the columns.

  5. Finally, write a simple user-space program printirq that 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

This site uses Just the Docs, a documentation theme for Jekyll.