1 /* Copyright (c) 2008, 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 Foundation,
21   51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22 
23 /**
24   @file storage/perfschema/table_setup_consumers.cc
25   Table SETUP_CONSUMERS (implementation).
26 */
27 
28 #include "my_global.h"
29 #include "my_thread.h"
30 #include "table_setup_consumers.h"
31 #include "pfs_instr.h"
32 #include "pfs_events_waits.h"
33 #include "pfs_digest.h"
34 #include "field.h"
35 
36 #define COUNT_SETUP_CONSUMERS 15
37 
38 static row_setup_consumers all_setup_consumers_data[COUNT_SETUP_CONSUMERS]=
39 {
40   {
41     { C_STRING_WITH_LEN("events_stages_current") },
42     &flag_events_stages_current,
43     false,
44     false
45   },
46   {
47     { C_STRING_WITH_LEN("events_stages_history") },
48     &flag_events_stages_history,
49     false,
50     true
51   },
52   {
53     { C_STRING_WITH_LEN("events_stages_history_long") },
54     &flag_events_stages_history_long,
55     false,
56     true
57   },
58   {
59     { C_STRING_WITH_LEN("events_statements_current") },
60     &flag_events_statements_current,
61     false,
62     false
63   },
64   {
65     { C_STRING_WITH_LEN("events_statements_history") },
66     &flag_events_statements_history,
67     false,
68     true
69   },
70   {
71     { C_STRING_WITH_LEN("events_statements_history_long") },
72     &flag_events_statements_history_long,
73     false,
74     true
75   },
76   {
77     { C_STRING_WITH_LEN("events_transactions_current") },
78     &flag_events_transactions_current,
79     false,
80     false
81   },
82   {
83     { C_STRING_WITH_LEN("events_transactions_history") },
84     &flag_events_transactions_history,
85     false,
86     true
87   },
88   {
89     { C_STRING_WITH_LEN("events_transactions_history_long") },
90     &flag_events_transactions_history_long,
91     false,
92     true
93   },
94   {
95     { C_STRING_WITH_LEN("events_waits_current") },
96     &flag_events_waits_current,
97     false,
98     false
99   },
100   {
101     { C_STRING_WITH_LEN("events_waits_history") },
102     &flag_events_waits_history,
103     false,
104     true
105   },
106   {
107     { C_STRING_WITH_LEN("events_waits_history_long") },
108     &flag_events_waits_history_long,
109     false,
110     true
111   },
112   {
113     { C_STRING_WITH_LEN("global_instrumentation") },
114     &flag_global_instrumentation,
115     true,
116     true
117   },
118   {
119     { C_STRING_WITH_LEN("thread_instrumentation") },
120     &flag_thread_instrumentation,
121     false,
122     true
123   },
124   {
125     { C_STRING_WITH_LEN("statements_digest") },
126     &flag_statements_digest,
127     false,
128     false
129   }
130 };
131 
132 THR_LOCK table_setup_consumers::m_table_lock;
133 
134 static const TABLE_FIELD_TYPE field_types[]=
135 {
136   {
137     { C_STRING_WITH_LEN("NAME") },
138     { C_STRING_WITH_LEN("varchar(64)") },
139     { NULL, 0}
140   },
141   {
142     { C_STRING_WITH_LEN("ENABLED") },
143     { C_STRING_WITH_LEN("enum(\'YES\',\'NO\')") },
144     { NULL, 0}
145   }
146 };
147 
148 TABLE_FIELD_DEF
149 table_setup_consumers::m_field_def=
150 { 2, field_types };
151 
152 PFS_engine_table_share
153 table_setup_consumers::m_share=
154 {
155   { C_STRING_WITH_LEN("setup_consumers") },
156   &pfs_updatable_acl,
157   table_setup_consumers::create,
158   NULL, /* write_row */
159   NULL, /* delete_all_rows */
160   table_setup_consumers::get_row_count,
161   sizeof(PFS_simple_index), /* ref length */
162   &m_table_lock,
163   &m_field_def,
164   false, /* checked */
165   false  /* perpetual */
166 };
167 
create(void)168 PFS_engine_table* table_setup_consumers::create(void)
169 {
170   return new table_setup_consumers();
171 }
172 
173 ha_rows
get_row_count(void)174 table_setup_consumers::get_row_count(void)
175 {
176   return COUNT_SETUP_CONSUMERS;
177 }
178 
table_setup_consumers()179 table_setup_consumers::table_setup_consumers()
180   : PFS_engine_table(&m_share, &m_pos),
181     m_row(NULL), m_pos(0), m_next_pos(0)
182 {}
183 
reset_position(void)184 void table_setup_consumers::reset_position(void)
185 {
186   m_pos.m_index= 0;
187   m_next_pos.m_index= 0;
188 }
189 
rnd_next(void)190 int table_setup_consumers::rnd_next(void)
191 {
192   int result;
193 
194   m_pos.set_at(&m_next_pos);
195 
196   if (m_pos.m_index < COUNT_SETUP_CONSUMERS)
197   {
198     m_row= &all_setup_consumers_data[m_pos.m_index];
199     m_next_pos.set_after(&m_pos);
200     result= 0;
201   }
202   else
203   {
204     m_row= NULL;
205     result= HA_ERR_END_OF_FILE;
206   }
207 
208   return result;
209 }
210 
rnd_pos(const void * pos)211 int table_setup_consumers::rnd_pos(const void *pos)
212 {
213   set_position(pos);
214   assert(m_pos.m_index < COUNT_SETUP_CONSUMERS);
215   m_row= &all_setup_consumers_data[m_pos.m_index];
216   return 0;
217 }
218 
read_row_values(TABLE * table,unsigned char *,Field ** fields,bool read_all)219 int table_setup_consumers::read_row_values(TABLE *table,
220                                            unsigned char *,
221                                            Field **fields,
222                                            bool read_all)
223 {
224   Field *f;
225 
226   assert(m_row);
227 
228 
229   /* Set the null bits */
230   assert(table->s->null_bytes == 0);
231 
232   for (; (f= *fields) ; fields++)
233   {
234     if (read_all || bitmap_is_set(table->read_set, f->field_index))
235     {
236       switch(f->field_index)
237       {
238       case 0: /* NAME */
239         set_field_varchar_utf8(f, m_row->m_name.str, m_row->m_name.length);
240         break;
241       case 1: /* ENABLED */
242         set_field_enum(f, (*m_row->m_enabled_ptr) ? ENUM_YES : ENUM_NO);
243         break;
244       default:
245         assert(false);
246       }
247     }
248   }
249 
250   return 0;
251 }
252 
update_row_values(TABLE * table,const unsigned char *,unsigned char *,Field ** fields)253 int table_setup_consumers::update_row_values(TABLE *table,
254                                              const unsigned char *,
255                                              unsigned char *,
256                                              Field **fields)
257 {
258   Field *f;
259   enum_yes_no value;
260 
261   assert(m_row);
262 
263   for (; (f= *fields) ; fields++)
264   {
265     if (bitmap_is_set(table->write_set, f->field_index))
266     {
267       switch(f->field_index)
268       {
269       case 0: /* NAME */
270         return HA_ERR_WRONG_COMMAND;
271       case 1: /* ENABLED */
272       {
273         value= (enum_yes_no) get_field_enum(f);
274         *m_row->m_enabled_ptr= (value == ENUM_YES) ? true : false;
275         break;
276       }
277       default:
278         assert(false);
279       }
280     }
281   }
282 
283   if (m_row->m_instrument_refresh)
284     update_instruments_derived_flags();
285 
286   if (m_row->m_thread_refresh)
287     update_thread_derived_flags();
288 
289   return 0;
290 }
291 
292 
293