ktstr 0.4.14

Test harness for Linux process schedulers
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
//! Worker-level checks: `assert_not_starved`, `assert_isolation`,
//! gap / spread / stuck classification, single-worker passthroughs,
//! and negative-diagnostic-message tests that pin the
//! human-readable strings every consumer greps for.

use super::tests_common::rpt;
use super::*;

#[test]
fn healthy_pass() {
    let r = assert_not_starved(&[
        rpt(1, 1000, 5_000_000_000, 500_000_000, &[0, 1], 50),
        rpt(2, 1000, 5_000_000_000, 600_000_000, &[0, 1], 60),
        rpt(3, 1000, 5_000_000_000, 550_000_000, &[0, 1], 45),
    ]);
    assert!(r.passed, "{:?}", r.details);
}

#[test]
fn starved_fail() {
    let r = assert_not_starved(&[
        rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0], 50),
        rpt(2, 0, 5e9 as u64, 5e9 as u64, &[0], 50),
    ]);
    assert!(!r.passed);
    assert!(r.details.iter().any(|d| d.contains("starved")));
}

#[test]
fn unfair_spread_fail() {
    let r = assert_not_starved(&[
        rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0, 1], 50), // 10%
        rpt(2, 500, 5e9 as u64, 4e9 as u64, &[0, 1], 50),  // 80%
        rpt(3, 800, 5e9 as u64, 2e9 as u64, &[0, 1], 50),  // 40%
    ]);
    assert!(!r.passed);
    assert!(r.details.iter().any(|d| d.contains("unfair")));
}

#[test]
fn fair_oversubscribed_pass() {
    let r = assert_not_starved(&[
        rpt(1, 100, 5e9 as u64, (3.75e9) as u64, &[0], 50),
        rpt(2, 100, 5e9 as u64, (3.70e9) as u64, &[0], 50),
        rpt(3, 100, 5e9 as u64, (3.80e9) as u64, &[0], 50),
        rpt(4, 100, 5e9 as u64, (3.75e9) as u64, &[0], 50),
    ]);
    assert!(r.passed, "{:?}", r.details);
}

#[test]
fn stuck_fail() {
    let threshold = gap_threshold_ms();
    let r = assert_not_starved(&[
        rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0], 50),
        rpt(2, 1000, 5e9 as u64, 5e8 as u64, &[0], threshold + 500),
    ]);
    assert!(!r.passed);
    assert!(r.details.iter().any(|d| d.contains("stuck")));
}

#[test]
fn isolation_pass() {
    let expected: BTreeSet<usize> = [0, 1, 2, 3].into_iter().collect();
    let r = assert_isolation(
        &[
            rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0, 1], 50),
            rpt(2, 1000, 5e9 as u64, 5e8 as u64, &[2, 3], 50),
        ],
        &expected,
    );
    assert!(r.passed);
}

#[test]
fn isolation_fail() {
    let expected: BTreeSet<usize> = [0, 1].into_iter().collect();
    let r = assert_isolation(
        &[rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0, 1, 4], 50)],
        &expected,
    );
    assert!(!r.passed);
    assert!(r.details.iter().any(|d| d.contains("unexpected")));
}

#[test]
fn spread_boundary() {
    let threshold = spread_threshold_pct();
    // At threshold exactly - pass
    // Worker 1: 10% off-CPU, Worker 2: 10%+threshold off-CPU
    let at_threshold_ns = ((10.0 + threshold) / 100.0 * 5e9) as u64;
    let r = assert_not_starved(&[
        rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0], 50), // 10%
        rpt(2, 1000, 5e9 as u64, at_threshold_ns, &[0], 50), // 10% + threshold
    ]);
    assert!(
        r.passed,
        "{threshold}% spread at threshold: {:?}",
        r.details
    );
    // Above threshold - fail
    let above_ns = ((15.0 + threshold) / 100.0 * 5e9) as u64;
    let r = assert_not_starved(&[
        rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0], 50), // 10%
        rpt(2, 1000, 5e9 as u64, above_ns, &[0], 50),   // 10% + threshold + 5%
    ]);
    assert!(!r.passed, "spread above {threshold}% should fail");
}

#[test]
fn empty_pass() {
    assert!(assert_not_starved(&[]).passed);
}

#[test]
fn zero_wall_time() {
    let r = assert_not_starved(&[
        rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0], 50),
        rpt(2, 0, 0, 0, &[], 0),
    ]);
    assert!(!r.passed);
    assert!(r.details.iter().any(|d| d.contains("starved")));
}

