Rev 816 | Rev 824 | 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 | |||
405 | trimarchi | 14 | #include "ll/i386/64bit.h" |
679 | trimarchi | 15 | #include <kernel/kern.h> |
16 | #include <modules/sem.h> |
||
17 | #include <modules/hartport.h> |
||
18 | #include <modules/cabs.h> |
||
405 | trimarchi | 19 | |
221 | giacomo | 20 | #include "fsf_contract.h" |
241 | giacomo | 21 | #include "fsf_server.h" |
679 | trimarchi | 22 | #include "fsf_service_task.h" |
23 | #include "message.h" |
||
221 | giacomo | 24 | |
25 | #include <pthread.h> |
||
26 | #include <stdlib.h> |
||
808 | trimarchi | 27 | #include "pistar.h" |
221 | giacomo | 28 | |
679 | trimarchi | 29 | |
417 | giacomo | 30 | //#define FSF_DEBUG |
221 | giacomo | 31 | |
410 | trimarchi | 32 | int current_server=0; |
407 | giacomo | 33 | server_elem server_list[FSF_MAX_N_SERVERS]; |
406 | giacomo | 34 | bandwidth_t fsf_max_bw = 0; |
405 | trimarchi | 35 | |
241 | giacomo | 36 | int fsf_server_level; |
808 | trimarchi | 37 | int shared_object_level; |
38 | |||
679 | trimarchi | 39 | PID server_task; |
221 | giacomo | 40 | |
679 | trimarchi | 41 | PORT channel[2]; |
42 | |||
808 | trimarchi | 43 | int FSF_register_shared_object_module(void) { |
811 | trimarchi | 44 | fsf_register_shared_object(); |
808 | trimarchi | 45 | return PISTAR_register_module(); |
46 | } |
||
47 | |||
48 | |||
810 | trimarchi | 49 | int FSF_get_shared_object_level() { |
50 | return shared_object_level; |
||
51 | } |
||
52 | |||
406 | giacomo | 53 | int FSF_register_module(int server_level, bandwidth_t max_bw) |
224 | giacomo | 54 | { |
55 | printk("FSF Module\n"); |
||
410 | trimarchi | 56 | current_server=0; |
241 | giacomo | 57 | fsf_server_level = server_level; |
406 | giacomo | 58 | fsf_max_bw = max_bw; |
811 | trimarchi | 59 | shared_object_level = FSF_register_shared_object_module(); |
679 | trimarchi | 60 | |
224 | giacomo | 61 | return 0; |
62 | |||
63 | } |
||
64 | |||
679 | trimarchi | 65 | void FSF_start_service_task(SOFT_TASK_MODEL *model) { |
66 | |||
67 | // create the service task |
||
68 | // create the communication channel for negotiation and renegotiation |
||
69 | |||
70 | channel[1] = port_create("CHANW",sizeof(struct mess),1,STREAM,WRITE); |
||
71 | |||
72 | channel[0] = port_create("CHANR",sizeof(struct mess),1,STREAM,READ); |
||
73 | |||
74 | server_task = task_create("stask",service_task,model,NULL); |
||
75 | if (server_task == -1) { |
||
76 | cprintf("error creating service task\n"); |
||
77 | sys_shutdown_message("Could not create service_task"); |
||
78 | sys_end(); |
||
79 | return; |
||
80 | } |
||
81 | |||
82 | task_activate(server_task); |
||
83 | |||
84 | } |
||
85 | |||
86 | |||
221 | giacomo | 87 | /* Convert the contract specification to |
88 | * budget parameters |
||
89 | */ |
||
241 | giacomo | 90 | int set_SERVER_budget_from_contract |
221 | giacomo | 91 | (const fsf_contract_parameters_t *contract, |
92 | int *budget) |
||
93 | { |
||
94 | |||
95 | int local_scheduler_level = 0; |
||
96 | |||
97 | switch (contract->local_scheduler_id) { |
||
98 | case FSF_SCHEDULER_POSIX: |
||
241 | giacomo | 99 | local_scheduler_level = POSIXSTAR_register_level(fsf_server_level,5000,32); |
221 | giacomo | 100 | break; |
101 | case FSF_SCHEDULER_EDF: |
||
241 | giacomo | 102 | local_scheduler_level = EDFSTAR_register_level(fsf_server_level); |
221 | giacomo | 103 | break; |
104 | case FSF_SCHEDULER_RM: |
||
241 | giacomo | 105 | local_scheduler_level = RMSTAR_register_level(fsf_server_level); |
221 | giacomo | 106 | break; |
334 | giacomo | 107 | case FSF_SCHEDULER_MPEG: |
108 | local_scheduler_level = MPEGSTAR_register_level(fsf_server_level); |
||
109 | break; |
||
221 | giacomo | 110 | } |
405 | trimarchi | 111 | |
661 | giacomo | 112 | if (contract->d_equals_t == TRUE) { |
241 | giacomo | 113 | *budget = SERVER_setbudget(fsf_server_level, |
221 | giacomo | 114 | TIMESPEC2USEC(&(contract->budget_min)), |
115 | TIMESPEC2USEC(&(contract->period_max)), |
||
661 | giacomo | 116 | TIMESPEC2USEC(&(contract->period_max)), |
221 | giacomo | 117 | local_scheduler_level,contract->local_scheduler_id); |
661 | giacomo | 118 | } else { |
119 | *budget = SERVER_setbudget(fsf_server_level, |
||
120 | TIMESPEC2USEC(&(contract->budget_min)), |
||
121 | TIMESPEC2USEC(&(contract->period_max)), |
||
122 | TIMESPEC2USEC(&(contract->deadline)), |
||
123 | local_scheduler_level,contract->local_scheduler_id); |
||
124 | } |
||
221 | giacomo | 125 | |
126 | return 0; |
||
127 | |||
128 | } |
||
129 | |||
405 | trimarchi | 130 | int adjust_SERVER_budget |
131 | (int budget, const TIME budget_actual, |
||
661 | giacomo | 132 | const TIME period_actual, const TIME dline_actual) |
221 | giacomo | 133 | { |
134 | |||
241 | giacomo | 135 | SERVER_adjust_budget(fsf_server_level, |
405 | trimarchi | 136 | budget_actual, |
137 | period_actual, |
||
661 | giacomo | 138 | dline_actual, |
241 | giacomo | 139 | budget); |
221 | giacomo | 140 | |
141 | return 0; |
||
142 | |||
143 | } |
||
144 | |||
145 | /* Admission Test function */ |
||
146 | int add_contract(const fsf_contract_parameters_t *contract) |
||
147 | { |
||
148 | return 0; |
||
149 | |||
150 | } |
||
151 | |||
687 | trimarchi | 152 | void contract_to_server(const fsf_contract_parameters_t *contract, int i) { |
153 | |||
410 | trimarchi | 154 | TIME T,Q; |
155 | #ifdef FSF_DEBUG |
||
156 | int temp; |
||
157 | #endif |
||
158 | |||
159 | T=TIMESPEC2USEC(&contract->period_min); |
||
160 | Q=TIMESPEC2USEC(&contract->budget_max); |
||
161 | mul32div32to32(MAX_BANDWIDTH,Q,T,server_list[current_server].Umax); |
||
162 | |||
417 | giacomo | 163 | T=TIMESPEC2USEC(&contract->period_min); |
164 | server_list[i].Tmin=T; |
||
165 | |||
410 | trimarchi | 166 | T=TIMESPEC2USEC(&contract->period_max); |
167 | server_list[i].Tmax=T; |
||
417 | giacomo | 168 | |
410 | trimarchi | 169 | Q=TIMESPEC2USEC(&contract->budget_min); |
170 | server_list[i].Cmin=Q; |
||
171 | |||
172 | mul32div32to32(MAX_BANDWIDTH,Q,T,server_list[i].Umin); |
||
416 | trimarchi | 173 | server_list[i].U=server_list[i].Umin; |
661 | giacomo | 174 | |
175 | if (contract->d_equals_t == TRUE) { |
||
176 | server_list[i].deadline = 0; |
||
177 | server_list[i].d_equals_t = TRUE; |
||
178 | } else { |
||
179 | server_list[i].deadline = TIMESPEC2USEC(&contract->deadline);; |
||
180 | server_list[i].d_equals_t = FALSE; |
||
181 | } |
||
182 | |||
410 | trimarchi | 183 | #ifdef FSF_DEBUG |
184 | mul32div32to32(server_list[i].Umax,100, MAX_BANDWIDTH, temp); |
||
185 | kern_printf("(Umax %d)",temp); |
||
186 | mul32div32to32(server_list[i].Umin,100, MAX_BANDWIDTH, temp); |
||
187 | kern_printf("(Umin %d)",temp); |
||
188 | #endif |
||
189 | |||
687 | trimarchi | 190 | } |
191 | |||
192 | int relink_contract_to_server(const fsf_contract_parameters_t *contract, |
||
193 | fsf_server_id_t server) |
||
194 | { |
||
195 | int i=0; |
||
196 | #ifdef FSF_DEBUG |
||
197 | kern_printf("(Relink Server %d)",server); |
||
198 | #endif |
||
199 | // find contract |
||
200 | while(i<current_server) { |
||
201 | if (server_list[i].server==server) break; |
||
202 | i++; |
||
203 | } |
||
204 | |||
205 | server_list[i].server=server; |
||
206 | server_list[i].Qs=1; |
||
207 | |||
208 | contract_to_server(contract, i); |
||
209 | |||
210 | |||
410 | trimarchi | 211 | return 0; |
212 | |||
213 | } |
||
214 | |||
221 | giacomo | 215 | int link_contract_to_server(const fsf_contract_parameters_t *contract, |
216 | fsf_server_id_t server) |
||
217 | { |
||
407 | giacomo | 218 | #ifdef FSF_DEBUG |
405 | trimarchi | 219 | kern_printf("(Link Server %d)",server); |
220 | #endif |
||
221 | |||
410 | trimarchi | 222 | server_list[current_server].server=server; |
223 | server_list[current_server].Qs=1; |
||
405 | trimarchi | 224 | |
687 | trimarchi | 225 | contract_to_server(contract,current_server); |
405 | trimarchi | 226 | |
410 | trimarchi | 227 | current_server++; |
221 | giacomo | 228 | return 0; |
229 | |||
230 | } |
||
231 | |||
232 | int remove_contract(fsf_server_id_t server) |
||
233 | { |
||
405 | trimarchi | 234 | int i=0; |
235 | // find the contract |
||
410 | trimarchi | 236 | while(i<current_server) { |
405 | trimarchi | 237 | if (server_list[i].server==server) break; |
238 | i++; |
||
239 | } |
||
221 | giacomo | 240 | |
405 | trimarchi | 241 | // compress the array; |
410 | trimarchi | 242 | while (i<(current_server-1)) { |
405 | trimarchi | 243 | server_list[i].server=server_list[i+1].server; |
244 | server_list[i].Umin=server_list[i+1].Umin; |
||
416 | trimarchi | 245 | server_list[i].U=server_list[i+1].Umin; |
405 | trimarchi | 246 | server_list[i].Umax=server_list[i+1].Umax; |
247 | server_list[i].Cmin=server_list[i+1].Cmin; |
||
417 | giacomo | 248 | server_list[i].Tmin=server_list[i+1].Tmin; |
405 | trimarchi | 249 | server_list[i].Tmax=server_list[i+1].Tmax; |
250 | server_list[i].Qs=server_list[i+1].Qs; |
||
661 | giacomo | 251 | server_list[i].deadline = server_list[i+1].deadline; |
252 | server_list[i].d_equals_t = server_list[i+1].d_equals_t; |
||
417 | giacomo | 253 | |
405 | trimarchi | 254 | i++; |
255 | } |
||
410 | trimarchi | 256 | current_server--; |
405 | trimarchi | 257 | |
258 | |||
221 | giacomo | 259 | return 0; |
260 | |||
261 | } |
||
262 | |||
263 | |||
264 | int fsf_negotiate_contract |
||
265 | (const fsf_contract_parameters_t *contract, |
||
266 | fsf_server_id_t *server) |
||
267 | { |
||
679 | trimarchi | 268 | struct mess message; |
221 | giacomo | 269 | |
679 | trimarchi | 270 | // send response server is -1 if the operation fail |
271 | message.type=NEGOTIATE_CONTRACT; |
||
272 | memmove(&message.contract,contract, sizeof(fsf_contract_parameters_t)); |
||
221 | giacomo | 273 | |
681 | trimarchi | 274 | port_send(channel[1],&message,BLOCK); |
679 | trimarchi | 275 | |
681 | trimarchi | 276 | port_receive(channel[0], &message, BLOCK); |
679 | trimarchi | 277 | |
278 | if (message.server==-1) |
||
279 | return FSF_ERR_CONTRACT_REJECTED; |
||
221 | giacomo | 280 | |
679 | trimarchi | 281 | *server=message.server; |
282 | |||
221 | giacomo | 283 | return 0; |
284 | |||
285 | } |
||
286 | |||
241 | giacomo | 287 | int fsf_create_thread |
288 | (fsf_server_id_t server, |
||
289 | pthread_t *thread, |
||
290 | pthread_attr_t *attr, |
||
291 | fsf_thread_code_t thread_code, |
||
292 | void *arg, |
||
293 | void *local_scheduler_arg) |
||
221 | giacomo | 294 | { |
295 | |||
296 | int local_scheduler_level,scheduler_id; |
||
297 | |||
298 | /* Check if server and thread exsist */ |
||
241 | giacomo | 299 | if (server == NIL) |
300 | return FSF_ERR_INVALID_SERVER; |
||
221 | giacomo | 301 | |
241 | giacomo | 302 | local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server); |
303 | scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server); |
||
221 | giacomo | 304 | |
305 | /* Check if thread is already bind */ |
||
306 | switch (scheduler_id) { |
||
307 | case FSF_SCHEDULER_POSIX: |
||
308 | |||
298 | giacomo | 309 | nrt_task_def_arg(*(NRT_TASK_MODEL *)(local_scheduler_arg),arg); |
310 | nrt_task_def_level(*(NRT_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
221 | giacomo | 311 | |
298 | giacomo | 312 | *thread = task_create("POSIXSTAR", thread_code, local_scheduler_arg, NULL); |
267 | giacomo | 313 | if (*thread == NIL) { |
314 | #ifdef FSF_DEBUG |
||
315 | kern_printf("(FSF:Error creating thread)"); |
||
316 | #endif |
||
241 | giacomo | 317 | return FSF_ERR_CREATE_THREAD; |
267 | giacomo | 318 | } |
221 | giacomo | 319 | |
241 | giacomo | 320 | POSIXSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
234 | giacomo | 321 | |
221 | giacomo | 322 | break; |
323 | case FSF_SCHEDULER_EDF: |
||
324 | |||
250 | giacomo | 325 | hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg); |
326 | hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
327 | |||
328 | *thread = task_create("EDFSTAR", thread_code, local_scheduler_arg, NULL); |
||
329 | if (*thread == NIL) |
||
330 | return FSF_ERR_CREATE_THREAD; |
||
331 | |||
332 | EDFSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
||
333 | |||
221 | giacomo | 334 | break; |
235 | giacomo | 335 | |
221 | giacomo | 336 | case FSF_SCHEDULER_RM: |
235 | giacomo | 337 | |
273 | giacomo | 338 | hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg); |
339 | hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
340 | |||
341 | *thread = task_create("RMSTAR", thread_code, local_scheduler_arg, NULL); |
||
342 | if (*thread == NIL) |
||
343 | return FSF_ERR_CREATE_THREAD; |
||
344 | |||
345 | RMSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
||
346 | |||
347 | break; |
||
334 | giacomo | 348 | case FSF_SCHEDULER_MPEG: |
349 | |||
350 | hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg); |
||
351 | hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
352 | |||
338 | giacomo | 353 | *thread = task_create("MPEGSTAR", thread_code, local_scheduler_arg, NULL); |
334 | giacomo | 354 | if (*thread == NIL) |
355 | return FSF_ERR_CREATE_THREAD; |
||
356 | |||
357 | MPEGSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
||
358 | |||
359 | break; |
||
360 | |||
221 | giacomo | 361 | default: |
241 | giacomo | 362 | return FSF_ERR_INVALID_SERVER; |
221 | giacomo | 363 | break; |
364 | } |
||
811 | trimarchi | 365 | |
366 | #ifdef FSF_DEBUG |
||
367 | kern_printf("(FSF:Insert thread = %d to Server = %d)",*thread,server); |
||
368 | #endif |
||
221 | giacomo | 369 | |
370 | return 0; |
||
371 | |||
372 | } |
||
373 | |||
808 | trimarchi | 374 | int fsf_settask_nopreemptive |
221 | giacomo | 375 | (fsf_server_id_t *server, |
376 | pthread_t thread) |
||
377 | { |
||
378 | int local_scheduler_level, scheduler_id; |
||
379 | |||
241 | giacomo | 380 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
381 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread); |
||
221 | giacomo | 382 | |
383 | switch (scheduler_id) { |
||
808 | trimarchi | 384 | case FSF_SCHEDULER_POSIX: |
816 | trimarchi | 385 | POSIXSTAR_set_nopreemtive_current(local_scheduler_level); |
386 | return 1; |
||
808 | trimarchi | 387 | break; |
388 | case FSF_SCHEDULER_EDF: |
||
389 | EDFSTAR_set_nopreemtive_current(local_scheduler_level); |
||
390 | return 1; |
||
391 | break; |
||
392 | case FSF_SCHEDULER_RM: |
||
812 | trimarchi | 393 | RMSTAR_set_nopreemtive_current(local_scheduler_level); |
394 | return 1; |
||
808 | trimarchi | 395 | break; |
396 | case FSF_SCHEDULER_MPEG: |
||
397 | break; |
||
398 | default: |
||
399 | return -1; |
||
400 | } |
||
401 | return -1; |
||
402 | } |
||
403 | |||
404 | |||
405 | int fsf_settask_preemptive |
||
406 | (fsf_server_id_t *server, |
||
407 | pthread_t thread) |
||
408 | { |
||
409 | int local_scheduler_level, scheduler_id; |
||
410 | |||
411 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
||
412 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread); |
||
413 | |||
414 | switch (scheduler_id) { |
||
415 | case FSF_SCHEDULER_POSIX: |
||
823 | trimarchi | 416 | POSIXSTAR_unset_nopreemtive_current(local_scheduler_level); |
816 | trimarchi | 417 | return 1; |
808 | trimarchi | 418 | break; |
419 | case FSF_SCHEDULER_EDF: |
||
420 | EDFSTAR_unset_nopreemtive_current(local_scheduler_level); |
||
421 | return 1; |
||
422 | break; |
||
423 | case FSF_SCHEDULER_RM: |
||
812 | trimarchi | 424 | RMSTAR_unset_nopreemtive_current(local_scheduler_level); |
425 | return 1; |
||
808 | trimarchi | 426 | break; |
427 | case FSF_SCHEDULER_MPEG: |
||
428 | break; |
||
429 | default: |
||
430 | return -1; |
||
431 | } |
||
432 | |||
433 | return -1; |
||
434 | |||
435 | } |
||
436 | |||
437 | |||
438 | int fsf_get_server |
||
439 | (fsf_server_id_t *server, |
||
440 | pthread_t thread) |
||
441 | { |
||
442 | int local_scheduler_level, scheduler_id; |
||
443 | |||
444 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
||
445 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread); |
||
446 | |||
447 | switch (scheduler_id) { |
||
221 | giacomo | 448 | case FSF_SCHEDULER_POSIX: |
449 | return POSIXSTAR_getbudget(local_scheduler_level,thread); |
||
450 | case FSF_SCHEDULER_EDF: |
||
235 | giacomo | 451 | return EDFSTAR_getbudget(local_scheduler_level,thread); |
221 | giacomo | 452 | case FSF_SCHEDULER_RM: |
235 | giacomo | 453 | return RMSTAR_getbudget(local_scheduler_level,thread); |
334 | giacomo | 454 | case FSF_SCHEDULER_MPEG: |
455 | return MPEGSTAR_getbudget(local_scheduler_level,thread); |
||
235 | giacomo | 456 | default: |
221 | giacomo | 457 | return -1; |
458 | } |
||
459 | |||
460 | return -1; |
||
461 | |||
462 | } |
||
463 | |||
339 | giacomo | 464 | int fsf_get_server_level(void) |
465 | { |
||
466 | |||
467 | return fsf_server_level; |
||
468 | |||
469 | } |
||
470 | |||
221 | giacomo | 471 | int fsf_cancel_contract |
472 | (fsf_server_id_t *server) |
||
473 | { |
||
474 | |||
475 | int local_scheduler_level, scheduler_id; |
||
411 | trimarchi | 476 | SYS_FLAGS f; |
477 | TIME T,Q; |
||
478 | int i=0; |
||
221 | giacomo | 479 | |
480 | #ifdef FSF_DEBUG |
||
481 | kern_printf("(Remove server %d)",*server); |
||
482 | #endif |
||
483 | |||
484 | /* Check server id */ |
||
485 | if (*server < 0) |
||
486 | return FSF_ERR_INVALID_SERVER; |
||
487 | |||
241 | giacomo | 488 | local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,*server); |
489 | scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,*server); |
||
221 | giacomo | 490 | |
491 | switch (scheduler_id) { |
||
492 | case FSF_SCHEDULER_POSIX: |
||
493 | |||
494 | /* Check if some thread use the server */ |
||
495 | if(POSIXSTAR_budget_has_thread(local_scheduler_level,*server)) |
||
496 | return FSF_ERR_SERVER_USED; |
||
497 | |||
498 | break; |
||
499 | case FSF_SCHEDULER_EDF: |
||
235 | giacomo | 500 | /* Check if some thread use the server */ |
501 | if(EDFSTAR_budget_has_thread(local_scheduler_level,*server)) |
||
502 | return FSF_ERR_SERVER_USED; |
||
503 | break; |
||
504 | |||
221 | giacomo | 505 | case FSF_SCHEDULER_RM: |
235 | giacomo | 506 | /* Check if some thread use the server */ |
507 | if(RMSTAR_budget_has_thread(local_scheduler_level,*server)) |
||
508 | return FSF_ERR_SERVER_USED; |
||
509 | |||
221 | giacomo | 510 | break; |
334 | giacomo | 511 | |
512 | case FSF_SCHEDULER_MPEG: |
||
513 | /* Check if some thread use the server */ |
||
514 | if(MPEGSTAR_budget_has_thread(local_scheduler_level,*server)) |
||
515 | return FSF_ERR_SERVER_USED; |
||
516 | |||
517 | break; |
||
518 | |||
221 | giacomo | 519 | } |
520 | |||
241 | giacomo | 521 | SERVER_removebudget(fsf_server_level,*server); |
221 | giacomo | 522 | |
523 | level_free_descriptor(local_scheduler_level); |
||
405 | trimarchi | 524 | |
221 | giacomo | 525 | remove_contract(*server); |
411 | trimarchi | 526 | |
527 | f=kern_fsave(); |
||
528 | if (recalculate_contract(fsf_max_bw)==-1) { |
||
529 | kern_frestore(f); |
||
530 | return FSF_ERR_CREATE_SERVER; |
||
531 | } |
||
532 | #ifdef FSF_DEBUG |
||
533 | kern_printf("(Adjust budget)"); |
||
534 | #endif |
||
535 | for (i=0; i<current_server; i++) { |
||
536 | mul32div32to32(MAX_BANDWIDTH,server_list[i].Cmin,server_list[i].U,T); |
||
417 | giacomo | 537 | if (T > server_list[i].Tmin ) { |
411 | trimarchi | 538 | server_list[i].actual_budget = server_list[i].Cmin; |
539 | server_list[i].actual_period = T; |
||
417 | giacomo | 540 | #ifdef FSF_DEBUG |
541 | kern_printf("(1 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period); |
||
542 | #endif |
||
661 | giacomo | 543 | |
544 | if (server_list[i].d_equals_t == TRUE) |
||
545 | adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, T); |
||
546 | else |
||
547 | adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, server_list[i].deadline); |
||
548 | |||
411 | trimarchi | 549 | } else { |
417 | giacomo | 550 | mul32div32to32(server_list[i].Tmin,server_list[i].U,MAX_BANDWIDTH,Q); |
411 | trimarchi | 551 | server_list[i].actual_budget = Q; |
417 | giacomo | 552 | server_list[i].actual_period = server_list[i].Tmin; |
553 | #ifdef FSF_DEBUG |
||
554 | kern_printf("(2 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period); |
||
555 | #endif |
||
661 | giacomo | 556 | |
557 | if (server_list[i].d_equals_t == TRUE) |
||
558 | adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].Tmin); |
||
559 | else |
||
560 | adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].deadline); |
||
561 | |||
411 | trimarchi | 562 | } |
416 | trimarchi | 563 | server_list[i].U=server_list[i].Umin; |
411 | trimarchi | 564 | |
565 | } |
||
221 | giacomo | 566 | |
405 | trimarchi | 567 | |
221 | giacomo | 568 | *server = -1; |
411 | trimarchi | 569 | kern_frestore(f); |
221 | giacomo | 570 | |
571 | return 0; |
||
411 | trimarchi | 572 | |
221 | giacomo | 573 | } |
574 | |||
405 | trimarchi | 575 | int recalculate_contract(bandwidth_t U) { |
416 | trimarchi | 576 | bandwidth_t current_bandwidth; |
418 | giacomo | 577 | unsigned int temp_U; |
416 | trimarchi | 578 | int Qt; |
405 | trimarchi | 579 | int isok=0; |
580 | int i=0; |
||
581 | |||
407 | giacomo | 582 | #ifdef FSF_DEBUG |
583 | int temp; |
||
584 | |||
585 | kern_printf("(Recalculate contract)"); |
||
586 | #endif |
||
405 | trimarchi | 587 | |
588 | /* The current bandwidth is the min bandwidth */ |
||
589 | //current_bandwidth=SERVER_return_bandwidth(fsf_server_level); |
||
590 | #ifdef FSF_DEBUG |
||
410 | trimarchi | 591 | kern_printf("(nserver %d)", current_server); |
405 | trimarchi | 592 | #endif |
593 | |||
594 | do { |
||
595 | current_bandwidth=0; |
||
596 | Qt=0; |
||
410 | trimarchi | 597 | for (i=0; i<current_server; i++) { |
416 | trimarchi | 598 | if (server_list[i].Qs!=0 && server_list[i].U<server_list[i].Umax) |
405 | trimarchi | 599 | Qt+=server_list[i].Qs; |
416 | trimarchi | 600 | current_bandwidth+=server_list[i].U; |
405 | trimarchi | 601 | } |
413 | trimarchi | 602 | |
405 | trimarchi | 603 | #ifdef FSF_DEBUG |
604 | kern_printf("(Total Quality %d)", Qt); |
||
605 | #endif |
||
606 | isok=1; |
||
410 | trimarchi | 607 | for (i=0; i<current_server; i++) { |
416 | trimarchi | 608 | if (server_list[i].Qs!=0 && server_list[i].U<server_list[i].Umax) { |
609 | temp_U=server_list[i].U; |
||
610 | server_list[i].U=U-current_bandwidth; |
||
611 | mul32div32to32(server_list[i].U, server_list[i].Qs, Qt, server_list[i].U); |
||
612 | temp_U+=server_list[i].U; |
||
613 | |||
614 | if (temp_U<=server_list[i].Umin) { |
||
405 | trimarchi | 615 | server_list[i].U=server_list[i].Umin; |
416 | trimarchi | 616 | } else if (temp_U>server_list[i].Umax) { |
617 | server_list[i].U=server_list[i].Umax; |
||
405 | trimarchi | 618 | isok=0; |
619 | } else server_list[i].U=temp_U; |
||
620 | |||
621 | #ifdef FSF_DEBUG |
||
622 | mul32div32to32(server_list[i].U,100, MAX_BANDWIDTH, temp); |
||
623 | kern_printf("(Server %d bw %d)", server_list[i].server, temp); |
||
624 | #endif |
||
625 | } |
||
626 | } |
||
627 | |||
628 | } while (!isok); |
||
629 | |||
630 | return 0; |
||
631 | } |
||
632 | |||
221 | giacomo | 633 | int fsf_renegotiate_contract |
634 | (const fsf_contract_parameters_t *new_contract, |
||
635 | fsf_server_id_t server) |
||
636 | { |
||
679 | trimarchi | 637 | |
638 | struct mess message; |
||
221 | giacomo | 639 | |
679 | trimarchi | 640 | // send response server is -1 if the operation fail |
641 | message.type=RENEGOTIATE_CONTRACT; |
||
642 | memmove(&message.contract,new_contract, sizeof(fsf_contract_parameters_t)); |
||
643 | message.server = server; |
||
221 | giacomo | 644 | |
679 | trimarchi | 645 | //port_send(channel,&message,BLOCK); |
646 | |||
647 | //port_receive(channel, &message, BLOCK); |
||
648 | |||
649 | if (message.server==-1) return FSF_ERR_CONTRACT_REJECTED; |
||
650 | |||
405 | trimarchi | 651 | return 0; |
221 | giacomo | 652 | } |
241 | giacomo | 653 | |
407 | giacomo | 654 | void print_server_list() |
655 | { |
||
656 | |||
657 | int i; |
||
658 | |||
659 | kern_printf("Server List\n"); |
||
660 | |||
410 | trimarchi | 661 | for(i=0;i<current_server;i++) { |
407 | giacomo | 662 | |
662 | giacomo | 663 | kern_printf("[%d] Q:%d T:%d D:%d [DeT = %d]\n",server_list[i].server,(int)server_list[i].actual_budget,(int)server_list[i].actual_period,(int)server_list[i].deadline,(int)server_list[i].d_equals_t); |
407 | giacomo | 664 | |
665 | } |
||
666 | |||
667 | } |
||
808 | trimarchi | 668 | |
669 | |||
670 | int fsf_get_remain_budget(fsf_server_id_t server) { |
||
671 | |||
672 | return SERVER_get_remain_capacity(fsf_server_level, server); |
||
673 | |||
674 | } |