Rev 859 | Rev 872 | 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 | |
868 | trimarchi | 20 | #include "fsf_basic_types.h" |
21 | #include "fsf_configuration_parameters.h" |
||
22 | #include "fsf_core.h" |
||
241 | giacomo | 23 | #include "fsf_server.h" |
679 | trimarchi | 24 | #include "fsf_service_task.h" |
868 | trimarchi | 25 | #include "fsf_hierarchical.h" |
679 | trimarchi | 26 | #include "message.h" |
221 | giacomo | 27 | |
868 | trimarchi | 28 | #include "posixstar.h" |
29 | #include "edfstar.h" |
||
30 | #include "nonestar.h" |
||
31 | #include "rmstar.h" |
||
32 | |||
221 | giacomo | 33 | #include <pthread.h> |
34 | #include <stdlib.h> |
||
808 | trimarchi | 35 | #include "pistar.h" |
868 | trimarchi | 36 | #include <modules/comm_message.h> |
221 | giacomo | 37 | |
417 | giacomo | 38 | //#define FSF_DEBUG |
221 | giacomo | 39 | |
410 | trimarchi | 40 | int current_server=0; |
407 | giacomo | 41 | server_elem server_list[FSF_MAX_N_SERVERS]; |
406 | giacomo | 42 | bandwidth_t fsf_max_bw = 0; |
405 | trimarchi | 43 | |
241 | giacomo | 44 | int fsf_server_level; |
868 | trimarchi | 45 | int fsf_posix_level = -1; |
808 | trimarchi | 46 | int shared_object_level; |
679 | trimarchi | 47 | PID server_task; |
868 | trimarchi | 48 | fsf_contract_parameters_t contract; |
221 | giacomo | 49 | |
868 | trimarchi | 50 | |
679 | trimarchi | 51 | PORT channel[2]; |
52 | |||
868 | trimarchi | 53 | fsf_server_id_t service_server = -1; |
54 | fsf_contract_parameters_t service_contract; |
||
55 | |||
808 | trimarchi | 56 | int FSF_register_shared_object_module(void) { |
811 | trimarchi | 57 | fsf_register_shared_object(); |
808 | trimarchi | 58 | return PISTAR_register_module(); |
59 | } |
||
60 | |||
61 | |||
810 | trimarchi | 62 | int FSF_get_shared_object_level() { |
63 | return shared_object_level; |
||
64 | } |
||
65 | |||
868 | trimarchi | 66 | int FSF_register_module(int posix_level, int server_level, bandwidth_t max_bw) |
224 | giacomo | 67 | { |
68 | printk("FSF Module\n"); |
||
410 | trimarchi | 69 | current_server=0; |
241 | giacomo | 70 | fsf_server_level = server_level; |
868 | trimarchi | 71 | fsf_posix_level = posix_level; |
406 | giacomo | 72 | fsf_max_bw = max_bw; |
811 | trimarchi | 73 | shared_object_level = FSF_register_shared_object_module(); |
679 | trimarchi | 74 | |
224 | giacomo | 75 | return 0; |
76 | |||
77 | } |
||
78 | |||
868 | trimarchi | 79 | void FSF_start_service_task(void) { |
679 | trimarchi | 80 | |
868 | trimarchi | 81 | int err; |
82 | struct timespec default_period = {0,50000000}; |
||
83 | struct timespec default_budget = {0,1000000}; |
||
84 | DUMMY_TASK_MODEL m; |
||
85 | |||
86 | dummy_task_default_model(m); |
||
87 | |||
679 | trimarchi | 88 | // create the service task |
89 | // create the communication channel for negotiation and renegotiation |
||
90 | |||
91 | channel[1] = port_create("CHANW",sizeof(struct mess),1,STREAM,WRITE); |
||
92 | |||
93 | channel[0] = port_create("CHANR",sizeof(struct mess),1,STREAM,READ); |
||
94 | |||
868 | trimarchi | 95 | fsf_initialize_contract(&service_contract); |
96 | |||
97 | fsf_set_contract_basic_parameters(&service_contract,&default_budget,&default_period,FSF_DEFAULT_WORKLOAD); |
||
98 | negotiate_contract(&service_contract,&service_server); |
||
99 | |||
100 | //server_task = task_create("stask",service_task,model,NULL); |
||
101 | err = fsf_create_thread(service_server,&server_task,NULL,service_task,NULL,&m); |
||
102 | if (err) { |
||
679 | trimarchi | 103 | cprintf("error creating service task\n"); |
104 | sys_shutdown_message("Could not create service_task"); |
||
105 | sys_end(); |
||
106 | return; |
||
107 | } |
||
108 | |||
109 | task_activate(server_task); |
||
110 | |||
111 | } |
||
112 | |||
113 | |||
221 | giacomo | 114 | /* Convert the contract specification to |
115 | * budget parameters |
||
116 | */ |
||
241 | giacomo | 117 | int set_SERVER_budget_from_contract |
221 | giacomo | 118 | (const fsf_contract_parameters_t *contract, |
119 | int *budget) |
||
120 | { |
||
121 | |||
122 | int local_scheduler_level = 0; |
||
123 | |||
868 | trimarchi | 124 | switch (contract->policy) { |
125 | case FSF_POSIX: |
||
241 | giacomo | 126 | local_scheduler_level = POSIXSTAR_register_level(fsf_server_level,5000,32); |
221 | giacomo | 127 | break; |
868 | trimarchi | 128 | case FSF_EDF: |
241 | giacomo | 129 | local_scheduler_level = EDFSTAR_register_level(fsf_server_level); |
221 | giacomo | 130 | break; |
868 | trimarchi | 131 | case FSF_RM: |
241 | giacomo | 132 | local_scheduler_level = RMSTAR_register_level(fsf_server_level); |
221 | giacomo | 133 | break; |
868 | trimarchi | 134 | case FSF_NONE: |
855 | trimarchi | 135 | local_scheduler_level = NONESTAR_register_level(fsf_server_level); |
334 | giacomo | 136 | break; |
221 | giacomo | 137 | } |
405 | trimarchi | 138 | |
661 | giacomo | 139 | if (contract->d_equals_t == TRUE) { |
241 | giacomo | 140 | *budget = SERVER_setbudget(fsf_server_level, |
221 | giacomo | 141 | TIMESPEC2USEC(&(contract->budget_min)), |
142 | TIMESPEC2USEC(&(contract->period_max)), |
||
661 | giacomo | 143 | TIMESPEC2USEC(&(contract->period_max)), |
868 | trimarchi | 144 | local_scheduler_level,contract->policy); |
661 | giacomo | 145 | } else { |
146 | *budget = SERVER_setbudget(fsf_server_level, |
||
147 | TIMESPEC2USEC(&(contract->budget_min)), |
||
148 | TIMESPEC2USEC(&(contract->period_max)), |
||
149 | TIMESPEC2USEC(&(contract->deadline)), |
||
868 | trimarchi | 150 | local_scheduler_level,contract->policy); |
661 | giacomo | 151 | } |
221 | giacomo | 152 | |
153 | return 0; |
||
154 | |||
155 | } |
||
156 | |||
405 | trimarchi | 157 | int adjust_SERVER_budget |
158 | (int budget, const TIME budget_actual, |
||
661 | giacomo | 159 | const TIME period_actual, const TIME dline_actual) |
221 | giacomo | 160 | { |
161 | |||
241 | giacomo | 162 | SERVER_adjust_budget(fsf_server_level, |
405 | trimarchi | 163 | budget_actual, |
164 | period_actual, |
||
661 | giacomo | 165 | dline_actual, |
241 | giacomo | 166 | budget); |
221 | giacomo | 167 | |
168 | return 0; |
||
169 | |||
170 | } |
||
171 | |||
172 | /* Admission Test function */ |
||
173 | int add_contract(const fsf_contract_parameters_t *contract) |
||
174 | { |
||
175 | return 0; |
||
176 | |||
177 | } |
||
178 | |||
687 | trimarchi | 179 | void contract_to_server(const fsf_contract_parameters_t *contract, int i) { |
180 | |||
410 | trimarchi | 181 | TIME T,Q; |
182 | #ifdef FSF_DEBUG |
||
183 | int temp; |
||
184 | #endif |
||
185 | |||
186 | T=TIMESPEC2USEC(&contract->period_min); |
||
187 | Q=TIMESPEC2USEC(&contract->budget_max); |
||
188 | mul32div32to32(MAX_BANDWIDTH,Q,T,server_list[current_server].Umax); |
||
189 | |||
417 | giacomo | 190 | T=TIMESPEC2USEC(&contract->period_min); |
191 | server_list[i].Tmin=T; |
||
192 | |||
410 | trimarchi | 193 | T=TIMESPEC2USEC(&contract->period_max); |
194 | server_list[i].Tmax=T; |
||
417 | giacomo | 195 | |
410 | trimarchi | 196 | Q=TIMESPEC2USEC(&contract->budget_min); |
197 | server_list[i].Cmin=Q; |
||
198 | |||
199 | mul32div32to32(MAX_BANDWIDTH,Q,T,server_list[i].Umin); |
||
416 | trimarchi | 200 | server_list[i].U=server_list[i].Umin; |
661 | giacomo | 201 | |
202 | if (contract->d_equals_t == TRUE) { |
||
203 | server_list[i].deadline = 0; |
||
204 | server_list[i].d_equals_t = TRUE; |
||
205 | } else { |
||
206 | server_list[i].deadline = TIMESPEC2USEC(&contract->deadline);; |
||
207 | server_list[i].d_equals_t = FALSE; |
||
208 | } |
||
209 | |||
843 | trimarchi | 210 | server_list[i].Qs = contract->quality; |
211 | server_list[i].Is = contract->importance; |
||
212 | |||
410 | trimarchi | 213 | #ifdef FSF_DEBUG |
214 | mul32div32to32(server_list[i].Umax,100, MAX_BANDWIDTH, temp); |
||
215 | kern_printf("(Umax %d)",temp); |
||
216 | mul32div32to32(server_list[i].Umin,100, MAX_BANDWIDTH, temp); |
||
217 | kern_printf("(Umin %d)",temp); |
||
218 | #endif |
||
219 | |||
687 | trimarchi | 220 | } |
221 | |||
222 | int relink_contract_to_server(const fsf_contract_parameters_t *contract, |
||
223 | fsf_server_id_t server) |
||
224 | { |
||
225 | int i=0; |
||
226 | #ifdef FSF_DEBUG |
||
227 | kern_printf("(Relink Server %d)",server); |
||
228 | #endif |
||
229 | // find contract |
||
230 | while(i<current_server) { |
||
231 | if (server_list[i].server==server) break; |
||
232 | i++; |
||
233 | } |
||
234 | |||
235 | server_list[i].server=server; |
||
843 | trimarchi | 236 | // server_list[i].Qs=1; |
687 | trimarchi | 237 | |
238 | contract_to_server(contract, i); |
||
239 | |||
240 | |||
410 | trimarchi | 241 | return 0; |
242 | |||
243 | } |
||
244 | |||
221 | giacomo | 245 | int link_contract_to_server(const fsf_contract_parameters_t *contract, |
246 | fsf_server_id_t server) |
||
247 | { |
||
407 | giacomo | 248 | #ifdef FSF_DEBUG |
405 | trimarchi | 249 | kern_printf("(Link Server %d)",server); |
250 | #endif |
||
251 | |||
410 | trimarchi | 252 | server_list[current_server].server=server; |
843 | trimarchi | 253 | //server_list[current_server].Qs=1; |
405 | trimarchi | 254 | |
687 | trimarchi | 255 | contract_to_server(contract,current_server); |
405 | trimarchi | 256 | |
410 | trimarchi | 257 | current_server++; |
221 | giacomo | 258 | return 0; |
259 | |||
260 | } |
||
261 | |||
262 | int remove_contract(fsf_server_id_t server) |
||
263 | { |
||
405 | trimarchi | 264 | int i=0; |
265 | // find the contract |
||
410 | trimarchi | 266 | while(i<current_server) { |
405 | trimarchi | 267 | if (server_list[i].server==server) break; |
268 | i++; |
||
269 | } |
||
221 | giacomo | 270 | |
405 | trimarchi | 271 | // compress the array; |
410 | trimarchi | 272 | while (i<(current_server-1)) { |
405 | trimarchi | 273 | server_list[i].server=server_list[i+1].server; |
274 | server_list[i].Umin=server_list[i+1].Umin; |
||
416 | trimarchi | 275 | server_list[i].U=server_list[i+1].Umin; |
405 | trimarchi | 276 | server_list[i].Umax=server_list[i+1].Umax; |
277 | server_list[i].Cmin=server_list[i+1].Cmin; |
||
417 | giacomo | 278 | server_list[i].Tmin=server_list[i+1].Tmin; |
405 | trimarchi | 279 | server_list[i].Tmax=server_list[i+1].Tmax; |
280 | server_list[i].Qs=server_list[i+1].Qs; |
||
661 | giacomo | 281 | server_list[i].deadline = server_list[i+1].deadline; |
282 | server_list[i].d_equals_t = server_list[i+1].d_equals_t; |
||
843 | trimarchi | 283 | server_list[i].Is = server_list[i+1].Is; |
417 | giacomo | 284 | |
405 | trimarchi | 285 | i++; |
286 | } |
||
410 | trimarchi | 287 | current_server--; |
405 | trimarchi | 288 | |
289 | |||
221 | giacomo | 290 | return 0; |
291 | |||
292 | } |
||
293 | |||
868 | trimarchi | 294 | int |
295 | fsf_get_renegotiation_status |
||
296 | (fsf_server_id_t server, |
||
297 | fsf_renegotiation_status_t *renegotiation_status) |
||
298 | { |
||
221 | giacomo | 299 | |
868 | trimarchi | 300 | if (SERVER_get_renegotiation_status(fsf_server_level,server)) |
301 | *renegotiation_status=FSF_IN_PROGRESS; |
||
302 | else |
||
303 | *renegotiation_status=FSF_ADMITTED; |
||
304 | |||
305 | return 0; |
||
306 | |||
307 | |||
308 | } |
||
309 | |||
310 | |||
221 | giacomo | 311 | int fsf_negotiate_contract |
312 | (const fsf_contract_parameters_t *contract, |
||
313 | fsf_server_id_t *server) |
||
314 | { |
||
679 | trimarchi | 315 | struct mess message; |
221 | giacomo | 316 | |
679 | trimarchi | 317 | // send response server is -1 if the operation fail |
318 | message.type=NEGOTIATE_CONTRACT; |
||
319 | memmove(&message.contract,contract, sizeof(fsf_contract_parameters_t)); |
||
221 | giacomo | 320 | |
681 | trimarchi | 321 | port_send(channel[1],&message,BLOCK); |
679 | trimarchi | 322 | |
681 | trimarchi | 323 | port_receive(channel[0], &message, BLOCK); |
868 | trimarchi | 324 | |
679 | trimarchi | 325 | if (message.server==-1) |
326 | return FSF_ERR_CONTRACT_REJECTED; |
||
221 | giacomo | 327 | |
679 | trimarchi | 328 | *server=message.server; |
329 | |||
221 | giacomo | 330 | return 0; |
331 | |||
332 | } |
||
333 | |||
868 | trimarchi | 334 | int fsf_unbind_thread_from_server |
335 | (pthread_t thread) |
||
336 | { |
||
337 | |||
338 | int local_scheduler_level, scheduler_id; |
||
339 | |||
340 | /* Move thread from the local scheduler module to posix level */ |
||
341 | |||
342 | #ifdef FSF_DEBUG |
||
343 | kern_printf("(UnBind thread = %d)",thread); |
||
344 | #endif |
||
345 | |||
346 | /* Check if thread exsists */ |
||
347 | if (thread == -1) |
||
348 | return FSF_ERR_BAD_ARGUMENT; |
||
349 | |||
350 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
||
351 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level,thread); |
||
352 | |||
353 | /* Check if thread is already bind */ |
||
354 | if (scheduler_id == FSF_NONE) { |
||
355 | /* Check if it is bind to a server */ |
||
356 | if (NONESTAR_getbudget(local_scheduler_level,thread) == -1) |
||
357 | return FSF_ERR_BAD_ARGUMENT; |
||
358 | else { |
||
359 | |||
360 | STD_command_message *msg; |
||
361 | NRT_TASK_MODEL nrt; |
||
362 | |||
363 | nrt_task_default_model(nrt); |
||
364 | nrt_task_def_save_arrivals(nrt); |
||
365 | |||
366 | /* Send change level command to local scheduler */ |
||
367 | msg = (STD_command_message *)malloc(sizeof(STD_command_message)); |
||
368 | |||
369 | msg->command = STD_SET_NEW_MODEL; |
||
370 | msg->param = (void *)(&nrt); |
||
371 | level_table[fsf_posix_level]->public_message(fsf_posix_level,thread,msg); |
||
372 | |||
373 | msg->command = STD_SET_NEW_LEVEL; |
||
374 | msg->param = (void *)(fsf_posix_level); |
||
375 | task_message(msg,thread,0); |
||
376 | |||
377 | free(msg); |
||
378 | } |
||
379 | } |
||
380 | |||
381 | return 0; |
||
382 | |||
383 | } |
||
384 | |||
385 | |||
386 | int fsf_bind_thread_to_server |
||
387 | (fsf_server_id_t server, |
||
388 | pthread_t thread) |
||
389 | { |
||
390 | |||
391 | STD_command_message *msg; |
||
392 | int local_scheduler_level,scheduler_id; |
||
393 | |||
394 | /* Move thread from the posix module to local scheduler */ |
||
395 | |||
396 | #ifdef FSF_DEBUG |
||
397 | kern_printf("(Bind thread = %d to Server = %d)",thread,server); |
||
398 | #endif |
||
399 | |||
400 | /* Check if server and thread exsist */ |
||
401 | if (server == -1 || thread == -1) |
||
402 | return FSF_ERR_BAD_ARGUMENT; |
||
403 | |||
404 | local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server); |
||
405 | scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server); |
||
406 | |||
407 | /* Check if thread is already bind */ |
||
408 | if (scheduler_id == FSF_NONE) { |
||
409 | DUMMY_TASK_MODEL rt_arg; |
||
410 | |||
411 | if (NONESTAR_getbudget(local_scheduler_level,thread) != -1) |
||
412 | return FSF_ERR_BAD_ARGUMENT; |
||
413 | |||
414 | /* Set server on local scheduler */ |
||
415 | NONESTAR_setbudget(local_scheduler_level,thread,(int)(server)); |
||
416 | |||
417 | /* Send change level command to posix level */ |
||
418 | msg = (STD_command_message *)malloc(sizeof(STD_command_message)); |
||
419 | |||
420 | msg->command = STD_SET_NEW_MODEL; |
||
421 | msg->param = (void *)(&rt_arg); |
||
422 | level_table[local_scheduler_level]->public_message(local_scheduler_level,thread,msg); |
||
423 | |||
424 | msg->command = STD_SET_NEW_LEVEL; |
||
425 | msg->param = (void *)(local_scheduler_level); |
||
426 | task_message(msg,thread,0); |
||
427 | |||
428 | free(msg); |
||
429 | } else return FSF_ERR_BAD_ARGUMENT; |
||
430 | |||
431 | return 0; |
||
432 | } |
||
433 | |||
241 | giacomo | 434 | int fsf_create_thread |
435 | (fsf_server_id_t server, |
||
436 | pthread_t *thread, |
||
437 | pthread_attr_t *attr, |
||
438 | fsf_thread_code_t thread_code, |
||
439 | void *arg, |
||
440 | void *local_scheduler_arg) |
||
221 | giacomo | 441 | { |
442 | |||
443 | int local_scheduler_level,scheduler_id; |
||
444 | |||
445 | /* Check if server and thread exsist */ |
||
241 | giacomo | 446 | if (server == NIL) |
447 | return FSF_ERR_INVALID_SERVER; |
||
221 | giacomo | 448 | |
241 | giacomo | 449 | local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server); |
450 | scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server); |
||
868 | trimarchi | 451 | #ifdef FSF_DEBUG |
452 | kern_printf("sched policy %d", scheduler_id); |
||
453 | #endif |
||
221 | giacomo | 454 | /* Check if thread is already bind */ |
455 | switch (scheduler_id) { |
||
868 | trimarchi | 456 | case FSF_POSIX: |
221 | giacomo | 457 | |
298 | giacomo | 458 | nrt_task_def_arg(*(NRT_TASK_MODEL *)(local_scheduler_arg),arg); |
459 | nrt_task_def_level(*(NRT_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
221 | giacomo | 460 | |
298 | giacomo | 461 | *thread = task_create("POSIXSTAR", thread_code, local_scheduler_arg, NULL); |
267 | giacomo | 462 | if (*thread == NIL) { |
463 | #ifdef FSF_DEBUG |
||
464 | kern_printf("(FSF:Error creating thread)"); |
||
465 | #endif |
||
241 | giacomo | 466 | return FSF_ERR_CREATE_THREAD; |
267 | giacomo | 467 | } |
221 | giacomo | 468 | |
241 | giacomo | 469 | POSIXSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
234 | giacomo | 470 | |
221 | giacomo | 471 | break; |
868 | trimarchi | 472 | case FSF_EDF: |
221 | giacomo | 473 | |
250 | giacomo | 474 | hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg); |
475 | hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
476 | |||
477 | *thread = task_create("EDFSTAR", thread_code, local_scheduler_arg, NULL); |
||
478 | if (*thread == NIL) |
||
479 | return FSF_ERR_CREATE_THREAD; |
||
480 | |||
481 | EDFSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
||
482 | |||
221 | giacomo | 483 | break; |
235 | giacomo | 484 | |
868 | trimarchi | 485 | case FSF_RM: |
235 | giacomo | 486 | |
273 | giacomo | 487 | hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg); |
488 | hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
489 | |||
490 | *thread = task_create("RMSTAR", thread_code, local_scheduler_arg, NULL); |
||
491 | if (*thread == NIL) |
||
492 | return FSF_ERR_CREATE_THREAD; |
||
493 | |||
494 | RMSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
||
495 | |||
496 | break; |
||
868 | trimarchi | 497 | case FSF_NONE: |
334 | giacomo | 498 | |
868 | trimarchi | 499 | //dummy_task_def_arg(*( DUMMY_TASK_MODEL *)(local_scheduler_arg),arg); |
500 | dummy_task_def_level(*( DUMMY_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
334 | giacomo | 501 | |
855 | trimarchi | 502 | *thread = task_create("NONESTAR", thread_code, local_scheduler_arg, NULL); |
334 | giacomo | 503 | if (*thread == NIL) |
504 | return FSF_ERR_CREATE_THREAD; |
||
505 | |||
855 | trimarchi | 506 | NONESTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
334 | giacomo | 507 | |
508 | break; |
||
509 | |||
221 | giacomo | 510 | default: |
241 | giacomo | 511 | return FSF_ERR_INVALID_SERVER; |
221 | giacomo | 512 | break; |
513 | } |
||
811 | trimarchi | 514 | |
515 | #ifdef FSF_DEBUG |
||
516 | kern_printf("(FSF:Insert thread = %d to Server = %d)",*thread,server); |
||
517 | #endif |
||
221 | giacomo | 518 | |
519 | return 0; |
||
520 | |||
521 | } |
||
522 | |||
808 | trimarchi | 523 | int fsf_settask_nopreemptive |
221 | giacomo | 524 | (fsf_server_id_t *server, |
525 | pthread_t thread) |
||
526 | { |
||
527 | int local_scheduler_level, scheduler_id; |
||
528 | |||
241 | giacomo | 529 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
530 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread); |
||
221 | giacomo | 531 | |
532 | switch (scheduler_id) { |
||
868 | trimarchi | 533 | case FSF_POSIX: |
816 | trimarchi | 534 | POSIXSTAR_set_nopreemtive_current(local_scheduler_level); |
535 | return 1; |
||
808 | trimarchi | 536 | break; |
868 | trimarchi | 537 | case FSF_EDF: |
808 | trimarchi | 538 | EDFSTAR_set_nopreemtive_current(local_scheduler_level); |
539 | return 1; |
||
540 | break; |
||
868 | trimarchi | 541 | case FSF_RM: |
812 | trimarchi | 542 | RMSTAR_set_nopreemtive_current(local_scheduler_level); |
543 | return 1; |
||
808 | trimarchi | 544 | break; |
868 | trimarchi | 545 | case FSF_NONE: |
808 | trimarchi | 546 | break; |
547 | default: |
||
548 | return -1; |
||
549 | } |
||
550 | return -1; |
||
551 | } |
||
552 | |||
553 | |||
554 | int fsf_settask_preemptive |
||
555 | (fsf_server_id_t *server, |
||
556 | pthread_t thread) |
||
557 | { |
||
558 | int local_scheduler_level, scheduler_id; |
||
559 | |||
560 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
||
561 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread); |
||
562 | |||
563 | switch (scheduler_id) { |
||
868 | trimarchi | 564 | case FSF_POSIX: |
823 | trimarchi | 565 | POSIXSTAR_unset_nopreemtive_current(local_scheduler_level); |
816 | trimarchi | 566 | return 1; |
808 | trimarchi | 567 | break; |
868 | trimarchi | 568 | case FSF_EDF: |
808 | trimarchi | 569 | EDFSTAR_unset_nopreemtive_current(local_scheduler_level); |
570 | return 1; |
||
571 | break; |
||
868 | trimarchi | 572 | case FSF_RM: |
812 | trimarchi | 573 | RMSTAR_unset_nopreemtive_current(local_scheduler_level); |
574 | return 1; |
||
808 | trimarchi | 575 | break; |
868 | trimarchi | 576 | case FSF_NONE: |
808 | trimarchi | 577 | break; |
578 | default: |
||
579 | return -1; |
||
580 | } |
||
581 | |||
582 | return -1; |
||
583 | |||
584 | } |
||
585 | |||
586 | |||
587 | int fsf_get_server |
||
868 | trimarchi | 588 | (pthread_t thread, |
589 | fsf_server_id_t *server) |
||
808 | trimarchi | 590 | { |
591 | int local_scheduler_level, scheduler_id; |
||
592 | |||
593 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
||
594 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread); |
||
595 | |||
596 | switch (scheduler_id) { |
||
868 | trimarchi | 597 | case FSF_POSIX: |
829 | giacomo | 598 | *server = POSIXSTAR_getbudget(local_scheduler_level,thread); |
599 | return 0; |
||
868 | trimarchi | 600 | case FSF_EDF: |
829 | giacomo | 601 | *server = EDFSTAR_getbudget(local_scheduler_level,thread); |
602 | return 0; |
||
868 | trimarchi | 603 | case FSF_RM: |
829 | giacomo | 604 | *server = RMSTAR_getbudget(local_scheduler_level,thread); |
605 | return 0; |
||
868 | trimarchi | 606 | case FSF_NONE: |
855 | trimarchi | 607 | *server = NONESTAR_getbudget(local_scheduler_level,thread); |
829 | giacomo | 608 | return 0; |
235 | giacomo | 609 | default: |
221 | giacomo | 610 | return -1; |
611 | } |
||
612 | |||
613 | return -1; |
||
614 | |||
615 | } |
||
616 | |||
339 | giacomo | 617 | int fsf_get_server_level(void) |
618 | { |
||
619 | |||
620 | return fsf_server_level; |
||
621 | |||
622 | } |
||
623 | |||
221 | giacomo | 624 | int fsf_cancel_contract |
868 | trimarchi | 625 | (fsf_server_id_t server) |
221 | giacomo | 626 | { |
627 | |||
628 | int local_scheduler_level, scheduler_id; |
||
411 | trimarchi | 629 | SYS_FLAGS f; |
630 | TIME T,Q; |
||
631 | int i=0; |
||
221 | giacomo | 632 | |
633 | #ifdef FSF_DEBUG |
||
868 | trimarchi | 634 | kern_printf("(Remove server %d)",server); |
221 | giacomo | 635 | #endif |
636 | |||
637 | /* Check server id */ |
||
868 | trimarchi | 638 | if (server < 0) |
221 | giacomo | 639 | return FSF_ERR_INVALID_SERVER; |
640 | |||
868 | trimarchi | 641 | local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server); |
642 | scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server); |
||
221 | giacomo | 643 | |
644 | switch (scheduler_id) { |
||
868 | trimarchi | 645 | case FSF_POSIX: |
221 | giacomo | 646 | |
647 | /* Check if some thread use the server */ |
||
868 | trimarchi | 648 | if(POSIXSTAR_budget_has_thread(local_scheduler_level,server)) |
221 | giacomo | 649 | return FSF_ERR_SERVER_USED; |
650 | |||
651 | break; |
||
868 | trimarchi | 652 | case FSF_EDF: |
235 | giacomo | 653 | /* Check if some thread use the server */ |
868 | trimarchi | 654 | if(EDFSTAR_budget_has_thread(local_scheduler_level,server)) |
235 | giacomo | 655 | return FSF_ERR_SERVER_USED; |
656 | break; |
||
657 | |||
868 | trimarchi | 658 | case FSF_RM: |
235 | giacomo | 659 | /* Check if some thread use the server */ |
868 | trimarchi | 660 | if(RMSTAR_budget_has_thread(local_scheduler_level,server)) |
235 | giacomo | 661 | return FSF_ERR_SERVER_USED; |
662 | |||
221 | giacomo | 663 | break; |
334 | giacomo | 664 | |
868 | trimarchi | 665 | case FSF_NONE: |
334 | giacomo | 666 | /* Check if some thread use the server */ |
868 | trimarchi | 667 | if(NONESTAR_budget_has_thread(local_scheduler_level,server)) |
334 | giacomo | 668 | return FSF_ERR_SERVER_USED; |
669 | |||
670 | break; |
||
671 | |||
221 | giacomo | 672 | } |
673 | |||
868 | trimarchi | 674 | SERVER_removebudget(fsf_server_level,server); |
221 | giacomo | 675 | |
676 | level_free_descriptor(local_scheduler_level); |
||
405 | trimarchi | 677 | |
868 | trimarchi | 678 | remove_contract(server); |
411 | trimarchi | 679 | |
680 | f=kern_fsave(); |
||
681 | if (recalculate_contract(fsf_max_bw)==-1) { |
||
682 | kern_frestore(f); |
||
683 | return FSF_ERR_CREATE_SERVER; |
||
684 | } |
||
685 | #ifdef FSF_DEBUG |
||
686 | kern_printf("(Adjust budget)"); |
||
687 | #endif |
||
688 | for (i=0; i<current_server; i++) { |
||
689 | mul32div32to32(MAX_BANDWIDTH,server_list[i].Cmin,server_list[i].U,T); |
||
417 | giacomo | 690 | if (T > server_list[i].Tmin ) { |
411 | trimarchi | 691 | server_list[i].actual_budget = server_list[i].Cmin; |
692 | server_list[i].actual_period = T; |
||
417 | giacomo | 693 | #ifdef FSF_DEBUG |
694 | kern_printf("(1 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period); |
||
695 | #endif |
||
661 | giacomo | 696 | |
697 | if (server_list[i].d_equals_t == TRUE) |
||
698 | adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, T); |
||
699 | else |
||
700 | adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, server_list[i].deadline); |
||
701 | |||
411 | trimarchi | 702 | } else { |
417 | giacomo | 703 | mul32div32to32(server_list[i].Tmin,server_list[i].U,MAX_BANDWIDTH,Q); |
411 | trimarchi | 704 | server_list[i].actual_budget = Q; |
417 | giacomo | 705 | server_list[i].actual_period = server_list[i].Tmin; |
706 | #ifdef FSF_DEBUG |
||
707 | kern_printf("(2 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period); |
||
708 | #endif |
||
661 | giacomo | 709 | |
710 | if (server_list[i].d_equals_t == TRUE) |
||
711 | adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].Tmin); |
||
712 | else |
||
713 | adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].deadline); |
||
714 | |||
411 | trimarchi | 715 | } |
416 | trimarchi | 716 | server_list[i].U=server_list[i].Umin; |
411 | trimarchi | 717 | |
718 | } |
||
221 | giacomo | 719 | |
405 | trimarchi | 720 | |
411 | trimarchi | 721 | kern_frestore(f); |
221 | giacomo | 722 | |
723 | return 0; |
||
411 | trimarchi | 724 | |
221 | giacomo | 725 | } |
726 | |||
405 | trimarchi | 727 | int recalculate_contract(bandwidth_t U) { |
416 | trimarchi | 728 | bandwidth_t current_bandwidth; |
418 | giacomo | 729 | unsigned int temp_U; |
416 | trimarchi | 730 | int Qt; |
405 | trimarchi | 731 | int isok=0; |
732 | int i=0; |
||
868 | trimarchi | 733 | int target_importance=FSF_DEFAULT_IMPORTANCE; |
734 | |||
735 | #define MAX_IMPORTANCE 5 |
||
405 | trimarchi | 736 | |
407 | giacomo | 737 | #ifdef FSF_DEBUG |
738 | int temp; |
||
739 | |||
740 | kern_printf("(Recalculate contract)"); |
||
741 | #endif |
||
405 | trimarchi | 742 | |
743 | /* The current bandwidth is the min bandwidth */ |
||
744 | //current_bandwidth=SERVER_return_bandwidth(fsf_server_level); |
||
745 | #ifdef FSF_DEBUG |
||
410 | trimarchi | 746 | kern_printf("(nserver %d)", current_server); |
405 | trimarchi | 747 | #endif |
748 | |||
749 | do { |
||
750 | current_bandwidth=0; |
||
751 | Qt=0; |
||
410 | trimarchi | 752 | for (i=0; i<current_server; i++) { |
843 | trimarchi | 753 | if (server_list[i].Is==target_importance |
754 | && server_list[i].U<server_list[i].Umax && server_list[i].Qs>0) |
||
405 | trimarchi | 755 | Qt+=server_list[i].Qs; |
416 | trimarchi | 756 | current_bandwidth+=server_list[i].U; |
868 | trimarchi | 757 | #ifdef FSF_DEBUG |
843 | trimarchi | 758 | kern_printf("(Qs %d, Qt %d, Is %d)", server_list[i].Qs, Qt,server_list[i].Is); |
868 | trimarchi | 759 | #endif |
405 | trimarchi | 760 | } |
413 | trimarchi | 761 | |
868 | trimarchi | 762 | #ifdef FSF_DEBUG |
405 | trimarchi | 763 | kern_printf("(Total Quality %d)", Qt); |
868 | trimarchi | 764 | #endif |
405 | trimarchi | 765 | isok=1; |
410 | trimarchi | 766 | for (i=0; i<current_server; i++) { |
843 | trimarchi | 767 | if (server_list[i].Is==target_importance && server_list[i].U<server_list[i].Umax && server_list[i].Qs>0) { |
416 | trimarchi | 768 | temp_U=server_list[i].U; |
769 | server_list[i].U=U-current_bandwidth; |
||
770 | mul32div32to32(server_list[i].U, server_list[i].Qs, Qt, server_list[i].U); |
||
771 | temp_U+=server_list[i].U; |
||
772 | |||
773 | if (temp_U<=server_list[i].Umin) { |
||
405 | trimarchi | 774 | server_list[i].U=server_list[i].Umin; |
416 | trimarchi | 775 | } else if (temp_U>server_list[i].Umax) { |
776 | server_list[i].U=server_list[i].Umax; |
||
405 | trimarchi | 777 | isok=0; |
778 | } else server_list[i].U=temp_U; |
||
779 | |||
780 | #ifdef FSF_DEBUG |
||
781 | mul32div32to32(server_list[i].U,100, MAX_BANDWIDTH, temp); |
||
782 | kern_printf("(Server %d bw %d)", server_list[i].server, temp); |
||
783 | #endif |
||
784 | } |
||
785 | } |
||
843 | trimarchi | 786 | target_importance++; |
868 | trimarchi | 787 | } while (!isok && target_importance<=MAX_IMPORTANCE); |
405 | trimarchi | 788 | |
789 | return 0; |
||
790 | } |
||
791 | |||
221 | giacomo | 792 | int fsf_renegotiate_contract |
793 | (const fsf_contract_parameters_t *new_contract, |
||
794 | fsf_server_id_t server) |
||
795 | { |
||
679 | trimarchi | 796 | |
797 | struct mess message; |
||
221 | giacomo | 798 | |
679 | trimarchi | 799 | // send response server is -1 if the operation fail |
800 | message.type=RENEGOTIATE_CONTRACT; |
||
801 | memmove(&message.contract,new_contract, sizeof(fsf_contract_parameters_t)); |
||
802 | message.server = server; |
||
221 | giacomo | 803 | |
859 | trimarchi | 804 | port_send(channel[1],&message,BLOCK); |
805 | |||
806 | port_receive(channel[0], &message, BLOCK); |
||
807 | |||
679 | trimarchi | 808 | if (message.server==-1) return FSF_ERR_CONTRACT_REJECTED; |
809 | |||
405 | trimarchi | 810 | return 0; |
221 | giacomo | 811 | } |
241 | giacomo | 812 | |
868 | trimarchi | 813 | int fsf_request_contract_renegotiation |
814 | (const fsf_contract_parameters_t *new_contract, |
||
815 | fsf_server_id_t server, |
||
816 | int sig_notify, |
||
817 | union sigval sig_value) |
||
818 | { |
||
819 | |||
820 | struct mess message; |
||
821 | |||
822 | // send response server is -1 if the operation fail |
||
823 | message.type=REQUEST_RENEGOTIATE_CONTRACT; |
||
824 | memmove(&message.contract,new_contract, sizeof(fsf_contract_parameters_t)); |
||
825 | message.server = server; |
||
826 | |||
827 | port_send(channel[1],&message,BLOCK); |
||
828 | |||
829 | port_receive(channel[0], &message, BLOCK); |
||
830 | |||
831 | if (message.server==-1) return FSF_ERR_CONTRACT_REJECTED; |
||
832 | |||
833 | return 0; |
||
834 | } |
||
835 | |||
407 | giacomo | 836 | void print_server_list() |
837 | { |
||
838 | |||
839 | int i; |
||
840 | |||
841 | kern_printf("Server List\n"); |
||
842 | |||
410 | trimarchi | 843 | for(i=0;i<current_server;i++) { |
407 | giacomo | 844 | |
662 | giacomo | 845 | 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 | 846 | |
847 | } |
||
848 | |||
849 | } |
||
808 | trimarchi | 850 | |
851 | |||
852 | int fsf_get_remain_budget(fsf_server_id_t server) { |
||
853 | |||
825 | trimarchi | 854 | return SERVER_get_remain_capacity(fsf_server_level, server); |
808 | trimarchi | 855 | } |
868 | trimarchi | 856 | |
857 | int fsf_get_budget_and_period |
||
858 | (fsf_server_id_t server, |
||
859 | struct timespec *budget, |
||
860 | struct timespec *period) { |
||
861 | TIME bg; |
||
862 | TIME pd; |
||
863 | |||
864 | if (!SERVER_getbudgetinfo(fsf_server_level, &bg, &pd, NULL, server)) { |
||
865 | if (budget) { |
||
866 | NULL_TIMESPEC(budget); |
||
867 | ADDUSEC2TIMESPEC(bg, budget); |
||
868 | } |
||
869 | if (period) { |
||
870 | NULL_TIMESPEC(period); |
||
871 | ADDUSEC2TIMESPEC(pd, period); |
||
872 | } |
||
873 | |||
874 | return 0; |
||
875 | } |
||
876 | return FSF_ERR_BAD_ARGUMENT; |
||
877 | } |
||
878 | |||
879 | int |
||
880 | fsf_set_service_thread_data |
||
881 | (const struct timespec *budget, |
||
882 | const struct timespec *period, |
||
883 | bool *accepted) { |
||
884 | |||
885 | if (budget==NULL && period==NULL) return FSF_ERR_BAD_ARGUMENT; |
||
886 | fsf_set_contract_basic_parameters(&service_contract,budget,period,FSF_DEFAULT_WORKLOAD); |
||
887 | *accepted = !fsf_renegotiate_contract(&service_contract,service_server)?true:false; |
||
888 | return 0; |
||
889 | |||
890 | } |
||
891 | |||
892 | |||
893 | |||
894 | int fsf_get_service_thread_data |
||
895 | (struct timespec *budget, |
||
896 | struct timespec *period) { |
||
897 | |||
898 | return fsf_get_budget_and_period(service_server, budget, period); |
||
899 | |||
900 | } |
||
901 |