Subversion Repositories shark

Rev

Rev 14 | 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
 ------------
29 pj 23
 CVS :        $Id: rr2.c,v 1.3 2002-11-11 08:32:06 pj Exp $
2 pj 24
 
25
 File:        $File$
29 pj 26
 Revision:    $Revision: 1.3 $
27
 Last update: $Date: 2002-11-11 08:32:06 $
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
 
64
/*+ Status used in the level +*/
65
#define RR2_READY   MODULE_STATUS_BASE
66
 
67
/*+ the level redefinition for the Round Robin level +*/
68
typedef struct {
69
  level_des l;     /*+ the standard level descriptor          +*/
70
 
71
  int nact[MAX_PROC]; /*+ number of pending activations       +*/
72
 
29 pj 73
  IQUEUE ready;    /*+ the ready queue                        +*/
2 pj 74
 
75
  int slice;       /*+ the level's time slice                 +*/
76
 
77
  struct multiboot_info *multiboot; /*+ used if the level have to insert
78
                                        the main task +*/
79
} RR2_level_des;
80
 
81
 
82
static char *RR2_status_to_a(WORD status)
83
{
84
  if (status < MODULE_STATUS_BASE)
85
    return status_to_a(status);
86
 
87
  switch (status) {
88
    case RR2_READY: return "RR2_Ready";
89
    default      : return "RR2_Unknown";
90
  }
91
}
92
 
93
static int RR2_level_accept_task_model(LEVEL l, TASK_MODEL *m)
94
{
95
  if (m->pclass == NRT_PCLASS || m->pclass == (NRT_PCLASS | l))
96
    return 0;
97
  else
98
    return -1;
99
}
100
 
101
static int RR2_level_accept_guest_model(LEVEL l, TASK_MODEL *m)
102
{
103
    return -1;
104
}
105
 
106
static void RR2_level_status(LEVEL l)
107
{
108
  RR2_level_des *lev = (RR2_level_des *)(level_table[l]);
29 pj 109
  PID p = iq_query_first(&lev->ready);
2 pj 110
 
111
  kern_printf("Slice: %d \n", lev->slice);
112
 
113
  while (p != NIL) {
114
    kern_printf("Pid: %d\t Name: %20s Status: %s\n",p,proc_table[p].name,
115
              RR2_status_to_a(proc_table[p].status));
29 pj 116
    p = iq_query_next(p, &lev->ready);
2 pj 117
  }
118
 
119
  for (p=0; p<MAX_PROC; p++)
120
    if (proc_table[p].task_level == l && proc_table[p].status != RR2_READY
121
        && proc_table[p].status != FREE )
122
      kern_printf("Pid: %d\t Name: %20s Status: %s\n",p,proc_table[p].name,
123
                RR2_status_to_a(proc_table[p].status));
124
 
125
}
126
 
127
 
128
/* This is not efficient but very fair :-)
129
   The need of all this stuff is because if a task execute a long time
130
   due to (shadow!) priority inheritance, then the task shall go to the
131
   tail of the queue many times... */
132
static PID RR2_level_scheduler(LEVEL l)
133
{
134
  RR2_level_des *lev = (RR2_level_des *)(level_table[l]);
135
 
136
  PID p;
137
 
138
  for (;;) {
29 pj 139
    p = iq_query_first(&lev->ready);
2 pj 140
    if (p == -1)
141
      return p;
142
 
143
    if (proc_table[p].avail_time <= 0) {
144
      proc_table[p].avail_time += proc_table[p].wcet;
29 pj 145
      iq_extract(p,&lev->ready);
146
      iq_insertlast(p,&lev->ready);
2 pj 147
    }
148
    else
149
      return p;
150
  }
151
}
152
 
153
static int RR2_level_guarantee(LEVEL l, bandwidth_t *freebandwidth)
154
{
155
  /* the RR2 level always guarantee... the function is defined because
156
     there can be an aperiodic server at a level with less priority than
157
     the RR2 that need guarantee (e.g., a TBS server) */
158
  return 1;
159
}
160
 
161
 
