Subversion Repositories shark

Rev

Rev 825 | Rev 843 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
221 giacomo 1
//=====================================================================
2
//       FFFFFFIII   RRRRR      SSTTTTTTT
3
//      FF         IIR   RR    SS
4
//     FF           IR        SS
5
//    FFFFFF         RRRR    SSSSST      
6
//   FF       FI       RRR  SS
7
//  FF         II     RRR  SS
8
// FF           IIIIIR    RS 
9
//       
10
// Basic FSF(FIRST Scheduling Framework) contract management
11
// S.Ha.R.K. Implementation
12
//=====================================================================
13
 
405 trimarchi 14
#include "ll/i386/64bit.h"
679 trimarchi 15
#include <kernel/kern.h>
16
#include <modules/sem.h>
17
#include <modules/hartport.h>
18
#include <modules/cabs.h>
405 trimarchi 19
 
221 giacomo 20
#include "fsf_contract.h"
241 giacomo 21
#include "fsf_server.h"
679 trimarchi 22
#include "fsf_service_task.h"
23
#include "message.h"
221 giacomo 24
 
25
#include <pthread.h>
26
#include <stdlib.h>
808 trimarchi 27
#include "pistar.h"
221 giacomo 28
 
679 trimarchi 29
 
417 giacomo 30
//#define FSF_DEBUG
221 giacomo 31
 
410 trimarchi 32
int current_server=0;
407 giacomo 33
server_elem server_list[FSF_MAX_N_SERVERS];
406 giacomo 34
bandwidth_t fsf_max_bw = 0;
405 trimarchi 35
 
241 giacomo 36
int fsf_server_level;
808 trimarchi 37
int shared_object_level;
38
 
679 trimarchi 39
PID server_task;
221 giacomo 40
 
679 trimarchi 41
PORT channel[2];
42
 
808 trimarchi 43
int FSF_register_shared_object_module(void) {
811 trimarchi 44
  fsf_register_shared_object();
808 trimarchi 45
  return PISTAR_register_module();
46
}
47
 
48
 
810 trimarchi 49
int FSF_get_shared_object_level() {
50
  return shared_object_level;
51
}
52
 
406 giacomo 53
int FSF_register_module(int server_level, bandwidth_t max_bw)
224 giacomo 54
{
55
  printk("FSF Module\n");
410 trimarchi 56
  current_server=0;
241 giacomo 57
  fsf_server_level = server_level;
406 giacomo 58
  fsf_max_bw = max_bw;
811 trimarchi 59
  shared_object_level = FSF_register_shared_object_module();
679 trimarchi 60
 
224 giacomo 61
  return 0;
62
 
63
}
64
 
679 trimarchi 65
void FSF_start_service_task(SOFT_TASK_MODEL *model) {
66
 
67
  // create the service task
68
  // create the communication channel for negotiation and renegotiation
69
 
70
  channel[1] = port_create("CHANW",sizeof(struct mess),1,STREAM,WRITE);
71
 
72
  channel[0] = port_create("CHANR",sizeof(struct mess),1,STREAM,READ);
73
 
74
  server_task = task_create("stask",service_task,model,NULL);
75
  if (server_task == -1) {
76
    cprintf("error creating service task\n");
77
    sys_shutdown_message("Could not create service_task");
78
    sys_end();
79
    return;
80
  }
81
 
82
  task_activate(server_task);
83
 
84
}
85
 
86
 
221 giacomo 87
/* Convert the contract specification to
88
 * budget parameters
89
 */
241 giacomo 90
int set_SERVER_budget_from_contract
221 giacomo 91
  (const fsf_contract_parameters_t *contract,
92
   int *budget)
