source: rtems-docs/c-user/constant_bandwidth_server.rst

Last change on this file was aaba6e5, checked in by Joel Sherrill <joel@…>, on 11/30/22 at 15:59:20

c-user/*: Add trailing parentheses on methods in index which were missing it

Closes #4766.

  • Property mode set to 100644
File size: 19.6 KB
Line 
1.. SPDX-License-Identifier: CC-BY-SA-4.0
2
3.. Copyright (C) 2011 Petr Benes
4.. Copyright (C) 1989, 2011 On-Line Applications Research Corporation (OAR)
5
6.. index:: cbs
7
8Constant Bandwidth Server Scheduler API
9***************************************
10
11Introduction
12============
13
14Unlike simple schedulers, the Constant Bandwidth Server (CBS) requires a
15special API for tasks to indicate their scheduling parameters.  The directives
16provided by the CBS API are:
17
18- rtems_cbs_initialize_ - Initialize the CBS library
19
20- rtems_cbs_cleanup_ - Cleanup the CBS library
21
22- rtems_cbs_create_server_ - Create a new bandwidth server
23
24- rtems_cbs_attach_thread_ - Attach a thread to server
25
26- rtems_cbs_detach_thread_ - Detach a thread from server
27
28- rtems_cbs_destroy_server_ - Destroy a bandwidth server
29
30- rtems_cbs_get_server_id_ - Get an ID of a server
31
32- rtems_cbs_get_parameters_ - Get scheduling parameters of a server
33
34- rtems_cbs_set_parameters_ - Set scheduling parameters of a server
35
36- rtems_cbs_get_execution_time_ - Get elapsed execution time
37
38- rtems_cbs_get_remaining_budget_ - Get remainig execution time
39
40- rtems_cbs_get_approved_budget_ - Get scheduler approved execution time
41
42.. index:: CBS parameters
43.. index:: rtems_cbs_parameters
44
45Background
46==========
47
48Constant Bandwidth Server Definitions
49-------------------------------------
50
51The Constant Bandwidth Server API enables tasks to communicate with the
52scheduler and indicate its scheduling parameters. The scheduler has to be set
53up first (by defining ``CONFIGURE_SCHEDULER_CBS`` macro).
54
55The difference to a plain EDF is the presence of servers.  It is a budget aware
56extention of the EDF scheduler, therefore, tasks attached to servers behave in
57a similar way as with EDF unless they exceed their budget.
58
59The intention of servers is reservation of a certain computation time (budget)
60of the processor for all subsequent periods. The structure
61``rtems_cbs_parameters`` determines the behavior of a server. It contains
62``deadline`` which is equal to period, and ``budget`` which is the time the
63server is allowed to spend on CPU per each period. The ratio between those two
64parameters yields the maximum percentage of the CPU the server can use
65(bandwidth). Moreover, thanks to this limitation the overall utilization of CPU
66is under control, and the sum of bandwidths of all servers in the system yields
67the overall reserved portion of processor. The rest is still available for
68ordinary tasks that are not attached to any server.
69
70In order to make the server effective to the executing tasks, tasks have to be
71attached to the servers. The ``rtems_cbs_server_id`` is a type denoting an id
72of a server and ``rtems_id`` a type for id of tasks.
73.. index:: CBS periodic tasks
74
75Handling Periodic Tasks
76-----------------------
77
78Each task's execution begins with a default background priority (see the
79chapter Scheduling Concepts to understand the concept of priorities in
80EDF). Once you decide the tasks should start periodic execution, you have two
81possibilities. Either you use only the Rate Monotonic manager which takes care
82of periodic behavior, or you declare deadline and budget using the CBS API in
83which case these properties are constant for all subsequent periods, unless you
84change them using the CBS API again. Task now only has to indicate and end of
85each period using ``rtems_rate_monotonic_period``.
86.. index:: CBS overrun handler
87
88Registering a Callback Function
89-------------------------------
90
91In case tasks attached to servers are not aware of their execution time and
92happen to exceed it, the scheduler does not guarantee execution any more and
93pulls the priority of the task to background, which would possibly lead to
94immediate preemption (if there is at least one ready task with a higher
95pirority). However, the task is not blocked but a callback function is
96invoked. The callback function (``rtems_cbs_budget_overrun``) might be
97optionally registered upon a server creation (``rtems_cbs_create_server``).
98
99This enables the user to define what should happen in case of budget
100overrun. There is obviously no space for huge operations because the priority
101is down and not real time any more, however, you still can at least in release
102resources for other tasks, restart the task or log an error information. Since
103the routine is called directly from kernel, use ``printk()`` instead of
104``printf()``.
105
106The calling convention of the callback function is:
107
108.. index:: rtems_asr
109
110.. code-block:: c
111
112    void overrun_handler(
113        rtems_cbs_server_id server_id
114    );
115.. index:: CBS limitations
116
117Limitations
118-----------
119
120When using this scheduler you have to keep in mind several things:
121
122- it_limitations
123
124- In the current implementation it is possible to attach only a single task to
125  each server.
126
127- If you have a task attached to a server and you voluntatily block it in the
128  beginning of its execution, its priority will be probably pulled to
129  background upon unblock, thus not guaranteed deadline any more. This is
130  because you are effectively raising computation time of the task. When
131  unbocking, you should be always sure that the ratio between remaining
132  computation time and remaining deadline is not higher that the utilization
133  you have agreed with the scheduler.
134
135Operations
136==========
137
138Setting up a server
139-------------------
140
141The directive ``rtems_cbs_create_server`` is used to create a new server that
142is characterized by ``rtems_cbs_parameters``. You also might want to register
143the ``rtems_cbs_budget_overrun`` callback routine. After this step tasks can be
144attached to the server. The directive ``rtems_cbs_set_parameters`` can change
145the scheduling parameters to avoid destroying and creating a new server again.
146
147Attaching Task to a Server
148--------------------------
149
150If a task is attached to a server using ``rtems_cbs_attach_thread``, the task's
151computation time per period is limited by the server and the deadline (period)
152of task is equal to deadline of the server which means if you conclude a period
153using ``rate_monotonic_period``, the length of next period is always determined
154by the server's property.
155
156The task has a guaranteed bandwidth given by the server but should not exceed
157it, otherwise the priority is pulled to background until the start of next
158period and the ``rtems_cbs_budget_overrun`` callback function is invoked.
159
160When attaching a task to server, the preemptability flag of the task is raised,
161otherwise it would not be possible to control the execution of the task.
162
163Detaching Task from a Server
164----------------------------
165
166The directive ``rtems_cbs_detach_thread`` is just an inverse operation to the
167previous one, the task continues its execution with the initial priority.
168
169Preemptability of the task is restored to the initial value.
170
171Examples
172--------
173
174The following example presents a simple common use of the API.
175
176You can see the initialization and cleanup call here, if there are multiple
177tasks in the system, it is obvious that the initialization should be called
178before creating the task.
179
180Notice also that in this case we decided to register an overrun handler,
181instead of which there could be ``NULL``. This handler just prints a message to
182terminal, what else may be done here depends on a specific application.
183
184During the periodic execution, remaining budget should be watched to avoid
185overrun.
186
187.. code-block:: c
188
189    void overrun_handler (
190        rtems_cbs_server_id server_id
191    )
192    {
193        printk( "Budget overrun, fixing the task\n" );
194        return;
195    }
196
197    rtems_task Tasks_Periodic(
198        rtems_task_argument argument
199    )
200    {
201        rtems_id             rmid;
202        rtems_cbs_server_id  server_id;
203        rtems_cbs_parameters params;
204
205        params.deadline = 10;
206        params.budget = 4;
207
208        rtems_cbs_initialize();
209        rtems_cbs_create_server( &params, &overrun_handler, &server_id );
210        rtems_cbs_attach_thread( server_id, RTEMS_SELF );
211        rtems_rate_monotonic_create( argument, &rmid );
212
213        while ( 1 ) {
214            if (rtems_rate_monotonic_period(rmid, params.deadline) == RTEMS_TIMEOUT)
215                break;
216            /* Perform some periodic action */
217        }
218
219        rtems_rate_monotonic_delete( rmid );
220        rtems_cbs_cleanup();
221        exit( 1 );
222    }
223
224Directives
225==========
226
227This section details the Constant Bandwidth Server's directives.  A subsection
228is dedicated to each of this manager's directives and describes the calling
229sequence, related constants, usage, and status codes.
230
231.. raw:: latex
232
233   \clearpage
234
235.. index:: initialize the CBS library
236.. index:: rtems_cbs_initialize()
237.. _rtems_cbs_initialize:
238
239CBS_INITIALIZE - Initialize the CBS library
240-------------------------------------------
241
242CALLING SEQUENCE:
243    .. code-block:: c
244
245        int rtems_cbs_initialize( void );
246
247DIRECTIVE STATUS CODES:
248    .. list-table::
249     :class: rtems-table
250
251     * - ``RTEMS_CBS_OK``
252       - successful initialization
253     * - ``RTEMS_CBS_ERROR_NO_MEMORY``
254       - not enough memory for data
255
256DESCRIPTION:
257    This routine initializes the library in terms of allocating necessary
258    memory for the servers. In case not enough memory is available in the
259    system, ``RTEMS_CBS_ERROR_NO_MEMORY`` is returned, otherwise
260    ``RTEMS_CBS_OK``.
261
262NOTES:
263    Additional memory per each server is allocated upon invocation of
264    ``rtems_cbs_create_server``.
265
266    Tasks in the system are not influenced, they still keep executing with
267    their initial parameters.
268
269.. raw:: latex
270
271   \clearpage
272
273.. index:: cleanup the CBS library
274.. index:: rtems_cbs_cleanup()
275
276.. _rtems_cbs_cleanup:
277
278CBS_CLEANUP - Cleanup the CBS library
279-------------------------------------
280
281CALLING SEQUENCE:
282    .. code-block:: c
283
284        int rtems_cbs_cleanup( void );
285
286DIRECTIVE STATUS CODES:
287    .. list-table::
288     :class: rtems-table
289
290     * - ``RTEMS_CBS_OK``
291       - always successful
292
293DESCRIPTION:
294    This routine detaches all tasks from their servers, destroys all servers
295    and returns memory back to the system.
296
297NOTES:
298    All tasks continue executing with their initial priorities.
299
300.. raw:: latex
301
302   \clearpage
303
304.. index:: create a new bandwidth server
305.. index:: rtems_cbs_create_server()
306
307.. _rtems_cbs_create_server:
308
309CBS_CREATE_SERVER - Create a new bandwidth server
310-------------------------------------------------
311
312CALLING SEQUENCE:
313    .. code-block:: c
314
315        int rtems_cbs_create_server (
316            rtems_cbs_parameters     *params,
317            rtems_cbs_budget_overrun  budget_overrun_callback,
318            rtems_cbs_server_id      *server_id
319        );
320
321DIRECTIVE STATUS CODES:
322    .. list-table::
323     :class: rtems-table
324
325     * - ``RTEMS_CBS_OK``
326       - successfully created
327     * - ``RTEMS_CBS_ERROR_NO_MEMORY``
328       - not enough memory for data
329     * - ``RTEMS_CBS_ERROR_FULL``
330       - maximum servers exceeded
331     * - ``RTEMS_CBS_ERROR_INVALID_PARAMETER``
332       - invalid input argument
333
334DESCRIPTION:
335    This routine prepares an instance of a constant bandwidth server.  The
336    input parameter ``rtems_cbs_parameters`` specifies scheduling parameters of
337    the server (period and budget). If these are not valid,
338    ``RTEMS_CBS_ERROR_INVALID_PARAMETER`` is returned.  The
339    ``budget_overrun_callback`` is an optional callback function, which is
340    invoked in case the server's budget within one period is exceeded.  Output
341    parameter ``server_id`` becomes an id of the newly created server.  If
342    there is not enough memory, the ``RTEMS_CBS_ERROR_NO_MEMORY`` is
343    returned. If the maximum server count in the system is exceeded,
344    ``RTEMS_CBS_ERROR_FULL`` is returned.
345
346NOTES:
347    No task execution is being influenced so far.
348
349.. raw:: latex
350
351   \clearpage
352
353.. index:: attach a thread to server
354.. index:: rtems_cbs_attach_thread()
355
356.. _rtems_cbs_attach_thread:
357
358CBS_ATTACH_THREAD - Attach a thread to server
359---------------------------------------------
360
361CALLING SEQUENCE:
362    .. code-block:: c
363
364        int rtems_cbs_attach_thread (
365            rtems_cbs_server_id server_id,
366            rtems_id            task_id
367        );
368
369DIRECTIVE STATUS CODES:
370    .. list-table::
371     :class: rtems-table
372
373     * - ``RTEMS_CBS_OK``
374       - successfully attached
375     * - ``RTEMS_CBS_ERROR_FULL``
376       - server maximum tasks exceeded
377     * - ``RTEMS_CBS_ERROR_INVALID_PARAMETER``
378       - invalid input argument
379     * - ``RTEMS_CBS_ERROR_NOSERVER``
380       - server is not valid
381
382DESCRIPTION:
383    Attaches a task (``task_id``) to a server (``server_id``).  The server has
384    to be previously created. Now, the task starts to be scheduled according to
385    the server parameters and not using initial priority. This implementation
386    allows only one task per server, if the user tries to bind another task to
387    the same server, ``RTEMS_CBS_ERROR_FULL`` is returned.
388
389NOTES:
390    Tasks attached to servers become preemptible.
391
392.. raw:: latex
393
394   \clearpage
395
396.. index:: detach a thread from server
397.. index:: rtems_cbs_detach_thread()
398
399.. _rtems_cbs_detach_thread:
400
401CBS_DETACH_THREAD - Detach a thread from server
402-----------------------------------------------
403
404CALLING SEQUENCE:
405    .. code-block:: c
406
407        int rtems_cbs_detach_thread (
408            rtems_cbs_server_id server_id,
409            rtems_id            task_id
410        );
411
412DIRECTIVE STATUS CODES:
413    .. list-table::
414     :class: rtems-table
415
416     * - ``RTEMS_CBS_OK``
417       - successfully detached
418     * - ``RTEMS_CBS_ERROR_INVALID_PARAMETER``
419       - invalid input argument
420     * - ``RTEMS_CBS_ERROR_NOSERVER``
421       - server is not valid
422
423DESCRIPTION:
424    This directive detaches a thread from server. The task continues its
425    execution with initial priority.
426
427NOTES:
428    The server can be reused for any other task.
429
430.. raw:: latex
431
432   \clearpage
433
434.. index:: destroy a bandwidth server
435.. index:: rtems_cbs_destroy_server()
436
437.. _rtems_cbs_destroy_server:
438
439CBS_DESTROY_SERVER - Destroy a bandwidth server
440-----------------------------------------------
441
442CALLING SEQUENCE:
443    .. code-block:: c
444
445        int rtems_cbs_destroy_server (
446            rtems_cbs_server_id server_id
447        );
448
449DIRECTIVE STATUS CODES:
450    .. list-table::
451     :class: rtems-table
452
453     * - ``RTEMS_CBS_OK``
454       - successfully destroyed
455     * - ``RTEMS_CBS_ERROR_INVALID_PARAMETER``
456       - invalid input argument
457     * - ``RTEMS_CBS_ERROR_NOSERVER``
458       - server is not valid
459
460DESCRIPTION:
461    This directive destroys a server. If any task was attached to the server,
462    the task is detached and continues its execution according to EDF rules
463    with initial properties.
464
465NOTES:
466    This again enables one more task to be created.
467
468.. raw:: latex
469
470   \clearpage
471
472.. index:: get an ID of a server
473.. index:: rtems_cbs_get_server_id()
474
475.. _rtems_cbs_get_server_id:
476
477CBS_GET_SERVER_ID - Get an ID of a server
478-----------------------------------------
479
480CALLING SEQUENCE:
481    .. code-block:: c
482
483        int rtems_cbs_get_server_id (
484            rtems_id             task_id,
485            rtems_cbs_server_id *server_id
486        );
487
488DIRECTIVE STATUS CODES:
489    .. list-table::
490     :class: rtems-table
491
492     * - ``RTEMS_CBS_OK``
493       - successful
494     * - ``RTEMS_CBS_ERROR_NOSERVER``
495       - server is not valid
496
497DESCRIPTION:
498    This directive returns an id of server belonging to a given task.
499
500.. raw:: latex
501
502   \clearpage
503
504.. index:: get scheduling parameters of a server
505.. index:: rtems_cbs_get_parameters()
506
507.. _rtems_cbs_get_parameters:
508
509CBS_GET_PARAMETERS - Get scheduling parameters of a server
510----------------------------------------------------------
511
512CALLING SEQUENCE:
513    .. code-block:: c
514
515        rtems_cbs_get_parameters (
516            rtems_cbs_server_id   server_id,
517            rtems_cbs_parameters *params
518        );
519
520DIRECTIVE STATUS CODES:
521    .. list-table::
522     :class: rtems-table
523
524     * - ``RTEMS_CBS_OK``
525       - successful
526     * - ``RTEMS_CBS_ERROR_INVALID_PARAMETER``
527       - invalid input argument
528     * - ``RTEMS_CBS_ERROR_NOSERVER``
529       - server is not valid
530
531DESCRIPTION:
532    This directive returns a structure with current scheduling parameters of a
533    given server (period and execution time).
534
535NOTES:
536    It makes no difference if any task is assigned or not.
537
538.. raw:: latex
539
540   \clearpage
541
542.. index:: set scheduling parameters
543.. index:: rtems_cbs_set_parameters()
544
545.. _rtems_cbs_set_parameters:
546
547CBS_SET_PARAMETERS - Set scheduling parameters
548----------------------------------------------
549
550CALLING SEQUENCE:
551    .. code-block:: c
552
553        int rtems_cbs_set_parameters (
554            rtems_cbs_server_id   server_id,
555            rtems_cbs_parameters *params
556        );
557
558DIRECTIVE STATUS CODES:
559    .. list-table::
560     :class: rtems-table
561
562     * - ``RTEMS_CBS_OK``
563       - successful
564     * - ``RTEMS_CBS_ERROR_INVALID_PARAMETER``
565       - invalid input argument
566     * - ``RTEMS_CBS_ERROR_NOSERVER``
567       - server is not valid
568
569DESCRIPTION:
570    This directive sets new scheduling parameters to the server. This operation
571    can be performed regardless of whether a task is assigned or not.  If a
572    task is assigned, the parameters become effective imediately, therefore it
573    is recommended to apply the change between two subsequent periods.
574
575NOTES:
576    There is an upper limit on both period and budget equal to (2^31)-1 ticks.
577
578.. raw:: latex
579
580   \clearpage
581
582.. index:: get elapsed execution time
583.. index:: rtems_cbs_get_execution_time()
584
585.. _rtems_cbs_get_execution_time:
586
587CBS_GET_EXECUTION_TIME - Get elapsed execution time
588---------------------------------------------------
589
590CALLING SEQUENCE:
591    .. code-block:: c
592
593        int rtems_cbs_get_execution_time (
594            rtems_cbs_server_id    server_id,
595            time_t                *exec_time,
596            time_t                *abs_time
597        );
598
599DIRECTIVE STATUS CODES:
600    .. list-table::
601     :class: rtems-table
602
603     * - ``RTEMS_CBS_OK``
604       - successful
605     * - ``RTEMS_CBS_ERROR_INVALID_PARAMETER``
606       - invalid input argument
607     * - ``RTEMS_CBS_ERROR_NOSERVER``
608       - server is not valid
609
610DESCRIPTION:
611    This routine returns consumed execution time (``exec_time``) of a server
612    during the current period.
613
614NOTES:
615    Absolute time (``abs_time``) not supported now.
616
617.. raw:: latex
618
619   \clearpage
620
621.. index:: get remaining execution time
622.. index:: rtems_cbs_get_remaining_budget()
623
624.. _rtems_cbs_get_remaining_budget:
625
626CBS_GET_REMAINING_BUDGET - Get remaining execution time
627-------------------------------------------------------
628
629CALLING SEQUENCE:
630    .. code-block:: c
631
632        int rtems_cbs_get_remaining_budget (
633            rtems_cbs_server_id  server_id,
634            time_t              *remaining_budget
635        );
636
637DIRECTIVE STATUS CODES:
638    .. list-table::
639     :class: rtems-table
640
641     * - ``RTEMS_CBS_OK``
642       - successful
643     * - ``RTEMS_CBS_ERROR_INVALID_PARAMETER``
644       - invalid input argument
645     * - ``RTEMS_CBS_ERROR_NOSERVER``
646       - server is not valid
647
648DESCRIPTION:
649    This directive returns remaining execution time of a given server for
650    current period.
651
652NOTES:
653    If the execution time approaches zero, the assigned task should finish
654    computations of the current period.
655
656.. raw:: latex
657
658   \clearpage
659
660.. index:: get scheduler approved execution time
661.. index:: rtems_cbs_get_approved_budget()
662
663.. _rtems_cbs_get_approved_budget:
664
665CBS_GET_APPROVED_BUDGET - Get scheduler approved execution time
666---------------------------------------------------------------
667
668CALLING SEQUENCE:
669    .. code-block:: c
670
671        int rtems_cbs_get_approved_budget (
672            rtems_cbs_server_id  server_id,
673            time_t              *appr_budget
674        );
675
676DIRECTIVE STATUS CODES:
677    .. list-table::
678     :class: rtems-table
679
680     * - ``RTEMS_CBS_OK``
681       - successful
682     * - ``RTEMS_CBS_ERROR_INVALID_PARAMETER``
683       - invalid input argument
684     * - ``RTEMS_CBS_ERROR_NOSERVER``
685       - server is not valid
686
687DESCRIPTION:
688    This directive returns server's approved budget for subsequent periods.
Note: See TracBrowser for help on using the repository browser.