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_user_by_event_name.cc |
18 | Table EVENTS_STAGES_SUMMARY_BY_USER_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_user_by_event_name.h" |
27 | #include "pfs_global.h" |
28 | #include "pfs_account.h" |
29 | #include "pfs_visitor.h" |
30 | |
31 | THR_LOCK table_esgs_by_user_by_event_name::m_table_lock; |
32 | |
33 | PFS_engine_table_share |
34 | table_esgs_by_user_by_event_name::m_share= |
35 | { |
36 | { C_STRING_WITH_LEN("events_stages_summary_by_user_by_event_name" ) }, |
37 | &pfs_truncatable_acl, |
38 | table_esgs_by_user_by_event_name::create, |
39 | NULL, /* write_row */ |
40 | table_esgs_by_user_by_event_name::delete_all_rows, |
41 | NULL, /* get_row_count */ |
42 | 1000, /* records */ |
43 | sizeof(pos_esgs_by_user_by_event_name), |
44 | &m_table_lock, |
45 | { C_STRING_WITH_LEN("CREATE TABLE events_stages_summary_by_user_by_event_name(" |
46 | "USER CHAR(16) 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 | |
55 | PFS_engine_table* |
56 | table_esgs_by_user_by_event_name::create(void) |
57 | { |
58 | return new table_esgs_by_user_by_event_name(); |
59 | } |
60 | |
61 | int |
62 | table_esgs_by_user_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_user(); |
67 | return 0; |
68 | } |
69 | |
70 | table_esgs_by_user_by_event_name::table_esgs_by_user_by_event_name() |
71 | : PFS_engine_table(&m_share, &m_pos), |
72 | m_row_exists(false), m_pos(), m_next_pos() |
73 | {} |
74 | |
75 | void table_esgs_by_user_by_event_name::reset_position(void) |
76 | { |
77 | m_pos.reset(); |
78 | m_next_pos.reset(); |
79 | } |
80 | |
81 | int table_esgs_by_user_by_event_name::rnd_init(bool scan) |
82 | { |
83 | m_normalizer= time_normalizer::get(stage_timer); |
84 | return 0; |
85 | } |
86 | |
87 | int table_esgs_by_user_by_event_name::rnd_next(void) |
88 | { |
89 | PFS_user *user; |
90 | PFS_stage_class *stage_class; |
91 | |
92 | for (m_pos.set_at(&m_next_pos); |
93 | m_pos.has_more_user(); |
94 | m_pos.next_user()) |
95 | { |
96 | user= &user_array[m_pos.m_index_1]; |
97 | if (user->m_lock.is_populated()) |
98 | { |
99 | stage_class= find_stage_class(m_pos.m_index_2); |
100 | if (stage_class) |
101 | { |
102 | make_row(user, 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 | |
112 | int |
113 | table_esgs_by_user_by_event_name::rnd_pos(const void *pos) |
114 | { |
115 | PFS_user *user; |
116 | PFS_stage_class *stage_class; |
117 | |
118 | set_position(pos); |
119 | DBUG_ASSERT(m_pos.m_index_1 < user_max); |
120 | |
121 | user= &user_array[m_pos.m_index_1]; |
122 | if (! user->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(user, stage_class); |
129 | return 0; |
130 | } |
131 | |
132 | return HA_ERR_RECORD_DELETED; |
133 | } |
134 | |
135 | void table_esgs_by_user_by_event_name |
136 | ::make_row(PFS_user *user, PFS_stage_class *klass) |
137 | { |
138 | pfs_lock lock; |
139 | m_row_exists= false; |
140 | |
141 | user->m_lock.begin_optimistic_lock(&lock); |
142 | |
143 | if (m_row.m_user.make_row(user)) |
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_user(user, true, true, & visitor); |
150 | |
151 | if (! user->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 | |
158 | int table_esgs_by_user_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: /* USER */ |
178 | m_row.m_user.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 | |