Rev 385 | 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 | ------------ |
||
502 | giacomo | 23 | CVS : $Id: rm.c,v 1.8 2004-03-10 14:51:44 giacomo Exp $ |
2 | pj | 24 | |
25 | File: $File$ |
||
502 | giacomo | 26 | Revision: $Revision: 1.8 $ |
27 | Last update: $Date: 2004-03-10 14:51:44 $ |
||
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 | |||
353 | giacomo | 71 | #include <tracer.h> |
72 | |||
2 | pj | 73 | /*+ Status used in the level +*/ |
74 | #define RM_READY MODULE_STATUS_BASE /*+ - Ready status +*/ |
||
75 | #define RM_WCET_VIOLATED MODULE_STATUS_BASE+2 /*+ when wcet is finished +*/ |
||
76 | #define RM_WAIT MODULE_STATUS_BASE+3 /*+ to wait the deadline +*/ |
||
77 | #define RM_IDLE MODULE_STATUS_BASE+4 /*+ to wait the deadline +*/ |
||
78 | #define RM_ZOMBIE MODULE_STATUS_BASE+5 /*+ to wait the free time +*/ |
||
79 | |||
80 | /*+ flags +*/ |
||
81 | #define RM_FLAG_SPORADIC 1 |
||
82 | #define RM_FLAG_NORAISEEXC 2 |
||
83 | |||
84 | /*+ the level redefinition for the Rate Monotonic +*/ |
||
85 | typedef struct { |
||
86 | level_des l; /*+ the standard level descriptor +*/ |
||
87 | |||
88 | TIME period[MAX_PROC]; /*+ The task periods; the deadlines are |
||
89 | stored in the priority field +*/ |
||
90 | int deadline_timer[MAX_PROC]; |
||
91 | /*+ The task deadline timers +*/ |
||
92 | |||
93 | int flag[MAX_PROC]; |
||
94 | /*+ used to manage the JOB_TASK_MODEL and the |
||
95 | periodicity +*/ |
||
96 | |||
29 | pj | 97 | IQUEUE ready; /*+ the ready queue +*/ |
2 | pj | 98 | |
99 | int flags; /*+ the init flags... +*/ |
||
100 | |||
101 | bandwidth_t U; /*+ the used bandwidth +*/ |
||
102 | |||
103 | } RM_level_des; |
||
104 | |||
105 | |||
106 | static void RM_timer_deadline(void *par) |
||
107 | { |
||
108 | PID p = (PID) par; |
||
109 | RM_level_des *lev; |
||
29 | pj | 110 | struct timespec *temp; |
2 | pj | 111 | |
112 | lev = (RM_level_des *)level_table[proc_table[p].task_level]; |
||
113 | |||
114 | switch (proc_table[p].status) { |
||
115 | case RM_ZOMBIE: |
||
116 | /* we finally put the task in the ready queue */ |
||
117 | proc_table[p].status = FREE; |
||
29 | pj | 118 | iq_insertfirst(p,&freedesc); |
2 | pj | 119 | /* and free the allocated bandwidth */ |
120 | lev->U -= (MAX_BANDWIDTH/lev->period[p]) * proc_table[p].wcet; |
||
121 | break; |
||
122 | |||
123 | case RM_IDLE: |
||
124 | /* tracer stuff */ |
||
502 | giacomo | 125 | TRACER_LOGEVENT(FTrace_EVT_task_timer,(unsigned short int)proc_table[p].context,(unsigned int)proc_table[p].task_level); |
2 | pj | 126 | /* similar to RM_task_activate */ |
29 | pj | 127 | temp = iq_query_timespec(p, &lev->ready); |
128 | ADDUSEC2TIMESPEC(lev->period[p], temp); |
||
2 | pj | 129 | proc_table[p].status = RM_READY; |
29 | pj | 130 | iq_priority_insert(p,&lev->ready); |
131 | lev->deadline_timer[p] = kern_event_post(temp, |
||
2 | pj | 132 | RM_timer_deadline, |
133 | (void *)p); |
||
134 | //printk("(d%d idle priority set to %d)",p,proc_table[p].priority ); |
||
135 | event_need_reschedule(); |
||
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 | |||
159 | pj | 171 | if (*freebandwidth >= lev->U) { |
172 | *freebandwidth -= lev->U; |
||
173 | return 1; |
||
2 | pj | 174 | } |
175 | else |
||
159 | pj | 176 | return 0; |
2 | pj | 177 | } |
178 | |||
38 | pj | 179 | static int RM_public_create(LEVEL l, PID p, TASK_MODEL *m) |
2 | pj | 180 | { |
181 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
182 | |||
38 | pj | 183 | HARD_TASK_MODEL *h; |
2 | pj | 184 | |
38 | pj | 185 | if (m->pclass != HARD_PCLASS) return -1; |
186 | if (m->level != 0 && m->level != l) return -1; |
||
187 | h = (HARD_TASK_MODEL *)m; |
||
188 | if (!h->wcet || !h->mit) return -1; |
||
159 | pj | 189 | |
190 | /* update the bandwidth... */ |
||
191 | if (lev->flags & RM_ENABLE_GUARANTEE) { |
||
192 | bandwidth_t b; |
||
193 | b = (MAX_BANDWIDTH / h->mit) * h->wcet; |
||
194 | |||
195 | /* really update lev->U, checking an overflow... */ |
||
196 | if (MAX_BANDWIDTH - lev->U > b) |
||
197 | lev->U += b; |
||
198 | else |
||
199 | return -1; |
||
200 | } |
||
201 | |||
38 | pj | 202 | /* now we know that m is a valid model */ |
2 | pj | 203 | |
29 | pj | 204 | *iq_query_priority(p, &lev->ready) = lev->period[p] = h->mit; |
2 | pj | 205 | |
206 | if (h->periodicity == APERIODIC) |
||
207 | lev->flag[p] = RM_FLAG_SPORADIC; |
||
208 | else |
||
209 | lev->flag[p] = 0; |
||
210 | lev->deadline_timer[p] = -1; |
||
211 | |||
212 | /* Enable wcet check */ |
||
213 | if (lev->flags & RM_ENABLE_WCET_CHECK) { |
||
214 | proc_table[p].avail_time = h->wcet; |
||
215 | proc_table[p].wcet = h->wcet; |
||
216 | proc_table[p].control |= CONTROL_CAP; |
||
217 | } |
||
218 | |||
219 | return 0; /* OK, also if the task cannot be guaranteed... */ |
||
220 | } |
||
221 | |||
38 | pj | 222 | static void RM_public_detach(LEVEL l, PID p) |
2 | pj | 223 | { |
224 | /* the RM level doesn't introduce any dinamic allocated new field. |
||
225 | we have only to reset the NO_GUARANTEE FIELD and decrement the allocated |
||
226 | bandwidth */ |
||
227 | |||
228 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
229 | |||
159 | pj | 230 | if (lev->flags & RM_ENABLE_GUARANTEE) { |
2 | pj | 231 | lev->U -= (MAX_BANDWIDTH / lev->period[p]) * proc_table[p].wcet; |
159 | pj | 232 | } |
2 | pj | 233 | } |
234 | |||
38 | pj | 235 | static void RM_public_dispatch(LEVEL l, PID p, int nostop) |
2 | pj | 236 | { |
237 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
238 | |||
239 | // kern_printf("(disp %d)",p); |
||
240 | |||
241 | /* the task state is set EXE by the scheduler() |
||
242 | we extract the task from the ready queue |
||
243 | NB: we can't assume that p is the first task in the queue!!! */ |
||
29 | pj | 244 | iq_extract(p, &lev->ready); |
2 | pj | 245 | } |
246 | |||
38 | pj | 247 | static void RM_public_epilogue(LEVEL l, PID p) |
2 | pj | 248 | { |
249 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
250 | |||
251 | // kern_printf("(epil %d)",p); |
||
252 | |||
253 | /* check if the wcet is finished... */ |
||
254 | if ((lev->flags & RM_ENABLE_WCET_CHECK) && proc_table[p].avail_time <= 0) { |
||
255 | /* if it is, raise a XWCET_VIOLATION exception */ |
||
256 | kern_raise(XWCET_VIOLATION,p); |
||
257 | proc_table[p].status = RM_WCET_VIOLATED; |
||
258 | } |
||
259 | else { |
||
260 | /* the task has been preempted. it returns into the ready queue... */ |
||
29 | pj | 261 | iq_priority_insert(p,&lev->ready); |
2 | pj | 262 | proc_table[p].status = RM_READY; |
263 | } |
||
264 | } |
||
265 | |||
38 | pj | 266 | static void RM_public_activate(LEVEL l, PID p) |
2 | pj | 267 | { |
268 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
29 | pj | 269 | struct timespec *temp; |
2 | pj | 270 | |
271 | if (proc_table[p].status == RM_WAIT) { |
||
272 | kern_raise(XACTIVATION,p); |
||
273 | return; |
||
274 | } |
||
275 | |||
276 | /* Test if we are trying to activate a non sleeping task */ |
||
277 | /* Ignore this; the task is already active */ |
||
278 | if (proc_table[p].status != SLEEP && |
||
279 | proc_table[p].status != RM_WCET_VIOLATED) |
||
280 | return; |
||
281 | |||
282 | |||
283 | /* see also RM_timer_deadline */ |
||
29 | pj | 284 | temp = iq_query_timespec(p, &lev->ready); |
38 | pj | 285 | kern_gettime(temp); |
29 | pj | 286 | ADDUSEC2TIMESPEC(lev->period[p], temp); |
2 | pj | 287 | |
288 | /* Insert task in the correct position */ |
||
289 | proc_table[p].status = RM_READY; |
||
29 | pj | 290 | iq_priority_insert(p,&lev->ready); |
2 | pj | 291 | |
292 | /* Set the deadline timer */ |
||
29 | pj | 293 | lev->deadline_timer[p] = kern_event_post(temp, |
2 | pj | 294 | RM_timer_deadline, |
295 | (void *)p); |
||
296 | } |
||
297 | |||
38 | pj | 298 | static void RM_public_unblock(LEVEL l, PID p) |
2 | pj | 299 | { |
300 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
301 | |||
38 | pj | 302 | /* Similar to RM_task_activate, |
303 | but we don't check in what state the task is */ |
||
2 | pj | 304 | |
305 | /* Insert task in the correct position */ |
||
306 | proc_table[p].status = RM_READY; |
||
29 | pj | 307 | iq_priority_insert(p,&lev->ready); |
2 | pj | 308 | } |
309 | |||
38 | pj | 310 | static void RM_public_block(LEVEL l, PID p) |
2 | pj | 311 | { |
312 | /* Extract the running task from the level |
||
313 | . we have already extract it from the ready queue at the dispatch time. |
||
314 | . the capacity event have to be removed by the generic kernel |
||
315 | . the wcet don't need modification... |
||
316 | . the state of the task is set by the calling function |
||
317 | . the deadline must remain... |
||
318 | |||
319 | So, we do nothing!!! |
||
320 | */ |
||
321 | } |
||
322 | |||
38 | pj | 323 | static int RM_public_message(LEVEL l, PID p, void *m) |
2 | pj | 324 | { |
325 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
326 | |||
327 | /* the task has terminated his job before it consume the wcet. All OK! */ |
||
328 | if (lev->flag[p] & RM_FLAG_SPORADIC) |
||
329 | proc_table[p].status = RM_WAIT; |
||
330 | else /* pclass = sporadic_pclass */ |
||
331 | proc_table[p].status = RM_IDLE; |
||
332 | |||
333 | /* we reset the capacity counters... */ |
||
334 | if (lev->flags & RM_ENABLE_WCET_CHECK) |
||
335 | proc_table[p].avail_time = proc_table[p].wcet; |
||
336 | |||
38 | pj | 337 | jet_update_endcycle(); /* Update the Jet data... */ |
502 | giacomo | 338 | TRACER_LOGEVENT(FTrace_EVT_task_end_cycle,(unsigned short int)proc_table[p].context,(unsigned int)l); |
353 | giacomo | 339 | |
2 | pj | 340 | /* when the deadline timer fire, it recognize the situation and set |
38 | pj | 341 | correctly all the stuffs (like reactivation, sleep, etc... ) */ |
342 | |||
343 | return 0; |
||
2 | pj | 344 | } |
345 | |||
38 | pj | 346 | static void RM_public_end(LEVEL l, PID p) |
2 | pj | 347 | { |
348 | proc_table[p].status = RM_ZOMBIE; |
||
349 | |||
350 | /* When the deadline timer fire, it put the task descriptor in |
||
351 | the free queue, and free the allocated bandwidth... */ |
||
352 | } |
||
353 | |||
38 | pj | 354 | static void RM_private_insert(LEVEL l, PID p, TASK_MODEL *m) |
2 | pj | 355 | { |
356 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
38 | pj | 357 | JOB_TASK_MODEL *job; |
2 | pj | 358 | |
38 | pj | 359 | if (m->pclass != JOB_PCLASS || (m->level != 0 && m->level != l) ) { |
360 | kern_raise(XINVALID_TASK, p); |
||
361 | return; |
||
362 | } |
||
2 | pj | 363 | |
38 | pj | 364 | job = (JOB_TASK_MODEL *)m; |
2 | pj | 365 | |
29 | pj | 366 | *iq_query_timespec(p,&lev->ready) = job->deadline; |
38 | pj | 367 | *iq_query_priority(p, &lev->ready) = lev->period[p] = job->period; |
2 | pj | 368 | |
369 | lev->deadline_timer[p] = -1; |
||
370 | |||
38 | pj | 371 | /* Insert task in the correct position */ |
372 | iq_priority_insert(p,&lev->ready); |
||
373 | proc_table[p].status = RM_READY; |
||
374 | |||
2 | pj | 375 | if (job->noraiseexc) |
376 | lev->flag[p] = RM_FLAG_NORAISEEXC; |
||
38 | pj | 377 | else { |
2 | pj | 378 | lev->flag[p] = 0; |
38 | pj | 379 | lev->deadline_timer[p] = kern_event_post(iq_query_timespec(p, &lev->ready), |
380 | RM_timer_guest_deadline, |
||
381 | (void *)p); |
||
382 | } |
||
2 | pj | 383 | } |
384 | |||
38 | pj | 385 | static void RM_private_dispatch(LEVEL l, PID p, int nostop) |
2 | pj | 386 | { |
387 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
388 | |||
389 | /* the task state is set to EXE by the scheduler() |
||
390 | we extract the task from the ready queue |
||
391 | NB: we can't assume that p is the first task in the queue!!! */ |
||
29 | pj | 392 | iq_extract(p, &lev->ready); |
2 | pj | 393 | } |
394 | |||
38 | pj | 395 | static void RM_private_epilogue(LEVEL l, PID p) |
2 | pj | 396 | { |
397 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
398 | |||
399 | /* the task has been preempted. it returns into the ready queue... */ |
||
29 | pj | 400 | iq_priority_insert(p,&lev->ready); |
2 | pj | 401 | proc_table[p].status = RM_READY; |
402 | } |
||
403 | |||
38 | pj | 404 | static void RM_private_extract(LEVEL l, PID p) |
2 | pj | 405 | { |
406 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
407 | |||
408 | //kern_printf("RM_guest_end: dline timer %d\n",lev->deadline_timer[p]); |
||
409 | if (proc_table[p].status == RM_READY) |
||
410 | { |
||
29 | pj | 411 | iq_extract(p, &lev->ready); |
2 | pj | 412 | //kern_printf("(g_end rdy extr)"); |
413 | } |
||
414 | |||
415 | /* we remove the deadline timer, because the slice is finished */ |
||
416 | if (lev->deadline_timer[p] != NIL) { |
||
417 | // kern_printf("RM_guest_end: dline timer %d\n",lev->deadline_timer[p]); |
||
38 | pj | 418 | kern_event_delete(lev->deadline_timer[p]); |
2 | pj | 419 | lev->deadline_timer[p] = NIL; |
420 | } |
||
421 | |||
422 | } |
||
423 | |||
424 | /* Registration functions */ |
||
425 | |||
426 | /*+ Registration function: |
||
427 | int flags the init flags ... see rm.h +*/ |
||
38 | pj | 428 | LEVEL RM_register_level(int flags) |
2 | pj | 429 | { |
430 | LEVEL l; /* the level that we register */ |
||
431 | RM_level_des *lev; /* for readableness only */ |
||
432 | PID i; /* a counter */ |
||
433 | |||
434 | printk("RM_register_level\n"); |
||
435 | |||
436 | /* request an entry in the level_table */ |
||
38 | pj | 437 | l = level_alloc_descriptor(sizeof(RM_level_des)); |
2 | pj | 438 | |
38 | pj | 439 | lev = (RM_level_des *)level_table[l]; |
2 | pj | 440 | |
441 | /* fill the standard descriptor */ |
||
38 | pj | 442 | lev->l.private_insert = RM_private_insert; |
443 | lev->l.private_extract = RM_private_extract; |
||
444 | lev->l.private_dispatch = RM_private_dispatch; |
||
445 | lev->l.private_epilogue = RM_private_epilogue; |
||
2 | pj | 446 | |
38 | pj | 447 | lev->l.public_scheduler = RM_public_scheduler; |
2 | pj | 448 | if (flags & RM_ENABLE_GUARANTEE) |
38 | pj | 449 | lev->l.public_guarantee = RM_public_guarantee; |
2 | pj | 450 | else |
38 | pj | 451 | lev->l.public_guarantee = NULL; |
2 | pj | 452 | |
38 | pj | 453 | lev->l.public_create = RM_public_create; |
454 | lev->l.public_detach = RM_public_detach; |
||
455 | lev->l.public_end = RM_public_end; |
||
456 | lev->l.public_dispatch = RM_public_dispatch; |
||
457 | lev->l.public_epilogue = RM_public_epilogue; |
||
458 | lev->l.public_activate = RM_public_activate; |
||
459 | lev->l.public_unblock = RM_public_unblock; |
||
460 | lev->l.public_block = RM_public_block; |
||
461 | lev->l.public_message = RM_public_message; |
||
2 | pj | 462 | |
463 | /* fill the RM descriptor part */ |
||
464 | for(i=0; i<MAX_PROC; i++) { |
||
465 | lev->period[i] = 0; |
||
466 | lev->deadline_timer[i] = -1; |
||
467 | lev->flag[i] = 0; |
||
468 | } |
||
469 | |||
29 | pj | 470 | iq_init(&lev->ready, &freedesc, 0); |
159 | pj | 471 | lev->flags = flags; |
2 | pj | 472 | lev->U = 0; |
38 | pj | 473 | |
474 | return l; |
||
2 | pj | 475 | } |
476 | |||
477 | bandwidth_t RM_usedbandwidth(LEVEL l) |
||
478 | { |
||
479 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
38 | pj | 480 | |
481 | return lev->U; |
||
2 | pj | 482 | } |
483 |