Subversion Repositories shark

Rev

Rev 241 | Rev 293 | 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
 
270 giacomo 436
  lev->l.public_guarantee = NULL;
221 giacomo 437
  lev->l.public_create    = POSIXSTAR_public_create;
438
  lev->l.public_end       = POSIXSTAR_public_end;
439
  lev->l.public_dispatch  = POSIXSTAR_public_dispatch;
440
  lev->l.public_epilogue  = POSIXSTAR_public_epilogue;
441
  lev->l.public_activate  = POSIXSTAR_public_activate;
442
  lev->l.public_unblock   = POSIXSTAR_public_unblock;
443
  lev->l.public_block     = POSIXSTAR_public_block;
444
  lev->l.public_message   = POSIXSTAR_public_message;
445
  lev->l.public_eligible  = POSIXSTAR_public_eligible;
446
 
447
  /* fill the POSIX descriptor part */
448
  for (i = 0; i < MAX_PROC; i++) {
449
    lev->nact[i] = -1;
450
    lev->budget[i] = -1;
451
    lev->flag[i] = 0;
452
 
453
    lev->new_level[i] = 0;
454
    lev->new_slice[i] = 0;
455
    lev->new_control[i] = 0;
456
 
457
  }
458
 
459
  lev->maxpriority = prioritylevels - 1;
460
 
461
  lev->ready = (IQUEUE *)kern_alloc(sizeof(IQUEUE) * prioritylevels);
462
 
463
  for (x = 0; x < prioritylevels; x++)
464
    iq_init(&lev->ready[x], NULL, 0);
465
 
466
  if (slice < POSIXSTAR_MINIMUM_SLICE) slice = POSIXSTAR_MINIMUM_SLICE;
467
  if (slice > POSIXSTAR_MAXIMUM_SLICE) slice = POSIXSTAR_MAXIMUM_SLICE;
468
  lev->slice = slice;
469
  lev->activated = NIL;
470
  lev->scheduling_level = master;
471
 
472
  return l;
473
 
474
}
475
 
476
int POSIXSTAR_setbudget(LEVEL l, PID p, int budget)
477
{
478
 
479
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
480
 
481
  lev->budget[p] = budget;
482
 
483
  return 0;
484
 
485
}
486
 
487
int POSIXSTAR_getbudget(LEVEL l, PID p)
488
{
489
 
490
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
491
 
492
  return lev->budget[p];
493
 
494
}
495
 
496
int POSIXSTAR_budget_has_thread(LEVEL l, int budget)
497
{
498
 
499
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
500
  int i;
501
 
502
  for(i = 0; i< MAX_PROC; i++)
503
    if (lev->budget[i] == budget) return 1;
504
 
505
  return 0;
506
 
507
}
508
 
509
/*+ this function forces the running task to go to his queue tail;
510
    (it works only on the POSIX level) +*/
511
int POSIXSTAR_sched_yield(LEVEL l)
512
{
513
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
514
 
515
  if (proc_table[exec_shadow].task_level != l)
516
    return -1;
517
 
518
  proc_table[exec_shadow].context = kern_context_save();
519
  lev->yielding = 1;
520
  scheduler();
521
  kern_context_load(proc_table[exec_shadow].context);
522
  return 0;
523
}
524
 
525
/*+ this function returns the maximum level allowed for the POSIX level +*/
526
int POSIXSTAR_get_priority_max(LEVEL l)
527
{
528
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
529
  return lev->maxpriority;
530
}
531
 
532
/*+ this function returns the default timeslice for the POSIX level +*/
533
int POSIXSTAR_rr_get_interval(LEVEL l)
534
{
535
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
536
  return lev->slice;
537
}
538
 
539
/*+ this functions returns some paramaters of a task;
540
    policy must be NRT_RR_POLICY or NRT_FIFO_POLICY;
541
    priority must be in the range [0..prioritylevels]
542
    returns ENOSYS or ESRCH if there are problems +*/
543
int POSIXSTAR_getschedparam(LEVEL l, PID p, int *policy, int *priority)
544
{
545
  if (p<0 || p>= MAX_PROC || proc_table[p].status == FREE)
546
    return ESRCH;
547
 
548
  if (proc_table[p].task_level != l)
549
    return ENOSYS;
550
 
551
  if (proc_table[p].control & CONTROL_CAP)
552
    *policy = NRT_RR_POLICY;
553
  else
554
    *policy = NRT_FIFO_POLICY;
555
 
556
  *priority = ((POSIXSTAR_level_des *)(level_table[l]))->priority[p];
557
 
558
  return 0;
559
}
560
 
561
/*+ this functions sets paramaters of a task +*/
562
int POSIXSTAR_setschedparam(LEVEL l, PID p, int policy, int priority)
563
{
564
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
565
 
566
  if (p<0 || p>= MAX_PROC || proc_table[p].status == FREE)
567
    return ESRCH;
568
 
569
  if (proc_table[p].task_level != l)
570
    return ENOSYS;
571
 
572
  if (policy == SCHED_RR)
573
    proc_table[p].control |= CONTROL_CAP;
574
  else if (policy == SCHED_FIFO)
575
    proc_table[p].control &= ~CONTROL_CAP;
576
  else
577
    return EINVAL;
578
 
579
  if (lev->priority[p] != priority) {
580
    if (proc_table[p].status == POSIXSTAR_READY) {
581
      iq_extract(p,&lev->ready[lev->priority[p]]);
582
      lev->priority[p] = priority;
583
      iq_insertlast(p,&lev->ready[priority]);
584
    }
585
    else
586
      lev->priority[p] = priority;
587
  }
588
 
589
  return 0;
590
}
591