Subversion Repositories shark

Rev

Rev 1238 | Rev 1241 | 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
 
12
#define EVENT_HEADER "eventh.h"
13
#define EVENT_DEFINE "eventc.c"
1238 giacomo 14
#define ACT_LIST "eventc.c"
1225 giacomo 15
 
16
 
1227 giacomo 17
int act_number;
18
 
1228 giacomo 19
int write_struct(void)
20
{
21
 
22
  FILE *file_event_header;
23
 
1237 giacomo 24
  file_event_header = fopen(EVENT_DEFINE,"w");
1228 giacomo 25
  if (file_event_header == NULL) return 1;
26
 
1240 giacomo 27
  fprintf(file_event_header, "\n#include \"func.h\"\n");
1228 giacomo 28
 
29
  fclose(file_event_header);
30
 
31
  return 0;
32
 
33
}
34
 
1238 giacomo 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
}
48
 
1237 giacomo 49
int write_basic_par(struct loader_task *c)
1228 giacomo 50
{
51
 
52
  FILE *file_event_header;
53
 
1237 giacomo 54
  file_event_header = fopen(EVENT_DEFINE,"a+");
1228 giacomo 55
  if (file_event_header == NULL) return 1;
56
 
1237 giacomo 57
  fprintf(file_event_header, "  {\"%s\",%d,%d,%d,%d,%d,{%d,%d},{%d,%d},%d,0,act_%s,exec_%s},\n",
58
                             c->name,c->task_type,c->server,c->local_scheduler,c->number,c->group,
1240 giacomo 59
                             c->deadline.tv_sec, c->deadline.tv_nsec,
60
                             c->wcet.tv_sec, c->wcet.tv_nsec,
61
                             c->act_number, c->name, c->name, c->name);
1228 giacomo 62
 
63
  fclose(file_event_header);
64
 
65
  return 0;  
66
 
67
}
68
 
69
int close_loader()
70
{
71
 
72
  FILE *file_event_header;
73
 
1237 giacomo 74
  file_event_header = fopen(EVENT_DEFINE,"a+");
1228 giacomo 75
  if (file_event_header == NULL) return 1;
76
 
77
  fprintf(file_event_header,"};\n\n");
78
 
79
  fclose(file_event_header);
80
 
81
  return 0;
82
 
83
}
84
 
1237 giacomo 85
int write_single_act(struct timespec *t, struct loader_task *c)
1225 giacomo 86
{
87
 
88
  FILE *file_act_header;
89
 
1228 giacomo 90
  file_act_header = fopen(ACT_LIST,"a+");
1225 giacomo 91
  if (file_act_header == NULL) return 1;
92
 
1237 giacomo 93
  if (TIMESPEC_A_GT_B(t,&c->act_par_1)) {
94
    fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d}};\n\n",c->name,
95
          c->act_par_1.tv_sec,c->act_par_1.tv_nsec);
96
    c->act_number = 1;
97
  } else {
98
    fprintf(file_act_header,"struct timespec act_%s[] = {{0,0}};\n\n",c->name);
99
    c->act_number = 0;
100
  }
1227 giacomo 101
 
1225 giacomo 102
  fclose(file_act_header);
103
 
104
  return 0;
105
 
106
}
107
 
1237 giacomo 108
int write_periodic_act(struct timespec *t, struct loader_task *c)
1225 giacomo 109
{
110
 
111
  FILE *file_act_header;
1237 giacomo 112
  int i;
113
  struct timespec tot_time;
114
  int period;
1228 giacomo 115
 
116
  file_act_header = fopen(ACT_LIST,"a+");
1225 giacomo 117
  if (file_act_header == NULL) return 1;
118
 
1237 giacomo 119
  fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d},\n",c->name,
120
          c->act_par_1.tv_sec,c->act_par_1.tv_nsec);
1225 giacomo 121
 
1237 giacomo 122
  c->act_number = 1;
123
  TIMESPEC_ASSIGN(&tot_time,&c->act_par_1);
124
  period = TIMESPEC2USEC(&c->act_par_2);
125
  while (TIMESPEC_A_GT_B(t, &tot_time)) {
126
    c->act_number++;
127
    ADDUSEC2TIMESPEC(period,&tot_time);
1228 giacomo 128
    fprintf(file_act_header,"  {%d,%d},\n",
1237 giacomo 129
            c->act_par_2.tv_sec,c->act_par_2.tv_nsec);
1225 giacomo 130
  }
131
 
1228 giacomo 132
  fprintf(file_act_header,"  };\n\n");
1225 giacomo 133
 
134
  fclose(file_act_header);
135
 
136
  return 0;
137
 
138
}
139
 
