Subversion Repositories shark

Rev

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