Subversion Repositories shark

Rev

Rev 1120 | Rev 1377 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1120 pj 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
 *   Paolo Gai           <pj@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, Gerardo Lamastra and Giuseppe Lipari
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 WARRANTY; without even the implied warranty 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
 *
1123 pj 37
 * CVS :        $Id: aster5.c,v 1.2 2003-01-07 17:10:15 pj Exp $
1120 pj 38
 
39
 this is a part of the classic Hartik demo Aster.
40
 
41
 it is based on test 13 (d), and use the CBS to serve the periodic tasks.
42
 
43
 There are not periodic tasks, only CBS tasks.
44
 
45
 The tasks use a PI, NPP or NOP mutex to access the video memory.
46
 
47
 A flag (LONGSC) is provided to try long and short critical sections.
48
 
49
 This demo is really interesting because you can note the behavior of
50
 the system, and the differences between the various protocols...
51
 
52
*/
53
 
54
#include "kernel/kern.h"
55
#include "modules/edf.h"
56
#include "modules/cbs.h"
57
#include "drivers/keyb.h"
58
 
59
int num_aster = 0;
60
#define ASTER_LIM       60
61
#define DISPLAY_MAX     15
62
#define ASTER_MAX       70
63
#define STAT_Y           9
64
 
65
#define PER_MAX          5
66
#define APER_MAX         8
67
 
68
#define PER_WCET      6200
69
#define APER_WCET    18400
70
#define JET_WCET     10000
71
#define JET_PERIOD  100000
72
 
73
#define APER_REP     22000
74
 
75
//PID aper_table[APER_MAX];
76
 
77
mutex_t m1;
78
 
79
 
80
#define PIMUTEX
81
//#define NPPMUTEX
82
//#define NOPMUTEX
83
 
84
#define LONGSC
85
 
86
#ifdef LONGSC
87
#define SOFT_MET      3000 /* 3000 12000 */
88
#define CLOCK_WCET     400 /*  200   300*/
89
#define ASTER_WCET     400 /*  200   300*/
90
#else
91
#define SOFT_MET     80000 /* 4500 */
92
#define CLOCK_WCET    2000 /* 200*/
93
#define ASTER_WCET    2000 /* 200*/
94
#endif
95
 
96
TASK asteroide(void)
97
{
98
  int i;
99
  int y = rand() % 7 + 1;
100
 
101
  int load1,j;
102
 
103
  char s[2];
104
 
105
  s[0] = '*'; s[1] = 0;
106
 
107
  for (;;) {
108
    i = 1;
109
    while (i < ASTER_LIM) {
110
      load1 = 10000; //8000 + rand()%2000;
111
#ifdef LONGSC
112
      mutex_lock(&m1);
113
#endif
114
      for (j=0; j<load1; j++) {
115
        s[0] = '*' + rand() % 100;
116
#ifndef LONGSC
117
        mutex_lock(&m1);
118
#endif
119
        puts_xy(i,y,rand()%15+1,s);
120
#ifndef LONGSC
121
        mutex_unlock(&m1);
122
#endif
123
      }
124
#ifdef LONGSC
125
      mutex_unlock(&m1);
126
#endif
127
 
128
      //        task_activate(aper_table[rand()%APER_MAX]);
129
      task_endcycle();
130
 
131
      mutex_lock(&m1);
132
      puts_xy(i,y,WHITE," ");
133
      mutex_unlock(&m1);
134
      i++;
135
    }
136
  }
137
  //num_aster--;
138
}
139
 
140
TASK aper_asteroid(void *a)
141
{
142
  int i;
143
  int y = rand() % 7 + 1;
144
 
145
  int load1,j;
146
  int c;
147
 
148
  char s[2];
149
 
150
  c = (int)a;
151
  s[0] = '*'; s[1] = 0;
152
 
153
  for (;;) {
154
    i = 1;
155
    while (i < ASTER_LIM) {
156
      load1 = APER_REP; //8000 + rand()%2000;
157
#ifdef LONGSC
158
      mutex_lock(&m1);
159
#endif
160
      for (j=0; j<load1; j++) {
161
        s[0] = '*' + rand() % 100;
162
#ifndef LONGSC
163
        mutex_lock(&m1);
164
#endif
165
        puts_xy(i,y,rand()%15+1,s);
166
#ifndef LONGSC
167
        mutex_unlock(&m1);
168
#endif
169
      }
170
      s[0] = c;
171
#ifndef LONGSC
172
      mutex_lock(&m1);
173
#endif
174
      puts_xy(i,y,rand()%15+1,s);
175
      mutex_unlock(&m1);
176
 
177
      task_endcycle();
178
 
179
      mutex_lock(&m1);
180
      puts_xy(i,y,WHITE," ");
181
      mutex_unlock(&m1);
182
      i++;
183
    }
184
  }
185
}
186
 
