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