Rev 988 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
221 | giacomo | 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 | * (see the web pages for full authors list) |
||
11 | * |
||
12 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
13 | * |
||
14 | * http://www.sssup.it |
||
15 | * http://retis.sssup.it |
||
16 | * http://shark.sssup.it |
||
17 | */ |
||
18 | |||
19 | /* |
||
20 | * Copyright (C) 2001 Paolo Gai |
||
21 | * |
||
22 | * This program is free software; you can redistribute it and/or modify |
||
23 | * it under the terms of the GNU General Public License as published by |
||
24 | * the Free Software Foundation; either version 2 of the License, or |
||
25 | * (at your option) any later version. |
||
26 | * |
||
27 | * This program is distributed in the hope that it will be useful, |
||
28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
30 | * GNU General Public License for more details. |
||
31 | * |
||
32 | * You should have received a copy of the GNU General Public License |
||
33 | * along with this program; if not, write to the Free Software |
||
34 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
35 | * |
||
36 | */ |
||
37 | |||
38 | #include "edfstar.h" |
||
1689 | fabio | 39 | #include <arch/stdio.h> |
40 | #include <arch/string.h> |
||
221 | giacomo | 41 | #include <kernel/model.h> |
42 | #include <kernel/descr.h> |
||
43 | #include <kernel/var.h> |
||
44 | #include <kernel/func.h> |
||
45 | |||
783 | giacomo | 46 | #include <tracer.h> |
221 | giacomo | 47 | |
48 | /* for iqueues */ |
||
49 | /* #include "iqueue.h" Now iqueues are the only queue type available |
||
50 | into the kernel */ |
||
51 | #include <kernel/iqueue.h> |
||
52 | |||
53 | /* for BUDGET_TASK_MODEL */ |
||
867 | trimarchi | 54 | #include "fsf_configuration_parameters.h" |
55 | #include "fsf_core.h" |
||
241 | giacomo | 56 | #include "fsf_server.h" |
973 | trimarchi | 57 | #include <posix/posix/comm_message.h> |
221 | giacomo | 58 | |
876 | trimarchi | 59 | |
221 | giacomo | 60 | /* |
61 | * DEBUG stuffs begin |
||
62 | */ |
||
63 | |||
224 | giacomo | 64 | //#define EDFSTAR_DEBUG |
221 | giacomo | 65 | |
66 | #ifdef EDFSTAR_DEBUG |
||
67 | |||
68 | static __inline__ fake_printf(char *fmt, ...) {} |
||
69 | |||
70 | //#define edfstar_printf fake_printf |
||
71 | //#define edfstar_printf2 fake_printf |
||
72 | //#define edfstar_printf3 fake_printf |
||
73 | |||
74 | #define edfstar_printf kern_printf |
||
75 | #define edfstar_printf2 kern_printf |
||
76 | #define edfstar_printf3 kern_printf |
||
77 | #endif |
||
78 | |||
79 | /* |
||
80 | * DEBUG stuffs end |
||
81 | */ |
||
82 | |||
83 | /* Status used in the level */ |
||
84 | #define EDFSTAR_READY MODULE_STATUS_BASE /* - Ready status */ |
||
85 | #define EDFSTAR_IDLE MODULE_STATUS_BASE+4 /* to wait the deadline */ |
||
86 | |||
87 | /* flags */ |
||
877 | trimarchi | 88 | #define EDFSTAR_CHANGE_LEVEL 8 |
792 | trimarchi | 89 | #define EDFSTAR_FLAG_NOPREEMPT 4 |
221 | giacomo | 90 | #define EDFSTAR_FLAG_NORAISEEXC 2 |
348 | trimarchi | 91 | #define EDFSTAR_FLAG_SPORADIC 1 |
221 | giacomo | 92 | |
348 | trimarchi | 93 | |
221 | giacomo | 94 | /* the level redefinition for the Earliest Deadline First level */ |
95 | typedef struct { |
||
96 | level_des l; /* the standard level descriptor */ |
||
97 | |||
98 | TIME period[MAX_PROC]; /* The task periods; the deadlines are |
||
99 | stored in the priority field */ |
||
100 | int deadline_timer[MAX_PROC]; |
||
101 | /* The task deadline timers */ |
||
102 | |||
103 | struct timespec deadline_timespec[MAX_PROC]; |
||
104 | |||
105 | int dline_miss[MAX_PROC]; /* Deadline miss counter */ |
||
106 | int wcet_miss[MAX_PROC]; /* Wcet miss counter */ |
||
107 | |||
108 | int nact[MAX_PROC]; /* Wcet miss counter */ |
||
109 | |||
110 | int flag[MAX_PROC]; |
||
111 | /* used to manage the JOB_TASK_MODEL and the |
||
112 | periodicity */ |
||
113 | |||
114 | IQUEUE ready; /* the ready queue */ |
||
115 | |||
116 | PID activated; /* the task that has been inserted into the |
||
117 | master module */ |
||
118 | |||
119 | int budget[MAX_PROC]; |
||
120 | |||
121 | int scheduling_level; |
||
122 | |||
296 | trimarchi | 123 | int cap_lev; |
124 | struct timespec cap_lasttime; |
||
125 | |||
876 | trimarchi | 126 | int new_level[MAX_PROC]; |
127 | int wcet[MAX_PROC]; /* save the wcet fields */ |
||
128 | |||
296 | trimarchi | 129 | |
221 | giacomo | 130 | } EDFSTAR_level_des; |
131 | |||
296 | trimarchi | 132 | static void capacity_handler(void *l) |
133 | { |
||
134 | EDFSTAR_level_des *lev = l; |
||
135 | lev->cap_lev = NIL; |
||
136 | event_need_reschedule(); |
||
137 | } |
||
138 | |||
221 | giacomo | 139 | static void EDFSTAR_check_preemption(EDFSTAR_level_des *lev) |
140 | { |
||
982 | trimarchi | 141 | PID first=NIL; |
221 | giacomo | 142 | |
351 | giacomo | 143 | #ifdef EDFSTAR_DEBUG |
144 | edfstar_printf("(E:chk)"); |
||
145 | #endif |
||
792 | trimarchi | 146 | /* check if the task is preempteble or not */ |
821 | trimarchi | 147 | if (lev->activated != NIL && lev->flag[lev->activated] & EDFSTAR_FLAG_NOPREEMPT) return; |
221 | giacomo | 148 | |
149 | if ((first = iq_query_first(&lev->ready)) != lev->activated) { |
||
150 | if (lev->activated != NIL) |
||
151 | level_table[ lev->scheduling_level ]-> |
||
152 | private_extract(lev->scheduling_level, lev->activated); |
||
153 | |||
154 | lev->activated = first; |
||
155 | |||
156 | if (first != NIL) { |
||
157 | BUDGET_TASK_MODEL b; |
||
158 | budget_task_default_model(b, lev->budget[first]); |
||
159 | |||
160 | level_table[ lev->scheduling_level ]-> |
||
161 | private_insert(lev->scheduling_level, first, (TASK_MODEL *)&b); |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | |||
166 | static void EDFSTAR_timer_deadline(void *par); |
||
167 | |||
168 | static void EDFSTAR_internal_activate(EDFSTAR_level_des *lev, PID p, |
||
169 | struct timespec *t) |
||
170 | { |
||
171 | |||
351 | giacomo | 172 | #ifdef EDFSTAR_DEBUG |
173 | edfstar_printf("(E:iact)"); |
||
174 | #endif |
||
175 | |||
221 | giacomo | 176 | ADDUSEC2TIMESPEC(lev->period[p], t); |
177 | |||
178 | *iq_query_timespec(p, &lev->ready) = *t; |
||
179 | lev->deadline_timespec[p] = *t; |
||
180 | |||
181 | /* Insert task in the correct position */ |
||
182 | proc_table[p].status = EDFSTAR_READY; |
||
183 | iq_timespec_insert(p,&lev->ready); |
||
296 | trimarchi | 184 | proc_table[p].control &= ~CONTROL_CAP; |
221 | giacomo | 185 | |
186 | /* check for preemption */ |
||
187 | EDFSTAR_check_preemption(lev); |
||
188 | } |
||
189 | |||
190 | static void EDFSTAR_timer_deadline(void *par) |
||
191 | { |
||
192 | PID p = (PID) par; |
||
193 | EDFSTAR_level_des *lev; |
||
194 | |||
195 | lev = (EDFSTAR_level_des *)level_table[proc_table[p].task_level]; |
||
799 | giacomo | 196 | lev->deadline_timer[p] = NIL; |
221 | giacomo | 197 | |
198 | switch (proc_table[p].status) { |
||
199 | case EDFSTAR_IDLE: |
||
200 | /* set the request time */ |
||
350 | giacomo | 201 | if (!(lev->flag[p] & EDFSTAR_FLAG_SPORADIC)) |
202 | EDFSTAR_internal_activate(lev,p,iq_query_timespec(p, &lev->ready)); |
||
221 | giacomo | 203 | |
204 | event_need_reschedule(); |
||
205 | break; |
||
206 | |||
207 | default: |
||
988 | trimarchi | 208 | #ifdef EDFSTAR_DEBUG |
351 | giacomo | 209 | kern_printf("(E:Dl:%d)",p); |
988 | trimarchi | 210 | #endif |
221 | giacomo | 211 | /* else, a deadline miss occurred!!! */ |
212 | lev->dline_miss[p]++; |
||
783 | giacomo | 213 | TRACER_LOGEVENT(FTrace_EVT_task_deadline_miss,proc_table[p].context,proc_table[p].task_level); |
221 | giacomo | 214 | |
215 | /* the task is into another state */ |
||
351 | giacomo | 216 | if (!(lev->flag[p] & EDFSTAR_FLAG_SPORADIC)) { |
217 | lev->nact[p]++; |
||
218 | ADDUSEC2TIMESPEC(lev->period[p], &lev->deadline_timespec[p]); |
||
219 | } |
||
221 | giacomo | 220 | } |
221 | |||
222 | /* Set the deadline timer */ |
||
350 | giacomo | 223 | if (!(lev->flag[p] & EDFSTAR_FLAG_SPORADIC)) |
224 | lev->deadline_timer[p] = kern_event_post(&lev->deadline_timespec[p], |
||
225 | EDFSTAR_timer_deadline, |
||
226 | (void *)p); |
||
221 | giacomo | 227 | |
228 | } |
||
229 | |||
876 | trimarchi | 230 | static int EDFSTAR_private_change_level(LEVEL l, PID p) |
231 | { |
||
232 | |||
233 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
234 | |||
235 | /* Change task level */ |
||
236 | if (lev->flag[p] & EDFSTAR_CHANGE_LEVEL) { |
||
237 | |||
877 | trimarchi | 238 | #ifdef EDFSTAR_DEBUG |
239 | edfstar_printf("(E:clev)"); |
||
240 | #endif |
||
241 | |||
876 | trimarchi | 242 | STD_command_message msg; |
243 | |||
244 | proc_table[p].status = SLEEP; |
||
877 | trimarchi | 245 | lev->flag[p] &= ~ EDFSTAR_CHANGE_LEVEL; |
876 | trimarchi | 246 | |
247 | level_table[lev->scheduling_level]->private_extract(lev->scheduling_level,p); |
||
248 | iq_extract(p,&lev->ready); |
||
249 | |||
250 | if (lev->deadline_timer[p] != -1) |
||
251 | kern_event_delete(lev->deadline_timer[p]); |
||
980 | trimarchi | 252 | lev->deadline_timer[p]=NIL; |
876 | trimarchi | 253 | |
254 | EDFSTAR_check_preemption(lev); |
||
255 | |||
256 | lev->nact[p] = 0; |
||
257 | lev->budget[p] = -1; |
||
258 | proc_table[p].task_level = lev->new_level[p]; |
||
259 | |||
260 | /* Send change level command to local scheduler */ |
||
261 | |||
262 | msg.command = STD_ACTIVATE_TASK; |
||
263 | msg.param = NULL; |
||
264 | |||
265 | level_table[ lev->new_level[p] ]->public_message(lev->new_level[p],p,&msg); |
||
266 | |||
267 | return 1; |
||
268 | |||
269 | } |
||
270 | |||
271 | return 0; |
||
272 | |||
273 | } |
||
274 | |||
275 | |||
221 | giacomo | 276 | static void EDFSTAR_timer_guest_deadline(void *par) |
277 | { |
||
278 | PID p = (PID) par; |
||
279 | |||
351 | giacomo | 280 | #ifdef EDFSTAR_DEBUG |
281 | edfstar_printf("(E:gdl)"); |
||
282 | #endif |
||
221 | giacomo | 283 | |
284 | kern_raise(XDEADLINE_MISS,p); |
||
285 | } |
||
286 | |||
287 | static int EDFSTAR_public_create(LEVEL l, PID p, TASK_MODEL *m) |
||
288 | { |
||
289 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
290 | |||
291 | /* if the EDFSTAR_task_create is called, then the pclass must be a |
||
292 | valid pclass. */ |
||
293 | HARD_TASK_MODEL *h; |
||
294 | |||
295 | if (m->pclass != HARD_PCLASS) return -1; |
||
296 | if (m->level != 0 && m->level != l) return -1; |
||
297 | h = (HARD_TASK_MODEL *)m; |
||
351 | giacomo | 298 | if (!h->wcet || !h->mit) return -1; |
221 | giacomo | 299 | /* now we know that m is a valid model */ |
300 | |||
351 | giacomo | 301 | #ifdef EDFSTAR_DEBUG |
302 | edfstar_printf("(E:Crt)"); |
||
303 | #endif |
||
221 | giacomo | 304 | |
305 | lev->period[p] = h->mit; |
||
306 | |||
307 | lev->flag[p] = 0; |
||
348 | trimarchi | 308 | |
309 | if (h->periodicity == APERIODIC) |
||
310 | lev->flag[p] |= EDFSTAR_FLAG_SPORADIC; |
||
311 | |||
221 | giacomo | 312 | lev->deadline_timer[p] = -1; |
313 | lev->dline_miss[p] = 0; |
||
314 | lev->wcet_miss[p] = 0; |
||
315 | lev->nact[p] = 0; |
||
316 | |||
317 | /* Enable wcet check */ |
||
318 | proc_table[p].avail_time = h->wcet; |
||
319 | proc_table[p].wcet = h->wcet; |
||
982 | trimarchi | 320 | proc_table[p].status = SLEEP; |
221 | giacomo | 321 | |
322 | return 0; /* OK, also if the task cannot be guaranteed... */ |
||
323 | } |
||
324 | |||
296 | trimarchi | 325 | |
326 | static void EDFSTAR_account_capacity(EDFSTAR_level_des *lev, PID p) |
||
327 | { |
||
328 | struct timespec ty; |
||
329 | TIME tx; |
||
330 | |||
982 | trimarchi | 331 | |
296 | trimarchi | 332 | SUBTIMESPEC(&schedule_time, &lev->cap_lasttime, &ty); |
333 | tx = TIMESPEC2USEC(&ty); |
||
334 | |||
335 | proc_table[p].avail_time -= tx; |
||
351 | giacomo | 336 | } |
296 | trimarchi | 337 | |
221 | giacomo | 338 | static int EDFSTAR_public_eligible(LEVEL l, PID p) |
339 | { |
||
340 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
341 | |||
351 | giacomo | 342 | #ifdef EDFSTAR_DEBUG |
877 | trimarchi | 343 | edfstar_printf2("(E:eli:%d)",p); |
351 | giacomo | 344 | #endif |
221 | giacomo | 345 | |
346 | return level_table[ lev->scheduling_level ]-> |
||
347 | private_eligible(lev->scheduling_level,p); |
||
351 | giacomo | 348 | |
221 | giacomo | 349 | } |
350 | |||
351 | static void EDFSTAR_public_dispatch(LEVEL l, PID p, int nostop) |
||
352 | { |
||
353 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
296 | trimarchi | 354 | struct timespec ty; |
221 | giacomo | 355 | |
351 | giacomo | 356 | #ifdef EDFSTAR_DEBUG |
357 | edfstar_printf("(E:dis)"); |
||
358 | #endif |
||
359 | |||
296 | trimarchi | 360 | if (!nostop || proc_table[exec].task_level==l) { |
361 | TIMESPEC_ASSIGN(&ty, &schedule_time); |
||
362 | TIMESPEC_ASSIGN(&lev->cap_lasttime, &schedule_time); |
||
221 | giacomo | 363 | |
296 | trimarchi | 364 | /* ...and finally, we have to post a capacity event on exec task because the shadow_task consume |
365 | * * capacity on exe task always */ |
||
784 | giacomo | 366 | if (proc_table[exec].avail_time > 0) { |
367 | ADDUSEC2TIMESPEC(proc_table[exec].avail_time ,&ty); |
||
368 | lev->cap_lev = kern_event_post(&ty,capacity_handler, lev); |
||
369 | } |
||
296 | trimarchi | 370 | level_table[lev->scheduling_level]->private_dispatch(lev->scheduling_level, p, nostop); |
371 | } |
||
372 | else |
||
373 | level_table[proc_table[exec].task_level]->public_dispatch(proc_table[exec].task_level, p, nostop); |
||
374 | |||
221 | giacomo | 375 | } |
376 | |||
377 | static void EDFSTAR_public_epilogue(LEVEL l, PID p) |
||
378 | { |
||
379 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
380 | |||
351 | giacomo | 381 | #ifdef EDFSTAR_DEBUG |
382 | edfstar_printf("(E:epi "); |
||
383 | #endif |
||
221 | giacomo | 384 | |
296 | trimarchi | 385 | if (lev->cap_lev!=NIL) { |
386 | kern_event_delete(lev->cap_lev); |
||
387 | lev->cap_lev=NIL; |
||
388 | } |
||
389 | |||
876 | trimarchi | 390 | |
296 | trimarchi | 391 | if ( proc_table[exec].task_level==l ) { |
392 | |||
785 | giacomo | 393 | if (proc_table[exec].avail_time > 0) EDFSTAR_account_capacity(lev,exec); |
296 | trimarchi | 394 | |
876 | trimarchi | 395 | if (EDFSTAR_private_change_level(l, p)) return; |
396 | |||
296 | trimarchi | 397 | /* check if the wcet is finished... */ |
784 | giacomo | 398 | if (proc_table[exec].avail_time < 0) { |
296 | trimarchi | 399 | /* wcet finished: disable wcet event and count wcet miss */ |
351 | giacomo | 400 | |
401 | #ifdef EDFSTAR_DEBUG |
||
402 | edfstar_printf2("W%d",p); |
||
403 | #endif |
||
296 | trimarchi | 404 | //proc_table[p].control &= ~CONTROL_CAP; |
405 | lev->wcet_miss[exec]++; |
||
784 | giacomo | 406 | proc_table[exec].avail_time = 0; |
407 | TRACER_LOGEVENT(FTrace_EVT_task_wcet_violation,proc_table[exec].context,proc_table[exec].task_level); |
||
296 | trimarchi | 408 | } |
221 | giacomo | 409 | |
351 | giacomo | 410 | #ifdef EDFSTAR_DEBUG |
411 | edfstar_printf(")"); |
||
412 | #endif |
||
221 | giacomo | 413 | |
351 | giacomo | 414 | level_table[ lev->scheduling_level ]-> |
415 | private_epilogue(lev->scheduling_level,p); |
||
416 | |||
982 | trimarchi | 417 | proc_table[exec].status = EDFSTAR_READY; |
296 | trimarchi | 418 | } else |
419 | level_table[proc_table[exec].task_level]->public_epilogue(proc_table[exec].task_level,p); |
||
420 | |||
221 | giacomo | 421 | } |
422 | |||
783 | giacomo | 423 | static void EDFSTAR_public_activate(LEVEL l, PID p, struct timespec *o) |
221 | giacomo | 424 | { |
425 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
426 | struct timespec t; |
||
427 | |||
428 | #ifdef EDFSTAR_DEBUG |
||
429 | edfstar_printf("(E:act:%d)",p); |
||
430 | #endif |
||
431 | |||
432 | /* Test if we are trying to activate a non sleeping task */ |
||
433 | /* save activation (only if needed... */ |
||
434 | if (proc_table[p].status != SLEEP) { |
||
435 | /* a periodic task cannot be activated when it is already active */ |
||
348 | trimarchi | 436 | /* but aperiodic task can be reactivate before */ |
802 | giacomo | 437 | if (lev->flag[p] & EDFSTAR_FLAG_SPORADIC) { |
438 | if (proc_table[p].status != EDFSTAR_IDLE) { |
||
439 | lev->nact[p]++; |
||
982 | trimarchi | 440 | //kern_printf("(Inc nact %d)",p); |
441 | //kern_printf("(%d STATUS %d %ds %dns)", p, proc_table[p].status, o->tv_sec, o->tv_nsec/1000); |
||
802 | giacomo | 442 | return; |
443 | } |
||
444 | } else { |
||
988 | trimarchi | 445 | return; |
446 | //kern_raise(XACTIVATION,p); |
||
351 | giacomo | 447 | } |
221 | giacomo | 448 | } |
449 | |||
450 | kern_gettime(&t); |
||
451 | |||
452 | EDFSTAR_internal_activate(lev,p, &t); |
||
453 | |||
454 | /* Set the deadline timer */ |
||
455 | lev->deadline_timer[p] = kern_event_post(&lev->deadline_timespec[p], |
||
456 | EDFSTAR_timer_deadline, |
||
457 | (void *)p); |
||
458 | |||
459 | } |
||
460 | |||
461 | static void EDFSTAR_public_unblock(LEVEL l, PID p) |
||
462 | { |
||
463 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
464 | |||
351 | giacomo | 465 | #ifdef EDFSTAR_DEBUG |
466 | edfstar_printf("(E:ins)"); |
||
467 | #endif |
||
221 | giacomo | 468 | |
351 | giacomo | 469 | /* Insert task in the correct position */ |
470 | proc_table[p].status = EDFSTAR_READY; |
||
471 | iq_timespec_insert(p,&lev->ready); |
||
221 | giacomo | 472 | |
351 | giacomo | 473 | /* and check for preemption! */ |
474 | EDFSTAR_check_preemption(lev); |
||
475 | |||
221 | giacomo | 476 | } |
477 | |||
478 | static void EDFSTAR_public_block(LEVEL l, PID p) |
||
479 | { |
||
480 | |||
351 | giacomo | 481 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
221 | giacomo | 482 | |
351 | giacomo | 483 | #ifdef EDFSTAR_DEBUG |
484 | edfstar_printf("(E:ext)"); |
||
485 | #endif |
||
221 | giacomo | 486 | |
351 | giacomo | 487 | /* the task is blocked on a synchronization primitive. we have to |
488 | remove it from the master module -and- from the local queue! */ |
||
489 | iq_extract(p,&lev->ready); |
||
490 | |||
491 | /* and finally, a preemption check! (it will also call guest_end) */ |
||
492 | EDFSTAR_check_preemption(lev); |
||
221 | giacomo | 493 | } |
494 | |||
495 | static int EDFSTAR_public_message(LEVEL l, PID p, void *m) |
||
496 | { |
||
497 | |||
351 | giacomo | 498 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
499 | struct timespec temp; |
||
876 | trimarchi | 500 | STD_command_message *msg; |
501 | HARD_TASK_MODEL *h; |
||
221 | giacomo | 502 | |
351 | giacomo | 503 | #ifdef EDFSTAR_DEBUG |
504 | edfstar_printf("(E:ecy "); |
||
505 | #endif |
||
221 | giacomo | 506 | |
351 | giacomo | 507 | switch ((long)(m)) { |
221 | giacomo | 508 | |
351 | giacomo | 509 | /* Task EndCycle */ |
510 | case (long)(NULL): |
||
348 | trimarchi | 511 | |
876 | trimarchi | 512 | if (EDFSTAR_private_change_level(l,p)) return 0; |
513 | |||
932 | trimarchi | 514 | if (proc_table[p].avail_time > 0) EDFSTAR_account_capacity(lev,p); |
515 | |||
351 | giacomo | 516 | /* we call guest_end directly here because the same task may |
517 | be reinserted in the queue before calling the preemption check! */ |
||
518 | level_table[ lev->scheduling_level ]-> |
||
519 | private_extract(lev->scheduling_level,p); |
||
520 | lev->activated = NIL; |
||
348 | trimarchi | 521 | |
351 | giacomo | 522 | iq_extract(p,&lev->ready); |
348 | trimarchi | 523 | |
351 | giacomo | 524 | /* we reset the capacity counters... */ |
525 | proc_table[p].avail_time = proc_table[p].wcet; |
||
526 | |||
527 | if (lev->nact[p] > 0) { |
||
221 | giacomo | 528 | |
351 | giacomo | 529 | #ifdef EDFSTAR_DEBUG |
982 | trimarchi | 530 | kern_printf("E%d",p); |
531 | #endif |
||
221 | giacomo | 532 | |
351 | giacomo | 533 | /* Pending activation: reactivate the thread!!! */ |
534 | lev->nact[p]--; |
||
221 | giacomo | 535 | |
351 | giacomo | 536 | /* see also EDFSTAR_timer_deadline */ |
537 | kern_gettime(&temp); |
||
221 | giacomo | 538 | |
351 | giacomo | 539 | EDFSTAR_internal_activate(lev,p, &temp); |
221 | giacomo | 540 | |
351 | giacomo | 541 | /* check if the deadline has already expired */ |
542 | temp = *iq_query_timespec(p, &lev->ready); |
||
543 | if (TIMESPEC_A_LT_B(&temp, &schedule_time)) { |
||
544 | /* count the deadline miss */ |
||
545 | lev->dline_miss[p]++; |
||
546 | kern_event_delete(lev->deadline_timer[p]); |
||
799 | giacomo | 547 | lev->deadline_timer[p] = NIL; |
351 | giacomo | 548 | } |
783 | giacomo | 549 | |
351 | giacomo | 550 | } else { |
221 | giacomo | 551 | |
351 | giacomo | 552 | #ifdef EDFSTAR_DEBUG |
553 | edfstar_printf("e%d",p); |
||
554 | #endif |
||
221 | giacomo | 555 | |
351 | giacomo | 556 | /* the task has terminated his job before it consume the wcet. All OK! */ |
799 | giacomo | 557 | if (lev->flag[p] & EDFSTAR_FLAG_SPORADIC) |
558 | proc_table[p].status = SLEEP; |
||
559 | else |
||
560 | proc_table[p].status = EDFSTAR_IDLE; |
||
351 | giacomo | 561 | |
799 | giacomo | 562 | if (lev->flag[p] & EDFSTAR_FLAG_SPORADIC && lev->deadline_timer[p] != NIL) { |
351 | giacomo | 563 | kern_event_delete(lev->deadline_timer[p]); |
799 | giacomo | 564 | lev->deadline_timer[p] = NIL; |
565 | } |
||
221 | giacomo | 566 | |
351 | giacomo | 567 | /* and finally, a preemption check! */ |
568 | EDFSTAR_check_preemption(lev); |
||
221 | giacomo | 569 | |
351 | giacomo | 570 | /* when the deadline timer fire, it recognize the situation and set |
571 | correctly all the stuffs (like reactivation, etc... ) */ |
||
572 | } |
||
573 | |||
574 | #ifdef EDFSTAR_DEBUG |
||
575 | edfstar_printf(")"); |
||
576 | #endif |
||
783 | giacomo | 577 | |
578 | TRACER_LOGEVENT(FTrace_EVT_task_end_cycle,proc_table[p].context,proc_table[p].task_level); |
||
221 | giacomo | 579 | jet_update_endcycle(); /* Update the Jet data... */ |
580 | break; |
||
581 | |||
582 | default: |
||
876 | trimarchi | 583 | msg = (STD_command_message *)m; |
584 | |||
585 | #ifdef EDFSTAR_DEBUG |
||
586 | edfstar_printf("(E:MSG %d)",msg->command); |
||
587 | #endif |
||
588 | |||
589 | switch(msg->command) { |
||
590 | case STD_SET_NEW_MODEL: |
||
591 | /* if the EDFSTAR_task_create is called, then the pclass must be a |
||
592 | valid pclass. */ |
||
593 | h=(HARD_TASK_MODEL *)(msg->param); |
||
877 | trimarchi | 594 | |
876 | trimarchi | 595 | /* now we know that m is a valid model */ |
596 | lev->wcet[p] = h->wcet; |
||
597 | lev->period[p] = h->mit; |
||
877 | trimarchi | 598 | |
599 | #ifdef EDFSTAR_DEBUG |
||
600 | kern_printf("(EDF:NM p%d w%d m%d)", p, h->wcet, h->mit); |
||
601 | #endif |
||
876 | trimarchi | 602 | lev->flag[p] = 0; |
603 | lev->deadline_timer[p] = -1; |
||
604 | lev->dline_miss[p] = 0; |
||
605 | lev->wcet_miss[p] = 0; |
||
606 | lev->nact[p] = 0; |
||
221 | giacomo | 607 | |
876 | trimarchi | 608 | break; |
609 | |||
610 | case STD_SET_NEW_LEVEL: |
||
611 | |||
612 | lev->flag[p] |= EDFSTAR_CHANGE_LEVEL; |
||
613 | lev->new_level[p] = (int)(msg->param); |
||
614 | |||
615 | break; |
||
616 | |||
617 | case STD_ACTIVATE_TASK: |
||
877 | trimarchi | 618 | #ifdef EDFSTAR_DEBUG |
619 | kern_printf("(EDF:SA)"); |
||
620 | #endif |
||
876 | trimarchi | 621 | /* Enable wcet check */ |
622 | proc_table[p].avail_time = lev->wcet[p]; |
||
623 | proc_table[p].wcet = lev->wcet[p]; |
||
624 | proc_table[p].control &= ~CONTROL_CAP; |
||
625 | |||
626 | EDFSTAR_public_activate(l, p,NULL); |
||
627 | |||
628 | break; |
||
629 | |||
630 | |||
631 | } |
||
632 | |||
241 | giacomo | 633 | break; |
221 | giacomo | 634 | |
635 | } |
||
636 | return 0; |
||
637 | } |
||
638 | |||
639 | static void EDFSTAR_public_end(LEVEL l, PID p) |
||
640 | { |
||
641 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
642 | |||
351 | giacomo | 643 | #ifdef EDFSTAR_DEBUG |
644 | edfstar_printf("(E:end)"); |
||
645 | #endif |
||
221 | giacomo | 646 | |
647 | iq_extract(p,&lev->ready); |
||
982 | trimarchi | 648 | level_table[ lev->scheduling_level ]-> |
649 | private_extract(lev->scheduling_level, p); |
||
221 | giacomo | 650 | |
982 | trimarchi | 651 | |
221 | giacomo | 652 | /* we finally put the task in the ready queue */ |
653 | proc_table[p].status = FREE; |
||
654 | |||
655 | iq_insertfirst(p,&freedesc); |
||
348 | trimarchi | 656 | lev->activated=NIL; |
221 | giacomo | 657 | |
658 | if (lev->deadline_timer[p] != -1) { |
||
659 | kern_event_delete(lev->deadline_timer[p]); |
||
799 | giacomo | 660 | lev->deadline_timer[p] = NIL; |
221 | giacomo | 661 | } |
662 | |||
663 | /* and finally, a preemption check! (it will also call guest_end) */ |
||
664 | EDFSTAR_check_preemption(lev); |
||
665 | } |
||
666 | |||
667 | /* Guest Functions |
||
668 | These functions manages a JOB_TASK_MODEL, that is used to put |
||
669 | a guest task in the EDFSTAR ready queue. */ |
||
670 | |||
671 | static void EDFSTAR_private_insert(LEVEL l, PID p, TASK_MODEL *m) |
||
672 | { |
||
673 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
674 | JOB_TASK_MODEL *job; |
||
675 | |||
676 | if (m->pclass != JOB_PCLASS || (m->level != 0 && m->level != l) ) { |
||
677 | kern_raise(XINVALID_TASK, p); |
||
678 | return; |
||
679 | } |
||
680 | |||
681 | job = (JOB_TASK_MODEL *)m; |
||
682 | |||
683 | /* if the EDFSTAR_guest_create is called, then the pclass must be a |
||
684 | valid pclass. */ |
||
685 | |||
686 | *iq_query_timespec(p, &lev->ready) = job->deadline; |
||
687 | |||
688 | lev->deadline_timer[p] = -1; |
||
689 | lev->dline_miss[p] = 0; |
||
690 | lev->wcet_miss[p] = 0; |
||
691 | lev->nact[p] = 0; |
||
692 | |||
693 | if (job->noraiseexc) |
||
694 | lev->flag[p] |= EDFSTAR_FLAG_NORAISEEXC; |
||
695 | else { |
||
696 | lev->flag[p] &= ~EDFSTAR_FLAG_NORAISEEXC; |
||
697 | lev->deadline_timer[p] = kern_event_post(iq_query_timespec(p, &lev->ready), |
||
698 | EDFSTAR_timer_guest_deadline, |
||
699 | (void *)p); |
||
700 | } |
||
701 | |||
702 | lev->period[p] = job->period; |
||
703 | |||
704 | /* Insert task in the correct position */ |
||
705 | iq_timespec_insert(p,&lev->ready); |
||
706 | proc_table[p].status = EDFSTAR_READY; |
||
707 | |||
708 | /* check for preemption */ |
||
709 | EDFSTAR_check_preemption(lev); |
||
710 | |||
711 | /* there is no bandwidth guarantee at this level, it is performed |
||
712 | by the level that inserts guest tasks... */ |
||
713 | } |
||
714 | |||
715 | static void EDFSTAR_private_dispatch(LEVEL l, PID p, int nostop) |
||
716 | { |
||
717 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
718 | |||
719 | level_table[ lev->scheduling_level ]-> |
||
720 | private_dispatch(lev->scheduling_level,p,nostop); |
||
721 | } |
||
722 | |||
723 | static void EDFSTAR_private_epilogue(LEVEL l, PID p) |
||
724 | { |
||
725 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
726 | |||
727 | /* the task has been preempted. it returns into the ready queue... */ |
||
728 | level_table[ lev->scheduling_level ]-> |
||
729 | private_epilogue(lev->scheduling_level,p); |
||
730 | |||
731 | proc_table[p].status = EDFSTAR_READY; |
||
732 | } |
||
733 | |||
734 | static void EDFSTAR_private_extract(LEVEL l, PID p) |
||
735 | { |
||
736 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
737 | |||
738 | #ifdef EDFSTAR_DEBUG |
||
877 | trimarchi | 739 | kern_printf("EDFSTAR_guest_end: dline timer %d\n",lev->deadline_timer[p]); |
221 | giacomo | 740 | #endif |
741 | |||
742 | iq_extract(p, &lev->ready); |
||
743 | |||
744 | /* we remove the deadline timer, because the slice is finished */ |
||
745 | if (lev->deadline_timer[p] != NIL) { |
||
746 | #ifdef EDFSTAR_DEBUG |
||
877 | trimarchi | 747 | kern_printf("EDFSTAR_guest_end: dline timer %d\n",lev->deadline_timer[p]); |
221 | giacomo | 748 | #endif |
749 | kern_event_delete(lev->deadline_timer[p]); |
||
750 | lev->deadline_timer[p] = NIL; |
||
751 | } |
||
752 | |||
753 | /* and finally, a preemption check! (it will also call guest_end() */ |
||
754 | EDFSTAR_check_preemption(lev); |
||
755 | } |
||
756 | |||
757 | /* Registration functions */ |
||
758 | |||
759 | /* Registration function: |
||
760 | int flags the init flags ... see EDFSTAR.h */ |
||
761 | |||
762 | LEVEL EDFSTAR_register_level(int master) |
||
763 | { |
||
764 | LEVEL l; /* the level that we register */ |
||
765 | EDFSTAR_level_des *lev; /* for readableness only */ |
||
766 | PID i; /* a counter */ |
||
767 | |||
768 | #ifdef EDFSTAR_DEBUG |
||
769 | printk("EDFSTAR_register_level\n"); |
||
770 | #endif |
||
771 | |||
772 | /* request an entry in the level_table */ |
||
773 | l = level_alloc_descriptor(sizeof(EDFSTAR_level_des)); |
||
774 | |||
775 | lev = (EDFSTAR_level_des *)level_table[l]; |
||
776 | |||
777 | /* fill the standard descriptor */ |
||
778 | lev->l.private_insert = EDFSTAR_private_insert; |
||
779 | lev->l.private_extract = EDFSTAR_private_extract; |
||
780 | lev->l.private_dispatch = EDFSTAR_private_dispatch; |
||
781 | lev->l.private_epilogue = EDFSTAR_private_epilogue; |
||
782 | |||
783 | lev->l.public_guarantee = NULL; |
||
784 | lev->l.public_eligible = EDFSTAR_public_eligible; |
||
785 | lev->l.public_create = EDFSTAR_public_create; |
||
786 | lev->l.public_end = EDFSTAR_public_end; |
||
787 | lev->l.public_dispatch = EDFSTAR_public_dispatch; |
||
788 | lev->l.public_epilogue = EDFSTAR_public_epilogue; |
||
789 | lev->l.public_activate = EDFSTAR_public_activate; |
||
790 | lev->l.public_unblock = EDFSTAR_public_unblock; |
||
791 | lev->l.public_block = EDFSTAR_public_block; |
||
792 | lev->l.public_message = EDFSTAR_public_message; |
||
793 | |||
794 | /* fill the EDFSTAR descriptor part */ |
||
795 | for(i=0; i<MAX_PROC; i++) { |
||
796 | lev->period[i] = 0; |
||
797 | lev->deadline_timer[i] = -1; |
||
798 | lev->flag[i] = 0; |
||
799 | lev->dline_miss[i] = 0; |
||
800 | lev->wcet_miss[i] = 0; |
||
801 | lev->nact[i] = 0; |
||
802 | lev->budget[i] = NIL; |
||
880 | trimarchi | 803 | lev->new_level[i] = -1; |
221 | giacomo | 804 | } |
805 | |||
806 | iq_init(&lev->ready, NULL, IQUEUE_NO_PRIORITY); |
||
807 | lev->activated = NIL; |
||
808 | |||
809 | lev->scheduling_level = master; |
||
298 | giacomo | 810 | lev->cap_lev = NIL; |
296 | trimarchi | 811 | NULL_TIMESPEC(&lev->cap_lasttime); |
221 | giacomo | 812 | |
813 | return l; |
||
814 | } |
||
815 | |||
816 | int EDFSTAR_get_dline_miss(PID p) |
||
817 | { |
||
818 | LEVEL l = proc_table[p].task_level; |
||
819 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
820 | |||
821 | return lev->dline_miss[p]; |
||
822 | } |
||
823 | |||
824 | int EDFSTAR_get_wcet_miss(PID p) |
||
825 | { |
||
826 | LEVEL l = proc_table[p].task_level; |
||
827 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
828 | |||
829 | return lev->wcet_miss[p]; |
||
830 | } |
||
831 | |||
832 | int EDFSTAR_get_nact(PID p) |
||
833 | { |
||
834 | LEVEL l = proc_table[p].task_level; |
||
835 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
836 | |||
837 | return lev->nact[p]; |
||
838 | } |
||
839 | |||
840 | int EDFSTAR_reset_dline_miss(PID p) |
||
841 | { |
||
842 | LEVEL l = proc_table[p].task_level; |
||
843 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
844 | |||
845 | lev->dline_miss[p] = 0; |
||
846 | return 0; |
||
847 | } |
||
848 | |||
849 | int EDFSTAR_reset_wcet_miss(PID p) |
||
850 | { |
||
851 | LEVEL l = proc_table[p].task_level; |
||
852 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
853 | |||
854 | lev->wcet_miss[p] = 0; |
||
855 | return 0; |
||
856 | } |
||
857 | |||
858 | int EDFSTAR_setbudget(LEVEL l, PID p, int budget) |
||
859 | { |
||
860 | |||
861 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
862 | |||
863 | lev->budget[p] = budget; |
||
864 | |||
865 | return 0; |
||
866 | |||
867 | } |
||
868 | |||
869 | int EDFSTAR_getbudget(LEVEL l, PID p) |
||
870 | { |
||
871 | |||
872 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
873 | |||
874 | return lev->budget[p]; |
||
875 | |||
876 | } |
||
236 | giacomo | 877 | |
792 | trimarchi | 878 | void EDFSTAR_set_nopreemtive_current(LEVEL l) { |
879 | |||
880 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
881 | |||
882 | lev->flag[lev->activated]|=EDFSTAR_FLAG_NOPREEMPT; |
||
883 | } |
||
884 | |||
885 | void EDFSTAR_unset_nopreemtive_current(LEVEL l) { |
||
886 | |||
887 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
888 | |||
889 | lev->flag[lev->activated]&=~EDFSTAR_FLAG_NOPREEMPT; |
||
890 | } |
||
891 | |||
236 | giacomo | 892 | int EDFSTAR_budget_has_thread(LEVEL l, int budget) |
893 | { |
||
894 | |||
895 | EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]); |
||
896 | int i; |
||
897 | |||
898 | for(i = 0; i< MAX_PROC; i++) |
||
899 | if (lev->budget[i] == budget) return 1; |
||
900 | |||
901 | return 0; |
||
902 | |||
903 | } |