Subversion Repositories shark

Rev

Rev 407 | Rev 409 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

//=====================================================================
//       FFFFFFIII   RRRRR      SSTTTTTTT
//      FF         IIR   RR    SS
//     FF           IR        SS
//    FFFFFF         RRRR    SSSSST      
//   FF       FI       RRR  SS
//  FF         II     RRR  SS
// FF           IIIIIR    RS 
//       
// Basic FSF(FIRST Scheduling Framework) contract management
// S.Ha.R.K. Implementation
//=====================================================================

#include "ll/i386/64bit.h"

#include "fsf_contract.h"
#include "fsf_server.h"

#include <pthread.h>
#include <stdlib.h>

//#define FSF_DEBUG

int current=0;
server_elem server_list[FSF_MAX_N_SERVERS];
bandwidth_t fsf_max_bw = 0;
 
int fsf_server_level;

int FSF_register_module(int server_level, bandwidth_t max_bw)
{
  printk("FSF Module\n");
  current=0;
  fsf_server_level = server_level;
  fsf_max_bw = max_bw;

  return 0;

}

/* Convert the contract specification to
 * budget parameters
 */
int set_SERVER_budget_from_contract
  (const fsf_contract_parameters_t *contract,
   int *budget)
{

  int local_scheduler_level = 0;

   switch (contract->local_scheduler_id) {
     case FSF_SCHEDULER_POSIX:
       local_scheduler_level = POSIXSTAR_register_level(fsf_server_level,5000,32);
       break;
     case FSF_SCHEDULER_EDF:
       local_scheduler_level = EDFSTAR_register_level(fsf_server_level);
       break;
     case FSF_SCHEDULER_RM:
       local_scheduler_level = RMSTAR_register_level(fsf_server_level);
       break;
     case FSF_SCHEDULER_MPEG:
       local_scheduler_level = MPEGSTAR_register_level(fsf_server_level);
       break;
   }    

  *budget = SERVER_setbudget(fsf_server_level,
                              TIMESPEC2USEC(&(contract->budget_min)),
                              TIMESPEC2USEC(&(contract->period_max)),
                              local_scheduler_level,contract->local_scheduler_id);

  return 0;

}

int adjust_SERVER_budget
   (int budget, const TIME budget_actual, 
    const TIME period_actual)
{

  SERVER_adjust_budget(fsf_server_level, 
                       budget_actual,
                       period_actual,
                       budget);

  return 0;

}

/* Admission Test function */
int add_contract(const fsf_contract_parameters_t *contract)
{
  return 0;

}

int link_contract_to_server(const fsf_contract_parameters_t *contract,
                            fsf_server_id_t server)
{
  TIME T,Q;
#ifdef FSF_DEBUG
  int temp;

  kern_printf("(Link Server %d)",server);
#endif
  
  server_list[current].server=server;
  server_list[current].Qs=1;
  
  T=TIMESPEC2USEC(&contract->period_min);
  Q=TIMESPEC2USEC(&contract->budget_max); 
  mul32div32to32(MAX_BANDWIDTH,Q,T,server_list[current].Umax);
  
  T=TIMESPEC2USEC(&contract->period_max);
  server_list[current].Tmax=T;
          
  Q=TIMESPEC2USEC(&contract->budget_min);
  server_list[current].Cmin=Q;
  
  mul32div32to32(MAX_BANDWIDTH,Q,T,server_list[current].Umin);
  server_list[current].U=server_list[current].Umax;

#ifdef FSF_DEBUG
  mul32div32to32(server_list[current].Umax,100, MAX_BANDWIDTH, temp);
  kern_printf("(Umax %d)",temp);
  mul32div32to32(server_list[current].Umin,100, MAX_BANDWIDTH, temp);
  kern_printf("(Umin %d)",temp);
#endif
    
  
  current++;
  return 0;

}

int remove_contract(fsf_server_id_t server)
{
  int i=0;
  // find the contract
  while(i<current) {
     if (server_list[i].server==server) break;
     i++;
  }

  // compress the array;
  while (i<(current-1)) {
     server_list[i].server=server_list[i+1].server;
     server_list[i].Umin=server_list[i+1].Umin;
     server_list[i].U=server_list[i+1].Umax;     
     server_list[i].Umax=server_list[i+1].Umax;
     server_list[i].Cmin=server_list[i+1].Cmin;
     server_list[i].Tmax=server_list[i+1].Tmax;
     server_list[i].Qs=server_list[i+1].Qs;
     i++;
  }
  current--;


  return 0;

}


