Subversion Repositories shark

Rev

Rev 1243 | 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
 
1237 giacomo 46
int write_basic_par(struct loader_task *c)
1228 giacomo 47
{
48
 
49
  FILE *file_event_header;
50
 
1237 giacomo 51
  file_event_header = fopen(EVENT_DEFINE,"a+");
1228 giacomo 52
  if (file_event_header == NULL) return 1;
53
 
1237 giacomo 54
  fprintf(file_event_header, "  {\"%s\",%d,%d,%d,%d,%d,{%d,%d},{%d,%d},%d,0,act_%s,exec_%s},\n",
55
                             c->name,c->task_type,c->server,c->local_scheduler,c->number,c->group,
1240 giacomo 56
                             c->deadline.tv_sec, c->deadline.tv_nsec,
57
                             c->wcet.tv_sec, c->wcet.tv_nsec,
58
                             c->act_number, c->name, c->name, c->name);
1228 giacomo 59
 
60
  fclose(file_event_header);
61
 
62
  return 0;  
63
 
64
}
65
 
1241 giacomo 66
int close_loader(int total_task_number)
1228 giacomo 67
{
68
 
69
  FILE *file_event_header;
70
 
1237 giacomo 71
  file_event_header = fopen(EVENT_DEFINE,"a+");
1228 giacomo 72
  if (file_event_header == NULL) return 1;
73
 
74
  fprintf(file_event_header,"};\n\n");
75
 
1241 giacomo 76
  fprintf(file_event_header,"int total_loader_task = %d;\n\n",total_task_number);
77
 
1228 giacomo 78
  fclose(file_event_header);
79
 
80
  return 0;
81
 
82
}
83
 
1237 giacomo 84
int write_single_act(struct timespec *t, struct loader_task *c)
1225 giacomo 85
{
86
 
87
  FILE *file_act_header;
88
 
1228 giacomo 89
  file_act_header = fopen(ACT_LIST,"a+");
1225 giacomo 90
  if (file_act_header == NULL) return 1;
91
 
1237 giacomo 92
  if (TIMESPEC_A_GT_B(t,&c->act_par_1)) {
93
    fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d}};\n\n",c->name,
94
          c->act_par_1.tv_sec,c->act_par_1.tv_nsec);
95
    c->act_number = 1;
96
  } else {
97
    fprintf(file_act_header,"struct timespec act_%s[] = {{0,0}};\n\n",c->name);
98
    c->act_number = 0;
99
  }
1227 giacomo 100
 
1225 giacomo 101
  fclose(file_act_header);
102
 
103
  return 0;
104
 
105
}
106
 
1237 giacomo 107
int write_periodic_act(struct timespec *t, struct loader_task *c)
1225 giacomo 108
{
109
 
110
  FILE *file_act_header;
1237 giacomo 111
  int i;
112
  struct timespec tot_time;
113
  int period;
1228 giacomo 114
 
115
  file_act_header = fopen(ACT_LIST,"a+");
1225 giacomo 116
  if (file_act_header == NULL) return 1;
117
 
1237 giacomo 118
  fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d},\n",c->name,
119
          c->act_par_1.tv_sec,c->act_par_1.tv_nsec);
1225 giacomo 120
 
1237 giacomo 121
  c->act_number = 1;
122
  TIMESPEC_ASSIGN(&tot_time,&c->act_par_1);
123
  period = TIMESPEC2USEC(&c->act_par_2);
124
  while (TIMESPEC_A_GT_B(t, &tot_time)) {
125
    c->act_number++;
126
    ADDUSEC2TIMESPEC(period,&tot_time);
1228 giacomo 127
    fprintf(file_act_header,"  {%d,%d},\n",
1237 giacomo 128
            c->act_par_2.tv_sec,c->act_par_2.tv_nsec);
1225 giacomo 129
  }
130
 
1228 giacomo 131
  fprintf(file_act_header,"  };\n\n");
1225 giacomo 132
 
133
  fclose(file_act_header);
134
 
135
  return 0;
136
 
137
}
138
 
