Subversion Repositories shark

Rev

Rev 226 | Rev 270 | 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
  int flag[MAX_PROC];
83
 
84
  int new_level[MAX_PROC];
85
  int new_slice[MAX_PROC];
86
  int new_control[MAX_PROC];
87
 
88
} POSIXSTAR_level_des;
89
 
90
/* the private scheduler choice a task and insert in cbsstar module */
91
/* This is not efficient but very fair :-)
92
   The need of all this stuff is because if a task execute a long time
93
   due to (shadow!) priority inheritance, then the task shall go to the
94
   tail of the queue many times... */
95
 
96
static void POSIXSTAR_private_scheduler(POSIXSTAR_level_des * lev)
97
{
98
  /* the old posix scheduler select the private job for CBS */
99
  PID p = NIL;
100
 
101
  int prio;
102
 
103
  prio = lev->maxpriority;
104
 
105
  for (;;) {
106
    p = iq_query_first(&lev->ready[prio]);
107
    if (p == NIL) {
108
      if (prio) {
109
        prio--;
110
        continue;
111
      }
112
      else {
113
        p=NIL;
114
        break;
115
      }
116
    }
117
 
118
    if ((proc_table[p].control & CONTROL_CAP) &&
119
        (proc_table[p].avail_time <= 0)) {
120
      if (proc_table[p].avail_time<=0)
121
        proc_table[p].avail_time += proc_table[p].wcet;
122
      iq_extract(p,&lev->ready[prio]);
123
      iq_insertlast(p,&lev->ready[prio]);
124
    }
125
    else {
126
      break;
127
    }
128
  }
129
 
130
  if (p!=lev->activated) {
131
    if (lev->activated != NIL )  {
132
     level_table[ lev->scheduling_level ]->
133
       private_extract(lev->scheduling_level, lev->activated);
134
   }
135
   lev->activated = p;
136
 
137
   if (p != NIL) {
138
     BUDGET_TASK_MODEL b;
139
     budget_task_default_model(b, lev->budget[p]);
140
     #ifdef POSIXSTAR_DEBUG
141
       kern_printf("(PS:SchAct:%d:%d)",p,lev->budget[p]);
142
     #endif
143
     level_table[ lev->scheduling_level ]->
144
       private_insert(lev->scheduling_level, p, (TASK_MODEL *)&b);
145
   }
146
 }
147
}
148
 
149
static int POSIXSTAR_public_eligible(LEVEL l, PID p)
150
{
151
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
152
  if (p==lev->activated) {
153
    return level_table[ lev->scheduling_level ]->
154
      private_eligible(lev->scheduling_level,p);
155
  }
156
  return 0;
157
}
158
 
159
 
160
static int POSIXSTAR_public_create(LEVEL l, PID p, TASK_MODEL *m)
161
{
162
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
163
  NRT_TASK_MODEL *nrt;
164
 
165
  /* DEBUG */
166
  #ifdef POSIXSTAR_DEBUG
167
    kern_printf("(PS:Crt:%d)",p);
168
  #endif
169
 
170
  if (m->pclass != NRT_PCLASS) return -1;
171
  if (m->level != 0 && m->level != l) return -1;
172
 
173
  nrt = (NRT_TASK_MODEL *)m;
174
 
175
  /* the task state is set at SLEEP by the general task_create */
176
 
177
  /* I used the wcet field because using wcet can account if a task
178
     consume more than the timeslice... */
179
 
180
  if (nrt->inherit == NRT_INHERIT_SCHED &&
181
      proc_table[exec_shadow].task_level == l) {
182
    /* We inherit the scheduling properties if the scheduling level
183
       *is* the same */
184
    lev->priority[p] = lev->priority[exec_shadow];
185
 
186
    proc_table[p].avail_time = proc_table[exec_shadow].avail_time;
187
    proc_table[p].wcet       = proc_table[exec_shadow].wcet;
188
 
189
    proc_table[p].control = (proc_table[p].control & ~CONTROL_CAP) |
190
                            (proc_table[exec_shadow].control & CONTROL_CAP);
191
 
192
    lev->nact[p] = (lev->nact[exec_shadow] == -1) ? -1 : 0;
193
  }
194
  else {
195
    if (nrt->weight<=lev->maxpriority)
196
      lev->priority[p] = nrt->weight;
197
    else lev->priority[p]=lev->maxpriority;
198
 
199
    if (nrt->slice) {
200
      proc_table[p].avail_time = nrt->slice;
201
      proc_table[p].wcet       = nrt->slice;
202
    }
203
    else {
204
      proc_table[p].avail_time = lev->slice;
205
      proc_table[p].wcet       = lev->slice;
206
    }
207
 
208
    if (nrt->policy == NRT_RR_POLICY) {
209
      proc_table[p].control   |= CONTROL_CAP;
210
    }
211
 
212
    if (nrt->arrivals == SAVE_ARRIVALS)
213
      lev->nact[p] = 0;
214
    else
215
      lev->nact[p] = -1;
216
 
217
  }
218
 
219
  return 0; /* OK */
220
}
221
 
