1 // Copyright (c) 2024 Apple Inc. All rights reserved. 2 3 /* 4 * Since the Edge scheduler depends on the Clutch scheduler as most of its 5 * timesharing policy, the Edge scheduler should also pass all of the Clutch 6 * unit tests. 7 */ 8 #include "clutch_runqueue.c" 9 10 #include "sched_test_harness/sched_edge_harness.h" 11 12 SCHED_POLICY_T_DECL(runq_shared_rsrc_bound, 13 "Shared resource threads should be enqueued into bound root buckets") 14 { 15 int ret; 16 init_migration_harness(single_core); 17 struct thread_group *tg = create_tg(0); 18 /* Test both shared resource types */ 19 for (int i = 0; i < CLUSTER_SHARED_RSRC_TYPE_COUNT; i++) { 20 thread_t thread = create_thread(TH_BUCKET_SHARE_DF, tg, root_bucket_to_highest_pri[TH_BUCKET_SHARE_DF]); 21 edge_set_thread_shared_rsrc(thread, i); 22 enqueue_thread(default_target, thread); 23 ret = dequeue_thread_expect(default_target, thread); 24 T_QUIET; T_EXPECT_TRUE(ret, "Single shared rsrc thread"); 25 uint64_t bound_arg = SELECTION_WAS_CLUSTER_BOUND | SELECTION_WAS_EDF | CTS_VERSION; 26 ret = tracepoint_expect(CLUTCH_THREAD_SELECT, i, 0, TH_BUCKET_SHARE_DF, bound_arg); 27 T_EXPECT_TRUE(ret, "CLUTCH_THREAD_SELECT tracepoint confirms shared resource " 28 "(%s) thread was enqueued as bound", i == 0 ? "native first" : "round robin"); 29 } 30 SCHED_POLICY_PASS("Shared resource threads enqueued as bound"); 31 } 32 33 SCHED_POLICY_T_DECL(runq_aboveui_bound_tiebreaks, 34 "Tiebreaking Above UI vs. timeshare FG and bound vs. unbound root buckets") 35 { 36 int ret; 37 init_migration_harness(single_core); 38 39 /* Create a thread for each permutation (4 total), all at matching priority 63 */ 40 struct thread_group *same_tg = create_tg(clutch_interactivity_score_max); 41 test_thread_t unbound_aboveui = create_thread(TH_BUCKET_FIXPRI, same_tg, root_bucket_to_highest_pri[TH_BUCKET_FIXPRI]); 42 set_thread_sched_mode(unbound_aboveui, TH_MODE_FIXED); 43 test_thread_t bound_aboveui = create_thread(TH_BUCKET_FIXPRI, same_tg, root_bucket_to_highest_pri[TH_BUCKET_FIXPRI]); 44 set_thread_sched_mode(bound_aboveui, TH_MODE_FIXED); 45 set_thread_cluster_bound(bound_aboveui, 0); 46 test_thread_t unbound_timeshare_fg = create_thread(TH_BUCKET_SHARE_FG, same_tg, root_bucket_to_highest_pri[TH_BUCKET_FIXPRI]); 47 test_thread_t bound_timeshare_fg = create_thread(TH_BUCKET_SHARE_FG, same_tg, root_bucket_to_highest_pri[TH_BUCKET_FIXPRI]); 48 set_thread_cluster_bound(bound_timeshare_fg, 0); 49 50 for (int i = 0; i < NUM_RAND_SEEDS; i++) { 51 enqueue_threads_rand_order(default_target, rand_seeds[i], 4, unbound_aboveui, bound_aboveui, unbound_timeshare_fg, bound_timeshare_fg); 52 ret = dequeue_threads_expect_ordered(default_target, 2, bound_aboveui, unbound_aboveui); 53 T_QUIET; T_EXPECT_EQ(ret, -1, "Aboveui buckets didn't come out first and correctly ordered in iteration %d", i); 54 /* Needed because bound/unbound root buckets alternate picks, as demonstrated below */ 55 disable_auto_current_thread(); 56 ret = dequeue_threads_expect_ordered(default_target, 2, bound_timeshare_fg, unbound_timeshare_fg); 57 T_QUIET; T_EXPECT_EQ(ret, -1, "Timeshare buckets didn't come out second and correctly ordered in iteration %d", i); 58 T_QUIET; T_ASSERT_EQ(runqueue_empty(default_target), true, "runqueue_empty"); 59 reenable_auto_current_thread(); 60 } 61 SCHED_POLICY_PASS("Correct tiebreaking for aboveui vs. foreground and unbound vs. bound root buckets"); 62 } 63 64 SCHED_POLICY_T_DECL(runq_cluster_bound, 65 "Cluster-bound threads vs. regular threads") 66 { 67 int ret; 68 init_migration_harness(basic_amp); 69 struct thread_group *tg = create_tg(0); 70 int num_threads = 4; 71 test_thread_t threads[num_threads]; 72 for (int i = 0; i < NUM_RAND_SEEDS; i++) { 73 /* High root bucket unbound */ 74 threads[0] = create_thread(TH_BUCKET_SHARE_IN, tg, root_bucket_to_highest_pri[TH_BUCKET_SHARE_IN]); 75 /* Middle root bucket bound */ 76 threads[1] = create_thread(TH_BUCKET_SHARE_DF, tg, root_bucket_to_highest_pri[TH_BUCKET_SHARE_DF]); 77 set_thread_cluster_bound(threads[1], 0); 78 /* Low root bucket unbound */ 79 threads[2] = create_thread(TH_BUCKET_SHARE_UT, tg, root_bucket_to_highest_pri[TH_BUCKET_SHARE_UT]); 80 /* Lowest root bucket bound */ 81 threads[3] = create_thread(TH_BUCKET_SHARE_BG, tg, root_bucket_to_highest_pri[TH_BUCKET_SHARE_BG]); 82 set_thread_cluster_bound(threads[3], 0); 83 enqueue_threads_arr_rand_order(default_target, rand_seeds[i], num_threads, threads); 84 /* Bound comes out first due to bound/unbound root bucket tie break in favor of bound */ 85 ret = dequeue_threads_expect_ordered_arr(default_target, num_threads, threads); 86 T_QUIET; T_EXPECT_EQ(ret, -1, "Threads dequeued without respect to QoS"); 87 T_QUIET; T_EXPECT_TRUE(runqueue_empty(default_target), "runqueue_empty"); 88 } 89 SCHED_POLICY_PASS("Cluster bound respects QoS level"); 90 91 int num_tie_break_threads = 10; 92 test_thread_t tie_break_threads[num_tie_break_threads]; 93 for (int k = 0; k < num_tie_break_threads / 2; k++) { 94 tie_break_threads[k * 2] = create_thread(TH_BUCKET_SHARE_DF, tg, root_bucket_to_highest_pri[TH_BUCKET_SHARE_DF]); 95 set_thread_cluster_bound(tie_break_threads[k * 2], 0); 96 increment_mock_time_us(5); 97 enqueue_thread(default_target, tie_break_threads[k * 2]); 98 } 99 for (int k = 0; k < num_tie_break_threads / 2; k++) { 100 tie_break_threads[k * 2 + 1] = create_thread(TH_BUCKET_SHARE_DF, tg, root_bucket_to_highest_pri[TH_BUCKET_SHARE_DF]); 101 increment_mock_time_us(5); 102 enqueue_thread(default_target, tie_break_threads[k * 2 + 1]); 103 } 104 /* Disable current thread check because bound and unbound alternate without time passing */ 105 disable_auto_current_thread(); 106 for (int k = 0; k < num_tie_break_threads; k++) { 107 /* Simulates repeatedly dequeing threads over time */ 108 increment_mock_time_us(5); 109 ret = dequeue_thread_expect(default_target, tie_break_threads[k]); 110 T_QUIET; T_EXPECT_TRUE(ret, "Out-of-order thread\n"); 111 } 112 T_QUIET; T_EXPECT_TRUE(runqueue_empty(default_target), "runqueue_empty"); 113 SCHED_POLICY_PASS("Unbound vs. bound tie-break"); 114 115 struct thread_group *low_iscore_tg = create_tg(0); 116 test_thread_t low_iscore_bound = create_thread(TH_BUCKET_SHARE_DF, low_iscore_tg, root_bucket_to_highest_pri[TH_BUCKET_SHARE_DF]); 117 struct thread_group *high_iscore_tg = create_tg(clutch_interactivity_score_max); 118 test_thread_t high_iscore_bound = create_thread(TH_BUCKET_SHARE_DF, high_iscore_tg, root_bucket_to_highest_pri[TH_BUCKET_SHARE_DF]); 119 set_thread_cluster_bound(low_iscore_bound, 0); 120 set_thread_cluster_bound(high_iscore_bound, 0); 121 enqueue_threads(default_target, 2, low_iscore_bound, high_iscore_bound); 122 ret = dequeue_threads_expect_ordered(default_target, 2, low_iscore_bound, high_iscore_bound); 123 T_QUIET; T_EXPECT_EQ(ret, -1, "Threads dequeued in non-FIFO order"); 124 T_QUIET; T_EXPECT_TRUE(runqueue_empty(default_target), "runqueue_empty"); 125 SCHED_POLICY_PASS("Cluster bound threads don't use interactivity score"); 126 } 127