187
TASK soft_aster(void)
188
{
189
  int i;
190
  int y = rand() % 7 + 1;
191
 
192
  int load1,j;
193
 
194
  char s[2];
195
 
196
  s[0] = '*'; s[1] = 0;
197
 
198
  /*for (;;)*/ {
199
    i = 1;
200
    while (i < ASTER_LIM) {
201
      load1 = 1000 + rand()%9000;
202
#ifdef LONGSC
203
      mutex_lock(&m1);
204
#endif
205
      for (j=0; j<load1; j++) {
206
        s[0] = '*' + rand() % 100;
207
#ifndef LONGSC
208
        mutex_lock(&m1);
209
#endif
210
        puts_xy(i,y,rand()%15+1,s);
211
#ifndef LONGSC
212
        mutex_unlock(&m1);
213
#endif
214
      }
215
      s[0] = 1;
216
#ifndef LONGSC
217
      mutex_lock(&m1);
218
#endif
219
      puts_xy(i,y,rand()%15+1,s);
220
      mutex_unlock(&m1);
221
 
222
      //        task_activate(aper_table[rand()%APER_MAX]);
223
      task_endcycle();
224
 
225
      mutex_lock(&m1);
226
      puts_xy(i,y,WHITE," ");
227
      mutex_unlock(&m1);
228
      i++;
229
    }
230
  }
231
  num_aster--;
232
  return 0;
233
}
234
 
235
TASK aster()
236
{
237
  PID p;
238
  //    HARD_TASK_MODEL m;
239
  SOFT_TASK_MODEL m_soft;
240
  int r;
241
  int x; // adaptive bandwidth...
242
 
243
  srand(7);
244
 
245
  /*    periodic_task_default_model(m,0,PER_WCET);
246
        periodic_task_def_ctrl_jet(m);
247
        for (x=0; x<PER_MAX; x++) {
248
        r = (rand() % 200);
249
        periodic_task_def_period(m, (64+r)*1000);
250
        p = task_create("per",asteroide,&m,NULL);
251
        if (p!=-1) task_activate(p);
252
        }
253
  */
254
  soft_task_default_model(m_soft);
255
  soft_task_def_met(m_soft,SOFT_MET);
256
  soft_task_def_ctrl_jet(m_soft);
257
 
258
  x = 128; //64;
259
 
260
  while (1) {
261
    /*        {
262
              PID p;
263
              int x;
264
              p = level_table[0]->level_scheduler(0);
265
              printf_xy(1,8,WHITE,"                                                                               ");
266
 
267
              x = 0;
268
              do {
269
              printf_xy(3*x+1,8,WHITE,"%3d",p);
270
              p = proc_table[p].next;
271
              x++;
272
              } while (p != NIL);
273
              }
274
    */
275
    if (num_aster < ASTER_MAX) {
276
      r = (rand() % 200);
277
 
278
      soft_task_def_period(m_soft, (x+r)*1000);
279
      p = task_create("aaa",soft_aster,&m_soft,NULL);
280
      if (p == -1)
281
        {
282
          if (x < 500 && errno != ENO_AVAIL_TASK)  x += 1;
283
          mutex_lock(&m1);
284
          printf_xy(62,3,WHITE,"adapt=%3u err=%d",
285
                    iq_query_first(&freedesc),errno);
286
          mutex_unlock(&m1);
287
        }
288
      else {
289
        num_aster++;
290
        mutex_lock(&m1);
291
        printf_xy(62,3,WHITE,"adapt=%3u           ",x);//,errno);
292
        mutex_unlock(&m1);
293
 
294
        task_activate(p);
295
        x /= 2;
296
        if (x<50) x = 50;
297
      }
298
    }
299
    task_endcycle();
300
  }
301
}
302
 
