Subversion Repositories shark

Rev

Rev 963 | Rev 977 | 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>
975 trimarchi 16
#include <sem/sem/sem.h>
17
#include <hartport/hartport/hartport.h>
18
#include <cabs/cabs/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"
975 trimarchi 34
#include <posix/posix/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) {
952 trimarchi 142
     case FSF_RR:
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;
952 trimarchi 148
     case FSF_FP:
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
  port_send(channel[1],&m,BLOCK);
388
  port_receive(channel[0], &m, BLOCK);
963 trimarchi 389
  if (m.server==-1) {
390
    *server=0;
391
    return 0;
392
  }
221 giacomo 393
 
880 trimarchi 394
  *server=m.server;
679 trimarchi 395
 
221 giacomo 396
  return 0;
397
 
398
}
399
 
872 trimarchi 400
int
401
fsf_negotiate_contract_for_new_thread
402
  (const fsf_contract_parameters_t *contract,
403
   fsf_server_id_t      *server,
404
   pthread_t            *thread,
405
   pthread_attr_t       *attr,
406
   fsf_thread_code_t     thread_code,
407
   void                 *arg) {
408
 
409
  int err=0;
410
 
411
  err = fsf_negotiate_contract(contract,server);
412
  if (!err) {
413
    err = pthread_create(thread, attr, thread_code, arg);
414
    if (!err)
415
      err = fsf_bind_thread_to_server(*server,*thread);
416
  } else return err;
417
 
418
  return err;
419
}
420
 
421
int
422
fsf_negotiate_contract_for_myself
423
  (const fsf_contract_parameters_t *contract,
424
   fsf_server_id_t      *server) {
425
 
426
  int err=0;
427
 
428
  err = fsf_negotiate_contract(contract,server);
429
  if (!err) {
937 trimarchi 430
#ifdef FSF_DEBUG
431
    kern_printf("Bind task");
432
#endif
872 trimarchi 433
     err = fsf_bind_thread_to_server(*server,exec_shadow);
937 trimarchi 434
 
872 trimarchi 435
  } else return err;
436
 
437
  return err;
438
}
439
 
868 trimarchi 440
int fsf_unbind_thread_from_server
441
  (pthread_t       thread)
442
{
443
 
444
  int local_scheduler_level, scheduler_id;
963 trimarchi 445
  SYS_FLAGS f;
868 trimarchi 446
  /* Move thread from the local scheduler module to posix level */
447
 
448
  #ifdef FSF_DEBUG
449
    kern_printf("(UnBind thread = %d)",thread);
450
  #endif
963 trimarchi 451
  f=kern_fsave();
868 trimarchi 452
  /* Check if thread exsists */
963 trimarchi 453
  if (thread == -1) {
454
    kern_frestore(f);
868 trimarchi 455
    return FSF_ERR_BAD_ARGUMENT;
963 trimarchi 456
  }
868 trimarchi 457
 
458
  local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread);
459
  scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level,thread);
963 trimarchi 460
 
868 trimarchi 461
  /* Check if thread is already bind */
462
  if (scheduler_id == FSF_NONE) {
463
      /* Check if it is bind to a server */
963 trimarchi 464
    if (NONESTAR_getbudget(local_scheduler_level,thread) == -1) {
465
      kern_frestore(f);
868 trimarchi 466
        return FSF_ERR_BAD_ARGUMENT;
963 trimarchi 467
      }
868 trimarchi 468
      else {
469
 
470
        STD_command_message *msg;
471
        NRT_TASK_MODEL nrt;
472
 
473
        nrt_task_default_model(nrt);
474
        nrt_task_def_save_arrivals(nrt);
963 trimarchi 475
        nrt_task_def_weight(nrt,0);
476
        nrt_task_def_policy(nrt,NRT_RR_POLICY);
477
        nrt_task_def_inherit(nrt,NRT_EXPLICIT_SCHED);
868 trimarchi 478
 
479
        /* Send change level command to local scheduler */
480
        msg = (STD_command_message *)malloc(sizeof(STD_command_message));
481
 
482
        msg->command = STD_SET_NEW_MODEL;
483
        msg->param = (void *)(&nrt);
484
        level_table[fsf_posix_level]->public_message(fsf_posix_level,thread,msg);
485
 
486
        msg->command = STD_SET_NEW_LEVEL;
487
        msg->param = (void *)(fsf_posix_level);
488
        task_message(msg,thread,0);
963 trimarchi 489
	//if (exec_shadow!=thread)
490
	level_table[proc_table[thread].task_level]->public_epilogue(proc_table[thread].task_level, thread);
491
	//else {
492
	//scheduler();
493
	//kern_context_load(proc_table[exec_shadow].context);
494
	//}
495
	kern_frestore(f);
868 trimarchi 496
 
497
        free(msg);
498
      }
963 trimarchi 499
  } else {
500
    kern_frestore(f);
501
    return FSF_ERR_BAD_ARGUMENT;
868 trimarchi 502
  }
503
 
504
  return 0;
505
 