1237 giacomo 140
int write_mean_act(struct timespec *t,struct loader_task *c)
1225 giacomo 141
{
142
 
143
  FILE *file_act_header;
1227 giacomo 144
  int i,first_time_usec,mean_time_usec,delta_time_usec;
1237 giacomo 145
  struct timespec tot_time;
146
  int next_act;          
147
 
1228 giacomo 148
  file_act_header = fopen(ACT_LIST,"a+");
1225 giacomo 149
  if (file_act_header == NULL) return 1;
150
 
1237 giacomo 151
  fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d},\n",c->name,
152
          c->act_par_1.tv_sec,c->act_par_1.tv_nsec);
153
 
154
  c->act_number = 1;                                                                                                        
155
  TIMESPEC_ASSIGN(&tot_time,&c->act_par_1);
156
  while (TIMESPEC_A_GT_B(t, &tot_time)) {
157
    c->act_number++;
158
    next_act = TIMESPEC2USEC(&c->act_par_2) + random() % TIMESPEC2USEC(&c->act_par_3) - TIMESPEC2USEC(&c->act_par_3) / 2;
159
    ADDUSEC2TIMESPEC(next_act,&tot_time);
1232 giacomo 160
    fprintf(file_act_header,"  {%d,%d},\n",
1237 giacomo 161
            next_act / 1000000, next_act % 1000000 * 1000);
1225 giacomo 162
  }
163
 
1228 giacomo 164
  fprintf(file_act_header,"  };\n\n");
1225 giacomo 165
 
166
  fclose(file_act_header);
167
 
168
  return 0;
169
 
170
}
171
 
1237 giacomo 172
int write_exec_const(struct loader_task *c)
1226 giacomo 173
{
174
 
1227 giacomo 175
  FILE *file_exec_header;
176
  int exec_time_usec,i;
177
 
1228 giacomo 178
  file_exec_header = fopen(ACT_LIST,"a+");
1227 giacomo 179
  if (file_exec_header == NULL) return 1;
180
 
1237 giacomo 181
  fprintf(file_exec_header,"struct timespec exec_%s[] =  {{%d,%d},\n",c->name,
182
          c->exec_par_1.tv_sec,c->exec_par_1.tv_nsec);
1227 giacomo 183
 
184
  for (i=0; i< act_number-1; i++)  
1228 giacomo 185
    fprintf(file_exec_header,"  {%d,%d},\n",
1237 giacomo 186
          c->exec_par_1.tv_sec,c->exec_par_1.tv_nsec);
1227 giacomo 187
 
1228 giacomo 188
  fprintf(file_exec_header,"  };\n\n");
1227 giacomo 189
 
190
  fclose(file_exec_header);
191
 
1226 giacomo 192
  return 0;
193
 
194
}
195
 
1237 giacomo 196
int write_exec_mean(struct loader_task *c)
1226 giacomo 197
{
198
 
1227 giacomo 199
  FILE *file_exec_header;
1240 giacomo 200
  int exec_time_usec;
1237 giacomo 201
  int i;
1227 giacomo 202
 
1228 giacomo 203
  file_exec_header = fopen(ACT_LIST,"a+");
1227 giacomo 204
  if (file_exec_header == NULL) return 1;
205
 
1240 giacomo 206
  exec_time_usec = TIMESPEC2USEC(&c->exec_par_2) + random() % TIMESPEC2USEC(&c->exec_par_3) - TIMESPEC2USEC(&c->exec_par_3) / 2;
1228 giacomo 207
  fprintf(file_exec_header,"struct timespec exec_%s[] = {{%d,%d},\n",
1240 giacomo 208
          exec_time_usec / 1000000, exec_time_usec % 1000000 * 1000);
1237 giacomo 209
 
1240 giacomo 210
  for (i=0; i< act_number-1; i++) {
211
    exec_time_usec = TIMESPEC2USEC(&c->exec_par_2) + random() % TIMESPEC2USEC(&c->exec_par_3) - TIMESPEC2USEC(&c->exec_par_3) / 2;
1228 giacomo 212
    fprintf(file_exec_header,"  {%d,%d},\n",
1240 giacomo 213
          exec_time_usec / 1000000, exec_time_usec % 1000000 * 1000);
1227 giacomo 214
  }
215
 
1228 giacomo 216
  fprintf(file_exec_header,"  };\n\n");
1227 giacomo 217
 
218
  fclose(file_exec_header);
219
 
1226 giacomo 220
  return 0;
221
 
222
}
223
 
1237 giacomo 224
void *start;
225
void *end;
226
 
1225 giacomo 227
int main(void) {
228
 
229
  char task_name[100];
1226 giacomo 230
  char act_type,exec_type;
1237 giacomo 231
  struct timespec total_time;
232
  struct loader_task *start_loader_task, *current;
233
  int err,ldnum;
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
 
1238 giacomo 302
  current = start_loader_task;
303
  while(current != NULL) {
304
 
305
    write_basic_par_start();
306
 
307
    write_basic_par(current);
308
 
309
    current = current->next;
310
 
311
  }
312
 
1237 giacomo 313
  close_loader();
314
 
1225 giacomo 315
  return 0;
316
 
317
}