---
title: Arbiters and shared resources
heading: Fixed priority and round-robin arbitration, fairness, weights and arbiter hardware
eyebrow: S3 · Lesson 37 · Arbiters and shared resources
xp: 50
---

::: hook
Whenever several masters share one bus, memory port or network link, an arbiter decides who goes next, every cycle. The choice of rule sets each master's worst-case latency and its share of bandwidth: a fixed-priority arbiter is tiny but can starve its lowest requester forever, while a round-robin arbiter bounds every wait at the cost of a little state. Real systems mix the two, with weights for bandwidth guarantees and priorities for latency-critical traffic, and the arbiter's own logic depth can end up on the critical path.
:::

::: play
- The stems are the fraction of cycles each of 16 requesters wins under fixed priority (requester 0 highest), when each requests independently with probability p in every cycle: requester i wins $p(1 - p)^i$.
- At low load everyone is served. As p rises, the high-priority requesters take nearly everything and the low ones starve.
- The dashed line is the share round-robin guarantees each when all of them request: 1/16.
:::

```sim plot
view: graph
x: i = 0..15
y: s = 0..1
samples: 16
slider: p = 0.02..0.9 = 0.3
plot: p*(1 - p)^i | stems | label: fixed-priority share
plot: 1/16 + 0*i | dashed | label: round-robin share under full load
readout: lowest requester's share = p*(1 - p)^15 | digits: 3
```

::: predict
## Eight masters share a bus through a round-robin arbiter, and each transfer holds the bus for 4 cycles. A master raises its request just after losing its turn. What is its longest possible wait?

- [ ] 4 cycles
- [x] 28 cycles
- [ ] 32 cycles
- [ ] Unbounded

::: reveal
Round-robin passes the grant around in order, so in the worst case the other seven masters each get one 4-cycle transfer first: $7 \times 4 = 28$ cycles. That bound holds however busy the others are, which is the point of round-robin. Under fixed priority, the lowest master's wait has no bound at all.
:::
:::

::: nameit
## Fixed priority

Grant the lowest-numbered requester: a priority encoder, $\lceil\log_2N\rceil$ levels deep. Simple and fast, but a requester is served only in cycles when every higher one is idle, so under load the lowest can **starve**.

## Round-robin

After granting requester i, the highest priority passes to i + 1. A master waits at most for every other master's turn:
$$t_\text{wait,max} = (N - 1)T$$
for transfers of T cycles. Hardware: a pointer of $\lceil\log_2N\rceil$ bits and either two priority encoders (masked and unmasked requests) or one double-width encoder.

## Weighted round-robin

Requester i gets $w_i$ turns per round, a share $w_i/\sum w$ of the resource when everyone is requesting: bandwidth guarantees for, say, a display controller that must never underflow.

## Matrix arbiter

A least-recently-granted arbiter keeps one bit for each pair of requesters saying which has priority over the other, $N(N - 1)/2$ bits, updated so the winner drops below everyone. It's fair and fast but grows quadratically.

## Load

If N requesters each want a fraction r of the cycles, the total demand is Nr. Below 1, every requester is served eventually and waits are short; at or above 1 the resource is saturated and the arbitration rule decides who loses.
:::

::: example
## A 1.6 GB/s memory port is shared by weighted round-robin with weights 4, 2, 1 and 1. What bandwidth does each requester get when all are busy?

::: solution
1. **Shares.** The weights sum to 8, so the shares are 4/8, 2/8, 1/8 and 1/8: 50%, 25%, 12.5% and 12.5%.
2. **Bandwidth.** Of 1.6 GB/s, that's 800, 400, 200 and 200 MB/s.
3. **Idle requesters.** When a requester is idle its turns go to the others, so each share is a guaranteed minimum, and a busy requester can get more.

**Answer:** 800, 400, 200 and 200 MB/s.
:::
:::

