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: rrsoft.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 RRSOFT (Round Robin)
31
 
32
 Read rrsoft.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 WARRSOFTANTY; without even the implied waRRSOFTanty 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/rrsoft.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 RRSOFT_READY   MODULE_STATUS_BASE
68
#define RRSOFT_IDLE    MODULE_STATUS_BASE+2
69
 
70
/*+ the level redefinition for the Round Robin level +*/
71
typedef struct {
72
  level_des l;     /*+ the standard level descriptor          +*/
73
 
74
  int nact[MAX_PROC]; /*+ number of pending activations       +*/
75
 
29 pj 76
  IQUEUE ready;    /*+ the ready queue                        +*/
2 pj 77
 
78
  int slice;       /*+ the level's time slice                 +*/
79
 
80
  TIME period[MAX_PROC]; /*+  activation period               +*/
81
 
82
  struct timespec reactivation_time[MAX_PROC];
83
        /*+ the time at witch  the reactivation timer is post +*/
84
  int reactivation_timer[MAX_PROC];
85
                                   /*+ the recativation timer +*/
86
 
87
  BYTE periodic[MAX_PROC];
88
 
89
 
90
  struct multiboot_info *multiboot; /*+ used if the level have to insert
91
                                        the main task +*/
92
 
93
  BYTE models;      /*+ Task Model that the Module can Handle +*/
94
} RRSOFT_level_des;
95
 
96
 
97
/* this is the periodic reactivation of the task... it is posted only
98
   if the task is a periodic task */
99
static void RRSOFT_timer_reactivate(void *par)
100
{
101
  PID p = (PID) par;
102
  RRSOFT_level_des *lev;
103
//  kern_printf("react");
104
 
105
  lev = (RRSOFT_level_des *)level_table[proc_table[p].task_level];
106
 
107
  if (proc_table[p].status == RRSOFT_IDLE) {
108
    /* the task has finished the current activation and must be
109
       reactivated */
110
    proc_table[p].status = RRSOFT_READY;
29 pj 111
    iq_insertlast(p,&lev->ready);
2 pj 112
 
113
    event_need_reschedule();
114
  }
115
  else if (lev->nact[p] >= 0)
116
    /* the task has not completed the current activation, so we save
117
       the activation incrementing nact... */
118
    lev->nact[p]++;
119
 
120
  /* repost the event at the next period end... */
121
  ADDUSEC2TIMESPEC(lev->period[p], &lev->reactivation_time[p]);
122
  lev->reactivation_timer[p] = kern_event_post(&lev->reactivation_time[p],
123
                                               RRSOFT_timer_reactivate,
124
                                               (void *)p);
125
}
126
 
127
/* This is not efficient but very fair :-)
128
   The need of all this stuff is because if a task execute a long time
129
   due to (shadow!) priority inheritance, then the task shall go to the
130
   tail of the queue many times... */
38 pj 131
static PID RRSOFT_public_scheduler(LEVEL l)
2 pj 132
{
133
  RRSOFT_level_des *lev = (RRSOFT_level_des *)(level_table[l]);
134
 
135
  PID p;
136
 
137
  for (;;) {
29 pj 138
    p = iq_query_first(&lev->ready);
2 pj 139
    if (p == -1)
140
      return p;
141
//{kern_printf("(s%d)",p);      return p;}
142
 
143
//    kern_printf("(p=%d l=%d avail=%d wcet =%d)\n",p,l,proc_table[p].avail_time, proc_table[p].wcet);
144
    if (proc_table[p].avail_time <= 0) {
145
      proc_table[p].avail_time += proc_table[p].wcet;
29 pj 146
      iq_extract(p,&lev->ready);
147
      iq_insertlast(p,&lev->ready);
2 pj 148
    }
149
    else
150
//{kern_printf("(s%d)",p);      return p;}
151
      return p;
152
 
153
  }
154
}
155
 
