time_meas.h 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */
ghaddab's avatar
ghaddab committed
21

22 23 24
#ifndef __TIME_MEAS_DEFS__H__
#define __TIME_MEAS_DEFS__H__

25
#include <unistd.h>
gauthier's avatar
gauthier committed
26
#include <math.h>
27 28 29 30 31 32 33
#include <stdint.h>
#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <pthread.h>
#include <linux/kernel.h>
#include <linux/types.h>
34
// global var to enable openair performance profiler
35
extern int opp_enabled;
36
double cpu_freq_GHz;
37
#if defined(__x86_64__) || defined(__i386__)
38

39 40 41 42
typedef struct {

  long long in;
  long long diff;
43
  long long diff_now;
44 45
  long long p_time; /*!< \brief absolute process duration */
  long long diff_square; /*!< \brief process duration square */
46 47
  long long max;
  int trials;
48
  int meas_flag;
49
} time_stats_t;
50 51 52 53 54 55 56 57 58 59
#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;
60

61
#endif
62 63 64
static inline void start_meas(time_stats_t *ts) __attribute__((always_inline));
static inline void stop_meas(time_stats_t *ts) __attribute__((always_inline));

65

66
void print_meas_now(time_stats_t *ts, const char* name, FILE* file_name);
67
void print_meas(time_stats_t *ts, const char* name, time_stats_t * total_exec_time, time_stats_t * sf_exec_time);
68
double get_time_meas_us(time_stats_t *ts);
69
double get_cpu_freq_GHz(void);
70

71 72
#if defined(__i386__)
static inline unsigned long long rdtsc_oai(void) __attribute__((always_inline));
73 74 75 76 77
static inline unsigned long long rdtsc_oai(void)
{
  unsigned long long int x;
  __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
  return x;
78 79
}
#elif defined(__x86_64__)
80
static inline unsigned long long rdtsc_oai(void) __attribute__((always_inline));
81 82
static inline unsigned long long rdtsc_oai(void)
{
83 84 85 86
  unsigned long long a, d;
  __asm__ volatile ("rdtsc" : "=a" (a), "=d" (d));
  return (d<<32) | a;
}
knopp's avatar
knopp committed
87 88

#elif defined(__arm__)
89 90
static inline uint32_t rdtsc_oai(void) __attribute__((always_inline));
static inline uint32_t rdtsc_oai(void)
knopp's avatar
knopp committed
91
{
92 93
  uint32_t r = 0;
  asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(r) );
94
  return r;
knopp's avatar
knopp committed
95
}
96 97
#endif

98 99
static inline void start_meas(time_stats_t *ts)
{
100

101
  if (opp_enabled) {
102
    if (ts->meas_flag==0) {
103 104
      ts->trials++;
      ts->in = rdtsc_oai();
105 106 107 108 109
      ts->meas_flag=1;
    }
    else {
      ts->in = rdtsc_oai();
    }
110
  }
111 112
}

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

116 117
  if (opp_enabled) {
    long long out = rdtsc_oai();
118 119 120 121 122 123 124 125 126 127 128
    
    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;
129 130

    ts->meas_flag=0;    
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
  ts->meas_flag=0;
143
  
144
}
145

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

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