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