Rev 657 | 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 | * |
||
761 | anton | 8 | * Authors: |
2 | pj | 9 | * Paolo Gai <pj@gandalf.sssup.it> |
10 | * Massimiliano Giorgi <massy@gandalf.sssup.it> |
||
11 | * Luca Abeni <luca@gandalf.sssup.it> |
||
761 | anton | 12 | * Anton Cervin |
2 | pj | 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 | ------------ |
||
761 | anton | 23 | CVS : $Id: rm.c,v 1.10 2004-06-21 11:43:02 anton Exp $ |
2 | pj | 24 | |
25 | File: $File$ |
||
761 | anton | 26 | Revision: $Revision: 1.10 $ |
27 | Last update: $Date: 2004-06-21 11:43:02 $ |
||
2 | pj | 28 | ------------ |
29 | |||
761 | anton | 30 | This file contains the scheduling module RM (rate-/deadline-monotonic) |
2 | pj | 31 | |
32 | Read rm.h for further details. |
||
33 | |||
34 | **/ |
||
35 | |||
36 | /* |
||
38 | pj | 37 | * Copyright (C) 2000,2002 Paolo Gai |
2 | pj | 38 | * |
39 | * This program is free software; you can redistribute it and/or modify |
||
40 | * it under the terms of the GNU General Public License as published by |
||
41 | * the Free Software Foundation; either version 2 of the License, or |
||
42 | * (at your option) any later version. |
||
43 | * |
||
44 | * This program is distributed in the hope that it will be useful, |
||
45 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
46 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
47 | * GNU General Public License for more details. |
||
48 | * |
||
49 | * You should have received a copy of the GNU General Public License |
||
50 | * along with this program; if not, write to the Free Software |
||
51 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
52 | * |
||
53 | */ |
||
54 | |||
55 | |||
56 | #include <modules/rm.h> |
||
57 | #include <ll/stdio.h> |
||
58 | #include <ll/string.h> |
||
59 | #include <kernel/model.h> |
||
60 | #include <kernel/descr.h> |
||
61 | #include <kernel/var.h> |
||
62 | #include <kernel/func.h> |
||
353 | giacomo | 63 | #include <tracer.h> |
64 | |||
657 | anton | 65 | //#define RM_DEBUG |
66 | #define rm_printf kern_printf |
||
67 | #ifdef RM_DEBUG |
||
68 | char *pnow() { |
||
69 | static char buf[40]; |
||
70 | struct timespec t; |
||
71 | sys_gettime(&t); |
||
72 | sprintf(buf, "%ld.%06ld", t.tv_sec, t.tv_nsec/1000); |
||
73 | return buf; |
||
74 | } |
||
75 | char *ptime1(struct timespec *t) { |
||
76 | static char buf[40]; |
||
77 | sprintf(buf, "%ld.%06ld", t->tv_sec, t->tv_nsec/1000); |
||
78 | return buf; |
||
79 | } |
||
80 | char *ptime2(struct timespec *t) { |
||
81 | static char buf[40]; |
||
82 | sprintf(buf, "%ld.%06ld", t->tv_sec, t->tv_nsec/1000); |
||
83 | return buf; |
||
84 | } |
||
85 | #endif |
||
2 | pj | 86 | |
761 | anton | 87 | /* Statuses used in the level */ |
88 | #define RM_READY MODULE_STATUS_BASE /* ready */ |
||
89 | #define RM_IDLE MODULE_STATUS_BASE+1 /* idle, waiting for offset/eop */ |
||
90 | #define RM_WAIT MODULE_STATUS_BASE+2 /* to sleep, waiting for eop */ |
||
91 | #define RM_ZOMBIE MODULE_STATUS_BASE+3 /* to free, waiting for eop */ |
||
2 | pj | 92 | |
761 | anton | 93 | /* Task flags */ |
657 | anton | 94 | #define RM_FLAG_SPORADIC 1 /* the task is sporadic */ |
95 | #define RM_FLAG_SPOR_LATE 2 /* sporadic task with period overrun */ |
||
2 | pj | 96 | |
97 | |||
761 | anton | 98 | /* Task descriptor */ |
657 | anton | 99 | typedef struct { |
761 | anton | 100 | int flags; /* task flags */ |
101 | TIME period; /* period (or inter-arrival interval) */ |
||
102 | TIME rdeadline; /* relative deadline */ |
||
103 | TIME offset; /* release offset */ |
||
104 | struct timespec release; /* release time of current instance */ |
||
105 | struct timespec adeadline; /* latest assigned deadline */ |
||
106 | int dl_timer; /* deadline timer */ |
||
107 | int eop_timer; /* end of period timer */ |
||
108 | int dl_miss; /* deadline miss counter */ |
||
109 | int wcet_miss; /* WCET miss counter */ |
||
110 | int act_miss; /* activation miss counter */ |
||
111 | int nact; /* number of pending periodic jobs */ |
||
112 | } RM_task_des; |
||
2 | pj | 113 | |
761 | anton | 114 | |
115 | /* Level descriptor */ |
||
116 | typedef struct { |
||
117 | level_des l; /* standard level descriptor */ |
||
118 | int flags; /* level flags */ |
||
119 | IQUEUE ready; /* the ready queue */ |
||
120 | bandwidth_t U; /* used bandwidth */ |
||
121 | RM_task_des tvec[MAX_PROC]; /* vector of task descriptors */ |
||
657 | anton | 122 | } RM_level_des; |
2 | pj | 123 | |
124 | |||
761 | anton | 125 | /* Module function cross-references */ |
126 | static void RM_intern_release(PID p, RM_level_des *lev); |
||
2 | pj | 127 | |
128 | |||
761 | anton | 129 | /**** Timer event handler functions ****/ |
657 | anton | 130 | |
761 | anton | 131 | /* This timer event handler is called at the end of the period */ |
132 | static void RM_timer_endperiod(void *par) |
||
2 | pj | 133 | { |
134 | PID p = (PID) par; |
||
761 | anton | 135 | RM_level_des *lev = (RM_level_des *)level_table[proc_table[p].task_level]; |
136 | RM_task_des *td = &lev->tvec[p]; |
||
657 | anton | 137 | |
761 | anton | 138 | td->eop_timer = -1; |
2 | pj | 139 | |
761 | anton | 140 | if (proc_table[p].status == RM_ZOMBIE) { |
141 | /* put the task in the FREE state */ |
||
142 | proc_table[p].status = FREE; |
||
143 | iq_insertfirst(p,&freedesc); |
||
144 | /* free the allocated bandwidth */ |
||
145 | lev->U -= (MAX_BANDWIDTH/td->rdeadline) * proc_table[p].wcet; |
||
146 | return; |
||
147 | } |
||
148 | |||
149 | if (proc_table[p].status == RM_WAIT) { |
||
150 | proc_table[p].status = SLEEP; |
||
151 | return; |
||
152 | } |
||
153 | |||
154 | if (td->flags & RM_FLAG_SPORADIC) { |
||
155 | /* the task is sporadic and still busy, mark it as late */ |
||
156 | td->flags |= RM_FLAG_SPOR_LATE; |
||
157 | } else { |
||
158 | /* the task is periodic, release/queue another instance */ |
||
159 | RM_intern_release(p, lev); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | /* This timer event handler is called when a task misses its deadline */ |
||
164 | static void RM_timer_deadline(void *par) |
||
165 | { |
||
166 | PID p = (PID) par; |
||
167 | RM_level_des *lev = (RM_level_des *)level_table[proc_table[p].task_level]; |
||
168 | RM_task_des *td = &lev->tvec[p]; |
||
169 | |||
170 | TRACER_LOGEVENT(FTrace_EVT_task_deadline_miss, |
||
171 | (unsigned short int)proc_table[p].context,0); |
||
172 | |||
657 | anton | 173 | if (lev->flags & RM_ENABLE_DL_EXCEPTION) { |
174 | kern_raise(XDEADLINE_MISS,p); |
||
175 | } else { |
||
761 | anton | 176 | td->dl_miss++; |
657 | anton | 177 | } |
178 | } |
||
179 | |||
761 | anton | 180 | /* This timer event handler is called after waiting for an offset */ |
181 | static void RM_timer_offset(void *par) |
||
182 | { |
||
183 | PID p = (PID) par; |
||
184 | RM_level_des *lev; |
||
185 | lev = (RM_level_des *)level_table[proc_table[p].task_level]; |
||
186 | /* release the task now */ |
||
187 | RM_intern_release(p, lev); |
||
188 | } |
||
657 | anton | 189 | |
761 | anton | 190 | /* This function is called when a guest task misses its deadline */ |
191 | static void RM_timer_guest_deadline(void *par) |
||
192 | { |
||
193 | PID p = (PID) par; |
||
194 | TRACER_LOGEVENT(FTrace_EVT_task_deadline_miss, |
||
195 | (unsigned short int)proc_table[p].context,0); |
||
196 | kern_raise(XDEADLINE_MISS,p); |
||
197 | } |
||
657 | anton | 198 | |
761 | anton | 199 | |
200 | /**** Internal utility functions ****/ |
||
201 | |||
202 | /* Release (or queue) a task, post deadline and endperiod timers */ |
||
657 | anton | 203 | static void RM_intern_release(PID p, RM_level_des *lev) |
204 | { |
||
205 | struct timespec temp; |
||
761 | anton | 206 | RM_task_des *td = &lev->tvec[p]; |
657 | anton | 207 | |
208 | /* post deadline timer */ |
||
209 | if (lev->flags & RM_ENABLE_DL_CHECK) { |
||
761 | anton | 210 | temp = td->release; |
211 | ADDUSEC2TIMESPEC(td->rdeadline, &temp); |
||
212 | td->dl_timer = kern_event_post(&temp,RM_timer_deadline,(void *)p); |
||
657 | anton | 213 | } |
214 | |||
215 | /* release or queue next job */ |
||
216 | if (proc_table[p].status == RM_IDLE) { |
||
217 | /* assign deadline, insert task in the ready queue */ |
||
218 | proc_table[p].status = RM_READY; |
||
761 | anton | 219 | *iq_query_priority(p,&lev->ready) = td->rdeadline; |
657 | anton | 220 | iq_priority_insert(p,&lev->ready); |
221 | #ifdef RM_DEBUG |
||
761 | anton | 222 | rm_printf("At %s: releasing %s with deadline %s\n", pnow(), |
223 | proc_table[p].name, ptime1(&td->adeadline)); |
||
657 | anton | 224 | #endif |
761 | anton | 225 | /* increase assigned deadline */ |
226 | ADDUSEC2TIMESPEC(td->period, &td->adeadline); |
||
657 | anton | 227 | /* reschedule */ |
228 | event_need_reschedule(); |
||
229 | } else { |
||
230 | /* queue */ |
||
761 | anton | 231 | td->nact++; |
657 | anton | 232 | } |
233 | |||
234 | /* increase release time */ |
||
761 | anton | 235 | ADDUSEC2TIMESPEC(td->period, &td->release); |
657 | anton | 236 | /* post end of period timer */ |
761 | anton | 237 | td->eop_timer = kern_event_post(&td->release, RM_timer_endperiod,(void *)p); |
657 | anton | 238 | |
761 | anton | 239 | TRACER_LOGEVENT(FTrace_EVT_task_timer, |
240 | (unsigned short int)proc_table[p].context, |
||
241 | (unsigned int)proc_table[p].task_level); |
||
657 | anton | 242 | } |
243 | |||
244 | |||
761 | anton | 245 | /**** Public generic kernel interface functions ****/ |
657 | anton | 246 | |
761 | anton | 247 | /* Returns the first task in the ready queue */ |
38 | pj | 248 | static PID RM_public_scheduler(LEVEL l) |
2 | pj | 249 | { |
250 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
29 | pj | 251 | return iq_query_first(&lev->ready); |
2 | pj | 252 | } |
253 | |||
761 | anton | 254 | /* Checks and decreases the available system bandwidth */ |
38 | pj | 255 | static int RM_public_guarantee(LEVEL l, bandwidth_t *freebandwidth) |
2 | pj | 256 | { |
257 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
258 | |||
159 | pj | 259 | if (*freebandwidth >= lev->U) { |
260 | *freebandwidth -= lev->U; |
||
261 | return 1; |
||
2 | pj | 262 | } |
263 | else |
||
159 | pj | 264 | return 0; |
2 | pj | 265 | } |
266 | |||
761 | anton | 267 | /* Called by task_create: Checks task model and creates a task */ |
38 | pj | 268 | static int RM_public_create(LEVEL l, PID p, TASK_MODEL *m) |
2 | pj | 269 | { |
270 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
761 | anton | 271 | RM_task_des *td = &lev->tvec[p]; |
38 | pj | 272 | HARD_TASK_MODEL *h; |
2 | pj | 273 | |
38 | pj | 274 | if (m->pclass != HARD_PCLASS) return -1; |
275 | if (m->level != 0 && m->level != l) return -1; |
||
276 | h = (HARD_TASK_MODEL *)m; |
||
277 | if (!h->wcet || !h->mit) return -1; |
||
657 | anton | 278 | if (h->drel > h->mit) return -1; /* only D <= T supported */ |
159 | pj | 279 | |
657 | anton | 280 | if (!h->drel) { |
761 | anton | 281 | td->rdeadline = h->mit; |
657 | anton | 282 | } else { |
761 | anton | 283 | td->rdeadline = h->drel; |
657 | anton | 284 | } |
285 | |||
286 | /* check the free bandwidth... */ |
||
159 | pj | 287 | if (lev->flags & RM_ENABLE_GUARANTEE) { |
288 | bandwidth_t b; |
||
761 | anton | 289 | b = (MAX_BANDWIDTH / td->rdeadline) * h->wcet; |
159 | pj | 290 | |
291 | /* really update lev->U, checking an overflow... */ |
||
657 | anton | 292 | if (MAX_BANDWIDTH - lev->U > b) { |
159 | pj | 293 | lev->U += b; |
657 | anton | 294 | } else { |
159 | pj | 295 | return -1; |
657 | anton | 296 | } |
159 | pj | 297 | } |
298 | |||
761 | anton | 299 | td->flags = 0; |
300 | if (h->periodicity == APERIODIC) { |
||
301 | td->flags |= RM_FLAG_SPORADIC; |
||
657 | anton | 302 | } |
761 | anton | 303 | td->period = h->mit; |
304 | if (td->rdeadline == td->period) { |
||
305 | /* Ensure that D <= T-eps to make dl_timer trigger before eop_timer */ |
||
306 | td->rdeadline = td->period - 1; |
||
657 | anton | 307 | } |
761 | anton | 308 | td->offset = h->offset; |
309 | td->dl_timer = -1; |
||
310 | td->eop_timer = -1; |
||
311 | td->dl_miss = 0; |
||
312 | td->wcet_miss = 0; |
||
313 | td->act_miss = 0; |
||
314 | td->nact = 0; |
||
2 | pj | 315 | |
316 | /* Enable wcet check */ |
||
317 | if (lev->flags & RM_ENABLE_WCET_CHECK) { |
||
318 | proc_table[p].avail_time = h->wcet; |
||
319 | proc_table[p].wcet = h->wcet; |
||
657 | anton | 320 | proc_table[p].control |= CONTROL_CAP; /* turn on measurement */ |
2 | pj | 321 | } |
322 | |||
323 | return 0; /* OK, also if the task cannot be guaranteed... */ |
||
324 | } |
||
325 | |||
761 | anton | 326 | /* Reclaim the bandwidth used by the task */ |
38 | pj | 327 | static void RM_public_detach(LEVEL l, PID p) |
2 | pj | 328 | { |
329 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
761 | anton | 330 | RM_task_des *td = &lev->tvec[p]; |
2 | pj | 331 | |
159 | pj | 332 | if (lev->flags & RM_ENABLE_GUARANTEE) { |
761 | anton | 333 | lev->U -= (MAX_BANDWIDTH / td->rdeadline) * proc_table[p].wcet; |
159 | pj | 334 | } |
2 | pj | 335 | } |
336 | |||
761 | anton | 337 | /* Extracts the running task from the ready queue */ |
38 | pj | 338 | static void RM_public_dispatch(LEVEL l, PID p, int nostop) |
2 | pj | 339 | { |
340 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
29 | pj | 341 | iq_extract(p, &lev->ready); |
2 | pj | 342 | } |
343 | |||
761 | anton | 344 | /* Called when the task is preempted or when its budget is exhausted */ |
38 | pj | 345 | static void RM_public_epilogue(LEVEL l, PID p) |
2 | pj | 346 | { |
347 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
761 | anton | 348 | RM_task_des *td = &lev->tvec[p]; |
2 | pj | 349 | |
350 | /* check if the wcet is finished... */ |
||
657 | anton | 351 | if (lev->flags & RM_ENABLE_WCET_CHECK) { |
352 | if (proc_table[p].avail_time <= 0) { |
||
761 | anton | 353 | TRACER_LOGEVENT(FTrace_EVT_task_wcet_violation, |
354 | (unsigned short int)proc_table[p].context,0); |
||
657 | anton | 355 | if (lev->flags & RM_ENABLE_WCET_EXCEPTION) { |
356 | kern_raise(XWCET_VIOLATION,p); |
||
357 | } else { |
||
358 | proc_table[p].control &= ~CONTROL_CAP; |
||
761 | anton | 359 | td->wcet_miss++; |
657 | anton | 360 | } |
361 | } |
||
2 | pj | 362 | } |
657 | anton | 363 | |
364 | /* the task returns to the ready queue */ |
||
365 | iq_priority_insert(p,&lev->ready); |
||
366 | proc_table[p].status = RM_READY; |
||
367 | |||
2 | pj | 368 | } |
369 | |||
761 | anton | 370 | /* Called by task_activate or group_activate: Activates the task at time t */ |
657 | anton | 371 | static void RM_public_activate(LEVEL l, PID p, struct timespec *t) |
2 | pj | 372 | { |
657 | anton | 373 | struct timespec clocktime; |
2 | pj | 374 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
761 | anton | 375 | RM_task_des *td = &lev->tvec[p]; |
2 | pj | 376 | |
657 | anton | 377 | kern_gettime(&clocktime); |
378 | |||
379 | /* check if we are not in the SLEEP state */ |
||
380 | if (proc_table[p].status != SLEEP) { |
||
381 | if (lev->flags & RM_ENABLE_ACT_EXCEPTION) { |
||
382 | /* too frequent or wrongful activation: raise exception */ |
||
383 | kern_raise(XACTIVATION,p); |
||
384 | } else { |
||
385 | /* skip the sporadic job, but increase a counter */ |
||
386 | #ifdef RM_DEBUG |
||
761 | anton | 387 | rm_printf("At %s: activation of %s skipped\n", pnow(), |
388 | proc_table[p].name); |
||
657 | anton | 389 | #endif |
761 | anton | 390 | td->act_miss++; |
657 | anton | 391 | } |
2 | pj | 392 | return; |
393 | } |
||
657 | anton | 394 | |
395 | /* set the release time to the activation time + offset */ |
||
761 | anton | 396 | td->release = *t; |
397 | ADDUSEC2TIMESPEC(td->offset, &td->release); |
||
2 | pj | 398 | |
761 | anton | 399 | /* set the absolute deadline to the activation time + offset + rdeadline */ |
400 | td->adeadline = td->release; |
||
401 | ADDUSEC2TIMESPEC(td->rdeadline, &td->adeadline); |
||
402 | |||
403 | /* Check if release > clocktime. If yes, release it later, |
||
657 | anton | 404 | otherwise release it now. */ |
2 | pj | 405 | |
657 | anton | 406 | proc_table[p].status = RM_IDLE; |
2 | pj | 407 | |
761 | anton | 408 | if (TIMESPEC_A_GT_B(&td->release, &clocktime)) { |
409 | /* release later, post an offset timer */ |
||
410 | kern_event_post(&td->release,RM_timer_offset,(void *)p); |
||
657 | anton | 411 | } else { |
412 | /* release now */ |
||
413 | RM_intern_release(p, lev); |
||
414 | } |
||
2 | pj | 415 | } |
416 | |||
761 | anton | 417 | /* Reinserts a task that has been blocked into the ready queue */ |
38 | pj | 418 | static void RM_public_unblock(LEVEL l, PID p) |
2 | pj | 419 | { |
420 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
421 | |||
422 | /* Insert task in the correct position */ |
||
423 | proc_table[p].status = RM_READY; |
||
29 | pj | 424 | iq_priority_insert(p,&lev->ready); |
2 | pj | 425 | } |
426 | |||
761 | anton | 427 | /* Called when a task experiences a synchronization block */ |
38 | pj | 428 | static void RM_public_block(LEVEL l, PID p) |
2 | pj | 429 | { |
430 | /* Extract the running task from the level |
||
431 | . we have already extract it from the ready queue at the dispatch time. |
||
432 | . the capacity event have to be removed by the generic kernel |
||
433 | . the wcet don't need modification... |
||
434 | . the state of the task is set by the calling function |
||
435 | . the deadline must remain... |
||
436 | |||
437 | So, we do nothing!!! |
||
438 | */ |
||
439 | } |
||
440 | |||
761 | anton | 441 | /* Called by task_endcycle or task_sleep: Ends the current instance */ |
38 | pj | 442 | static int RM_public_message(LEVEL l, PID p, void *m) |
2 | pj | 443 | { |
444 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
761 | anton | 445 | RM_task_des *td = &lev->tvec[p]; |
2 | pj | 446 | |
657 | anton | 447 | switch((long)(m)) { |
448 | /* task_endcycle() */ |
||
449 | case 0: |
||
450 | /* if there are no pending jobs */ |
||
761 | anton | 451 | if (td->nact == 0) { |
657 | anton | 452 | /* remove deadline timer, if any */ |
761 | anton | 453 | if (td->dl_timer != -1) { |
454 | kern_event_delete(td->dl_timer); |
||
455 | td->dl_timer = -1; |
||
657 | anton | 456 | } |
761 | anton | 457 | if (td->flags & RM_FLAG_SPORADIC) { |
657 | anton | 458 | /* sporadic task */ |
761 | anton | 459 | if (!(td->flags & RM_FLAG_SPOR_LATE)) { |
657 | anton | 460 | proc_table[p].status = RM_WAIT; |
461 | } else { |
||
462 | /* it's late, move it directly to SLEEP */ |
||
463 | proc_table[p].status = SLEEP; |
||
761 | anton | 464 | td->flags &= ~RM_FLAG_SPOR_LATE; |
657 | anton | 465 | } |
466 | } else { |
||
467 | /* periodic task */ |
||
468 | proc_table[p].status = RM_IDLE; |
||
469 | } |
||
470 | } else { |
||
471 | /* we are late / there are pending jobs */ |
||
761 | anton | 472 | td->nact--; |
473 | /* compute and assign absolute deadline */ |
||
474 | *iq_query_priority(p,&lev->ready) = td->rdeadline; |
||
657 | anton | 475 | iq_priority_insert(p,&lev->ready); |
761 | anton | 476 | /* increase assigned deadline */ |
477 | ADDUSEC2TIMESPEC(td->period, &td->adeadline); |
||
657 | anton | 478 | #ifdef RM_DEBUG |
761 | anton | 479 | rm_printf("(Late) At %s: releasing %s with deadline %s\n", |
480 | pnow(),proc_table[p].name,ptime1(&td->adeadline)); |
||
657 | anton | 481 | #endif |
482 | } |
||
483 | break; |
||
484 | |||
485 | /* task_sleep() */ |
||
486 | case 1: |
||
487 | /* remove deadline timer, if any */ |
||
761 | anton | 488 | if (td->dl_timer != -1) { |
489 | kern_event_delete(td->dl_timer); |
||
490 | td->dl_timer = -1; |
||
657 | anton | 491 | } |
761 | anton | 492 | if (td->flags & RM_FLAG_SPORADIC) { |
657 | anton | 493 | /* sporadic task */ |
761 | anton | 494 | if (!(td->flags & RM_FLAG_SPOR_LATE)) { |
657 | anton | 495 | proc_table[p].status = RM_WAIT; |
496 | } else { |
||
497 | /* it's late, move it directly to SLEEP */ |
||
498 | proc_table[p].status = SLEEP; |
||
761 | anton | 499 | td->flags &= ~RM_FLAG_SPOR_LATE; |
657 | anton | 500 | } |
501 | } else { |
||
502 | /* periodic task */ |
||
761 | anton | 503 | if (!(td->nact > 0)) { |
657 | anton | 504 | /* we are on time. go to the RM_WAIT state */ |
505 | proc_table[p].status = RM_WAIT; |
||
506 | } else { |
||
507 | /* we are late. delete pending activations and go to SLEEP */ |
||
761 | anton | 508 | td->nact = 0; |
657 | anton | 509 | proc_table[p].status = SLEEP; |
510 | /* remove end of period timer */ |
||
761 | anton | 511 | if (td->eop_timer != -1) { |
512 | kern_event_delete(td->eop_timer); |
||
513 | td->eop_timer = -1; |
||
657 | anton | 514 | } |
515 | } |
||
516 | } |
||
517 | break; |
||
518 | } |
||
2 | pj | 519 | |
657 | anton | 520 | if (lev->flags & RM_ENABLE_WCET_CHECK) { |
521 | proc_table[p].control |= CONTROL_CAP; |
||
522 | } |
||
761 | anton | 523 | jet_update_endcycle(); /* Update the Jet data... */ |
657 | anton | 524 | proc_table[p].avail_time = proc_table[p].wcet; |
761 | anton | 525 | TRACER_LOGEVENT(FTrace_EVT_task_end_cycle, |
526 | (unsigned short int)proc_table[p].context,(unsigned int)l); |
||
527 | |||
38 | pj | 528 | return 0; |
657 | anton | 529 | |
2 | pj | 530 | } |
531 | |||
761 | anton | 532 | /* End the task and free the resources at the end of the period */ |
38 | pj | 533 | static void RM_public_end(LEVEL l, PID p) |
2 | pj | 534 | { |
657 | anton | 535 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
761 | anton | 536 | RM_task_des *td = &lev->tvec[p]; |
2 | pj | 537 | |
761 | anton | 538 | if (!(td->flags & RM_FLAG_SPOR_LATE)) { |
657 | anton | 539 | /* remove the deadline timer (if any) */ |
761 | anton | 540 | if (td->dl_timer != -1) { |
541 | kern_event_delete(td->dl_timer); |
||
542 | td->dl_timer = -1; |
||
657 | anton | 543 | } |
544 | proc_table[p].status = RM_ZOMBIE; |
||
545 | } else { |
||
546 | /* no endperiod timer will be fired, free the task now! */ |
||
547 | proc_table[p].status = FREE; |
||
548 | iq_insertfirst(p,&freedesc); |
||
549 | /* free the allocated bandwidth */ |
||
761 | anton | 550 | lev->U -= (MAX_BANDWIDTH/td->rdeadline) * proc_table[p].wcet; |
657 | anton | 551 | } |
2 | pj | 552 | } |
553 | |||
761 | anton | 554 | /**** Private generic kernel interface functions (guest calls) ****/ |
555 | |||
556 | /* Insert a guest task */ |
||
38 | pj | 557 | static void RM_private_insert(LEVEL l, PID p, TASK_MODEL *m) |
2 | pj | 558 | { |
559 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
761 | anton | 560 | RM_task_des *td = &lev->tvec[p]; |
38 | pj | 561 | JOB_TASK_MODEL *job; |
2 | pj | 562 | |
38 | pj | 563 | if (m->pclass != JOB_PCLASS || (m->level != 0 && m->level != l) ) { |
564 | kern_raise(XINVALID_TASK, p); |
||
565 | return; |
||
566 | } |
||
2 | pj | 567 | |
38 | pj | 568 | job = (JOB_TASK_MODEL *)m; |
2 | pj | 569 | |
657 | anton | 570 | /* Insert task in the correct position */ |
571 | *iq_query_timespec(p, &lev->ready) = job->deadline; |
||
761 | anton | 572 | *iq_query_priority(p, &lev->ready) = job->period; |
573 | /* THIS IS QUESTIONABLE!! relative deadline? */ |
||
38 | pj | 574 | iq_priority_insert(p,&lev->ready); |
575 | proc_table[p].status = RM_READY; |
||
657 | anton | 576 | |
761 | anton | 577 | td->dl_timer = -1; |
38 | pj | 578 | |
761 | anton | 579 | td->period = job->period; |
657 | anton | 580 | |
581 | if (!job->noraiseexc) { |
||
761 | anton | 582 | td->dl_timer = kern_event_post(iq_query_timespec(p, &lev->ready), |
657 | anton | 583 | RM_timer_guest_deadline,(void *)p); |
38 | pj | 584 | } |
2 | pj | 585 | } |
586 | |||
761 | anton | 587 | /* Dispatch a guest task */ |
38 | pj | 588 | static void RM_private_dispatch(LEVEL l, PID p, int nostop) |
2 | pj | 589 | { |
590 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
591 | |||
592 | /* the task state is set to EXE by the scheduler() |
||
593 | we extract the task from the ready queue |
||
594 | NB: we can't assume that p is the first task in the queue!!! */ |
||
29 | pj | 595 | iq_extract(p, &lev->ready); |
2 | pj | 596 | } |
597 | |||
761 | anton | 598 | /* Called when a guest task is preempted/out of budget */ |
38 | pj | 599 | static void RM_private_epilogue(LEVEL l, PID p) |
2 | pj | 600 | { |
601 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
602 | |||
603 | /* the task has been preempted. it returns into the ready queue... */ |
||
29 | pj | 604 | iq_priority_insert(p,&lev->ready); |
2 | pj | 605 | proc_table[p].status = RM_READY; |
606 | } |
||
607 | |||
761 | anton | 608 | /* Extract a guest task */ |
38 | pj | 609 | static void RM_private_extract(LEVEL l, PID p) |
2 | pj | 610 | { |
611 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
761 | anton | 612 | RM_task_des *td = &lev->tvec[p]; |
2 | pj | 613 | |
614 | if (proc_table[p].status == RM_READY) |
||
29 | pj | 615 | iq_extract(p, &lev->ready); |
2 | pj | 616 | |
617 | /* we remove the deadline timer, because the slice is finished */ |
||
761 | anton | 618 | if (td->dl_timer != -1) { |
619 | kern_event_delete(td->dl_timer); |
||
620 | td->dl_timer = -1; |
||
2 | pj | 621 | } |
622 | |||
623 | } |
||
624 | |||
625 | |||
761 | anton | 626 | /**** Level registration function ****/ |
657 | anton | 627 | |
38 | pj | 628 | LEVEL RM_register_level(int flags) |
2 | pj | 629 | { |
630 | LEVEL l; /* the level that we register */ |
||
631 | RM_level_des *lev; /* for readableness only */ |
||
632 | |||
633 | printk("RM_register_level\n"); |
||
634 | |||
635 | /* request an entry in the level_table */ |
||
38 | pj | 636 | l = level_alloc_descriptor(sizeof(RM_level_des)); |
2 | pj | 637 | |
38 | pj | 638 | lev = (RM_level_des *)level_table[l]; |
2 | pj | 639 | |
640 | /* fill the standard descriptor */ |
||
38 | pj | 641 | lev->l.private_insert = RM_private_insert; |
642 | lev->l.private_extract = RM_private_extract; |
||
643 | lev->l.private_dispatch = RM_private_dispatch; |
||
644 | lev->l.private_epilogue = RM_private_epilogue; |
||
2 | pj | 645 | |
38 | pj | 646 | lev->l.public_scheduler = RM_public_scheduler; |
2 | pj | 647 | if (flags & RM_ENABLE_GUARANTEE) |
38 | pj | 648 | lev->l.public_guarantee = RM_public_guarantee; |
2 | pj | 649 | else |
38 | pj | 650 | lev->l.public_guarantee = NULL; |
2 | pj | 651 | |
38 | pj | 652 | lev->l.public_create = RM_public_create; |
653 | lev->l.public_detach = RM_public_detach; |
||
654 | lev->l.public_end = RM_public_end; |
||
655 | lev->l.public_dispatch = RM_public_dispatch; |
||
656 | lev->l.public_epilogue = RM_public_epilogue; |
||
657 | lev->l.public_activate = RM_public_activate; |
||
658 | lev->l.public_unblock = RM_public_unblock; |
||
659 | lev->l.public_block = RM_public_block; |
||
660 | lev->l.public_message = RM_public_message; |
||
2 | pj | 661 | |
761 | anton | 662 | iq_init(&lev->ready, &freedesc, 0); |
2 | pj | 663 | |
159 | pj | 664 | lev->flags = flags; |
761 | anton | 665 | if (lev->flags & RM_ENABLE_WCET_EXCEPTION) { |
666 | lev->flags |= RM_ENABLE_WCET_CHECK; |
||
667 | } |
||
668 | if (lev->flags & RM_ENABLE_DL_EXCEPTION) { |
||
669 | lev->flags |= RM_ENABLE_DL_CHECK; |
||
670 | } |
||
38 | pj | 671 | |
761 | anton | 672 | lev->U = 0; |
673 | |||
38 | pj | 674 | return l; |
2 | pj | 675 | } |
676 | |||
761 | anton | 677 | |
678 | /**** Public utility functions ****/ |
||
679 | |||
680 | /* Get the bandwidth used by the level */ |
||
2 | pj | 681 | bandwidth_t RM_usedbandwidth(LEVEL l) |
682 | { |
||
683 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
38 | pj | 684 | |
685 | return lev->U; |
||
2 | pj | 686 | } |
687 | |||
761 | anton | 688 | /* Get the number of missed deadlines for a task */ |
689 | int RM_get_dl_miss(PID p) |
||
657 | anton | 690 | { |
691 | LEVEL l = proc_table[p].task_level; |
||
692 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
761 | anton | 693 | RM_task_des *td = &lev->tvec[p]; |
694 | |||
695 | return td->dl_miss; |
||
657 | anton | 696 | } |
697 | |||
761 | anton | 698 | /* Get the number of execution overruns for a task */ |
699 | int RM_get_wcet_miss(PID p) |
||
657 | anton | 700 | { |
701 | LEVEL l = proc_table[p].task_level; |
||
702 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
761 | anton | 703 | RM_task_des *td = &lev->tvec[p]; |
657 | anton | 704 | |
761 | anton | 705 | return td->wcet_miss; |
657 | anton | 706 | } |
707 | |||
761 | anton | 708 | /* Get the number of skipped activations for a task */ |
709 | int RM_get_act_miss(PID p) |
||
657 | anton | 710 | { |
711 | LEVEL l = proc_table[p].task_level; |
||
712 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
||
761 | anton | 713 | RM_task_des *td = &lev->tvec[p]; |
714 | |||
715 | return td->act_miss; |
||
657 | anton | 716 | } |
717 | |||
761 | anton | 718 | /* Get the current number of queued activations for a task */ |
719 | int RM_get_nact(PID p) |
||
657 | anton | 720 | { |
721 | LEVEL l = proc_table[p].task_level; |
||
761 | anton | 722 | |
657 | anton | 723 | RM_level_des *lev = (RM_level_des *)(level_table[l]); |
761 | anton | 724 | RM_task_des *td = &lev->tvec[p]; |
657 | anton | 725 | |
761 | anton | 726 | return td->nact; |
657 | anton | 727 | } |
728 |