38 pj 156
static int RRSOFT_public_create(LEVEL l, PID p, TASK_MODEL *m)
2 pj 157
{
158
  RRSOFT_level_des *lev = (RRSOFT_level_des *)(level_table[l]);
159
 
160
//  kern_printf("create %d mod %d\n",p,m->pclass);
161
  /* the task state is set at SLEEP by the general task_create
162
     the only thing to set remains the capacity stuffs that are set
163
     to the values passed in the model... */
164
 
156 pj 165
  if (m->pclass==NRT_PCLASS  && !(lev->models & RRSOFT_ONLY_NRT) ) return -1;
166
  if (m->pclass==SOFT_PCLASS && !(lev->models & RRSOFT_ONLY_SOFT) ) return -1;
167
  if (m->pclass==HARD_PCLASS && !(lev->models & RRSOFT_ONLY_HARD) ) return -1;
38 pj 168
  if (m->level != 0 && m->level != l) return -1;
169
 
2 pj 170
  /* I used the wcet field because using wcet can account if a task
171
     consume more than the timeslice... */
172
 
173
  if (lev->models & RRSOFT_ONLY_NRT &&
174
      (m->pclass == NRT_PCLASS || m->pclass == (NRT_PCLASS | l))) {
175
    NRT_TASK_MODEL *nrt = (NRT_TASK_MODEL *)m;
176
 
177
//    kern_printf("nrt");
178
    if (nrt->slice) {
179
      proc_table[p].avail_time = nrt->slice;
180
      proc_table[p].wcet       = nrt->slice;
181
    }
182
    else {
183
      proc_table[p].avail_time = lev->slice;
184
      proc_table[p].wcet       = lev->slice;
185
    }
186
    proc_table[p].control   |= CONTROL_CAP;
187
 
188
    if (nrt->arrivals == SAVE_ARRIVALS)
189
      lev->nact[p] = 0;
190
    else
191
      lev->nact[p] = -1;
192
 
193
    lev->periodic[p] = 0;
194
    lev->period[p] = 0;
195
  }
196
  else if (lev->models & RRSOFT_ONLY_SOFT &&
197
           (m->pclass == SOFT_PCLASS || m->pclass == (SOFT_PCLASS | l))) {
198
    SOFT_TASK_MODEL *soft = (SOFT_TASK_MODEL *)m;
199
//    kern_printf("soft");
200
    proc_table[p].avail_time = lev->slice;
201
    proc_table[p].wcet       = lev->slice;
202
    proc_table[p].control   |= CONTROL_CAP;
203
 
204
    if (soft->arrivals == SAVE_ARRIVALS)
205
      lev->nact[p] = 0;
206
    else
207
      lev->nact[p] = -1;
208
 
209
    if (soft->periodicity == PERIODIC) {
210
      lev->periodic[p] = 1;
211
      lev->period[p] = soft->period;
212
    }
213
  }
214
  else if (lev->models & RRSOFT_ONLY_HARD &&
215
           (m->pclass == HARD_PCLASS || m->pclass == (HARD_PCLASS | l))) {
216
    HARD_TASK_MODEL *hard = (HARD_TASK_MODEL *)m;
217
//    kern_printf("hard");
218
    proc_table[p].avail_time = lev->slice;
219
    proc_table[p].wcet       = lev->slice;
220
    proc_table[p].control   |= CONTROL_CAP;
221
 
222
    lev->nact[p] = 0;
223
 
224
    if (hard->periodicity == PERIODIC) {
225
      lev->periodic[p] = 1;
226
      lev->period[p] = hard->mit;
227
    }
228
  }
229
 
230
  return 0; /* OK */
231
}
232
 
38 pj 233
static void RRSOFT_public_dispatch(LEVEL l, PID p, int nostop)
2 pj 234
{
235
  RRSOFT_level_des *lev = (RRSOFT_level_des *)(level_table[l]);
236
 
237
  /* the task state is set EXE by the scheduler()
238
     we extract the task from the ready queue
239
     NB: we can't assume that p is the first task in the queue!!! */
29 pj 240
  iq_extract(p, &lev->ready);
2 pj 241
}
242
 
