| 1 | /* Copyright (c) 2010, 2026, Oracle and/or its affiliates. |
| 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, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | @file storage/perfschema/table_esgs_by_user_by_event_name.cc |
| 27 | Table EVENTS_STAGES_SUMMARY_BY_USER_BY_EVENT_NAME (implementation). |
| 28 | */ |
| 29 | |
| 30 | #include "storage/perfschema/table_esgs_by_user_by_event_name.h" |
| 31 | |
| 32 | #include <assert.h> |
| 33 | #include <stddef.h> |
| 34 | |
| 35 | #include "my_thread.h" |
| 36 | #include "sql/field.h" |
| 37 | #include "sql/plugin_table.h" |
| 38 | #include "sql/table.h" |
| 39 | #include "storage/perfschema/pfs_buffer_container.h" |
| 40 | #include "storage/perfschema/pfs_column_types.h" |
| 41 | #include "storage/perfschema/pfs_column_values.h" |
| 42 | #include "storage/perfschema/pfs_global.h" |
| 43 | #include "storage/perfschema/pfs_instr_class.h" |
| 44 | #include "storage/perfschema/pfs_user.h" |
| 45 | #include "storage/perfschema/pfs_visitor.h" |
| 46 | |
| 47 | THR_LOCK table_esgs_by_user_by_event_name::m_table_lock; |
| 48 | |
| 49 | Plugin_table table_esgs_by_user_by_event_name::m_table_def( |
| 50 | /* Schema name */ |
| 51 | "performance_schema", |
| 52 | /* Name */ |
| 53 | "events_stages_summary_by_user_by_event_name", |
| 54 | /* Definition */ |
| 55 | " USER CHAR(32) collate utf8mb4_bin default null,\n" |
| 56 | " EVENT_NAME VARCHAR(128) not null,\n" |
| 57 | " COUNT_STAR BIGINT unsigned not null,\n" |
| 58 | " SUM_TIMER_WAIT BIGINT unsigned not null,\n" |
| 59 | " MIN_TIMER_WAIT BIGINT unsigned not null,\n" |
| 60 | " AVG_TIMER_WAIT BIGINT unsigned not null,\n" |
| 61 | " MAX_TIMER_WAIT BIGINT unsigned not null,\n" |
| 62 | " UNIQUE KEY (USER, EVENT_NAME) USING HASH\n", |
| 63 | /* Options */ |
| 64 | " ENGINE=PERFORMANCE_SCHEMA", |
| 65 | /* Tablespace */ |
| 66 | nullptr); |
| 67 | |
| 68 | PFS_engine_table_share table_esgs_by_user_by_event_name::m_share = { |
| 69 | &pfs_truncatable_acl, |
| 70 | table_esgs_by_user_by_event_name::create, |
| 71 | nullptr, /* write_row */ |
| 72 | table_esgs_by_user_by_event_name::delete_all_rows, |
| 73 | table_esgs_by_user_by_event_name::get_row_count, |
| 74 | sizeof(pos_esgs_by_user_by_event_name), |
| 75 | &m_table_lock, |
| 76 | &m_table_def, |
| 77 | false /* perpetual */, |
| 78 | PFS_engine_table_proxy(), |
| 79 | {0}, |
| 80 | false /* m_in_purgatory */ |
| 81 | }; |
| 82 | |
| 83 | bool PFS_index_esgs_by_user_by_event_name::match(PFS_user *pfs) { |
| 84 | if (m_fields >= 1) { |
| 85 | if (!m_key_1.match(pfs)) { |
| 86 | return false; |
| 87 | } |
| 88 | } |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | bool PFS_index_esgs_by_user_by_event_name::match(PFS_instr_class *instr_class) { |
| 93 | if (m_fields >= 2) { |
| 94 | if (!m_key_2.match(instr_class)) { |
| 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | PFS_engine_table *table_esgs_by_user_by_event_name::create( |
| 102 | PFS_engine_table_share *) { |
| 103 | return new table_esgs_by_user_by_event_name(); |
| 104 | } |
| 105 | |
| 106 | int table_esgs_by_user_by_event_name::delete_all_rows() { |
| 107 | reset_events_stages_by_thread(); |
| 108 | reset_events_stages_by_account(); |
| 109 | reset_events_stages_by_user(); |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | ha_rows table_esgs_by_user_by_event_name::get_row_count() { |
| 114 | return global_user_container.get_row_count() * stage_class_max; |
| 115 | } |
| 116 | |
| 117 | table_esgs_by_user_by_event_name::table_esgs_by_user_by_event_name() |
| 118 | : PFS_engine_table(&m_share, &m_pos), m_pos(), m_next_pos() { |
| 119 | m_normalizer = time_normalizer::get_stage(); |
| 120 | } |
| 121 | |
| 122 | void table_esgs_by_user_by_event_name::reset_position() { |
| 123 | m_pos.reset(); |
| 124 | m_next_pos.reset(); |
| 125 | } |
| 126 | |
| 127 | int table_esgs_by_user_by_event_name::rnd_init(bool) { return 0; } |
| 128 | |
| 129 | int table_esgs_by_user_by_event_name::rnd_next() { |
| 130 | PFS_user *user; |
| 131 | PFS_stage_class *stage_class; |
| 132 | bool has_more_user = true; |
| 133 | |
| 134 | for (m_pos.set_at(&m_next_pos); has_more_user; m_pos.next_user()) { |
| 135 | user = global_user_container.get(m_pos.m_index_1, &has_more_user); |
| 136 | if (user != nullptr) { |
| 137 | stage_class = find_stage_class(m_pos.m_index_2); |
| 138 | if (stage_class) { |
| 139 | m_next_pos.set_after(&m_pos); |
| 140 | return make_row(user, stage_class); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return HA_ERR_END_OF_FILE; |
| 146 | } |
| 147 | |
| 148 | int table_esgs_by_user_by_event_name::rnd_pos(const void *pos) { |
| 149 | PFS_user *user; |
| 150 | PFS_stage_class *stage_class; |
| 151 | |
| 152 | set_position(pos); |
| 153 | |
| 154 | user = global_user_container.get(m_pos.m_index_1); |
| 155 | if (user != nullptr) { |
| 156 | stage_class = find_stage_class(m_pos.m_index_2); |
| 157 | if (stage_class) { |
| 158 | return make_row(user, stage_class); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return HA_ERR_RECORD_DELETED; |
| 163 | } |
| 164 | |
| 165 | int table_esgs_by_user_by_event_name::index_init(uint idx [[maybe_unused]], |
| 166 | bool) { |
| 167 | PFS_index_esgs_by_user_by_event_name *result = nullptr; |
| 168 | assert(idx == 0); |
| 169 | result = PFS_NEW(PFS_index_esgs_by_user_by_event_name); |
| 170 | m_opened_index = result; |
| 171 | m_index = result; |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | int table_esgs_by_user_by_event_name::index_next() { |
| 176 | PFS_user *user; |
| 177 | PFS_stage_class *stage_class; |
| 178 | bool has_more_user = true; |
| 179 | |
| 180 | for (m_pos.set_at(&m_next_pos); has_more_user; m_pos.next_user()) { |
| 181 | user = global_user_container.get(m_pos.m_index_1, &has_more_user); |
| 182 | if (user != nullptr) { |
| 183 | if (m_opened_index->match(user)) { |
| 184 | do { |
| 185 | stage_class = find_stage_class(m_pos.m_index_2); |
| 186 | if (stage_class) { |
| 187 | if (m_opened_index->match(stage_class)) { |
| 188 | if (!make_row(user, stage_class)) { |
| 189 | m_next_pos.set_after(&m_pos); |
| 190 | return 0; |
| 191 | } |
| 192 | } |
| 193 | m_pos.m_index_2++; |
| 194 | } |
| 195 | } while (stage_class != nullptr); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return HA_ERR_END_OF_FILE; |
| 201 | } |
| 202 | |
| 203 | int table_esgs_by_user_by_event_name::make_row(PFS_user *user, |
| 204 | PFS_stage_class *klass) { |
| 205 | pfs_optimistic_state lock; |
| 206 | |
| 207 | user->m_lock.begin_optimistic_lock(&lock); |
| 208 | |
| 209 | if (m_row.m_user.make_row(user)) { |
| 210 | return HA_ERR_RECORD_DELETED; |
| 211 | } |
| 212 | |
| 213 | m_row.m_event_name.make_row(klass); |
| 214 | |
| 215 | PFS_connection_stage_visitor visitor(klass); |
| 216 | PFS_connection_iterator::visit_user(user, true, /* accounts */ |
| 217 | true, /* threads */ |
| 218 | false, /* THDs */ |
| 219 | &visitor); |
| 220 | |
| 221 | if (!user->m_lock.end_optimistic_lock(&lock)) { |
| 222 | return HA_ERR_RECORD_DELETED; |
| 223 | } |
| 224 | |
| 225 | m_row.m_stat.set(m_normalizer, &visitor.m_stat); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | int table_esgs_by_user_by_event_name::read_row_values(TABLE *table, |
| 231 | unsigned char *buf, |
| 232 | Field **fields, |
| 233 | bool read_all) { |
| 234 | Field *f; |
| 235 | |
| 236 | /* Set the null bits */ |
| 237 | assert(table->s->null_bytes == 1); |
| 238 | buf[0] = 0; |
| 239 | |
| 240 | for (; (f = *fields); fields++) { |
| 241 | if (read_all || bitmap_is_set(table->read_set, f->field_index())) { |
| 242 | switch (f->field_index()) { |
| 243 | case 0: /* USER */ |
| 244 | m_row.m_user.set_nullable_field(f); |
| 245 | break; |
| 246 | case 1: /* EVENT_NAME */ |
| 247 | m_row.m_event_name.set_field(f); |
| 248 | break; |
| 249 | default: /* 2, ... COUNT/SUM/MIN/AVG/MAX */ |
| 250 | m_row.m_stat.set_field(f->field_index() - 2, f); |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | } |