Subversion Repositories shark

Rev

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

//////////////////////////////////////////////////////////////////
// Pong
// Written for CS 324, UIUC
// Spencer Hoke
// James Kim
// Tao Luo
//
// Last Modified: April 29, 2004

#include "pong.h"

///////////////////////////////////////
TASK jetdummy_task(void *arg)
{
  TIME   now_dummy, last_dummy, diff_dummy, slice;
  struct timespec now, last, diff;
  int x = 0;
  int height;

  NULL_TIMESPEC(&last);
  last_dummy = 0;
  for (;;) {
    task_nopreempt();
    jet_getstat(DUMMY_PID, NULL, NULL, NULL, &now_dummy);
    sys_gettime(&now);
    task_preempt();

    SUBTIMESPEC(&now, &last, &diff);
    slice = diff.tv_sec * 1000000 + diff.tv_nsec/1000;
    diff_dummy = now_dummy - last_dummy;

    height = (int)(JET_DUMMY_HEIGHT*((float)diff_dummy)/((float)slice));

    TIMESPEC_ASSIGN(&last, &now);
    last_dummy = now_dummy;

        mutex_lock(&draw_mutex);
    grx_line(JET_DUMMY_X+x,JET_DUMMY_Y,
             JET_DUMMY_X+x,JET_DUMMY_Y+height          ,black);
    grx_line(JET_DUMMY_X+x,JET_DUMMY_Y+height,
             JET_DUMMY_X+x,JET_DUMMY_Y+JET_DUMMY_HEIGHT,white);
    grx_line(JET_DUMMY_X+(x+1)%JET_DUMMY_WIDTH,JET_DUMMY_Y,
             JET_DUMMY_X+(x+1)%JET_DUMMY_WIDTH,JET_DUMMY_Y+JET_DUMMY_HEIGHT,blue);
        mutex_unlock(&draw_mutex);

    x = (x+1)%JET_DUMMY_WIDTH;

    task_endcycle();
  }
}

///////////////////////////////////////
TASK jetctrl_task(void *arg)
{
  char st[50];
  TIME sum, max;
  int n;

  PID i;
  int printed = 0;

  for (;;) {
    for (i=2, printed=0; i<MAX_PROC && printed<JET_NTASK; i++) {
      if (jet_getstat(i, &sum, &max, &n, NULL) != -1) {
        if (!n) n=1;
        sprintf(st, " %4d %4d  %s", (int)sum/n, (int)max, proc_table[i].name);
        mutex_lock(&draw_mutex);
        grx_text(st, JET_DUMMY_X-45, JET_Y_NAME+16+printed*8, gray, black);
        mutex_unlock(&draw_mutex);
        printed++;
      }
    }
    while (printed < JET_NTASK) {
      mutex_lock(&draw_mutex);
      grx_text("                    ", JET_DUMMY_X-45, JET_Y_NAME+16+printed*8, gray, black);
      mutex_unlock(&draw_mutex);
      printed++;
    }
    task_endcycle();
  }
}

