slice_test.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: LGPL-2.1
  2. #define _GNU_SOURCE
  3. #include <assert.h>
  4. #include <pthread.h>
  5. #include <sched.h>
  6. #include <signal.h>
  7. #include <stdbool.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <syscall.h>
  11. #include <unistd.h>
  12. #include <linux/prctl.h>
  13. #include <sys/prctl.h>
  14. #include <sys/time.h>
  15. #include "rseq.h"
  16. #include "../kselftest_harness.h"
  17. #ifndef __NR_rseq_slice_yield
  18. # define __NR_rseq_slice_yield 471
  19. #endif
  20. #define BITS_PER_INT 32
  21. #define BITS_PER_BYTE 8
  22. #ifndef PR_RSEQ_SLICE_EXTENSION
  23. # define PR_RSEQ_SLICE_EXTENSION 79
  24. # define PR_RSEQ_SLICE_EXTENSION_GET 1
  25. # define PR_RSEQ_SLICE_EXTENSION_SET 2
  26. # define PR_RSEQ_SLICE_EXT_ENABLE 0x01
  27. #endif
  28. #ifndef RSEQ_SLICE_EXT_REQUEST_BIT
  29. # define RSEQ_SLICE_EXT_REQUEST_BIT 0
  30. # define RSEQ_SLICE_EXT_GRANTED_BIT 1
  31. #endif
  32. #ifndef asm_inline
  33. # define asm_inline asm __inline
  34. #endif
  35. #define NSEC_PER_SEC 1000000000L
  36. #define NSEC_PER_USEC 1000L
  37. struct noise_params {
  38. int64_t noise_nsecs;
  39. int64_t sleep_nsecs;
  40. int64_t run;
  41. };
  42. FIXTURE(slice_ext)
  43. {
  44. pthread_t noise_thread;
  45. struct noise_params noise_params;
  46. };
  47. FIXTURE_VARIANT(slice_ext)
  48. {
  49. int64_t total_nsecs;
  50. int64_t slice_nsecs;
  51. int64_t noise_nsecs;
  52. int64_t sleep_nsecs;
  53. bool no_yield;
  54. };
  55. FIXTURE_VARIANT_ADD(slice_ext, n2_2_50)
  56. {
  57. .total_nsecs = 5LL * NSEC_PER_SEC,
  58. .slice_nsecs = 2LL * NSEC_PER_USEC,
  59. .noise_nsecs = 2LL * NSEC_PER_USEC,
  60. .sleep_nsecs = 50LL * NSEC_PER_USEC,
  61. };
  62. FIXTURE_VARIANT_ADD(slice_ext, n50_2_50)
  63. {
  64. .total_nsecs = 5LL * NSEC_PER_SEC,
  65. .slice_nsecs = 50LL * NSEC_PER_USEC,
  66. .noise_nsecs = 2LL * NSEC_PER_USEC,
  67. .sleep_nsecs = 50LL * NSEC_PER_USEC,
  68. };
  69. FIXTURE_VARIANT_ADD(slice_ext, n2_2_50_no_yield)
  70. {
  71. .total_nsecs = 5LL * NSEC_PER_SEC,
  72. .slice_nsecs = 2LL * NSEC_PER_USEC,
  73. .noise_nsecs = 2LL * NSEC_PER_USEC,
  74. .sleep_nsecs = 50LL * NSEC_PER_USEC,
  75. .no_yield = true,
  76. };
  77. static inline bool elapsed(struct timespec *start, struct timespec *now,
  78. int64_t span)
  79. {
  80. int64_t delta = now->tv_sec - start->tv_sec;
  81. delta *= NSEC_PER_SEC;
  82. delta += now->tv_nsec - start->tv_nsec;
  83. return delta >= span;
  84. }
  85. static void *noise_thread(void *arg)
  86. {
  87. struct noise_params *p = arg;
  88. while (RSEQ_READ_ONCE(p->run)) {
  89. struct timespec ts_start, ts_now;
  90. clock_gettime(CLOCK_MONOTONIC, &ts_start);
  91. do {
  92. clock_gettime(CLOCK_MONOTONIC, &ts_now);
  93. } while (!elapsed(&ts_start, &ts_now, p->noise_nsecs));
  94. ts_start.tv_sec = 0;
  95. ts_start.tv_nsec = p->sleep_nsecs;
  96. clock_nanosleep(CLOCK_MONOTONIC, 0, &ts_start, NULL);
  97. }
  98. return NULL;
  99. }
  100. FIXTURE_SETUP(slice_ext)
  101. {
  102. cpu_set_t affinity;
  103. ASSERT_EQ(sched_getaffinity(0, sizeof(affinity), &affinity), 0);
  104. /* Pin it on a single CPU. Avoid CPU 0 */
  105. for (int i = 1; i < CPU_SETSIZE; i++) {
  106. if (!CPU_ISSET(i, &affinity))
  107. continue;
  108. CPU_ZERO(&affinity);
  109. CPU_SET(i, &affinity);
  110. ASSERT_EQ(sched_setaffinity(0, sizeof(affinity), &affinity), 0);
  111. break;
  112. }
  113. ASSERT_EQ(rseq_register_current_thread(), 0);
  114. ASSERT_EQ(prctl(PR_RSEQ_SLICE_EXTENSION, PR_RSEQ_SLICE_EXTENSION_SET,
  115. PR_RSEQ_SLICE_EXT_ENABLE, 0, 0), 0);
  116. self->noise_params.noise_nsecs = variant->noise_nsecs;
  117. self->noise_params.sleep_nsecs = variant->sleep_nsecs;
  118. self->noise_params.run = 1;
  119. ASSERT_EQ(pthread_create(&self->noise_thread, NULL, noise_thread, &self->noise_params), 0);
  120. }
  121. FIXTURE_TEARDOWN(slice_ext)
  122. {
  123. self->noise_params.run = 0;
  124. pthread_join(self->noise_thread, NULL);
  125. }
  126. TEST_F(slice_ext, slice_test)
  127. {
  128. unsigned long success = 0, yielded = 0, scheduled = 0, raced = 0;
  129. unsigned long total = 0, aborted = 0;
  130. struct rseq_abi *rs = rseq_get_abi();
  131. struct timespec ts_start, ts_now;
  132. ASSERT_NE(rs, NULL);
  133. clock_gettime(CLOCK_MONOTONIC, &ts_start);
  134. do {
  135. struct timespec ts_cs;
  136. bool req = false;
  137. clock_gettime(CLOCK_MONOTONIC, &ts_cs);
  138. total++;
  139. RSEQ_WRITE_ONCE(rs->slice_ctrl.request, 1);
  140. do {
  141. clock_gettime(CLOCK_MONOTONIC, &ts_now);
  142. } while (!elapsed(&ts_cs, &ts_now, variant->slice_nsecs));
  143. /*
  144. * request can be cleared unconditionally, but for making
  145. * the stats work this is actually checking it first
  146. */
  147. if (RSEQ_READ_ONCE(rs->slice_ctrl.request)) {
  148. RSEQ_WRITE_ONCE(rs->slice_ctrl.request, 0);
  149. /* Race between check and clear! */
  150. req = true;
  151. success++;
  152. }
  153. if (RSEQ_READ_ONCE(rs->slice_ctrl.granted)) {
  154. /* The above raced against a late grant */
  155. if (req)
  156. success--;
  157. if (variant->no_yield) {
  158. syscall(__NR_getpid);
  159. aborted++;
  160. } else {
  161. yielded++;
  162. if (!syscall(__NR_rseq_slice_yield))
  163. raced++;
  164. }
  165. } else {
  166. if (!req)
  167. scheduled++;
  168. }
  169. clock_gettime(CLOCK_MONOTONIC, &ts_now);
  170. } while (!elapsed(&ts_start, &ts_now, variant->total_nsecs));
  171. printf("# Total %12ld\n", total);
  172. printf("# Success %12ld\n", success);
  173. printf("# Yielded %12ld\n", yielded);
  174. printf("# Aborted %12ld\n", aborted);
  175. printf("# Scheduled %12ld\n", scheduled);
  176. printf("# Raced %12ld\n", raced);
  177. }
  178. TEST_HARNESS_MAIN