Subversion Repositories shark

Rev

Rev 1302 | 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>
 *
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://shark.sssup.it
 */


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#define READ_BUFFER 2000
#define DELTA_BUFFER 100

struct event_type {
  int type;
  unsigned long long tsc;
  int par[10];
};

struct ctx_time_type {
  int ctx;
  unsigned long long start_time;
  unsigned long long total_time;
};

struct ctx_time_type ctx_time[256];
struct ctx_time_type int_time;
unsigned long long last_time;
int total_ctx = 0;
int last_ctx = -1;

void analyze_event(struct event_type *e) {

  int i;

  // Start interrupt
  if (e->type == 0x2b || e->type == 0x03) {
   
    if (int_time.start_time == 0) int_time.start_time = e->tsc;
     
    if (last_ctx != -1) {

      if (e->tsc < last_time) {
        printf("Error events sequence\n");
        exit(2);
      }

      ctx_time[last_ctx].total_time += (e->tsc - last_time);
      last_time = e->tsc;

    } else {

      last_time = e->tsc;

    }

  }

  //End Interrupt
  if (e->type == 0x3b || e->type == 0x13) {

    if (int_time.start_time == 0) int_time.start_time = e->tsc;

    if (last_time != 0) {

      if (e->tsc < last_time) {
        printf("Error events sequence\n");
        exit(2);
      }

      int_time.total_time += (e->tsc - last_time);
      last_time = e->tsc;

    } else {

      last_time = e->tsc;

    }

    i = 0;
    while (ctx_time[i].ctx != e->par[0] && i < total_ctx) {
      i++;
    }

    if (ctx_time[i].start_time == 0) {
      ctx_time[i].start_time = e->tsc;
      ctx_time[i].total_time = 0;
      ctx_time[i].ctx = e->par[0];
      last_ctx = i;
      total_ctx++;
    } else {
      last_ctx = i;
    }

  }

  if (e->type == 0x25) {
    printf("Inheritance S %d E %d A %d\n",e->par[0],e->par[1],e->par[2]);
  }

  //Context Switch
  if (e->type == 0x15) {

    i = 0;
    while (ctx_time[i].ctx != e->par[0] && i < total_ctx) {
      i++;
    }

    if (ctx_time[i].start_time == 0) {
      ctx_time[i].ctx = e->par[0];
      ctx_time[i].start_time = e->tsc;
      ctx_time[i].total_time = 0;

      if (e->tsc < last_time) {
        printf("Error events sequence\n");
        exit(2);
      }

      if (last_ctx != -1) ctx_time[last_ctx].total_time += (e->tsc - last_time);  

      last_time = e->tsc;
      last_ctx = i;
      total_ctx++;

    } else {

      if (e->tsc < last_time) {
        printf("Error events sequence\n");
        exit(2);
      }

      if (last_ctx != -1) ctx_time[last_ctx].total_time += (e->tsc - last_time);
      last_time = e->tsc;
      last_ctx = i;

    }

  }

}

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

  char buffer[READ_BUFFER+DELTA_BUFFER];
  void *p, *last;
  int n,i,delta,size;
  struct event_type evt;

  unsigned long long ev = 0,start,stop;
  unsigned int total_simulation = 0;
 
  FILE *input_file;
 
  if (argc < 2) {
    printf("%s: Enter the input file name [%s filename]\n",argv[0],argv[0]);
    exit(1);
  }

  memset(ctx_time,0,sizeof(ctx_time));
  memset(&int_time,0,sizeof(int_time));
  total_ctx = 0;

  input_file = fopen(argv[1],"rb");

  last = buffer + READ_BUFFER;

  while(!feof(input_file)) {
 
    //move remaining byte
    delta = (unsigned int)(buffer) + READ_BUFFER - (unsigned int)(last);
    if (delta > 0) memcpy(buffer,last,delta);    

    n = fread(buffer+delta,1,READ_BUFFER+10,input_file);
 
    fseek(input_file,-(delta+10),SEEK_CUR);

    p = buffer;

    while ((unsigned int)(p) + *(unsigned char *)(p+9) <= (unsigned int)(buffer + READ_BUFFER) &&
           (unsigned int)(p) + *(unsigned char *)(p+9) <= (unsigned int)(buffer + n + delta)) {

      evt.type = (int)(*(unsigned char *)(p));
      evt.tsc = (unsigned long long)(*(unsigned int *)(p+1));
      evt.tsc <<= 32;
      evt.tsc += (unsigned long long)(*(unsigned int *)(p+5));

      size = *(unsigned char *)(p+9);

      if (evt.type == 0x20) {
         printf("Start Simulation\n");
         start = evt.tsc;
      }

      if (evt.type == 0x30) {
         printf("Stop Simulation\n");
         stop = evt.tsc;
      }

      size -= 10;
      i = 0;
      while (size > 0) {
        evt.par[i] = (int)(*(unsigned int *)(p+10+i*4));
        i++;
        size -= 4;
      }

      ev++;
     
      analyze_event(&evt);
 
      p += *(unsigned char *)(p+9);

      if ((unsigned int)p + 10 > (unsigned int)(buffer + n + delta)) break;

      last = p;
 
    }

    if ((unsigned int)p + 10 > (unsigned int)(buffer + n + delta)) break;

  }

  fclose(input_file);

  printf("Total Time %d\n",(unsigned int)((stop-start) * 1000 / 501162));

  printf("Total Ctx = %d\n",total_ctx);

  printf("Total Interrupt = %d\n",(unsigned int)(int_time.total_time * 1000 / 501162));

  total_simulation = 0;
  for (i=0;i<total_ctx;i++) {
    printf("Total Time Ctx %d => %d\n",ctx_time[i].ctx,(unsigned int)(ctx_time[i].total_time * 1000 / 501162));
    total_simulation += (unsigned int)(ctx_time[i].total_time * 1000 / 501162);
  }

  printf ("Total Simulation %d\n",total_simulation);

  return 0;

}