Subversion Repositories shark

Rev

Rev 1176 | Details | Compare with Previous | 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
 ------------
1180 trimarchi 21
 CVS :        $Id: posixstar.c,v 1.2 2003-08-01 13:20:38 trimarchi Exp $
1176 trimarchi 22
 
23
 File:        $File$
1180 trimarchi 24
 Revision:    $Revision: 1.2 $
25
 Last update: $Date: 2003-08-01 13:20:38 $
1176 trimarchi 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)) {
1180 trimarchi 131
      if (proc_table[p].avail_time<=0)
132
        proc_table[p].avail_time += proc_table[p].wcet;
1176 trimarchi 133
      //kern_printf("RR policy");
134
      iq_extract(p,&lev->ready[prio]);
135
      iq_insertlast(p,&lev->ready[prio]);
136
    }
137
    else {
138
      break;
139
    }
140
  }
141
 
142
  if (p!=lev->activated) {
143
    if (lev->activated != NIL )  {
144
     level_table[ lev->scheduling_level ]->
145
       private_extract(lev->scheduling_level, lev->activated);
1180 trimarchi 146
       //kern_printf("CBS ext %d",p);
1176 trimarchi 147
   }
148
   lev->activated = p;
149
 
150
   if (p != NIL) {
151
     BUDGET_TASK_MODEL b;
152
     budget_task_default_model(b, lev->budget);
1180 trimarchi 153
     //kern_printf("(Act %d",p); 
1176 trimarchi 154
    level_table[ lev->scheduling_level ]->
155
      private_insert(lev->scheduling_level, p, (TASK_MODEL *)&b);
156
   }
157
 }
158
}
159
 
160
static int POSIXSTAR_public_eligible(LEVEL l, PID p)
161
{
162
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
163
  if (p==lev->activated) {
164
    //kern_printf("eli %d", p);
165
 
166
  return level_table[ lev->scheduling_level ]->
167
    private_eligible(lev->scheduling_level,p);
168
  }
169
  return 0;
170
}
171
 
172
 
173
static int POSIXSTAR_public_create(LEVEL l, PID p, TASK_MODEL *m)
174
{
175
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
176
  NRT_TASK_MODEL *nrt;
177
 
178
  if (m->pclass != NRT_PCLASS) return -1;
179
  if (m->level != 0 && m->level != l) return -1;
180
 
181
  nrt = (NRT_TASK_MODEL *)m;
182
 
183
  /* the task state is set at SLEEP by the general task_create */
184
 
185
  /* I used the wcet field because using wcet can account if a task
186
     consume more than the timeslice... */
187
 
188
  if (nrt->inherit == NRT_INHERIT_SCHED &&
189
      proc_table[exec_shadow].task_level == l) {
190
    /* We inherit the scheduling properties if the scheduling level
191
       *is* the same */
192
    lev->priority[p] = lev->priority[exec_shadow];
193
 
194
    proc_table[p].avail_time = proc_table[exec_shadow].avail_time;
195
    proc_table[p].wcet       = proc_table[exec_shadow].wcet;
196
 
197
    proc_table[p].control = (proc_table[p].control & ~CONTROL_CAP) |
198
                            (proc_table[exec_shadow].control & CONTROL_CAP);
199
 
200
    lev->nact[p] = (lev->nact[exec_shadow] == -1) ? -1 : 0;
201
  }
202
  else {
203
    if (nrt->weight<=lev->maxpriority)
204
      lev->priority[p] = nrt->weight;
205
    else lev->priority[p]=lev->maxpriority;
206
 
207
    if (nrt->slice) {
208
      proc_table[p].avail_time = nrt->slice;
209
      proc_table[p].wcet       = nrt->slice;
210
    }
211
    else {
212
      proc_table[p].avail_time = lev->slice;
213
      proc_table[p].wcet       = lev->slice;
214
    }
215
 
216
    if (nrt->policy == NRT_RR_POLICY) {
217
      proc_table[p].control   |= CONTROL_CAP;
218
      //kern_printf("CCAP set:%d",p);
219
 
220
    }
221
    if (nrt->arrivals == SAVE_ARRIVALS)
222
      lev->nact[p] = 0;
223
    else
224
      lev->nact[p] = -1;
225
  }
226
 
227
  return 0; /* OK */
228
}
229
 
