summaryrefslogtreecommitdiffstats
path: root/cpukit/include/rtems/score/schedulercbs.h
blob: 4b7efc8340accef35a2665882ca00ea701a0494f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
 * @file
 *
 * @ingroup RTEMSScoreSchedulerCBS
 *
 * @brief This header file provides interfaces of the
 *   @ref RTEMSScoreSchedulerCBS which are used by the implementation and the
 *   @ref RTEMSImplApplConfig.
 */

/*
 *  Copryight (c) 2011 Petr Benes.
 *  Copyright (C) 2011 On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#ifndef _RTEMS_SCORE_SCHEDULERCBS_H
#define _RTEMS_SCORE_SCHEDULERCBS_H

#include <rtems/score/chain.h>
#include <rtems/score/priority.h>
#include <rtems/score/scheduler.h>
#include <rtems/score/rbtree.h>
#include <rtems/score/scheduleredf.h>
#include <rtems/rtems/signal.h>
#include <rtems/rtems/timer.h>
#include <rtems/score/thread.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @defgroup RTEMSScoreSchedulerCBS Constant Bandwidth Server (CBS) Scheduler
 *
 * @ingroup RTEMSScoreScheduler
 *
 * @brief This group contains the Constant Bandwidth Server (CBS) Scheduler
 *   implementation.
 *
 * @{
 */

#define SCHEDULER_CBS_MAXIMUM_PRIORITY SCHEDULER_EDF_MAXIMUM_PRIORITY

/**
 *  Entry points for the Constant Bandwidth Server Scheduler.
 *
 *  @note: The CBS scheduler is an enhancement of EDF scheduler,
 *         therefor some routines are similar.
 */
#define SCHEDULER_CBS_ENTRY_POINTS \
  { \
    _Scheduler_EDF_Initialize,       /* initialize entry point */ \
    _Scheduler_EDF_Schedule,         /* schedule entry point */ \
    _Scheduler_EDF_Yield,            /* yield entry point */ \
    _Scheduler_EDF_Block,            /* block entry point */ \
    _Scheduler_CBS_Unblock,          /* unblock entry point */ \
    _Scheduler_EDF_Update_priority,  /* update priority entry point */ \
    _Scheduler_EDF_Map_priority,     /* map priority entry point */ \
    _Scheduler_EDF_Unmap_priority,   /* unmap priority entry point */ \
    SCHEDULER_DEFAULT_SMP_OPERATIONS \
    _Scheduler_CBS_Node_initialize,  /* node initialize entry point */ \
    _Scheduler_default_Node_destroy, /* node destroy entry point */ \
    _Scheduler_CBS_Release_job,      /* new period of task */ \
    _Scheduler_CBS_Cancel_job,       /* cancel period of task */ \
    _Scheduler_default_Tick,         /* tick entry point */ \
    _Scheduler_default_Start_idle    /* start idle entry point */ \
    SCHEDULER_DEFAULT_SET_AFFINITY_OPERATION \
  }

/* Return values for CBS server. */
#define SCHEDULER_CBS_OK                        0
#define SCHEDULER_CBS_ERROR_GENERIC            -16
#define SCHEDULER_CBS_ERROR_NO_MEMORY          -17
#define SCHEDULER_CBS_ERROR_INVALID_PARAMETER  -18
#define SCHEDULER_CBS_ERROR_UNAUTHORIZED       -19
#define SCHEDULER_CBS_ERROR_UNIMPLEMENTED      -20
#define SCHEDULER_CBS_ERROR_MISSING_COMPONENT  -21
#define SCHEDULER_CBS_ERROR_INCONSISTENT_STATE -22
#define SCHEDULER_CBS_ERROR_SYSTEM_OVERLOAD    -23
#define SCHEDULER_CBS_ERROR_INTERNAL_ERROR     -24
#define SCHEDULER_CBS_ERROR_NOT_FOUND          -25
#define SCHEDULER_CBS_ERROR_FULL               -26
#define SCHEDULER_CBS_ERROR_EMPTY              -27
#define SCHEDULER_CBS_ERROR_NOSERVER           SCHEDULER_CBS_ERROR_NOT_FOUND