162
static int RR2_task_create(LEVEL l, PID p, TASK_MODEL *m)
163
{
164
  RR2_level_des *lev = (RR2_level_des *)(level_table[l]);
165
  NRT_TASK_MODEL *nrt = (NRT_TASK_MODEL *)m;
166
 
167
  /* the task state is set at SLEEP by the general task_create
168
     the only thing to set remains the capacity stuffs that are set
169
     to the values passed in the model... */
170
 
171
  /* I used the wcet field because using wcet can account if a task
172
     consume more than the timeslice... */
173
 
174
  if (nrt->slice) {
175
    proc_table[p].avail_time = nrt->slice;
176
    proc_table[p].wcet       = nrt->slice;
177
  }
178
  else {
179
    proc_table[p].avail_time = lev->slice;
180
    proc_table[p].wcet       = lev->slice;
181
  }
182
  proc_table[p].control   |= CONTROL_CAP;
183
 
184
  if (nrt->arrivals == SAVE_ARRIVALS)
185
    lev->nact[p] = 0;
186
  else
187
    lev->nact[p] = -1;
188
 
189
  return 0; /* OK */
190
}
191
 
192
static void RR2_task_detach(LEVEL l, PID p)
193
{
194
  /* the RR2 level doesn't introduce any new field in the TASK_MODEL
195
     so, all detach stuffs are done by the task_create
196
     The task state is set at FREE by the general task_create */
197
}
198
 
199
static int RR2_task_eligible(LEVEL l, PID p)
200
{
201
  return 0; /* if the task p is chosen, it is always eligible */
202
}
203
 
204
static void RR2_task_dispatch(LEVEL l, PID p, int nostop)
205
{
206
  RR2_level_des *lev = (RR2_level_des *)(level_table[l]);
207
 
208
  /* the task state is set EXE by the scheduler()
209
     we extract the task from the ready queue
210
     NB: we can't assume that p is the first task in the queue!!! */
29 pj 211
  iq_extract(p, &lev->ready);
2 pj 212
}
213
 
214
static void RR2_task_epilogue(LEVEL l, PID p)
215
{
216
  RR2_level_des *lev = (RR2_level_des *)(level_table[l]);
217
 
218
  /* check if the slice is finished and insert the task in the coRR2ect
219
     qqueue position */
220
  if (proc_table[p].avail_time <= 0) {
221
    proc_table[p].avail_time += proc_table[p].wcet;
29 pj 222
    iq_insertlast(p,&lev->ready);
2 pj 223
  }
224
  else
225
    /* cuRR2 is >0, so the running task have to run for another cuRR2 usec */
29 pj 226
    iq_insertfirst(p,&lev->ready);
2 pj 227
 
228
  proc_table[p].status = RR2_READY;
229
}
230
 
231
static void RR2_task_activate(LEVEL l, PID p)
232
{
233
  RR2_level_des *lev = (RR2_level_des *)(level_table[l]);
234
 
235
  /* Test if we are trying to activate a non sleeping task    */
236
  /* save activation (only if needed... */
237
  if (proc_table[p].status != SLEEP) {
238
    if (lev->nact[p] != -1)
239
      lev->nact[p]++;
240
    return;
241
  }
242
 
243
  ll_gettime(TIME_EXACT, &proc_table[p].request_time);
244
 
245
  /* Insert task in the coRR2ect position */
246
  proc_table[p].status = RR2_READY;
29 pj 247
  iq_insertlast(p,&lev->ready);
2 pj 248
}
249
 
250
static void RR2_task_insert(LEVEL l, PID p)
251
{
252
  RR2_level_des *lev = (RR2_level_des *)(level_table[l]);
253
 
254
  /* Similar to RR2_task_activate, but we don't check in what state
255
     the task is and we don't set the request_time */
256
 
257
  /* Insert task in the coRR2ect position */
258
  proc_table[p].status = RR2_READY;
29 pj 259
  iq_insertlast(p,&lev->ready);
2 pj 260
}
261
 
