Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1670 pj 1
#include "hlpdemo.h"
2
#include <drivers/shark_keyb26.h>
3
#include "snapshot.h"
4
 
5
//#define ESC 27
6
 
7
/*
8
#define DEBUG
9
*/
10
 
11
/* tipologia di task e mutex */
12
HARD_TASK_MODEL m;
13
 
14
/* scala temporale del sistema in us */
15
#define TIMESCALE (100 * 1000)
16
 
17
/* buffer che contiene la configurazione letto in nrt mode */
18
extern char myfilebuf[1000];
19
 
20
extern int read_conf(char *, unsigned int);
21
extern void print_taskconf(taskconf_list_t *);
22
 
23
void trace_info_hlp(void);
24
 
25
/* tempo dell'avvio */
26
TIME start;
27
 
28
/* struttura dati contenente la configurazione */
29
taskconf_list_t *taskconf = NULL;
30
 
31
/* lista dei mutex del sistema */
32
mutex_list_t *mutexes = NULL;
33
 
34
#define YMENU    10             /* menu level                   */
35
#define XMIN     50
36
#define XMAX     600
37
#define YMIN     100
38
#define YMAX     450
39
#define YFLOOR(index) YMIN + (50 * (index + 1))
40
 
41
#define COLWHITE rgb16(255,255,255)
42
#define COLLOAD  rgb16(180,180,180)
43
 
44
/*--------------------------------------------------------------*/
45
 
46
/* posizione lungo l'asse x dopo t sec. */
47
int xpost(unsigned int t)
48
{
49
  int ret;
50
 
51
  ret = XMIN + 10 + (((sys_gettime(NULL) - start) / TIMESCALE) + t);
52
  if (ret > XMAX)
53
    ret = XMAX;
54
  if (ret < XMIN)
55
    ret = XMIN;
56
 
57
  return ret;
58
}
59
 
60
/* posizione lungo l'asse x al tempo t corrente */
61
int xpos(void)
62
{
63
  return xpost(0);
64
}
65
 
66
typedef struct color_stack {
67
  int color;
68
  struct color_stack *next;
69
} color_stack_t;
70
 
71
void color_stack_push(int color, color_stack_t **head)
72
{
73
  color_stack_t *new;
74
 
75
  new = (color_stack_t *)kern_alloc(sizeof(color_stack_t));
76
  new->color = color;
77
  new->next = *head;
78
 
79
  *head = new;
80
}
81
 
82
int color_stack_pop(color_stack_t **head)
83
{
84
  int ret = COLLOAD;
85
  color_stack_t *tmp = *head;
86
 
87
  if (*head != NULL)
88
    {
89
      ret = (*head)->color;
90
      *head = (*head)->next;
91
      kern_free(tmp, (sizeof(color_stack_t)));
92
    }
93
 
94
  return ret;
95
}
96
 
97
 
98
void load(int time, int i, int color, char *name)
99
{
100
  int j;
101
  TIME last;
102
  char buf[50];
103
 
104
  for (j = 0; j < time; j++)
105
    {
106
      last = sys_gettime(NULL);
107
 
108
      grx_line(xpos(), YFLOOR(i), xpos(), YFLOOR(i) - 10, color);
109
      sprintf(buf,
110
              "Time: %.4d, task active: %.5s(PID=%.2d, Shadow=%.2d)",
111
              (int)((sys_gettime(NULL) - start) / TIMESCALE) + 2,
112
              name,
113
              exec,
114
              exec_shadow);
115
      grx_text(buf, 100,100,COLWHITE,0);
116
 
117
      while (sys_gettime(NULL) - last + (TIMESCALE/100) < TIMESCALE);
118
    }
119
}
120
 
121
TASK    Ji(void *arg)
122
{
123
  taskconf_list_t *cur = (taskconf_list_t *)arg;
124
  action_list_t *act;
125
  color_stack_t *colorstack;
126
 
127
  color_stack_push(COLLOAD, &colorstack);
128
 
129
  while (1)
130
    {
131
 
132
      act = cur->actionlist;
133
 
134
      while (act)
135
        {
136
          switch (act->type)
137
            {
138
            case LOAD:
139
              load(act->time, cur->i, colorstack->color, cur->name);
140
              if (xpos() == XMAX)
141
                {
142
                  task_kill(cur->pid);
143
                  return 0;
144
                }
145
              break;
146
 
147
            case LOCK: /* task lock */
148
              color_stack_push(act->mutex->color, &colorstack);
149
              grx_text(act->mutex->name, xpos() - 2, YFLOOR(cur->i) + 10, COLWHITE, 0);
150
              mutex_lock(&act->mutex->m);
151
              trace_info_hlp();
152
              break;
153
 
154
            case UNLOCK: /* task unlock */
155
              color_stack_pop(&colorstack);
156
              mutex_unlock(&act->mutex->m);
157
              trace_info_hlp();
158
              break;
159
            }
160
          act = act->next;
161
        }
162
      task_endcycle();
163
    }
164
 
165
  return 0;
166
}
167
 
