Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1672 tullio 1
/* Event Generator
2
 *
3
 * Giacomo Guidi
4
 */
5
 
6
#include <stdio.h>
7
#include <stdlib.h>
8
 
9
#include "lparser.h"
10
#include "lread.h"
11
#include "time.h"
12
#include "common/time.h"
13
 
14
#define LOADFILE_DIR "../loadfile/"
15
 
16
#define EVENT_DEFINE "event.c"
17
#define ACT_LIST "event.c"
18
 
19
int write_struct(void)
20
{
21
 
22
  FILE *file_event_header;
23
 
24
  file_event_header = fopen(EVENT_DEFINE,"w");
25
  if (file_event_header == NULL) return 1;
26
 
27
  fprintf(file_event_header, "\n#include \"func.h\"\n");
28
 
29
  fclose(file_event_header);
30
 
31
  return 0;
32
 
33
}
34
 
35
int write_basic_par_start(void)
36
{
37
 
38
  FILE *file_event_header;
39
 
40
  file_event_header = fopen(EVENT_DEFINE,"a+");
41
  if (file_event_header == NULL) return 1;
42
 
43
  fprintf(file_event_header, "struct loader_task loader_task_list[] = {\n");
44
 
45
  fclose(file_event_header);
46
 
47
  return 0;
48
 
49
}
50
 
51
int write_contract_start(void)
52
{
53
 
54
  FILE *file_event_header;
55
 
56
  file_event_header = fopen(EVENT_DEFINE,"a+");
57
  if (file_event_header == NULL) return 1;
58
 
59
  fprintf(file_event_header, "struct loader_contract loader_contract_list[] = {\n");
60
 
61
  fclose(file_event_header);
62
 
63
  return 0;
64
 
65
}
66
 
67
int write_basic_par(struct loader_task *c)
68
{
69
 
70
  FILE *file_event_header;
71
  int muxpres;
72
  char muxtemp[30];
73
 
74
  file_event_header = fopen(EVENT_DEFINE,"a+");
75
  if (file_event_header == NULL) return 1;
76
 
77
  if (c->crit_type == PAR_CRIT) {
78
    muxpres = 1;
79
    sprintf(muxtemp,"block_%s",c->name);
80
  } else {
81
    muxpres = 0;
82
    sprintf(muxtemp,"NULL");
83
  }
84
 
85
  fprintf(file_event_header, "  {\"%s\",%d,%d,%d,%d,%d,{%d,%d},{%d,%d},%d,0,%d,%d,act_%s,exec_%s,%s},\n",
86
                             c->name,(int)c->task_type,(int)c->server,(int)c->local_scheduler,(int)c->number,(int)c->group,
87
                             (int)c->deadline.tv_sec, (int)c->deadline.tv_nsec,
88
                             (int)c->wcet.tv_sec, (int)c->wcet.tv_nsec,
89
                             (int)c->act_number, (int)c->resource, muxpres, c->name, c->name, muxtemp);
90
 
91
  fclose(file_event_header);
92
 
93
  return 0;  
94
 
95
}
96
 
97
int write_contract(struct loader_contract *c)
98
{
99
 
100
  FILE *file_event_header;
101
 
102
  file_event_header = fopen(EVENT_DEFINE,"a+");
103
  if (file_event_header == NULL) return 1;
104
 
105
  fprintf(file_event_header, "  {%d,{%d,%d},{%d,%d},{%d,%d},{%d,%d},%d,{%d,%d},%d,-1},\n",
106
                             (int)c->number,(int)c->cmin.tv_sec,(int)c->cmin.tv_nsec,
107
                             (int)c->tmax.tv_sec,(int)c->tmax.tv_nsec,
108
                             (int)c->cmax.tv_sec,(int)c->cmax.tv_nsec,
109
                             (int)c->tmin.tv_sec,(int)c->tmin.tv_nsec,
110
                             (int)c->workload,
111
                             (int)c->deadline.tv_sec,(int)c->deadline.tv_nsec,
112
                             (int)c->local_scheduler);
113
 
114
  fclose(file_event_header);
115
 
116
  return 0;
117
 
118
}
119
 
