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_account_by_event_name.cc
18 Table EVENTS_STAGES_SUMMARY_BY_ACCOUNT_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_account_by_event_name.h"
27#include "pfs_global.h"
28#include "pfs_visitor.h"
29
30THR_LOCK table_esgs_by_account_by_event_name::m_table_lock;
31
32PFS_engine_table_share
33table_esgs_by_account_by_event_name::m_share=
34{
35 { C_STRING_WITH_LEN("events_stages_summary_by_account_by_event_name") },
36 &pfs_truncatable_acl,
37 table_esgs_by_account_by_event_name::create,
38 NULL, /* write_row */
39 table_esgs_by_account_by_event_name::delete_all_rows,
40 NULL, /* get_row_count */
41 1000, /* records */
42 sizeof(pos_esgs_by_account_by_event_name),
43 &m_table_lock,
44 { C_STRING_WITH_LEN("CREATE TABLE events_stages_summary_by_account_by_event_name("
45 "USER CHAR(16) collate utf8_bin default null,"
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_account_by_event_name::create(void)
57{
58 return new table_esgs_by_account_by_event_name();
59}
60
61int
62table_esgs_by_account_by_event_name::delete_all_rows(void)
63{
64 reset_events_stages_by_thread();
65 reset_events_stages_by_account();
66 return 0;
67}
68
69table_esgs_by_account_by_event_name::table_esgs_by_account_by_event_name()
70 : PFS_engine_table(&m_share, &m_pos),
71 m_row_exists(false), m_pos(), m_next_pos()
72{}
73
74void table_esgs_by_account_by_event_name::reset_position(void)
75{
76 m_pos.reset();
77 m_next_pos.reset();
78}
79
80int table_esgs_by_account_by_event_name::rnd_init(bool scan)
81{
82 m_normalizer= time_normalizer::get(stage_timer);
83 return 0;
84}
85
86int table_esgs_by_account_by_event_name::rnd_next(void)
87{
88 PFS_account *account;
89 PFS_stage_class *stage_class;
90
91 for (m_pos.set_at(&m_next_pos);
92 m_pos.has_more_account();
93 m_pos.next_account())
94 {
95 account= &account_array[m_pos.m_index_1];
96 if (account->m_lock.is_populated())
97 {
98 stage_class= find_stage_class(m_pos.m_index_2);
99 if (stage_class)
100 {
101 make_row(account, stage_class);
102 m_next_pos.set_after(&m_pos);
103 return 0;
104 }
105 }
106 }
107
108 return HA_ERR_END_OF_FILE;
109}
110
111int
112table_esgs_by_account_by_event_name::rnd_pos(const void *pos)
113{
114 PFS_account *account;
115 PFS_stage_class *stage_class;
116
117 set_position(pos);
118 DBUG_ASSERT(m_pos.m_index_1 < account_max);
119
120 account= &account_array[m_pos.m_index_1];
121 if (! account->m_lock.is_populated())
122 return HA_ERR_RECORD_DELETED;
123
124 stage_class= find_stage_class(m_pos.m_index_2);
125 if (stage_class)
126 {
127 make_row(account, stage_class);
128 return 0;
129 }
130
131 return HA_ERR_RECORD_DELETED;
132}
133
134void table_esgs_by_account_by_event_name
135::make_row(PFS_account *account, PFS_stage_class *klass)
136{
137 pfs_lock lock;
138 m_row_exists= false;
139
140 account->m_lock.begin_optimistic_lock(&lock);
141
142 if (m_row.m_account.make_row(account))
143 return;
144
145 m_row.m_event_name.make_row(klass);
146
147 PFS_connection_stage_visitor visitor(klass);
148 PFS_connection_iterator::visit_account(account, true, & visitor);
149
150 if (! account->m_lock.end_optimistic_lock(&lock))
151 return;
152
153 m_row_exists= true;
154 m_row.m_stat.set(m_normalizer, & visitor.m_stat);
155}
156
157int table_esgs_by_account_by_event_name
158::read_row_values(TABLE *table, unsigned char *buf, Field **fields,
159 bool read_all)
160{
161 Field *f;
162
163 if (unlikely(! m_row_exists))
164 return HA_ERR_RECORD_DELETED;
165
166 /* Set the null bits */
167 DBUG_ASSERT(table->s->null_bytes == 1);
168 buf[0]= 0;
169
170 for (; (f= *fields) ; fields++)
171 {
172 if (read_all || bitmap_is_set(table->read_set, f->field_index))
173 {
174 switch(f->field_index)
175 {
176 case 0: /* USER */
177 case 1: /* HOST */
178 m_row.m_account.set_field(f->field_index, f);
179 break;
180 case 2: /* EVENT_NAME */
181 m_row.m_event_name.set_field(f);
182 break;
183 default: /* 3, ... COUNT/SUM/MIN/AVG/MAX */
184 m_row.m_stat.set_field(f->field_index - 3, f);
185 break;
186 }
187 }
188 }
189
190 return 0;
191}
192
193