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