Subversion Repositories shark

Rev

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