time_meas.h 4.2 KB
Newer Older
ghaddab's avatar
ghaddab committed
1
/*******************************************************************************
2
    OpenAirInterface
ghaddab's avatar
ghaddab committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16
    Copyright(c) 1999 - 2014 Eurecom

    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.


    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
17 18
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
ghaddab's avatar
ghaddab committed
19 20 21 22 23
   see <http://www.gnu.org/licenses/>.

  Contact Information
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
24
  OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr
25

ghaddab's avatar
ghaddab committed
26
  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
ghaddab's avatar
ghaddab committed
27 28

 *******************************************************************************/
29 30 31
#ifndef __TIME_MEAS_DEFS__H__
#define __TIME_MEAS_DEFS__H__

32
#include <unistd.h>
Lionel Gauthier's avatar
grr  
Lionel Gauthier committed
33
#include <math.h>
34 35 36 37 38 39 40
#include <stdint.h>
#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <pthread.h>
#include <linux/kernel.h>
#include <linux/types.h>
41
// global var to enable openair performance profiler
42
extern int opp_enabled;
43
double cpu_freq_GHz;
44
#if defined(__x86_64__) || defined(__i386__)
45

46 47 48 49
typedef struct {

  long long in;
  long long diff;
50
  long long diff_now;
51 52
  long long p_time; /*!< \brief absolute process duration */
  long long diff_square; /*!< \brief process duration square */
53 54 55
  long long max;
  int trials;
} time_stats_t;
56 57 58 59 60 61 62 63 64 65
#elif defined(__arm__)
typedef struct {
  uint32_t in;
  uint32_t diff_now;
  uint32_t diff;
  uint32_t p_time; /*!< \brief absolute process duration */
  uint32_t diff_square; /*!< \brief process duration square */
  uint32_t max;
  int trials;
} time_stats_t;
66

67
#endif
68 69 70
static inline void start_meas(time_stats_t *ts) __attribute__((always_inline));
static inline void stop_meas(time_stats_t *ts) __attribute__((always_inline));

71 72

void print_meas_now(time_stats_t *ts, const char* name, int subframe, FILE* file_name);
73
void print_meas(time_stats_t *ts, const char* name, time_stats_t * total_exec_time, time_stats_t * sf_exec_time);
74
double get_time_meas_us(time_stats_t *ts);
75
double get_cpu_freq_GHz(void);
76

77 78
#if defined(__i386__)
static inline unsigned long long rdtsc_oai(void) __attribute__((always_inline));
79 80 81 82 83
static inline unsigned long long rdtsc_oai(void)
{
  unsigned long long int x;
  __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
  return x;
84 85
}
#elif defined(__x86_64__)
86
static inline unsigned long long rdtsc_oai(void) __attribute__((always_inline));
87 88
static inline unsigned long long rdtsc_oai(void)
{
89 90 91 92
  unsigned long long a, d;
  __asm__ volatile ("rdtsc" : "=a" (a), "=d" (d));
  return (d<<32) | a;
}
Raymond Knopp's avatar
Raymond Knopp committed
93 94

#elif defined(__arm__)
95 96
static inline uint32_t rdtsc_oai(void) __attribute__((always_inline));
static inline uint32_t rdtsc_oai(void)
Raymond Knopp's avatar
Raymond Knopp committed
97
{
98 99
  uint32_t r = 0;
  asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(r) );
100
  return r;
Raymond Knopp's avatar
Raymond Knopp committed
101
}
102 103
#endif

104 105
static inline void start_meas(time_stats_t *ts)
{
106

107
  if (opp_enabled) {
108

109 110
      ts->trials++;
      ts->in = rdtsc_oai();
111
  }
112 113
}

114 115
static inline void stop_meas(time_stats_t *ts)
{
116

117 118
  if (opp_enabled) {
    long long out = rdtsc_oai();
119 120 121 122 123 124 125 126 127 128 129 130
    
    ts->diff_now = (out-ts->in);
    
    ts->diff_now = (out-ts->in);
    ts->diff += (out-ts->in);
    /// process duration is the difference between two clock points
    ts->p_time = (out-ts->in);
    ts->diff_square += (out-ts->in)*(out-ts->in);
    
    if ((out-ts->in) > ts->max)
      ts->max = out-ts->in;
    
131
  }
132 133
}

134 135 136 137 138 139 140 141
static inline void reset_meas(time_stats_t *ts) {

  ts->trials=0;
  ts->diff=0;
  ts->diff_now=0;
  ts->p_time=0;
  ts->diff_square=0;
  ts->max=0;
142
  
143
}
144

145 146 147 148
static inline void copy_meas(time_stats_t *dst_ts,time_stats_t *src_ts)
{

  if (opp_enabled) {
149 150 151 152 153
    dst_ts->trials=src_ts->trials;
    dst_ts->diff=src_ts->diff;
    dst_ts->max=src_ts->max;
  }
}
154
#endif