Rev 1538 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1254 | giacomo | 1 | /* FSF Loader |
2 | * |
||
3 | * Load and run a specific set of tasks/contracts |
||
4 | * |
||
5 | * This is the system indipendent part |
||
6 | * |
||
7 | * Giacomo Guidi <giacomo@gandalf.sssup.it> |
||
8 | * Michael Timarchi <trimarchi@gandalf.sssup.it> |
||
9 | * |
||
10 | */ |
||
11 | |||
1538 | trimarchi | 12 | #include "fsf_basic_types.h" |
13 | #include "fsf_core.h" //Framework main header |
||
14 | |||
1254 | giacomo | 15 | #include "calibrate.h" |
16 | #include "func.h" //Generic function definitions |
||
1255 | giacomo | 17 | #include "lconst.h" |
1254 | giacomo | 18 | |
19 | /* Activate task output debug */ |
||
20 | #define TASK_OUTPUT |
||
21 | |||
1255 | giacomo | 22 | int cal_cycles = CALIBRATION_RESULT; //Calibration const, it converts usec to cycles |
1254 | giacomo | 23 | struct timespec zero_time; //Zero time of the simulation |
24 | extern struct loader_task loader_task_list[]; //Loader task array |
||
25 | extern int total_loader_task; //Loader task number |
||
26 | |||
27 | /* OS: Oneshot Task: |
||
28 | begin |
||
29 | - execution |
||
30 | end |
||
31 | */ |
||
32 | void *oneshot_task(void *arg) |
||
33 | { |
||
34 | long long i,exec_cycles = 0; |
||
35 | struct loader_task *l = (struct loader_task *)(arg); |
||
1262 | giacomo | 36 | #ifdef TASK_OUTPUT |
37 | #ifdef OS_SHARK |
||
38 | char tmp[20]; |
||
39 | #endif |
||
40 | #endif |
||
1254 | giacomo | 41 | |
1264 | giacomo | 42 | start_oneshot_task(); |
43 | |||
44 | /* to avoid problem if the task start inside the create function */ |
||
1254 | giacomo | 45 | if (l->act_current == 0) l->act_current = 1; |
46 | #ifdef TASK_OUTPUT |
||
1262 | giacomo | 47 | #ifdef OS_SHARK |
48 | sprintf(tmp,"[ONESHOT]"); |
||
49 | printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task() / 5 + 5, GREEN, tmp); |
||
50 | #endif |
||
1254 | giacomo | 51 | #endif |
52 | |||
53 | exec_cycles = (long long)(TIMESPEC2USEC(&l->exec[l->act_current-1])) * CALIBRATION_DELTA / cal_cycles; |
||
54 | |||
55 | /* Execution delay */ |
||
56 | for (i=0;i<exec_cycles;i++) |
||
57 | __asm__ __volatile__ ("xorl %%eax,%%eax\n\t" |
||
58 | "cpuid\n\t" |
||
59 | :::"eax","ebx","ecx","edx"); |
||
60 | |||
1264 | giacomo | 61 | end_oneshot_task(); |
1266 | giacomo | 62 | |
63 | #ifdef TASK_OUTPUT |
||
64 | #ifdef OS_SHARK |
||
65 | sprintf(tmp,"[--END--]"); |
||
66 | printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task() / 5 + 5, GREEN, tmp); |
||
67 | #endif |
||
68 | #endif |
||
1264 | giacomo | 69 | |
1254 | giacomo | 70 | return NULL; |
71 | |||
72 | } |
||
73 | |||
74 | /* CT: Cyclical Task: |
||
75 | begin |
||
76 | while (1) { |
||
77 | - execution |
||
78 | - end_cycle |
||
79 | } |
||
80 | end (never end) |
||
81 | */ |
||
82 | void *periodic_task(void *arg) |
||
83 | { |
||
1304 | giacomo | 84 | long long i,exec_cycles = 0,block_cycles = 0; |
1254 | giacomo | 85 | int act = 0; |
86 | struct loader_task *l = (struct loader_task *)(arg); |
||
87 | |||
1262 | giacomo | 88 | #ifdef TASK_OUTPUT |
89 | #ifdef OS_SHARK |
||
90 | char tmp[20]; |
||
91 | #endif |
||
92 | #endif |
||
93 | |||
1264 | giacomo | 94 | start_periodic_task(); |
95 | |||
1254 | giacomo | 96 | if (l->act_current == 0) l->act_current = 1; |
97 | |||
98 | while(1) { |
||
99 | |||
1264 | giacomo | 100 | start_job_periodic_task(); |
101 | |||
1254 | giacomo | 102 | #ifdef TASK_OUTPUT |
1262 | giacomo | 103 | #ifdef OS_SHARK |
104 | sprintf(tmp,"C[%06d]",act); |
||
105 | printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task() / 5 + 5, GREEN, tmp); |
||
106 | #endif |
||
1254 | giacomo | 107 | #endif |
108 | |||
109 | exec_cycles = (long long)(TIMESPEC2USEC(&l->exec[l->act_current-1])) * CALIBRATION_DELTA / cal_cycles; |
||
1304 | giacomo | 110 | block_cycles = (long long)(TIMESPEC2USEC(&l->block[l->act_current-1])) * CALIBRATION_DELTA / cal_cycles; |
111 | |||
1254 | giacomo | 112 | /* Execution delay */ |
113 | for (i=0;i<exec_cycles;i++) |
||
114 | __asm__ __volatile__ ("xorl %%eax,%%eax\n\t" |
||
115 | "cpuid\n\t" |
||
116 | :::"eax","ebx","ecx","edx"); |
||
1304 | giacomo | 117 | if (l->muxstatus == 2) { |
118 | |||
119 | #ifdef TASK_OUTPUT |
||
120 | #ifdef OS_SHARK |
||
121 | sprintf(tmp,"C[LOCK%02d]",l->resource); |
||
122 | printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task() / 5 + 5, RED, tmp); |
||
123 | #endif |
||
124 | #endif |
||
125 | |||
126 | generic_lock_mutex(l->resource); |
||
127 | for (i=0;i<block_cycles;i++) |
||
128 | __asm__ __volatile__ ("xorl %%eax,%%eax\n\t" |
||
129 | "cpuid\n\t" |
||
130 | :::"eax","ebx","ecx","edx"); |
||
131 | generic_unlock_mutex(l->resource); |
||
132 | |||
133 | #ifdef TASK_OUTPUT |
||
134 | #ifdef OS_SHARK |
||
135 | sprintf(tmp,"C[FREE%02d]",l->resource); |
||
136 | printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task() / 5 + 5, GREEN, tmp); |
||
137 | #endif |
||
138 | #endif |
||
139 | |||
140 | } |
||
141 | |||
1264 | giacomo | 142 | end_job_periodic_task(); |
143 | |||
1254 | giacomo | 144 | generic_task_endcycle(); |
145 | |||
146 | act++; |
||
147 | |||
148 | } |
||
1264 | giacomo | 149 | |
150 | end_periodic_task(); |
||
1266 | giacomo | 151 | |
152 | #ifdef TASK_OUTPUT |
||
153 | #ifdef OS_SHARK |
||
154 | sprintf(tmp,"[--END--]"); |
||
155 | printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task() / 5 + 5, GREEN, tmp); |
||
156 | #endif |
||
157 | #endif |
||
1254 | giacomo | 158 | |
159 | return NULL; |
||
160 | |||
161 | } |
||
162 | |||
163 | /* BT: Background Task: |
||
164 | begin |
||
165 | while (1) { |
||
166 | - execution |
||
167 | } |
||
168 | end (never end) |
||
169 | */ |
||
170 | void *back_task(void *arg) |
||
171 | { |
||
1304 | giacomo | 172 | long long i,exec_cycles = 0,block_cycles = 0; |
1254 | giacomo | 173 | int act = 0; |
174 | struct loader_task *l = (struct loader_task *)(arg); |
||
1262 | giacomo | 175 | #ifdef TASK_OUTPUT |
176 | #ifdef OS_SHARK |
||
177 | char tmp[20]; |
||
178 | #endif |
||
179 | #endif |
||
1254 | giacomo | 180 | |
1264 | giacomo | 181 | start_back_task(); |
182 | |||
1254 | giacomo | 183 | if (l->act_current == 0) l->act_current = 1; |
184 | |||
185 | while(1) { |
||
186 | |||
1264 | giacomo | 187 | start_job_back_task(); |
188 | |||
1254 | giacomo | 189 | #ifdef TASK_OUTPUT |
1262 | giacomo | 190 | #ifdef OS_SHARK |
191 | sprintf(tmp,"B[%06d]",act); |
||
192 | printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task() / 5 + 5, GREEN, tmp); |
||
193 | #endif |
||
1254 | giacomo | 194 | #endif |
195 | |||
196 | exec_cycles = (long long)(TIMESPEC2USEC(&l->exec[l->act_current-1])) * CALIBRATION_DELTA / cal_cycles; |
||
1304 | giacomo | 197 | block_cycles = (long long)(TIMESPEC2USEC(&l->block[l->act_current-1])) * CALIBRATION_DELTA / cal_cycles; |
198 | |||
1254 | giacomo | 199 | /* Execution delay */ |
200 | for (i=0;i<exec_cycles;i++) |
||
201 | __asm__ __volatile__ ("xorl %%eax,%%eax\n\t" |
||
202 | "cpuid\n\t" |
||
203 | :::"eax","ebx","ecx","edx"); |
||
1304 | giacomo | 204 | if (l->muxstatus == 2) { |
205 | |||
206 | #ifdef TASK_OUTPUT |
||
207 | #ifdef OS_SHARK |
||
208 | sprintf(tmp,"B[LOCK%02d]",l->resource); |
||
209 | printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task() / 5 + 5, RED, tmp); |
||
210 | #endif |
||
211 | #endif |
||
212 | |||
213 | generic_lock_mutex(l->resource); |
||
214 | for (i=0;i<block_cycles;i++) |
||
215 | __asm__ __volatile__ ("xorl %%eax,%%eax\n\t" |
||
216 | "cpuid\n\t" |
||
217 | :::"eax","ebx","ecx","edx"); |
||
218 | generic_unlock_mutex(l->resource); |
||
219 | |||
220 | #ifdef TASK_OUTPUT |
||
221 | #ifdef OS_SHARK |
||
222 | sprintf(tmp,"C[FREE%02d]",l->resource); |
||
223 | printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task() / 5 + 5, GREEN, tmp); |
||
224 | #endif |
||
225 | #endif |
||
226 | |||
227 | } |
||
228 | |||
1264 | giacomo | 229 | end_job_back_task(); |
230 | |||
1254 | giacomo | 231 | act++; |
232 | |||
1264 | giacomo | 233 | } |
234 | |||
235 | end_back_task(); |
||
1266 | giacomo | 236 | |
237 | #ifdef TASK_OUTPUT |
||
238 | #ifdef OS_SHARK |
||
239 | sprintf(tmp,"[--END--]"); |
||
240 | printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task() / 5 + 5, GREEN, tmp); |
||
241 | #endif |
||
242 | #endif |
||
243 | |||
1254 | giacomo | 244 | return NULL; |
245 | |||
246 | } |
||
247 | |||
248 | /* Task create */ |
||
249 | /* this function create the task struct in memory */ |
||
250 | void loader_task_create() |
||
251 | { |
||
252 | |||
253 | struct loader_task *current = loader_task_list; |
||
254 | int i=0, k=0; |
||
255 | |||
256 | while (k <total_loader_task) { |
||
257 | k++; |
||
258 | |||
259 | for (i=0; i < current->number; i++) { |
||
260 | |||
261 | pthread_t j; |
||
262 | int err = 0; |
||
263 | |||
264 | switch(current->task_type) { |
||
265 | case PAR_TASK_OS: |
||
266 | err = generic_create_thread(generic_get_server_from_contract(current->contract),&j,NULL, |
||
267 | oneshot_task,(void *)current,generic_get_task_model(current)); |
||
268 | break; |
||
269 | case PAR_TASK_BT: |
||
270 | err = generic_create_thread(generic_get_server_from_contract(current->contract),&j,NULL, |
||
271 | back_task,(void *)current,generic_get_task_model(current)); |
||
272 | break; |
||
273 | case PAR_TASK_CT: |
||
274 | err = generic_create_thread(generic_get_server_from_contract(current->contract),&j,NULL, |
||
275 | periodic_task,(void *)current,generic_get_task_model(current)); |
||
276 | break; |
||
277 | } |
||
278 | if (err) { |
||
1540 | trimarchi | 279 | printf("Error fsf task creating %d\n", err); |
1254 | giacomo | 280 | generic_end_simulation(); |
281 | } |
||
282 | |||
283 | } |
||
284 | |||
285 | current = &loader_task_list[k]; |
||
286 | |||
287 | } |
||
288 | |||
289 | printf("Created %d loader tasks\n",k); |
||
290 | |||
291 | |||
292 | } |
||
293 | |||
294 | /* Main Function */ |
||
295 | int start_environment() |
||
296 | { |
||
297 | |||
298 | extern struct timespec total_time; |
||
299 | |||
300 | /* Calibrate the exec time */ |
||
301 | generic_calibrate_cycle(); |
||
302 | |||
303 | /* Create the servers usign defined contracts */ |
||
304 | generic_fsfinit(); |
||
305 | |||
306 | /* Create the tasks */ |
||
307 | loader_task_create(); |
||
308 | |||
309 | /* Start the simulation */ |
||
310 | generic_start_simulation(); |
||
311 | |||
312 | /* Set the simulation end time */ |
||
313 | generic_set_simulation_time(&total_time); |
||
314 | |||
315 | return 0; |
||
316 | |||
317 | } |