Rev 38 | 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 | ------------ |
||
137 | giacomo | 23 | CVS : $Id: rr.c,v 1.5 2003-04-28 11:52:05 giacomo Exp $ |
2 | pj | 24 | |
25 | File: $File$ |
||
137 | giacomo | 26 | Revision: $Revision: 1.5 $ |
27 | Last update: $Date: 2003-04-28 11:52:05 $ |
||
2 | pj | 28 | ------------ |
29 | |||
30 | This file contains the scheduling module RR (Round Robin) |
||
31 | |||
32 | Read rr.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/rr.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 | |
38 | pj | 65 | //#define RRDEBUG |
66 | |||
67 | #define rr_printf kern_printf |
||
68 | |||
2 | pj | 69 | /*+ Status used in the level +*/ |
70 | #define RR_READY MODULE_STATUS_BASE |
||
71 | |||
72 | /*+ the level redefinition for the Round Robin level +*/ |
||
73 | typedef struct { |
||
74 | level_des l; /*+ the standard level descriptor +*/ |
||
75 | |||
29 | pj | 76 | IQUEUE ready; /*+ the ready queue +*/ |
2 | pj | 77 | |
78 | int slice; /*+ the level's time slice +*/ |
||
79 | |||
80 | struct multiboot_info *multiboot; /*+ used if the level have to insert |
||
81 | the main task +*/ |
||
82 | } RR_level_des; |
||
83 | |||
84 | /* This is not efficient but very fair :-) |
||
85 | The need of all this stuff is because if a task execute a long time |
||
86 | due to (shadow!) priority inheritance, then the task shall go to the |
||
87 | tail of the queue many times... */ |
||
38 | pj | 88 | static PID RR_public_scheduler(LEVEL l) |
2 | pj | 89 | { |
90 | RR_level_des *lev = (RR_level_des *)(level_table[l]); |
||
91 | |||
92 | PID p; |
||
93 | |||
38 | pj | 94 | #ifdef RRDEBUG |
95 | rr_printf("(RRs",p); |
||
96 | #endif |
||
97 | |||
2 | pj | 98 | for (;;) { |
29 | pj | 99 | p = iq_query_first(&lev->ready); |
38 | pj | 100 | |
101 | if (p == -1) { |
||
102 | #ifdef RRDEBUG |
||
103 | rr_printf(" %d)",p); |
||
104 | #endif |
||
2 | pj | 105 | return p; |
38 | pj | 106 | } |
2 | pj | 107 | |
108 | if (proc_table[p].avail_time <= 0) { |
||
109 | proc_table[p].avail_time += proc_table[p].wcet; |
||
29 | pj | 110 | iq_extract(p,&lev->ready); |
111 | iq_insertlast(p,&lev->ready); |
||
2 | pj | 112 | } |
38 | pj | 113 | else { |
114 | #ifdef RRDEBUG |
||
115 | rr_printf(" %d)",p); |
||
116 | #endif |
||
2 | pj | 117 | return p; |
38 | pj | 118 | } |
2 | pj | 119 | } |
120 | } |
||
121 | |||
38 | pj | 122 | static int RR_public_create(LEVEL l, PID p, TASK_MODEL *m) |
2 | pj | 123 | { |
38 | pj | 124 | RR_level_des *lev = (RR_level_des *)(level_table[l]); |
125 | NRT_TASK_MODEL *nrt; |
||
2 | pj | 126 | |
38 | pj | 127 | #ifdef RRDEBUG |
128 | rr_printf("(create %d!!!!)",p); |
||
129 | #endif |
||
2 | pj | 130 | |
38 | pj | 131 | if (m->pclass != NRT_PCLASS) return -1; |
132 | if (m->level != 0 && m->level != l) return -1; |
||
2 | pj | 133 | |
38 | pj | 134 | nrt = (NRT_TASK_MODEL *)m; |
2 | pj | 135 | /* the task state is set at SLEEP by the general task_create |
136 | the only thing to set remains the capacity stuffs that are set |
||
137 | to the values passed in the model... */ |
||
138 | |||
139 | /* I used the wcet field because using wcet can account if a task |
||
140 | consume more than the timeslice... */ |
||
141 | |||
142 | if (nrt->slice) { |
||
143 | proc_table[p].avail_time = nrt->slice; |
||
144 | proc_table[p].wcet = nrt->slice; |
||
145 | } |
||
146 | else { |
||
147 | proc_table[p].avail_time = lev->slice; |
||
148 | proc_table[p].wcet = lev->slice; |
||
149 | } |
||
150 | proc_table[p].control |= CONTROL_CAP; |
||
151 | |||
38 | pj | 152 | #ifdef RRDEBUG |
153 | rr_printf("(c%d av%d w%d )",p,proc_table[p].avail_time,proc_table[p].wcet); |
||
154 | #endif |
||
2 | pj | 155 | return 0; /* OK */ |
156 | } |
||
157 | |||
38 | pj | 158 | static void RR_public_dispatch(LEVEL l, PID p, int nostop) |
2 | pj | 159 | { |
160 | RR_level_des *lev = (RR_level_des *)(level_table[l]); |
||
161 | |||
162 | /* the task state is set EXE by the scheduler() |
||
163 | we extract the task from the ready queue |
||
164 | NB: we can't assume that p is the first task in the queue!!! */ |
||
29 | pj | 165 | iq_extract(p, &lev->ready); |
38 | pj | 166 | |
167 | #ifdef RRDEBUG |
||
168 | rr_printf("(dis%d)",p); |
||
169 | #endif |
||
2 | pj | 170 | } |
171 | |||
38 | pj | 172 | static void RR_public_epilogue(LEVEL l, PID p) |
2 | pj | 173 | { |
174 | RR_level_des *lev = (RR_level_des *)(level_table[l]); |
||
175 | |||
176 | /* check if the slice is finished and insert the task in the correct |
||
177 | qqueue position */ |
||
178 | if (proc_table[p].avail_time <= 0) { |
||
179 | proc_table[p].avail_time += proc_table[p].wcet; |
||
29 | pj | 180 | iq_insertlast(p,&lev->ready); |
2 | pj | 181 | } |
182 | else |
||
183 | /* curr is >0, so the running task have to run for another curr usec */ |
||
29 | pj | 184 | iq_insertfirst(p,&lev->ready); |
2 | pj | 185 | |
186 | proc_table[p].status = RR_READY; |
||
38 | pj | 187 | |
188 | #ifdef RRDEBUG |
||
189 | rr_printf("(epi%d)",p); |
||
190 | #endif |
||
2 | pj | 191 | } |
192 | |||
38 | pj | 193 | static void RR_public_activate(LEVEL l, PID p) |
2 | pj | 194 | { |
195 | RR_level_des *lev = (RR_level_des *)(level_table[l]); |
||
196 | |||
197 | /* Test if we are trying to activate a non sleeping task */ |
||
198 | /* Ignore this; the task is already active */ |
||
199 | if (proc_table[p].status != SLEEP) |
||
200 | return; |
||
201 | |||
202 | /* Insert task in the correct position */ |
||
203 | proc_table[p].status = RR_READY; |
||
29 | pj | 204 | iq_insertlast(p,&lev->ready); |
38 | pj | 205 | |
206 | #ifdef RRDEBUG |
||
207 | rr_printf("(act%d)",p); |
||
208 | #endif |
||
209 | |||
2 | pj | 210 | } |
211 | |||
38 | pj | 212 | static void RR_public_unblock(LEVEL l, PID p) |
2 | pj | 213 | { |
214 | RR_level_des *lev = (RR_level_des *)(level_table[l]); |
||
215 | |||
38 | pj | 216 | /* Similar to RR_task_activate, |
217 | but we don't check in what state the task is */ |
||
2 | pj | 218 | |
219 | /* Insert task in the correct position */ |
||
220 | proc_table[p].status = RR_READY; |
||
29 | pj | 221 | iq_insertlast(p,&lev->ready); |
38 | pj | 222 | |
223 | #ifdef RRDEBUG |
||
224 | rr_printf("(ubl%d)",p); |
||
225 | #endif |
||
2 | pj | 226 | } |
227 | |||
38 | pj | 228 | static void RR_public_block(LEVEL l, PID p) |
2 | pj | 229 | { |
230 | /* Extract the running task from the level |
||
231 | . we have already extract it from the ready queue at the dispatch time. |
||
232 | . the capacity event have to be removed by the generic kernel |
||
233 | . the wcet don't need modification... |
||
234 | . the state of the task is set by the calling function |
||
235 | |||
236 | So, we do nothing!!! |
||
237 | */ |
||
38 | pj | 238 | #ifdef RRDEBUG |
239 | rr_printf("(bl%d)",p); |
||
240 | #endif |
||
2 | pj | 241 | } |
242 | |||
38 | pj | 243 | static int RR_public_message(LEVEL l, PID p, void *m) |
2 | pj | 244 | { |
38 | pj | 245 | proc_table[p].status = SLEEP; |
2 | pj | 246 | |
38 | pj | 247 | jet_update_endcycle(); /* Update the Jet data... */ |
248 | trc_logevent(TRC_ENDCYCLE,&exec_shadow); /* tracer stuff */ |
||
249 | |||
250 | #ifdef RRDEBUG |
||
251 | rr_printf("(msg%d)",p); |
||
252 | #endif |
||
253 | |||
254 | return 0; |
||
2 | pj | 255 | } |
256 | |||
38 | pj | 257 | static void RR_public_end(LEVEL l, PID p) |
2 | pj | 258 | { |
259 | /* we insert the task in the free queue */ |
||
260 | proc_table[p].status = FREE; |
||
29 | pj | 261 | iq_insertlast(p,&freedesc); |
2 | pj | 262 | |
38 | pj | 263 | #ifdef RRDEBUG |
264 | rr_printf("(end%d)",p); |
||
265 | #endif |
||
2 | pj | 266 | } |
267 | |||
268 | /* Registration functions */ |
||
269 | |||
270 | /*+ This init function install the "main" task +*/ |
||
271 | static void RR_call_main(void *l) |
||
272 | { |
||
273 | LEVEL lev; |
||
274 | PID p; |
||
275 | NRT_TASK_MODEL m; |
||
276 | void *mb; |
||
277 | |||
278 | lev = (LEVEL)l; |
||
279 | |||
280 | nrt_task_default_model(m); |
||
281 | nrt_task_def_level(m,lev); /* with this we are sure that the task arrives |
||
282 | to the correct level */ |
||
283 | |||
284 | mb = ((RR_level_des *)level_table[lev])->multiboot; |
||
285 | nrt_task_def_arg(m,mb); |
||
286 | nrt_task_def_usemath(m); |
||
287 | nrt_task_def_nokill(m); |
||
288 | nrt_task_def_ctrl_jet(m); |
||
137 | giacomo | 289 | nrt_task_def_stack(m,30000); |
2 | pj | 290 | |
291 | p = task_create("Main", __init__, (TASK_MODEL *)&m, NULL); |
||
292 | |||
293 | if (p == NIL) |
||
38 | pj | 294 | printk(KERN_EMERG "Panic!!! can't create main task... errno =%d\n",errno); |
2 | pj | 295 | |
38 | pj | 296 | RR_public_activate(lev,p); |
297 | |||
298 | #ifdef RRDEBUG |
||
299 | rr_printf("(main created %d)",p); |
||
300 | #endif |
||
2 | pj | 301 | } |
302 | |||
303 | |||
304 | /*+ Registration function: |
||
305 | TIME slice the slice for the Round Robin queue |
||
306 | int createmain 1 if the level creates the main task 0 otherwise |
||
307 | struct multiboot_info *mb used if createmain specified +*/ |
||
38 | pj | 308 | LEVEL RR_register_level(TIME slice, |
2 | pj | 309 | int createmain, |
310 | struct multiboot_info *mb) |
||
311 | { |
||
312 | LEVEL l; /* the level that we register */ |
||
313 | RR_level_des *lev; /* for readableness only */ |
||
314 | |||
315 | printk("RR_register_level\n"); |
||
316 | |||
317 | /* request an entry in the level_table */ |
||
38 | pj | 318 | l = level_alloc_descriptor(sizeof(RR_level_des)); |
2 | pj | 319 | |
38 | pj | 320 | lev = (RR_level_des *)level_table[l]; |
2 | pj | 321 | |
322 | printk(" lev=%d\n",(int)lev); |
||
323 | |||
324 | /* fill the standard descriptor */ |
||
38 | pj | 325 | lev->l.public_scheduler = RR_public_scheduler; |
326 | lev->l.public_create = RR_public_create; |
||
327 | lev->l.public_end = RR_public_end; |
||
328 | lev->l.public_dispatch = RR_public_dispatch; |
||
329 | lev->l.public_epilogue = RR_public_epilogue; |
||
330 | lev->l.public_activate = RR_public_activate; |
||
331 | lev->l.public_unblock = RR_public_unblock; |
||
332 | lev->l.public_block = RR_public_block; |
||
333 | lev->l.public_message = RR_public_message; |
||
2 | pj | 334 | |
335 | /* fill the RR descriptor part */ |
||
29 | pj | 336 | iq_init(&lev->ready, &freedesc, 0); |
2 | pj | 337 | |
338 | if (slice < RR_MINIMUM_SLICE) slice = RR_MINIMUM_SLICE; |
||
339 | if (slice > RR_MAXIMUM_SLICE) slice = RR_MAXIMUM_SLICE; |
||
340 | lev->slice = slice; |
||
341 | |||
342 | lev->multiboot = mb; |
||
343 | |||
344 | if (createmain) |
||
345 | sys_atrunlevel(RR_call_main,(void *) l, RUNLEVEL_INIT); |
||
38 | pj | 346 | |
347 | return l; |
||
2 | pj | 348 | } |