303
TASK clock()
304
{
305
  int s = 0, m = 0;
306
 
307
  while(1) {
308
    mutex_lock(&m1);
309
    printf_xy(62,1,WHITE,"%2d:%2d ast=%d",m,s, num_aster);
310
    printf_xy(62,2,WHITE,"Uedf=%12u",EDF_usedbandwidth(0));
311
    printf_xy(62,4,WHITE,"Ucbs=%12u",CBS_usedbandwidth(2));
312
 
313
    mutex_unlock(&m1);
314
    task_endcycle();
315
 
316
    if (++s > 59) {
317
      s = 0;
318
      m++;
319
    }
320
    mutex_lock(&m1);
321
    printf_xy(62,1,WHITE,"%2d:%2d ast=%d",m,s, num_aster);
322
    printf_xy(62,2,WHITE,"Uedf=%12u",EDF_usedbandwidth(0));
323
    printf_xy(62,4,WHITE,"Ucbs=%12u",CBS_usedbandwidth(2));
324
    mutex_unlock(&m1);
325
    task_endcycle();
326
  }
327
}
328
 
329
 
330
 
331
/* we consider the first ASTER_MAX + 2 tasks from the PID 2
332
   and plot on the screen the elapsed times... */
333
TASK jetcontrol()
334
{
335
  int i;  /* a counter */
336
  TIME sum, max, curr, last[5];
337
  int nact;
338
  int j; /* the elements set by jet_gettable */
339
  PID p;
340
 
341
 
342
  mutex_lock(&m1);
343
  printf_xy(0,STAT_Y,WHITE,"PID  Mean T. Max T.  N.A.  Curr.    Last1  Last2  Last3  Last4  Last5");
344
  mutex_unlock(&m1);
345
 
346
  for (;;) {
347
    for (i=0,p=0; i<DISPLAY_MAX+5 && p<MAX_PROC; p++) {
348
      if (jet_getstat(p, &sum, &max, &nact, &curr) == -1 /*||
349
                                                           (proc_table[p].pclass & 0xFF00) == APERIODIC_PCLASS ||
350
                                                           (proc_table[p].pclass & 0xFF00) == PERIODIC_PCLASS*/ ) continue;
351
 
352
      for (j=0; j<5; j++) last[j] = 0;
353
      jet_gettable(p, &last[0], 5);
354
      mutex_lock(&m1);
355
      if (proc_table[p].task_level == 2)
356
        printf_xy(0,STAT_Y+i+1,WHITE,"%-3d  %-6d  %-6d  %-4d p%-7d  %-5d  %-5d  %-5d  %-5d  %-5d",
357
                  p, (int)sum/(nact==0 ? 1 : nact), (int)max, nact, (int)CBS_get_nact(2,p), (int)last[0], (int)last[1], (int)last[2], (int)last[3], (int)last[4]);
358
      //                   p, sum/(nact==0 ? 1 : nact), max, proc_table[p].avail_time, proc_table[p].status, proc_table[p].shadow, proc_table[p].timespec_priority.tv_sec,proc_table[p].timespec_priority.tv_nsec/1000 , CBS_get_nact(2,p), last[4]);
359
      else
360
        printf_xy(0,STAT_Y+i+1,WHITE,"%-3d  %-6d  %-6d  %-4d  %-7d  %-5d  %-5d  %-5d  %-5d  %-5d",
361
                  p, (int)sum/(nact==0 ? 1 : nact), (int)max, nact, (int)curr, (int)last[0], (int)last[1], (int)last[2], (int)last[3], (int)last[4]);
362
      //                   p, (int)sum/(nact==0 ? 1 : nact), (int)max, nact, (int)proc_table[p].status, (int)proc_table[p].shadow, (int)proc_table[p].timespec_priority.tv_sec,(int)proc_table[p].timespec_priority.tv_nsec/1000 , (int)last[3], (int)last[4]);
363
      mutex_unlock(&m1);
364
      i++;
365
    }
366
  }
367
}
368
 
369
void fine(KEY_EVT *e)
370
{
371
  sys_end();
372
}
373
 
