Subversion Repositories shark

Rev

Rev 843 | Rev 859 | 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;
855 trimarchi 107
     case FSF_SCHEDULER_NONE:
108
       local_scheduler_level = NONESTAR_register_level(fsf_server_level);
334 giacomo 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
 
843 trimarchi 183
  server_list[i].Qs = contract->quality;
184
  server_list[i].Is = contract->importance;
185
 
410 trimarchi 186
#ifdef FSF_DEBUG
187
  mul32div32to32(server_list[i].Umax,100, MAX_BANDWIDTH, temp);
188
  kern_printf("(Umax %d)",temp);
189
  mul32div32to32(server_list[i].Umin,100, MAX_BANDWIDTH, temp);
190
  kern_printf("(Umin %d)",temp);
191
#endif
192
 
687 trimarchi 193
}
194
 
195
int relink_contract_to_server(const fsf_contract_parameters_t *contract,
196
                              fsf_server_id_t server)
197
{
198
  int i=0;
199
#ifdef FSF_DEBUG
200
  kern_printf("(Relink Server %d)",server);
201
#endif
202
  // find contract
203
  while(i<current_server) {
204
    if (server_list[i].server==server) break;
205
    i++;
206
  }
207
 
208
  server_list[i].server=server;
843 trimarchi 209
//  server_list[i].Qs=1;
687 trimarchi 210
 
211
  contract_to_server(contract, i);
212
 
213
 
410 trimarchi 214
 return 0;
215
 
216
}
217
 
221 giacomo 218
int link_contract_to_server(const fsf_contract_parameters_t *contract,
219
                            fsf_server_id_t server)
220
{
407 giacomo 221
#ifdef FSF_DEBUG
405 trimarchi 222
  kern_printf("(Link Server %d)",server);
223
#endif
224
 
410 trimarchi 225
  server_list[current_server].server=server;
843 trimarchi 226
  //server_list[current_server].Qs=1;
405 trimarchi 227
 
687 trimarchi 228
  contract_to_server(contract,current_server);    
405 trimarchi 229
 
410 trimarchi 230
  current_server++;
221 giacomo 231
  return 0;
232
 
233
}
234
 
235
int remove_contract(fsf_server_id_t server)
236
{
405 trimarchi 237
  int i=0;
238
  // find the contract
410 trimarchi 239
  while(i<current_server) {
405 trimarchi 240
     if (server_list[i].server==server) break;
241
     i++;
242
  }
221 giacomo 243
 
405 trimarchi 244
  // compress the array;
410 trimarchi 245
  while (i<(current_server-1)) {
405 trimarchi 246
     server_list[i].server=server_list[i+1].server;
247
     server_list[i].Umin=server_list[i+1].Umin;
416 trimarchi 248
     server_list[i].U=server_list[i+1].Umin;    
405 trimarchi 249
     server_list[i].Umax=server_list[i+1].Umax;
250
     server_list[i].Cmin=server_list[i+1].Cmin;
417 giacomo 251
     server_list[i].Tmin=server_list[i+1].Tmin;
405 trimarchi 252
     server_list[i].Tmax=server_list[i+1].Tmax;
253
     server_list[i].Qs=server_list[i+1].Qs;
661 giacomo 254
     server_list[i].deadline = server_list[i+1].deadline;
255
     server_list[i].d_equals_t = server_list[i+1].d_equals_t;
843 trimarchi 256
     server_list[i].Is = server_list[i+1].Is;
417 giacomo 257
 
405 trimarchi 258
     i++;
259
  }
410 trimarchi 260
  current_server--;
405 trimarchi 261
 
262
 
221 giacomo 263
  return 0;
264
 
265
}
266
 
267
 
268
int fsf_negotiate_contract
269
  (const fsf_contract_parameters_t *contract,
270
   fsf_server_id_t                 *server)
