Rev 29 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pj | 1 | /* |
2 | * Project: S.Ha.R.K. |
||
3 | * |
||
4 | * Coordinators: |
||
5 | * Giorgio Buttazzo <giorgio@sssup.it> |
||
6 | * Paolo Gai <pj@gandalf.sssup.it> |
||
7 | * |
||
8 | * Authors : |
||
9 | * Paolo Gai <pj@gandalf.sssup.it> |
||
10 | * Massimiliano Giorgi <massy@gandalf.sssup.it> |
||
11 | * Luca Abeni <luca@gandalf.sssup.it> |
||
12 | * (see the web pages for full authors list) |
||
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 | ------------ |
||
38 | pj | 23 | CVS : $Id: tbs.c,v 1.4 2003-01-07 17:07:51 pj Exp $ |
2 | pj | 24 | |
25 | File: $File$ |
||
38 | pj | 26 | Revision: $Revision: 1.4 $ |
27 | Last update: $Date: 2003-01-07 17:07:51 $ |
||
2 | pj | 28 | ------------ |
29 | |||
30 | This file contains the aperiodic server TBS (Total Bandwidth Server) |
||
31 | |||
32 | Read tbs.h for further details. |
||
33 | |||
34 | **/ |
||
35 | |||
36 | /* |
||
37 | * Copyright (C) 2000 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 <modules/tbs.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> |
||
38 | pj | 63 | #include <kernel/trace.h> |
2 | pj | 64 | |
65 | /*+ 4 debug purposes +*/ |
||
66 | #undef TBS_TEST |
||
67 | |||
68 | /*+ Status used in the level +*/ |
||
69 | #define TBS_WCET_VIOLATED APER_STATUS_BASE+2 /*+ when wcet is finished +*/ |
||
70 | #define TBS_WAIT APER_STATUS_BASE /*+ waiting the service +*/ |
||
71 | |||
72 | /*+ task flags +*/ |
||
73 | #define TBS_SAVE_ARRIVALS 1 |
||
74 | |||
75 | /*+ the level redefinition for the Total Bandwidth Server level +*/ |
||
76 | typedef struct { |
||
77 | level_des l; /*+ the standard level descriptor +*/ |
||
78 | |||
79 | /* The wcet are stored in the task descriptor's priority |
||
80 | field. */ |
||
81 | |||
82 | int nact[MAX_PROC]; /*+ used to record activations +*/ |
||
83 | BYTE flag[MAX_PROC]; |
||
84 | |||
85 | struct timespec lastdline; /*+ the last deadline assigned to |
||
86 | a TBS task +*/ |
||
87 | |||
29 | pj | 88 | IQUEUE wait; /*+ the wait queue of the TBS +*/ |
2 | pj | 89 | PID activated; /*+ the task inserted in another queue +*/ |
90 | |||
91 | int flags; /*+ the init flags... +*/ |
||
92 | |||
93 | bandwidth_t U; /*+ the used bandwidth by the server +*/ |
||
94 | int band_num; |
||
95 | int band_den; |
||
96 | |||
97 | LEVEL scheduling_level; |
||
98 | |||
99 | } TBS_level_des; |
||
100 | |||
101 | #ifdef TESTG |
||
102 | #include "drivers/glib.h" |
||
103 | #endif |
||
104 | |||
105 | /* This static function activates the task pointed by lev->activated) */ |
||
106 | static __inline__ void TBS_activation(TBS_level_des *lev) |
||
107 | { |
||
108 | PID p; /* for readableness */ |
||
109 | JOB_TASK_MODEL j; /* the guest model */ |
||
110 | TIME drel; /* the relative deadline of the task */ |
||
111 | LEVEL m; /* the master level... only for readableness */ |
||
112 | |||
113 | #ifdef TESTG |
||
114 | TIME x; |
||
115 | extern TIME starttime; |
||
116 | #endif |
||
117 | |||
118 | p = lev->activated; |
||
119 | /* we compute a suitable deadline for the task */ |
||
120 | drel = (proc_table[p].wcet * lev->band_den) / lev->band_num; |
||
121 | |||
122 | ADDUSEC2TIMESPEC(drel, &lev->lastdline); |
||
123 | |||
124 | #ifdef TESTG |
||
125 | if (starttime) { |
||
126 | x = ((lev->lastdline.tv_sec*1000000+lev->lastdline.tv_nsec/1000)/5000 - starttime) + 20; |
||
127 | if (x<640) |
||
128 | grx_plot(x, 15, 7); |
||
129 | } |
||
130 | #endif |
||
131 | |||
132 | /* and we insert the task in another level */ |
||
133 | m = lev->scheduling_level; |
||
134 | job_task_default_model(j,lev->lastdline); |
||
38 | pj | 135 | level_table[m]->private_insert(m,p,(TASK_MODEL *)&j); |
2 | pj | 136 | |
137 | #ifdef TBS_TEST |
||
138 | kern_printf("TBS_activation: lastdline %ds %dns\n",lev->lastdline.tv_sec,lev->lastdline.tv_nsec); |
||
139 | #endif |
||
140 | } |
||
141 | |||
142 | /* This static function reclaims the unused time of the task p */ |
||
143 | static __inline__ void TBS_bandwidth_reclaiming(TBS_level_des *lev, PID p) |
||
144 | { |
||
145 | TIME reclaimed; |
||
146 | struct timespec r, sos; |
||
147 | |||
148 | // kern_printf("%d ", proc_table[p].avail_time); |
||
149 | reclaimed = (proc_table[p].avail_time * lev->band_den) / lev->band_num; |
||
150 | |||
151 | r.tv_nsec = (reclaimed % 1000000) * 1000; |
||
152 | r.tv_sec = reclaimed / 1000000; |
||
153 | |||
154 | SUBTIMESPEC(&lev->lastdline, &r, &sos); |
||
155 | TIMESPEC_ASSIGN(&lev->lastdline, &sos); |
||
156 | |||
157 | #ifdef TBS_TEST |
||
158 | kern_printf("TBS_bandwidth_reclaiming: lastdline %ds %dns, reclaimed %d, avail %d\n", |
||
159 | lev->lastdline.tv_sec, lev->lastdline.tv_nsec, reclaimed, proc_table[p].avail_time); |
||
160 | #endif |
||
161 | } |
||
162 | |||
163 | /* The on-line guarantee is enabled only if the appropriate flag is set... */ |
||
38 | pj | 164 | static int TBS_public_guarantee(LEVEL l, bandwidth_t *freebandwidth) |
2 | pj | 165 | { |
166 | TBS_level_des *lev = (TBS_level_des *)(level_table[l]); |
||
167 | |||
168 | if (*freebandwidth >= lev->U) { |
||
169 | *freebandwidth -= lev->U; |
||
170 | return 1; |
||
171 | } |
||
172 | else |
||
173 | return 0; |
||
174 | } |
||
175 | |||
38 | pj | 176 | static int TBS_public_create(LEVEL l, PID p, TASK_MODEL *m) |
2 | pj | 177 | { |
178 | TBS_level_des *lev = (TBS_level_des *)(level_table[l]); |
||
179 | |||
180 | /* if the TBS_task_create is called, then the pclass must be a |
||
181 | valid pclass. */ |
||
38 | pj | 182 | SOFT_TASK_MODEL *s; |
2 | pj | 183 | |
38 | pj | 184 | if (m->pclass != SOFT_PCLASS) return -1; |
185 | if (m->level != 0 && m->level != l) return -1; |
||
186 | s = (SOFT_TASK_MODEL *)m; |
||
187 | if (!(s->wcet && s->periodicity == APERIODIC)) return -1; |
||
188 | |||
2 | pj | 189 | proc_table[p].wcet = s->wcet; |
190 | |||
191 | /* Enable wcet check */ |
||
192 | if (lev->flags & TBS_ENABLE_WCET_CHECK) { |
||
193 | proc_table[p].avail_time = s->wcet; |
||
194 | proc_table[p].control |= CONTROL_CAP; |
||
195 | } |
||
196 | |||
197 | lev->nact[p] = 0; |
||
198 | if (s->arrivals == SAVE_ARRIVALS) |
||
199 | lev->flag[p] = TBS_SAVE_ARRIVALS; |
||
200 | |||
201 | return 0; /* OK, also if the task cannot be guaranteed... */ |
||
202 | } |
||
203 | |||
38 | pj | 204 | static void TBS_public_dispatch(LEVEL l, PID p, int nostop) |
2 | pj | 205 | { |
206 | TBS_level_des *lev = (TBS_level_des *)(level_table[l]); |
||
207 | |||
208 | /* there is at least one task ready inserted in an EDF or similar |
||
209 | level */ |
||
210 | |||
211 | level_table[ lev->scheduling_level ]-> |
||
38 | pj | 212 | private_dispatch(lev->scheduling_level,p,nostop); |
2 | pj | 213 | } |
214 | |||
38 | pj | 215 | static void TBS_public_epilogue(LEVEL l, PID p) |
2 | pj | 216 | { |
217 | TBS_level_des *lev = (TBS_level_des *)(level_table[l]); |
||
218 | |||
219 | /* check if the wcet is finished... */ |
||
220 | if ((lev->flags & TBS_ENABLE_WCET_CHECK) && proc_table[p].avail_time <= 0) { |
||
221 | /* if it is, raise a XWCET_VIOLATION exception */ |
||
222 | kern_raise(XWCET_VIOLATION,p); |
||
223 | proc_table[p].status = TBS_WCET_VIOLATED; |
||
224 | |||
225 | /* the current task have to die in the scheduling queue, and another |
||
226 | have to be put in place... this code is identical to the |
||
227 | TBS_task_end */ |
||
228 | level_table[ lev->scheduling_level ]-> |
||
38 | pj | 229 | private_extract(lev->scheduling_level,p); |
2 | pj | 230 | |
231 | /* we reclaim an avail time that can be <0 due to the timer |
||
232 | approximations -> we have to postpone the deadline a little! |
||
233 | we can use the ADDUSEC2TIMESPEC because the time postponed is |
||
234 | less than 55ms */ |
||
235 | ADDUSEC2TIMESPEC((-proc_table[p].avail_time * lev->band_den) |
||
236 | / lev->band_num, &lev->lastdline); |
||
237 | |||
238 | #ifdef TBS_TEST |
||
239 | kern_printf("TBS_task_epilogue: Deadline posponed to %ds %dns\n", |
||
240 | lev->lastdline.tv_sec, lev->lastdline.tv_nsec); |
||
241 | #endif |
||
242 | |||
29 | pj | 243 | lev->activated = iq_getfirst(&lev->wait); |
2 | pj | 244 | if (lev->activated != NIL) |
245 | TBS_activation(lev); |
||
246 | } |
||
247 | else |
||
248 | /* the task has been preempted. it returns into the ready queue by |
||
249 | calling the guest_epilogue... */ |
||
250 | level_table[ lev->scheduling_level ]-> |
||
38 | pj | 251 | private_epilogue(lev->scheduling_level,p); |
2 | pj | 252 | } |
253 | |||
38 | pj | 254 | static void TBS_public_activate(LEVEL l, PID p) |
2 | pj | 255 | { |
256 | TBS_level_des *lev = (TBS_level_des *)(level_table[l]); |
||
38 | pj | 257 | struct timespec t; |
2 | pj | 258 | |
259 | if (proc_table[p].status == SLEEP || |
||
260 | proc_table[p].status == TBS_WCET_VIOLATED) { |
||
261 | |||
38 | pj | 262 | kern_gettime(&t); |
263 | if (TIMESPEC_A_GT_B(&t, &lev->lastdline)) |
||
264 | TIMESPEC_ASSIGN(&lev->lastdline, &t ); |
||
265 | |||
266 | |||
2 | pj | 267 | if (lev->activated == NIL) { |
268 | /* This is the first task in the level, so we activate it immediately */ |
||
269 | lev->activated = p; |
||
270 | TBS_activation(lev); |
||
271 | } |
||
272 | else { |
||
273 | proc_table[p].status = TBS_WAIT; |
||
29 | pj | 274 | iq_insertlast(p, &lev->wait); |
2 | pj | 275 | } |
276 | } |
||
277 | else if (lev->flag[p] & TBS_SAVE_ARRIVALS) |
||
278 | lev->nact[p]++; |
||
279 | /* else |
||
280 | kern_printf("TBSREJ!!!");*/ |
||
281 | } |
||
282 | |||
38 | pj | 283 | static void TBS_public_unblock(LEVEL l, PID p) |
2 | pj | 284 | { |
285 | TBS_level_des *lev = (TBS_level_des *)(level_table[l]); |
||
38 | pj | 286 | JOB_TASK_MODEL j; |
2 | pj | 287 | |
38 | pj | 288 | job_task_default_model(j,lev->lastdline); |
289 | level_table[lev->scheduling_level]-> |
||
290 | private_insert(lev->scheduling_level,p,(TASK_MODEL *)&j); |
||
2 | pj | 291 | } |
292 | |||
38 | pj | 293 | static void TBS_public_block(LEVEL l, PID p) |
2 | pj | 294 | { |
295 | TBS_level_des *lev = (TBS_level_des *)(level_table[l]); |
||
296 | |||
297 | level_table[ lev->scheduling_level ]-> |
||
38 | pj | 298 | private_extract(lev->scheduling_level,p); |
2 | pj | 299 | } |
300 | |||
38 | pj | 301 | static int TBS_public_message(LEVEL l, PID p, void *m) |
2 | pj | 302 | { |
303 | TBS_level_des *lev = (TBS_level_des *)(level_table[l]); |
||
304 | |||
305 | /* a task activation is finished, but we are using a JOB_TASK_MODEL |
||
306 | that implements a single activation, so we have to call |
||
307 | the guest_end, that representsa single activation... */ |
||
308 | level_table[ lev->scheduling_level ]-> |
||
38 | pj | 309 | private_extract(lev->scheduling_level,p); |
2 | pj | 310 | |
311 | TBS_bandwidth_reclaiming(lev,p); |
||
312 | |||
313 | /* we reset the capacity counters... */ |
||
314 | if (lev->flags & TBS_ENABLE_WCET_CHECK) |
||
315 | proc_table[p].avail_time = proc_table[p].wcet; |
||
316 | |||
317 | if (lev->nact[p]) { |
||
318 | // lev->nact[p] can be >0 only if the SAVE_ARRIVALS bit is set |
||
319 | lev->nact[p]--; |
||
320 | proc_table[p].status = TBS_WAIT; |
||
29 | pj | 321 | iq_insertlast(p, &lev->wait); |
2 | pj | 322 | } |
323 | else |
||
324 | proc_table[p].status = SLEEP; |
||
325 | |||
29 | pj | 326 | lev->activated = iq_getfirst(&lev->wait); |
2 | pj | 327 | if (lev->activated != NIL) |
328 | TBS_activation(lev); |
||
329 | |||
38 | pj | 330 | jet_update_endcycle(); /* Update the Jet data... */ |
331 | trc_logevent(TRC_ENDCYCLE,&exec_shadow); /* tracer stuff */ |
||
332 | |||
333 | return 0; |
||
2 | pj | 334 | } |
335 | |||
38 | pj | 336 | static void TBS_public_end(LEVEL l, PID p) |
2 | pj | 337 | { |
338 | TBS_level_des *lev = (TBS_level_des *)(level_table[l]); |
||
339 | |||
340 | level_table[ lev->scheduling_level ]-> |
||
38 | pj | 341 | private_extract(lev->scheduling_level,p); |
2 | pj | 342 | |
343 | TBS_bandwidth_reclaiming(lev,p); |
||
344 | |||
345 | proc_table[p].status = FREE; |
||
29 | pj | 346 | iq_insertfirst(p,&freedesc); |
2 | pj | 347 | |
29 | pj | 348 | lev->activated = iq_getfirst(&lev->wait); |
2 | pj | 349 | if (lev->activated != NIL) |
350 | TBS_activation(lev); |
||
351 | } |
||
352 | |||
353 | /* Registration functions */ |
||
354 | |||
355 | /*+ Registration function: |
||
356 | int flags the init flags ... see TBS.h +*/ |
||
357 | void TBS_register_level(int flags, LEVEL master, int num, int den) |
||
358 | { |
||
359 | LEVEL l; /* the level that we register */ |
||
360 | TBS_level_des *lev; /* for readableness only */ |
||
361 | PID i; /* a counter */ |
||
362 | |||
363 | printk("TBS_register_level\n"); |
||
364 | |||
365 | /* request an entry in the level_table */ |
||
38 | pj | 366 | l = level_alloc_descriptor(sizeof(TBS_level_des)); |
2 | pj | 367 | |
38 | pj | 368 | lev = (TBS_level_des *)level_table[l]; |
2 | pj | 369 | |
370 | printk(" lev=%d\n",(int)lev); |
||
371 | |||
372 | /* fill the standard descriptor */ |
||
373 | if (flags & TBS_ENABLE_GUARANTEE) |
||
38 | pj | 374 | lev->l.public_guarantee = TBS_public_guarantee; |
2 | pj | 375 | else |
38 | pj | 376 | lev->l.public_guarantee = NULL; |
2 | pj | 377 | |
38 | pj | 378 | lev->l.public_guarantee = TBS_public_guarantee; |
379 | lev->l.public_create = TBS_public_create; |
||
380 | lev->l.public_end = TBS_public_end; |
||
381 | lev->l.public_dispatch = TBS_public_dispatch; |
||
382 | lev->l.public_epilogue = TBS_public_epilogue; |
||
383 | lev->l.public_activate = TBS_public_activate; |
||
384 | lev->l.public_unblock = TBS_public_unblock; |
||
385 | lev->l.public_block = TBS_public_block; |
||
386 | lev->l.public_message = TBS_public_message; |
||
2 | pj | 387 | |
388 | /* fill the TBS descriptor part */ |
||
389 | |||
390 | for (i = 0; i < MAX_PROC; i++) { |
||
391 | lev->nact[i] = 0; |
||
392 | lev->flag[i] = 0; |
||
393 | } |
||
394 | |||
395 | NULL_TIMESPEC(&lev->lastdline); |
||
396 | |||
29 | pj | 397 | iq_init(&lev->wait, &freedesc, 0); |
2 | pj | 398 | lev->activated = NIL; |
399 | |||
400 | lev->U = (MAX_BANDWIDTH / den) * num; |
||
401 | lev->band_num = num; |
||
402 | lev->band_den = den; |
||
403 | |||
404 | lev->scheduling_level = master; |
||
405 | |||
406 | lev->flags = flags & 0x07; |
||
407 | } |
||
408 | |||
409 | bandwidth_t TBS_usedbandwidth(LEVEL l) |
||
410 | { |
||
411 | TBS_level_des *lev = (TBS_level_des *)(level_table[l]); |
||
38 | pj | 412 | |
413 | return lev->U; |
||
2 | pj | 414 | } |
415 | |||
416 | int TBS_get_nact(LEVEL l, PID p) |
||
417 | { |
||
418 | TBS_level_des *lev = (TBS_level_des *)(level_table[l]); |
||
38 | pj | 419 | |
420 | return lev->nact[p]; |
||
2 | pj | 421 | } |
422 |