Subversion Repositories shark

Rev

Blame | Last modification | View Log | RSS feed

/*
 * Project: S.Ha.R.K.
 *
 * Coordinators:
 *   Giorgio Buttazzo    <giorgio@sssup.it>
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *
 * Authors     :
 *   Giacomo Guidi       <giacomo@gandalf.sssup.it>
 *
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://shark.sssup.it
 */


/*
 * Copyright (C) 2000 Paolo Gai
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include "kernel/kern.h"

#include "fsf.h"
#include "fsf_server.h"

#include "stdlib.h"
#include "unistd.h"
#include "string.h"
#include "pistar.h"

#include "pthread.h"

#include "drivers/glib.h"
#include "global.h"

#include <drivers/shark_keyb26.h>


char *readFilePortName = "readFilePort";
char *displayPortName = "displayPortName";

#define TEST_PERIOD 50000

mutex_t mux;
int main_chunk;



/* 10% */
struct timespec decoderPeriod = {0,30000000};
struct timespec decoderBudget = {0,3000000};

/* 10% */
struct timespec readTaskPeriod = {0,30000000};
struct timespec readTaskBudget = {0,3000000};

/* 45% */
struct timespec displayPeriod = {0,40000000};
struct timespec displayBudget = {0,18000000};

// 5 %
struct timespec overloadPeriod = {0,10000000};
struct timespec overloadBudget = {0,500000};

fsf_server_id_t decoderServer;
fsf_server_id_t readTaskServer;
fsf_server_id_t displayServer;
fsf_server_id_t overloadServer;

fsf_contract_parameters_t overloadContract;
fsf_contract_parameters_t decoderContract;
fsf_contract_parameters_t readTaskContract;
fsf_contract_parameters_t displayContract;


void program_end()
{

  grx_close();
  exit(0);
}

void *mpeg2decoder(void *arg);
void *readTask(void *arg);
void *displayTask(void *arg);
void *overLoadTask(void *arg);

/***************************************************************
 *
 * Create the display task and bind it to a server
 *
 * Author: Robert Bäckgren
 ******************************************************************/

void addDisplayTaskToServer(fsf_server_id_t server)
{
  int err;
  pthread_t j = -1;
  HARD_TASK_MODEL ht;
  static fsf_sched_params_t pr;
 

  pr.policy=FSF_EDF;
  pr.params=&ht;
 

  hard_task_default_model(ht);
  hard_task_def_mit(ht,40000);
  hard_task_def_wcet(ht,18000);
  nrt_task_def_group(ht,3);

  /*  
  err = pthread_create(&j, NULL, displayTask, NULL);
  if (err) {  
  grx_close();
  grx_text("failed to create displayTask",0,0,rgb16(255,255,255),0);
  exit(err);
  }
 
  err=fsf_bind_local_thread_to_server(server, j,(fsf_sched_params_t *)(&pr));
  if (err) {
  grx_close();
  exit(err);
 
  }
  */
   
  err = fsf_create_local_thread(server,(fsf_sched_params_t *)(&pr), &j,NULL,
              displayTask,NULL);
  if (err) {  
    //grx_close();
    //grx_text("failed to create displayTask",0,0,rgb16(255,255,255),0);
    exit(err);
  }
 task_activate(j);

}

/*********************************************************************'
 *
 * Create the input task and bind it to a server
 *
 * Author: Robert Bäckgren
 *********************************************************************/

void addReadTaskToServer(fsf_server_id_t server)
{
  int err;
  pthread_t j = -1;
  NRT_TASK_MODEL ht;
  static fsf_sched_params_t pr;
 

  pr.policy=FSF_RR;
  pr.params=&ht;
 

  nrt_task_default_model(ht);
  nrt_task_def_group(ht,1);

  /*
  err = pthread_create(&j, NULL, readTask, NULL);
  if (err) {  
  grx_close();
  perror("Could not create task...");
  exit(err);
  }
 
  err=fsf_bind_local_thread_to_server(server, j,(fsf_sched_params_t *)(&pr));
  if (err) {
   grx_close();
   //perror("Could not bind task..");
   exit(err);
   }  
  */

 
 err = fsf_create_local_thread(server,(fsf_sched_params_t *)(&pr), &j,NULL,
                                readTask,NULL);
 if (err) {  
   //grx_close();
   //grx_text("failed to create displayTask",0,0,rgb16(255,255,255),0);
   exit(err);
 }
 task_activate(j);
}

/**********************************************************************
 *
 * Create the decoder task and bind it to a server
 *
 * Author: Robert Bäckgren
 **********************************************************************/

