Subversion Repositories shark

Rev

Rev 1550 | Go to most recent revision | Blame | Compare with Previous | 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>
 *   (see the web pages for full authors list)
 *
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://shark.sssup.it
 */


/*
 * Copyright (C) 2002 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 "edf/edf/edf.h"
#include "cbs/cbs/cbs.h"

#include "pthread.h"
#include "posix/posix/posix.h"

#include "fsf.h"
#include "fsf_server.h"
#include "dummy/dummy/dummy.h"

#include "sem/sem/sem.h"
#include "pi/pi/pi.h"
#include "pc/pc/pc.h"

#include "hartport/hartport/hartport.h"
#include "cabs/cabs/cabs.h"

#include "drivers/keyb.h"
#include <stdlib.h>

// --------------------------------------------------
// --------------------------------------------------
// Init Part
// --------------------------------------------------
// --------------------------------------------------

/*+ sysyem tick in us +*/
#define TICK 0

/*+ RR tick in us +*/
#define RRTICK 10000

int grubstar_level;
int posix_level;


TIME __kernel_register_levels__(void *arg)
{
  struct multiboot_info *mb = (struct multiboot_info *)arg;
 
  EDF_register_level(EDF_ENABLE_ALL);
  posix_level=POSIX_register_level(RRTICK, 1, mb, 32);
  grubstar_level = GRUBSTAR_register_level(5, 0);
  FSF_register_module(posix_level,grubstar_level, (int)(MAX_BANDWIDTH * 0.9));
  dummy_register_level();

  // for the keyboard...
  CBS_register_level(CBS_ENABLE_ALL, 0);

  PI_register_module();
  PC_register_module();

  SEM_register_module();

  PTHREAD_register_module(1, 0, 1);

  return TICK;

}


TASK __init__(void *arg)
{
  struct multiboot_info *mb = (struct multiboot_info *)arg;
 
  HARTPORT_init();

  KEYB_init(NULL);
 
  __call_main__(mb);

  return (void *)0;

}

// --------------------------------------------------
// --------------------------------------------------
// The Test
// --------------------------------------------------
// --------------------------------------------------

pthread_t j1 = -1;
pthread_t j2 = -1;
pthread_t j3 = -1;
pthread_t j4 = -1;
fsf_server_id_t server1 = -1;
fsf_server_id_t server2 = -1;
fsf_server_id_t server3 = -1;
fsf_server_id_t server4 = -1;
fsf_contract_parameters_t contract1, contract2;


#define TASK_PERIOD 1000000

void *periodic_star(void *arg)
{
  struct timespec actual,end,next_time;
  int actpersecond,act,cycle;
  int mean,nmean;
  bool was_deadline_missed, was_budget_overran;

  act = 0;
  actpersecond = 0;
  mean = 0;
  nmean = 0;
  cycle = 0;
  for (;;) {
   
    kern_gettime(&actual);
    cycle++;

    if (act == 0) {
      TIMESPEC_ASSIGN(&end,&actual);
      end.tv_sec++;
    }

    if (TIMESPEC_A_LT_B(&actual,&end)) {
      act++;
    } else {
      actpersecond = act;
      act = 0;
      mean = (mean * nmean + actpersecond) / (nmean+1);
      nmean++;
    }

      printf_xy(0,exec_shadow,WHITE,"Thread %3d Act_per_Second = %8d Mean = %8d Cycle = %8d",
                                        exec_shadow,actpersecond,mean,cycle);
    kern_gettime(&next_time);
    ADDUSEC2TIMESPEC(TASK_PERIOD, &next_time);
    fsf_schedule_timed_job(&next_time, NULL, NULL, &was_deadline_missed, &was_budget_overran);

  }

  return NULL;

}

void *star(void *arg)
{
  struct timespec actual,end;
  int actpersecond,act;
  int mean,nmean,cycle;

  act = 0;
  actpersecond = 0;
  mean = 0;
  nmean = 0;
  cycle = 0;
  for (;;) {
   
    cycle++;
    kern_gettime(&actual);

    if (act == 0) {
      TIMESPEC_ASSIGN(&end,&actual);
      end.tv_sec++;
    }

    if (TIMESPEC_A_LT_B(&actual,&end)) {
      act++;
    } else {
      actpersecond = act;
      act = 0;
      mean = (mean * nmean + actpersecond) / (nmean+1);
      nmean++;
    }

      printf_xy(0,exec_shadow,WHITE,"Thread %3d Act_per_Second = %8d Mean = %8d Cycle = %8d",
                                    exec_shadow,actpersecond,mean,cycle);

  }

  return NULL;

}

