Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1663 | pj | 1 | /* |
2 | * Project: S.Ha.R.K. |
||
3 | * |
||
4 | * Coordinators: |
||
5 | * Giorgio Buttazzo <giorgio@sssup.it> |
||
6 | * Paolo Gai <pj@gandalf.sssup.it> |
||
7 | * |
||
8 | * Authors : |
||
9 | * Paolo Gai <pj@gandalf.sssup.it> |
||
10 | * (see the web pages for full authors list) |
||
11 | * |
||
12 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
13 | * |
||
14 | * http://www.sssup.it |
||
15 | * http://retis.sssup.it |
||
16 | * http://shark.sssup.it |
||
17 | */ |
||
18 | |||
19 | /** |
||
20 | ------------ |
||
21 | CVS : $Id: step3.c,v 1.1 2004-07-05 14:17:15 pj Exp $ |
||
22 | |||
23 | File: $File$ |
||
24 | Revision: $Revision: 1.1 $ |
||
25 | Last update: $Date: 2004-07-05 14:17:15 $ |
||
26 | ------------ |
||
27 | **/ |
||
28 | |||
29 | /* |
||
30 | * Copyright (C) 2000 Paolo Gai |
||
31 | * |
||
32 | * This program is free software; you can redistribute it and/or modify |
||
33 | * it under the terms of the GNU General Public License as published by |
||
34 | * the Free Software Foundation; either version 2 of the License, or |
||
35 | * (at your option) any later version. |
||
36 | * |
||
37 | * This program is distributed in the hope that it will be useful, |
||
38 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
39 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
40 | * GNU General Public License for more details. |
||
41 | * |
||
42 | * You should have received a copy of the GNU General Public License |
||
43 | * along with this program; if not, write to the Free Software |
||
44 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
45 | * |
||
46 | */ |
||
47 | |||
48 | #include "kernel/kern.h" |
||
49 | #include "string.h" |
||
50 | #include "ll/i386/x-dos.h" |
||
51 | #include "modules/rr.h" |
||
52 | #include "modules/dummy.h" |
||
53 | #include "modules/edf.h" |
||
54 | #include "modules/sem.h" |
||
55 | #include "modules/hartport.h" |
||
56 | #include "drivers/keyb.h" |
||
57 | #include "ctype.h" |
||
58 | #include "tune.h" |
||
59 | |||
60 | |||
61 | int ending=0; |
||
62 | |||
63 | |||
64 | // Patterns |
||
65 | #define MAX_PATTERN 10000 |
||
66 | int max_pattern = 0; |
||
67 | TIME pattern_t[MAX_PATTERN]; |
||
68 | TIME pattern_period[MAX_PATTERN]; |
||
69 | TIME pattern_wcet[MAX_PATTERN]; |
||
70 | TIME pattern_iter[MAX_PATTERN]; |
||
71 | TIME pattern_value[MAX_PATTERN]; |
||
72 | TIME pattern_penalty[MAX_PATTERN]; |
||
73 | |||
74 | // Random load |
||
75 | int max_random = 0; |
||
76 | TIME random_t[MAX_PATTERN]; |
||
77 | TIME random_reldline[MAX_PATTERN]; |
||
78 | TIME random_wcet[MAX_PATTERN]; |
||
79 | int random_value[MAX_PATTERN]; |
||
80 | int random_penalty[MAX_PATTERN]; |
||
81 | |||
82 | // Bad load |
||
83 | int max_bad = 0; |
||
84 | TIME bad_t[MAX_PATTERN]; |
||
85 | TIME bad_reldline[MAX_PATTERN]; |
||
86 | TIME bad_wcet[MAX_PATTERN]; |
||
87 | int bad_value[MAX_PATTERN]; |
||
88 | int bad_penalty[MAX_PATTERN]; |
||
89 | |||
90 | |||
91 | |||
92 | #define WCET_SLACK 500 |
||
93 | |||
94 | void tune_pattern(void) |
||
95 | { |
||
96 | int i,w; |
||
97 | |||
98 | for (i=0; i<max_pattern; i++) { |
||
99 | place(0,15); |
||
100 | cprintf("Tuning index %d/%d.\n",i,max_pattern); |
||
101 | if (i>0 && pattern_wcet[i]==pattern_wcet[i-1]) |
||
102 | pattern_iter[i] = pattern_iter[i-1]; |
||
103 | else { |
||
104 | w = (pattern_wcet[i]*80)/100-WCET_SLACK; |
||
105 | // if (w>pattern_wcet[i]-WCET_SLACK) w=pattern_wcet[i]-WCET_SLACK; |
||
106 | pattern_iter[i] = tune_CPU_speed(w); |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | |||
111 | |||
112 | |||
113 | |||
114 | // Shape modification |
||
115 | |||
116 | #define DEFAULT_SHIFT 1000000 |
||
117 | #define MAX_SHAPE 1000 |
||
118 | |||
119 | bandwidth_t shape_B[MAX_SHAPE]; |
||
120 | TIME shape_T[MAX_SHAPE]; |
||
121 | int max_shape; |
||
122 | |||
123 | |||
124 | |||
125 | // this function creates a set if tasks (1 or 2) that comes at random |
||
126 | // time, with random values and random wcet. |
||
127 | #define MAX_PERIOD 2998 |
||
128 | void generate_random_load(void) |
||
129 | { |
||
130 | int current_time = DEFAULT_SHIFT; |
||
131 | |||
132 | while (current_time<shape_T[max_shape-1]*1000000) { |
||
133 | random_t[max_random] = current_time; |
||
134 | random_reldline[max_random] = rand()%MAX_PERIOD*1000+2; |
||
135 | random_wcet[max_random] = rand()%(random_reldline[max_random]-1000)+1000; |
||
136 | random_value[max_random] = rand()%101; |
||
137 | random_penalty[max_random] = rand()%101; |
||
138 | |||
139 | if (rand()%2) |
||
140 | current_time += random_reldline[max_random] + 100000; |
||
141 | |||
142 | max_random++; |
||
143 | } |
||
144 | } |
||
145 | |||
146 | #define MAX_VALUE_REL_DLINE 3000000 |
||
147 | #define SUBTASKS 10 |
||
148 | #define SUBSUBTASKS 30 |
||
149 | void generate_bad_load(void) |
||
150 | { |
||
151 | int i,j,k; |
||
152 | bandwidth_t freeB; |
||
153 | TIME t,l; |
||
154 | |||
155 | |||
156 | int subtasks, rdline; |
||
157 | |||
158 | for (i=1; i < max_shape; i++) { |
||
159 | //length of the period with the same max bandwidth |
||
160 | l = (shape_T[i]-shape_T[i-1])* 1000000; |
||
161 | freeB = MAX_BANDWIDTH - shape_B[i-1]; |
||
162 | t = shape_T[i-1]*1000000 + DEFAULT_SHIFT; |
||
163 | |||
164 | if (l/SUBTASKS > MAX_VALUE_REL_DLINE) { |
||
165 | subtasks = l/MAX_VALUE_REL_DLINE; |
||
166 | rdline = MAX_VALUE_REL_DLINE; |
||
167 | } |
||
168 | else { |
||
169 | subtasks = SUBTASKS; |
||
170 | rdline = l/SUBTASKS; |
||
171 | } |
||
172 | |||
173 | for (j=0; j<subtasks; j++) { |
||
174 | // create the first "big" task |
||
175 | bad_t[max_bad] = t; |
||
176 | bad_reldline[max_bad] = rdline; |
||
177 | bad_wcet[max_bad] = (int)(((double)freeB/(double)MAX_BANDWIDTH)*bad_reldline[max_bad]); |
||
178 | bad_value[max_bad] = 80; |
||
179 | bad_penalty[max_bad] = 90; |
||
180 | max_bad++; |
||
181 | |||
182 | // create the small subtasks!! |
||
183 | for (k=0; k<SUBSUBTASKS; k++) { |
||
184 | bad_t[max_bad] = t; |
||
185 | bad_reldline[max_bad] = rdline/SUBSUBTASKS; |
||
186 | bad_wcet[max_bad] = (int)(((double)freeB/(double)MAX_BANDWIDTH)*bad_reldline[max_bad]); |
||
187 | bad_value[max_bad] = 100/SUBSUBTASKS; |
||
188 | bad_penalty[max_bad] = 200/SUBSUBTASKS; |
||
189 | max_bad++; |
||
190 | |||
191 | t += rdline/SUBSUBTASKS; |
||
192 | } |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | |||
197 | #define INSERT_RANDOM 1 |
||
198 | #define INSERT_BAD 2 |
||
199 | |||
200 | // This function merges the random load and the bad_load into the pattern! |
||
201 | // This function subsumes that the two arrays are ordered by time. |
||
202 | void generate_pattern(void) |
||
203 | { |
||
204 | int random_i, bad_i; |
||
205 | int todo = 0; |
||
206 | |||
207 | random_i = 0; |
||
208 | bad_i = 0; |
||
209 | |||
210 | while (!(random_i == max_random && bad_i == max_bad)) { |
||
211 | if (random_i < max_random) { |
||
212 | // there are random available |
||
213 | if (bad_i < max_bad) |
||
214 | // random and bad available |
||
215 | if (random_t[random_i] < bad_t[bad_i]) |
||
216 | todo = INSERT_RANDOM; |
||
217 | else |
||
218 | todo = INSERT_BAD; |
||
219 | else |
||
220 | // only random |
||
221 | todo = INSERT_RANDOM; |
||
222 | } |
||
223 | else |
||
224 | // only bad available |
||
225 | todo = INSERT_BAD; |
||
226 | |||
227 | if (todo == INSERT_RANDOM) { |
||
228 | pattern_t[max_pattern] = random_t[random_i]; |
||
229 | pattern_period[max_pattern] = random_reldline[random_i]; |
||
230 | pattern_wcet[max_pattern] = random_wcet[random_i]; |
||
231 | pattern_value[max_pattern] = random_value[random_i]; |
||
232 | pattern_penalty[max_pattern] = random_penalty[random_i]; |
||
233 | max_pattern++; |
||
234 | random_i++; |
||
235 | } |
||
236 | else { |
||
237 | pattern_t[max_pattern] = bad_t[bad_i]; |
||
238 | pattern_period[max_pattern] = bad_reldline[bad_i]; |
||
239 | pattern_wcet[max_pattern] = bad_wcet[bad_i]; |
||
240 | pattern_value[max_pattern] = bad_value[bad_i]; |
||
241 | pattern_penalty[max_pattern] = bad_penalty[bad_i]; |
||
242 | max_pattern++; |
||
243 | bad_i++; |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 | |||
248 | // Read/Write part |
||
249 | |||
250 | #define FILEBUF_DIM 10000 |
||
251 | |||
252 | /* This is the buffer used by read_myfile */ |
||
253 | char myfilebuf[FILEBUF_DIM]; |
||
254 | |||
255 | /* This is the number of bytes read by read_myfile */ |
||
256 | int myfilebuf_length; |
||
257 | |||
258 | /* the buffer b is scannedc to search for numbers |
||
259 | at the first non-number the function stops */ |
||
260 | int geti(char *b, int *pos, int maxpos) |
||
261 | { |
||
262 | int res = 0; |
||
263 | |||
264 | // skip first chars |
||
265 | while (!isdigit(b[*pos])) { |
||
266 | (*pos)++; |
||
267 | if (*pos == maxpos) return -1; // Error!!! |
||
268 | } |
||
269 | |||
270 | |||
271 | // read the numbers |
||
272 | do { |
||
273 | res = (res * 10) + b[*pos] - '0'; |
||
274 | (*pos)++; |
||
275 | } while (isdigit(b[*pos])); |
||
276 | |||
277 | return res; |
||
278 | } |
||
279 | |||
280 | |||
281 | void parse_shape(void) |
||
282 | { |
||
283 | int curr_filepos = 0; |
||
284 | int i = 0; |
||
285 | |||
286 | // read the number of tasks into the data file |
||
287 | max_shape = geti(myfilebuf, &curr_filepos, myfilebuf_length); |
||
288 | if (max_shape == -1) { |
||
289 | cprintf("Error parsing shape.dat file...\n"); |
||
290 | sys_end(); |
||
291 | } |
||
292 | |||
293 | for (i=0; i<max_shape; i++) { |
||
294 | // read the seven datas |
||
295 | shape_T[i] = geti(myfilebuf, &curr_filepos, myfilebuf_length); |
||
296 | shape_B[i] = geti(myfilebuf, &curr_filepos, myfilebuf_length); |
||
297 | |||
298 | // check the last one for an error |
||
299 | if (shape_B[i] == -1) { |
||
300 | cprintf("Error parsing shape %d...\n",i); |
||
301 | sys_end(); |
||
302 | } |
||
303 | } |
||
304 | } |
||
305 | |||
306 | void read_shapes(void) |
||
307 | { |
||
308 | char name[]="shape.dat"; |
||
309 | |||
310 | /* DOS file descriptor */ |
||
311 | DOS_FILE *f; |
||
312 | |||
313 | /* Error code */ |
||
314 | int err; |
||
315 | |||
316 | /* open the DOS file for reading (you can specify only "r" or "w") */ |
||
317 | f = DOS_fopen(name,"r"); |
||
318 | |||
319 | /* check for open errors */ |
||
320 | if (!f) { |
||
321 | /* error!! */ |
||
322 | err = DOS_error(); |
||
323 | |||
324 | /* note that if you call DOS_error() here, it return 0!!! */ |
||
325 | cprintf("Error %d opening %s...\n", err, name); |
||
326 | myfilebuf_length = 0; |
||
327 | return; |
||
328 | } |
||
329 | |||
330 | /* read up to 1000 chars */ |
||
331 | myfilebuf_length = DOS_fread(&myfilebuf,1,FILEBUF_DIM,f); |
||
332 | |||
333 | /* check for errors */ |
||
334 | err = DOS_error(); |
||
335 | |||
336 | cprintf("Read %d bytes from %s...\n", myfilebuf_length, name); |
||
337 | |||
338 | if (err) { |
||
339 | cprintf("Error %d reading %s...\n", err, name); |
||
340 | myfilebuf_length = 0; |
||
341 | /* there is not return because I want to close the file! */ |
||
342 | } |
||
343 | |||
344 | /* Close the file */ |
||
345 | DOS_fclose(f); |
||
346 | } |
||
347 | |||
348 | |||
349 | /* This function write myfile.out (up to 30 chars) */ |
||
350 | void write_vpattern(void *arg) |
||
351 | { |
||
352 | DOS_FILE *f; /* DOS file descriptor */ |
||
353 | int err=0; /* Error code */ |
||
354 | int writtenbytes = 0; /* number of files written */ |
||
355 | char buf[100]; |
||
356 | int i; |
||
357 | |||
358 | /* open the DOS file for writing (you can specify only "r" or "w") */ |
||
359 | f = DOS_fopen("vpattern.dat","w"); |
||
360 | |||
361 | /* check for open errors */ |
||
362 | if (!f) { |
||
363 | /* error!! */ |
||
364 | err = DOS_error(); |
||
365 | |||
366 | /* note that if you call DOS_error() here, it return 0!!! */ |
||
367 | cprintf("Error %d opening hpattern.dat...\n", err); |
||
368 | return; |
||
369 | } |
||
370 | |||
371 | sprintf(buf,"%d\r\n",max_pattern); |
||
372 | writtenbytes += DOS_fwrite(buf,1,strlen(buf),f); |
||
373 | |||
374 | for (i=0; i<max_pattern; i++) { |
||
375 | sprintf(buf,"%ld %ld %ld %ld %ld %ld\r\n", |
||
376 | pattern_t[i], |
||
377 | pattern_period[i], |
||
378 | pattern_wcet[i], |
||
379 | pattern_iter[i], |
||
380 | pattern_value[i], |
||
381 | pattern_penalty[i]); |
||
382 | |||
383 | writtenbytes += DOS_fwrite(buf,1,strlen(buf),f); |
||
384 | |||
385 | /* check for errors */ |
||
386 | err = DOS_error(); |
||
387 | if (err) break; |
||
388 | } |
||
389 | |||
390 | cprintf("Written %d bytes into vpattern.dat...\n", writtenbytes); |
||
391 | |||
392 | if (err) { |
||
393 | cprintf("Error %d writing vpattern.dat...\n", err); |
||
394 | /* there is not return because I want to close the file! */ |
||
395 | } |
||
396 | |||
397 | /* Close the file */ |
||
398 | DOS_fclose(f); |
||
399 | } |
||
400 | |||
401 | |||
402 | |||
403 | |||
404 | |||
405 | |||
406 | |||
407 | |||
408 | |||
409 | /*+ sysyem tick in us +*/ |
||
410 | #define TICK 0 |
||
411 | |||
412 | /*+ RR tick in us +*/ |
||
413 | #define RRTICK 10000 |
||
414 | |||
415 | void read_myfile(void); |
||
416 | |||
417 | TIME __kernel_register_levels__(void *arg) |
||
418 | { |
||
419 | struct multiboot_info *mb = (struct multiboot_info *)arg; |
||
420 | |||
421 | EDF_register_level(EDF_ENABLE_ALL); |
||
422 | RR_register_level(RRTICK, RR_MAIN_NO, mb); |
||
423 | RR_register_level(RRTICK, RR_MAIN_YES, mb); |
||
424 | dummy_register_level(); |
||
425 | |||
426 | SEM_register_module(); |
||
427 | |||
428 | read_shapes(); |
||
429 | |||
430 | return TICK; |
||
431 | } |
||
432 | |||
433 | |||
434 | NRT_TASK_MODEL keyb_model; |
||
435 | |||
436 | TASK __init__(void *arg) |
||
437 | { |
||
438 | KEYB_PARMS kparms = BASE_KEYB; |
||
439 | |||
440 | HARTPORT_init(); |
||
441 | |||
442 | nrt_task_default_model(keyb_model); |
||
443 | nrt_task_def_system(keyb_model); |
||
444 | nrt_task_def_nokill(keyb_model); |
||
445 | keyb_def_task(kparms,(TASK_MODEL *)&keyb_model); |
||
446 | KEYB_init(&kparms); |
||
447 | |||
448 | set_exchandler_grx(); |
||
449 | |||
450 | srand(sys_gettime(NULL)); |
||
451 | |||
452 | clear(); |
||
453 | |||
454 | sys_atrunlevel(write_vpattern, NULL, RUNLEVEL_AFTER_EXIT); |
||
455 | |||
456 | parse_shape(); |
||
457 | |||
458 | // random load... |
||
459 | generate_random_load(); |
||
460 | |||
461 | // a big task and a lot of small tasks that uses 2*(MAX_BANDWIDTH-shape) |
||
462 | generate_bad_load(); |
||
463 | |||
464 | // mixes the two loads |
||
465 | generate_pattern(); |
||
466 | |||
467 | tune_pattern(); |
||
468 | |||
469 | return (void *)0; |
||
470 | } |
||
471 | |||
472 | // never called!!! |
||
473 | int main() |
||
474 | { |
||
475 | return 0; |
||
476 | } |