Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2024 Intel Corporation
3 : : *
4 : : * SPDX-License-Identifier: Apache-2.0
5 : : */
6 : :
7 : : #ifndef ZEPHYR_KERNEL_INCLUDE_PRIORITY_Q_H_
8 : : #define ZEPHYR_KERNEL_INCLUDE_PRIORITY_Q_H_
9 : :
10 : : #include <zephyr/sys/math_extras.h>
11 : : #include <zephyr/sys/dlist.h>
12 : :
13 : : /* Simple Scheduling */
14 : : #if defined(CONFIG_SCHED_SIMPLE)
15 : : #define _priq_run_init z_priq_simple_init
16 : : #define _priq_run_add z_priq_simple_add
17 : : #define _priq_run_remove z_priq_simple_remove
18 : : #define _priq_run_yield z_priq_simple_yield
19 : : # if defined(CONFIG_SCHED_CPU_MASK) && !defined(CONFIG_SCHED_CPU_MASK_PIN_ONLY)
20 : : # define _priq_run_best z_priq_simple_mask_best
21 : : # else
22 : : # define _priq_run_best z_priq_simple_best
23 : : # endif /* CONFIG_SCHED_CPU_MASK */
24 : : /* Scalable Scheduling */
25 : : #elif defined(CONFIG_SCHED_SCALABLE)
26 : : #define _priq_run_init z_priq_rb_init
27 : : #define _priq_run_add z_priq_rb_add
28 : : #define _priq_run_remove z_priq_rb_remove
29 : : #define _priq_run_yield z_priq_rb_yield
30 : : # if defined(CONFIG_SCHED_CPU_MASK) && !defined(CONFIG_SCHED_CPU_MASK_PIN_ONLY)
31 : : # define _priq_run_best z_priq_rb_mask_best
32 : : # else
33 : : # define _priq_run_best z_priq_rb_best
34 : : # endif /* CONFIG_SCHED_CPU_MASK */
35 : : /* Multi Queue Scheduling */
36 : : #elif defined(CONFIG_SCHED_MULTIQ)
37 : : #define _priq_run_init z_priq_mq_init
38 : : #define _priq_run_add z_priq_mq_add
39 : : #define _priq_run_remove z_priq_mq_remove
40 : : #define _priq_run_yield z_priq_mq_yield
41 : : # if defined(CONFIG_SCHED_CPU_MASK) && !defined(CONFIG_SCHED_CPU_MASK_PIN_ONLY)
42 : : # define _priq_run_best z_priq_mq_mask_best
43 : : # else
44 : : # define _priq_run_best z_priq_mq_best
45 : : # endif /* CONFIG_SCHED_CPU_MASK */
46 : : #endif
47 : :
48 : : /* Scalable Wait Queue */
49 : : #if defined(CONFIG_WAITQ_SCALABLE)
50 : : #define _priq_wait_add z_priq_rb_add
51 : : #define _priq_wait_remove z_priq_rb_remove
52 : : #define _priq_wait_best z_priq_rb_best
53 : : /* Simple Wait Queue */
54 : : #elif defined(CONFIG_WAITQ_SIMPLE)
55 : : #define _priq_wait_add z_priq_simple_add
56 : : #define _priq_wait_remove z_priq_simple_remove
57 : : #define _priq_wait_best z_priq_simple_best
58 : : #endif
59 : :
60 : : #if defined(CONFIG_64BIT)
61 : : #define NBITS 64
62 : : #define TRAILING_ZEROS u64_count_trailing_zeros
63 : : #else
64 : : #define NBITS 32
65 : : #define TRAILING_ZEROS u32_count_trailing_zeros
66 : : #endif /* CONFIG_64BIT */
67 : :
68 : 1 : static ALWAYS_INLINE void z_priq_simple_init(sys_dlist_t *pq)
69 : : {
70 : 1 : sys_dlist_init(pq);
71 : 1 : }
72 : :
73 : : /*
74 : : * Return value same as e.g. memcmp
75 : : * > 0 -> thread 1 priority > thread 2 priority
76 : : * = 0 -> thread 1 priority == thread 2 priority
77 : : * < 0 -> thread 1 priority < thread 2 priority
78 : : * Do not rely on the actual value returned aside from the above.
79 : : * (Again, like memcmp.)
80 : : */
81 : 66 : static ALWAYS_INLINE int32_t z_sched_prio_cmp(struct k_thread *thread_1, struct k_thread *thread_2)
82 : : {
83 : : /* `prio` is <32b, so the below cannot overflow. */
84 : 66 : int32_t b1 = thread_1->base.prio;
85 : 66 : int32_t b2 = thread_2->base.prio;
86 : :
87 [ + + ]: 66 : if (b1 != b2) {
88 : 60 : return b2 - b1;
89 : : }
90 : :
91 : : #ifdef CONFIG_SCHED_DEADLINE
92 : : /* If we assume all deadlines live within the same "half" of
93 : : * the 32 bit modulus space (this is a documented API rule),
94 : : * then the latest deadline in the queue minus the earliest is
95 : : * guaranteed to be (2's complement) non-negative. We can
96 : : * leverage that to compare the values without having to check
97 : : * the current time.
98 : : */
99 : : uint32_t d1 = thread_1->base.prio_deadline;
100 : : uint32_t d2 = thread_2->base.prio_deadline;
101 : :
102 : : if (d1 != d2) {
103 : : /* Sooner deadline means higher effective priority.
104 : : * Doing the calculation with unsigned types and casting
105 : : * to signed isn't perfect, but at least reduces this
106 : : * from UB on overflow to impdef.
107 : : */
108 : : return (int32_t)(d2 - d1);
109 : : }
110 : : #endif /* CONFIG_SCHED_DEADLINE */
111 : : return 0;
112 : : }
113 : :
114 : 75 : static ALWAYS_INLINE void z_priq_simple_add(sys_dlist_t *pq, struct k_thread *thread)
115 : : {
116 : 75 : struct k_thread *t;
117 : :
118 [ + + + + ]: 170 : SYS_DLIST_FOR_EACH_CONTAINER(pq, t, base.qnode_dlist) {
119 [ + + ]: 66 : if (z_sched_prio_cmp(thread, t) > 0) {
120 : 52 : sys_dlist_insert(&t->base.qnode_dlist, &thread->base.qnode_dlist);
121 : 52 : return;
122 : : }
123 : : }
124 : :
125 : 23 : sys_dlist_append(pq, &thread->base.qnode_dlist);
126 : : }
127 : :
128 : 74 : static ALWAYS_INLINE void z_priq_simple_remove(sys_dlist_t *pq, struct k_thread *thread)
129 : : {
130 : 74 : ARG_UNUSED(pq);
131 : :
132 : 74 : sys_dlist_remove(&thread->base.qnode_dlist);
133 : 74 : }
134 : :
135 : 0 : static ALWAYS_INLINE void z_priq_simple_yield(sys_dlist_t *pq)
136 : : {
137 : : #ifndef CONFIG_SMP
138 : 0 : sys_dnode_t *n;
139 : :
140 : 0 : n = sys_dlist_peek_next_no_check(pq, &_current->base.qnode_dlist);
141 : :
142 : 0 : sys_dlist_dequeue(&_current->base.qnode_dlist);
143 : :
144 : 0 : struct k_thread *t;
145 : :
146 : : /*
147 : : * As it is possible that the current thread was not at the head of
148 : : * the run queue, start searching from the present position for where
149 : : * to re-insert it.
150 : : */
151 : :
152 [ # # ]: 0 : while (n != NULL) {
153 : 0 : t = CONTAINER_OF(n, struct k_thread, base.qnode_dlist);
154 [ # # ]: 0 : if (z_sched_prio_cmp(_current, t) > 0) {
155 : 0 : sys_dlist_insert(&t->base.qnode_dlist,
156 : : &_current->base.qnode_dlist);
157 : 0 : return;
158 : : }
159 : 0 : n = sys_dlist_peek_next_no_check(pq, n);
160 : : }
161 : :
162 : 0 : sys_dlist_append(pq, &_current->base.qnode_dlist);
163 : : #endif
164 : : }
165 : :
166 : 136 : static ALWAYS_INLINE struct k_thread *z_priq_simple_best(sys_dlist_t *pq)
167 : : {
168 : 136 : struct k_thread *thread = NULL;
169 : 136 : sys_dnode_t *n = sys_dlist_peek_head(pq);
170 : :
171 : 136 : if (n != NULL) {
172 : : thread = CONTAINER_OF(n, struct k_thread, base.qnode_dlist);
173 : : }
174 : 136 : return thread;
175 : : }
176 : :
177 : : #ifdef CONFIG_SCHED_CPU_MASK
178 : : static ALWAYS_INLINE struct k_thread *z_priq_simple_mask_best(sys_dlist_t *pq)
179 : : {
180 : : /* With masks enabled we need to be prepared to walk the list
181 : : * looking for one we can run
182 : : */
183 : : unsigned long current_cpu_bit = BIT(_current_cpu->id);
184 : : struct k_thread *thread;
185 : :
186 : : SYS_DLIST_FOR_EACH_CONTAINER(pq, thread, base.qnode_dlist) {
187 : : if ((thread->base.cpu_mask & current_cpu_bit) != 0) {
188 : : return thread;
189 : : }
190 : : }
191 : : return NULL;
192 : : }
193 : : #endif /* CONFIG_SCHED_CPU_MASK */
194 : :
195 : : #if defined(CONFIG_SCHED_SCALABLE) || defined(CONFIG_WAITQ_SCALABLE)
196 : : static ALWAYS_INLINE void z_priq_rb_init(struct _priq_rb *pq)
197 : : {
198 : : bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
199 : :
200 : : *pq = (struct _priq_rb) {
201 : : .tree = {
202 : : .lessthan_fn = z_priq_rb_lessthan,
203 : : }
204 : : };
205 : : }
206 : :
207 : : static ALWAYS_INLINE void z_priq_rb_add(struct _priq_rb *pq, struct k_thread *thread)
208 : : {
209 : : struct k_thread *t;
210 : :
211 : : thread->base.order_key = pq->next_order_key;
212 : : ++pq->next_order_key;
213 : :
214 : : /* Renumber at wraparound. This is tiny code, and in practice
215 : : * will almost never be hit on real systems. BUT on very
216 : : * long-running systems where a priq never completely empties
217 : : * AND that contains very large numbers of threads, it can be
218 : : * a latency glitch to loop over all the threads like this.
219 : : */
220 : : if (!pq->next_order_key) {
221 : : RB_FOR_EACH_CONTAINER(&pq->tree, t, base.qnode_rb) {
222 : : t->base.order_key = pq->next_order_key;
223 : : ++pq->next_order_key;
224 : : }
225 : : }
226 : :
227 : : rb_insert(&pq->tree, &thread->base.qnode_rb);
228 : : }
229 : :
230 : : static ALWAYS_INLINE void z_priq_rb_remove(struct _priq_rb *pq, struct k_thread *thread)
231 : : {
232 : : rb_remove(&pq->tree, &thread->base.qnode_rb);
233 : :
234 : : if (!pq->tree.root) {
235 : : pq->next_order_key = 0;
236 : : }
237 : : }
238 : :
239 : : static ALWAYS_INLINE void z_priq_rb_yield(struct _priq_rb *pq)
240 : : {
241 : : #ifndef CONFIG_SMP
242 : : z_priq_rb_remove(pq, _current);
243 : : z_priq_rb_add(pq, _current);
244 : : #endif
245 : : }
246 : :
247 : : static ALWAYS_INLINE struct k_thread *z_priq_rb_best(struct _priq_rb *pq)
248 : : {
249 : : struct k_thread *thread = NULL;
250 : : struct rbnode *n = rb_get_min(&pq->tree);
251 : :
252 : : if (n != NULL) {
253 : : thread = CONTAINER_OF(n, struct k_thread, base.qnode_rb);
254 : : }
255 : : return thread;
256 : : }
257 : :
258 : : #ifdef CONFIG_SCHED_CPU_MASK
259 : : static ALWAYS_INLINE struct k_thread *z_priq_rb_mask_best(struct _priq_rb *pq)
260 : : {
261 : : unsigned long current_cpu_bit = BIT(_current_cpu->id);
262 : : struct k_thread *thread;
263 : :
264 : : RB_FOR_EACH_CONTAINER(&pq->tree, thread, base.qnode_rb) {
265 : : if ((thread->base.cpu_mask & current_cpu_bit) != 0) {
266 : : return thread;
267 : : }
268 : : }
269 : : return NULL;
270 : : }
271 : : #endif /* CONFIG_SCHED_CPU_MASK */
272 : : #endif /* CONFIG_SCHED_SCALABLE || CONFIG_WAITQ_SCALABLE */
273 : :
274 : : struct prio_info {
275 : : uint8_t offset_prio;
276 : : uint8_t idx;
277 : : uint8_t bit;
278 : : };
279 : :
280 : : static ALWAYS_INLINE struct prio_info get_prio_info(int8_t old_prio)
281 : : {
282 : : struct prio_info ret;
283 : :
284 : : ret.offset_prio = old_prio - K_HIGHEST_THREAD_PRIO;
285 : : ret.idx = ret.offset_prio / NBITS;
286 : : ret.bit = ret.offset_prio % NBITS;
287 : :
288 : : return ret;
289 : : }
290 : :
291 : : static ALWAYS_INLINE unsigned int z_priq_mq_best_queue_index(struct _priq_mq *pq)
292 : : {
293 : : unsigned int i = 0;
294 : :
295 : : do {
296 : : if (likely(pq->bitmask[i])) {
297 : : return i * NBITS + TRAILING_ZEROS(pq->bitmask[i]);
298 : : }
299 : : i++;
300 : : } while (i < PRIQ_BITMAP_SIZE);
301 : :
302 : : return K_NUM_THREAD_PRIO - 1;
303 : : }
304 : :
305 : : static ALWAYS_INLINE void z_priq_mq_init(struct _priq_mq *q)
306 : : {
307 : : for (size_t i = 0; i < ARRAY_SIZE(q->queues); i++) {
308 : : sys_dlist_init(&q->queues[i]);
309 : : }
310 : :
311 : : #ifndef CONFIG_SMP
312 : : q->cached_queue_index = K_NUM_THREAD_PRIO - 1;
313 : : #endif
314 : : }
315 : :
316 : : static ALWAYS_INLINE void z_priq_mq_add(struct _priq_mq *pq,
317 : : struct k_thread *thread)
318 : : {
319 : : struct prio_info pos = get_prio_info(thread->base.prio);
320 : :
321 : : sys_dlist_append(&pq->queues[pos.offset_prio], &thread->base.qnode_dlist);
322 : : pq->bitmask[pos.idx] |= BIT(pos.bit);
323 : :
324 : : #ifndef CONFIG_SMP
325 : : if (pos.offset_prio < pq->cached_queue_index) {
326 : : pq->cached_queue_index = pos.offset_prio;
327 : : }
328 : : #endif
329 : : }
330 : :
331 : : static ALWAYS_INLINE void z_priq_mq_remove(struct _priq_mq *pq,
332 : : struct k_thread *thread)
333 : : {
334 : : struct prio_info pos = get_prio_info(thread->base.prio);
335 : :
336 : : sys_dlist_dequeue(&thread->base.qnode_dlist);
337 : : if (unlikely(sys_dlist_is_empty(&pq->queues[pos.offset_prio]))) {
338 : : pq->bitmask[pos.idx] &= ~BIT(pos.bit);
339 : : #ifndef CONFIG_SMP
340 : : pq->cached_queue_index = z_priq_mq_best_queue_index(pq);
341 : : #endif
342 : : }
343 : : }
344 : :
345 : : static ALWAYS_INLINE void z_priq_mq_yield(struct _priq_mq *pq)
346 : : {
347 : : #ifndef CONFIG_SMP
348 : : struct prio_info pos = get_prio_info(_current->base.prio);
349 : :
350 : : sys_dlist_dequeue(&_current->base.qnode_dlist);
351 : : sys_dlist_append(&pq->queues[pos.offset_prio],
352 : : &_current->base.qnode_dlist);
353 : : #endif
354 : : }
355 : :
356 : : static ALWAYS_INLINE struct k_thread *z_priq_mq_best(struct _priq_mq *pq)
357 : : {
358 : : #ifdef CONFIG_SMP
359 : : unsigned int index = z_priq_mq_best_queue_index(pq);
360 : : #else
361 : : unsigned int index = pq->cached_queue_index;
362 : : #endif
363 : :
364 : : sys_dnode_t *n = sys_dlist_peek_head(&pq->queues[index]);
365 : :
366 : : if (likely(n != NULL)) {
367 : : return CONTAINER_OF(n, struct k_thread, base.qnode_dlist);
368 : : }
369 : :
370 : : return NULL;
371 : : }
372 : :
373 : : #ifdef CONFIG_SCHED_CPU_MASK
374 : : static ALWAYS_INLINE struct k_thread *z_priq_mq_mask_best(struct _priq_mq *pq)
375 : : {
376 : : unsigned long current_cpu_bit = BIT(_current_cpu->id);
377 : :
378 : : /* Walk priority levels from highest to lowest, and within each
379 : : * level walk the thread list to find the first thread runnable
380 : : * on the current CPU.
381 : : */
382 : : for (unsigned int i = 0U; i < PRIQ_BITMAP_SIZE; i++) {
383 : : unsigned long bitmask = pq->bitmask[i];
384 : :
385 : : while (bitmask != 0UL) {
386 : : unsigned int bit = TRAILING_ZEROS(bitmask);
387 : : unsigned int index = i * NBITS + bit;
388 : : struct k_thread *thread;
389 : :
390 : : SYS_DLIST_FOR_EACH_CONTAINER(&pq->queues[index],
391 : : thread, base.qnode_dlist) {
392 : : if ((thread->base.cpu_mask &
393 : : current_cpu_bit) != 0) {
394 : : return thread;
395 : : }
396 : : }
397 : : bitmask &= (bitmask - 1UL);
398 : : }
399 : : }
400 : : return NULL;
401 : : }
402 : : #endif /* CONFIG_SCHED_CPU_MASK */
403 : :
404 : : #endif /* ZEPHYR_KERNEL_INCLUDE_PRIORITY_Q_H_ */
|