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