Subversion Repositories shark

Rev

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