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, Suite 500, Boston, MA 02110-1335 USA */
22 
23 /**
24   @file storage/perfschema/table_esms_by_program.cc
25   Table EVENTS_STATEMENTS_SUMMARY_BY_PROGRAM (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 "pfs_global.h"
34 #include "pfs_instr.h"
35 #include "pfs_timer.h"
36 #include "pfs_visitor.h"
37 #include "pfs_program.h"
38 #include "table_esms_by_program.h"
39 #include "pfs_buffer_container.h"
40 #include "field.h"
41 
42 THR_LOCK table_esms_by_program::m_table_lock;
43 
44 static const TABLE_FIELD_TYPE field_types[]=
45 {
46   {
47     { C_STRING_WITH_LEN("OBJECT_TYPE") },
48     { C_STRING_WITH_LEN("enum(\'EVENT\',\'FUNCTION\',\'PROCEDURE\',\'TABLE\',\'TRIGGER\')") },
49     { NULL, 0}
50   },
51   {
52     { C_STRING_WITH_LEN("OBJECT_SCHEMA") },
53     { C_STRING_WITH_LEN("varchar(64)") },
54     { NULL, 0}
55   },
56   {
57     { C_STRING_WITH_LEN("OBJECT_NAME") },
58     { C_STRING_WITH_LEN("varchar(64)") },
59     { NULL, 0}
60   },
61   {
62     { C_STRING_WITH_LEN("COUNT_STAR") },
63     { C_STRING_WITH_LEN("bigint(20)") },
64     { NULL, 0}
65   },
66   {
67     { C_STRING_WITH_LEN("SUM_TIMER_WAIT") },
68     { C_STRING_WITH_LEN("bigint(20)") },
69     { NULL, 0}
70   },
71   {
72     { C_STRING_WITH_LEN("MIN_TIMER_WAIT") },
73     { C_STRING_WITH_LEN("bigint(20)") },
74     { NULL, 0}
75   },
76   {
77     { C_STRING_WITH_LEN("AVG_TIMER_WAIT") },
78     { C_STRING_WITH_LEN("bigint(20)") },
79     { NULL, 0}
80   },
81   {
82     { C_STRING_WITH_LEN("MAX_TIMER_WAIT") },
83     { C_STRING_WITH_LEN("bigint(20)") },
84     { NULL, 0}
85   },
86   {
87     { C_STRING_WITH_LEN("COUNT_STATEMENTS") },
88     { C_STRING_WITH_LEN("bigint(20)") },
89     { NULL, 0}
90   },
91   {
92     { C_STRING_WITH_LEN("SUM_STATEMENTS_WAIT") },
93     { C_STRING_WITH_LEN("bigint(20)") },
94     { NULL, 0}
95   },
96   {
97     { C_STRING_WITH_LEN("MIN_STATEMENTS_WAIT") },
98     { C_STRING_WITH_LEN("bigint(20)") },
99     { NULL, 0}
100   },
101   {
102     { C_STRING_WITH_LEN("AVG_STATEMENTS_WAIT") },
103     { C_STRING_WITH_LEN("bigint(20)") },
104     { NULL, 0}
105   },
106   {
107     { C_STRING_WITH_LEN("MAX_STATEMENTS_WAIT") },
108     { C_STRING_WITH_LEN("bigint(20)") },
109     { NULL, 0}
110   },
111   {
112     { C_STRING_WITH_LEN("SUM_LOCK_TIME") },
113     { C_STRING_WITH_LEN("bigint(20)") },
114     { NULL, 0}
115   },
116   {
117     { C_STRING_WITH_LEN("SUM_ERRORS") },
118     { C_STRING_WITH_LEN("bigint(20)") },
119     { NULL, 0}
120   },
121   {
122     { C_STRING_WITH_LEN("SUM_WARNINGS") },
123     { C_STRING_WITH_LEN("bigint(20)") },
124     { NULL, 0}
125   },
126   {
127     { C_STRING_WITH_LEN("SUM_ROWS_AFFECTED") },
128     { C_STRING_WITH_LEN("bigint(20)") },
129     { NULL, 0}
130   },
131   {
132     { C_STRING_WITH_LEN("SUM_ROWS_SENT") },
133     { C_STRING_WITH_LEN("bigint(20)") },
134     { NULL, 0}
135   },
136   {
137     { C_STRING_WITH_LEN("SUM_ROWS_EXAMINED") },
138     { C_STRING_WITH_LEN("bigint(20)") },
139     { NULL, 0}
140   },
141   {
142     { C_STRING_WITH_LEN("SUM_CREATED_TMP_DISK_TABLES") },
143     { C_STRING_WITH_LEN("bigint(20)") },
144     { NULL, 0}
145   },
146   {
147     { C_STRING_WITH_LEN("SUM_CREATED_TMP_TABLES") },
148     { C_STRING_WITH_LEN("bigint(20)") },
149     { NULL, 0}
150   },
151   {
152     { C_STRING_WITH_LEN("SUM_SELECT_FULL_JOIN") },
153     { C_STRING_WITH_LEN("bigint(20)") },
154     { NULL, 0}
155   },
156   {
157     { C_STRING_WITH_LEN("SUM_SELECT_FULL_RANGE_JOIN") },
158     { C_STRING_WITH_LEN("bigint(20)") },
159     { NULL, 0}
160   },
161   {
162     { C_STRING_WITH_LEN("SUM_SELECT_RANGE") },
163     { C_STRING_WITH_LEN("bigint(20)") },
164     { NULL, 0}
165   },
166   {
167     { C_STRING_WITH_LEN("SUM_SELECT_RANGE_CHECK") },
168     { C_STRING_WITH_LEN("bigint(20)") },
169     { NULL, 0}
170   },
171   {
172     { C_STRING_WITH_LEN("SUM_SELECT_SCAN") },
173     { C_STRING_WITH_LEN("bigint(20)") },
174     { NULL, 0}
175   },
176   {
177     { C_STRING_WITH_LEN("SUM_SORT_MERGE_PASSES") },
178     { C_STRING_WITH_LEN("bigint(20)") },
179     { NULL, 0}
180   },
181   {
182     { C_STRING_WITH_LEN("SUM_SORT_RANGE") },
183     { C_STRING_WITH_LEN("bigint(20)") },
184     { NULL, 0}
185   },
186   {
187     { C_STRING_WITH_LEN("SUM_SORT_ROWS") },
188     { C_STRING_WITH_LEN("bigint(20)") },
189     { NULL, 0}
190   },
191   {
192     { C_STRING_WITH_LEN("SUM_SORT_SCAN") },
193     { C_STRING_WITH_LEN("bigint(20)") },
194     { NULL, 0}
195   },
196   {
197     { C_STRING_WITH_LEN("SUM_NO_INDEX_USED") },
198     { C_STRING_WITH_LEN("bigint(20)") },
199     { NULL, 0}
200   },
201   {
202     { C_STRING_WITH_LEN("SUM_NO_GOOD_INDEX_USED") },
203     { C_STRING_WITH_LEN("bigint(20)") },
204     { NULL, 0}
205   },
206 };
207 
208 TABLE_FIELD_DEF
209 table_esms_by_program::m_field_def=
210 { 32, field_types };
211 
212 PFS_engine_table_share
213 table_esms_by_program::m_share=
214 {
215   { C_STRING_WITH_LEN("events_statements_summary_by_program") },
216   &pfs_truncatable_acl,
217   table_esms_by_program::create,
218   NULL, /* write_row */
219   table_esms_by_program::delete_all_rows,
220   table_esms_by_program::get_row_count,
221   sizeof(PFS_simple_index),
222   &m_table_lock,
223   &m_field_def,
224   false, /* checked */
225   false  /* perpetual */
226 };
227 
228 PFS_engine_table*
create(void)229 table_esms_by_program::create(void)
230 {
231   return new table_esms_by_program();
232 }
233 
234 int
delete_all_rows(void)235 table_esms_by_program::delete_all_rows(void)
236 {
237   reset_esms_by_program();
238   return 0;
239 }
240 
241 ha_rows
get_row_count(void)242 table_esms_by_program::get_row_count(void)
243 {
244   return global_program_container.get_row_count();
245 }
246 
table_esms_by_program()247 table_esms_by_program::table_esms_by_program()
248   : PFS_engine_table(&m_share, &m_pos),
249     m_row_exists(false), m_pos(0), m_next_pos(0)
250 {}
251 
reset_position(void)252 void table_esms_by_program::reset_position(void)
253 {
254   m_pos= 0;
255   m_next_pos= 0;
256 }
257 
rnd_next(void)258 int table_esms_by_program::rnd_next(void)
259 {
260   PFS_program* pfs;
261 
262   m_pos.set_at(&m_next_pos);
263   PFS_program_iterator it= global_program_container.iterate(m_pos.m_index);
264   pfs= it.scan_next(& m_pos.m_index);
265   if (pfs != NULL)
266   {
267     make_row(pfs);
268     m_next_pos.set_after(&m_pos);
269     return 0;
270   }
271 
272   return HA_ERR_END_OF_FILE;
273 }
274 
275 int
rnd_pos(const void * pos)276 table_esms_by_program::rnd_pos(const void *pos)
277 {
278   PFS_program* pfs;
279 
280   set_position(pos);
281 
282   pfs= global_program_container.get(m_pos.m_index);
283   if (pfs != NULL)
284   {
285     make_row(pfs);
286     return 0;
287   }
288 
289   return HA_ERR_RECORD_DELETED;
290 }
291 
292 
make_row(PFS_program * program)293 void table_esms_by_program::make_row(PFS_program* program)
294 {
295   pfs_optimistic_state lock;
296   m_row_exists= false;
297 
298   program->m_lock.begin_optimistic_lock(&lock);
299 
300   m_row.m_object_type= program->m_type;
301 
302   m_row.m_object_name_length= program->m_object_name_length;
303   if(m_row.m_object_name_length > 0)
304     memcpy(m_row.m_object_name, program->m_object_name,
305            m_row.m_object_name_length);
306 
307   m_row.m_schema_name_length= program->m_schema_name_length;
308   if(m_row.m_schema_name_length > 0)
309     memcpy(m_row.m_schema_name, program->m_schema_name,
310            m_row.m_schema_name_length);
311 
312   time_normalizer *normalizer= time_normalizer::get(statement_timer);
313   /* Get stored program's over all stats. */
314   m_row.m_sp_stat.set(normalizer, &program->m_sp_stat);
315   /* Get sub statements' stats. */
316   m_row.m_stmt_stat.set(normalizer, & program->m_stmt_stat);
317 
318   if (! program->m_lock.end_optimistic_lock(&lock))
319     return;
320 
321   m_row_exists= true;
322 }
323 
324 int table_esms_by_program
read_row_values(TABLE * table,unsigned char * buf,Field ** fields,bool read_all)325 ::read_row_values(TABLE *table, unsigned char *buf, Field **fields,
326                   bool read_all)
327 {
328   Field *f;
329 
330   if (unlikely(! m_row_exists))
331     return HA_ERR_RECORD_DELETED;
332 
333   /*
334     Set the null bits. It indicates how many fields could be null
335     in the table.
336   */
337   assert(table->s->null_bytes == 1);
338   buf[0]= 0;
339 
340   for (; (f= *fields) ; fields++)
341   {
342     if (read_all || bitmap_is_set(table->read_set, f->field_index))
343     {
344       switch(f->field_index)
345       {
346       case 0: /* OBJECT_TYPE */
347         if(m_row.m_object_type != 0)
348           set_field_enum(f, m_row.m_object_type);
349         else
350           f->set_null();
351         break;
352       case 1: /* OBJECT_SCHEMA */
353         if(m_row.m_schema_name_length > 0)
354           set_field_varchar_utf8(f, m_row.m_schema_name,
355                                  m_row.m_schema_name_length);
356         else
357           f->set_null();
358         break;
359       case 2: /* OBJECT_NAME */
360         if(m_row.m_object_name_length > 0)
361           set_field_varchar_utf8(f, m_row.m_object_name,
362                                  m_row.m_object_name_length);
363         else
364           f->set_null();
365         break;
366       case 3: /* COUNT_STAR */
367       case 4: /* SUM_TIMER_WAIT */
368       case 5: /* MIN_TIMER_WAIT */
369       case 6: /* AVG_TIMER_WAIT */
370       case 7: /* MAX_TIMER_WAIT */
371         m_row.m_sp_stat.set_field(f->field_index - 3, f);
372         break;
373       default: /* 8, ... COUNT/SUM/MIN/AVG/MAX */
374         m_row.m_stmt_stat.set_field(f->field_index - 8, f);
375         break;
376       }
377     }
378   }
379 
380   return 0;
381 }
382 
383