Subversion Repositories shark

Rev

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