Subversion Repositories shark

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1176 trimarchi 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
 *   Trimarchi Michael   <trimarchi@gandalf.sssup.it>
10
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/**
20
 ------------
21
 CVS :        $Id: posixstar.c,v 1.1 2003-06-18 08:13:02 trimarchi Exp $
22
 
23
 File:        $File$
24
 Revision:    $Revision: 1.1 $
25
 Last update: $Date: 2003-06-18 08:13:02 $
26
 ------------
27
 
28
 This file contains the scheduling module compatible with POSIX
29
 specifications
30
 
31
 Read posixstar.h for further details.
32
 
33
 RR tasks have the CONTROL_CAP bit set
34
 
35
**/
36
 
37
/*
38
 * Copyright (C) 2000 Paolo Gai
39
 *
40
 * This program is free software; you can redistribute it and/or modify
41
 * it under the terms of the GNU General Public License as published by
42
 * the Free Software Foundation; either version 2 of the License, or
43
 * (at your option) any later version.
44
 *
45
 * This program is distributed in the hope that it will be useful,
46
 * but WITHOUT ANY WARR2ANTY; without even the implied waRR2anty of
47
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
48
 * GNU General Public License for more details.
49
 *
50
 * You should have received a copy of the GNU General Public License
51
 * along with this program; if not, write to the Free Software
52
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
53
 *
54
 */
55
 
56
 
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
#include <kernel/trace.h>
64
#include "posixstar.h"
65
#include "cbsstar.h"
66
//#define POSIXSTAR_DEBUG
67
/*+ Status used in the level +*/
68
#define POSIXSTAR_READY   MODULE_STATUS_BASE
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
 
76
  int priority[MAX_PROC]; /*+ priority of each task                +*/
77
 
78
  IQUEUE *ready;        /*+ the ready queue array                  +*/
79
 
80
  int slice;            /*+ the level's time slice                 +*/
81
 
82
//  the multiboot is not usefull for this module 
83
//  struct multiboot_info *multiboot; /*+ used if the level have to insert
84
  //                                      the main task +*/
85
  int maxpriority;      /*+ the priority are from 0 to maxpriority
86
                            (i.e 0 to 31)                          +*/
87
 
88
  int yielding;         /*+ equal to 1 when a sched_yield is called +*/
89
 
90
  int budget;
91
 
92
  PID activated;
93
  int scheduling_level;
94
 
95
} POSIXSTAR_level_des;
96
 
97
/* the private scheduler choice a task and insert in cbsstar module */
98
/* This is not efficient but very fair :-)
99
   The need of all this stuff is because if a task execute a long time
100
   due to (shadow!) priority inheritance, then the task shall go to the
101
   tail of the queue many times... */
102
 
103
static void POSIXSTAR_private_scheduler(POSIXSTAR_level_des * lev)
104
{
105
  /* the old posix scheduler select the private job for CBS */
106
  PID p=NIL;
107
 
108
  int prio;
109
 
110
  prio = lev->maxpriority;
111
 
112
  for (;;) {
113
    p = iq_query_first(&lev->ready[prio]);
114
    if (p == NIL) {
115
      if (prio) {
116
        prio--;
117
        continue;
118
      }
119
      else {
120
        p=NIL;
121
        break;
122
      }
123
    }
124
    //if (p != NIL && (proc_table[p].control & CONTROL_CAP))
125
    //  kern_printf("CC SET %d",p);
126
 
127
    //kern_printf("task %d", p);
128
 
129
    if ((proc_table[p].control & CONTROL_CAP) &&
130
        (proc_table[p].avail_time <= 0)) {
131
      proc_table[p].avail_time += proc_table[p].wcet;
132
      //kern_printf("RR policy");
133
      iq_extract(p,&lev->ready[prio]);
134
      iq_insertlast(p,&lev->ready[prio]);
135
    }
136
    else {
137
      break;
138
    }
139
  }
140
 
141
  if (p!=lev->activated) {
142
    if (lev->activated != NIL )  {
143
     level_table[ lev->scheduling_level ]->
144
       private_extract(lev->scheduling_level, lev->activated);
145
     //kern_printf("CBS ext %d", lev->activated);
146
   }
147
   lev->activated = p;
148
 
149
   if (p != NIL) {
150
     BUDGET_TASK_MODEL b;
151
     budget_task_default_model(b, lev->budget);
152
     // viene inserito nel CBS
153
     //#ifdef POSIXSTAR_DEBUG
154
     //kern_printf("CBS Ins %d",p);
155
     //#endif
156
    level_table[ lev->scheduling_level ]->
157
      private_insert(lev->scheduling_level, p, (TASK_MODEL *)&b);
158
   }
159
 }
160
}
161
 