93
{
94
 
95
  int local_scheduler_level = 0;
96
 
97
   switch (contract->local_scheduler_id) {
98
     case FSF_SCHEDULER_POSIX:
241 giacomo 99
       local_scheduler_level = POSIXSTAR_register_level(fsf_server_level,5000,32);
221 giacomo 100
       break;
101
     case FSF_SCHEDULER_EDF:
241 giacomo 102
       local_scheduler_level = EDFSTAR_register_level(fsf_server_level);
221 giacomo 103
       break;
104
     case FSF_SCHEDULER_RM:
241 giacomo 105
       local_scheduler_level = RMSTAR_register_level(fsf_server_level);
221 giacomo 106
       break;
334 giacomo 107
     case FSF_SCHEDULER_MPEG:
108
       local_scheduler_level = MPEGSTAR_register_level(fsf_server_level);
109
       break;
221 giacomo 110
   }    
405 trimarchi 111
 
661 giacomo 112
   if (contract->d_equals_t == TRUE) {
241 giacomo 113
  *budget = SERVER_setbudget(fsf_server_level,
221 giacomo 114
                              TIMESPEC2USEC(&(contract->budget_min)),
115
                              TIMESPEC2USEC(&(contract->period_max)),
661 giacomo 116
                              TIMESPEC2USEC(&(contract->period_max)),
221 giacomo 117
                              local_scheduler_level,contract->local_scheduler_id);
661 giacomo 118
  } else {
119
  *budget = SERVER_setbudget(fsf_server_level,
120
                              TIMESPEC2USEC(&(contract->budget_min)),
121
                              TIMESPEC2USEC(&(contract->period_max)),
122
                              TIMESPEC2USEC(&(contract->deadline)),
123
                              local_scheduler_level,contract->local_scheduler_id);
124
  }
221 giacomo 125
 
126
  return 0;
127
 
128
}
129
 
405 trimarchi 130
int adjust_SERVER_budget
131
   (int budget, const TIME budget_actual,
661 giacomo 132
    const TIME period_actual, const TIME dline_actual)
221 giacomo 133
{
134
 
241 giacomo 135
  SERVER_adjust_budget(fsf_server_level,
405 trimarchi 136
                       budget_actual,
137
                       period_actual,
661 giacomo 138
                       dline_actual,
241 giacomo 139
                       budget);
221 giacomo 140
 
141
  return 0;
142
 
143
}
144
 
145
/* Admission Test function */
146
int add_contract(const fsf_contract_parameters_t *contract)
147
{
148
  return 0;
149
 
150
}
151
 
687 trimarchi 152
void contract_to_server(const fsf_contract_parameters_t *contract, int i) {
153
 
410 trimarchi 154
  TIME T,Q;
155
#ifdef FSF_DEBUG
156
  int temp;
157
#endif
158
 
159
  T=TIMESPEC2USEC(&contract->period_min);
160
  Q=TIMESPEC2USEC(&contract->budget_max);
161
  mul32div32to32(MAX_BANDWIDTH,Q,T,server_list[current_server].Umax);
162
 
417 giacomo 163
  T=TIMESPEC2USEC(&contract->period_min);
164
  server_list[i].Tmin=T;
165
 
410 trimarchi 166
  T=TIMESPEC2USEC(&contract->period_max);
167
  server_list[i].Tmax=T;
417 giacomo 168
 
410 trimarchi 169
  Q=TIMESPEC2USEC(&contract->budget_min);
170
  server_list[i].Cmin=Q;
171
 
172
  mul32div32to32(MAX_BANDWIDTH,Q,T,server_list[i].Umin);
416 trimarchi 173
  server_list[i].U=server_list[i].Umin;
661 giacomo 174
 
175
  if (contract->d_equals_t == TRUE) {
176
    server_list[i].deadline = 0;
177
    server_list[i].d_equals_t = TRUE;
178
  } else {
179
    server_list[i].deadline = TIMESPEC2USEC(&contract->deadline);;
180
    server_list[i].d_equals_t = FALSE;
181
  }
182
 
410 trimarchi 183
#ifdef FSF_DEBUG
184
  mul32div32to32(server_list[i].Umax,100, MAX_BANDWIDTH, temp);
185
  kern_printf("(Umax %d)",temp);
186
  mul32div32to32(server_list[i].Umin,100, MAX_BANDWIDTH, temp);
187
  kern_printf("(Umin %d)",temp);
188
#endif
189
 
687 trimarchi 190
}
191
 
192
int relink_contract_to_server(const fsf_contract_parameters_t *contract,
193
                              fsf_server_id_t server)
194
{
195
  int i=0;
196
#ifdef FSF_DEBUG
197
  kern_printf("(Relink Server %d)",server);
198
#endif
199
  // find contract
200
  while(i<current_server) {
201
    if (server_list[i].server==server) break;
202
    i++;
203
  }
204
 
205
  server_list[i].server=server;
206
  server_list[i].Qs=1;
207
 
208
  contract_to_server(contract, i);
209
 
210
 
410 trimarchi 211
 return 0;
212
 
213
}
214
 
221 giacomo 215
int link_contract_to_server(const fsf_contract_parameters_t *contract,
216
                            fsf_server_id_t server)