262
static void RR2_task_extract(LEVEL l, PID p)
263
{
264
  /* Extract the running task from the level
265
     . we have already extract it from the ready queue at the dispatch time.
266
     . the capacity event have to be removed by the generic kernel
267
     . the wcet don't need modification...
268
     . the state of the task is set by the calling function
269
 
270
     So, we do nothing!!!
271
  */
272
}
273
 
274
static void RR2_task_endcycle(LEVEL l, PID p)
275
{
276
  RR2_level_des *lev = (RR2_level_des *)(level_table[l]);
277
 
278
  if (lev->nact[p] > 0) {
279
    /* continue!!!! */
280
    ll_gettime(TIME_EXACT, &proc_table[p].request_time);
281
    lev->nact[p]--;
29 pj 282
    iq_insertfirst(p,&lev->ready);
2 pj 283
    proc_table[p].status = RR2_READY;
284
  }
285
  else
286
    proc_table[p].status = SLEEP;
287
}
288
 
289
static void RR2_task_end(LEVEL l, PID p)
290
{
291
  RR2_level_des *lev = (RR2_level_des *)(level_table[l]);
292
 
293
  lev->nact[p] = -1;
294
 
295
  /* then, we insert the task in the free queue */
296
  proc_table[p].status = FREE;
29 pj 297
  iq_insertlast(p,&freedesc);
2 pj 298
}
299
 
300
static void RR2_task_sleep(LEVEL l, PID p)
301
{
302
  RR2_level_des *lev = (RR2_level_des *)(level_table[l]);
303
  if (lev->nact[p] >= 0) lev->nact[p] = 0;
304
  proc_table[p].status = SLEEP;
305
}
306
 
307
static int RR2_guest_create(LEVEL l, PID p, TASK_MODEL *m)
14 pj 308
{ kern_raise(XINVALID_GUEST,exec_shadow); return 0; }
2 pj 309
 
310
static void RR2_guest_detach(LEVEL l, PID p)
14 pj 311
{ kern_raise(XINVALID_GUEST,exec_shadow); }
2 pj 312
 
313
static void RR2_guest_dispatch(LEVEL l, PID p, int nostop)
14 pj 314
{ kern_raise(XINVALID_GUEST,exec_shadow); }
2 pj 315
 
316
static void RR2_guest_epilogue(LEVEL l, PID p)
14 pj 317
{ kern_raise(XINVALID_GUEST,exec_shadow); }
2 pj 318
 
319
static void RR2_guest_activate(LEVEL l, PID p)
14 pj 320
{ kern_raise(XINVALID_GUEST,exec_shadow); }
2 pj 321
 
322
static void RR2_guest_insert(LEVEL l, PID p)
14 pj 323
{ kern_raise(XINVALID_GUEST,exec_shadow); }
2 pj 324
 
325
static void RR2_guest_extract(LEVEL l, PID p)
14 pj 326
{ kern_raise(XINVALID_GUEST,exec_shadow); }
2 pj 327
 
328
static void RR2_guest_endcycle(LEVEL l, PID p)
14 pj 329
{ kern_raise(XINVALID_GUEST,exec_shadow); }
2 pj 330
 
331
static void RR2_guest_end(LEVEL l, PID p)
14 pj 332
{ kern_raise(XINVALID_GUEST,exec_shadow); }
2 pj 333
 
334
static void RR2_guest_sleep(LEVEL l, PID p)
14 pj 335
{ kern_raise(XINVALID_GUEST,exec_shadow); }
2 pj 336
 
337
 
338
 
339
 
340
/* Registration functions */
341
 
342
/*+ This init function install the "main" task +*/
343
static void RR2_call_main(void *l)
344
{
345
  LEVEL lev;
346
  PID p;
347
  NRT_TASK_MODEL m;
348
  void *mb;
349
 
350
  lev = (LEVEL)l;
351
 
352
  nrt_task_default_model(m);
353
  nrt_task_def_level(m,lev); /* with this we are sure that the task aRR2ives
354
                                to the coRR2ect level */
355
 
356
  mb = ((RR2_level_des *)level_table[lev])->multiboot;
357
  nrt_task_def_arg(m,mb);
358
  nrt_task_def_usemath(m);
359
  nrt_task_def_nokill(m);
360
  nrt_task_def_ctrl_jet(m);
361
 
362
  p = task_create("Main", __init__, (TASK_MODEL *)&m, NULL);
363
 
364
  if (p == NIL)
365
    printk("\nPanic!!! can't create main task...\n");
366
 
367
  RR2_task_activate(lev,p);
368
}
369
 
