select_cpu_dispatch.bpf.c 1003 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * A scheduler that validates the behavior of direct dispatching with a default
  4. * select_cpu implementation.
  5. *
  6. * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
  7. * Copyright (c) 2023 David Vernet <dvernet@meta.com>
  8. * Copyright (c) 2023 Tejun Heo <tj@kernel.org>
  9. */
  10. #include <scx/common.bpf.h>
  11. char _license[] SEC("license") = "GPL";
  12. s32 BPF_STRUCT_OPS(select_cpu_dispatch_select_cpu, struct task_struct *p,
  13. s32 prev_cpu, u64 wake_flags)
  14. {
  15. u64 dsq_id = SCX_DSQ_LOCAL;
  16. s32 cpu = prev_cpu;
  17. if (scx_bpf_test_and_clear_cpu_idle(cpu))
  18. goto dispatch;
  19. cpu = scx_bpf_pick_idle_cpu(p->cpus_ptr, 0);
  20. if (cpu >= 0)
  21. goto dispatch;
  22. dsq_id = SCX_DSQ_GLOBAL;
  23. cpu = prev_cpu;
  24. dispatch:
  25. scx_bpf_dsq_insert(p, dsq_id, SCX_SLICE_DFL, 0);
  26. return cpu;
  27. }
  28. SEC(".struct_ops.link")
  29. struct sched_ext_ops select_cpu_dispatch_ops = {
  30. .select_cpu = (void *) select_cpu_dispatch_select_cpu,
  31. .name = "select_cpu_dispatch",
  32. .timeout_ms = 1000U,
  33. };