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_by_thread_by_event_name.cc
18 Table EVENTS_WAITS_SUMMARY_BY_HOST_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_by_thread_by_event_name.h"
27#include "pfs_global.h"
28#include "pfs_visitor.h"
29
30THR_LOCK table_ews_by_thread_by_event_name::m_table_lock;
31
32PFS_engine_table_share
33table_ews_by_thread_by_event_name::m_share=
34{
35 { C_STRING_WITH_LEN("events_waits_summary_by_thread_by_event_name") },
36 &pfs_truncatable_acl,
37 table_ews_by_thread_by_event_name::create,
38 NULL, /* write_row */
39 table_ews_by_thread_by_event_name::delete_all_rows,
40 NULL, /* get_row_count */
41 1000, /* records */
42 sizeof(pos_ews_by_thread_by_event_name),
43 &m_table_lock,
44 { C_STRING_WITH_LEN("CREATE TABLE events_waits_summary_by_thread_by_event_name("
45 "THREAD_ID BIGINT unsigned not null,"
46 "EVENT_NAME VARCHAR(128) not null,"
47 "COUNT_STAR BIGINT unsigned not null,"
48 "SUM_TIMER_WAIT BIGINT unsigned not null,"
49 "MIN_TIMER_WAIT BIGINT unsigned not null,"
50 "AVG_TIMER_WAIT BIGINT unsigned not null,"
51 "MAX_TIMER_WAIT BIGINT unsigned not null)") }
52};
53
54PFS_engine_table*
55table_ews_by_thread_by_event_name::create(void)
56{
57 return new table_ews_by_thread_by_event_name();
58}
59
60int
61table_ews_by_thread_by_event_name::delete_all_rows(void)
62{
63 reset_events_waits_by_thread();
64 return 0;
65}
66
67table_ews_by_thread_by_event_name::table_ews_by_thread_by_event_name()
68 : PFS_engine_table(&m_share, &m_pos),
69 m_row_exists(false), m_pos(), m_next_pos()
70{}
71
72void table_ews_by_thread_by_event_name::reset_position(void)
73{
74 m_pos.reset();
75 m_next_pos.reset();
76}
77
78int table_ews_by_thread_by_event_name::rnd_next(void)
79{
80 PFS_thread *thread;
81 PFS_instr_class *instr_class;
82
83 for (m_pos.set_at(&m_next_pos);
84 m_pos.has_more_thread();
85 m_pos.next_thread())
86 {
87 thread= &thread_array[m_pos.m_index_1];
88
89 /*
90 Important note: the thread scan is the outer loop (index 1),
91 to minimize the number of calls to atomic operations.
92 */
93 if (thread->m_lock.is_populated())
94 {
95 for ( ;
96 m_pos.has_more_view();
97 m_pos.next_view())
98 {
99 switch (m_pos.m_index_2)
100 {
101 case pos_ews_by_thread_by_event_name::VIEW_MUTEX:
102 instr_class= find_mutex_class(m_pos.m_index_3);
103 break;
104 case pos_ews_by_thread_by_event_name::VIEW_RWLOCK:
105 instr_class= find_rwlock_class(m_pos.m_index_3);
106 break;
107 case pos_ews_by_thread_by_event_name::VIEW_COND:
108 instr_class= find_cond_class(m_pos.m_index_3);
109 break;
110 case pos_ews_by_thread_by_event_name::VIEW_FILE:
111 instr_class= find_file_class(m_pos.m_index_3);
112 break;
113 case pos_ews_by_thread_by_event_name::VIEW_TABLE:
114 instr_class= find_table_class(m_pos.m_index_3);
115 break;
116 case pos_ews_by_thread_by_event_name::VIEW_SOCKET:
117 instr_class= find_socket_class(m_pos.m_index_3);
118 break;
119 case pos_ews_by_thread_by_event_name::VIEW_IDLE:
120 instr_class= find_idle_class(m_pos.m_index_3);
121 break;
122 default:
123 DBUG_ASSERT(false);
124 instr_class= NULL;
125 break;
126 }
127
128 if (instr_class != NULL)
129 {
130 make_row(thread, instr_class);
131 m_next_pos.set_after(&m_pos);
132 return 0;
133 }
134 }
135 }
136 }
137
138 return HA_ERR_END_OF_FILE;
139}
140
141int
142table_ews_by_thread_by_event_name::rnd_pos(const void *pos)
143{
144 PFS_thread *thread;
145 PFS_instr_class *instr_class;
146
147 set_position(pos);
148 DBUG_ASSERT(m_pos.m_index_1 < thread_max);
149
150 thread= &thread_array[m_pos.m_index_1];
151 if (! thread->m_lock.is_populated())
152 return HA_ERR_RECORD_DELETED;
153
154 switch (m_pos.m_index_2)
155 {
156 case pos_ews_by_thread_by_event_name::VIEW_MUTEX:
157 instr_class= find_mutex_class(m_pos.m_index_3);
158 break;
159 case pos_ews_by_thread_by_event_name::VIEW_RWLOCK:
160 instr_class= find_rwlock_class(m_pos.m_index_3);
161 break;
162 case pos_ews_by_thread_by_event_name::VIEW_COND:
163 instr_class= find_cond_class(m_pos.m_index_3);
164 break;
165 case pos_ews_by_thread_by_event_name::VIEW_FILE:
166 instr_class= find_file_class(m_pos.m_index_3);
167 break;
168 case pos_ews_by_thread_by_event_name::VIEW_TABLE:
169 instr_class= find_table_class(m_pos.m_index_3);
170 break;
171 case pos_ews_by_thread_by_event_name::VIEW_SOCKET:
172 instr_class= find_socket_class(m_pos.m_index_3);
173 break;
174 case pos_ews_by_thread_by_event_name::VIEW_IDLE:
175 instr_class= find_idle_class(m_pos.m_index_3);
176 break;
177 default:
178 DBUG_ASSERT(false);
179 instr_class= NULL;
180 }
181
182 if (instr_class)
183 {
184 make_row(thread, instr_class);
185 return 0;
186 }
187 return HA_ERR_RECORD_DELETED;
188}
189
190void table_ews_by_thread_by_event_name
191::make_row(PFS_thread *thread, PFS_instr_class *klass)
192{
193 pfs_lock lock;
194 m_row_exists= false;
195
196 /* Protect this reader against a thread termination */
197 thread->m_lock.begin_optimistic_lock(&lock);
198
199 m_row.m_thread_internal_id= thread->m_thread_internal_id;
200
201 m_row.m_event_name.make_row(klass);
202
203 PFS_connection_wait_visitor visitor(klass);
204 PFS_connection_iterator::visit_thread(thread, &visitor);
205
206 /*
207 If the aggregation for this class is deferred, then we must pull the
208 current wait stats from the instances associated with this thread.
209 */
210 if (klass->is_deferred())
211 {
212 /* Visit instances owned by this thread. Do not visit the class. */
213 PFS_instance_wait_visitor inst_visitor;
214 PFS_instance_iterator::visit_instances(klass, &inst_visitor,
215 thread, false);
216 /* Combine the deferred stats and global stats */
217 visitor.m_stat.aggregate(&inst_visitor.m_stat);
218 }
219
220 if (! thread->m_lock.end_optimistic_lock(&lock))
221 return;
222
223 m_row_exists= true;
224
225 get_normalizer(klass);
226 m_row.m_stat.set(m_normalizer, & visitor.m_stat);
227}
228
229int table_ews_by_thread_by_event_name
230::read_row_values(TABLE *table, unsigned char *, Field **fields,
231 bool read_all)
232{
233 Field *f;
234
235 if (unlikely(! m_row_exists))
236 return HA_ERR_RECORD_DELETED;
237
238 /* Set the null bits */
239 DBUG_ASSERT(table->s->null_bytes == 0);
240
241 for (; (f= *fields) ; fields++)
242 {
243 if (read_all || bitmap_is_set(table->read_set, f->field_index))
244 {
245 switch(f->field_index)
246 {
247 case 0: /* THREAD_ID */
248 set_field_ulonglong(f, m_row.m_thread_internal_id);
249 break;
250 case 1: /* EVENT_NAME */
251 m_row.m_event_name.set_field(f);
252 break;
253 default: /* 2, ... COUNT/SUM/MIN/AVG/MAX */
254 m_row.m_stat.set_field(f->field_index - 2, f);
255 break;
256 }
257 }
258 }
259
260 return 0;
261}
262
263