Subversion Repositories shark

Rev

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