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