time_meas.c 3.8 KB
Newer Older
1
2
3
4
5
/*
 * 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
Cedric Roux's avatar
Cedric Roux committed
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 * 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
 */

22
23
#include <stdio.h>
#include "time_meas.h"
knopp's avatar
   
knopp committed
24
25
#include <math.h>
#include <unistd.h>
26

27
// global var for openair performance profiler
28
int opp_enabled = 0;
29

30

31
32
33
34
double get_cpu_freq_GHz(void) {

  time_stats_t ts = {0};
  reset_meas(&ts);
35
36
  ts.trials++;
  ts.in = rdtsc_oai();
37
  sleep(1);
38
  ts.diff = (rdtsc_oai()-ts.in);
39
  cpu_freq_GHz = (double)ts.diff/1000000000;
Elena Lukashova's avatar
Elena Lukashova committed
40
41
  //printf("CPU Freq is %f \n", cpu_freq_GHz);
  return cpu_freq_GHz;
42
43
}

44
void print_meas_now(time_stats_t *ts, const char* name, FILE* file_name){
45
46
47
48
49
50
51
52
53
54
55

  if (opp_enabled) {

    //static double cpu_freq_GHz = 3.2;

    //if (cpu_freq_GHz == 0.0)
      //cpu_freq_GHz = get_cpu_freq_GHz(); // super slow

    if (ts->trials>0) {

      //fprintf(file_name,"Name %25s: Processing %15.3f ms for SF %d, diff_now %15.3f \n", name,(ts->diff_now/(cpu_freq_GHz*1000000.0)),subframe,ts->diff_now);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
56
      fprintf(file_name,"%15.3f us, diff_now %15.3f \n",(ts->diff_now/(cpu_freq_GHz*1000.0)),(double)ts->diff_now);
Elena Lukashova's avatar
Elena Lukashova committed
57

58
59
60
61
    }
  }
}

62
63
void print_meas(time_stats_t *ts, const char* name, time_stats_t * total_exec_time, time_stats_t * sf_exec_time)
{
64
65

  if (opp_enabled) {
66

67
68
    static int first_time = 0;
    static double cpu_freq_GHz = 0.0;
69

70
71
    if (cpu_freq_GHz == 0.0)
      cpu_freq_GHz = get_cpu_freq_GHz();
72

73
    if (first_time == 0) {
74
75
      first_time=1;

76
      if ((total_exec_time == NULL) || (sf_exec_time== NULL))
77
        fprintf(stderr, "%25s  %25s  %25s  %25s %25s %6f\n","Name","Total","Per Trials",   "Num Trials","CPU_F_GHz", cpu_freq_GHz);
78
      else
79
        fprintf(stderr, "%25s  %25s  %25s  %20s %15s %6f\n","Name","Total","Average/Frame","Trials",    "CPU_F_GHz", cpu_freq_GHz);
80
    }
81

82
83
    if (ts->trials>0) {
      //printf("%20s: total: %10.3f ms, average: %10.3f us (%10d trials)\n", name, ts->diff/cpu_freq_GHz/1000000.0, ts->diff/ts->trials/cpu_freq_GHz/1000.0, ts->trials);
84
85

      if ((total_exec_time == NULL) || (sf_exec_time== NULL)) {
86
        fprintf(stderr, "%25s:  %15.3f us; %15d;\n",
87
88
89
90
91
92
93
94
95
96
97
                name,
                (ts->diff/ts->trials/cpu_freq_GHz/1000.0),
                ts->trials);
      } else {
        fprintf(stderr, "%25s:  %15.3f ms (%5.2f%%); %15.3f us (%5.2f%%); %15d;\n",
                name,
                (ts->diff/cpu_freq_GHz/1000000.0),
                ((ts->diff/cpu_freq_GHz/1000000.0)/(total_exec_time->diff/cpu_freq_GHz/1000000.0))*100,  // percentage
                (ts->diff/ts->trials/cpu_freq_GHz/1000.0),
                ((ts->diff/ts->trials/cpu_freq_GHz/1000.0)/(sf_exec_time->diff/sf_exec_time->trials/cpu_freq_GHz/1000.0))*100,  // percentage
                ts->trials);
98
      }
99
100
    }
  }
101

102
}
103

104
105
double get_time_meas_us(time_stats_t *ts)
{
106

107
  static double cpu_freq_GHz = 0.0;
108

109
110
  if (cpu_freq_GHz == 0.0)
    cpu_freq_GHz = get_cpu_freq_GHz();
111
112

  if (ts->trials>0)
113
    return  (ts->diff/ts->trials/cpu_freq_GHz/1000.0);
114

115
  return 0;
116
}