xref: /linux/tools/testing/selftests/sched_ext/nohz_tick.bpf.c (revision 298bb2b8903323f6ef2eab4819a2e477765f0ff1)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES
4  *
5  * Exercise tick dependency transitions between infinite and finite slices.
6  */
7 #include <scx/common.bpf.h>
8 
9 char _license[] SEC("license") = "GPL";
10 
11 const volatile s32 test_cpu;
12 bool finite_phase;
13 u64 nr_inf_running;
14 u64 nr_finite_running;
15 u64 nr_finite_ticks;
16 
17 UEI_DEFINE(uei);
18 
BPF_STRUCT_OPS(nohz_tick_select_cpu,struct task_struct * p,s32 prev_cpu,u64 wake_flags)19 s32 BPF_STRUCT_OPS(nohz_tick_select_cpu, struct task_struct *p, s32 prev_cpu,
20 		   u64 wake_flags)
21 {
22 	return prev_cpu;
23 }
24 
BPF_STRUCT_OPS(nohz_tick_enqueue,struct task_struct * p,u64 enq_flags)25 void BPF_STRUCT_OPS(nohz_tick_enqueue, struct task_struct *p, u64 enq_flags)
26 {
27 	u64 slice = finite_phase ? 1000000ULL : SCX_SLICE_INF;
28 
29 	scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL, slice, enq_flags);
30 	if (enq_flags & SCX_ENQ_LAST)
31 		scx_bpf_kick_cpu(test_cpu, SCX_KICK_IDLE);
32 }
33 
BPF_STRUCT_OPS(nohz_tick_running,struct task_struct * p)34 void BPF_STRUCT_OPS(nohz_tick_running, struct task_struct *p)
35 {
36 	if (bpf_get_smp_processor_id() != test_cpu)
37 		return;
38 
39 	if (finite_phase)
40 		__sync_fetch_and_add(&nr_finite_running, 1);
41 	else
42 		__sync_fetch_and_add(&nr_inf_running, 1);
43 }
44 
BPF_STRUCT_OPS(nohz_tick_tick,struct task_struct * p)45 void BPF_STRUCT_OPS(nohz_tick_tick, struct task_struct *p)
46 {
47 	if (bpf_get_smp_processor_id() == test_cpu && finite_phase)
48 		__sync_fetch_and_add(&nr_finite_ticks, 1);
49 }
50 
BPF_STRUCT_OPS(nohz_tick_exit,struct scx_exit_info * ei)51 void BPF_STRUCT_OPS(nohz_tick_exit, struct scx_exit_info *ei)
52 {
53 	UEI_RECORD(uei, ei);
54 }
55 
56 SEC(".struct_ops.link")
57 struct sched_ext_ops nohz_tick_ops = {
58 	.select_cpu		= (void *)nohz_tick_select_cpu,
59 	.enqueue		= (void *)nohz_tick_enqueue,
60 	.running		= (void *)nohz_tick_running,
61 	.tick			= (void *)nohz_tick_tick,
62 	.exit			= (void *)nohz_tick_exit,
63 	.name			= "nohz_tick",
64 	.timeout_ms		= 1000U,
65 };
66