Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2018, 2024 Intel Corporation
3 : : *
4 : : * SPDX-License-Identifier: Apache-2.0
5 : : */
6 : : #include <zephyr/kernel.h>
7 : : #include <zephyr/sys/atomic.h>
8 : : #include <kspinlock.h>
9 : : #include <kswap.h>
10 : : #include <ksched.h>
11 : : #include <ipi.h>
12 : : #include <timeslicing.h>
13 : :
14 : : static int slice_ticks = DIV_ROUND_UP(CONFIG_TIMESLICE_SIZE * Z_HZ_ticks, Z_HZ_ms);
15 : : static int slice_max_prio = CONFIG_TIMESLICE_PRIORITY;
16 : : static struct _timeout slice_timeouts[CONFIG_MP_MAX_NUM_CPUS];
17 : :
18 : : /* Timeouts can expire on another CPU, so each CPU's flag must be atomic. */
19 : : static atomic_t slice_expired[CONFIG_MP_MAX_NUM_CPUS];
20 : :
21 : : #ifdef CONFIG_SWAP_NONATOMIC
22 : : /* If z_swap() isn't atomic, then it's possible for a timer interrupt
23 : : * to try to timeslice away _current after it has already pended
24 : : * itself but before the corresponding context switch. Treat that as
25 : : * a noop condition in z_time_slice().
26 : : */
27 : : struct k_thread *pending_current;
28 : : #endif
29 : :
30 : 121 : static inline int slice_time(struct k_thread *thread)
31 : : {
32 : 121 : int ret = slice_ticks;
33 : :
34 : : #ifdef CONFIG_TIMESLICE_PER_THREAD
35 : : if (thread->base.slice_ticks != 0) {
36 : : ret = thread->base.slice_ticks;
37 : : }
38 : : #else
39 : 121 : ARG_UNUSED(thread);
40 : : #endif
41 : 121 : return ret;
42 : : }
43 : :
44 : 121 : static int z_time_slice_size(struct k_thread *thread)
45 : : {
46 [ + - ]: 121 : if (z_is_thread_prevented_from_running(thread) ||
47 [ + - ]: 121 : z_is_idle_thread_object(thread) ||
48 [ + - ]: 121 : (slice_time(thread) == 0)) {
49 : : return 0;
50 : : }
51 : :
52 : : #ifdef CONFIG_TIMESLICE_PER_THREAD
53 : : if (thread->base.slice_ticks != 0) {
54 : : return thread->base.slice_ticks;
55 : : }
56 : : #endif
57 : :
58 [ + + ]: 121 : if (thread_is_preemptible(thread) &&
59 [ + - ]: 63 : !z_is_prio_higher(thread->base.prio, slice_max_prio)) {
60 : 63 : return slice_ticks;
61 : : }
62 : :
63 : : return 0;
64 : : }
65 : :
66 : 0 : static void slice_timeout(struct _timeout *timeout)
67 : : {
68 [ # # # # : 0 : int cpu = ARRAY_INDEX(slice_timeouts, timeout);
# # # # ]
69 : :
70 : 0 : atomic_set(&slice_expired[cpu], 1);
71 : :
72 : : /* We need an IPI if we just handled a timeslice expiration
73 : : * for a different CPU.
74 : : */
75 : 0 : if (cpu != _current_cpu->id) {
76 : 0 : flag_ipi(IPI_CPU_MASK(cpu));
77 : : }
78 : 0 : }
79 : :
80 : 121 : static void slice_reset(int slice_size)
81 : : {
82 : 121 : int cpu = _current_cpu->id;
83 : :
84 : : /* Best-effort cancel: if the slice timeout is already in flight,
85 : : * its handler only flips slice_expired[cpu] (which we clear below)
86 : : * and possibly raises an IPI -- harmless either way.
87 : : */
88 : 121 : (void)z_try_abort_timeout(&slice_timeouts[cpu]);
89 [ + + ]: 121 : if (slice_size != 0) {
90 : : /* When invoked because the slicer just fired (this CPU or
91 : : * via IPI from another), we're at a tick edge but past the
92 : : * announce window, so subtract 1 to cancel z_add_timeout()'s
93 : : * "+1" round-up and land at exactly slice_size ticks.
94 : : */
95 [ - + ]: 63 : int delay = atomic_get(&slice_expired[cpu]) ? slice_size - 1 : slice_size;
96 : :
97 : 63 : z_add_timeout(&slice_timeouts[cpu], slice_timeout,
98 : 63 : K_TICKS(delay));
99 : : }
100 : 121 : atomic_clear(&slice_expired[cpu]);
101 : 121 : }
102 : :
103 : 121 : void z_time_slice_reset(struct k_thread *thread)
104 : : {
105 : 121 : slice_reset(z_time_slice_size(thread));
106 : 121 : }
107 : :
108 : 0 : static ALWAYS_INLINE bool thread_defines_time_slice_size(struct k_thread *thread)
109 : : {
110 : : #ifdef CONFIG_TIMESLICE_PER_THREAD
111 : : return (thread->base.slice_ticks != 0);
112 : : #else /* !CONFIG_TIMESLICE_PER_THREAD */
113 : 0 : ARG_UNUSED(thread);
114 : 0 : return false;
115 : : #endif /* !CONFIG_TIMESLICE_PER_THREAD */
116 : : }
117 : :
118 : 0 : void k_sched_time_slice_set(int32_t slice, int prio)
119 : : {
120 [ # # ]: 0 : Z_SCHED_SPINLOCK {
121 : :
122 [ # # # # : 0 : slice_ticks = k_ms_to_ticks_ceil32(slice);
# # ]
123 : 0 : slice_max_prio = prio;
124 : :
125 : : /*
126 : : * Threads that define their own time slice size should not have
127 : : * their time slices reset here as a thread-specific time slice size
128 : : * take precedence over the global time slice size.
129 : : */
130 : :
131 [ # # ]: 0 : if (!thread_defines_time_slice_size(_current)) {
132 : 0 : z_time_slice_reset(_current);
133 : : }
134 : : }
135 : 0 : }
136 : :
137 : : #ifdef CONFIG_TIMESLICE_PER_THREAD
138 : : void k_thread_time_slice_set(struct k_thread *thread, int32_t thread_slice_ticks,
139 : : k_thread_timeslice_fn_t expired, void *data)
140 : : {
141 : : Z_SCHED_SPINLOCK {
142 : : thread->base.slice_ticks = thread_slice_ticks;
143 : : thread->base.slice_expired = expired;
144 : : thread->base.slice_data = data;
145 : : z_time_slice_reset(thread);
146 : : }
147 : : }
148 : : #endif
149 : :
150 : : /* Called out of each timer and IPI interrupt */
151 : 0 : void z_time_slice(void)
152 : : {
153 : : /*
154 : : * Atomic context switches need no scheduler work until this CPU's
155 : : * time slice expires. Non-atomic context switches must still take
156 : : * the scheduler lock to synchronize pending_current.
157 : : */
158 [ # # ]: 0 : if (!IS_ENABLED(CONFIG_SWAP_NONATOMIC) &&
159 : 0 : !atomic_get(&slice_expired[_current_cpu->id])) {
160 : 0 : return;
161 : : }
162 : :
163 : 0 : k_spinlock_key_t key = z_sched_spinlock_lock();
164 : 0 : struct k_thread *curr = _current;
165 : :
166 : : #ifdef CONFIG_SWAP_NONATOMIC
167 : : if (pending_current == curr) {
168 : : z_time_slice_reset(curr);
169 : : z_sched_spinlock_unlock(key);
170 : : return;
171 : : }
172 : : pending_current = NULL;
173 : : #endif
174 : :
175 : 0 : int slice_size = 0;
176 : :
177 [ # # ]: 0 : if (atomic_get(&slice_expired[_current_cpu->id])) {
178 : 0 : slice_size = z_time_slice_size(curr);
179 : : }
180 : :
181 [ # # ]: 0 : if (slice_size != 0) {
182 : : #ifdef CONFIG_TIMESLICE_PER_THREAD
183 : : k_thread_timeslice_fn_t handler = curr->base.slice_expired;
184 : :
185 : : if (handler != NULL) {
186 : : z_sched_spinlock_unlock(key);
187 : : handler(curr, curr->base.slice_data);
188 : : key = z_sched_spinlock_lock();
189 : :
190 : : /* The handler ran with the lock dropped and may have
191 : : * changed this thread's slice configuration, so the
192 : : * cached size is stale; recompute before rearming.
193 : : */
194 : : slice_size = z_time_slice_size(curr);
195 : : }
196 : : #endif
197 [ # # ]: 0 : if (!z_is_thread_prevented_from_running(curr)) {
198 : 0 : move_current_to_end_of_prio_q();
199 : : /* If the rotation kept curr at the front (no other
200 : : * runnable thread of equal-or-higher priority is
201 : : * waiting), no swap will happen and we must rearm
202 : : * here. Otherwise the dispatch path will rearm for
203 : : * the new thread with slice_expired still set,
204 : : * picking up the tick-aligned delay.
205 : : */
206 : : #ifdef CONFIG_SMP
207 : : struct k_thread *next = runq_best();
208 : :
209 : : if (next == NULL || z_is_idle_thread_object(next)) {
210 : : slice_reset(slice_size);
211 : : }
212 : : #else
213 [ # # ]: 0 : if (_kernel.ready_q.cache == curr) {
214 : 0 : slice_reset(slice_size);
215 : : }
216 : : #endif
217 : : }
218 : : }
219 : 0 : z_sched_spinlock_unlock(key);
220 : : }
|