271
{
679 trimarchi 272
  struct mess message;
221 giacomo 273
 
679 trimarchi 274
  // send response server is -1 if the operation fail
275
  message.type=NEGOTIATE_CONTRACT;
276
  memmove(&message.contract,contract, sizeof(fsf_contract_parameters_t));
221 giacomo 277
 
681 trimarchi 278
  port_send(channel[1],&message,BLOCK);
679 trimarchi 279
 
681 trimarchi 280
  port_receive(channel[0], &message, BLOCK);
679 trimarchi 281
 
282
  if (message.server==-1)
283
    return FSF_ERR_CONTRACT_REJECTED;
221 giacomo 284
 
679 trimarchi 285
  *server=message.server;
286
 
221 giacomo 287
  return 0;
288
 
289
}
290
 
241 giacomo 291
int fsf_create_thread
292
  (fsf_server_id_t    server,
293
   pthread_t         *thread,
294
   pthread_attr_t    *attr,
295
   fsf_thread_code_t  thread_code,
296
   void              *arg,
297
   void              *local_scheduler_arg)
221 giacomo 298
{
299
 
300
  int local_scheduler_level,scheduler_id;
301
 
302
  /* Check if server and thread exsist */
241 giacomo 303
  if (server == NIL)
304
    return FSF_ERR_INVALID_SERVER;
221 giacomo 305
 
241 giacomo 306
  local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server);
307
  scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server);
221 giacomo 308
 
309
  /* Check if thread is already bind */
310
  switch (scheduler_id) {
311
    case FSF_SCHEDULER_POSIX:
312
 
298 giacomo 313
      nrt_task_def_arg(*(NRT_TASK_MODEL *)(local_scheduler_arg),arg);
314
      nrt_task_def_level(*(NRT_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level);
221 giacomo 315
 
298 giacomo 316
      *thread = task_create("POSIXSTAR", thread_code, local_scheduler_arg, NULL);
267 giacomo 317
      if (*thread == NIL) {
318
        #ifdef FSF_DEBUG
319
          kern_printf("(FSF:Error creating thread)");
320
        #endif
241 giacomo 321
        return FSF_ERR_CREATE_THREAD;
267 giacomo 322
      }
221 giacomo 323
 
241 giacomo 324
      POSIXSTAR_setbudget(local_scheduler_level, *thread, (int)(server));
234 giacomo 325
 
221 giacomo 326
    break;
327
    case FSF_SCHEDULER_EDF:
328
 
250 giacomo 329
      hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg);
330
      hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level);
331
 
332
      *thread = task_create("EDFSTAR", thread_code, local_scheduler_arg, NULL);
333
      if (*thread == NIL)
334
        return FSF_ERR_CREATE_THREAD;
335
 
336
      EDFSTAR_setbudget(local_scheduler_level, *thread, (int)(server));
337
 
221 giacomo 338
      break;
235 giacomo 339
 
221 giacomo 340
    case FSF_SCHEDULER_RM:
235 giacomo 341
 
273 giacomo 342
      hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg);
343
      hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level);
344
 
345
      *thread = task_create("RMSTAR", thread_code, local_scheduler_arg, NULL);
346
      if (*thread == NIL)
347
        return FSF_ERR_CREATE_THREAD;
348
 
349
      RMSTAR_setbudget(local_scheduler_level, *thread, (int)(server));
350
 
351
      break;
855 trimarchi 352
    case FSF_SCHEDULER_NONE:
334 giacomo 353
 
354
      hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg);
355
      hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level);
356
 
855 trimarchi 357
      *thread = task_create("NONESTAR", thread_code, local_scheduler_arg, NULL);
334 giacomo 358
      if (*thread == NIL)
359
        return FSF_ERR_CREATE_THREAD;
360
 
855 trimarchi 361
      NONESTAR_setbudget(local_scheduler_level, *thread, (int)(server));
334 giacomo 362
 
363
      break;
364
 
221 giacomo 365
    default:
241 giacomo 366
      return FSF_ERR_INVALID_SERVER;
221 giacomo 367
      break;
368
  }
811 trimarchi 369
 
370
  #ifdef FSF_DEBUG
371
    kern_printf("(FSF:Insert thread = %d to Server = %d)",*thread,server);
372
  #endif
221 giacomo 373
 
374
  return 0;
375
 
376
}
377
 
