1/* Copyright (c) 2010, 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 as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16/**
17 @file storage/perfschema/table_ews_global_by_event_name.cc
18 Table EVENTS_WAITS_SUMMARY_GLOBAL_BY_EVENT_NAME (implementation).
19*/
20
21#include "my_global.h"
22#include "my_pthread.h"
23#include "pfs_instr_class.h"
24#include "pfs_column_types.h"
25#include "pfs_column_values.h"
26#include "table_ews_global_by_event_name.h"
27#include "pfs_global.h"
28#include "pfs_instr.h"
29#include "pfs_timer.h"
30#include "pfs_visitor.h"
31
32THR_LOCK table_ews_global_by_event_name::m_table_lock;
33
34PFS_engine_table_share
35table_ews_global_by_event_name::m_share=
36{
37 { C_STRING_WITH_LEN("events_waits_summary_global_by_event_name") },
38 &pfs_truncatable_acl,
39 table_ews_global_by_event_name::create,
40 NULL, /* write_row */
41 table_ews_global_by_event_name::delete_all_rows,
42 NULL, /* get_row_count */
43 1000, /* records */
44 sizeof(pos_ews_global_by_event_name),
45 &m_table_lock,
46 { C_STRING_WITH_LEN("CREATE TABLE events_waits_summary_global_by_event_name("
47 "EVENT_NAME VARCHAR(128) not null,"
48 "COUNT_STAR BIGINT unsigned not null,"
49 "SUM_TIMER_WAIT BIGINT unsigned not null,"
50 "MIN_TIMER_WAIT BIGINT unsigned not null,"
51 "AVG_TIMER_WAIT BIGINT unsigned not null,"
52 "MAX_TIMER_WAIT BIGINT unsigned not null)") }
53};
54
55PFS_engine_table*
56table_ews_global_by_event_name::create(void)
57{
58 return new table_ews_global_by_event_name();
59}
60
61int
62table_ews_global_by_event_name::delete_all_rows(void)
63{
64 reset_events_waits_by_instance();
65 reset_table_waits_by_table_handle();
66 reset_table_waits_by_table();
67 reset_events_waits_by_class();
68 return 0;
69}
70
71table_ews_global_by_event_name::table_ews_global_by_event_name()
72 : PFS_engine_table(&m_share, &m_pos),
73 m_row_exists(false), m_pos(), m_next_pos()
74{}
75
76void table_ews_global_by_event_name::reset_position(void)
77{
78 m_pos.reset();
79 m_next_pos.reset();
80}
81
82int table_ews_global_by_event_name::rnd_next(void)
83{
84 PFS_mutex_class *mutex_class;
85 PFS_rwlock_class *rwlock_class;
86 PFS_cond_class *cond_class;
87 PFS_file_class *file_class;
88 PFS_socket_class *socket_class;
89 PFS_instr_class *instr_class;
90
91 for (m_pos.set_at(&m_next_pos);
92 m_pos.has_more_view();
93 m_pos.next_view())
94 {
95 switch (m_pos.m_index_1)
96 {
97 case pos_ews_global_by_event_name::VIEW_MUTEX:
98 mutex_class= find_mutex_class(m_pos.m_index_2);
99 if (mutex_class)
100 {
101 make_mutex_row(mutex_class);
102 m_next_pos.set_after(&m_pos);
103 return 0;
104 }
105 break;
106 case pos_ews_global_by_event_name::VIEW_RWLOCK:
107 rwlock_class= find_rwlock_class(m_pos.m_index_2);
108 if (rwlock_class)
109 {
110 make_rwlock_row(rwlock_class);
111 m_next_pos.set_after(&m_pos);
112 return 0;
113 }
114 break;
115 case pos_ews_global_by_event_name::VIEW_COND:
116 cond_class= find_cond_class(m_pos.m_index_2);
117 if (cond_class)
118 {
119 make_cond_row(cond_class);
120 m_next_pos.set_after(&m_pos);
121 return 0;
122 }
123 break;
124 case pos_ews_global_by_event_name::VIEW_FILE:
125 file_class= find_file_class(m_pos.m_index_2);
126 if (file_class)
127 {
128 make_file_row(file_class);
129 m_next_pos.set_after(&m_pos);
130 return 0;
131 }
132 break;
133 case pos_ews_global_by_event_name::VIEW_TABLE:
134 if (m_pos.m_index_2 == 1)
135 {
136 make_table_io_row(&global_table_io_class);
137 m_next_pos.set_after(&m_pos);
138 return 0;
139 }
140 if (m_pos.m_index_2 == 2)
141 {
142 make_table_lock_row(&global_table_lock_class);
143 m_next_pos.set_after(&m_pos);
144 return 0;
145 }
146 break;
147 case pos_ews_global_by_event_name::VIEW_SOCKET:
148 socket_class= find_socket_class(m_pos.m_index_2);
149 if (socket_class)
150 {
151 make_socket_row(socket_class);
152 m_next_pos.set_after(&m_pos);
153 return 0;
154 }
155 break;
156 case pos_ews_global_by_event_name::VIEW_IDLE:
157 instr_class= find_idle_class(m_pos.m_index_2);
158 if (instr_class)
159 {
160 make_idle_row(instr_class);
161 m_next_pos.set_after(&m_pos);
162 return 0;
163 }
164 break;
165 default:
166 break;
167 }
168 }
169
170 return HA_ERR_END_OF_FILE;
171}
172
173int
174table_ews_global_by_event_name::rnd_pos(const void *pos)
175{
176 PFS_mutex_class *mutex_class;
177 PFS_rwlock_class *rwlock_class;
178 PFS_cond_class *cond_class;
179 PFS_file_class *file_class;
180 PFS_socket_class *socket_class;
181 PFS_instr_class *instr_class;
182
183 set_position(pos);
184
185 switch (m_pos.m_index_1)
186 {
187 case pos_ews_global_by_event_name::VIEW_MUTEX:
188 mutex_class= find_mutex_class(m_pos.m_index_2);
189 if (mutex_class)
190 {
191 make_mutex_row(mutex_class);
192 return 0;
193 }
194 break;
195 case pos_ews_global_by_event_name::VIEW_RWLOCK:
196 rwlock_class= find_rwlock_class(m_pos.m_index_2);
197 if (rwlock_class)
198 {
199 make_rwlock_row(rwlock_class);
200 return 0;
201 }
202 break;
203 case pos_ews_global_by_event_name::VIEW_COND:
204 cond_class= find_cond_class(m_pos.m_index_2);
205 if (cond_class)
206 {
207 make_cond_row(cond_class);
208 return 0;
209 }
210 break;
211 case pos_ews_global_by_event_name::VIEW_FILE:
212 file_class= find_file_class(m_pos.m_index_2);
213 if (file_class)
214 {
215 make_file_row(file_class);
216 return 0;
217 }
218 break;
219 case pos_ews_global_by_event_name::VIEW_TABLE:
220 DBUG_ASSERT(m_pos.m_index_2 >= 1);
221 DBUG_ASSERT(m_pos.m_index_2 <= 2);
222 if (m_pos.m_index_2 == 1)
223 make_table_io_row(&global_table_io_class);
224 else
225 make_table_lock_row(&global_table_lock_class);
226 break;
227 case pos_ews_global_by_event_name::VIEW_SOCKET:
228 socket_class= find_socket_class(m_pos.m_index_2);
229 if (socket_class)
230 {
231 make_socket_row(socket_class);
232 return 0;
233 }
234 break;
235 case pos_ews_global_by_event_name::VIEW_IDLE:
236 instr_class= find_idle_class(m_pos.m_index_2);
237 if (instr_class)
238 {
239 make_idle_row(instr_class);
240 return 0;
241 }
242 break;
243 }
244
245 return HA_ERR_RECORD_DELETED;
246}
247
248void table_ews_global_by_event_name
249::make_mutex_row(PFS_mutex_class *klass)
250{
251 m_row.m_event_name.make_row(klass);
252
253 PFS_instance_wait_visitor visitor;
254 PFS_instance_iterator::visit_mutex_instances(klass, & visitor);
255
256 get_normalizer(klass);
257 m_row.m_stat.set(m_normalizer, & visitor.m_stat);
258 m_row_exists= true;
259}
260
261void table_ews_global_by_event_name
262::make_rwlock_row(PFS_rwlock_class *klass)
263{
264 m_row.m_event_name.make_row(klass);
265
266 PFS_instance_wait_visitor visitor;
267 PFS_instance_iterator::visit_rwlock_instances(klass, & visitor);
268
269 get_normalizer(klass);
270 m_row.m_stat.set(m_normalizer, & visitor.m_stat);
271 m_row_exists= true;
272}
273
274void table_ews_global_by_event_name
275::make_cond_row(PFS_cond_class *klass)
276{
277 m_row.m_event_name.make_row(klass);
278
279 PFS_instance_wait_visitor visitor;
280 PFS_instance_iterator::visit_cond_instances(klass, & visitor);
281
282 get_normalizer(klass);
283 m_row.m_stat.set(m_normalizer, & visitor.m_stat);
284 m_row_exists= true;
285}
286
287void table_ews_global_by_event_name
288::make_file_row(PFS_file_class *klass)
289{
290 m_row.m_event_name.make_row(klass);
291
292 PFS_instance_wait_visitor visitor;
293 PFS_instance_iterator::visit_file_instances(klass, & visitor);
294
295 get_normalizer(klass);
296 m_row.m_stat.set(m_normalizer, & visitor.m_stat);
297 m_row_exists= true;
298}
299
300void table_ews_global_by_event_name
301::make_table_io_row(PFS_instr_class *klass)
302{
303 m_row.m_event_name.make_row(klass);
304
305 PFS_table_io_wait_visitor visitor;
306 PFS_object_iterator::visit_all_tables(& visitor);
307
308 get_normalizer(klass);
309 m_row.m_stat.set(m_normalizer, & visitor.m_stat);
310 m_row_exists= true;
311}
312
313void table_ews_global_by_event_name
314::make_table_lock_row(PFS_instr_class *klass)
315{
316 m_row.m_event_name.make_row(klass);
317
318 PFS_table_lock_wait_visitor visitor;
319 PFS_object_iterator::visit_all_tables(& visitor);
320
321 get_normalizer(klass);
322 m_row.m_stat.set(m_normalizer, & visitor.m_stat);
323 m_row_exists= true;
324}
325
326void table_ews_global_by_event_name
327::make_socket_row(PFS_socket_class *klass)
328{
329 m_row.m_event_name.make_row(klass);
330
331 PFS_instance_wait_visitor visitor;
332 PFS_instance_iterator::visit_socket_instances(klass, &visitor);
333
334 get_normalizer(klass);
335 m_row.m_stat.set(m_normalizer, &visitor.m_stat);
336 m_row_exists= true;
337}
338
339void table_ews_global_by_event_name
340::make_idle_row(PFS_instr_class *klass)
341{
342 m_row.m_event_name.make_row(klass);
343
344 PFS_connection_wait_visitor visitor(klass);
345 PFS_connection_iterator::visit_global(false, /* hosts */
346 false, /* users */
347 false, /* accts */
348 true, /* threads */ &visitor);
349 get_normalizer(klass);
350 m_row.m_stat.set(m_normalizer, &visitor.m_stat);
351 m_row_exists= true;
352}
353
354int table_ews_global_by_event_name
355::read_row_values(TABLE *table, unsigned char *, Field **fields,
356 bool read_all)
357{
358 Field *f;
359
360 if (unlikely(! m_row_exists))
361 return HA_ERR_RECORD_DELETED;
362
363 /* Set the null bits */
364 DBUG_ASSERT(table->s->null_bytes == 0);
365
366 for (; (f= *fields) ; fields++)
367 {
368 if (read_all || bitmap_is_set(table->read_set, f->field_index))
369 {
370 switch(f->field_index)
371 {
372 case 0: /* EVENT_NAME */
373 m_row.m_event_name.set_field(f);
374 break;
375 default: /* 1, ... COUNT/SUM/MIN/AVG/MAX */
376 m_row.m_stat.set_field(f->field_index - 1, f);
377 break;
378 }
379 }
380 }
381
382 return 0;
383}
384
385