1/* Copyright (c) 2008, 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 Foundation,
14 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15
16/**
17 @file storage/perfschema/table_file_summary.cc
18 Table FILE_SUMMARY_BY_INSTANCE (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_file_summary_by_instance.h"
27#include "pfs_global.h"
28
29THR_LOCK table_file_summary_by_instance::m_table_lock;
30
31PFS_engine_table_share
32table_file_summary_by_instance::m_share=
33{
34 { C_STRING_WITH_LEN("file_summary_by_instance") },
35 &pfs_truncatable_acl,
36 &table_file_summary_by_instance::create,
37 NULL, /* write_row */
38 table_file_summary_by_instance::delete_all_rows,
39 NULL, /* get_row_count */
40 1000, /* records */
41 sizeof(PFS_simple_index),
42 &m_table_lock,
43 { C_STRING_WITH_LEN("CREATE TABLE file_summary_by_instance("
44 "FILE_NAME VARCHAR(512) not null,"
45 "EVENT_NAME VARCHAR(128) not null,"
46 "OBJECT_INSTANCE_BEGIN BIGINT unsigned 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 "COUNT_READ BIGINT unsigned not null,"
53 "SUM_TIMER_READ BIGINT unsigned not null,"
54 "MIN_TIMER_READ BIGINT unsigned not null,"
55 "AVG_TIMER_READ BIGINT unsigned not null,"
56 "MAX_TIMER_READ BIGINT unsigned not null,"
57 "SUM_NUMBER_OF_BYTES_READ BIGINT 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 "SUM_NUMBER_OF_BYTES_WRITE BIGINT not null,"
64 "COUNT_MISC BIGINT unsigned not null,"
65 "SUM_TIMER_MISC BIGINT unsigned not null,"
66 "MIN_TIMER_MISC BIGINT unsigned not null,"
67 "AVG_TIMER_MISC BIGINT unsigned not null,"
68 "MAX_TIMER_MISC BIGINT unsigned not null)") }
69};
70
71PFS_engine_table* table_file_summary_by_instance::create(void)
72{
73 return new table_file_summary_by_instance();
74}
75
76int table_file_summary_by_instance::delete_all_rows(void)
77{
78 reset_file_instance_io();
79 return 0;
80}
81
82table_file_summary_by_instance::table_file_summary_by_instance()
83 : PFS_engine_table(&m_share, &m_pos),
84 m_row_exists(false), m_pos(0), m_next_pos(0)
85{}
86
87void table_file_summary_by_instance::reset_position(void)
88{
89 m_pos.m_index= 0;
90 m_next_pos.m_index= 0;
91}
92
93int table_file_summary_by_instance::rnd_next(void)
94{
95 PFS_file *pfs;
96
97 for (m_pos.set_at(&m_next_pos);
98 m_pos.m_index < file_max;
99 m_pos.next())
100 {
101 pfs= &file_array[m_pos.m_index];
102 if (pfs->m_lock.is_populated())
103 {
104 make_row(pfs);
105 m_next_pos.set_after(&m_pos);
106 return 0;
107 }
108 }
109
110 return HA_ERR_END_OF_FILE;
111}
112
113int table_file_summary_by_instance::rnd_pos(const void *pos)
114{
115 PFS_file *pfs;
116
117 set_position(pos);
118 DBUG_ASSERT(m_pos.m_index < file_max);
119 pfs= &file_array[m_pos.m_index];
120
121 if (! pfs->m_lock.is_populated())
122 return HA_ERR_RECORD_DELETED;
123
124 make_row(pfs);
125 return 0;
126}
127
128/**
129 Build a row.
130 @param pfs the file the cursor is reading
131*/
132void table_file_summary_by_instance::make_row(PFS_file *pfs)
133{
134 pfs_lock lock;
135 PFS_file_class *safe_class;
136
137 m_row_exists= false;
138
139 /* Protect this reader against a file delete */
140 pfs->m_lock.begin_optimistic_lock(&lock);
141
142 safe_class= sanitize_file_class(pfs->m_class);
143 if (unlikely(safe_class == NULL))
144 return;
145
146 m_row.m_filename= pfs->m_filename;
147 m_row.m_filename_length= pfs->m_filename_length;
148 m_row.m_event_name.make_row(safe_class);
149 m_row.m_identity= pfs->m_identity;
150
151 time_normalizer *normalizer= time_normalizer::get(wait_timer);
152
153 /* Collect timer and byte count stats */
154 m_row.m_io_stat.set(normalizer, &pfs->m_file_stat.m_io_stat);
155
156 if (pfs->m_lock.end_optimistic_lock(&lock))
157 m_row_exists= true;
158}
159
160int table_file_summary_by_instance::read_row_values(TABLE *table,
161 unsigned char *,
162 Field **fields,
163 bool read_all)
164{
165 Field *f;
166
167 if (unlikely(! m_row_exists))
168 return HA_ERR_RECORD_DELETED;
169
170 /* Set the null bits */
171 DBUG_ASSERT(table->s->null_bytes == 0);
172
173 for (; (f= *fields) ; fields++)
174 {
175 if (read_all || bitmap_is_set(table->read_set, f->field_index))
176 {
177 switch(f->field_index)
178 {
179 case 0: /* FILE_NAME */
180 set_field_varchar_utf8(f, m_row.m_filename, m_row.m_filename_length);
181 break;
182 case 1: /* EVENT_NAME */
183 m_row.m_event_name.set_field(f);
184 break;
185 case 2: /* OBJECT_INSTANCE */
186 set_field_ulonglong(f, (ulonglong)m_row.m_identity);
187 break;
188
189 case 3:/* COUNT_STAR */
190 set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_count);
191 break;
192 case 4:/* SUM_TIMER_WAIT */
193 set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_sum);
194 break;
195 case 5: /* MIN_TIMER_WAIT */
196 set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_min);
197 break;
198 case 6: /* AVG_TIMER_WAIT */
199 set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_avg);
200 break;
201 case 7: /* MAX_TIMER_WAIT */
202 set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_max);
203 break;
204
205 case 8: /* COUNT_READ */
206 set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_count);
207 break;
208 case 9: /* SUM_TIMER_READ */
209 set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_sum);
210 break;
211 case 10: /* MIN_TIMER_READ */
212 set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_min);
213 break;
214 case 11: /* AVG_TIMER_READ */
215 set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_avg);
216 break;
217 case 12: /* MAX_TIMER_READ */
218 set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_max);
219 break;
220 case 13: /* SUM_NUMBER_OF_BYTES_READ */
221 set_field_ulonglong(f, m_row.m_io_stat.m_read.m_bytes);
222 break;
223
224 case 14: /* COUNT_WRITE */
225 set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_count);
226 break;
227 case 15: /* SUM_TIMER_WRITE */
228 set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_sum);
229 break;
230 case 16: /* MIN_TIMER_WRITE */
231 set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_min);
232 break;
233 case 17: /* AVG_TIMER_WRITE */
234 set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_avg);
235 break;
236 case 18: /* MAX_TIMER_WRITE */
237 set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_max);
238 break;
239 case 19: /* SUM_NUMBER_OF_BYTES_WRITE */
240 set_field_ulonglong(f, m_row.m_io_stat.m_write.m_bytes);
241 break;
242
243 case 20: /* COUNT_MISC */
244 set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_count);
245 break;
246 case 21: /* SUM_TIMER_MISC */
247 set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_sum);
248 break;
249 case 22: /* MIN_TIMER_MISC */
250 set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_min);
251 break;
252 case 23: /* AVG_TIMER_MISC */
253 set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_avg);
254 break;
255 case 24: /* MAX_TIMER_MISC */
256 set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_max);
257 break;
258 default:
259 DBUG_ASSERT(false);
260 }
261 }
262 }
263
264 return 0;
265}
266
267