506
}
507
 
873 trimarchi 508
int
509
fsf_bind_local_thread_to_server
510
  (fsf_server_id_t      server,
511
   pthread_t            thread,
512
   fsf_sched_params_t  *sched_params)
513
{
868 trimarchi 514
 
873 trimarchi 515
  STD_command_message *msg;
516
  int local_scheduler_level,scheduler_id;
517
 
518
  /* Move thread from the posix module to local scheduler */
519
 
520
  #ifdef FSF_DEBUG
521
    kern_printf("(Bind thread = %d to Server = %d)",thread,server);
522
  #endif
523
 
524
  /* Check if server and thread exsist */
525
  if (server == -1 || thread == -1)
526
    return FSF_ERR_BAD_ARGUMENT;
527
 
528
  local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server);
529
  if (local_scheduler_level==-1)
952 trimarchi 530
    return FSF_ERR_UNKNOWN_SCHEDULED_THREAD;
873 trimarchi 531
 
532
  scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server);
881 trimarchi 533
 
534
  if (scheduler_id!=sched_params->policy)
535
     return FSF_ERR_SCHED_POLICY_NOT_COMPATIBLE;
873 trimarchi 536
 
537
  /* Check if thread is already bind */
538
  switch(scheduler_id) {
539
 
952 trimarchi 540
     case FSF_FP:
873 trimarchi 541
       {
881 trimarchi 542
	 TASK_MODEL      *m=(TASK_MODEL*)(sched_params->params);
543
	 HARD_TASK_MODEL *h=(HARD_TASK_MODEL *)(sched_params->params);
873 trimarchi 544
 
545
	 if (m->pclass != HARD_PCLASS)
546
	   return FSF_ERR_SCHED_POLICY_NOT_COMPATIBLE;
547
 
548
	 h = (HARD_TASK_MODEL *)m;
549
 
550
	 if (!h->wcet || !h->mit) return FSF_ERR_SCHED_POLICY_NOT_COMPATIBLE;
551
 
552
	 /* now we know that m is a valid model */
553
	 if (RMSTAR_getbudget(local_scheduler_level,thread) != -1)
554
	   return FSF_ERR_BAD_ARGUMENT;
555
 
556
	 /* Set server on local scheduler */
557
	 RMSTAR_setbudget(local_scheduler_level,thread,(int)(server));
558
 
559
	 /* Send change level command to posix level */
560
       }
877 trimarchi 561
       break;
562
 
873 trimarchi 563
     case FSF_EDF:
564
       {
881 trimarchi 565
	 TASK_MODEL      *m=(TASK_MODEL*)(sched_params->params);
566
	 HARD_TASK_MODEL *h=(HARD_TASK_MODEL *)(sched_params->params);
873 trimarchi 567
 
568
	 if (m->pclass != HARD_PCLASS)
569
	   return FSF_ERR_SCHED_POLICY_NOT_COMPATIBLE;
570
 
571
	 h = (HARD_TASK_MODEL *)m;
572
 
573
	 if (!h->wcet || !h->mit) return FSF_ERR_SCHED_POLICY_NOT_COMPATIBLE;
574
 
575
	 if (EDFSTAR_getbudget(local_scheduler_level,thread) != -1)
576
	   return FSF_ERR_BAD_ARGUMENT;
577
 
578
	 /* Set server on local scheduler */
579
	 EDFSTAR_setbudget(local_scheduler_level,thread,(int)(server));
580
 
581
       }
582
       break;
583
 
952 trimarchi 584
     case FSF_RR:
873 trimarchi 585
       {
881 trimarchi 586
	 TASK_MODEL      *m=(TASK_MODEL*)(sched_params->params);
873 trimarchi 587
 
588
	 if (m->pclass != NRT_PCLASS)
589
	   return  FSF_ERR_SCHED_POLICY_NOT_COMPATIBLE;
590
 
591
	 if (POSIXSTAR_getbudget(local_scheduler_level,thread) != -1)
592
	   return FSF_ERR_BAD_ARGUMENT;
593
 
594
	 /* Set server on local scheduler */
595
	 POSIXSTAR_setbudget(local_scheduler_level,thread,(int)(server));
596
       }
877 trimarchi 597
       break;
873 trimarchi 598
 
877 trimarchi 599
     default:
600
 
873 trimarchi 601
       return FSF_ERR_BAD_ARGUMENT;
877 trimarchi 602
 
873 trimarchi 603
  }
604
 
605
  msg = (STD_command_message *)malloc(sizeof(STD_command_message));
877 trimarchi 606
  if (msg) {
607
    SYS_FLAGS f;
608
    f=kern_fsave();
873 trimarchi 609
    msg->command = STD_SET_NEW_MODEL;
881 trimarchi 610
    msg->param = (void *)(sched_params->params);
873 trimarchi 611
    level_table[local_scheduler_level]->public_message(local_scheduler_level,thread,msg);
612
 
613
    msg->command = STD_SET_NEW_LEVEL;
614
    msg->param = (void *)(local_scheduler_level);
615
    task_message(msg,thread,0);
940 trimarchi 616
    scheduler();
617
    kern_context_load(proc_table[exec_shadow].context);
877 trimarchi 618
    kern_frestore(f);
873 trimarchi 619
    free(msg);
620
  } else return FSF_ERR_INTERNAL_ERROR;
