Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1624 giacomo 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.1.1 2004-05-24 17:54:51 giacomo Exp $
22
 
23
 File:        $File$
24
 Revision:    $Revision: 1.1.1.1 $
25
 Last update: $Date: 2004-05-24 17:54:51 $
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 "posixstar.h"
64
#include "cbsstar.h"
65
//#define POSIXSTAR_DEBUG
66
/*+ Status used in the level +*/
67
#define POSIXSTAR_READY   MODULE_STATUS_BASE
68
 
69
/*+ the level redefinition for the Round Robin level +*/
70
typedef struct {
71
  level_des l;          /*+ the standard level descriptor          +*/
72
 
73
  int nact[MAX_PROC];   /*+ number of pending activations          +*/
74
 
75
  int priority[MAX_PROC]; /*+ priority of each task                +*/
76
 
77
  IQUEUE *ready;        /*+ the ready queue array                  +*/
78
 
79
  int slice;            /*+ the level's time slice                 +*/
80
 
81
//  the multiboot is not usefull for this module 
82
//  struct multiboot_info *multiboot; /*+ used if the level have to insert
83
  //                                      the main task +*/
84
  int maxpriority;      /*+ the priority are from 0 to maxpriority
85
                            (i.e 0 to 31)                          +*/
86
 
87
  int yielding;         /*+ equal to 1 when a sched_yield is called +*/
88
 
89
  int budget;
90
 
91
  PID activated;
92
  int scheduling_level;
93
 
94
} POSIXSTAR_level_des;
95
 
96
/* the private scheduler choice a task and insert in cbsstar module */
97
/* This is not efficient but very fair :-)
98
   The need of all this stuff is because if a task execute a long time
99
   due to (shadow!) priority inheritance, then the task shall go to the
100
   tail of the queue many times... */
101
 
