Subversion Repositories shark

Rev

Rev 1123 | Rev 1388 | 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
 *
1377 giacomo 37
 * CVS :        $Id: aster5.c,v 1.3 2004-04-17 11:36:12 giacomo 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
 
1377 giacomo 58
#include <drivers/shark_linuxc26.h>
59
#include <drivers/shark_input26.h>
60
#include <drivers/shark_keyb26.h>
61
 
1120 pj 62
int num_aster = 0;
63
#define ASTER_LIM       60
64
#define DISPLAY_MAX     15
65
#define ASTER_MAX       70
66
#define STAT_Y           9
67
 
68
#define PER_MAX          5
69
#define APER_MAX         8
70
 
71
#define PER_WCET      6200
72
#define APER_WCET    18400
73
#define JET_WCET     10000
74
#define JET_PERIOD  100000
75
 
76
#define APER_REP     22000
77
 
78
//PID aper_table[APER_MAX];
79
 
80
mutex_t m1;
81
 
82
 
83
#define PIMUTEX
84
//#define NPPMUTEX
85
//#define NOPMUTEX
86
 
87
#define LONGSC
88
 
89
#ifdef LONGSC
90
#define SOFT_MET      3000 /* 3000 12000 */
91
#define CLOCK_WCET     400 /*  200   300*/
92
#define ASTER_WCET     400 /*  200   300*/
93
#else
94
#define SOFT_MET     80000 /* 4500 */
95
#define CLOCK_WCET    2000 /* 200*/
96
#define ASTER_WCET    2000 /* 200*/
97
#endif
98
 
1377 giacomo 99
PID shutdown_task_PID = -1;
100
 
101
int device_drivers_init() {
102
 
103
        KEYB_PARMS kparms = BASE_KEYB;
104
 
105
        LINUXC26_register_module();
106
 
107
        keyb_def_ctrlC(kparms, NULL);
108
 
109
        INPUT26_init();
110
 
111
        KEYB26_init(&kparms);
112
 
113
        return 0;
114
 
115
}
116
 
117
int device_drivers_close() {
118
 
119
        KEYB26_close();
120
 
121
        INPUT26_close();
122
 
123
        return 0;
124
 
125
}
126
 
127
TASK shutdown_task_body(void *arg) {
128
 
129
        device_drivers_close();
130
 
131
        sys_shutdown_message("-- S.Ha.R.K. Closed --\n");
132
 
133
        sys_end();
134
 
135
        return NULL;
136
 
137
}
138
 
139
void set_shutdown_task() {
140
 
141
        NRT_TASK_MODEL nrt;
142
 
143
        nrt_task_default_model(nrt);
144
 
145
        shutdown_task_PID = task_create("Shutdown Task",shutdown_task_body,&nrt,NULL);
146
        if (shutdown_task_PID == NIL) {
147
                sys_shutdown_message("Error: Cannot create shutdown task\n");
148
                sys_end();
149
        }
150
 
151
}
152
 
1120 pj 153
TASK asteroide(void)
154
{
155
  int i;
156
  int y = rand() % 7 + 1;
157
 
158
  int load1,j;
159
 
160
  char s[2];
161
 
162
  s[0] = '*'; s[1] = 0;
163
 
164
  for (;;) {
165
    i = 1;
166
    while (i < ASTER_LIM) {
167
      load1 = 10000; //8000 + rand()%2000;
168
#ifdef LONGSC
169
      mutex_lock(&m1);
170
#endif
171
      for (j=0; j<load1; j++) {
172
        s[0] = '*' + rand() % 100;
173
#ifndef LONGSC
174
        mutex_lock(&m1);
175
#endif
176
        puts_xy(i,y,rand()%15+1,s);
177
#ifndef LONGSC
178
        mutex_unlock(&m1);
179
#endif
180
      }
181
#ifdef LONGSC
182
      mutex_unlock(&m1);
183
#endif
184
 
185
      //        task_activate(aper_table[rand()%APER_MAX]);
186
      task_endcycle();
187
 
188
      mutex_lock(&m1);
189
      puts_xy(i,y,WHITE," ");
190
      mutex_unlock(&m1);
191
      i++;
192
    }
193
  }
194
  //num_aster--;
195
}
196
 