808 trimarchi 378
int  fsf_settask_nopreemptive
221 giacomo 379
  (fsf_server_id_t *server,
380
   pthread_t       thread)
381
{
382
  int local_scheduler_level, scheduler_id;
383
 
241 giacomo 384
  local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread);
385
  scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread);
221 giacomo 386
 
387
  switch (scheduler_id) {
808 trimarchi 388
    case FSF_SCHEDULER_POSIX:
816 trimarchi 389
      POSIXSTAR_set_nopreemtive_current(local_scheduler_level);
390
      return 1;
808 trimarchi 391
      break;
392
    case FSF_SCHEDULER_EDF:
393
      EDFSTAR_set_nopreemtive_current(local_scheduler_level);
394
      return 1;
395
      break;
396
    case FSF_SCHEDULER_RM:
812 trimarchi 397
      RMSTAR_set_nopreemtive_current(local_scheduler_level);
398
      return 1;
808 trimarchi 399
      break;
855 trimarchi 400
    case FSF_SCHEDULER_NONE:
808 trimarchi 401
      break;
402
    default:
403
      return -1;
404
  }
405
  return -1;
406
}
407
 
408
 
409
int  fsf_settask_preemptive
410
  (fsf_server_id_t *server,
411
   pthread_t       thread)
412
{
413
  int local_scheduler_level, scheduler_id;
414
 
415
  local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread);
416
  scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread);
417
 
418
  switch (scheduler_id) {
419
    case FSF_SCHEDULER_POSIX:
823 trimarchi 420
      POSIXSTAR_unset_nopreemtive_current(local_scheduler_level);
816 trimarchi 421
      return 1;
808 trimarchi 422
      break;
423
    case FSF_SCHEDULER_EDF:
424
      EDFSTAR_unset_nopreemtive_current(local_scheduler_level);
425
      return 1;
426
      break;
427
    case FSF_SCHEDULER_RM:
812 trimarchi 428
      RMSTAR_unset_nopreemtive_current(local_scheduler_level);
429
      return 1;
808 trimarchi 430
      break;
855 trimarchi 431
    case FSF_SCHEDULER_NONE:
808 trimarchi 432
      break;
433
    default:
434
      return -1;
435
  }
436
 
437
  return -1;
438
 
439
}
440
 
441
 
442
int fsf_get_server
443
  (fsf_server_id_t *server,
444
   pthread_t       thread)
445
{
446
  int local_scheduler_level, scheduler_id;
447
 
448
  local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread);
449
  scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread);
450
 
451
  switch (scheduler_id) {
221 giacomo 452
    case FSF_SCHEDULER_POSIX:  
829 giacomo 453
      *server = POSIXSTAR_getbudget(local_scheduler_level,thread);
454
      return 0;
221 giacomo 455
    case FSF_SCHEDULER_EDF:
829 giacomo 456
      *server = EDFSTAR_getbudget(local_scheduler_level,thread);
457
      return 0;
221 giacomo 458
    case FSF_SCHEDULER_RM:
829 giacomo 459
      *server = RMSTAR_getbudget(local_scheduler_level,thread);
460
      return 0;
855 trimarchi 461
    case FSF_SCHEDULER_NONE:
462
      *server = NONESTAR_getbudget(local_scheduler_level,thread);
829 giacomo 463
      return 0;
235 giacomo 464
    default:
221 giacomo 465
      return -1;
466
  }
467
 
468
  return -1;
469
 
470
}
471
 
339 giacomo 472
int fsf_get_server_level(void)
473
{
474
 
475
  return fsf_server_level;
476
 
477
}
478
 
221 giacomo 479
int fsf_cancel_contract
480
  (fsf_server_id_t *server)
481
{
482
 
483
  int local_scheduler_level, scheduler_id;
411 trimarchi 484
  SYS_FLAGS f;
485
  TIME T,Q;
486
  int i=0;
221 giacomo 487
 
488
  #ifdef FSF_DEBUG
489
    kern_printf("(Remove server %d)",*server);
490
  #endif
491
 
492
  /* Check server id */
493
  if (*server < 0)
494
    return FSF_ERR_INVALID_SERVER;
495
 
241 giacomo 496
  local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,*server);