::: example
## Under fixed priority, requester 0 asks for the bus in 60% of cycles and requester 1 in 50%, independently. At most what share can requester 2 get?

::: solution
1. **When requester 2 wins.** Only when both higher requesters are idle:
   $$0.4 \times 0.5 = 0.2$$
   20% of cycles, however much it needs.
2. **If it needs more.** If it needs 30% it falls further behind every second: it starves without any bug, purely from load.

**Answer:** at most 20% of cycles.
:::
:::

::: example
## How much state does a matrix arbiter need for 8 requesters, against a round-robin pointer? And for 32?

::: solution
1. **Eight requesters.** A matrix arbiter needs
   $$\frac{8 \times 7}{2} = 28\ \text{bits}$$
   against 3 bits for a round-robin pointer.
2. **Thirty-two requesters.**
   $$\frac{32 \times 31}{2} = 496\ \text{bits}$$
   against 5.
3. **Where each fits.** Matrix arbiters suit small, fast arbiters such as switch ports; wide arbiters use round-robin pointers, often in two levels.

**Answer:** 28 bits against 3 for 8 requesters, and 496 bits against 5 for 32.
:::
:::

::: check
kind: parts
given: N = 4 | 8 | 16; T = 1 | 4 | 16; f = 200 | 500

## {N} masters share a resource through a round-robin arbiter; each grant holds it for {T} cycles at {f} MHz.

::: part
answer: (N - 1)*T
unit: cycles
tolerance: 0

### What is the longest a master can wait?

::: misses
- [N*T] It never waits for its own turn.
- [any] $(N - 1)T$.
:::
:::

::: part
answer: (N - 1)*T*1000/f
unit: ns
tolerance: 0.5%

### How long is that in time?

::: misses
- [any] Cycles over the clock rate.
:::
:::
:::

::: check
kind: parts
given: a = 1 | 2 | 4; b = 1 | 2; c = 1 | 3; B = 1 | 3.2

## Three requesters share a {B} GB/s link by weighted round-robin with weights {a}, {b} and {c}.

::: part
answer: a/(a + b + c)*100
unit: %
tolerance: 0.5%

### What share does the first get when all are busy?

::: misses
- [any] $w_i/\sum w$.
:::
:::

::: part
answer: a/(a + b + c)*B
unit: GB/s
tolerance: 0.5%

### What bandwidth is that?

::: misses
- [any] Share × link bandwidth.
:::
:::
:::

::: check
kind: parts
given: N = 4 | 8 | 16 | 32

## An arbiter serves {N} requesters.

::: part
answer: N*(N - 1)/2
unit: bits
tolerance: 0

### How much state does a matrix arbiter need?

::: misses
- [N*N] One bit per pair, not per ordered pair.
- [any] $N(N - 1)/2$.
:::
:::

::: part
answer: ceil(log(N)/log(2) - 1e-9)
unit: bits
tolerance: 0

### And a round-robin pointer?

::: misses
- [any] $\lceil\log_2N\rceil$.
:::
:::
:::

::: check
kind: parts
given: p = 0.3 | 0.6; q = 0.2 | 0.5; r = 0.1 | 0.3

## Under fixed priority, requesters 0 and 1 ask independently in fractions {p} and {q} of cycles; requester 2 asks in {r}.

::: part
answer: (1 - p)*(1 - q)*100
unit: %
tolerance: 0.5%

### At most what share of cycles can requester 2 win?

::: misses
- [(1 - p - q)*100] Independent requests overlap: multiply the idle probabilities.
- [any] $(1 - p)(1 - q)$.
:::
:::

::: part
answer: r*(1 - p)*(1 - q)*100
unit: %
tolerance: 0.5%

### What share does it actually win, if its own requests are independent too?

::: misses
- [any] It must request and find both others idle.
:::
:::
:::

::: check
kind: choice

## A low-priority DMA engine shares memory with a CPU under fixed priority and sometimes stalls for milliseconds. What change bounds its wait without hurting the CPU's average latency much?