162
static int POSIXSTAR_public_eligible(LEVEL l, PID p)
163
{
164
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
165
  if (p==lev->activated) {
166
    //kern_printf("eli %d", p);
167
 
168
  return level_table[ lev->scheduling_level ]->
169
    private_eligible(lev->scheduling_level,p);
170
  }
171
  return 0;
172
}
173
 
174
 
175
static int POSIXSTAR_public_create(LEVEL l, PID p, TASK_MODEL *m)
176
{
177
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
178
  NRT_TASK_MODEL *nrt;
179
 
180
  if (m->pclass != NRT_PCLASS) return -1;
181
  if (m->level != 0 && m->level != l) return -1;
182
 
183
  nrt = (NRT_TASK_MODEL *)m;
184
 
185
  /* the task state is set at SLEEP by the general task_create */
186
 
187
  /* I used the wcet field because using wcet can account if a task
188
     consume more than the timeslice... */
189
 
190
  if (nrt->inherit == NRT_INHERIT_SCHED &&
191
      proc_table[exec_shadow].task_level == l) {
192
    /* We inherit the scheduling properties if the scheduling level
193
       *is* the same */
194
    lev->priority[p] = lev->priority[exec_shadow];
195
 
196
    proc_table[p].avail_time = proc_table[exec_shadow].avail_time;
197
    proc_table[p].wcet       = proc_table[exec_shadow].wcet;
198
 
199
    proc_table[p].control = (proc_table[p].control & ~CONTROL_CAP) |
200
                            (proc_table[exec_shadow].control & CONTROL_CAP);
201
 
202
    lev->nact[p] = (lev->nact[exec_shadow] == -1) ? -1 : 0;
203
  }
204
  else {
205
    if (nrt->weight<=lev->maxpriority)
206
      lev->priority[p] = nrt->weight;
207
    else lev->priority[p]=lev->maxpriority;
208
 
209
    if (nrt->slice) {
210
      proc_table[p].avail_time = nrt->slice;
211
      proc_table[p].wcet       = nrt->slice;
212
    }
213
    else {
214
      proc_table[p].avail_time = lev->slice;
215
      proc_table[p].wcet       = lev->slice;
216
    }
217
 
218
    if (nrt->policy == NRT_RR_POLICY) {
219
      proc_table[p].control   |= CONTROL_CAP;
220
      //kern_printf("CCAP set:%d",p);
221
 
222
    }
223
    if (nrt->arrivals == SAVE_ARRIVALS)
224
      lev->nact[p] = 0;
225
    else
226
      lev->nact[p] = -1;
227
  }
228
 
229
  return 0; /* OK */
230
}
231
 
232
static void POSIXSTAR_public_dispatch(LEVEL l, PID p, int nostop)
233
{
234
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
235
 
236
  //#ifdef POSIXSTAR_DEBUG
237
  //kern_printf("PDisp:%d(%d)",p, lev->activated);
238
  //#endif
239
 
240
  /* the task state is set EXE by the scheduler()
241
     we extract the task from the ready queue
242
     NB: we can't assume that p is the first task in the queue!!! */
243
  //iq_extract(p, &lev->ready[lev->priority[p]]);
244
  if (p==lev->activated) {
245
    level_table[lev->scheduling_level]->private_dispatch(lev->scheduling_level, p, nostop);
246
  }
247
}
248
 
249
static void POSIXSTAR_public_epilogue(LEVEL l, PID p)
250
{
251
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
252
  //#ifdef POSIXSTAR_DEBUG
253
  //kern_printf("PEpic:%d(%d)",p, lev->activated);
254
  //#endif 
255
  if (p==lev->activated) {
256
    if (lev->yielding) {
257
      lev->yielding = 0;
258
      iq_extract(p,&lev->ready[lev->priority[p]]);
259
      iq_insertlast(p,&lev->ready[lev->priority[p]]);
260
    }
261
    /* check if the slice is finished and insert the task in the coPOSIXect
262
       qqueue position */
263
    else if (proc_table[p].control & CONTROL_CAP &&
264
           proc_table[p].avail_time <= 0) {
265
 
266
      //proc_table[p].avail_time += proc_table[p].wcet;
267
      //kern_printf("avail_time %d", proc_table[p].avail_time);
268
      iq_extract(p,&lev->ready[lev->priority[p]]);
269
      iq_insertlast(p,&lev->ready[lev->priority[p]]);
270
      //level_table[lev->scheduling_level]->private_extract(lev->scheduling_level,p);
271
      //lev->activated=NIL;
272
      POSIXSTAR_private_scheduler(lev);
273
    }
274
    else  {
275
      //iq_insertfirst(p,&lev->ready[lev->priority[p]]);
276
      level_table[lev->scheduling_level]->private_epilogue(lev->scheduling_level,p);
277
    }
278
 
279
    proc_table[p].status = POSIXSTAR_READY;
280
 
281
  }
282
}
283
 