1237 giacomo 139
int write_mean_act(struct timespec *t,struct loader_task *c)
1225 giacomo 140
{
141
 
142
  FILE *file_act_header;
1227 giacomo 143
  int i,first_time_usec,mean_time_usec,delta_time_usec;
1237 giacomo 144
  struct timespec tot_time;
145
  int next_act;          
146
 
1228 giacomo 147
  file_act_header = fopen(ACT_LIST,"a+");
1225 giacomo 148
  if (file_act_header == NULL) return 1;
149
 
1237 giacomo 150
  fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d},\n",c->name,
151
          c->act_par_1.tv_sec,c->act_par_1.tv_nsec);
152
 
153
  c->act_number = 1;                                                                                                        
154
  TIMESPEC_ASSIGN(&tot_time,&c->act_par_1);
155
  while (TIMESPEC_A_GT_B(t, &tot_time)) {
156
    c->act_number++;
157
    next_act = TIMESPEC2USEC(&c->act_par_2) + random() % TIMESPEC2USEC(&c->act_par_3) - TIMESPEC2USEC(&c->act_par_3) / 2;
158
    ADDUSEC2TIMESPEC(next_act,&tot_time);
1232 giacomo 159
    fprintf(file_act_header,"  {%d,%d},\n",
1237 giacomo 160
            next_act / 1000000, next_act % 1000000 * 1000);
1225 giacomo 161
  }
162
 
1228 giacomo 163
  fprintf(file_act_header,"  };\n\n");
1225 giacomo 164
 
165
  fclose(file_act_header);
166
 
167
  return 0;
168
 
169
}
170
 
1237 giacomo 171
int write_exec_const(struct loader_task *c)
1226 giacomo 172
{
173
 
1227 giacomo 174
  FILE *file_exec_header;
175
  int exec_time_usec,i;
176
 
1228 giacomo 177
  file_exec_header = fopen(ACT_LIST,"a+");
1227 giacomo 178
  if (file_exec_header == NULL) return 1;
179
 
1237 giacomo 180
  fprintf(file_exec_header,"struct timespec exec_%s[] =  {{%d,%d},\n",c->name,
181
          c->exec_par_1.tv_sec,c->exec_par_1.tv_nsec);
1227 giacomo 182
 
1241 giacomo 183
  for (i=0; i< c->act_number-1; i++)  
1228 giacomo 184
    fprintf(file_exec_header,"  {%d,%d},\n",
1237 giacomo 185
          c->exec_par_1.tv_sec,c->exec_par_1.tv_nsec);
1227 giacomo 186
 
1228 giacomo 187
  fprintf(file_exec_header,"  };\n\n");
1227 giacomo 188
 
189
  fclose(file_exec_header);
190
 
1226 giacomo 191
  return 0;
192
 
193
}
194
 
1237 giacomo 195
int write_exec_mean(struct loader_task *c)
1226 giacomo 196
{
197
 
1227 giacomo 198
  FILE *file_exec_header;
1240 giacomo 199
  int exec_time_usec;
1237 giacomo 200
  int i;
1227 giacomo 201
 
1228 giacomo 202
  file_exec_header = fopen(ACT_LIST,"a+");
1227 giacomo 203
  if (file_exec_header == NULL) return 1;
204
 
1240 giacomo 205
  exec_time_usec = TIMESPEC2USEC(&c->exec_par_2) + random() % TIMESPEC2USEC(&c->exec_par_3) - TIMESPEC2USEC(&c->exec_par_3) / 2;
1228 giacomo 206
  fprintf(file_exec_header,"struct timespec exec_%s[] = {{%d,%d},\n",
1240 giacomo 207
          exec_time_usec / 1000000, exec_time_usec % 1000000 * 1000);
1237 giacomo 208
 
1241 giacomo 209
  for (i=0; i< c->act_number-1; i++) {
1240 giacomo 210
    exec_time_usec = TIMESPEC2USEC(&c->exec_par_2) + random() % TIMESPEC2USEC(&c->exec_par_3) - TIMESPEC2USEC(&c->exec_par_3) / 2;
1228 giacomo 211
    fprintf(file_exec_header,"  {%d,%d},\n",
1240 giacomo 212
          exec_time_usec / 1000000, exec_time_usec % 1000000 * 1000);
1227 giacomo 213
  }
214
 
1228 giacomo 215
  fprintf(file_exec_header,"  };\n\n");
1227 giacomo 216
 
217
  fclose(file_exec_header);
218
 
1226 giacomo 219
  return 0;
220
 
221
}
222
 
