source: rtems-docs/c_user/constant_bandwidth_server.rst @ fd6dc8c8

4.115
Last change on this file since fd6dc8c8 was fd6dc8c8, checked in by Amar Takhar <amar@…>, on 01/18/16 at 00:19:43

Split document into seperate files by section.

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