1 /* Copyright (c) 2010, 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
21   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
22 
23 /**
24   @file storage/perfschema/table_esms_by_host_by_event_name.cc
25   Table EVENTS_STATEMENTS_SUMMARY_BY_HOST_BY_EVENT_NAME (implementation).
26 */
27 
28 #include "my_global.h"
29 #include "my_thread.h"
30 #include "pfs_instr_class.h"
31 #include "pfs_column_types.h"
32 #include "pfs_column_values.h"
33 #include "table_esms_by_host_by_event_name.h"
34 #include "pfs_global.h"
35 #include "pfs_account.h"
36 #include "pfs_visitor.h"
37 #include "pfs_buffer_container.h"
38 #include "field.h"
39 
40 THR_LOCK table_esms_by_host_by_event_name::m_table_lock;
41 
42 static const TABLE_FIELD_TYPE field_types[]=
43 {
44   {
45     { C_STRING_WITH_LEN("HOST") },
46     { C_STRING_WITH_LEN("char(60)") },
47     { NULL, 0}
48   },
49   {
50     { C_STRING_WITH_LEN("EVENT_NAME") },
51     { C_STRING_WITH_LEN("varchar(128)") },
52     { NULL, 0}
53   },
54   {
55     { C_STRING_WITH_LEN("COUNT_STAR") },
56     { C_STRING_WITH_LEN("bigint(20)") },
57     { NULL, 0}
58   },
59   {
60     { C_STRING_WITH_LEN("SUM_TIMER_WAIT") },
61     { C_STRING_WITH_LEN("bigint(20)") },
62     { NULL, 0}
63   },
64   {
65     { C_STRING_WITH_LEN("MIN_TIMER_WAIT") },
66     { C_STRING_WITH_LEN("bigint(20)") },
67     { NULL, 0}
68   },
69   {
70     { C_STRING_WITH_LEN("AVG_TIMER_WAIT") },
71     { C_STRING_WITH_LEN("bigint(20)") },
72     { NULL, 0}
73   },
74   {
75     { C_STRING_WITH_LEN("MAX_TIMER_WAIT") },
76     { C_STRING_WITH_LEN("bigint(20)") },
77     { NULL, 0}
78   },
79   {
80     { C_STRING_WITH_LEN("SUM_LOCK_TIME") },
81     { C_STRING_WITH_LEN("bigint(20)") },
82     { NULL, 0}
83   },
84   {
85     { C_STRING_WITH_LEN("SUM_ERRORS") },
86     { C_STRING_WITH_LEN("bigint(20)") },
87     { NULL, 0}
88   },
89   {
90     { C_STRING_WITH_LEN("SUM_WARNINGS") },
91     { C_STRING_WITH_LEN("bigint(20)") },
92     { NULL, 0}
93   },
94   {
95     { C_STRING_WITH_LEN("SUM_ROWS_AFFECTED") },
96     { C_STRING_WITH_LEN("bigint(20)") },
97     { NULL, 0}
98   },
99   {
100     { C_STRING_WITH_LEN("SUM_ROWS_SENT") },
101     { C_STRING_WITH_LEN("bigint(20)") },
102     { NULL, 0}
103   },
104   {
105     { C_STRING_WITH_LEN("SUM_ROWS_EXAMINED") },
106     { C_STRING_WITH_LEN("bigint(20)") },
107     { NULL, 0}
108   },
109   {
110     { C_STRING_WITH_LEN("SUM_CREATED_TMP_DISK_TABLES") },
111     { C_STRING_WITH_LEN("bigint(20)") },
112     { NULL, 0}
113   },
114   {
115     { C_STRING_WITH_LEN("SUM_CREATED_TMP_TABLES") },
116     { C_STRING_WITH_LEN("bigint(20)") },
117     { NULL, 0}
118   },
119   {
120     { C_STRING_WITH_LEN("SUM_SELECT_FULL_JOIN") },
121     { C_STRING_WITH_LEN("bigint(20)") },
122     { NULL, 0}
123   },
124   {
125     { C_STRING_WITH_LEN("SUM_SELECT_FULL_RANGE_JOIN") },
126     { C_STRING_WITH_LEN("bigint(20)") },
127     { NULL, 0}
128   },
129   {
130     { C_STRING_WITH_LEN("SUM_SELECT_RANGE") },
131     { C_STRING_WITH_LEN("bigint(20)") },
132     { NULL, 0}
133   },
134   {
135     { C_STRING_WITH_LEN("SUM_SELECT_RANGE_CHECK") },
136     { C_STRING_WITH_LEN("bigint(20)") },
137     { NULL, 0}
138   },
139   {
140     { C_STRING_WITH_LEN("SUM_SELECT_SCAN") },
141     { C_STRING_WITH_LEN("bigint(20)") },
142     { NULL, 0}
143   },
144   {
145     { C_STRING_WITH_LEN("SUM_SORT_MERGE_PASSES") },
146     { C_STRING_WITH_LEN("bigint(20)") },
147     { NULL, 0}
148   },
149   {
150     { C_STRING_WITH_LEN("SUM_SORT_RANGE") },
151     { C_STRING_WITH_LEN("bigint(20)") },
152     { NULL, 0}
153   },
154   {
155     { C_STRING_WITH_LEN("SUM_SORT_ROWS") },
156     { C_STRING_WITH_LEN("bigint(20)") },
157     { NULL, 0}
158   },
159   {
160     { C_STRING_WITH_LEN("SUM_SORT_SCAN") },
161     { C_STRING_WITH_LEN("bigint(20)") },
162     { NULL, 0}
163   },
164   {
165     { C_STRING_WITH_LEN("SUM_NO_INDEX_USED") },
166     { C_STRING_WITH_LEN("bigint(20)") },
167     { NULL, 0}
168   },
169   {
170     { C_STRING_WITH_LEN("SUM_NO_GOOD_INDEX_USED") },
171     { C_STRING_WITH_LEN("bigint(20)") },
172     { NULL, 0}
173   }
174 };
175 
176 TABLE_FIELD_DEF
177 table_esms_by_host_by_event_name::m_field_def=
178 { 26, field_types };
179 
180 PFS_engine_table_share
181 table_esms_by_host_by_event_name::m_share=
182 {
183   { C_STRING_WITH_LEN("events_statements_summary_by_host_by_event_name") },
184   &pfs_truncatable_acl,
185   table_esms_by_host_by_event_name::create,
186   NULL, /* write_row */
187   table_esms_by_host_by_event_name::delete_all_rows,
188   table_esms_by_host_by_event_name::get_row_count,
189   sizeof(pos_esms_by_host_by_event_name),
190   &m_table_lock,
191   &m_field_def,
192   false, /* checked */
193   false  /* perpetual */
194 };
195 
196 PFS_engine_table*
create(void)197 table_esms_by_host_by_event_name::create(void)
198 {
199   return new table_esms_by_host_by_event_name();
200 }
201 
202 int
delete_all_rows(void)203 table_esms_by_host_by_event_name::delete_all_rows(void)
204 {
205   reset_events_statements_by_thread();
206   reset_events_statements_by_account();
207   reset_events_statements_by_host();
208   return 0;
209 }
210 
211 ha_rows
get_row_count(void)212 table_esms_by_host_by_event_name::get_row_count(void)
213 {
214   return global_host_container.get_row_count() * statement_class_max;
215 }
216 
table_esms_by_host_by_event_name()217 table_esms_by_host_by_event_name::table_esms_by_host_by_event_name()
218   : PFS_engine_table(&m_share, &m_pos),
219     m_row_exists(false), m_pos(), m_next_pos()
220 {}
221 
reset_position(void)222 void table_esms_by_host_by_event_name::reset_position(void)
223 {
224   m_pos.reset();
225   m_next_pos.reset();
226 }
227 
rnd_init(bool scan)228 int table_esms_by_host_by_event_name::rnd_init(bool scan)
229 {
230   m_normalizer= time_normalizer::get(statement_timer);
231   return 0;
232 }
233 
rnd_next(void)234 int table_esms_by_host_by_event_name::rnd_next(void)
235 {
236   PFS_host *host;
237   PFS_statement_class *statement_class;
238   bool has_more_host= true;
239 
240   for (m_pos.set_at(&m_next_pos);
241        has_more_host;
242        m_pos.next_host())
243   {
244     host= global_host_container.get(m_pos.m_index_1, & has_more_host);
245     if (host != NULL)
246     {
247       statement_class= find_statement_class(m_pos.m_index_2);
248       if (statement_class)
249       {
250         make_row(host, statement_class);
251         m_next_pos.set_after(&m_pos);
252         return 0;
253       }
254     }
255   }
256 
257   return HA_ERR_END_OF_FILE;
258 }
259 
260 int
rnd_pos(const void * pos)261 table_esms_by_host_by_event_name::rnd_pos(const void *pos)
262 {
263   PFS_host *host;
264   PFS_statement_class *statement_class;
265 
266   set_position(pos);
267 
268   host= global_host_container.get(m_pos.m_index_1);
269   if (host != NULL)
270   {
271     statement_class= find_statement_class(m_pos.m_index_2);
272     if (statement_class)
273     {
274       make_row(host, statement_class);
275       return 0;
276     }
277   }
278 
279   return HA_ERR_RECORD_DELETED;
280 }
281 
282 void table_esms_by_host_by_event_name
make_row(PFS_host * host,PFS_statement_class * klass)283 ::make_row(PFS_host *host, PFS_statement_class *klass)
284 {
285   pfs_optimistic_state lock;
286   m_row_exists= false;
287 
288   if (klass->is_mutable())
289     return;
290 
291   host->m_lock.begin_optimistic_lock(&lock);
292 
293   if (m_row.m_host.make_row(host))
294     return;
295 
296   m_row.m_event_name.make_row(klass);
297 
298   PFS_connection_statement_visitor visitor(klass);
299   PFS_connection_iterator::visit_host(host,
300                                       true,  /* accounts */
301                                       true,  /* threads */
302                                       false, /* THDs */
303                                       & visitor);
304 
305   if (! host->m_lock.end_optimistic_lock(&lock))
306     return;
307 
308   m_row_exists= true;
309   m_row.m_stat.set(m_normalizer, & visitor.m_stat);
310 }
311 
312 int table_esms_by_host_by_event_name
read_row_values(TABLE * table,unsigned char * buf,Field ** fields,bool read_all)313 ::read_row_values(TABLE *table, unsigned char *buf, Field **fields,
314                   bool read_all)
315 {
316   Field *f;
317 
318   if (unlikely(! m_row_exists))
319     return HA_ERR_RECORD_DELETED;
320 
321   /* Set the null bits */
322   assert(table->s->null_bytes == 1);
323   buf[0]= 0;
324 
325   for (; (f= *fields) ; fields++)
326   {
327     if (read_all || bitmap_is_set(table->read_set, f->field_index))
328     {
329       switch(f->field_index)
330       {
331       case 0: /* HOST */
332         m_row.m_host.set_field(f);
333         break;
334       case 1: /* EVENT_NAME */
335         m_row.m_event_name.set_field(f);
336         break;
337       default: /* 2, ... COUNT/SUM/MIN/AVG/MAX */
338         m_row.m_stat.set_field(f->field_index - 2, f);
339         break;
340       }
341     }
342   }
343 
344   return 0;
345 }
346 
347