197
TASK aper_asteroid(void *a)
198
{
199
  int i;
200
  int y = rand() % 7 + 1;
201
 
202
  int load1,j;
203
  int c;
204
 
205
  char s[2];
206
 
207
  c = (int)a;
208
  s[0] = '*'; s[1] = 0;
209
 
210
  for (;;) {
211
    i = 1;
212
    while (i < ASTER_LIM) {
213
      load1 = APER_REP; //8000 + rand()%2000;
214
#ifdef LONGSC
215
      mutex_lock(&m1);
216
#endif
217
      for (j=0; j<load1; j++) {
218
        s[0] = '*' + rand() % 100;
219
#ifndef LONGSC
220
        mutex_lock(&m1);
221
#endif
222
        puts_xy(i,y,rand()%15+1,s);
223
#ifndef LONGSC
224
        mutex_unlock(&m1);
225
#endif
226
      }
227
      s[0] = c;
228
#ifndef LONGSC
229
      mutex_lock(&m1);
230
#endif
231
      puts_xy(i,y,rand()%15+1,s);
232
      mutex_unlock(&m1);
233
 
234
      task_endcycle();
235
 
236
      mutex_lock(&m1);
237
      puts_xy(i,y,WHITE," ");
238
      mutex_unlock(&m1);
239
      i++;
240
    }
241
  }
242
}
243
 
244
TASK soft_aster(void)
245
{
246
  int i;
247
  int y = rand() % 7 + 1;
248
 
249
  int load1,j;
250
 
251
  char s[2];
252
 
253
  s[0] = '*'; s[1] = 0;
254
 
255
  /*for (;;)*/ {
256
    i = 1;
257
    while (i < ASTER_LIM) {
258
      load1 = 1000 + rand()%9000;
259
#ifdef LONGSC
260
      mutex_lock(&m1);
261
#endif
262
      for (j=0; j<load1; j++) {
263
        s[0] = '*' + rand() % 100;
264
#ifndef LONGSC
265
        mutex_lock(&m1);
266
#endif
267
        puts_xy(i,y,rand()%15+1,s);
268
#ifndef LONGSC
269
        mutex_unlock(&m1);
270
#endif
271
      }
272
      s[0] = 1;
273
#ifndef LONGSC
274
      mutex_lock(&m1);
275
#endif
276
      puts_xy(i,y,rand()%15+1,s);
277
      mutex_unlock(&m1);
278
 
279
      //        task_activate(aper_table[rand()%APER_MAX]);
280
      task_endcycle();
281
 
282
      mutex_lock(&m1);
283
      puts_xy(i,y,WHITE," ");
284
      mutex_unlock(&m1);
285
      i++;
286
    }
287
  }
288
  num_aster--;
289
  return 0;
290
}
291
 
292
TASK aster()
293
{
294
  PID p;
295
  //    HARD_TASK_MODEL m;
296
  SOFT_TASK_MODEL m_soft;
297
  int r;
298
  int x; // adaptive bandwidth...
299
 
300
  srand(7);
301
 
302
  /*    periodic_task_default_model(m,0,PER_WCET);
303
        periodic_task_def_ctrl_jet(m);
304
        for (x=0; x<PER_MAX; x++) {
305
        r = (rand() % 200);
306
        periodic_task_def_period(m, (64+r)*1000);
307
        p = task_create("per",asteroide,&m,NULL);
308
        if (p!=-1) task_activate(p);
309
        }
310
  */
311
  soft_task_default_model(m_soft);
312
  soft_task_def_met(m_soft,SOFT_MET);
313
  soft_task_def_ctrl_jet(m_soft);
314
 
315
  x = 128; //64;
316
 
317
  while (1) {
318
    if (num_aster < ASTER_MAX) {
319
      r = (rand() % 200);
320
 
321
      soft_task_def_period(m_soft, (x+r)*1000);
322
      p = task_create("aaa",soft_aster,&m_soft,NULL);
323
      if (p == -1)
324
        {
325
          if (x < 500 && errno != ENO_AVAIL_TASK)  x += 1;
326
          mutex_lock(&m1);
327
          printf_xy(62,3,WHITE,"adapt=%3u err=%d",
328
                    iq_query_first(&freedesc),errno);
329
          mutex_unlock(&m1);
330
        }
331
      else {
332
        num_aster++;
333
        mutex_lock(&m1);
334
        printf_xy(62,3,WHITE,"adapt=%3u           ",x);//,errno);
335
        mutex_unlock(&m1);
336
 
337
        task_activate(p);
338
        x /= 2;
339
        if (x<50) x = 50;
340
      }
341
    }
342
    task_endcycle();
343
  }
344
}
345
 
