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_ets_global_by_event_name.cc
25   Table EVENTS_TRANSACTIONS_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_ets_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_ets_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("COUNT_READ_WRITE") },
76     { C_STRING_WITH_LEN("bigint(20)") },
77     { NULL, 0}
78   },
79   {
80     { C_STRING_WITH_LEN("SUM_TIMER_READ_WRITE") },
81     { C_STRING_WITH_LEN("bigint(20)") },
82     { NULL, 0}
83   },
84   {
85     { C_STRING_WITH_LEN("MIN_TIMER_READ_WRITE") },
86     { C_STRING_WITH_LEN("bigint(20)") },
87     { NULL, 0}
88   },
89   {
90     { C_STRING_WITH_LEN("AVG_TIMER_READ_WRITE") },
91     { C_STRING_WITH_LEN("bigint(20)") },
92     { NULL, 0}
93   },
94   {
95     { C_STRING_WITH_LEN("MAX_TIMER_READ_WRITE") },
96     { C_STRING_WITH_LEN("bigint(20)") },
97     { NULL, 0}
98   },
99   {
100     { C_STRING_WITH_LEN("COUNT_READ_ONLY") },
101     { C_STRING_WITH_LEN("bigint(20)") },
102     { NULL, 0}
103   },
104   {
105     { C_STRING_WITH_LEN("SUM_TIMER_READ_ONLY") },
106     { C_STRING_WITH_LEN("bigint(20)") },
107     { NULL, 0}
108   },
109   {
110     { C_STRING_WITH_LEN("MIN_TIMER_READ_ONLY") },
111     { C_STRING_WITH_LEN("bigint(20)") },
112     { NULL, 0}
113   },
114   {
115     { C_STRING_WITH_LEN("AVG_TIMER_READ_ONLY") },
116     { C_STRING_WITH_LEN("bigint(20)") },
117     { NULL, 0}
118   },
119   {
120     { C_STRING_WITH_LEN("MAX_TIMER_READ_ONLY") },
121     { C_STRING_WITH_LEN("bigint(20)") },
122     { NULL, 0}
123   }
124 };
125 
126 TABLE_FIELD_DEF
127 table_ets_global_by_event_name::m_field_def=
128 { 16, field_types };
129 
130 PFS_engine_table_share
131 table_ets_global_by_event_name::m_share=
132 {
133   { C_STRING_WITH_LEN("events_transactions_summary_global_by_event_name") },
134   &pfs_truncatable_acl,
135   table_ets_global_by_event_name::create,
136   NULL, /* write_row */
137   table_ets_global_by_event_name::delete_all_rows,
138   table_ets_global_by_event_name::get_row_count,
139   sizeof(PFS_simple_index),
140   &m_table_lock,
141   &m_field_def,
142   false, /* checked */
143   false  /* perpetual */
144 };
145 
146 PFS_engine_table*
create(void)147 table_ets_global_by_event_name::create(void)
148 {
149   return new table_ets_global_by_event_name();
150 }
151 
152 int
delete_all_rows(void)153 table_ets_global_by_event_name::delete_all_rows(void)
154 {
155   reset_events_transactions_by_thread();
156   reset_events_transactions_by_account();
157   reset_events_transactions_by_user();
158   reset_events_transactions_by_host();
159   reset_events_transactions_global();
160   return 0;
161 }
162 
163 ha_rows
get_row_count(void)164 table_ets_global_by_event_name::get_row_count(void)
165 {
166   return transaction_class_max;
167 }
168 
table_ets_global_by_event_name()169 table_ets_global_by_event_name::table_ets_global_by_event_name()
170   : PFS_engine_table(&m_share, &m_pos),
171     m_row_exists(false), m_pos(1), m_next_pos(1)
172 {}
173 
reset_position(void)174 void table_ets_global_by_event_name::reset_position(void)
175 {
176   m_pos= 1;
177   m_next_pos= 1;
178 }
179 
rnd_init(bool scan)180 int table_ets_global_by_event_name::rnd_init(bool scan)
181 {
182   m_normalizer= time_normalizer::get(transaction_timer);
183   return 0;
184 }
185 
rnd_next(void)186 int table_ets_global_by_event_name::rnd_next(void)
187 {
188   PFS_transaction_class *transaction_class;
189 
190   m_pos.set_at(&m_next_pos);
191 
192   transaction_class= find_transaction_class(m_pos.m_index);
193   if (transaction_class)
194   {
195     make_row(transaction_class);
196     m_next_pos.set_after(&m_pos);
197     return 0;
198   }
199 
200   return HA_ERR_END_OF_FILE;
201 }
202 
203 int
rnd_pos(const void * pos)204 table_ets_global_by_event_name::rnd_pos(const void *pos)
205 {
206   PFS_transaction_class *transaction_class;
207 
208   set_position(pos);
209 
210   transaction_class=find_transaction_class(m_pos.m_index);
211   if (transaction_class)
212   {
213     make_row(transaction_class);
214     return 0;
215   }
216 
217   return HA_ERR_RECORD_DELETED;
218 }
219 
220 
221 void table_ets_global_by_event_name
make_row(PFS_transaction_class * klass)222 ::make_row(PFS_transaction_class *klass)
223 {
224   m_row.m_event_name.make_row(klass);
225 
226   PFS_connection_transaction_visitor visitor(klass);
227   PFS_connection_iterator::visit_global(true,  /* hosts */
228                                         false, /* users */
229                                         true,  /* accounts */
230                                         true,  /* threads */
231                                         false, /* THDs */
232                                         & visitor);
233 
234   m_row.m_stat.set(m_normalizer, & visitor.m_stat);
235   m_row_exists= true;
236 }
237 
238 int table_ets_global_by_event_name
read_row_values(TABLE * table,unsigned char *,Field ** fields,bool read_all)239 ::read_row_values(TABLE *table, unsigned char *, Field **fields,
240                   bool read_all)
241 {
242   Field *f;
243 
244   if (unlikely(! m_row_exists))
245     return HA_ERR_RECORD_DELETED;
246 
247   /* Set the null bits */
248   assert(table->s->null_bytes == 0);
249 
250   for (; (f= *fields) ; fields++)
251   {
252     if (read_all || bitmap_is_set(table->read_set, f->field_index))
253     {
254       switch(f->field_index)
255       {
256       case 0: /* NAME */
257         m_row.m_event_name.set_field(f);
258         break;
259       default:
260         /**
261           Columns COUNT_STAR, SUM/MIN/AVG/MAX_TIMER_WAIT,
262           COUNT_READ_WRITE, SUM/MIN/AVG/MAX_TIMER_READ_WRITE,
263           COUNT_READ_ONLY, SUM/MIN/AVG/MAX_TIMER_READ_ONLY
264         */
265         m_row.m_stat.set_field(f->field_index - 1, f);
266         break;
267       }
268     }
269   }
270 
271   return 0;
272 }
273 
274