Rev 1203 | Rev 1206 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1199 | giacomo | 1 | #include <kernel/kern.h> |
2 | #include "parser.h" |
||
1200 | giacomo | 3 | #include "dosread.h" |
1199 | giacomo | 4 | |
1203 | giacomo | 5 | /* Memory pointers on loaded file */ |
1200 | giacomo | 6 | extern void *start; |
7 | extern void *end; |
||
8 | |||
1203 | giacomo | 9 | /* Calibration Loops */ |
10 | #define CALIBRATION_DELTA 1000000 |
||
11 | |||
12 | /* Activate task output */ |
||
13 | #define TASK_OUTPUT |
||
14 | |||
1202 | giacomo | 15 | struct timespec zero_time; |
1203 | giacomo | 16 | int cal_cycles = 0; |
17 | int cal_rit_start = 0; |
||
18 | int cal_rit_calc_const = 0; |
||
19 | int cal_rit_calc_mean = 0; |
||
20 | int cal_rit_calc_gauss = 0; |
||
1202 | giacomo | 21 | |
1203 | giacomo | 22 | /* Not Real-Time Task */ |
1201 | giacomo | 23 | TASK nrt_test_task(void *arg) |
24 | { |
||
1203 | giacomo | 25 | long long i,exec_cycles = 0; |
26 | int exec_1,exec_2,exec_3; |
||
27 | int act_1,act_2,act_3,next_act; |
||
28 | struct timespec next_time; |
||
29 | static int act= 0; |
||
30 | struct loader_task *l = (struct loader_task *)(arg); |
||
31 | |||
32 | act++; |
||
33 | |||
34 | #ifdef TASK_OUTPUT |
||
35 | cprintf("(Act %d Pid %d Grp %d Lev %d)",act,exec_shadow,l->group,l->task_level); |
||
36 | #endif |
||
1201 | giacomo | 37 | |
1203 | giacomo | 38 | act_1 = TIMESPEC2USEC(&l->act_par_2); |
39 | act_2 = TIMESPEC2USEC(&l->act_par_3); |
||
40 | act_3 = TIMESPEC2USEC(&l->act_par_4); |
||
1202 | giacomo | 41 | |
1203 | giacomo | 42 | if (l->act_type == PAR_ACT_PERIODIC) { |
43 | kern_gettime(&next_time); |
||
44 | ADDUSEC2TIMESPEC(act_1,&next_time); |
||
45 | kern_event_post(&next_time,(void *)((void *)task_activate),(void *)exec_shadow); |
||
46 | } |
||
1201 | giacomo | 47 | |
1203 | giacomo | 48 | if (l->act_type == PAR_ACT_MEAN) { |
49 | next_act = act_1 + rand() % act_2 - act_2/2; |
||
50 | kern_gettime(&next_time); |
||
51 | ADDUSEC2TIMESPEC(next_act,&next_time); |
||
52 | kern_event_post(&next_time,(void *)((void *)task_activate),(void *)exec_shadow); |
||
53 | } |
||
1202 | giacomo | 54 | |
1203 | giacomo | 55 | if (l->act_type == PAR_ACT_GAUSS) { |
1201 | giacomo | 56 | } |
1203 | giacomo | 57 | |
58 | if (l->act_type == PAR_ACT_GAUSS_MAX) { |
||
59 | } |
||
60 | |||
61 | exec_1 = TIMESPEC2USEC(&l->exec_par_1); |
||
62 | exec_2 = TIMESPEC2USEC(&l->exec_par_2); |
||
63 | exec_3 = TIMESPEC2USEC(&l->exec_par_3); |
||
1205 | giacomo | 64 | |
65 | #ifdef TASK_OUTPUT |
||
66 | printf_xy(exec_shadow % 20 + 60, exec_shadow / 20, GREEN, "S"); |
||
67 | #endif |
||
68 | |||
1203 | giacomo | 69 | if (l->exec_type == PAR_EXEC_CONST) |
70 | exec_cycles = (long long)(exec_1 - cal_rit_start - cal_rit_calc_const) |
||
71 | * CALIBRATION_DELTA / cal_cycles; |
||
72 | |||
73 | if (l->exec_type == PAR_EXEC_MEAN) |
||
74 | exec_cycles = (long long)(exec_1 - cal_rit_start - cal_rit_calc_mean |
||
75 | + rand() % exec_2 - exec_2/2) * CALIBRATION_DELTA / cal_cycles; |
||
76 | |||
77 | if (l->exec_type == PAR_EXEC_GAUSS) |
||
78 | exec_cycles = 0; |
||
79 | |||
80 | if (l->exec_type == PAR_EXEC_GAUSS_MAX) |
||
81 | exec_cycles = 0; |
||
82 | |||
83 | for (i=0;i<exec_cycles;i++); |
||
1205 | giacomo | 84 | |
85 | #ifdef TASK_OUTPUT |
||
86 | printf_xy(exec_shadow % 20 + 60, exec_shadow / 20, WHITE, "E"); |
||
87 | #endif |
||
1203 | giacomo | 88 | |
1201 | giacomo | 89 | return NULL; |
1203 | giacomo | 90 | |
1201 | giacomo | 91 | } |
92 | |||
1203 | giacomo | 93 | /* Soft and hard Task */ |
1201 | giacomo | 94 | TASK test_task(void *arg) |
95 | { |
||
1203 | giacomo | 96 | long long i,exec_cycles = 0; |
97 | int exec_1,exec_2,exec_3; |
||
98 | int act_1,act_2,act_3,next_act; |
||
99 | struct timespec next_time; |
||
100 | static int act=0; |
||
1205 | giacomo | 101 | int extra_rit, k; |
1203 | giacomo | 102 | struct loader_task *l = (struct loader_task *)(arg); |
103 | |||
104 | act++; |
||
105 | |||
106 | #ifdef TASK_OUTPUT |
||
107 | cprintf("(Act %d Pid %d Grp %d Lev %d)",act,exec_shadow,l->group,l->task_level); |
||
108 | #endif |
||
1201 | giacomo | 109 | |
1203 | giacomo | 110 | act_1 = TIMESPEC2USEC(&l->act_par_2); |
111 | act_2 = TIMESPEC2USEC(&l->act_par_3); |
||
112 | act_3 = TIMESPEC2USEC(&l->act_par_4); |
||
113 | |||
114 | exec_1 = TIMESPEC2USEC(&l->exec_par_1); |
||
115 | exec_2 = TIMESPEC2USEC(&l->exec_par_2); |
||
116 | exec_3 = TIMESPEC2USEC(&l->exec_par_3); |
||
1202 | giacomo | 117 | |
1203 | giacomo | 118 | extra_rit = cal_rit_start; |
1205 | giacomo | 119 | |
120 | k = 0; |
||
121 | |||
1201 | giacomo | 122 | while(1) { |
123 | |||
1203 | giacomo | 124 | task_testcancel(); |
1202 | giacomo | 125 | |
1205 | giacomo | 126 | #ifdef TASK_OUTPUT |
127 | k++; |
||
128 | if (k > 15) k = 1; |
||
129 | printf_xy(exec_shadow % 20 + 59, exec_shadow / 20, k,"X"); |
||
130 | #endif |
||
131 | |||
1203 | giacomo | 132 | if (l->act_type == PAR_ACT_MEAN) { |
133 | next_act = act_1 + rand() % act_2 - act_2/2; |
||
134 | kern_gettime(&next_time); |
||
135 | ADDUSEC2TIMESPEC(next_act,&next_time); |
||
136 | kern_event_post(&next_time,(void *)((void *)task_activate),(void *)exec_shadow); |
||
137 | } |
||
138 | |||
139 | if (l->act_type == PAR_ACT_GAUSS) { |
||
140 | } |
||
141 | |||
142 | if (l->act_type == PAR_ACT_GAUSS_MAX) { |
||
143 | } |
||
144 | |||
145 | if (l->exec_type == PAR_EXEC_CONST) |
||
146 | exec_cycles = (long long)(exec_1 - extra_rit - cal_rit_calc_const) |
||
147 | * CALIBRATION_DELTA / cal_cycles; |
||
148 | |||
149 | if (l->exec_type == PAR_EXEC_MEAN) |
||
150 | exec_cycles = (long long)(exec_1 - extra_rit - cal_rit_calc_mean |
||
151 | + rand() % exec_2 - exec_2/2) * CALIBRATION_DELTA / cal_cycles; |
||
152 | |||
153 | if (l->exec_type == PAR_EXEC_GAUSS) |
||
154 | exec_cycles = 0; |
||
155 | |||
156 | if (l->exec_type == PAR_EXEC_GAUSS_MAX) |
||
157 | exec_cycles = 0; |
||
158 | |||
159 | extra_rit = 0; |
||
160 | |||
161 | for (i=0;i<exec_cycles;i++); |
||
162 | |||
1201 | giacomo | 163 | task_endcycle(); |
1203 | giacomo | 164 | |
1201 | giacomo | 165 | } |
1203 | giacomo | 166 | |
1201 | giacomo | 167 | return NULL; |
1203 | giacomo | 168 | |
169 | } |
||
1201 | giacomo | 170 | |
1203 | giacomo | 171 | /* Delay Calibration */ |
172 | int calibrate_cycle() |
||
173 | { |
||
174 | long long i; |
||
175 | struct timespec start,end,diff; |
||
176 | int temp = 1234567; |
||
177 | int temp_1 = 1234567; |
||
178 | int temp_2 = 1234567; |
||
179 | |||
180 | kern_cli(); |
||
181 | kern_gettime(&start); |
||
182 | for (i=0;i<CALIBRATION_DELTA;i++); |
||
183 | kern_gettime(&end); |
||
184 | kern_sti(); |
||
185 | |||
186 | SUBTIMESPEC(&end,&start,&diff); |
||
187 | cal_cycles = TIMESPEC2USEC(&diff); |
||
188 | |||
189 | cprintf("Calibration usec/[%d cycles] = %d\n",CALIBRATION_DELTA,cal_cycles); |
||
190 | |||
191 | kern_cli(); |
||
192 | kern_gettime(&start); |
||
193 | temp = (long long)(temp_1) * CALIBRATION_DELTA / cal_cycles; |
||
194 | kern_gettime(&end); |
||
195 | kern_sti(); |
||
196 | |||
197 | SUBTIMESPEC(&end,&start,&diff); |
||
198 | cal_rit_calc_const = TIMESPEC2USEC(&diff); |
||
199 | |||
200 | kern_cli(); |
||
201 | kern_gettime(&start); |
||
202 | temp = (long long)(temp_1 + rand() % temp_2 - temp_2/2) * CALIBRATION_DELTA / cal_cycles; |
||
203 | kern_gettime(&end); |
||
204 | kern_sti(); |
||
205 | |||
206 | SUBTIMESPEC(&end,&start,&diff); |
||
207 | cal_rit_calc_mean = TIMESPEC2USEC(&diff); |
||
208 | |||
209 | kern_cli(); |
||
210 | kern_gettime(&start); |
||
211 | temp = TIMESPEC2USEC(&start); |
||
212 | kern_gettime(&end); |
||
213 | kern_sti(); |
||
214 | |||
215 | SUBTIMESPEC(&end,&start,&diff); |
||
216 | cal_rit_start = TIMESPEC2USEC(&diff) * 6 + cal_rit_calc_const; |
||
217 | |||
218 | cprintf("Calibration delay start = %d const = %d mean = %d gauss = %d\n", |
||
219 | cal_rit_start,cal_rit_calc_const,cal_rit_calc_mean,cal_rit_calc_gauss); |
||
220 | |||
221 | return 0; |
||
222 | |||
1201 | giacomo | 223 | } |
224 | |||
1203 | giacomo | 225 | /* Task create */ |
1201 | giacomo | 226 | void loader_task_create(struct loader_task *start_loader_task) |
227 | { |
||
228 | |||
229 | struct loader_task *current = start_loader_task; |
||
1203 | giacomo | 230 | char tmp[30]; |
1201 | giacomo | 231 | int i, total_task; |
1202 | giacomo | 232 | int total_group = 0; |
1201 | giacomo | 233 | PID p; |
234 | |||
235 | total_task = 0; |
||
236 | |||
237 | while (current != NULL) { |
||
238 | |||
1202 | giacomo | 239 | total_group++; |
240 | current->group = total_group; |
||
1203 | giacomo | 241 | current->bandwidth = 0; |
1202 | giacomo | 242 | |
1201 | giacomo | 243 | for (i=0; i < current->number; i++) { |
244 | |||
245 | if (current->task_type == PAR_TASK_NRT) { |
||
246 | NRT_TASK_MODEL nrt; |
||
247 | |||
248 | nrt_task_default_model(nrt); |
||
249 | nrt_task_def_save_arrivals(nrt); |
||
250 | nrt_task_def_arg(nrt,(void *)(current)); |
||
251 | nrt_task_def_ctrl_jet(nrt); |
||
252 | nrt_task_def_level(nrt,current->task_level); |
||
1202 | giacomo | 253 | nrt_task_def_group(nrt,total_group); |
1201 | giacomo | 254 | |
1203 | giacomo | 255 | sprintf(tmp,"NRT %d:%d",current->group,i); |
256 | p = task_create(tmp,nrt_test_task,&nrt,NULL); |
||
1201 | giacomo | 257 | if (p == NIL) { |
258 | cprintf("Error nrt task creating\n"); |
||
259 | sys_end(); |
||
260 | } |
||
261 | |||
262 | total_task++; |
||
263 | |||
264 | } |
||
265 | |||
266 | if (current->task_type == PAR_TASK_HARD) { |
||
267 | HARD_TASK_MODEL ht; |
||
268 | |||
269 | hard_task_default_model(ht); |
||
270 | hard_task_def_arg(ht,(void *)(current)); |
||
271 | hard_task_def_wcet(ht,TIMESPEC2USEC(¤t->wcet)); |
||
272 | hard_task_def_ctrl_jet(ht); |
||
273 | hard_task_def_level(ht,current->task_level); |
||
1202 | giacomo | 274 | hard_task_def_group(ht,total_group); |
1201 | giacomo | 275 | |
1203 | giacomo | 276 | if (current->act_type != PAR_ACT_PERIODIC) { |
277 | hard_task_def_mit(ht,TIMESPEC2USEC(¤t->deadline)); |
||
278 | current->bandwidth += MAX_BANDWIDTH / TIMESPEC2USEC(¤t->deadline) |
||
279 | * TIMESPEC2USEC(¤t->wcet); |
||
1201 | giacomo | 280 | hard_task_def_aperiodic(ht); |
281 | } else { |
||
1203 | giacomo | 282 | hard_task_def_mit(ht,TIMESPEC2USEC(¤t->act_par_2)); |
283 | current->bandwidth += MAX_BANDWIDTH / TIMESPEC2USEC(¤t->act_par_2) |
||
284 | * TIMESPEC2USEC(¤t->wcet); |
||
1201 | giacomo | 285 | } |
1203 | giacomo | 286 | |
287 | sprintf(tmp,"HARD %d:%d",current->group,i); |
||
288 | p = task_create(tmp,test_task,&ht,NULL); |
||
1201 | giacomo | 289 | if (p == NIL) { |
290 | cprintf("Error hard task creating\n"); |
||
291 | sys_end(); |
||
292 | } |
||
293 | |||
294 | total_task++; |
||
295 | |||
296 | } |
||
297 | |||
298 | if (current->task_type == PAR_TASK_SOFT) { |
||
299 | SOFT_TASK_MODEL st; |
||
300 | |||
301 | soft_task_default_model(st); |
||
302 | soft_task_def_save_arrivals(st); |
||
303 | soft_task_def_arg(st,(void *)(current)); |
||
304 | soft_task_def_met(st,TIMESPEC2USEC(¤t->wcet)); |
||
305 | soft_task_def_ctrl_jet(st); |
||
306 | soft_task_def_level(st,current->task_level); |
||
1202 | giacomo | 307 | soft_task_def_group(st,total_group); |
308 | |||
1201 | giacomo | 309 | if (current->act_type == PAR_ACT_PERIODIC) { |
310 | soft_task_def_period(st,TIMESPEC2USEC(¤t->act_par_2)); |
||
1203 | giacomo | 311 | current->bandwidth += MAX_BANDWIDTH / TIMESPEC2USEC(¤t->act_par_2) |
312 | * TIMESPEC2USEC(¤t->wcet); |
||
1201 | giacomo | 313 | } else if (current->act_type != PAR_ACT_SINGLE) { |
314 | soft_task_def_period(st,TIMESPEC2USEC(¤t->act_par_2)); |
||
1203 | giacomo | 315 | current->bandwidth += MAX_BANDWIDTH / TIMESPEC2USEC(¤t->act_par_2) |
316 | * TIMESPEC2USEC(¤t->wcet); |
||
1201 | giacomo | 317 | soft_task_def_aperiodic(st); |
318 | } else { |
||
1203 | giacomo | 319 | soft_task_def_period(st,TIMESPEC2USEC(¤t->wcet)*1000); |
320 | current->bandwidth += MAX_BANDWIDTH / 1000; |
||
1201 | giacomo | 321 | soft_task_def_aperiodic(st); |
322 | } |
||
1203 | giacomo | 323 | |
324 | sprintf(tmp,"SOFT %d:%d",current->group,i); |
||
325 | p = task_create(tmp,test_task,&st,NULL); |
||
1201 | giacomo | 326 | if (p == NIL) { |
327 | cprintf("Error soft task creating\n"); |
||
328 | sys_end(); |
||
329 | } |
||
330 | |||
331 | total_task++; |
||
332 | |||
333 | } |
||
334 | |||
335 | } |
||
336 | |||
1203 | giacomo | 337 | cprintf("Task group %d created. Worst case BW = %d.%d \n", |
338 | current->group,(int)( (long long)current->bandwidth * 100 / MAX_BANDWIDTH), |
||
339 | (int)( (long long)current->bandwidth* 100000 / MAX_BANDWIDTH % 1000)); |
||
340 | |||
1201 | giacomo | 341 | current = current->next; |
342 | |||
343 | } |
||
344 | |||
345 | cprintf("Created %d tasks\n",total_task); |
||
346 | |||
347 | } |
||
348 | |||
1203 | giacomo | 349 | /* Set the first extivation events */ |
1202 | giacomo | 350 | void loader_first_execution(struct loader_task *start_loader_task) |
351 | { |
||
352 | |||
353 | struct loader_task *current = start_loader_task; |
||
354 | struct timespec start_time; |
||
355 | |||
356 | while (current != NULL) { |
||
357 | |||
358 | ADDTIMESPEC(&zero_time,¤t->act_par_1,&start_time); |
||
359 | |||
360 | kern_event_post(&start_time, (void *)((void *)group_activate), (void *)(current->group)); |
||
361 | |||
362 | current = current->next; |
||
363 | |||
364 | } |
||
365 | |||
366 | } |
||
367 | |||
1199 | giacomo | 368 | int main() |
369 | { |
||
370 | |||
1200 | giacomo | 371 | struct loader_task *start_loader_task = NULL; |
372 | struct timespec total; |
||
1201 | giacomo | 373 | struct timespec end_time; |
1199 | giacomo | 374 | |
1201 | giacomo | 375 | line_reader(start, end, &total, &start_loader_task); |
1200 | giacomo | 376 | |
1203 | giacomo | 377 | srand(kern_gettime(NULL)); |
378 | |||
379 | calibrate_cycle(); |
||
380 | |||
1201 | giacomo | 381 | loader_task_create(start_loader_task); |
382 | |||
383 | kern_gettime(&zero_time); |
||
1202 | giacomo | 384 | |
385 | loader_first_execution(start_loader_task); |
||
386 | |||
1201 | giacomo | 387 | ADDTIMESPEC(&zero_time,&total,&end_time); |
388 | kern_event_post(&end_time,(void *)((void *)(sys_end)),NULL); |
||
389 | |||
1199 | giacomo | 390 | return 0; |
391 | |||
392 | } |