230
static void POSIXSTAR_public_dispatch(LEVEL l, PID p, int nostop)
231
{
232
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
233
 
234
  //#ifdef POSIXSTAR_DEBUG
1180 trimarchi 235
 
1176 trimarchi 236
  //#endif
237
 
238
  /* the task state is set EXE by the scheduler()
239
     we extract the task from the ready queue
240
     NB: we can't assume that p is the first task in the queue!!! */
241
  //iq_extract(p, &lev->ready[lev->priority[p]]);
1180 trimarchi 242
  //if (!nostop) {
243
    //kern_printf("PDisp:%d(%d)",p, lev->activated);
244
  if (p==lev->activated)
1176 trimarchi 245
    level_table[lev->scheduling_level]->private_dispatch(lev->scheduling_level, p, nostop);
1180 trimarchi 246
  //} else
247
   // kern_printf("PDisp:%d(%d)",p, lev->activated);
1176 trimarchi 248
}
249
 
250
static void POSIXSTAR_public_epilogue(LEVEL l, PID p)
251
{
252
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
253
  //#ifdef POSIXSTAR_DEBUG
1180 trimarchi 254
  //kern_printf("PEpic:%d",p);
1176 trimarchi 255
  //#endif 
256
  if (p==lev->activated) {
257
    if (lev->yielding) {
258
      lev->yielding = 0;
259
      iq_extract(p,&lev->ready[lev->priority[p]]);
260
      iq_insertlast(p,&lev->ready[lev->priority[p]]);
261
    }
262
    /* check if the slice is finished and insert the task in the coPOSIXect
263
       qqueue position */
264
    else if (proc_table[p].control & CONTROL_CAP &&
265
           proc_table[p].avail_time <= 0) {
266
 
267
      //proc_table[p].avail_time += proc_table[p].wcet;
268
      //kern_printf("avail_time %d", proc_table[p].avail_time);
269
      iq_extract(p,&lev->ready[lev->priority[p]]);
270
      iq_insertlast(p,&lev->ready[lev->priority[p]]);
271
      //level_table[lev->scheduling_level]->private_extract(lev->scheduling_level,p);
272
      //lev->activated=NIL;
1180 trimarchi 273
 
1176 trimarchi 274
      POSIXSTAR_private_scheduler(lev);
1180 trimarchi 275
      if (p==lev->activated)
276
        level_table[lev->scheduling_level]->private_epilogue(lev->scheduling_level,p);
277
 
1176 trimarchi 278
    }
279
    else  {
280
      //iq_insertfirst(p,&lev->ready[lev->priority[p]]);
281
      level_table[lev->scheduling_level]->private_epilogue(lev->scheduling_level,p);
1180 trimarchi 282
 
1176 trimarchi 283
    }
284
 
285
    proc_table[p].status = POSIXSTAR_READY;
286
 
1180 trimarchi 287
  }
288
 
289
 
1176 trimarchi 290
}
291
 
292
static void POSIXSTAR_internal_activate(POSIXSTAR_level_des *lev, PID p)
293
{
294
 
295
  /* Insert task in the correct position */
296
  proc_table[p].status = POSIXSTAR_READY;
297
  iq_insertlast(p,&lev->ready[lev->priority[p]]);
298
 
299
}
300
 
301
 
302
static void POSIXSTAR_public_activate(LEVEL l, PID p)
303
{
304
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
305
 
306
  /* Test if we are trying to activate a non sleeping task    */
307
  /* save activation (only if needed...) */
308
  if (proc_table[p].status != SLEEP) {
309
    if (lev->nact[p] != -1)
310
      lev->nact[p]++;
311
    return;
312
  }
313
#ifdef POSIXSTAR_DEBUG 
314
  kern_printf("PA:%d",p);
315
#endif  
316
  POSIXSTAR_internal_activate(lev, p);
317
  POSIXSTAR_private_scheduler(lev);
318
 
319
}
320
 
321
 
322
static void POSIXSTAR_public_unblock(LEVEL l, PID p)
323
{
324
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
325
 
326
  /* Similar to POSIX_task_activate, but we don't check in what state
327
     the task is */
328
 
329
  /* Insert task in the coPOSIXect position */
1180 trimarchi 330
  //kern_printf("PU:%d", p);
1176 trimarchi 331
  proc_table[p].status = POSIXSTAR_READY;
332
  iq_insertlast(p,&lev->ready[lev->priority[p]]);
333
  POSIXSTAR_private_scheduler(lev);
334
}
335
 