/** Maximum number of simultaneous servers. */
extern const uint32_t _Scheduler_CBS_Maximum_servers;

/** Server id. */
typedef uint32_t Scheduler_CBS_Server_id;

/** Callback function invoked when a budget overrun of a task occurs. */
typedef void (*Scheduler_CBS_Budget_overrun)(
    Scheduler_CBS_Server_id server_id
);

/**
 * This structure handles server parameters.
 */
typedef struct {
  /** Relative deadline of the server. */
  time_t deadline;
  /** Budget (computation time) of the server. */
  time_t budget;
} Scheduler_CBS_Parameters;

/**
 * This structure represents a time server.
 */
typedef struct {
  /**
   * Task id.
   *
   * @note: The current implementation of CBS handles only one task per server.
   */
  rtems_id                 task_id;
  /** Server paramenters. */
  Scheduler_CBS_Parameters parameters;
  /** Callback function invoked when a budget overrun occurs. */
  Scheduler_CBS_Budget_overrun  cbs_budget_overrun;

  /**
   * @brief Indicates if this CBS server is initialized.
   *
   * @see _Scheduler_CBS_Create_server() and _Scheduler_CBS_Destroy_server().
   */
  bool initialized;
} Scheduler_CBS_Server;

/**
 * This structure handles CBS specific data of a thread.
 */
typedef struct {
  /** EDF scheduler specific data of a task. */
  Scheduler_EDF_Node            Base;
  /** CBS server specific data of a task. */
  Scheduler_CBS_Server         *cbs_server;

  Priority_Node                *deadline_node;
} Scheduler_CBS_Node;


/**
 * List of servers. The @a Scheduler_CBS_Server is the index to the array
 * of pointers to @a _Scheduler_CBS_Server_list.
 */
extern Scheduler_CBS_Server _Scheduler_CBS_Server_list[];

/**
 * @brief Unblocks a thread.
 *
 * @param scheduler The scheduler control.
 * @param the_thread The thread to unblock.
 * @param node The scheduler node.
 */
void _Scheduler_CBS_Unblock(
  const Scheduler_Control *scheduler,
  Thread_Control          *the_thread,
  Scheduler_Node          *node
);

/**
 * @brief Releases a job.
 *
 * @param scheduler The scheduler for the operation.
 * @param the_thread The corresponding thread.
 * @param priority_node The priority node for the operation.
 * @param deadline The deadline for the job.
 * @param queue_context The thread queue context.
 */
void _Scheduler_CBS_Release_job(
  const Scheduler_Control *scheduler,
  Thread_Control          *the_thread,
  Priority_Node           *priority_node,
  uint64_t                 deadline,
  Thread_queue_Context    *queue_context
);

/**
 * @brief Cancels a job.
 *
 * @param scheduler The scheduler for the operation.
 * @param the_thread The corresponding thread.
 * @param priority_node The priority node for the operation.
 * @param queue_context The thread queue context.
 */
void _Scheduler_CBS_Cancel_job(
  const Scheduler_Control *scheduler,
  Thread_Control          *the_thread,
  Priority_Node           *priority_node,
  Thread_queue_Context    *queue_context
);

/**
 * @brief _Scheduler_CBS_Initialize
 *
 * Initializes the CBS library.
 *
 * @return SCHEDULER_CBS_OK This method always returns this status.
 */
int _Scheduler_CBS_Initialize(void);