102
static void POSIXSTAR_private_scheduler(POSIXSTAR_level_des * lev)
103
{
104
  /* the old posix scheduler select the private job for CBS */
105
  PID p=NIL;
106
 
107
  int prio;
108
 
109
  prio = lev->maxpriority;
110
 
111
  for (;;) {
112
    p = iq_query_first(&lev->ready[prio]);
113
    if (p == NIL) {
114
      if (prio) {
115
        prio--;
116
        continue;
117
      }
118
      else {
119
        p=NIL;
120
        break;
121
      }
122
    }
123
    //if (p != NIL && (proc_table[p].control & CONTROL_CAP))
124
    //  kern_printf("CC SET %d",p);
125
 
126
    //kern_printf("task %d", p);
127
 
128
    if ((proc_table[p].control & CONTROL_CAP) &&
129
        (proc_table[p].avail_time <= 0)) {
130
      if (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",p);
146
   }
147
   lev->activated = p;
148
 
149
   if (p != NIL) {
150
     BUDGET_TASK_MODEL b;
151
     budget_task_default_model(b, lev->budget);
152
     //kern_printf("(Act %d",p); 
153
    level_table[ lev->scheduling_level ]->
154
      private_insert(lev->scheduling_level, p, (TASK_MODEL *)&b);
155
   }
156
 }
157
}
158
 
159
static int POSIXSTAR_public_eligible(LEVEL l, PID p)
160
{
161
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
162
  if (p==lev->activated) {
163
    //kern_printf("eli %d", p);
164
 
165
  return level_table[ lev->scheduling_level ]->
166
    private_eligible(lev->scheduling_level,p);
167
  }
168
  return 0;
169
}
170
 
171
 
172
static int POSIXSTAR_public_create(LEVEL l, PID p, TASK_MODEL *m)
173
{
174
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
175
  NRT_TASK_MODEL *nrt;
176
 
177
  if (m->pclass != NRT_PCLASS) return -1;
178
  if (m->level != 0 && m->level != l) return -1;
179
 
180
  nrt = (NRT_TASK_MODEL *)m;
181
 
182
  /* the task state is set at SLEEP by the general task_create */
183
 
184
  /* I used the wcet field because using wcet can account if a task
185
     consume more than the timeslice... */
186
 
187
  if (nrt->inherit == NRT_INHERIT_SCHED &&
188
      proc_table[exec_shadow].task_level == l) {
189
    /* We inherit the scheduling properties if the scheduling level
190
       *is* the same */
191
    lev->priority[p] = lev->priority[exec_shadow];
192
 
193
    proc_table[p].avail_time = proc_table[exec_shadow].avail_time;
194
    proc_table[p].wcet       = proc_table[exec_shadow].wcet;
195
 
196
    proc_table[p].control = (proc_table[p].control & ~CONTROL_CAP) |
197
                            (proc_table[exec_shadow].control & CONTROL_CAP);
198
 
199
    lev->nact[p] = (lev->nact[exec_shadow] == -1) ? -1 : 0;
200
  }
201
  else {
202
    if (nrt->weight<=lev->maxpriority)
203
      lev->priority[p] = nrt->weight;
204
    else lev->priority[p]=lev->maxpriority;
205
 
206
    if (nrt->slice) {
207
      proc_table[p].avail_time = nrt->slice;
208
      proc_table[p].wcet       = nrt->slice;
209
    }
210
    else {
211
      proc_table[p].avail_time = lev->slice;
212
      proc_table[p].wcet       = lev->slice;
213
    }
214
 
215
    if (nrt->policy == NRT_RR_POLICY) {
216
      proc_table[p].control   |= CONTROL_CAP;
217
      //kern_printf("CCAP set:%d",p);
218
 
219
    }
220
    if (nrt->arrivals == SAVE_ARRIVALS)
221
      lev->nact[p] = 0;
222
    else
223
      lev->nact[p] = -1;
224
  }
225
 
226
  return 0; /* OK */
227
}
228
 
229
static void POSIXSTAR_public_dispatch(LEVEL l, PID p, int nostop)
230
{
231
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
232
 
233
  //#ifdef POSIXSTAR_DEBUG
234
 
235
  //#endif
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!!! */
240
  //iq_extract(p, &lev->ready[lev->priority[p]]);
241
  //if (!nostop) {
242
    //kern_printf("PDisp:%d(%d)",p, lev->activated);
243
  if (p==lev->activated)
244
    level_table[lev->scheduling_level]->private_dispatch(lev->scheduling_level, p, nostop);
245
  //} else
246
   // kern_printf("PDisp:%d(%d)",p, lev->activated);
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",p);
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
 
273
      POSIXSTAR_private_scheduler(lev);
274
      if (p==lev->activated)
275
        level_table[lev->scheduling_level]->private_epilogue(lev->scheduling_level,p);
276
 
277
    }
278
    else  {
279
      //iq_insertfirst(p,&lev->ready[lev->priority[p]]);
280
      level_table[lev->scheduling_level]->private_epilogue(lev->scheduling_level,p);
281
 
282
    }
283
 
284
    proc_table[p].status = POSIXSTAR_READY;
285
 
286
  }
287
 
288
 
289
}
290
 
291
static void POSIXSTAR_internal_activate(POSIXSTAR_level_des *lev, PID p)
292
{
293
 
294
  /* Insert task in the correct position */
295
  proc_table[p].status = POSIXSTAR_READY;
296
  iq_insertlast(p,&lev->ready[lev->priority[p]]);
297
 
298
}
299
 
300
 
301
static void POSIXSTAR_public_activate(LEVEL l, PID p)
302
{
303
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
304
 
305
  /* Test if we are trying to activate a non sleeping task    */
306
  /* save activation (only if needed...) */
307
  if (proc_table[p].status != SLEEP) {
308
    if (lev->nact[p] != -1)
309
      lev->nact[p]++;
310
    return;
311
  }
312
#ifdef POSIXSTAR_DEBUG 
313
  kern_printf("PA:%d",p);
314
#endif  
315
  POSIXSTAR_internal_activate(lev, p);
316
  POSIXSTAR_private_scheduler(lev);
317
 
318
}
319
 
320
 
