Subversion Repositories shark

Rev

Rev 270 | Rev 296 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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