336
static void POSIXSTAR_public_block(LEVEL l, PID p)
1180 trimarchi 337
{  
338
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
339
 
1176 trimarchi 340
  /* Extract the running task from the level
341
     . we have already extract it from the ready queue at the dispatch time.
342
     . the capacity event have to be removed by the generic kernel
343
     . the wcet don't need modification...
344
     . the state of the task is set by the calling function
345
 
346
     So, we do nothing!!!
347
  */
348
 
349
//#ifdef POSIXSTAR_DEBUG
350
//kern_printf("PB:%d", p);      
351
//#endif
1180 trimarchi 352
  iq_extract(p,&lev->ready[lev->priority[p]]);
353
  POSIXSTAR_private_scheduler(lev);
354
 
1176 trimarchi 355
}
356
 
357
static int POSIXSTAR_public_message(LEVEL l, PID p, void *m)
358
{
359
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
360
 
361
  if (lev->nact[p] > 0) {
362
    /* continue!!!! */
363
    lev->nact[p]--;
1180 trimarchi 364
    iq_extract(p,&lev->ready[lev->priority[p]]);
1176 trimarchi 365
    iq_insertfirst(p,&lev->ready[lev->priority[p]]);
366
    proc_table[p].status = POSIXSTAR_READY;
367
  }
368
  else {
369
    proc_table[p].status = SLEEP;
370
 
371
  }
372
  //#ifdef POSIXSTAR_DEBUG
1180 trimarchi 373
  kern_printf("PM:%d",p);
1176 trimarchi 374
  //#endif
375
  //jet_update_endcycle(); /* Update the Jet data... */
376
  //trc_logevent(TRC_ENDCYCLE,&exec_shadow); /* tracer stuff */
377
  POSIXSTAR_private_scheduler(lev);
378
  return 0;
379
}
380
 
381
static void POSIXSTAR_public_end(LEVEL l, PID p)
382
{
383
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
384
#ifdef POSIXSTAR_DEBUG
385
        kern_printf("PEnd:%d", p);
386
#endif
387
  lev->nact[p] = -1;
388
 
389
  /* then, we insert the task in the free queue */
390
  proc_table[p].status = FREE;
391
  iq_priority_insert(p,NULL);
392
  POSIXSTAR_private_scheduler(lev);
393
}
394
 
395
/* Registration functions */
396
 
397
/*+ Registration function:
398
    TIME slice                the slice for the Round Robin queue
399
    struct multiboot_info *mb used if createmain specified   +*/
400
LEVEL POSIXSTAR_register_level(int budget, int master, TIME slice,
401
                       int prioritylevels)
402
{
403
  LEVEL l;            /* the level that we register */
404
  POSIXSTAR_level_des *lev;  /* for readableness only */
405
  PID i;              /* a counter */
406
  int x;              /* a counter */
407
 
408
  printk("POSIXSTAR_register_level\n");
409
 
410
  l = level_alloc_descriptor(sizeof(POSIXSTAR_level_des));
411
 
412
  lev = (POSIXSTAR_level_des *)level_table[l];
413
 
414
  printk("    lev=%d\n",(int)lev);
415
 
416
  /* fill the standard descriptor */
417
  /*
418
    lev->l.private_insert   = NULL;
419
    lev->l.private_extract  = NULL;
420
    lev->l.private_dispatch = NULL;
421
    lev->l.private_epilogue = NULL;
422
  */
423
 
424
  //lev->l.public_scheduler = NULL;
425
  lev->l.public_create    = POSIXSTAR_public_create;
426
  lev->l.public_end       = POSIXSTAR_public_end;
427
  lev->l.public_dispatch  = POSIXSTAR_public_dispatch;
428
  lev->l.public_epilogue  = POSIXSTAR_public_epilogue;
429
  lev->l.public_activate  = POSIXSTAR_public_activate;
430
  lev->l.public_unblock   = POSIXSTAR_public_unblock;
431
  lev->l.public_block     = POSIXSTAR_public_block;
432
  lev->l.public_message   = POSIXSTAR_public_message;
433
  lev->l.public_eligible  = POSIXSTAR_public_eligible;
434
 
435
  /* fill the POSIX descriptor part */
436
  for (i = 0; i < MAX_PROC; i++)
437
    lev->nact[i] = -1;
438
 
439
  lev->maxpriority = prioritylevels -1;
440
 
441
  lev->ready = (IQUEUE *)kern_alloc(sizeof(IQUEUE) * prioritylevels);
442
 
443
  for (x = 0; x < prioritylevels; x++)
444
    iq_init(&lev->ready[x], NULL, 0);
445
 
446
  if (slice < POSIXSTAR_MINIMUM_SLICE) slice = POSIXSTAR_MINIMUM_SLICE;
447
  if (slice > POSIXSTAR_MAXIMUM_SLICE) slice = POSIXSTAR_MAXIMUM_SLICE;
448
  lev->slice      = slice;
449
  lev->activated=NIL;
450
  lev->budget = budget;
451
  lev->scheduling_level = master;
452
  //lev->multiboot  = mb;
453
 
454
  //if (createmain)
455
  //  sys_atrunlevel(POSIXSTAR_call_main,(void *) l, RUNLEVEL_INIT);
456
 
457
  return l;
458
}
459
 
