flynnel 0.6.0

K-aware NUMA-aware work-stealing scheduler with extended-Flynn-taxonomy dispatch (MIMD / SIMC / MIMC / MISD / SIMT / MIMT): a rayon-shaped pool reshaped around per-call execution-class plans with first-class CPU+GPU hybrid (join_hybrid, hybrid_pipeline) and per-call dispatch profiles.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//! Host calibration: every timing constant the substrate relies on is
//! Measured on the running machine at init, validated by live
//! self-tests, and stored in the region header so attaching processes
//! inherit the same numbers. Nothing here is baked from any reference
//! box: a host with a coherent CPU-GPU link measures a smaller
//! visibility bound (and may even pass the system-atomics probe), and
//! every derived constant tightens automatically.
//!
//! Measured quantities:
//! - doorbell round-trip (min / median / p99) against a resident
//!   kernel: the substrate's signaling latency;
//! - CPU/GPU clock offset error via paired QPC/globaltimer sampling
//!   (integer-domain differencing; best-RTT-quartile spread);
//! - launch+sync baseline: the cost a wake-from-idle pays and the
//!   number the doorbell path is beating;
//! - visibility bound `L = p99/2 + clock_err`, Fischer margin
//!   `Delta = clamp(10 x L, 5us ..= 100us)` - then validated by a
//!   real CPU-vs-GPU contention run (escalating x2 on violation);
//! - system-atomics conservation probe deciding `sys_atomics_ok`.

use std::sync::Arc;
use std::time::{Duration, Instant};

use cudarc::driver::sys as cu;
use cudarc::driver::{CudaFunction, CudaStream, LaunchConfig, PushKernelArg};

use super::GpuPeerError;
use super::layout::{
    self, FLAG_CALIBRATED, FLAG_DOORBELL_OK, FLAG_SYS_ATOMICS_OK, FLAG_TIMED_LOCK_OK, GTS_SLOTS,
    HDR_CALIB_PING_OFF, HDR_CALIB_PONG_OFF, HDR_FISCHER_ACQS_OFF, HDR_FISCHER_CS_OFF,
    HDR_FISCHER_VIOL_OFF, HDR_FISCHER_X_OFF, HDR_GTS_OFF,
};
use super::region::PeerRegion;
use super::timed_lock;

/// Host-measured constants + capability flags for one (host, GPU,
/// driver) combination.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PeerCalibration {
    /// Doorbell round-trip, minimum observed.
    pub rtt_min_ns: u64,
    /// Doorbell round-trip, median.
    pub rtt_median_ns: u64,
    /// Doorbell round-trip, 99th percentile.
    pub rtt_p99_ns: u64,
    /// One-way visibility bound (p99 / 2 + clock error).
    pub one_way_ns: u64,
    /// Cross-device clock alignment error (best-quartile spread / 2).
    pub clock_err_ns: u64,
    /// Fischer margin actually validated by the self-test.
    pub delta_ns: u64,
    /// Kernel launch + synchronize baseline (median).
    pub launch_ns: u64,
    /// Doorbell handshake completed and measured.
    pub doorbell_ok: bool,
    /// Fischer self-test passed at `delta_ns` (0 violations).
    pub timed_lock_ok: bool,
    /// Cross-device CAS conserved claims (coherent-link hosts).
    pub sys_atomics_ok: bool,
    /// Evidence behind `timed_lock_ok`: CPU-side contended rounds in
    /// the granting self-test run (of 150).
    pub lock_cpu_contended: u32,
    /// Evidence behind `timed_lock_ok`: GPU-side contended rounds in
    /// the granting self-test run (of 150).
    pub lock_gpu_contended: u32,
}

