scxtop 1.1.3

sched_ext scheduler tool for observability
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
// Copyright (c) Meta Platforms, Inc. and affiliates.
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2.

use scx_utils::Topology;
use scxtop::bpf_intf;
use scxtop::bpf_skel::types::bpf_event;
use scxtop::mcp::{create_shared_stats, SharedStats};
use std::mem::MaybeUninit;
use std::sync::Arc;

/// Helper function to get system topology for testing
/// Returns None if topology cannot be loaded
fn try_create_test_topology() -> Option<Topology> {
    Topology::new().ok()
}

/// Helper function to create a sched_switch event
fn create_sched_switch_event(
    cpu: u32,
    timestamp_ns: u64,
    prev_pid: u32,
    next_pid: u32,
    next_comm: &str,
    next_dsq_id: u64,
    prev_dsq_id: u64,
) -> bpf_event {
    create_sched_switch_event_with_wakeup(
        cpu,
        timestamp_ns,
        prev_pid,
        next_pid,
        next_comm,
        next_dsq_id,
        prev_dsq_id,
        0,
    )
}

fn create_sched_switch_event_with_wakeup(
    cpu: u32,
    timestamp_ns: u64,
    prev_pid: u32,
    next_pid: u32,
    next_comm: &str,
    next_dsq_id: u64,
    prev_dsq_id: u64,
    next_wakeup_ts: u64,
) -> bpf_event {
    let mut event: bpf_event = unsafe { MaybeUninit::zeroed().assume_init() };
    event.r#type = bpf_intf::event_type_SCHED_SWITCH as i32;
    event.cpu = cpu;
    event.ts = timestamp_ns;

    unsafe {
        event.event.sched_switch.prev_pid = prev_pid;
        event.event.sched_switch.next_pid = next_pid;
        event.event.sched_switch.next_dsq_id = next_dsq_id;
        event.event.sched_switch.prev_dsq_id = prev_dsq_id;
        event.event.sched_switch.next_wakeup_ts = next_wakeup_ts;

        // Copy command name
        let comm_bytes = next_comm.as_bytes();
        let len = std::cmp::min(comm_bytes.len(), event.event.sched_switch.next_comm.len());
        event.event.sched_switch.next_comm[..len].copy_from_slice(&comm_bytes[..len]);
    }

    event
}

/// Helper function to create a sched_wakeup event
fn create_sched_wakeup_event(cpu: u32, timestamp_ns: u64, pid: u32, comm: &str) -> bpf_event {
    let mut event: bpf_event = unsafe { MaybeUninit::zeroed().assume_init() };
    event.r#type = bpf_intf::event_type_SCHED_WAKEUP as i32;
    event.cpu = cpu;
    event.ts = timestamp_ns;

    unsafe {
        event.event.wakeup.pid = pid;

        // Copy command name
        let comm_bytes = comm.as_bytes();
        let len = std::cmp::min(comm_bytes.len(), event.event.wakeup.comm.len());
        event.event.wakeup.comm[..len].copy_from_slice(&comm_bytes[..len]);
    }

    event
}

/// Helper function to create a sched_migrate event
fn create_sched_migrate_event(cpu: u32, timestamp_ns: u64) -> bpf_event {
    let mut event: bpf_event = unsafe { MaybeUninit::zeroed().assume_init() };
    event.r#type = bpf_intf::event_type_SCHED_MIGRATE as i32;
    event.cpu = cpu;
    event.ts = timestamp_ns;

    event
}

#[test]
fn test_shared_stats_new() {
    let stats = SharedStats::new();
    assert_eq!(stats.cpu_stats.len(), 0);
    assert_eq!(stats.process_stats.len(), 0);
    assert_eq!(stats.dsq_stats.len(), 0);
    assert!(stats.start_time_ns > 0);
}

#[test]
fn test_create_shared_stats() {
    let stats_handle = create_shared_stats();
    let stats = stats_handle.read().unwrap();
    assert_eq!(stats.cpu_stats.len(), 0);
    assert_eq!(stats.process_stats.len(), 0);
}

