| 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 | @file storage/perfschema/table_ets_by_account_by_event_name.cc |
| 26 | Table EVENTS_TRANSACTIONS_SUMMARY_BY_ACCOUNT_BY_EVENT_NAME (implementation). |
| 27 | */ |
| 28 | |
| 29 | #include "storage/perfschema/table_ets_by_account_by_event_name.h" |
| 30 | |
| 31 | #include <assert.h> |
| 32 | #include <stddef.h> |
| 33 | |
| 34 | #include "my_thread.h" |
| 35 | #include "sql/field.h" |
| 36 | #include "sql/plugin_table.h" |
| 37 | #include "sql/table.h" |
| 38 | #include "storage/perfschema/pfs_buffer_container.h" |
| 39 | #include "storage/perfschema/pfs_column_types.h" |
| 40 | #include "storage/perfschema/pfs_column_values.h" |
| 41 | #include "storage/perfschema/pfs_events_transactions.h" |
| 42 | #include "storage/perfschema/pfs_global.h" |
| 43 | #include "storage/perfschema/pfs_instr_class.h" |
| 44 | #include "storage/perfschema/pfs_visitor.h" |
| 45 | |
| 46 | THR_LOCK table_ets_by_account_by_event_name::m_table_lock; |
| 47 | |
| 48 | Plugin_table table_ets_by_account_by_event_name::m_table_def( |
| 49 | /* Schema name */ |
| 50 | "performance_schema", |
| 51 | /* Name */ |
| 52 | "events_transactions_summary_by_account_by_event_name", |
| 53 | /* Definition */ |
| 54 | " USER CHAR(32) collate utf8mb4_bin default null,\n" |
| 55 | " HOST CHAR(255) CHARACTER SET ASCII 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 | " COUNT_READ_WRITE BIGINT unsigned not null,\n" |
| 63 | " SUM_TIMER_READ_WRITE BIGINT unsigned not null,\n" |
| 64 | " MIN_TIMER_READ_WRITE BIGINT unsigned not null,\n" |
| 65 | " AVG_TIMER_READ_WRITE BIGINT unsigned not null,\n" |
| 66 | " MAX_TIMER_READ_WRITE BIGINT unsigned not null,\n" |
| 67 | " COUNT_READ_ONLY BIGINT unsigned not null,\n" |
| 68 | " SUM_TIMER_READ_ONLY BIGINT unsigned not null,\n" |
| 69 | " MIN_TIMER_READ_ONLY BIGINT unsigned not null,\n" |
| 70 | " AVG_TIMER_READ_ONLY BIGINT unsigned not null,\n" |
| 71 | " MAX_TIMER_READ_ONLY BIGINT unsigned not null,\n" |
| 72 | " UNIQUE KEY `ACCOUNT` (USER, HOST, EVENT_NAME) USING HASH\n", |
| 73 | /* Options */ |
| 74 | " ENGINE=PERFORMANCE_SCHEMA", |
| 75 | /* Tablespace */ |
| 76 | nullptr); |
| 77 | |
| 78 | PFS_engine_table_share table_ets_by_account_by_event_name::m_share = { |
| 79 | &pfs_truncatable_acl, |
| 80 | table_ets_by_account_by_event_name::create, |
| 81 | nullptr, /* write_row */ |
| 82 | table_ets_by_account_by_event_name::delete_all_rows, |
| 83 | table_ets_by_account_by_event_name::get_row_count, |
| 84 | sizeof(pos_ets_by_account_by_event_name), |
| 85 | &m_table_lock, |
| 86 | &m_table_def, |
| 87 | false, /* perpetual */ |
| 88 | PFS_engine_table_proxy(), |
| 89 | {0}, |
| 90 | false /* m_in_purgatory */ |
| 91 | }; |
| 92 | |
| 93 | bool PFS_index_ets_by_account_by_event_name::match(PFS_account *pfs) { |
| 94 | if (m_fields >= 1) { |
| 95 | if (!m_key_1.match(pfs)) { |
| 96 | return false; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if (m_fields >= 2) { |
| 101 | if (!m_key_2.match(pfs)) { |
| 102 | return false; |
| 103 | } |
| 104 | } |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | bool PFS_index_ets_by_account_by_event_name::match( |
| 109 | PFS_instr_class *instr_class) { |
| 110 | if (m_fields >= 3) { |
| 111 | if (!m_key_3.match(instr_class)) { |
| 112 | return false; |
| 113 | } |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | PFS_engine_table *table_ets_by_account_by_event_name::create( |
| 119 | PFS_engine_table_share *) { |
| 120 | return new table_ets_by_account_by_event_name(); |
| 121 | } |
| 122 | |
| 123 | int table_ets_by_account_by_event_name::delete_all_rows() { |
| 124 | reset_events_transactions_by_thread(); |
| 125 | reset_events_transactions_by_account(); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | ha_rows table_ets_by_account_by_event_name::get_row_count() { |
| 130 | return global_account_container.get_row_count() * transaction_class_max; |
| 131 | } |
| 132 | |
| 133 | table_ets_by_account_by_event_name::table_ets_by_account_by_event_name() |
| 134 | : PFS_engine_table(&m_share, &m_pos), m_pos(), m_next_pos() { |
| 135 | m_normalizer = time_normalizer::get_transaction(); |
| 136 | } |
| 137 | |
| 138 | void table_ets_by_account_by_event_name::reset_position() { |
| 139 | m_pos.reset(); |
| 140 | m_next_pos.reset(); |
| 141 | } |
| 142 | |
| 143 | int table_ets_by_account_by_event_name::rnd_init(bool) { return 0; } |
| 144 | |
| 145 | int table_ets_by_account_by_event_name::rnd_next() { |
| 146 | PFS_account *account; |
| 147 | PFS_transaction_class *transaction_class; |
| 148 | bool has_more_account = true; |
| 149 | |
| 150 | for (m_pos.set_at(&m_next_pos); has_more_account; m_pos.next_account()) { |
| 151 | account = global_account_container.get(m_pos.m_index_1, &has_more_account); |
| 152 | if (account != nullptr) { |
| 153 | transaction_class = find_transaction_class(m_pos.m_index_2); |
| 154 | if (transaction_class) { |
| 155 | m_next_pos.set_after(&m_pos); |
| 156 | return make_row(account, transaction_class); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return HA_ERR_END_OF_FILE; |
| 162 | } |
| 163 | |
| 164 | int table_ets_by_account_by_event_name::rnd_pos(const void *pos) { |
| 165 | PFS_account *account; |
| 166 | PFS_transaction_class *transaction_class; |
| 167 | |
| 168 | set_position(pos); |
| 169 | |
| 170 | account = global_account_container.get(m_pos.m_index_1); |
| 171 | if (account != nullptr) { |
| 172 | transaction_class = find_transaction_class(m_pos.m_index_2); |
| 173 | if (transaction_class) { |
| 174 | return make_row(account, transaction_class); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return HA_ERR_RECORD_DELETED; |
| 179 | } |
| 180 | |
| 181 | int table_ets_by_account_by_event_name::index_init(uint idx [[maybe_unused]], |
| 182 | bool) { |
| 183 | PFS_index_ets_by_account_by_event_name *result = nullptr; |
| 184 | assert(idx == 0); |
| 185 | result = PFS_NEW(PFS_index_ets_by_account_by_event_name); |
| 186 | m_opened_index = result; |
| 187 | m_index = result; |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | int table_ets_by_account_by_event_name::index_next() { |
| 192 | PFS_account *account; |
| 193 | PFS_transaction_class *transaction_class; |
| 194 | bool has_more_account = true; |
| 195 | |
| 196 | for (m_pos.set_at(&m_next_pos); has_more_account; m_pos.next_account()) { |
| 197 | account = global_account_container.get(m_pos.m_index_1, &has_more_account); |
| 198 | if (account != nullptr) { |
| 199 | if (m_opened_index->match(account)) { |
| 200 | do { |
| 201 | transaction_class = find_transaction_class(m_pos.m_index_2); |
| 202 | if (transaction_class) { |
| 203 | if (m_opened_index->match(transaction_class)) { |
| 204 | if (!make_row(account, transaction_class)) { |
| 205 | m_next_pos.set_after(&m_pos); |
| 206 | return 0; |
| 207 | } |
| 208 | } |
| 209 | m_pos.m_index_2++; |
| 210 | } |
| 211 | } while (transaction_class != nullptr); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return HA_ERR_END_OF_FILE; |
| 217 | } |
| 218 | |
| 219 | int table_ets_by_account_by_event_name::make_row(PFS_account *account, |
| 220 | PFS_transaction_class *klass) { |
| 221 | pfs_optimistic_state lock; |
| 222 | |
| 223 | account->m_lock.begin_optimistic_lock(&lock); |
| 224 | |
| 225 | if (m_row.m_account.make_row(account)) { |
| 226 | return HA_ERR_RECORD_DELETED; |
| 227 | } |
| 228 | |
| 229 | m_row.m_event_name.make_row(klass); |
| 230 | |
| 231 | PFS_connection_transaction_visitor visitor(klass); |
| 232 | PFS_connection_iterator::visit_account(account, true, /* threads */ |
| 233 | false, /* THDs */ |
| 234 | &visitor); |
| 235 | |
| 236 | if (!account->m_lock.end_optimistic_lock(&lock)) { |
| 237 | return HA_ERR_RECORD_DELETED; |
| 238 | } |
| 239 | |
| 240 | m_row.m_stat.set(m_normalizer, &visitor.m_stat); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | int table_ets_by_account_by_event_name::read_row_values(TABLE *table, |
| 246 | unsigned char *buf, |
| 247 | Field **fields, |
| 248 | bool read_all) { |
| 249 | Field *f; |
| 250 | |
| 251 | /* Set the null bits */ |
| 252 | assert(table->s->null_bytes == 1); |
| 253 | buf[0] = 0; |
| 254 | |
| 255 | for (; (f = *fields); fields++) { |
| 256 | if (read_all || bitmap_is_set(table->read_set, f->field_index())) { |
| 257 | switch (f->field_index()) { |
| 258 | case 0: /* USER */ |
| 259 | case 1: /* HOST */ |
| 260 | m_row.m_account.set_nullable_field(f->field_index(), f); |
| 261 | break; |
| 262 | case 2: /* EVENT_NAME */ |
| 263 | m_row.m_event_name.set_field(f); |
| 264 | break; |
| 265 | default: |
| 266 | /** |
| 267 | COUNT_STAR, SUM/MIN/AVG/MAX_TIMER_WAIT, |
| 268 | COUNT_READ_WRITE, SUM/MIN/AVG/MAX_TIMER_READ_WRITE, |
| 269 | COUNT_READ_ONLY, SUM/MIN/AVG/MAX_TIMER_READ_ONLY |
| 270 | */ |
| 271 | m_row.m_stat.set_field(f->field_index() - 3, f); |
| 272 | break; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return 0; |
| 278 | } |