Subversion Repositories shark

Rev

Rev 1123 | Go to most recent revision | Details | 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
 *
37
 * CVS :        $Id: aster5.c,v 1.1 2002-11-11 08:22:45 pj Exp $
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
 
404
  clear();
405
 
406
  hard_task_default_model(m);
407
  hard_task_def_wcet(m,ASTER_WCET);
408
  hard_task_def_mit(m,100000);
409
  hard_task_def_group(m,1);
410
  hard_task_def_ctrl_jet(m);
411
 
412
  //    nrt_task_default_model(m_nrt);
413
  //    nrt_task_def_group(m_nrt,1);
414
  //    nrt_task_def_ctrl_jet(m_nrt);
415
 
416
 
417
  soft_task_default_model(m_aper);
418
  soft_task_def_group(m_aper,1);
419
  soft_task_def_ctrl_jet(m_aper);
420
  soft_task_def_aperiodic(m_aper);
421
 
422
  soft_task_default_model(m_soft);
423
  soft_task_def_period(m_soft,JET_PERIOD);
424
  soft_task_def_met(m_soft,JET_WCET);
425
  soft_task_def_group(m_soft,1);
426
  soft_task_def_ctrl_jet(m_soft);
427
  soft_task_def_aperiodic(m_soft);
428
 
429
 
430
  p1 = task_create("Aster",aster,&m,NULL);
431
  if (p1 == -1) {
432
    perror("test7.c(main): Could not create task <aster> ...");
433
    sys_end();
434
    l1_exit(-1);
435
  }
436
 
437
  hard_task_def_mit(m,500000);
438
  hard_task_def_wcet(m,CLOCK_WCET);
439
  p2 = task_create("Clock",clock,&m,NULL);
440
  if (p2 == -1) {
441
    perror("test7.c(main): Could not create task <Clock> ...");
442
    sys_end();
443
    l1_exit(-1);
444
  }
445
 
446
  //    p3 = task_create("JetControl",jetcontrol,&m_nrt,NULL);
447
  p3 = task_create("JetControl",jetcontrol,&m_soft,NULL);
448
  if (p2 == -1) {
449
    perror("test7.c(main): Could not create task <JetControl> ...");
450
    sys_end();
451
    l1_exit(-1);
452
  }
453
  /*
454
    aperiodic_task_default_model(m_aper,APER_WCET);
455
    aperiodic_task_def_ctrl_jet(m_aper);
456
    aperiodic_task_def_system(m_aper);
457
 
458
    for (i=0; i<APER_MAX; i++) {
459
    aperiodic_task_def_level(m_aper, i/4 + 2);
460
    aperiodic_task_def_arg(m_aper, (i/4 ? 'Û' : '±'));
461
    aper_table[i] = task_create("aper",aper_asteroid,&m_aper,NULL);
462
    if (aper_table[i] == -1) {
463
    perror("test7.c(main): Could not create task <aper> ...");
464
    sys_end();
465
    l1_exit(-1);
466
    }
467
    }
468
  */
469
  task_nopreempt();
470
 
471
#ifdef PIMUTEX
472
  PI_mutexattr_default(a);
473
#endif
474
 
475
#ifdef NPPMUTEX
476
  NPP_mutexattr_default(a);
477
#endif
478
 
479
#ifdef NOPMUTEX
480
  NOP_mutexattr_default(a);
481
#endif
482
 
483
  mutex_init(&m1, &a);
484
 
485
  fineprg.tv_sec = 1800;
486
  fineprg.tv_nsec = 0;
487
  //kern_event_post(&fineprg,(void (*)(void *))fine,NULL);
488
  group_activate(1);
489
  return 0;
490
}
491