1 /* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23 /**
24 @file storage/perfschema/pfs_timer.cc
25 Performance schema timers (implementation).
26 */
27
28 #include "storage/perfschema/pfs_timer.h"
29
30 #include <math.h>
31 #include <stddef.h>
32 #include <sys/types.h>
33
34 #include "my_dbug.h"
35 #include "my_rdtsc.h"
36 #include "mysqld_error.h"
37 #include "sql/log.h" /* log_errlog */
38
39 MY_TIMER_INFO pfs_timer_info;
40
41 static ulonglong cycle_v0;
42 static ulonglong nanosec_v0;
43 static ulonglong microsec_v0;
44 static ulonglong millisec_v0;
45
46 static ulong cycle_to_pico; /* 1000 at 1 GHz, 333 at 3GHz, 250 at 4GHz */
47 static ulong nanosec_to_pico; /* In theory, 1 000 */
48 static ulong microsec_to_pico; /* In theory, 1 000 000 */
49 static ulong millisec_to_pico; /* In theory, 1 000 000 000, fits in uint32 */
50
51 /* Indexed by enum enum_timer_name */
52 static struct time_normalizer
53 to_pico_data[FIRST_TIMER_NAME + COUNT_TIMER_NAME] = {
54 {0, 0, {0}}, /* pico (identity) */
55 {0, 0, {0}}, /* cycle */
56 {0, 0, {0}}, /* nanosec */
57 {0, 0, {0}}, /* microsec */
58 {0, 0, {0}}, /* millisec */
59 };
60
init_timers(void)61 void init_timers(void) {
62 double pico_frequency = 1.0e12;
63
64 my_timer_init(&pfs_timer_info);
65
66 cycle_v0 = my_timer_cycles();
67 nanosec_v0 = my_timer_nanoseconds();
68 microsec_v0 = my_timer_microseconds();
69 millisec_v0 = my_timer_milliseconds();
70
71 if (pfs_timer_info.cycles.frequency > 0) {
72 cycle_to_pico =
73 lrint(pico_frequency / (double)pfs_timer_info.cycles.frequency);
74 } else {
75 cycle_to_pico = 0;
76 }
77
78 if (pfs_timer_info.nanoseconds.frequency > 0) {
79 nanosec_to_pico =
80 lrint(pico_frequency / (double)pfs_timer_info.nanoseconds.frequency);
81 } else {
82 nanosec_to_pico = 0;
83 }
84
85 if (pfs_timer_info.microseconds.frequency > 0) {
86 microsec_to_pico =
87 lrint(pico_frequency / (double)pfs_timer_info.microseconds.frequency);
88 } else {
89 microsec_to_pico = 0;
90 }
91
92 if (pfs_timer_info.milliseconds.frequency > 0) {
93 millisec_to_pico =
94 lrint(pico_frequency / (double)pfs_timer_info.milliseconds.frequency);
95 } else {
96 millisec_to_pico = 0;
97 }
98
99 to_pico_data[TIMER_NAME_CYCLE].m_v0 = cycle_v0;
100 to_pico_data[TIMER_NAME_CYCLE].m_factor = cycle_to_pico;
101
102 to_pico_data[TIMER_NAME_NANOSEC].m_v0 = nanosec_v0;
103 to_pico_data[TIMER_NAME_NANOSEC].m_factor = nanosec_to_pico;
104
105 to_pico_data[TIMER_NAME_MICROSEC].m_v0 = microsec_v0;
106 to_pico_data[TIMER_NAME_MICROSEC].m_factor = microsec_to_pico;
107
108 to_pico_data[TIMER_NAME_MILLISEC].m_v0 = millisec_v0;
109 to_pico_data[TIMER_NAME_MILLISEC].m_factor = millisec_to_pico;
110
111 if (cycle_to_pico == 0) {
112 log_errlog(WARNING_LEVEL, ER_CYCLE_TIMER_IS_NOT_AVAILABLE);
113 }
114
115 #ifdef HAVE_NANOSEC_TIMER
116 if (nanosec_to_pico == 0) {
117 log_errlog(WARNING_LEVEL, ER_NANOSECOND_TIMER_IS_NOT_AVAILABLE);
118 }
119 #else
120 if (microsec_to_pico == 0) {
121 log_errlog(WARNING_LEVEL, ER_MICROSECOND_TIMER_IS_NOT_AVAILABLE);
122 }
123 #endif
124
125 /* Initialize histograms bucket timers. */
126
127 uint timer_index;
128
129 for (timer_index = FIRST_TIMER_NAME; timer_index <= LAST_TIMER_NAME;
130 timer_index++) {
131 time_normalizer *normalizer = &to_pico_data[timer_index];
132 ulonglong to_pico = normalizer->m_factor;
133 ulonglong bucket_index;
134
135 if (to_pico != 0) {
136 for (bucket_index = 0; bucket_index < NUMBER_OF_BUCKETS; bucket_index++) {
137 normalizer->m_bucket_timer[bucket_index] =
138 g_histogram_pico_timers.m_bucket_timer[bucket_index] / to_pico;
139 }
140 } else {
141 for (bucket_index = 0; bucket_index < NUMBER_OF_BUCKETS; bucket_index++) {
142 normalizer->m_bucket_timer[bucket_index] = 0;
143 }
144 }
145
146 normalizer->m_bucket_timer[NUMBER_OF_BUCKETS] = UINT64_MAX;
147 }
148 }
149
get_idle()150 time_normalizer *time_normalizer::get_idle() {
151 return &to_pico_data[USED_TIMER_NAME];
152 }
153
get_wait()154 time_normalizer *time_normalizer::get_wait() {
155 return &to_pico_data[TIMER_NAME_CYCLE];
156 }
157
get_stage()158 time_normalizer *time_normalizer::get_stage() {
159 return &to_pico_data[USED_TIMER_NAME];
160 }
161
get_statement()162 time_normalizer *time_normalizer::get_statement() {
163 return &to_pico_data[USED_TIMER_NAME];
164 }
165
get_transaction()166 time_normalizer *time_normalizer::get_transaction() {
167 return &to_pico_data[USED_TIMER_NAME];
168 }
169
to_pico(ulonglong start,ulonglong end,ulonglong * pico_start,ulonglong * pico_end,ulonglong * pico_wait)170 void time_normalizer::to_pico(ulonglong start, ulonglong end,
171 ulonglong *pico_start, ulonglong *pico_end,
172 ulonglong *pico_wait) {
173 if (start == 0) {
174 *pico_start = 0;
175 *pico_end = 0;
176 *pico_wait = 0;
177 } else {
178 *pico_start = (start - m_v0) * m_factor;
179 if (end == 0) {
180 *pico_end = 0;
181 *pico_wait = 0;
182 } else {
183 *pico_end = (end - m_v0) * m_factor;
184 *pico_wait = (end - start) * m_factor;
185 }
186 }
187 }
188
bucket_index(ulonglong t)189 ulong time_normalizer::bucket_index(ulonglong t) {
190 ulong low = 0;
191 ulong mid;
192 ulong high = NUMBER_OF_BUCKETS;
193
194 DBUG_ASSERT(m_bucket_timer[low] <= t);
195 DBUG_ASSERT(t <= m_bucket_timer[high]);
196
197 do {
198 mid = (low + high) / 2;
199 DBUG_ASSERT(low < mid);
200 DBUG_ASSERT(mid < high);
201 if (t < m_bucket_timer[mid]) {
202 high = mid;
203 } else {
204 low = mid;
205 }
206 } while (low + 1 < high);
207
208 DBUG_ASSERT(m_bucket_timer[low] <= t);
209 DBUG_ASSERT((t < m_bucket_timer[high]) || (high == NUMBER_OF_BUCKETS));
210
211 return low;
212 }
213