source: rtems/doc/user/cbs.t @ bce41f4

4.115
Last change on this file since bce41f4 was bce41f4, checked in by Petr Benes <petben@…>, on 04/15/12 at 17:54:10

PR 1912: Add Scheduler Documentation

Add new file for CBS documentation.

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