/*
 *  Copyright 2009 OpenSourceStewardshipFoundation.org
 *  Licensed under GNU General Public License version 2
 *
 * Author: seanhalle@yahoo.com
 * 
 */

#ifndef _PROBES_H
#define	_PROBES_H
#define __USE_GNU

#include "VMS_primitive_data_types.h"

#include <sys/time.h>


   //when STATS__TURN_ON_PROBES is defined allows using probes to measure
   // time intervals.  The probes are macros that only compile to something
   // when STATS__TURN_ON_PROBES is defined.  The probes are saved in the
   // master env -- but only when this is defined.
   //The TSC probes use RDTSC instr, can be unreliable, Dbl uses gettimeofday
#define STATS__TURN_ON_PROBES
//#define STATS__USE_TSC_PROBES
#define STATS__USE_DBL_PROBES

//typedef struct _IntervalProbe IntervalProbe; //in VMS.h

struct _IntervalProbe
 {
   char           *nameStr;
   int32           probeID;

   int32           schedChoiceWasRecorded;
   int32           coreNum;
   int32           procrID;
   float64         procrCreateSecs;

   #ifdef STATS__USE_TSC_PROBES
   TSCount    startStamp;
   TSCount    endStamp;
   #else
   struct timeval  startStamp;
   struct timeval  endStamp;
   #endif
   float64         startSecs;
   float64         endSecs;
   float64         interval;
   DblHist        *hist;//if NULL, then is single interval probe
 };


//============================= Statistics ==================================

   //Frequency of TS counts
   //TODO: change freq for each machine
#define TSCOUNT_FREQ 3180000000

inline TSCount getTSCount();


//======================== Probes =============================
//
// Use macros to allow turning probes off with a #define switch
#ifdef STATS__ENABLE_PROBES
int32
VMS_impl__record_time_point_into_new_probe( char *nameStr,VirtProcr *animPr);
#define VMS__record_time_point_into_new_probe( nameStr, animPr ) \
        VMS_impl__record_time_point_in_new_probe( nameStr, animPr )

int32
VMS_ext_impl__record_time_point_into_new_probe( char *nameStr );
#define VMS_ext__record_time_point_into_new_probe( nameStr ) \
        VMS_ext_impl__record_time_point_into_new_probe( nameStr )


int32
VMS_impl__create_single_interval_probe( char *nameStr, VirtProcr *animPr );
#define VMS__create_single_interval_probe( nameStr, animPr ) \
        VMS_impl__create_single_interval_probe( nameStr, animPr )


int32
VMS_impl__create_histogram_probe( int32   numBins, float64    startValue,
               float64 binWidth, char    *nameStr, VirtProcr *animPr );
#define VMS__create_histogram_probe(      numBins, startValue,              \
                                          binWidth, nameStr, animPr )       \
        VMS_impl__create_histogram_probe( numBins, startValue,              \
                                          binWidth, nameStr, animPr )
void
VMS_impl__free_probe( IntervalProbe *probe );
#define VMS__free_probe( probe ) \
        VMS_impl__free_probe( probe )

void
VMS_impl__index_probe_by_its_name( int32 probeID, VirtProcr *animPr );
#define VMS__index_probe_by_its_name( probeID, animPr ) \
        VMS_impl__index_probe_by_its_name( probeID, animPr )

IntervalProbe *
VMS_impl__get_probe_by_name( char *probeName, VirtProcr *animPr );
#define VMS__get_probe_by_name( probeID, animPr ) \
        VMS_impl__get_probe_by_name( probeName, animPr )

void
VMS_impl__record_sched_choice_into_probe( int32 probeID, VirtProcr *animPr );
#define VMS__record_sched_choice_into_probe( probeID, animPr ) \
        VMS_impl__record_sched_choice_into_probe( probeID, animPr )

void
VMS_impl__record_interval_start_in_probe( int32 probeID );
#define VMS__record_interval_start_in_probe( probeID ) \
        VMS_impl__record_interval_start_in_probe( probeID )

void
VMS_impl__record_interval_end_in_probe( int32 probeID );
#define VMS__record_interval_end_in_probe( probeID ) \
        VMS_impl__record_interval_end_in_probe( probeID )

void
VMS_impl__print_stats_of_probe( int32 probeID );
#define VMS__print_stats_of_probe( probeID ) \
        VMS_impl__print_stats_of_probe( probeID )

void
VMS_impl__print_stats_of_all_probes();
#define VMS__print_stats_of_all_probes \
        VMS_impl__print_stats_of_all_probes


#else
int32
VMS_impl__record_time_point_into_new_probe( char *nameStr,VirtProcr *animPr);
#define VMS__record_time_point_into_new_probe( nameStr, animPr ) \
       0 /* do nothing */

int32
VMS_ext_impl__record_time_point_into_new_probe( char *nameStr );
#define VMS_ext__record_time_point_into_new_probe( nameStr ) \
       0 /* do nothing */


int32
VMS_impl__create_single_interval_probe( char *nameStr, VirtProcr *animPr );
#define VMS__create_single_interval_probe( nameStr, animPr ) \
       0 /* do nothing */


int32
VMS_impl__create_histogram_probe( int32   numBins, float64    startValue,
               float64 binWidth, char    *nameStr, VirtProcr *animPr );
#define VMS__create_histogram_probe(      numBins, startValue,              \
                                          binWidth, nameStr, animPr )       \
       0 /* do nothing */

void
VMS_impl__index_probe_by_its_name( int32 probeID, VirtProcr *animPr );
#define VMS__index_probe_by_its_name( probeID, animPr ) \
        /* do nothing */

IntervalProbe *
VMS_impl__get_probe_by_name( char *probeName, VirtProcr *animPr );
#define VMS__get_probe_by_name( probeID, animPr ) \
       NULL /* do nothing */

void
VMS_impl__record_sched_choice_into_probe( int32 probeID, VirtProcr *animPr );
#define VMS__record_sched_choice_into_probe( probeID, animPr ) \
        /* do nothing */

void
VMS_impl__record_interval_start_in_probe( int32 probeID );
#define VMS__record_interval_start_in_probe( probeID ) \
        /* do nothing */

void
VMS_impl__record_interval_end_in_probe( int32 probeID );
#define VMS__record_interval_end_in_probe( probeID ) \
        /* do nothing */

inline void doNothing();
void
VMS_impl__print_stats_of_probe( int32 probeID );
#define VMS__print_stats_of_probe( probeID ) \
        doNothing/* do nothing */

void
VMS_impl__print_stats_of_all_probes();
#define VMS__print_stats_of_all_probes \
        doNothing/* do nothing */

#endif   /* defined STATS__ENABLE_PROBES */

#endif	/* _PROBES_H */