217
{
407 giacomo 218
#ifdef FSF_DEBUG
405 trimarchi 219
  kern_printf("(Link Server %d)",server);
220
#endif
221
 
410 trimarchi 222
  server_list[current_server].server=server;
223
  server_list[current_server].Qs=1;
405 trimarchi 224
 
687 trimarchi 225
  contract_to_server(contract,current_server);    
405 trimarchi 226
 
410 trimarchi 227
  current_server++;
221 giacomo 228
  return 0;
229
 
230
}
231
 
232
int remove_contract(fsf_server_id_t server)
233
{
405 trimarchi 234
  int i=0;
235
  // find the contract
410 trimarchi 236
  while(i<current_server) {
405 trimarchi 237
     if (server_list[i].server==server) break;
238
     i++;
239
  }
221 giacomo 240
 
405 trimarchi 241
  // compress the array;
410 trimarchi 242
  while (i<(current_server-1)) {
405 trimarchi 243
     server_list[i].server=server_list[i+1].server;
244
     server_list[i].Umin=server_list[i+1].Umin;
416 trimarchi 245
     server_list[i].U=server_list[i+1].Umin;    
405 trimarchi 246
     server_list[i].Umax=server_list[i+1].Umax;
247
     server_list[i].Cmin=server_list[i+1].Cmin;
417 giacomo 248
     server_list[i].Tmin=server_list[i+1].Tmin;
405 trimarchi 249
     server_list[i].Tmax=server_list[i+1].Tmax;
250
     server_list[i].Qs=server_list[i+1].Qs;
661 giacomo 251
     server_list[i].deadline = server_list[i+1].deadline;
252
     server_list[i].d_equals_t = server_list[i+1].d_equals_t;
417 giacomo 253
 
405 trimarchi 254
     i++;
255
  }
410 trimarchi 256
  current_server--;
405 trimarchi 257
 
258
 
221 giacomo 259
  return 0;
260
 
261
}
262
 
263
 
264
int fsf_negotiate_contract
265
  (const fsf_contract_parameters_t *contract,
266
   fsf_server_id_t                 *server)
267
{
679 trimarchi 268
  struct mess message;
221 giacomo 269
 
679 trimarchi 270
  // send response server is -1 if the operation fail
271
  message.type=NEGOTIATE_CONTRACT;
272
  memmove(&message.contract,contract, sizeof(fsf_contract_parameters_t));
221 giacomo 273
 
681 trimarchi 274
  port_send(channel[1],&message,BLOCK);
679 trimarchi 275
 
681 trimarchi 276
  port_receive(channel[0], &message, BLOCK);
679 trimarchi 277
 
278
  if (message.server==-1)
279
    return FSF_ERR_CONTRACT_REJECTED;
221 giacomo 280
 
679 trimarchi 281
  *server=message.server;
282
 
221 giacomo 283
  return 0;
284
 
285
}
286
 
241 giacomo 287
int fsf_create_thread
288
  (fsf_server_id_t    server,
289
   pthread_t         *thread,
290
   pthread_attr_t    *attr,
291
   fsf_thread_code_t  thread_code,
292
   void              *arg,
293
   void              *local_scheduler_arg)
221 giacomo 294
{
295
 
296
  int local_scheduler_level,scheduler_id;
297
 
298
  /* Check if server and thread exsist */
241 giacomo 299
  if (server == NIL)
300
    return FSF_ERR_INVALID_SERVER;
221 giacomo 301
 
241 giacomo 302
  local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server);
303
  scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server);
221 giacomo 304
 
305
  /* Check if thread is already bind */
306
  switch (scheduler_id) {
307
    case FSF_SCHEDULER_POSIX:
308
 
298 giacomo 309
      nrt_task_def_arg(*(NRT_TASK_MODEL *)(local_scheduler_arg),arg);
310
      nrt_task_def_level(*(NRT_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level);
221 giacomo 311
 
298 giacomo 312
      *thread = task_create("POSIXSTAR", thread_code, local_scheduler_arg, NULL);
267 giacomo 313
      if (*thread == NIL) {
314
        #ifdef FSF_DEBUG
315
          kern_printf("(FSF:Error creating thread)");
316
        #endif
241 giacomo 317
        return FSF_ERR_CREATE_THREAD;
267 giacomo 318
      }
221 giacomo 319
 
241 giacomo 320
      POSIXSTAR_setbudget(local_scheduler_level, *thread, (int)(server));
234 giacomo 321
 
221 giacomo 322
    break;
323
    case FSF_SCHEDULER_EDF:
324
 
250 giacomo 325
      hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg);
326
      hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level);
327
 
