Rev 29 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pj | 1 | /* |
2 | * Project: S.Ha.R.K. |
||
3 | * |
||
4 | * Coordinators: |
||
5 | * Giorgio Buttazzo <giorgio@sssup.it> |
||
6 | * Paolo Gai <pj@gandalf.sssup.it> |
||
7 | * |
||
8 | * Authors : |
||
9 | * Paolo Gai <pj@gandalf.sssup.it> |
||
10 | * Massimiliano Giorgi <massy@gandalf.sssup.it> |
||
11 | * Luca Abeni <luca@gandalf.sssup.it> |
||
12 | * (see the web pages for full authors list) |
||
13 | * |
||
14 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
15 | * |
||
16 | * http://www.sssup.it |
||
17 | * http://retis.sssup.it |
||
18 | * http://shark.sssup.it |
||
19 | */ |
||
20 | |||
21 | /** |
||
22 | ------------ |
||
38 | pj | 23 | CVS : $Id: rm.c,v 1.4 2003-01-07 17:07:50 pj Exp $ |
2 | pj | 24 | |
25 | File: $File$ |
||
38 | pj | 26 | Revision: $Revision: 1.4 $ |
27 | Last update: $Date: 2003-01-07 17:07:50 $ |
||
2 | pj | 28 | ------------ |
29 | |||
30 | This file contains the scheduling module RM (Rate Monotonic) |
||
31 | |||
32 | Read rm.h for further details. |
||
33 | |||
34 | This file is equal to EDF.c except for: |
||
35 | |||
36 | . EDF changed to RM :-) |
||
37 | . q_timespec_insert changed to q_insert |
||
38 | . proc_table[p].priority is also modified when we modify lev->period[p] |
||
39 | |||
40 | |||
41 | **/ |
||
42 | |||
43 | /* |
||
38 | pj | 44 | * Copyright (C) 2000,2002 Paolo Gai |
2 | pj | 45 | * |
46 | * This program is free software; you can redistribute it and/or modify |
||
47 | * it under the terms of the GNU General Public License as published by |
||
48 | * the Free Software Foundation; either version 2 of the License, or |
||
49 | * (at your option) any later version. |
||
50 | * |
||
51 | * This program is distributed in the hope that it will be useful, |
||
52 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
53 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
54 | * GNU General Public License for more details. |
||
55 | * |
||
56 | * You should have received a copy of the GNU General Public License |
||
57 | * along with this program; if not, write to the Free Software |
||
58 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
59 | * |
||
60 | */ |
||
61 | |||
62 | |||
63 | #include <modules/rm.h> |
||
64 | #include <ll/stdio.h> |
||
65 | #include <ll/string.h> |
||
66 | #include <kernel/model.h> |
||
67 | #include <kernel/descr.h> |
||
68 | #include <kernel/var.h> |
||
69 | #include <kernel/func.h> |
||
70 | #include <kernel/trace.h> |
||
71 | |||
72 | /*+ Status used in the level +*/ |
||
73 | #define RM_READY MODULE_STATUS_BASE /*+ - Ready status +*/ |
||
74 | #define RM_WCET_VIOLATED MODULE_STATUS_BASE+2 /*+ when wcet is finished +*/ |
||
75 | #define RM_WAIT MODULE_STATUS_BASE+3 /*+ to wait the deadline +*/ |
||
76 | #define RM_IDLE MODULE_STATUS_BASE+4 /*+ to wait the deadline +*/ |
||
77 | #define RM_ZOMBIE MODULE_STATUS_BASE+5 /*+ to wait the free time +*/ |
||
78 | |||
79 | /*+ flags +*/ |
||
80 | #define RM_FLAG_SPORADIC 1 |
||
81 | #define RM_FLAG_NORAISEEXC 2 |
||
82 | |||
83 | /*+ the level redefinition for the Rate Monotonic +*/ |
||
84 | typedef struct { |
||
85 | level_des l; /*+ the standard level descriptor +*/ |
||
86 | |||
87 | TIME period[MAX_PROC]; /*+ The task periods; the deadlines are |
||
88 | stored in the priority field +*/ |
||
89 | int deadline_timer[MAX_PROC]; |
||
90 | /*+ The task deadline timers +*/ |
||
91 | |||
92 | int flag[MAX_PROC]; |
||
93 | /*+ used to manage the JOB_TASK_MODEL and the |
||
94 | periodicity +*/ |
||
95 | |||
29 | pj | 96 | IQUEUE ready; /*+ the ready queue +*/ |
2 | pj | 97 | |
98 | int flags; /*+ the init flags... +*/ |
||
99 | |||
100 | bandwidth_t U; /*+ the used bandwidth +*/ |
||
101 | |||
102 | } RM_level_des; |
||
103 | |||
104 | |||
105 | static void RM_timer_deadline(void *par) |
||
106 | { |
||
107 | PID p = (PID) par; |
||
108 | RM_level_des *lev; |
||
29 | pj | 109 | struct timespec *temp; |
2 | pj | 110 | |
111 | lev = (RM_level_des *)level_table[proc_table[p].task_level]; |
||
112 | |||
113 | switch (proc_table[p].status) { |
||
114 | case RM_ZOMBIE: |
||
115 | /* we finally put the task in the ready queue */ |
||
116 | proc_table[p].status = FREE; |
||
29 | pj | 117 | iq_insertfirst(p,&freedesc); |
2 | pj | 118 | /* and free the allocated bandwidth */ |
119 | lev->U -= (MAX_BANDWIDTH/lev->period[p]) * proc_table[p].wcet; |
||
120 | break; |
||
121 | |||
122 | case RM_IDLE: |
||
123 | /* tracer stuff */ |
||
124 | trc_logevent(TRC_INTACTIVATION,&p); |
||
125 | /* similar to RM_task_activate */ |
||
29 | pj | 126 | temp = iq_query_timespec(p, &lev->ready); |
127 | ADDUSEC2TIMESPEC(lev->period[p], temp); |
||
2 | pj | 128 | proc_table[p].status = RM_READY; |
29 | pj | 129 | iq_priority_insert(p,&lev->ready); |
130 | lev->deadline_timer[p] = kern_event_post(temp, |
||
2 | pj | 131 | RM_timer_deadline, |
132 | (void *)p); |
||
133 | //printk("(d%d idle priority set to %d)",p,proc_table[p].priority ); |
||
134 | event_need_reschedule(); |
||
135 | printk("el%d|",p); |
||
136 | break; |
||
137 | |||
138 | case RM_WAIT: |
||
139 | /* Without this, the task cannot be reactivated!!! */ |
||
140 | proc_table[p].status = SLEEP; |
||
141 | break; |
||
142 | |||
143 | default: |
||
144 | /* else, a deadline miss occurred!!! */ |
||
145 | kern_printf("timer_deadline:AAARRRGGGHHH!!!"); |
||
146 | kern_raise(XDEADLINE_MISS,p); |
||
147 | } |
||
148 | } |
||
149 | |||
150 | static void RM_timer_guest_deadline(void *par) |
||
151 | { |
||
152 | PID p = (PID) par; |
||
153 | |||
154 | kern_printf("AAARRRGGGHHH!!!"); |
||
155 | kern_raise(XDEADLINE_MISS,p); |
||
156 | } |
||
157 | |||
158 | /* The scheduler only gets the first task in the queue */ |
||
38 | pj | 159 | static PID RM_public_scheduler(LEVEL l) |
2 | pj | 160 | { |
161 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
162 | |||
29 | pj | 163 | return iq_query_first(&lev->ready); |
2 | pj | 164 | } |
165 | |||
166 | /* The on-line guarantee is enabled only if the appropriate flag is set... */ |
||
38 | pj | 167 | static int RM_public_guarantee(LEVEL l, bandwidth_t *freebandwidth) |
2 | pj | 168 | { |
169 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
170 | |||
171 | if (lev->flags & RM_FAILED_GUARANTEE) { |
||
172 | *freebandwidth = 0; |
||
173 | return 0; |
||
174 | } |
||
175 | else |
||
176 | if (*freebandwidth >= lev->U) { |
||
177 | *freebandwidth -= lev->U; |
||
178 | return 1; |
||
179 | } |
||
180 | else |
||
181 | return 0; |
||
182 | |||
183 | } |
||
184 | |||
38 | pj | 185 | static int RM_public_create(LEVEL l, PID p, TASK_MODEL *m) |
2 | pj | 186 | { |
187 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
188 | |||
38 | pj | 189 | HARD_TASK_MODEL *h; |
2 | pj | 190 | |
38 | pj | 191 | if (m->pclass != HARD_PCLASS) return -1; |
192 | if (m->level != 0 && m->level != l) return -1; |
||
193 | h = (HARD_TASK_MODEL *)m; |
||
194 | if (!h->wcet || !h->mit) return -1; |
||
195 | /* now we know that m is a valid model */ |
||
2 | pj | 196 | |
29 | pj | 197 | *iq_query_priority(p, &lev->ready) = lev->period[p] = h->mit; |
2 | pj | 198 | |
199 | if (h->periodicity == APERIODIC) |
||
200 | lev->flag[p] = RM_FLAG_SPORADIC; |
||
201 | else |
||
202 | lev->flag[p] = 0; |
||
203 | lev->deadline_timer[p] = -1; |
||
204 | |||
205 | /* Enable wcet check */ |
||
206 | if (lev->flags & RM_ENABLE_WCET_CHECK) { |
||
207 | proc_table[p].avail_time = h->wcet; |
||
208 | proc_table[p].wcet = h->wcet; |
||
209 | proc_table[p].control |= CONTROL_CAP; |
||
210 | } |
||
211 | |||
212 | /* update the bandwidth... */ |
||
213 | if (lev->flags & RM_ENABLE_GUARANTEE) { |
||
214 | bandwidth_t b; |
||
215 | b = (MAX_BANDWIDTH / h->mit) * h->wcet; |
||
216 | |||
217 | /* really update lev->U, checking an overflow... */ |
||
218 | if (MAX_BANDWIDTH - lev->U > b) |
||
219 | lev->U += b; |
||
220 | else |
||
221 | /* The task can NOT be guaranteed (U>MAX_BANDWIDTH)... |
||
222 | in this case, we don't raise an exception... in fact, after the |
||
223 | RM_task_create the task_create will call level_guarantee that return |
||
224 | -1... return -1 in RM_task_create isn't correct, because: |
||
225 | . generally, the guarantee must be done when also the resources |
||
226 | are registered |
||
227 | . returning -1 will cause the task_create to return with an errno |
||
228 | ETASK_CREATE instead of ENO_GUARANTEE!!! |
||
229 | |||
230 | Why I use the flag??? because if the lev->U overflows, if i.e. I set |
||
231 | it to MAX_BANDWIDTH, I lose the correct allocated bandwidth... |
||
232 | */ |
||
233 | lev->flags |= RM_FAILED_GUARANTEE; |
||
234 | } |
||
235 | |||
236 | return 0; /* OK, also if the task cannot be guaranteed... */ |
||
237 | } |
||
238 | |||
38 | pj | 239 | static void RM_public_detach(LEVEL l, PID p) |
2 | pj | 240 | { |
241 | /* the RM level doesn't introduce any dinamic allocated new field. |
||
242 | we have only to reset the NO_GUARANTEE FIELD and decrement the allocated |
||
243 | bandwidth */ |
||
244 | |||
245 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
246 | |||
247 | if (lev->flags & RM_FAILED_GUARANTEE) |
||
248 | lev->flags &= ~RM_FAILED_GUARANTEE; |
||
249 | else |
||
250 | lev->U -= (MAX_BANDWIDTH / lev->period[p]) * proc_table[p].wcet; |
||
251 | } |
||
252 | |||
38 | pj | 253 | static void RM_public_dispatch(LEVEL l, PID p, int nostop) |
2 | pj | 254 | { |
255 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
256 | |||
257 | // kern_printf("(disp %d)",p); |
||
258 | |||
259 | /* the task state is set EXE by the scheduler() |
||
260 | we extract the task from the ready queue |
||
261 | NB: we can't assume that p is the first task in the queue!!! */ |
||
29 | pj | 262 | iq_extract(p, &lev->ready); |
2 | pj | 263 | } |
264 | |||
38 | pj | 265 | static void RM_public_epilogue(LEVEL l, PID p) |
2 | pj | 266 | { |
267 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
268 | |||
269 | // kern_printf("(epil %d)",p); |
||
270 | |||
271 | /* check if the wcet is finished... */ |
||
272 | if ((lev->flags & RM_ENABLE_WCET_CHECK) && proc_table[p].avail_time <= 0) { |
||
273 | /* if it is, raise a XWCET_VIOLATION exception */ |
||
274 | kern_raise(XWCET_VIOLATION,p); |
||
275 | proc_table[p].status = RM_WCET_VIOLATED; |
||
276 | } |
||
277 | else { |
||
278 | /* the task has been preempted. it returns into the ready queue... */ |
||
29 | pj | 279 | iq_priority_insert(p,&lev->ready); |
2 | pj | 280 | proc_table[p].status = RM_READY; |
281 | } |
||
282 | } |
||
283 | |||
38 | pj | 284 | static void RM_public_activate(LEVEL l, PID p) |
2 | pj | 285 | { |
286 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
29 | pj | 287 | struct timespec *temp; |
2 | pj | 288 | |
289 | if (proc_table[p].status == RM_WAIT) { |
||
290 | kern_raise(XACTIVATION,p); |
||
291 | return; |
||
292 | } |
||
293 | |||
294 | /* Test if we are trying to activate a non sleeping task */ |
||
295 | /* Ignore this; the task is already active */ |
||
296 | if (proc_table[p].status != SLEEP && |
||
297 | proc_table[p].status != RM_WCET_VIOLATED) |
||
298 | return; |
||
299 | |||
300 | |||
301 | /* see also RM_timer_deadline */ |
||
29 | pj | 302 | temp = iq_query_timespec(p, &lev->ready); |
38 | pj | 303 | kern_gettime(temp); |
29 | pj | 304 | ADDUSEC2TIMESPEC(lev->period[p], temp); |
2 | pj | 305 | |
306 | /* Insert task in the correct position */ |
||
307 | proc_table[p].status = RM_READY; |
||
29 | pj | 308 | iq_priority_insert(p,&lev->ready); |
2 | pj | 309 | |
310 | /* Set the deadline timer */ |
||
29 | pj | 311 | lev->deadline_timer[p] = kern_event_post(temp, |
2 | pj | 312 | RM_timer_deadline, |
313 | (void *)p); |
||
314 | } |
||
315 | |||
38 | pj | 316 | static void RM_public_unblock(LEVEL l, PID p) |
2 | pj | 317 | { |
318 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
319 | |||
38 | pj | 320 | /* Similar to RM_task_activate, |
321 | but we don't check in what state the task is */ |
||
2 | pj | 322 | |
323 | /* Insert task in the correct position */ |
||
324 | proc_table[p].status = RM_READY; |
||
29 | pj | 325 | iq_priority_insert(p,&lev->ready); |
2 | pj | 326 | } |
327 | |||
38 | pj | 328 | static void RM_public_block(LEVEL l, PID p) |
2 | pj | 329 | { |
330 | /* Extract the running task from the level |
||
331 | . we have already extract it from the ready queue at the dispatch time. |
||
332 | . the capacity event have to be removed by the generic kernel |
||
333 | . the wcet don't need modification... |
||
334 | . the state of the task is set by the calling function |
||
335 | . the deadline must remain... |
||
336 | |||
337 | So, we do nothing!!! |
||
338 | */ |
||
339 | } |
||
340 | |||
38 | pj | 341 | static int RM_public_message(LEVEL l, PID p, void *m) |
2 | pj | 342 | { |
343 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
344 | |||
345 | /* the task has terminated his job before it consume the wcet. All OK! */ |
||
346 | if (lev->flag[p] & RM_FLAG_SPORADIC) |
||
347 | proc_table[p].status = RM_WAIT; |
||
348 | else /* pclass = sporadic_pclass */ |
||
349 | proc_table[p].status = RM_IDLE; |
||
350 | |||
351 | /* we reset the capacity counters... */ |
||
352 | if (lev->flags & RM_ENABLE_WCET_CHECK) |
||
353 | proc_table[p].avail_time = proc_table[p].wcet; |
||
354 | |||
38 | pj | 355 | jet_update_endcycle(); /* Update the Jet data... */ |
356 | trc_logevent(TRC_ENDCYCLE,&exec_shadow); /* tracer stuff */ |
||
357 | |||
2 | pj | 358 | /* when the deadline timer fire, it recognize the situation and set |
38 | pj | 359 | correctly all the stuffs (like reactivation, sleep, etc... ) */ |
360 | |||
361 | return 0; |
||
2 | pj | 362 | } |
363 | |||
38 | pj | 364 | static void RM_public_end(LEVEL l, PID p) |
2 | pj | 365 | { |
366 | proc_table[p].status = RM_ZOMBIE; |
||
367 | |||
368 | /* When the deadline timer fire, it put the task descriptor in |
||
369 | the free queue, and free the allocated bandwidth... */ |
||
370 | } |
||
371 | |||
38 | pj | 372 | static void RM_private_insert(LEVEL l, PID p, TASK_MODEL *m) |
2 | pj | 373 | { |
374 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
38 | pj | 375 | JOB_TASK_MODEL *job; |
2 | pj | 376 | |
38 | pj | 377 | if (m->pclass != JOB_PCLASS || (m->level != 0 && m->level != l) ) { |
378 | kern_raise(XINVALID_TASK, p); |
||
379 | return; |
||
380 | } |
||
2 | pj | 381 | |
38 | pj | 382 | job = (JOB_TASK_MODEL *)m; |
2 | pj | 383 | |
29 | pj | 384 | *iq_query_timespec(p,&lev->ready) = job->deadline; |
38 | pj | 385 | *iq_query_priority(p, &lev->ready) = lev->period[p] = job->period; |
2 | pj | 386 | |
387 | lev->deadline_timer[p] = -1; |
||
388 | |||
38 | pj | 389 | /* Insert task in the correct position */ |
390 | iq_priority_insert(p,&lev->ready); |
||
391 | proc_table[p].status = RM_READY; |
||
392 | |||
2 | pj | 393 | if (job->noraiseexc) |
394 | lev->flag[p] = RM_FLAG_NORAISEEXC; |
||
38 | pj | 395 | else { |
2 | pj | 396 | lev->flag[p] = 0; |
38 | pj | 397 | lev->deadline_timer[p] = kern_event_post(iq_query_timespec(p, &lev->ready), |
398 | RM_timer_guest_deadline, |
||
399 | (void *)p); |
||
400 | } |
||
2 | pj | 401 | } |
402 | |||
38 | pj | 403 | static void RM_private_dispatch(LEVEL l, PID p, int nostop) |
2 | pj | 404 | { |
405 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
406 | |||
407 | /* the task state is set to EXE by the scheduler() |
||
408 | we extract the task from the ready queue |
||
409 | NB: we can't assume that p is the first task in the queue!!! */ |
||
29 | pj | 410 | iq_extract(p, &lev->ready); |
2 | pj | 411 | } |
412 | |||
38 | pj | 413 | static void RM_private_epilogue(LEVEL l, PID p) |
2 | pj | 414 | { |
415 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
416 | |||
417 | /* the task has been preempted. it returns into the ready queue... */ |
||
29 | pj | 418 | iq_priority_insert(p,&lev->ready); |
2 | pj | 419 | proc_table[p].status = RM_READY; |
420 | } |
||
421 | |||
38 | pj | 422 | static void RM_private_extract(LEVEL l, PID p) |
2 | pj | 423 | { |
424 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
425 | |||
426 | //kern_printf("RM_guest_end: dline timer %d\n",lev->deadline_timer[p]); |
||
427 | if (proc_table[p].status == RM_READY) |
||
428 | { |
||
29 | pj | 429 | iq_extract(p, &lev->ready); |
2 | pj | 430 | //kern_printf("(g_end rdy extr)"); |
431 | } |
||
432 | |||
433 | /* we remove the deadline timer, because the slice is finished */ |
||
434 | if (lev->deadline_timer[p] != NIL) { |
||
435 | // kern_printf("RM_guest_end: dline timer %d\n",lev->deadline_timer[p]); |
||
38 | pj | 436 | kern_event_delete(lev->deadline_timer[p]); |
2 | pj | 437 | lev->deadline_timer[p] = NIL; |
438 | } |
||
439 | |||
440 | } |
||
441 | |||
442 | /* Registration functions */ |
||
443 | |||
444 | /*+ Registration function: |
||
445 | int flags the init flags ... see rm.h +*/ |
||
38 | pj | 446 | LEVEL RM_register_level(int flags) |
2 | pj | 447 | { |
448 | LEVEL l; /* the level that we register */ |
||
449 | RM_level_des *lev; /* for readableness only */ |
||
450 | PID i; /* a counter */ |
||
451 | |||
452 | printk("RM_register_level\n"); |
||
453 | |||
454 | /* request an entry in the level_table */ |
||
38 | pj | 455 | l = level_alloc_descriptor(sizeof(RM_level_des)); |
2 | pj | 456 | |
38 | pj | 457 | lev = (RM_level_des *)level_table[l]; |
2 | pj | 458 | |
459 | printk(" lev=%d\n",(int)lev); |
||
460 | |||
461 | /* fill the standard descriptor */ |
||
38 | pj | 462 | lev->l.private_insert = RM_private_insert; |
463 | lev->l.private_extract = RM_private_extract; |
||
464 | lev->l.private_dispatch = RM_private_dispatch; |
||
465 | lev->l.private_epilogue = RM_private_epilogue; |
||
2 | pj | 466 | |
38 | pj | 467 | lev->l.public_scheduler = RM_public_scheduler; |
2 | pj | 468 | if (flags & RM_ENABLE_GUARANTEE) |
38 | pj | 469 | lev->l.public_guarantee = RM_public_guarantee; |
2 | pj | 470 | else |
38 | pj | 471 | lev->l.public_guarantee = NULL; |
2 | pj | 472 | |
38 | pj | 473 | lev->l.public_create = RM_public_create; |
474 | lev->l.public_detach = RM_public_detach; |
||
475 | lev->l.public_end = RM_public_end; |
||
476 | lev->l.public_dispatch = RM_public_dispatch; |
||
477 | lev->l.public_epilogue = RM_public_epilogue; |
||
478 | lev->l.public_activate = RM_public_activate; |
||
479 | lev->l.public_unblock = RM_public_unblock; |
||
480 | lev->l.public_block = RM_public_block; |
||
481 | lev->l.public_message = RM_public_message; |
||
2 | pj | 482 | |
483 | /* fill the RM descriptor part */ |
||
484 | for(i=0; i<MAX_PROC; i++) { |
||
485 | lev->period[i] = 0; |
||
486 | lev->deadline_timer[i] = -1; |
||
487 | lev->flag[i] = 0; |
||
488 | } |
||
489 | |||
29 | pj | 490 | iq_init(&lev->ready, &freedesc, 0); |
2 | pj | 491 | lev->flags = flags & 0x07; |
492 | lev->U = 0; |
||
38 | pj | 493 | |
494 | return l; |
||
2 | pj | 495 | } |
496 | |||
497 | bandwidth_t RM_usedbandwidth(LEVEL l) |
||
498 | { |
||
499 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
38 | pj | 500 | |
501 | return lev->U; |
||
2 | pj | 502 | } |
503 |