Rev 691 | 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 | * |
||
759 | 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> |
||
759 | 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 | ------------ |
||
759 | anton | 23 | CVS : $Id: edf.c,v 1.16 2004-06-21 11:22:05 anton Exp $ |
2 | pj | 24 | |
25 | File: $File$ |
||
759 | anton | 26 | Revision: $Revision: 1.16 $ |
27 | Last update: $Date: 2004-06-21 11:22:05 $ |
||
2 | pj | 28 | ------------ |
29 | |||
30 | This file contains the scheduling module EDF (Earliest Deadline First) |
||
31 | |||
32 | Read edf.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/edf.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 | |||
240 | giacomo | 65 | //#define EDF_DEBUG |
38 | pj | 66 | #define edf_printf kern_printf |
657 | anton | 67 | #ifdef EDF_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 | |
759 | anton | 87 | /* Statuses used in the level */ |
657 | anton | 88 | #define EDF_READY MODULE_STATUS_BASE /* ready */ |
89 | #define EDF_IDLE MODULE_STATUS_BASE+1 /* idle, waiting for offset/eop */ |
||
90 | #define EDF_WAIT MODULE_STATUS_BASE+2 /* to sleep, waiting for eop */ |
||
759 | anton | 91 | #define EDF_ZOMBIE MODULE_STATUS_BASE+3 /* to free, waiting for eop */ |
2 | pj | 92 | |
759 | anton | 93 | /* Task flags */ |
657 | anton | 94 | #define EDF_FLAG_SPORADIC 1 /* the task is sporadic */ |
95 | #define EDF_FLAG_SPOR_LATE 2 /* sporadic task with period overrun */ |
||
2 | pj | 96 | |
97 | |||
759 | anton | 98 | /* Task descriptor */ |
657 | anton | 99 | typedef struct { |
759 | 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 | } EDF_task_des; |
||
2 | pj | 113 | |
759 | 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 | EDF_task_des tvec[MAX_PROC]; /* vector of task descriptors */ |
||
657 | anton | 122 | } EDF_level_des; |
2 | pj | 123 | |
124 | |||
759 | anton | 125 | /* Module function cross-references */ |
126 | static void EDF_intern_release(PID p, EDF_level_des *lev); |
||
2 | pj | 127 | |
128 | |||
759 | anton | 129 | /**** Timer event handler functions ****/ |
2 | pj | 130 | |
759 | anton | 131 | /* This timer event handler is called at the end of the period */ |
132 | static void EDF_timer_endperiod(void *par) |
||
2 | pj | 133 | { |
134 | PID p = (PID) par; |
||
759 | anton | 135 | EDF_level_des *lev = (EDF_level_des *)level_table[proc_table[p].task_level]; |
136 | EDF_task_des *td = &lev->tvec[p]; |
||
657 | anton | 137 | |
759 | anton | 138 | td->eop_timer = -1; |
2 | pj | 139 | |
759 | anton | 140 | if (proc_table[p].status == EDF_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 == EDF_WAIT) { |
||
150 | proc_table[p].status = SLEEP; |
||
151 | return; |
||
152 | } |
||
153 | |||
154 | if (td->flags & EDF_FLAG_SPORADIC) { |
||
155 | /* the task is sporadic and still busy, mark it as late */ |
||
156 | td->flags |= EDF_FLAG_SPOR_LATE; |
||
157 | } else { |
||
158 | /* the task is periodic, release/queue another instance */ |
||
159 | EDF_intern_release(p, lev); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | /* This timer event handler is called when a task misses its deadline */ |
||
164 | static void EDF_timer_deadline(void *par) |
||
165 | { |
||
166 | PID p = (PID) par; |
||
167 | EDF_level_des *lev = (EDF_level_des *)level_table[proc_table[p].task_level]; |
||
168 | EDF_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 & EDF_ENABLE_DL_EXCEPTION) { |
174 | kern_raise(XDEADLINE_MISS,p); |
||
175 | } else { |
||
759 | anton | 176 | td->dl_miss++; |
657 | anton | 177 | } |
178 | } |
||
179 | |||
759 | anton | 180 | /* This timer event handler is called after waiting for an offset */ |
181 | static void EDF_timer_offset(void *par) |
||
182 | { |
||
183 | PID p = (PID) par; |
||
184 | EDF_level_des *lev; |
||
185 | lev = (EDF_level_des *)level_table[proc_table[p].task_level]; |
||
186 | /* release the task now */ |
||
187 | EDF_intern_release(p, lev); |
||
188 | } |
||
657 | anton | 189 | |
759 | anton | 190 | /* This function is called when a guest task misses its deadline */ |
191 | static void EDF_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 | |
759 | anton | 199 | |
200 | /**** Internal utility functions ****/ |
||
201 | |||
202 | /* Release (or queue) a task, post deadline and endperiod timers */ |
||
657 | anton | 203 | static void EDF_intern_release(PID p, EDF_level_des *lev) |
204 | { |
||
205 | struct timespec temp; |
||
759 | anton | 206 | EDF_task_des *td = &lev->tvec[p]; |
657 | anton | 207 | |
208 | /* post deadline timer */ |
||
209 | if (lev->flags & EDF_ENABLE_DL_CHECK) { |
||
759 | anton | 210 | temp = td->release; |
211 | ADDUSEC2TIMESPEC(td->rdeadline, &temp); |
||
212 | td->dl_timer = kern_event_post(&temp,EDF_timer_deadline,(void *)p); |
||
657 | anton | 213 | } |
214 | |||
215 | /* release or queue next job */ |
||
216 | if (proc_table[p].status == EDF_IDLE) { |
||
217 | /* assign deadline, insert task in the ready queue */ |
||
218 | proc_table[p].status = EDF_READY; |
||
759 | anton | 219 | *iq_query_timespec(p,&lev->ready) = td->adeadline; |
657 | anton | 220 | iq_timespec_insert(p,&lev->ready); |
221 | #ifdef EDF_DEBUG |
||
222 | edf_printf("At %s: releasing %s with deadline %s\n", pnow(), |
||
759 | anton | 223 | proc_table[p].name, ptime1(&td->adeadline)); |
657 | anton | 224 | #endif |
225 | /* increase assigned deadline */ |
||
759 | anton | 226 | ADDUSEC2TIMESPEC(td->period, &td->adeadline); |
657 | anton | 227 | /* reschedule */ |
228 | event_need_reschedule(); |
||
229 | } else { |
||
230 | /* queue */ |
||
759 | anton | 231 | td->nact++; |
657 | anton | 232 | } |
233 | |||
234 | /* increase release time */ |
||
759 | anton | 235 | ADDUSEC2TIMESPEC(td->period, &td->release); |
657 | anton | 236 | /* post end of period timer */ |
759 | anton | 237 | td->eop_timer = kern_event_post(&td->release, EDF_timer_endperiod,(void *)p); |
657 | anton | 238 | |
759 | 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 | |||
759 | anton | 245 | /**** Public generic kernel interface functions ****/ |
657 | anton | 246 | |
759 | anton | 247 | /* Returns the first task in the ready queue */ |
38 | pj | 248 | static PID EDF_public_scheduler(LEVEL l) |
2 | pj | 249 | { |
250 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
29 | pj | 251 | return iq_query_first(&lev->ready); |
2 | pj | 252 | } |
253 | |||
759 | anton | 254 | /* Checks and decreases the available system bandwidth */ |
38 | pj | 255 | static int EDF_public_guarantee(LEVEL l, bandwidth_t *freebandwidth) |
2 | pj | 256 | { |
257 | EDF_level_des *lev = (EDF_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 | |||
759 | anton | 267 | /* Called by task_create: Checks task model and creates a task */ |
38 | pj | 268 | static int EDF_public_create(LEVEL l, PID p, TASK_MODEL *m) |
2 | pj | 269 | { |
270 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
759 | anton | 271 | EDF_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) { |
759 | anton | 281 | td->rdeadline = h->mit; |
657 | anton | 282 | } else { |
759 | anton | 283 | td->rdeadline = h->drel; |
657 | anton | 284 | } |
285 | |||
159 | pj | 286 | /* check the free bandwidth... */ |
287 | if (lev->flags & EDF_ENABLE_GUARANTEE) { |
||
288 | bandwidth_t b; |
||
759 | 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 | |||
759 | anton | 299 | td->flags = 0; |
300 | if (h->periodicity == APERIODIC) { |
||
301 | td->flags |= EDF_FLAG_SPORADIC; |
||
657 | anton | 302 | } |
759 | 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 | } |
759 | 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 & EDF_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 | |||
759 | anton | 326 | /* Reclaim the bandwidth used by the task */ |
38 | pj | 327 | static void EDF_public_detach(LEVEL l, PID p) |
2 | pj | 328 | { |
329 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
759 | anton | 330 | EDF_task_des *td = &lev->tvec[p]; |
2 | pj | 331 | |
159 | pj | 332 | if (lev->flags & EDF_ENABLE_GUARANTEE) { |
759 | anton | 333 | lev->U -= (MAX_BANDWIDTH / td->rdeadline) * proc_table[p].wcet; |
159 | pj | 334 | } |
2 | pj | 335 | } |
336 | |||
759 | anton | 337 | /* Extracts the running task from the ready queue */ |
38 | pj | 338 | static void EDF_public_dispatch(LEVEL l, PID p, int nostop) |
2 | pj | 339 | { |
340 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
29 | pj | 341 | iq_extract(p, &lev->ready); |
2 | pj | 342 | } |
343 | |||
759 | anton | 344 | /* Called when the task is preempted or when its budget is exhausted */ |
38 | pj | 345 | static void EDF_public_epilogue(LEVEL l, PID p) |
2 | pj | 346 | { |
347 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
759 | anton | 348 | EDF_task_des *td = &lev->tvec[p]; |
2 | pj | 349 | |
350 | /* check if the wcet is finished... */ |
||
657 | anton | 351 | if (lev->flags & EDF_ENABLE_WCET_CHECK) { |
352 | if (proc_table[p].avail_time <= 0) { |
||
759 | anton | 353 | TRACER_LOGEVENT(FTrace_EVT_task_wcet_violation, |
354 | (unsigned short int)proc_table[p].context,0); |
||
657 | anton | 355 | if (lev->flags & EDF_ENABLE_WCET_EXCEPTION) { |
356 | kern_raise(XWCET_VIOLATION,p); |
||
357 | } else { |
||
358 | proc_table[p].control &= ~CONTROL_CAP; |
||
759 | 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_timespec_insert(p,&lev->ready); |
||
366 | proc_table[p].status = EDF_READY; |
||
367 | |||
2 | pj | 368 | } |
369 | |||
759 | anton | 370 | /* Called by task_activate or group_activate: Activates the task at time t */ |
657 | anton | 371 | static void EDF_public_activate(LEVEL l, PID p, struct timespec *t) |
2 | pj | 372 | { |
657 | anton | 373 | struct timespec clocktime; |
2 | pj | 374 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
759 | anton | 375 | EDF_task_des *td = &lev->tvec[p]; |
2 | pj | 376 | |
657 | anton | 377 | kern_gettime(&clocktime); |
38 | pj | 378 | |
657 | anton | 379 | /* check if we are not in the SLEEP state */ |
380 | if (proc_table[p].status != SLEEP) { |
||
381 | if (lev->flags & EDF_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 EDF_DEBUG |
||
759 | anton | 387 | edf_printf("At %s: activation of %s skipped\n", pnow(), |
388 | proc_table[p].name); |
||
657 | anton | 389 | #endif |
759 | anton | 390 | td->act_miss++; |
657 | anton | 391 | } |
212 | giacomo | 392 | return; |
393 | } |
||
394 | |||
657 | anton | 395 | /* set the release time to the activation time + offset */ |
759 | anton | 396 | td->release = *t; |
397 | ADDUSEC2TIMESPEC(td->offset, &td->release); |
||
2 | pj | 398 | |
657 | anton | 399 | /* set the absolute deadline to the activation time + offset + rdeadline */ |
759 | anton | 400 | td->adeadline = td->release; |
401 | ADDUSEC2TIMESPEC(td->rdeadline, &td->adeadline); |
||
2 | pj | 402 | |
759 | anton | 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 = EDF_IDLE; |
2 | pj | 407 | |
759 | anton | 408 | if (TIMESPEC_A_GT_B(&td->release, &clocktime)) { |
409 | /* release later, post an offset timer */ |
||
410 | kern_event_post(&td->release,EDF_timer_offset,(void *)p); |
||
657 | anton | 411 | } else { |
412 | /* release now */ |
||
413 | EDF_intern_release(p, lev); |
||
414 | } |
||
2 | pj | 415 | } |
416 | |||
759 | anton | 417 | /* Reinserts a task that has been blocked into the ready queue */ |
38 | pj | 418 | static void EDF_public_unblock(LEVEL l, PID p) |
2 | pj | 419 | { |
420 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
421 | |||
657 | anton | 422 | /* Insert task in the correct position */ |
2 | pj | 423 | proc_table[p].status = EDF_READY; |
29 | pj | 424 | iq_timespec_insert(p,&lev->ready); |
2 | pj | 425 | } |
426 | |||
759 | anton | 427 | /* Called when a task experiences a synchronization block */ |
38 | pj | 428 | static void EDF_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 | |||
759 | anton | 441 | /* Called by task_endcycle or task_sleep: Ends the current instance */ |
38 | pj | 442 | static int EDF_public_message(LEVEL l, PID p, void *m) |
2 | pj | 443 | { |
444 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
759 | anton | 445 | EDF_task_des *td = &lev->tvec[p]; |
2 | pj | 446 | |
212 | giacomo | 447 | switch((long)(m)) { |
657 | anton | 448 | /* task_endcycle() */ |
449 | case 0: |
||
450 | /* if there are no pending jobs */ |
||
759 | anton | 451 | if (td->nact == 0) { |
657 | anton | 452 | /* remove deadline timer, if any */ |
759 | anton | 453 | if (td->dl_timer != -1) { |
454 | kern_event_delete(td->dl_timer); |
||
455 | td->dl_timer = -1; |
||
657 | anton | 456 | } |
759 | anton | 457 | if (td->flags & EDF_FLAG_SPORADIC) { |
657 | anton | 458 | /* sporadic task */ |
759 | anton | 459 | if (!(td->flags & EDF_FLAG_SPOR_LATE)) { |
657 | anton | 460 | proc_table[p].status = EDF_WAIT; |
461 | } else { |
||
462 | /* it's late, move it directly to SLEEP */ |
||
463 | proc_table[p].status = SLEEP; |
||
759 | anton | 464 | td->flags &= ~EDF_FLAG_SPOR_LATE; |
657 | anton | 465 | } |
466 | } else { |
||
467 | /* periodic task */ |
||
468 | proc_table[p].status = EDF_IDLE; |
||
469 | } |
||
470 | } else { |
||
471 | /* we are late / there are pending jobs */ |
||
759 | anton | 472 | td->nact--; |
657 | anton | 473 | /* compute and assign absolute deadline */ |
759 | anton | 474 | *iq_query_timespec(p,&lev->ready) = td->adeadline; |
657 | anton | 475 | iq_timespec_insert(p,&lev->ready); |
476 | /* increase assigned deadline */ |
||
759 | anton | 477 | ADDUSEC2TIMESPEC(td->period, &td->adeadline); |
657 | anton | 478 | #ifdef EDF_DEBUG |
479 | edf_printf("(Late) At %s: releasing %s with deadline %s\n", |
||
759 | anton | 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 */ |
||
759 | anton | 488 | if (td->dl_timer != -1) { |
489 | kern_event_delete(td->dl_timer); |
||
490 | td->dl_timer = -1; |
||
657 | anton | 491 | } |
759 | anton | 492 | if (td->flags & EDF_FLAG_SPORADIC) { |
657 | anton | 493 | /* sporadic task */ |
759 | anton | 494 | if (!(td->flags & EDF_FLAG_SPOR_LATE)) { |
212 | giacomo | 495 | proc_table[p].status = EDF_WAIT; |
657 | anton | 496 | } else { |
497 | /* it's late, move it directly to SLEEP */ |
||
498 | proc_table[p].status = SLEEP; |
||
759 | anton | 499 | td->flags &= ~EDF_FLAG_SPOR_LATE; |
657 | anton | 500 | } |
501 | } else { |
||
502 | /* periodic task */ |
||
759 | anton | 503 | if (!(td->nact > 0)) { |
657 | anton | 504 | /* we are on time. go to the EDF_WAIT state */ |
505 | proc_table[p].status = EDF_WAIT; |
||
506 | } else { |
||
507 | /* we are late. delete pending activations and go to SLEEP */ |
||
759 | anton | 508 | td->nact = 0; |
657 | anton | 509 | proc_table[p].status = SLEEP; |
510 | /* remove end of period timer */ |
||
759 | 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 | } |
||
212 | giacomo | 519 | |
657 | anton | 520 | if (lev->flags & EDF_ENABLE_WCET_CHECK) { |
521 | proc_table[p].control |= CONTROL_CAP; |
||
212 | giacomo | 522 | } |
691 | anton | 523 | jet_update_endcycle(); /* Update the Jet data... */ |
657 | anton | 524 | proc_table[p].avail_time = proc_table[p].wcet; |
759 | anton | 525 | TRACER_LOGEVENT(FTrace_EVT_task_end_cycle, |
526 | (unsigned short int)proc_table[p].context,(unsigned int)l); |
||
657 | anton | 527 | |
38 | pj | 528 | return 0; |
212 | giacomo | 529 | |
2 | pj | 530 | } |
531 | |||
759 | anton | 532 | /* End the task and free the resources at the end of the period */ |
38 | pj | 533 | static void EDF_public_end(LEVEL l, PID p) |
2 | pj | 534 | { |
657 | anton | 535 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
759 | anton | 536 | EDF_task_des *td = &lev->tvec[p]; |
2 | pj | 537 | |
759 | anton | 538 | if (!(td->flags & EDF_FLAG_SPOR_LATE)) { |
657 | anton | 539 | /* remove the deadline timer (if any) */ |
759 | 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 = EDF_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 */ |
||
759 | anton | 550 | lev->U -= (MAX_BANDWIDTH/td->rdeadline) * proc_table[p].wcet; |
657 | anton | 551 | } |
2 | pj | 552 | } |
553 | |||
759 | anton | 554 | /**** Private generic kernel interface functions (guest calls) ****/ |
555 | |||
556 | /* Insert a guest task */ |
||
38 | pj | 557 | static void EDF_private_insert(LEVEL l, PID p, TASK_MODEL *m) |
2 | pj | 558 | { |
559 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
759 | anton | 560 | EDF_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 | |
38 | pj | 570 | /* Insert task in the correct position */ |
29 | pj | 571 | *iq_query_timespec(p, &lev->ready) = job->deadline; |
38 | pj | 572 | iq_timespec_insert(p,&lev->ready); |
573 | proc_table[p].status = EDF_READY; |
||
2 | pj | 574 | |
759 | anton | 575 | td->dl_timer = -1; |
2 | pj | 576 | |
759 | anton | 577 | td->period = job->period; |
38 | pj | 578 | |
657 | anton | 579 | if (!job->noraiseexc) { |
759 | anton | 580 | td->dl_timer = kern_event_post(iq_query_timespec(p, &lev->ready), |
657 | anton | 581 | EDF_timer_guest_deadline,(void *)p); |
38 | pj | 582 | } |
2 | pj | 583 | } |
584 | |||
759 | anton | 585 | /* Dispatch a guest task */ |
38 | pj | 586 | static void EDF_private_dispatch(LEVEL l, PID p, int nostop) |
2 | pj | 587 | { |
588 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
589 | |||
590 | /* the task state is set to EXE by the scheduler() |
||
591 | we extract the task from the ready queue |
||
592 | NB: we can't assume that p is the first task in the queue!!! */ |
||
29 | pj | 593 | iq_extract(p, &lev->ready); |
2 | pj | 594 | } |
595 | |||
759 | anton | 596 | /* Called when a guest task is preempted/out of budget */ |
38 | pj | 597 | static void EDF_private_epilogue(LEVEL l, PID p) |
2 | pj | 598 | { |
599 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
600 | |||
601 | /* the task has been preempted. it returns into the ready queue... */ |
||
29 | pj | 602 | iq_timespec_insert(p,&lev->ready); |
2 | pj | 603 | proc_table[p].status = EDF_READY; |
604 | } |
||
605 | |||
759 | anton | 606 | /* Extract a guest task */ |
38 | pj | 607 | static void EDF_private_extract(LEVEL l, PID p) |
2 | pj | 608 | { |
609 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
759 | anton | 610 | EDF_task_des *td = &lev->tvec[p]; |
2 | pj | 611 | |
612 | if (proc_table[p].status == EDF_READY) |
||
29 | pj | 613 | iq_extract(p, &lev->ready); |
2 | pj | 614 | |
615 | /* we remove the deadline timer, because the slice is finished */ |
||
759 | anton | 616 | if (td->dl_timer != -1) { |
617 | kern_event_delete(td->dl_timer); |
||
618 | td->dl_timer = -1; |
||
2 | pj | 619 | } |
620 | |||
621 | } |
||
622 | |||
623 | |||
759 | anton | 624 | /**** Level registration function ****/ |
625 | |||
38 | pj | 626 | LEVEL EDF_register_level(int flags) |
2 | pj | 627 | { |
628 | LEVEL l; /* the level that we register */ |
||
629 | EDF_level_des *lev; /* for readableness only */ |
||
630 | |||
631 | printk("EDF_register_level\n"); |
||
632 | |||
633 | /* request an entry in the level_table */ |
||
38 | pj | 634 | l = level_alloc_descriptor(sizeof(EDF_level_des)); |
2 | pj | 635 | |
38 | pj | 636 | lev = (EDF_level_des *)level_table[l]; |
2 | pj | 637 | |
638 | /* fill the standard descriptor */ |
||
38 | pj | 639 | lev->l.private_insert = EDF_private_insert; |
640 | lev->l.private_extract = EDF_private_extract; |
||
641 | lev->l.private_dispatch = EDF_private_dispatch; |
||
642 | lev->l.private_epilogue = EDF_private_epilogue; |
||
2 | pj | 643 | |
38 | pj | 644 | lev->l.public_scheduler = EDF_public_scheduler; |
2 | pj | 645 | if (flags & EDF_ENABLE_GUARANTEE) |
38 | pj | 646 | lev->l.public_guarantee = EDF_public_guarantee; |
2 | pj | 647 | else |
38 | pj | 648 | lev->l.public_guarantee = NULL; |
2 | pj | 649 | |
38 | pj | 650 | lev->l.public_create = EDF_public_create; |
651 | lev->l.public_detach = EDF_public_detach; |
||
652 | lev->l.public_end = EDF_public_end; |
||
653 | lev->l.public_dispatch = EDF_public_dispatch; |
||
654 | lev->l.public_epilogue = EDF_public_epilogue; |
||
655 | lev->l.public_activate = EDF_public_activate; |
||
656 | lev->l.public_unblock = EDF_public_unblock; |
||
657 | lev->l.public_block = EDF_public_block; |
||
658 | lev->l.public_message = EDF_public_message; |
||
2 | pj | 659 | |
759 | anton | 660 | iq_init(&lev->ready, &freedesc, 0); |
2 | pj | 661 | |
159 | pj | 662 | lev->flags = flags; |
759 | anton | 663 | if (lev->flags & EDF_ENABLE_WCET_EXCEPTION) { |
664 | lev->flags |= EDF_ENABLE_WCET_CHECK; |
||
665 | } |
||
666 | if (lev->flags & EDF_ENABLE_DL_EXCEPTION) { |
||
667 | lev->flags |= EDF_ENABLE_DL_CHECK; |
||
668 | } |
||
38 | pj | 669 | |
759 | anton | 670 | lev->U = 0; |
671 | |||
38 | pj | 672 | return l; |
2 | pj | 673 | } |
674 | |||
759 | anton | 675 | |
676 | /**** Public utility functions ****/ |
||
677 | |||
678 | /* Get the bandwidth used by the level */ |
||
2 | pj | 679 | bandwidth_t EDF_usedbandwidth(LEVEL l) |
680 | { |
||
681 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
38 | pj | 682 | |
683 | return lev->U; |
||
2 | pj | 684 | } |
685 | |||
759 | anton | 686 | /* Get the number of missed deadlines for a task */ |
687 | int EDF_get_dl_miss(PID p) |
||
657 | anton | 688 | { |
689 | LEVEL l = proc_table[p].task_level; |
||
690 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
759 | anton | 691 | EDF_task_des *td = &lev->tvec[p]; |
692 | |||
693 | return td->dl_miss; |
||
657 | anton | 694 | } |
695 | |||
759 | anton | 696 | /* Get the number of execution overruns for a task */ |
697 | int EDF_get_wcet_miss(PID p) |
||
657 | anton | 698 | { |
699 | LEVEL l = proc_table[p].task_level; |
||
700 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
759 | anton | 701 | EDF_task_des *td = &lev->tvec[p]; |
657 | anton | 702 | |
759 | anton | 703 | return td->wcet_miss; |
657 | anton | 704 | } |
705 | |||
759 | anton | 706 | /* Get the number of skipped activations for a task */ |
707 | int EDF_get_act_miss(PID p) |
||
657 | anton | 708 | { |
709 | LEVEL l = proc_table[p].task_level; |
||
710 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
759 | anton | 711 | EDF_task_des *td = &lev->tvec[p]; |
712 | |||
713 | return td->act_miss; |
||
657 | anton | 714 | } |
715 | |||
759 | anton | 716 | /* Get the current number of queued activations for a task */ |
717 | int EDF_get_nact(PID p) |
||
657 | anton | 718 | { |
719 | LEVEL l = proc_table[p].task_level; |
||
759 | anton | 720 | |
657 | anton | 721 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
759 | anton | 722 | EDF_task_des *td = &lev->tvec[p]; |
657 | anton | 723 | |
759 | anton | 724 | return td->nact; |
657 | anton | 725 | } |
726 |