621
 
622
  return 0;
623
 
624
}
625
 
626
 
868 trimarchi 627
int fsf_bind_thread_to_server
628
  (fsf_server_id_t server,
629
   pthread_t       thread)
630
{
631
 
963 trimarchi 632
  STD_command_message *msg=0;
868 trimarchi 633
  int local_scheduler_level,scheduler_id;
937 trimarchi 634
  SYS_FLAGS f;
868 trimarchi 635
  /* Move thread from the posix module to local scheduler */
636
 
963 trimarchi 637
  f=kern_fsave();
868 trimarchi 638
  #ifdef FSF_DEBUG
880 trimarchi 639
    kern_printf("(Bthr=%d to Sr=%d)",thread,server);
868 trimarchi 640
  #endif
641
 
642
  /* Check if server and thread exsist */
643
  if (server == -1 || thread == -1)
644
    return FSF_ERR_BAD_ARGUMENT;
645
 
646
  local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server);
873 trimarchi 647
  if (local_scheduler_level==-1)
952 trimarchi 648
    return FSF_ERR_UNKNOWN_SCHEDULED_THREAD;
873 trimarchi 649
 
868 trimarchi 650
  scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server);
651
  /* Check if thread is already bind */
652
  if (scheduler_id == FSF_NONE) {
653
      DUMMY_TASK_MODEL rt_arg;
963 trimarchi 654
      if (NONESTAR_getbudget(local_scheduler_level,thread) != -1) {
655
	kern_frestore(f);
868 trimarchi 656
        return FSF_ERR_BAD_ARGUMENT;
963 trimarchi 657
      }
868 trimarchi 658
      /* Set server on local scheduler */
659
      NONESTAR_setbudget(local_scheduler_level,thread,(int)(server));
963 trimarchi 660
 
868 trimarchi 661
      /* Send change level command to posix level */
662
      msg = (STD_command_message *)malloc(sizeof(STD_command_message));
963 trimarchi 663
      if (!msg) exit(-1);
868 trimarchi 664
      msg->command = STD_SET_NEW_MODEL;
665
      msg->param = (void *)(&rt_arg);
937 trimarchi 666
      level_table[local_scheduler_level]->public_message(local_scheduler_level,thread,msg);
868 trimarchi 667
      msg->command = STD_SET_NEW_LEVEL;
668
      msg->param = (void *)(local_scheduler_level);
963 trimarchi 669
      task_message(msg,thread,0);
670
      //if (thread!=exec_shadow) {
671
      	  level_table[proc_table[thread].task_level]->public_dispatch(proc_table[thread].task_level, thread, 0);
672
          level_table[proc_table[thread].task_level]->public_epilogue(proc_table[thread].task_level, thread);
673
      //} else {
674
      if (cap_timer != NIL) {
675
        event_delete(cap_timer);
676
        cap_timer = NIL;
677
      }
678
 
937 trimarchi 679
      scheduler();
680
      kern_context_load(proc_table[exec_shadow].context);
963 trimarchi 681
      //}
937 trimarchi 682
      kern_frestore(f);
683
 
684
 
963 trimarchi 685
      if (msg) free(msg);
937 trimarchi 686
 
963 trimarchi 687
   } else {
688
       kern_frestore(f);
689
       return FSF_ERR_BAD_ARGUMENT;
690
   }
868 trimarchi 691
  return 0;
692
}
693
 
881 trimarchi 694
int fsf_create_local_thread
695
(fsf_server_id_t        server,
696
   fsf_sched_params_t    *local_scheduler_arg,
697
   pthread_t             *thread,
698
   pthread_attr_t        *attr,
699
   fsf_thread_code_t      thread_code,
700
   void                  *arg)
221 giacomo 701
{
702
 
703
  int local_scheduler_level,scheduler_id;
704
 
705
  /* Check if server and thread exsist */
241 giacomo 706
  if (server == NIL)
873 trimarchi 707
    return  FSF_ERR_BAD_ARGUMENT;
221 giacomo 708
 
241 giacomo 709
  local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server);
710
  scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server);
868 trimarchi 711
#ifdef FSF_DEBUG
712
  kern_printf("sched policy %d", scheduler_id);
713
#endif
888 trimarchi 714
 
715
  if (scheduler_id!=local_scheduler_arg->policy)
716
    return FSF_ERR_SCHED_POLICY_NOT_COMPATIBLE;
717
 
221 giacomo 718
  /* Check if thread is already bind */