#[test]
fn single_worker_always_pass() {
    let r = assert_not_starved(&[rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0, 1], 50)]);
    assert!(r.passed);
    assert_eq!(r.stats.total_workers, 1);
    assert_eq!(r.stats.cgroups.len(), 1);
}

#[test]
fn stats_accuracy() {
    let r = assert_not_starved(&[
        rpt(1, 1000, 5e9 as u64, 1e9 as u64, &[0], 50),  // 20%
        rpt(2, 1000, 5e9 as u64, 15e8 as u64, &[1], 60), // 30%
    ]);
    assert!(r.passed); // spread = 10% < 15%
    let c = &r.stats.cgroups[0];
    assert_eq!(c.num_workers, 2);
    assert_eq!(c.num_cpus, 2);
    assert!((c.min_off_cpu_pct - 20.0).abs() < 0.1);
    assert!((c.max_off_cpu_pct - 30.0).abs() < 0.1);
    assert!((c.spread - 10.0).abs() < 0.1);
    assert!((c.avg_off_cpu_pct - 25.0).abs() < 0.1);
}

#[test]
fn isolation_empty_reports() {
    let expected: BTreeSet<usize> = [0, 1].into_iter().collect();
    assert!(assert_isolation(&[], &expected).passed);
}

#[test]
fn gap_boundary_at_threshold_pass() {
    let threshold = gap_threshold_ms();
    let r = assert_not_starved(&[rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0], threshold)]);
    assert!(r.passed, "gap at threshold should pass: {:?}", r.details);
}

#[test]
fn gap_boundary_above_threshold_fail() {
    let threshold = gap_threshold_ms();
    let r = assert_not_starved(&[rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0], threshold + 1)]);
    assert!(!r.passed);
    assert!(r.details.iter().any(|d| d.contains("stuck")));
}

#[test]
fn multiple_stuck_workers() {
    let threshold = gap_threshold_ms();
    let r = assert_not_starved(&[
        rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0], threshold + 500),
        rpt(2, 1000, 5e9 as u64, 5e8 as u64, &[1], threshold + 1500),
    ]);
    assert!(!r.passed);
    let stuck_count = r.details.iter().filter(|d| d.contains("stuck")).count();
    assert_eq!(stuck_count, 2, "both workers should be flagged stuck");
}

#[test]
fn migration_tracking() {
    let mut report = rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0, 1, 2], 50);
    report.migration_count = 5;
    let r = assert_not_starved(&[report]);
    assert_eq!(r.stats.total_migrations, 5);
}

#[test]
fn single_worker_spread_zero() {
    let r = assert_not_starved(&[rpt(1, 500, 5e9 as u64, 25e8 as u64, &[0, 1], 50)]);
    assert!(r.passed);
    let c = &r.stats.cgroups[0];
    assert!((c.spread - 0.0).abs() < f64::EPSILON);
}

#[test]
fn zero_wall_time_nonzero_work() {
    // wall_time=0 but work_units>0: the worker did work but the timer
    // didn't advance. Should not produce a starved failure since work was done.
    // The off_cpu_pct computation skips this worker (no pcts entry).
    let r = assert_not_starved(&[rpt(1, 100, 0, 0, &[0], 0)]);
    assert!(
        r.passed,
        "nonzero work with zero wall_time: {:?}",
        r.details
    );
}

#[test]
fn isolation_empty_expected_set() {
    // Empty expected set means no CPUs are "expected", so any CPU
    // used by the worker is unexpected. difference(empty) == worker's set.
    let expected: BTreeSet<usize> = BTreeSet::new();
    let r = assert_isolation(
        &[rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0, 1], 50)],
        &expected,
    );
    // Worker used CPUs {0,1}, expected is empty, so all are unexpected.
    assert!(!r.passed);
    assert!(r.details.iter().any(|d| d.contains("unexpected")));
}

#[test]
fn isolation_worker_used_no_cpus() {
    // Worker used no CPUs -- difference with expected is empty, so passes.
    let expected: BTreeSet<usize> = [0, 1].into_iter().collect();
    let r = assert_isolation(&[rpt(1, 0, 0, 0, &[], 0)], &expected);
    assert!(r.passed);
}

#[test]
fn isolation_all_unexpected_cpus() {
    let expected: BTreeSet<usize> = [0, 1].into_iter().collect();
    let r = assert_isolation(
        &[rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[4, 5, 6], 50)],
        &expected,
    );
    assert!(!r.passed);
    assert!(r.details.iter().any(|d| d.contains("unexpected")));
}