impl PeerCalibration {
    /// Persist into the region header (other processes attach and
    /// read these instead of re-calibrating).
    pub fn store(&self, r: &PeerRegion) {
        r.store_u64(layout::HDR_RTT_MIN_OFF, self.rtt_min_ns);
        r.store_u64(layout::HDR_RTT_MED_OFF, self.rtt_median_ns);
        r.store_u64(layout::HDR_RTT_P99_OFF, self.rtt_p99_ns);
        r.store_u64(layout::HDR_ONE_WAY_OFF, self.one_way_ns);
        r.store_u64(layout::HDR_CLOCK_ERR_OFF, self.clock_err_ns);
        r.store_u64(layout::HDR_DELTA_OFF, self.delta_ns);
        r.store_u64(layout::HDR_LAUNCH_OFF, self.launch_ns);
        let mut flags = FLAG_CALIBRATED;
        if self.doorbell_ok {
            flags |= FLAG_DOORBELL_OK;
        }
        if self.timed_lock_ok {
            flags |= FLAG_TIMED_LOCK_OK;
        }
        if self.sys_atomics_ok {
            flags |= FLAG_SYS_ATOMICS_OK;
        }
        r.store_u64(layout::HDR_FLAGS_OFF, flags);
    }
}

/// Derive the Fischer margin from a measured visibility bound.
/// Clamped: below 5us the OS scheduler jitter on the CPU side eats
/// the margin; above 100us the lock is slower than useful and the
/// capability is refused instead.
pub fn derive_delta_ns(one_way_ns: u64) -> u64 {
    (one_way_ns.saturating_mul(10)).clamp(5_000, 100_000)
}

/// The kernel handles calibration needs.
pub struct CalibKernels {
    /// Doorbell ping-pong + globaltimer sampler.
    pub calib_pong: CudaFunction,
    /// GPU-side Fischer contender for the timed-lock self-test.
    pub fischer: CudaFunction,
    /// `atomicCAS_system` claim racer for the atomics probe.
    pub cas_probe: CudaFunction,
}

fn one_one() -> LaunchConfig {
    LaunchConfig { grid_dim: (1, 1, 1), block_dim: (1, 1, 1), shared_mem_bytes: 0 }
}

/// Force submission of buffered work without blocking (the WDDM
/// queue holds launches back until a flush; a resident spinning
/// kernel must actually reach the GPU before the CPU pings it).
fn flush_stream(stream: &Arc<CudaStream>) {
    // SAFETY: valid stream handle; NOT_READY is the expected benign
    // result for a stream with a resident kernel.
    let _rc: cu::CUresult = unsafe { cu::cuStreamQuery(stream.cu_stream()) };
}

fn now_ns(t0: Instant) -> u64 {
    t0.elapsed().as_nanos() as u64
}