719
  switch (scheduler_id) {
952 trimarchi 720
     case FSF_RR:
221 giacomo 721
 
888 trimarchi 722
      nrt_task_def_arg(*(NRT_TASK_MODEL *)(local_scheduler_arg->params),arg);
723
      nrt_task_def_level(*(NRT_TASK_MODEL *)(local_scheduler_arg->params),local_scheduler_level);
221 giacomo 724
 
888 trimarchi 725
      *thread = task_create("POSIXSTAR", thread_code, local_scheduler_arg->params, NULL);
267 giacomo 726
      if (*thread == NIL) {
727
        #ifdef FSF_DEBUG
728
          kern_printf("(FSF:Error creating thread)");
729
        #endif
873 trimarchi 730
        return FSF_ERR_INTERNAL_ERROR;
267 giacomo 731
      }
221 giacomo 732
 
241 giacomo 733
      POSIXSTAR_setbudget(local_scheduler_level, *thread, (int)(server));
234 giacomo 734
 
221 giacomo 735
    break;
868 trimarchi 736
    case FSF_EDF:
221 giacomo 737
 
888 trimarchi 738
      hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg->params),arg);
739
      hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg->params),local_scheduler_level);
250 giacomo 740
 
888 trimarchi 741
      *thread = task_create("EDFSTAR", thread_code, local_scheduler_arg->params, NULL);
250 giacomo 742
      if (*thread == NIL)
873 trimarchi 743
        return  FSF_ERR_INTERNAL_ERROR;
250 giacomo 744
 
745
      EDFSTAR_setbudget(local_scheduler_level, *thread, (int)(server));
746
 
221 giacomo 747
      break;
235 giacomo 748
 
952 trimarchi 749
    case FSF_FP:
235 giacomo 750
 
888 trimarchi 751
      hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg->params),arg);
752
      hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg->params),local_scheduler_level);
273 giacomo 753
 
888 trimarchi 754
      *thread = task_create("RMSTAR", thread_code, local_scheduler_arg->params, NULL);
273 giacomo 755
      if (*thread == NIL)
873 trimarchi 756
        return  FSF_ERR_INTERNAL_ERROR;
273 giacomo 757
 
758
      RMSTAR_setbudget(local_scheduler_level, *thread, (int)(server));
759
 
760
      break;
868 trimarchi 761
    case FSF_NONE:
334 giacomo 762
 
888 trimarchi 763
      dummy_task_def_arg(*( DUMMY_TASK_MODEL *)(local_scheduler_arg->params),arg);
764
      dummy_task_def_level(*( DUMMY_TASK_MODEL *)(local_scheduler_arg->params),local_scheduler_level);
910 trimarchi 765
 
888 trimarchi 766
      *thread = task_create("NONESTAR", thread_code, local_scheduler_arg->params, NULL);
334 giacomo 767
      if (*thread == NIL)
873 trimarchi 768
        return  FSF_ERR_INTERNAL_ERROR;
334 giacomo 769
 
855 trimarchi 770
      NONESTAR_setbudget(local_scheduler_level, *thread, (int)(server));
334 giacomo 771
 
772
      break;
910 trimarchi 773
 
774
    case FSF_TABLE_DRIVEN:
775
      {
944 trimarchi 776
	HARD_TASK_MODEL ht;
777
	hard_task_default_model(ht);
778
	hard_task_def_aperiodic(ht);
910 trimarchi 779
 
944 trimarchi 780
	hard_task_def_arg(ht,arg);
781
	hard_task_def_level(ht,local_scheduler_level);
910 trimarchi 782
 
944 trimarchi 783
	*thread = task_create("TDSTAR", thread_code, &ht, NULL);
910 trimarchi 784
	if (*thread == NIL)
785
	  return  FSF_ERR_INTERNAL_ERROR;
786
 
930 trimarchi 787
	TDSTAR_setbudget(local_scheduler_level, *thread, (int)(server));
944 trimarchi 788
	TDSTAR_settable(local_scheduler_level, (fsf_table_driven_params_t *)(local_scheduler_arg->params),*thread);
910 trimarchi 789
      }
790
      break;
791
 
334 giacomo 792
 
221 giacomo 793
    default:
873 trimarchi 794
      return FSF_ERR_INTERNAL_ERROR;
221 giacomo 795
      break;
796
  }
811 trimarchi 797
 
798
  #ifdef FSF_DEBUG
799
    kern_printf("(FSF:Insert thread = %d to Server = %d)",*thread,server);
800
  #endif
221 giacomo 801
 
802
  return 0;
803
 
804
}
805
 
808 trimarchi 806
int  fsf_settask_nopreemptive
221 giacomo 807
  (fsf_server_id_t *server,
808
   pthread_t       thread)
809
{
810
  int local_scheduler_level, scheduler_id;
811
 
241 giacomo 812
  local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread);
813
  scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread);
221 giacomo 814
 