321
static void POSIXSTAR_public_unblock(LEVEL l, PID p)
322
{
323
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
324
 
325
  /* Similar to POSIX_task_activate, but we don't check in what state
326
     the task is */
327
 
328
  /* Insert task in the coPOSIXect position */
329
  //kern_printf("PU:%d", p);
330
  proc_table[p].status = POSIXSTAR_READY;
331
  iq_insertlast(p,&lev->ready[lev->priority[p]]);
332
  POSIXSTAR_private_scheduler(lev);
333
}
334
 
335
static void POSIXSTAR_public_block(LEVEL l, PID p)
336
{  
337
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
338
 
339
  /* Extract the running task from the level
340
     . we have already extract it from the ready queue at the dispatch time.
341
     . the capacity event have to be removed by the generic kernel
342
     . the wcet don't need modification...
343
     . the state of the task is set by the calling function
344
 
345
     So, we do nothing!!!
346
  */
347
 
348
//#ifdef POSIXSTAR_DEBUG
349
//kern_printf("PB:%d", p);      
350
//#endif
351
  iq_extract(p,&lev->ready[lev->priority[p]]);
352
  POSIXSTAR_private_scheduler(lev);
353
 
354
}
355
 
356
static int POSIXSTAR_public_message(LEVEL l, PID p, void *m)
357
{
358
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
359
 
360
  if (lev->nact[p] > 0) {
361
    /* continue!!!! */
362
    lev->nact[p]--;
363
    iq_extract(p,&lev->ready[lev->priority[p]]);
364
    iq_insertfirst(p,&lev->ready[lev->priority[p]]);
365
    proc_table[p].status = POSIXSTAR_READY;
366
  }
367
  else {
368
    proc_table[p].status = SLEEP;
369
 
370
  }
371
  //#ifdef POSIXSTAR_DEBUG
372
  kern_printf("PM:%d",p);
373
  //#endif
374
  POSIXSTAR_private_scheduler(lev);
375
  return 0;
376
}
377
 
378
static void POSIXSTAR_public_end(LEVEL l, PID p)
379
{
380
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
381
#ifdef POSIXSTAR_DEBUG
382
        kern_printf("PEnd:%d", p);
383
#endif
384
  lev->nact[p] = -1;
385
 
386
  /* then, we insert the task in the free queue */
387
  proc_table[p].status = FREE;
388
  iq_priority_insert(p,NULL);
389
  POSIXSTAR_private_scheduler(lev);
390
}
391
 
392
/* Registration functions */
393
 
394
/*+ Registration function:
395
    TIME slice                the slice for the Round Robin queue
396
    struct multiboot_info *mb used if createmain specified   +*/
397
LEVEL POSIXSTAR_register_level(int budget, int master, TIME slice,
398
                       int prioritylevels)
399
{
400
  LEVEL l;            /* the level that we register */
401
  POSIXSTAR_level_des *lev;  /* for readableness only */
402
  PID i;              /* a counter */
403
  int x;              /* a counter */
404
 
405
  printk("POSIXSTAR_register_level\n");
406
 
407
  l = level_alloc_descriptor(sizeof(POSIXSTAR_level_des));
408
 
409
  lev = (POSIXSTAR_level_des *)level_table[l];
410
 
411
  printk("    lev=%d\n",(int)lev);
412
 
413
  /* fill the standard descriptor */
414
  /*
415
    lev->l.private_insert   = NULL;
416
    lev->l.private_extract  = NULL;
417
    lev->l.private_dispatch = NULL;
418
    lev->l.private_epilogue = NULL;
419
  */
420
 
421
  //lev->l.public_scheduler = NULL;
422
  lev->l.public_create    = POSIXSTAR_public_create;
423
  lev->l.public_end       = POSIXSTAR_public_end;
424
  lev->l.public_dispatch  = POSIXSTAR_public_dispatch;
425
  lev->l.public_epilogue  = POSIXSTAR_public_epilogue;
426
  lev->l.public_activate  = POSIXSTAR_public_activate;
427
  lev->l.public_unblock   = POSIXSTAR_public_unblock;
428
  lev->l.public_block     = POSIXSTAR_public_block;
429
  lev->l.public_message   = POSIXSTAR_public_message;
430
  lev->l.public_eligible  = POSIXSTAR_public_eligible;
431
 
432
  /* fill the POSIX descriptor part */
433
  for (i = 0; i < MAX_PROC; i++)
434
    lev->nact[i] = -1;
435
 
436
  lev->maxpriority = prioritylevels -1;
437
 
438
  lev->ready = (IQUEUE *)kern_alloc(sizeof(IQUEUE) * prioritylevels);
439
 
440
  for (x = 0; x < prioritylevels; x++)
441
    iq_init(&lev->ready[x], NULL, 0);
442
 
443
  if (slice < POSIXSTAR_MINIMUM_SLICE) slice = POSIXSTAR_MINIMUM_SLICE;
444
  if (slice > POSIXSTAR_MAXIMUM_SLICE) slice = POSIXSTAR_MAXIMUM_SLICE;
445
  lev->slice      = slice;
446
  lev->activated=NIL;
447
  lev->budget = budget;
448
  lev->scheduling_level = master;
449
  //lev->multiboot  = mb;
450
 
451
  //if (createmain)
452
  //  sys_atrunlevel(POSIXSTAR_call_main,(void *) l, RUNLEVEL_INIT);
453
 
454
  return l;
455
}
456
 