38 pj 243
static void RRSOFT_public_epilogue(LEVEL l, PID p)
2 pj 244
{
245
  RRSOFT_level_des *lev = (RRSOFT_level_des *)(level_table[l]);
246
 
247
  /* check if the slice is finished and insert the task in the coRRSOFTect
248
     qqueue position */
249
  if (proc_table[p].avail_time <= 0) {
250
    proc_table[p].avail_time += proc_table[p].wcet;
29 pj 251
    iq_insertlast(p,&lev->ready);
2 pj 252
  }
253
  else
254
    /* curr is >0, so the running task have to run for another cuRRSOFT usec */
29 pj 255
    iq_insertfirst(p,&lev->ready);
2 pj 256
 
257
  proc_table[p].status = RRSOFT_READY;
258
}
259
 
38 pj 260
static void RRSOFT_public_activate(LEVEL l, PID p)
2 pj 261
{
262
  RRSOFT_level_des *lev = (RRSOFT_level_des *)(level_table[l]);
263
 
264
  /* Test if we are trying to activate a non sleeping task    */
265
  /* save activation (only if needed... */
266
  if (proc_table[p].status != SLEEP && proc_table[p].status != RRSOFT_IDLE) {
267
    if (lev->nact[p] != -1)
268
      lev->nact[p]++;
269
    return;
270
  }
271
 
38 pj 272
  /* Insert task in the correct position */
2 pj 273
  proc_table[p].status = RRSOFT_READY;
29 pj 274
  iq_insertlast(p,&lev->ready);
2 pj 275
 
276
  /* Set the reactivation timer */
277
  if (lev->periodic[p])
278
  {
38 pj 279
    kern_gettime(&lev->reactivation_time[p]);
2 pj 280
    ADDUSEC2TIMESPEC(lev->period[p], &lev->reactivation_time[p]);
281
    lev->reactivation_timer[p] = kern_event_post(&lev->reactivation_time[p],
282
                                                 RRSOFT_timer_reactivate,
283
                                                 (void *)p);
284
  }
285
}
286
 
38 pj 287
static void RRSOFT_public_unblock(LEVEL l, PID p)
2 pj 288
{
289
  RRSOFT_level_des *lev = (RRSOFT_level_des *)(level_table[l]);
290
 
291
  /* Similar to RRSOFT_task_activate, but we don't check in what state
38 pj 292
     the task is */
2 pj 293
 
294
  /* Insert task in the coRRSOFTect position */
295
  proc_table[p].status = RRSOFT_READY;
29 pj 296
  iq_insertlast(p,&lev->ready);
2 pj 297
}
298
 
38 pj 299
static void RRSOFT_public_block(LEVEL l, PID p)
2 pj 300
{
301
  /* Extract the running task from the level
302
     . we have already extract it from the ready queue at the dispatch time.
303
     . the capacity event have to be removed by the generic kernel
304
     . the wcet don't need modification...
305
     . the state of the task is set by the calling function
306
 
307
     So, we do nothing!!!
308
  */
309
}
310
 
38 pj 311
static int RRSOFT_public_message(LEVEL l, PID p, void *m)
2 pj 312
{
313
  RRSOFT_level_des *lev = (RRSOFT_level_des *)(level_table[l]);
314
 
315
  if (lev->nact[p] > 0) {
316
    /* continue!!!! */
317
    lev->nact[p]--;
318
//    qq_insertlast(p,&lev->ready);
29 pj 319
    iq_insertfirst(p,&lev->ready);
2 pj 320
    proc_table[p].status = RRSOFT_READY;
321
  }
322
  else
323
    proc_table[p].status = RRSOFT_IDLE;
38 pj 324
 
325
  jet_update_endcycle(); /* Update the Jet data... */
385 giacomo 326
  TRACER_LOGEVENT(FTrace_EVT_task_end_cycle,3,proc_table[p].context,l);
38 pj 327
 
328
  return 0;
2 pj 329
}
330
 
38 pj 331
static void RRSOFT_public_end(LEVEL l, PID p)
2 pj 332
{
333
  RRSOFT_level_des *lev = (RRSOFT_level_des *)(level_table[l]);
334
 
335
  lev->nact[p] = -1;
336
 
337
  /* we delete the reactivation timer */
338
  if (lev->periodic[p]) {
38 pj 339
    kern_event_delete(lev->reactivation_timer[p]);
2 pj 340
    lev->reactivation_timer[p] = -1;
341
  }
342
 
343
  /* then, we insert the task in the free queue */
344
  proc_table[p].status = FREE;
29 pj 345
  iq_insertlast(p,&freedesc);
2 pj 346
}
347
 
