1/* Copyright (c) 2008, 2010, 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_events_waits_summary.cc
18 Table EVENTS_WAITS_SUMMARY_BY_xxx (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_events_waits_summary.h"
27#include "pfs_global.h"
28
29THR_LOCK table_events_waits_summary_by_instance::m_table_lock;
30
31PFS_engine_table_share
32table_events_waits_summary_by_instance::m_share=
33{
34 { C_STRING_WITH_LEN("events_waits_summary_by_instance") },
35 &pfs_truncatable_acl,
36 &table_events_waits_summary_by_instance::create,
37 NULL, /* write_row */
38 &table_events_waits_summary_by_instance::delete_all_rows,
39 NULL, /* get_row_count */
40 1000, /* records */
41 sizeof(pos_all_instr),
42 &m_table_lock,
43 { C_STRING_WITH_LEN("CREATE TABLE events_waits_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};
52
53PFS_engine_table* table_events_waits_summary_by_instance::create(void)
54{
55 return new table_events_waits_summary_by_instance();
56}
57
58int table_events_waits_summary_by_instance::delete_all_rows(void)
59{
60 reset_events_waits_by_instance();
61 return 0;
62}
63
64table_events_waits_summary_by_instance
65::table_events_waits_summary_by_instance()
66 : table_all_instr(&m_share), m_row_exists(false)
67{}
68
69void table_events_waits_summary_by_instance
70::make_instr_row(PFS_instr *pfs, PFS_instr_class *klass,
71 const void *object_instance_begin,
72 PFS_single_stat *pfs_stat)
73{
74 pfs_lock lock;
75 m_row_exists= false;
76
77 /*
78 Protect this reader against a mutex/rwlock/cond destroy,
79 file delete, table drop.
80 */
81 pfs->m_lock.begin_optimistic_lock(&lock);
82
83 m_row.m_name= klass->m_name;
84 m_row.m_name_length= klass->m_name_length;
85 m_row.m_object_instance_addr= (intptr) object_instance_begin;
86
87 get_normalizer(klass);
88 m_row.m_stat.set(m_normalizer, pfs_stat);
89
90 if (pfs->m_lock.end_optimistic_lock(&lock))
91 m_row_exists= true;
92}
93
94/**
95 Build a row, for mutex statistics in a thread.
96 @param pfs the mutex this cursor is reading
97*/
98void table_events_waits_summary_by_instance::make_mutex_row(PFS_mutex *pfs)
99{
100 PFS_mutex_class *safe_class;
101 safe_class= sanitize_mutex_class(pfs->m_class);
102 if (unlikely(safe_class == NULL))
103 return;
104
105 make_instr_row(pfs, safe_class, pfs->m_identity, &pfs->m_mutex_stat.m_wait_stat);
106}
107
108/**
109 Build a row, for rwlock statistics in a thread.
110 @param pfs the rwlock this cursor is reading
111*/
112void table_events_waits_summary_by_instance::make_rwlock_row(PFS_rwlock *pfs)
113{
114 PFS_rwlock_class *safe_class;
115 safe_class= sanitize_rwlock_class(pfs->m_class);
116 if (unlikely(safe_class == NULL))
117 return;
118
119 make_instr_row(pfs, safe_class, pfs->m_identity, &pfs->m_rwlock_stat.m_wait_stat);
120}
121
122/**
123 Build a row, for condition statistics in a thread.
124 @param pfs the condition this cursor is reading
125*/
126void table_events_waits_summary_by_instance::make_cond_row(PFS_cond *pfs)
127{
128 PFS_cond_class *safe_class;
129 safe_class= sanitize_cond_class(pfs->m_class);
130 if (unlikely(safe_class == NULL))
131 return;
132
133 make_instr_row(pfs, safe_class, pfs->m_identity, &pfs->m_cond_stat.m_wait_stat);
134}
135
136/**
137 Build a row, for file statistics in a thread.
138 @param pfs the file this cursor is reading
139*/
140void table_events_waits_summary_by_instance::make_file_row(PFS_file *pfs)
141{
142 PFS_file_class *safe_class;
143 safe_class= sanitize_file_class(pfs->m_class);
144 if (unlikely(safe_class == NULL))
145 return;
146
147 PFS_single_stat sum;
148 pfs->m_file_stat.m_io_stat.sum_waits(& sum);
149 /*
150 Files don't have a in memory structure associated to it,
151 so we use the address of the PFS_file buffer as object_instance_begin
152 */
153 make_instr_row(pfs, safe_class, pfs, & sum);
154}
155
156/**
157 Build a row, for socket statistics in a thread.
158 @param pfs the socket this cursor is reading
159*/
160void table_events_waits_summary_by_instance::make_socket_row(PFS_socket *pfs)
161{
162 PFS_socket_class *safe_class;
163 safe_class= sanitize_socket_class(pfs->m_class);
164 if (unlikely(safe_class == NULL))
165 return;
166
167 /*
168 Consolidate wait times and byte counts for individual operations. This is
169 done by the consumer in order to reduce overhead on the socket instrument.
170 */
171 PFS_byte_stat pfs_stat;
172 pfs->m_socket_stat.m_io_stat.sum(&pfs_stat);
173
174 /*
175 Sockets don't have an associated in-memory structure, so use the address of
176 the PFS_socket buffer as object_instance_begin.
177 */
178 make_instr_row(pfs, safe_class, pfs, &pfs_stat);
179}
180
181int table_events_waits_summary_by_instance
182::read_row_values(TABLE *table, unsigned char *, Field **fields,
183 bool read_all)
184{
185 Field *f;
186
187 if (unlikely(! m_row_exists))
188 return HA_ERR_RECORD_DELETED;
189
190 /* Set the null bits */
191 DBUG_ASSERT(table->s->null_bytes == 0);
192
193 for (; (f= *fields) ; fields++)
194 {
195 if (read_all || bitmap_is_set(table->read_set, f->field_index))
196 {
197 switch(f->field_index)
198 {
199 case 0: /* NAME */
200 set_field_varchar_utf8(f, m_row.m_name, m_row.m_name_length);
201 break;
202 case 1: /* OBJECT_INSTANCE */
203 set_field_ulonglong(f, m_row.m_object_instance_addr);
204 break;
205 case 2: /* COUNT */
206 set_field_ulonglong(f, m_row.m_stat.m_count);
207 break;
208 case 3: /* SUM */
209 set_field_ulonglong(f, m_row.m_stat.m_sum);
210 break;
211 case 4: /* MIN */
212 set_field_ulonglong(f, m_row.m_stat.m_min);
213 break;
214 case 5: /* AVG */
215 set_field_ulonglong(f, m_row.m_stat.m_avg);
216 break;
217 case 6: /* MAX */
218 set_field_ulonglong(f, m_row.m_stat.m_max);
219 break;
220 default:
221 DBUG_ASSERT(false);
222 }
223 }
224 }
225
226 return 0;
227}
228
229