1/* Copyright (c) 2010, 2013, 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_esms_by_thread_by_event_name.cc
18 Table EVENTS_STATEMENTS_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_esms_by_thread_by_event_name.h"
27#include "pfs_global.h"
28#include "pfs_visitor.h"
29
30THR_LOCK table_esms_by_thread_by_event_name::m_table_lock;
31
32PFS_engine_table_share
33table_esms_by_thread_by_event_name::m_share=
34{
35 { C_STRING_WITH_LEN("events_statements_summary_by_thread_by_event_name") },
36 &pfs_truncatable_acl,
37 table_esms_by_thread_by_event_name::create,
38 NULL, /* write_row */
39 table_esms_by_thread_by_event_name::delete_all_rows,
40 NULL, /* get_row_count */
41 1000, /* records */
42 sizeof(pos_esms_by_thread_by_event_name),
43 &m_table_lock,
44 { C_STRING_WITH_LEN("CREATE TABLE events_statements_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 "SUM_LOCK_TIME BIGINT unsigned not null,"
53 "SUM_ERRORS BIGINT unsigned not null,"
54 "SUM_WARNINGS BIGINT unsigned not null,"
55 "SUM_ROWS_AFFECTED BIGINT unsigned not null,"
56 "SUM_ROWS_SENT BIGINT unsigned not null,"
57 "SUM_ROWS_EXAMINED BIGINT unsigned not null,"
58 "SUM_CREATED_TMP_DISK_TABLES BIGINT unsigned not null,"
59 "SUM_CREATED_TMP_TABLES BIGINT unsigned not null,"
60 "SUM_SELECT_FULL_JOIN BIGINT unsigned not null,"
61 "SUM_SELECT_FULL_RANGE_JOIN BIGINT unsigned not null,"
62 "SUM_SELECT_RANGE BIGINT unsigned not null,"
63 "SUM_SELECT_RANGE_CHECK BIGINT unsigned not null,"
64 "SUM_SELECT_SCAN BIGINT unsigned not null,"
65 "SUM_SORT_MERGE_PASSES BIGINT unsigned not null,"
66 "SUM_SORT_RANGE BIGINT unsigned not null,"
67 "SUM_SORT_ROWS BIGINT unsigned not null,"
68 "SUM_SORT_SCAN BIGINT unsigned not null,"
69 "SUM_NO_INDEX_USED BIGINT unsigned not null,"
70 "SUM_NO_GOOD_INDEX_USED BIGINT unsigned not null)") }
71};
72
73PFS_engine_table*
74table_esms_by_thread_by_event_name::create(void)
75{
76 return new table_esms_by_thread_by_event_name();
77}
78
79int
80table_esms_by_thread_by_event_name::delete_all_rows(void)
81{
82 reset_events_statements_by_thread();
83 return 0;
84}
85
86table_esms_by_thread_by_event_name::table_esms_by_thread_by_event_name()
87 : PFS_engine_table(&m_share, &m_pos),
88 m_row_exists(false), m_pos(), m_next_pos()
89{}
90
91void table_esms_by_thread_by_event_name::reset_position(void)
92{
93 m_pos.reset();
94 m_next_pos.reset();
95}
96
97int table_esms_by_thread_by_event_name::rnd_init(bool scan)
98{
99 m_normalizer= time_normalizer::get(statement_timer);
100 return 0;
101}
102
103int table_esms_by_thread_by_event_name::rnd_next(void)
104{
105 PFS_thread *thread;
106 PFS_statement_class *statement_class;
107
108 for (m_pos.set_at(&m_next_pos);
109 m_pos.has_more_thread();
110 m_pos.next_thread())
111 {
112 thread= &thread_array[m_pos.m_index_1];
113
114 /*
115 Important note: the thread scan is the outer loop (index 1),
116 to minimize the number of calls to atomic operations.
117 */
118 if (thread->m_lock.is_populated())
119 {
120 statement_class= find_statement_class(m_pos.m_index_2);
121 if (statement_class)
122 {
123 make_row(thread, statement_class);
124 m_next_pos.set_after(&m_pos);
125 return 0;
126 }
127 }
128 }
129
130 return HA_ERR_END_OF_FILE;
131}
132
133int
134table_esms_by_thread_by_event_name::rnd_pos(const void *pos)
135{
136 PFS_thread *thread;
137 PFS_statement_class *statement_class;
138
139 set_position(pos);
140 DBUG_ASSERT(m_pos.m_index_1 < thread_max);
141
142 thread= &thread_array[m_pos.m_index_1];
143 if (! thread->m_lock.is_populated())
144 return HA_ERR_RECORD_DELETED;
145
146 statement_class= find_statement_class(m_pos.m_index_2);
147 if (statement_class)
148 {
149 make_row(thread, statement_class);
150 return 0;
151 }
152
153 return HA_ERR_RECORD_DELETED;
154}
155
156void table_esms_by_thread_by_event_name
157::make_row(PFS_thread *thread, PFS_statement_class *klass)
158{
159 pfs_lock lock;
160 m_row_exists= false;
161
162 if (klass->is_mutable())
163 return;
164
165 /* Protect this reader against a thread termination */
166 thread->m_lock.begin_optimistic_lock(&lock);
167
168 m_row.m_thread_internal_id= thread->m_thread_internal_id;
169
170 m_row.m_event_name.make_row(klass);
171
172 PFS_connection_statement_visitor visitor(klass);
173 PFS_connection_iterator::visit_thread(thread, & visitor);
174
175 if (! thread->m_lock.end_optimistic_lock(&lock))
176 return;
177
178 m_row_exists= true;
179 m_row.m_stat.set(m_normalizer, & visitor.m_stat);
180}
181
182int table_esms_by_thread_by_event_name
183::read_row_values(TABLE *table, unsigned char *, Field **fields,
184 bool read_all)
185{
186 Field *f;
187
188 if (unlikely(! m_row_exists))
189 return HA_ERR_RECORD_DELETED;
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: /* THREAD_ID */
201 set_field_ulonglong(f, m_row.m_thread_internal_id);
202 break;
203 case 1: /* EVENT_NAME */
204 m_row.m_event_name.set_field(f);
205 break;
206 default: /* 2, ... COUNT/SUM/MIN/AVG/MAX */
207 m_row.m_stat.set_field(f->field_index - 2, f);
208 break;
209 }
210 }
211 }
212
213 return 0;
214}
215
216