///////////////////////////////////////
TASK jetslide_task(void *arg)
{
  TIME sum, curr, max;

  TIME total[JET_NTASK];
  int slides[JET_NTASK];

  PID i;
  int printed = 0;

  for (;;) {
    // Fill the total array in a nonpreemptive section
    task_nopreempt();
    for (i=2, printed=0; i<MAX_PROC && printed<JET_NTASK; i++) {
      if (jet_getstat(i, &sum, NULL, NULL, &curr) != -1) {
        total[printed] = sum+curr;
        printed++;
      }
    }
    task_preempt();

    while (printed < JET_NTASK)
      total[printed++] = 0;

    // Compute the Max elapsed time
    max = 0;
    for (i=0; i<JET_NTASK; i++)
      if (total[i] > max) max = total[i];
    if (!max) max = 1;

    // Compute the slides width
    for (i=0; i<JET_NTASK; i++)
      slides[i] = (int)( (((float)total[i])/max) * JET_SLIDE_WIDTH);

    // print the data
        mutex_lock(&draw_mutex);
    for (i=0; i<JET_NTASK; i++) {
      grx_box(JET_SLIDE_X,                 JET_Y_NAME+16+i*8,
              JET_SLIDE_X+slides[i],       JET_Y_NAME+23+i*8, gray);
      grx_box(JET_SLIDE_X+slides[i],       JET_Y_NAME+16+i*8,
              JET_SLIDE_X+JET_SLIDE_WIDTH, JET_Y_NAME+23+i*8, black);
    }

    while (i<JET_NTASK) {
      grx_box(JET_SLIDE_X,                 JET_Y_NAME+16+i*8,
              JET_SLIDE_X+JET_SLIDE_WIDTH, JET_Y_NAME+20+i*8, black);
      i++;
    }
        mutex_unlock(&draw_mutex);
    task_endcycle();
  }
}

///////////////////////////////////////
void scenario_jetcontrol(void)
{
  grx_line(JET_DUMMY_X-40,JET_DUMMY_Y-15,JET_DUMMY_X+200,JET_DUMMY_Y-15, redorange);
  grx_text("Mean  Max  Name     Slide", JET_DUMMY_X-40, JET_Y_NAME, gray, black);
  grx_line(JET_DUMMY_X-40,JET_Y_NAME+10,JET_DUMMY_X+200,JET_Y_NAME+10,lime);
  grx_rect(JET_DUMMY_X-1,               JET_DUMMY_Y-1,
           JET_DUMMY_X+JET_DUMMY_WIDTH, JET_DUMMY_Y+JET_DUMMY_HEIGHT+1, lime);
  grx_text("100%", JET_DUMMY_X-40, JET_DUMMY_Y, lime, black);
  grx_text("  0%", JET_DUMMY_X-40, JET_DUMMY_Y+JET_DUMMY_HEIGHT-8, lime, black);
}

///////////////////////////////////////
void init_jetcontrol(void)
{
    SOFT_TASK_MODEL m3, m4, m5;

    PID p3, p4, p5;

    soft_task_default_model(m3);
    soft_task_def_level(m3,1);
    soft_task_def_period(m3, PERIOD_JETCTRL);
    soft_task_def_met(m3, WCET_JETCTRL);
    soft_task_def_ctrl_jet(m3);
    soft_task_def_group(m3, JET_GROUP);
    p3 = task_create("jctrl", jetctrl_task, &m3, NULL);
    if (p3 == -1) {
        grx_close();
        sys_shutdown_message("Could not create task <jetcontrol> errno=%d", errno);
        sys_end();
    }

    soft_task_default_model(m4);
    soft_task_def_level(m4,1);
    soft_task_def_period(m4, PERIOD_JETDUMMY);
    soft_task_def_met(m4, WCET_JETDUMMY);
    soft_task_def_group(m4, JET_GROUP);
    soft_task_def_usemath(m4);
    soft_task_def_ctrl_jet(m4);
    p4 = task_create("jdmy", jetdummy_task, &m4, NULL);
    if (p4 == -1) {
        grx_close();
        sys_shutdown_message("Could not create task <jetdummy> errno=%d", errno);
        sys_end();
    }

    soft_task_default_model(m5);
    soft_task_def_level(m5,1);
    soft_task_def_period(m5, PERIOD_JETSLIDE);
    soft_task_def_met(m5, WCET_JETSLIDE);
    soft_task_def_group(m5, JET_GROUP);
    soft_task_def_usemath(m5);
    soft_task_def_ctrl_jet(m5);
    p5 = task_create("jsli", jetslide_task, &m5, NULL);
    if (p5 == -1) {
        grx_close();
        sys_shutdown_message("Could not create task <jetslide> errno=%d", errno);
        sys_end();
    }
}