222
static void POSIXSTAR_public_dispatch(LEVEL l, PID p, int nostop)
223
{
224
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
225
 
226
  /* the task state is set EXE by the scheduler()
227
     we extract the task from the ready queue
228
     NB: we can't assume that p is the first task in the queue!!! */
229
 
230
  #ifdef POSIXSTAR_DEBUG
231
     kern_printf("(PS:Dsp:%d)",p);
232
  #endif
233
 
234
  if (p==lev->activated)
235
    level_table[lev->scheduling_level]->private_dispatch(lev->scheduling_level, p, nostop);
236
 
237
}
238
 
226 giacomo 239
static void POSIXSTAR_public_epilogue(LEVEL l, PID p)
240
{
241
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
221 giacomo 242
 
226 giacomo 243
  #ifdef POSIXSTAR_DEBUG
244
    kern_printf("(PS:Epi:%d)",p);
245
  #endif 
246
 
221 giacomo 247
  if (p==lev->activated) {
248
    if (lev->yielding) {
249
      lev->yielding = 0;
250
      iq_extract(p,&lev->ready[lev->priority[p]]);
251
      iq_insertlast(p,&lev->ready[lev->priority[p]]);
252
    }
253
    /* check if the slice is finished and insert the task in the coPOSIXect
254
       qqueue position */
255
    else if (proc_table[p].control & CONTROL_CAP &&
256
           proc_table[p].avail_time <= 0) {
257
 
258
      iq_extract(p,&lev->ready[lev->priority[p]]);
259
      iq_insertlast(p,&lev->ready[lev->priority[p]]);
260
 
261
      POSIXSTAR_private_scheduler(lev);
262
      if (p==lev->activated)
263
        level_table[lev->scheduling_level]->private_epilogue(lev->scheduling_level,p);
264
 
265
    }
266
    else  {
267
 
268
      level_table[lev->scheduling_level]->private_epilogue(lev->scheduling_level,p);
269
 
270
      POSIXSTAR_private_scheduler(lev);
271
 
272
    }
273
 
274
    proc_table[p].status = POSIXSTAR_READY;
275
 
276
  }
277
 
278
 
279
}
280
 
281
static void POSIXSTAR_internal_activate(POSIXSTAR_level_des *lev, PID p)
282
{
283
 
284
  /* Insert task in the correct position */
285
  proc_table[p].status = POSIXSTAR_READY;
286
  iq_insertlast(p,&lev->ready[lev->priority[p]]);
287
 
288
}
289
 
290
 
291
static void POSIXSTAR_public_activate(LEVEL l, PID p)
292
{
293
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
294
 
295
  /* Test if we are trying to activate a non sleeping task    */
296
  /* save activation (only if needed...) */
297
  if (proc_table[p].status != SLEEP) {
298
    if (lev->nact[p] != -1)
299
      lev->nact[p]++;
300
    return;
301
  }
302
 
303
  #ifdef POSIXSTAR_DEBUG 
304
    kern_printf("(PS:Act:%d)",p);
305
  #endif
306
 
307
  POSIXSTAR_internal_activate(lev, p);
308
  POSIXSTAR_private_scheduler(lev);
309
 
310
}
311
 
312
 
313
static void POSIXSTAR_public_unblock(LEVEL l, PID p)
314
{
315
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
316
 
317
  /* Similar to POSIX_task_activate, but we don't check in what state
318
     the task is */
319
 
320
  #ifdef POSIXSTAR_DEBUG
321
    kern_printf("(PS:UnBlk:%d)",p);
322
  #endif
323
  /* Insert task in the coPOSIXect position */
324
  proc_table[p].status = POSIXSTAR_READY;
325
  iq_insertlast(p,&lev->ready[lev->priority[p]]);
326
  POSIXSTAR_private_scheduler(lev);
327
 
328
}
329
 
