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