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, Fifth Floor, Boston, MA 02110-1301, USA */
22 
23 /**
24   @file storage/perfschema/table_esgs_by_host_by_event_name.cc
25   Table EVENTS_STAGES_SUMMARY_BY_HOST_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_by_host_by_event_name.h"
34 #include "pfs_global.h"
35 #include "pfs_account.h"
36 #include "pfs_visitor.h"
37 #include "pfs_buffer_container.h"
38 #include "field.h"
39 
40 THR_LOCK table_esgs_by_host_by_event_name::m_table_lock;
41 
42 static const TABLE_FIELD_TYPE field_types[]=
43 {
44   {
45     { C_STRING_WITH_LEN("HOST") },
46     { C_STRING_WITH_LEN("char(60)") },
47     { NULL, 0}
48   },
49   {
50     { C_STRING_WITH_LEN("EVENT_NAME") },
51     { C_STRING_WITH_LEN("varchar(128)") },
52     { NULL, 0}
53   },
54   {
55     { C_STRING_WITH_LEN("COUNT_STAR") },
56     { C_STRING_WITH_LEN("bigint(20)") },
57     { NULL, 0}
58   },
59   {
60     { C_STRING_WITH_LEN("SUM_TIMER_WAIT") },
61     { C_STRING_WITH_LEN("bigint(20)") },
62     { NULL, 0}
63   },
64   {
65     { C_STRING_WITH_LEN("MIN_TIMER_WAIT") },
66     { C_STRING_WITH_LEN("bigint(20)") },
67     { NULL, 0}
68   },
69   {
70     { C_STRING_WITH_LEN("AVG_TIMER_WAIT") },
71     { C_STRING_WITH_LEN("bigint(20)") },
72     { NULL, 0}
73   },
74   {
75     { C_STRING_WITH_LEN("MAX_TIMER_WAIT") },
76     { C_STRING_WITH_LEN("bigint(20)") },
77     { NULL, 0}
78   }
79 };
80 
81 TABLE_FIELD_DEF
82 table_esgs_by_host_by_event_name::m_field_def=
83 { 7, field_types };
84 
85 PFS_engine_table_share
86 table_esgs_by_host_by_event_name::m_share=
87 {
88   { C_STRING_WITH_LEN("events_stages_summary_by_host_by_event_name") },
89   &pfs_truncatable_acl,
90   table_esgs_by_host_by_event_name::create,
91   NULL, /* write_row */
92   table_esgs_by_host_by_event_name::delete_all_rows,
93   table_esgs_by_host_by_event_name::get_row_count,
94   sizeof(pos_esgs_by_host_by_event_name),
95   &m_table_lock,
96   &m_field_def,
97   false, /* checked */
98   false  /* perpetual */
99 };
100 
101 PFS_engine_table*
create(void)102 table_esgs_by_host_by_event_name::create(void)
103 {
104   return new table_esgs_by_host_by_event_name();
105 }
106 
107 int
delete_all_rows(void)108 table_esgs_by_host_by_event_name::delete_all_rows(void)
109 {
110   reset_events_stages_by_thread();
111   reset_events_stages_by_account();
112   reset_events_stages_by_host();
113   return 0;
114 }
115 
116 ha_rows
get_row_count(void)117 table_esgs_by_host_by_event_name::get_row_count(void)
118 {
119   return global_host_container.get_row_count() * stage_class_max;
120 }
121 
table_esgs_by_host_by_event_name()122 table_esgs_by_host_by_event_name::table_esgs_by_host_by_event_name()
123   : PFS_engine_table(&m_share, &m_pos),
124     m_row_exists(false), m_pos(), m_next_pos()
125 {}
126 
reset_position(void)127 void table_esgs_by_host_by_event_name::reset_position(void)
128 {
129   m_pos.reset();
130   m_next_pos.reset();
131 }
132 
rnd_init(bool scan)133 int table_esgs_by_host_by_event_name::rnd_init(bool scan)
134 {
135   m_normalizer= time_normalizer::get(stage_timer);
136   return 0;
137 }
138 
rnd_next(void)139 int table_esgs_by_host_by_event_name::rnd_next(void)
140 {
141   PFS_host *host;
142   PFS_stage_class *stage_class;
143   bool has_more_host= true;
144 
145   for (m_pos.set_at(&m_next_pos);
146        has_more_host;
147        m_pos.next_host())
148   {
149     host= global_host_container.get(m_pos.m_index_1, & has_more_host);
150     if (host != NULL)
151     {
152       stage_class= find_stage_class(m_pos.m_index_2);
153       if (stage_class)
154       {
155         make_row(host, stage_class);
156         m_next_pos.set_after(&m_pos);
157         return 0;
158       }
159     }
160   }
161 
162   return HA_ERR_END_OF_FILE;
163 }
164 
165 int
rnd_pos(const void * pos)166 table_esgs_by_host_by_event_name::rnd_pos(const void *pos)
167 {
168   PFS_host *host;
169   PFS_stage_class *stage_class;
170 
171   set_position(pos);
172 
173   host= global_host_container.get(m_pos.m_index_1);
174   if (host != NULL)
175   {
176     stage_class= find_stage_class(m_pos.m_index_2);
177     if (stage_class)
178     {
179       make_row(host, stage_class);
180       return 0;
181     }
182   }
183 
184   return HA_ERR_RECORD_DELETED;
185 }
186 
187 void table_esgs_by_host_by_event_name
make_row(PFS_host * host,PFS_stage_class * klass)188 ::make_row(PFS_host *host, PFS_stage_class *klass)
189 {
190   pfs_optimistic_state lock;
191   m_row_exists= false;
192 
193   host->m_lock.begin_optimistic_lock(&lock);
194 
195   if (m_row.m_host.make_row(host))
196     return;
197 
198   m_row.m_event_name.make_row(klass);
199 
200   PFS_connection_stage_visitor visitor(klass);
201   PFS_connection_iterator::visit_host(host,
202                                       true,  /* accounts */
203                                       true,  /* threads */
204                                       false, /* THDs */
205                                       & visitor);
206 
207   if (! host->m_lock.end_optimistic_lock(&lock))
208     return;
209 
210   m_row_exists= true;
211   m_row.m_stat.set(m_normalizer, & visitor.m_stat);
212 }
213 
214 int table_esgs_by_host_by_event_name
read_row_values(TABLE * table,unsigned char * buf,Field ** fields,bool read_all)215 ::read_row_values(TABLE *table, unsigned char *buf, Field **fields,
216                   bool read_all)
217 {
218   Field *f;
219 
220   if (unlikely(! m_row_exists))
221     return HA_ERR_RECORD_DELETED;
222 
223   /* Set the null bits */
224   assert(table->s->null_bytes == 1);
225   buf[0]= 0;
226 
227   for (; (f= *fields) ; fields++)
228   {
229     if (read_all || bitmap_is_set(table->read_set, f->field_index))
230     {
231       switch(f->field_index)
232       {
233       case 0: /* HOST */
234         m_row.m_host.set_field(f);
235         break;
236       case 1: /* EVENT_NAME */
237         m_row.m_event_name.set_field(f);
238         break;
239       default: /* 2, ... COUNT/SUM/MIN/AVG/MAX */
240         m_row.m_stat.set_field(f->field_index - 2, f);
241         break;
242       }
243     }
244   }
245 
246   return 0;
247 }
248 
249