/// Run the full calibration. `region` must be freshly created (lane
/// traffic has not started).
pub fn calibrate(
    region: &PeerRegion,
    stream: &Arc<CudaStream>,
    kernels: &CalibKernels,
) -> Result<PeerCalibration, GpuPeerError> {
    let dev_base = region.dev_base();

    // ---- launch+sync baseline (rounds = 0 exits immediately) ----
    let mut launch_samples = Vec::with_capacity(32);
    for _ in 0..32 {
        let zero_rounds: u32 = 0;
        let t0 = Instant::now();
        let mut b = stream.launch_builder(&kernels.calib_pong);
        b.arg(&dev_base);
        b.arg(&zero_rounds);
        // SAFETY: argument types match the kernel signature
        // (unsigned char* base, u32 rounds); dev_base is the live
        // registered mapping.
        unsafe { b.launch(one_one()) }
            .map_err(|e| GpuPeerError::Driver(format!("calib launch: {e:?}")))?;
        stream
            .synchronize()
            .map_err(|e| GpuPeerError::Driver(format!("calib sync: {e:?}")))?;
        launch_samples.push(now_ns(t0));
    }
    launch_samples.sort_unstable();
    let launch_ns = launch_samples[launch_samples.len() / 2];

    // ---- doorbell RTT + clock pairing ----
    let rounds: u32 = (GTS_SLOTS - 8) as u32;
    region.store_u32(HDR_CALIB_PING_OFF, 0);
    region.store_u32(HDR_CALIB_PONG_OFF, 0);
    let mut b = stream.launch_builder(&kernels.calib_pong);
    b.arg(&dev_base);
    b.arg(&rounds);
    // SAFETY: as above.
    unsafe { b.launch(one_one()) }
        .map_err(|e| GpuPeerError::Driver(format!("pong launch: {e:?}")))?;
    flush_stream(stream);

    let wall = Instant::now();
    let mut rtt = Vec::with_capacity(rounds as usize);
    let mut pairs: Vec<(u64, u64, u64)> = Vec::with_capacity(rounds as usize); // (rtt, gts, mid)
    let mut doorbell_ok = true;
    for i in 1..=rounds {
        let t0 = now_ns(wall);
        region.store_u32(HDR_CALIB_PING_OFF, i);
        region.release_fence();
        loop {
            if region.load_u32(HDR_CALIB_PONG_OFF) == i {
                break;
            }
            if now_ns(wall).saturating_sub(t0) > 2_000_000_000 {
                doorbell_ok = false;
                break;
            }
            core::hint::spin_loop();
        }
        if !doorbell_ok {
            break;
        }
        let t1 = now_ns(wall);
        let gts = region.load_u64(HDR_GTS_OFF + (i as usize) * 8);
        rtt.push(t1 - t0);
        pairs.push((t1 - t0, gts, t0 + (t1 - t0) / 2));
    }
    stream
        .synchronize()
        .map_err(|e| GpuPeerError::Driver(format!("pong drain: {e:?}")))?;

    if !doorbell_ok || rtt.len() < 16 {
        // Without a doorbell there is no substrate; report what we
        // know and refuse the capabilities.
        let cal = PeerCalibration {
            rtt_min_ns: 0,
            rtt_median_ns: 0,
            rtt_p99_ns: 0,
            one_way_ns: 0,
            clock_err_ns: 0,
            delta_ns: 0,
            launch_ns,
            doorbell_ok: false,
            timed_lock_ok: false,
            sys_atomics_ok: false,
            lock_cpu_contended: 0,
            lock_gpu_contended: 0,
        };
        cal.store(region);
        return Ok(cal);
    }

    rtt.sort_unstable();
    let rtt_min_ns = rtt[0];
    let rtt_median_ns = rtt[rtt.len() / 2];
    let rtt_p99_ns = rtt[(rtt.len() * 99) / 100];

    // Clock error: difference the (gts - mid) offsets against the
    // first sample in the INTEGER domain (both clocks carry
    // epoch-sized magnitudes a double cannot hold to ns precision),
    // then take the spread over the best-RTT quartile.
    pairs.sort_unstable_by_key(|p| p.0);
    let q = (pairs.len() / 4).clamp(4, pairs.len());
    let (_, g0, m0) = pairs[0];
    let mut mn = i64::MAX;
    let mut mx = i64::MIN;
    for &(_, g, m) in pairs.iter().take(q) {
        let off = (g.wrapping_sub(g0) as i64) - (m.wrapping_sub(m0) as i64);
        mn = mn.min(off);
        mx = mx.max(off);
    }
    let clock_err_ns = ((mx - mn) / 2).max(1) as u64;

    let one_way_ns = rtt_p99_ns / 2 + clock_err_ns;
    let delta_ns = derive_delta_ns(one_way_ns);

    let validated = validate(region, stream, kernels, delta_ns)?;

    let cal = PeerCalibration {
        rtt_min_ns,
        rtt_median_ns,
        rtt_p99_ns,
        one_way_ns,
        clock_err_ns,
        delta_ns: validated.delta_ns,
        launch_ns,
        doorbell_ok: true,
        timed_lock_ok: validated.timed_lock_ok,
        sys_atomics_ok: validated.sys_atomics_ok,
        lock_cpu_contended: validated.lock_cpu_contended,
        lock_gpu_contended: validated.lock_gpu_contended,
    };
    cal.store(region);
    Ok(cal)
}

