time_meas.h 2.18 KB
Newer Older
1 2 3
#ifdef OMP
#include <omp.h>
#endif
4 5 6
#include <unistd.h>
// global var to enable openair performance profiler 
extern int opp_enabled;
7 8 9 10 11 12 13 14 15 16 17 18

typedef struct {

  long long in;
  long long diff;
  long long max;
  int trials;
} time_stats_t;

static inline void start_meas(time_stats_t *ts) __attribute__((always_inline));
static inline void stop_meas(time_stats_t *ts) __attribute__((always_inline));

19
void print_meas(time_stats_t *ts, const char* name, time_stats_t * total_exec_time, time_stats_t * sf_exec_time);
20
double get_time_meas_us(time_stats_t *ts);
21

22 23 24 25 26 27 28 29
#if defined(__i386__)
static inline unsigned long long rdtsc_oai(void) __attribute__((always_inline));
static inline unsigned long long rdtsc_oai(void) {
    unsigned long long int x;
    __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
    return x;
}
#elif defined(__x86_64__)
30 31
static inline unsigned long long rdtsc_oai(void) __attribute__((always_inline));
static inline unsigned long long rdtsc_oai(void) { 
32 33 34 35 36 37 38 39
  unsigned long long a, d;
  __asm__ volatile ("rdtsc" : "=a" (a), "=d" (d));
  return (d<<32) | a;
}
#endif

static inline void start_meas(time_stats_t *ts) {

40
  if (opp_enabled) {
41

42 43 44 45 46
#ifdef OMP
    int tid;
    
    tid = omp_get_thread_num();
    if (tid==0)
47
#endif
48 49 50 51 52
      {
	ts->trials++;
	ts->in = rdtsc_oai();
      }
  }
53 54 55 56
}

static inline void stop_meas(time_stats_t *ts) {

57 58 59
  if (opp_enabled) {
    long long out = rdtsc_oai();
    
60
#ifdef OMP
61 62 63
    int tid;
    tid = omp_get_thread_num();
    if (tid==0)
64
#endif
65 66 67 68 69 70 71
      {
	ts->diff += (out-ts->in);
	if ((out-ts->in) > ts->max)
	  ts->max = out-ts->in;
	
      }
  }
72 73 74 75
}

static inline void reset_meas(time_stats_t *ts) {

76 77 78 79 80
  if (opp_enabled){
    ts->trials=0;
    ts->diff=0;
    ts->max=0;
  }
81
}
82 83 84 85 86 87 88 89
static inline void copy_meas(time_stats_t *dst_ts,time_stats_t *src_ts) {
  
  if (opp_enabled){
    dst_ts->trials=src_ts->trials;
    dst_ts->diff=src_ts->diff;
    dst_ts->max=src_ts->max;
  }
}
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

/*static inline double get_mean_meas_us(time_stats_t *ts, double cpu_freq_GHz) {

  return (double) ts->diff/ts->trials/cpu_freq_GHz/1000.0;

  }*/

static inline double get_cpu_freq_GHz(void) {

  time_stats_t ts;
  reset_meas(&ts);
  start_meas(&ts);
  sleep(1);
  stop_meas(&ts);
  return (double)ts.diff/1000000000;
}