120
int close_loader_task(int total_task_number)
121
{
122
 
123
  FILE *file_event_header;
124
 
125
  file_event_header = fopen(EVENT_DEFINE,"a+");
126
  if (file_event_header == NULL) return 1;
127
 
128
  fprintf(file_event_header,"};\n\n");
129
 
130
  fprintf(file_event_header,"int total_loader_task = %d;\n\n",total_task_number);
131
 
132
  fclose(file_event_header);
133
 
134
  return 0;
135
 
136
}
137
 
138
int close_loader_contract(int total_contract_number)
139
{
140
 
141
  FILE *file_event_header;
142
 
143
  file_event_header = fopen(EVENT_DEFINE,"a+");
144
  if (file_event_header == NULL) return 1;
145
 
146
  fprintf(file_event_header,"};\n\n");
147
 
148
  fprintf(file_event_header,"int total_loader_contract = %d;\n\n",total_contract_number);
149
 
150
  fclose(file_event_header);
151
 
152
  return 0;
153
 
154
}
155
 
156
int write_simulation_time(struct timespec *total)
157
{
158
 
159
  FILE *file_event_header;
160
 
161
  file_event_header = fopen(EVENT_DEFINE,"a+");
162
  if (file_event_header == NULL) return 1;
163
 
164
  fprintf(file_event_header,"struct timespec total_time = {%d,%d};\n\n",(int)total->tv_sec,(int)total->tv_nsec);
165
 
166
  fclose(file_event_header);
167
 
168
  return 0;
169
 
170
 
171
}
172
 
173
int write_single_act(struct timespec *t, struct loader_task *c)
174
{
175
 
176
  FILE *file_act_header;
177
 
178
  file_act_header = fopen(ACT_LIST,"a+");
179
  if (file_act_header == NULL) return 1;
180
 
181
  if (TIMESPEC_A_GT_B(t,&c->act_par_1)) {
182
    fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d}};\n\n",c->name,
183
          (int)c->act_par_1.tv_sec,(int)c->act_par_1.tv_nsec);
184
    c->act_number = 1;
185
  } else {
186
    fprintf(file_act_header,"struct timespec act_%s[] = {{0,0}};\n\n",c->name);
187
    c->act_number = 0;
188
  }
189
 
190
  fclose(file_act_header);
191
 
192
  return 0;
193
 
194
}
195
 
196
int write_periodic_act(struct timespec *t, struct loader_task *c)
197
{
198
 
199
  FILE *file_act_header;
200
  struct timespec tot_time;
201
  int period;
202
 
203
  file_act_header = fopen(ACT_LIST,"a+");
204
  if (file_act_header == NULL) return 1;
205
 
206
  fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d},\n",c->name,
207
          (int)c->act_par_1.tv_sec,(int)c->act_par_1.tv_nsec);
208
 
209
  c->act_number = 1;
210
  TIMESPEC_ASSIGN(&tot_time,&c->act_par_1);
211
  period = TIMESPEC2USEC(&c->act_par_2);
212
  while (TIMESPEC_A_GT_B(t, &tot_time)) {
213
    c->act_number++;
214
    ADDUSEC2TIMESPEC(period,&tot_time);
215
    fprintf(file_act_header,"  {%d,%d},\n",
216
            (int)c->act_par_2.tv_sec,(int)c->act_par_2.tv_nsec);
217
  }
218
 
219
  fprintf(file_act_header,"  };\n\n");
220
 
221
  fclose(file_act_header);
222
 
223
  return 0;
224
 
225
}
226
 
227
int write_mean_act(struct timespec *t,struct loader_task *c)
228
{
229
 
230
  FILE *file_act_header;
231
  struct timespec tot_time;
232
  int next_act;          
233
 
234
  file_act_header = fopen(ACT_LIST,"a+");
235
  if (file_act_header == NULL) return 1;
236
 
237
  fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d},\n",c->name,