/**
 * @brief Attaches a task to an already existing server.
 *
 * Attach a task to an already existing server.
 *
 * @param server_id The id of the existing server.
 * @param task_id The id of the task to attach.
 *
 * @retval SCHEDULER_CBS_OK The operation was successful.
 * @retval SCHEDULER_CBS_ERROR_INVALID_PARAMETER The server id is so big
 *      or there is no thread for this task id.
 * @retval SCHEDULER_CBS_ERROR_NOSERVER The server is not yet initialized.
 * @retval SCHEDULER_CBS_ERROR_FULL The server already has a task.
 */
int _Scheduler_CBS_Attach_thread (
  Scheduler_CBS_Server_id server_id,
  rtems_id                task_id
);

/**
 * @brief Detaches from the CBS Server.
 *
 * Detach from the CBS Server.
 *
 * @param server_id The id of the existing server.
 * @param task_id The id of the task to attach.
 *
 * @retval SCHEDULER_CBS_OK The operation was successful.
 * @retval SCHEDULER_CBS_ERROR_INVALID_PARAMETER The server id is to big,
 *      or the task with this id is not attached to this server or there is
 *      no thread with this task.
 * @retval SCHEDULER_CBS_ERROR_NOSERVER The server is not yet initialized.
 */
int _Scheduler_CBS_Detach_thread (
  Scheduler_CBS_Server_id server_id,
  rtems_id                task_id
);

/**
 * @brief Cleans up resources associated to the CBS Library.
 *
 * Cleanup resources associated to the CBS Library.
 *
 * @return This method always returns SCHEDULER_CBS_OK.
 */
int _Scheduler_CBS_Cleanup (void);

/**
 * @brief Creates a new server with specified parameters.
 *
 * Create a new server with specified parameters.
 *
 * @param params The parameters for the server.
 * @param budget_overrun_callback The budget overrun for the new server.
 * @param[out] server_id In the case of success, this parameter contains the
 *      id of the newly created server.
 *
 * @retval SCHEDULER_CBS_OK The operation succeeded.
 * @retval SCHEDULER_CBS_ERROR_INVALID_PARAMETER The given parameters are invalid.
 * @retval SCHEDULER_CBS_ERROR_FULL The maximum number of servers was already
 *      created, a new server cannot be created.
 */
int _Scheduler_CBS_Create_server (
  Scheduler_CBS_Parameters     *params,
  Scheduler_CBS_Budget_overrun  budget_overrun_callback,
  rtems_id                     *server_id
);

/**
 * @brief Detaches all tasks from a server and destroys it.
 *
 * Detach all tasks from a server and destroy it.
 *
 * @param server_id The id of the server to destroy.
 *
 * @retval SCHEDULER_CBS_OK The operation was successful.
 * @retval SCHEDULER_CBS_ERROR_INVALID_PARAMETER The server id is too big.
 * @retval SCHEDULER_CBS_ERROR_NOSERVER There is no initialized server with this id.
 */
int _Scheduler_CBS_Destroy_server (
  Scheduler_CBS_Server_id server_id
);

/**
 * @brief Retrieves the approved budget.
 *
 * Retrieve the budget that has been approved for the subsequent
 * server instances.
 *
 * @param server_id The id of the server instance of which the budget is wanted.
 * @param[out] approved_budget Contains the approved budget after a successful method call.
 *
 * @retval SCHEDULER_CBS_OK The operation was successful.
 * @retval SCHEDULER_CBS_ERROR_INVALID_PARAMETER The server id is too big.
 * @retval SCHEDULER_CBS_ERROR_NOSERVER There is no initialized server with this id.
 */
int _Scheduler_CBS_Get_approved_budget (
  Scheduler_CBS_Server_id  server_id,
  time_t                  *approved_budget
);

/**
 * @brief Retrieves remaining budget for the current server instance.
 *
 * Retrieve remaining budget for the current server instance.
 *
 * @param server_id The id of the server instance of which the remaining budget is wanted.
 * @param[out] remaining_budget Contains the remaining budget after a successful method call.
 *
 * @retval SCHEDULER_CBS_OK The operation was successful.
 * @retval SCHEDULER_CBS_ERROR_INVALID_PARAMETER The server id is too big.
 * @retval SCHEDULER_CBS_ERROR_NOSERVER There is no initialized server with this id.
 */
