Rev 159 | 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 | ------------ |
||
353 | giacomo | 23 | CVS : $Id: rm.c,v 1.6 2003-12-10 16:55:00 giacomo Exp $ |
2 | pj | 24 | |
25 | File: $File$ |
||
353 | giacomo | 26 | Revision: $Revision: 1.6 $ |
27 | Last update: $Date: 2003-12-10 16:55:00 $ |
||
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 */ |
||
353 | giacomo | 125 | TRACER_LOGEVENT(FTrace_EVT_task_timer,3,p,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 | printk("el%d|",p); |
||
137 | break; |
||
138 | |||
139 | case RM_WAIT: |
||
140 | /* Without this, the task cannot be reactivated!!! */ |
||
141 | proc_table[p].status = SLEEP; |
||
142 | break; |
||
143 | |||
144 | default: |
||
145 | /* else, a deadline miss occurred!!! */ |
||
146 | kern_printf("timer_deadline:AAARRRGGGHHH!!!"); |
||
147 | kern_raise(XDEADLINE_MISS,p); |
||
148 | } |
||
149 | } |
||
150 | |||
151 | static void RM_timer_guest_deadline(void *par) |
||
152 | { |
||
153 | PID p = (PID) par; |
||
154 | |||
155 | kern_printf("AAARRRGGGHHH!!!"); |
||
156 | kern_raise(XDEADLINE_MISS,p); |
||
157 | } |
||
158 | |||
159 | /* The scheduler only gets the first task in the queue */ |
||
38 | pj | 160 | static PID RM_public_scheduler(LEVEL l) |
2 | pj | 161 | { |
162 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
163 | |||
29 | pj | 164 | return iq_query_first(&lev->ready); |
2 | pj | 165 | } |
166 | |||
167 | /* The on-line guarantee is enabled only if the appropriate flag is set... */ |
||
38 | pj | 168 | static int RM_public_guarantee(LEVEL l, bandwidth_t *freebandwidth) |
2 | pj | 169 | { |
170 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
171 | |||
159 | pj | 172 | if (*freebandwidth >= lev->U) { |
173 | *freebandwidth -= lev->U; |
||
174 | return 1; |
||
2 | pj | 175 | } |
176 | else |
||
159 | pj | 177 | return 0; |
2 | pj | 178 | } |
179 | |||
38 | pj | 180 | static int RM_public_create(LEVEL l, PID p, TASK_MODEL *m) |
2 | pj | 181 | { |
182 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
183 | |||
38 | pj | 184 | HARD_TASK_MODEL *h; |
2 | pj | 185 | |
38 | pj | 186 | if (m->pclass != HARD_PCLASS) return -1; |
187 | if (m->level != 0 && m->level != l) return -1; |
||
188 | h = (HARD_TASK_MODEL *)m; |
||
189 | if (!h->wcet || !h->mit) return -1; |
||
159 | pj | 190 | |
191 | /* update the bandwidth... */ |
||
192 | if (lev->flags & RM_ENABLE_GUARANTEE) { |
||
193 | bandwidth_t b; |
||
194 | b = (MAX_BANDWIDTH / h->mit) * h->wcet; |
||
195 | |||
196 | /* really update lev->U, checking an overflow... */ |
||
197 | if (MAX_BANDWIDTH - lev->U > b) |
||
198 | lev->U += b; |
||
199 | else |
||
200 | return -1; |
||
201 | } |
||
202 | |||
38 | pj | 203 | /* now we know that m is a valid model */ |
2 | pj | 204 | |
29 | pj | 205 | *iq_query_priority(p, &lev->ready) = lev->period[p] = h->mit; |
2 | pj | 206 | |
207 | if (h->periodicity == APERIODIC) |
||
208 | lev->flag[p] = RM_FLAG_SPORADIC; |
||
209 | else |
||
210 | lev->flag[p] = 0; |
||
211 | lev->deadline_timer[p] = -1; |
||
212 | |||
213 | /* Enable wcet check */ |
||
214 | if (lev->flags & RM_ENABLE_WCET_CHECK) { |
||
215 | proc_table[p].avail_time = h->wcet; |
||
216 | proc_table[p].wcet = h->wcet; |
||
217 | proc_table[p].control |= CONTROL_CAP; |
||
218 | } |
||
219 | |||
220 | return 0; /* OK, also if the task cannot be guaranteed... */ |
||
221 | } |
||
222 | |||
38 | pj | 223 | static void RM_public_detach(LEVEL l, PID p) |
2 | pj | 224 | { |
225 | /* the RM level doesn't introduce any dinamic allocated new field. |
||
226 | we have only to reset the NO_GUARANTEE FIELD and decrement the allocated |
||
227 | bandwidth */ |
||
228 | |||
229 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
230 | |||
159 | pj | 231 | if (lev->flags & RM_ENABLE_GUARANTEE) { |
2 | pj | 232 | lev->U -= (MAX_BANDWIDTH / lev->period[p]) * proc_table[p].wcet; |
159 | pj | 233 | } |
2 | pj | 234 | } |
235 | |||
38 | pj | 236 | static void RM_public_dispatch(LEVEL l, PID p, int nostop) |
2 | pj | 237 | { |
238 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
239 | |||
240 | // kern_printf("(disp %d)",p); |
||
241 | |||
242 | /* the task state is set EXE by the scheduler() |
||
243 | we extract the task from the ready queue |
||
244 | NB: we can't assume that p is the first task in the queue!!! */ |
||
29 | pj | 245 | iq_extract(p, &lev->ready); |
2 | pj | 246 | } |
247 | |||
38 | pj | 248 | static void RM_public_epilogue(LEVEL l, PID p) |
2 | pj | 249 | { |
250 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
251 | |||
252 | // kern_printf("(epil %d)",p); |
||
253 | |||
254 | /* check if the wcet is finished... */ |
||
255 | if ((lev->flags & RM_ENABLE_WCET_CHECK) && proc_table[p].avail_time <= 0) { |
||
256 | /* if it is, raise a XWCET_VIOLATION exception */ |
||
257 | kern_raise(XWCET_VIOLATION,p); |
||
258 | proc_table[p].status = RM_WCET_VIOLATED; |
||
259 | } |
||
260 | else { |
||
261 | /* the task has been preempted. it returns into the ready queue... */ |
||
29 | pj | 262 | iq_priority_insert(p,&lev->ready); |
2 | pj | 263 | proc_table[p].status = RM_READY; |
264 | } |
||
265 | } |
||
266 | |||
38 | pj | 267 | static void RM_public_activate(LEVEL l, PID p) |
2 | pj | 268 | { |
269 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
29 | pj | 270 | struct timespec *temp; |
2 | pj | 271 | |
272 | if (proc_table[p].status == RM_WAIT) { |
||
273 | kern_raise(XACTIVATION,p); |
||
274 | return; |
||
275 | } |
||
276 | |||
277 | /* Test if we are trying to activate a non sleeping task */ |
||
278 | /* Ignore this; the task is already active */ |
||
279 | if (proc_table[p].status != SLEEP && |
||
280 | proc_table[p].status != RM_WCET_VIOLATED) |
||
281 | return; |
||
282 | |||
283 | |||
284 | /* see also RM_timer_deadline */ |
||
29 | pj | 285 | temp = iq_query_timespec(p, &lev->ready); |
38 | pj | 286 | kern_gettime(temp); |
29 | pj | 287 | ADDUSEC2TIMESPEC(lev->period[p], temp); |
2 | pj | 288 | |
289 | /* Insert task in the correct position */ |
||
290 | proc_table[p].status = RM_READY; |
||
29 | pj | 291 | iq_priority_insert(p,&lev->ready); |
2 | pj | 292 | |
293 | /* Set the deadline timer */ |
||
29 | pj | 294 | lev->deadline_timer[p] = kern_event_post(temp, |
2 | pj | 295 | RM_timer_deadline, |
296 | (void *)p); |
||
297 | } |
||
298 | |||
38 | pj | 299 | static void RM_public_unblock(LEVEL l, PID p) |
2 | pj | 300 | { |
301 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
302 | |||
38 | pj | 303 | /* Similar to RM_task_activate, |
304 | but we don't check in what state the task is */ |
||
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 | |||
38 | pj | 311 | static void RM_public_block(LEVEL l, PID p) |
2 | pj | 312 | { |
313 | /* Extract the running task from the level |
||
314 | . we have already extract it from the ready queue at the dispatch time. |
||
315 | . the capacity event have to be removed by the generic kernel |
||
316 | . the wcet don't need modification... |
||
317 | . the state of the task is set by the calling function |
||
318 | . the deadline must remain... |
||
319 | |||
320 | So, we do nothing!!! |
||
321 | */ |
||
322 | } |
||
323 | |||
38 | pj | 324 | static int RM_public_message(LEVEL l, PID p, void *m) |
2 | pj | 325 | { |
326 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
327 | |||
328 | /* the task has terminated his job before it consume the wcet. All OK! */ |
||
329 | if (lev->flag[p] & RM_FLAG_SPORADIC) |
||
330 | proc_table[p].status = RM_WAIT; |
||
331 | else /* pclass = sporadic_pclass */ |
||
332 | proc_table[p].status = RM_IDLE; |
||
333 | |||
334 | /* we reset the capacity counters... */ |
||
335 | if (lev->flags & RM_ENABLE_WCET_CHECK) |
||
336 | proc_table[p].avail_time = proc_table[p].wcet; |
||
337 | |||
38 | pj | 338 | jet_update_endcycle(); /* Update the Jet data... */ |
353 | giacomo | 339 | TRACER_LOGEVENT(FTrace_EVT_task_end_cycle,3,p,l); |
340 | |||
2 | pj | 341 | /* when the deadline timer fire, it recognize the situation and set |
38 | pj | 342 | correctly all the stuffs (like reactivation, sleep, etc... ) */ |
343 | |||
344 | return 0; |
||
2 | pj | 345 | } |
346 | |||
38 | pj | 347 | static void RM_public_end(LEVEL l, PID p) |
2 | pj | 348 | { |
349 | proc_table[p].status = RM_ZOMBIE; |
||
350 | |||
351 | /* When the deadline timer fire, it put the task descriptor in |
||
352 | the free queue, and free the allocated bandwidth... */ |
||
353 | } |
||
354 | |||
38 | pj | 355 | static void RM_private_insert(LEVEL l, PID p, TASK_MODEL *m) |
2 | pj | 356 | { |
357 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
38 | pj | 358 | JOB_TASK_MODEL *job; |
2 | pj | 359 | |
38 | pj | 360 | if (m->pclass != JOB_PCLASS || (m->level != 0 && m->level != l) ) { |
361 | kern_raise(XINVALID_TASK, p); |
||
362 | return; |
||
363 | } |
||
2 | pj | 364 | |
38 | pj | 365 | job = (JOB_TASK_MODEL *)m; |
2 | pj | 366 | |
29 | pj | 367 | *iq_query_timespec(p,&lev->ready) = job->deadline; |
38 | pj | 368 | *iq_query_priority(p, &lev->ready) = lev->period[p] = job->period; |
2 | pj | 369 | |
370 | lev->deadline_timer[p] = -1; |
||
371 | |||
38 | pj | 372 | /* Insert task in the correct position */ |
373 | iq_priority_insert(p,&lev->ready); |
||
374 | proc_table[p].status = RM_READY; |
||
375 | |||
2 | pj | 376 | if (job->noraiseexc) |
377 | lev->flag[p] = RM_FLAG_NORAISEEXC; |
||
38 | pj | 378 | else { |
2 | pj | 379 | lev->flag[p] = 0; |
38 | pj | 380 | lev->deadline_timer[p] = kern_event_post(iq_query_timespec(p, &lev->ready), |
381 | RM_timer_guest_deadline, |
||
382 | (void *)p); |
||
383 | } |
||
2 | pj | 384 | } |
385 | |||
38 | pj | 386 | static void RM_private_dispatch(LEVEL l, PID p, int nostop) |
2 | pj | 387 | { |
388 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
389 | |||
390 | /* the task state is set to EXE by the scheduler() |
||
391 | we extract the task from the ready queue |
||
392 | NB: we can't assume that p is the first task in the queue!!! */ |
||
29 | pj | 393 | iq_extract(p, &lev->ready); |
2 | pj | 394 | } |
395 | |||
38 | pj | 396 | static void RM_private_epilogue(LEVEL l, PID p) |
2 | pj | 397 | { |
398 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
399 | |||
400 | /* the task has been preempted. it returns into the ready queue... */ |
||
29 | pj | 401 | iq_priority_insert(p,&lev->ready); |
2 | pj | 402 | proc_table[p].status = RM_READY; |
403 | } |
||
404 | |||
38 | pj | 405 | static void RM_private_extract(LEVEL l, PID p) |
2 | pj | 406 | { |
407 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
408 | |||
409 | //kern_printf("RM_guest_end: dline timer %d\n",lev->deadline_timer[p]); |
||
410 | if (proc_table[p].status == RM_READY) |
||
411 | { |
||
29 | pj | 412 | iq_extract(p, &lev->ready); |
2 | pj | 413 | //kern_printf("(g_end rdy extr)"); |
414 | } |
||
415 | |||
416 | /* we remove the deadline timer, because the slice is finished */ |
||
417 | if (lev->deadline_timer[p] != NIL) { |
||
418 | // kern_printf("RM_guest_end: dline timer %d\n",lev->deadline_timer[p]); |
||
38 | pj | 419 | kern_event_delete(lev->deadline_timer[p]); |
2 | pj | 420 | lev->deadline_timer[p] = NIL; |
421 | } |
||
422 | |||
423 | } |
||
424 | |||
425 | /* Registration functions */ |
||
426 | |||
427 | /*+ Registration function: |
||
428 | int flags the init flags ... see rm.h +*/ |
||
38 | pj | 429 | LEVEL RM_register_level(int flags) |
2 | pj | 430 | { |
431 | LEVEL l; /* the level that we register */ |
||
432 | RM_level_des *lev; /* for readableness only */ |
||
433 | PID i; /* a counter */ |
||
434 | |||
435 | printk("RM_register_level\n"); |
||
436 | |||
437 | /* request an entry in the level_table */ |
||
38 | pj | 438 | l = level_alloc_descriptor(sizeof(RM_level_des)); |
2 | pj | 439 | |
38 | pj | 440 | lev = (RM_level_des *)level_table[l]; |
2 | pj | 441 | |
442 | printk(" lev=%d\n",(int)lev); |
||
443 | |||
444 | /* fill the standard descriptor */ |
||
38 | pj | 445 | lev->l.private_insert = RM_private_insert; |
446 | lev->l.private_extract = RM_private_extract; |
||
447 | lev->l.private_dispatch = RM_private_dispatch; |
||
448 | lev->l.private_epilogue = RM_private_epilogue; |
||
2 | pj | 449 | |
38 | pj | 450 | lev->l.public_scheduler = RM_public_scheduler; |
2 | pj | 451 | if (flags & RM_ENABLE_GUARANTEE) |
38 | pj | 452 | lev->l.public_guarantee = RM_public_guarantee; |
2 | pj | 453 | else |
38 | pj | 454 | lev->l.public_guarantee = NULL; |
2 | pj | 455 | |
38 | pj | 456 | lev->l.public_create = RM_public_create; |
457 | lev->l.public_detach = RM_public_detach; |
||
458 | lev->l.public_end = RM_public_end; |
||
459 | lev->l.public_dispatch = RM_public_dispatch; |
||
460 | lev->l.public_epilogue = RM_public_epilogue; |
||
461 | lev->l.public_activate = RM_public_activate; |
||
462 | lev->l.public_unblock = RM_public_unblock; |
||
463 | lev->l.public_block = RM_public_block; |
||
464 | lev->l.public_message = RM_public_message; |
||
2 | pj | 465 | |
466 | /* fill the RM descriptor part */ |
||
467 | for(i=0; i<MAX_PROC; i++) { |
||
468 | lev->period[i] = 0; |
||
469 | lev->deadline_timer[i] = -1; |
||
470 | lev->flag[i] = 0; |
||
471 | } |
||
472 | |||
29 | pj | 473 | iq_init(&lev->ready, &freedesc, 0); |
159 | pj | 474 | lev->flags = flags; |
2 | pj | 475 | lev->U = 0; |
38 | pj | 476 | |
477 | return l; |
||
2 | pj | 478 | } |
479 | |||
480 | bandwidth_t RM_usedbandwidth(LEVEL l) |
||
481 | { |
||
482 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
38 | pj | 483 | |
484 | return lev->U; |
||
2 | pj | 485 | } |
486 |