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