328
      *thread = task_create("EDFSTAR", thread_code, local_scheduler_arg, NULL);
329
      if (*thread == NIL)
330
        return FSF_ERR_CREATE_THREAD;
331
 
332
      EDFSTAR_setbudget(local_scheduler_level, *thread, (int)(server));
333
 
221 giacomo 334
      break;
235 giacomo 335
 
221 giacomo 336
    case FSF_SCHEDULER_RM:
235 giacomo 337
 
273 giacomo 338
      hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg);
339
      hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level);
340
 
341
      *thread = task_create("RMSTAR", thread_code, local_scheduler_arg, NULL);
342
      if (*thread == NIL)
343
        return FSF_ERR_CREATE_THREAD;
344
 
345
      RMSTAR_setbudget(local_scheduler_level, *thread, (int)(server));
346
 
347
      break;
334 giacomo 348
    case FSF_SCHEDULER_MPEG:
349
 
350
      hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg);
351
      hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level);
352
 
338 giacomo 353
      *thread = task_create("MPEGSTAR", thread_code, local_scheduler_arg, NULL);
334 giacomo 354
      if (*thread == NIL)
355
        return FSF_ERR_CREATE_THREAD;
356
 
357
      MPEGSTAR_setbudget(local_scheduler_level, *thread, (int)(server));
358
 
359
      break;
360
 
221 giacomo 361
    default:
241 giacomo 362
      return FSF_ERR_INVALID_SERVER;
221 giacomo 363
      break;
364
  }
811 trimarchi 365
 
366
  #ifdef FSF_DEBUG
367
    kern_printf("(FSF:Insert thread = %d to Server = %d)",*thread,server);
368
  #endif
221 giacomo 369
 
370
  return 0;
371
 
372
}
373
 
808 trimarchi 374
int  fsf_settask_nopreemptive
221 giacomo 375
  (fsf_server_id_t *server,
376
   pthread_t       thread)
377
{
378
  int local_scheduler_level, scheduler_id;
379
 
241 giacomo 380
  local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread);
381
  scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread);
221 giacomo 382
 
383
  switch (scheduler_id) {
808 trimarchi 384
    case FSF_SCHEDULER_POSIX:
816 trimarchi 385
      POSIXSTAR_set_nopreemtive_current(local_scheduler_level);
386
      return 1;
808 trimarchi 387
      break;
388
    case FSF_SCHEDULER_EDF:
389
      EDFSTAR_set_nopreemtive_current(local_scheduler_level);
390
      return 1;
391
      break;
392
    case FSF_SCHEDULER_RM:
812 trimarchi 393
      RMSTAR_set_nopreemtive_current(local_scheduler_level);
394
      return 1;
808 trimarchi 395
      break;
396
    case FSF_SCHEDULER_MPEG:
397
      break;
398
    default:
399
      return -1;
400
  }
401
  return -1;
402
}
403
 
404
 
405
int  fsf_settask_preemptive
406
  (fsf_server_id_t *server,
407
   pthread_t       thread)
408
{
409
  int local_scheduler_level, scheduler_id;
410
 
411
  local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread);
412
  scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread);
413
 
414
  switch (scheduler_id) {
415
    case FSF_SCHEDULER_POSIX:
823 trimarchi 416
      POSIXSTAR_unset_nopreemtive_current(local_scheduler_level);
816 trimarchi 417
      return 1;
808 trimarchi 418
      break;
419
    case FSF_SCHEDULER_EDF:
420
      EDFSTAR_unset_nopreemtive_current(local_scheduler_level);
421
      return 1;
422
      break;
423
    case FSF_SCHEDULER_RM:
812 trimarchi 424
      RMSTAR_unset_nopreemtive_current(local_scheduler_level);
425
      return 1;
808 trimarchi 426
      break;
427
    case FSF_SCHEDULER_MPEG:
428
      break;
429
    default:
430
      return -1;
431
  }
432
 
433
  return -1;
434
 
435
}
436
 
437
 
438
int fsf_get_server
439
  (fsf_server_id_t *server,
440
   pthread_t       thread)
441
{
442
  int local_scheduler_level, scheduler_id;
443
 
444
  local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread);
445
  scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread);
446
 