497
  scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,*server);
221 giacomo 498
 
499
  switch (scheduler_id) {
500
    case FSF_SCHEDULER_POSIX:
501
 
502
      /* Check if some thread use the server */
503
      if(POSIXSTAR_budget_has_thread(local_scheduler_level,*server))
504
        return FSF_ERR_SERVER_USED;
505
 
506
      break;
507
    case FSF_SCHEDULER_EDF:
235 giacomo 508
      /* Check if some thread use the server */
509
      if(EDFSTAR_budget_has_thread(local_scheduler_level,*server))
510
        return FSF_ERR_SERVER_USED;
511
      break;
512
 
221 giacomo 513
    case FSF_SCHEDULER_RM:
235 giacomo 514
      /* Check if some thread use the server */
515
      if(RMSTAR_budget_has_thread(local_scheduler_level,*server))
516
        return FSF_ERR_SERVER_USED;
517
 
221 giacomo 518
      break;
334 giacomo 519
 
855 trimarchi 520
    case FSF_SCHEDULER_NONE:
334 giacomo 521
      /* Check if some thread use the server */
855 trimarchi 522
      if(NONESTAR_budget_has_thread(local_scheduler_level,*server))
334 giacomo 523
        return FSF_ERR_SERVER_USED;
524
 
525
      break;
526
 
221 giacomo 527
  }
528
 
241 giacomo 529
  SERVER_removebudget(fsf_server_level,*server);
221 giacomo 530
 
531
  level_free_descriptor(local_scheduler_level);
405 trimarchi 532
 
221 giacomo 533
  remove_contract(*server);
411 trimarchi 534
 
535
  f=kern_fsave();              
536
  if (recalculate_contract(fsf_max_bw)==-1)  {
537
       kern_frestore(f);
538
       return FSF_ERR_CREATE_SERVER;
539
  }
540
#ifdef  FSF_DEBUG
541
  kern_printf("(Adjust budget)");
542
#endif    
543
  for (i=0; i<current_server; i++) {
544
    mul32div32to32(MAX_BANDWIDTH,server_list[i].Cmin,server_list[i].U,T);
417 giacomo 545
    if (T > server_list[i].Tmin ) {
411 trimarchi 546
      server_list[i].actual_budget = server_list[i].Cmin;
547
      server_list[i].actual_period = T;
417 giacomo 548
      #ifdef FSF_DEBUG
549
        kern_printf("(1 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period);
550
      #endif
661 giacomo 551
 
552
      if (server_list[i].d_equals_t == TRUE)
553
        adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, T);
554
      else
555
        adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, server_list[i].deadline);
556
 
411 trimarchi 557
    } else {
417 giacomo 558
      mul32div32to32(server_list[i].Tmin,server_list[i].U,MAX_BANDWIDTH,Q);
411 trimarchi 559
      server_list[i].actual_budget = Q;
417 giacomo 560
      server_list[i].actual_period = server_list[i].Tmin;
561
      #ifdef FSF_DEBUG
562
         kern_printf("(2 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period);
563
      #endif
661 giacomo 564
 
565
      if (server_list[i].d_equals_t == TRUE)
566
        adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].Tmin);
567
      else
568
        adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].deadline);
569
 
411 trimarchi 570
    }
416 trimarchi 571
    server_list[i].U=server_list[i].Umin;
411 trimarchi 572
 
573
  }                                          
221 giacomo 574
 
405 trimarchi 575
 
221 giacomo 576
  *server = -1;
411 trimarchi 577
  kern_frestore(f);
221 giacomo 578
 
579
  return 0;
411 trimarchi 580
 
221 giacomo 581
}
582
 
