| 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_ews_global_by_event_name.cc |
| 26 | Table EVENTS_WAITS_SUMMARY_GLOBAL_BY_EVENT_NAME (implementation). |
| 27 | */ |
| 28 | |
| 29 | #include "storage/perfschema/table_ews_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_ews_global_by_event_name::m_table_lock; |
| 47 | |
| 48 | Plugin_table table_ews_global_by_event_name::m_table_def( |
| 49 | /* Schema name */ |
| 50 | "performance_schema", |
| 51 | /* Name */ |
| 52 | "events_waits_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_ews_global_by_event_name::m_share = { |
| 67 | &pfs_truncatable_acl, |
| 68 | table_ews_global_by_event_name::create, |
| 69 | nullptr, /* write_row */ |
| 70 | table_ews_global_by_event_name::delete_all_rows, |
| 71 | table_ews_global_by_event_name::get_row_count, |
| 72 | sizeof(pos_ews_global_by_event_name), |
| 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_ews_global_by_event_name::match_view(uint view) { |
| 82 | if (m_fields >= 1) { |
| 83 | return m_key.match_view(view); |
| 84 | } |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | bool PFS_index_ews_global_by_event_name::match(PFS_instr_class *instr_class) { |
| 89 | if (m_fields >= 1) { |
| 90 | if (!m_key.match(instr_class)) { |
| 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | PFS_engine_table *table_ews_global_by_event_name::create( |
| 98 | PFS_engine_table_share *) { |
| 99 | return new table_ews_global_by_event_name(); |
| 100 | } |
| 101 | |
| 102 | int table_ews_global_by_event_name::delete_all_rows() { |
| 103 | reset_events_waits_by_instance(); |
| 104 | reset_table_waits_by_table_handle(); |
| 105 | reset_table_waits_by_table(); |
| 106 | reset_events_waits_by_class(); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | ha_rows table_ews_global_by_event_name::get_row_count() { |
| 111 | return wait_class_max; |
| 112 | } |
| 113 | |
| 114 | table_ews_global_by_event_name::table_ews_global_by_event_name() |
| 115 | : PFS_engine_table(&m_share, &m_pos), m_pos(), m_next_pos() { |
| 116 | // For all cases except IDLE |
| 117 | m_normalizer = time_normalizer::get_wait(); |
| 118 | } |
| 119 | |
| 120 | void table_ews_global_by_event_name::reset_position() { |
| 121 | m_pos.reset(); |
| 122 | m_next_pos.reset(); |
| 123 | } |
| 124 | |
| 125 | int table_ews_global_by_event_name::rnd_next() { |
| 126 | PFS_mutex_class *mutex_class; |
| 127 | PFS_rwlock_class *rwlock_class; |
| 128 | PFS_cond_class *cond_class; |
| 129 | PFS_file_class *file_class; |
| 130 | PFS_socket_class *socket_class; |
| 131 | PFS_instr_class *instr_class; |
| 132 | |
| 133 | for (m_pos.set_at(&m_next_pos); m_pos.has_more_view(); m_pos.next_view()) { |
| 134 | switch (m_pos.m_index_1) { |
| 135 | case pos_ews_global_by_event_name::VIEW_MUTEX: |
| 136 | mutex_class = find_mutex_class(m_pos.m_index_2); |
| 137 | if (mutex_class) { |
| 138 | m_next_pos.set_after(&m_pos); |
| 139 | return make_mutex_row(mutex_class); |
| 140 | } |
| 141 | break; |
| 142 | case pos_ews_global_by_event_name::VIEW_RWLOCK: |
| 143 | rwlock_class = find_rwlock_class(m_pos.m_index_2); |
| 144 | if (rwlock_class) { |
| 145 | m_next_pos.set_after(&m_pos); |
| 146 | return make_rwlock_row(rwlock_class); |
| 147 | } |
| 148 | break; |
| 149 | case pos_ews_global_by_event_name::VIEW_COND: |
| 150 | cond_class = find_cond_class(m_pos.m_index_2); |
| 151 | if (cond_class) { |
| 152 | m_next_pos.set_after(&m_pos); |
| 153 | return make_cond_row(cond_class); |
| 154 | } |
| 155 | break; |
| 156 | case pos_ews_global_by_event_name::VIEW_FILE: |
| 157 | file_class = find_file_class(m_pos.m_index_2); |
| 158 | if (file_class) { |
| 159 | m_next_pos.set_after(&m_pos); |
| 160 | return make_file_row(file_class); |
| 161 | } |
| 162 | break; |
| 163 | case pos_ews_global_by_event_name::VIEW_TABLE: |
| 164 | if (m_pos.m_index_2 == 1) { |
| 165 | m_next_pos.set_after(&m_pos); |
| 166 | return make_table_io_row(&global_table_io_class); |
| 167 | } |
| 168 | if (m_pos.m_index_2 == 2) { |
| 169 | m_next_pos.set_after(&m_pos); |
| 170 | return make_table_lock_row(&global_table_lock_class); |
| 171 | } |
| 172 | break; |
| 173 | case pos_ews_global_by_event_name::VIEW_SOCKET: |
| 174 | socket_class = find_socket_class(m_pos.m_index_2); |
| 175 | if (socket_class) { |
| 176 | m_next_pos.set_after(&m_pos); |
| 177 | return make_socket_row(socket_class); |
| 178 | } |
| 179 | break; |
| 180 | case pos_ews_global_by_event_name::VIEW_IDLE: |
| 181 | instr_class = find_idle_class(m_pos.m_index_2); |
| 182 | if (instr_class) { |
| 183 | m_next_pos.set_after(&m_pos); |
| 184 | return make_idle_row(instr_class); |
| 185 | } |
| 186 | break; |
| 187 | case pos_ews_global_by_event_name::VIEW_METADATA: |
| 188 | instr_class = find_metadata_class(m_pos.m_index_2); |
| 189 | if (instr_class) { |
| 190 | m_next_pos.set_after(&m_pos); |
| 191 | return make_metadata_row(instr_class); |
| 192 | } |
| 193 | break; |
| 194 | default: |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return HA_ERR_END_OF_FILE; |
| 200 | } |
| 201 | |
| 202 | int table_ews_global_by_event_name::rnd_pos(const void *pos) { |
| 203 | PFS_mutex_class *mutex_class; |
| 204 | PFS_rwlock_class *rwlock_class; |
| 205 | PFS_cond_class *cond_class; |
| 206 | PFS_file_class *file_class; |
| 207 | PFS_socket_class *socket_class; |
| 208 | PFS_instr_class *instr_class; |
| 209 | |
| 210 | set_position(pos); |
| 211 | |
| 212 | switch (m_pos.m_index_1) { |
| 213 | case pos_ews_global_by_event_name::VIEW_MUTEX: |
| 214 | mutex_class = find_mutex_class(m_pos.m_index_2); |
| 215 | if (mutex_class) { |
| 216 | return make_mutex_row(mutex_class); |
| 217 | } |
| 218 | break; |
| 219 | case pos_ews_global_by_event_name::VIEW_RWLOCK: |
| 220 | rwlock_class = find_rwlock_class(m_pos.m_index_2); |
| 221 | if (rwlock_class) { |
| 222 | return make_rwlock_row(rwlock_class); |
| 223 | } |
| 224 | break; |
| 225 | case pos_ews_global_by_event_name::VIEW_COND: |
| 226 | cond_class = find_cond_class(m_pos.m_index_2); |
| 227 | if (cond_class) { |
| 228 | return make_cond_row(cond_class); |
| 229 | } |
| 230 | break; |
| 231 | case pos_ews_global_by_event_name::VIEW_FILE: |
| 232 | file_class = find_file_class(m_pos.m_index_2); |
| 233 | if (file_class) { |
| 234 | return make_file_row(file_class); |
| 235 | } |
| 236 | break; |
| 237 | case pos_ews_global_by_event_name::VIEW_TABLE: |
| 238 | assert(m_pos.m_index_2 >= 1); |
| 239 | assert(m_pos.m_index_2 <= 2); |
| 240 | if (m_pos.m_index_2 == 1) { |
| 241 | return make_table_io_row(&global_table_io_class); |
| 242 | } |
| 243 | return make_table_lock_row(&global_table_lock_class); |
| 244 | break; |
| 245 | case pos_ews_global_by_event_name::VIEW_SOCKET: |
| 246 | socket_class = find_socket_class(m_pos.m_index_2); |
| 247 | if (socket_class) { |
| 248 | return make_socket_row(socket_class); |
| 249 | } |
| 250 | break; |
| 251 | case pos_ews_global_by_event_name::VIEW_IDLE: |
| 252 | instr_class = find_idle_class(m_pos.m_index_2); |
| 253 | if (instr_class) { |
| 254 | return make_idle_row(instr_class); |
| 255 | } |
| 256 | break; |
| 257 | case pos_ews_global_by_event_name::VIEW_METADATA: |
| 258 | instr_class = find_metadata_class(m_pos.m_index_2); |
| 259 | if (instr_class) { |
| 260 | return make_metadata_row(instr_class); |
| 261 | } |
| 262 | break; |
| 263 | default: |
| 264 | assert(false); |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | return HA_ERR_RECORD_DELETED; |
| 269 | } |
| 270 | |
| 271 | int table_ews_global_by_event_name::index_init(uint idx [[maybe_unused]], |
| 272 | bool) { |
| 273 | PFS_index_ews_global_by_event_name *result = nullptr; |
| 274 | assert(idx == 0); |
| 275 | result = PFS_NEW(PFS_index_ews_global_by_event_name); |
| 276 | m_opened_index = result; |
| 277 | m_index = result; |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | int table_ews_global_by_event_name::index_next() { |
| 282 | PFS_mutex_class *mutex_class; |
| 283 | PFS_rwlock_class *rwlock_class; |
| 284 | PFS_cond_class *cond_class; |
| 285 | PFS_file_class *file_class; |
| 286 | PFS_instr_class *table_class; |
| 287 | PFS_socket_class *socket_class; |
| 288 | PFS_instr_class *instr_class; |
| 289 | |
| 290 | for (m_pos.set_at(&m_next_pos); m_pos.has_more_view(); m_pos.next_view()) { |
| 291 | if (!m_opened_index->match_view(m_pos.m_index_1)) { |
| 292 | continue; |
| 293 | } |
| 294 | |
| 295 | switch (m_pos.m_index_1) { |
| 296 | case pos_ews_global_by_event_name::VIEW_MUTEX: |
| 297 | do { |
| 298 | mutex_class = find_mutex_class(m_pos.m_index_2); |
| 299 | if (mutex_class) { |
| 300 | if (m_opened_index->match(mutex_class)) { |
| 301 | m_next_pos.set_after(&m_pos); |
| 302 | return make_mutex_row(mutex_class); |
| 303 | } |
| 304 | m_pos.set_after(&m_pos); |
| 305 | } |
| 306 | } while (mutex_class != nullptr); |
| 307 | break; |
| 308 | case pos_ews_global_by_event_name::VIEW_RWLOCK: |
| 309 | do { |
| 310 | rwlock_class = find_rwlock_class(m_pos.m_index_2); |
| 311 | if (rwlock_class) { |
| 312 | if (m_opened_index->match(rwlock_class)) { |
| 313 | m_next_pos.set_after(&m_pos); |
| 314 | return make_rwlock_row(rwlock_class); |
| 315 | } |
| 316 | m_pos.set_after(&m_pos); |
| 317 | } |
| 318 | } while (rwlock_class != nullptr); |
| 319 | |
| 320 | break; |
| 321 | case pos_ews_global_by_event_name::VIEW_COND: |
| 322 | do { |
| 323 | cond_class = find_cond_class(m_pos.m_index_2); |
| 324 | if (cond_class) { |
| 325 | if (m_opened_index->match(cond_class)) { |
| 326 | m_next_pos.set_after(&m_pos); |
| 327 | return make_cond_row(cond_class); |
| 328 | } |
| 329 | m_pos.set_after(&m_pos); |
| 330 | } |
| 331 | } while (cond_class != nullptr); |
| 332 | break; |
| 333 | case pos_ews_global_by_event_name::VIEW_FILE: |
| 334 | do { |
| 335 | file_class = find_file_class(m_pos.m_index_2); |
| 336 | if (file_class) { |
| 337 | if (m_opened_index->match(file_class)) { |
| 338 | m_next_pos.set_after(&m_pos); |
| 339 | return make_file_row(file_class); |
| 340 | } |
| 341 | m_pos.set_after(&m_pos); |
| 342 | } |
| 343 | } while (file_class != nullptr); |
| 344 | break; |
| 345 | case pos_ews_global_by_event_name::VIEW_TABLE: |
| 346 | do { |
| 347 | table_class = find_table_class(m_pos.m_index_2); |
| 348 | if (table_class) { |
| 349 | if (m_opened_index->match(table_class)) { |
| 350 | m_next_pos.set_after(&m_pos); |
| 351 | if (m_pos.m_index_2 == 1) { |
| 352 | return make_table_io_row(table_class); |
| 353 | } |
| 354 | return make_table_lock_row(table_class); |
| 355 | } |
| 356 | m_pos.set_after(&m_pos); |
| 357 | } |
| 358 | } while (table_class != nullptr); |
| 359 | break; |
| 360 | case pos_ews_global_by_event_name::VIEW_SOCKET: |
| 361 | do { |
| 362 | socket_class = find_socket_class(m_pos.m_index_2); |
| 363 | if (socket_class) { |
| 364 | if (m_opened_index->match(socket_class)) { |
| 365 | m_next_pos.set_after(&m_pos); |
| 366 | return make_socket_row(socket_class); |
| 367 | } |
| 368 | m_pos.set_after(&m_pos); |
| 369 | } |
| 370 | } while (socket_class != nullptr); |
| 371 | break; |
| 372 | case pos_ews_global_by_event_name::VIEW_IDLE: |
| 373 | do { |
| 374 | instr_class = find_idle_class(m_pos.m_index_2); |
| 375 | if (instr_class) { |
| 376 | if (m_opened_index->match(instr_class)) { |
| 377 | m_next_pos.set_after(&m_pos); |
| 378 | return make_idle_row(instr_class); |
| 379 | } |
| 380 | m_pos.set_after(&m_pos); |
| 381 | } |
| 382 | } while (instr_class != nullptr); |
| 383 | break; |
| 384 | case pos_ews_global_by_event_name::VIEW_METADATA: |
| 385 | do { |
| 386 | instr_class = find_metadata_class(m_pos.m_index_2); |
| 387 | if (instr_class) { |
| 388 | if (m_opened_index->match(instr_class)) { |
| 389 | m_next_pos.set_after(&m_pos); |
| 390 | return make_metadata_row(instr_class); |
| 391 | } |
| 392 | m_pos.set_after(&m_pos); |
| 393 | } |
| 394 | } while (instr_class != nullptr); |
| 395 | break; |
| 396 | default: |
| 397 | break; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | return HA_ERR_END_OF_FILE; |
| 402 | } |
| 403 | |
| 404 | int table_ews_global_by_event_name::make_mutex_row(PFS_mutex_class *klass) { |
| 405 | m_row.m_event_name.make_row(klass); |
| 406 | |
| 407 | PFS_instance_wait_visitor visitor; |
| 408 | PFS_instance_iterator::visit_mutex_instances(klass, &visitor); |
| 409 | |
| 410 | m_row.m_stat.set(m_normalizer, &visitor.m_stat); |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | int table_ews_global_by_event_name::make_rwlock_row(PFS_rwlock_class *klass) { |
| 415 | m_row.m_event_name.make_row(klass); |
| 416 | |
| 417 | PFS_instance_wait_visitor visitor; |
| 418 | PFS_instance_iterator::visit_rwlock_instances(klass, &visitor); |
| 419 | |
| 420 | m_row.m_stat.set(m_normalizer, &visitor.m_stat); |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | int table_ews_global_by_event_name::make_cond_row(PFS_cond_class *klass) { |
| 425 | m_row.m_event_name.make_row(klass); |
| 426 | |
| 427 | PFS_instance_wait_visitor visitor; |
| 428 | PFS_instance_iterator::visit_cond_instances(klass, &visitor); |
| 429 | |
| 430 | m_row.m_stat.set(m_normalizer, &visitor.m_stat); |
| 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | int table_ews_global_by_event_name::make_file_row(PFS_file_class *klass) { |
| 435 | m_row.m_event_name.make_row(klass); |
| 436 | |
| 437 | PFS_instance_wait_visitor visitor; |
| 438 | PFS_instance_iterator::visit_file_instances(klass, &visitor); |
| 439 | |
| 440 | m_row.m_stat.set(m_normalizer, &visitor.m_stat); |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | int table_ews_global_by_event_name::make_table_io_row(PFS_instr_class *klass) { |
| 445 | m_row.m_event_name.make_row(klass); |
| 446 | |
| 447 | PFS_table_io_wait_visitor visitor; |
| 448 | PFS_object_iterator::visit_all_tables(&visitor); |
| 449 | |
| 450 | m_row.m_stat.set(m_normalizer, &visitor.m_stat); |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | int table_ews_global_by_event_name::make_table_lock_row( |
| 455 | PFS_instr_class *klass) { |
| 456 | m_row.m_event_name.make_row(klass); |
| 457 | |
| 458 | PFS_table_lock_wait_visitor visitor; |
| 459 | PFS_object_iterator::visit_all_tables(&visitor); |
| 460 | |
| 461 | m_row.m_stat.set(m_normalizer, &visitor.m_stat); |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | int table_ews_global_by_event_name::make_socket_row(PFS_socket_class *klass) { |
| 466 | m_row.m_event_name.make_row(klass); |
| 467 | |
| 468 | PFS_instance_wait_visitor visitor; |
| 469 | PFS_instance_iterator::visit_socket_instances(klass, &visitor); |
| 470 | |
| 471 | m_row.m_stat.set(m_normalizer, &visitor.m_stat); |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | int table_ews_global_by_event_name::make_idle_row(PFS_instr_class *klass) { |
| 476 | m_row.m_event_name.make_row(klass); |
| 477 | |
| 478 | PFS_connection_wait_visitor visitor(klass); |
| 479 | PFS_connection_iterator::visit_global(false, /* hosts */ |
| 480 | false, /* users */ |
| 481 | false, /* accounts */ |
| 482 | true, /* threads */ |
| 483 | false, /* THDs */ |
| 484 | &visitor); |
| 485 | |
| 486 | time_normalizer *normalizer = time_normalizer::get_idle(); |
| 487 | m_row.m_stat.set(normalizer, &visitor.m_stat); |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | int table_ews_global_by_event_name::make_metadata_row(PFS_instr_class *klass) { |
| 492 | m_row.m_event_name.make_row(klass); |
| 493 | |
| 494 | PFS_connection_wait_visitor visitor(klass); |
| 495 | PFS_connection_iterator::visit_global(false, /* hosts */ |
| 496 | true, /* users */ |
| 497 | true, /* accounts */ |
| 498 | true, /* threads */ |
| 499 | false, /* THDs */ |
| 500 | &visitor); |
| 501 | m_row.m_stat.set(m_normalizer, &visitor.m_stat); |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | int table_ews_global_by_event_name::read_row_values(TABLE *table, |
| 506 | unsigned char *, |
| 507 | Field **fields, |
| 508 | bool read_all) { |
| 509 | Field *f; |
| 510 | |
| 511 | /* Set the null bits */ |
| 512 | assert(table->s->null_bytes == 0); |
| 513 | |
| 514 | for (; (f = *fields); fields++) { |
| 515 | if (read_all || bitmap_is_set(table->read_set, f->field_index())) { |
| 516 | switch (f->field_index()) { |
| 517 | case 0: /* EVENT_NAME */ |
| 518 | m_row.m_event_name.set_field(f); |
| 519 | break; |
| 520 | default: /* 1, ... COUNT/SUM/MIN/AVG/MAX */ |
| 521 | m_row.m_stat.set_field(f->field_index() - 1, f); |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | return 0; |
| 528 | } |