Subversion Repositories shark

Rev

Rev 29 | Go to most recent revision | 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: rr.c,v 1.4 2003-01-07 17:07:50 pj Exp $
2 pj 24
 
25
 File:        $File$
38 pj 26
 Revision:    $Revision: 1.4 $
27
 Last update: $Date: 2003-01-07 17:07:50 $
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);
289
 
290
  p = task_create("Main", __init__, (TASK_MODEL *)&m, NULL);
291
 
292
  if (p == NIL)
38 pj 293
    printk(KERN_EMERG "Panic!!! can't create main task... errno =%d\n",errno);
2 pj 294
 
38 pj 295
  RR_public_activate(lev,p);
296
 
297
#ifdef RRDEBUG
298
  rr_printf("(main created %d)",p);
299
#endif
2 pj 300
}
301
 
302
 
303
/*+ Registration function:
304
    TIME slice                the slice for the Round Robin queue
305
    int createmain            1 if the level creates the main task 0 otherwise
306
    struct multiboot_info *mb used if createmain specified   +*/
38 pj 307
LEVEL RR_register_level(TIME slice,
2 pj 308
                       int createmain,
309
                       struct multiboot_info *mb)
310
{
311
  LEVEL l;            /* the level that we register */
312
  RR_level_des *lev;  /* for readableness only */
313
 
314
  printk("RR_register_level\n");
315
 
316
  /* request an entry in the level_table */
38 pj 317
  l = level_alloc_descriptor(sizeof(RR_level_des));
2 pj 318
 
38 pj 319
  lev = (RR_level_des *)level_table[l];
2 pj 320
 
321
  printk("    lev=%d\n",(int)lev);
322
 
323
  /* fill the standard descriptor */
38 pj 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;
2 pj 333
 
334
  /* fill the RR descriptor part */
29 pj 335
  iq_init(&lev->ready, &freedesc, 0);
2 pj 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);
38 pj 345
 
346
  return l;
2 pj 347
}