Subversion Repositories shark

Rev

Rev 1240 | 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;
231
  struct loader_task *start_loader_task, *current;
232
  int err,ldnum;
1241 giacomo 233
  int total_task_number;
1225 giacomo 234
 
1237 giacomo 235
  printf("\nEvent Generator\n");
1225 giacomo 236
 
1237 giacomo 237
  printf("Read loader file\n");
238
 
1238 giacomo 239
  dos_preload("load.txt",100000,&start,&end);
1237 giacomo 240
 
241
  printf("Parsing file\n");
242
 
243
  line_reader(start, end, &total_time, &start_loader_task);
244
 
1225 giacomo 245
  srandom(12354132);
246
 
1228 giacomo 247
  write_struct();
248
 
1237 giacomo 249
  current = start_loader_task;
250
  ldnum = 0;
1225 giacomo 251
 
1237 giacomo 252
  while(current != NULL) {
1225 giacomo 253
 
1237 giacomo 254
    sprintf(current->name,"ltask%d",ldnum);    
255
    current->group = ldnum;
1226 giacomo 256
 
1237 giacomo 257
    switch (current->act_type) {
258
     case PAR_ACT_SINGLE:
259
       err = write_single_act(&total_time,current);
1226 giacomo 260
        if (err != 0) {
261
          printf("Error writing activation header\n");
262
          exit(1);
263
        }
264
        break;
1237 giacomo 265
      case PAR_ACT_PERIODIC:
266
        err = write_periodic_act(&total_time,current);
1226 giacomo 267
        if (err != 0) {
268
          printf("Error writing activation header\n");
269
          exit(1);
270
        }
271
        break;
1237 giacomo 272
      case PAR_ACT_MEAN:
273
        err = write_mean_act(&total_time,current);
1226 giacomo 274
        if (err != 0) {
275
          printf("Error writing activation header\n");
276
          exit(1);
277
        }
278
        break;
279
    }
280
 
1237 giacomo 281
    switch (current->exec_type) {
282
      case PAR_EXEC_CONST:
283
        err = write_exec_const(current);
1226 giacomo 284
        if (err != 0) {
285
          printf("Error writing exec header\n");
286
          exit(1);
287
        }
288
        break;
1237 giacomo 289
      case PAR_EXEC_MEAN:
290
        err = write_exec_mean(current);
1226 giacomo 291
        if (err != 0) {
292
          printf("Error writing exec header\n");
293
          exit(1);
294
        }
295
        break;
296
    }
297
 
1237 giacomo 298
    current = current->next;
299
 
1225 giacomo 300
  }
301
 
1241 giacomo 302
  total_task_number = 0;
1238 giacomo 303
  current = start_loader_task;
304
  while(current != NULL) {
305
 
306
    write_basic_par_start();
307
 
308
    write_basic_par(current);
309
 
310
    current = current->next;
311
 
1241 giacomo 312
    total_task_number++;
313
 
1238 giacomo 314
  }
315
 
1241 giacomo 316
  close_loader(total_task_number);
1237 giacomo 317
 
1225 giacomo 318
  return 0;
319
 
320
}