int fsf_negotiate_contract
  (const fsf_contract_parameters_t *contract,
   fsf_server_id_t                 *server)
{
  SYS_FLAGS f;
  int i=0;
  TIME T;
  TIME Q;
  /* Check if contract is initialized */
  if (!contract) return FSF_ERR_NOT_INITIALIZED;

  /* Admission Test */
  if (FSF_ADMISSION_TEST_IS_ENABLED)
    if (add_contract(contract))
      return FSF_ERR_CONTRACT_REJECTED; 
  f = kern_fsave();

  /* SERVER => BUDGET */    
  set_SERVER_budget_from_contract(contract,server);

  #ifdef FSF_DEBUG
    kern_printf("(New Server %d)",*server);
  #endif

  if (*server >= 0) {
    link_contract_to_server(contract,*server);
    if (recalculate_contract(fsf_max_bw)==-1)  {
       kern_frestore(f);
       return FSF_ERR_CREATE_SERVER;
    }
#ifdef  FSF_DEBUG
    kern_printf("(Adjust budget)");
#endif    
    for (i=0; i<current; i++) {
       mul32div32to32(MAX_BANDWIDTH,server_list[i].Cmin,server_list[i].U,T);
       #ifdef FSF_DEBUG
       kern_printf("(T %ld)", T);
       #endif
       if (T<=server_list[i].Tmax) {
          server_list[i].actual_budget = server_list[i].Cmin;
          server_list[i].actual_period = T;
          adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T);
       } else {
         mul32div32to32(server_list[i].Tmax,server_list[i].U,MAX_BANDWIDTH,Q);
         #ifdef FSF_DEBUG
         kern_printf("(Q %ld)", Q);
         #endif
         server_list[i].U=server_list[i].Umax;
         server_list[i].actual_budget = Q;
         server_list[i].actual_period = server_list[i].Tmax;
         adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmax);
       }

    }
  }
  else  {
    kern_frestore(f);
    return FSF_ERR_CREATE_SERVER;
  }
  kern_frestore(f);
 
  return 0;

}

int fsf_create_thread
  (fsf_server_id_t    server,
   pthread_t         *thread,
   pthread_attr_t    *attr,
   fsf_thread_code_t  thread_code,
   void              *arg,
   void              *local_scheduler_arg)
{

  int local_scheduler_level,scheduler_id;

  #ifdef FSF_DEBUG 
    kern_printf("(FSF:Insert thread = %d to Server = %d)",*thread,server);
  #endif

  /* Check if server and thread exsist */
  if (server == NIL)
    return FSF_ERR_INVALID_SERVER;

  local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server);
  scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server);

  /* Check if thread is already bind */
  switch (scheduler_id) {
    case FSF_SCHEDULER_POSIX:

      nrt_task_def_arg(*(NRT_TASK_MODEL *)(local_scheduler_arg),arg);
      nrt_task_def_level(*(NRT_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level);

      *thread = task_create("POSIXSTAR", thread_code, local_scheduler_arg, NULL);
      if (*thread == NIL) {
        #ifdef FSF_DEBUG
          kern_printf("(FSF:Error creating thread)");
        #endif
        return FSF_ERR_CREATE_THREAD;
      }

      POSIXSTAR_setbudget(local_scheduler_level, *thread, (int)(server));

    break;
    case FSF_SCHEDULER_EDF:

      hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg);
      hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level);

      *thread = task_create("EDFSTAR", thread_code, local_scheduler_arg, NULL);
      if (*thread == NIL)
        return FSF_ERR_CREATE_THREAD;

      EDFSTAR_setbudget(local_scheduler_level, *thread, (int)(server));

      break;

    case FSF_SCHEDULER_RM:

      hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg);
      hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level);

      *thread = task_create("RMSTAR", thread_code, local_scheduler_arg, NULL);
      if (*thread == NIL)
        return FSF_ERR_CREATE_THREAD;

      RMSTAR_setbudget(local_scheduler_level, *thread, (int)(server));

      break;
    case FSF_SCHEDULER_MPEG:
                                                                                                                             
      hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg);
      hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level);
                                                                                                                             
      *thread = task_create("MPEGSTAR", thread_code, local_scheduler_arg, NULL);
      if (*thread == NIL)
        return FSF_ERR_CREATE_THREAD;
                                                                                                                             
      MPEGSTAR_setbudget(local_scheduler_level, *thread, (int)(server));
                                                                                                                             
      break;
 
    default:
      return FSF_ERR_INVALID_SERVER;
      break;
  }

  return 0;
  
}

int fsf_get_server
  (fsf_server_id_t *server,
   pthread_t       thread)
{
  int local_scheduler_level, scheduler_id;

  local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread);
  scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread);

  switch (scheduler_id) {
    case FSF_SCHEDULER_POSIX:  
      return POSIXSTAR_getbudget(local_scheduler_level,thread);
    case FSF_SCHEDULER_EDF:
      return EDFSTAR_getbudget(local_scheduler_level,thread);
    case FSF_SCHEDULER_RM:
      return RMSTAR_getbudget(local_scheduler_level,thread);
    case FSF_SCHEDULER_MPEG:
      return MPEGSTAR_getbudget(local_scheduler_level,thread);
    default:
      return -1;
  }

  return -1;

}

