Rev 811 | Rev 816 | 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 | #ifdef FSF_DEBUG |
||
267 | giacomo | 299 | kern_printf("(FSF:Insert thread = %d to Server = %d)",*thread,server); |
221 | giacomo | 300 | #endif |
301 | |||
302 | /* Check if server and thread exsist */ |
||
241 | giacomo | 303 | if (server == NIL) |
304 | return FSF_ERR_INVALID_SERVER; |
||
221 | giacomo | 305 | |
241 | giacomo | 306 | local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server); |
307 | scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server); |
||
221 | giacomo | 308 | |
309 | /* Check if thread is already bind */ |
||
310 | switch (scheduler_id) { |
||
311 | case FSF_SCHEDULER_POSIX: |
||
312 | |||
298 | giacomo | 313 | nrt_task_def_arg(*(NRT_TASK_MODEL *)(local_scheduler_arg),arg); |
314 | nrt_task_def_level(*(NRT_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
221 | giacomo | 315 | |
298 | giacomo | 316 | *thread = task_create("POSIXSTAR", thread_code, local_scheduler_arg, NULL); |
267 | giacomo | 317 | if (*thread == NIL) { |
318 | #ifdef FSF_DEBUG |
||
319 | kern_printf("(FSF:Error creating thread)"); |
||
320 | #endif |
||
241 | giacomo | 321 | return FSF_ERR_CREATE_THREAD; |
267 | giacomo | 322 | } |
221 | giacomo | 323 | |
241 | giacomo | 324 | POSIXSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
234 | giacomo | 325 | |
221 | giacomo | 326 | break; |
327 | case FSF_SCHEDULER_EDF: |
||
328 | |||
250 | giacomo | 329 | hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg); |
330 | hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
331 | |||
332 | *thread = task_create("EDFSTAR", thread_code, local_scheduler_arg, NULL); |
||
333 | if (*thread == NIL) |
||
334 | return FSF_ERR_CREATE_THREAD; |
||
335 | |||
336 | EDFSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
||
337 | |||
221 | giacomo | 338 | break; |
235 | giacomo | 339 | |
221 | giacomo | 340 | case FSF_SCHEDULER_RM: |
235 | giacomo | 341 | |
273 | giacomo | 342 | hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg); |
343 | hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
344 | |||
345 | *thread = task_create("RMSTAR", thread_code, local_scheduler_arg, NULL); |
||
346 | if (*thread == NIL) |
||
347 | return FSF_ERR_CREATE_THREAD; |
||
348 | |||
349 | RMSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
||
350 | |||
351 | break; |
||
334 | giacomo | 352 | case FSF_SCHEDULER_MPEG: |
353 | |||
354 | hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg); |
||
355 | hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
356 | |||
338 | giacomo | 357 | *thread = task_create("MPEGSTAR", thread_code, local_scheduler_arg, NULL); |
334 | giacomo | 358 | if (*thread == NIL) |
359 | return FSF_ERR_CREATE_THREAD; |
||
360 | |||
361 | MPEGSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
||
362 | |||
363 | break; |
||
364 | |||
221 | giacomo | 365 | default: |
241 | giacomo | 366 | return FSF_ERR_INVALID_SERVER; |
221 | giacomo | 367 | break; |
368 | } |
||
811 | trimarchi | 369 | |
370 | #ifdef FSF_DEBUG |
||
371 | kern_printf("(FSF:Insert thread = %d to Server = %d)",*thread,server); |
||
372 | #endif |
||
221 | giacomo | 373 | |
374 | return 0; |
||
375 | |||
376 | } |
||
377 | |||
808 | trimarchi | 378 | int fsf_settask_nopreemptive |
221 | giacomo | 379 | (fsf_server_id_t *server, |
380 | pthread_t thread) |
||
381 | { |
||
382 | int local_scheduler_level, scheduler_id; |
||
383 | |||
241 | giacomo | 384 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
385 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread); |
||
221 | giacomo | 386 | |
387 | switch (scheduler_id) { |
||
808 | trimarchi | 388 | case FSF_SCHEDULER_POSIX: |
389 | |||
390 | break; |
||
391 | case FSF_SCHEDULER_EDF: |
||
392 | EDFSTAR_set_nopreemtive_current(local_scheduler_level); |
||
393 | return 1; |
||
394 | break; |
||
395 | case FSF_SCHEDULER_RM: |
||
812 | trimarchi | 396 | RMSTAR_set_nopreemtive_current(local_scheduler_level); |
397 | return 1; |
||
808 | trimarchi | 398 | break; |
399 | case FSF_SCHEDULER_MPEG: |
||
400 | break; |
||
401 | default: |
||
402 | return -1; |
||
403 | } |
||
404 | return -1; |
||
405 | } |
||
406 | |||
407 | |||
408 | int fsf_settask_preemptive |
||
409 | (fsf_server_id_t *server, |
||
410 | pthread_t thread) |
||
411 | { |
||
412 | int local_scheduler_level, scheduler_id; |
||
413 | |||
414 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
||
415 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread); |
||
416 | |||
417 | switch (scheduler_id) { |
||
418 | case FSF_SCHEDULER_POSIX: |
||
419 | |||
420 | break; |
||
421 | case FSF_SCHEDULER_EDF: |
||
422 | EDFSTAR_unset_nopreemtive_current(local_scheduler_level); |
||
423 | return 1; |
||
424 | break; |
||
425 | case FSF_SCHEDULER_RM: |
||
812 | trimarchi | 426 | RMSTAR_unset_nopreemtive_current(local_scheduler_level); |
427 | return 1; |
||
808 | trimarchi | 428 | break; |
429 | case FSF_SCHEDULER_MPEG: |
||
430 | break; |
||
431 | default: |
||
432 | return -1; |
||
433 | } |
||
434 | |||
435 | return -1; |
||
436 | |||
437 | } |
||
438 | |||
439 | |||
440 | int fsf_get_server |
||
441 | (fsf_server_id_t *server, |
||
442 | pthread_t thread) |
||
443 | { |
||
444 | int local_scheduler_level, scheduler_id; |
||
445 | |||
446 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
||
447 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread); |
||
448 | |||
449 | switch (scheduler_id) { |
||
221 | giacomo | 450 | case FSF_SCHEDULER_POSIX: |
451 | return POSIXSTAR_getbudget(local_scheduler_level,thread); |
||
452 | case FSF_SCHEDULER_EDF: |
||
235 | giacomo | 453 | return EDFSTAR_getbudget(local_scheduler_level,thread); |
221 | giacomo | 454 | case FSF_SCHEDULER_RM: |
235 | giacomo | 455 | return RMSTAR_getbudget(local_scheduler_level,thread); |
334 | giacomo | 456 | case FSF_SCHEDULER_MPEG: |
457 | return MPEGSTAR_getbudget(local_scheduler_level,thread); |
||
235 | giacomo | 458 | default: |
221 | giacomo | 459 | return -1; |
460 | } |
||
461 | |||
462 | return -1; |
||
463 | |||
464 | } |
||
465 | |||
339 | giacomo | 466 | int fsf_get_server_level(void) |
467 | { |
||
468 | |||
469 | return fsf_server_level; |
||
470 | |||
471 | } |
||
472 | |||
221 | giacomo | 473 | int fsf_cancel_contract |
474 | (fsf_server_id_t *server) |
||
475 | { |
||
476 | |||
477 | int local_scheduler_level, scheduler_id; |
||
411 | trimarchi | 478 | SYS_FLAGS f; |
479 | TIME T,Q; |
||
480 | int i=0; |
||
221 | giacomo | 481 | |
482 | #ifdef FSF_DEBUG |
||
483 | kern_printf("(Remove server %d)",*server); |
||
484 | #endif |
||
485 | |||
486 | /* Check server id */ |
||
487 | if (*server < 0) |
||
488 | return FSF_ERR_INVALID_SERVER; |
||
489 | |||
241 | giacomo | 490 | local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,*server); |
491 | scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,*server); |
||
221 | giacomo | 492 | |
493 | switch (scheduler_id) { |
||
494 | case FSF_SCHEDULER_POSIX: |
||
495 | |||
496 | /* Check if some thread use the server */ |
||
497 | if(POSIXSTAR_budget_has_thread(local_scheduler_level,*server)) |
||
498 | return FSF_ERR_SERVER_USED; |
||
499 | |||
500 | break; |
||
501 | case FSF_SCHEDULER_EDF: |
||
235 | giacomo | 502 | /* Check if some thread use the server */ |
503 | if(EDFSTAR_budget_has_thread(local_scheduler_level,*server)) |
||
504 | return FSF_ERR_SERVER_USED; |
||
505 | break; |
||
506 | |||
221 | giacomo | 507 | case FSF_SCHEDULER_RM: |
235 | giacomo | 508 | /* Check if some thread use the server */ |
509 | if(RMSTAR_budget_has_thread(local_scheduler_level,*server)) |
||
510 | return FSF_ERR_SERVER_USED; |
||
511 | |||
221 | giacomo | 512 | break; |
334 | giacomo | 513 | |
514 | case FSF_SCHEDULER_MPEG: |
||
515 | /* Check if some thread use the server */ |
||
516 | if(MPEGSTAR_budget_has_thread(local_scheduler_level,*server)) |
||
517 | return FSF_ERR_SERVER_USED; |
||
518 | |||
519 | break; |
||
520 | |||
221 | giacomo | 521 | } |
522 | |||
241 | giacomo | 523 | SERVER_removebudget(fsf_server_level,*server); |
221 | giacomo | 524 | |
525 | level_free_descriptor(local_scheduler_level); |
||
405 | trimarchi | 526 | |
221 | giacomo | 527 | remove_contract(*server); |
411 | trimarchi | 528 | |
529 | f=kern_fsave(); |
||
530 | if (recalculate_contract(fsf_max_bw)==-1) { |
||
531 | kern_frestore(f); |
||
532 | return FSF_ERR_CREATE_SERVER; |
||
533 | } |
||
534 | #ifdef FSF_DEBUG |
||
535 | kern_printf("(Adjust budget)"); |
||
536 | #endif |
||
537 | for (i=0; i<current_server; i++) { |
||
538 | mul32div32to32(MAX_BANDWIDTH,server_list[i].Cmin,server_list[i].U,T); |
||
417 | giacomo | 539 | if (T > server_list[i].Tmin ) { |
411 | trimarchi | 540 | server_list[i].actual_budget = server_list[i].Cmin; |
541 | server_list[i].actual_period = T; |
||
417 | giacomo | 542 | #ifdef FSF_DEBUG |
543 | kern_printf("(1 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period); |
||
544 | #endif |
||
661 | giacomo | 545 | |
546 | if (server_list[i].d_equals_t == TRUE) |
||
547 | adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, T); |
||
548 | else |
||
549 | adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, server_list[i].deadline); |
||
550 | |||
411 | trimarchi | 551 | } else { |
417 | giacomo | 552 | mul32div32to32(server_list[i].Tmin,server_list[i].U,MAX_BANDWIDTH,Q); |
411 | trimarchi | 553 | server_list[i].actual_budget = Q; |
417 | giacomo | 554 | server_list[i].actual_period = server_list[i].Tmin; |
555 | #ifdef FSF_DEBUG |
||
556 | kern_printf("(2 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period); |
||
557 | #endif |
||
661 | giacomo | 558 | |
559 | if (server_list[i].d_equals_t == TRUE) |
||
560 | adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].Tmin); |
||
561 | else |
||
562 | adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].deadline); |
||
563 | |||
411 | trimarchi | 564 | } |
416 | trimarchi | 565 | server_list[i].U=server_list[i].Umin; |
411 | trimarchi | 566 | |
567 | } |
||
221 | giacomo | 568 | |
405 | trimarchi | 569 | |
221 | giacomo | 570 | *server = -1; |
411 | trimarchi | 571 | kern_frestore(f); |
221 | giacomo | 572 | |
573 | return 0; |
||
411 | trimarchi | 574 | |
221 | giacomo | 575 | } |
576 | |||
405 | trimarchi | 577 | int recalculate_contract(bandwidth_t U) { |
416 | trimarchi | 578 | bandwidth_t current_bandwidth; |
418 | giacomo | 579 | unsigned int temp_U; |
416 | trimarchi | 580 | int Qt; |
405 | trimarchi | 581 | int isok=0; |
582 | int i=0; |
||
583 | |||
407 | giacomo | 584 | #ifdef FSF_DEBUG |
585 | int temp; |
||
586 | |||
587 | kern_printf("(Recalculate contract)"); |
||
588 | #endif |
||
405 | trimarchi | 589 | |
590 | /* The current bandwidth is the min bandwidth */ |
||
591 | //current_bandwidth=SERVER_return_bandwidth(fsf_server_level); |
||
592 | #ifdef FSF_DEBUG |
||
410 | trimarchi | 593 | kern_printf("(nserver %d)", current_server); |
405 | trimarchi | 594 | #endif |
595 | |||
596 | do { |
||
597 | current_bandwidth=0; |
||
598 | Qt=0; |
||
410 | trimarchi | 599 | for (i=0; i<current_server; i++) { |
416 | trimarchi | 600 | if (server_list[i].Qs!=0 && server_list[i].U<server_list[i].Umax) |
405 | trimarchi | 601 | Qt+=server_list[i].Qs; |
416 | trimarchi | 602 | current_bandwidth+=server_list[i].U; |
405 | trimarchi | 603 | } |
413 | trimarchi | 604 | |
405 | trimarchi | 605 | #ifdef FSF_DEBUG |
606 | kern_printf("(Total Quality %d)", Qt); |
||
607 | #endif |
||
608 | isok=1; |
||
410 | trimarchi | 609 | for (i=0; i<current_server; i++) { |
416 | trimarchi | 610 | if (server_list[i].Qs!=0 && server_list[i].U<server_list[i].Umax) { |
611 | temp_U=server_list[i].U; |
||
612 | server_list[i].U=U-current_bandwidth; |
||
613 | mul32div32to32(server_list[i].U, server_list[i].Qs, Qt, server_list[i].U); |
||
614 | temp_U+=server_list[i].U; |
||
615 | |||
616 | if (temp_U<=server_list[i].Umin) { |
||
405 | trimarchi | 617 | server_list[i].U=server_list[i].Umin; |
416 | trimarchi | 618 | } else if (temp_U>server_list[i].Umax) { |
619 | server_list[i].U=server_list[i].Umax; |
||
405 | trimarchi | 620 | isok=0; |
621 | } else server_list[i].U=temp_U; |
||
622 | |||
623 | #ifdef FSF_DEBUG |
||
624 | mul32div32to32(server_list[i].U,100, MAX_BANDWIDTH, temp); |
||
625 | kern_printf("(Server %d bw %d)", server_list[i].server, temp); |
||
626 | #endif |
||
627 | } |
||
628 | } |
||
629 | |||
630 | } while (!isok); |
||
631 | |||
632 | return 0; |
||
633 | } |
||
634 | |||
221 | giacomo | 635 | int fsf_renegotiate_contract |
636 | (const fsf_contract_parameters_t *new_contract, |
||
637 | fsf_server_id_t server) |
||
638 | { |
||
679 | trimarchi | 639 | |
640 | struct mess message; |
||
221 | giacomo | 641 | |
679 | trimarchi | 642 | // send response server is -1 if the operation fail |
643 | message.type=RENEGOTIATE_CONTRACT; |
||
644 | memmove(&message.contract,new_contract, sizeof(fsf_contract_parameters_t)); |
||
645 | message.server = server; |
||
221 | giacomo | 646 | |
679 | trimarchi | 647 | //port_send(channel,&message,BLOCK); |
648 | |||
649 | //port_receive(channel, &message, BLOCK); |
||
650 | |||
651 | if (message.server==-1) return FSF_ERR_CONTRACT_REJECTED; |
||
652 | |||
405 | trimarchi | 653 | return 0; |
221 | giacomo | 654 | } |
241 | giacomo | 655 | |
407 | giacomo | 656 | void print_server_list() |
657 | { |
||
658 | |||
659 | int i; |
||
660 | |||
661 | kern_printf("Server List\n"); |
||
662 | |||
410 | trimarchi | 663 | for(i=0;i<current_server;i++) { |
407 | giacomo | 664 | |
662 | giacomo | 665 | 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 | 666 | |
667 | } |
||
668 | |||
669 | } |
||
808 | trimarchi | 670 | |
671 | |||
672 | int fsf_get_remain_budget(fsf_server_id_t server) { |
||
673 | |||
674 | return SERVER_get_remain_capacity(fsf_server_level, server); |
||
675 | |||
676 | } |