238
          (int)c->act_par_1.tv_sec,(int)c->act_par_1.tv_nsec);
239
 
240
  c->act_number = 1;                                                                                                        
241
  TIMESPEC_ASSIGN(&tot_time,&c->act_par_1);
242
  while (TIMESPEC_A_GT_B(t, &tot_time)) {
243
    c->act_number++;
244
    next_act = TIMESPEC2USEC(&c->act_par_2) + random() % TIMESPEC2USEC(&c->act_par_3) - TIMESPEC2USEC(&c->act_par_3) / 2;
245
    ADDUSEC2TIMESPEC(next_act,&tot_time);
246
    fprintf(file_act_header,"  {%d,%d},\n",
247
            next_act / 1000000, next_act % 1000000 * 1000);
248
  }
249
 
250
  fprintf(file_act_header,"  };\n\n");
251
 
252
  fclose(file_act_header);
253
 
254
  return 0;
255
 
256
}
257
 
258
int write_block_const(struct loader_task *c)
259
{
260
 
261
  FILE *file_block_header;
262
  int i;
263
 
264
  file_block_header = fopen(ACT_LIST,"a+");
265
  if (file_block_header == NULL) return 1;
266
 
267
  fprintf(file_block_header,"struct timespec block_%s[] =  {{%d,%d},\n",c->name,
268
          (int)c->crit_par.tv_sec,(int)c->crit_par.tv_nsec);
269
 
270
  for (i=0; i< c->act_number-1; i++)
271
    fprintf(file_block_header,"  {%d,%d},\n",
272
          (int)c->crit_par.tv_sec,(int)c->crit_par.tv_nsec);
273
 
274
  fprintf(file_block_header,"  };\n\n");
275
 
276
  fclose(file_block_header);
277
 
278
  return 0;
279
 
280
}
281
 
282
int write_exec_const(struct loader_task *c)
283
{
284
 
285
  FILE *file_exec_header;
286
  int i;
287
 
288
  file_exec_header = fopen(ACT_LIST,"a+");
289
  if (file_exec_header == NULL) return 1;
290
 
291
  fprintf(file_exec_header,"struct timespec exec_%s[] =  {{%d,%d},\n",c->name,
292
          (int)c->exec_par_1.tv_sec,(int)c->exec_par_1.tv_nsec);
293
 
294
  for (i=0; i< c->act_number-1; i++)  
295
    fprintf(file_exec_header,"  {%d,%d},\n",
296
          (int)c->exec_par_1.tv_sec,(int)c->exec_par_1.tv_nsec);
297
 
298
  fprintf(file_exec_header,"  };\n\n");
299
 
300
  fclose(file_exec_header);
301
 
302
  return 0;
303
 
304
}
305
 
306
int write_exec_mean(struct loader_task *c)
307
{
308
 
309
  FILE *file_exec_header;
310
  int exec_time_usec;
311
  int i;
312
 
313
  file_exec_header = fopen(ACT_LIST,"a+");
314
  if (file_exec_header == NULL) return 1;
315
 
316
  exec_time_usec = TIMESPEC2USEC(&c->exec_par_1)
317
                        + random() % TIMESPEC2USEC(&c->exec_par_2) - TIMESPEC2USEC(&c->exec_par_2) / 2;
318
  fprintf(file_exec_header,"struct timespec exec_%s[] = {{%d,%d},\n",c->name,
319
          exec_time_usec / 1000000, exec_time_usec % 1000000 * 1000);
320
 
321
  for (i=0; i< c->act_number-1; i++) {
322
    exec_time_usec = TIMESPEC2USEC(&c->exec_par_1)
323
                        + random() % TIMESPEC2USEC(&c->exec_par_2) - TIMESPEC2USEC(&c->exec_par_2) / 2;
324
    fprintf(file_exec_header,"  {%d,%d},\n",
325
          exec_time_usec / 1000000, exec_time_usec % 1000000 * 1000);
326
  }
327
 
328
  fprintf(file_exec_header,"  };\n\n");
329
 
330
  fclose(file_exec_header);
331
 
332
  return 0;
333
 
334
}
335
 
