Rev 810 | Rev 812 | 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: |
||
396 | break; |
||
397 | case FSF_SCHEDULER_MPEG: |
||
398 | break; |
||
399 | default: |
||
400 | return -1; |
||
401 | } |
||
402 | return -1; |
||
403 | } |
||
404 | |||
405 | |||
406 | int fsf_settask_preemptive |
||
407 | (fsf_server_id_t *server, |
||
408 | pthread_t thread) |
||
409 | { |
||
410 | int local_scheduler_level, scheduler_id; |
||
411 | |||
412 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
||
413 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread); |
||
414 | |||
415 | switch (scheduler_id) { |
||
416 | case FSF_SCHEDULER_POSIX: |
||
417 | |||
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: |
||
424 | break; |
||
425 | case FSF_SCHEDULER_MPEG: |
||
426 | break; |
||
427 | default: |
||
428 | return -1; |
||
429 | } |
||
430 | |||
431 | return -1; |
||
432 | |||
433 | } |
||
434 | |||
435 | |||
436 | int fsf_get_server |
||
437 | (fsf_server_id_t *server, |
||
438 | pthread_t thread) |
||
439 | { |
||
440 | int local_scheduler_level, scheduler_id; |
||
441 | |||
442 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
||
443 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread); |
||
444 | |||
445 | switch (scheduler_id) { |
||
221 | giacomo | 446 | case FSF_SCHEDULER_POSIX: |
447 | return POSIXSTAR_getbudget(local_scheduler_level,thread); |
||
448 | case FSF_SCHEDULER_EDF: |
||
235 | giacomo | 449 | return EDFSTAR_getbudget(local_scheduler_level,thread); |
221 | giacomo | 450 | case FSF_SCHEDULER_RM: |
235 | giacomo | 451 | return RMSTAR_getbudget(local_scheduler_level,thread); |
334 | giacomo | 452 | case FSF_SCHEDULER_MPEG: |
453 | return MPEGSTAR_getbudget(local_scheduler_level,thread); |
||
235 | giacomo | 454 | default: |
221 | giacomo | 455 | return -1; |
456 | } |
||
457 | |||
458 | return -1; |
||
459 | |||
460 | } |
||
461 | |||
339 | giacomo | 462 | int fsf_get_server_level(void) |
463 | { |
||
464 | |||
465 | return fsf_server_level; |
||
466 | |||
467 | } |
||
468 | |||
221 | giacomo | 469 | int fsf_cancel_contract |
470 | (fsf_server_id_t *server) |
||
471 | { |
||
472 | |||
473 | int local_scheduler_level, scheduler_id; |
||
411 | trimarchi | 474 | SYS_FLAGS f; |
475 | TIME T,Q; |
||
476 | int i=0; |
||
221 | giacomo | 477 | |
478 | #ifdef FSF_DEBUG |
||
479 | kern_printf("(Remove server %d)",*server); |
||
480 | #endif |
||
481 | |||
482 | /* Check server id */ |
||
483 | if (*server < 0) |
||
484 | return FSF_ERR_INVALID_SERVER; |
||
485 | |||
241 | giacomo | 486 | local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,*server); |
487 | scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,*server); |
||
221 | giacomo | 488 | |
489 | switch (scheduler_id) { |
||
490 | case FSF_SCHEDULER_POSIX: |
||
491 | |||
492 | /* Check if some thread use the server */ |
||
493 | if(POSIXSTAR_budget_has_thread(local_scheduler_level,*server)) |
||
494 | return FSF_ERR_SERVER_USED; |
||
495 | |||
496 | break; |
||
497 | case FSF_SCHEDULER_EDF: |
||
235 | giacomo | 498 | /* Check if some thread use the server */ |
499 | if(EDFSTAR_budget_has_thread(local_scheduler_level,*server)) |
||
500 | return FSF_ERR_SERVER_USED; |
||
501 | break; |
||
502 | |||
221 | giacomo | 503 | case FSF_SCHEDULER_RM: |
235 | giacomo | 504 | /* Check if some thread use the server */ |
505 | if(RMSTAR_budget_has_thread(local_scheduler_level,*server)) |
||
506 | return FSF_ERR_SERVER_USED; |
||
507 | |||
221 | giacomo | 508 | break; |
334 | giacomo | 509 | |
510 | case FSF_SCHEDULER_MPEG: |
||
511 | /* Check if some thread use the server */ |
||
512 | if(MPEGSTAR_budget_has_thread(local_scheduler_level,*server)) |
||
513 | return FSF_ERR_SERVER_USED; |
||
514 | |||
515 | break; |
||
516 | |||
221 | giacomo | 517 | } |
518 | |||
241 | giacomo | 519 | SERVER_removebudget(fsf_server_level,*server); |
221 | giacomo | 520 | |
521 | level_free_descriptor(local_scheduler_level); |
||
405 | trimarchi | 522 | |
221 | giacomo | 523 | remove_contract(*server); |
411 | trimarchi | 524 | |
525 | f=kern_fsave(); |
||
526 | if (recalculate_contract(fsf_max_bw)==-1) { |
||
527 | kern_frestore(f); |
||
528 | return FSF_ERR_CREATE_SERVER; |
||
529 | } |
||
530 | #ifdef FSF_DEBUG |
||
531 | kern_printf("(Adjust budget)"); |
||
532 | #endif |
||
533 | for (i=0; i<current_server; i++) { |
||
534 | mul32div32to32(MAX_BANDWIDTH,server_list[i].Cmin,server_list[i].U,T); |
||
417 | giacomo | 535 | if (T > server_list[i].Tmin ) { |
411 | trimarchi | 536 | server_list[i].actual_budget = server_list[i].Cmin; |
537 | server_list[i].actual_period = T; |
||
417 | giacomo | 538 | #ifdef FSF_DEBUG |
539 | kern_printf("(1 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period); |
||
540 | #endif |
||
661 | giacomo | 541 | |
542 | if (server_list[i].d_equals_t == TRUE) |
||
543 | adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, T); |
||
544 | else |
||
545 | adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, server_list[i].deadline); |
||
546 | |||
411 | trimarchi | 547 | } else { |
417 | giacomo | 548 | mul32div32to32(server_list[i].Tmin,server_list[i].U,MAX_BANDWIDTH,Q); |
411 | trimarchi | 549 | server_list[i].actual_budget = Q; |
417 | giacomo | 550 | server_list[i].actual_period = server_list[i].Tmin; |
551 | #ifdef FSF_DEBUG |
||
552 | kern_printf("(2 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period); |
||
553 | #endif |
||
661 | giacomo | 554 | |
555 | if (server_list[i].d_equals_t == TRUE) |
||
556 | adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].Tmin); |
||
557 | else |
||
558 | adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].deadline); |
||
559 | |||
411 | trimarchi | 560 | } |
416 | trimarchi | 561 | server_list[i].U=server_list[i].Umin; |
411 | trimarchi | 562 | |
563 | } |
||
221 | giacomo | 564 | |
405 | trimarchi | 565 | |
221 | giacomo | 566 | *server = -1; |
411 | trimarchi | 567 | kern_frestore(f); |
221 | giacomo | 568 | |
569 | return 0; |
||
411 | trimarchi | 570 | |
221 | giacomo | 571 | } |
572 | |||
405 | trimarchi | 573 | int recalculate_contract(bandwidth_t U) { |
416 | trimarchi | 574 | bandwidth_t current_bandwidth; |
418 | giacomo | 575 | unsigned int temp_U; |
416 | trimarchi | 576 | int Qt; |
405 | trimarchi | 577 | int isok=0; |
578 | int i=0; |
||
579 | |||
407 | giacomo | 580 | #ifdef FSF_DEBUG |
581 | int temp; |
||
582 | |||
583 | kern_printf("(Recalculate contract)"); |
||
584 | #endif |
||
405 | trimarchi | 585 | |
586 | /* The current bandwidth is the min bandwidth */ |
||
587 | //current_bandwidth=SERVER_return_bandwidth(fsf_server_level); |
||
588 | #ifdef FSF_DEBUG |
||
410 | trimarchi | 589 | kern_printf("(nserver %d)", current_server); |
405 | trimarchi | 590 | #endif |
591 | |||
592 | do { |
||
593 | current_bandwidth=0; |
||
594 | Qt=0; |
||
410 | trimarchi | 595 | for (i=0; i<current_server; i++) { |
416 | trimarchi | 596 | if (server_list[i].Qs!=0 && server_list[i].U<server_list[i].Umax) |
405 | trimarchi | 597 | Qt+=server_list[i].Qs; |
416 | trimarchi | 598 | current_bandwidth+=server_list[i].U; |
405 | trimarchi | 599 | } |
413 | trimarchi | 600 | |
405 | trimarchi | 601 | #ifdef FSF_DEBUG |
602 | kern_printf("(Total Quality %d)", Qt); |
||
603 | #endif |
||
604 | isok=1; |
||
410 | trimarchi | 605 | for (i=0; i<current_server; i++) { |
416 | trimarchi | 606 | if (server_list[i].Qs!=0 && server_list[i].U<server_list[i].Umax) { |
607 | temp_U=server_list[i].U; |
||
608 | server_list[i].U=U-current_bandwidth; |
||
609 | mul32div32to32(server_list[i].U, server_list[i].Qs, Qt, server_list[i].U); |
||
610 | temp_U+=server_list[i].U; |
||
611 | |||
612 | if (temp_U<=server_list[i].Umin) { |
||
405 | trimarchi | 613 | server_list[i].U=server_list[i].Umin; |
416 | trimarchi | 614 | } else if (temp_U>server_list[i].Umax) { |
615 | server_list[i].U=server_list[i].Umax; |
||
405 | trimarchi | 616 | isok=0; |
617 | } else server_list[i].U=temp_U; |
||
618 | |||
619 | #ifdef FSF_DEBUG |
||
620 | mul32div32to32(server_list[i].U,100, MAX_BANDWIDTH, temp); |
||
621 | kern_printf("(Server %d bw %d)", server_list[i].server, temp); |
||
622 | #endif |
||
623 | } |
||
624 | } |
||
625 | |||
626 | } while (!isok); |
||
627 | |||
628 | return 0; |
||
629 | } |
||
630 | |||
221 | giacomo | 631 | int fsf_renegotiate_contract |
632 | (const fsf_contract_parameters_t *new_contract, |
||
633 | fsf_server_id_t server) |
||
634 | { |
||
679 | trimarchi | 635 | |
636 | struct mess message; |
||
221 | giacomo | 637 | |
679 | trimarchi | 638 | // send response server is -1 if the operation fail |
639 | message.type=RENEGOTIATE_CONTRACT; |
||
640 | memmove(&message.contract,new_contract, sizeof(fsf_contract_parameters_t)); |
||
641 | message.server = server; |
||
221 | giacomo | 642 | |
679 | trimarchi | 643 | //port_send(channel,&message,BLOCK); |
644 | |||
645 | //port_receive(channel, &message, BLOCK); |
||
646 | |||
647 | if (message.server==-1) return FSF_ERR_CONTRACT_REJECTED; |
||
648 | |||
405 | trimarchi | 649 | return 0; |
221 | giacomo | 650 | } |
241 | giacomo | 651 | |
407 | giacomo | 652 | void print_server_list() |
653 | { |
||
654 | |||
655 | int i; |
||
656 | |||
657 | kern_printf("Server List\n"); |
||
658 | |||
410 | trimarchi | 659 | for(i=0;i<current_server;i++) { |
407 | giacomo | 660 | |
662 | giacomo | 661 | 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 | 662 | |
663 | } |
||
664 | |||
665 | } |
||
808 | trimarchi | 666 | |
667 | |||
668 | int fsf_get_remain_budget(fsf_server_id_t server) { |
||
669 | |||
670 | return SERVER_get_remain_capacity(fsf_server_level, server); |
||
671 | |||
672 | } |