284
static void POSIXSTAR_internal_activate(POSIXSTAR_level_des *lev, PID p)
285
{
286
 
287
  /* Insert task in the correct position */
288
  proc_table[p].status = POSIXSTAR_READY;
289
  iq_insertlast(p,&lev->ready[lev->priority[p]]);
290
 
291
}
292
 
293
 
294
static void POSIXSTAR_public_activate(LEVEL l, PID p)
295
{
296
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
297
 
298
  /* Test if we are trying to activate a non sleeping task    */
299
  /* save activation (only if needed...) */
300
  if (proc_table[p].status != SLEEP) {
301
    if (lev->nact[p] != -1)
302
      lev->nact[p]++;
303
    return;
304
  }
305
#ifdef POSIXSTAR_DEBUG 
306
  kern_printf("PA:%d",p);
307
#endif  
308
  POSIXSTAR_internal_activate(lev, p);
309
  POSIXSTAR_private_scheduler(lev);
310
 
311
}
312
 
313
 
314
static void POSIXSTAR_public_unblock(LEVEL l, PID p)
315
{
316
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
317
 
318
  /* Similar to POSIX_task_activate, but we don't check in what state
319
     the task is */
320
 
321
  /* Insert task in the coPOSIXect position */
322
  proc_table[p].status = POSIXSTAR_READY;
323
  iq_insertlast(p,&lev->ready[lev->priority[p]]);
324
  POSIXSTAR_private_scheduler(lev);
325
}
326
 
327
static void POSIXSTAR_public_block(LEVEL l, PID p)
328
{
329
  /* Extract the running task from the level
330
     . we have already extract it from the ready queue at the dispatch time.
331
     . the capacity event have to be removed by the generic kernel
332
     . the wcet don't need modification...
333
     . the state of the task is set by the calling function
334
 
335
     So, we do nothing!!!
336
  */
337
 
338
//#ifdef POSIXSTAR_DEBUG
339
//kern_printf("PB:%d", p);      
340
//#endif
341
}
342
 
343
static int POSIXSTAR_public_message(LEVEL l, PID p, void *m)
344
{
345
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
346
 
347
  if (lev->nact[p] > 0) {
348
    /* continue!!!! */
349
    lev->nact[p]--;
350
    iq_insertfirst(p,&lev->ready[lev->priority[p]]);
351
    proc_table[p].status = POSIXSTAR_READY;
352
  }
353
  else {
354
    proc_table[p].status = SLEEP;
355
 
356
  }
357
  //#ifdef POSIXSTAR_DEBUG
358
  //kern_printf("PM:%d",p);
359
  //#endif
360
  //jet_update_endcycle(); /* Update the Jet data... */
361
  //trc_logevent(TRC_ENDCYCLE,&exec_shadow); /* tracer stuff */
362
  POSIXSTAR_private_scheduler(lev);
363
  return 0;
364
}
365
 
366
static void POSIXSTAR_public_end(LEVEL l, PID p)
367
{
368
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
369
#ifdef POSIXSTAR_DEBUG
370
        kern_printf("PEnd:%d", p);
371
#endif
372
  lev->nact[p] = -1;
373
 
374
  /* then, we insert the task in the free queue */
375
  proc_table[p].status = FREE;
376
  iq_priority_insert(p,NULL);
377
  POSIXSTAR_private_scheduler(lev);
378
}
379
 
380
/* Registration functions */
381
 
382
/*+ Registration function:
383
    TIME slice                the slice for the Round Robin queue
384
    struct multiboot_info *mb used if createmain specified   +*/
385
LEVEL POSIXSTAR_register_level(int budget, int master, TIME slice,
386
                       int prioritylevels)
