/**********************************************************
 * exampleUnixTime -  this program illustrates how to obtain
*    a Unix timestamp, how to compute a RTT, and how to print 
*    a timestamp as a human readable string.
*
* Notes:
*
* Revisions
*
 **************************************************************/
#include "exampleTime2.h"


extern unsigned short csum(unsigned short *ptr,int nbytes);

void signal_handler();
double getTime(int);
double getTime1();



#define TEST_SLEEP 1
// 1: test sleep  for TEST_SLEEP seconds
// 0: loop forever running instructions
#define LOOP_TEST  0


int main(int argc, char *argv[])
{
  double time = 0.0;
  struct sigaction sig;
  unsigned short csumResult = 0;
  unsigned int numberIterations = 0;
  unsigned int dataBlockSize = 0;
  // 1:getTimeofDay; 
  // 2:clock_gettime with CLOCK_REALTIME
  // 3:clock_gettime with CLOCK_PROCESS_CPU_TIME
  int clockType = 1;  

  if (argc < 3)    /* Test for correct number of arguments */
  {
    fprintf(stderr,"Usage: %s : iterations  dataBlockSize CLOCK \n", argv[0]);
    exit(1);
  }
  else {
    numberIterations = atoi(argv[1]);
    dataBlockSize = atoi(argv[2]);
  }

  if (argc == 4)    
    clockType = atoi(argv[3]);


  fprintf(stderr,"exampleTime2: numberIterations:%d, dataBlockSize:%d, clockType:%d \n", numberIterations,dataBlockSize,clockType);


  /* setup signals */
  memset(&sig,0,sizeof(sig));
  sig.sa_handler = signal_handler;
  sigaction (SIGINT,  &sig, NULL);
  sigaction (SIGALRM, &sig, NULL);


  //Params
  int bufferSize = dataBlockSize;

  
  //make nbytes at least 2 and even
  if (bufferSize < 2)
    bufferSize = 2;
  
  unsigned short *bufferPtr = (unsigned short *)malloc((size_t) bufferSize);
  unsigned char *cptr = (unsigned char *)bufferPtr;
//  unsigned short *ptr = bufferPtr;

  //init the buffer
  srand((unsigned int) (unsigned int)getTime1());
  
 int nbytes = bufferSize;

  while(nbytes>1) 
  {
//    *cptr++= (unsigned char) nbytes;
    *cptr++= (unsigned char) rand();
    nbytes-=1;
//    *ptr++= (unsigned short) nbytes;
//    nbytes-=2;
  }
	
  unsigned int i=0;
  while (i < numberIterations) 
  {
    i++;
    //The alarm will pop is the inner function does not finish first`
    time = getTime(clockType);
    csumResult = csum(bufferPtr, bufferSize);
    time = getTime(clockType) - time;	
    printf("%9.9f 0X%xd  \n",time,csumResult);
  }     

  exit(0);
}


void signal_handler(int sig) {
  switch(sig) {
    case SIGINT:
      printf("SIGINT:   Exit! \n");
      exit(0);
      break;
    case SIGALRM:
      printf("SIGALRM:   Exit! \n");
      exit(0);
      break;
  }
}


double getTime(int clockType) {

  clockid_t clockLinuxType = CLOCK_REALTIME;
  struct timeval myTime;
  struct timespec curTime;
  int rc;

  switch(clockType) {
    case 1:
	rc= gettimeofday (&myTime, (struct timezone *) NULL);
        if (rc==0) 
	  return (((((double) myTime.tv_sec) * 1000000.0) 
             + (double) myTime.tv_usec) / 1000000.0); 
        else{
         printf("getTime: Error on gettimeofday:%d, errno:%d (clockType:%d) \n",
           rc,errno,clockType);
          return(0.0);
        }

    break;

    case 2:
      clockLinuxType = CLOCK_REALTIME;
      //Use clock_gettime
      rc =clock_gettime(clockLinuxType, &curTime);
      if (rc==0) { 
        //return (1000000000 * (double)curTime.tv_sec + (double)curTime.tv_nsec);
        return ((double)curTime.tv_sec + ((double)curTime.tv_nsec)/1000000000);
      }
      else{
        printf("getTime: Error on clock_gettime:%d, errno:%d (clockType:%d) \n",
          rc,errno,clockType);
        return(0.0);
      }
    break;

    case 3:
      clockLinuxType = CLOCK_PROCESS_CPUTIME_ID;
      //Use clock_gettime
      rc =clock_gettime(clockLinuxType, &curTime);
      if (rc==0) { 
        //return (1000000000 * (double)curTime.tv_sec + (double)curTime.tv_nsec);
        return ((double)curTime.tv_sec + ((double)curTime.tv_nsec)/1000000000);
      }
      else{
        printf("getTime: Error on clock_gettime:%d, errno:%d (clockType:%d) \n",
          rc,errno,clockType);
        return(0.0);
      }
    break;

    default:
      printf("Error on clockType: %d \n",clockType);
      return(0.0);
  }

}

double getTime1()
{
	struct timeval curTime;
	(void) gettimeofday (&curTime, (struct timezone *) NULL);
	return (((((double) curTime.tv_sec) * 1000000.0) 
             + (double) curTime.tv_usec) / 1000000.0); 
}

