---
title: Problem set 6
heading: "Problem set 6: interrupts, input and output, linking and performance"
eyebrow: C8 · Problem set 6
type: problems
---

::: hook
These problems use lessons 25 to 32: interrupts and exceptions, privilege modes and system calls, polling, interrupts and DMA for moving data, critical sections and atomic instructions, linking and startup code, measuring performance, and what the compiler does. Unless a problem says otherwise, each instruction takes one cycle and a UART byte is 10 bit times.
:::

::: pool
pick: 12

::: check
kind: parts
given: e = 12 | 20; k = 4 | 8 | 15; b = 10 | 30; r = 1000 | 20000; f = 16 | 64

## An interrupt costs {e} cycles of hardware entry, then its handler saves and restores {k} registers (one `sw` and one `lw` each), does {b} instructions of work and ends with `mret`. Interrupts arrive {r} times a second on a {f} MHz processor.

::: part
answer: e + 2*k + b + 1
unit: cycles
tolerance: 0

### How many cycles does each interrupt take?

::: misses
- [e + k + b + 1] Each register takes two instructions: a save and a restore.
- [e + 2*k + b] Count the `mret`.
- [any] Not yet. Entry + 2k + work + 1.
:::
:::

::: part
answer: r*(e + 2*k + b + 1)/(f*1e6)*100
unit: %
tolerance: 0.5%

### What percentage of the processor's time do the interrupts use?

::: misses
- [any] Rate × cycles each / clock rate.
:::
:::
:::

::: check
kind: parts
given: f = 8 | 48 | 100; n = 1000 | 48000 | 100000

## A timer interrupt handler moves TIMECMP forward by {n} cycles each time, on a {f} MHz clock.

::: part
answer: f*1e6/n
tolerance: 0.5%

### How many interrupts a second does that give?

::: misses
- [any] Clock rate over the step.
:::
:::

::: part
answer: n/f
unit: µs
tolerance: 0.5%

### How long is it between interrupts?

::: misses
- [any] n cycles at f MHz.
:::
:::
:::

::: check
kind: parts
given: B = 9600 | 57600 | 115200; L = 20 | 80 | 256

## A device sends {L}-byte messages over a {B}-baud UART.

::: part
answer: B/10
unit: bytes/s
tolerance: 0.5%

### How many bytes a second can the UART carry?

::: misses
- [B/8] Count the start and stop bits.
- [any] Not yet. Baud over 10.
:::
:::

::: part
answer: 10*L/B*1000
unit: ms
tolerance: 0.5%

### How long does one message take?

::: misses
- [any] 10L bit times.
:::
:::
:::

::: check
kind: parts
given: T = 2 | 10 | 50; c = 20 | 60; f = 8 | 32

## Firmware polls a sensor's status register every {T} µs, each poll costing {c} cycles on a {f} MHz clock.

::: part
answer: c/(T*f)*100
unit: %
tolerance: 0.5%

### What percentage of the processor's time does polling use?

::: misses
- [any] Cycles per poll over cycles per polling period.
:::
:::

::: part
answer: T
unit: µs
tolerance: 0.5%

### What is the worst-case delay before a new reading is noticed?

::: misses
- [any] One polling period.
:::
:::
:::

::: check
kind: parts
given: R = 100000 | 500000 | 2000000; n = 128 | 512; c = 40 | 80; f = 48 | 168

## An ADC delivers {R} samples a second. Each interrupt costs {c} cycles on a {f} MHz processor.

::: part
answer: R*c/(f*1e6)*100
unit: %
tolerance: 0.5%

### With an interrupt per sample, what percentage load would that be? (Over 100% means impossible.)

::: misses
- [any] R × c / f.
:::
:::

::: part
answer: R/n*c/(f*1e6)*100
unit: %
tolerance: 0.5%

### With DMA in blocks of {n}, what is the interrupt load?

::: misses
- [R*c/(f*1e6)*100] With DMA there's one interrupt per block of {n}.
- [any] Not yet. The interrupt rate falls by the block size.
:::
:::
:::