374
int main(int argc, char **argv)
375
{
376
  PID p1,p2,p3;//,p4,p5,p6;
377
  HARD_TASK_MODEL m;
378
  //    NRT_TASK_MODEL m_nrt;
379
  SOFT_TASK_MODEL m_aper;
380
  SOFT_TASK_MODEL m_soft;
381
  //    int i;
382
  struct timespec fineprg;
383
 
384
#ifdef PIMUTEX
385
  PI_mutexattr_t a;
386
#endif
387
 
388
#ifdef NPPMUTEX
389
  NPP_mutexattr_t a;
390
#endif
391
 
392
#ifdef NOPMUTEX
393
  NOP_mutexattr_t a;
394
#endif
395
 
396
  KEY_EVT emerg;
397
  //keyb_set_map(itaMap);
398
  emerg.ascii = 'x';
399
  emerg.scan = KEY_X;
400
  emerg.flag = ALTL_BIT;
401
  keyb_hook(emerg,fine);
402
 
403
  clear();
404
 
405
  hard_task_default_model(m);
406
  hard_task_def_wcet(m,ASTER_WCET);
407
  hard_task_def_mit(m,100000);
408
  hard_task_def_group(m,1);
409
  hard_task_def_ctrl_jet(m);
410
 
411
  //    nrt_task_default_model(m_nrt);
412
  //    nrt_task_def_group(m_nrt,1);
413
  //    nrt_task_def_ctrl_jet(m_nrt);
414
 
415
 
416
  soft_task_default_model(m_aper);
417
  soft_task_def_group(m_aper,1);
418
  soft_task_def_ctrl_jet(m_aper);
419
  soft_task_def_aperiodic(m_aper);
420
 
421
  soft_task_default_model(m_soft);
422
  soft_task_def_period(m_soft,JET_PERIOD);
423
  soft_task_def_met(m_soft,JET_WCET);
424
  soft_task_def_group(m_soft,1);
425
  soft_task_def_ctrl_jet(m_soft);
426
  soft_task_def_aperiodic(m_soft);
427
 
428
 
429
  p1 = task_create("Aster",aster,&m,NULL);
430
  if (p1 == -1) {
431
    perror("test7.c(main): Could not create task <aster> ...");
432
    sys_end();
433
    l1_exit(-1);
434
  }
435
 
436
  hard_task_def_mit(m,500000);
437
  hard_task_def_wcet(m,CLOCK_WCET);
438
  p2 = task_create("Clock",clock,&m,NULL);
439
  if (p2 == -1) {
440
    perror("test7.c(main): Could not create task <Clock> ...");
441
    sys_end();
442
    l1_exit(-1);
443
  }
444
 
445
  //    p3 = task_create("JetControl",jetcontrol,&m_nrt,NULL);
446
  p3 = task_create("JetControl",jetcontrol,&m_soft,NULL);
447
  if (p2 == -1) {
448
    perror("test7.c(main): Could not create task <JetControl> ...");
449
    sys_end();
450
    l1_exit(-1);
451
  }
452
  /*
453
    aperiodic_task_default_model(m_aper,APER_WCET);
454
    aperiodic_task_def_ctrl_jet(m_aper);
455
    aperiodic_task_def_system(m_aper);
456
 
457
    for (i=0; i<APER_MAX; i++) {
458
    aperiodic_task_def_level(m_aper, i/4 + 2);
459
    aperiodic_task_def_arg(m_aper, (i/4 ? '' : ''));
460
    aper_table[i] = task_create("aper",aper_asteroid,&m_aper,NULL);
461
    if (aper_table[i] == -1) {
462
    perror("test7.c(main): Could not create task <aper> ...");
463
    sys_end();
464
    l1_exit(-1);
465
    }
466
    }
467
  */
468
  task_nopreempt();
469
 
470
#ifdef PIMUTEX
471
  PI_mutexattr_default(a);
472
#endif
473
 
474
#ifdef NPPMUTEX
475
  NPP_mutexattr_default(a);
476
#endif
477
 
478
#ifdef NOPMUTEX
479
  NOP_mutexattr_default(a);
480
#endif
481
 
482
  mutex_init(&m1, &a);
483
 
484
  fineprg.tv_sec = 1800;
485
  fineprg.tv_nsec = 0;
486
  //kern_event_post(&fineprg,(void (*)(void *))fine,NULL);
487
  group_activate(1);
488
  return 0;
489
}
490