xref: /xnu-12377.81.4/tests/sched/sched_test_harness/sched_clutch_harness.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1 // Copyright (c) 2024 Apple Inc.  All rights reserved.
2 
3 #include "sched_clutch_harness_impl.c"
4 
5 void
impl_init_runqueue(void)6 impl_init_runqueue(void)
7 {
8 	/* Init runqueue */
9 	curr_hw_topo = single_core;
10 	clutch_impl_init_topology(curr_hw_topo);
11 	assert(processor_avail_count == 1);
12 	increment_mock_time(100);
13 	clutch_impl_init_params();
14 	clutch_impl_init_tracepoints();
15 }
16 
17 struct thread_group *
impl_create_tg(int interactivity_score)18 impl_create_tg(int interactivity_score)
19 {
20 	return clutch_impl_create_tg(interactivity_score);
21 }
22 
23 test_thread_t
impl_create_thread(int root_bucket,struct thread_group * tg,int pri)24 impl_create_thread(int root_bucket, struct thread_group *tg, int pri)
25 {
26 	return clutch_impl_create_thread(root_bucket, tg, pri);
27 }
28 
29 void
impl_set_thread_processor_bound(test_thread_t thread,int cpu_id)30 impl_set_thread_processor_bound(test_thread_t thread, int cpu_id)
31 {
32 	clutch_impl_set_thread_processor_bound(thread, cpu_id);
33 }
34 
35 void
impl_cpu_set_thread_current(int cpu_id,test_thread_t thread)36 impl_cpu_set_thread_current(int cpu_id, test_thread_t thread)
37 {
38 	clutch_impl_cpu_set_thread_current(cpu_id, thread);
39 }
40 
41 test_thread_t
impl_cpu_clear_thread_current(int cpu_id)42 impl_cpu_clear_thread_current(int cpu_id)
43 {
44 	return clutch_impl_cpu_clear_thread_current(cpu_id);
45 }
46 
47 void
impl_cpu_enqueue_thread(int cpu_id,test_thread_t thread)48 impl_cpu_enqueue_thread(int cpu_id, test_thread_t thread)
49 {
50 	if (impl_get_thread_is_realtime(thread)) {
51 		rt_runq_insert(cpus[cpu_id], cpus[cpu_id]->processor_set, (thread_t) thread);
52 	} else {
53 		sched_clutch_processor_enqueue(cpus[cpu_id], (thread_t) thread, SCHED_TAILQ);
54 	}
55 }
56 
57 test_thread_t
impl_cpu_dequeue_thread(int cpu_id)58 impl_cpu_dequeue_thread(int cpu_id)
59 {
60 	test_thread_t chosen_thread = sched_rt_choose_thread(cpus[cpu_id]);
61 	if (chosen_thread != THREAD_NULL) {
62 		return chosen_thread;
63 	}
64 	/* No realtime threads. */
65 	return sched_clutch_choose_thread(cpus[cpu_id], MINPRI, NULL, 0);
66 }
67 
68 test_thread_t
impl_cpu_dequeue_thread_compare_current(int cpu_id)69 impl_cpu_dequeue_thread_compare_current(int cpu_id)
70 {
71 	assert(cpus[cpu_id]->active_thread != NULL);
72 	assert(impl_get_thread_is_realtime(cpus[cpu_id]->active_thread) == false); /* should not be called when realtime threads are running */
73 	return sched_clutch_choose_thread(cpus[cpu_id], MINPRI, cpus[cpu_id]->active_thread, 0);
74 }
75 
76 bool
impl_processor_csw_check(int cpu_id)77 impl_processor_csw_check(int cpu_id)
78 {
79 	ast_t preempt_ast = sched_clutch_processor_csw_check(cpus[cpu_id]);
80 	return preempt_ast & AST_PREEMPT;
81 }
82 
83 void
impl_pop_tracepoint(uint64_t clutch_trace_code,uint64_t * arg1,uint64_t * arg2,uint64_t * arg3,uint64_t * arg4)84 impl_pop_tracepoint(uint64_t clutch_trace_code, uint64_t *arg1, uint64_t *arg2, uint64_t *arg3, uint64_t *arg4)
85 {
86 	clutch_impl_pop_tracepoint(clutch_trace_code, arg1, arg2, arg3, arg4);
87 }
88