mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git
synced 2025-01-18 06:15:12 +00:00
64 lines
1.6 KiB
C
64 lines
1.6 KiB
C
|
/* SPDX-License-Identifier: GPL-2.0 */
|
||
|
/*
|
||
|
* A simple scheduler.
|
||
|
*
|
||
|
* A simple global FIFO scheduler. It also demonstrates the following niceties.
|
||
|
*
|
||
|
* - Statistics tracking how many tasks are queued to local and global dsq's.
|
||
|
* - Termination notification for userspace.
|
||
|
*
|
||
|
* Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
|
||
|
* Copyright (c) 2022 Tejun Heo <tj@kernel.org>
|
||
|
* Copyright (c) 2022 David Vernet <dvernet@meta.com>
|
||
|
*/
|
||
|
#include <scx/common.bpf.h>
|
||
|
|
||
|
char _license[] SEC("license") = "GPL";
|
||
|
|
||
|
UEI_DEFINE(uei);
|
||
|
|
||
|
struct {
|
||
|
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
|
||
|
__uint(key_size, sizeof(u32));
|
||
|
__uint(value_size, sizeof(u64));
|
||
|
__uint(max_entries, 2); /* [local, global] */
|
||
|
} 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(simple_select_cpu, struct task_struct *p, s32 prev_cpu, u64 wake_flags)
|
||
|
{
|
||
|
bool is_idle = false;
|
||
|
s32 cpu;
|
||
|
|
||
|
cpu = scx_bpf_select_cpu_dfl(p, prev_cpu, wake_flags, &is_idle);
|
||
|
if (is_idle) {
|
||
|
stat_inc(0); /* count local queueing */
|
||
|
scx_bpf_dispatch(p, SCX_DSQ_LOCAL, SCX_SLICE_DFL, 0);
|
||
|
}
|
||
|
|
||
|
return cpu;
|
||
|
}
|
||
|
|
||
|
void BPF_STRUCT_OPS(simple_enqueue, struct task_struct *p, u64 enq_flags)
|
||
|
{
|
||
|
stat_inc(1); /* count global queueing */
|
||
|
scx_bpf_dispatch(p, SCX_DSQ_GLOBAL, SCX_SLICE_DFL, enq_flags);
|
||
|
}
|
||
|
|
||
|
void BPF_STRUCT_OPS(simple_exit, struct scx_exit_info *ei)
|
||
|
{
|
||
|
UEI_RECORD(uei, ei);
|
||
|
}
|
||
|
|
||
|
SCX_OPS_DEFINE(simple_ops,
|
||
|
.select_cpu = (void *)simple_select_cpu,
|
||
|
.enqueue = (void *)simple_enqueue,
|
||
|
.exit = (void *)simple_exit,
|
||
|
.name = "simple");
|