Subversion Repositories shark

Rev

Rev 1244 | 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
 
12
#define EVENT_HEADER "eventh.h"
13
#define EVENT_DEFINE "eventc.c"
1238 giacomo 14
#define ACT_LIST "eventc.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);
130
 
131
  fclose(file_event_header);
132
 
133
  return 0;
134
 
135
}
136
 
1237 giacomo 137
int write_single_act(struct timespec *t, struct loader_task *c)
1225 giacomo 138
{
139
 
140
  FILE *file_act_header;
141
 
1228 giacomo 142
  file_act_header = fopen(ACT_LIST,"a+");
1225 giacomo 143
  if (file_act_header == NULL) return 1;
144
 
1237 giacomo 145
  if (TIMESPEC_A_GT_B(t,&c->act_par_1)) {
146
    fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d}};\n\n",c->name,
147
          c->act_par_1.tv_sec,c->act_par_1.tv_nsec);
148
    c->act_number = 1;
149
  } else {
150
    fprintf(file_act_header,"struct timespec act_%s[] = {{0,0}};\n\n",c->name);
151
    c->act_number = 0;
152
  }
1227 giacomo 153
 
1225 giacomo 154
  fclose(file_act_header);
155
 
156
  return 0;
157
 
158
}
159
 
1237 giacomo 160
int write_periodic_act(struct timespec *t, struct loader_task *c)
1225 giacomo 161
{
162
 
163
  FILE *file_act_header;
1237 giacomo 164
  int i;
165
  struct timespec tot_time;
166
  int period;
1228 giacomo 167
 
168
  file_act_header = fopen(ACT_LIST,"a+");
1225 giacomo 169
  if (file_act_header == NULL) return 1;
170
 
1237 giacomo 171
  fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d},\n",c->name,
172
          c->act_par_1.tv_sec,c->act_par_1.tv_nsec);
1225 giacomo 173
 
1237 giacomo 174
  c->act_number = 1;
175
  TIMESPEC_ASSIGN(&tot_time,&c->act_par_1);
176
  period = TIMESPEC2USEC(&c->act_par_2);
177
  while (TIMESPEC_A_GT_B(t, &tot_time)) {
178
    c->act_number++;
179
    ADDUSEC2TIMESPEC(period,&tot_time);
1228 giacomo 180
    fprintf(file_act_header,"  {%d,%d},\n",
1237 giacomo 181
            c->act_par_2.tv_sec,c->act_par_2.tv_nsec);
1225 giacomo 182
  }
183
 
1228 giacomo 184
  fprintf(file_act_header,"  };\n\n");
1225 giacomo 185
 
186
  fclose(file_act_header);
187
 
188
  return 0;
189
 
190
}
191
 
1237 giacomo 192
int write_mean_act(struct timespec *t,struct loader_task *c)
1225 giacomo 193
{
194
 
195
  FILE *file_act_header;
1227 giacomo 196
  int i,first_time_usec,mean_time_usec,delta_time_usec;
1237 giacomo 197
  struct timespec tot_time;
198
  int next_act;          
199
 
1228 giacomo 200
  file_act_header = fopen(ACT_LIST,"a+");
1225 giacomo 201
  if (file_act_header == NULL) return 1;
202
 
1237 giacomo 203
  fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d},\n",c->name,
204
          c->act_par_1.tv_sec,c->act_par_1.tv_nsec);
205
 
206
  c->act_number = 1;                                                                                                        
207
  TIMESPEC_ASSIGN(&tot_time,&c->act_par_1);
208
  while (TIMESPEC_A_GT_B(t, &tot_time)) {
209
    c->act_number++;
210
    next_act = TIMESPEC2USEC(&c->act_par_2) + random() % TIMESPEC2USEC(&c->act_par_3) - TIMESPEC2USEC(&c->act_par_3) / 2;
211
    ADDUSEC2TIMESPEC(next_act,&tot_time);
1232 giacomo 212
    fprintf(file_act_header,"  {%d,%d},\n",
1237 giacomo 213
            next_act / 1000000, next_act % 1000000 * 1000);
1225 giacomo 214
  }
215
 
1228 giacomo 216
  fprintf(file_act_header,"  };\n\n");
1225 giacomo 217
 
218
  fclose(file_act_header);
219
 
220
  return 0;
221
 
222
}
223
 
1237 giacomo 224
int write_exec_const(struct loader_task *c)
1226 giacomo 225
{
226
 
1227 giacomo 227
  FILE *file_exec_header;
228
  int exec_time_usec,i;
229
 
1228 giacomo 230
  file_exec_header = fopen(ACT_LIST,"a+");
1227 giacomo 231
  if (file_exec_header == NULL) return 1;
232
 
1237 giacomo 233
  fprintf(file_exec_header,"struct timespec exec_%s[] =  {{%d,%d},\n",c->name,
234
          c->exec_par_1.tv_sec,c->exec_par_1.tv_nsec);
1227 giacomo 235
 
1241 giacomo 236
  for (i=0; i< c->act_number-1; i++)  
1228 giacomo 237
    fprintf(file_exec_header,"  {%d,%d},\n",
1237 giacomo 238
          c->exec_par_1.tv_sec,c->exec_par_1.tv_nsec);
1227 giacomo 239
 
1228 giacomo 240
  fprintf(file_exec_header,"  };\n\n");
1227 giacomo 241
 
242
  fclose(file_exec_header);
243
 
1226 giacomo 244
  return 0;
245
 
246
}
247
 
