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