int fsf_get_server_level(void)
{

  return fsf_server_level;

}

int fsf_cancel_contract
  (fsf_server_id_t *server)
{

  int local_scheduler_level, scheduler_id;

  #ifdef FSF_DEBUG
    kern_printf("(Remove server %d)",*server);
  #endif

  /* Check server id */
  if (*server < 0)
    return FSF_ERR_INVALID_SERVER;

  local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,*server);
  scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,*server);

  switch (scheduler_id) {
    case FSF_SCHEDULER_POSIX:
  
      /* Check if some thread use the server */
      if(POSIXSTAR_budget_has_thread(local_scheduler_level,*server))
        return FSF_ERR_SERVER_USED;
 
      break;
    case FSF_SCHEDULER_EDF:
      /* Check if some thread use the server */
      if(EDFSTAR_budget_has_thread(local_scheduler_level,*server))
        return FSF_ERR_SERVER_USED;
      break;

    case FSF_SCHEDULER_RM:
      /* Check if some thread use the server */
      if(RMSTAR_budget_has_thread(local_scheduler_level,*server))
        return FSF_ERR_SERVER_USED;

      break;

    case FSF_SCHEDULER_MPEG:
      /* Check if some thread use the server */
      if(MPEGSTAR_budget_has_thread(local_scheduler_level,*server))
        return FSF_ERR_SERVER_USED;
                                                                                                                             
      break;

  }

  SERVER_removebudget(fsf_server_level,*server);

  level_free_descriptor(local_scheduler_level); 
        
  remove_contract(*server);

  recalculate_contract(fsf_max_bw); 

  *server = -1;

  return 0;

}

int recalculate_contract(bandwidth_t U) {
  long int current_bandwidth,temp_U;
  int        Qt;
  int isok=0;
  int i=0;
  
  #ifdef FSF_DEBUG
    int temp;
  
    kern_printf("(Recalculate contract)");
  #endif

  /* The current bandwidth is the min bandwidth */
  //current_bandwidth=SERVER_return_bandwidth(fsf_server_level);
  #ifdef FSF_DEBUG
     kern_printf("(nserver %d)", current);
  #endif  
  
  do  {
    current_bandwidth=0;
    Qt=0;
    for (i=0; i<current; i++) {
      if (server_list[i].Qs!=0 && server_list[i].U>server_list[i].Umin)
         Qt+=server_list[i].Qs; 
       current_bandwidth+=server_list[i].U;
    }
    
    #ifdef FSF_DEBUG
    kern_printf("(Total Quality %d)", Qt);
    #endif
    isok=1;
    for (i=0; i<current; i++) {
      if (server_list[i].Qs!=0 && server_list[i].U>server_list[i].Umin) {
        temp_U=server_list[i].U;
        temp_U=temp_U-(current_bandwidth-U)*server_list[i].Qs/Qt;
        
        if (temp_U<server_list[i].Umin) {
           server_list[i].U=server_list[i].Umin;
           isok=0;
        } else if (temp_U>=server_list[i].Umax)  {
           server_list[i].U=server_list[i].Umax;
        } else server_list[i].U=temp_U; 
        
#ifdef FSF_DEBUG
        mul32div32to32(server_list[i].U,100, MAX_BANDWIDTH, temp);
        kern_printf("(Server %d bw %d)", server_list[i].server, temp);
#endif 
      } 
    }  

  } while (!isok);
  
 return 0;
}

        
int fsf_renegotiate_contract
  (const fsf_contract_parameters_t *new_contract,
   fsf_server_id_t server)
{
  SYS_FLAGS f; 
  #ifdef FSF_DEBUG
    kern_printf("(Renegotiate for server %d)",server);
  #endif

  if (!new_contract)
    return FSF_ERR_NOT_INITIALIZED;

  if (server < 0)
    return FSF_ERR_INVALID_SERVER;
  // compute the new value
  //
   f = kern_fsave();
   if (recalculate_contract(fsf_max_bw)==-1)  {
       kern_frestore(f);
       return FSF_ERR_CREATE_SERVER;
   }
   
   kern_frestore(f);
   return 0;
}

void print_server_list()
{

  int i;
  
  kern_printf("Server List\n");
 
  for(i=0;i<current;i++) {

    kern_printf("[%d] Q:%d T:%d\n",server_list[i].server,(int)server_list[i].actual_budget,(int)server_list[i].actual_period);

  }

}