Subversion Repositories shark

Rev

Rev 402 | 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"
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]);
402 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]]);
402 giacomo 278
   } else {
279
 
280
     if (proc_table[exec].avail_time <= 0) {
281
 
282
       POSIXSTAR_private_scheduler(lev);
283
 
284
       if (exec==lev->activated) {
285
         level_table[lev->scheduling_level]->private_epilogue(lev->scheduling_level,p);  
286
       }
287
 
288
     } else  {
289
 
290
       level_table[lev->scheduling_level]->private_epilogue(lev->scheduling_level,p);
291
 
292
     }
293
 
393 giacomo 294
   }
295
 
402 giacomo 296
   proc_table[exec].status = POSIXSTAR_READY;
221 giacomo 297
 
402 giacomo 298
 } else {
299
 
300
   level_table[proc_table[exec].task_level]->public_epilogue(proc_table[exec].task_level,p);
301
 
302
 }
303
 
221 giacomo 304
}
305
 
306
static void POSIXSTAR_internal_activate(POSIXSTAR_level_des *lev, PID p)
307
{
308
 
309
  /* Insert task in the correct position */
310
  proc_table[p].status = POSIXSTAR_READY;
311
  iq_insertlast(p,&lev->ready[lev->priority[p]]);
312
 
313
}
314
 
315
 
783 giacomo 316
static void POSIXSTAR_public_activate(LEVEL l, PID p, struct timespec *t)
221 giacomo 317
{
318
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
319
 
320
  /* Test if we are trying to activate a non sleeping task    */
321
  /* save activation (only if needed...) */
322
  if (proc_table[p].status != SLEEP) {
323
    if (lev->nact[p] != -1)
324
      lev->nact[p]++;
325
    return;
326
  }
327
 
328
  #ifdef POSIXSTAR_DEBUG 
329
    kern_printf("(PS:Act:%d)",p);
330
  #endif
331
 
332
  POSIXSTAR_internal_activate(lev, p);
333
  POSIXSTAR_private_scheduler(lev);
334
 
335
}
336
 
337
 
338
static void POSIXSTAR_public_unblock(LEVEL l, PID p)
339
{
340
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
341
 
342
  /* Similar to POSIX_task_activate, but we don't check in what state
343
     the task is */
344
 
345
  #ifdef POSIXSTAR_DEBUG
346
    kern_printf("(PS:UnBlk:%d)",p);
347
  #endif
348
  /* Insert task in the coPOSIXect position */
349
  proc_table[p].status = POSIXSTAR_READY;
350
  iq_insertlast(p,&lev->ready[lev->priority[p]]);
351
  POSIXSTAR_private_scheduler(lev);
352
 
353
}
354
 
355
static void POSIXSTAR_public_block(LEVEL l, PID p)
356
{  
357
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
358
 
359
  /* Extract the running task from the level
360
     . we have already extract it from the ready queue at the dispatch time.
361
     . the capacity event have to be removed by the generic kernel
362
     . the wcet don't need modification...
363
     . the state of the task is set by the calling function
364
 
365
     So, we do nothing!!!
366
  */
367
 
368
  #ifdef POSIXSTAR_DEBUG
369
    kern_printf("(PS:Blk:%d)", p);     
370
  #endif
371
 
372
  iq_extract(p,&lev->ready[lev->priority[p]]);
373
  POSIXSTAR_private_scheduler(lev);
374
 
375
}
376
 
377
static int POSIXSTAR_public_message(LEVEL l, PID p, void *m)
378
{
379
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
380
 
381
  #ifdef POSIXSTAR_DEBUG
382
    kern_printf("(PS:Msg:%d)",p);
383
  #endif
384
 
385
  switch ((long)(m)) {
386
 
387
    /* Task EndCycle */
388
    case (long)(NULL):
389
 
390
      if (lev->nact[p] > 0) {
391
        /* continue!!!! */
392
        lev->nact[p]--;
393
        iq_extract(p,&lev->ready[lev->priority[p]]);
394
        iq_insertfirst(p,&lev->ready[lev->priority[p]]);
395
        proc_table[p].status = POSIXSTAR_READY;
396
      }
397
      else {
398
        proc_table[p].status = SLEEP;
399
        iq_extract(p,&lev->ready[lev->priority[p]]);
400
      }
401
 
402
      jet_update_endcycle(); /* Update the Jet data... */
403
      POSIXSTAR_private_scheduler(lev);
404
 
405
      break;
406
 
407
    /* Task Disable */
408
    case (long)(1):
409
 
410
      break;
411
 
412
    default:
413
 
241 giacomo 414
      break;
221 giacomo 415
 
416
  }
417
 
418
  return 0;
419
 
420
}
421
 
422
static void POSIXSTAR_public_end(LEVEL l, PID p)
423
{
424
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
425
 
426
  #ifdef POSIXSTAR_DEBUG
427
    kern_printf("(PS:End:%d)", p);
428
  #endif
429
 
430
  lev->nact[p] = -1;
348 trimarchi 431
  /* extract task from the queue */
432
  iq_extract(p,&lev->ready[lev->priority[p]]);
221 giacomo 433
 
434
  /* then, we insert the task in the free queue */
435
  proc_table[p].status = FREE;
436
  iq_priority_insert(p,NULL);
437
  POSIXSTAR_private_scheduler(lev);
438
 
439
}
440
 
441
/* Registration functions */
442
 
443
/*+ Registration function:
444
    TIME slice                the slice for the Round Robin queue +*/
445
LEVEL POSIXSTAR_register_level(int master, TIME slice,
446
                       int prioritylevels)