/// What a validation run establishes about this device, now.
struct Validated {
    /// The margin that survived, which a violation escalates.
    delta_ns: u64,
    timed_lock_ok: bool,
    sys_atomics_ok: bool,
    lock_cpu_contended: u32,
    lock_gpu_contended: u32,
}

/// Validate a margin against the live device and probe cross-device
/// atomics.
///
/// A violation escalates the margin twofold; a run with no contention
/// evidence is inconclusive and retried rather than counted as a pass;
/// each grant takes two consecutive contended runs without a
/// violation. The atomics flag unlocks protocols, so a false positive
/// there costs more than a false negative and it takes the same two
/// clean runs.
///
/// Every start runs this, including one that took its timings from a
/// stored record. A margin validated under one set of conditions is not
/// evidence about the next, and a margin that is wrong surfaces as a
/// lock violation under contention rather than as a slow path.
fn validate(
    region: &PeerRegion,
    stream: &Arc<CudaStream>,
    kernels: &CalibKernels,
    mut delta_ns: u64,
) -> Result<Validated, GpuPeerError> {
    let mut timed_lock_ok = false;
    let mut passes = 0u32;
    let mut lock_cpu_contended = 0u32;
    let mut lock_gpu_contended = 0u32;
    for _attempt in 0..6 {
        match fischer_self_test(region, stream, kernels, delta_ns)? {
            SelfTest::Pass { cpu_contended, gpu_contended } => {
                passes += 1;
                lock_cpu_contended = cpu_contended;
                lock_gpu_contended = gpu_contended;
                if passes >= 2 {
                    timed_lock_ok = true;
                    break;
                }
            }
            SelfTest::Violated => {
                passes = 0;
                delta_ns = (delta_ns * 2).min(100_000);
            }
            SelfTest::Inconclusive => {
                passes = 0;
            }
        }
    }
    let sys_atomics_ok = cas_conservation_probe(region, stream, kernels)?
        && cas_conservation_probe(region, stream, kernels)?;
    Ok(Validated {
        delta_ns,
        timed_lock_ok,
        sys_atomics_ok,
        lock_cpu_contended,
        lock_gpu_contended,
    })
}

/// Calibrate from a stored record's timings, validating the margin
/// against this device anyway.
///
/// The round trip, the clock error and the launch baseline are
/// properties of a (host, device, driver) combination and are taken as
/// they stand. The margin and the atomics flag are not taken: they are
/// re-established here, because what they assert is that this device
/// behaved a particular way under contention, and only a run on it can
/// say that.
pub fn calibrate_with_prior(
    region: &PeerRegion,
    stream: &Arc<CudaStream>,
    kernels: &CalibKernels,
    prior: PeerCalibration,
) -> Result<PeerCalibration, GpuPeerError> {
    let validated = validate(region, stream, kernels, derive_delta_ns(prior.one_way_ns))?;
    let cal = PeerCalibration {
        delta_ns: validated.delta_ns,
        timed_lock_ok: validated.timed_lock_ok,
        sys_atomics_ok: validated.sys_atomics_ok,
        lock_cpu_contended: validated.lock_cpu_contended,
        lock_gpu_contended: validated.lock_gpu_contended,
        ..prior
    };
    cal.store(region);
    Ok(cal)
}

/// Outcome of one Fischer self-test run.
enum SelfTest {
    /// Zero violations, with contention evidence on both sides.
    Pass { cpu_contended: u32, gpu_contended: u32 },
    /// Mutual exclusion observed broken at this Delta.
    Violated,
    /// Completed without enough contention (or did not complete):
    /// proves nothing either way.
    Inconclusive,
}

