| 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_esgs_global_by_event_name.cc |
| 26 | Table EVENTS_STAGES_SUMMARY_GLOBAL_BY_EVENT_NAME (implementation). |
| 27 | */ |
| 28 | |
| 29 | #include "storage/perfschema/table_esgs_global_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_column_types.h" |
| 39 | #include "storage/perfschema/pfs_column_values.h" |
| 40 | #include "storage/perfschema/pfs_global.h" |
| 41 | #include "storage/perfschema/pfs_instr.h" |
| 42 | #include "storage/perfschema/pfs_instr_class.h" |
| 43 | #include "storage/perfschema/pfs_timer.h" |
| 44 | #include "storage/perfschema/pfs_visitor.h" |
| 45 | |
| 46 | THR_LOCK table_esgs_global_by_event_name::m_table_lock; |
| 47 | |
| 48 | Plugin_table table_esgs_global_by_event_name::m_table_def( |
| 49 | /* Schema name */ |
| 50 | "performance_schema", |
| 51 | /* Name */ |
| 52 | "events_stages_summary_global_by_event_name", |
| 53 | /* Definition */ |
| 54 | " EVENT_NAME VARCHAR(128) not null,\n" |
| 55 | " COUNT_STAR BIGINT unsigned not null,\n" |
| 56 | " SUM_TIMER_WAIT BIGINT unsigned not null,\n" |
| 57 | " MIN_TIMER_WAIT BIGINT unsigned not null,\n" |
| 58 | " AVG_TIMER_WAIT BIGINT unsigned not null,\n" |
| 59 | " MAX_TIMER_WAIT BIGINT unsigned not null,\n" |
| 60 | " PRIMARY KEY (EVENT_NAME) USING HASH\n", |
| 61 | /* Options */ |
| 62 | " ENGINE=PERFORMANCE_SCHEMA", |
| 63 | /* Tablespace */ |
| 64 | nullptr); |
| 65 | |
| 66 | PFS_engine_table_share table_esgs_global_by_event_name::m_share = { |
| 67 | &pfs_truncatable_acl, |
| 68 | table_esgs_global_by_event_name::create, |
| 69 | nullptr, /* write_row */ |
| 70 | table_esgs_global_by_event_name::delete_all_rows, |
| 71 | table_esgs_global_by_event_name::get_row_count, |
| 72 | sizeof(PFS_simple_index), |
| 73 | &m_table_lock, |
| 74 | &m_table_def, |
| 75 | false /* perpetual */, |
| 76 | PFS_engine_table_proxy(), |
| 77 | {0}, |
| 78 | false /* m_in_purgatory */ |
| 79 | }; |
| 80 | |
| 81 | bool PFS_index_esgs_global_by_event_name::match(PFS_instr_class *instr_class) { |
| 82 | if (m_fields >= 1) { |
| 83 | if (!m_key.match(instr_class)) { |
| 84 | return false; |
| 85 | } |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | PFS_engine_table *table_esgs_global_by_event_name::create( |
| 91 | PFS_engine_table_share *) { |
| 92 | return new table_esgs_global_by_event_name(); |
| 93 | } |
| 94 | |
| 95 | int table_esgs_global_by_event_name::delete_all_rows() { |
| 96 | reset_events_stages_by_thread(); |
| 97 | reset_events_stages_by_account(); |
| 98 | reset_events_stages_by_user(); |
| 99 | reset_events_stages_by_host(); |
| 100 | reset_events_stages_global(); |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | ha_rows table_esgs_global_by_event_name::get_row_count() { |
| 105 | return stage_class_max; |
| 106 | } |
| 107 | |
| 108 | table_esgs_global_by_event_name::table_esgs_global_by_event_name() |
| 109 | : PFS_engine_table(&m_share, &m_pos), m_pos(1), m_next_pos(1) { |
| 110 | m_normalizer = time_normalizer::get_stage(); |
| 111 | } |
| 112 | |
| 113 | void table_esgs_global_by_event_name::reset_position() { |
| 114 | m_pos = 1; |
| 115 | m_next_pos = 1; |
| 116 | } |
| 117 | |
| 118 | int table_esgs_global_by_event_name::rnd_init(bool) { return 0; } |
| 119 | |
| 120 | int table_esgs_global_by_event_name::rnd_next() { |
| 121 | PFS_stage_class *stage_class; |
| 122 | |
| 123 | if (global_instr_class_stages_array == nullptr) { |
| 124 | return HA_ERR_END_OF_FILE; |
| 125 | } |
| 126 | |
| 127 | m_pos.set_at(&m_next_pos); |
| 128 | |
| 129 | stage_class = find_stage_class(m_pos.m_index); |
| 130 | if (stage_class) { |
| 131 | m_next_pos.set_after(&m_pos); |
| 132 | return make_row(stage_class); |
| 133 | } |
| 134 | |
| 135 | return HA_ERR_END_OF_FILE; |
| 136 | } |
| 137 | |
| 138 | int table_esgs_global_by_event_name::rnd_pos(const void *pos) { |
| 139 | PFS_stage_class *stage_class; |
| 140 | |
| 141 | set_position(pos); |
| 142 | |
| 143 | if (global_instr_class_stages_array == nullptr) { |
| 144 | return HA_ERR_END_OF_FILE; |
| 145 | } |
| 146 | |
| 147 | stage_class = find_stage_class(m_pos.m_index); |
| 148 | if (stage_class) { |
| 149 | return make_row(stage_class); |
| 150 | } |
| 151 | |
| 152 | return HA_ERR_RECORD_DELETED; |
| 153 | } |
| 154 | |
| 155 | int table_esgs_global_by_event_name::index_init(uint idx [[maybe_unused]], |
| 156 | bool) { |
| 157 | PFS_index_esgs_global_by_event_name *result = nullptr; |
| 158 | assert(idx == 0); |
| 159 | result = PFS_NEW(PFS_index_esgs_global_by_event_name); |
| 160 | m_opened_index = result; |
| 161 | m_index = result; |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | int table_esgs_global_by_event_name::index_next() { |
| 166 | PFS_stage_class *stage_class; |
| 167 | |
| 168 | if (global_instr_class_stages_array == nullptr) { |
| 169 | return HA_ERR_END_OF_FILE; |
| 170 | } |
| 171 | |
| 172 | m_pos.set_at(&m_next_pos); |
| 173 | |
| 174 | do { |
| 175 | stage_class = find_stage_class(m_pos.m_index); |
| 176 | if (stage_class) { |
| 177 | if (m_opened_index->match(stage_class)) { |
| 178 | if (!make_row(stage_class)) { |
| 179 | m_next_pos.set_after(&m_pos); |
| 180 | return 0; |
| 181 | } |
| 182 | } |
| 183 | m_pos.m_index++; |
| 184 | } |
| 185 | } while (stage_class != nullptr); |
| 186 | |
| 187 | return HA_ERR_END_OF_FILE; |
| 188 | } |
| 189 | |
| 190 | int table_esgs_global_by_event_name::make_row(PFS_stage_class *klass) { |
| 191 | m_row.m_event_name.make_row(klass); |
| 192 | |
| 193 | PFS_connection_stage_visitor visitor(klass); |
| 194 | PFS_connection_iterator::visit_global(true, /* hosts */ |
| 195 | false, /* users */ |
| 196 | true, /* accounts */ |
| 197 | true, /* threads */ |
| 198 | false, /* THDs */ |
| 199 | &visitor); |
| 200 | |
| 201 | m_row.m_stat.set(m_normalizer, &visitor.m_stat); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | int table_esgs_global_by_event_name::read_row_values(TABLE *table, |
| 207 | unsigned char *, |
| 208 | Field **fields, |
| 209 | bool read_all) { |
| 210 | Field *f; |
| 211 | |
| 212 | /* Set the null bits */ |
| 213 | assert(table->s->null_bytes == 0); |
| 214 | |
| 215 | for (; (f = *fields); fields++) { |
| 216 | if (read_all || bitmap_is_set(table->read_set, f->field_index())) { |
| 217 | switch (f->field_index()) { |
| 218 | case 0: /* NAME */ |
| 219 | m_row.m_event_name.set_field(f); |
| 220 | break; |
| 221 | default: /* 1, ... COUNT/SUM/MIN/AVG/MAX */ |
| 222 | m_row.m_stat.set_field(f->field_index() - 1, f); |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |