1 /* Copyright (c) 2008, 2021, Oracle and/or its affiliates.
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 Foundation,
21 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22
23 /**
24 @file storage/perfschema/table_socket_summary_by_instance.cc
25 Table SOCKET_SUMMARY_BY_INSTANCE (implementation).
26 */
27
28 #include "my_global.h"
29 #include "my_thread.h"
30 #include "pfs_instr.h"
31 #include "pfs_column_types.h"
32 #include "pfs_column_values.h"
33 #include "table_socket_summary_by_instance.h"
34 #include "pfs_global.h"
35 #include "pfs_buffer_container.h"
36 #include "field.h"
37
38 THR_LOCK table_socket_summary_by_instance::m_table_lock;
39
40 static const TABLE_FIELD_TYPE field_types[]=
41 {
42 {
43 { C_STRING_WITH_LEN("EVENT_NAME") },
44 { C_STRING_WITH_LEN("varchar(128)") },
45 { NULL, 0}
46 },
47 {
48 { C_STRING_WITH_LEN("OBJECT_INSTANCE_BEGIN") },
49 { C_STRING_WITH_LEN("bigint(20)") },
50 { NULL, 0}
51 },
52 {
53 { C_STRING_WITH_LEN("COUNT_STAR") },
54 { C_STRING_WITH_LEN("bigint(20)") },
55 { NULL, 0}
56 },
57 {
58 { C_STRING_WITH_LEN("SUM_TIMER_WAIT") },
59 { C_STRING_WITH_LEN("bigint(20)") },
60 { NULL, 0}
61 },
62 {
63 { C_STRING_WITH_LEN("MIN_TIMER_WAIT") },
64 { C_STRING_WITH_LEN("bigint(20)") },
65 { NULL, 0}
66 },
67 {
68 { C_STRING_WITH_LEN("AVG_TIMER_WAIT") },
69 { C_STRING_WITH_LEN("bigint(20)") },
70 { NULL, 0}
71 },
72 {
73 { C_STRING_WITH_LEN("MAX_TIMER_WAIT") },
74 { C_STRING_WITH_LEN("bigint(20)") },
75 { NULL, 0}
76 },
77
78 /** Read */
79 {
80 { C_STRING_WITH_LEN("COUNT_READ") },
81 { C_STRING_WITH_LEN("bigint(20)") },
82 { NULL, 0}
83 },
84 {
85 { C_STRING_WITH_LEN("SUM_TIMER_READ") },
86 { C_STRING_WITH_LEN("bigint(20)") },
87 { NULL, 0}
88 },
89 {
90 { C_STRING_WITH_LEN("MIN_TIMER_READ") },
91 { C_STRING_WITH_LEN("bigint(20)") },
92 { NULL, 0}
93 },
94 {
95 { C_STRING_WITH_LEN("AVG_TIMER_READ") },
96 { C_STRING_WITH_LEN("bigint(20)") },
97 { NULL, 0}
98 },
99 {
100 { C_STRING_WITH_LEN("MAX_TIMER_READ") },
101 { C_STRING_WITH_LEN("bigint(20)") },
102 { NULL, 0}
103 },
104 {
105 { C_STRING_WITH_LEN("SUM_NUMBER_OF_BYTES_READ") },
106 { C_STRING_WITH_LEN("bigint(20)") },
107 { NULL, 0}
108 },
109
110 /** Write */
111 {
112 { C_STRING_WITH_LEN("COUNT_WRITE") },
113 { C_STRING_WITH_LEN("bigint(20)") },
114 { NULL, 0}
115 },
116 {
117 { C_STRING_WITH_LEN("SUM_TIMER_WRITE") },
118 { C_STRING_WITH_LEN("bigint(20)") },
119 { NULL, 0}
120 },
121 {
122 { C_STRING_WITH_LEN("MIN_TIMER_WRITE") },
123 { C_STRING_WITH_LEN("bigint(20)") },
124 { NULL, 0}
125 },
126 {
127 { C_STRING_WITH_LEN("AVG_TIMER_WRITE") },
128 { C_STRING_WITH_LEN("bigint(20)") },
129 { NULL, 0}
130 },
131 {
132 { C_STRING_WITH_LEN("MAX_TIMER_WRITE") },
133 { C_STRING_WITH_LEN("bigint(20)") },
134 { NULL, 0}
135 },
136 {
137 { C_STRING_WITH_LEN("SUM_NUMBER_OF_BYTES_WRITE") },
138 { C_STRING_WITH_LEN("bigint(20)") },
139 { NULL, 0}
140 },
141
142 /** Misc */
143 {
144 { C_STRING_WITH_LEN("COUNT_MISC") },
145 { C_STRING_WITH_LEN("bigint(20)") },
146 { NULL, 0}
147 },
148 {
149 { C_STRING_WITH_LEN("SUM_TIMER_MISC") },
150 { C_STRING_WITH_LEN("bigint(20)") },
151 { NULL, 0}
152 },
153 {
154 { C_STRING_WITH_LEN("MIN_TIMER_MISC") },
155 { C_STRING_WITH_LEN("bigint(20)") },
156 { NULL, 0}
157 },
158 {
159 { C_STRING_WITH_LEN("AVG_TIMER_MISC") },
160 { C_STRING_WITH_LEN("bigint(20)") },
161 { NULL, 0}
162 },
163 {
164 { C_STRING_WITH_LEN("MAX_TIMER_MISC") },
165 { C_STRING_WITH_LEN("bigint(20)") },
166 { NULL, 0}
167 }
168 };
169
170 TABLE_FIELD_DEF
171 table_socket_summary_by_instance::m_field_def=
172 { 24, field_types };
173
174 PFS_engine_table_share
175 table_socket_summary_by_instance::m_share=
176 {
177 { C_STRING_WITH_LEN("socket_summary_by_instance") },
178 &pfs_readonly_acl,
179 table_socket_summary_by_instance::create,
180 NULL, /* write_row */
181 table_socket_summary_by_instance::delete_all_rows,
182 table_socket_summary_by_instance::get_row_count,
183 sizeof(PFS_simple_index),
184 &m_table_lock,
185 &m_field_def,
186 false, /* checked */
187 false /* perpetual */
188 };
189
create(void)190 PFS_engine_table* table_socket_summary_by_instance::create(void)
191 {
192 return new table_socket_summary_by_instance();
193 }
194
table_socket_summary_by_instance()195 table_socket_summary_by_instance::table_socket_summary_by_instance()
196 : PFS_engine_table(&m_share, &m_pos),
197 m_row_exists(false), m_pos(0), m_next_pos(0)
198 {}
199
delete_all_rows(void)200 int table_socket_summary_by_instance::delete_all_rows(void)
201 {
202 reset_socket_instance_io();
203 return 0;
204 }
205
206 ha_rows
get_row_count(void)207 table_socket_summary_by_instance::get_row_count(void)
208 {
209 return global_socket_container.get_row_count();
210 }
211
reset_position(void)212 void table_socket_summary_by_instance::reset_position(void)
213 {
214 m_pos.m_index= 0;
215 m_next_pos.m_index= 0;
216 }
217
rnd_next(void)218 int table_socket_summary_by_instance::rnd_next(void)
219 {
220 PFS_socket *pfs;
221
222 m_pos.set_at(&m_next_pos);
223 PFS_socket_iterator it= global_socket_container.iterate(m_pos.m_index);
224 pfs= it.scan_next(& m_pos.m_index);
225 if (pfs != NULL)
226 {
227 make_row(pfs);
228 m_next_pos.set_after(&m_pos);
229 return 0;
230 }
231
232 return HA_ERR_END_OF_FILE;
233 }
234
rnd_pos(const void * pos)235 int table_socket_summary_by_instance::rnd_pos(const void *pos)
236 {
237 PFS_socket *pfs;
238
239 set_position(pos);
240
241 pfs= global_socket_container.get(m_pos.m_index);
242 if (pfs != NULL)
243 {
244 make_row(pfs);
245 return 0;
246 }
247
248 return HA_ERR_RECORD_DELETED;
249 }
250
make_row(PFS_socket * pfs)251 void table_socket_summary_by_instance::make_row(PFS_socket *pfs)
252 {
253 pfs_optimistic_state lock;
254 PFS_socket_class *safe_class;
255
256 m_row_exists= false;
257
258 /* Protect this reader against a socket delete */
259 pfs->m_lock.begin_optimistic_lock(&lock);
260
261 safe_class= sanitize_socket_class(pfs->m_class);
262 if (unlikely(safe_class == NULL))
263 return;
264
265 m_row.m_event_name.make_row(safe_class);
266 m_row.m_identity= pfs->m_identity;
267
268 time_normalizer *normalizer= time_normalizer::get(wait_timer);
269
270 /* Collect timer and byte count stats */
271 m_row.m_io_stat.set(normalizer, &pfs->m_socket_stat.m_io_stat);
272
273 if (!pfs->m_lock.end_optimistic_lock(&lock))
274 return;
275
276 m_row_exists= true;
277 }
278
read_row_values(TABLE * table,unsigned char *,Field ** fields,bool read_all)279 int table_socket_summary_by_instance::read_row_values(TABLE *table,
280 unsigned char *,
281 Field **fields,
282 bool read_all)
283 {
284 Field *f;
285
286 if (unlikely(!m_row_exists))
287 return HA_ERR_RECORD_DELETED;
288
289 /* Set the null bits */
290 assert(table->s->null_bytes == 0);
291
292 for (; (f= *fields) ; fields++)
293 {
294 if (read_all || bitmap_is_set(table->read_set, f->field_index))
295 {
296 switch(f->field_index)
297 {
298 case 0: /* EVENT_NAME */
299 m_row.m_event_name.set_field(f);
300 break;
301 case 1: /* OBJECT_INSTANCE */
302 set_field_ulonglong(f, (ulonglong)m_row.m_identity);
303 break;
304
305 case 2:/* COUNT_STAR */
306 set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_count);
307 break;
308 case 3:/* SUM_TIMER_WAIT */
309 set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_sum);
310 break;
311 case 4: /* MIN_TIMER_WAIT */
312 set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_min);
313 break;
314 case 5: /* AVG_TIMER_WAIT */
315 set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_avg);
316 break;
317 case 6: /* MAX_TIMER_WAIT */
318 set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_max);
319 break;
320
321 case 7: /* COUNT_READ */
322 set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_count);
323 break;
324 case 8: /* SUM_TIMER_READ */
325 set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_sum);
326 break;
327 case 9: /* MIN_TIMER_READ */
328 set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_min);
329 break;
330 case 10: /* AVG_TIMER_READ */
331 set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_avg);
332 break;
333 case 11: /* MAX_TIMER_READ */
334 set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_max);
335 break;
336 case 12: /* SUM_NUMBER_OF_BYTES_READ */
337 set_field_ulonglong(f, m_row.m_io_stat.m_read.m_bytes);
338 break;
339
340 case 13: /* COUNT_WRITE */
341 set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_count);
342 break;
343 case 14: /* SUM_TIMER_WRITE */
344 set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_sum);
345 break;
346 case 15: /* MIN_TIMER_WRITE */
347 set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_min);
348 break;
349 case 16: /* AVG_TIMER_WRITE */
350 set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_avg);
351 break;
352 case 17: /* MAX_TIMER_WRITE */
353 set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_max);
354 break;
355 case 18: /* SUM_NUMBER_OF_BYTES_WRITE */
356 set_field_ulonglong(f, m_row.m_io_stat.m_write.m_bytes);
357 break;
358
359 case 19: /* COUNT_MISC */
360 set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_count);
361 break;
362 case 20: /* SUM_TIMER_MISC */
363 set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_sum);
364 break;
365 case 21: /* MIN_TIMER_MISC */
366 set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_min);
367 break;
368 case 22: /* AVG_TIMER_MISC */
369 set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_avg);
370 break;
371 case 23: /* MAX_TIMER_MISC */
372 set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_max);
373 break;
374 default:
375 assert(false);
376 break;
377 }
378 }
379 }
380
381 return 0;
382 }
383