1/* Copyright (c) 2010, 2012, 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_tiws_by_table.cc
18 Table TABLE_IO_WAITS_SUMMARY_BY_TABLE (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_tiws_by_table.h"
27#include "pfs_global.h"
28#include "pfs_visitor.h"
29
30THR_LOCK table_tiws_by_table::m_table_lock;
31
32PFS_engine_table_share
33table_tiws_by_table::m_share=
34{
35 { C_STRING_WITH_LEN("table_io_waits_summary_by_table") },
36 &pfs_truncatable_acl,
37 table_tiws_by_table::create,
38 NULL, /* write_row */
39 table_tiws_by_table::delete_all_rows,
40 NULL, /* get_row_count */
41 1000, /* records */
42 sizeof(PFS_simple_index),
43 &m_table_lock,
44 { C_STRING_WITH_LEN("CREATE TABLE table_io_waits_summary_by_table("
45 "OBJECT_TYPE VARCHAR(64),"
46 "OBJECT_SCHEMA VARCHAR(64),"
47 "OBJECT_NAME VARCHAR(64),"
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 "COUNT_READ BIGINT unsigned not null,"
54 "SUM_TIMER_READ BIGINT unsigned not null,"
55 "MIN_TIMER_READ BIGINT unsigned not null,"
56 "AVG_TIMER_READ BIGINT unsigned not null,"
57 "MAX_TIMER_READ BIGINT unsigned not null,"
58 "COUNT_WRITE BIGINT unsigned not null,"
59 "SUM_TIMER_WRITE BIGINT unsigned not null,"
60 "MIN_TIMER_WRITE BIGINT unsigned not null,"
61 "AVG_TIMER_WRITE BIGINT unsigned not null,"
62 "MAX_TIMER_WRITE BIGINT unsigned not null,"
63 "COUNT_FETCH BIGINT unsigned not null,"
64 "SUM_TIMER_FETCH BIGINT unsigned not null,"
65 "MIN_TIMER_FETCH BIGINT unsigned not null,"
66 "AVG_TIMER_FETCH BIGINT unsigned not null,"
67 "MAX_TIMER_FETCH BIGINT unsigned not null,"
68 "COUNT_INSERT BIGINT unsigned not null,"
69 "SUM_TIMER_INSERT BIGINT unsigned not null,"
70 "MIN_TIMER_INSERT BIGINT unsigned not null,"
71 "AVG_TIMER_INSERT BIGINT unsigned not null,"
72 "MAX_TIMER_INSERT BIGINT unsigned not null,"
73 "COUNT_UPDATE BIGINT unsigned not null,"
74 "SUM_TIMER_UPDATE BIGINT unsigned not null,"
75 "MIN_TIMER_UPDATE BIGINT unsigned not null,"
76 "AVG_TIMER_UPDATE BIGINT unsigned not null,"
77 "MAX_TIMER_UPDATE BIGINT unsigned not null,"
78 "COUNT_DELETE BIGINT unsigned not null,"
79 "SUM_TIMER_DELETE BIGINT unsigned not null,"
80 "MIN_TIMER_DELETE BIGINT unsigned not null,"
81 "AVG_TIMER_DELETE BIGINT unsigned not null,"
82 "MAX_TIMER_DELETE BIGINT unsigned not null)") }
83};
84
85PFS_engine_table*
86table_tiws_by_table::create(void)
87{
88 return new table_tiws_by_table();
89}
90
91int
92table_tiws_by_table::delete_all_rows(void)
93{
94 reset_table_io_waits_by_table_handle();
95 reset_table_io_waits_by_table();
96 return 0;
97}
98
99table_tiws_by_table::table_tiws_by_table()
100 : PFS_engine_table(&m_share, &m_pos),
101 m_row_exists(false), m_pos(0), m_next_pos(0)
102{}
103
104void table_tiws_by_table::reset_position(void)
105{
106 m_pos.m_index= 0;
107 m_next_pos.m_index= 0;
108}
109
110int table_tiws_by_table::rnd_init(bool scan)
111{
112 m_normalizer= time_normalizer::get(wait_timer);
113 return 0;
114}
115
116int table_tiws_by_table::rnd_next(void)
117{
118 PFS_table_share *table_share;
119
120 for (m_pos.set_at(&m_next_pos);
121 m_pos.m_index < table_share_max;
122 m_pos.m_index++)
123 {
124 table_share= &table_share_array[m_pos.m_index];
125 if (table_share->m_lock.is_populated())
126 {
127 make_row(table_share);
128 m_next_pos.set_after(&m_pos);
129 return 0;
130 }
131 }
132
133 return HA_ERR_END_OF_FILE;
134}
135
136int
137table_tiws_by_table::rnd_pos(const void *pos)
138{
139 PFS_table_share *table_share;
140
141 set_position(pos);
142
143 table_share= &table_share_array[m_pos.m_index];
144 if (table_share->m_lock.is_populated())
145 {
146 make_row(table_share);
147 return 0;
148 }
149
150 return HA_ERR_RECORD_DELETED;
151}
152
153void table_tiws_by_table::make_row(PFS_table_share *share)
154{
155 pfs_lock lock;
156
157 m_row_exists= false;
158
159 share->m_lock.begin_optimistic_lock(&lock);
160
161 if (m_row.m_object.make_row(share))
162 return;
163
164 PFS_table_io_stat_visitor visitor;
165 PFS_object_iterator::visit_tables(share, & visitor);
166
167 if (! share->m_lock.end_optimistic_lock(&lock))
168 return;
169
170 m_row_exists= true;
171 m_row.m_stat.set(m_normalizer, &visitor.m_stat);
172}
173
174int table_tiws_by_table::read_row_values(TABLE *table,
175 unsigned char *buf,
176 Field **fields,
177 bool read_all)
178{
179 Field *f;
180
181 if (unlikely(! m_row_exists))
182 return HA_ERR_RECORD_DELETED;
183
184 /* Set the null bits */
185 DBUG_ASSERT(table->s->null_bytes == 1);
186 buf[0]= 0;
187
188 for (; (f= *fields) ; fields++)
189 {
190 if (read_all || bitmap_is_set(table->read_set, f->field_index))
191 {
192 switch(f->field_index)
193 {
194 case 0: /* OBJECT_TYPE */
195 case 1: /* SCHEMA_NAME */
196 case 2: /* OBJECT_NAME */
197 m_row.m_object.set_field(f->field_index, f);
198 break;
199 case 3: /* COUNT_STAR */
200 set_field_ulonglong(f, m_row.m_stat.m_all.m_count);
201 break;
202 case 4: /* SUM */
203 set_field_ulonglong(f, m_row.m_stat.m_all.m_sum);
204 break;
205 case 5: /* MIN */
206 set_field_ulonglong(f, m_row.m_stat.m_all.m_min);
207 break;
208 case 6: /* AVG */
209 set_field_ulonglong(f, m_row.m_stat.m_all.m_avg);
210 break;
211 case 7: /* MAX */
212 set_field_ulonglong(f, m_row.m_stat.m_all.m_max);
213 break;
214 case 8: /* COUNT_READ */
215 set_field_ulonglong(f, m_row.m_stat.m_all_read.m_count);
216 break;
217 case 9: /* SUM_READ */
218 set_field_ulonglong(f, m_row.m_stat.m_all_read.m_sum);
219 break;
220 case 10: /* MIN_READ */
221 set_field_ulonglong(f, m_row.m_stat.m_all_read.m_min);
222 break;
223 case 11: /* AVG_READ */
224 set_field_ulonglong(f, m_row.m_stat.m_all_read.m_avg);
225 break;
226 case 12: /* MAX_READ */
227 set_field_ulonglong(f, m_row.m_stat.m_all_read.m_max);
228 break;
229 case 13: /* COUNT_WRITE */
230 set_field_ulonglong(f, m_row.m_stat.m_all_write.m_count);
231 break;
232 case 14: /* SUM_WRITE */
233 set_field_ulonglong(f, m_row.m_stat.m_all_write.m_sum);
234 break;
235 case 15: /* MIN_WRITE */
236 set_field_ulonglong(f, m_row.m_stat.m_all_write.m_min);
237 break;
238 case 16: /* AVG_WRITE */
239 set_field_ulonglong(f, m_row.m_stat.m_all_write.m_avg);
240 break;
241 case 17: /* MAX_WRITE */
242 set_field_ulonglong(f, m_row.m_stat.m_all_write.m_max);
243 break;
244 case 18: /* COUNT_FETCH */
245 set_field_ulonglong(f, m_row.m_stat.m_fetch.m_count);
246 break;
247 case 19: /* SUM_FETCH */
248 set_field_ulonglong(f, m_row.m_stat.m_fetch.m_sum);
249 break;
250 case 20: /* MIN_FETCH */
251 set_field_ulonglong(f, m_row.m_stat.m_fetch.m_min);
252 break;
253 case 21: /* AVG_FETCH */
254 set_field_ulonglong(f, m_row.m_stat.m_fetch.m_avg);
255 break;
256 case 22: /* MAX_FETCH */
257 set_field_ulonglong(f, m_row.m_stat.m_fetch.m_max);
258 break;
259 case 23: /* COUNT_INSERT */
260 set_field_ulonglong(f, m_row.m_stat.m_insert.m_count);
261 break;
262 case 24: /* SUM_INSERT */
263 set_field_ulonglong(f, m_row.m_stat.m_insert.m_sum);
264 break;
265 case 25: /* MIN_INSERT */
266 set_field_ulonglong(f, m_row.m_stat.m_insert.m_min);
267 break;
268 case 26: /* AVG_INSERT */
269 set_field_ulonglong(f, m_row.m_stat.m_insert.m_avg);
270 break;
271 case 27: /* MAX_INSERT */
272 set_field_ulonglong(f, m_row.m_stat.m_insert.m_max);
273 break;
274 case 28: /* COUNT_UPDATE */
275 set_field_ulonglong(f, m_row.m_stat.m_update.m_count);
276 break;
277 case 29: /* SUM_UPDATE */
278 set_field_ulonglong(f, m_row.m_stat.m_update.m_sum);
279 break;
280 case 30: /* MIN_UPDATE */
281 set_field_ulonglong(f, m_row.m_stat.m_update.m_min);
282 break;
283 case 31: /* AVG_UPDATE */
284 set_field_ulonglong(f, m_row.m_stat.m_update.m_avg);
285 break;
286 case 32: /* MAX_UPDATE */
287 set_field_ulonglong(f, m_row.m_stat.m_update.m_max);
288 break;
289 case 33: /* COUNT_DELETE */
290 set_field_ulonglong(f, m_row.m_stat.m_delete.m_count);
291 break;
292 case 34: /* SUM_DELETE */
293 set_field_ulonglong(f, m_row.m_stat.m_delete.m_sum);
294 break;
295 case 35: /* MIN_DELETE */
296 set_field_ulonglong(f, m_row.m_stat.m_delete.m_min);
297 break;
298 case 36: /* AVG_DELETE */
299 set_field_ulonglong(f, m_row.m_stat.m_delete.m_avg);
300 break;
301 case 37: /* MAX_DELETE */
302 set_field_ulonglong(f, m_row.m_stat.m_delete.m_max);
303 break;
304 default:
305 DBUG_ASSERT(false);
306 }
307 }
308 }
309
310 return 0;
311}
312
313