457
/*+ this function forces the running task to go to his queue tail;
458
    (it works only on the POSIX level) +*/
459
int POSIXSTAR_sched_yield(LEVEL l)
460
{
461
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
462
 
463
  if (proc_table[exec_shadow].task_level != l)
464
    return -1;
465
 
466
  proc_table[exec_shadow].context = kern_context_save();
467
  lev->yielding = 1;
468
  scheduler();
469
  kern_context_load(proc_table[exec_shadow].context);
470
  return 0;
471
}
472
 
473
/*+ this function returns the maximum level allowed for the POSIX level +*/
474
int POSIXSTAR_get_priority_max(LEVEL l)
475
{
476
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
477
  return lev->maxpriority;
478
}
479
 
480
/*+ this function returns the default timeslice for the POSIX level +*/
481
int POSIXSTAR_rr_get_interval(LEVEL l)
482
{
483
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
484
  return lev->slice;
485
}
486
 
487
/*+ this functions returns some paramaters of a task;
488
    policy must be NRT_RR_POLICY or NRT_FIFO_POLICY;
489
    priority must be in the range [0..prioritylevels]
490
    returns ENOSYS or ESRCH if there are problems +*/
491
int POSIXSTAR_getschedparam(LEVEL l, PID p, int *policy, int *priority)
492
{
493
  if (p<0 || p>= MAX_PROC || proc_table[p].status == FREE)
494
    return ESRCH;
495
 
496
  if (proc_table[p].task_level != l)
497
    return ENOSYS;
498
 
499
  if (proc_table[p].control & CONTROL_CAP)
500
    *policy = NRT_RR_POLICY;
501
  else
502
    *policy = NRT_FIFO_POLICY;
503
 
504
  *priority = ((POSIXSTAR_level_des *)(level_table[l]))->priority[p];
505
 
506
  return 0;
507
}
508
 
509
/*+ this functions sets paramaters of a task +*/
510
int POSIXSTAR_setschedparam(LEVEL l, PID p, int policy, int priority)
511
{
512
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
513
 
514
  if (p<0 || p>= MAX_PROC || proc_table[p].status == FREE)
515
    return ESRCH;
516
 
517
  if (proc_table[p].task_level != l)
518
    return ENOSYS;
519
 
520
  if (policy == SCHED_RR)
521
    proc_table[p].control |= CONTROL_CAP;
522
  else if (policy == SCHED_FIFO)
523
    proc_table[p].control &= ~CONTROL_CAP;
524
  else
525
    return EINVAL;
526
 
527
  if (lev->priority[p] != priority) {
528
    if (proc_table[p].status == POSIXSTAR_READY) {
529
      iq_extract(p,&lev->ready[lev->priority[p]]);
530
      lev->priority[p] = priority;
531
      iq_insertlast(p,&lev->ready[priority]);
532
    }
533
    else
534
      lev->priority[p] = priority;
535
  }
536
 
537
  return 0;
538
}
539
 
540
 
541