815
  switch (scheduler_id) {
952 trimarchi 816
    case FSF_RR:
816 trimarchi 817
      POSIXSTAR_set_nopreemtive_current(local_scheduler_level);
818
      return 1;
808 trimarchi 819
      break;
868 trimarchi 820
    case FSF_EDF:
808 trimarchi 821
      EDFSTAR_set_nopreemtive_current(local_scheduler_level);
822
      return 1;
823
      break;
952 trimarchi 824
    case FSF_FP:
812 trimarchi 825
      RMSTAR_set_nopreemtive_current(local_scheduler_level);
826
      return 1;
808 trimarchi 827
      break;
868 trimarchi 828
    case FSF_NONE:
808 trimarchi 829
      break;
830
    default:
831
      return -1;
832
  }
833
  return -1;
834
}
835
 
836
 
837
int  fsf_settask_preemptive
838
  (fsf_server_id_t *server,
839
   pthread_t       thread)
840
{
841
  int local_scheduler_level, scheduler_id;
842
 
843
  local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread);
844
  scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread);
845
 
846
  switch (scheduler_id) {
952 trimarchi 847
    case FSF_RR:
823 trimarchi 848
      POSIXSTAR_unset_nopreemtive_current(local_scheduler_level);
816 trimarchi 849
      return 1;
808 trimarchi 850
      break;
868 trimarchi 851
    case FSF_EDF:
808 trimarchi 852
      EDFSTAR_unset_nopreemtive_current(local_scheduler_level);
853
      return 1;
854
      break;
952 trimarchi 855
    case FSF_FP:
812 trimarchi 856
      RMSTAR_unset_nopreemtive_current(local_scheduler_level);
857
      return 1;
808 trimarchi 858
      break;
868 trimarchi 859
    case FSF_NONE:
808 trimarchi 860
      break;
861
    default:
862
      return -1;
863
  }
864
 
865
  return -1;
866
 
867
}
868
 
869
 
870
int fsf_get_server
868 trimarchi 871
  (pthread_t       thread,
872
   fsf_server_id_t *server)
808 trimarchi 873
{
874
  int local_scheduler_level, scheduler_id;
875
 
876
  local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread);
877
  scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread);
878
 
879
  switch (scheduler_id) {
952 trimarchi 880
    case FSF_RR:
829 giacomo 881
      *server = POSIXSTAR_getbudget(local_scheduler_level,thread);
882
      return 0;
868 trimarchi 883
    case FSF_EDF:
829 giacomo 884
      *server = EDFSTAR_getbudget(local_scheduler_level,thread);
885
      return 0;
952 trimarchi 886
    case FSF_FP:
829 giacomo 887
      *server = RMSTAR_getbudget(local_scheduler_level,thread);
888
      return 0;
868 trimarchi 889
    case FSF_NONE:
855 trimarchi 890
      *server = NONESTAR_getbudget(local_scheduler_level,thread);
829 giacomo 891
      return 0;
235 giacomo 892
    default:
221 giacomo 893
      return -1;
894
  }
895
 
896
  return -1;
897
 
898
}
899
 
339 giacomo 900
int fsf_get_server_level(void)
901
{
902
 
903
  return fsf_server_level;
904
 
905
}
906
 
221 giacomo 907
int fsf_cancel_contract
868 trimarchi 908
  (fsf_server_id_t server)
221 giacomo 909
{
910
 
911
  int local_scheduler_level, scheduler_id;
411 trimarchi 912
  SYS_FLAGS f;
913
  TIME T,Q;
963 trimarchi 914
  int i=0,err=0;
221 giacomo 915
 
963 trimarchi 916
 
221 giacomo 917
  #ifdef FSF_DEBUG
868 trimarchi 918
    kern_printf("(Remove server %d)",server);
221 giacomo 919
  #endif
920
 
921
  /* Check server id */
868 trimarchi 922
  if (server < 0)
873 trimarchi 923
    return FSF_ERR_BAD_ARGUMENT;
221 giacomo 924
 
868 trimarchi 925
  local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server);
926
  scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server);
221 giacomo 927
 
928
  switch (scheduler_id) {
952 trimarchi 929
    case FSF_RR:
221 giacomo 930
 
931
      /* Check if some thread use the server */
868 trimarchi 932
      if(POSIXSTAR_budget_has_thread(local_scheduler_level,server))
873 trimarchi 933
        return FSF_ERR_NOT_CONTRACTED_SERVER;
221 giacomo 934
 
935
      break;
868 trimarchi 936
    case FSF_EDF:
235 giacomo 937
      /* Check if some thread use the server */
868 trimarchi 938
      if(EDFSTAR_budget_has_thread(local_scheduler_level,server))
873 trimarchi 939
        return FSF_ERR_NOT_CONTRACTED_SERVER;
235 giacomo 940
      break;
941
 
952 trimarchi 942
    case FSF_FP:
235 giacomo 943
      /* Check if some thread use the server */
868 trimarchi 944
      if(RMSTAR_budget_has_thread(local_scheduler_level,server))
873 trimarchi 945
        return FSF_ERR_NOT_CONTRACTED_SERVER;
235 giacomo 946
 
221 giacomo 947
      break;
334 giacomo 948
 
868 trimarchi 949
    case FSF_NONE:
334 giacomo 950
      /* Check if some thread use the server */
963 trimarchi 951
      if(NONESTAR_budget_has_thread(local_scheduler_level,server)) {
952
        err=fsf_unbind_thread_from_server(NONESTAR_get_current(local_scheduler_level));
953
	if (err) return err;
954
      }
334 giacomo 955
 
956
      break;
957
 
221 giacomo 958
  }
