Rev 1182 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1181 | giacomo | 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 | * Giacomo Guidi <giacomo@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 | * Copyright (C) 2002 Paolo Gai |
||
21 | * |
||
22 | * This program is free software; you can redistribute it and/or modify |
||
23 | * it under the terms of the GNU General Public License as published by |
||
24 | * the Free Software Foundation; either version 2 of the License, or |
||
25 | * (at your option) any later version. |
||
26 | * |
||
27 | * This program is distributed in the hope that it will be useful, |
||
28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
30 | * GNU General Public License for more details. |
||
31 | * |
||
32 | * You should have received a copy of the GNU General Public License |
||
33 | * along with this program; if not, write to the Free Software |
||
34 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
35 | * |
||
36 | */ |
||
37 | |||
38 | #include "kernel/kern.h" |
||
39 | |||
40 | #include "modules/edf.h" |
||
41 | #include "modules/cbs.h" |
||
42 | |||
43 | #include "pthread.h" |
||
44 | #include "time.h" |
||
45 | #include "posix.h" |
||
46 | |||
47 | #include "fsf_contract.h" |
||
48 | |||
49 | #include "cbsstar.h" |
||
50 | #include "posixstar.h" |
||
51 | #include "modules/rr.h" |
||
52 | #include "modules/dummy.h" |
||
53 | |||
54 | #include "modules/sem.h" |
||
55 | #include "modules/pi.h" |
||
56 | #include "modules/pc.h" |
||
57 | |||
58 | #include "modules/hartport.h" |
||
59 | #include "modules/cabs.h" |
||
60 | |||
61 | #include "drivers/keyb.h" |
||
62 | #include <stdlib.h> |
||
63 | |||
64 | // -------------------------------------------------- |
||
65 | // -------------------------------------------------- |
||
66 | // Init Part |
||
67 | // -------------------------------------------------- |
||
68 | // -------------------------------------------------- |
||
69 | |||
70 | /*+ sysyem tick in us +*/ |
||
71 | #define TICK 0 |
||
72 | |||
73 | /*+ RR tick in us +*/ |
||
74 | #define RRTICK 10000 |
||
75 | |||
76 | extern int cbsstar_level; |
||
77 | extern int posix_level; |
||
78 | |||
79 | TIME __kernel_register_levels__(void *arg) |
||
80 | { |
||
81 | struct multiboot_info *mb = (struct multiboot_info *)arg; |
||
82 | |||
83 | EDF_register_level(EDF_ENABLE_ALL); |
||
84 | posix_level = POSIX_register_level(RRTICK, RR_MAIN_YES, mb, 32); |
||
85 | dummy_register_level(); |
||
86 | cbsstar_level = CBSSTAR_register_level(4, 0); |
||
87 | |||
88 | // for the keyboard... |
||
89 | CBS_register_level(CBS_ENABLE_ALL, 0); |
||
90 | |||
91 | PI_register_module(); |
||
92 | PC_register_module(); |
||
93 | |||
94 | SEM_register_module(); |
||
95 | |||
96 | PTHREAD_register_module(1, 0, 1); |
||
97 | |||
98 | return TICK; |
||
99 | } |
||
100 | |||
101 | |||
102 | TASK __init__(void *arg) |
||
103 | { |
||
104 | struct multiboot_info *mb = (struct multiboot_info *)arg; |
||
105 | |||
106 | HARTPORT_init(); |
||
107 | |||
108 | KEYB_init(NULL); |
||
109 | |||
110 | __call_main__(mb); |
||
111 | |||
112 | return (void *)0; |
||
113 | |||
114 | } |
||
115 | |||
116 | // -------------------------------------------------- |
||
117 | // -------------------------------------------------- |
||
118 | // The Test |
||
119 | // -------------------------------------------------- |
||
120 | // -------------------------------------------------- |
||
121 | |||
122 | pthread_t j1 = -1; |
||
123 | pthread_t j2 = -1; |
||
124 | pthread_t j3 = -1; |
||
125 | pthread_t j4 = -1; |
||
126 | fsf_server_id_t server1 = -1; |
||
127 | fsf_server_id_t server2 = -1; |
||
128 | fsf_server_id_t server3 = -1; |
||
129 | fsf_server_id_t server4 = -1; |
||
130 | fsf_contract_parameters_t contract1, contract2; |
||
131 | |||
132 | pthread_mutex_t mux; |
||
133 | |||
134 | #define TASK_PERIOD 1000000 |
||
135 | |||
136 | extern struct timespec maxdiffint; |
||
137 | |||
138 | void *periodic_star(void *arg) |
||
139 | { |
||
140 | struct timespec actual,end,next_time; |
||
141 | int actpersecond,act,cycle; |
||
142 | int mean,nmean; |
||
143 | bool was_deadline_missed, was_budget_overran; |
||
144 | |||
145 | act = 0; |
||
146 | actpersecond = 0; |
||
147 | mean = 0; |
||
148 | nmean = 0; |
||
149 | cycle = 0; |
||
150 | for (;;) { |
||
151 | |||
152 | kern_gettime(&actual); |
||
153 | cycle++; |
||
154 | |||
155 | if (act == 0) { |
||
156 | TIMESPEC_ASSIGN(&end,&actual); |
||
157 | end.tv_sec++; |
||
158 | } |
||
159 | |||
160 | if (TIMESPEC_A_LT_B(&actual,&end)) { |
||
161 | act++; |
||
162 | } else { |
||
163 | actpersecond = act; |
||
164 | act = 0; |
||
165 | mean = (mean * nmean + actpersecond) / (nmean+1); |
||
166 | nmean++; |
||
167 | } |
||
168 | |||
169 | pthread_mutex_lock(&mux); |
||
170 | printf_xy(0,exec_shadow,WHITE,"Thread %3d Act_per_Second = %8d Mean = %8d Cycle = %8d", |
||
171 | exec_shadow,actpersecond,mean,cycle); |
||
172 | printf_xy(50,20,RED,"<DISTIME Ts%ld:Tns%ld>",maxdiffint.tv_sec,maxdiffint.tv_nsec); |
||
173 | |||
174 | pthread_mutex_unlock(&mux); |
||
175 | |||
176 | kern_gettime(&next_time); |
||
177 | ADDUSEC2TIMESPEC(TASK_PERIOD, &next_time); |
||
178 | fsf_schedule_next_timed_job(NULL, NULL, NULL, &was_deadline_missed, &was_budget_overran); |
||
179 | |||
180 | } |
||
181 | |||
182 | return NULL; |
||
183 | |||
184 | } |
||
185 | |||
186 | void *star(void *arg) |
||
187 | { |
||
188 | struct timespec actual,end; |
||
189 | int actpersecond,act; |
||
190 | int mean,nmean,cycle; |
||
191 | |||
192 | act = 0; |
||
193 | actpersecond = 0; |
||
194 | mean = 0; |
||
195 | nmean = 0; |
||
196 | cycle = 0; |
||
197 | for (;;) { |
||
198 | |||
199 | cycle++; |
||
200 | kern_gettime(&actual); |
||
201 | |||
202 | if (act == 0) { |
||
203 | TIMESPEC_ASSIGN(&end,&actual); |
||
204 | end.tv_sec++; |
||
205 | } |
||
206 | |||
207 | if (TIMESPEC_A_LT_B(&actual,&end)) { |
||
208 | act++; |
||
209 | } else { |
||
210 | actpersecond = act; |
||
211 | act = 0; |
||
212 | mean = (mean * nmean + actpersecond) / (nmean+1); |
||
213 | nmean++; |
||
214 | } |
||
215 | |||
216 | pthread_mutex_lock(&mux); |
||
217 | printf_xy(0,exec_shadow,WHITE,"Thread %3d Act_per_Second = %8d Mean = %8d Cycle = %8d", |
||
218 | exec_shadow,actpersecond,mean,cycle); |
||
219 | pthread_mutex_unlock(&mux); |
||
220 | |||
221 | } |
||
222 | |||
223 | return NULL; |
||
224 | |||
225 | } |
||
226 | |||
227 | void *edftask(void *arg) |
||
228 | { |
||
229 | int i,j; |
||
230 | while(1) { |
||
231 | for (i=0;i<5; i++) { |
||
232 | for (j=0; j<10; j++); |
||
233 | //cputc('#'); |
||
234 | //cputs((char *)(arg)); |
||
235 | } |
||
236 | |||
237 | task_endcycle(); |
||
238 | } |
||
239 | |||
240 | return NULL; |
||
241 | } |
||
242 | |||
243 | |||
244 | void create() |
||
245 | { |
||
246 | HARD_TASK_MODEL mhard; |
||
247 | |||
248 | struct timespec period1 = {0,100000000}; |
||
249 | struct timespec period2 = {0,100000000}; |
||
250 | struct timespec budget1 = {0,30000000}; |
||
251 | struct timespec budget2 = {0,30000000}; |
||
252 | |||
253 | PID t1, t2; |
||
254 | int err; |
||
255 | |||
256 | kern_printf("(Start Create)"); |
||
257 | |||
258 | hard_task_default_model(mhard); |
||
259 | hard_task_def_ctrl_jet(mhard); |
||
260 | hard_task_def_mit(mhard,32000); |
||
261 | hard_task_def_wcet(mhard,3000); |
||
262 | hard_task_def_arg(mhard,(void *)"X"); |
||
263 | hard_task_def_group(mhard,1); |
||
264 | hard_task_def_periodic(mhard); |
||
265 | |||
266 | t1 = task_create("X", edftask, &mhard, NULL); |
||
267 | if (t1 == NIL) { |
||
268 | perror("Could not create task X ..."); |
||
269 | sys_end(); |
||
270 | } |
||
271 | |||
272 | hard_task_def_mit(mhard,32000); |
||
273 | hard_task_def_wcet(mhard,3000); |
||
274 | hard_task_def_arg(mhard,(void *)"Y"); |
||
275 | t2 = task_create("Y", edftask, &mhard, NULL); |
||
276 | if (t2 == NIL) { |
||
277 | perror("Could not create task Y ..."); |
||
278 | sys_end(); |
||
279 | } |
||
280 | |||
281 | group_activate(1); |
||
282 | |||
283 | err = pthread_create(&j1, NULL, star, (void *)"A"); |
||
284 | if (err) { |
||
285 | perror("Could not create task A ..."); |
||
286 | sys_end(); |
||
287 | } |
||
288 | |||
289 | err = pthread_create(&j2, NULL, periodic_star, (void *)"B"); |
||
290 | if (err) { |
||
291 | perror("Could not create task B ..."); |
||
292 | sys_end(); |
||
293 | } |
||
294 | |||
295 | err = pthread_create(&j3, NULL, star, (void *)"C"); |
||
296 | if (err) { |
||
297 | perror("Could not create task C ..."); |
||
298 | sys_end(); |
||
299 | } |
||
300 | |||
301 | err = pthread_create(&j4, NULL, periodic_star, (void *)"D"); |
||
302 | if (err) { |
||
303 | perror("Could not create task D ..."); |
||
304 | sys_end(); |
||
305 | } |
||
306 | |||
307 | fsf_initialize_contract(&contract1); |
||
308 | fsf_set_contract_basic_parameters(&contract1,&budget1,&period1,NULL,NULL,FSF_DEFAULT_WORKLOAD); |
||
309 | fsf_initialize_contract(&contract2); |
||
310 | fsf_set_contract_basic_parameters(&contract2,&budget2,&period2,NULL,NULL,FSF_DEFAULT_WORKLOAD); |
||
311 | fsf_set_local_scheduler_parameter(&contract2,FSF_SCHEDULER_EDF); |
||
312 | |||
313 | kern_printf("(End Create)"); |
||
314 | |||
315 | } |
||
316 | |||
317 | int main(int argc, char **argv) |
||
318 | { |
||
319 | |||
320 | char ch = 0; |
||
321 | int err; |
||
322 | NRT_TASK_MODEL nrt; |
||
323 | HARD_TASK_MODEL ht; |
||
324 | |||
325 | pthread_mutex_init(&mux,NULL); |
||
326 | |||
327 | create(); |
||
328 | |||
329 | nrt_task_default_model(nrt); |
||
330 | nrt_task_def_save_arrivals(nrt); |
||
331 | |||
332 | hard_task_default_model(ht); |
||
333 | hard_task_def_mit(ht,1000000); |
||
334 | hard_task_def_wcet(ht,10000); |
||
335 | |||
336 | do { |
||
337 | ch = keyb_getch(BLOCK); |
||
338 | |||
339 | switch(ch) { |
||
340 | case '1': |
||
341 | err = fsf_bind_thread_to_server(server1,j1,&nrt); |
||
342 | kern_printf("(%d)",err); |
||
343 | break; |
||
344 | case '2': |
||
345 | err = fsf_bind_thread_to_server(server2,j2,&ht); |
||
346 | kern_printf("(%d)",err); |
||
347 | break; |
||
348 | case '3': |
||
349 | err = fsf_bind_thread_to_server(server1,j3,&nrt); |
||
350 | kern_printf("(%d)",err); |
||
351 | break; |
||
352 | case '4': |
||
353 | err = fsf_bind_thread_to_server(server2,j4,&ht); |
||
354 | kern_printf("(%d)",err); |
||
355 | break; |
||
356 | case '5': |
||
357 | err = fsf_unbind_thread_from_server(j1); |
||
358 | kern_printf("(%d)",err); |
||
359 | break; |
||
360 | case '6': |
||
361 | err = fsf_unbind_thread_from_server(j2); |
||
362 | kern_printf("(%d)",err); |
||
363 | break; |
||
364 | case '7': |
||
365 | err = fsf_unbind_thread_from_server(j3); |
||
366 | kern_printf("(%d)",err); |
||
367 | break; |
||
368 | case '8': |
||
369 | err = fsf_unbind_thread_from_server(j4); |
||
370 | kern_printf("(%d)",err); |
||
371 | break; |
||
372 | case 'q': |
||
373 | err = fsf_negotiate_contract(&contract1,&server1); |
||
374 | cprintf("(%d)",err); |
||
375 | break; |
||
376 | case 'w': |
||
377 | err = fsf_negotiate_contract(&contract2,&server2); |
||
378 | kern_printf("(%d)",err); |
||
379 | break; |
||
380 | case 'e': |
||
381 | err = fsf_negotiate_contract(&contract1,&server3); |
||
382 | kern_printf("(%d)",err); |
||
383 | break; |
||
384 | case 'r': |
||
385 | err = fsf_cancel_contract(&server1); |
||
386 | kern_printf("(%d)",err); |
||
387 | break; |
||
388 | case 't': |
||
389 | err = fsf_cancel_contract(&server2); |
||
390 | kern_printf("(%d)",err); |
||
391 | break; |
||
392 | case 'y': |
||
393 | err = fsf_cancel_contract(&server3); |
||
394 | kern_printf("(%d)",err); |
||
395 | break; |
||
396 | |||
397 | } |
||
398 | |||
399 | } while(ch != ESC); |
||
400 | |||
401 | sys_end(); |
||
402 | |||
403 | return 0; |
||
404 | |||
405 | } |
||
406 |