|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Instituto Superior de Engenharia do Porto (ISEP) |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * @file |
| 8 | + * |
| 9 | + * @brief Constant Bandwidth Server (CBS) public API |
| 10 | + */ |
| 11 | + |
| 12 | +#ifndef ZEPHYR_CBS |
| 13 | +#define ZEPHYR_CBS |
| 14 | + |
| 15 | +#include <zephyr/kernel.h> |
| 16 | + |
| 17 | +#ifdef __cplusplus |
| 18 | +extern "C" { |
| 19 | +#endif |
| 20 | + |
| 21 | +#ifdef CONFIG_CBS |
| 22 | + |
| 23 | +/** |
| 24 | + * @defgroup cbs_apis Constant Bandwidth Server (CBS) APIs |
| 25 | + * @ingroup kernel_apis |
| 26 | + * @{ |
| 27 | + */ |
| 28 | + |
| 29 | +/** |
| 30 | + * @brief CBS job format. |
| 31 | + * |
| 32 | + * A job pushed to a CBS is a regular function that must |
| 33 | + * have a void pointer as an argument (can be NULL if not |
| 34 | + * needed) and return nothing. jobs are pushed to the CBS by |
| 35 | + * invoking k_cbs_push_job(). |
| 36 | + */ |
| 37 | +typedef void (*cbs_callback_t)(void *arg); |
| 38 | + |
| 39 | +/** |
| 40 | + * @cond INTERNAL_HIDDEN |
| 41 | + */ |
| 42 | + |
| 43 | +#ifdef CONFIG_TIMER_HAS_64BIT_CYCLE_COUNTER |
| 44 | +typedef uint64_t cbs_cycle_t; |
| 45 | +#else |
| 46 | +typedef uint32_t cbs_cycle_t; |
| 47 | +#endif |
| 48 | + |
| 49 | +struct cbs_job { |
| 50 | + cbs_callback_t function; |
| 51 | + void *arg; |
| 52 | +}; |
| 53 | + |
| 54 | +struct cbs_budget { |
| 55 | + cbs_cycle_t current; |
| 56 | + cbs_cycle_t max; |
| 57 | +}; |
| 58 | + |
| 59 | +struct cbs_arg { |
| 60 | + k_timeout_t budget; |
| 61 | + k_timeout_t period; |
| 62 | +}; |
| 63 | + |
| 64 | +struct k_cbs { |
| 65 | + struct k_timer timer; |
| 66 | + struct k_msgq *queue; |
| 67 | + struct k_thread *thread; |
| 68 | + struct cbs_budget budget; |
| 69 | + cbs_cycle_t period; |
| 70 | + cbs_cycle_t abs_deadline; |
| 71 | + cbs_cycle_t start_cycle; |
| 72 | + cbs_cycle_t bandwidth; |
| 73 | + bool is_active; |
| 74 | + unsigned int left_shift; |
| 75 | +#ifdef CONFIG_CBS_LOG |
| 76 | + char name[CONFIG_CBS_THREAD_MAX_NAME_LEN]; |
| 77 | +#endif |
| 78 | +}; |
| 79 | + |
| 80 | +extern void cbs_thread(void *job_queue, void *cbs_struct, void *unused); |
| 81 | + |
| 82 | +/** @endcond */ |
| 83 | + |
| 84 | +/** |
| 85 | + * @brief pushes a job to a CBS queue. |
| 86 | + * |
| 87 | + * This routine inserts a job (i.e. a regular C function) in a CBS queue, |
| 88 | + * which will eventually execute it when the CBS deadline becomes the |
| 89 | + * earliest of the taskset. Inserted jobs are always served in a FIFO |
| 90 | + * manner. The job queue can store up to @kconfig{CONFIG_CBS_QUEUE_LENGTH} |
| 91 | + * jobs at once. |
| 92 | + * |
| 93 | + * @param cbs Name of the CBS. |
| 94 | + * @param job_function Function of the job. |
| 95 | + * @param job_arg Argument to be passed to the job function. |
| 96 | + * @param timeout Waiting period to push the job, or one of the special |
| 97 | + * values K_NO_WAIT and K_FOREVER. |
| 98 | + * |
| 99 | + * @retval 0 the job was pushed to the CBS queue. |
| 100 | + * @retval -ENOMSG if CBS is not defined, returned without waiting or CBS queue purged. |
| 101 | + * @retval -EAGAIN if waiting period timed out. |
| 102 | + */ |
| 103 | +int k_cbs_push_job(struct k_cbs *cbs, cbs_callback_t job_function, void *job_arg, |
| 104 | + k_timeout_t timeout); |
| 105 | + |
| 106 | +/** |
| 107 | + * @brief Statically define and initialize a Constant Bandwidth Server (CBS). |
| 108 | + * |
| 109 | + * A CBS is an extension of the Earliest Deadline First (EDF) scheduler |
| 110 | + * that allows tasks to be executed virtually isolated from each other, |
| 111 | + * in a way that if a task executes for longer than expected it doesn’t |
| 112 | + * interfere on the execution of the others. In other words, the CBS |
| 113 | + * prevents that a task misbehavior causes other tasks to miss their |
| 114 | + * own deadlines. |
| 115 | + * |
| 116 | + * In a nutshell, the CBS is a work-conserving wrapper for the tasks |
| 117 | + * that automatically recalculates their deadlines when they exceed their |
| 118 | + * allowed execution time slice. This time slice is known as the CBS "budget". |
| 119 | + * The value used to recalculate the deadline is known as the CBS "period". |
| 120 | + * |
| 121 | + * When a task instance (i.e. job) runs within a CBS, it consumes the "budget". |
| 122 | + * When the "budget" runs out, the deadline is postponed by "period" time units |
| 123 | + * and the "budget" is replenished to its maximum capacity. when there are no jobs |
| 124 | + * left for a CBS to execute, it remains idle and takes no CPU time. |
| 125 | + * |
| 126 | + * Finally, whenever a new job is pushed to an idle server, the kernel verifies |
| 127 | + * if the current pair of (budget, deadline) are proportionally compatible with |
| 128 | + * the configured values. If not, the deadline is also recalculated here. |
| 129 | + * These two procedures ensure the CBS will never use the CPU more than |
| 130 | + * what was configured, and that it will not endanger other thread's deadlines. |
| 131 | + * |
| 132 | + * Once created, the CBS can be referenced through its name: |
| 133 | + * |
| 134 | + * @code extern const struct k_cbs <cbs_name>; @endcode |
| 135 | + * |
| 136 | + * @param cbs_name Name of the CBS. |
| 137 | + * @param cbs_budget Budget of the CBS thread, in system ticks. |
| 138 | + * Used for triggering deadline recalculations. |
| 139 | + * @param cbs_period Period of the CBS thread. in system ticks. |
| 140 | + * Used for recalculating the absolute deadline. |
| 141 | + * @param cbs_static_priority Static priority of the CBS thread. |
| 142 | + * |
| 143 | + * @note The CBS is meant to be used alongside the EDF policy, which in Zephyr |
| 144 | + * is effectively used as a "tie-breaker" when two threads of equal static priorities |
| 145 | + * are ready for execution. Therefore it is recommended that all user threads feature |
| 146 | + * the same preemptive static priority (e.g. 5) in order to ensure the scheduling |
| 147 | + * to work as expected, and that this same value is passed as @a cbs_static_priority. |
| 148 | + * |
| 149 | + * @note you should have @kconfig{CONFIG_CBS} enabled in your project to use the CBS. |
| 150 | + */ |
| 151 | +#define K_CBS_DEFINE(cbs_name, cbs_budget, cbs_period, cbs_static_priority) \ |
| 152 | + K_MSGQ_DEFINE(queue_##cbs_name, sizeof(struct cbs_job), CONFIG_CBS_QUEUE_LENGTH, 1); \ |
| 153 | + static struct k_cbs cbs_name = { \ |
| 154 | + .queue = &queue_##cbs_name, \ |
| 155 | + .is_active = false, \ |
| 156 | + }; \ |
| 157 | + static struct cbs_arg args_##cbs_name = {.budget = cbs_budget, .period = cbs_period}; \ |
| 158 | + K_THREAD_DEFINE(thread_##cbs_name, CONFIG_CBS_THREAD_STACK_SIZE, cbs_thread, \ |
| 159 | + (void *)STRINGIFY(cbs_name), (void *)&cbs_name, (void *)&args_##cbs_name, \ |
| 160 | + cbs_static_priority, 0, CONFIG_CBS_INITIAL_DELAY) |
| 161 | +/** @} */ /* end of Constant Bandwidth Server (CBS) APIs */ |
| 162 | + |
| 163 | +#endif /* CONFIG_CBS */ |
| 164 | + |
| 165 | +#ifdef __cplusplus |
| 166 | +} |
| 167 | +#endif |
| 168 | + |
| 169 | +#endif /* ZEPHYR_CBS */ |
0 commit comments