Subversion Repositories shark

Rev

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: rr2.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 RR2 (Round Robin) version 2
31
 
32
 Read rr2.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 WARR2ANTY; without even the implied waRR2anty 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/rr2.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 RR2_READY   MODULE_STATUS_BASE
67
 
68
/*+ the level redefinition for the Round Robin level +*/
69
typedef struct {
70
  level_des l;     /*+ the standard level descriptor          +*/
71
 
72
  int nact[MAX_PROC]; /*+ number of pending activations       +*/
73
 
29 pj 74
  IQUEUE ready;    /*+ the ready queue                        +*/
2 pj 75
 
76
  int slice;       /*+ the level's time slice                 +*/
77
 
78
  struct multiboot_info *multiboot; /*+ used if the level have to insert
79
                                        the main task +*/
80
} RR2_level_des;
81
 
82
 
83
/* This is not efficient but very fair :-)
84
   The need of all this stuff is because if a task execute a long time
85
   due to (shadow!) priority inheritance, then the task shall go to the
86
   tail of the queue many times... */
38 pj 87
static PID RR2_public_scheduler(LEVEL l)
2 pj 88
{
89
  RR2_level_des *lev = (RR2_level_des *)(level_table[l]);
90
 
91
  PID p;
92
 
93
  for (;;) {
29 pj 94
    p = iq_query_first(&lev->ready);
2 pj 95
    if (p == -1)
96
      return p;
97
 
98
    if (proc_table[p].avail_time <= 0) {
99
      proc_table[p].avail_time += proc_table[p].wcet;
29 pj 100
      iq_extract(p,&lev->ready);
101
      iq_insertlast(p,&lev->ready);
2 pj 102
    }
103
    else
104
      return p;
105
  }
106
}
107
 
38 pj 108
static int RR2_public_create(LEVEL l, PID p, TASK_MODEL *m)
2 pj 109
{
38 pj 110
  RR2_level_des *lev = (RR2_level_des *)(level_table[l]);
111
  NRT_TASK_MODEL *nrt;
2 pj 112
 
38 pj 113
  if (m->pclass != NRT_PCLASS) return -1;
114
  if (m->level != 0 && m->level != l) return -1;
115
  nrt = (NRT_TASK_MODEL *)m;
2 pj 116
 
117
  /* the task state is set at SLEEP by the general task_create
118
     the only thing to set remains the capacity stuffs that are set
119
     to the values passed in the model... */
120
 
121
  /* I used the wcet field because using wcet can account if a task
122
     consume more than the timeslice... */
123
 
124
  if (nrt->slice) {
125
    proc_table[p].avail_time = nrt->slice;
126
    proc_table[p].wcet       = nrt->slice;
127
  }
128
  else {
129
    proc_table[p].avail_time = lev->slice;
130
    proc_table[p].wcet       = lev->slice;
131
  }
132
  proc_table[p].control   |= CONTROL_CAP;
133
 
134
  if (nrt->arrivals == SAVE_ARRIVALS)
135
    lev->nact[p] = 0;
136
  else
137
    lev->nact[p] = -1;
138
 
139
  return 0; /* OK */
140
}
141
 
38 pj 142
static void RR2_public_dispatch(LEVEL l, PID p, int nostop)
2 pj 143
{
144
  RR2_level_des *lev = (RR2_level_des *)(level_table[l]);
145
 
146
  /* the task state is set EXE by the scheduler()
147
     we extract the task from the ready queue
148
     NB: we can't assume that p is the first task in the queue!!! */
29 pj 149
  iq_extract(p, &lev->ready);
2 pj 150
}
151
 
38 pj 152
static void RR2_public_epilogue(LEVEL l, PID p)
2 pj 153
{
154
  RR2_level_des *lev = (RR2_level_des *)(level_table[l]);
155
 
156
  /* check if the slice is finished and insert the task in the coRR2ect
157
     qqueue position */
158
  if (proc_table[p].avail_time <= 0) {
159
    proc_table[p].avail_time += proc_table[p].wcet;
29 pj 160
    iq_insertlast(p,&lev->ready);
2 pj 161
  }
162
  else
163
    /* cuRR2 is >0, so the running task have to run for another cuRR2 usec */
29 pj 164
    iq_insertfirst(p,&lev->ready);
2 pj 165
 
166
  proc_table[p].status = RR2_READY;
167
}
168
 
38 pj 169
static void RR2_public_activate(LEVEL l, PID p)
2 pj 170
{
171
  RR2_level_des *lev = (RR2_level_des *)(level_table[l]);
172
 
173
  /* Test if we are trying to activate a non sleeping task    */
174
  /* save activation (only if needed... */
175
  if (proc_table[p].status != SLEEP) {
176
    if (lev->nact[p] != -1)
177
      lev->nact[p]++;
178
    return;
179
  }
180
 
181
  /* Insert task in the coRR2ect position */
182
  proc_table[p].status = RR2_READY;
29 pj 183
  iq_insertlast(p,&lev->ready);
2 pj 184
}
185
 