370
 
371
/*+ Registration function:
372
    TIME slice                the slice for the Round Robin queue
373
    int createmain            1 if the level creates the main task 0 otherwise
374
    struct multiboot_info *mb used if createmain specified   +*/
375
void RR2_register_level(TIME slice,
376
                       int createmain,
377
                       struct multiboot_info *mb)
378
{
379
  LEVEL l;            /* the level that we register */
380
  RR2_level_des *lev;  /* for readableness only */
381
  PID i;
382
 
383
  printk("RR2_register_level\n");
384
 
385
  /* request an entry in the level_table */
386
  l = level_alloc_descriptor();
387
 
388
  /* alloc the space needed for the RR2_level_des */
389
  lev = (RR2_level_des *)kern_alloc(sizeof(RR2_level_des));
390
 
391
  printk("    lev=%d\n",(int)lev);
392
 
393
  /* update the level_table with the new entry */
394
  level_table[l] = (level_des *)lev;
395
 
396
  /* fill the standard descriptor */
397
  strncpy(lev->l.level_name,  RR2_LEVELNAME, MAX_LEVELNAME);
398
  lev->l.level_code               = RR2_LEVEL_CODE;
399
  lev->l.level_version            = RR2_LEVEL_VERSION;
400
 
401
  lev->l.level_accept_task_model  = RR2_level_accept_task_model;
402
  lev->l.level_accept_guest_model = RR2_level_accept_guest_model;
403
  lev->l.level_status             = RR2_level_status;
404
  lev->l.level_scheduler          = RR2_level_scheduler;
405
  lev->l.level_guarantee          = RR2_level_guarantee;
406
 
407
  lev->l.task_create              = RR2_task_create;
408
  lev->l.task_detach              = RR2_task_detach;
409
  lev->l.task_eligible            = RR2_task_eligible;
410
  lev->l.task_dispatch            = RR2_task_dispatch;
411
  lev->l.task_epilogue            = RR2_task_epilogue;
412
  lev->l.task_activate            = RR2_task_activate;
413
  lev->l.task_insert              = RR2_task_insert;
414
  lev->l.task_extract             = RR2_task_extract;
415
  lev->l.task_endcycle            = RR2_task_endcycle;
416
  lev->l.task_end                 = RR2_task_end;
417
  lev->l.task_sleep               = RR2_task_sleep;
418
 
419
  lev->l.guest_create             = RR2_guest_create;
420
  lev->l.guest_detach             = RR2_guest_detach;
421
  lev->l.guest_dispatch           = RR2_guest_dispatch;
422
  lev->l.guest_epilogue           = RR2_guest_epilogue;
423
  lev->l.guest_activate           = RR2_guest_activate;
424
  lev->l.guest_insert             = RR2_guest_insert;
425
  lev->l.guest_extract            = RR2_guest_extract;
426
  lev->l.guest_endcycle           = RR2_guest_endcycle;
427
  lev->l.guest_end                = RR2_guest_end;
428
  lev->l.guest_sleep              = RR2_guest_sleep;
429
 
430
  /* fill the RR2 descriptor part */
431
  for (i = 0; i < MAX_PROC; i++)
432
    lev->nact[i] = -1;
433
 
29 pj 434
  iq_init(&lev->ready, &freedesc, 0);
2 pj 435
 
436
  if (slice < RR2_MINIMUM_SLICE) slice = RR2_MINIMUM_SLICE;
437
  if (slice > RR2_MAXIMUM_SLICE) slice = RR2_MAXIMUM_SLICE;
438
  lev->slice      = slice;
439
 
440
  lev->multiboot  = mb;
441
 
442
  if (createmain)
443
    sys_atrunlevel(RR2_call_main,(void *) l, RUNLEVEL_INIT);
444
}
445
 
446