void *edftask(void *arg)
{
  int i,j;
  while(1) {
  for (i=0;i<5; i++) {
    for (j=0; j<10; j++);
    //cputc('#');
    //cputs((char *)(arg));
  }

  task_endcycle();
  }

  return NULL;
}


void create()
{
  HARD_TASK_MODEL mhard;

  struct timespec period1 = {0,100000000};
  struct timespec period2 = {0,100000000};
  struct timespec budget1 = {0,30000000};
  struct timespec budget2 = {0,30000000};
 
  PID t1, t2;
  int err;
 
  FSF_start_service_task();

  kern_printf("(Start Create)");

  hard_task_default_model(mhard);
  hard_task_def_ctrl_jet(mhard);
  hard_task_def_mit(mhard,32000);
  hard_task_def_wcet(mhard,3000);
  hard_task_def_arg(mhard,(void *)"X");
  hard_task_def_group(mhard,1);
  hard_task_def_periodic(mhard);
   
  //t1 = task_create("X", edftask, &mhard, NULL);
  //if (t1 == NIL) {
  //  perror("Could not create task X ...");
  //  exit(1);
  //}

  hard_task_def_mit(mhard,32000);
  hard_task_def_wcet(mhard,3000);
  hard_task_def_arg(mhard,(void *)"Y");
  //t2 = task_create("Y", edftask, &mhard, NULL);
  //if (t2 == NIL) {
  //  perror("Could not create task Y ...");
  //  exit(1);
  //}

  //group_activate(1);

  err = pthread_create(&j1, NULL, star, (void *)"A");
  if (err) {
    perror("Could not create task A ...");
    exit(1);
  }

  err = pthread_create(&j2, NULL, periodic_star, (void *)"B");
  if (err) {
    perror("Could not create task B ...");
    exit(1);
  }

  err = pthread_create(&j3, NULL, star, (void *)"C");
  if (err) {
    perror("Could not create task C ...");
    exit(1);
  }

  err = pthread_create(&j4, NULL, periodic_star, (void *)"D");
  if (err) {
    perror("Could not create task D ...");
    exit(1);
  }
 
  fsf_initialize_contract(&contract1);
  fsf_set_contract_basic_parameters(&contract1,&budget1,&period1,FSF_DEFAULT_WORKLOAD);
  fsf_initialize_contract(&contract2);
  fsf_set_contract_basic_parameters(&contract2,&budget2,&period2,FSF_DEFAULT_WORKLOAD);

  kern_printf("(End Create)");

}

int main(int argc, char **argv)
{

  char ch = 0;
  int err;

  create();

  do {
    ch = keyb_getch(BLOCK);

    switch(ch) {
      case '1':
        err = fsf_bind_thread_to_server(server1,j1);
        kern_printf("(%d)",err);
        break;
      case '2':
        err = fsf_bind_thread_to_server(server2,j2);
        kern_printf("(%d)",err);
        break;
      case '3':
        err = fsf_bind_thread_to_server(server1,j3);
        kern_printf("(%d)",err);
        break;
      case '4':
        err = fsf_bind_thread_to_server(server2,j4);
        kern_printf("(%d)",err);
        break;
      case '5':
        err = fsf_unbind_thread_from_server(j1);
        kern_printf("(%d)",err);
        break;
      case '6':
        err = fsf_unbind_thread_from_server(j2);
        kern_printf("(%d)",err);
        break;
      case '7':
        err = fsf_unbind_thread_from_server(j3);
        kern_printf("(%d)",err);
        break;
      case '8':
        err = fsf_unbind_thread_from_server(j4);
        kern_printf("(%d)",err);
        break;
      case 'q':
        err = fsf_negotiate_contract(&contract1,&server1);
        cprintf("(%d)",err);
        break;
      case 'w':
        err = fsf_negotiate_contract(&contract2,&server2);
        kern_printf("(%d)",err);
        break;
      case 'e':
        err = fsf_negotiate_contract(&contract1,&server3);
        kern_printf("(%d)",err);
        break;
      case 'r':
        err = fsf_cancel_contract(server1);
        kern_printf("(%d)",err);
        break;
      case 't':
        err = fsf_cancel_contract(server2);
        kern_printf("(%d)",err);
        break;
      case 'y':
        err = fsf_cancel_contract(server3);
        kern_printf("(%d)",err);
        break;

    }

  } while(ch != ESC);

  exit(1);

  return 0;

}