346
TASK clock()
347
{
348
  int s = 0, m = 0;
349
 
350
  while(1) {
351
    mutex_lock(&m1);
352
    printf_xy(62,1,WHITE,"%2d:%2d ast=%d",m,s, num_aster);
353
    printf_xy(62,2,WHITE,"Uedf=%12u",EDF_usedbandwidth(0));
354
    printf_xy(62,4,WHITE,"Ucbs=%12u",CBS_usedbandwidth(2));
355
 
356
    mutex_unlock(&m1);
357
    task_endcycle();
358
 
359
    if (++s > 59) {
360
      s = 0;
361
      m++;
362
    }
363
    mutex_lock(&m1);
364
    printf_xy(62,1,WHITE,"%2d:%2d ast=%d",m,s, num_aster);
365
    printf_xy(62,2,WHITE,"Uedf=%12u",EDF_usedbandwidth(0));
366
    printf_xy(62,4,WHITE,"Ucbs=%12u",CBS_usedbandwidth(2));
367
    mutex_unlock(&m1);
368
    task_endcycle();
369
  }
370
}
371
 
372
 
373
 
374
/* we consider the first ASTER_MAX + 2 tasks from the PID 2
375
   and plot on the screen the elapsed times... */
376
TASK jetcontrol()
377
{
378
  int i;  /* a counter */
379
  TIME sum, max, curr, last[5];
380
  int nact;
381
  int j; /* the elements set by jet_gettable */
382
  PID p;
383
 
384
 
385
  mutex_lock(&m1);
386
  printf_xy(0,STAT_Y,WHITE,"PID  Mean T. Max T.  N.A.  Curr.    Last1  Last2  Last3  Last4  Last5");
387
  mutex_unlock(&m1);
388
 
389
  for (;;) {
390
    for (i=0,p=0; i<DISPLAY_MAX+5 && p<MAX_PROC; p++) {
391
      if (jet_getstat(p, &sum, &max, &nact, &curr) == -1 /*||
392
                                                           (proc_table[p].pclass & 0xFF00) == APERIODIC_PCLASS ||
393
                                                           (proc_table[p].pclass & 0xFF00) == PERIODIC_PCLASS*/ ) continue;
394
 
395
      for (j=0; j<5; j++) last[j] = 0;
396
      jet_gettable(p, &last[0], 5);
397
      mutex_lock(&m1);
398
      if (proc_table[p].task_level == 2)
399
        printf_xy(0,STAT_Y+i+1,WHITE,"%-3d  %-6d  %-6d  %-4d p%-7d  %-5d  %-5d  %-5d  %-5d  %-5d",
400
                  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]);
401
      //                   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]);
402
      else
403
        printf_xy(0,STAT_Y+i+1,WHITE,"%-3d  %-6d  %-6d  %-4d  %-7d  %-5d  %-5d  %-5d  %-5d  %-5d",
404
                  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]);
405
      //                   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]);
406
      mutex_unlock(&m1);
407
      i++;
408
    }
409
  }
410
}
411
 
412
void fine(KEY_EVT *e)
413
{
1377 giacomo 414
  task_activate(shutdown_task_PID);
1120 pj 415
}
416
 
