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 Street, Fifth Floor, Boston, MA 02110-1301, USA */
15
16/**
17 @file storage/perfschema/table_esms_by_account_by_event_name.cc
18 Table EVENTS_STATEMENTS_SUMMARY_BY_ACCOUNT_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_account_by_event_name.h"
27#include "pfs_global.h"
28#include "pfs_visitor.h"
29
30THR_LOCK table_esms_by_account_by_event_name::m_table_lock;
31
32PFS_engine_table_share
33table_esms_by_account_by_event_name::m_share=
34{
35 { C_STRING_WITH_LEN("events_statements_summary_by_account_by_event_name") },
36 &pfs_truncatable_acl,
37 table_esms_by_account_by_event_name::create,
38 NULL, /* write_row */
39 table_esms_by_account_by_event_name::delete_all_rows,
40 NULL, /* get_row_count */
41 1000, /* records */
42 sizeof(pos_esms_by_account_by_event_name),
43 &m_table_lock,
44 { C_STRING_WITH_LEN("CREATE TABLE events_statements_summary_by_account_by_event_name("
45 "USER CHAR(16) collate utf8_bin default null,"
46 "HOST CHAR(60) collate utf8_bin default null,"
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 "SUM_LOCK_TIME BIGINT unsigned not null,"
54 "SUM_ERRORS BIGINT unsigned not null,"
55 "SUM_WARNINGS BIGINT unsigned not null,"
56 "SUM_ROWS_AFFECTED BIGINT unsigned not null,"
57 "SUM_ROWS_SENT BIGINT unsigned not null,"
58 "SUM_ROWS_EXAMINED BIGINT unsigned not null,"
59 "SUM_CREATED_TMP_DISK_TABLES BIGINT unsigned not null,"
60 "SUM_CREATED_TMP_TABLES BIGINT unsigned not null,"
61 "SUM_SELECT_FULL_JOIN BIGINT unsigned not null,"
62 "SUM_SELECT_FULL_RANGE_JOIN BIGINT unsigned not null,"
63 "SUM_SELECT_RANGE BIGINT unsigned not null,"
64 "SUM_SELECT_RANGE_CHECK BIGINT unsigned not null,"
65 "SUM_SELECT_SCAN BIGINT unsigned not null,"
66 "SUM_SORT_MERGE_PASSES BIGINT unsigned not null,"
67 "SUM_SORT_RANGE BIGINT unsigned not null,"
68 "SUM_SORT_ROWS BIGINT unsigned not null,"
69 "SUM_SORT_SCAN BIGINT unsigned not null,"
70 "SUM_NO_INDEX_USED BIGINT unsigned not null,"
71 "SUM_NO_GOOD_INDEX_USED BIGINT unsigned not null)") }
72};
73
74PFS_engine_table*
75table_esms_by_account_by_event_name::create(void)
76{
77 return new table_esms_by_account_by_event_name();
78}
79
80int
81table_esms_by_account_by_event_name::delete_all_rows(void)
82{
83 reset_events_statements_by_thread();
84 reset_events_statements_by_account();
85 return 0;
86}
87
88table_esms_by_account_by_event_name::table_esms_by_account_by_event_name()
89 : PFS_engine_table(&m_share, &m_pos),
90 m_row_exists(false), m_pos(), m_next_pos()
91{}
92
93void table_esms_by_account_by_event_name::reset_position(void)
94{
95 m_pos.reset();
96 m_next_pos.reset();
97}
98
99int table_esms_by_account_by_event_name::rnd_init(bool scan)
100{
101 m_normalizer= time_normalizer::get(statement_timer);
102 return 0;
103}
104
105int table_esms_by_account_by_event_name::rnd_next(void)
106{
107 PFS_account *account;
108 PFS_statement_class *statement_class;
109
110 for (m_pos.set_at(&m_next_pos);
111 m_pos.has_more_account();
112 m_pos.next_account())
113 {
114 account= &account_array[m_pos.m_index_1];
115 if (account->m_lock.is_populated())
116 {
117 statement_class= find_statement_class(m_pos.m_index_2);
118 if (statement_class)
119 {
120 make_row(account, statement_class);
121 m_next_pos.set_after(&m_pos);
122 return 0;
123 }
124 }
125 }
126
127 return HA_ERR_END_OF_FILE;
128}
129
130int
131table_esms_by_account_by_event_name::rnd_pos(const void *pos)
132{
133 PFS_account *account;
134 PFS_statement_class *statement_class;
135
136 set_position(pos);
137 DBUG_ASSERT(m_pos.m_index_1 < account_max);
138
139 account= &account_array[m_pos.m_index_1];
140 if (! account->m_lock.is_populated())
141 return HA_ERR_RECORD_DELETED;
142
143 statement_class= find_statement_class(m_pos.m_index_2);
144 if (statement_class)
145 {
146 make_row(account, statement_class);
147 return 0;
148 }
149
150 return HA_ERR_RECORD_DELETED;
151}
152
153void table_esms_by_account_by_event_name
154::make_row(PFS_account *account, PFS_statement_class *klass)
155{
156 pfs_lock lock;
157 m_row_exists= false;
158
159 if (klass->is_mutable())
160 return;
161
162 account->m_lock.begin_optimistic_lock(&lock);
163
164 if (m_row.m_account.make_row(account))
165 return;
166
167 m_row.m_event_name.make_row(klass);
168
169 PFS_connection_statement_visitor visitor(klass);
170 PFS_connection_iterator::visit_account(account, true, & visitor);
171
172 if (! account->m_lock.end_optimistic_lock(&lock))
173 return;
174
175 m_row_exists= true;
176 m_row.m_stat.set(m_normalizer, & visitor.m_stat);
177}
178
179int table_esms_by_account_by_event_name
180::read_row_values(TABLE *table, unsigned char *buf, Field **fields,
181 bool read_all)
182{
183 Field *f;
184
185 if (unlikely(! m_row_exists))
186 return HA_ERR_RECORD_DELETED;
187
188 /* Set the null bits */
189 DBUG_ASSERT(table->s->null_bytes == 1);
190 buf[0]= 0;
191
192 for (; (f= *fields) ; fields++)
193 {
194 if (read_all || bitmap_is_set(table->read_set, f->field_index))
195 {
196 switch(f->field_index)
197 {
198 case 0: /* USER */
199 case 1: /* HOST */
200 m_row.m_account.set_field(f->field_index, f);
201 break;
202 case 2: /* EVENT_NAME */
203 m_row.m_event_name.set_field(f);
204 break;
205 default: /* 3, ... COUNT/SUM/MIN/AVG/MAX */
206 m_row.m_stat.set_field(f->field_index - 3, f);
207 break;
208 }
209 }
210 }
211
212 return 0;
213}
214
215