- [ ] Give the DMA engine the highest fixed priority
- [x] Use round-robin or a weighted scheme with a small guaranteed share for the DMA engine, so it's served within a bounded number of turns
- [ ] Make the arbiter faster
- [ ] Add a larger FIFO to the CPU

::: misses
- [1] That would starve the CPU instead.
- [3] A faster arbiter decides sooner but with the same unfair rule.
- [4] The CPU isn't the one waiting.
:::
:::

::: check
kind: derivation
vars: N = 2..64, r = 0.001..0.5, T = 1..64

## N requesters each want the resource in a fraction r of cycles, and each grant lasts T cycles under round-robin.

::: step
answer: N*r

### What is the total demand, as a fraction of the resource?
:::

::: step
answer: (N - 1)*T

### What is the worst-case wait?
:::

::: step
answer: 1/N

### What share does each get when all request continuously?
:::

::: right
Right. Round-robin bounds waits and splits a saturated resource evenly; weights change the split.
:::
:::

::: check
kind: python

## Write `rr_arbiter(requests)`: requests is a list of per-cycle lists of booleans (one per requester). Each cycle, grant the first requesting index at or after the round-robin pointer (wrapping), then move the pointer just past the winner; return the list of grants (None when nobody requests). The pointer starts at 0.

```python starter
def rr_arbiter(requests):
    return [None for _ in requests]
```

```python solution
def rr_arbiter(requests):
    ptr, grants = 0, []
    for req in requests:
        n = len(req)
        g = None
        for k in range(n):
            i = (ptr + k) % n
            if req[i]:
                g = i
                break
        grants.append(g)
        if g is not None:
            ptr = (g + 1) % n
    return grants
```

```python tests
assert rr_arbiter([[True] * 4] * 6) == [0, 1, 2, 3, 0, 1], "All requesting: turns in order."
assert rr_arbiter([[False, True, False, True]] * 4) == [1, 3, 1, 3], "Only 1 and 3: they alternate."
assert rr_arbiter([[False] * 3, [False, False, True]]) == [None, 2], "Idle, then the only requester."
g = rr_arbiter([[True] * 8] * 64)
assert all(g[i:i + 8].count(3) == 1 for i in range(0, 64, 8)), "Each requester exactly once per 8 grants."
```

::: right
Right. Moving the pointer past the winner is what makes round-robin fair: the winner becomes the lowest priority.
:::
:::

::: explain
## Why can a fixed-priority arbiter starve a requester even when the total demand is below the resource's capacity?

::: model
Under fixed priority the lowest requester is served only in cycles when every higher requester happens to be idle. If the higher requesters' requests arrive independently, those idle cycles are rarer than the leftover capacity suggests, and if the lowest requester needs more than that fraction, it falls behind steadily. Worse, bursts from the higher requesters can lock it out for long stretches, so its worst-case wait has no bound. A round-robin arbiter passes priority around, so every requester gets a turn at least once per round, which bounds its wait at the other requesters' turns.
:::
:::

::: practice
::: check
given: N = 6 | 12; T = 2 | 8
answer: (N - 1)*T
unit: cycles
tolerance: 0

## Round-robin worst-case wait for {N} masters, {T}-cycle grants?

::: misses
- [any] $(N - 1)T$.
:::
:::

::: check
given: a = 3 | 5; b = 1 | 2; c = 2 | 4
answer: a/(a + b + c)*100
unit: %
tolerance: 0.5%

## Weighted round-robin share for weight {a} among {a}, {b}, {c}?

::: misses
- [any] $w_i/\sum w$.
:::
:::

::: check
given: N = 4 | 6 | 8 | 12 | 16 | 24 | 32
answer: N*(N - 1)/2
unit: bits
tolerance: 0

## Matrix arbiter bits for {N} requesters?

::: misses
- [any] $N(N - 1)/2$.
:::
:::
:::