#[test]
fn test_sched_switch_updates_cpu_stats() {
    let mut stats = SharedStats::new();
    stats.enable_tracking();
    let event = create_sched_switch_event(0, 1000000, 100, 200, "test_proc", u64::MAX, u64::MAX);

    stats.update_from_event(&event);

    assert_eq!(stats.cpu_stats.len(), 1);
    let cpu_stats = stats.cpu_stats.get(&0).unwrap();
    assert_eq!(cpu_stats.cpu_id, 0);
    assert_eq!(cpu_stats.nr_switches, 1);
    assert_eq!(cpu_stats.nr_wakeups, 0);
}

#[test]
fn test_sched_switch_updates_process_stats() {
    let mut stats = SharedStats::new();
    stats.enable_tracking();
    let event = create_sched_switch_event(0, 1000000, 100, 200, "test_proc", u64::MAX, u64::MAX);

    stats.update_from_event(&event);

    assert_eq!(stats.process_stats.len(), 1);
    let proc_stats = stats.process_stats.get(&200).unwrap();
    assert_eq!(proc_stats.pid, 200);
    assert_eq!(proc_stats.comm, "test_proc");
    assert_eq!(proc_stats.nr_switches, 1);
}

#[test]
fn test_multiple_sched_switches() {
    let mut stats = SharedStats::new();
    stats.enable_tracking();

    // First switch on CPU 0
    let event1 = create_sched_switch_event(0, 1000000, 100, 200, "proc1", u64::MAX, u64::MAX);
    stats.update_from_event(&event1);

    // Second switch on CPU 0
    let event2 = create_sched_switch_event(0, 2000000, 200, 300, "proc2", u64::MAX, u64::MAX);
    stats.update_from_event(&event2);

    // Switch on CPU 1
    let event3 = create_sched_switch_event(1, 1500000, 400, 500, "proc3", u64::MAX, u64::MAX);
    stats.update_from_event(&event3);

    // Verify CPU stats
    assert_eq!(stats.cpu_stats.len(), 2);
    assert_eq!(stats.cpu_stats.get(&0).unwrap().nr_switches, 2);
    assert_eq!(stats.cpu_stats.get(&1).unwrap().nr_switches, 1);

    // Verify process stats
    assert_eq!(stats.process_stats.len(), 3);
    assert_eq!(stats.process_stats.get(&200).unwrap().nr_switches, 1);
    assert_eq!(stats.process_stats.get(&300).unwrap().nr_switches, 1);
    assert_eq!(stats.process_stats.get(&500).unwrap().nr_switches, 1);
}

#[test]
fn test_sched_wakeup_updates_stats() {
    let mut stats = SharedStats::new();
    stats.enable_tracking();
    let event = create_sched_wakeup_event(0, 1000000, 200, "test_proc");

    stats.update_from_event(&event);

    // Verify CPU stats
    assert_eq!(stats.cpu_stats.len(), 1);
    let cpu_stats = stats.cpu_stats.get(&0).unwrap();
    assert_eq!(cpu_stats.nr_wakeups, 1);

    // Verify process stats
    assert_eq!(stats.process_stats.len(), 1);
    let proc_stats = stats.process_stats.get(&200).unwrap();
    assert_eq!(proc_stats.nr_wakeups, 1);
    assert_eq!(proc_stats.comm, "test_proc");
}

#[test]
fn test_wakeup_latency_calculation() {
    let mut stats = SharedStats::new();
    stats.enable_tracking();

    // Switch at t=1500000 with wakeup_ts=1000000 (latency = 500000)
    let switch = create_sched_switch_event_with_wakeup(
        0,
        1500000,
        100,
        200,
        "test_proc",
        u64::MAX,
        u64::MAX,
        1000000,
    );
    stats.update_from_event(&switch);

    // Verify CPU latency stats
    let cpu_stats = stats.cpu_stats.get(&0).unwrap();
    assert_eq!(cpu_stats.total_latency_ns, 500000);
    assert_eq!(cpu_stats.min_latency_ns, 500000);
    assert_eq!(cpu_stats.max_latency_ns, 500000);
    assert_eq!(cpu_stats.latency_samples, 1);

    // Verify process latency stats
    let proc_stats = stats.process_stats.get(&200).unwrap();
    assert_eq!(proc_stats.total_latency_ns, 500000);
    assert_eq!(proc_stats.min_latency_ns, 500000);
    assert_eq!(proc_stats.max_latency_ns, 500000);
    assert_eq!(proc_stats.latency_samples, 1);
}

