tools/sched_ext/scx_cpu0.bpf.c

Source file repositories/reference/linux-study-clean/tools/sched_ext/scx_cpu0.bpf.c

File Facts

System
Linux kernel
Corpus path
tools/sched_ext/scx_cpu0.bpf.c
Extension
.c
Size
2395 bytes
Lines
95
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

#include <scx/common.bpf.h>

char _license[] SEC("license") = "GPL";

UEI_DEFINE(uei);

/*
 * We create a custom DSQ with ID 0 that we dispatch to and consume from on
 * CPU0.
 */
#define DSQ_CPU0 0

struct {
	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
	__uint(key_size, sizeof(u32));
	__uint(value_size, sizeof(u64));
	__uint(max_entries, 2);			/* [local, cpu0] */
} stats SEC(".maps");

static void stat_inc(u32 idx)
{
	u64 *cnt_p = bpf_map_lookup_elem(&stats, &idx);
	if (cnt_p)
		(*cnt_p)++;
}

s32 BPF_STRUCT_OPS(cpu0_select_cpu, struct task_struct *p, s32 prev_cpu, u64 wake_flags)
{
	return 0;
}

void BPF_STRUCT_OPS(cpu0_enqueue, struct task_struct *p, u64 enq_flags)
{
	/*
	 * select_cpu() always picks CPU0. If @p is not on CPU0, it can't run on
	 * CPU 0. Queue on whichever CPU it's currently only.
	 */
	if (scx_bpf_task_cpu(p) != 0) {
		stat_inc(0);	/* count local queueing */
		scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL, SCX_SLICE_DFL, 0);
		return;
	}

	stat_inc(1);	/* count cpu0 queueing */
	scx_bpf_dsq_insert(p, DSQ_CPU0, SCX_SLICE_DFL, enq_flags);
}

void BPF_STRUCT_OPS(cpu0_dispatch, s32 cpu, struct task_struct *prev)
{
	if (cpu == 0)
		scx_bpf_dsq_move_to_local(DSQ_CPU0, 0);
}

s32 BPF_STRUCT_OPS_SLEEPABLE(cpu0_init)
{
	int ret;

	ret = scx_bpf_create_dsq(DSQ_CPU0, -1);
	if (ret) {
		scx_bpf_error("failed to create DSQ %d (%d)", DSQ_CPU0, ret);
		return ret;
	}

	return 0;
}

void BPF_STRUCT_OPS(cpu0_exit, struct scx_exit_info *ei)
{
	UEI_RECORD(uei, ei);
}

SCX_OPS_DEFINE(cpu0_ops,
	       .select_cpu		= (void *)cpu0_select_cpu,
	       .enqueue			= (void *)cpu0_enqueue,
	       .dispatch		= (void *)cpu0_dispatch,
	       .init			= (void *)cpu0_init,
	       .exit			= (void *)cpu0_exit,
	       .name			= "cpu0");

Annotation

Implementation Notes