source: rtems-docs/c_user/constant_bandwidth_server.rst @ 01a36ee

4.115
Last change on this file since 01a36ee was 36def91, checked in by Joel Sherrill <joel@…>, on 10/28/16 at 00:47:07

rtems-docs: Fix many unnecessary back slashes

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