// ---------------------------------------------------------------
// Negative tests: check that diagnostics catch controlled failures
// ---------------------------------------------------------------

#[test]
fn neg_starvation_zero_work_detected() {
    let r = assert_not_starved(&[
        rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0, 1], 50),
        rpt(2, 0, 5e9 as u64, 0, &[0], 0), // starved
        rpt(3, 1000, 5e9 as u64, 5e8 as u64, &[0, 1], 50),
    ]);
    assert!(!r.passed, "starvation must be caught");
    let starved = r.details.iter().filter(|d| d.contains("starved")).count();
    assert_eq!(starved, 1, "exactly one starved worker expected");
    // Format: "tid 2 starved (0 work units)"
    let detail = r.details.iter().find(|d| d.contains("starved")).unwrap();
    assert!(
        detail.contains("tid 2"),
        "must name the starved tid: {detail}"
    );
    assert!(
        detail.contains("0 work units"),
        "must state zero work: {detail}"
    );
}

#[test]
fn neg_isolation_violation_outside_cpuset() {
    let expected: BTreeSet<usize> = [0, 1].into_iter().collect();
    let reports = [
        rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0, 1], 50),
        rpt(2, 1000, 5e9 as u64, 5e8 as u64, &[0, 1, 2, 3], 50),
    ];
    let r = assert_isolation(&reports, &expected);
    assert!(!r.passed, "isolation violation must be caught");
    // Format: "tid 2 ran on unexpected CPUs {2, 3}"
    let detail = r
        .details
        .iter()
        .find(|d| d.contains("unexpected CPUs"))
        .unwrap();
    assert!(
        detail.contains("tid 2"),
        "must name violating tid: {detail}"
    );
    assert!(detail.contains("2"), "must list out-of-set CPU 2: {detail}");
    assert!(detail.contains("3"), "must list out-of-set CPU 3: {detail}");
    // Worker 1 ran only on {0,1} which is within expected — no violation.
    assert_eq!(r.details.len(), 1, "only tid 2 should violate");
}

#[test]
fn neg_unfairness_extreme_spread_detected() {
    let r = assert_not_starved(&[
        rpt(1, 100, 5e9 as u64, 25e7 as u64, &[0, 1], 50), // 5%
        rpt(2, 5000, 5e9 as u64, 475e7 as u64, &[0, 1], 50), // 95%
    ]);
    assert!(!r.passed, "extreme unfairness must be caught");
    // Format: "unfair cgroup: spread=90% (5-95%) 2 workers on 2 cpus (threshold 15%)"
    let detail = r.details.iter().find(|d| d.contains("unfair")).unwrap();
    assert!(
        detail.contains("spread="),
        "must include spread value: {detail}"
    );
    assert!(
        detail.contains("workers"),
        "must include worker count: {detail}"
    );
    assert!(detail.contains("cpus"), "must include cpu count: {detail}");
    // Threshold must appear so a regression dropping the bound surfaces here.
    // The literal value comes from `spread_threshold_pct()` which differs
    // between debug and release builds; pin only the textual prefix.
    assert!(
        detail.contains("threshold "),
        "must include threshold bound: {detail}"
    );
    let c = &r.stats.cgroups[0];
    assert!(
        c.spread > 80.0,
        "spread should be >80%, got {:.1}",
        c.spread
    );
    assert_eq!(c.num_workers, 2);
    assert_eq!(c.num_cpus, 2);
    assert!(
        c.min_off_cpu_pct < 10.0,
        "min pct should be ~5%: {:.1}",
        c.min_off_cpu_pct
    );
    assert!(
        c.max_off_cpu_pct > 90.0,
        "max pct should be ~95%: {:.1}",
        c.max_off_cpu_pct
    );
}

#[test]
fn neg_scheduling_gap_exceeds_threshold() {
    let threshold = gap_threshold_ms();
    let gap = threshold + 2000;
    let r = assert_not_starved(&[
        rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0], 50),
        rpt(2, 1000, 5e9 as u64, 5e8 as u64, &[1], gap),
    ]);
    assert!(!r.passed, "scheduling gap must be caught");
    // Format: "tid 2 stuck {gap}ms on cpu1 at +1000ms (threshold 2000ms)"
    let detail = r.details.iter().find(|d| d.contains("stuck")).unwrap();
    assert!(
        detail.contains(&format!("{}ms", gap)),
        "must include gap duration: {detail}"
    );
    assert!(
        detail.contains("on cpu"),
        "must include CPU number: {detail}"
    );
    assert!(
        detail.contains("at +"),
        "must include timing offset: {detail}"
    );
    assert!(detail.contains("cpu1"), "gap is on cpu1: {detail}");
    // tid must be named so an operator triaging a multi-worker cgroup can
    // identify the offender without reverse-mapping CPU placement.
    assert!(
        detail.contains("tid 2"),
        "must name violating tid (2): {detail}"
    );
    // Threshold appears for parity with the AssertPlan custom-threshold path
    // and so a regression dropping the bound from the default-path message
    // surfaces here.
    assert!(
        detail.contains(&format!("threshold {}ms", threshold)),
        "must include default-path threshold: {detail}"
    );
    // Stats must reflect the gap.
    assert_eq!(r.stats.worst_gap_ms, gap);
    assert_eq!(r.stats.worst_gap_cpu, 1);
}