168
void draw_scenario(void)
169
{
170
  /* The scenario */
171
  grx_rect(XMIN-1, YMIN-1, XMAX+1, YMAX+1, COLWHITE);
172
  grx_text("Simulation of HLP tasks", XMIN, YMENU+10, COLWHITE, 0);
173
  grx_text("SPACE Start demo"        , XMIN, YMENU+20, COLWHITE, 0);
174
  grx_text("ESC   exit to DOS"         , XMIN, YMENU+30, COLWHITE, 0);
175
}
176
 
177
void trace_info_hlp()
178
{
179
  HLP_mutex_resource_des *m;
180
  RLEVEL l;
181
  HLP_mutex_t *mutscan;
182
  HLP_tasklist_t *taskscan;
183
  int i = 0;
184
  char buf[60];
185
 
186
  if (mutexes)
187
    {
188
      l = mutexes->m.mutexlevel;
189
      m = (HLP_mutex_resource_des *)(resource_table[l]);
190
 
191
      for (mutscan = m->mutexlist; mutscan; mutscan = mutscan->next)
192
        {
193
          if (mutscan->owner != NIL)
194
            sprintf(buf, "mutex: %p owner=%2d, used by", mutscan, mutscan->owner);
195
          else
196
            sprintf(buf, "mutex: %p no owner, used by", mutscan);
197
 
198
          grx_text(buf, 50, 320, COLWHITE, 0);
199
 
200
          for (i = 1, taskscan = mutscan->tasklist; taskscan; taskscan = taskscan->next, i++)
201
            {
202
              sprintf(buf, "task: %p, pid=%d, preempt=%ld",
203
                      taskscan,
204
                      taskscan->pid,
205
                      taskscan->preempt);
206
              grx_text(buf, 100, 320+i*8, COLWHITE, 0);
207
            }
208
        }
209
 
210
      grx_text("All tasks:", 100, 320 + i++ * 8, COLWHITE, 0);
211
 
212
      if (taskconf)
213
        for (taskscan = m->tasklist; taskscan; taskscan = taskscan->next, i++)
214
          {
215
            sprintf(buf,
216
                    "task: %p, pid=%d, preempt=%ld",
217
                    taskscan,
218
                    taskscan->pid,
219
                    taskscan->preempt);
220
 
221
            grx_text(buf, 100, 320+i*8, COLWHITE, 0);
222
          }
223
    }
224
  else
225
    {
226
      grx_text("No mutexes", 200, 200, COLWHITE, 0);
227
    }
228
 
229
}
230
 
231
/* get preemption level inverse proportional to period and starting by 1 */
232
int get_preemption_level(taskconf_list_t *task)
233
{
234
  taskconf_list_t *scan;
235
  int ret = 1;
236
 
237
  for (scan = taskconf; scan; scan = scan->next)
238
    if (scan != task && scan->period < task->period)
239
      ret++;
240
 
241
  return ret;
242
}
243
 
244
/****************************** MAIN ******************************/
245
 