963 trimarchi 959
 
868 trimarchi 960
  SERVER_removebudget(fsf_server_level,server);
221 giacomo 961
 
962
  level_free_descriptor(local_scheduler_level);
405 trimarchi 963
 
868 trimarchi 964
  remove_contract(server);
411 trimarchi 965
 
966
  f=kern_fsave();
967
  if (recalculate_contract(fsf_max_bw)==-1)  {
968
       kern_frestore(f);
873 trimarchi 969
       return  FSF_ERR_INTERNAL_ERROR;
411 trimarchi 970
  }
971
#ifdef  FSF_DEBUG
972
  kern_printf("(Adjust budget)");
973
#endif
974
  for (i=0; i<current_server; i++) {
963 trimarchi 975
 
411 trimarchi 976
    mul32div32to32(MAX_BANDWIDTH,server_list[i].Cmin,server_list[i].U,T);
963 trimarchi 977
 
417 giacomo 978
    if (T > server_list[i].Tmin ) {
411 trimarchi 979
      server_list[i].actual_budget = server_list[i].Cmin;
963 trimarchi 980
 
981
      if (T > server_list[i].Tmax)
982
	T=server_list[i].Tmax;
983
 
411 trimarchi 984
      server_list[i].actual_period = T;
963 trimarchi 985
      server_list[i].actual_budget = server_list[i].Cmin;
986
 
417 giacomo 987
      #ifdef FSF_DEBUG
988
        kern_printf("(1 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period);
989
      #endif
963 trimarchi 990
 
908 trimarchi 991
      if (server_list[i].d_equals_t == true)
963 trimarchi 992
	adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, T);
661 giacomo 993
      else
963 trimarchi 994
	adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, T); // server_list[i].deadline);
995
 
411 trimarchi 996
    } else {
963 trimarchi 997
 
417 giacomo 998
      mul32div32to32(server_list[i].Tmin,server_list[i].U,MAX_BANDWIDTH,Q);
963 trimarchi 999
 
1000
 
1001
      if (Q>server_list[i].Cmax)
1002
	Q=server_list[i].Cmax;
1003
 
411 trimarchi 1004
      server_list[i].actual_budget = Q;
417 giacomo 1005
      server_list[i].actual_period = server_list[i].Tmin;
1006
      #ifdef FSF_DEBUG
1007
         kern_printf("(2 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period);
1008
      #endif
661 giacomo 1009
 
908 trimarchi 1010
      if (server_list[i].d_equals_t == true)
963 trimarchi 1011
	adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].Tmin);
661 giacomo 1012
      else
963 trimarchi 1013
	adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].Tmin); //server_list[i].deadline);
661 giacomo 1014
 
963 trimarchi 1015
 
411 trimarchi 1016
    }
416 trimarchi 1017
    server_list[i].U=server_list[i].Umin;
411 trimarchi 1018
 
1019
  }
221 giacomo 1020
 
405 trimarchi 1021
 
411 trimarchi 1022
  kern_frestore(f);
221 giacomo 1023
 
1024
  return 0;
411 trimarchi 1025
 
221 giacomo 1026
}
1027
 
881 trimarchi 1028
bandwidth_t SERVER_return_bandwidth() {
1029
  int i=0;
1030
  bandwidth_t U;
1031
  U=0;
1032
  for(i=0;i<current_server;i++) {
1033
 
1034
    U+=server_list[i].Umin;
1035
 
1036
  }
1037
 
1038
  return U;
1039
}
1040
 