/// One CPU-vs-GPU Fischer contention run.
fn fischer_self_test(
    region: &PeerRegion,
    stream: &Arc<CudaStream>,
    kernels: &CalibKernels,
    delta_ns: u64,
) -> Result<SelfTest, GpuPeerError> {
    const ACQS: u32 = 150;
    const CS_NS: u64 = 2_000;
    // A pass without real interleaving proves nothing: both sides
    // must have contended at least this many rounds.
    const CONTENTION_FLOOR: u32 = ACQS / 10;
    region.store_u32(HDR_FISCHER_X_OFF, 0);
    region.store_i32(HDR_FISCHER_CS_OFF, 0);
    region.store_u32(HDR_FISCHER_VIOL_OFF, 0);
    region.store_u32(HDR_FISCHER_ACQS_OFF, 0);
    region.store_u32(layout::HDR_FISCHER_STARTED_OFF, 0);
    region.store_u32(layout::HDR_FISCHER_GPU_CONT_OFF, 0);
    region.release_fence();

    let dev_base = region.dev_base();
    let acqs = ACQS;
    let cs = CS_NS;
    let mut b = stream.launch_builder(&kernels.fischer);
    b.arg(&dev_base);
    b.arg(&acqs);
    b.arg(&delta_ns);
    b.arg(&cs);
    // SAFETY: argument types match flynnel_peer_fischer(u8*, u32, u64, u64).
    unsafe { b.launch(one_one()) }
        .map_err(|e| GpuPeerError::Driver(format!("fischer launch: {e:?}")))?;
    flush_stream(stream);

    // Overlap is forced, not hoped for: contend only once the GPU
    // contender is resident (its first act is raising `started`).
    let t0 = Instant::now();
    while region.load_u32(layout::HDR_FISCHER_STARTED_OFF) == 0 {
        if t0.elapsed() > Duration::from_secs(3) {
            stream.synchronize().ok();
            return Ok(SelfTest::Inconclusive);
        }
        core::hint::spin_loop();
    }

    let delta = Duration::from_nanos(delta_ns);
    let cs_hold = Duration::from_nanos(CS_NS);
    let deadline = Duration::from_secs(6);
    let mut cpu_done = 0u32;
    let mut cpu_contended = 0u32;
    for _ in 0..ACQS {
        match timed_lock::acquire_with_contention(region, delta, deadline) {
            Some(contended) => {
                if contended {
                    cpu_contended += 1;
                }
                timed_lock::critical_section_checked(region, cs_hold);
                timed_lock::release(region);
                cpu_done += 1;
            }
            None => break,
        }
    }
    // Wait for the GPU side to finish its acquisitions.
    let t0 = Instant::now();
    while region.load_u32(HDR_FISCHER_ACQS_OFF) < ACQS {
        if t0.elapsed() > Duration::from_secs(6) {
            break;
        }
        std::thread::yield_now();
    }
    stream
        .synchronize()
        .map_err(|e| GpuPeerError::Driver(format!("fischer drain: {e:?}")))?;
    let gpu_done = region.load_u32(HDR_FISCHER_ACQS_OFF);
    let gpu_contended = region.load_u32(layout::HDR_FISCHER_GPU_CONT_OFF);
    let violations = region.load_u32(HDR_FISCHER_VIOL_OFF);
    if violations > 0 {
        return Ok(SelfTest::Violated);
    }
    if cpu_done == ACQS
        && gpu_done == ACQS
        && cpu_contended >= CONTENTION_FLOOR
        && gpu_contended >= CONTENTION_FLOOR
    {
        Ok(SelfTest::Pass { cpu_contended, gpu_contended })
    } else {
        Ok(SelfTest::Inconclusive)
    }
}

