/*****************************************************************************
File: stat.h
Author: Salas López, Jorge
Date:   03/01/2005
Description: Statistics for Middle-R.
Invocation:  Nothing
*****************************************************************************/

/*****************************************************************************
   Table of Contents:
   Reusable Functions:
    
   Intern Functions:
    ---
   Includes:
    
   Source code in this file:
    All
*********************************************************************************/

/********************************************************************************
Import Libraries .
*********************************************************************************/
#include <sys/time.h>

#define NUMSTEPS 7
#define NUMCLIENTS 33
#define INITWARMUP 0
#define FINISHWARMUP 0
#define ALLTRANSACTIONS 10000 + INITWARMUP + FINISHWARMUP

/*#define ALLTRANSACTIONS NUMTRANSACTIONS + INITWARMUP + FINISHWARMUP
int NUMCLIENTS = 0;
int INITWARMUP = 0;
int FINISHWARMUP = 0;
int NUMTRANSACTIONS = 0;*/

/* STEPS */
/*
 * 0 Arrivals to middler (Thread Client)
 * 1 Delivery (Total Order) 
 * 2 Submitted for execution
 * 3 Finished execution
 * 4 Removed from middler
*/
 
/********
* TYPES *
*********/
struct DATASTAT{
  struct timeval startTime;
  struct timeval finishTime;
  int inUse;
};

struct STAT{
  double mean;
  double std;
  double var;
  double throughput;
  double totaltime;
  int length;  
};

#define SETSTARTTIMELOCAL(__step, __client, __index, __tv_sec, __tv_usec, __local)                                   \
  if ((__step < NUMSTEPS) && (__client < NUMCLIENTS) && (__index < ALLTRANSACTIONS)) {       \
    (steps[__step][__client][__index]).startTime.tv_sec = __tv_sec;   \
    (steps[__step][__client][__index]).startTime.tv_usec = __tv_usec;   \
    (steps[__step][__client][__index]).inUse = 1;   \
    (local[__step][__client][__index]) = __local;   \
  }

#define SETFINISHTIMELOCAL(__step, __client, __index, __tv_sec, __tv_usec, __local)                                   \
  if ((__step < NUMSTEPS) && (__client < NUMCLIENTS) && (__index < ALLTRANSACTIONS)) {       \
    (steps[__step][__client][__index]).finishTime.tv_sec = __tv_sec;   \
    (steps[__step][__client][__index]).finishTime.tv_usec = __tv_usec;   \
    (steps[__step][__client][__index]).inUse = 1;   \
    (local[__step][__client][__index]) = __local;   \
  }
  
#define SETSTARTTIME(__step, __client, __index, __tv_sec, __tv_usec)                                   \
  if ((__step < NUMSTEPS) && (__client < NUMCLIENTS) && (__index < ALLTRANSACTIONS)) {       \
    (steps[__step][__client][__index]).startTime.tv_sec = __tv_sec;   \
    (steps[__step][__client][__index]).startTime.tv_usec = __tv_usec;   \
    (steps[__step][__client][__index]).inUse = 1;   \
  }

#define SETFINISHTIME(__step, __client, __index, __tv_sec, __tv_usec)                                   \
  if ((__step < NUMSTEPS) && (__client < NUMCLIENTS) && (__index < ALLTRANSACTIONS)) {       \
    (steps[__step][__client][__index]).finishTime.tv_sec = __tv_sec;   \
    (steps[__step][__client][__index]).finishTime.tv_usec = __tv_usec;   \
    (steps[__step][__client][__index]).inUse = 1;   \
  }
  
struct DATASTAT steps[NUMSTEPS][NUMCLIENTS][ALLTRANSACTIONS];
int local[NUMSTEPS][NUMCLIENTS][ALLTRANSACTIONS];
struct STAT stats[NUMSTEPS];

void toFile();
//void setStartTimeArray(int step, int client, int index, struct timeval times);
//void setFinishTimeArray(int step, int client, int index, struct timeval times);
//void setNumClients(int);
//void setWarmup(int, int);
//void setNumTransactions(int);
void initStatData(char *, int, int);
void setFileProperties(char *);
