1/* Copyright (c) 2010, 2016, 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_digest.cc
18 Table EVENTS_STATEMENTS_SUMMARY_GLOBAL_BY_DIGEST (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_digest.h"
27#include "pfs_global.h"
28#include "pfs_instr.h"
29#include "pfs_timer.h"
30#include "pfs_visitor.h"
31#include "table_esms_by_digest.h"
32#include "pfs_digest.h"
33
34THR_LOCK table_esms_by_digest::m_table_lock;
35
36PFS_engine_table_share
37table_esms_by_digest::m_share=
38{
39 { C_STRING_WITH_LEN("events_statements_summary_by_digest") },
40 &pfs_truncatable_acl,
41 table_esms_by_digest::create,
42 NULL, /* write_row */
43 table_esms_by_digest::delete_all_rows,
44 NULL, /* get_row_count */
45 1000, /* records */
46 sizeof(PFS_simple_index),
47 &m_table_lock,
48 { C_STRING_WITH_LEN("CREATE TABLE events_statements_summary_by_digest("
49 "SCHEMA_NAME VARCHAR(64),"
50 "DIGEST VARCHAR(32),"
51 "DIGEST_TEXT LONGTEXT,"
52 "COUNT_STAR BIGINT unsigned not null,"
53 "SUM_TIMER_WAIT BIGINT unsigned not null,"
54 "MIN_TIMER_WAIT BIGINT unsigned not null,"
55 "AVG_TIMER_WAIT BIGINT unsigned not null,"
56 "MAX_TIMER_WAIT BIGINT unsigned not null,"
57 "SUM_LOCK_TIME BIGINT unsigned not null,"
58 "SUM_ERRORS BIGINT unsigned not null,"
59 "SUM_WARNINGS BIGINT unsigned not null,"
60 "SUM_ROWS_AFFECTED BIGINT unsigned not null,"
61 "SUM_ROWS_SENT BIGINT unsigned not null,"
62 "SUM_ROWS_EXAMINED BIGINT unsigned not null,"
63 "SUM_CREATED_TMP_DISK_TABLES BIGINT unsigned not null,"
64 "SUM_CREATED_TMP_TABLES BIGINT unsigned not null,"
65 "SUM_SELECT_FULL_JOIN BIGINT unsigned not null,"
66 "SUM_SELECT_FULL_RANGE_JOIN BIGINT unsigned not null,"
67 "SUM_SELECT_RANGE BIGINT unsigned not null,"
68 "SUM_SELECT_RANGE_CHECK BIGINT unsigned not null,"
69 "SUM_SELECT_SCAN BIGINT unsigned not null,"
70 "SUM_SORT_MERGE_PASSES BIGINT unsigned not null,"
71 "SUM_SORT_RANGE BIGINT unsigned not null,"
72 "SUM_SORT_ROWS BIGINT unsigned not null,"
73 "SUM_SORT_SCAN BIGINT unsigned not null,"
74 "SUM_NO_INDEX_USED BIGINT unsigned not null,"
75 "SUM_NO_GOOD_INDEX_USED BIGINT unsigned not null,"
76 "FIRST_SEEN TIMESTAMP(0) NOT NULL default 0,"
77 "LAST_SEEN TIMESTAMP(0) NOT NULL default 0)") }
78};
79
80PFS_engine_table*
81table_esms_by_digest::create(void)
82{
83 return new table_esms_by_digest();
84}
85
86int
87table_esms_by_digest::delete_all_rows(void)
88{
89 reset_esms_by_digest();
90 return 0;
91}
92
93table_esms_by_digest::table_esms_by_digest()
94 : PFS_engine_table(&m_share, &m_pos),
95 m_row_exists(false), m_pos(0), m_next_pos(0)
96{}
97
98void table_esms_by_digest::reset_position(void)
99{
100 m_pos= 0;
101 m_next_pos= 0;
102}
103
104int table_esms_by_digest::rnd_next(void)
105{
106 PFS_statements_digest_stat* digest_stat;
107
108 if (statements_digest_stat_array == NULL)
109 return HA_ERR_END_OF_FILE;
110
111 for (m_pos.set_at(&m_next_pos);
112 m_pos.m_index < digest_max;
113 m_pos.next())
114 {
115 digest_stat= &statements_digest_stat_array[m_pos.m_index];
116 if (digest_stat->m_lock.is_populated())
117 {
118 if (digest_stat->m_first_seen != 0)
119 {
120 make_row(digest_stat);
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_digest::rnd_pos(const void *pos)
132{
133 PFS_statements_digest_stat* digest_stat;
134
135 if (statements_digest_stat_array == NULL)
136 return HA_ERR_END_OF_FILE;
137
138 set_position(pos);
139 digest_stat= &statements_digest_stat_array[m_pos.m_index];
140
141 if (digest_stat->m_lock.is_populated())
142 {
143 if (digest_stat->m_first_seen != 0)
144 {
145 make_row(digest_stat);
146 return 0;
147 }
148 }
149
150 return HA_ERR_RECORD_DELETED;
151}
152
153
154void table_esms_by_digest::make_row(PFS_statements_digest_stat* digest_stat)
155{
156 m_row_exists= false;
157 m_row.m_first_seen= digest_stat->m_first_seen;
158 m_row.m_last_seen= digest_stat->m_last_seen;
159 m_row.m_digest.make_row(digest_stat);
160
161 /*
162 Get statements stats.
163 */
164 time_normalizer *normalizer= time_normalizer::get(statement_timer);
165 m_row.m_stat.set(normalizer, & digest_stat->m_stat);
166
167 m_row_exists= true;
168}
169
170int table_esms_by_digest
171::read_row_values(TABLE *table, unsigned char *buf, Field **fields,
172 bool read_all)
173{
174 Field *f;
175
176 if (unlikely(! m_row_exists))
177 return HA_ERR_RECORD_DELETED;
178
179 /*
180 Set the null bits. It indicates how many fields could be null
181 in the table.
182 */
183 DBUG_ASSERT(table->s->null_bytes == 1);
184 buf[0]= 0;
185
186 for (; (f= *fields) ; fields++)
187 {
188 if (read_all || bitmap_is_set(table->read_set, f->field_index))
189 {
190 switch(f->field_index)
191 {
192 case 0: /* SCHEMA_NAME */
193 case 1: /* DIGEST */
194 case 2: /* DIGEST_TEXT */
195 m_row.m_digest.set_field(f->field_index, f);
196 break;
197 case 27: /* FIRST_SEEN */
198 set_field_timestamp(f, m_row.m_first_seen);
199 break;
200 case 28: /* LAST_SEEN */
201 set_field_timestamp(f, m_row.m_last_seen);
202 break;
203 default: /* 3, ... COUNT/SUM/MIN/AVG/MAX */
204 m_row.m_stat.set_field(f->field_index - 3, f);
205 break;
206 }
207 }
208 }
209
210 return 0;
211}
212
213