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_esgs_global_by_event_name.cc
25   Table EVENTS_STAGES_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_esgs_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_esgs_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 
76 TABLE_FIELD_DEF
77 table_esgs_global_by_event_name::m_field_def=
78 { 6, field_types };
79 
80 PFS_engine_table_share
81 table_esgs_global_by_event_name::m_share=
82 {
83   { C_STRING_WITH_LEN("events_stages_summary_global_by_event_name") },
84   &pfs_truncatable_acl,
85   table_esgs_global_by_event_name::create,
86   NULL, /* write_row */
87   table_esgs_global_by_event_name::delete_all_rows,
88   table_esgs_global_by_event_name::get_row_count,
89   sizeof(PFS_simple_index),
90   &m_table_lock,
91   &m_field_def,
92   false, /* checked */
93   false  /* perpetual */
94 };
95 
96 PFS_engine_table*
create(void)97 table_esgs_global_by_event_name::create(void)
98 {
99   return new table_esgs_global_by_event_name();
100 }
101 
102 int
delete_all_rows(void)103 table_esgs_global_by_event_name::delete_all_rows(void)
104 {
105   reset_events_stages_by_thread();
106   reset_events_stages_by_account();
107   reset_events_stages_by_user();
108   reset_events_stages_by_host();
109   reset_events_stages_global();
110   return 0;
111 }
112 
113 ha_rows
get_row_count(void)114 table_esgs_global_by_event_name::get_row_count(void)
115 {
116   return stage_class_max;
117 }
118 
table_esgs_global_by_event_name()119 table_esgs_global_by_event_name::table_esgs_global_by_event_name()
120   : PFS_engine_table(&m_share, &m_pos),
121     m_row_exists(false), m_pos(1), m_next_pos(1)
122 {}
123 
reset_position(void)124 void table_esgs_global_by_event_name::reset_position(void)
125 {
126   m_pos= 1;
127   m_next_pos= 1;
128 }
129 
rnd_init(bool scan)130 int table_esgs_global_by_event_name::rnd_init(bool scan)
131 {
132   m_normalizer= time_normalizer::get(stage_timer);
133   return 0;
134 }
135 
rnd_next(void)136 int table_esgs_global_by_event_name::rnd_next(void)
137 {
138   PFS_stage_class *stage_class;
139 
140   if (global_instr_class_stages_array == NULL)
141     return HA_ERR_END_OF_FILE;
142 
143   m_pos.set_at(&m_next_pos);
144 
145   stage_class= find_stage_class(m_pos.m_index);
146   if (stage_class)
147   {
148     make_row(stage_class);
149     m_next_pos.set_after(&m_pos);
150     return 0;
151   }
152 
153   return HA_ERR_END_OF_FILE;
154 }
155 
156 int
rnd_pos(const void * pos)157 table_esgs_global_by_event_name::rnd_pos(const void *pos)
158 {
159   PFS_stage_class *stage_class;
160 
161   set_position(pos);
162 
163   if (global_instr_class_stages_array == NULL)
164     return HA_ERR_END_OF_FILE;
165 
166   stage_class=find_stage_class(m_pos.m_index);
167   if (stage_class)
168   {
169     make_row(stage_class);
170     return 0;
171   }
172 
173   return HA_ERR_RECORD_DELETED;
174 }
175 
176 
177 void table_esgs_global_by_event_name
make_row(PFS_stage_class * klass)178 ::make_row(PFS_stage_class *klass)
179 {
180   m_row.m_event_name.make_row(klass);
181 
182   PFS_connection_stage_visitor visitor(klass);
183   PFS_connection_iterator::visit_global(true,  /* hosts */
184                                         false, /* users */
185                                         true,  /* accounts */
186                                         true,  /* threads */
187                                         false, /* THDs */
188                                         & visitor);
189 
190   m_row.m_stat.set(m_normalizer, & visitor.m_stat);
191   m_row_exists= true;
192 }
193 
194 int table_esgs_global_by_event_name
read_row_values(TABLE * table,unsigned char *,Field ** fields,bool read_all)195 ::read_row_values(TABLE *table, unsigned char *, Field **fields,
196                   bool read_all)
197 {
198   Field *f;
199 
200   if (unlikely(! m_row_exists))
201     return HA_ERR_RECORD_DELETED;
202 
203   /* Set the null bits */
204   assert(table->s->null_bytes == 0);
205 
206   for (; (f= *fields) ; fields++)
207   {
208     if (read_all || bitmap_is_set(table->read_set, f->field_index))
209     {
210       switch(f->field_index)
211       {
212       case 0: /* NAME */
213         m_row.m_event_name.set_field(f);
214         break;
215       default: /* 1, ... COUNT/SUM/MIN/AVG/MAX */
216         m_row.m_stat.set_field(f->field_index - 1, f);
217         break;
218       }
219     }
220   }
221 
222   return 0;
223 }
224 
225