#[test]
fn test_multiple_latency_samples() {
    let mut stats = SharedStats::new();
    stats.enable_tracking();

    // First switch with wakeup_ts (latency = 500000)
    let switch1 = create_sched_switch_event_with_wakeup(
        0,
        1500000,
        100,
        200,
        "test_proc",
        u64::MAX,
        u64::MAX,
        1000000,
    );
    stats.update_from_event(&switch1);

    // Second switch with wakeup_ts (latency = 300000)
    let switch2 = create_sched_switch_event_with_wakeup(
        0,
        2300000,
        100,
        200,
        "test_proc",
        u64::MAX,
        u64::MAX,
        2000000,
    );
    stats.update_from_event(&switch2);

    // Third switch with wakeup_ts (latency = 700000)
    let switch3 = create_sched_switch_event_with_wakeup(
        0,
        3700000,
        100,
        200,
        "test_proc",
        u64::MAX,
        u64::MAX,
        3000000,
    );
    stats.update_from_event(&switch3);

    let proc_stats = stats.process_stats.get(&200).unwrap();
    assert_eq!(proc_stats.latency_samples, 3);
    assert_eq!(proc_stats.total_latency_ns, 1500000); // 500000 + 300000 + 700000
    assert_eq!(proc_stats.min_latency_ns, 300000);
    assert_eq!(proc_stats.max_latency_ns, 700000);
}

#[test]
fn test_sched_migrate_updates_stats() {
    let mut stats = SharedStats::new();
    stats.enable_tracking();
    let event = create_sched_migrate_event(0, 1000000);

    stats.update_from_event(&event);

    assert_eq!(stats.cpu_stats.len(), 1);
    let cpu_stats = stats.cpu_stats.get(&0).unwrap();
    assert_eq!(cpu_stats.nr_migrations, 1);
}

#[test]
fn test_multiple_migrations() {
    let mut stats = SharedStats::new();
    stats.enable_tracking();

    let event1 = create_sched_migrate_event(0, 1000000);
    stats.update_from_event(&event1);

    let event2 = create_sched_migrate_event(0, 2000000);
    stats.update_from_event(&event2);

    let event3 = create_sched_migrate_event(1, 1500000);
    stats.update_from_event(&event3);

    assert_eq!(stats.cpu_stats.get(&0).unwrap().nr_migrations, 2);
    assert_eq!(stats.cpu_stats.get(&1).unwrap().nr_migrations, 1);
}

#[test]
fn test_dsq_stats_tracking() {
    let mut stats = SharedStats::new();
    stats.enable_tracking();

    // Switch with DSQ IDs
    let event = create_sched_switch_event(0, 1000000, 100, 200, "proc", 42, 43);
    stats.update_from_event(&event);

    assert_eq!(stats.dsq_stats.len(), 2);
    assert_eq!(stats.dsq_stats.get(&42).unwrap().nr_dispatches, 1);
    assert_eq!(stats.dsq_stats.get(&43).unwrap().nr_enqueues, 1);
}

#[test]
fn test_dsq_stats_ignores_max_value() {
    let mut stats = SharedStats::new();
    stats.enable_tracking();

    // Switch with MAX DSQ IDs (should be ignored)
    let event = create_sched_switch_event(0, 1000000, 100, 200, "proc", u64::MAX, u64::MAX);
    stats.update_from_event(&event);

    assert_eq!(stats.dsq_stats.len(), 0);
}

#[test]
fn test_get_cpu_stats_json() {
    let mut stats = SharedStats::new();
    stats.enable_tracking();

    // Add some events
    let wakeup = create_sched_wakeup_event(0, 1000000, 200, "test");
    stats.update_from_event(&wakeup);

    let switch = create_sched_switch_event_with_wakeup(
        0,
        1500000,
        100,
        200,
        "test",
        u64::MAX,
        u64::MAX,
        1000000,
    );
    stats.update_from_event(&switch);

    let json = stats.get_cpu_stats_json();

    assert!(json["cpus"].is_array());
    assert_eq!(json["total_cpus"], 1);

    let cpu0 = &json["cpus"][0];
    assert_eq!(cpu0["cpu_id"], 0);
    assert_eq!(cpu0["nr_switches"], 1);
    assert_eq!(cpu0["nr_wakeups"], 1);
    assert_eq!(cpu0["latency"]["avg_ns"], 500000);
}

