Subversion Repositories shark

Rev

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