447
{
448
  LEVEL l;            /* the level that we register */
449
  POSIXSTAR_level_des *lev;  /* for readableness only */
450
  PID i;              /* a counter */
451
  int x;              /* a counter */
452
 
453
  #ifdef POSIXSTRA_DEBUG
454
    kern_printf("POSIXSTAR_register_level\n");
455
  #endif
456
 
457
  l = level_alloc_descriptor(sizeof(POSIXSTAR_level_des));
458
 
459
  lev = (POSIXSTAR_level_des *)level_table[l];
460
 
270 giacomo 461
  lev->l.public_guarantee = NULL;
221 giacomo 462
  lev->l.public_create    = POSIXSTAR_public_create;
463
  lev->l.public_end       = POSIXSTAR_public_end;
464
  lev->l.public_dispatch  = POSIXSTAR_public_dispatch;
465
  lev->l.public_epilogue  = POSIXSTAR_public_epilogue;
466
  lev->l.public_activate  = POSIXSTAR_public_activate;
467
  lev->l.public_unblock   = POSIXSTAR_public_unblock;
468
  lev->l.public_block     = POSIXSTAR_public_block;
469
  lev->l.public_message   = POSIXSTAR_public_message;
470
  lev->l.public_eligible  = POSIXSTAR_public_eligible;
471
 
472
  /* fill the POSIX descriptor part */
473
  for (i = 0; i < MAX_PROC; i++) {
474
    lev->nact[i] = -1;
475
    lev->budget[i] = -1;
476
  }
477
 
478
  lev->maxpriority = prioritylevels - 1;
479
 
480
  lev->ready = (IQUEUE *)kern_alloc(sizeof(IQUEUE) * prioritylevels);
481
 
482
  for (x = 0; x < prioritylevels; x++)
483
    iq_init(&lev->ready[x], NULL, 0);
484
 
485
  if (slice < POSIXSTAR_MINIMUM_SLICE) slice = POSIXSTAR_MINIMUM_SLICE;
486
  if (slice > POSIXSTAR_MAXIMUM_SLICE) slice = POSIXSTAR_MAXIMUM_SLICE;
487
  lev->slice = slice;
488
  lev->activated = NIL;
489
  lev->scheduling_level = master;
298 giacomo 490
  lev->cap_lev = NIL;
296 trimarchi 491
  NULL_TIMESPEC(&lev->cap_lasttime);
492
 
221 giacomo 493
  return l;
494
 
495
}
496
 
497
int POSIXSTAR_setbudget(LEVEL l, PID p, int budget)
498
{
499
 
500
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
501
 
502
  lev->budget[p] = budget;
503
 
504
  return 0;
505
 
506
}
507
 
508
int POSIXSTAR_getbudget(LEVEL l, PID p)
509
{
510
 
511
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
512
 
513
  return lev->budget[p];
514
 
515
}
516
 
517
int POSIXSTAR_budget_has_thread(LEVEL l, int budget)
518
{
519
 
520
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
521
  int i;
522
 
523
  for(i = 0; i< MAX_PROC; i++)
524
    if (lev->budget[i] == budget) return 1;
525
 
526
  return 0;
527
 
528
}
529
 
530
/*+ this function forces the running task to go to his queue tail;
531
    (it works only on the POSIX level) +*/
532
int POSIXSTAR_sched_yield(LEVEL l)
533
{
534
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
535
 
536
  if (proc_table[exec_shadow].task_level != l)
537
    return -1;
538
 
539
  proc_table[exec_shadow].context = kern_context_save();
540
  lev->yielding = 1;
541
  scheduler();
542
  kern_context_load(proc_table[exec_shadow].context);
543
  return 0;
544
}
545
 
546
/*+ this function returns the maximum level allowed for the POSIX level +*/
547
int POSIXSTAR_get_priority_max(LEVEL l)
548
{
549
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
550
  return lev->maxpriority;
551
}
552
 
553
/*+ this function returns the default timeslice for the POSIX level +*/
554
int POSIXSTAR_rr_get_interval(LEVEL l)
555
{
556
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
557
  return lev->slice;
558
}
559
 
560
/*+ this functions returns some paramaters of a task;
561
    policy must be NRT_RR_POLICY or NRT_FIFO_POLICY;
562
    priority must be in the range [0..prioritylevels]
563
    returns ENOSYS or ESRCH if there are problems +*/
564
int POSIXSTAR_getschedparam(LEVEL l, PID p, int *policy, int *priority)
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
 
393 giacomo 572
  *policy = NRT_RR_POLICY;
221 giacomo 573
  *priority = ((POSIXSTAR_level_des *)(level_table[l]))->priority[p];
574
 
575
  return 0;
576
}
577
 
578
/*+ this functions sets paramaters of a task +*/
579
int POSIXSTAR_setschedparam(LEVEL l, PID p, int policy, int priority)
580
{
581
  POSIXSTAR_level_des *lev = (POSIXSTAR_level_des *)(level_table[l]);
582
 
583
  if (p<0 || p>= MAX_PROC || proc_table[p].status == FREE)
584
    return ESRCH;
585
 
586
  if (proc_table[p].task_level != l)
587
    return ENOSYS;
588
  if (lev->priority[p] != priority) {
589
    if (proc_table[p].status == POSIXSTAR_READY) {
590
      iq_extract(p,&lev->ready[lev->priority[p]]);
591
      lev->priority[p] = priority;
592
      iq_insertlast(p,&lev->ready[priority]);
593
    }
594
    else
595
      lev->priority[p] = priority;
596
  }
597
 
598
  return 0;
599
}
600