summaryrefslogtreecommitdiff
path: root/storage/perfschema/pfs_timer.cc
blob: 3191a0514e77ccfb3ce0c573f89e049cc9db64d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.

  This program 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; version 2 of the License.

  This program 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
  along with this program; if not, write to the Free Software Foundation,
  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */

/**
  @file storage/perfschema/pfs_timer.cc
  Performance schema timers (implementation).
*/

#include "my_global.h"
#include "pfs_timer.h"
#include "my_rdtsc.h"

enum_timer_name wait_timer= TIMER_NAME_CYCLE;
MY_TIMER_INFO pfs_timer_info;

static ulonglong cycle_v0;
static ulonglong nanosec_v0;
static ulonglong microsec_v0;
static ulonglong millisec_v0;
static ulonglong tick_v0;

static ulong cycle_to_pico; /* 1000 at 1 GHz, 333 at 3GHz, 250 at 4GHz */
static ulong nanosec_to_pico; /* In theory, 1 000 */
static ulong microsec_to_pico; /* In theory, 1 000 000 */
static ulong millisec_to_pico; /* In theory, 1 000 000 000, fits in uint32 */
static ulonglong tick_to_pico; /* 1e10 at 100 Hz, 1.666e10 at 60 Hz */

static inline ulong round_to_ulong(double value)
{
  return (ulong) (value + 0.5);
}

static inline ulonglong round_to_ulonglong(double value)
{
  return (ulonglong) (value + 0.5);
}

void init_timers(void)
{
  double pico_frequency= 1.0e12;

  my_timer_init(&pfs_timer_info);

  cycle_v0= my_timer_cycles();
  nanosec_v0= my_timer_nanoseconds();
  microsec_v0= my_timer_microseconds();
  millisec_v0= my_timer_milliseconds();
  tick_v0= my_timer_ticks();

  if (pfs_timer_info.cycles.frequency > 0)
    cycle_to_pico= round_to_ulong(pico_frequency/
                                  (double)pfs_timer_info.cycles.frequency);
  else
    cycle_to_pico= 0;

  if (pfs_timer_info.nanoseconds.frequency > 0)
    nanosec_to_pico= round_to_ulong(pico_frequency/
                                    (double)pfs_timer_info.nanoseconds.frequency);
  else
    nanosec_to_pico= 0;

  if (pfs_timer_info.microseconds.frequency > 0)
    microsec_to_pico= round_to_ulong(pico_frequency/
                                     (double)pfs_timer_info.microseconds.frequency);
  else
    microsec_to_pico= 0;

  if (pfs_timer_info.milliseconds.frequency > 0)
    millisec_to_pico= round_to_ulong(pico_frequency/
                                     (double)pfs_timer_info.milliseconds.frequency);
  else
    millisec_to_pico= 0;

  if (pfs_timer_info.ticks.frequency > 0)
    tick_to_pico= round_to_ulonglong(pico_frequency/
                                     (double)pfs_timer_info.ticks.frequency);
  else
    tick_to_pico= 0;

  /*
    Depending on the platform and build options, some timers may not be
    available. Pick best replacements.
  */

  /*
    For WAIT, the cycle timer is used by default. However, it is not available
    on all architectures. Fall back to the nanosecond timer in this case. It is
    unlikely that neither cycle nor nanosecond are available, but we continue
    probing less resolution timers anyway for consistency with other events.
  */
  if (cycle_to_pico != 0)
  {
    /* Normal case. */
    wait_timer= TIMER_NAME_CYCLE;
  }
  else if (nanosec_to_pico != 0)
  {
    /* Robustness, no known cases. */
    wait_timer= TIMER_NAME_NANOSEC;
  }
  else if (microsec_to_pico != 0)
  {
    /* Robustness, no known cases. */
    wait_timer= TIMER_NAME_MICROSEC;
  }
  else if (millisec_to_pico != 0)
  {
    /* Robustness, no known cases. */
    wait_timer= TIMER_NAME_MILLISEC;
  }
  else
  {
    /*
       Will never be reached on any architecture, but must provide a default if
       no other timers are available.
    */
    wait_timer= TIMER_NAME_TICK;
  }
}

ulonglong get_timer_value(enum_timer_name timer_name)
{
  ulonglong result;

  switch (timer_name) {
  case TIMER_NAME_CYCLE:
    result= (my_timer_cycles() - cycle_v0) * cycle_to_pico;
    break;
  case TIMER_NAME_NANOSEC:
    result= (my_timer_nanoseconds() - nanosec_v0) * nanosec_to_pico;
    break;
  case TIMER_NAME_MICROSEC:
    result= (my_timer_microseconds() - microsec_v0) * microsec_to_pico;
    break;
  case TIMER_NAME_MILLISEC:
    result= (my_timer_milliseconds() - millisec_v0) * millisec_to_pico;
    break;
  case TIMER_NAME_TICK:
    result= (my_timer_ticks() - tick_v0) * tick_to_pico;
    break;
  default:
    result= 0;
    DBUG_ASSERT(false);
  }
  return result;
}