1 /* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
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_pthread.h"
30 #include "table_setup_consumers.h"
31 #include "pfs_instr.h"
32 #include "pfs_events_waits.h"
33 #include "pfs_digest.h"
34 
35 #define COUNT_SETUP_CONSUMERS 12
36 static row_setup_consumers all_setup_consumers_data[COUNT_SETUP_CONSUMERS]=
37 {
38   {
39     { C_STRING_WITH_LEN("events_stages_current") },
40     &flag_events_stages_current,
41     false
42   },
43   {
44     { C_STRING_WITH_LEN("events_stages_history") },
45     &flag_events_stages_history,
46     false
47   },
48   {
49     { C_STRING_WITH_LEN("events_stages_history_long") },
50     &flag_events_stages_history_long,
51     false
52   },
53   {
54     { C_STRING_WITH_LEN("events_statements_current") },
55     &flag_events_statements_current,
56     false
57   },
58   {
59     { C_STRING_WITH_LEN("events_statements_history") },
60     &flag_events_statements_history,
61     false
62   },
63   {
64     { C_STRING_WITH_LEN("events_statements_history_long") },
65     &flag_events_statements_history_long,
66     false
67   },
68   {
69     { C_STRING_WITH_LEN("events_waits_current") },
70     &flag_events_waits_current,
71     false
72   },
73   {
74     { C_STRING_WITH_LEN("events_waits_history") },
75     &flag_events_waits_history,
76     false
77   },
78   {
79     { C_STRING_WITH_LEN("events_waits_history_long") },
80     &flag_events_waits_history_long,
81     false
82   },
83   {
84     { C_STRING_WITH_LEN("global_instrumentation") },
85     &flag_global_instrumentation,
86     true
87   },
88   {
89     { C_STRING_WITH_LEN("thread_instrumentation") },
90     &flag_thread_instrumentation,
91     false
92   },
93   {
94     { C_STRING_WITH_LEN("statements_digest") },
95     &flag_statements_digest,
96     false
97   }
98 };
99 
100 THR_LOCK table_setup_consumers::m_table_lock;
101 
102 static const TABLE_FIELD_TYPE field_types[]=
103 {
104   {
105     { C_STRING_WITH_LEN("NAME") },
106     { C_STRING_WITH_LEN("varchar(64)") },
107     { NULL, 0}
108   },
109   {
110     { C_STRING_WITH_LEN("ENABLED") },
111     { C_STRING_WITH_LEN("enum(\'YES\',\'NO\')") },
112     { NULL, 0}
113   }
114 };
115 
116 TABLE_FIELD_DEF
117 table_setup_consumers::m_field_def=
118 { 2, field_types };
119 
120 PFS_engine_table_share
121 table_setup_consumers::m_share=
122 {
123   { C_STRING_WITH_LEN("setup_consumers") },
124   &pfs_updatable_acl,
125   &table_setup_consumers::create,
126   NULL, /* write_row */
127   NULL, /* delete_all_rows */
128   NULL, /* get_row_count */
129   COUNT_SETUP_CONSUMERS, /* records */
130   sizeof(PFS_simple_index), /* ref length */
131   &m_table_lock,
132   &m_field_def,
133   false /* checked */
134 };
135 
create(void)136 PFS_engine_table* table_setup_consumers::create(void)
137 {
138   return new table_setup_consumers();
139 }
140 
table_setup_consumers()141 table_setup_consumers::table_setup_consumers()
142   : PFS_engine_table(&m_share, &m_pos),
143     m_row(NULL), m_pos(0), m_next_pos(0)
144 {}
145 
reset_position(void)146 void table_setup_consumers::reset_position(void)
147 {
148   m_pos.m_index= 0;
149   m_next_pos.m_index= 0;
150 }
151 
rnd_next(void)152 int table_setup_consumers::rnd_next(void)
153 {
154   int result;
155 
156   m_pos.set_at(&m_next_pos);
157 
158   if (m_pos.m_index < COUNT_SETUP_CONSUMERS)
159   {
160     m_row= &all_setup_consumers_data[m_pos.m_index];
161     m_next_pos.set_after(&m_pos);
162     result= 0;
163   }
164   else
165   {
166     m_row= NULL;
167     result= HA_ERR_END_OF_FILE;
168   }
169 
170   return result;
171 }
172 
rnd_pos(const void * pos)173 int table_setup_consumers::rnd_pos(const void *pos)
174 {
175   set_position(pos);
176   DBUG_ASSERT(m_pos.m_index < COUNT_SETUP_CONSUMERS);
177   m_row= &all_setup_consumers_data[m_pos.m_index];
178   return 0;
179 }
180 
read_row_values(TABLE * table,unsigned char *,Field ** fields,bool read_all)181 int table_setup_consumers::read_row_values(TABLE *table,
182                                            unsigned char *,
183                                            Field **fields,
184                                            bool read_all)
185 {
186   Field *f;
187 
188   DBUG_ASSERT(m_row);
189 
190 
191   /* Set the null bits */
192   DBUG_ASSERT(table->s->null_bytes == 0);
193 
194   for (; (f= *fields) ; fields++)
195   {
196     if (read_all || bitmap_is_set(table->read_set, f->field_index))
197     {
198       switch(f->field_index)
199       {
200       case 0: /* NAME */
201         set_field_varchar_utf8(f, m_row->m_name.str, m_row->m_name.length);
202         break;
203       case 1: /* ENABLED */
204         set_field_enum(f, (*m_row->m_enabled_ptr) ? ENUM_YES : ENUM_NO);
205         break;
206       default:
207         DBUG_ASSERT(false);
208       }
209     }
210   }
211 
212   return 0;
213 }
214 
update_row_values(TABLE * table,const unsigned char *,unsigned char *,Field ** fields)215 int table_setup_consumers::update_row_values(TABLE *table,
216                                              const unsigned char *,
217                                              unsigned char *,
218                                              Field **fields)
219 {
220   Field *f;
221   enum_yes_no value;
222 
223   DBUG_ASSERT(m_row);
224 
225   for (; (f= *fields) ; fields++)
226   {
227     if (bitmap_is_set(table->write_set, f->field_index))
228     {
229       switch(f->field_index)
230       {
231       case 0: /* NAME */
232         return HA_ERR_WRONG_COMMAND;
233       case 1: /* ENABLED */
234       {
235         value= (enum_yes_no) get_field_enum(f);
236         *m_row->m_enabled_ptr= (value == ENUM_YES) ? true : false;
237         break;
238       }
239       default:
240         DBUG_ASSERT(false);
241       }
242     }
243   }
244 
245   if (m_row->m_refresh)
246     update_instruments_derived_flags();
247 
248   return 0;
249 }
250 
251 
252