1 | /* Copyright (c) 2010, 2015, 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 as published by |
5 | the Free Software Foundation; version 2 of the License. |
6 | |
7 | This program is distributed in the hope that it will be useful, |
8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10 | GNU General Public License for more details. |
11 | |
12 | You should have received a copy of the GNU General Public License |
13 | along with this program; if not, write to the Free Software |
14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ |
15 | |
16 | /** |
17 | @file storage/perfschema/table_helper.cc |
18 | Performance schema table helpers (implementation). |
19 | */ |
20 | |
21 | #include "my_global.h" |
22 | #include "my_pthread.h" |
23 | #include "pfs_engine_table.h" |
24 | #include "table_helper.h" |
25 | #include "pfs_host.h" |
26 | #include "pfs_user.h" |
27 | #include "pfs_account.h" |
28 | #include "pfs_instr.h" |
29 | |
30 | int PFS_host_row::make_row(PFS_host *pfs) |
31 | { |
32 | m_hostname_length= pfs->m_hostname_length; |
33 | if (m_hostname_length > sizeof(m_hostname)) |
34 | return 1; |
35 | if (m_hostname_length > 0) |
36 | memcpy(m_hostname, pfs->m_hostname, sizeof(m_hostname)); |
37 | return 0; |
38 | } |
39 | |
40 | void PFS_host_row::set_field(Field *f) |
41 | { |
42 | if (m_hostname_length > 0) |
43 | PFS_engine_table::set_field_char_utf8(f, m_hostname, m_hostname_length); |
44 | else |
45 | f->set_null(); |
46 | } |
47 | |
48 | int PFS_user_row::make_row(PFS_user *pfs) |
49 | { |
50 | m_username_length= pfs->m_username_length; |
51 | if (m_username_length > sizeof(m_username)) |
52 | return 1; |
53 | if (m_username_length > 0) |
54 | memcpy(m_username, pfs->m_username, sizeof(m_username)); |
55 | return 0; |
56 | } |
57 | |
58 | void PFS_user_row::set_field(Field *f) |
59 | { |
60 | if (m_username_length > 0) |
61 | PFS_engine_table::set_field_char_utf8(f, m_username, m_username_length); |
62 | else |
63 | f->set_null(); |
64 | } |
65 | |
66 | int PFS_account_row::make_row(PFS_account *pfs) |
67 | { |
68 | m_username_length= pfs->m_username_length; |
69 | if (m_username_length > sizeof(m_username)) |
70 | return 1; |
71 | if (m_username_length > 0) |
72 | memcpy(m_username, pfs->m_username, sizeof(m_username)); |
73 | |
74 | m_hostname_length= pfs->m_hostname_length; |
75 | if (m_hostname_length > sizeof(m_hostname)) |
76 | return 1; |
77 | if (m_hostname_length > 0) |
78 | memcpy(m_hostname, pfs->m_hostname, sizeof(m_hostname)); |
79 | |
80 | return 0; |
81 | } |
82 | |
83 | void PFS_account_row::set_field(uint index, Field *f) |
84 | { |
85 | switch (index) |
86 | { |
87 | case 0: /* USER */ |
88 | if (m_username_length > 0) |
89 | PFS_engine_table::set_field_char_utf8(f, m_username, m_username_length); |
90 | else |
91 | f->set_null(); |
92 | break; |
93 | case 1: /* HOST */ |
94 | if (m_hostname_length > 0) |
95 | PFS_engine_table::set_field_char_utf8(f, m_hostname, m_hostname_length); |
96 | else |
97 | f->set_null(); |
98 | break; |
99 | default: |
100 | DBUG_ASSERT(false); |
101 | break; |
102 | } |
103 | } |
104 | |
105 | int PFS_digest_row::make_row(PFS_statements_digest_stat* pfs) |
106 | { |
107 | m_schema_name_length= pfs->m_digest_key.m_schema_name_length; |
108 | if (m_schema_name_length > sizeof(m_schema_name)) |
109 | m_schema_name_length= 0; |
110 | if (m_schema_name_length > 0) |
111 | memcpy(m_schema_name, pfs->m_digest_key.m_schema_name, m_schema_name_length); |
112 | |
113 | size_t safe_byte_count= pfs->m_digest_storage.m_byte_count; |
114 | if (safe_byte_count > pfs_max_digest_length) |
115 | safe_byte_count= 0; |
116 | |
117 | /* |
118 | "0" value for byte_count indicates special entry i.e. aggregated |
119 | stats at index 0 of statements_digest_stat_array. So do not calculate |
120 | digest/digest_text as it should always be "NULL". |
121 | */ |
122 | if (safe_byte_count > 0) |
123 | { |
124 | /* |
125 | Calculate digest from MD5 HASH collected to be shown as |
126 | DIGEST in this row. |
127 | */ |
128 | MD5_HASH_TO_STRING(pfs->m_digest_storage.m_md5, m_digest); |
129 | m_digest_length= MD5_HASH_TO_STRING_LENGTH; |
130 | |
131 | /* |
132 | Calculate digest_text information from the token array collected |
133 | to be shown as DIGEST_TEXT column. |
134 | */ |
135 | compute_digest_text(&pfs->m_digest_storage, &m_digest_text); |
136 | |
137 | if (m_digest_text.length() == 0) |
138 | m_digest_length= 0; |
139 | } |
140 | else |
141 | { |
142 | m_digest_length= 0; |
143 | m_digest_text.length(0); |
144 | } |
145 | |
146 | return 0; |
147 | } |
148 | |
149 | void PFS_digest_row::set_field(uint index, Field *f) |
150 | { |
151 | switch (index) |
152 | { |
153 | case 0: /* SCHEMA_NAME */ |
154 | if (m_schema_name_length > 0) |
155 | PFS_engine_table::set_field_varchar_utf8(f, m_schema_name, |
156 | m_schema_name_length); |
157 | else |
158 | f->set_null(); |
159 | break; |
160 | case 1: /* DIGEST */ |
161 | if (m_digest_length > 0) |
162 | PFS_engine_table::set_field_varchar_utf8(f, m_digest, |
163 | m_digest_length); |
164 | else |
165 | f->set_null(); |
166 | break; |
167 | case 2: /* DIGEST_TEXT */ |
168 | if (m_digest_text.length() > 0) |
169 | PFS_engine_table::set_field_longtext_utf8(f, m_digest_text.ptr(), |
170 | m_digest_text.length()); |
171 | else |
172 | f->set_null(); |
173 | break; |
174 | default: |
175 | DBUG_ASSERT(false); |
176 | break; |
177 | } |
178 | } |
179 | |
180 | int PFS_object_row::make_row(PFS_table_share *pfs) |
181 | { |
182 | m_object_type= pfs->get_object_type(); |
183 | |
184 | m_schema_name_length= pfs->m_schema_name_length; |
185 | if (m_schema_name_length > sizeof(m_schema_name)) |
186 | return 1; |
187 | if (m_schema_name_length > 0) |
188 | memcpy(m_schema_name, pfs->m_schema_name, sizeof(m_schema_name)); |
189 | |
190 | m_object_name_length= pfs->m_table_name_length; |
191 | if (m_object_name_length > sizeof(m_object_name)) |
192 | return 1; |
193 | if (m_object_name_length > 0) |
194 | memcpy(m_object_name, pfs->m_table_name, sizeof(m_object_name)); |
195 | |
196 | return 0; |
197 | } |
198 | |
199 | void PFS_object_row::set_field(uint index, Field *f) |
200 | { |
201 | switch(index) |
202 | { |
203 | case 0: /* OBJECT_TYPE */ |
204 | set_field_object_type(f, m_object_type); |
205 | break; |
206 | case 1: /* SCHEMA_NAME */ |
207 | PFS_engine_table::set_field_varchar_utf8(f, m_schema_name, m_schema_name_length); |
208 | break; |
209 | case 2: /* OBJECT_NAME */ |
210 | PFS_engine_table::set_field_varchar_utf8(f, m_object_name, m_object_name_length); |
211 | break; |
212 | default: |
213 | DBUG_ASSERT(false); |
214 | } |
215 | } |
216 | |
217 | int PFS_index_row::make_row(PFS_table_share *pfs, uint table_index) |
218 | { |
219 | if (m_object_row.make_row(pfs)) |
220 | return 1; |
221 | |
222 | if (table_index < MAX_INDEXES) |
223 | { |
224 | PFS_table_key *key= &pfs->m_keys[table_index]; |
225 | m_index_name_length= key->m_name_length; |
226 | if (m_index_name_length > sizeof(m_index_name)) |
227 | return 1; |
228 | memcpy(m_index_name, key->m_name, sizeof(m_index_name)); |
229 | } |
230 | else |
231 | m_index_name_length= 0; |
232 | |
233 | return 0; |
234 | } |
235 | |
236 | void PFS_index_row::set_field(uint index, Field *f) |
237 | { |
238 | switch(index) |
239 | { |
240 | case 0: /* OBJECT_TYPE */ |
241 | case 1: /* SCHEMA_NAME */ |
242 | case 2: /* OBJECT_NAME */ |
243 | m_object_row.set_field(index, f); |
244 | break; |
245 | case 3: /* INDEX_NAME */ |
246 | if (m_index_name_length > 0) |
247 | PFS_engine_table::set_field_varchar_utf8(f, m_index_name, m_index_name_length); |
248 | else |
249 | f->set_null(); |
250 | break; |
251 | default: |
252 | DBUG_ASSERT(false); |
253 | } |
254 | } |
255 | |
256 | void PFS_statement_stat_row::set_field(uint index, Field *f) |
257 | { |
258 | switch (index) |
259 | { |
260 | case 0: /* COUNT_STAR */ |
261 | case 1: /* SUM_TIMER_WAIT */ |
262 | case 2: /* MIN_TIMER_WAIT */ |
263 | case 3: /* AVG_TIMER_WAIT */ |
264 | case 4: /* MAX_TIMER_WAIT */ |
265 | m_timer1_row.set_field(index, f); |
266 | break; |
267 | case 5: /* SUM_LOCK_TIME */ |
268 | PFS_engine_table::set_field_ulonglong(f, m_lock_time); |
269 | break; |
270 | case 6: /* SUM_ERRORS */ |
271 | PFS_engine_table::set_field_ulonglong(f, m_error_count); |
272 | break; |
273 | case 7: /* SUM_WARNINGS */ |
274 | PFS_engine_table::set_field_ulonglong(f, m_warning_count); |
275 | break; |
276 | case 8: /* SUM_ROWS_AFFECTED */ |
277 | PFS_engine_table::set_field_ulonglong(f, m_rows_affected); |
278 | break; |
279 | case 9: /* SUM_ROWS_SENT */ |
280 | PFS_engine_table::set_field_ulonglong(f, m_rows_sent); |
281 | break; |
282 | case 10: /* SUM_ROWS_EXAMINED */ |
283 | PFS_engine_table::set_field_ulonglong(f, m_rows_examined); |
284 | break; |
285 | case 11: /* SUM_CREATED_TMP_DISK_TABLES */ |
286 | PFS_engine_table::set_field_ulonglong(f, m_created_tmp_disk_tables); |
287 | break; |
288 | case 12: /* SUM_CREATED_TMP_TABLES */ |
289 | PFS_engine_table::set_field_ulonglong(f, m_created_tmp_tables); |
290 | break; |
291 | case 13: /* SUM_SELECT_FULL_JOIN */ |
292 | PFS_engine_table::set_field_ulonglong(f, m_select_full_join); |
293 | break; |
294 | case 14: /* SUM_SELECT_FULL_RANGE_JOIN */ |
295 | PFS_engine_table::set_field_ulonglong(f, m_select_full_range_join); |
296 | break; |
297 | case 15: /* SUM_SELECT_RANGE */ |
298 | PFS_engine_table::set_field_ulonglong(f, m_select_range); |
299 | break; |
300 | case 16: /* SUM_SELECT_RANGE_CHECK */ |
301 | PFS_engine_table::set_field_ulonglong(f, m_select_range_check); |
302 | break; |
303 | case 17: /* SUM_SELECT_SCAN */ |
304 | PFS_engine_table::set_field_ulonglong(f, m_select_scan); |
305 | break; |
306 | case 18: /* SUM_SORT_MERGE_PASSES */ |
307 | PFS_engine_table::set_field_ulonglong(f, m_sort_merge_passes); |
308 | break; |
309 | case 19: /* SUM_SORT_RANGE */ |
310 | PFS_engine_table::set_field_ulonglong(f, m_sort_range); |
311 | break; |
312 | case 20: /* SUM_SORT_ROWS */ |
313 | PFS_engine_table::set_field_ulonglong(f, m_sort_rows); |
314 | break; |
315 | case 21: /* SUM_SORT_SCAN */ |
316 | PFS_engine_table::set_field_ulonglong(f, m_sort_scan); |
317 | break; |
318 | case 22: /* SUM_NO_INDEX_USED */ |
319 | PFS_engine_table::set_field_ulonglong(f, m_no_index_used); |
320 | break; |
321 | case 23: /* SUM_NO_GOOD_INDEX_USED */ |
322 | PFS_engine_table::set_field_ulonglong(f, m_no_good_index_used); |
323 | break; |
324 | default: |
325 | DBUG_ASSERT(false); |
326 | break; |
327 | } |
328 | } |
329 | |
330 | void PFS_connection_stat_row::set_field(uint index, Field *f) |
331 | { |
332 | switch (index) |
333 | { |
334 | case 0: /* CURRENT_CONNECTIONS */ |
335 | PFS_engine_table::set_field_ulonglong(f, m_current_connections); |
336 | break; |
337 | case 1: /* TOTAL_CONNECTIONS */ |
338 | PFS_engine_table::set_field_ulonglong(f, m_total_connections); |
339 | break; |
340 | default: |
341 | DBUG_ASSERT(false); |
342 | break; |
343 | } |
344 | } |
345 | |
346 | void set_field_object_type(Field *f, enum_object_type object_type) |
347 | { |
348 | switch (object_type) |
349 | { |
350 | case OBJECT_TYPE_TABLE: |
351 | PFS_engine_table::set_field_varchar_utf8(f, "TABLE" , 5); |
352 | break; |
353 | case OBJECT_TYPE_TEMPORARY_TABLE: |
354 | PFS_engine_table::set_field_varchar_utf8(f, "TEMPORARY TABLE" , 15); |
355 | break; |
356 | default: |
357 | DBUG_ASSERT(false); |
358 | } |
359 | } |
360 | |
361 | |