allowed_cpus.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2025 Andrea Righi <arighi@nvidia.com>
  4. */
  5. #include <bpf/bpf.h>
  6. #include <scx/common.h>
  7. #include <sys/wait.h>
  8. #include <unistd.h>
  9. #include "allowed_cpus.bpf.skel.h"
  10. #include "scx_test.h"
  11. static enum scx_test_status setup(void **ctx)
  12. {
  13. struct allowed_cpus *skel;
  14. skel = allowed_cpus__open();
  15. SCX_FAIL_IF(!skel, "Failed to open");
  16. SCX_ENUM_INIT(skel);
  17. SCX_FAIL_IF(allowed_cpus__load(skel), "Failed to load skel");
  18. *ctx = skel;
  19. return SCX_TEST_PASS;
  20. }
  21. static int test_select_cpu_from_user(const struct allowed_cpus *skel)
  22. {
  23. int fd, ret;
  24. __u64 args[1];
  25. LIBBPF_OPTS(bpf_test_run_opts, attr,
  26. .ctx_in = args,
  27. .ctx_size_in = sizeof(args),
  28. );
  29. args[0] = getpid();
  30. fd = bpf_program__fd(skel->progs.select_cpu_from_user);
  31. if (fd < 0)
  32. return fd;
  33. ret = bpf_prog_test_run_opts(fd, &attr);
  34. if (ret < 0)
  35. return ret;
  36. fprintf(stderr, "%s: CPU %d\n", __func__, attr.retval);
  37. return 0;
  38. }
  39. static enum scx_test_status run(void *ctx)
  40. {
  41. struct allowed_cpus *skel = ctx;
  42. struct bpf_link *link;
  43. link = bpf_map__attach_struct_ops(skel->maps.allowed_cpus_ops);
  44. SCX_FAIL_IF(!link, "Failed to attach scheduler");
  45. /* Pick an idle CPU from user-space */
  46. SCX_FAIL_IF(test_select_cpu_from_user(skel), "Failed to pick idle CPU");
  47. /* Just sleeping is fine, plenty of scheduling events happening */
  48. sleep(1);
  49. SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_NONE));
  50. bpf_link__destroy(link);
  51. return SCX_TEST_PASS;
  52. }
  53. static void cleanup(void *ctx)
  54. {
  55. struct allowed_cpus *skel = ctx;
  56. allowed_cpus__destroy(skel);
  57. }
  58. struct scx_test allowed_cpus = {
  59. .name = "allowed_cpus",
  60. .description = "Verify scx_bpf_select_cpu_and()",
  61. .setup = setup,
  62. .run = run,
  63. .cleanup = cleanup,
  64. };
  65. REGISTER_SCX_TEST(&allowed_cpus)