246
int main(int argc, char **argv)
247
{
248
    char c;             /* character from keyboard      */
249
    int i = 0, j = 0, started = 0, numtask = 0, nummutex = 0;
250
    taskconf_list_t *curtask = NULL;
251
    mutex_list_t *mutexlist = NULL, *curmut = NULL;
252
    PID pid = NIL;
253
    HLP_mutexattr_t a;
254
    HLP_RES_MODEL r;
255
 
256
    if (read_conf(myfilebuf, 1000) == -1)
257
      {
258
        sys_shutdown_message("Error reading configuration file");
259
        sys_end();
260
      }
261
 
262
    for (curtask = taskconf; curtask != NULL; curtask = curtask->next)
263
      numtask++;
264
 
265
    draw_scenario();
266
    curtask = taskconf;
267
 
268
    for (curmut = mutexes; curmut; curmut = curmut->next)
269
      {
270
        HLP_mutexattr_default(a);
271
        mutex_init(&(curmut->m), &a);
272
      }
273
 
274
    snapshot_freeslot(0);
275
 
276
    c = keyb_getch(BLOCK);
277
    i = started = 0;
278
    do {
279
 
280
      if ((c == ' ') && !started) {
281
        started = 1;
282
        start = sys_gettime(NULL);
283
 
284
        while (curtask) {
285
 
286
          /* traccio gli assi temporali per ogni task */
287
          grx_line(XMIN + 10, YFLOOR(i), XMAX - 10, YFLOOR(i), COLWHITE);
288
          grx_text(curtask->name, XMIN - 20, YFLOOR(i) - 10, COLWHITE, 0);                     
289
 
290
          /* disegna frecce di attivazione */
291
          for (j = 0; xpost(j) < XMAX; j += curtask->period)
292
            {
293
              grx_line(xpost(j), YFLOOR(i), xpost(j), YFLOOR(i) - 20, COLWHITE);
294
              grx_line(xpost(j), YFLOOR(i) - 20, xpost(j) - 5, YFLOOR(i) - 15, COLWHITE);
295
              grx_line(xpost(j), YFLOOR(i) - 20, xpost(j) + 5, YFLOOR(i) - 15, COLWHITE);
296
            }
297
 
298
          hard_task_default_model(m);
299
          hard_task_def_level(m,0);
300
          hard_task_def_ctrl_jet(m);
301
          hard_task_def_periodic(m);
302
          hard_task_def_wcet(m, (curtask->wcet + 2) * TIMESCALE);
303
          hard_task_def_mit(m, curtask->period * TIMESCALE);
304
          hard_task_def_arg(m,(void *)curtask);
305
          hard_task_def_group(m, 1);
306
 
307
          /* registro i mutex per il pid */
308
          nummutex = 0;
309
          mutexlist = curtask->mutexlist;
310
          while(mutexlist) {
311
            mutexlist = mutexlist->next;
312
            nummutex++;
313
          }
314
 
315
          //      HLP_res_default_model(r, get_preemption_level(curtask));
316
          HLP_res_default_model(r, 1000 - curtask->period);
317
 
318
          switch (nummutex) {
319
          case 0:
320
            pid = task_create(curtask->name, Ji, &m, &r);
321
            break;
322
 
323
          case 1:
324
            pid = task_createn(curtask->name, Ji, (TASK_MODEL *)&m, &r,
325
                               HLP_usemutex(&(curtask->mutexlist->m)), NULL);
326
            break;
327
 
328
          case 2:
329
            pid = task_createn(curtask->name, Ji, (TASK_MODEL *)&m, &r,
330
                               HLP_usemutex(&(curtask->mutexlist->m)),
331
                               HLP_usemutex(&(curtask->mutexlist->next->m)), NULL);
332
            break;
333
          case 3:
334
            pid = task_createn(curtask->name, Ji, (TASK_MODEL *)&m, &r,
335
                               HLP_usemutex(&(curtask->mutexlist->m)),
336
                               HLP_usemutex(&(curtask->mutexlist->next->m)),
337
                               HLP_usemutex(&(curtask->mutexlist->next->next->m)), NULL);
338
            break;
339
          case 4:
340
            pid = task_createn(curtask->name, Ji, (TASK_MODEL *)&m, &r,
341
                               HLP_usemutex(&(curtask->mutexlist->m)),
342
                               HLP_usemutex(&(curtask->mutexlist->next->m)),
343
                               HLP_usemutex(&(curtask->mutexlist->next->next->m)),
344
                               HLP_usemutex(&(curtask->mutexlist->next->next->next->m)), NULL);
345
            break;
346
 
347
          default:
348
            sys_shutdown_message("Too many mutexes! I am stupid!");
349
            sys_end();
350
          }
351
 
352
          if (pid == NIL)
353
            {
354
              sys_shutdown_message("Could not create task %s", curtask->name);
355
              sys_end();
356
              print_taskconf(curtask);
357
              return 0;
358
            }
359
          curtask->pid = pid;
360
          curtask->i = i++;
361
 
362
          curtask = curtask->next;
363
        }
364
 
365
        trace_info_hlp();
366
 
367
        start = sys_gettime(NULL);
368
        group_activate(1);
369
      }
370
 
371
 
372
      c = keyb_getch(BLOCK);
373
 
374
      if (xpos() > XMAX)
375
        {
376
          group_kill(1);
377
        }
378
 
379
    } while (c != ESC);
380
 
381
    snapshot_getslot(0, 640, 480, 2);
382
    snapshot_grab(0);
383
 
384
    sys_end();
385
 
386
    snapshot_save_ppm(0, "snapshot.ppm");
387
 
388
    return 0;
389
 
390
}
391
 
392
/*--------------------------------------------------------------*/