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_esgs_by_host_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_host_by_event_name.h"
27#include "pfs_global.h"
28#include "pfs_account.h"
29#include "pfs_visitor.h"
30
31THR_LOCK table_esgs_by_host_by_event_name::m_table_lock;
32
33PFS_engine_table_share
34table_esgs_by_host_by_event_name::m_share=
35{
36 { C_STRING_WITH_LEN("events_stages_summary_by_host_by_event_name") },
37 &pfs_truncatable_acl,
38 table_esgs_by_host_by_event_name::create,
39 NULL, /* write_row */
40 table_esgs_by_host_by_event_name::delete_all_rows,
41 NULL, /* get_row_count */
42 1000, /* records */
43 sizeof(pos_esgs_by_host_by_event_name),
44 &m_table_lock,
45 { C_STRING_WITH_LEN("CREATE TABLE events_stages_summary_by_host_by_event_name("
46 "HOST CHAR(60) collate utf8_bin default null,"
47 "EVENT_NAME VARCHAR(128) not null,"
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};
54
55PFS_engine_table*
56table_esgs_by_host_by_event_name::create(void)
57{
58 return new table_esgs_by_host_by_event_name();
59}
60
61int
62table_esgs_by_host_by_event_name::delete_all_rows(void)
63{
64 reset_events_stages_by_thread();
65 reset_events_stages_by_account();
66 reset_events_stages_by_host();
67 return 0;
68}
69
70table_esgs_by_host_by_event_name::table_esgs_by_host_by_event_name()
71 : PFS_engine_table(&m_share, &m_pos),
72 m_row_exists(false), m_pos(), m_next_pos()
73{}
74
75void table_esgs_by_host_by_event_name::reset_position(void)
76{
77 m_pos.reset();
78 m_next_pos.reset();
79}
80
81int table_esgs_by_host_by_event_name::rnd_init(bool scan)
82{
83 m_normalizer= time_normalizer::get(stage_timer);
84 return 0;
85}
86
87int table_esgs_by_host_by_event_name::rnd_next(void)
88{
89 PFS_host *host;
90 PFS_stage_class *stage_class;
91
92 for (m_pos.set_at(&m_next_pos);
93 m_pos.has_more_host();
94 m_pos.next_host())
95 {
96 host= &host_array[m_pos.m_index_1];
97 if (host->m_lock.is_populated())
98 {
99 stage_class= find_stage_class(m_pos.m_index_2);
100 if (stage_class)
101 {
102 make_row(host, stage_class);
103 m_next_pos.set_after(&m_pos);
104 return 0;
105 }
106 }
107 }
108
109 return HA_ERR_END_OF_FILE;
110}
111
112int
113table_esgs_by_host_by_event_name::rnd_pos(const void *pos)
114{
115 PFS_host *host;
116 PFS_stage_class *stage_class;
117
118 set_position(pos);
119 DBUG_ASSERT(m_pos.m_index_1 < host_max);
120
121 host= &host_array[m_pos.m_index_1];
122 if (! host->m_lock.is_populated())
123 return HA_ERR_RECORD_DELETED;
124
125 stage_class= find_stage_class(m_pos.m_index_2);
126 if (stage_class)
127 {
128 make_row(host, stage_class);
129 return 0;
130 }
131
132 return HA_ERR_RECORD_DELETED;
133}
134
135void table_esgs_by_host_by_event_name
136::make_row(PFS_host *host, PFS_stage_class *klass)
137{
138 pfs_lock lock;
139 m_row_exists= false;
140
141 host->m_lock.begin_optimistic_lock(&lock);
142
143 if (m_row.m_host.make_row(host))
144 return;
145
146 m_row.m_event_name.make_row(klass);
147
148 PFS_connection_stage_visitor visitor(klass);
149 PFS_connection_iterator::visit_host(host, true, true, & visitor);
150
151 if (! host->m_lock.end_optimistic_lock(&lock))
152 return;
153
154 m_row_exists= true;
155 m_row.m_stat.set(m_normalizer, & visitor.m_stat);
156}
157
158int table_esgs_by_host_by_event_name
159::read_row_values(TABLE *table, unsigned char *buf, Field **fields,
160 bool read_all)
161{
162 Field *f;
163
164 if (unlikely(! m_row_exists))
165 return HA_ERR_RECORD_DELETED;
166
167 /* Set the null bits */
168 DBUG_ASSERT(table->s->null_bytes == 1);
169 buf[0]= 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: /* HOST */
178 m_row.m_host.set_field(f);
179 break;
180 case 1: /* EVENT_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