void addDecoderThreadToServer(fsf_server_id_t server)
{

  int err;
  pthread_t j = -1;
  NRT_TASK_MODEL ht;
  static fsf_sched_params_t pr;
 
 
 
  pr.policy=FSF_RR;
  pr.params=&ht;
 

  nrt_task_default_model(ht);
  nrt_task_def_group(ht,1);
 
  /*
    err = pthread_create(&j, NULL, mpeg2decoder, NULL);
    if (err) {  
    grx_close();
    perror("Could not create task...");
    exit(err);
    }
   
    err=fsf_bind_local_thread_to_server(server, j,(fsf_sched_params_t *)(&pr));
 if (err) {
 grx_close();
 //perror("Could not bind task..");
 exit(err);
 }
  */

 err = fsf_create_local_thread(server,(fsf_sched_params_t *)(&pr), &j,NULL,
                                mpeg2decoder,NULL);
 if (err) {  
   //grx_close();
   //grx_text("failed to create displayTask",0,0,rgb16(255,255,255),0);
   exit(err);
 }
 task_activate(j);
}


/***************************************************************************
 *
 * Create a dummy task and bind it to a server
 *
 * Author: Robert Bäckgren
 *****************************************************************************/

int  addOverLoadTaskToServer(fsf_server_id_t server)
{
  int err;
  pthread_t j = -1;
  HARD_TASK_MODEL ht;
  static fsf_sched_params_t pr;

 

  pr.policy=FSF_EDF;
  pr.params=&ht;
 

  hard_task_default_model(ht);
  hard_task_def_mit(ht,2000000);
  hard_task_def_wcet(ht,20000);
  nrt_task_def_group(ht,4);

  /*
    err = pthread_create(&j, NULL, overLoadTask, NULL);
    if (err) {
    grx_text("Failed to create the decoder task",270,270,rgb16(255,255,255),0);  
    return 0;
    }
   
    err=fsf_bind_local_thread_to_server(server, j,(fsf_sched_params_t *)(&pr));
    if (err) {
    grx_close();
    //perror("Could not bind task..");
    exit(err);
    }
  */

  err = fsf_create_local_thread(server,(fsf_sched_params_t *)(&pr), &j,NULL,
                                overLoadTask,NULL);
  if (err) {  
    //grx_close();
    //grx_text("failed to create displayTask",0,0,rgb16(255,255,255),0);
    exit(err);
  }
  task_activate(j);
 return 1;
}

void ending_system(KEY_EVT *e) {
 
  grx_close();

  exit(0);
}