::: check
kind: parts
given: w = 16 | 32; f = 25 | 80; B = 4 | 64

## A DMA controller moves {B} kB over a {w}-bit bus at {f} million transfers a second.

::: part
answer: w*f/8
unit: MB/s
tolerance: 0.5%

### What is the bus bandwidth?

::: misses
- [w*f] That's megabits a second.
- [any] Not yet. Bytes per transfer × transfers per second.
:::
:::

::: part
answer: B*1000/(w*f/8)
unit: µs
tolerance: 0.5%

### How long does the transfer take, using the whole bus?

::: misses
- [any] Size over bandwidth (1 kB = 1000 bytes here).
:::
:::
:::

::: check
kind: parts
given: T = 14 | 28 | 50; R = 2 | 6; D = 1 | 3; B = 2 | 8; S = 1 | 4

## A build reports {T} kB of `.text`, {R} kB of `.rodata`, {D} kB of `.data` and {B} kB of `.bss`, with {S} kB set aside for the stack.

::: part
answer: T + R + D
unit: kB
tolerance: 0.5%

### How much flash does it need?

::: misses
- [T + R] `.data`'s initial values are stored in flash.
- [T + R + D + B] `.bss` takes no flash.
- [any] Not yet. .text + .rodata + .data.
:::
:::

::: part
answer: D + B + S
unit: kB
tolerance: 0.5%

### How much RAM does it need?

::: misses
- [any] .data + .bss + stack.
:::
:::
:::

::: check
kind: parts
given: P = 128 | 1024; A = 5000 | 9000 | 12000 | 20000; let d = A - P; let hi = floor((d + 2048)/4096)

## `la t0, table` is at {P:hex4} and `table` is at {A:hex4}.

::: part
answer: hi
tolerance: 0

### What is the `auipc` immediate?

::: misses
- [any] The distance, rounded so the remainder fits in signed 12 bits.
:::
:::

::: part
answer: d - 4096*hi
tolerance: 0

### What is the `addi` immediate?

::: misses
- [any] The distance minus 4096 × the upper part.
:::
:::
:::

::: check
kind: parts
given: N = 10 | 40; Ca = 1.2 | 1.6; fa = 50 | 80; r = 0.6 | 0.8; Cb = 1.4 | 1.9; fb = 120 | 150

## A program runs {N} million instructions on processor A (CPI {Ca}, {fa} MHz). Recompiled for processor B, it needs {r} as many instructions, at CPI {Cb} and {fb} MHz.

::: part
answer: N*Ca/fa*1000
unit: ms
tolerance: 0.5%

### How long does it take on A?

::: misses
- [any] N × CPI / f.
:::
:::

::: part
answer: (N*Ca/fa)/(r*N*Cb/fb)
tolerance: 0.5%

### How many times faster is B?

::: misses
- [(N*Ca/fa)/(N*Cb/fb)] B also runs fewer instructions.
- [any] Not yet. A's time over B's.
:::
:::
:::

::: check
kind: parts
given: p = 0.4 | 0.65 | 0.85; s = 3 | 8 | 50

## A fraction {p} of a program's time is in a routine that gets {s} times faster.

::: part
answer: 1/((1 - p) + p/s)
tolerance: 0.5%

### What is the overall speedup?

::: misses
- [s] Only part of the program got faster.
- [any] Not yet. $1/((1 - p) + p/s)$.
:::
:::

::: part
answer: (1 - p)/((1 - p) + p/s)*100
unit: %
tolerance: 0.5%

### What percentage of the new running time is spent outside the routine?

::: misses
- [(1 - p)*100] That was the old share; the total has shrunk.
- [any] Not yet. The untouched part over the new total.
:::
:::
:::

::: check
kind: parts
given: a = 0.45 | 0.55; l = 0.25 | 0.3; m = 0.05 | 0.1; cm = 20 | 50; f = 100 | 200

## Of a program's instructions, {a} are ALU (1 cycle), {l} are loads that hit in a cache (2 cycles) and the rest branches (1 cycle). A fraction {m} of the loads miss and take {cm} extra cycles each.