417
int main(int argc, char **argv)
418
{
419
  PID p1,p2,p3;//,p4,p5,p6;
420
  HARD_TASK_MODEL m;
421
  //    NRT_TASK_MODEL m_nrt;
422
  SOFT_TASK_MODEL m_aper;
423
  SOFT_TASK_MODEL m_soft;
424
  //    int i;
425
  struct timespec fineprg;
426
 
1377 giacomo 427
  set_shutdown_task();
428
 
429
  device_drivers_init();
430
 
1120 pj 431
#ifdef PIMUTEX
432
  PI_mutexattr_t a;
433
#endif
434
 
435
#ifdef NPPMUTEX
436
  NPP_mutexattr_t a;
437
#endif
438
 
439
#ifdef NOPMUTEX
440
  NOP_mutexattr_t a;
441
#endif
442
 
443
  KEY_EVT emerg;
1377 giacomo 444
 
1120 pj 445
  emerg.ascii = 'x';
446
  emerg.scan = KEY_X;
447
  emerg.flag = ALTL_BIT;
1377 giacomo 448
  emerg.status = KEY_PRESSED;
449
  keyb_hook(emerg,fine,FALSE);
1120 pj 450
 
451
  clear();
452
 
453
  hard_task_default_model(m);
454
  hard_task_def_wcet(m,ASTER_WCET);
455
  hard_task_def_mit(m,100000);
456
  hard_task_def_group(m,1);
457
  hard_task_def_ctrl_jet(m);
458
 
459
  //    nrt_task_default_model(m_nrt);
460
  //    nrt_task_def_group(m_nrt,1);
461
  //    nrt_task_def_ctrl_jet(m_nrt);
462
 
463
 
464
  soft_task_default_model(m_aper);
465
  soft_task_def_group(m_aper,1);
466
  soft_task_def_ctrl_jet(m_aper);
467
  soft_task_def_aperiodic(m_aper);
468
 
469
  soft_task_default_model(m_soft);
470
  soft_task_def_period(m_soft,JET_PERIOD);
471
  soft_task_def_met(m_soft,JET_WCET);
472
  soft_task_def_group(m_soft,1);
473
  soft_task_def_ctrl_jet(m_soft);
474
  soft_task_def_aperiodic(m_soft);
475
 
476
 
477
  p1 = task_create("Aster",aster,&m,NULL);
478
  if (p1 == -1) {
1377 giacomo 479
    sys_shutdown_message("test7.c(main): Could not create task <aster> ...");
480
    task_activate(shutdown_task_PID);
481
    return 0;
1120 pj 482
  }
483
 
484
  hard_task_def_mit(m,500000);
485
  hard_task_def_wcet(m,CLOCK_WCET);
486
  p2 = task_create("Clock",clock,&m,NULL);
487
  if (p2 == -1) {
1377 giacomo 488
    sys_shutdown_message("test7.c(main): Could not create task <Clock> ...");
489
    task_activate(shutdown_task_PID);
490
    return 0;
1120 pj 491
  }
492
 
493
  //    p3 = task_create("JetControl",jetcontrol,&m_nrt,NULL);
494
  p3 = task_create("JetControl",jetcontrol,&m_soft,NULL);
495
  if (p2 == -1) {
1377 giacomo 496
    sys_shutdown_message("test7.c(main): Could not create task <JetControl> ...");
497
    task_activate(shutdown_task_PID);
498
    return 0;
1120 pj 499
  }
500
  /*
501
    aperiodic_task_default_model(m_aper,APER_WCET);
502
    aperiodic_task_def_ctrl_jet(m_aper);
503
    aperiodic_task_def_system(m_aper);
504
 
505
    for (i=0; i<APER_MAX; i++) {
506
    aperiodic_task_def_level(m_aper, i/4 + 2);
507
    aperiodic_task_def_arg(m_aper, (i/4 ? '' : ''));
508
    aper_table[i] = task_create("aper",aper_asteroid,&m_aper,NULL);
509
    if (aper_table[i] == -1) {
510
    perror("test7.c(main): Could not create task <aper> ...");
511
    sys_end();
512
    l1_exit(-1);
513
    }
514
    }
515
  */
516
  task_nopreempt();
517
 
518
#ifdef PIMUTEX
519
  PI_mutexattr_default(a);
520
#endif
521
 
522
#ifdef NPPMUTEX
523
  NPP_mutexattr_default(a);
524
#endif
525
 
526
#ifdef NOPMUTEX
527
  NOP_mutexattr_default(a);
528
#endif
529
 
530
  mutex_init(&m1, &a);
531
 
532
  fineprg.tv_sec = 1800;
533
  fineprg.tv_nsec = 0;
534
  group_activate(1);
535
  return 0;
536
}
537