1237 giacomo 223
void *start;
224
void *end;
225
 
1225 giacomo 226
int main(void) {
227
 
228
  char task_name[100];
1226 giacomo 229
  char act_type,exec_type;
1237 giacomo 230
  struct timespec total_time;
1244 giacomo 231
  struct loader_task *start_loader_task = NULL, *current_t;
232
  struct loader_contract *start_loader_contract = NULL, *current_c;
1237 giacomo 233
  int err,ldnum;
1241 giacomo 234
  int total_task_number;
1225 giacomo 235
 
1237 giacomo 236
  printf("\nEvent Generator\n");
1225 giacomo 237
 
1237 giacomo 238
  printf("Read loader file\n");
239
 
1238 giacomo 240
  dos_preload("load.txt",100000,&start,&end);
1237 giacomo 241
 
242
  printf("Parsing file\n");
243
 
1244 giacomo 244
  line_reader(start, end, &total_time, &start_loader_task, &start_loader_contract);
1237 giacomo 245
 
1225 giacomo 246
  srandom(12354132);
247
 
1228 giacomo 248
  write_struct();
249
 
1244 giacomo 250
  current_t = start_loader_task;
1243 giacomo 251
  ldnum = 1;
1225 giacomo 252
 
1244 giacomo 253
  while(current_t != NULL) {
1225 giacomo 254
 
1244 giacomo 255
    sprintf(current_t->name,"ltask%d",ldnum);    
256
    current_t->group = ldnum;
257
    ldnum++;
1226 giacomo 258
 
1244 giacomo 259
    switch (current_t->act_type) {
1237 giacomo 260
     case PAR_ACT_SINGLE:
1244 giacomo 261
       err = write_single_act(&total_time,current_t);
1226 giacomo 262
        if (err != 0) {
263
          printf("Error writing activation header\n");
264
          exit(1);
265
        }
266
        break;
1237 giacomo 267
      case PAR_ACT_PERIODIC:
1244 giacomo 268
        err = write_periodic_act(&total_time,current_t);
1226 giacomo 269
        if (err != 0) {
270
          printf("Error writing activation header\n");
271
          exit(1);
272
        }
273
        break;
1237 giacomo 274
      case PAR_ACT_MEAN:
1244 giacomo 275
        err = write_mean_act(&total_time,current_t);
1226 giacomo 276
        if (err != 0) {
277
          printf("Error writing activation header\n");
278
          exit(1);
279
        }
280
        break;
281
    }
282
 
1244 giacomo 283
    switch (current_t->exec_type) {
1237 giacomo 284
      case PAR_EXEC_CONST:
1244 giacomo 285
        err = write_exec_const(current_t);
1226 giacomo 286
        if (err != 0) {
287
          printf("Error writing exec header\n");
288
          exit(1);
289
        }
290
        break;
1237 giacomo 291
      case PAR_EXEC_MEAN:
1244 giacomo 292
        err = write_exec_mean(current_t);
1226 giacomo 293
        if (err != 0) {
294
          printf("Error writing exec header\n");
295
          exit(1);
296
        }
297
        break;
298
    }
299
 
1244 giacomo 300
    current_t = current_t->next;
1237 giacomo 301
 
1225 giacomo 302
  }
303
 
1244 giacomo 304
  write_basic_par_start();
305
 
1241 giacomo 306
  total_task_number = 0;
1244 giacomo 307
  current_t = start_loader_task;
308
  while(current_t != NULL) {
1238 giacomo 309
 
1244 giacomo 310
    write_basic_par(current_t);
1238 giacomo 311
 
1244 giacomo 312
    current_t = current_t->next;
1238 giacomo 313
 
1241 giacomo 314
    total_task_number++;
315
 
1238 giacomo 316
  }
317
 
1241 giacomo 318
  close_loader(total_task_number);
1237 giacomo 319
 
1225 giacomo 320
  return 0;
321
 
322
}