336
void *start;
337
void *end;
338
 
339
int main(int argc, char **argv) {
340
 
341
  char loadfile[100];
342
  struct timespec total_time;
343
  struct loader_task *start_loader_task = NULL, *current_t;
344
  struct loader_contract *start_loader_contract = NULL, *current_c;
345
  int err,ldnum;
346
  int total_task_number;
347
  int total_contract_number;
348
 
349
  printf("\nEvent Generator\n");
350
 
351
  if (argc < 2) {
352
    printf("Error: event_gen loadfile.fsf\n");
353
    exit(1);
354
  }
355
 
356
  printf("Read loader file %s\n",argv[1]);
357
 
358
  sprintf(loadfile,"%s%s",LOADFILE_DIR,argv[1]);
359
  err = dos_preload(loadfile,100000,&start,&end);
360
 
361
  if (err != 0) {
362
    printf("Error: File not found\n");
363
    exit(1);
364
  }
365
 
366
  printf("Parsing file\n");
367
 
368
  line_reader(start, end, &total_time, &start_loader_task, &start_loader_contract);
369
 
370
  srandom(time(NULL));
371
 
372
  write_struct();
373
 
374
  current_t = start_loader_task;
375
  ldnum = 1;
376
 
377
  while(current_t != NULL) {
378
 
379
    sprintf(current_t->name,"ltask%d",ldnum);    
380
    current_t->group = ldnum;
381
    ldnum++;
382
 
383
    switch (current_t->act_type) {
384
     case PAR_ACT_SINGLE:
385
       err = write_single_act(&total_time,current_t);
386
        if (err != 0) {
387
          printf("Error writing activation header\n");
388
          exit(1);
389
        }
390
        break;
391
      case PAR_ACT_PERIODIC:
392
        err = write_periodic_act(&total_time,current_t);
393
        if (err != 0) {
394
          printf("Error writing activation header\n");
395
          exit(1);
396
        }
397
        break;
398
      case PAR_ACT_MEAN:
399
        err = write_mean_act(&total_time,current_t);
400
        if (err != 0) {
401
          printf("Error writing activation header\n");
402
          exit(1);
403
        }
404
        break;
405
    }
406
 
407
    switch (current_t->exec_type) {
408
      case PAR_EXEC_CONST:
409
        err = write_exec_const(current_t);
410
        if (err != 0) {
411
          printf("Error writing exec header\n");
412
          exit(1);
413
        }
414
        break;
415
      case PAR_EXEC_MEAN:
416
        err = write_exec_mean(current_t);
417
        if (err != 0) {
418
          printf("Error writing exec header\n");
419
          exit(1);
420
        }
421
        break;
422
    }
423
 
424
    switch (current_t->crit_type) {
425
      case PAR_CRIT:
426
        err = write_block_const(current_t);
427
        if (err != 0) {
428
          printf("Error writing block header\n");
429
          exit(1);
430
        }
431
        break;
432
    }
433
 
434
    current_t = current_t->next;
435
 
436
  }
437
 
438
  write_basic_par_start();
439
 
440
  total_task_number = 0;
441
  current_t = start_loader_task;
442
  while(current_t != NULL) {
443
 
444
    write_basic_par(current_t);
445
 
446
    current_t = current_t->next;
447
 
448
    total_task_number++;
449
 
450
  }
451
 
452
  close_loader_task(total_task_number);
453
 
454
  write_contract_start();
455
 
456
  total_contract_number = 0;
457
  current_c = start_loader_contract;
458
  while(current_c != NULL) {
459
 
460
    write_contract(current_c);
461
 
462
    current_c = current_c->next;
463
 
464
    total_contract_number++;
465
 
466
  }
467
 
468
  close_loader_contract(total_contract_number);
469
 
470
  write_simulation_time(&total_time);
471
 
472
  return 0;
473
 
474
}