330
static void POSIXSTAR_public_block(LEVEL l, PID p)
331
{  
332
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
333
 
334
  /* Extract the running task from the level
335
     . we have already extract it from the ready queue at the dispatch time.
336
     . the capacity event have to be removed by the generic kernel
337
     . the wcet don't need modification...
338
     . the state of the task is set by the calling function
339
 
340
     So, we do nothing!!!
341
  */
342
 
343
  #ifdef POSIXSTAR_DEBUG
344
    kern_printf("(PS:Blk:%d)", p);     
345
  #endif
346
 
347
  iq_extract(p,&lev->ready[lev->priority[p]]);
348
  if (p==lev->activated) lev->activated = NIL;
349
  POSIXSTAR_private_scheduler(lev);
350
 
351
}
352
 
353
static int POSIXSTAR_public_message(LEVEL l, PID p, void *m)
354
{
355
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
356
 
357
  #ifdef POSIXSTAR_DEBUG
358
    kern_printf("(PS:Msg:%d)",p);
359
  #endif
360
 
361
  switch ((long)(m)) {
362
 
363
    /* Task EndCycle */
364
    case (long)(NULL):
365
 
366
      if (lev->nact[p] > 0) {
367
        /* continue!!!! */
368
        lev->nact[p]--;
369
        iq_extract(p,&lev->ready[lev->priority[p]]);
370
        iq_insertfirst(p,&lev->ready[lev->priority[p]]);
371
        proc_table[p].status = POSIXSTAR_READY;
372
      }
373
      else {
374
        proc_table[p].status = SLEEP;
375
        iq_extract(p,&lev->ready[lev->priority[p]]);
376
      }
377
 
378
      jet_update_endcycle(); /* Update the Jet data... */
379
      trc_logevent(TRC_ENDCYCLE,&exec_shadow); /* tracer stuff */
380
      POSIXSTAR_private_scheduler(lev);
381
 
382
      break;
383
 
384
    /* Task Disable */
385
    case (long)(1):
386
 
387
      break;
388
 
389
    default:
390
 
241 giacomo 391
      break;
221 giacomo 392
 
393
  }
394
 
395
  return 0;
396
 
397
}
398
 
399
static void POSIXSTAR_public_end(LEVEL l, PID p)
400
{
401
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
402
 
403
  #ifdef POSIXSTAR_DEBUG
404
    kern_printf("(PS:End:%d)", p);
405
  #endif
406
 
407
  lev->nact[p] = -1;
408
 
409
  /* then, we insert the task in the free queue */
410
  proc_table[p].status = FREE;
411
  iq_priority_insert(p,NULL);
412
  POSIXSTAR_private_scheduler(lev);
413
 
414
}
415
 
416
/* Registration functions */
417
 
418
/*+ Registration function:
419
    TIME slice                the slice for the Round Robin queue +*/
420
LEVEL POSIXSTAR_register_level(int master, TIME slice,
421
                       int prioritylevels)
422
{
423
  LEVEL l;            /* the level that we register */
424
  POSIXSTAR_level_des *lev;  /* for readableness only */
425
  PID i;              /* a counter */
426
  int x;              /* a counter */
427
 
428
  #ifdef POSIXSTRA_DEBUG
429
    kern_printf("POSIXSTAR_register_level\n");
430
  #endif
431
 
432
  l = level_alloc_descriptor(sizeof(POSIXSTAR_level_des));
433
 
434
  lev = (POSIXSTAR_level_des *)level_table[l];
435
 
436
  lev->l.public_create    = POSIXSTAR_public_create;
437
  lev->l.public_end       = POSIXSTAR_public_end;
438
  lev->l.public_dispatch  = POSIXSTAR_public_dispatch;
439
  lev->l.public_epilogue  = POSIXSTAR_public_epilogue;
440
  lev->l.public_activate  = POSIXSTAR_public_activate;
441
  lev->l.public_unblock   = POSIXSTAR_public_unblock;
442
  lev->l.public_block     = POSIXSTAR_public_block;
443
  lev->l.public_message   = POSIXSTAR_public_message;
444
  lev->l.public_eligible  = POSIXSTAR_public_eligible;
445
 
446
  /* fill the POSIX descriptor part */
447
  for (i = 0; i < MAX_PROC; i++) {
448
    lev->nact[i] = -1;
449
    lev->budget[i] = -1;
450
    lev->flag[i] = 0;
451
 
452
    lev->new_level[i] = 0;
453
    lev->new_slice[i] = 0;
454
    lev->new_control[i] = 0;
455
 
456
  }
457
 
458
  lev->maxpriority = prioritylevels - 1;
459
 
460
  lev->ready = (IQUEUE *)kern_alloc(sizeof(IQUEUE) * prioritylevels);
461
 
462
  for (x = 0; x < prioritylevels; x++)
463
    iq_init(&lev->ready[x], NULL, 0);
464
 
465
  if (slice < POSIXSTAR_MINIMUM_SLICE) slice = POSIXSTAR_MINIMUM_SLICE;
466
  if (slice > POSIXSTAR_MAXIMUM_SLICE) slice = POSIXSTAR_MAXIMUM_SLICE;
467
  lev->slice = slice;
468
  lev->activated = NIL;
469
  lev->scheduling_level = master;
470
 
471
  return l;
472
 
473
}
474
 