405 trimarchi 1041
int recalculate_contract(bandwidth_t U) {
416 trimarchi 1042
  bandwidth_t current_bandwidth;
418 giacomo 1043
  unsigned int temp_U;
416 trimarchi 1044
  int        Qt;
405 trimarchi 1045
  int isok=0;
1046
  int i=0;
868 trimarchi 1047
  int target_importance=FSF_DEFAULT_IMPORTANCE;
1048
 
1049
#define MAX_IMPORTANCE 5
405 trimarchi 1050
 
896 trimarchi 1051
#ifdef FSF_DEBUG
1052
  int temp;
1053
 
908 trimarchi 1054
  kern_printf("(RC)");
896 trimarchi 1055
#endif
405 trimarchi 1056
 
1057
  /* The current bandwidth is the min bandwidth */
881 trimarchi 1058
  current_bandwidth=SERVER_return_bandwidth(fsf_server_level);
405 trimarchi 1059
  #ifdef FSF_DEBUG
908 trimarchi 1060
     kern_printf("(SER%d)", current_server);
405 trimarchi 1061
  #endif
880 trimarchi 1062
  //kern_printf("(CS:%d)", current_server);
405 trimarchi 1063
  do  {
1064
    current_bandwidth=0;
1065
    Qt=0;
410 trimarchi 1066
    for (i=0; i<current_server; i++) {
843 trimarchi 1067
      if (server_list[i].Is==target_importance
1068
	  && server_list[i].U<server_list[i].Umax && server_list[i].Qs>0)
405 trimarchi 1069
         Qt+=server_list[i].Qs;
416 trimarchi 1070
       current_bandwidth+=server_list[i].U;
868 trimarchi 1071
#ifdef FSF_DEBUG
843 trimarchi 1072
       kern_printf("(Qs %d, Qt %d, Is %d)", server_list[i].Qs, Qt,server_list[i].Is);
868 trimarchi 1073
#endif
405 trimarchi 1074
    }
413 trimarchi 1075
 
868 trimarchi 1076
#ifdef FSF_DEBUG
908 trimarchi 1077
    kern_printf("(TQ%d)", Qt);
868 trimarchi 1078
#endif
405 trimarchi 1079
    isok=1;
410 trimarchi 1080
    for (i=0; i<current_server; i++) {
843 trimarchi 1081
      if (server_list[i].Is==target_importance && server_list[i].U<server_list[i].Umax && server_list[i].Qs>0) {
416 trimarchi 1082
	temp_U=server_list[i].U;
1083
        server_list[i].U=U-current_bandwidth;
963 trimarchi 1084
	//kern_printf("before mull");
416 trimarchi 1085
	mul32div32to32(server_list[i].U, server_list[i].Qs, Qt, server_list[i].U);
963 trimarchi 1086
	//kern_printf("after mull");
416 trimarchi 1087
	temp_U+=server_list[i].U;
1088
 
1089
        if (temp_U<=server_list[i].Umin) {
405 trimarchi 1090
	   server_list[i].U=server_list[i].Umin;
416 trimarchi 1091
	} else if (temp_U>server_list[i].Umax)  {
1092
	   server_list[i].U=server_list[i].Umax;
405 trimarchi 1093
	   isok=0;
1094
	} else server_list[i].U=temp_U;
1095
 
1096
#ifdef FSF_DEBUG
1097
	mul32div32to32(server_list[i].U,100, MAX_BANDWIDTH, temp);
908 trimarchi 1098
        kern_printf("(SER %d BW %d)", server_list[i].server, temp);
405 trimarchi 1099
#endif
1100
      }
1101
    }
843 trimarchi 1102
    target_importance++;
880 trimarchi 1103
  } while (!isok || target_importance<=MAX_IMPORTANCE);
963 trimarchi 1104
 
405 trimarchi 1105
 return 0;
1106
}
1107
 
937 trimarchi 1108
 
1109
int fsf_negotiate_group
1110
   (const fsf_contracts_group_t *contracts_up,
1111
    const fsf_servers_group_t   *severs_down,
1112
    fsf_servers_group_t         *severs_up,
1113
    bool                        *accepted) {
1114
 
1115
  return 0;
1116
}
1117
 
221 giacomo 1118
int fsf_renegotiate_contract
1119
  (const fsf_contract_parameters_t *new_contract,
1120
   fsf_server_id_t server)
1121
{
679 trimarchi 1122
 
908 trimarchi 1123
  struct mess msg;
221 giacomo 1124
 
679 trimarchi 1125
  // send response server is -1 if the operation fail
908 trimarchi 1126
  msg.type=RENEGOTIATE_CONTRACT;
1127
  memmove(&msg.contract,new_contract, sizeof(fsf_contract_parameters_t));
1128
  msg.server = server;
1129
  //kern_printf("(REN %d)", server);
1130
  port_send(channel[1],&msg,BLOCK);
963 trimarchi 1131
  //kern_printf("After send\n");
908 trimarchi 1132
  port_receive(channel[0], &msg, BLOCK);
963 trimarchi 1133
  //kern_printf("After receive\n");
1134
  //kern_printf("(EREN %d)", msg.server);
908 trimarchi 1135
  if (msg.server==-1) return FSF_ERR_CONTRACT_REJECTED;
679 trimarchi 1136
 
405 trimarchi 1137
   return 0;
221 giacomo 1138
}
241 giacomo 1139
 
868 trimarchi 1140
int fsf_request_contract_renegotiation
1141
  (const fsf_contract_parameters_t *new_contract,
1142
   fsf_server_id_t                  server,
1143
   int                              sig_notify,
1144
   union sigval                     sig_value)
1145
{
1146
 
908 trimarchi 1147
  struct mess msg;
868 trimarchi 1148
 
1149
  // send response server is -1 if the operation fail
908 trimarchi 1150
  msg.type=REQUEST_RENEGOTIATE_CONTRACT;
1151
  memmove(&msg.contract,new_contract, sizeof(fsf_contract_parameters_t));
1152
  msg.server = server;
910 trimarchi 1153
  msg.sig_notify=sig_notify;
1154
  msg.sig_value=sig_value;
1155
  msg.process=exec_shadow;
868 trimarchi 1156
 
908 trimarchi 1157
  port_send(channel[1],&msg,BLOCK);
868 trimarchi 1158
 
908 trimarchi 1159
  port_receive(channel[0], &msg, BLOCK);
868 trimarchi 1160
 
908 trimarchi 1161
  if (msg.server==-1) return FSF_ERR_CONTRACT_REJECTED;
868 trimarchi 1162
 
1163
   return 0;
1164
}
1165
 
