reload_loop.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 <bpf/bpf.h>
  7. #include <pthread.h>
  8. #include <scx/common.h>
  9. #include <sys/wait.h>
  10. #include <unistd.h>
  11. #include "maximal.bpf.skel.h"
  12. #include "scx_test.h"
  13. static struct maximal *skel;
  14. static pthread_t threads[2];
  15. bool force_exit = false;
  16. static enum scx_test_status setup(void **ctx)
  17. {
  18. skel = maximal__open_and_load();
  19. if (!skel) {
  20. SCX_ERR("Failed to open and load skel");
  21. return SCX_TEST_FAIL;
  22. }
  23. return SCX_TEST_PASS;
  24. }
  25. static void *do_reload_loop(void *arg)
  26. {
  27. u32 i;
  28. for (i = 0; i < 1024 && !force_exit; i++) {
  29. struct bpf_link *link;
  30. link = bpf_map__attach_struct_ops(skel->maps.maximal_ops);
  31. if (link)
  32. bpf_link__destroy(link);
  33. }
  34. return NULL;
  35. }
  36. static enum scx_test_status run(void *ctx)
  37. {
  38. int err;
  39. void *ret;
  40. err = pthread_create(&threads[0], NULL, do_reload_loop, NULL);
  41. SCX_FAIL_IF(err, "Failed to create thread 0");
  42. err = pthread_create(&threads[1], NULL, do_reload_loop, NULL);
  43. SCX_FAIL_IF(err, "Failed to create thread 1");
  44. SCX_FAIL_IF(pthread_join(threads[0], &ret), "thread 0 failed");
  45. SCX_FAIL_IF(pthread_join(threads[1], &ret), "thread 1 failed");
  46. return SCX_TEST_PASS;
  47. }
  48. static void cleanup(void *ctx)
  49. {
  50. force_exit = true;
  51. maximal__destroy(skel);
  52. }
  53. struct scx_test reload_loop = {
  54. .name = "reload_loop",
  55. .description = "Stress test loading and unloading schedulers repeatedly in a tight loop",
  56. .setup = setup,
  57. .run = run,
  58. .cleanup = cleanup,
  59. };
  60. REGISTER_SCX_TEST(&reload_loop)