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: rrsoft.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 scheduling module RRSOFT (Round Robin) |
||
31 | |||
32 | Read rrsoft.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 WARRSOFTANTY; without even the implied waRRSOFTanty 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/rrsoft.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 | /*+ Status used in the level +*/ |
||
66 | #define RRSOFT_READY MODULE_STATUS_BASE |
||
67 | #define RRSOFT_IDLE MODULE_STATUS_BASE+2 |
||
68 | |||
69 | /*+ the level redefinition for the Round Robin level +*/ |
||
70 | typedef struct { |
||
71 | level_des l; /*+ the standard level descriptor +*/ |
||
72 | |||
73 | int nact[MAX_PROC]; /*+ number of pending activations +*/ |
||
74 | |||
29 | pj | 75 | IQUEUE ready; /*+ the ready queue +*/ |
2 | pj | 76 | |
77 | int slice; /*+ the level's time slice +*/ |
||
78 | |||
79 | TIME period[MAX_PROC]; /*+ activation period +*/ |
||
80 | |||
81 | struct timespec reactivation_time[MAX_PROC]; |
||
82 | /*+ the time at witch the reactivation timer is post +*/ |
||
83 | int reactivation_timer[MAX_PROC]; |
||
84 | /*+ the recativation timer +*/ |
||
85 | |||
86 | BYTE periodic[MAX_PROC]; |
||
87 | |||
88 | |||
89 | struct multiboot_info *multiboot; /*+ used if the level have to insert |
||
90 | the main task +*/ |
||
91 | |||
92 | BYTE models; /*+ Task Model that the Module can Handle +*/ |
||
93 | } RRSOFT_level_des; |
||
94 | |||
95 | |||
96 | /* this is the periodic reactivation of the task... it is posted only |
||
97 | if the task is a periodic task */ |
||
98 | static void RRSOFT_timer_reactivate(void *par) |
||
99 | { |
||
100 | PID p = (PID) par; |
||
101 | RRSOFT_level_des *lev; |
||
102 | // kern_printf("react"); |
||
103 | |||
104 | lev = (RRSOFT_level_des *)level_table[proc_table[p].task_level]; |
||
105 | |||
106 | if (proc_table[p].status == RRSOFT_IDLE) { |
||
107 | /* the task has finished the current activation and must be |
||
108 | reactivated */ |
||
109 | proc_table[p].status = RRSOFT_READY; |
||
29 | pj | 110 | iq_insertlast(p,&lev->ready); |
2 | pj | 111 | |
112 | event_need_reschedule(); |
||
113 | } |
||
114 | else if (lev->nact[p] >= 0) |
||
115 | /* the task has not completed the current activation, so we save |
||
116 | the activation incrementing nact... */ |
||
117 | lev->nact[p]++; |
||
118 | |||
119 | /* repost the event at the next period end... */ |
||
120 | ADDUSEC2TIMESPEC(lev->period[p], &lev->reactivation_time[p]); |
||
121 | lev->reactivation_timer[p] = kern_event_post(&lev->reactivation_time[p], |
||
122 | RRSOFT_timer_reactivate, |
||
123 | (void *)p); |
||
124 | /* tracer stuff */ |
||
125 | // trc_logevent(TRC_INTACTIVATION,&p); |
||
126 | } |
||
127 | |||
128 | /* This is not efficient but very fair :-) |
||
129 | The need of all this stuff is because if a task execute a long time |
||
130 | due to (shadow!) priority inheritance, then the task shall go to the |
||
131 | tail of the queue many times... */ |
||
38 | pj | 132 | static PID RRSOFT_public_scheduler(LEVEL l) |
2 | pj | 133 | { |
134 | RRSOFT_level_des *lev = (RRSOFT_level_des *)(level_table[l]); |
||
135 | |||
136 | PID p; |
||
137 | |||
138 | for (;;) { |
||
29 | pj | 139 | p = iq_query_first(&lev->ready); |
2 | pj | 140 | if (p == -1) |
141 | return p; |
||
142 | //{kern_printf("(s%d)",p); return p;} |
||
143 | |||
144 | // kern_printf("(p=%d l=%d avail=%d wcet =%d)\n",p,l,proc_table[p].avail_time, proc_table[p].wcet); |
||
145 | if (proc_table[p].avail_time <= 0) { |
||
146 | proc_table[p].avail_time += proc_table[p].wcet; |
||
29 | pj | 147 | iq_extract(p,&lev->ready); |
148 | iq_insertlast(p,&lev->ready); |
||
2 | pj | 149 | } |
150 | else |
||
151 | //{kern_printf("(s%d)",p); return p;} |
||
152 | return p; |
||
153 | |||
154 | } |
||
155 | } |
||
156 | |||
38 | pj | 157 | static int RRSOFT_public_create(LEVEL l, PID p, TASK_MODEL *m) |
2 | pj | 158 | { |
159 | RRSOFT_level_des *lev = (RRSOFT_level_des *)(level_table[l]); |
||
160 | |||
161 | // kern_printf("create %d mod %d\n",p,m->pclass); |
||
162 | /* the task state is set at SLEEP by the general task_create |
||
163 | the only thing to set remains the capacity stuffs that are set |
||
164 | to the values passed in the model... */ |
||
165 | |||
38 | pj | 166 | if ( !(m->pclass==NRT_PCLASS && lev->models & RRSOFT_ONLY_NRT ) ) return -1; |
167 | if ( !(m->pclass==SOFT_PCLASS && lev->models & RRSOFT_ONLY_SOFT) ) return -1; |
||
168 | if ( !(m->pclass==HARD_PCLASS && lev->models & RRSOFT_ONLY_HARD) ) return -1; |
||
169 | if (m->level != 0 && m->level != l) return -1; |
||
170 | |||
2 | pj | 171 | /* I used the wcet field because using wcet can account if a task |
172 | consume more than the timeslice... */ |
||
173 | |||
174 | if (lev->models & RRSOFT_ONLY_NRT && |
||
175 | (m->pclass == NRT_PCLASS || m->pclass == (NRT_PCLASS | l))) { |
||
176 | NRT_TASK_MODEL *nrt = (NRT_TASK_MODEL *)m; |
||
177 | |||
178 | // kern_printf("nrt"); |
||
179 | if (nrt->slice) { |
||
180 | proc_table[p].avail_time = nrt->slice; |
||
181 | proc_table[p].wcet = nrt->slice; |
||
182 | } |
||
183 | else { |
||
184 | proc_table[p].avail_time = lev->slice; |
||
185 | proc_table[p].wcet = lev->slice; |
||
186 | } |
||
187 | proc_table[p].control |= CONTROL_CAP; |
||
188 | |||
189 | if (nrt->arrivals == SAVE_ARRIVALS) |
||
190 | lev->nact[p] = 0; |
||
191 | else |
||
192 | lev->nact[p] = -1; |
||
193 | |||
194 | lev->periodic[p] = 0; |
||
195 | lev->period[p] = 0; |
||
196 | } |
||
197 | else if (lev->models & RRSOFT_ONLY_SOFT && |
||
198 | (m->pclass == SOFT_PCLASS || m->pclass == (SOFT_PCLASS | l))) { |
||
199 | SOFT_TASK_MODEL *soft = (SOFT_TASK_MODEL *)m; |
||
200 | // kern_printf("soft"); |
||
201 | proc_table[p].avail_time = lev->slice; |
||
202 | proc_table[p].wcet = lev->slice; |
||
203 | proc_table[p].control |= CONTROL_CAP; |
||
204 | |||
205 | if (soft->arrivals == SAVE_ARRIVALS) |
||
206 | lev->nact[p] = 0; |
||
207 | else |
||
208 | lev->nact[p] = -1; |
||
209 | |||
210 | if (soft->periodicity == PERIODIC) { |
||
211 | lev->periodic[p] = 1; |
||
212 | lev->period[p] = soft->period; |
||
213 | } |
||
214 | } |
||
215 | else if (lev->models & RRSOFT_ONLY_HARD && |
||
216 | (m->pclass == HARD_PCLASS || m->pclass == (HARD_PCLASS | l))) { |
||
217 | HARD_TASK_MODEL *hard = (HARD_TASK_MODEL *)m; |
||
218 | // kern_printf("hard"); |
||
219 | proc_table[p].avail_time = lev->slice; |
||
220 | proc_table[p].wcet = lev->slice; |
||
221 | proc_table[p].control |= CONTROL_CAP; |
||
222 | |||
223 | lev->nact[p] = 0; |
||
224 | |||
225 | if (hard->periodicity == PERIODIC) { |
||
226 | lev->periodic[p] = 1; |
||
227 | lev->period[p] = hard->mit; |
||
228 | } |
||
229 | } |
||
230 | |||
231 | return 0; /* OK */ |
||
232 | } |
||
233 | |||
38 | pj | 234 | static void RRSOFT_public_dispatch(LEVEL l, PID p, int nostop) |
2 | pj | 235 | { |
236 | RRSOFT_level_des *lev = (RRSOFT_level_des *)(level_table[l]); |
||
237 | |||
238 | /* the task state is set EXE by the scheduler() |
||
239 | we extract the task from the ready queue |
||
240 | NB: we can't assume that p is the first task in the queue!!! */ |
||
29 | pj | 241 | iq_extract(p, &lev->ready); |
2 | pj | 242 | } |
243 | |||
38 | pj | 244 | static void RRSOFT_public_epilogue(LEVEL l, PID p) |
2 | pj | 245 | { |
246 | RRSOFT_level_des *lev = (RRSOFT_level_des *)(level_table[l]); |
||
247 | |||
248 | /* check if the slice is finished and insert the task in the coRRSOFTect |
||
249 | qqueue position */ |
||
250 | if (proc_table[p].avail_time <= 0) { |
||
251 | proc_table[p].avail_time += proc_table[p].wcet; |
||
29 | pj | 252 | iq_insertlast(p,&lev->ready); |
2 | pj | 253 | } |
254 | else |
||
255 | /* curr is >0, so the running task have to run for another cuRRSOFT usec */ |
||
29 | pj | 256 | iq_insertfirst(p,&lev->ready); |
2 | pj | 257 | |
258 | proc_table[p].status = RRSOFT_READY; |
||
259 | } |
||
260 | |||
38 | pj | 261 | static void RRSOFT_public_activate(LEVEL l, PID p) |
2 | pj | 262 | { |
263 | RRSOFT_level_des *lev = (RRSOFT_level_des *)(level_table[l]); |
||
264 | |||
265 | /* Test if we are trying to activate a non sleeping task */ |
||
266 | /* save activation (only if needed... */ |
||
267 | if (proc_table[p].status != SLEEP && proc_table[p].status != RRSOFT_IDLE) { |
||
268 | if (lev->nact[p] != -1) |
||
269 | lev->nact[p]++; |
||
270 | return; |
||
271 | } |
||
272 | |||
38 | pj | 273 | /* Insert task in the correct position */ |
2 | pj | 274 | proc_table[p].status = RRSOFT_READY; |
29 | pj | 275 | iq_insertlast(p,&lev->ready); |
2 | pj | 276 | |
277 | /* Set the reactivation timer */ |
||
278 | if (lev->periodic[p]) |
||
279 | { |
||
38 | pj | 280 | kern_gettime(&lev->reactivation_time[p]); |
2 | pj | 281 | ADDUSEC2TIMESPEC(lev->period[p], &lev->reactivation_time[p]); |
282 | lev->reactivation_timer[p] = kern_event_post(&lev->reactivation_time[p], |
||
283 | RRSOFT_timer_reactivate, |
||
284 | (void *)p); |
||
285 | } |
||
286 | } |
||
287 | |||
38 | pj | 288 | static void RRSOFT_public_unblock(LEVEL l, PID p) |
2 | pj | 289 | { |
290 | RRSOFT_level_des *lev = (RRSOFT_level_des *)(level_table[l]); |
||
291 | |||
292 | /* Similar to RRSOFT_task_activate, but we don't check in what state |
||
38 | pj | 293 | the task is */ |
2 | pj | 294 | |
295 | /* Insert task in the coRRSOFTect position */ |
||
296 | proc_table[p].status = RRSOFT_READY; |
||
29 | pj | 297 | iq_insertlast(p,&lev->ready); |
2 | pj | 298 | } |
299 | |||
38 | pj | 300 | static void RRSOFT_public_block(LEVEL l, PID p) |
2 | pj | 301 | { |
302 | /* Extract the running task from the level |
||
303 | . we have already extract it from the ready queue at the dispatch time. |
||
304 | . the capacity event have to be removed by the generic kernel |
||
305 | . the wcet don't need modification... |
||
306 | . the state of the task is set by the calling function |
||
307 | |||
308 | So, we do nothing!!! |
||
309 | */ |
||
310 | } |
||
311 | |||
38 | pj | 312 | static int RRSOFT_public_message(LEVEL l, PID p, void *m) |
2 | pj | 313 | { |
314 | RRSOFT_level_des *lev = (RRSOFT_level_des *)(level_table[l]); |
||
315 | |||
316 | if (lev->nact[p] > 0) { |
||
317 | /* continue!!!! */ |
||
318 | lev->nact[p]--; |
||
319 | // qq_insertlast(p,&lev->ready); |
||
29 | pj | 320 | iq_insertfirst(p,&lev->ready); |
2 | pj | 321 | proc_table[p].status = RRSOFT_READY; |
322 | } |
||
323 | else |
||
324 | proc_table[p].status = RRSOFT_IDLE; |
||
38 | pj | 325 | |
326 | jet_update_endcycle(); /* Update the Jet data... */ |
||
327 | trc_logevent(TRC_ENDCYCLE,&exec_shadow); /* tracer stuff */ |
||
328 | |||
329 | return 0; |
||
2 | pj | 330 | } |
331 | |||
38 | pj | 332 | static void RRSOFT_public_end(LEVEL l, PID p) |
2 | pj | 333 | { |
334 | RRSOFT_level_des *lev = (RRSOFT_level_des *)(level_table[l]); |
||
335 | |||
336 | lev->nact[p] = -1; |
||
337 | |||
338 | /* we delete the reactivation timer */ |
||
339 | if (lev->periodic[p]) { |
||
38 | pj | 340 | kern_event_delete(lev->reactivation_timer[p]); |
2 | pj | 341 | lev->reactivation_timer[p] = -1; |
342 | } |
||
343 | |||
344 | /* then, we insert the task in the free queue */ |
||
345 | proc_table[p].status = FREE; |
||
29 | pj | 346 | iq_insertlast(p,&freedesc); |
2 | pj | 347 | } |
348 | |||
349 | /* Registration functions */ |
||
350 | |||
351 | /*+ This init function install the "main" task +*/ |
||
352 | static void RRSOFT_call_main(void *l) |
||
353 | { |
||
354 | LEVEL lev; |
||
355 | PID p; |
||
356 | NRT_TASK_MODEL m; |
||
357 | void *mb; |
||
358 | |||
359 | lev = (LEVEL)l; |
||
360 | |||
361 | nrt_task_default_model(m); |
||
362 | nrt_task_def_level(m,lev); /* with this we are sure that the task aRRSOFTives |
||
363 | to the coRRSOFTect level */ |
||
364 | |||
365 | mb = ((RRSOFT_level_des *)level_table[lev])->multiboot; |
||
366 | nrt_task_def_arg(m,mb); |
||
367 | nrt_task_def_usemath(m); |
||
368 | nrt_task_def_nokill(m); |
||
369 | nrt_task_def_ctrl_jet(m); |
||
370 | |||
371 | p = task_create("Main", __init__, (TASK_MODEL *)&m, NULL); |
||
372 | |||
373 | if (p == NIL) |
||
374 | printk("\nPanic!!! can't create main task...\n"); |
||
375 | |||
38 | pj | 376 | RRSOFT_public_activate(lev,p); |
2 | pj | 377 | } |
378 | |||
379 | |||
380 | /*+ Registration function: |
||
381 | TIME slice the slice for the Round Robin queue |
||
382 | int createmain 1 if the level creates the main task 0 otherwise |
||
383 | struct multiboot_info *mb used if createmain specified +*/ |
||
38 | pj | 384 | LEVEL RRSOFT_register_level(TIME slice, |
2 | pj | 385 | int createmain, |
386 | struct multiboot_info *mb, |
||
387 | BYTE models) |
||
388 | { |
||
389 | LEVEL l; /* the level that we register */ |
||
390 | RRSOFT_level_des *lev; /* for readableness only */ |
||
391 | PID i; |
||
392 | |||
393 | printk("RRSOFT_register_level\n"); |
||
394 | |||
395 | /* request an entry in the level_table */ |
||
38 | pj | 396 | l = level_alloc_descriptor(sizeof(RRSOFT_level_des)); |
2 | pj | 397 | |
38 | pj | 398 | lev = (RRSOFT_level_des *)level_table[l]; |
2 | pj | 399 | |
400 | printk(" lev=%d\n",(int)lev); |
||
401 | |||
402 | /* fill the standard descriptor */ |
||
38 | pj | 403 | lev->l.public_scheduler = RRSOFT_public_scheduler; |
404 | lev->l.public_create = RRSOFT_public_create; |
||
405 | lev->l.public_end = RRSOFT_public_end; |
||
406 | lev->l.public_dispatch = RRSOFT_public_dispatch; |
||
407 | lev->l.public_epilogue = RRSOFT_public_epilogue; |
||
408 | lev->l.public_activate = RRSOFT_public_activate; |
||
409 | lev->l.public_unblock = RRSOFT_public_unblock; |
||
410 | lev->l.public_block = RRSOFT_public_block; |
||
411 | lev->l.public_message = RRSOFT_public_message; |
||
2 | pj | 412 | |
413 | /* fill the RRSOFT descriptor part */ |
||
414 | for (i = 0; i < MAX_PROC; i++) { |
||
415 | lev->nact[i] = -1; |
||
416 | NULL_TIMESPEC(&lev->reactivation_time[i]); |
||
417 | lev->reactivation_timer[i] = -1; |
||
418 | lev->periodic[i] = 0; |
||
419 | lev->period[i] = 0; |
||
420 | } |
||
421 | |||
29 | pj | 422 | iq_init(&lev->ready, &freedesc, 0); |
2 | pj | 423 | |
424 | if (slice < RRSOFT_MINIMUM_SLICE) slice = RRSOFT_MINIMUM_SLICE; |
||
425 | if (slice > RRSOFT_MAXIMUM_SLICE) slice = RRSOFT_MAXIMUM_SLICE; |
||
426 | lev->slice = slice; |
||
427 | |||
428 | lev->multiboot = mb; |
||
429 | |||
430 | lev->models = models; |
||
431 | |||
432 | if (createmain) |
||
433 | sys_atrunlevel(RRSOFT_call_main,(void *) l, RUNLEVEL_INIT); |
||
38 | pj | 434 | |
435 | return l; |
||
2 | pj | 436 | } |
437 | |||
438 | |||
439 | |||
440 |