Rev 837 | 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 | ------------ |
||
849 | giacomo | 23 | CVS : $Id: edf.c,v 1.18 2004-09-14 09:10:10 giacomo Exp $ |
2 | pj | 24 | |
25 | File: $File$ |
||
849 | giacomo | 26 | Revision: $Revision: 1.18 $ |
27 | Last update: $Date: 2004-09-14 09:10:10 $ |
||
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 | |||
837 | 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 | |||
837 | giacomo | 170 | td->dl_timer = -1; |
171 | |||
759 | anton | 172 | TRACER_LOGEVENT(FTrace_EVT_task_deadline_miss, |
173 | (unsigned short int)proc_table[p].context,0); |
||
174 | |||
657 | anton | 175 | if (lev->flags & EDF_ENABLE_DL_EXCEPTION) { |
176 | kern_raise(XDEADLINE_MISS,p); |
||
177 | } else { |
||
759 | anton | 178 | td->dl_miss++; |
657 | anton | 179 | } |
180 | } |
||
181 | |||
759 | anton | 182 | /* This timer event handler is called after waiting for an offset */ |
183 | static void EDF_timer_offset(void *par) |
||
184 | { |
||
185 | PID p = (PID) par; |
||
186 | EDF_level_des *lev; |
||
187 | lev = (EDF_level_des *)level_table[proc_table[p].task_level]; |
||
188 | /* release the task now */ |
||
189 | EDF_intern_release(p, lev); |
||
190 | } |
||
657 | anton | 191 | |
759 | anton | 192 | /* This function is called when a guest task misses its deadline */ |
193 | static void EDF_timer_guest_deadline(void *par) |
||
194 | { |
||
195 | PID p = (PID) par; |
||
837 | giacomo | 196 | EDF_level_des *lev = (EDF_level_des *)level_table[proc_table[p].task_level]; |
197 | EDF_task_des *td = &lev->tvec[p]; |
||
198 | |||
199 | td->dl_timer = -1; |
||
200 | |||
759 | anton | 201 | TRACER_LOGEVENT(FTrace_EVT_task_deadline_miss, |
202 | (unsigned short int)proc_table[p].context,0); |
||
203 | kern_raise(XDEADLINE_MISS,p); |
||
204 | } |
||
657 | anton | 205 | |
759 | anton | 206 | |
207 | /**** Internal utility functions ****/ |
||
208 | |||
209 | /* Release (or queue) a task, post deadline and endperiod timers */ |
||
657 | anton | 210 | static void EDF_intern_release(PID p, EDF_level_des *lev) |
211 | { |
||
212 | struct timespec temp; |
||
759 | anton | 213 | EDF_task_des *td = &lev->tvec[p]; |
657 | anton | 214 | |
215 | /* post deadline timer */ |
||
216 | if (lev->flags & EDF_ENABLE_DL_CHECK) { |
||
759 | anton | 217 | temp = td->release; |
218 | ADDUSEC2TIMESPEC(td->rdeadline, &temp); |
||
837 | giacomo | 219 | if (td->dl_timer != -1) { |
220 | kern_event_delete(td->dl_timer); |
||
221 | td->dl_timer = -1; |
||
222 | } |
||
759 | anton | 223 | td->dl_timer = kern_event_post(&temp,EDF_timer_deadline,(void *)p); |
657 | anton | 224 | } |
225 | |||
226 | /* release or queue next job */ |
||
227 | if (proc_table[p].status == EDF_IDLE) { |
||
228 | /* assign deadline, insert task in the ready queue */ |
||
229 | proc_table[p].status = EDF_READY; |
||
759 | anton | 230 | *iq_query_timespec(p,&lev->ready) = td->adeadline; |
657 | anton | 231 | iq_timespec_insert(p,&lev->ready); |
232 | #ifdef EDF_DEBUG |
||
233 | edf_printf("At %s: releasing %s with deadline %s\n", pnow(), |
||
759 | anton | 234 | proc_table[p].name, ptime1(&td->adeadline)); |
657 | anton | 235 | #endif |
236 | /* increase assigned deadline */ |
||
759 | anton | 237 | ADDUSEC2TIMESPEC(td->period, &td->adeadline); |
657 | anton | 238 | /* reschedule */ |
239 | event_need_reschedule(); |
||
240 | } else { |
||
241 | /* queue */ |
||
759 | anton | 242 | td->nact++; |
657 | anton | 243 | } |
244 | |||
245 | /* increase release time */ |
||
759 | anton | 246 | ADDUSEC2TIMESPEC(td->period, &td->release); |
657 | anton | 247 | /* post end of period timer */ |
849 | giacomo | 248 | if (td->eop_timer != -1) { |
249 | kern_event_delete(td->eop_timer); |
||
250 | td->eop_timer = -1; |
||
251 | } |
||
252 | |||
759 | anton | 253 | td->eop_timer = kern_event_post(&td->release, EDF_timer_endperiod,(void *)p); |
657 | anton | 254 | |
759 | anton | 255 | TRACER_LOGEVENT(FTrace_EVT_task_timer, |
256 | (unsigned short int)proc_table[p].context, |
||
257 | (unsigned int)proc_table[p].task_level); |
||
657 | anton | 258 | } |
259 | |||
260 | |||
759 | anton | 261 | /**** Public generic kernel interface functions ****/ |
657 | anton | 262 | |
759 | anton | 263 | /* Returns the first task in the ready queue */ |
38 | pj | 264 | static PID EDF_public_scheduler(LEVEL l) |
2 | pj | 265 | { |
266 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
29 | pj | 267 | return iq_query_first(&lev->ready); |
2 | pj | 268 | } |
269 | |||
759 | anton | 270 | /* Checks and decreases the available system bandwidth */ |
38 | pj | 271 | static int EDF_public_guarantee(LEVEL l, bandwidth_t *freebandwidth) |
2 | pj | 272 | { |
273 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
274 | |||
159 | pj | 275 | if (*freebandwidth >= lev->U) { |
276 | *freebandwidth -= lev->U; |
||
277 | return 1; |
||
2 | pj | 278 | } |
279 | else |
||
159 | pj | 280 | return 0; |
2 | pj | 281 | } |
282 | |||
759 | anton | 283 | /* Called by task_create: Checks task model and creates a task */ |
38 | pj | 284 | static int EDF_public_create(LEVEL l, PID p, TASK_MODEL *m) |
2 | pj | 285 | { |
286 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
759 | anton | 287 | EDF_task_des *td = &lev->tvec[p]; |
38 | pj | 288 | HARD_TASK_MODEL *h; |
2 | pj | 289 | |
38 | pj | 290 | if (m->pclass != HARD_PCLASS) return -1; |
291 | if (m->level != 0 && m->level != l) return -1; |
||
292 | h = (HARD_TASK_MODEL *)m; |
||
293 | if (!h->wcet || !h->mit) return -1; |
||
657 | anton | 294 | if (h->drel > h->mit) return -1; /* only D <= T supported */ |
159 | pj | 295 | |
657 | anton | 296 | if (!h->drel) { |
759 | anton | 297 | td->rdeadline = h->mit; |
657 | anton | 298 | } else { |
759 | anton | 299 | td->rdeadline = h->drel; |
657 | anton | 300 | } |
301 | |||
159 | pj | 302 | /* check the free bandwidth... */ |
303 | if (lev->flags & EDF_ENABLE_GUARANTEE) { |
||
304 | bandwidth_t b; |
||
759 | anton | 305 | b = (MAX_BANDWIDTH / td->rdeadline) * h->wcet; |
159 | pj | 306 | |
307 | /* really update lev->U, checking an overflow... */ |
||
657 | anton | 308 | if (MAX_BANDWIDTH - lev->U > b) { |
159 | pj | 309 | lev->U += b; |
657 | anton | 310 | } else { |
159 | pj | 311 | return -1; |
657 | anton | 312 | } |
159 | pj | 313 | } |
314 | |||
759 | anton | 315 | td->flags = 0; |
316 | if (h->periodicity == APERIODIC) { |
||
317 | td->flags |= EDF_FLAG_SPORADIC; |
||
657 | anton | 318 | } |
759 | anton | 319 | td->period = h->mit; |
320 | if (td->rdeadline == td->period) { |
||
321 | /* Ensure that D <= T-eps to make dl_timer trigger before eop_timer */ |
||
322 | td->rdeadline = td->period - 1; |
||
657 | anton | 323 | } |
759 | anton | 324 | td->offset = h->offset; |
325 | td->dl_timer = -1; |
||
326 | td->eop_timer = -1; |
||
327 | td->dl_miss = 0; |
||
328 | td->wcet_miss = 0; |
||
329 | td->act_miss = 0; |
||
330 | td->nact = 0; |
||
2 | pj | 331 | |
332 | /* Enable wcet check */ |
||
333 | if (lev->flags & EDF_ENABLE_WCET_CHECK) { |
||
334 | proc_table[p].avail_time = h->wcet; |
||
335 | proc_table[p].wcet = h->wcet; |
||
657 | anton | 336 | proc_table[p].control |= CONTROL_CAP; /* turn on measurement */ |
2 | pj | 337 | } |
338 | |||
339 | return 0; /* OK, also if the task cannot be guaranteed... */ |
||
340 | } |
||
341 | |||
759 | anton | 342 | /* Reclaim the bandwidth used by the task */ |
38 | pj | 343 | static void EDF_public_detach(LEVEL l, PID p) |
2 | pj | 344 | { |
345 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
759 | anton | 346 | EDF_task_des *td = &lev->tvec[p]; |
2 | pj | 347 | |
159 | pj | 348 | if (lev->flags & EDF_ENABLE_GUARANTEE) { |
759 | anton | 349 | lev->U -= (MAX_BANDWIDTH / td->rdeadline) * proc_table[p].wcet; |
159 | pj | 350 | } |
2 | pj | 351 | } |
352 | |||
759 | anton | 353 | /* Extracts the running task from the ready queue */ |
38 | pj | 354 | static void EDF_public_dispatch(LEVEL l, PID p, int nostop) |
2 | pj | 355 | { |
356 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
29 | pj | 357 | iq_extract(p, &lev->ready); |
2 | pj | 358 | } |
359 | |||
759 | anton | 360 | /* Called when the task is preempted or when its budget is exhausted */ |
38 | pj | 361 | static void EDF_public_epilogue(LEVEL l, PID p) |
2 | pj | 362 | { |
363 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
759 | anton | 364 | EDF_task_des *td = &lev->tvec[p]; |
2 | pj | 365 | |
366 | /* check if the wcet is finished... */ |
||
657 | anton | 367 | if (lev->flags & EDF_ENABLE_WCET_CHECK) { |
368 | if (proc_table[p].avail_time <= 0) { |
||
759 | anton | 369 | TRACER_LOGEVENT(FTrace_EVT_task_wcet_violation, |
370 | (unsigned short int)proc_table[p].context,0); |
||
657 | anton | 371 | if (lev->flags & EDF_ENABLE_WCET_EXCEPTION) { |
372 | kern_raise(XWCET_VIOLATION,p); |
||
373 | } else { |
||
374 | proc_table[p].control &= ~CONTROL_CAP; |
||
759 | anton | 375 | td->wcet_miss++; |
657 | anton | 376 | } |
377 | } |
||
2 | pj | 378 | } |
657 | anton | 379 | |
380 | /* the task returns to the ready queue */ |
||
381 | iq_timespec_insert(p,&lev->ready); |
||
382 | proc_table[p].status = EDF_READY; |
||
383 | |||
2 | pj | 384 | } |
385 | |||
759 | anton | 386 | /* Called by task_activate or group_activate: Activates the task at time t */ |
657 | anton | 387 | static void EDF_public_activate(LEVEL l, PID p, struct timespec *t) |
2 | pj | 388 | { |
657 | anton | 389 | struct timespec clocktime; |
2 | pj | 390 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
759 | anton | 391 | EDF_task_des *td = &lev->tvec[p]; |
2 | pj | 392 | |
657 | anton | 393 | kern_gettime(&clocktime); |
38 | pj | 394 | |
657 | anton | 395 | /* check if we are not in the SLEEP state */ |
396 | if (proc_table[p].status != SLEEP) { |
||
397 | if (lev->flags & EDF_ENABLE_ACT_EXCEPTION) { |
||
398 | /* too frequent or wrongful activation: raise exception */ |
||
399 | kern_raise(XACTIVATION,p); |
||
400 | } else { |
||
401 | /* skip the sporadic job, but increase a counter */ |
||
402 | #ifdef EDF_DEBUG |
||
759 | anton | 403 | edf_printf("At %s: activation of %s skipped\n", pnow(), |
404 | proc_table[p].name); |
||
657 | anton | 405 | #endif |
759 | anton | 406 | td->act_miss++; |
657 | anton | 407 | } |
212 | giacomo | 408 | return; |
409 | } |
||
410 | |||
657 | anton | 411 | /* set the release time to the activation time + offset */ |
759 | anton | 412 | td->release = *t; |
413 | ADDUSEC2TIMESPEC(td->offset, &td->release); |
||
2 | pj | 414 | |
657 | anton | 415 | /* set the absolute deadline to the activation time + offset + rdeadline */ |
759 | anton | 416 | td->adeadline = td->release; |
417 | ADDUSEC2TIMESPEC(td->rdeadline, &td->adeadline); |
||
2 | pj | 418 | |
759 | anton | 419 | /* Check if release > clocktime. If yes, release it later, |
657 | anton | 420 | otherwise release it now. */ |
2 | pj | 421 | |
657 | anton | 422 | proc_table[p].status = EDF_IDLE; |
2 | pj | 423 | |
759 | anton | 424 | if (TIMESPEC_A_GT_B(&td->release, &clocktime)) { |
425 | /* release later, post an offset timer */ |
||
426 | kern_event_post(&td->release,EDF_timer_offset,(void *)p); |
||
657 | anton | 427 | } else { |
428 | /* release now */ |
||
429 | EDF_intern_release(p, lev); |
||
430 | } |
||
2 | pj | 431 | } |
432 | |||
759 | anton | 433 | /* Reinserts a task that has been blocked into the ready queue */ |
38 | pj | 434 | static void EDF_public_unblock(LEVEL l, PID p) |
2 | pj | 435 | { |
436 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
437 | |||
657 | anton | 438 | /* Insert task in the correct position */ |
2 | pj | 439 | proc_table[p].status = EDF_READY; |
29 | pj | 440 | iq_timespec_insert(p,&lev->ready); |
2 | pj | 441 | } |
442 | |||
759 | anton | 443 | /* Called when a task experiences a synchronization block */ |
38 | pj | 444 | static void EDF_public_block(LEVEL l, PID p) |
2 | pj | 445 | { |
446 | /* Extract the running task from the level |
||
447 | . we have already extract it from the ready queue at the dispatch time. |
||
448 | . the capacity event have to be removed by the generic kernel |
||
449 | . the wcet don't need modification... |
||
450 | . the state of the task is set by the calling function |
||
451 | . the deadline must remain... |
||
452 | |||
453 | So, we do nothing!!! |
||
454 | */ |
||
455 | } |
||
456 | |||
759 | anton | 457 | /* Called by task_endcycle or task_sleep: Ends the current instance */ |
38 | pj | 458 | static int EDF_public_message(LEVEL l, PID p, void *m) |
2 | pj | 459 | { |
460 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
759 | anton | 461 | EDF_task_des *td = &lev->tvec[p]; |
2 | pj | 462 | |
212 | giacomo | 463 | switch((long)(m)) { |
657 | anton | 464 | /* task_endcycle() */ |
465 | case 0: |
||
466 | /* if there are no pending jobs */ |
||
759 | anton | 467 | if (td->nact == 0) { |
657 | anton | 468 | /* remove deadline timer, if any */ |
759 | anton | 469 | if (td->dl_timer != -1) { |
470 | kern_event_delete(td->dl_timer); |
||
471 | td->dl_timer = -1; |
||
657 | anton | 472 | } |
759 | anton | 473 | if (td->flags & EDF_FLAG_SPORADIC) { |
657 | anton | 474 | /* sporadic task */ |
759 | anton | 475 | if (!(td->flags & EDF_FLAG_SPOR_LATE)) { |
657 | anton | 476 | proc_table[p].status = EDF_WAIT; |
477 | } else { |
||
478 | /* it's late, move it directly to SLEEP */ |
||
479 | proc_table[p].status = SLEEP; |
||
759 | anton | 480 | td->flags &= ~EDF_FLAG_SPOR_LATE; |
657 | anton | 481 | } |
482 | } else { |
||
483 | /* periodic task */ |
||
484 | proc_table[p].status = EDF_IDLE; |
||
485 | } |
||
486 | } else { |
||
487 | /* we are late / there are pending jobs */ |
||
759 | anton | 488 | td->nact--; |
657 | anton | 489 | /* compute and assign absolute deadline */ |
759 | anton | 490 | *iq_query_timespec(p,&lev->ready) = td->adeadline; |
657 | anton | 491 | iq_timespec_insert(p,&lev->ready); |
492 | /* increase assigned deadline */ |
||
759 | anton | 493 | ADDUSEC2TIMESPEC(td->period, &td->adeadline); |
657 | anton | 494 | #ifdef EDF_DEBUG |
495 | edf_printf("(Late) At %s: releasing %s with deadline %s\n", |
||
759 | anton | 496 | pnow(),proc_table[p].name,ptime1(&td->adeadline)); |
657 | anton | 497 | #endif |
498 | } |
||
499 | break; |
||
500 | |||
501 | /* task_sleep() */ |
||
502 | case 1: |
||
503 | /* remove deadline timer, if any */ |
||
759 | anton | 504 | if (td->dl_timer != -1) { |
505 | kern_event_delete(td->dl_timer); |
||
506 | td->dl_timer = -1; |
||
657 | anton | 507 | } |
759 | anton | 508 | if (td->flags & EDF_FLAG_SPORADIC) { |
657 | anton | 509 | /* sporadic task */ |
759 | anton | 510 | if (!(td->flags & EDF_FLAG_SPOR_LATE)) { |
212 | giacomo | 511 | proc_table[p].status = EDF_WAIT; |
657 | anton | 512 | } else { |
513 | /* it's late, move it directly to SLEEP */ |
||
514 | proc_table[p].status = SLEEP; |
||
759 | anton | 515 | td->flags &= ~EDF_FLAG_SPOR_LATE; |
657 | anton | 516 | } |
517 | } else { |
||
518 | /* periodic task */ |
||
759 | anton | 519 | if (!(td->nact > 0)) { |
657 | anton | 520 | /* we are on time. go to the EDF_WAIT state */ |
521 | proc_table[p].status = EDF_WAIT; |
||
522 | } else { |
||
523 | /* we are late. delete pending activations and go to SLEEP */ |
||
759 | anton | 524 | td->nact = 0; |
657 | anton | 525 | proc_table[p].status = SLEEP; |
526 | /* remove end of period timer */ |
||
759 | anton | 527 | if (td->eop_timer != -1) { |
528 | kern_event_delete(td->eop_timer); |
||
529 | td->eop_timer = -1; |
||
657 | anton | 530 | } |
531 | } |
||
532 | } |
||
533 | break; |
||
534 | } |
||
212 | giacomo | 535 | |
657 | anton | 536 | if (lev->flags & EDF_ENABLE_WCET_CHECK) { |
537 | proc_table[p].control |= CONTROL_CAP; |
||
212 | giacomo | 538 | } |
691 | anton | 539 | jet_update_endcycle(); /* Update the Jet data... */ |
657 | anton | 540 | proc_table[p].avail_time = proc_table[p].wcet; |
759 | anton | 541 | TRACER_LOGEVENT(FTrace_EVT_task_end_cycle, |
542 | (unsigned short int)proc_table[p].context,(unsigned int)l); |
||
657 | anton | 543 | |
38 | pj | 544 | return 0; |
212 | giacomo | 545 | |
2 | pj | 546 | } |
547 | |||
759 | anton | 548 | /* End the task and free the resources at the end of the period */ |
38 | pj | 549 | static void EDF_public_end(LEVEL l, PID p) |
2 | pj | 550 | { |
657 | anton | 551 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
759 | anton | 552 | EDF_task_des *td = &lev->tvec[p]; |
2 | pj | 553 | |
759 | anton | 554 | if (!(td->flags & EDF_FLAG_SPOR_LATE)) { |
657 | anton | 555 | /* remove the deadline timer (if any) */ |
759 | anton | 556 | if (td->dl_timer != -1) { |
557 | kern_event_delete(td->dl_timer); |
||
558 | td->dl_timer = -1; |
||
657 | anton | 559 | } |
560 | proc_table[p].status = EDF_ZOMBIE; |
||
561 | } else { |
||
562 | /* no endperiod timer will be fired, free the task now! */ |
||
563 | proc_table[p].status = FREE; |
||
564 | iq_insertfirst(p,&freedesc); |
||
565 | /* free the allocated bandwidth */ |
||
759 | anton | 566 | lev->U -= (MAX_BANDWIDTH/td->rdeadline) * proc_table[p].wcet; |
657 | anton | 567 | } |
2 | pj | 568 | } |
569 | |||
759 | anton | 570 | /**** Private generic kernel interface functions (guest calls) ****/ |
571 | |||
572 | /* Insert a guest task */ |
||
38 | pj | 573 | static void EDF_private_insert(LEVEL l, PID p, TASK_MODEL *m) |
2 | pj | 574 | { |
575 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
759 | anton | 576 | EDF_task_des *td = &lev->tvec[p]; |
38 | pj | 577 | JOB_TASK_MODEL *job; |
2 | pj | 578 | |
38 | pj | 579 | if (m->pclass != JOB_PCLASS || (m->level != 0 && m->level != l) ) { |
580 | kern_raise(XINVALID_TASK, p); |
||
581 | return; |
||
582 | } |
||
2 | pj | 583 | |
38 | pj | 584 | job = (JOB_TASK_MODEL *)m; |
2 | pj | 585 | |
38 | pj | 586 | /* Insert task in the correct position */ |
29 | pj | 587 | *iq_query_timespec(p, &lev->ready) = job->deadline; |
38 | pj | 588 | iq_timespec_insert(p,&lev->ready); |
589 | proc_table[p].status = EDF_READY; |
||
2 | pj | 590 | |
837 | giacomo | 591 | if (td->dl_timer != -1) { |
592 | kern_event_delete(td->dl_timer); |
||
593 | td->dl_timer = -1; |
||
594 | } |
||
2 | pj | 595 | |
759 | anton | 596 | td->period = job->period; |
38 | pj | 597 | |
657 | anton | 598 | if (!job->noraiseexc) { |
759 | anton | 599 | td->dl_timer = kern_event_post(iq_query_timespec(p, &lev->ready), |
657 | anton | 600 | EDF_timer_guest_deadline,(void *)p); |
38 | pj | 601 | } |
2 | pj | 602 | } |
603 | |||
759 | anton | 604 | /* Dispatch a guest task */ |
38 | pj | 605 | static void EDF_private_dispatch(LEVEL l, PID p, int nostop) |
2 | pj | 606 | { |
607 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
608 | |||
609 | /* the task state is set to EXE by the scheduler() |
||
610 | we extract the task from the ready queue |
||
611 | NB: we can't assume that p is the first task in the queue!!! */ |
||
29 | pj | 612 | iq_extract(p, &lev->ready); |
2 | pj | 613 | } |
614 | |||
759 | anton | 615 | /* Called when a guest task is preempted/out of budget */ |
38 | pj | 616 | static void EDF_private_epilogue(LEVEL l, PID p) |
2 | pj | 617 | { |
618 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
619 | |||
620 | /* the task has been preempted. it returns into the ready queue... */ |
||
29 | pj | 621 | iq_timespec_insert(p,&lev->ready); |
2 | pj | 622 | proc_table[p].status = EDF_READY; |
623 | } |
||
624 | |||
759 | anton | 625 | /* Extract a guest task */ |
38 | pj | 626 | static void EDF_private_extract(LEVEL l, PID p) |
2 | pj | 627 | { |
628 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
759 | anton | 629 | EDF_task_des *td = &lev->tvec[p]; |
2 | pj | 630 | |
631 | if (proc_table[p].status == EDF_READY) |
||
29 | pj | 632 | iq_extract(p, &lev->ready); |
2 | pj | 633 | |
634 | /* we remove the deadline timer, because the slice is finished */ |
||
759 | anton | 635 | if (td->dl_timer != -1) { |
636 | kern_event_delete(td->dl_timer); |
||
637 | td->dl_timer = -1; |
||
2 | pj | 638 | } |
639 | |||
640 | } |
||
641 | |||
642 | |||
759 | anton | 643 | /**** Level registration function ****/ |
644 | |||
38 | pj | 645 | LEVEL EDF_register_level(int flags) |
2 | pj | 646 | { |
647 | LEVEL l; /* the level that we register */ |
||
648 | EDF_level_des *lev; /* for readableness only */ |
||
649 | |||
650 | printk("EDF_register_level\n"); |
||
651 | |||
652 | /* request an entry in the level_table */ |
||
38 | pj | 653 | l = level_alloc_descriptor(sizeof(EDF_level_des)); |
2 | pj | 654 | |
38 | pj | 655 | lev = (EDF_level_des *)level_table[l]; |
2 | pj | 656 | |
657 | /* fill the standard descriptor */ |
||
38 | pj | 658 | lev->l.private_insert = EDF_private_insert; |
659 | lev->l.private_extract = EDF_private_extract; |
||
660 | lev->l.private_dispatch = EDF_private_dispatch; |
||
661 | lev->l.private_epilogue = EDF_private_epilogue; |
||
2 | pj | 662 | |
38 | pj | 663 | lev->l.public_scheduler = EDF_public_scheduler; |
2 | pj | 664 | if (flags & EDF_ENABLE_GUARANTEE) |
38 | pj | 665 | lev->l.public_guarantee = EDF_public_guarantee; |
2 | pj | 666 | else |
38 | pj | 667 | lev->l.public_guarantee = NULL; |
2 | pj | 668 | |
38 | pj | 669 | lev->l.public_create = EDF_public_create; |
670 | lev->l.public_detach = EDF_public_detach; |
||
671 | lev->l.public_end = EDF_public_end; |
||
672 | lev->l.public_dispatch = EDF_public_dispatch; |
||
673 | lev->l.public_epilogue = EDF_public_epilogue; |
||
674 | lev->l.public_activate = EDF_public_activate; |
||
675 | lev->l.public_unblock = EDF_public_unblock; |
||
676 | lev->l.public_block = EDF_public_block; |
||
677 | lev->l.public_message = EDF_public_message; |
||
2 | pj | 678 | |
759 | anton | 679 | iq_init(&lev->ready, &freedesc, 0); |
2 | pj | 680 | |
159 | pj | 681 | lev->flags = flags; |
759 | anton | 682 | if (lev->flags & EDF_ENABLE_WCET_EXCEPTION) { |
683 | lev->flags |= EDF_ENABLE_WCET_CHECK; |
||
684 | } |
||
685 | if (lev->flags & EDF_ENABLE_DL_EXCEPTION) { |
||
686 | lev->flags |= EDF_ENABLE_DL_CHECK; |
||
687 | } |
||
38 | pj | 688 | |
759 | anton | 689 | lev->U = 0; |
690 | |||
38 | pj | 691 | return l; |
2 | pj | 692 | } |
693 | |||
759 | anton | 694 | |
695 | /**** Public utility functions ****/ |
||
696 | |||
697 | /* Get the bandwidth used by the level */ |
||
2 | pj | 698 | bandwidth_t EDF_usedbandwidth(LEVEL l) |
699 | { |
||
700 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
38 | pj | 701 | |
702 | return lev->U; |
||
2 | pj | 703 | } |
704 | |||
759 | anton | 705 | /* Get the number of missed deadlines for a task */ |
706 | int EDF_get_dl_miss(PID p) |
||
657 | anton | 707 | { |
708 | LEVEL l = proc_table[p].task_level; |
||
709 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
759 | anton | 710 | EDF_task_des *td = &lev->tvec[p]; |
711 | |||
712 | return td->dl_miss; |
||
657 | anton | 713 | } |
714 | |||
759 | anton | 715 | /* Get the number of execution overruns for a task */ |
716 | int EDF_get_wcet_miss(PID p) |
||
657 | anton | 717 | { |
718 | LEVEL l = proc_table[p].task_level; |
||
719 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
759 | anton | 720 | EDF_task_des *td = &lev->tvec[p]; |
657 | anton | 721 | |
759 | anton | 722 | return td->wcet_miss; |
657 | anton | 723 | } |
724 | |||
759 | anton | 725 | /* Get the number of skipped activations for a task */ |
726 | int EDF_get_act_miss(PID p) |
||
657 | anton | 727 | { |
728 | LEVEL l = proc_table[p].task_level; |
||
729 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
||
759 | anton | 730 | EDF_task_des *td = &lev->tvec[p]; |
731 | |||
732 | return td->act_miss; |
||
657 | anton | 733 | } |
734 | |||
759 | anton | 735 | /* Get the current number of queued activations for a task */ |
736 | int EDF_get_nact(PID p) |
||
657 | anton | 737 | { |
738 | LEVEL l = proc_table[p].task_level; |
||
759 | anton | 739 | |
657 | anton | 740 | EDF_level_des *lev = (EDF_level_des *)(level_table[l]); |
759 | anton | 741 | EDF_task_des *td = &lev->tvec[p]; |
657 | anton | 742 | |
759 | anton | 743 | return td->nact; |
657 | anton | 744 | } |
745 |