int _Scheduler_CBS_Get_remaining_budget (
  Scheduler_CBS_Server_id  server_id,
  time_t                  *remaining_budget
);

/**
 * @brief Gets relative time info.
 *
 * Retrieve time info relative to @a server_id. The server status code is returned.
 *
 * @param server_id is the server to get the status code from.
 * @param[out] exec_time Contains the execution time after a successful method call.
 * @param abs_time Not apparently used.
 *
 * @retval SCHEDULER_CBS_OK The operation was successful.
 * @retval SCHEDULER_CBS_ERROR_INVALID_PARAMETER The server id is too big.
 * @retval SCHEDULER_CBS_ERROR_NOSERVER There is no initialized server with this id.
 */
int _Scheduler_CBS_Get_execution_time (
  Scheduler_CBS_Server_id   server_id,
  time_t                   *exec_time,
  time_t                   *abs_time
);

/**
 * @brief Retrieves CBS scheduling parameters.
 *
 * Retrieve CBS scheduling parameters.
 *
 * @param server_id The id of the server to get the scheduling parameters from.
 * @param[out] params Will contain the scheduling parameters after successful method call.
 *
 * @retval SCHEDULER_CBS_OK The operation was successful.
 * @retval SCHEDULER_CBS_ERROR_INVALID_PARAMETER The server id is too big.
 * @retval SCHEDULER_CBS_ERROR_NOSERVER There is no initialized server with this id.
 */
int _Scheduler_CBS_Get_parameters (
  Scheduler_CBS_Server_id   server_id,
  Scheduler_CBS_Parameters *params
);

/**
 * @brief Gets a thread server id.
 *
 * Get a thread server id, or SCHEDULER_CBS_ERROR_NOT_FOUND if it is not
 * attached to any server.
 *
 * @param task_id The id of the task to get the corresponding server.
 * @param[out] server_id Will contain the server id after successful method call.
 * @retval SCHEDULER_CBS_OK The operation was successful
 * @retval SCHEDULER_CBS_ERROR_NOSERVER There is no server with this task attached.
 */
int _Scheduler_CBS_Get_server_id (
  rtems_id                 task_id,
  Scheduler_CBS_Server_id *server_id
);

/**
 * @brief Sets parameters for CBS scheduling.
 *
 * Change CBS scheduling parameters.
 *
 * @param server_id The id of the server.
 * @param parameters The parameters to set.
 *
 * @retval SCHEDULER_CBS_OK The operation was successful.
 * @retval SCHEDULER_CBS_ERROR_INVALID_PARAMETER The server id is too big or the
 *      given parameters are invalid.
 * @retval SCHEDULER_CBS_ERROR_NOSERVER There is no server with this id.
 */
int _Scheduler_CBS_Set_parameters (
  Scheduler_CBS_Server_id   server_id,
  Scheduler_CBS_Parameters *parameters
);

/**
 * @brief Invoked when a limited time quantum is exceeded.
 *
 * This routine is invoked when a limited time quantum is exceeded.
 *
 * @param the_thread The thread that exceeded a limited time quantum.
 */
void _Scheduler_CBS_Budget_callout(
  Thread_Control *the_thread
);

/**
 * @brief Initializes a CBS specific scheduler node of @a the_thread.
 *
 * @param scheduler The scheduler control for the operation.
 * @param[out] node The scheduler node to initalize.
 * @param the_thread The thread to initialize a scheduler node for.
 * @param priority The priority for the node.
 */
void _Scheduler_CBS_Node_initialize(
  const Scheduler_Control *scheduler,
  Scheduler_Node          *node,
  Thread_Control          *the_thread,
  Priority_Control         priority
);

#ifdef __cplusplus
}
#endif

/** @} */

#endif
/* end of include file */