| 1 | #include "sch/tests/test_internal.h" |
| 2 | |
| 3 | TEST_GROUP_DEFINE(sched, .intensity_desc = { |
| 4 | .curve = SCALE_PIECEWISE_LOG, |
| 5 | .unit = "iters" , |
| 6 | }); |
| 7 | |
| 8 | static void sleepy_entry(void *arg) { |
| 9 | cc_ignore(arg); |
| 10 | thread_sleep_for_ms(ms: 50); |
| 11 | } |
| 12 | |
| 13 | TEST_DEFINE_INTEGRATION(sched, sleep_ms) { |
| 14 | struct thread *t = |
| 15 | thread_spawn("sched_sleepy_test" , sleepy_entry, .joinable = true); |
| 16 | TEST_ASSERT_NONNULL(t); |
| 17 | thread_join(t); |
| 18 | return TEST_SUCCESS; |
| 19 | } |
| 20 | |
| 21 | static atomic_bool slept_for_us = false; |
| 22 | |
| 23 | static void micro_sleep_entry(void *arg) { |
| 24 | cc_ignore(arg); |
| 25 | thread_sleep_for_us(us: 200); |
| 26 | atomic_store(&slept_for_us, true); |
| 27 | } |
| 28 | |
| 29 | TEST_DEFINE_INTEGRATION(sched, sleep_us) { |
| 30 | atomic_store(&slept_for_us, false); |
| 31 | struct thread *t = thread_spawn("sched_micro_sleep_test" , micro_sleep_entry, |
| 32 | .joinable = true); |
| 33 | TEST_ASSERT_NONNULL(t); |
| 34 | thread_join(t); |
| 35 | TEST_ASSERT(atomic_load(&slept_for_us)); |
| 36 | return TEST_SUCCESS; |
| 37 | } |
| 38 | |
| 39 | static atomic_bool short_sleep_stop = false; |
| 40 | static atomic_size_t short_sleep_count = 0; |
| 41 | |
| 42 | static void short_sleep_entry(void *arg) { |
| 43 | cc_ignore(arg); |
| 44 | for (size_t i = 0; i < 200 && !atomic_load(&short_sleep_stop); i++) { |
| 45 | thread_sleep_for_us(us: 1); |
| 46 | atomic_inc(&short_sleep_count); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | TEST_DEFINE_INTEGRATION(sched, short_sleep_lost_wake) { |
| 51 | atomic_store(&short_sleep_stop, false); |
| 52 | atomic_store(&short_sleep_count, 0); |
| 53 | |
| 54 | struct thread *t = thread_spawn("sched_short_sleep_lost_wake_test" , |
| 55 | short_sleep_entry, .joinable = true); |
| 56 | TEST_ASSERT_NONNULL(t); |
| 57 | |
| 58 | bool joined = thread_join_timeout(t, timeout_ms: 1000, NULL); |
| 59 | if (!joined) { |
| 60 | atomic_store(&short_sleep_stop, true); |
| 61 | /* alert must not rescue a uninterruptible wait */ |
| 62 | enforce(joined, "short timed sleep lost its object completion" ); |
| 63 | thread_join(t); |
| 64 | } |
| 65 | |
| 66 | if (!joined) |
| 67 | test_info("short timed sleep lost its wake after %zu cycles" , |
| 68 | atomic_load(&short_sleep_count)); |
| 69 | TEST_ASSERT(joined); |
| 70 | TEST_ASSERT_EQ(200, atomic_load(&short_sleep_count)); |
| 71 | return TEST_SUCCESS; |
| 72 | } |
| 73 | |
| 74 | static struct thread_wait_header si_wait; |
| 75 | static atomic_bool si_apc_ran = false; |
| 76 | static struct thread *si_t; |
| 77 | static atomic_bool si_ok = false; |
| 78 | static atomic_bool si_started = false; |
| 79 | |
| 80 | static void apc_si(struct apc_context *ac, void *apc) { |
| 81 | cc_ignore(ac); |
| 82 | cc_ignore(apc); |
| 83 | atomic_store(&si_apc_ran, true); |
| 84 | } |
| 85 | |
| 86 | static void apc_enqueue_thread(void *arg) { |
| 87 | cc_ignore(arg); |
| 88 | struct apc *apc = apc_create(); |
| 89 | apc_init(a: apc, fn: apc_si, NULL, destroy: apc_destroy_free); |
| 90 | |
| 91 | while (!atomic_load(&si_started)) |
| 92 | cpu_pause(); |
| 93 | |
| 94 | rcu_read_lock(); |
| 95 | bool got = thread_getref_rcu(t: si_t); |
| 96 | rcu_read_unlock(); |
| 97 | |
| 98 | if (got) { |
| 99 | apc_enqueue(t: si_t, a: apc, type: APC_TYPE_KERNEL); |
| 100 | thread_putref(t: si_t); |
| 101 | } |
| 102 | apc_putref(a: apc); |
| 103 | } |
| 104 | |
| 105 | static void sleeping_thread(void *arg) { |
| 106 | cc_ignore(arg); |
| 107 | thread_wait_prepare_to_sleep(header: &si_wait, object: &si_wait, type: THREAD_WAIT_INTERRUPTIBLE); |
| 108 | atomic_store(&si_started, true); |
| 109 | thread_wait_complete(); |
| 110 | |
| 111 | atomic_store(&si_ok, true); |
| 112 | } |
| 113 | |
| 114 | static void waking_thread(void *arg) { |
| 115 | cc_ignore(arg); |
| 116 | while (!atomic_load(&si_apc_ran)) |
| 117 | scheduler_yield(); |
| 118 | |
| 119 | thread_wait_header_satisfy(hdr: &si_wait, reason: THREAD_WAKE_REASON_SLEEP_MANUAL, NULL); |
| 120 | } |
| 121 | |
| 122 | TEST_DEFINE_INTEGRATION(sched, sleep_interruptible_apc, .min_cores = 4) { |
| 123 | thread_wait_header_init(header: &si_wait); |
| 124 | atomic_store(&si_apc_ran, false); |
| 125 | atomic_store(&si_ok, false); |
| 126 | atomic_store(&si_started, false); |
| 127 | |
| 128 | si_t = thread_spawn("si_thread" , sleeping_thread, .on_cpu = 1, |
| 129 | .joinable = true); |
| 130 | struct thread *waker = |
| 131 | thread_spawn("si_wake" , waking_thread, .on_cpu = 2, .joinable = true); |
| 132 | struct thread *enq = thread_spawn("si_apc_e" , apc_enqueue_thread, |
| 133 | .on_cpu = 3, .joinable = true); |
| 134 | |
| 135 | TEST_ASSERT_NONNULL(si_t); |
| 136 | TEST_ASSERT_NONNULL(waker); |
| 137 | TEST_ASSERT_NONNULL(enq); |
| 138 | |
| 139 | thread_join(t: si_t); |
| 140 | thread_join(t: waker); |
| 141 | thread_join(t: enq); |
| 142 | |
| 143 | TEST_ASSERT(atomic_load(&si_ok)); |
| 144 | |
| 145 | return TEST_SUCCESS; |
| 146 | } |
| 147 | |
| 148 | static atomic_bool sub_apc_ran = false; |
| 149 | static struct thread *sub_t; |
| 150 | static atomic_bool sub_interrupted = false; |
| 151 | static atomic_bool sub_started = false; |
| 152 | |
| 153 | static void apc_sub(struct apc_context *ac, void *apc) { |
| 154 | cc_ignore(ac); |
| 155 | cc_ignore(apc); |
| 156 | atomic_store(&sub_apc_ran, true); |
| 157 | } |
| 158 | |
| 159 | static void apc_sub_enq_thread(void *arg) { |
| 160 | cc_ignore(arg); |
| 161 | struct apc *apc = apc_create(); |
| 162 | apc_init(a: apc, fn: apc_sub, NULL, destroy: apc_destroy_free); |
| 163 | |
| 164 | while (!atomic_load(&sub_started)) |
| 165 | cpu_pause(); |
| 166 | |
| 167 | rcu_read_lock(); |
| 168 | bool got = thread_getref_rcu(t: sub_t); |
| 169 | rcu_read_unlock(); |
| 170 | |
| 171 | if (got) { |
| 172 | apc_enqueue(t: sub_t, a: apc, type: APC_TYPE_KERNEL); |
| 173 | while (!atomic_load(&sub_apc_ran)) |
| 174 | scheduler_yield(); |
| 175 | thread_alert(thread: sub_t); |
| 176 | thread_putref(t: sub_t); |
| 177 | } |
| 178 | apc_putref(a: apc); |
| 179 | } |
| 180 | |
| 181 | static void sleeping_sub_thread(void *arg) { |
| 182 | cc_ignore(arg); |
| 183 | atomic_store(&sub_started, true); |
| 184 | |
| 185 | thread_park(); |
| 186 | atomic_store(&sub_interrupted, true); |
| 187 | } |
| 188 | |
| 189 | TEST_DEFINE_INTEGRATION(sched, wait_interruptible_substrate, .min_cores = 3) { |
| 190 | atomic_store(&sub_apc_ran, false); |
| 191 | atomic_store(&sub_interrupted, false); |
| 192 | atomic_store(&sub_started, false); |
| 193 | |
| 194 | sub_t = thread_spawn("sub_th" , sleeping_sub_thread, .on_cpu = 1, |
| 195 | .joinable = true); |
| 196 | struct thread *enq = thread_spawn("sub_enq" , apc_sub_enq_thread, |
| 197 | .on_cpu = 2, .joinable = true); |
| 198 | |
| 199 | TEST_ASSERT_NONNULL(sub_t); |
| 200 | TEST_ASSERT_NONNULL(enq); |
| 201 | |
| 202 | thread_join(t: sub_t); |
| 203 | thread_join(t: enq); |
| 204 | |
| 205 | TEST_ASSERT(atomic_load(&sub_apc_ran)); |
| 206 | TEST_ASSERT(atomic_load(&sub_interrupted)); |
| 207 | |
| 208 | return TEST_SUCCESS; |
| 209 | } |
| 210 | |
| 211 | static struct thread *arb_t; |
| 212 | static atomic_bool arb_started = false; |
| 213 | static atomic_bool arb_matched = false; |
| 214 | |
| 215 | static void arbitrary_sleeping_thread(void *arg) { |
| 216 | cc_ignore(arg); |
| 217 | atomic_store(&arb_started, true); |
| 218 | |
| 219 | thread_park(); |
| 220 | atomic_store(&arb_matched, true); |
| 221 | } |
| 222 | |
| 223 | static void arbitrary_waking_thread(void *arg) { |
| 224 | cc_ignore(arg); |
| 225 | while (!atomic_load(&arb_started)) |
| 226 | cpu_pause(); |
| 227 | |
| 228 | thread_sleep_for_ms(ms: 5); |
| 229 | |
| 230 | thread_alert(thread: arb_t); |
| 231 | } |
| 232 | |
| 233 | TEST_DEFINE_INTEGRATION(sched, park_alert, .min_cores = 3) { |
| 234 | atomic_store(&arb_started, false); |
| 235 | atomic_store(&arb_matched, false); |
| 236 | |
| 237 | arb_t = thread_spawn("arb_th" , arbitrary_sleeping_thread, .on_cpu = 1, |
| 238 | .joinable = true); |
| 239 | struct thread *waker = thread_spawn("arb_waker" , arbitrary_waking_thread, |
| 240 | .on_cpu = 2, .joinable = true); |
| 241 | |
| 242 | TEST_ASSERT_NONNULL(arb_t); |
| 243 | TEST_ASSERT_NONNULL(waker); |
| 244 | |
| 245 | thread_join(t: arb_t); |
| 246 | thread_join(t: waker); |
| 247 | |
| 248 | TEST_ASSERT(atomic_load(&arb_matched)); |
| 249 | |
| 250 | return TEST_SUCCESS; |
| 251 | } |
| 252 | |