source: rtems-docs/ada_user/constant_bandwidth_server_scheduler_api.rst @ 4783b0d

4.115
Last change on this file since 4783b0d was 4783b0d, checked in by Amar Takhar <amar@…>, on 01/17/16 at 16:37:28

Split document into seperate files by section.

  • Property mode set to 100644
File size: 15.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:
106
107Limitations
108-----------
109.. index:: CBS limitations
110
111When using this scheduler you have to keep in mind several things:
112
113- it_limitations
114
115- In the current implementation it is possible to attach only
116  a single task to each server.
117
118- If you have a task attached to a server and you voluntatily
119  block it in the beginning of its execution, its priority will be
120  probably pulled to background upon unblock, thus not guaranteed
121  deadline any more. This is because you are effectively raising
122  computation time of the task. When unbocking, you should be always
123  sure that the ratio between remaining computation time and remaining
124  deadline is not higher that the utilization you have agreed with the
125  scheduler.
126
127Operations
128==========
129
130Setting up a server
131-------------------
132
133The directive ``rtems_cbs_create_server`` is used to create a new
134server that is characterized by ``rtems_cbs_parameters``. You also
135might want to register the ``rtems_cbs_budget_overrun`` callback
136routine. After this step tasks can be attached to the server. The directive``rtems_cbs_set_parameters`` can change the scheduling parameters
137to avoid destroying and creating a new server again.
138
139Attaching Task to a Server
140--------------------------
141
142If a task is attached to a server using ``rtems_cbs_attach_thread``,
143the task’s computation time per period is limited by the server and
144the deadline (period) of task is equal to deadline of the server which
145means if you conclude a period using ``rate_monotonic_period``,
146the length of next period is always determined by the server’s property.
147
148The task has a guaranteed bandwidth given by the server but should not
149exceed it, otherwise the priority is pulled to background until the
150start of next period and the ``rtems_cbs_budget_overrun`` callback
151function is invoked.
152
153When attaching a task to server, the preemptability flag of the task
154is raised, otherwise it would not be possible to control the execution
155of the task.
156
157Detaching Task from a Server
158----------------------------
159
160The directive ``rtems_cbs_detach_thread`` is just an inverse
161operation to the previous one, the task continues its execution with
162the initial priority.
163
164Preemptability of the task is restored to the initial value.
165
166Examples
167--------
168
169The following example presents a simple common use of the API.
170
171You can see the initialization and cleanup call here, if there are
172multiple tasks in the system, it is obvious that the initialization
173should be called before creating the task.
174
175Notice also that in this case we decided to register an overrun handler,
176instead of which there could be ``NULL``. This handler just prints
177a message to terminal, what else may be done here depends on a specific
178application.
179
180During the periodic execution, remaining budget should be watched
181to avoid overrun.
182.. code:: c
183
184    void overrun_handler (
185    rtems_cbs_server_id server_id
186    )
187    {
188    printk( "Budget overrun, fixing the task\\n" );
189    return;
190    }
191    rtems_task Tasks_Periodic(
192    rtems_task_argument argument
193    )
194    {
195    rtems_id          rmid;
196    rtems_cbs_server_id server_id;
197    rtems_cbs_parameters params;
198    params.deadline = 10;
199    params.budget = 4;
200    rtems_cbs_initialize();
201    rtems_cbs_create_server( &params, &overrun_handler, &server_id )
202    rtems_cbs_attach_thread( server_id, SELF );
203    rtems_rate_monotonic_create( argument, &rmid );
204    while ( 1 ) {
205    if (rtems_rate_monotonic_period(rmid, params.deadline)==RTEMS_TIMEOUT)
206    break;
207    /* Perform some periodic action \*/
208    }
209    rtems_rate_monotonic_delete( rmid );
210    rtems_cbs_cleanup();
211    exit( 1 );
212    }
213
214Directives
215==========
216
217This section details the Constant Bandwidth Server’s directives.
218A subsection is dedicated to each of this manager’s directives
219and describes the calling sequence, related constants, usage,
220and status codes.
221
222CBS_INITIALIZE - Initialize the CBS library
223-------------------------------------------
224.. index:: initialize the CBS library
225
226**CALLING SEQUENCE:**
227
228**DIRECTIVE STATUS CODES:**
229
230``RTEMS.CBS_OK`` - successful initialization
231``RTEMS.CBS_ERROR_NO_MEMORY`` - not enough memory for data
232
233**DESCRIPTION:**
234
235This routine initializes the library in terms of allocating necessary memory
236for the servers. In case not enough memory is available in the system,``RTEMS.CBS_ERROR_NO_MEMORY`` is returned, otherwise``RTEMS.CBS_OK``.
237
238**NOTES:**
239
240Additional memory per each server is allocated upon invocation of``rtems_cbs_create_server``.
241
242Tasks in the system are not influenced, they still keep executing
243with their initial parameters.
244
245CBS_CLEANUP - Cleanup the CBS library
246-------------------------------------
247.. index:: cleanup the CBS library
248
249**CALLING SEQUENCE:**
250
251**DIRECTIVE STATUS CODES:**
252
253``RTEMS.CBS_OK`` - always successful
254
255**DESCRIPTION:**
256
257This routine detaches all tasks from their servers, destroys all servers
258and returns memory back to the system.
259
260**NOTES:**
261
262All tasks continue executing with their initial priorities.
263
264CBS_CREATE_SERVER - Create a new bandwidth server
265-------------------------------------------------
266.. index:: create a new bandwidth server
267
268**CALLING SEQUENCE:**
269
270**DIRECTIVE STATUS CODES:**
271
272``RTEMS.CBS_OK`` - successfully created
273``RTEMS.CBS_ERROR_NO_MEMORY`` - not enough memory for data
274``RTEMS.CBS_ERROR_FULL`` - maximum servers exceeded
275``RTEMS.CBS_ERROR_INVALID_PARAMETER`` - invalid input argument
276
277**DESCRIPTION:**
278
279This routine prepares an instance of a constant bandwidth server.
280The input parameter ``rtems_cbs_parameters`` specifies scheduling
281parameters of the server (period and budget). If these are not valid,``RTEMS.CBS_ERROR_INVALID_PARAMETER`` is returned.
282The ``budget_overrun_callback`` is an optional callback function, which is
283invoked in case the server’s budget within one period is exceeded.
284Output parameter ``server_id`` becomes an id of the newly created server.
285If there is not enough memory, the ``RTEMS.CBS_ERROR_NO_MEMORY``
286is returned. If the maximum server count in the system is exceeded,``RTEMS.CBS_ERROR_FULL`` is returned.
287
288**NOTES:**
289
290No task execution is being influenced so far.
291
292CBS_ATTACH_THREAD - Attach a thread to server
293---------------------------------------------
294.. index:: attach a thread to server
295
296**CALLING SEQUENCE:**
297
298**DIRECTIVE STATUS CODES:**
299
300``RTEMS.CBS_OK`` - successfully attached
301``RTEMS.CBS_ERROR_FULL`` - server maximum tasks exceeded
302``RTEMS.CBS_ERROR_INVALID_PARAMETER`` - invalid input argument
303``RTEMS.CBS_ERROR_NOSERVER`` - server is not valid
304
305**DESCRIPTION:**
306
307Attaches a task (``task_id``) to a server (``server_id``).
308The server has to be previously created. Now, the task starts
309to be scheduled according to the server parameters and not
310using initial priority. This implementation allows only one task
311per server, if the user tries to bind another task to the same
312server, ``RTEMS.CBS_ERROR_FULL`` is returned.
313
314**NOTES:**
315
316Tasks attached to servers become preemptible.
317
318CBS_DETACH_THREAD - Detach a thread from server
319-----------------------------------------------
320.. index:: detach a thread from server
321
322**CALLING SEQUENCE:**
323
324**DIRECTIVE STATUS CODES:**
325
326``RTEMS.CBS_OK`` - successfully detached
327``RTEMS.CBS_ERROR_INVALID_PARAMETER`` - invalid input argument
328``RTEMS.CBS_ERROR_NOSERVER`` - server is not valid
329
330**DESCRIPTION:**
331
332This directive detaches a thread from server. The task continues its
333execution with initial priority.
334
335**NOTES:**
336
337The server can be reused for any other task.
338
339CBS_DESTROY_SERVER - Destroy a bandwidth server
340-----------------------------------------------
341.. index:: destroy a bandwidth server
342
343**CALLING SEQUENCE:**
344
345**DIRECTIVE STATUS CODES:**
346
347``RTEMS.CBS_OK`` - successfully destroyed
348``RTEMS.CBS_ERROR_INVALID_PARAMETER`` - invalid input argument
349``RTEMS.CBS_ERROR_NOSERVER`` - server is not valid
350
351**DESCRIPTION:**
352
353This directive destroys a server. If any task was attached to the server,
354the task is detached and continues its execution according to EDF rules
355with initial properties.
356
357**NOTES:**
358
359This again enables one more task to be created.
360
361CBS_GET_SERVER_ID - Get an ID of a server
362-----------------------------------------
363.. index:: get an ID of a server
364
365**CALLING SEQUENCE:**
366
367**DIRECTIVE STATUS CODES:**
368
369``RTEMS.CBS_OK`` - successful
370``RTEMS.CBS_ERROR_NOSERVER`` - server is not valid
371
372**DESCRIPTION:**
373
374This directive returns an id of server belonging to a given task.
375
376CBS_GET_PARAMETERS - Get scheduling parameters of a server
377----------------------------------------------------------
378.. index:: get scheduling parameters of a server
379
380**CALLING SEQUENCE:**
381
382**DIRECTIVE STATUS CODES:**
383
384``RTEMS.CBS_OK`` - successful
385``RTEMS.CBS_ERROR_INVALID_PARAMETER`` - invalid input argument
386``RTEMS.CBS_ERROR_NOSERVER`` - server is not valid
387
388**DESCRIPTION:**
389
390This directive returns a structure with current scheduling parameters
391of a given server (period and execution time).
392
393**NOTES:**
394
395It makes no difference if any task is assigned or not.
396
397CBS_SET_PARAMETERS - Set scheduling parameters
398----------------------------------------------
399.. index:: set scheduling parameters
400
401**CALLING SEQUENCE:**
402
403**DIRECTIVE STATUS CODES:**
404
405``RTEMS.CBS_OK`` - successful
406``RTEMS.CBS_ERROR_INVALID_PARAMETER`` - invalid input argument
407``RTEMS.CBS_ERROR_NOSERVER`` - server is not valid
408
409**DESCRIPTION:**
410
411This directive sets new scheduling parameters to the server. This operation
412can be performed regardless of whether a task is assigned or not.
413If a task is assigned, the parameters become effective imediately, therefore it
414is recommended to apply the change between two subsequent periods.
415
416**NOTES:**
417
418There is an upper limit on both period and budget equal to (2^31)-1 ticks.
419
420CBS_GET_EXECUTION_TIME - Get elapsed execution time
421---------------------------------------------------
422.. index:: get elapsed execution time
423
424**CALLING SEQUENCE:**
425
426**DIRECTIVE STATUS CODES:**
427
428``RTEMS.CBS_OK`` - successful
429``RTEMS.CBS_ERROR_INVALID_PARAMETER`` - invalid input argument
430``RTEMS.CBS_ERROR_NOSERVER`` - server is not valid
431
432**DESCRIPTION:**
433
434This routine returns consumed execution time (``exec_time``) of a server
435during the current period.
436
437**NOTES:**
438
439Absolute time (``abs_time``) not supported now.
440
441CBS_GET_REMAINING_BUDGET - Get remaining execution time
442-------------------------------------------------------
443.. index:: get remaining execution time
444
445**CALLING SEQUENCE:**
446
447**DIRECTIVE STATUS CODES:**
448
449``RTEMS.CBS_OK`` - successful
450``RTEMS.CBS_ERROR_INVALID_PARAMETER`` - invalid input argument
451``RTEMS.CBS_ERROR_NOSERVER`` - server is not valid
452
453**DESCRIPTION:**
454
455This directive returns remaining execution time of a given server for
456current period.
457
458**NOTES:**
459
460If the execution time approaches zero, the assigned task should finish
461computations of the current period.
462
463CBS_GET_APPROVED_BUDGET - Get scheduler approved execution time
464---------------------------------------------------------------
465.. index:: get scheduler approved execution time
466
467**CALLING SEQUENCE:**
468
469**DIRECTIVE STATUS CODES:**
470
471``RTEMS.CBS_OK`` - successful
472``RTEMS.CBS_ERROR_INVALID_PARAMETER`` - invalid input argument
473``RTEMS.CBS_ERROR_NOSERVER`` - server is not valid
474
475**DESCRIPTION:**
476
477This directive returns server’s approved budget for subsequent periods.
478
479.. COMMENT: COPYRIGHT (c) 1989-2011.
480
481.. COMMENT: On-Line Applications Research Corporation (OAR).
482
483.. COMMENT: All rights reserved.
484
Note: See TracBrowser for help on using the repository browser.