dsp_local_on.bpf.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2024 Meta Platforms, Inc. and affiliates.
  4. * Copyright (c) 2024 David Vernet <dvernet@meta.com>
  5. */
  6. #include <scx/common.bpf.h>
  7. char _license[] SEC("license") = "GPL";
  8. const volatile s32 nr_cpus;
  9. UEI_DEFINE(uei);
  10. struct {
  11. __uint(type, BPF_MAP_TYPE_QUEUE);
  12. __uint(max_entries, 8192);
  13. __type(value, s32);
  14. } queue SEC(".maps");
  15. s32 BPF_STRUCT_OPS(dsp_local_on_select_cpu, struct task_struct *p,
  16. s32 prev_cpu, u64 wake_flags)
  17. {
  18. return prev_cpu;
  19. }
  20. void BPF_STRUCT_OPS(dsp_local_on_enqueue, struct task_struct *p,
  21. u64 enq_flags)
  22. {
  23. s32 pid = p->pid;
  24. if (bpf_map_push_elem(&queue, &pid, 0))
  25. scx_bpf_error("Failed to enqueue %s[%d]", p->comm, p->pid);
  26. }
  27. void BPF_STRUCT_OPS(dsp_local_on_dispatch, s32 cpu, struct task_struct *prev)
  28. {
  29. s32 pid, target;
  30. struct task_struct *p;
  31. if (bpf_map_pop_elem(&queue, &pid))
  32. return;
  33. p = bpf_task_from_pid(pid);
  34. if (!p)
  35. return;
  36. if (p->nr_cpus_allowed == nr_cpus && !is_migration_disabled(p))
  37. target = bpf_get_prandom_u32() % nr_cpus;
  38. else
  39. target = scx_bpf_task_cpu(p);
  40. scx_bpf_dispatch(p, SCX_DSQ_LOCAL_ON | target, SCX_SLICE_DFL, 0);
  41. bpf_task_release(p);
  42. }
  43. void BPF_STRUCT_OPS(dsp_local_on_exit, struct scx_exit_info *ei)
  44. {
  45. UEI_RECORD(uei, ei);
  46. }
  47. SEC(".struct_ops.link")
  48. struct sched_ext_ops dsp_local_on_ops = {
  49. .select_cpu = (void *) dsp_local_on_select_cpu,
  50. .enqueue = (void *) dsp_local_on_enqueue,
  51. .dispatch = (void *) dsp_local_on_dispatch,
  52. .exit = (void *) dsp_local_on_exit,
  53. .name = "dsp_local_on",
  54. .timeout_ms = 1000U,
  55. };