447
  switch (scheduler_id) {
221 giacomo 448
    case FSF_SCHEDULER_POSIX:  
829 giacomo 449
      *server = POSIXSTAR_getbudget(local_scheduler_level,thread);
450
      return 0;
221 giacomo 451
    case FSF_SCHEDULER_EDF:
829 giacomo 452
      *server = EDFSTAR_getbudget(local_scheduler_level,thread);
453
      return 0;
221 giacomo 454
    case FSF_SCHEDULER_RM:
829 giacomo 455
      *server = RMSTAR_getbudget(local_scheduler_level,thread);
456
      return 0;
334 giacomo 457
    case FSF_SCHEDULER_MPEG:
829 giacomo 458
      *server = MPEGSTAR_getbudget(local_scheduler_level,thread);
459
      return 0;
235 giacomo 460
    default:
221 giacomo 461
      return -1;
462
  }
463
 
464
  return -1;
465
 
466
}
467
 
339 giacomo 468
int fsf_get_server_level(void)
469
{
470
 
471
  return fsf_server_level;
472
 
473
}
474
 
221 giacomo 475
int fsf_cancel_contract
476
  (fsf_server_id_t *server)
477
{
478
 
479
  int local_scheduler_level, scheduler_id;
411 trimarchi 480
  SYS_FLAGS f;
481
  TIME T,Q;
482
  int i=0;
221 giacomo 483
 
484
  #ifdef FSF_DEBUG
485
    kern_printf("(Remove server %d)",*server);
486
  #endif
487
 
488
  /* Check server id */
489
  if (*server < 0)
490
    return FSF_ERR_INVALID_SERVER;
491
 
241 giacomo 492
  local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,*server);
493
  scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,*server);
221 giacomo 494
 
495
  switch (scheduler_id) {
496
    case FSF_SCHEDULER_POSIX:
497
 
498
      /* Check if some thread use the server */
499
      if(POSIXSTAR_budget_has_thread(local_scheduler_level,*server))
500
        return FSF_ERR_SERVER_USED;
501
 
502
      break;
503
    case FSF_SCHEDULER_EDF:
235 giacomo 504
      /* Check if some thread use the server */
505
      if(EDFSTAR_budget_has_thread(local_scheduler_level,*server))
506
        return FSF_ERR_SERVER_USED;
507
      break;
508
 
221 giacomo 509
    case FSF_SCHEDULER_RM:
235 giacomo 510
      /* Check if some thread use the server */
511
      if(RMSTAR_budget_has_thread(local_scheduler_level,*server))
512
        return FSF_ERR_SERVER_USED;
513
 
221 giacomo 514
      break;
334 giacomo 515
 
516
    case FSF_SCHEDULER_MPEG:
517
      /* Check if some thread use the server */
518
      if(MPEGSTAR_budget_has_thread(local_scheduler_level,*server))
519
        return FSF_ERR_SERVER_USED;
520
 
521
      break;
522
 
221 giacomo 523
  }
524
 
241 giacomo 525
  SERVER_removebudget(fsf_server_level,*server);
221 giacomo 526
 
527
  level_free_descriptor(local_scheduler_level);
405 trimarchi 528
 
221 giacomo 529
  remove_contract(*server);
411 trimarchi 530
 
531
  f=kern_fsave();              
532
  if (recalculate_contract(fsf_max_bw)==-1)  {
533
       kern_frestore(f);
534
       return FSF_ERR_CREATE_SERVER;
535
  }
536
#ifdef  FSF_DEBUG
537
  kern_printf("(Adjust budget)");
538
#endif    
539
  for (i=0; i<current_server; i++) {
540
    mul32div32to32(MAX_BANDWIDTH,server_list[i].Cmin,server_list[i].U,T);
417 giacomo 541
    if (T > server_list[i].Tmin ) {
411 trimarchi 542
      server_list[i].actual_budget = server_list[i].Cmin;
543
      server_list[i].actual_period = T;
417 giacomo 544
      #ifdef FSF_DEBUG
545
        kern_printf("(1 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period);
546
      #endif
661 giacomo 547
 
548
      if (server_list[i].d_equals_t == TRUE)
549
        adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, T);
550
      else
551
        adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, server_list[i].deadline);
552
 
411 trimarchi 553
    } else {
417 giacomo 554
      mul32div32to32(server_list[i].Tmin,server_list[i].U,MAX_BANDWIDTH,Q);
411 trimarchi 555
      server_list[i].actual_budget = Q;
417 giacomo 556
      server_list[i].actual_period = server_list[i].Tmin;
557
      #ifdef FSF_DEBUG
558
         kern_printf("(2 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period);
559
      #endif
661 giacomo 560
 
561
      if (server_list[i].d_equals_t == TRUE)
562
        adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].Tmin);
563
      else