475
int POSIXSTAR_setbudget(LEVEL l, PID p, int budget)
476
{
477
 
478
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
479
 
480
  lev->budget[p] = budget;
481
 
482
  return 0;
483
 
484
}
485
 
486
int POSIXSTAR_getbudget(LEVEL l, PID p)
487
{
488
 
489
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
490
 
491
  return lev->budget[p];
492
 
493
}
494
 
495
int POSIXSTAR_budget_has_thread(LEVEL l, int budget)
496
{
497
 
498
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
499
  int i;
500
 
501
  for(i = 0; i< MAX_PROC; i++)
502
    if (lev->budget[i] == budget) return 1;
503
 
504
  return 0;
505
 
506
}
507
 
508
/*+ this function forces the running task to go to his queue tail;
509
    (it works only on the POSIX level) +*/
510
int POSIXSTAR_sched_yield(LEVEL l)
511
{
512
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
513
 
514
  if (proc_table[exec_shadow].task_level != l)
515
    return -1;
516
 
517
  proc_table[exec_shadow].context = kern_context_save();
518
  lev->yielding = 1;
519
  scheduler();
520
  kern_context_load(proc_table[exec_shadow].context);
521
  return 0;
522
}
523
 
524
/*+ this function returns the maximum level allowed for the POSIX level +*/
525
int POSIXSTAR_get_priority_max(LEVEL l)
526
{
527
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
528
  return lev->maxpriority;
529
}
530
 
531
/*+ this function returns the default timeslice for the POSIX level +*/
532
int POSIXSTAR_rr_get_interval(LEVEL l)
533
{
534
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
535
  return lev->slice;
536
}
537
 
538
/*+ this functions returns some paramaters of a task;
539
    policy must be NRT_RR_POLICY or NRT_FIFO_POLICY;
540
    priority must be in the range [0..prioritylevels]
541
    returns ENOSYS or ESRCH if there are problems +*/
542
int POSIXSTAR_getschedparam(LEVEL l, PID p, int *policy, int *priority)
543
{
544
  if (p<0 || p>= MAX_PROC || proc_table[p].status == FREE)
545
    return ESRCH;
546
 
547
  if (proc_table[p].task_level != l)
548
    return ENOSYS;
549
 
550
  if (proc_table[p].control & CONTROL_CAP)
551
    *policy = NRT_RR_POLICY;
552
  else
553
    *policy = NRT_FIFO_POLICY;
554
 
555
  *priority = ((POSIXSTAR_level_des *)(level_table[l]))->priority[p];
556
 
557
  return 0;
558
}
559
 
560
/*+ this functions sets paramaters of a task +*/
561
int POSIXSTAR_setschedparam(LEVEL l, PID p, int policy, int priority)
562
{
563
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
564
 
565
  if (p<0 || p>= MAX_PROC || proc_table[p].status == FREE)
566
    return ESRCH;
567
 
568
  if (proc_table[p].task_level != l)
569
    return ENOSYS;
570
 
571
  if (policy == SCHED_RR)
572
    proc_table[p].control |= CONTROL_CAP;
573
  else if (policy == SCHED_FIFO)
574
    proc_table[p].control &= ~CONTROL_CAP;
575
  else
576
    return EINVAL;
577
 
578
  if (lev->priority[p] != priority) {
579
    if (proc_table[p].status == POSIXSTAR_READY) {
580
      iq_extract(p,&lev->ready[lev->priority[p]]);
581
      lev->priority[p] = priority;
582
      iq_insertlast(p,&lev->ready[priority]);
583
    }
584
    else
585
      lev->priority[p] = priority;
586
  }
587
 
588
  return 0;
589
}
590