Rev 1226 | Rev 1228 | 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 | |||
9 | #include "event_gen.h" |
||
10 | |||
1226 | giacomo | 11 | #define ACT_HEADER "act_header.h" |
12 | #define EXEC_HEADER "exec_header.h" |
||
13 | |||
1225 | giacomo | 14 | #define ACT_SINGLE 0 |
15 | #define ACT_PERIODIC 1 |
||
16 | #define ACT_MEAN 2 |
||
17 | #define ACT_INVALID 3 |
||
18 | |||
1226 | giacomo | 19 | #define EXEC_CONST 0 |
20 | #define EXEC_MEAN 1 |
||
21 | #define EXEC_INVALID 2 |
||
22 | |||
1227 | giacomo | 23 | int act_number; |
24 | |||
1225 | giacomo | 25 | int select_act_type(void) |
26 | { |
||
27 | char act_type[10]; |
||
28 | |||
29 | printf("\nInsert the activation type\n"); |
||
30 | printf(" S - Single\n"); |
||
31 | printf(" P - Periodic\n"); |
||
32 | printf(" M - Sporadic with constant distribution (Mean,Delta)\n"); |
||
33 | printf("> "); |
||
34 | scanf("%s",act_type); |
||
35 | |||
36 | switch (act_type[0]) { |
||
37 | |||
38 | case 's': |
||
39 | case 'S': |
||
40 | |||
41 | return ACT_SINGLE; |
||
42 | break; |
||
43 | |||
44 | case 'p': |
||
45 | case 'P': |
||
46 | |||
47 | return ACT_PERIODIC; |
||
48 | break; |
||
49 | |||
50 | case 'm': |
||
51 | case 'M': |
||
52 | |||
53 | return ACT_MEAN; |
||
54 | break; |
||
55 | |||
56 | default: |
||
57 | |||
58 | return ACT_INVALID; |
||
59 | |||
60 | } |
||
61 | |||
62 | return ACT_INVALID; |
||
63 | |||
64 | } |
||
65 | |||
1226 | giacomo | 66 | int select_exec_type(void) |
67 | { |
||
68 | char exec_type[10]; |
||
69 | |||
70 | printf("\nInsert the execution time\n"); |
||
71 | printf(" C - Const Exec Time\n"); |
||
72 | printf(" M - Mean with constant distribution (Mean,Delta)\n"); |
||
73 | printf("> "); |
||
74 | scanf("%s",exec_type); |
||
75 | |||
76 | switch (exec_type[0]) { |
||
77 | |||
78 | case 'c': |
||
79 | case 'C': |
||
80 | |||
81 | return EXEC_CONST; |
||
82 | break; |
||
83 | |||
84 | case 'm': |
||
85 | case 'M': |
||
86 | |||
87 | return EXEC_MEAN; |
||
88 | break; |
||
89 | |||
90 | default: |
||
91 | |||
92 | return EXEC_INVALID; |
||
93 | |||
94 | } |
||
95 | |||
96 | return EXEC_INVALID; |
||
97 | |||
98 | } |
||
99 | |||
1225 | giacomo | 100 | int write_single_act(char *task_name) |
101 | { |
||
102 | |||
103 | FILE *file_act_header; |
||
104 | int time_usec; |
||
105 | |||
1226 | giacomo | 106 | file_act_header = fopen(ACT_HEADER,"a+"); |
1225 | giacomo | 107 | if (file_act_header == NULL) return 1; |
108 | |||
1227 | giacomo | 109 | act_number = 1; |
110 | |||
1225 | giacomo | 111 | printf("\nInsert single activation time [us]\n"); |
112 | printf("> "); |
||
113 | scanf("%d",&time_usec); |
||
114 | |||
115 | fprintf(file_act_header,"\nstruct timespec %s_act_event[1] = {%d,%d};\n", |
||
1227 | giacomo | 116 | task_name,time_usec/1000000,time_usec%1000000*1000); |
1225 | giacomo | 117 | |
118 | fclose(file_act_header); |
||
119 | |||
120 | return 0; |
||
121 | |||
122 | } |
||
123 | |||
124 | int write_periodic_act(char *task_name) |
||
125 | { |
||
126 | |||
127 | FILE *file_act_header; |
||
1227 | giacomo | 128 | int i,first_time_usec,per_time_usec; |
1225 | giacomo | 129 | long long tot_time_usec; |
130 | |||
1226 | giacomo | 131 | file_act_header = fopen(ACT_HEADER,"a+"); |
1225 | giacomo | 132 | if (file_act_header == NULL) return 1; |
133 | |||
134 | printf("\nInsert the number of activations\n"); |
||
135 | printf("> "); |
||
1227 | giacomo | 136 | scanf("%d",&act_number); |
1225 | giacomo | 137 | printf("Insert first activation time [us]\n"); |
138 | printf("> "); |
||
139 | scanf("%d",&first_time_usec); |
||
140 | printf("Insert periodic activation time [us]\n"); |
||
141 | printf("> "); |
||
142 | scanf("%d",&per_time_usec); |
||
143 | |||
144 | fprintf(file_act_header,"\nstruct timespec %s_act_event[%d] = {{%d,%d},\n", |
||
1227 | giacomo | 145 | task_name,act_number,first_time_usec/1000000,first_time_usec%1000000*1000); |
1225 | giacomo | 146 | |
147 | tot_time_usec = first_time_usec; |
||
1227 | giacomo | 148 | for (i=0;i<act_number-1;i++) { |
1225 | giacomo | 149 | tot_time_usec += per_time_usec; |
150 | fprintf(file_act_header," {%d,%d},\n", |
||
1227 | giacomo | 151 | (int)tot_time_usec/1000000,(int)tot_time_usec%1000000*1000); |
1225 | giacomo | 152 | } |
153 | |||
154 | fprintf(file_act_header,"};\n"); |
||
155 | |||
156 | fclose(file_act_header); |
||
157 | |||
158 | return 0; |
||
159 | |||
160 | } |
||
161 | |||
162 | int write_mean_act(char *task_name) |
||
163 | { |
||
164 | |||
165 | FILE *file_act_header; |
||
1227 | giacomo | 166 | int i,first_time_usec,mean_time_usec,delta_time_usec; |
1225 | giacomo | 167 | long long tot_time_usec; |
168 | |||
1226 | giacomo | 169 | file_act_header = fopen(ACT_HEADER,"a+"); |
1225 | giacomo | 170 | if (file_act_header == NULL) return 1; |
171 | |||
172 | printf("\nInsert the number of activations\n"); |
||
173 | printf("> "); |
||
1227 | giacomo | 174 | scanf("%d",&act_number); |
1225 | giacomo | 175 | printf("Insert first activation time [us]\n"); |
176 | printf("> "); |
||
177 | scanf("%d",&first_time_usec); |
||
178 | printf("Insert mean peridic time [us]\n"); |
||
179 | printf("> "); |
||
180 | scanf("%d",&mean_time_usec); |
||
181 | printf("Insert delta time [us]\n"); |
||
182 | printf("> "); |
||
183 | scanf("%d",&delta_time_usec); |
||
184 | |||
185 | fprintf(file_act_header,"\nstruct timespec %s_act_event[%d] = {{%d,%d},\n", |
||
1227 | giacomo | 186 | task_name,act_number,first_time_usec/1000000,first_time_usec%1000000*1000); |
1225 | giacomo | 187 | |
188 | tot_time_usec = first_time_usec; |
||
1227 | giacomo | 189 | for (i=0;i<act_number-1;i++) { |
1225 | giacomo | 190 | tot_time_usec += mean_time_usec + random() % delta_time_usec - delta_time_usec/2; |
191 | fprintf(file_act_header," {%d,%d},\n", |
||
1227 | giacomo | 192 | (int)tot_time_usec/1000000,(int)tot_time_usec%1000000*1000); |
1225 | giacomo | 193 | } |
194 | |||
195 | fprintf(file_act_header,"};\n"); |
||
196 | |||
197 | fclose(file_act_header); |
||
198 | |||
199 | return 0; |
||
200 | |||
201 | } |
||
202 | |||
1226 | giacomo | 203 | int write_exec_const(char *task_name) |
204 | { |
||
205 | |||
1227 | giacomo | 206 | FILE *file_exec_header; |
207 | int exec_time_usec,i; |
||
208 | |||
209 | file_exec_header = fopen(EXEC_HEADER,"a+"); |
||
210 | if (file_exec_header == NULL) return 1; |
||
211 | |||
212 | printf("Insert execution time [us]\n"); |
||
213 | printf("> "); |
||
214 | scanf("%d",&exec_time_usec); |
||
215 | |||
216 | fprintf(file_exec_header,"\nstruct timespec %s_exec_time[%d] = {{%d,%d},\n", |
||
217 | task_name,act_number,exec_time_usec/1000000,exec_time_usec%1000000*1000); |
||
218 | |||
219 | for (i=0; i< act_number-1; i++) |
||
220 | fprintf(file_exec_header," {%d,%d},\n", |
||
221 | exec_time_usec/1000000,exec_time_usec%1000000*1000); |
||
222 | |||
223 | fprintf(file_exec_header,"};\n"); |
||
224 | |||
225 | fclose(file_exec_header); |
||
226 | |||
1226 | giacomo | 227 | return 0; |
228 | |||
229 | } |
||
230 | |||
231 | int write_exec_mean(char *task_name) |
||
232 | { |
||
233 | |||
1227 | giacomo | 234 | FILE *file_exec_header; |
235 | int exec_time_usec,mean_time_usec,delta_time_usec,i; |
||
236 | |||
237 | file_exec_header = fopen(EXEC_HEADER,"a+"); |
||
238 | if (file_exec_header == NULL) return 1; |
||
239 | |||
240 | printf("Insert mean execution time [us]\n"); |
||
241 | printf("> "); |
||
242 | scanf("%d",&mean_time_usec); |
||
243 | printf("Insert delta execution time [us]\n"); |
||
244 | printf("> "); |
||
245 | scanf("%d",&delta_time_usec); |
||
246 | |||
247 | exec_time_usec = mean_time_usec + random() % delta_time_usec - delta_time_usec / 2; |
||
248 | |||
249 | fprintf(file_exec_header,"\nstruct timespec %s_exec_time[%d] = {{%d,%d},\n", |
||
250 | task_name,act_number,exec_time_usec/1000000,exec_time_usec%1000000*1000); |
||
251 | |||
252 | for (i=0; i< act_number-1; i++) { |
||
253 | exec_time_usec = mean_time_usec + random() % delta_time_usec - delta_time_usec / 2; |
||
254 | fprintf(file_exec_header," {%d,%d},\n", |
||
255 | exec_time_usec/1000000,exec_time_usec%1000000*1000); |
||
256 | } |
||
257 | |||
258 | fprintf(file_exec_header,"};\n"); |
||
259 | |||
260 | fclose(file_exec_header); |
||
261 | |||
1226 | giacomo | 262 | return 0; |
263 | |||
264 | } |
||
265 | |||
1225 | giacomo | 266 | int main(void) { |
267 | |||
268 | char task_name[100]; |
||
1226 | giacomo | 269 | char act_type,exec_type; |
1225 | giacomo | 270 | int err; |
271 | |||
272 | printf("\nEvent Generator\n\n"); |
||
273 | |||
274 | srandom(12354132); |
||
275 | |||
1226 | giacomo | 276 | while(1) { |
1225 | giacomo | 277 | |
1226 | giacomo | 278 | printf("Insert the task name\n"); |
279 | printf("Write \"q\" to quit program\n"); |
||
280 | printf("> "); |
||
281 | scanf("%s",task_name); |
||
1225 | giacomo | 282 | |
1226 | giacomo | 283 | if (strlen(task_name) == 1 && task_name[0] == 'q') exit(0); |
284 | |||
285 | while((act_type = select_act_type()) == ACT_INVALID) { |
||
286 | printf("Error: Invalid Act Type\n"); |
||
287 | } |
||
288 | |||
289 | switch (act_type) { |
||
290 | case ACT_SINGLE: |
||
291 | err = write_single_act(task_name); |
||
292 | if (err != 0) { |
||
293 | printf("Error writing activation header\n"); |
||
294 | exit(1); |
||
295 | } |
||
296 | break; |
||
297 | case ACT_PERIODIC: |
||
298 | err = write_periodic_act(task_name); |
||
299 | if (err != 0) { |
||
300 | printf("Error writing activation header\n"); |
||
301 | exit(1); |
||
302 | } |
||
303 | break; |
||
304 | case ACT_MEAN: |
||
305 | err = write_mean_act(task_name); |
||
306 | if (err != 0) { |
||
307 | printf("Error writing activation header\n"); |
||
308 | exit(1); |
||
309 | } |
||
310 | break; |
||
311 | } |
||
312 | |||
313 | while((exec_type = select_exec_type()) == EXEC_INVALID) { |
||
314 | printf("Error: Invalid Exec Type\n"); |
||
315 | } |
||
316 | |||
317 | switch (exec_type) { |
||
318 | case EXEC_CONST: |
||
319 | err = write_exec_const(task_name); |
||
320 | if (err != 0) { |
||
321 | printf("Error writing exec header\n"); |
||
322 | exit(1); |
||
323 | } |
||
324 | break; |
||
325 | case EXEC_MEAN: |
||
326 | err = write_exec_mean(task_name); |
||
327 | if (err != 0) { |
||
328 | printf("Error writing exec header\n"); |
||
329 | exit(1); |
||
330 | } |
||
331 | break; |
||
332 | } |
||
333 | |||
1225 | giacomo | 334 | } |
335 | |||
336 | return 0; |
||
337 | |||
338 | } |