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