387
{
388
  LEVEL l;            /* the level that we register */
389
  POSIXSTAR_level_des *lev;  /* for readableness only */
390
  PID i;              /* a counter */
391
  int x;              /* a counter */
392
 
393
  printk("POSIXSTAR_register_level\n");
394
 
395
  l = level_alloc_descriptor(sizeof(POSIXSTAR_level_des));
396
 
397
  lev = (POSIXSTAR_level_des *)level_table[l];
398
 
399
  printk("    lev=%d\n",(int)lev);
400
 
401
  /* fill the standard descriptor */
402
  /*
403
    lev->l.private_insert   = NULL;
404
    lev->l.private_extract  = NULL;
405
    lev->l.private_dispatch = NULL;
406
    lev->l.private_epilogue = NULL;
407
  */
408
 
409
  //lev->l.public_scheduler = NULL;
410
  lev->l.public_create    = POSIXSTAR_public_create;
411
  lev->l.public_end       = POSIXSTAR_public_end;
412
  lev->l.public_dispatch  = POSIXSTAR_public_dispatch;
413
  lev->l.public_epilogue  = POSIXSTAR_public_epilogue;
414
  lev->l.public_activate  = POSIXSTAR_public_activate;
415
  lev->l.public_unblock   = POSIXSTAR_public_unblock;
416
  lev->l.public_block     = POSIXSTAR_public_block;
417
  lev->l.public_message   = POSIXSTAR_public_message;
418
  lev->l.public_eligible  = POSIXSTAR_public_eligible;
419
 
420
  /* fill the POSIX descriptor part */
421
  for (i = 0; i < MAX_PROC; i++)
422
    lev->nact[i] = -1;
423
 
424
  lev->maxpriority = prioritylevels -1;
425
 
426
  lev->ready = (IQUEUE *)kern_alloc(sizeof(IQUEUE) * prioritylevels);
427
 
428
  for (x = 0; x < prioritylevels; x++)
429
    iq_init(&lev->ready[x], NULL, 0);
430
 
431
  if (slice < POSIXSTAR_MINIMUM_SLICE) slice = POSIXSTAR_MINIMUM_SLICE;
432
  if (slice > POSIXSTAR_MAXIMUM_SLICE) slice = POSIXSTAR_MAXIMUM_SLICE;
433
  lev->slice      = slice;
434
  lev->activated=NIL;
435
  lev->budget = budget;
436
  lev->scheduling_level = master;
437
  //lev->multiboot  = mb;
438
 
439
  //if (createmain)
440
  //  sys_atrunlevel(POSIXSTAR_call_main,(void *) l, RUNLEVEL_INIT);
441
 
442
  return l;
443
}
444
 
445
/*+ this function forces the running task to go to his queue tail;
446
    (it works only on the POSIX level) +*/
447
int POSIXSTAR_sched_yield(LEVEL l)
448
{
449
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
450
 
451
  if (proc_table[exec_shadow].task_level != l)
452
    return -1;
453
 
454
  proc_table[exec_shadow].context = kern_context_save();
455
  lev->yielding = 1;
456
  scheduler();
457
  kern_context_load(proc_table[exec_shadow].context);
458
  return 0;
459
}
460
 
461
/*+ this function returns the maximum level allowed for the POSIX level +*/
462
int POSIXSTAR_get_priority_max(LEVEL l)
463
{
464
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
465
  return lev->maxpriority;
466
}
467
 
468
/*+ this function returns the default timeslice for the POSIX level +*/
469
int POSIXSTAR_rr_get_interval(LEVEL l)
470
{
471
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
472
  return lev->slice;
473
}
474
 
475
/*+ this functions returns some paramaters of a task;
476
    policy must be NRT_RR_POLICY or NRT_FIFO_POLICY;
477
    priority must be in the range [0..prioritylevels]
478
    returns ENOSYS or ESRCH if there are problems +*/
479
int POSIXSTAR_getschedparam(LEVEL l, PID p, int *policy, int *priority)
480
{
481
  if (p<0 || p>= MAX_PROC || proc_table[p].status == FREE)
482
    return ESRCH;
483
 
484
  if (proc_table[p].task_level != l)
485
    return ENOSYS;
486
 
487
  if (proc_table[p].control & CONTROL_CAP)
488
    *policy = NRT_RR_POLICY;
489
  else
490
    *policy = NRT_FIFO_POLICY;
491
 
492
  *priority = ((POSIXSTAR_level_des *)(level_table[l]))->priority[p];
493
 
494
  return 0;
495
}
496
 
497
/*+ this functions sets paramaters of a task +*/
498
int POSIXSTAR_setschedparam(LEVEL l, PID p, int policy, int priority)
499
{
500
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
501
 
502
  if (p<0 || p>= MAX_PROC || proc_table[p].status == FREE)
503
    return ESRCH;
504
 
505
  if (proc_table[p].task_level != l)
506
    return ENOSYS;
507
 
508
  if (policy == SCHED_RR)
509
    proc_table[p].control |= CONTROL_CAP;
510
  else if (policy == SCHED_FIFO)
511
    proc_table[p].control &= ~CONTROL_CAP;
512
  else
513
    return EINVAL;
514
 
515
  if (lev->priority[p] != priority) {
516
    if (proc_table[p].status == POSIXSTAR_READY) {
517
      iq_extract(p,&lev->ready[lev->priority[p]]);
518
      lev->priority[p] = priority;
519
      iq_insertlast(p,&lev->ready[priority]);
520
    }
521
    else
522
      lev->priority[p] = priority;
523
  }
524
 
525
  return 0;
526
}
527
 
528
 
529