int main(int argc, char **argv)
{
 
  char ch;
  int err;
  int i = 1;
  char tmp[100];
  struct timespec simtime;
  KEY_EVT k;
  union sigval no_sigval = {0};

  PI_mutexattr_t a;
  KEY_EVT ev;



  union sigval sval;
 

  ev.ascii = 'c';
  ev.scan  = KEY_C;
  ev.status = KEY_PRESSED;
  ev.flag = CNTR_BIT;
  keyb_hook(ev, ending_system, FALSE);

  PI_mutexattr_default(a);
  fsf_init();
 
  mutex_init(&mux,&a);
 
  // Set up the different servers
  fsf_initialize_contract(&readTaskContract);
  err=fsf_set_contract_basic_parameters(&readTaskContract,&readTaskBudget,&readTaskPeriod,FSF_DEFAULT_WORKLOAD);
  if (err) {
    exit(err);
  }
  err=fsf_set_contract_reclamation_parameters(&readTaskContract,&readTaskBudget,&readTaskPeriod,FSF_DEFAULT_GRANULARITY, NULL, 1,1);
  if (err) exit(err);
  err=fsf_set_contract_timing_requirements(&readTaskContract,true,NULL,0,no_sigval,0,no_sigval);
  if (err) exit(err);

  fsf_set_contract_scheduling_policy(&readTaskContract, FSF_RR);

  fsf_initialize_contract(&decoderContract);
  err=fsf_set_contract_basic_parameters(&decoderContract,&decoderBudget,&decoderPeriod,FSF_DEFAULT_WORKLOAD);
  if (err) {
    grx_close();
    exit(err);
  }

  err=fsf_set_contract_reclamation_parameters(&decoderContract,&decoderBudget,&decoderPeriod,FSF_DEFAULT_GRANULARITY, NULL, 1,1);
  if (err) exit(err);
  err=fsf_set_contract_timing_requirements(&decoderContract,true,NULL,0,no_sigval,0,no_sigval);
  if (err) exit(err);

  fsf_set_contract_scheduling_policy(&decoderContract, FSF_RR);

  fsf_initialize_contract(&displayContract);
  err=fsf_set_contract_basic_parameters(&displayContract,&displayBudget,&displayPeriod,FSF_DEFAULT_WORKLOAD);
  if (err) {
    exit(err);
  }

  err=fsf_set_contract_reclamation_parameters(&displayContract,&displayBudget,&displayPeriod,FSF_DEFAULT_GRANULARITY, NULL, 1,1);
  if (err) exit(err);
  err=fsf_set_contract_timing_requirements(&displayContract,true,NULL,0,no_sigval,0,no_sigval);
  if (err) exit(err);

  fsf_set_contract_scheduling_policy(&displayContract, FSF_EDF);

  fsf_initialize_contract(&overloadContract);
  err=fsf_set_contract_basic_parameters(&overloadContract,&overloadBudget,&overloadPeriod,FSF_DEFAULT_WORKLOAD);
  if (err) {
    exit(err);
  }

  err=fsf_set_contract_reclamation_parameters(&overloadContract,&overloadBudget,&overloadPeriod,FSF_DEFAULT_GRANULARITY, NULL, 1,1);
  if (err) exit(err);
  err=fsf_set_contract_timing_requirements(&overloadContract,true,NULL,0,no_sigval,0,no_sigval);
  if (err) exit(err);

  fsf_set_contract_scheduling_policy(&overloadContract, FSF_EDF);
 
  err = fsf_negotiate_contract(&readTaskContract,&readTaskServer);
  if (err) {
    exit(err);
  }
 
  err = fsf_negotiate_contract(&decoderContract,&decoderServer);
  if (err) {
    exit(err);
  }
  err = fsf_negotiate_contract(&displayContract,&displayServer);
  if (err) {
    exit(err);
  }

  err = fsf_negotiate_contract(&overloadContract,&overloadServer);
  if (err) {
    exit(err);
  }

  print_server_list();
 

  // Create and bind different ports to communication links (This ports a declared in global.h)
  if((readTaskPort = port_create(readFilePortName, sizeof(struct readDecodeMessage), NUMBER_OF_MESSAGE, STREAM, WRITE)) == -1)
    {
      //grx_text("failed to create readTask Port",0,0,rgb16(255,255,255),0);
      exit(-1);
    }
  if((decoderTaskPort->displayTaskPort = port_create(displayPortName, sizeof(struct decodeDisplayMessage), NUMBER_OF_MESSAGE, STREAM, WRITE)) == -1)
    {
      //grx_text("failed to create displayTask Port",0,0,rgb16(255,255,255),0);      
      exit(-1);
    }
  if((decoderTaskPort->readTaskPort = port_connect(readFilePortName, sizeof(struct readDecodeMessage), STREAM, READ)) == -1)
    {
      //grx_text("failed to connect to readTask Port",0,0,rgb16(255,255,255),0);
      exit(-1);
    }
  if((displayTaskPort = port_connect(displayPortName, sizeof(struct decodeDisplayMessage), STREAM, READ)) == -1)
    {
      //grx_text("failed to connect to displayTask Port",0,0,rgb16(255,255,255),0);
      exit(-1);
    }

  // graphic card Initialization
  if (grx_init() < 1) {    
    exit(-1);
  }

  if (grx_open(800, 600, 16) < 0) {
    cprintf("GRX Err\n");
    exit(-1);
  }
 
  /*
  kern_gettime(&simtime);
  for (i=1; i<10;i++)  {
    simtime.tv_sec+=1;
    kern_event_post(&simtime,(void *)((void *)(addOverLoadTaskToServer)), (void *)overloadServer);
  }

  simtime.tv_sec+=10;
  kern_event_post(&simtime,(void *)((void *)(ending_system)), NULL);
  */

  ch = keyb_getch(BLOCK);
  kern_printf("Main %d\n", ch);
  while(ch != ESC) {

      switch (ch)
        {

            case '1':
              addReadTaskToServer(readTaskServer);
              addDecoderThreadToServer(decoderServer);
              addDisplayTaskToServer(displayServer);

            break;
            case '2':
              sprintf(tmp,"Number of dummy task: %d", i);
              //grx_text(tmp,0,0,rgb16(255,255,255),0);
              i++;
              addOverLoadTaskToServer(overloadServer);
            break;
            case '4':
              ending_system(NULL);
              break;
        }

    ch = keyb_getch(BLOCK);
               
  }

   ending_system(NULL);
   //   while(1);
 
 return 0;

}