enq_select_cpu.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
  4. * Copyright (c) 2023 David Vernet <dvernet@meta.com>
  5. * Copyright (c) 2023 Tejun Heo <tj@kernel.org>
  6. */
  7. #include <bpf/bpf.h>
  8. #include <scx/common.h>
  9. #include <sys/wait.h>
  10. #include <unistd.h>
  11. #include "enq_select_cpu.bpf.skel.h"
  12. #include "scx_test.h"
  13. static enum scx_test_status setup(void **ctx)
  14. {
  15. struct enq_select_cpu *skel;
  16. skel = enq_select_cpu__open();
  17. SCX_FAIL_IF(!skel, "Failed to open");
  18. SCX_ENUM_INIT(skel);
  19. SCX_FAIL_IF(enq_select_cpu__load(skel), "Failed to load skel");
  20. *ctx = skel;
  21. return SCX_TEST_PASS;
  22. }
  23. static int test_select_cpu_from_user(const struct enq_select_cpu *skel)
  24. {
  25. int fd, ret;
  26. __u64 args[1];
  27. LIBBPF_OPTS(bpf_test_run_opts, attr,
  28. .ctx_in = args,
  29. .ctx_size_in = sizeof(args),
  30. );
  31. args[0] = getpid();
  32. fd = bpf_program__fd(skel->progs.select_cpu_from_user);
  33. if (fd < 0)
  34. return fd;
  35. ret = bpf_prog_test_run_opts(fd, &attr);
  36. if (ret < 0)
  37. return ret;
  38. fprintf(stderr, "%s: CPU %d\n", __func__, attr.retval);
  39. return 0;
  40. }
  41. static enum scx_test_status run(void *ctx)
  42. {
  43. struct enq_select_cpu *skel = ctx;
  44. struct bpf_link *link;
  45. link = bpf_map__attach_struct_ops(skel->maps.enq_select_cpu_ops);
  46. if (!link) {
  47. SCX_ERR("Failed to attach scheduler");
  48. return SCX_TEST_FAIL;
  49. }
  50. /* Pick an idle CPU from user-space */
  51. SCX_FAIL_IF(test_select_cpu_from_user(skel), "Failed to pick idle CPU");
  52. sleep(1);
  53. SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_NONE));
  54. bpf_link__destroy(link);
  55. return SCX_TEST_PASS;
  56. }
  57. static void cleanup(void *ctx)
  58. {
  59. struct enq_select_cpu *skel = ctx;
  60. enq_select_cpu__destroy(skel);
  61. }
  62. struct scx_test enq_select_cpu = {
  63. .name = "enq_select_cpu",
  64. .description = "Verify scx_bpf_select_cpu_dfl() from multiple contexts",
  65. .setup = setup,
  66. .run = run,
  67. .cleanup = cleanup,
  68. };
  69. REGISTER_SCX_TEST(&enq_select_cpu)