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: rr.c,v 1.7 2004-01-08 20:10:42 giacomo Exp $
2 pj 24
 
25
 File:        $File$
385 giacomo 26
 Revision:    $Revision: 1.7 $
27
 Last update: $Date: 2004-01-08 20:10:42 $
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>
63
 
353 giacomo 64
#include <tracer.h>
65
 
38 pj 66
//#define RRDEBUG
67
 
68
#define rr_printf kern_printf
69
 
2 pj 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
 
29 pj 77
  IQUEUE ready;    /*+ the ready queue                        +*/
2 pj 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... */
38 pj 89
static PID RR_public_scheduler(LEVEL l)
2 pj 90
{
91
  RR_level_des *lev = (RR_level_des *)(level_table[l]);
92
 
93
  PID p;
94
 
38 pj 95
#ifdef RRDEBUG
96
      rr_printf("(RRs",p);
97
#endif
98
 
2 pj 99
  for (;;) {
29 pj 100
    p = iq_query_first(&lev->ready);
38 pj 101
 
102
    if (p == -1) {
103
#ifdef RRDEBUG
104
      rr_printf(" %d)",p);
105
#endif
2 pj 106
      return p;
38 pj 107
    }
2 pj 108
 
109
    if (proc_table[p].avail_time <= 0) {
110
      proc_table[p].avail_time += proc_table[p].wcet;
29 pj 111
      iq_extract(p,&lev->ready);
112
      iq_insertlast(p,&lev->ready);
2 pj 113
    }
38 pj 114
    else {
115
#ifdef RRDEBUG
116
      rr_printf(" %d)",p);
117
#endif
2 pj 118
      return p;
38 pj 119
    }
2 pj 120
  }
121
}
122
 
38 pj 123
static int RR_public_create(LEVEL l, PID p, TASK_MODEL *m)
2 pj 124
{
38 pj 125
  RR_level_des *lev = (RR_level_des *)(level_table[l]);
126
  NRT_TASK_MODEL *nrt;
2 pj 127
 
38 pj 128
#ifdef RRDEBUG
129
  rr_printf("(create %d!!!!)",p);
130
#endif
2 pj 131
 
38 pj 132
  if (m->pclass != NRT_PCLASS) return -1;
133
  if (m->level != 0 && m->level != l) return -1;
2 pj 134
 
38 pj 135
  nrt = (NRT_TASK_MODEL *)m;
2 pj 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
 
38 pj 153
#ifdef RRDEBUG
154
  rr_printf("(c%d av%d w%d )",p,proc_table[p].avail_time,proc_table[p].wcet);
155
#endif
2 pj 156
  return 0; /* OK */
157
}
158
 
38 pj 159
static void RR_public_dispatch(LEVEL l, PID p, int nostop)
2 pj 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!!! */
29 pj 166
  iq_extract(p, &lev->ready);
38 pj 167
 
168
#ifdef RRDEBUG
169
  rr_printf("(dis%d)",p);
170
#endif
2 pj 171
}
172
 
38 pj 173
static void RR_public_epilogue(LEVEL l, PID p)
2 pj 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;
29 pj 181
    iq_insertlast(p,&lev->ready);
2 pj 182
  }
183
  else
184
    /* curr is >0, so the running task have to run for another curr usec */
29 pj 185
    iq_insertfirst(p,&lev->ready);
2 pj 186
 
187
  proc_table[p].status = RR_READY;
38 pj 188
 
189
#ifdef RRDEBUG
190
  rr_printf("(epi%d)",p);
191
#endif
2 pj 192
}
193
 
38 pj 194
static void RR_public_activate(LEVEL l, PID p)
2 pj 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;
29 pj 205
  iq_insertlast(p,&lev->ready);
38 pj 206
 
207
#ifdef RRDEBUG
208
  rr_printf("(act%d)",p);
209
#endif
210
 
2 pj 211
}
212
 
38 pj 213
static void RR_public_unblock(LEVEL l, PID p)
2 pj 214
{
215
  RR_level_des *lev = (RR_level_des *)(level_table[l]);
216
 
38 pj 217
  /* Similar to RR_task_activate,
218
     but we don't check in what state the task is */
2 pj 219
 
220
  /* Insert task in the correct position */
221
  proc_table[p].status = RR_READY;
29 pj 222
  iq_insertlast(p,&lev->ready);
38 pj 223
 
224
#ifdef RRDEBUG
225
  rr_printf("(ubl%d)",p);
226
#endif
2 pj 227
}
228
 
38 pj 229
static void RR_public_block(LEVEL l, PID p)
2 pj 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
  */
38 pj 239
#ifdef RRDEBUG
240
  rr_printf("(bl%d)",p);
241
#endif
2 pj 242
}
243
 
38 pj 244
static int RR_public_message(LEVEL l, PID p, void *m)
2 pj 245
{
38 pj 246
  proc_table[p].status = SLEEP;
2 pj 247
 
38 pj 248
  jet_update_endcycle(); /* Update the Jet data... */
385 giacomo 249
  TRACER_LOGEVENT(FTrace_EVT_task_end_cycle,3,proc_table[p].context,l); /* tracer stuff */
38 pj 250
 
251
#ifdef RRDEBUG
252
  rr_printf("(msg%d)",p);
253
#endif
254
 
255
  return 0;
2 pj 256
}
257
 
38 pj 258
static void RR_public_end(LEVEL l, PID p)
2 pj 259
{
260
  /* we insert the task in the free queue */
261
  proc_table[p].status = FREE;
29 pj 262
  iq_insertlast(p,&freedesc);
2 pj 263
 
38 pj 264
#ifdef RRDEBUG
265
  rr_printf("(end%d)",p);
266
#endif
2 pj 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);
137 giacomo 290
  nrt_task_def_stack(m,30000);
2 pj 291
 
292
  p = task_create("Main", __init__, (TASK_MODEL *)&m, NULL);
293
 
294
  if (p == NIL)
38 pj 295
    printk(KERN_EMERG "Panic!!! can't create main task... errno =%d\n",errno);
2 pj 296
 
38 pj 297
  RR_public_activate(lev,p);
298
 
299
#ifdef RRDEBUG
300
  rr_printf("(main created %d)",p);
301
#endif
2 pj 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   +*/
38 pj 309
LEVEL RR_register_level(TIME slice,
2 pj 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 */
38 pj 319
  l = level_alloc_descriptor(sizeof(RR_level_des));
2 pj 320
 
38 pj 321
  lev = (RR_level_des *)level_table[l];
2 pj 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
}