| 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_ews_by_host_by_event_name.cc |
| 27 | Table EVENTS_WAITS_SUMMARY_BY_HOST_BY_EVENT_NAME (implementation). |
| 28 | */ |
| 29 | |
| 30 | #include "storage/perfschema/table_ews_by_host_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_account.h" |
| 40 | #include "storage/perfschema/pfs_buffer_container.h" |
| 41 | #include "storage/perfschema/pfs_column_types.h" |
| 42 | #include "storage/perfschema/pfs_column_values.h" |
| 43 | #include "storage/perfschema/pfs_global.h" |
| 44 | #include "storage/perfschema/pfs_instr_class.h" |
| 45 | #include "storage/perfschema/pfs_visitor.h" |
| 46 | |
| 47 | THR_LOCK table_ews_by_host_by_event_name::m_table_lock; |
| 48 | |
| 49 | Plugin_table table_ews_by_host_by_event_name::m_table_def( |
| 50 | /* Schema name */ |
| 51 | "performance_schema", |
| 52 | /* Name */ |
| 53 | "events_waits_summary_by_host_by_event_name", |
| 54 | /* Definition */ |
| 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 | " UNIQUE KEY (HOST, EVENT_NAME) USING HASH\n", |
| 63 | /* Options */ |
| 64 | " ENGINE=PERFORMANCE_SCHEMA", |
| 65 | /* Tablespace */ |
| 66 | nullptr); |
| 67 | |
| 68 | PFS_engine_table_share table_ews_by_host_by_event_name::m_share = { |
| 69 | &pfs_truncatable_acl, |
| 70 | table_ews_by_host_by_event_name::create, |
| 71 | nullptr, /* write_row */ |
| 72 | table_ews_by_host_by_event_name::delete_all_rows, |
| 73 | table_ews_by_host_by_event_name::get_row_count, |
| 74 | sizeof(pos_ews_by_host_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_ews_by_host_by_event_name::match(PFS_host *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_ews_by_host_by_event_name::match_view(uint view) { |
| 93 | if (m_fields >= 2) { |
| 94 | return m_key_2.match_view(view); |
| 95 | } |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | bool PFS_index_ews_by_host_by_event_name::match(PFS_instr_class *instr_class) { |
| 100 | if (m_fields >= 2) { |
| 101 | return m_key_2.match(instr_class); |
| 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | PFS_engine_table *table_ews_by_host_by_event_name::create( |
| 107 | PFS_engine_table_share *) { |
| 108 | return new table_ews_by_host_by_event_name(); |
| 109 | } |
| 110 | |
| 111 | int table_ews_by_host_by_event_name::delete_all_rows() { |
| 112 | reset_events_waits_by_thread(); |
| 113 | reset_events_waits_by_account(); |
| 114 | reset_events_waits_by_host(); |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | ha_rows table_ews_by_host_by_event_name::get_row_count() { |
| 119 | return global_host_container.get_row_count() * wait_class_max; |
| 120 | } |
| 121 | |
| 122 | table_ews_by_host_by_event_name::table_ews_by_host_by_event_name() |
| 123 | : PFS_engine_table(&m_share, &m_pos), m_pos(), m_next_pos() { |
| 124 | // For all cases except IDLE |
| 125 | m_normalizer = time_normalizer::get_wait(); |
| 126 | } |
| 127 | |
| 128 | void table_ews_by_host_by_event_name::reset_position() { |
| 129 | m_pos.reset(); |
| 130 | m_next_pos.reset(); |
| 131 | } |
| 132 | |
| 133 | int table_ews_by_host_by_event_name::rnd_next() { |
| 134 | PFS_host *host; |
| 135 | PFS_instr_class *instr_class; |
| 136 | bool has_more_host = true; |
| 137 | |
| 138 | for (m_pos.set_at(&m_next_pos); has_more_host; m_pos.next_host()) { |
| 139 | host = global_host_container.get(m_pos.m_index_1, &has_more_host); |
| 140 | if (host != nullptr) { |
| 141 | for (; m_pos.has_more_view(); m_pos.next_view()) { |
| 142 | switch (m_pos.m_index_2) { |
| 143 | case pos_ews_by_host_by_event_name::VIEW_MUTEX: |
| 144 | instr_class = find_mutex_class(m_pos.m_index_3); |
| 145 | break; |
| 146 | case pos_ews_by_host_by_event_name::VIEW_RWLOCK: |
| 147 | instr_class = find_rwlock_class(m_pos.m_index_3); |
| 148 | break; |
| 149 | case pos_ews_by_host_by_event_name::VIEW_COND: |
| 150 | instr_class = find_cond_class(m_pos.m_index_3); |
| 151 | break; |
| 152 | case pos_ews_by_host_by_event_name::VIEW_FILE: |
| 153 | instr_class = find_file_class(m_pos.m_index_3); |
| 154 | break; |
| 155 | case pos_ews_by_host_by_event_name::VIEW_TABLE: |
| 156 | instr_class = find_table_class(m_pos.m_index_3); |
| 157 | break; |
| 158 | case pos_ews_by_host_by_event_name::VIEW_SOCKET: |
| 159 | instr_class = find_socket_class(m_pos.m_index_3); |
| 160 | break; |
| 161 | case pos_ews_by_host_by_event_name::VIEW_IDLE: |
| 162 | instr_class = find_idle_class(m_pos.m_index_3); |
| 163 | break; |
| 164 | case pos_ews_by_host_by_event_name::VIEW_METADATA: |
| 165 | instr_class = find_metadata_class(m_pos.m_index_3); |
| 166 | break; |
| 167 | default: |
| 168 | instr_class = nullptr; |
| 169 | assert(false); |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | if (instr_class) { |
| 174 | m_next_pos.set_after(&m_pos); |
| 175 | return make_row(host, instr_class); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return HA_ERR_END_OF_FILE; |
| 182 | } |
| 183 | |
| 184 | int table_ews_by_host_by_event_name::rnd_pos(const void *pos) { |
| 185 | PFS_host *host; |
| 186 | PFS_instr_class *instr_class; |
| 187 | |
| 188 | set_position(pos); |
| 189 | |
| 190 | host = global_host_container.get(m_pos.m_index_1); |
| 191 | if (host == nullptr) { |
| 192 | return HA_ERR_RECORD_DELETED; |
| 193 | } |
| 194 | |
| 195 | switch (m_pos.m_index_2) { |
| 196 | case pos_ews_by_host_by_event_name::VIEW_MUTEX: |
| 197 | instr_class = find_mutex_class(m_pos.m_index_3); |
| 198 | break; |
| 199 | case pos_ews_by_host_by_event_name::VIEW_RWLOCK: |
| 200 | instr_class = find_rwlock_class(m_pos.m_index_3); |
| 201 | break; |
| 202 | case pos_ews_by_host_by_event_name::VIEW_COND: |
| 203 | instr_class = find_cond_class(m_pos.m_index_3); |
| 204 | break; |
| 205 | case pos_ews_by_host_by_event_name::VIEW_FILE: |
| 206 | instr_class = find_file_class(m_pos.m_index_3); |
| 207 | break; |
| 208 | case pos_ews_by_host_by_event_name::VIEW_TABLE: |
| 209 | instr_class = find_table_class(m_pos.m_index_3); |
| 210 | break; |
| 211 | case pos_ews_by_host_by_event_name::VIEW_SOCKET: |
| 212 | instr_class = find_socket_class(m_pos.m_index_3); |
| 213 | break; |
| 214 | case pos_ews_by_host_by_event_name::VIEW_IDLE: |
| 215 | instr_class = find_idle_class(m_pos.m_index_3); |
| 216 | break; |
| 217 | case pos_ews_by_host_by_event_name::VIEW_METADATA: |
| 218 | instr_class = find_metadata_class(m_pos.m_index_3); |
| 219 | break; |
| 220 | default: |
| 221 | instr_class = nullptr; |
| 222 | assert(false); |
| 223 | break; |
| 224 | } |
| 225 | |
| 226 | if (instr_class) { |
| 227 | return make_row(host, instr_class); |
| 228 | } |
| 229 | |
| 230 | return HA_ERR_RECORD_DELETED; |
| 231 | } |
| 232 | |
| 233 | int table_ews_by_host_by_event_name::index_init(uint idx [[maybe_unused]], |
| 234 | bool) { |
| 235 | PFS_index_ews_by_host_by_event_name *result = nullptr; |
| 236 | assert(idx == 0); |
| 237 | result = PFS_NEW(PFS_index_ews_by_host_by_event_name); |
| 238 | m_opened_index = result; |
| 239 | m_index = result; |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | int table_ews_by_host_by_event_name::index_next() { |
| 244 | PFS_host *host; |
| 245 | PFS_instr_class *instr_class; |
| 246 | bool has_more_host = true; |
| 247 | |
| 248 | for (m_pos.set_at(&m_next_pos); has_more_host; m_pos.next_host()) { |
| 249 | host = global_host_container.get(m_pos.m_index_1, &has_more_host); |
| 250 | if (host != nullptr) { |
| 251 | if (m_opened_index->match(host)) { |
| 252 | for (; m_pos.has_more_view(); m_pos.next_view()) { |
| 253 | if (!m_opened_index->match_view(m_pos.m_index_2)) { |
| 254 | continue; |
| 255 | } |
| 256 | |
| 257 | do { |
| 258 | switch (m_pos.m_index_2) { |
| 259 | case pos_ews_by_host_by_event_name::VIEW_MUTEX: |
| 260 | instr_class = find_mutex_class(m_pos.m_index_3); |
| 261 | break; |
| 262 | case pos_ews_by_host_by_event_name::VIEW_RWLOCK: |
| 263 | instr_class = find_rwlock_class(m_pos.m_index_3); |
| 264 | break; |
| 265 | case pos_ews_by_host_by_event_name::VIEW_COND: |
| 266 | instr_class = find_cond_class(m_pos.m_index_3); |
| 267 | break; |
| 268 | case pos_ews_by_host_by_event_name::VIEW_FILE: |
| 269 | instr_class = find_file_class(m_pos.m_index_3); |
| 270 | break; |
| 271 | case pos_ews_by_host_by_event_name::VIEW_TABLE: |
| 272 | instr_class = find_table_class(m_pos.m_index_3); |
| 273 | break; |
| 274 | case pos_ews_by_host_by_event_name::VIEW_SOCKET: |
| 275 | instr_class = find_socket_class(m_pos.m_index_3); |
| 276 | break; |
| 277 | case pos_ews_by_host_by_event_name::VIEW_IDLE: |
| 278 | instr_class = find_idle_class(m_pos.m_index_3); |
| 279 | break; |
| 280 | case pos_ews_by_host_by_event_name::VIEW_METADATA: |
| 281 | instr_class = find_metadata_class(m_pos.m_index_3); |
| 282 | break; |
| 283 | default: |
| 284 | instr_class = nullptr; |
| 285 | assert(false); |
| 286 | break; |
| 287 | } |
| 288 | |
| 289 | if (instr_class != nullptr) { |
| 290 | if (m_opened_index->match(instr_class)) { |
| 291 | if (!make_row(host, instr_class)) { |
| 292 | m_next_pos.set_after(&m_pos); |
| 293 | return 0; |
| 294 | } |
| 295 | } |
| 296 | m_pos.set_after(&m_pos); |
| 297 | } |
| 298 | } while (instr_class != nullptr); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | return HA_ERR_END_OF_FILE; |
| 305 | } |
| 306 | |
| 307 | int table_ews_by_host_by_event_name::make_row(PFS_host *host, |
| 308 | PFS_instr_class *klass) { |
| 309 | time_normalizer *normalizer = m_normalizer; |
| 310 | pfs_optimistic_state lock; |
| 311 | |
| 312 | host->m_lock.begin_optimistic_lock(&lock); |
| 313 | |
| 314 | if (m_row.m_host.make_row(host)) { |
| 315 | return HA_ERR_RECORD_DELETED; |
| 316 | } |
| 317 | |
| 318 | m_row.m_event_name.make_row(klass); |
| 319 | |
| 320 | PFS_connection_wait_visitor visitor(klass); |
| 321 | PFS_connection_iterator::visit_host(host, true, /* accounts */ |
| 322 | true, /* threads */ |
| 323 | false, /* THDs */ |
| 324 | &visitor); |
| 325 | |
| 326 | if (!host->m_lock.end_optimistic_lock(&lock)) { |
| 327 | return HA_ERR_RECORD_DELETED; |
| 328 | } |
| 329 | |
| 330 | if (klass->m_type == PFS_CLASS_IDLE) { |
| 331 | normalizer = time_normalizer::get_idle(); |
| 332 | } |
| 333 | |
| 334 | m_row.m_stat.set(normalizer, &visitor.m_stat); |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | int table_ews_by_host_by_event_name::read_row_values(TABLE *table, |
| 340 | unsigned char *buf, |
| 341 | Field **fields, |
| 342 | bool read_all) { |
| 343 | Field *f; |
| 344 | |
| 345 | /* Set the null bits */ |
| 346 | assert(table->s->null_bytes == 1); |
| 347 | buf[0] = 0; |
| 348 | |
| 349 | for (; (f = *fields); fields++) { |
| 350 | if (read_all || bitmap_is_set(table->read_set, f->field_index())) { |
| 351 | switch (f->field_index()) { |
| 352 | case 0: /* HOST */ |
| 353 | m_row.m_host.set_nullable_field(f); |
| 354 | break; |
| 355 | case 1: /* EVENT_NAME */ |
| 356 | m_row.m_event_name.set_field(f); |
| 357 | break; |
| 358 | default: /* 2, ... COUNT/SUM/MIN/AVG/MAX */ |
| 359 | m_row.m_stat.set_field(f->field_index() - 2, f); |
| 360 | break; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | return 0; |
| 366 | } |