Rev 868 | Rev 873 | 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 | |||
872 | trimarchi | 334 | int |
335 | fsf_negotiate_contract_for_new_thread |
||
336 | (const fsf_contract_parameters_t *contract, |
||
337 | fsf_server_id_t *server, |
||
338 | pthread_t *thread, |
||
339 | pthread_attr_t *attr, |
||
340 | fsf_thread_code_t thread_code, |
||
341 | void *arg) { |
||
342 | |||
343 | int err=0; |
||
344 | |||
345 | err = fsf_negotiate_contract(contract,server); |
||
346 | if (!err) { |
||
347 | err = pthread_create(thread, attr, thread_code, arg); |
||
348 | if (!err) |
||
349 | err = fsf_bind_thread_to_server(*server,*thread); |
||
350 | } else return err; |
||
351 | |||
352 | return err; |
||
353 | } |
||
354 | |||
355 | int |
||
356 | fsf_negotiate_contract_for_myself |
||
357 | (const fsf_contract_parameters_t *contract, |
||
358 | fsf_server_id_t *server) { |
||
359 | |||
360 | int err=0; |
||
361 | |||
362 | err = fsf_negotiate_contract(contract,server); |
||
363 | if (!err) { |
||
364 | err = fsf_bind_thread_to_server(*server,exec_shadow); |
||
365 | } else return err; |
||
366 | |||
367 | return err; |
||
368 | } |
||
369 | |||
868 | trimarchi | 370 | int fsf_unbind_thread_from_server |
371 | (pthread_t thread) |
||
372 | { |
||
373 | |||
374 | int local_scheduler_level, scheduler_id; |
||
375 | |||
376 | /* Move thread from the local scheduler module to posix level */ |
||
377 | |||
378 | #ifdef FSF_DEBUG |
||
379 | kern_printf("(UnBind thread = %d)",thread); |
||
380 | #endif |
||
381 | |||
382 | /* Check if thread exsists */ |
||
383 | if (thread == -1) |
||
384 | return FSF_ERR_BAD_ARGUMENT; |
||
385 | |||
386 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
||
387 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level,thread); |
||
388 | |||
389 | /* Check if thread is already bind */ |
||
390 | if (scheduler_id == FSF_NONE) { |
||
391 | /* Check if it is bind to a server */ |
||
392 | if (NONESTAR_getbudget(local_scheduler_level,thread) == -1) |
||
393 | return FSF_ERR_BAD_ARGUMENT; |
||
394 | else { |
||
395 | |||
396 | STD_command_message *msg; |
||
397 | NRT_TASK_MODEL nrt; |
||
398 | |||
399 | nrt_task_default_model(nrt); |
||
400 | nrt_task_def_save_arrivals(nrt); |
||
401 | |||
402 | /* Send change level command to local scheduler */ |
||
403 | msg = (STD_command_message *)malloc(sizeof(STD_command_message)); |
||
404 | |||
405 | msg->command = STD_SET_NEW_MODEL; |
||
406 | msg->param = (void *)(&nrt); |
||
407 | level_table[fsf_posix_level]->public_message(fsf_posix_level,thread,msg); |
||
408 | |||
409 | msg->command = STD_SET_NEW_LEVEL; |
||
410 | msg->param = (void *)(fsf_posix_level); |
||
411 | task_message(msg,thread,0); |
||
412 | |||
413 | free(msg); |
||
414 | } |
||
415 | } |
||
416 | |||
417 | return 0; |
||
418 | |||
419 | } |
||
420 | |||
421 | |||
422 | int fsf_bind_thread_to_server |
||
423 | (fsf_server_id_t server, |
||
424 | pthread_t thread) |
||
425 | { |
||
426 | |||
427 | STD_command_message *msg; |
||
428 | int local_scheduler_level,scheduler_id; |
||
429 | |||
430 | /* Move thread from the posix module to local scheduler */ |
||
431 | |||
432 | #ifdef FSF_DEBUG |
||
433 | kern_printf("(Bind thread = %d to Server = %d)",thread,server); |
||
434 | #endif |
||
435 | |||
436 | /* Check if server and thread exsist */ |
||
437 | if (server == -1 || thread == -1) |
||
438 | return FSF_ERR_BAD_ARGUMENT; |
||
439 | |||
440 | local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server); |
||
441 | scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server); |
||
442 | |||
443 | /* Check if thread is already bind */ |
||
444 | if (scheduler_id == FSF_NONE) { |
||
445 | DUMMY_TASK_MODEL rt_arg; |
||
446 | |||
447 | if (NONESTAR_getbudget(local_scheduler_level,thread) != -1) |
||
448 | return FSF_ERR_BAD_ARGUMENT; |
||
449 | |||
450 | /* Set server on local scheduler */ |
||
451 | NONESTAR_setbudget(local_scheduler_level,thread,(int)(server)); |
||
452 | |||
453 | /* Send change level command to posix level */ |
||
454 | msg = (STD_command_message *)malloc(sizeof(STD_command_message)); |
||
455 | |||
456 | msg->command = STD_SET_NEW_MODEL; |
||
457 | msg->param = (void *)(&rt_arg); |
||
458 | level_table[local_scheduler_level]->public_message(local_scheduler_level,thread,msg); |
||
459 | |||
460 | msg->command = STD_SET_NEW_LEVEL; |
||
461 | msg->param = (void *)(local_scheduler_level); |
||
462 | task_message(msg,thread,0); |
||
463 | |||
464 | free(msg); |
||
465 | } else return FSF_ERR_BAD_ARGUMENT; |
||
466 | |||
467 | return 0; |
||
468 | } |
||
469 | |||
241 | giacomo | 470 | int fsf_create_thread |
471 | (fsf_server_id_t server, |
||
472 | pthread_t *thread, |
||
473 | pthread_attr_t *attr, |
||
474 | fsf_thread_code_t thread_code, |
||
475 | void *arg, |
||
476 | void *local_scheduler_arg) |
||
221 | giacomo | 477 | { |
478 | |||
479 | int local_scheduler_level,scheduler_id; |
||
480 | |||
481 | /* Check if server and thread exsist */ |
||
241 | giacomo | 482 | if (server == NIL) |
483 | return FSF_ERR_INVALID_SERVER; |
||
221 | giacomo | 484 | |
241 | giacomo | 485 | local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server); |
486 | scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server); |
||
868 | trimarchi | 487 | #ifdef FSF_DEBUG |
488 | kern_printf("sched policy %d", scheduler_id); |
||
489 | #endif |
||
221 | giacomo | 490 | /* Check if thread is already bind */ |
491 | switch (scheduler_id) { |
||
868 | trimarchi | 492 | case FSF_POSIX: |
221 | giacomo | 493 | |
298 | giacomo | 494 | nrt_task_def_arg(*(NRT_TASK_MODEL *)(local_scheduler_arg),arg); |
495 | nrt_task_def_level(*(NRT_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
221 | giacomo | 496 | |
298 | giacomo | 497 | *thread = task_create("POSIXSTAR", thread_code, local_scheduler_arg, NULL); |
267 | giacomo | 498 | if (*thread == NIL) { |
499 | #ifdef FSF_DEBUG |
||
500 | kern_printf("(FSF:Error creating thread)"); |
||
501 | #endif |
||
241 | giacomo | 502 | return FSF_ERR_CREATE_THREAD; |
267 | giacomo | 503 | } |
221 | giacomo | 504 | |
241 | giacomo | 505 | POSIXSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
234 | giacomo | 506 | |
221 | giacomo | 507 | break; |
868 | trimarchi | 508 | case FSF_EDF: |
221 | giacomo | 509 | |
250 | giacomo | 510 | hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg); |
511 | hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
512 | |||
513 | *thread = task_create("EDFSTAR", thread_code, local_scheduler_arg, NULL); |
||
514 | if (*thread == NIL) |
||
515 | return FSF_ERR_CREATE_THREAD; |
||
516 | |||
517 | EDFSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
||
518 | |||
221 | giacomo | 519 | break; |
235 | giacomo | 520 | |
868 | trimarchi | 521 | case FSF_RM: |
235 | giacomo | 522 | |
273 | giacomo | 523 | hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg); |
524 | hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
525 | |||
526 | *thread = task_create("RMSTAR", thread_code, local_scheduler_arg, NULL); |
||
527 | if (*thread == NIL) |
||
528 | return FSF_ERR_CREATE_THREAD; |
||
529 | |||
530 | RMSTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
||
531 | |||
532 | break; |
||
868 | trimarchi | 533 | case FSF_NONE: |
334 | giacomo | 534 | |
868 | trimarchi | 535 | //dummy_task_def_arg(*( DUMMY_TASK_MODEL *)(local_scheduler_arg),arg); |
536 | dummy_task_def_level(*( DUMMY_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level); |
||
334 | giacomo | 537 | |
855 | trimarchi | 538 | *thread = task_create("NONESTAR", thread_code, local_scheduler_arg, NULL); |
334 | giacomo | 539 | if (*thread == NIL) |
540 | return FSF_ERR_CREATE_THREAD; |
||
541 | |||
855 | trimarchi | 542 | NONESTAR_setbudget(local_scheduler_level, *thread, (int)(server)); |
334 | giacomo | 543 | |
544 | break; |
||
545 | |||
221 | giacomo | 546 | default: |
241 | giacomo | 547 | return FSF_ERR_INVALID_SERVER; |
221 | giacomo | 548 | break; |
549 | } |
||
811 | trimarchi | 550 | |
551 | #ifdef FSF_DEBUG |
||
552 | kern_printf("(FSF:Insert thread = %d to Server = %d)",*thread,server); |
||
553 | #endif |
||
221 | giacomo | 554 | |
555 | return 0; |
||
556 | |||
557 | } |
||
558 | |||
808 | trimarchi | 559 | int fsf_settask_nopreemptive |
221 | giacomo | 560 | (fsf_server_id_t *server, |
561 | pthread_t thread) |
||
562 | { |
||
563 | int local_scheduler_level, scheduler_id; |
||
564 | |||
241 | giacomo | 565 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
566 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread); |
||
221 | giacomo | 567 | |
568 | switch (scheduler_id) { |
||
868 | trimarchi | 569 | case FSF_POSIX: |
816 | trimarchi | 570 | POSIXSTAR_set_nopreemtive_current(local_scheduler_level); |
571 | return 1; |
||
808 | trimarchi | 572 | break; |
868 | trimarchi | 573 | case FSF_EDF: |
808 | trimarchi | 574 | EDFSTAR_set_nopreemtive_current(local_scheduler_level); |
575 | return 1; |
||
576 | break; |
||
868 | trimarchi | 577 | case FSF_RM: |
812 | trimarchi | 578 | RMSTAR_set_nopreemtive_current(local_scheduler_level); |
579 | return 1; |
||
808 | trimarchi | 580 | break; |
868 | trimarchi | 581 | case FSF_NONE: |
808 | trimarchi | 582 | break; |
583 | default: |
||
584 | return -1; |
||
585 | } |
||
586 | return -1; |
||
587 | } |
||
588 | |||
589 | |||
590 | int fsf_settask_preemptive |
||
591 | (fsf_server_id_t *server, |
||
592 | pthread_t thread) |
||
593 | { |
||
594 | int local_scheduler_level, scheduler_id; |
||
595 | |||
596 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
||
597 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread); |
||
598 | |||
599 | switch (scheduler_id) { |
||
868 | trimarchi | 600 | case FSF_POSIX: |
823 | trimarchi | 601 | POSIXSTAR_unset_nopreemtive_current(local_scheduler_level); |
816 | trimarchi | 602 | return 1; |
808 | trimarchi | 603 | break; |
868 | trimarchi | 604 | case FSF_EDF: |
808 | trimarchi | 605 | EDFSTAR_unset_nopreemtive_current(local_scheduler_level); |
606 | return 1; |
||
607 | break; |
||
868 | trimarchi | 608 | case FSF_RM: |
812 | trimarchi | 609 | RMSTAR_unset_nopreemtive_current(local_scheduler_level); |
610 | return 1; |
||
808 | trimarchi | 611 | break; |
868 | trimarchi | 612 | case FSF_NONE: |
808 | trimarchi | 613 | break; |
614 | default: |
||
615 | return -1; |
||
616 | } |
||
617 | |||
618 | return -1; |
||
619 | |||
620 | } |
||
621 | |||
622 | |||
623 | int fsf_get_server |
||
868 | trimarchi | 624 | (pthread_t thread, |
625 | fsf_server_id_t *server) |
||
808 | trimarchi | 626 | { |
627 | int local_scheduler_level, scheduler_id; |
||
628 | |||
629 | local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread); |
||
630 | scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread); |
||
631 | |||
632 | switch (scheduler_id) { |
||
868 | trimarchi | 633 | case FSF_POSIX: |
829 | giacomo | 634 | *server = POSIXSTAR_getbudget(local_scheduler_level,thread); |
635 | return 0; |
||
868 | trimarchi | 636 | case FSF_EDF: |
829 | giacomo | 637 | *server = EDFSTAR_getbudget(local_scheduler_level,thread); |
638 | return 0; |
||
868 | trimarchi | 639 | case FSF_RM: |
829 | giacomo | 640 | *server = RMSTAR_getbudget(local_scheduler_level,thread); |
641 | return 0; |
||
868 | trimarchi | 642 | case FSF_NONE: |
855 | trimarchi | 643 | *server = NONESTAR_getbudget(local_scheduler_level,thread); |
829 | giacomo | 644 | return 0; |
235 | giacomo | 645 | default: |
221 | giacomo | 646 | return -1; |
647 | } |
||
648 | |||
649 | return -1; |
||
650 | |||
651 | } |
||
652 | |||
339 | giacomo | 653 | int fsf_get_server_level(void) |
654 | { |
||
655 | |||
656 | return fsf_server_level; |
||
657 | |||
658 | } |
||
659 | |||
221 | giacomo | 660 | int fsf_cancel_contract |
868 | trimarchi | 661 | (fsf_server_id_t server) |
221 | giacomo | 662 | { |
663 | |||
664 | int local_scheduler_level, scheduler_id; |
||
411 | trimarchi | 665 | SYS_FLAGS f; |
666 | TIME T,Q; |
||
667 | int i=0; |
||
221 | giacomo | 668 | |
669 | #ifdef FSF_DEBUG |
||
868 | trimarchi | 670 | kern_printf("(Remove server %d)",server); |
221 | giacomo | 671 | #endif |
672 | |||
673 | /* Check server id */ |
||
868 | trimarchi | 674 | if (server < 0) |
221 | giacomo | 675 | return FSF_ERR_INVALID_SERVER; |
676 | |||
868 | trimarchi | 677 | local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server); |
678 | scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server); |
||
221 | giacomo | 679 | |
680 | switch (scheduler_id) { |
||
868 | trimarchi | 681 | case FSF_POSIX: |
221 | giacomo | 682 | |
683 | /* Check if some thread use the server */ |
||
868 | trimarchi | 684 | if(POSIXSTAR_budget_has_thread(local_scheduler_level,server)) |
221 | giacomo | 685 | return FSF_ERR_SERVER_USED; |
686 | |||
687 | break; |
||
868 | trimarchi | 688 | case FSF_EDF: |
235 | giacomo | 689 | /* Check if some thread use the server */ |
868 | trimarchi | 690 | if(EDFSTAR_budget_has_thread(local_scheduler_level,server)) |
235 | giacomo | 691 | return FSF_ERR_SERVER_USED; |
692 | break; |
||
693 | |||
868 | trimarchi | 694 | case FSF_RM: |
235 | giacomo | 695 | /* Check if some thread use the server */ |
868 | trimarchi | 696 | if(RMSTAR_budget_has_thread(local_scheduler_level,server)) |
235 | giacomo | 697 | return FSF_ERR_SERVER_USED; |
698 | |||
221 | giacomo | 699 | break; |
334 | giacomo | 700 | |
868 | trimarchi | 701 | case FSF_NONE: |
334 | giacomo | 702 | /* Check if some thread use the server */ |
868 | trimarchi | 703 | if(NONESTAR_budget_has_thread(local_scheduler_level,server)) |
334 | giacomo | 704 | return FSF_ERR_SERVER_USED; |
705 | |||
706 | break; |
||
707 | |||
221 | giacomo | 708 | } |
709 | |||
868 | trimarchi | 710 | SERVER_removebudget(fsf_server_level,server); |
221 | giacomo | 711 | |
712 | level_free_descriptor(local_scheduler_level); |
||
405 | trimarchi | 713 | |
868 | trimarchi | 714 | remove_contract(server); |
411 | trimarchi | 715 | |
716 | f=kern_fsave(); |
||
717 | if (recalculate_contract(fsf_max_bw)==-1) { |
||
718 | kern_frestore(f); |
||
719 | return FSF_ERR_CREATE_SERVER; |
||
720 | } |
||
721 | #ifdef FSF_DEBUG |
||
722 | kern_printf("(Adjust budget)"); |
||
723 | #endif |
||
724 | for (i=0; i<current_server; i++) { |
||
725 | mul32div32to32(MAX_BANDWIDTH,server_list[i].Cmin,server_list[i].U,T); |
||
417 | giacomo | 726 | if (T > server_list[i].Tmin ) { |
411 | trimarchi | 727 | server_list[i].actual_budget = server_list[i].Cmin; |
728 | server_list[i].actual_period = T; |
||
417 | giacomo | 729 | #ifdef FSF_DEBUG |
730 | kern_printf("(1 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period); |
||
731 | #endif |
||
661 | giacomo | 732 | |
733 | if (server_list[i].d_equals_t == TRUE) |
||
734 | adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, T); |
||
735 | else |
||
736 | adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, server_list[i].deadline); |
||
737 | |||
411 | trimarchi | 738 | } else { |
417 | giacomo | 739 | mul32div32to32(server_list[i].Tmin,server_list[i].U,MAX_BANDWIDTH,Q); |
411 | trimarchi | 740 | server_list[i].actual_budget = Q; |
417 | giacomo | 741 | server_list[i].actual_period = server_list[i].Tmin; |
742 | #ifdef FSF_DEBUG |
||
743 | kern_printf("(2 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period); |
||
744 | #endif |
||
661 | giacomo | 745 | |
746 | if (server_list[i].d_equals_t == TRUE) |
||
747 | adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].Tmin); |
||
748 | else |
||
749 | adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].deadline); |
||
750 | |||
411 | trimarchi | 751 | } |
416 | trimarchi | 752 | server_list[i].U=server_list[i].Umin; |
411 | trimarchi | 753 | |
754 | } |
||
221 | giacomo | 755 | |
405 | trimarchi | 756 | |
411 | trimarchi | 757 | kern_frestore(f); |
221 | giacomo | 758 | |
759 | return 0; |
||
411 | trimarchi | 760 | |
221 | giacomo | 761 | } |
762 | |||
405 | trimarchi | 763 | int recalculate_contract(bandwidth_t U) { |
416 | trimarchi | 764 | bandwidth_t current_bandwidth; |
418 | giacomo | 765 | unsigned int temp_U; |
416 | trimarchi | 766 | int Qt; |
405 | trimarchi | 767 | int isok=0; |
768 | int i=0; |
||
868 | trimarchi | 769 | int target_importance=FSF_DEFAULT_IMPORTANCE; |
770 | |||
771 | #define MAX_IMPORTANCE 5 |
||
405 | trimarchi | 772 | |
407 | giacomo | 773 | #ifdef FSF_DEBUG |
774 | int temp; |
||
775 | |||
776 | kern_printf("(Recalculate contract)"); |
||
777 | #endif |
||
405 | trimarchi | 778 | |
779 | /* The current bandwidth is the min bandwidth */ |
||
780 | //current_bandwidth=SERVER_return_bandwidth(fsf_server_level); |
||
781 | #ifdef FSF_DEBUG |
||
410 | trimarchi | 782 | kern_printf("(nserver %d)", current_server); |
405 | trimarchi | 783 | #endif |
784 | |||
785 | do { |
||
786 | current_bandwidth=0; |
||
787 | Qt=0; |
||
410 | trimarchi | 788 | for (i=0; i<current_server; i++) { |
843 | trimarchi | 789 | if (server_list[i].Is==target_importance |
790 | && server_list[i].U<server_list[i].Umax && server_list[i].Qs>0) |
||
405 | trimarchi | 791 | Qt+=server_list[i].Qs; |
416 | trimarchi | 792 | current_bandwidth+=server_list[i].U; |
868 | trimarchi | 793 | #ifdef FSF_DEBUG |
843 | trimarchi | 794 | kern_printf("(Qs %d, Qt %d, Is %d)", server_list[i].Qs, Qt,server_list[i].Is); |
868 | trimarchi | 795 | #endif |
405 | trimarchi | 796 | } |
413 | trimarchi | 797 | |
868 | trimarchi | 798 | #ifdef FSF_DEBUG |
405 | trimarchi | 799 | kern_printf("(Total Quality %d)", Qt); |
868 | trimarchi | 800 | #endif |
405 | trimarchi | 801 | isok=1; |
410 | trimarchi | 802 | for (i=0; i<current_server; i++) { |
843 | trimarchi | 803 | if (server_list[i].Is==target_importance && server_list[i].U<server_list[i].Umax && server_list[i].Qs>0) { |
416 | trimarchi | 804 | temp_U=server_list[i].U; |
805 | server_list[i].U=U-current_bandwidth; |
||
806 | mul32div32to32(server_list[i].U, server_list[i].Qs, Qt, server_list[i].U); |
||
807 | temp_U+=server_list[i].U; |
||
808 | |||
809 | if (temp_U<=server_list[i].Umin) { |
||
405 | trimarchi | 810 | server_list[i].U=server_list[i].Umin; |
416 | trimarchi | 811 | } else if (temp_U>server_list[i].Umax) { |
812 | server_list[i].U=server_list[i].Umax; |
||
405 | trimarchi | 813 | isok=0; |
814 | } else server_list[i].U=temp_U; |
||
815 | |||
816 | #ifdef FSF_DEBUG |
||
817 | mul32div32to32(server_list[i].U,100, MAX_BANDWIDTH, temp); |
||
818 | kern_printf("(Server %d bw %d)", server_list[i].server, temp); |
||
819 | #endif |
||
820 | } |
||
821 | } |
||
843 | trimarchi | 822 | target_importance++; |
868 | trimarchi | 823 | } while (!isok && target_importance<=MAX_IMPORTANCE); |
405 | trimarchi | 824 | |
825 | return 0; |
||
826 | } |
||
827 | |||
221 | giacomo | 828 | int fsf_renegotiate_contract |
829 | (const fsf_contract_parameters_t *new_contract, |
||
830 | fsf_server_id_t server) |
||
831 | { |
||
679 | trimarchi | 832 | |
833 | struct mess message; |
||
221 | giacomo | 834 | |
679 | trimarchi | 835 | // send response server is -1 if the operation fail |
836 | message.type=RENEGOTIATE_CONTRACT; |
||
837 | memmove(&message.contract,new_contract, sizeof(fsf_contract_parameters_t)); |
||
838 | message.server = server; |
||
221 | giacomo | 839 | |
859 | trimarchi | 840 | port_send(channel[1],&message,BLOCK); |
841 | |||
842 | port_receive(channel[0], &message, BLOCK); |
||
843 | |||
679 | trimarchi | 844 | if (message.server==-1) return FSF_ERR_CONTRACT_REJECTED; |
845 | |||
405 | trimarchi | 846 | return 0; |
221 | giacomo | 847 | } |
241 | giacomo | 848 | |
868 | trimarchi | 849 | int fsf_request_contract_renegotiation |
850 | (const fsf_contract_parameters_t *new_contract, |
||
851 | fsf_server_id_t server, |
||
852 | int sig_notify, |
||
853 | union sigval sig_value) |
||
854 | { |
||
855 | |||
856 | struct mess message; |
||
857 | |||
858 | // send response server is -1 if the operation fail |
||
859 | message.type=REQUEST_RENEGOTIATE_CONTRACT; |
||
860 | memmove(&message.contract,new_contract, sizeof(fsf_contract_parameters_t)); |
||
861 | message.server = server; |
||
862 | |||
863 | port_send(channel[1],&message,BLOCK); |
||
864 | |||
865 | port_receive(channel[0], &message, BLOCK); |
||
866 | |||
867 | if (message.server==-1) return FSF_ERR_CONTRACT_REJECTED; |
||
868 | |||
869 | return 0; |
||
870 | } |
||
871 | |||
407 | giacomo | 872 | void print_server_list() |
873 | { |
||
874 | |||
875 | int i; |
||
876 | |||
877 | kern_printf("Server List\n"); |
||
878 | |||
410 | trimarchi | 879 | for(i=0;i<current_server;i++) { |
407 | giacomo | 880 | |
662 | giacomo | 881 | 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 | 882 | |
883 | } |
||
884 | |||
885 | } |
||
808 | trimarchi | 886 | |
887 | |||
888 | int fsf_get_remain_budget(fsf_server_id_t server) { |
||
889 | |||
825 | trimarchi | 890 | return SERVER_get_remain_capacity(fsf_server_level, server); |
808 | trimarchi | 891 | } |
868 | trimarchi | 892 | |
893 | int fsf_get_budget_and_period |
||
894 | (fsf_server_id_t server, |
||
895 | struct timespec *budget, |
||
896 | struct timespec *period) { |
||
897 | TIME bg; |
||
898 | TIME pd; |
||
899 | |||
900 | if (!SERVER_getbudgetinfo(fsf_server_level, &bg, &pd, NULL, server)) { |
||
901 | if (budget) { |
||
902 | NULL_TIMESPEC(budget); |
||
903 | ADDUSEC2TIMESPEC(bg, budget); |
||
904 | } |
||
905 | if (period) { |
||
906 | NULL_TIMESPEC(period); |
||
907 | ADDUSEC2TIMESPEC(pd, period); |
||
908 | } |
||
909 | |||
910 | return 0; |
||
911 | } |
||
912 | return FSF_ERR_BAD_ARGUMENT; |
||
913 | } |
||
914 | |||
915 | int |
||
916 | fsf_set_service_thread_data |
||
917 | (const struct timespec *budget, |
||
918 | const struct timespec *period, |
||
919 | bool *accepted) { |
||
920 | |||
921 | if (budget==NULL && period==NULL) return FSF_ERR_BAD_ARGUMENT; |
||
922 | fsf_set_contract_basic_parameters(&service_contract,budget,period,FSF_DEFAULT_WORKLOAD); |
||
923 | *accepted = !fsf_renegotiate_contract(&service_contract,service_server)?true:false; |
||
924 | return 0; |
||
925 | |||
926 | } |
||
927 | |||
928 | |||
929 | |||
930 | int fsf_get_service_thread_data |
||
931 | (struct timespec *budget, |
||
932 | struct timespec *period) { |
||
933 | |||
934 | return fsf_get_budget_and_period(service_server, budget, period); |
||
935 | |||
936 | } |
||
937 |