407 giacomo 1166
void print_server_list()
1167
{
1168
 
1169
  int i;
1170
 
1171
  kern_printf("Server List\n");
1172
 
410 trimarchi 1173
  for(i=0;i<current_server;i++) {
407 giacomo 1174
 
662 giacomo 1175
    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 1176
 
1177
  }
1178
 
1179
}
808 trimarchi 1180
 
963 trimarchi 1181
int
1182
fsf_get_remaining_budget
1183
   (fsf_server_id_t server,
1184
    struct timespec *budget)  {
1185
  TIME t;
1186
  NULL_TIMESPEC(budget);
1187
  t=SERVER_get_remain_capacity(fsf_server_level, server);
1188
  ADDUSEC2TIMESPEC(t, budget);
1189
  return 0;
1190
 
1191
 
1192
}
1193
/*
1194
int fsf_get_remaining_budget(fsf_server_id_t server) {
1195
 
825 trimarchi 1196
  return SERVER_get_remain_capacity(fsf_server_level, server);
808 trimarchi 1197
}
963 trimarchi 1198
*/
868 trimarchi 1199
int fsf_get_budget_and_period
1200
   (fsf_server_id_t server,
1201
    struct timespec *budget,
1202
    struct timespec *period) {
1203
  TIME bg;
1204
  TIME pd;
1205
 
1206
  if (!SERVER_getbudgetinfo(fsf_server_level, &bg, &pd, NULL, server)) {
1207
    if (budget) {
1208
      NULL_TIMESPEC(budget);
1209
      ADDUSEC2TIMESPEC(bg, budget);
1210
    }
1211
    if (period) {
1212
      NULL_TIMESPEC(period);
1213
      ADDUSEC2TIMESPEC(pd, period);
1214
    }
1215
 
1216
    return 0;
1217
  }
1218
  return FSF_ERR_BAD_ARGUMENT;
1219
}
1220
 
1221
int
1222
fsf_set_service_thread_data
1223
   (const struct timespec *budget,
1224
    const struct timespec *period,
1225
    bool                  *accepted) {
1226
 
1227
  if (budget==NULL && period==NULL) return FSF_ERR_BAD_ARGUMENT;
1228
  fsf_set_contract_basic_parameters(&service_contract,budget,period,FSF_DEFAULT_WORKLOAD);
1229
  *accepted = !fsf_renegotiate_contract(&service_contract,service_server)?true:false;
1230
  return 0;
1231
 
1232
}
1233
 
1234
 
1235
 
1236
int fsf_get_service_thread_data
1237
   (struct timespec *budget,
1238
    struct timespec *period) {
1239
 
1240
  return fsf_get_budget_and_period(service_server, budget, period);
1241
 
1242
}
944 trimarchi 1243
 
1244
int fsf_init_local_scheduler(fsf_server_id_t server,
1245
			     fsf_sched_init_info_t info)  {
1246
  int scheduler_id, local_scheduler_level;
1247
 
1248
  struct timespec *duration=(struct timespec *)info;
1249
 
1250
  local_scheduler_level=SERVER_get_local_scheduler_level_from_budget(fsf_server_level, server);
1251
  scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server);
1252
 
1253
  if (scheduler_id!=FSF_TABLE_DRIVEN)
1254
    return FSF_ERR_SCHED_POLICY_NOT_COMPATIBLE;
1255
 
1256
  if (duration->tv_sec < 0 || duration->tv_nsec > 1000000000)
1257
    return FSF_ERR_BAD_ARGUMENT;
1258
 
1259
  //TDSTAR_debugtable(local_scheduler_level);
1260
  TDSTAR_start_simulation(local_scheduler_level);
1261
  return 0;
1262
 
1263
}
963 trimarchi 1264
 
1265
int
1266
fsf_get_total_quality
1267
(fsf_server_id_t server, int *total_quality) {
1268
 
1269
  return 0;
1270
}
1271
 
1272
int
1273
fsf_get_available_capacity (
1274
    fsf_server_id_t server, uint32_t *capacity){
1275
 
1276
  return 0;
1277
}
1278
 
1279
bool
1280
fsf_is_admission_test_enabled() {
1281
  return true;
1282
}
1283
 
1284
 
1285
int
1286
fsf_get_cpu_time
1287
   (fsf_server_id_t server,
1288
    struct timespec *cpu_time) {
1289
 
1290
  return 0;
1291
}
1292
 
1293
 
1294
int
1295
fsf_get_contract
1296
   (fsf_server_id_t server,
1297
    fsf_contract_parameters_t *contract) {
1298
 
1299
  return 0;
1300
}