38 pj 186
static void RR2_public_unblock(LEVEL l, PID p)
2 pj 187
{
188
  RR2_level_des *lev = (RR2_level_des *)(level_table[l]);
189
 
38 pj 190
  /* Similar to RR2_task_activate,
191
     but we don't check in what state the task is */
2 pj 192
 
193
  /* Insert task in the coRR2ect position */
194
  proc_table[p].status = RR2_READY;
29 pj 195
  iq_insertlast(p,&lev->ready);
2 pj 196
}
197
 
38 pj 198
static void RR2_public_block(LEVEL l, PID p)
2 pj 199
{
200
  /* Extract the running task from the level
201
     . we have already extract it from the ready queue at the dispatch time.
202
     . the capacity event have to be removed by the generic kernel
203
     . the wcet don't need modification...
204
     . the state of the task is set by the calling function
205
 
206
     So, we do nothing!!!
207
  */
208
}
209
 
38 pj 210
static int RR2_public_message(LEVEL l, PID p, void *m)
2 pj 211
{
212
  RR2_level_des *lev = (RR2_level_des *)(level_table[l]);
213
 
214
  if (lev->nact[p] > 0) {
215
    /* continue!!!! */
216
    lev->nact[p]--;
29 pj 217
    iq_insertfirst(p,&lev->ready);
2 pj 218
    proc_table[p].status = RR2_READY;
219
  }
220
  else
221
    proc_table[p].status = SLEEP;
38 pj 222
 
223
  jet_update_endcycle(); /* Update the Jet data... */
224
  trc_logevent(TRC_ENDCYCLE,&exec_shadow); /* tracer stuff */
225
 
226
  return 0;
2 pj 227
}
228
 
38 pj 229
static void RR2_public_end(LEVEL l, PID p)
2 pj 230
{
231
  RR2_level_des *lev = (RR2_level_des *)(level_table[l]);
232
 
233
  lev->nact[p] = -1;
234
 
235
  /* then, we insert the task in the free queue */
236
  proc_table[p].status = FREE;
29 pj 237
  iq_insertlast(p,&freedesc);
2 pj 238
}
239
 
240
/* Registration functions */
241
 
242
/*+ This init function install the "main" task +*/
243
static void RR2_call_main(void *l)
244
{
245
  LEVEL lev;
246
  PID p;
247
  NRT_TASK_MODEL m;
248
  void *mb;
249
 
250
  lev = (LEVEL)l;
251
 
252
  nrt_task_default_model(m);
253
  nrt_task_def_level(m,lev); /* with this we are sure that the task aRR2ives
254
                                to the coRR2ect level */
255
 
256
  mb = ((RR2_level_des *)level_table[lev])->multiboot;
257
  nrt_task_def_arg(m,mb);
258
  nrt_task_def_usemath(m);
259
  nrt_task_def_nokill(m);
260
  nrt_task_def_ctrl_jet(m);
261
 
262
  p = task_create("Main", __init__, (TASK_MODEL *)&m, NULL);
263
 
264
  if (p == NIL)
265
    printk("\nPanic!!! can't create main task...\n");
266
 
38 pj 267
  RR2_public_activate(lev,p);
2 pj 268
}
269
 
270
 
271
/*+ Registration function:
272
    TIME slice                the slice for the Round Robin queue
273
    int createmain            1 if the level creates the main task 0 otherwise
274
    struct multiboot_info *mb used if createmain specified   +*/
38 pj 275
LEVEL RR2_register_level(TIME slice,
2 pj 276
                       int createmain,
277
                       struct multiboot_info *mb)
278
{
38 pj 279
  LEVEL l;             /* the level that we register */
2 pj 280
  RR2_level_des *lev;  /* for readableness only */
281
  PID i;
282
 
283
  printk("RR2_register_level\n");
284
 
285
  /* request an entry in the level_table */
38 pj 286
  l = level_alloc_descriptor(sizeof(RR2_level_des));
2 pj 287
 
38 pj 288
  lev = (RR2_level_des *)level_table[l];
2 pj 289
 
290
  printk("    lev=%d\n",(int)lev);
291
 
292
  /* fill the standard descriptor */
38 pj 293
  lev->l.public_scheduler = RR2_public_scheduler;
294
  lev->l.public_create    = RR2_public_create;
295
  lev->l.public_end       = RR2_public_end;
296
  lev->l.public_dispatch  = RR2_public_dispatch;
297
  lev->l.public_epilogue  = RR2_public_epilogue;
298
  lev->l.public_activate  = RR2_public_activate;
299
  lev->l.public_unblock   = RR2_public_unblock;
300
  lev->l.public_block     = RR2_public_block;
301
  lev->l.public_message   = RR2_public_message;
2 pj 302
 
303
  /* fill the RR2 descriptor part */
304
  for (i = 0; i < MAX_PROC; i++)
305
    lev->nact[i] = -1;
306
 
29 pj 307
  iq_init(&lev->ready, &freedesc, 0);
2 pj 308
 
309
  if (slice < RR2_MINIMUM_SLICE) slice = RR2_MINIMUM_SLICE;
310
  if (slice > RR2_MAXIMUM_SLICE) slice = RR2_MAXIMUM_SLICE;
311
  lev->slice      = slice;
312
 
313
  lev->multiboot  = mb;
314
 
315
  if (createmain)
316
    sys_atrunlevel(RR2_call_main,(void *) l, RUNLEVEL_INIT);
38 pj 317
 
318
  return l;
2 pj 319
}
320
 
321