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/table_socket_summary_by_event_name.cc
25   Table SOCKET_EVENT_NAMES (implementation).
26 */
27 
28 #include "storage/perfschema/table_socket_summary_by_event_name.h"
29 
30 #include <stddef.h>
31 
32 #include "my_dbug.h"
33 #include "my_thread.h"
34 #include "sql/field.h"
35 #include "sql/plugin_table.h"
36 #include "sql/table.h"
37 #include "storage/perfschema/pfs_column_types.h"
38 #include "storage/perfschema/pfs_column_values.h"
39 #include "storage/perfschema/pfs_global.h"
40 #include "storage/perfschema/pfs_instr.h"
41 #include "storage/perfschema/pfs_visitor.h"
42 
43 THR_LOCK table_socket_summary_by_event_name::m_table_lock;
44 
45 Plugin_table table_socket_summary_by_event_name::m_table_def(
46     /* Schema name */
47     "performance_schema",
48     /* Name */
49     "socket_summary_by_event_name",
50     /* Definition */
51     "  EVENT_NAME VARCHAR(128) not null,\n"
52     "  COUNT_STAR BIGINT unsigned not null,\n"
53     "  SUM_TIMER_WAIT BIGINT unsigned not null,\n"
54     "  MIN_TIMER_WAIT BIGINT unsigned not null,\n"
55     "  AVG_TIMER_WAIT BIGINT unsigned not null,\n"
56     "  MAX_TIMER_WAIT BIGINT unsigned not null,\n"
57     "  COUNT_READ BIGINT unsigned not null,\n"
58     "  SUM_TIMER_READ BIGINT unsigned not null,\n"
59     "  MIN_TIMER_READ BIGINT unsigned not null,\n"
60     "  AVG_TIMER_READ BIGINT unsigned not null,\n"
61     "  MAX_TIMER_READ BIGINT unsigned not null,\n"
62     "  SUM_NUMBER_OF_BYTES_READ BIGINT unsigned not null,\n"
63     "  COUNT_WRITE BIGINT unsigned not null,\n"
64     "  SUM_TIMER_WRITE BIGINT unsigned not null,\n"
65     "  MIN_TIMER_WRITE BIGINT unsigned not null,\n"
66     "  AVG_TIMER_WRITE BIGINT unsigned not null,\n"
67     "  MAX_TIMER_WRITE BIGINT unsigned not null,\n"
68     "  SUM_NUMBER_OF_BYTES_WRITE BIGINT unsigned not null,\n"
69     "  COUNT_MISC BIGINT unsigned not null,\n"
70     "  SUM_TIMER_MISC BIGINT unsigned not null,\n"
71     "  MIN_TIMER_MISC BIGINT unsigned not null,\n"
72     "  AVG_TIMER_MISC BIGINT unsigned not null,\n"
73     "  MAX_TIMER_MISC BIGINT unsigned not null,\n"
74     "  PRIMARY KEY (EVENT_NAME) USING HASH\n",
75     /* Options */
76     " ENGINE=PERFORMANCE_SCHEMA",
77     /* Tablespace */
78     nullptr);
79 
80 PFS_engine_table_share table_socket_summary_by_event_name::m_share = {
81     &pfs_truncatable_acl,
82     table_socket_summary_by_event_name::create,
83     nullptr, /* write_row */
84     table_socket_summary_by_event_name::delete_all_rows,
85     table_socket_summary_by_event_name::get_row_count,
86     sizeof(PFS_simple_index),
87     &m_table_lock,
88     &m_table_def,
89     false, /* perpetual */
90     PFS_engine_table_proxy(),
91     {0},
92     false /* m_in_purgatory */
93 };
94 
match(const PFS_socket_class * pfs)95 bool PFS_index_socket_summary_by_event_name::match(
96     const PFS_socket_class *pfs) {
97   if (m_fields >= 1) {
98     if (!m_key.match(pfs)) {
99       return false;
100     }
101   }
102   return true;
103 }
104 
create(PFS_engine_table_share *)105 PFS_engine_table *table_socket_summary_by_event_name::create(
106     PFS_engine_table_share *) {
107   return new table_socket_summary_by_event_name();
108 }
109 
table_socket_summary_by_event_name()110 table_socket_summary_by_event_name::table_socket_summary_by_event_name()
111     : PFS_engine_table(&m_share, &m_pos), m_pos(1), m_next_pos(1) {
112   m_normalizer = time_normalizer::get_wait();
113 }
114 
delete_all_rows(void)115 int table_socket_summary_by_event_name::delete_all_rows(void) {
116   reset_socket_instance_io();
117   reset_socket_class_io();
118   return 0;
119 }
120 
get_row_count(void)121 ha_rows table_socket_summary_by_event_name::get_row_count(void) {
122   return socket_class_max;
123 }
124 
reset_position(void)125 void table_socket_summary_by_event_name::reset_position(void) {
126   m_pos.m_index = 1;
127   m_next_pos.m_index = 1;
128 }
129 
rnd_next(void)130 int table_socket_summary_by_event_name::rnd_next(void) {
131   PFS_socket_class *socket_class;
132 
133   m_pos.set_at(&m_next_pos);
134 
135   socket_class = find_socket_class(m_pos.m_index);
136   if (socket_class) {
137     m_next_pos.set_after(&m_pos);
138     return make_row(socket_class);
139   }
140 
141   return HA_ERR_END_OF_FILE;
142 }
143 
rnd_pos(const void * pos)144 int table_socket_summary_by_event_name::rnd_pos(const void *pos) {
145   PFS_socket_class *socket_class;
146 
147   set_position(pos);
148 
149   socket_class = find_socket_class(m_pos.m_index);
150   if (socket_class) {
151     return make_row(socket_class);
152   }
153 
154   return HA_ERR_RECORD_DELETED;
155 }
156 
index_init(uint idx MY_ATTRIBUTE ((unused)),bool)157 int table_socket_summary_by_event_name::index_init(
158     uint idx MY_ATTRIBUTE((unused)), bool) {
159   PFS_index_socket_summary_by_event_name *result = nullptr;
160   DBUG_ASSERT(idx == 0);
161   result = PFS_NEW(PFS_index_socket_summary_by_event_name);
162   m_opened_index = result;
163   m_index = result;
164   return 0;
165 }
166 
index_next(void)167 int table_socket_summary_by_event_name::index_next(void) {
168   PFS_socket_class *socket_class;
169 
170   m_pos.set_at(&m_next_pos);
171 
172   do {
173     socket_class = find_socket_class(m_pos.m_index);
174     if (socket_class) {
175       if (m_opened_index->match(socket_class)) {
176         if (!make_row(socket_class)) {
177           m_next_pos.set_after(&m_pos);
178           return 0;
179         }
180       }
181       m_pos.next();
182     }
183   } while (socket_class != nullptr);
184 
185   return HA_ERR_END_OF_FILE;
186 }
187 
make_row(PFS_socket_class * socket_class)188 int table_socket_summary_by_event_name::make_row(
189     PFS_socket_class *socket_class) {
190   m_row.m_event_name.make_row(socket_class);
191 
192   PFS_instance_socket_io_stat_visitor visitor;
193   PFS_instance_iterator::visit_socket_instances(socket_class, &visitor);
194 
195   /* Collect timer and byte count stats */
196   m_row.m_io_stat.set(m_normalizer, &visitor.m_socket_io_stat);
197 
198   return 0;
199 }
200 
read_row_values(TABLE * table,unsigned char *,Field ** fields,bool read_all)201 int table_socket_summary_by_event_name::read_row_values(TABLE *table,
202                                                         unsigned char *,
203                                                         Field **fields,
204                                                         bool read_all) {
205   Field *f;
206 
207   /* Set the null bits */
208   DBUG_ASSERT(table->s->null_bytes == 0);
209 
210   for (; (f = *fields); fields++) {
211     if (read_all || bitmap_is_set(table->read_set, f->field_index())) {
212       switch (f->field_index()) {
213         case 0: /* EVENT_NAME */
214           m_row.m_event_name.set_field(f);
215           break;
216         case 1: /* COUNT_STAR */
217           set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_count);
218           break;
219         case 2: /* SUM_TIMER_WAIT */
220           set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_sum);
221           break;
222         case 3: /* MIN_TIMER_WAIT */
223           set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_min);
224           break;
225         case 4: /* AVG_TIMER_WAIT */
226           set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_avg);
227           break;
228         case 5: /* MAX_TIMER_WAIT */
229           set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_max);
230           break;
231 
232         case 6: /* COUNT_READ */
233           set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_count);
234           break;
235         case 7: /* SUM_TIMER_READ */
236           set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_sum);
237           break;
238         case 8: /* MIN_TIMER_READ */
239           set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_min);
240           break;
241         case 9: /* AVG_TIMER_READ */
242           set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_avg);
243           break;
244         case 10: /* MAX_TIMER_READ */
245           set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_max);
246           break;
247         case 11: /* SUM_NUMBER_OF_BYTES_READ */
248           set_field_ulonglong(f, m_row.m_io_stat.m_read.m_bytes);
249           break;
250 
251         case 12: /* COUNT_WRITE */
252           set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_count);
253           break;
254         case 13: /* SUM_TIMER_WRITE */
255           set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_sum);
256           break;
257         case 14: /* MIN_TIMER_WRITE */
258           set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_min);
259           break;
260         case 15: /* AVG_TIMER_WRITE */
261           set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_avg);
262           break;
263         case 16: /* MAX_TIMER_WRITE */
264           set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_max);
265           break;
266         case 17: /* SUM_NUMBER_OF_BYTES_WRITE */
267           set_field_ulonglong(f, m_row.m_io_stat.m_write.m_bytes);
268           break;
269 
270         case 18: /* COUNT_MISC */
271           set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_count);
272           break;
273         case 19: /* SUM_TIMER_MISC */
274           set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_sum);
275           break;
276         case 20: /* MIN_TIMER_MISC */
277           set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_min);
278           break;
279         case 21: /* AVG_TIMER_MISC */
280           set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_avg);
281           break;
282         case 22: /* MAX_TIMER_MISC */
283           set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_max);
284           break;
285 
286         default:
287           DBUG_ASSERT(false);
288           break;
289       }
290     }  // if
291   }    // for
292 
293   return 0;
294 }
295