Subversion Repositories shark

Rev

Rev 1248 | Rev 1251 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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