348
/* Registration functions */
349
 
350
/*+ This init function install the "main" task +*/
351
static void RRSOFT_call_main(void *l)
352
{
353
  LEVEL lev;
354
  PID p;
355
  NRT_TASK_MODEL m;
356
  void *mb;
357
 
358
  lev = (LEVEL)l;
359
 
360
  nrt_task_default_model(m);
361
  nrt_task_def_level(m,lev); /* with this we are sure that the task aRRSOFTives
362
                                to the coRRSOFTect level */
363
 
364
  mb = ((RRSOFT_level_des *)level_table[lev])->multiboot;
365
  nrt_task_def_arg(m,mb);
366
  nrt_task_def_usemath(m);
367
  nrt_task_def_nokill(m);
368
  nrt_task_def_ctrl_jet(m);
369
 
370
  p = task_create("Main", __init__, (TASK_MODEL *)&m, NULL);
371
 
372
  if (p == NIL)
373
    printk("\nPanic!!! can't create main task...\n");
374
 
38 pj 375
  RRSOFT_public_activate(lev,p);
2 pj 376
}
377
 
378
 
379
/*+ Registration function:
380
    TIME slice                the slice for the Round Robin queue
381
    int createmain            1 if the level creates the main task 0 otherwise
382
    struct multiboot_info *mb used if createmain specified   +*/
38 pj 383
LEVEL RRSOFT_register_level(TIME slice,
2 pj 384
                       int createmain,
385
                       struct multiboot_info *mb,
386
                       BYTE models)
387
{
388
  LEVEL l;            /* the level that we register */
389
  RRSOFT_level_des *lev;  /* for readableness only */
390
  PID i;
391
 
392
  printk("RRSOFT_register_level\n");
393
 
394
  /* request an entry in the level_table */
38 pj 395
  l = level_alloc_descriptor(sizeof(RRSOFT_level_des));
2 pj 396
 
38 pj 397
  lev = (RRSOFT_level_des *)level_table[l];
2 pj 398
 
399
  /* fill the standard descriptor */
38 pj 400
  lev->l.public_scheduler = RRSOFT_public_scheduler;
401
  lev->l.public_create    = RRSOFT_public_create;
402
  lev->l.public_end       = RRSOFT_public_end;
403
  lev->l.public_dispatch  = RRSOFT_public_dispatch;
404
  lev->l.public_epilogue  = RRSOFT_public_epilogue;
405
  lev->l.public_activate  = RRSOFT_public_activate;
406
  lev->l.public_unblock   = RRSOFT_public_unblock;
407
  lev->l.public_block     = RRSOFT_public_block;
408
  lev->l.public_message   = RRSOFT_public_message;
2 pj 409
 
410
  /* fill the RRSOFT descriptor part */
411
  for (i = 0; i < MAX_PROC; i++) {
412
    lev->nact[i] = -1;
413
    NULL_TIMESPEC(&lev->reactivation_time[i]);
414
    lev->reactivation_timer[i] = -1;
415
    lev->periodic[i] = 0;
416
    lev->period[i] = 0;
417
  }
418
 
29 pj 419
  iq_init(&lev->ready, &freedesc, 0);
2 pj 420
 
421
  if (slice < RRSOFT_MINIMUM_SLICE) slice = RRSOFT_MINIMUM_SLICE;
422
  if (slice > RRSOFT_MAXIMUM_SLICE) slice = RRSOFT_MAXIMUM_SLICE;
423
  lev->slice      = slice;
424
 
425
  lev->multiboot  = mb;
426
 
427
  lev->models     = models;
428
 
429
  if (createmain)
430
    sys_atrunlevel(RRSOFT_call_main,(void *) l, RUNLEVEL_INIT);
38 pj 431
 
432
  return l;
2 pj 433
}
434
 
435
 
436
 
437