/// GPU `atomicCAS_system` vs CPU `compare_exchange` claim race over
/// scratch slots (the slot slab, which carries no lane traffic during
/// calibration). Claims must be conserved exactly for the flag.
fn cas_conservation_probe(
    region: &PeerRegion,
    stream: &Arc<CudaStream>,
    kernels: &CalibKernels,
) -> Result<bool, GpuPeerError> {
    let g = region.geometry();
    let slab = g.slab_off();
    let slab_bytes = g.region_bytes() - slab;
    // n slots + gpu_won + started cells, all inside the scratch slab.
    let n = ((slab_bytes / 4).saturating_sub(32)).min(60_000) as u32;
    if n < 4_096 {
        // Region too small to probe meaningfully; refuse the flag.
        return Ok(false);
    }
    for i in 0..n as usize {
        region.store_u32(slab + i * 4, 0);
    }
    let gpu_won_off = slab + n as usize * 4;
    let started_off = gpu_won_off + 4;
    region.store_u32(gpu_won_off, 0);
    region.store_u32(started_off, 0);
    region.release_fence();

    let dev_slots = region.dev_base() + slab as u64;
    let dev_won = region.dev_base() + gpu_won_off as u64;
    let dev_started = region.dev_base() + started_off as u64;
    let mut b = stream.launch_builder(&kernels.cas_probe);
    b.arg(&dev_slots);
    b.arg(&n);
    b.arg(&dev_won);
    b.arg(&dev_started);
    // SAFETY: argument types match
    // flynnel_peer_cas_probe(u32*, u32, u32*, u32*).
    // One narrow block on purpose: with thousands of GPU threads the
    // sweep finishes in microseconds and the CPU never contends; 64
    // threads make both sides sweep for a comparable duration so the
    // claim fronts interleave densely for the whole run.
    unsafe {
        b.launch(LaunchConfig {
            grid_dim: (1, 1, 1),
            block_dim: (64, 1, 1),
            shared_mem_bytes: 0,
        })
    }
    .map_err(|e| GpuPeerError::Driver(format!("cas probe launch: {e:?}")))?;
    flush_stream(stream);

    // Genuine overlap is a validity requirement, not a hope: wait
    // until the kernel reports it is resident and claiming (top-down)
    // before the CPU starts claiming bottom-up; the walks collide
    // wherever the two sides meet.
    let t0 = Instant::now();
    while region.load_u32(started_off) == 0 {
        if t0.elapsed() > Duration::from_secs(3) {
            stream.synchronize().ok();
            return Ok(false); // never started: inconclusive, refuse
        }
        core::hint::spin_loop();
    }

    // CPU contends through real atomic CAS on the same words.
    let base = region.base_addr() as usize;
    let mut cpu_won = 0u64;
    for i in 0..n as usize {
        // SAFETY: 4-aligned live mapping word inside the region; the
        // probe's entire purpose is contending this word with the
        // GPU's CAS, so an atomic view is the correct access.
        let a = unsafe { core::sync::atomic::AtomicU32::from_ptr((base + slab + i * 4) as *mut u32) };
        if a
            .compare_exchange(
                0,
                1,
                core::sync::atomic::Ordering::SeqCst,
                core::sync::atomic::Ordering::SeqCst,
            )
            .is_ok()
        {
            cpu_won += 1;
        }
    }
    stream
        .synchronize()
        .map_err(|e| GpuPeerError::Driver(format!("cas probe drain: {e:?}")))?;
    let gpu_won = region.load_u32(gpu_won_off) as u64;
    let mut unclaimed = 0u64;
    for i in 0..n as usize {
        if region.load_u32(slab + i * 4) == 0 {
            unclaimed += 1;
        }
    }
    // Conservation and real contention: every slot claimed exactly
    // once, with both sides winning a meaningful share (>= 5% each).
    // A lopsided sweep means the race barely happened and proves
    // nothing - refuse the flag.
    let conserved = unclaimed == 0 && cpu_won + gpu_won == n as u64;
    let floor = n as u64 / 20;
    let contended = cpu_won >= floor && gpu_won >= floor;
    Ok(conserved && contended)
}

#[cfg(test)]
mod tests {
    use super::derive_delta_ns;

    #[test]
    fn delta_derivation_clamps() {
        assert_eq!(derive_delta_ns(0), 5_000);
        assert_eq!(derive_delta_ns(100), 5_000);
        assert_eq!(derive_delta_ns(1_600), 16_000);
        assert_eq!(derive_delta_ns(2_500), 25_000);
        assert_eq!(derive_delta_ns(50_000), 100_000);
    }
}