1/* Copyright (c) 2010, 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
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16/**
17 @file storage/perfschema/table_esgs_by_thread_by_event_name.cc
18 Table EVENTS_STAGES_SUMMARY_BY_HOST_BY_EVENT_NAME (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_esgs_by_thread_by_event_name.h"
27#include "pfs_global.h"
28#include "pfs_visitor.h"
29
30THR_LOCK table_esgs_by_thread_by_event_name::m_table_lock;
31
32PFS_engine_table_share
33table_esgs_by_thread_by_event_name::m_share=
34{
35 { C_STRING_WITH_LEN("events_stages_summary_by_thread_by_event_name") },
36 &pfs_truncatable_acl,
37 table_esgs_by_thread_by_event_name::create,
38 NULL, /* write_row */
39 table_esgs_by_thread_by_event_name::delete_all_rows,
40 NULL, /* get_row_count */
41 1000, /* records */
42 sizeof(pos_esgs_by_thread_by_event_name),
43 &m_table_lock,
44 { C_STRING_WITH_LEN("CREATE TABLE events_stages_summary_by_thread_by_event_name("
45 "THREAD_ID BIGINT unsigned not null,"
46 "EVENT_NAME VARCHAR(128) 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};
53
54PFS_engine_table*
55table_esgs_by_thread_by_event_name::create(void)
56{
57 return new table_esgs_by_thread_by_event_name();
58}
59
60int
61table_esgs_by_thread_by_event_name::delete_all_rows(void)
62{
63 reset_events_stages_by_thread();
64 return 0;
65}
66
67table_esgs_by_thread_by_event_name::table_esgs_by_thread_by_event_name()
68 : PFS_engine_table(&m_share, &m_pos),
69 m_row_exists(false), m_pos(), m_next_pos()
70{}
71
72void table_esgs_by_thread_by_event_name::reset_position(void)
73{
74 m_pos.reset();
75 m_next_pos.reset();
76}
77
78int table_esgs_by_thread_by_event_name::rnd_init(bool scan)
79{
80 m_normalizer= time_normalizer::get(stage_timer);
81 return 0;
82}
83
84int table_esgs_by_thread_by_event_name::rnd_next(void)
85{
86 PFS_thread *thread;
87 PFS_stage_class *stage_class;
88
89 for (m_pos.set_at(&m_next_pos);
90 m_pos.has_more_thread();
91 m_pos.next_thread())
92 {
93 thread= &thread_array[m_pos.m_index_1];
94
95 /*
96 Important note: the thread scan is the outer loop (index 1),
97 to minimize the number of calls to atomic operations.
98 */
99 if (thread->m_lock.is_populated())
100 {
101 stage_class= find_stage_class(m_pos.m_index_2);
102 if (stage_class)
103 {
104 make_row(thread, stage_class);
105 m_next_pos.set_after(&m_pos);
106 return 0;
107 }
108 }
109 }
110
111 return HA_ERR_END_OF_FILE;
112}
113
114int
115table_esgs_by_thread_by_event_name::rnd_pos(const void *pos)
116{
117 PFS_thread *thread;
118 PFS_stage_class *stage_class;
119
120 set_position(pos);
121 DBUG_ASSERT(m_pos.m_index_1 < thread_max);
122
123 thread= &thread_array[m_pos.m_index_1];
124 if (! thread->m_lock.is_populated())
125 return HA_ERR_RECORD_DELETED;
126
127 stage_class= find_stage_class(m_pos.m_index_2);
128 if (stage_class)
129 {
130 make_row(thread, stage_class);
131 return 0;
132 }
133
134 return HA_ERR_RECORD_DELETED;
135}
136
137void table_esgs_by_thread_by_event_name
138::make_row(PFS_thread *thread, PFS_stage_class *klass)
139{
140 pfs_lock lock;
141 m_row_exists= false;
142
143 /* Protect this reader against a thread termination */
144 thread->m_lock.begin_optimistic_lock(&lock);
145
146 m_row.m_thread_internal_id= thread->m_thread_internal_id;
147
148 m_row.m_event_name.make_row(klass);
149
150 PFS_connection_stage_visitor visitor(klass);
151 PFS_connection_iterator::visit_thread(thread, & visitor);
152
153 if (thread->m_lock.end_optimistic_lock(&lock))
154 m_row_exists= true;
155
156 m_row.m_stat.set(m_normalizer, & visitor.m_stat);
157}
158
159int table_esgs_by_thread_by_event_name
160::read_row_values(TABLE *table, unsigned char *, Field **fields,
161 bool read_all)
162{
163 Field *f;
164
165 if (unlikely(! m_row_exists))
166 return HA_ERR_RECORD_DELETED;
167
168 /* Set the null bits */
169 DBUG_ASSERT(table->s->null_bytes == 0);
170
171 for (; (f= *fields) ; fields++)
172 {
173 if (read_all || bitmap_is_set(table->read_set, f->field_index))
174 {
175 switch(f->field_index)
176 {
177 case 0: /* THREAD_ID */
178 set_field_ulonglong(f, m_row.m_thread_internal_id);
179 break;
180 case 1: /* NAME */
181 m_row.m_event_name.set_field(f);
182 break;
183 default: /* 2, ... COUNT/SUM/MIN/AVG/MAX */
184 m_row.m_stat.set_field(f->field_index - 2, f);
185 break;
186 }
187 }
188 }
189
190 return 0;
191}
192
193