#[test]
fn neg_plan_custom_gap_catches_lower_threshold() {
    let plan = AssertPlan::new().check_not_starved().max_gap_ms(500);
    let reports = [
        rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0], 50),
        rpt(2, 1000, 5e9 as u64, 5e8 as u64, &[1], 1000),
    ];
    let r = plan.assert_cgroup(&reports, None, None);
    assert!(!r.passed, "custom 500ms threshold must catch 1000ms gap");
    // Format: "tid 2 stuck 1000ms on cpu1 at +1000ms (threshold 500ms)"
    let detail = r.details.iter().find(|d| d.contains("stuck")).unwrap();
    assert!(
        detail.contains("1000ms"),
        "must include gap duration: {detail}"
    );
    assert!(detail.contains("cpu1"), "must include CPU: {detail}");
    assert!(
        detail.contains("threshold 500ms"),
        "must include custom threshold: {detail}"
    );
    // tid must be named; pins parity with the bare-path message.
    assert!(
        detail.contains("tid 2"),
        "must name violating tid (2): {detail}"
    );
}

#[test]
fn neg_isolation_plus_starvation_both_reported() {
    let plan = AssertPlan::new().check_not_starved().check_isolation();
    let expected: BTreeSet<usize> = [0, 1].into_iter().collect();
    let reports = [
        rpt(1, 0, 5e9 as u64, 0, &[0], 0),
        rpt(2, 1000, 5e9 as u64, 5e8 as u64, &[4, 5], 50),
    ];
    let r = plan.assert_cgroup(&reports, Some(&expected), None);
    assert!(!r.passed);
    // Starvation detail must name tid 1 with "0 work units".
    let starved_detail = r.details.iter().find(|d| d.contains("starved")).unwrap();
    assert!(
        starved_detail.contains("tid 1"),
        "starved tid: {starved_detail}"
    );
    assert!(
        starved_detail.contains("0 work units"),
        "format: {starved_detail}"
    );
    // Isolation detail must name tid 2 with CPUs {4, 5}.
    let iso_detail = r.details.iter().find(|d| d.contains("unexpected")).unwrap();
    assert!(iso_detail.contains("tid 2"), "isolation tid: {iso_detail}");
    assert!(iso_detail.contains("4"), "must list CPU 4: {iso_detail}");
    assert!(iso_detail.contains("5"), "must list CPU 5: {iso_detail}");
}

#[test]
fn neg_assert_cgroup_via_assert_struct() {
    let v = Assert::NO_OVERRIDES.check_not_starved().check_isolation();
    let expected: BTreeSet<usize> = [0].into_iter().collect();
    let reports = [rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0, 1, 2], 50)];
    let r = v.assert_cgroup(&reports, Some(&expected));
    assert!(
        !r.passed,
        "Assert.assert_cgroup must catch isolation failure"
    );
    let detail = r.details.iter().find(|d| d.contains("unexpected")).unwrap();
    assert!(detail.contains("tid 1"), "must name tid: {detail}");
    assert!(detail.contains("1"), "must list CPU 1: {detail}");
    assert!(detail.contains("2"), "must list CPU 2: {detail}");
}

#[test]
fn neg_plan_custom_gap_passes_below_threshold() {
    let plan = AssertPlan::new().check_not_starved().max_gap_ms(5000);
    let reports = [
        rpt(1, 1000, 5e9 as u64, 5e8 as u64, &[0], 50),
        rpt(2, 1000, 5e9 as u64, 5e8 as u64, &[1], 1000),
    ];
    let r = plan.assert_cgroup(&reports, None, None);
    // 1000ms gap < 5000ms threshold, so it passes.
    let has_stuck = r.details.iter().any(|d| d.contains("stuck"));
    assert!(!has_stuck, "1000ms gap should pass 5000ms threshold");
}