460
/*+ this function forces the running task to go to his queue tail;
461
    (it works only on the POSIX level) +*/
462
int POSIXSTAR_sched_yield(LEVEL l)
463
{
464
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
465
 
466
  if (proc_table[exec_shadow].task_level != l)
467
    return -1;
468
 
469
  proc_table[exec_shadow].context = kern_context_save();
470
  lev->yielding = 1;
471
  scheduler();
472
  kern_context_load(proc_table[exec_shadow].context);
473
  return 0;
474
}
475
 
476
/*+ this function returns the maximum level allowed for the POSIX level +*/
477
int POSIXSTAR_get_priority_max(LEVEL l)
478
{
479
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
480
  return lev->maxpriority;
481
}
482
 
483
/*+ this function returns the default timeslice for the POSIX level +*/
484
int POSIXSTAR_rr_get_interval(LEVEL l)
485
{
486
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
487
  return lev->slice;
488
}
489
 
490
/*+ this functions returns some paramaters of a task;
491
    policy must be NRT_RR_POLICY or NRT_FIFO_POLICY;
492
    priority must be in the range [0..prioritylevels]
493
    returns ENOSYS or ESRCH if there are problems +*/
494
int POSIXSTAR_getschedparam(LEVEL l, PID p, int *policy, int *priority)
495
{
496
  if (p<0 || p>= MAX_PROC || proc_table[p].status == FREE)
497
    return ESRCH;
498
 
499
  if (proc_table[p].task_level != l)
500
    return ENOSYS;
501
 
502
  if (proc_table[p].control & CONTROL_CAP)
503
    *policy = NRT_RR_POLICY;
504
  else
505
    *policy = NRT_FIFO_POLICY;
506
 
507
  *priority = ((POSIXSTAR_level_des *)(level_table[l]))->priority[p];
508
 
509
  return 0;
510
}
511
 
512
/*+ this functions sets paramaters of a task +*/
513
int POSIXSTAR_setschedparam(LEVEL l, PID p, int policy, int priority)
514
{
515
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
516
 
517
  if (p<0 || p>= MAX_PROC || proc_table[p].status == FREE)
518
    return ESRCH;
519
 
520
  if (proc_table[p].task_level != l)
521
    return ENOSYS;
522
 
523
  if (policy == SCHED_RR)
524
    proc_table[p].control |= CONTROL_CAP;
525
  else if (policy == SCHED_FIFO)
526
    proc_table[p].control &= ~CONTROL_CAP;
527
  else
528
    return EINVAL;
529
 
530
  if (lev->priority[p] != priority) {
531
    if (proc_table[p].status == POSIXSTAR_READY) {
532
      iq_extract(p,&lev->ready[lev->priority[p]]);
533
      lev->priority[p] = priority;
534
      iq_insertlast(p,&lev->ready[priority]);
535
    }
536
    else
537
      lev->priority[p] = priority;
538
  }
539
 
540
  return 0;
541
}
542
 
543
 
544