Rev 298 | Rev 338 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
221 | giacomo | 1 | //===================================================================== |
2 | // FFFFFFIII RRRRR SSTTTTTTT |
||
3 | // FF IIR RR SS |
||
4 | // FF IR SS |
||
5 | // FFFFFF RRRR SSSSST |
||
6 | // FF FI RRR SS |
||
7 | // FF II RRR SS |
||
8 | // FF IIIIIR RS |
||
9 | // |
||
10 | // Basic FSF(FIRST Scheduling Framework) contract management |
||
11 | // S.Ha.R.K. Implementation |
||
12 | //===================================================================== |
||
13 | |||
14 | #include "fsf_contract.h" |
||
241 | giacomo | 15 | #include "fsf_server.h" |
221 | giacomo | 16 | |
17 | #include <pthread.h> |
||
18 | #include <stdlib.h> |
||
19 | |||
224 | giacomo | 20 | //#define FSF_DEBUG |
221 | giacomo | 21 | |
241 | giacomo | 22 | int fsf_server_level; |
221 | giacomo | 23 | |
241 | giacomo | 24 | int FSF_register_module(int server_level) |
224 | giacomo | 25 | { |
26 | |||
27 | printk("FSF Module\n"); |
||
28 | |||
241 | giacomo | 29 | fsf_server_level = server_level; |
224 | giacomo | 30 | |
31 | return 0; |
||
32 | |||
33 | } |
||
34 | |||
221 | giacomo | 35 | /* Convert the contract specification to |
36 | * budget parameters |
||
37 | */ |
||
241 | giacomo | 38 | int set_SERVER_budget_from_contract |
221 | giacomo | 39 | (const fsf_contract_parameters_t *contract, |
40 | int *budget) |
||
41 | { |
||
42 | |||
43 | int local_scheduler_level = 0; |
||
44 | |||
45 | switch (contract->local_scheduler_id) { |
||
46 | case FSF_SCHEDULER_POSIX: |
||
241 | giacomo | 47 | local_scheduler_level = POSIXSTAR_register_level(fsf_server_level,5000,32); |
221 | giacomo | 48 | break; |
49 | case FSF_SCHEDULER_EDF: |
||
241 | giacomo | 50 | local_scheduler_level = EDFSTAR_register_level(fsf_server_level); |
221 | giacomo | 51 | break; |
52 | case FSF_SCHEDULER_RM: |
||
241 | giacomo | 53 | local_scheduler_level = RMSTAR_register_level(fsf_server_level); |
221 | giacomo | 54 | break; |
334 | giacomo | 55 | case FSF_SCHEDULER_MPEG: |
56 | local_scheduler_level = MPEGSTAR_register_level(fsf_server_level); |
||
57 | break; |
||
221 | giacomo | 58 | } |
59 | |||
241 | giacomo | 60 | *budget = SERVER_setbudget(fsf_server_level, |
221 | giacomo | 61 | TIMESPEC2USEC(&(contract->budget_min)), |
62 | TIMESPEC2USEC(&(contract->period_max)), |
||
63 | local_scheduler_level,contract->local_scheduler_id); |
||
64 | |||
65 | return 0; |
||
66 | |||
67 | } |
||
68 | |||
241 | giacomo | 69 | int adjust_SERVER_budget_from_contract |
221 | giacomo | 70 | (const fsf_contract_parameters_t *contract, |
71 | int budget) |
||
72 | { |
||
73 | |||
241 | giacomo | 74 | SERVER_adjust_budget(fsf_server_level, |
75 | TIMESPEC2USEC(&(contract->budget_min)), |
||
76 | TIMESPEC2USEC(&(contract->period_max)), |
||
77 | budget); |
||
221 | giacomo | 78 | |
79 | return 0; |
||
80 | |||
81 | } |
||
82 | |||
83 | /* Admission Test function */ |
||
84 | int add_contract(const fsf_contract_parameters_t *contract) |
||
85 | { |
||
86 | |||
87 | return 0; |
||
88 | |||
89 | } |
||
90 | |||
91 | int link_contract_to_server(const fsf_contract_parameters_t *contract, |
||
92 | fsf_server_id_t server) |
||
93 | { |
||
94 | |||
95 | return 0; |
||
96 | |||
97 | } |
||
98 | |||
99 | int remove_contract(fsf_server_id_t server) |
||
100 | { |
||
101 | |||
102 | return 0; |
||
103 | |||
104 | } |
||
105 | |||
106 | |||
107 | int fsf_negotiate_contract |
||
108 | (const fsf_contract_parameters_t *contract, |
||
109 | fsf_server_id_t *server) |
||
110 | { |
||
111 | |||
112 | /* Check if contract is initialized */ |
||
113 | if (!contract) return FSF_ERR_NOT_INITIALIZED; |
||
114 | |||
115 | /* Admission Test */ |
||
116 | if (FSF_ADMISSION_TEST_IS_ENABLED) |
||
117 | if (add_contract(contract)) |
||
118 | return FSF_ERR_CONTRACT_REJECTED; |
||
119 | |||
241 | giacomo | 120 | /* SERVER => BUDGET */ |
121 | set_SERVER_budget_from_contract(contract,server); |
||
221 | giacomo | 122 | |
123 | #ifdef FSF_DEBUG |
||
124 | kern_printf("(New Server %d)",*server); |
||
125 | #endif |
||
126 | |||
127 | if (*server >= 0) |
||
128 | link_contract_to_server(contract,*server); |
||
129 | else |
||
130 | return FSF_ERR_CREATE_SERVER; |
||
131 | |||
132 | return 0; |
||
133 | |||
134 | } |
||
135 | |||
241 | giacomo | 136 | int fsf_create_thread |
137 | (fsf_server_id_t server, |
||
138 | pthread_t *thread, |
||
139 | pthread_attr_t *attr, |
||
140 | fsf_thread_code_t thread_code, |
||
141 | void *arg, |
||
142 | void *local_scheduler_arg) |
||
221 | giacomo | 143 | { |
144 | |||
145 | int local_scheduler_level,scheduler_id; |
||
146 | |||
147 | #ifdef FSF_DEBUG |
||
267 | giacomo | 148 | kern_printf("(FSF:Insert thread = %d to Server = %d)",*thread,server); |
221 | giacomo | 149 | #endif |
150 | |||
151 | /* Check if server and thread exsist */ |
||
241 | giacomo | 152 | if (server == NIL) |
153 | return FSF_ERR_INVALID_SERVER; |
||
221 | giacomo | 154 | |
241 | giacomo | 155 | local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server); |
156 | scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server); |
||
221 | giacomo | 157 | |
158 | /* Check if thread is already bind */ |
||
159 | switch (scheduler_id) { |
||
160 | case FSF_SCHEDULER_POSIX: |
||
161 | |||
298 | giacomo | 162 | nrt_task_def_arg(*(NRT_TASK_MODEL *)(local_scheduler_arg),arg); |
163 | nrt_task_def_level(*(NRT_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
221 | giacomo | 164 | |
298 | giacomo | 165 | *thread = task_create("POSIXSTAR", thread_code, local_scheduler_arg, NULL); |
267 | giacomo | 166 | if (*thread == NIL) { |
167 | #ifdef FSF_DEBUG |
||
168 | kern_printf("(FSF:Error creating thread)"); |
||
169 | #endif |
||
241 | giacomo | 170 | return FSF_ERR_CREATE_THREAD; |
267 | giacomo | 171 | } |
221 | giacomo | 172 | |
241 | giacomo | 173 | POSIXSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
234 | giacomo | 174 | |
241 | giacomo | 175 | task_activate(*thread); |
221 | giacomo | 176 | |
177 | break; |
||
178 | case FSF_SCHEDULER_EDF: |
||
179 | |||
250 | giacomo | 180 | hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg); |
181 | hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
182 | |||
183 | *thread = task_create("EDFSTAR", thread_code, local_scheduler_arg, NULL); |
||
184 | if (*thread == NIL) |
||
185 | return FSF_ERR_CREATE_THREAD; |
||
186 | |||
187 | EDFSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
||
188 | |||
189 | task_activate(*thread); |
||
190 | |||
221 | giacomo | 191 | break; |
235 | giacomo | 192 | |
221 | giacomo | 193 | case FSF_SCHEDULER_RM: |
235 | giacomo | 194 | |
273 | giacomo | 195 | hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg); |
196 | hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
197 | |||
198 | *thread = task_create("RMSTAR", thread_code, local_scheduler_arg, NULL); |
||
199 | if (*thread == NIL) |
||
200 | return FSF_ERR_CREATE_THREAD; |
||
201 | |||
202 | RMSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
||
203 | |||
204 | task_activate(*thread); |
||
205 | |||
206 | break; |
||
334 | giacomo | 207 | case FSF_SCHEDULER_MPEG: |
208 | |||
209 | hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg); |
||
210 | hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
211 | |||
212 | *thread = task_create("RMSTAR", thread_code, local_scheduler_arg, NULL); |
||
213 | if (*thread == NIL) |
||
214 | return FSF_ERR_CREATE_THREAD; |
||
215 | |||
216 | MPEGSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
||
217 | |||
218 | break; |
||
219 | |||
221 | giacomo | 220 | default: |
241 | giacomo | 221 | return FSF_ERR_INVALID_SERVER; |
221 | giacomo | 222 | break; |
223 | } |
||
224 | |||
225 | return 0; |
||
226 | |||
227 | } |
||
228 | |||
229 | int fsf_get_server |
||
230 | (fsf_server_id_t *server, |
||
231 | pthread_t thread) |
||
232 | { |
||
233 | int local_scheduler_level, scheduler_id; |
||
234 | |||
241 | giacomo | 235 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
236 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread); |
||
221 | giacomo | 237 | |
238 | switch (scheduler_id) { |
||
239 | case FSF_SCHEDULER_POSIX: |
||
240 | return POSIXSTAR_getbudget(local_scheduler_level,thread); |
||
241 | case FSF_SCHEDULER_EDF: |
||
235 | giacomo | 242 | return EDFSTAR_getbudget(local_scheduler_level,thread); |
221 | giacomo | 243 | case FSF_SCHEDULER_RM: |
235 | giacomo | 244 | return RMSTAR_getbudget(local_scheduler_level,thread); |
334 | giacomo | 245 | case FSF_SCHEDULER_MPEG: |
246 | return MPEGSTAR_getbudget(local_scheduler_level,thread); |
||
235 | giacomo | 247 | default: |
221 | giacomo | 248 | return -1; |
249 | } |
||
250 | |||
251 | return -1; |
||
252 | |||
253 | } |
||
254 | |||
255 | int fsf_cancel_contract |
||
256 | (fsf_server_id_t *server) |
||
257 | { |
||
258 | |||
259 | int local_scheduler_level, scheduler_id; |
||
260 | |||
261 | #ifdef FSF_DEBUG |
||
262 | kern_printf("(Remove server %d)",*server); |
||
263 | #endif |
||
264 | |||
265 | /* Check server id */ |
||
266 | if (*server < 0) |
||
267 | return FSF_ERR_INVALID_SERVER; |
||
268 | |||
241 | giacomo | 269 | local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,*server); |
270 | scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,*server); |
||
221 | giacomo | 271 | |
272 | switch (scheduler_id) { |
||
273 | case FSF_SCHEDULER_POSIX: |
||
274 | |||
275 | /* Check if some thread use the server */ |
||
276 | if(POSIXSTAR_budget_has_thread(local_scheduler_level,*server)) |
||
277 | return FSF_ERR_SERVER_USED; |
||
278 | |||
279 | break; |
||
280 | case FSF_SCHEDULER_EDF: |
||
235 | giacomo | 281 | /* Check if some thread use the server */ |
282 | if(EDFSTAR_budget_has_thread(local_scheduler_level,*server)) |
||
283 | return FSF_ERR_SERVER_USED; |
||
284 | break; |
||
285 | |||
221 | giacomo | 286 | case FSF_SCHEDULER_RM: |
235 | giacomo | 287 | /* Check if some thread use the server */ |
288 | if(RMSTAR_budget_has_thread(local_scheduler_level,*server)) |
||
289 | return FSF_ERR_SERVER_USED; |
||
290 | |||
221 | giacomo | 291 | break; |
334 | giacomo | 292 | |
293 | case FSF_SCHEDULER_MPEG: |
||
294 | /* Check if some thread use the server */ |
||
295 | if(MPEGSTAR_budget_has_thread(local_scheduler_level,*server)) |
||
296 | return FSF_ERR_SERVER_USED; |
||
297 | |||
298 | break; |
||
299 | |||
221 | giacomo | 300 | } |
301 | |||
241 | giacomo | 302 | SERVER_removebudget(fsf_server_level,*server); |
221 | giacomo | 303 | |
304 | level_free_descriptor(local_scheduler_level); |
||
305 | |||
306 | remove_contract(*server); |
||
307 | |||
308 | *server = -1; |
||
309 | |||
310 | return 0; |
||
311 | |||
312 | } |
||
313 | |||
314 | int fsf_renegotiate_contract |
||
315 | (const fsf_contract_parameters_t *new_contract, |
||
316 | fsf_server_id_t server) |
||
317 | { |
||
318 | |||
319 | #ifdef FSF_DEBUG |
||
320 | kern_printf("(Renegotiate for server %d)",server); |
||
321 | #endif |
||
322 | |||
323 | if (!new_contract) |
||
324 | return FSF_ERR_NOT_INITIALIZED; |
||
325 | |||
326 | if (server < 0) |
||
327 | return FSF_ERR_INVALID_SERVER; |
||
328 | |||
241 | giacomo | 329 | return adjust_SERVER_budget_from_contract(new_contract,server); |
221 | giacomo | 330 | |
331 | } |
||
241 | giacomo | 332 |