#[test]
fn test_get_process_stats_json() {
    let mut stats = SharedStats::new();
    stats.enable_tracking();

    // Add events for multiple processes
    for pid in 100..105 {
        let event = create_sched_switch_event(0, 1000000, pid - 1, pid, "proc", u64::MAX, u64::MAX);
        stats.update_from_event(&event);
    }

    let json = stats.get_process_stats_json(Some(3));

    assert!(json["processes"].is_array());
    assert_eq!(json["total_processes"], 5);
    assert_eq!(json["shown"], 3); // Limited to 3
}

#[test]
fn test_get_dsq_stats_json() {
    let mut stats = SharedStats::new();
    stats.enable_tracking();

    // Add DSQ events
    let event1 = create_sched_switch_event(0, 1000000, 100, 200, "proc", 10, 20);
    stats.update_from_event(&event1);

    let event2 = create_sched_switch_event(0, 2000000, 200, 300, "proc", 10, 20);
    stats.update_from_event(&event2);

    let json = stats.get_dsq_stats_json();

    assert!(json["dsqs"].is_array());
    assert_eq!(json["total_dsqs"], 2);
}

#[test]
fn test_get_llc_stats_json() {
    let Some(topology) = try_create_test_topology() else {
        // Skip test if topology is unavailable
        return;
    };

    let mut stats = SharedStats::new();
    stats.enable_tracking();

    // Add events on first two CPUs
    if let Some(cpu1) = topology.all_cpus.keys().next().copied() {
        let event1 =
            create_sched_switch_event(cpu1 as u32, 1000000, 100, 200, "proc", u64::MAX, u64::MAX);
        stats.update_from_event(&event1);
    }

    if let Some(cpu2) = topology.all_cpus.keys().nth(1).copied() {
        let event2 =
            create_sched_switch_event(cpu2 as u32, 2000000, 200, 300, "proc", u64::MAX, u64::MAX);
        stats.update_from_event(&event2);
    }

    let json = stats.get_llc_stats_json(&topology);

    assert!(json["llcs"].is_array());
    assert!(json["total_llcs"].as_u64().unwrap() > 0);
}

#[test]
fn test_get_node_stats_json() {
    let Some(topology) = try_create_test_topology() else {
        // Skip test if topology is unavailable
        return;
    };

    let mut stats = SharedStats::new();
    stats.enable_tracking();

    // Add events on first two CPUs
    if let Some(cpu1) = topology.all_cpus.keys().next().copied() {
        let event1 =
            create_sched_switch_event(cpu1 as u32, 1000000, 100, 200, "proc", u64::MAX, u64::MAX);
        stats.update_from_event(&event1);
    }

    if let Some(cpu2) = topology.all_cpus.keys().nth(1).copied() {
        let event2 =
            create_sched_switch_event(cpu2 as u32, 2000000, 200, 300, "proc", u64::MAX, u64::MAX);
        stats.update_from_event(&event2);
    }

    let json = stats.get_node_stats_json(&topology);

    assert!(json["nodes"].is_array());
    assert!(json["total_nodes"].as_u64().unwrap() > 0);
}

#[test]
fn test_thread_safe_shared_stats() {
    use std::thread;

    let stats_handle = create_shared_stats();
    let stats_handle_clone = Arc::clone(&stats_handle);

    // Spawn a thread that updates stats
    let handle = thread::spawn(move || {
        let mut stats = stats_handle_clone.write().unwrap();
        stats.enable_tracking();
        let event = create_sched_switch_event(0, 1000000, 100, 200, "test", u64::MAX, u64::MAX);
        stats.update_from_event(&event);
    });

    handle.join().unwrap();

    // Read stats from main thread
    let stats = stats_handle.read().unwrap();
    assert_eq!(stats.cpu_stats.len(), 1);
    assert_eq!(stats.process_stats.len(), 1);
}