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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
/* Copyright (c) 2008, 2023, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
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, version 2.0, 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, Fifth Floor, 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 idle_timer= TIMER_NAME_MICROSEC;
enum_timer_name wait_timer= TIMER_NAME_CYCLE;
enum_timer_name stage_timer= TIMER_NAME_NANOSEC;
enum_timer_name statement_timer= TIMER_NAME_NANOSEC;
enum_timer_name transaction_timer= TIMER_NAME_NANOSEC;
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 */
/* Indexed by enum enum_timer_name */
static struct time_normalizer to_pico_data[FIRST_TIMER_NAME + COUNT_TIMER_NAME]=
{
{ 0, 0}, /* unused */
{ 0, 0}, /* cycle */
{ 0, 0}, /* nanosec */
{ 0, 0}, /* microsec */
{ 0, 0}, /* millisec */
{ 0, 0} /* tick */
};
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;
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 (sys_timer_info.cycles.frequency > 0)
cycle_to_pico= round_to_ulong(pico_frequency/
(double)sys_timer_info.cycles.frequency);
else
cycle_to_pico= 0;
if (sys_timer_info.nanoseconds.frequency > 0)
nanosec_to_pico= round_to_ulong(pico_frequency/
(double)sys_timer_info.nanoseconds.frequency);
else
nanosec_to_pico= 0;
if (sys_timer_info.microseconds.frequency > 0)
microsec_to_pico= round_to_ulong(pico_frequency/
(double)sys_timer_info.microseconds.frequency);
else
microsec_to_pico= 0;
if (sys_timer_info.milliseconds.frequency > 0)
millisec_to_pico= round_to_ulong(pico_frequency/
(double)sys_timer_info.milliseconds.frequency);
else
millisec_to_pico= 0;
if (sys_timer_info.ticks.frequency > 0)
tick_to_pico= round_to_ulonglong(pico_frequency/
(double)sys_timer_info.ticks.frequency);
else
tick_to_pico= 0;
to_pico_data[TIMER_NAME_CYCLE].m_v0= cycle_v0;
to_pico_data[TIMER_NAME_CYCLE].m_factor= cycle_to_pico;
to_pico_data[TIMER_NAME_NANOSEC].m_v0= nanosec_v0;
to_pico_data[TIMER_NAME_NANOSEC].m_factor= nanosec_to_pico;
to_pico_data[TIMER_NAME_MICROSEC].m_v0= microsec_v0;
to_pico_data[TIMER_NAME_MICROSEC].m_factor= microsec_to_pico;
to_pico_data[TIMER_NAME_MILLISEC].m_v0= millisec_v0;
to_pico_data[TIMER_NAME_MILLISEC].m_factor= millisec_to_pico;
to_pico_data[TIMER_NAME_TICK].m_v0= tick_v0;
to_pico_data[TIMER_NAME_TICK].m_factor= tick_to_pico;
/*
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;
}
/*
For STAGE and STATEMENT, a timer with a fixed frequency is better.
The preferred timer is nanosecond, or lower resolutions.
*/
if (nanosec_to_pico != 0)
{
/* Normal case. */
stage_timer= TIMER_NAME_NANOSEC;
statement_timer= TIMER_NAME_NANOSEC;
transaction_timer= TIMER_NAME_NANOSEC;
}
else if (microsec_to_pico != 0)
{
/* Windows. */
stage_timer= TIMER_NAME_MICROSEC;
statement_timer= TIMER_NAME_MICROSEC;
transaction_timer= TIMER_NAME_MICROSEC;
}
else if (millisec_to_pico != 0)
{
/* Robustness, no known cases. */
stage_timer= TIMER_NAME_MILLISEC;
statement_timer= TIMER_NAME_MILLISEC;
transaction_timer= TIMER_NAME_MILLISEC;
}
else if (tick_to_pico != 0)
{
/* Robustness, no known cases. */
stage_timer= TIMER_NAME_TICK;
statement_timer= TIMER_NAME_TICK;
transaction_timer= TIMER_NAME_TICK;
}
else
{
/* Robustness, no known cases. */
stage_timer= TIMER_NAME_CYCLE;
statement_timer= TIMER_NAME_CYCLE;
transaction_timer= TIMER_NAME_CYCLE;
}
/*
For IDLE, a timer with a fixed frequency is critical,
as the CPU clock may slow down a lot if the server is completely idle.
The preferred timer is microsecond, or lower resolutions.
*/
if (microsec_to_pico != 0)
{
/* Normal case. */
idle_timer= TIMER_NAME_MICROSEC;
}
else if (millisec_to_pico != 0)
{
/* Robustness, no known cases. */
wait_timer= TIMER_NAME_MILLISEC;
}
else if (tick_to_pico != 0)
{
/* Robustness, no known cases. */
idle_timer= TIMER_NAME_TICK;
}
else
{
/* Robustness, no known cases. */
idle_timer= TIMER_NAME_CYCLE;
}
}
ulonglong get_timer_raw_value(enum_timer_name timer_name)
{
switch (timer_name)
{
case TIMER_NAME_CYCLE:
return my_timer_cycles();
case TIMER_NAME_NANOSEC:
return my_timer_nanoseconds();
case TIMER_NAME_MICROSEC:
return my_timer_microseconds();
case TIMER_NAME_MILLISEC:
return my_timer_milliseconds();
case TIMER_NAME_TICK:
return my_timer_ticks();
default:
assert(false);
}
return 0;
}
ulonglong get_timer_raw_value_and_function(enum_timer_name timer_name, timer_fct_t *fct)
{
switch (timer_name)
{
case TIMER_NAME_CYCLE:
*fct= my_timer_cycles;
return my_timer_cycles();
case TIMER_NAME_NANOSEC:
*fct= my_timer_nanoseconds;
return my_timer_nanoseconds();
case TIMER_NAME_MICROSEC:
*fct= my_timer_microseconds;
return my_timer_microseconds();
case TIMER_NAME_MILLISEC:
*fct= my_timer_milliseconds;
return my_timer_milliseconds();
case TIMER_NAME_TICK:
*fct= my_timer_ticks;
return my_timer_ticks();
default:
*fct= NULL;
assert(false);
}
return 0;
}
ulonglong get_timer_pico_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;
assert(false);
}
return result;
}
time_normalizer* time_normalizer::get(enum_timer_name timer_name)
{
uint index= static_cast<uint> (timer_name);
assert(index >= FIRST_TIMER_NAME);
assert(index <= LAST_TIMER_NAME);
return & to_pico_data[index];
}
void time_normalizer::to_pico(ulonglong start, ulonglong end,
ulonglong *pico_start, ulonglong *pico_end, ulonglong *pico_wait)
{
if (start == 0)
{
*pico_start= 0;
*pico_end= 0;
*pico_wait= 0;
}
else
{
*pico_start= (start - m_v0) * m_factor;
if (end == 0)
{
*pico_end= 0;
*pico_wait= 0;
}
else
{
*pico_end= (end - m_v0) * m_factor;
*pico_wait= (end - start) * m_factor;
}
}
}
|