::: part
answer: a + 2*l + (1 - a - l) + l*m*cm
tolerance: 0.5%

### What is the average CPI?

::: misses
- [a + 2*l + (1 - a - l)] Add the misses: {l} × {m} × {cm} cycles per instruction.
- [any] Not yet. The weighted sum, plus the loads' miss cycles.
:::
:::

::: part
answer: f/(a + 2*l + (1 - a - l) + l*m*cm)
unit: MIPS
tolerance: 0.5%

### What MIPS rate does that give at {f} MHz?

::: misses
- [any] f/CPI.
:::
:::
:::

::: check
kind: parts
given: P = 15 | 40 | 120; f = 16 | 64; C = 1.2 | 1.5; N = 1 | 20

## A microcontroller draws {P} mW at {f} MHz and averages CPI {C}. A task runs {N} million instructions.

::: part
answer: N*C/f
unit: s
tolerance: 0.5%

### How long does the task take?

::: misses
- [any] N × CPI / f.
:::
:::

::: part
answer: P*N*C/f
unit: mJ
tolerance: 0.5%

### How much energy does it use?

::: misses
- [any] Power × time.
:::
:::
:::

::: check
kind: parts
given: f = 16 | 48 | 100; e = 8 | 12 | 20; c = 10 | 30 | 60

## A {f} MHz core takes {e} cycles to enter an interrupt handler; its longest critical section is {c} cycles.

::: part
answer: (e + c)/f
unit: µs
tolerance: 0.5%

### What is the worst-case interrupt latency?

::: misses
- [e/f] The interrupt may wait out a critical section first.
- [any] (entry + section) ÷ clock.
:::
:::

::: part
answer: c/f
unit: µs
tolerance: 0.5%

### How much of that is the critical section?

::: misses
- [any] Its cycles ÷ clock.
:::
:::
:::

::: check
kind: parts
given: L = 5 | 6 | 10; w = 2 | 3; T = 50 | 400

## A main loop of {L} instructions has a {w}-instruction window between reading a shared counter and writing it back. {T} timer ticks, each adding to the counter in a handler, land at random points.

::: part
answer: w/L*100
unit: %
tolerance: 0.5%

### What share of the handler's updates are lost?

::: misses
- [any] The window's share of the loop.
:::
:::

::: part
answer: T*w/L
tolerance: 0.5%

### About how many updates are lost?

::: misses
- [any] Ticks × that share.
:::
:::
:::

::: check
kind: parts
given: N = 10000 | 1000000; b = 4 | 10; c = 4 | 6; f = 50 | 200

## Inlining removes the {c}-instruction call overhead from a {b}-instruction function called {N} times on a {f} MHz core (one instruction a cycle).

::: part
answer: N*c
tolerance: 0

### How many instructions does it save?

::: misses
- [any] Calls × overhead.
:::
:::

::: part
answer: N*c/f
unit: µs
tolerance: 0.5%

### How much time is that?

::: misses
- [any] Instructions ÷ clock.
:::
:::
:::

::: check
kind: parts
given: n = 2000 | 20000; c = 150 | 400; f = 100 | 200; h = 50 | 120

## A data logger on a {f} MHz processor makes {n} system calls a second at {c} cycles of overhead each, and also updates a counter shared between two cores whose cache line takes {h} ns per handoff.

::: part
answer: n*c/(f*1e6)*100
tolerance: 0.5%

### What percentage of one core goes to system-call overhead?

::: misses
- [n*c/(f*1e6)] Give a percentage.
- [any] Not yet. $nc/f$ as a percentage.
:::
:::

::: part
answer: 1e3/h
tolerance: 0.5%

### At most how many million times per second can the two cores together update the shared counter?

::: misses
- [2*1e3/h] Updates serialize on the cache line; the second core adds nothing.
- [any] Not yet. One update per handoff.
:::
:::

::: right
Right. Crossing into the kernel and sharing a cache line are both costs counted in hundreds of cycles.
:::
:::
:::
