Rev 941 | Rev 952 | 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 | |
881 | trimarchi | 20 | #include "fsf.h" |
679 | trimarchi | 21 | #include "fsf_service_task.h" |
896 | trimarchi | 22 | #include "fsf_server.h" |
679 | trimarchi | 23 | #include "message.h" |
221 | giacomo | 24 | |
868 | trimarchi | 25 | #include "posixstar.h" |
26 | #include "edfstar.h" |
||
27 | #include "nonestar.h" |
||
28 | #include "rmstar.h" |
||
944 | trimarchi | 29 | #include "tdstar.h" |
868 | trimarchi | 30 | |
221 | giacomo | 31 | #include <pthread.h> |
32 | #include <stdlib.h> |
||
808 | trimarchi | 33 | #include "pistar.h" |
868 | trimarchi | 34 | #include <modules/comm_message.h> |
221 | giacomo | 35 | |
417 | giacomo | 36 | //#define FSF_DEBUG |
221 | giacomo | 37 | |
410 | trimarchi | 38 | int current_server=0; |
407 | giacomo | 39 | server_elem server_list[FSF_MAX_N_SERVERS]; |
406 | giacomo | 40 | bandwidth_t fsf_max_bw = 0; |
405 | trimarchi | 41 | |
241 | giacomo | 42 | int fsf_server_level; |
868 | trimarchi | 43 | int fsf_posix_level = -1; |
808 | trimarchi | 44 | int shared_object_level; |
679 | trimarchi | 45 | PID server_task; |
868 | trimarchi | 46 | fsf_contract_parameters_t contract; |
221 | giacomo | 47 | |
868 | trimarchi | 48 | |
679 | trimarchi | 49 | PORT channel[2]; |
50 | |||
868 | trimarchi | 51 | fsf_server_id_t service_server = -1; |
52 | fsf_contract_parameters_t service_contract; |
||
53 | |||
896 | trimarchi | 54 | bandwidth_t SERVER_return_bandwidth(); |
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; |
881 | trimarchi | 82 | struct timespec default_period = FSF_SERVICE_THREAD_PERIOD; |
83 | struct timespec default_budget = FSF_SERVICE_THREAD_BUDGET; |
||
868 | trimarchi | 84 | DUMMY_TASK_MODEL m; |
881 | trimarchi | 85 | fsf_sched_params_t pr; |
937 | trimarchi | 86 | int budget_overrun_sig_notify = FSF_NULL_SIGNAL; |
87 | union sigval budget_overrun_sig_value = {0}; |
||
88 | int deadline_miss_sig_notify = FSF_NULL_SIGNAL; |
||
89 | union sigval deadline_miss_sig_value = {0}; |
||
868 | trimarchi | 90 | |
881 | trimarchi | 91 | pr.policy=FSF_NONE; |
92 | pr.params=&m; |
||
93 | |||
868 | trimarchi | 94 | dummy_task_default_model(m); |
941 | trimarchi | 95 | |
679 | trimarchi | 96 | // create the service task |
97 | // create the communication channel for negotiation and renegotiation |
||
98 | |||
99 | channel[1] = port_create("CHANW",sizeof(struct mess),1,STREAM,WRITE); |
||
100 | |||
101 | channel[0] = port_create("CHANR",sizeof(struct mess),1,STREAM,READ); |
||
102 | |||
868 | trimarchi | 103 | fsf_initialize_contract(&service_contract); |
941 | trimarchi | 104 | |
105 | err=fsf_set_contract_basic_parameters(&service_contract,&default_budget,&default_period,FSF_DEFAULT_WORKLOAD); |
||
106 | if (err) exit(err); |
||
868 | trimarchi | 107 | |
941 | trimarchi | 108 | err=fsf_set_contract_timing_requirements (&service_contract, |
937 | trimarchi | 109 | true,NULL, |
110 | budget_overrun_sig_notify, |
||
111 | budget_overrun_sig_value, |
||
112 | deadline_miss_sig_notify, |
||
113 | deadline_miss_sig_value); |
||
941 | trimarchi | 114 | if (err) exit(err); |
937 | trimarchi | 115 | |
868 | trimarchi | 116 | negotiate_contract(&service_contract,&service_server); |
117 | |||
118 | //server_task = task_create("stask",service_task,model,NULL); |
||
881 | trimarchi | 119 | err = fsf_create_local_thread(service_server,&pr, &server_task,NULL,(fsf_thread_code_t)service_task,NULL); |
868 | trimarchi | 120 | if (err) { |
679 | trimarchi | 121 | cprintf("error creating service task\n"); |
122 | sys_shutdown_message("Could not create service_task"); |
||
927 | pj | 123 | exit(1); |
679 | trimarchi | 124 | } |
125 | |||
126 | task_activate(server_task); |
||
127 | |||
128 | } |
||
129 | |||
130 | |||
221 | giacomo | 131 | /* Convert the contract specification to |
132 | * budget parameters |
||
133 | */ |
||
241 | giacomo | 134 | int set_SERVER_budget_from_contract |
221 | giacomo | 135 | (const fsf_contract_parameters_t *contract, |
136 | int *budget) |
||
137 | { |
||
138 | |||
139 | int local_scheduler_level = 0; |
||
140 | |||
868 | trimarchi | 141 | switch (contract->policy) { |
142 | case FSF_POSIX: |
||
241 | giacomo | 143 | local_scheduler_level = POSIXSTAR_register_level(fsf_server_level,5000,32); |
221 | giacomo | 144 | break; |
868 | trimarchi | 145 | case FSF_EDF: |
241 | giacomo | 146 | local_scheduler_level = EDFSTAR_register_level(fsf_server_level); |
221 | giacomo | 147 | break; |
868 | trimarchi | 148 | case FSF_RM: |
241 | giacomo | 149 | local_scheduler_level = RMSTAR_register_level(fsf_server_level); |
221 | giacomo | 150 | break; |
868 | trimarchi | 151 | case FSF_NONE: |
855 | trimarchi | 152 | local_scheduler_level = NONESTAR_register_level(fsf_server_level); |
334 | giacomo | 153 | break; |
944 | trimarchi | 154 | case FSF_TABLE_DRIVEN: |
155 | kern_printf("Register level"); |
||
156 | local_scheduler_level = TDSTAR_register_level(fsf_server_level); |
||
157 | break; |
||
221 | giacomo | 158 | } |
405 | trimarchi | 159 | |
908 | trimarchi | 160 | if (contract->d_equals_t == true) { |
241 | giacomo | 161 | *budget = SERVER_setbudget(fsf_server_level, |
221 | giacomo | 162 | TIMESPEC2USEC(&(contract->budget_min)), |
163 | TIMESPEC2USEC(&(contract->period_max)), |
||
661 | giacomo | 164 | TIMESPEC2USEC(&(contract->period_max)), |
868 | trimarchi | 165 | local_scheduler_level,contract->policy); |
661 | giacomo | 166 | } else { |
167 | *budget = SERVER_setbudget(fsf_server_level, |
||
168 | TIMESPEC2USEC(&(contract->budget_min)), |
||
169 | TIMESPEC2USEC(&(contract->period_max)), |
||
170 | TIMESPEC2USEC(&(contract->deadline)), |
||
868 | trimarchi | 171 | local_scheduler_level,contract->policy); |
661 | giacomo | 172 | } |
221 | giacomo | 173 | |
174 | return 0; |
||
175 | |||
176 | } |
||
177 | |||
405 | trimarchi | 178 | int adjust_SERVER_budget |
179 | (int budget, const TIME budget_actual, |
||
661 | giacomo | 180 | const TIME period_actual, const TIME dline_actual) |
221 | giacomo | 181 | { |
182 | |||
241 | giacomo | 183 | SERVER_adjust_budget(fsf_server_level, |
405 | trimarchi | 184 | budget_actual, |
185 | period_actual, |
||
661 | giacomo | 186 | dline_actual, |
241 | giacomo | 187 | budget); |
221 | giacomo | 188 | |
189 | return 0; |
||
190 | |||
191 | } |
||
192 | |||
193 | /* Admission Test function */ |
||
194 | int add_contract(const fsf_contract_parameters_t *contract) |
||
195 | { |
||
896 | trimarchi | 196 | bandwidth_t current_bandwidth,U; |
197 | |||
198 | TIME T,Q; |
||
897 | trimarchi | 199 | |
200 | #ifdef FSF_DEBUG |
||
201 | kern_printf("(GA TEST)"); |
||
202 | #endif |
||
896 | trimarchi | 203 | T=TIMESPEC2USEC(&(contract->period_max)); |
204 | Q=TIMESPEC2USEC(&(contract->budget_min)); |
||
205 | |||
206 | mul32div32to32(MAX_BANDWIDTH,Q,T,U); |
||
207 | /* The current bandwidth is the min bandwidth */ |
||
208 | current_bandwidth=SERVER_return_bandwidth(fsf_server_level); |
||
209 | |||
897 | trimarchi | 210 | if (fsf_max_bw<current_bandwidth+U) return -1; |
896 | trimarchi | 211 | |
221 | giacomo | 212 | return 0; |
213 | |||
214 | } |
||
215 | |||
687 | trimarchi | 216 | void contract_to_server(const fsf_contract_parameters_t *contract, int i) { |
217 | |||
410 | trimarchi | 218 | TIME T,Q; |
219 | #ifdef FSF_DEBUG |
||
220 | int temp; |
||
221 | #endif |
||
222 | |||
223 | T=TIMESPEC2USEC(&contract->period_min); |
||
224 | Q=TIMESPEC2USEC(&contract->budget_max); |
||
908 | trimarchi | 225 | server_list[i].Cmax=Q; |
226 | |||
410 | trimarchi | 227 | mul32div32to32(MAX_BANDWIDTH,Q,T,server_list[current_server].Umax); |
228 | |||
417 | giacomo | 229 | T=TIMESPEC2USEC(&contract->period_min); |
230 | server_list[i].Tmin=T; |
||
231 | |||
410 | trimarchi | 232 | T=TIMESPEC2USEC(&contract->period_max); |
233 | server_list[i].Tmax=T; |
||
417 | giacomo | 234 | |
410 | trimarchi | 235 | Q=TIMESPEC2USEC(&contract->budget_min); |
236 | server_list[i].Cmin=Q; |
||
237 | |||
238 | mul32div32to32(MAX_BANDWIDTH,Q,T,server_list[i].Umin); |
||
416 | trimarchi | 239 | server_list[i].U=server_list[i].Umin; |
661 | giacomo | 240 | |
908 | trimarchi | 241 | if (contract->d_equals_t == true) { |
661 | giacomo | 242 | server_list[i].deadline = 0; |
908 | trimarchi | 243 | server_list[i].d_equals_t = true; |
661 | giacomo | 244 | } else { |
245 | server_list[i].deadline = TIMESPEC2USEC(&contract->deadline);; |
||
908 | trimarchi | 246 | server_list[i].d_equals_t = false; |
661 | giacomo | 247 | } |
248 | |||
843 | trimarchi | 249 | server_list[i].Qs = contract->quality; |
250 | server_list[i].Is = contract->importance; |
||
251 | |||
410 | trimarchi | 252 | #ifdef FSF_DEBUG |
253 | mul32div32to32(server_list[i].Umax,100, MAX_BANDWIDTH, temp); |
||
254 | kern_printf("(Umax %d)",temp); |
||
255 | mul32div32to32(server_list[i].Umin,100, MAX_BANDWIDTH, temp); |
||
256 | kern_printf("(Umin %d)",temp); |
||
257 | #endif |
||
258 | |||
687 | trimarchi | 259 | } |
260 | |||
261 | int relink_contract_to_server(const fsf_contract_parameters_t *contract, |
||
262 | fsf_server_id_t server) |
||
263 | { |
||
264 | int i=0; |
||
265 | #ifdef FSF_DEBUG |
||
266 | kern_printf("(Relink Server %d)",server); |
||
267 | #endif |
||
268 | // find contract |
||
269 | while(i<current_server) { |
||
270 | if (server_list[i].server==server) break; |
||
271 | i++; |
||
272 | } |
||
273 | |||
274 | server_list[i].server=server; |
||
843 | trimarchi | 275 | // server_list[i].Qs=1; |
687 | trimarchi | 276 | |
277 | contract_to_server(contract, i); |
||
278 | |||
279 | |||
410 | trimarchi | 280 | return 0; |
281 | |||
282 | } |
||
283 | |||
221 | giacomo | 284 | int link_contract_to_server(const fsf_contract_parameters_t *contract, |
285 | fsf_server_id_t server) |
||
286 | { |
||
407 | giacomo | 287 | #ifdef FSF_DEBUG |
405 | trimarchi | 288 | kern_printf("(Link Server %d)",server); |
289 | #endif |
||
290 | |||
410 | trimarchi | 291 | server_list[current_server].server=server; |
843 | trimarchi | 292 | //server_list[current_server].Qs=1; |
405 | trimarchi | 293 | |
687 | trimarchi | 294 | contract_to_server(contract,current_server); |
405 | trimarchi | 295 | |
410 | trimarchi | 296 | current_server++; |
221 | giacomo | 297 | return 0; |
298 | |||
299 | } |
||
300 | |||
301 | int remove_contract(fsf_server_id_t server) |
||
302 | { |
||
405 | trimarchi | 303 | int i=0; |
304 | // find the contract |
||
410 | trimarchi | 305 | while(i<current_server) { |
405 | trimarchi | 306 | if (server_list[i].server==server) break; |
307 | i++; |
||
308 | } |
||
221 | giacomo | 309 | |
405 | trimarchi | 310 | // compress the array; |
410 | trimarchi | 311 | while (i<(current_server-1)) { |
405 | trimarchi | 312 | server_list[i].server=server_list[i+1].server; |
313 | server_list[i].Umin=server_list[i+1].Umin; |
||
416 | trimarchi | 314 | server_list[i].U=server_list[i+1].Umin; |
405 | trimarchi | 315 | server_list[i].Umax=server_list[i+1].Umax; |
316 | server_list[i].Cmin=server_list[i+1].Cmin; |
||
908 | trimarchi | 317 | server_list[i].Cmax=server_list[i+1].Cmax; |
417 | giacomo | 318 | server_list[i].Tmin=server_list[i+1].Tmin; |
405 | trimarchi | 319 | server_list[i].Tmax=server_list[i+1].Tmax; |
320 | server_list[i].Qs=server_list[i+1].Qs; |
||
661 | giacomo | 321 | server_list[i].deadline = server_list[i+1].deadline; |
322 | server_list[i].d_equals_t = server_list[i+1].d_equals_t; |
||
843 | trimarchi | 323 | server_list[i].Is = server_list[i+1].Is; |
417 | giacomo | 324 | |
405 | trimarchi | 325 | i++; |
326 | } |
||
410 | trimarchi | 327 | current_server--; |
405 | trimarchi | 328 | |
329 | |||
221 | giacomo | 330 | return 0; |
331 | |||
332 | } |
||
333 | |||
868 | trimarchi | 334 | int |
335 | fsf_get_renegotiation_status |
||
336 | (fsf_server_id_t server, |
||
337 | fsf_renegotiation_status_t *renegotiation_status) |
||
338 | { |
||
221 | giacomo | 339 | |
868 | trimarchi | 340 | if (SERVER_get_renegotiation_status(fsf_server_level,server)) |
341 | *renegotiation_status=FSF_IN_PROGRESS; |
||
342 | else |
||
343 | *renegotiation_status=FSF_ADMITTED; |
||
344 | |||
345 | return 0; |
||
346 | |||
347 | |||
348 | } |
||
349 | |||
896 | trimarchi | 350 | int |
351 | fsf_request_change_quality_and_importance |
||
352 | (fsf_server_id_t server, |
||
353 | int new_importance, |
||
354 | int new_quality) |
||
355 | { |
||
356 | struct mess m; |
||
868 | trimarchi | 357 | |
896 | trimarchi | 358 | // send response server is -1 if the operation fail |
359 | m.type=CHANGE_PARAMETER; |
||
908 | trimarchi | 360 | m.server=server; |
361 | m.qi.quality = new_quality; |
||
362 | m.qi.importance = new_importance; |
||
896 | trimarchi | 363 | //memmove(&m.contract,contract, sizeof(fsf_contract_parameters_t)); |
364 | |||
365 | port_send(channel[1],&m,BLOCK); |
||
366 | |||
367 | port_receive(channel[0], &m, BLOCK); |
||
368 | |||
369 | if (m.server==-1) |
||
370 | return FSF_ERR_CONTRACT_REJECTED; |
||
371 | |||
372 | //*server=m.server; |
||
373 | |||
908 | trimarchi | 374 | return 0; |
896 | trimarchi | 375 | } |
376 | |||
377 | |||
221 | giacomo | 378 | int fsf_negotiate_contract |
379 | (const fsf_contract_parameters_t *contract, |
||
380 | fsf_server_id_t *server) |
||
381 | { |
||
880 | trimarchi | 382 | struct mess m; |
221 | giacomo | 383 | |
679 | trimarchi | 384 | // send response server is -1 if the operation fail |
880 | trimarchi | 385 | m.type=NEGOTIATE_CONTRACT; |
386 | memmove(&m.contract,contract, sizeof(fsf_contract_parameters_t)); |
||
387 | //kern_printf("(SN:%d)",*server); |
||
388 | port_send(channel[1],&m,BLOCK); |
||
389 | //kern_printf("BR:%d)", *server); |
||
390 | port_receive(channel[0], &m, BLOCK); |
||
391 | //kern_printf("(EN:%d)", *server); |
||
392 | if (m.server==-1) |
||
679 | trimarchi | 393 | return FSF_ERR_CONTRACT_REJECTED; |
221 | giacomo | 394 | |
880 | trimarchi | 395 | *server=m.server; |
679 | trimarchi | 396 | |
221 | giacomo | 397 | return 0; |
398 | |||
399 | } |
||
400 | |||
872 | trimarchi | 401 | int |
402 | fsf_negotiate_contract_for_new_thread |
||
403 | (const fsf_contract_parameters_t *contract, |
||
404 | fsf_server_id_t *server, |
||
405 | pthread_t *thread, |
||
406 | pthread_attr_t *attr, |
||
407 | fsf_thread_code_t thread_code, |
||
408 | void *arg) { |
||
409 | |||
410 | int err=0; |
||
411 | |||
412 | err = fsf_negotiate_contract(contract,server); |
||
413 | if (!err) { |
||
414 | err = pthread_create(thread, attr, thread_code, arg); |
||
415 | if (!err) |
||
416 | err = fsf_bind_thread_to_server(*server,*thread); |
||
417 | } else return err; |
||
418 | |||
419 | return err; |
||
420 | } |
||
421 | |||
422 | int |
||
423 | fsf_negotiate_contract_for_myself |
||
424 | (const fsf_contract_parameters_t *contract, |
||
425 | fsf_server_id_t *server) { |
||
426 | |||
427 | int err=0; |
||
428 | |||
429 | err = fsf_negotiate_contract(contract,server); |
||
430 | if (!err) { |
||
937 | trimarchi | 431 | #ifdef FSF_DEBUG |
432 | kern_printf("Bind task"); |
||
433 | #endif |
||
872 | trimarchi | 434 | err = fsf_bind_thread_to_server(*server,exec_shadow); |
937 | trimarchi | 435 | |
872 | trimarchi | 436 | } else return err; |
437 | |||
438 | return err; |
||
439 | } |
||
440 | |||
868 | trimarchi | 441 | int fsf_unbind_thread_from_server |
442 | (pthread_t thread) |
||
443 | { |
||
444 | |||
445 | int local_scheduler_level, scheduler_id; |
||
446 | |||
447 | /* Move thread from the local scheduler module to posix level */ |
||
448 | |||
449 | #ifdef FSF_DEBUG |
||
450 | kern_printf("(UnBind thread = %d)",thread); |
||
451 | #endif |
||
452 | |||
453 | /* Check if thread exsists */ |
||
454 | if (thread == -1) |
||
455 | return FSF_ERR_BAD_ARGUMENT; |
||
456 | |||
457 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
||
458 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level,thread); |
||
459 | |||
460 | /* Check if thread is already bind */ |
||
461 | if (scheduler_id == FSF_NONE) { |
||
462 | /* Check if it is bind to a server */ |
||
463 | if (NONESTAR_getbudget(local_scheduler_level,thread) == -1) |
||
464 | return FSF_ERR_BAD_ARGUMENT; |
||
465 | else { |
||
466 | |||
467 | STD_command_message *msg; |
||
468 | NRT_TASK_MODEL nrt; |
||
469 | |||
470 | nrt_task_default_model(nrt); |
||
471 | nrt_task_def_save_arrivals(nrt); |
||
472 | |||
473 | /* Send change level command to local scheduler */ |
||
474 | msg = (STD_command_message *)malloc(sizeof(STD_command_message)); |
||
475 | |||
476 | msg->command = STD_SET_NEW_MODEL; |
||
477 | msg->param = (void *)(&nrt); |
||
478 | level_table[fsf_posix_level]->public_message(fsf_posix_level,thread,msg); |
||
479 | |||
480 | msg->command = STD_SET_NEW_LEVEL; |
||
481 | msg->param = (void *)(fsf_posix_level); |
||
482 | task_message(msg,thread,0); |
||
483 | |||
484 | free(msg); |
||
485 | } |
||
486 | } |
||
487 | |||
488 | return 0; |
||
489 | |||
490 | } |
||
491 | |||
873 | trimarchi | 492 | int |
493 | fsf_bind_local_thread_to_server |
||
494 | (fsf_server_id_t server, |
||
495 | pthread_t thread, |
||
496 | fsf_sched_params_t *sched_params) |
||
497 | { |
||
868 | trimarchi | 498 | |
873 | trimarchi | 499 | STD_command_message *msg; |
500 | int local_scheduler_level,scheduler_id; |
||
501 | |||
502 | /* Move thread from the posix module to local scheduler */ |
||
503 | |||
504 | #ifdef FSF_DEBUG |
||
505 | kern_printf("(Bind thread = %d to Server = %d)",thread,server); |
||
506 | #endif |
||
507 | |||
508 | /* Check if server and thread exsist */ |
||
509 | if (server == -1 || thread == -1) |
||
510 | return FSF_ERR_BAD_ARGUMENT; |
||
511 | |||
512 | local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server); |
||
513 | if (local_scheduler_level==-1) |
||
514 | return FSF_ERR_UNKNOWN_APPSCHEDULED_THREAD; |
||
515 | |||
516 | scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server); |
||
881 | trimarchi | 517 | |
518 | if (scheduler_id!=sched_params->policy) |
||
519 | return FSF_ERR_SCHED_POLICY_NOT_COMPATIBLE; |
||
873 | trimarchi | 520 | |
521 | /* Check if thread is already bind */ |
||
522 | switch(scheduler_id) { |
||
523 | |||
524 | case FSF_RM: |
||
525 | { |
||
881 | trimarchi | 526 | TASK_MODEL *m=(TASK_MODEL*)(sched_params->params); |
527 | HARD_TASK_MODEL *h=(HARD_TASK_MODEL *)(sched_params->params); |
||
873 | trimarchi | 528 | |
529 | if (m->pclass != HARD_PCLASS) |
||
530 | return FSF_ERR_SCHED_POLICY_NOT_COMPATIBLE; |
||
531 | |||
532 | h = (HARD_TASK_MODEL *)m; |
||
533 | |||
534 | if (!h->wcet || !h->mit) return FSF_ERR_SCHED_POLICY_NOT_COMPATIBLE; |
||
535 | |||
536 | /* now we know that m is a valid model */ |
||
537 | if (RMSTAR_getbudget(local_scheduler_level,thread) != -1) |
||
538 | return FSF_ERR_BAD_ARGUMENT; |
||
539 | |||
540 | /* Set server on local scheduler */ |
||
541 | RMSTAR_setbudget(local_scheduler_level,thread,(int)(server)); |
||
542 | |||
543 | /* Send change level command to posix level */ |
||
544 | } |
||
877 | trimarchi | 545 | break; |
546 | |||
873 | trimarchi | 547 | case FSF_EDF: |
548 | { |
||
881 | trimarchi | 549 | TASK_MODEL *m=(TASK_MODEL*)(sched_params->params); |
550 | HARD_TASK_MODEL *h=(HARD_TASK_MODEL *)(sched_params->params); |
||
873 | trimarchi | 551 | |
552 | if (m->pclass != HARD_PCLASS) |
||
553 | return FSF_ERR_SCHED_POLICY_NOT_COMPATIBLE; |
||
554 | |||
555 | h = (HARD_TASK_MODEL *)m; |
||
556 | |||
557 | if (!h->wcet || !h->mit) return FSF_ERR_SCHED_POLICY_NOT_COMPATIBLE; |
||
558 | |||
559 | if (EDFSTAR_getbudget(local_scheduler_level,thread) != -1) |
||
560 | return FSF_ERR_BAD_ARGUMENT; |
||
561 | |||
562 | /* Set server on local scheduler */ |
||
563 | EDFSTAR_setbudget(local_scheduler_level,thread,(int)(server)); |
||
564 | |||
565 | } |
||
566 | break; |
||
567 | |||
568 | case FSF_POSIX: |
||
569 | { |
||
881 | trimarchi | 570 | TASK_MODEL *m=(TASK_MODEL*)(sched_params->params); |
873 | trimarchi | 571 | |
572 | if (m->pclass != NRT_PCLASS) |
||
573 | return FSF_ERR_SCHED_POLICY_NOT_COMPATIBLE; |
||
574 | |||
575 | if (POSIXSTAR_getbudget(local_scheduler_level,thread) != -1) |
||
576 | return FSF_ERR_BAD_ARGUMENT; |
||
577 | |||
578 | /* Set server on local scheduler */ |
||
579 | POSIXSTAR_setbudget(local_scheduler_level,thread,(int)(server)); |
||
580 | } |
||
877 | trimarchi | 581 | break; |
873 | trimarchi | 582 | |
877 | trimarchi | 583 | default: |
584 | |||
873 | trimarchi | 585 | return FSF_ERR_BAD_ARGUMENT; |
877 | trimarchi | 586 | |
873 | trimarchi | 587 | } |
588 | |||
589 | msg = (STD_command_message *)malloc(sizeof(STD_command_message)); |
||
877 | trimarchi | 590 | if (msg) { |
591 | SYS_FLAGS f; |
||
592 | f=kern_fsave(); |
||
873 | trimarchi | 593 | msg->command = STD_SET_NEW_MODEL; |
881 | trimarchi | 594 | msg->param = (void *)(sched_params->params); |
873 | trimarchi | 595 | level_table[local_scheduler_level]->public_message(local_scheduler_level,thread,msg); |
596 | |||
597 | msg->command = STD_SET_NEW_LEVEL; |
||
598 | msg->param = (void *)(local_scheduler_level); |
||
599 | task_message(msg,thread,0); |
||
940 | trimarchi | 600 | scheduler(); |
601 | kern_context_load(proc_table[exec_shadow].context); |
||
877 | trimarchi | 602 | kern_frestore(f); |
873 | trimarchi | 603 | free(msg); |
604 | } else return FSF_ERR_INTERNAL_ERROR; |
||
605 | |||
606 | return 0; |
||
607 | |||
608 | } |
||
609 | |||
610 | |||
868 | trimarchi | 611 | int fsf_bind_thread_to_server |
612 | (fsf_server_id_t server, |
||
613 | pthread_t thread) |
||
614 | { |
||
615 | |||
616 | STD_command_message *msg; |
||
617 | int local_scheduler_level,scheduler_id; |
||
937 | trimarchi | 618 | SYS_FLAGS f; |
868 | trimarchi | 619 | /* Move thread from the posix module to local scheduler */ |
620 | |||
621 | #ifdef FSF_DEBUG |
||
880 | trimarchi | 622 | kern_printf("(Bthr=%d to Sr=%d)",thread,server); |
868 | trimarchi | 623 | #endif |
624 | |||
625 | /* Check if server and thread exsist */ |
||
626 | if (server == -1 || thread == -1) |
||
627 | return FSF_ERR_BAD_ARGUMENT; |
||
628 | |||
629 | local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server); |
||
873 | trimarchi | 630 | if (local_scheduler_level==-1) |
631 | return FSF_ERR_UNKNOWN_APPSCHEDULED_THREAD; |
||
632 | |||
868 | trimarchi | 633 | scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server); |
634 | |||
635 | /* Check if thread is already bind */ |
||
636 | if (scheduler_id == FSF_NONE) { |
||
637 | DUMMY_TASK_MODEL rt_arg; |
||
638 | |||
639 | if (NONESTAR_getbudget(local_scheduler_level,thread) != -1) |
||
640 | return FSF_ERR_BAD_ARGUMENT; |
||
641 | |||
642 | /* Set server on local scheduler */ |
||
643 | NONESTAR_setbudget(local_scheduler_level,thread,(int)(server)); |
||
937 | trimarchi | 644 | f=kern_fsave(); |
868 | trimarchi | 645 | /* Send change level command to posix level */ |
646 | msg = (STD_command_message *)malloc(sizeof(STD_command_message)); |
||
647 | |||
648 | msg->command = STD_SET_NEW_MODEL; |
||
649 | msg->param = (void *)(&rt_arg); |
||
937 | trimarchi | 650 | level_table[local_scheduler_level]->public_message(local_scheduler_level,thread,msg); |
868 | trimarchi | 651 | msg->command = STD_SET_NEW_LEVEL; |
652 | msg->param = (void *)(local_scheduler_level); |
||
653 | task_message(msg,thread,0); |
||
937 | trimarchi | 654 | |
655 | scheduler(); |
||
656 | kern_context_load(proc_table[exec_shadow].context); |
||
657 | kern_frestore(f); |
||
658 | |||
659 | |||
868 | trimarchi | 660 | free(msg); |
937 | trimarchi | 661 | |
868 | trimarchi | 662 | } else return FSF_ERR_BAD_ARGUMENT; |
663 | |||
664 | return 0; |
||
665 | } |
||
666 | |||
881 | trimarchi | 667 | int fsf_create_local_thread |
668 | (fsf_server_id_t server, |
||
669 | fsf_sched_params_t *local_scheduler_arg, |
||
670 | pthread_t *thread, |
||
671 | pthread_attr_t *attr, |
||
672 | fsf_thread_code_t thread_code, |
||
673 | void *arg) |
||
221 | giacomo | 674 | { |
675 | |||
676 | int local_scheduler_level,scheduler_id; |
||
677 | |||
678 | /* Check if server and thread exsist */ |
||
241 | giacomo | 679 | if (server == NIL) |
873 | trimarchi | 680 | return FSF_ERR_BAD_ARGUMENT; |
221 | giacomo | 681 | |
241 | giacomo | 682 | local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server); |
683 | scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server); |
||
868 | trimarchi | 684 | #ifdef FSF_DEBUG |
685 | kern_printf("sched policy %d", scheduler_id); |
||
686 | #endif |
||
888 | trimarchi | 687 | |
688 | if (scheduler_id!=local_scheduler_arg->policy) |
||
689 | return FSF_ERR_SCHED_POLICY_NOT_COMPATIBLE; |
||
690 | |||
221 | giacomo | 691 | /* Check if thread is already bind */ |
692 | switch (scheduler_id) { |
||
873 | trimarchi | 693 | case FSF_POSIX: |
221 | giacomo | 694 | |
888 | trimarchi | 695 | nrt_task_def_arg(*(NRT_TASK_MODEL *)(local_scheduler_arg->params),arg); |
696 | nrt_task_def_level(*(NRT_TASK_MODEL *)(local_scheduler_arg->params),local_scheduler_level); |
||
221 | giacomo | 697 | |
888 | trimarchi | 698 | *thread = task_create("POSIXSTAR", thread_code, local_scheduler_arg->params, NULL); |
267 | giacomo | 699 | if (*thread == NIL) { |
700 | #ifdef FSF_DEBUG |
||
701 | kern_printf("(FSF:Error creating thread)"); |
||
702 | #endif |
||
873 | trimarchi | 703 | return FSF_ERR_INTERNAL_ERROR; |
267 | giacomo | 704 | } |
221 | giacomo | 705 | |
241 | giacomo | 706 | POSIXSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
234 | giacomo | 707 | |
221 | giacomo | 708 | break; |
868 | trimarchi | 709 | case FSF_EDF: |
221 | giacomo | 710 | |
888 | trimarchi | 711 | hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg->params),arg); |
712 | hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg->params),local_scheduler_level); |
||
250 | giacomo | 713 | |
888 | trimarchi | 714 | *thread = task_create("EDFSTAR", thread_code, local_scheduler_arg->params, NULL); |
250 | giacomo | 715 | if (*thread == NIL) |
873 | trimarchi | 716 | return FSF_ERR_INTERNAL_ERROR; |
250 | giacomo | 717 | |
718 | EDFSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
||
719 | |||
221 | giacomo | 720 | break; |
235 | giacomo | 721 | |
868 | trimarchi | 722 | case FSF_RM: |
235 | giacomo | 723 | |
888 | trimarchi | 724 | hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg->params),arg); |
725 | hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg->params),local_scheduler_level); |
||
273 | giacomo | 726 | |
888 | trimarchi | 727 | *thread = task_create("RMSTAR", thread_code, local_scheduler_arg->params, NULL); |
273 | giacomo | 728 | if (*thread == NIL) |
873 | trimarchi | 729 | return FSF_ERR_INTERNAL_ERROR; |
273 | giacomo | 730 | |
731 | RMSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
||
732 | |||
733 | break; |
||
868 | trimarchi | 734 | case FSF_NONE: |
334 | giacomo | 735 | |
888 | trimarchi | 736 | dummy_task_def_arg(*( DUMMY_TASK_MODEL *)(local_scheduler_arg->params),arg); |
737 | dummy_task_def_level(*( DUMMY_TASK_MODEL *)(local_scheduler_arg->params),local_scheduler_level); |
||
910 | trimarchi | 738 | |
888 | trimarchi | 739 | *thread = task_create("NONESTAR", thread_code, local_scheduler_arg->params, NULL); |
334 | giacomo | 740 | if (*thread == NIL) |
873 | trimarchi | 741 | return FSF_ERR_INTERNAL_ERROR; |
334 | giacomo | 742 | |
855 | trimarchi | 743 | NONESTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
334 | giacomo | 744 | |
745 | break; |
||
910 | trimarchi | 746 | |
747 | case FSF_TABLE_DRIVEN: |
||
748 | { |
||
944 | trimarchi | 749 | HARD_TASK_MODEL ht; |
750 | hard_task_default_model(ht); |
||
751 | hard_task_def_aperiodic(ht); |
||
910 | trimarchi | 752 | |
944 | trimarchi | 753 | hard_task_def_arg(ht,arg); |
754 | hard_task_def_level(ht,local_scheduler_level); |
||
910 | trimarchi | 755 | |
944 | trimarchi | 756 | *thread = task_create("TDSTAR", thread_code, &ht, NULL); |
910 | trimarchi | 757 | if (*thread == NIL) |
758 | return FSF_ERR_INTERNAL_ERROR; |
||
759 | |||
930 | trimarchi | 760 | TDSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
944 | trimarchi | 761 | TDSTAR_settable(local_scheduler_level, (fsf_table_driven_params_t *)(local_scheduler_arg->params),*thread); |
910 | trimarchi | 762 | } |
763 | break; |
||
764 | |||
334 | giacomo | 765 | |
221 | giacomo | 766 | default: |
873 | trimarchi | 767 | return FSF_ERR_INTERNAL_ERROR; |
221 | giacomo | 768 | break; |
769 | } |
||
811 | trimarchi | 770 | |
771 | #ifdef FSF_DEBUG |
||
772 | kern_printf("(FSF:Insert thread = %d to Server = %d)",*thread,server); |
||
773 | #endif |
||
221 | giacomo | 774 | |
775 | return 0; |
||
776 | |||
777 | } |
||
778 | |||
808 | trimarchi | 779 | int fsf_settask_nopreemptive |
221 | giacomo | 780 | (fsf_server_id_t *server, |
781 | pthread_t thread) |
||
782 | { |
||
783 | int local_scheduler_level, scheduler_id; |
||
784 | |||
241 | giacomo | 785 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
786 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread); |
||
221 | giacomo | 787 | |
788 | switch (scheduler_id) { |
||
868 | trimarchi | 789 | case FSF_POSIX: |
816 | trimarchi | 790 | POSIXSTAR_set_nopreemtive_current(local_scheduler_level); |
791 | return 1; |
||
808 | trimarchi | 792 | break; |
868 | trimarchi | 793 | case FSF_EDF: |
808 | trimarchi | 794 | EDFSTAR_set_nopreemtive_current(local_scheduler_level); |
795 | return 1; |
||
796 | break; |
||
868 | trimarchi | 797 | case FSF_RM: |
812 | trimarchi | 798 | RMSTAR_set_nopreemtive_current(local_scheduler_level); |
799 | return 1; |
||
808 | trimarchi | 800 | break; |
868 | trimarchi | 801 | case FSF_NONE: |
808 | trimarchi | 802 | break; |
803 | default: |
||
804 | return -1; |
||
805 | } |
||
806 | return -1; |
||
807 | } |
||
808 | |||
809 | |||
810 | int fsf_settask_preemptive |
||
811 | (fsf_server_id_t *server, |
||
812 | pthread_t thread) |
||
813 | { |
||
814 | int local_scheduler_level, scheduler_id; |
||
815 | |||
816 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
||
817 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread); |
||
818 | |||
819 | switch (scheduler_id) { |
||
868 | trimarchi | 820 | case FSF_POSIX: |
823 | trimarchi | 821 | POSIXSTAR_unset_nopreemtive_current(local_scheduler_level); |
816 | trimarchi | 822 | return 1; |
808 | trimarchi | 823 | break; |
868 | trimarchi | 824 | case FSF_EDF: |
808 | trimarchi | 825 | EDFSTAR_unset_nopreemtive_current(local_scheduler_level); |
826 | return 1; |
||
827 | break; |
||
868 | trimarchi | 828 | case FSF_RM: |
812 | trimarchi | 829 | RMSTAR_unset_nopreemtive_current(local_scheduler_level); |
830 | return 1; |
||
808 | trimarchi | 831 | break; |
868 | trimarchi | 832 | case FSF_NONE: |
808 | trimarchi | 833 | break; |
834 | default: |
||
835 | return -1; |
||
836 | } |
||
837 | |||
838 | return -1; |
||
839 | |||
840 | } |
||
841 | |||
842 | |||
843 | int fsf_get_server |
||
868 | trimarchi | 844 | (pthread_t thread, |
845 | fsf_server_id_t *server) |
||
808 | trimarchi | 846 | { |
847 | int local_scheduler_level, scheduler_id; |
||
848 | |||
849 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
||
850 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread); |
||
851 | |||
852 | switch (scheduler_id) { |
||
868 | trimarchi | 853 | case FSF_POSIX: |
829 | giacomo | 854 | *server = POSIXSTAR_getbudget(local_scheduler_level,thread); |
855 | return 0; |
||
868 | trimarchi | 856 | case FSF_EDF: |
829 | giacomo | 857 | *server = EDFSTAR_getbudget(local_scheduler_level,thread); |
858 | return 0; |
||
868 | trimarchi | 859 | case FSF_RM: |
829 | giacomo | 860 | *server = RMSTAR_getbudget(local_scheduler_level,thread); |
861 | return 0; |
||
868 | trimarchi | 862 | case FSF_NONE: |
855 | trimarchi | 863 | *server = NONESTAR_getbudget(local_scheduler_level,thread); |
829 | giacomo | 864 | return 0; |
235 | giacomo | 865 | default: |
221 | giacomo | 866 | return -1; |
867 | } |
||
868 | |||
869 | return -1; |
||
870 | |||
871 | } |
||
872 | |||
339 | giacomo | 873 | int fsf_get_server_level(void) |
874 | { |
||
875 | |||
876 | return fsf_server_level; |
||
877 | |||
878 | } |
||
879 | |||
221 | giacomo | 880 | int fsf_cancel_contract |
868 | trimarchi | 881 | (fsf_server_id_t server) |
221 | giacomo | 882 | { |
883 | |||
884 | int local_scheduler_level, scheduler_id; |
||
411 | trimarchi | 885 | SYS_FLAGS f; |
886 | TIME T,Q; |
||
887 | int i=0; |
||
221 | giacomo | 888 | |
889 | #ifdef FSF_DEBUG |
||
868 | trimarchi | 890 | kern_printf("(Remove server %d)",server); |
221 | giacomo | 891 | #endif |
892 | |||
893 | /* Check server id */ |
||
868 | trimarchi | 894 | if (server < 0) |
873 | trimarchi | 895 | return FSF_ERR_BAD_ARGUMENT; |
221 | giacomo | 896 | |
868 | trimarchi | 897 | local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server); |
898 | scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server); |
||
221 | giacomo | 899 | |
900 | switch (scheduler_id) { |
||
868 | trimarchi | 901 | case FSF_POSIX: |
221 | giacomo | 902 | |
903 | /* Check if some thread use the server */ |
||
868 | trimarchi | 904 | if(POSIXSTAR_budget_has_thread(local_scheduler_level,server)) |
873 | trimarchi | 905 | return FSF_ERR_NOT_CONTRACTED_SERVER; |
221 | giacomo | 906 | |
907 | break; |
||
868 | trimarchi | 908 | case FSF_EDF: |
235 | giacomo | 909 | /* Check if some thread use the server */ |
868 | trimarchi | 910 | if(EDFSTAR_budget_has_thread(local_scheduler_level,server)) |
873 | trimarchi | 911 | return FSF_ERR_NOT_CONTRACTED_SERVER; |
235 | giacomo | 912 | break; |
913 | |||
868 | trimarchi | 914 | case FSF_RM: |
235 | giacomo | 915 | /* Check if some thread use the server */ |
868 | trimarchi | 916 | if(RMSTAR_budget_has_thread(local_scheduler_level,server)) |
873 | trimarchi | 917 | return FSF_ERR_NOT_CONTRACTED_SERVER; |
235 | giacomo | 918 | |
221 | giacomo | 919 | break; |
334 | giacomo | 920 | |
868 | trimarchi | 921 | case FSF_NONE: |
334 | giacomo | 922 | /* Check if some thread use the server */ |
868 | trimarchi | 923 | if(NONESTAR_budget_has_thread(local_scheduler_level,server)) |
873 | trimarchi | 924 | return FSF_ERR_NOT_CONTRACTED_SERVER; |
334 | giacomo | 925 | |
926 | break; |
||
927 | |||
221 | giacomo | 928 | } |
929 | |||
868 | trimarchi | 930 | SERVER_removebudget(fsf_server_level,server); |
221 | giacomo | 931 | |
932 | level_free_descriptor(local_scheduler_level); |
||
405 | trimarchi | 933 | |
868 | trimarchi | 934 | remove_contract(server); |
411 | trimarchi | 935 | |
936 | f=kern_fsave(); |
||
937 | if (recalculate_contract(fsf_max_bw)==-1) { |
||
938 | kern_frestore(f); |
||
873 | trimarchi | 939 | return FSF_ERR_INTERNAL_ERROR; |
411 | trimarchi | 940 | } |
941 | #ifdef FSF_DEBUG |
||
942 | kern_printf("(Adjust budget)"); |
||
943 | #endif |
||
944 | for (i=0; i<current_server; i++) { |
||
945 | mul32div32to32(MAX_BANDWIDTH,server_list[i].Cmin,server_list[i].U,T); |
||
417 | giacomo | 946 | if (T > server_list[i].Tmin ) { |
411 | trimarchi | 947 | server_list[i].actual_budget = server_list[i].Cmin; |
948 | server_list[i].actual_period = T; |
||
417 | giacomo | 949 | #ifdef FSF_DEBUG |
950 | kern_printf("(1 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period); |
||
951 | #endif |
||
661 | giacomo | 952 | |
908 | trimarchi | 953 | if (server_list[i].d_equals_t == true) |
661 | giacomo | 954 | adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, T); |
955 | else |
||
956 | adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, server_list[i].deadline); |
||
957 | |||
411 | trimarchi | 958 | } else { |
417 | giacomo | 959 | mul32div32to32(server_list[i].Tmin,server_list[i].U,MAX_BANDWIDTH,Q); |
411 | trimarchi | 960 | server_list[i].actual_budget = Q; |
417 | giacomo | 961 | server_list[i].actual_period = server_list[i].Tmin; |
962 | #ifdef FSF_DEBUG |
||
963 | kern_printf("(2 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period); |
||
964 | #endif |
||
661 | giacomo | 965 | |
908 | trimarchi | 966 | if (server_list[i].d_equals_t == true) |
661 | giacomo | 967 | adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].Tmin); |
968 | else |
||
969 | adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].deadline); |
||
970 | |||
411 | trimarchi | 971 | } |
416 | trimarchi | 972 | server_list[i].U=server_list[i].Umin; |
411 | trimarchi | 973 | |
974 | } |
||
221 | giacomo | 975 | |
405 | trimarchi | 976 | |
411 | trimarchi | 977 | kern_frestore(f); |
221 | giacomo | 978 | |
979 | return 0; |
||
411 | trimarchi | 980 | |
221 | giacomo | 981 | } |
982 | |||
881 | trimarchi | 983 | bandwidth_t SERVER_return_bandwidth() { |
984 | int i=0; |
||
985 | bandwidth_t U; |
||
986 | U=0; |
||
987 | for(i=0;i<current_server;i++) { |
||
988 | |||
989 | U+=server_list[i].Umin; |
||
990 | |||
991 | } |
||
992 | |||
993 | return U; |
||
994 | } |
||
995 | |||
405 | trimarchi | 996 | int recalculate_contract(bandwidth_t U) { |
416 | trimarchi | 997 | bandwidth_t current_bandwidth; |
418 | giacomo | 998 | unsigned int temp_U; |
416 | trimarchi | 999 | int Qt; |
405 | trimarchi | 1000 | int isok=0; |
1001 | int i=0; |
||
868 | trimarchi | 1002 | int target_importance=FSF_DEFAULT_IMPORTANCE; |
1003 | |||
1004 | #define MAX_IMPORTANCE 5 |
||
405 | trimarchi | 1005 | |
896 | trimarchi | 1006 | #ifdef FSF_DEBUG |
1007 | int temp; |
||
1008 | |||
908 | trimarchi | 1009 | kern_printf("(RC)"); |
896 | trimarchi | 1010 | #endif |
405 | trimarchi | 1011 | |
1012 | /* The current bandwidth is the min bandwidth */ |
||
881 | trimarchi | 1013 | current_bandwidth=SERVER_return_bandwidth(fsf_server_level); |
405 | trimarchi | 1014 | #ifdef FSF_DEBUG |
908 | trimarchi | 1015 | kern_printf("(SER%d)", current_server); |
405 | trimarchi | 1016 | #endif |
880 | trimarchi | 1017 | //kern_printf("(CS:%d)", current_server); |
405 | trimarchi | 1018 | do { |
1019 | current_bandwidth=0; |
||
1020 | Qt=0; |
||
410 | trimarchi | 1021 | for (i=0; i<current_server; i++) { |
843 | trimarchi | 1022 | if (server_list[i].Is==target_importance |
1023 | && server_list[i].U<server_list[i].Umax && server_list[i].Qs>0) |
||
405 | trimarchi | 1024 | Qt+=server_list[i].Qs; |
416 | trimarchi | 1025 | current_bandwidth+=server_list[i].U; |
868 | trimarchi | 1026 | #ifdef FSF_DEBUG |
843 | trimarchi | 1027 | kern_printf("(Qs %d, Qt %d, Is %d)", server_list[i].Qs, Qt,server_list[i].Is); |
868 | trimarchi | 1028 | #endif |
405 | trimarchi | 1029 | } |
413 | trimarchi | 1030 | |
868 | trimarchi | 1031 | #ifdef FSF_DEBUG |
908 | trimarchi | 1032 | kern_printf("(TQ%d)", Qt); |
868 | trimarchi | 1033 | #endif |
405 | trimarchi | 1034 | isok=1; |
410 | trimarchi | 1035 | for (i=0; i<current_server; i++) { |
843 | trimarchi | 1036 | if (server_list[i].Is==target_importance && server_list[i].U<server_list[i].Umax && server_list[i].Qs>0) { |
416 | trimarchi | 1037 | temp_U=server_list[i].U; |
1038 | server_list[i].U=U-current_bandwidth; |
||
1039 | mul32div32to32(server_list[i].U, server_list[i].Qs, Qt, server_list[i].U); |
||
1040 | temp_U+=server_list[i].U; |
||
1041 | |||
1042 | if (temp_U<=server_list[i].Umin) { |
||
405 | trimarchi | 1043 | server_list[i].U=server_list[i].Umin; |
416 | trimarchi | 1044 | } else if (temp_U>server_list[i].Umax) { |
1045 | server_list[i].U=server_list[i].Umax; |
||
405 | trimarchi | 1046 | isok=0; |
1047 | } else server_list[i].U=temp_U; |
||
1048 | |||
1049 | #ifdef FSF_DEBUG |
||
1050 | mul32div32to32(server_list[i].U,100, MAX_BANDWIDTH, temp); |
||
908 | trimarchi | 1051 | kern_printf("(SER %d BW %d)", server_list[i].server, temp); |
405 | trimarchi | 1052 | #endif |
1053 | } |
||
1054 | } |
||
843 | trimarchi | 1055 | target_importance++; |
880 | trimarchi | 1056 | } while (!isok || target_importance<=MAX_IMPORTANCE); |
1057 | //kern_printf("(RNDNL)"); |
||
405 | trimarchi | 1058 | return 0; |
1059 | } |
||
1060 | |||
937 | trimarchi | 1061 | |
1062 | int fsf_negotiate_group |
||
1063 | (const fsf_contracts_group_t *contracts_up, |
||
1064 | const fsf_servers_group_t *severs_down, |
||
1065 | fsf_servers_group_t *severs_up, |
||
1066 | bool *accepted) { |
||
1067 | |||
1068 | return 0; |
||
1069 | } |
||
1070 | |||
221 | giacomo | 1071 | int fsf_renegotiate_contract |
1072 | (const fsf_contract_parameters_t *new_contract, |
||
1073 | fsf_server_id_t server) |
||
1074 | { |
||
679 | trimarchi | 1075 | |
908 | trimarchi | 1076 | struct mess msg; |
221 | giacomo | 1077 | |
679 | trimarchi | 1078 | // send response server is -1 if the operation fail |
908 | trimarchi | 1079 | msg.type=RENEGOTIATE_CONTRACT; |
1080 | memmove(&msg.contract,new_contract, sizeof(fsf_contract_parameters_t)); |
||
1081 | msg.server = server; |
||
1082 | //kern_printf("(REN %d)", server); |
||
1083 | port_send(channel[1],&msg,BLOCK); |
||
221 | giacomo | 1084 | |
908 | trimarchi | 1085 | port_receive(channel[0], &msg, BLOCK); |
859 | trimarchi | 1086 | |
908 | trimarchi | 1087 | //kern_printf("(REN %d)", msg.server); |
1088 | if (msg.server==-1) return FSF_ERR_CONTRACT_REJECTED; |
||
679 | trimarchi | 1089 | |
405 | trimarchi | 1090 | return 0; |
221 | giacomo | 1091 | } |
241 | giacomo | 1092 | |
868 | trimarchi | 1093 | int fsf_request_contract_renegotiation |
1094 | (const fsf_contract_parameters_t *new_contract, |
||
1095 | fsf_server_id_t server, |
||
1096 | int sig_notify, |
||
1097 | union sigval sig_value) |
||
1098 | { |
||
1099 | |||
908 | trimarchi | 1100 | struct mess msg; |
868 | trimarchi | 1101 | |
1102 | // send response server is -1 if the operation fail |
||
908 | trimarchi | 1103 | msg.type=REQUEST_RENEGOTIATE_CONTRACT; |
1104 | memmove(&msg.contract,new_contract, sizeof(fsf_contract_parameters_t)); |
||
1105 | msg.server = server; |
||
910 | trimarchi | 1106 | msg.sig_notify=sig_notify; |
1107 | msg.sig_value=sig_value; |
||
1108 | msg.process=exec_shadow; |
||
868 | trimarchi | 1109 | |
908 | trimarchi | 1110 | port_send(channel[1],&msg,BLOCK); |
868 | trimarchi | 1111 | |
908 | trimarchi | 1112 | port_receive(channel[0], &msg, BLOCK); |
868 | trimarchi | 1113 | |
908 | trimarchi | 1114 | if (msg.server==-1) return FSF_ERR_CONTRACT_REJECTED; |
868 | trimarchi | 1115 | |
1116 | return 0; |
||
1117 | } |
||
1118 | |||
407 | giacomo | 1119 | void print_server_list() |
1120 | { |
||
1121 | |||
1122 | int i; |
||
1123 | |||
1124 | kern_printf("Server List\n"); |
||
1125 | |||
410 | trimarchi | 1126 | for(i=0;i<current_server;i++) { |
407 | giacomo | 1127 | |
662 | giacomo | 1128 | 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 | 1129 | |
1130 | } |
||
1131 | |||
1132 | } |
||
808 | trimarchi | 1133 | |
1134 | |||
1135 | int fsf_get_remain_budget(fsf_server_id_t server) { |
||
1136 | |||
825 | trimarchi | 1137 | return SERVER_get_remain_capacity(fsf_server_level, server); |
808 | trimarchi | 1138 | } |
868 | trimarchi | 1139 | |
1140 | int fsf_get_budget_and_period |
||
1141 | (fsf_server_id_t server, |
||
1142 | struct timespec *budget, |
||
1143 | struct timespec *period) { |
||
1144 | TIME bg; |
||
1145 | TIME pd; |
||
1146 | |||
1147 | if (!SERVER_getbudgetinfo(fsf_server_level, &bg, &pd, NULL, server)) { |
||
1148 | if (budget) { |
||
1149 | NULL_TIMESPEC(budget); |
||
1150 | ADDUSEC2TIMESPEC(bg, budget); |
||
1151 | } |
||
1152 | if (period) { |
||
1153 | NULL_TIMESPEC(period); |
||
1154 | ADDUSEC2TIMESPEC(pd, period); |
||
1155 | } |
||
1156 | |||
1157 | return 0; |
||
1158 | } |
||
1159 | return FSF_ERR_BAD_ARGUMENT; |
||
1160 | } |
||
1161 | |||
1162 | int |
||
1163 | fsf_set_service_thread_data |
||
1164 | (const struct timespec *budget, |
||
1165 | const struct timespec *period, |
||
1166 | bool *accepted) { |
||
1167 | |||
1168 | if (budget==NULL && period==NULL) return FSF_ERR_BAD_ARGUMENT; |
||
1169 | fsf_set_contract_basic_parameters(&service_contract,budget,period,FSF_DEFAULT_WORKLOAD); |
||
1170 | *accepted = !fsf_renegotiate_contract(&service_contract,service_server)?true:false; |
||
1171 | return 0; |
||
1172 | |||
1173 | } |
||
1174 | |||
1175 | |||
1176 | |||
1177 | int fsf_get_service_thread_data |
||
1178 | (struct timespec *budget, |
||
1179 | struct timespec *period) { |
||
1180 | |||
1181 | return fsf_get_budget_and_period(service_server, budget, period); |
||
1182 | |||
1183 | } |
||
944 | trimarchi | 1184 | |
1185 | int fsf_init_local_scheduler(fsf_server_id_t server, |
||
1186 | fsf_sched_init_info_t info) { |
||
1187 | int scheduler_id, local_scheduler_level; |
||
1188 | |||
1189 | struct timespec *duration=(struct timespec *)info; |
||
1190 | |||
1191 | local_scheduler_level=SERVER_get_local_scheduler_level_from_budget(fsf_server_level, server); |
||
1192 | scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server); |
||
1193 | |||
1194 | if (scheduler_id!=FSF_TABLE_DRIVEN) |
||
1195 | return FSF_ERR_SCHED_POLICY_NOT_COMPATIBLE; |
||
1196 | |||
1197 | if (duration->tv_sec < 0 || duration->tv_nsec > 1000000000) |
||
1198 | return FSF_ERR_BAD_ARGUMENT; |
||
1199 | |||
1200 | //TDSTAR_debugtable(local_scheduler_level); |
||
1201 | TDSTAR_start_simulation(local_scheduler_level); |
||
1202 | return 0; |
||
1203 | |||
1204 | } |