1237 giacomo 248
int write_exec_mean(struct loader_task *c)
1226 giacomo 249
{
250
 
1227 giacomo 251
  FILE *file_exec_header;
1240 giacomo 252
  int exec_time_usec;
1237 giacomo 253
  int i;
1227 giacomo 254
 
1228 giacomo 255
  file_exec_header = fopen(ACT_LIST,"a+");
1227 giacomo 256
  if (file_exec_header == NULL) return 1;
257
 
1240 giacomo 258
  exec_time_usec = TIMESPEC2USEC(&c->exec_par_2) + random() % TIMESPEC2USEC(&c->exec_par_3) - TIMESPEC2USEC(&c->exec_par_3) / 2;
1228 giacomo 259
  fprintf(file_exec_header,"struct timespec exec_%s[] = {{%d,%d},\n",
1240 giacomo 260
          exec_time_usec / 1000000, exec_time_usec % 1000000 * 1000);
1237 giacomo 261
 
1241 giacomo 262
  for (i=0; i< c->act_number-1; i++) {
1240 giacomo 263
    exec_time_usec = TIMESPEC2USEC(&c->exec_par_2) + random() % TIMESPEC2USEC(&c->exec_par_3) - TIMESPEC2USEC(&c->exec_par_3) / 2;
1228 giacomo 264
    fprintf(file_exec_header,"  {%d,%d},\n",
1240 giacomo 265
          exec_time_usec / 1000000, exec_time_usec % 1000000 * 1000);
1227 giacomo 266
  }
267
 
1228 giacomo 268
  fprintf(file_exec_header,"  };\n\n");
1227 giacomo 269
 
270
  fclose(file_exec_header);
271
 
1226 giacomo 272
  return 0;
273
 
274
}
275
 
1237 giacomo 276
void *start;
277
void *end;
278
 
1225 giacomo 279
int main(void) {
280
 
281
  char task_name[100];
1226 giacomo 282
  char act_type,exec_type;
1237 giacomo 283
  struct timespec total_time;
1244 giacomo 284
  struct loader_task *start_loader_task = NULL, *current_t;
285
  struct loader_contract *start_loader_contract = NULL, *current_c;
1237 giacomo 286
  int err,ldnum;
1241 giacomo 287
  int total_task_number;
1245 giacomo 288
  int total_contract_number;
1225 giacomo 289
 
1237 giacomo 290
  printf("\nEvent Generator\n");
1225 giacomo 291
 
1237 giacomo 292
  printf("Read loader file\n");
293
 
1238 giacomo 294
  dos_preload("load.txt",100000,&start,&end);
1237 giacomo 295
 
296
  printf("Parsing file\n");
297
 
1244 giacomo 298
  line_reader(start, end, &total_time, &start_loader_task, &start_loader_contract);
1237 giacomo 299
 
1225 giacomo 300
  srandom(12354132);
301
 
1228 giacomo 302
  write_struct();
303
 
1244 giacomo 304
  current_t = start_loader_task;
1243 giacomo 305
  ldnum = 1;
1225 giacomo 306
 
1244 giacomo 307
  while(current_t != NULL) {
1225 giacomo 308
 
1244 giacomo 309
    sprintf(current_t->name,"ltask%d",ldnum);    
310
    current_t->group = ldnum;
311
    ldnum++;
1226 giacomo 312
 
1244 giacomo 313
    switch (current_t->act_type) {
1237 giacomo 314
     case PAR_ACT_SINGLE:
1244 giacomo 315
       err = write_single_act(&total_time,current_t);
1226 giacomo 316
        if (err != 0) {
317
          printf("Error writing activation header\n");
318
          exit(1);
319
        }
320
        break;
1237 giacomo 321
      case PAR_ACT_PERIODIC:
1244 giacomo 322
        err = write_periodic_act(&total_time,current_t);
1226 giacomo 323
        if (err != 0) {
324
          printf("Error writing activation header\n");
325
          exit(1);
326
        }
327
        break;
1237 giacomo 328
      case PAR_ACT_MEAN:
1244 giacomo 329
        err = write_mean_act(&total_time,current_t);
1226 giacomo 330
        if (err != 0) {
331
          printf("Error writing activation header\n");
332
          exit(1);
333
        }
334
        break;
335
    }
336
 
1244 giacomo 337
    switch (current_t->exec_type) {
1237 giacomo 338
      case PAR_EXEC_CONST:
1244 giacomo 339
        err = write_exec_const(current_t);
1226 giacomo 340
        if (err != 0) {
341
          printf("Error writing exec header\n");
342
          exit(1);
343
        }
344
        break;
1237 giacomo 345
      case PAR_EXEC_MEAN:
1244 giacomo 346
        err = write_exec_mean(current_t);
1226 giacomo 347
        if (err != 0) {
348
          printf("Error writing exec header\n");
349
          exit(1);
350
        }
351
        break;
352
    }
353
 
1244 giacomo 354
    current_t = current_t->next;
1237 giacomo 355
 
1225 giacomo 356
  }
357
 
1244 giacomo 358
  write_basic_par_start();
359
 
1241 giacomo 360
  total_task_number = 0;
1244 giacomo 361
  current_t = start_loader_task;
362
  while(current_t != NULL) {
1238 giacomo 363
 
1244 giacomo 364
    write_basic_par(current_t);
1238 giacomo 365
 
1244 giacomo 366
    current_t = current_t->next;
1238 giacomo 367
 
1241 giacomo 368
    total_task_number++;
369
 
1238 giacomo 370
  }
371
 
1245 giacomo 372
  close_loader_task(total_task_number);
1237 giacomo 373
 
1245 giacomo 374
  write_contract_start();
375
 
376
  total_contract_number = 0;
377
  current_c = start_loader_contract;
378
  while(current_c != NULL) {
379
 
380
    write_contract(current_c);
381
 
382
    current_c = current_c->next;
383
 
384
    total_contract_number++;
385
 
386
  }
387
 
388
  close_loader_contract(total_contract_number);
389
 
1225 giacomo 390
  return 0;
391
 
392
}