405 trimarchi 583
int recalculate_contract(bandwidth_t U) {
416 trimarchi 584
  bandwidth_t current_bandwidth;
418 giacomo 585
  unsigned int temp_U;
416 trimarchi 586
  int        Qt;
405 trimarchi 587
  int isok=0;
588
  int i=0;
843 trimarchi 589
  int target_importance=1;
405 trimarchi 590
 
407 giacomo 591
  #ifdef FSF_DEBUG
592
    int temp;
593
 
594
    kern_printf("(Recalculate contract)");
595
  #endif
405 trimarchi 596
 
597
  /* The current bandwidth is the min bandwidth */
598
  //current_bandwidth=SERVER_return_bandwidth(fsf_server_level);
599
  #ifdef FSF_DEBUG
410 trimarchi 600
     kern_printf("(nserver %d)", current_server);
405 trimarchi 601
  #endif  
602
 
603
  do  {
604
    current_bandwidth=0;
605
    Qt=0;
410 trimarchi 606
    for (i=0; i<current_server; i++) {
843 trimarchi 607
      if (server_list[i].Is==target_importance
608
          && server_list[i].U<server_list[i].Umax && server_list[i].Qs>0)
405 trimarchi 609
         Qt+=server_list[i].Qs;
416 trimarchi 610
       current_bandwidth+=server_list[i].U;
843 trimarchi 611
    #ifdef FSF_DEBUG
612
       kern_printf("(Qs %d, Qt %d, Is %d)", server_list[i].Qs, Qt,server_list[i].Is);
613
    #endif
405 trimarchi 614
    }
413 trimarchi 615
 
405 trimarchi 616
    #ifdef FSF_DEBUG
617
    kern_printf("(Total Quality %d)", Qt);
618
    #endif
619
    isok=1;
410 trimarchi 620
    for (i=0; i<current_server; i++) {
843 trimarchi 621
      if (server_list[i].Is==target_importance && server_list[i].U<server_list[i].Umax && server_list[i].Qs>0) {
416 trimarchi 622
        temp_U=server_list[i].U;
623
        server_list[i].U=U-current_bandwidth;
624
        mul32div32to32(server_list[i].U, server_list[i].Qs, Qt, server_list[i].U);
625
        temp_U+=server_list[i].U;
626
 
627
        if (temp_U<=server_list[i].Umin) {
405 trimarchi 628
           server_list[i].U=server_list[i].Umin;
416 trimarchi 629
        } else if (temp_U>server_list[i].Umax)  {
630
           server_list[i].U=server_list[i].Umax;
405 trimarchi 631
           isok=0;
632
        } else server_list[i].U=temp_U;
633
 
634
#ifdef FSF_DEBUG
635
        mul32div32to32(server_list[i].U,100, MAX_BANDWIDTH, temp);
636
        kern_printf("(Server %d bw %d)", server_list[i].server, temp);
637
#endif 
638
      }
639
    }  
843 trimarchi 640
    target_importance++;
641
  } while (!isok && target_importance<6);
405 trimarchi 642
 
643
 return 0;
644
}
645
 
221 giacomo 646
int fsf_renegotiate_contract
647
  (const fsf_contract_parameters_t *new_contract,
648
   fsf_server_id_t server)
649
{
679 trimarchi 650
 
651
  struct mess message;
221 giacomo 652
 
679 trimarchi 653
  // send response server is -1 if the operation fail
654
  message.type=RENEGOTIATE_CONTRACT;
655
  memmove(&message.contract,new_contract, sizeof(fsf_contract_parameters_t));
656
  message.server = server;
221 giacomo 657
 
679 trimarchi 658
  //port_send(channel,&message,BLOCK);
659
 
660
  //port_receive(channel, &message, BLOCK);
661
 
662
  if (message.server==-1) return FSF_ERR_CONTRACT_REJECTED;
663
 
405 trimarchi 664
   return 0;
221 giacomo 665
}
241 giacomo 666
 
407 giacomo 667
void print_server_list()
668
{
669
 
670
  int i;
671
 
672
  kern_printf("Server List\n");
673
 
410 trimarchi 674
  for(i=0;i<current_server;i++) {
407 giacomo 675
 
662 giacomo 676
    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 677
 
678
  }
679
 
680
}
808 trimarchi 681
 
682
 
683
int fsf_get_remain_budget(fsf_server_id_t server) {
684
 
825 trimarchi 685
  return SERVER_get_remain_capacity(fsf_server_level, server);
808 trimarchi 686
}