Details | 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 | |||
12 | #include "fsf_contract.h" //Framework main header |
||
13 | #include "calibrate.h" |
||
14 | #include "func.h" //Generic function definitions |
||
15 | |||
16 | /* Activate task output debug */ |
||
17 | #define TASK_OUTPUT |
||
18 | |||
19 | int cal_cycles = CALIBRATING_RESULT; //Calibration const, it converts usec to cycles |
||
20 | struct timespec zero_time; //Zero time of the simulation |
||
21 | extern struct loader_task loader_task_list[]; //Loader task array |
||
22 | extern int total_loader_task; //Loader task number |
||
23 | |||
24 | /* OS: Oneshot Task: |
||
25 | begin |
||
26 | - execution |
||
27 | end |
||
28 | */ |
||
29 | void *oneshot_task(void *arg) |
||
30 | { |
||
31 | long long i,exec_cycles = 0; |
||
32 | struct loader_task *l = (struct loader_task *)(arg); |
||
33 | char tmp[20]; |
||
34 | |||
35 | if (l->act_current == 0) l->act_current = 1; |
||
36 | #ifdef TASK_OUTPUT |
||
37 | sprintf(tmp,"[ONESHOT]"); |
||
38 | printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task() / 5 + 5, GREEN, tmp); |
||
39 | #endif |
||
40 | |||
41 | exec_cycles = (long long)(TIMESPEC2USEC(&l->exec[l->act_current-1])) * CALIBRATION_DELTA / cal_cycles; |
||
42 | |||
43 | /* Execution delay */ |
||
44 | for (i=0;i<exec_cycles;i++) |
||
45 | __asm__ __volatile__ ("xorl %%eax,%%eax\n\t" |
||
46 | "cpuid\n\t" |
||
47 | :::"eax","ebx","ecx","edx"); |
||
48 | |||
49 | return NULL; |
||
50 | |||
51 | } |
||
52 | |||
53 | /* CT: Cyclical Task: |
||
54 | begin |
||
55 | while (1) { |
||
56 | - execution |
||
57 | - end_cycle |
||
58 | } |
||
59 | end (never end) |
||
60 | */ |
||
61 | void *periodic_task(void *arg) |
||
62 | { |
||
63 | long long i,exec_cycles = 0; |
||
64 | int act = 0; |
||
65 | struct loader_task *l = (struct loader_task *)(arg); |
||
66 | char tmp[20]; |
||
67 | |||
68 | if (l->act_current == 0) l->act_current = 1; |
||
69 | |||
70 | while(1) { |
||
71 | |||
72 | #ifdef TASK_OUTPUT |
||
73 | sprintf(tmp,"C[%06d]",act); |
||
74 | printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task() / 5 + 5, GREEN, tmp); |
||
75 | #endif |
||
76 | |||
77 | exec_cycles = (long long)(TIMESPEC2USEC(&l->exec[l->act_current-1])) * CALIBRATION_DELTA / cal_cycles; |
||
78 | |||
79 | /* Execution delay */ |
||
80 | for (i=0;i<exec_cycles;i++) |
||
81 | __asm__ __volatile__ ("xorl %%eax,%%eax\n\t" |
||
82 | "cpuid\n\t" |
||
83 | :::"eax","ebx","ecx","edx"); |
||
84 | |||
85 | generic_task_endcycle(); |
||
86 | |||
87 | act++; |
||
88 | |||
89 | } |
||
90 | |||
91 | return NULL; |
||
92 | |||
93 | } |
||
94 | |||
95 | /* BT: Background Task: |
||
96 | begin |
||
97 | while (1) { |
||
98 | - execution |
||
99 | } |
||
100 | end (never end) |
||
101 | */ |
||
102 | void *back_task(void *arg) |
||
103 | { |
||
104 | long long i,exec_cycles = 0; |
||
105 | int act = 0; |
||
106 | struct loader_task *l = (struct loader_task *)(arg); |
||
107 | char tmp[20]; |
||
108 | |||
109 | if (l->act_current == 0) l->act_current = 1; |
||
110 | |||
111 | while(1) { |
||
112 | |||
113 | #ifdef TASK_OUTPUT |
||
114 | sprintf(tmp,"B[%06d]",act); |
||
115 | printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task() / 5 + 5, GREEN, tmp); |
||
116 | #endif |
||
117 | |||
118 | exec_cycles = (long long)(TIMESPEC2USEC(&l->exec[l->act_current-1])) * CALIBRATION_DELTA / cal_cycles; |
||
119 | |||
120 | /* Execution delay */ |
||
121 | for (i=0;i<exec_cycles;i++) |
||
122 | __asm__ __volatile__ ("xorl %%eax,%%eax\n\t" |
||
123 | "cpuid\n\t" |
||
124 | :::"eax","ebx","ecx","edx"); |
||
125 | |||
126 | act++; |
||
127 | |||
128 | } |
||
129 | return NULL; |
||
130 | |||
131 | } |
||
132 | |||
133 | /* Task create */ |
||
134 | /* this function create the task struct in memory */ |
||
135 | void loader_task_create() |
||
136 | { |
||
137 | |||
138 | struct loader_task *current = loader_task_list; |
||
139 | int i=0, k=0; |
||
140 | |||
141 | while (k <total_loader_task) { |
||
142 | k++; |
||
143 | |||
144 | for (i=0; i < current->number; i++) { |
||
145 | |||
146 | pthread_t j; |
||
147 | int err = 0; |
||
148 | |||
149 | switch(current->task_type) { |
||
150 | case PAR_TASK_OS: |
||
151 | |||
152 | /* generic_task_create( |
||
153 | server number, |
||
154 | pthread_t of created task (-1 if failed), |
||
155 | pthread_attr_t, |
||
156 | task_body, |
||
157 | arg of the body (must be "(void *)current"), |
||
158 | arg for real-time task specification) |
||
159 | */ |
||
160 | |||
161 | err = generic_create_thread(generic_get_server_from_contract(current->contract),&j,NULL, |
||
162 | oneshot_task,(void *)current,generic_get_task_model(current)); |
||
163 | break; |
||
164 | case PAR_TASK_BT: |
||
165 | err = generic_create_thread(generic_get_server_from_contract(current->contract),&j,NULL, |
||
166 | back_task,(void *)current,generic_get_task_model(current)); |
||
167 | break; |
||
168 | case PAR_TASK_CT: |
||
169 | err = generic_create_thread(generic_get_server_from_contract(current->contract),&j,NULL, |
||
170 | periodic_task,(void *)current,generic_get_task_model(current)); |
||
171 | break; |
||
172 | } |
||
173 | if (err) { |
||
174 | printf("Error fsf task creating\n"); |
||
175 | generic_end_simulation(); |
||
176 | } |
||
177 | |||
178 | } |
||
179 | |||
180 | current = &loader_task_list[k]; |
||
181 | |||
182 | } |
||
183 | |||
184 | printf("Created %d loader tasks\n",k); |
||
185 | |||
186 | |||
187 | } |
||
188 | |||
189 | /* Main Function */ |
||
190 | int start_environment() |
||
191 | { |
||
192 | |||
193 | extern struct timespec total_time; |
||
194 | |||
195 | /* Calibrate the exec time */ |
||
196 | generic_calibrate_cycle(); |
||
197 | |||
198 | /* Create the servers usign defined contracts */ |
||
199 | generic_fsfinit(); |
||
200 | |||
201 | /* Create the tasks */ |
||
202 | loader_task_create(); |
||
203 | |||
204 | /* Start the simulation */ |
||
205 | generic_start_simulation(); |
||
206 | |||
207 | /* Set the simulation end time */ |
||
208 | generic_set_simulation_time(&total_time); |
||
209 | |||
210 | return 0; |
||
211 | |||
212 | } |