564
        adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].deadline);
565
 
411 trimarchi 566
    }
416 trimarchi 567
    server_list[i].U=server_list[i].Umin;
411 trimarchi 568
 
569
  }                                          
221 giacomo 570
 
405 trimarchi 571
 
221 giacomo 572
  *server = -1;
411 trimarchi 573
  kern_frestore(f);
221 giacomo 574
 
575
  return 0;
411 trimarchi 576
 
221 giacomo 577
}
578
 
405 trimarchi 579
int recalculate_contract(bandwidth_t U) {
416 trimarchi 580
  bandwidth_t current_bandwidth;
418 giacomo 581
  unsigned int temp_U;
416 trimarchi 582
  int        Qt;
405 trimarchi 583
  int isok=0;
584
  int i=0;
585
 
407 giacomo 586
  #ifdef FSF_DEBUG
587
    int temp;
588
 
589
    kern_printf("(Recalculate contract)");
590
  #endif
405 trimarchi 591
 
592
  /* The current bandwidth is the min bandwidth */
593
  //current_bandwidth=SERVER_return_bandwidth(fsf_server_level);
594
  #ifdef FSF_DEBUG
410 trimarchi 595
     kern_printf("(nserver %d)", current_server);
405 trimarchi 596
  #endif  
597
 
598
  do  {
599
    current_bandwidth=0;
600
    Qt=0;
410 trimarchi 601
    for (i=0; i<current_server; i++) {
416 trimarchi 602
      if (server_list[i].Qs!=0 && server_list[i].U<server_list[i].Umax)
405 trimarchi 603
         Qt+=server_list[i].Qs;
416 trimarchi 604
       current_bandwidth+=server_list[i].U;
405 trimarchi 605
    }
413 trimarchi 606
 
405 trimarchi 607
    #ifdef FSF_DEBUG
608
    kern_printf("(Total Quality %d)", Qt);
609
    #endif
610
    isok=1;
410 trimarchi 611
    for (i=0; i<current_server; i++) {
416 trimarchi 612
      if (server_list[i].Qs!=0 && server_list[i].U<server_list[i].Umax) {
613
        temp_U=server_list[i].U;
614
        server_list[i].U=U-current_bandwidth;
615
        mul32div32to32(server_list[i].U, server_list[i].Qs, Qt, server_list[i].U);
616
        temp_U+=server_list[i].U;
617
 
618
        if (temp_U<=server_list[i].Umin) {
405 trimarchi 619
           server_list[i].U=server_list[i].Umin;
416 trimarchi 620
        } else if (temp_U>server_list[i].Umax)  {
621
           server_list[i].U=server_list[i].Umax;
405 trimarchi 622
           isok=0;
623
        } else server_list[i].U=temp_U;
624
 
625
#ifdef FSF_DEBUG
626
        mul32div32to32(server_list[i].U,100, MAX_BANDWIDTH, temp);
627
        kern_printf("(Server %d bw %d)", server_list[i].server, temp);
628
#endif 
629
      }
630
    }  
631
 
632
  } while (!isok);
633
 
634
 return 0;
635
}
636
 
221 giacomo 637
int fsf_renegotiate_contract
638
  (const fsf_contract_parameters_t *new_contract,
639
   fsf_server_id_t server)
640
{
679 trimarchi 641
 
642
  struct mess message;
221 giacomo 643
 
679 trimarchi 644
  // send response server is -1 if the operation fail
645
  message.type=RENEGOTIATE_CONTRACT;
646
  memmove(&message.contract,new_contract, sizeof(fsf_contract_parameters_t));
647
  message.server = server;
221 giacomo 648
 
679 trimarchi 649
  //port_send(channel,&message,BLOCK);
650
 
651
  //port_receive(channel, &message, BLOCK);
652
 
653
  if (message.server==-1) return FSF_ERR_CONTRACT_REJECTED;
654
 
405 trimarchi 655
   return 0;
221 giacomo 656
}
241 giacomo 657
 
407 giacomo 658
void print_server_list()
659
{
660
 
661
  int i;
662
 
663
  kern_printf("Server List\n");
664
 
410 trimarchi 665
  for(i=0;i<current_server;i++) {
407 giacomo 666
 
662 giacomo 667
    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 668
 
669
  }
670
 
671
}
808 trimarchi 672
 
673
 
674
int fsf_get_remain_budget(fsf_server_id_t server) {
675
 
825 trimarchi 676
  return SERVER_get_remain_capacity(fsf_server_level, server);
808 trimarchi 677
}