| 1 | /* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
| 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 as published by |
| 5 | the Free Software Foundation; version 2 of the License. |
| 6 | |
| 7 | This program is distributed in the hope that it will be useful, |
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | GNU General Public License for more details. |
| 11 | |
| 12 | You should have received a copy of the GNU General Public License |
| 13 | along with this program; if not, write to the Free Software |
| 14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ |
| 15 | |
| 16 | /** |
| 17 | @file storage/perfschema/table_esms_by_user_by_event_name.cc |
| 18 | Table EVENTS_STATEMENTS_SUMMARY_BY_USER_BY_EVENT_NAME (implementation). |
| 19 | */ |
| 20 | |
| 21 | #include "my_global.h" |
| 22 | #include "my_pthread.h" |
| 23 | #include "pfs_instr_class.h" |
| 24 | #include "pfs_column_types.h" |
| 25 | #include "pfs_column_values.h" |
| 26 | #include "table_esms_by_user_by_event_name.h" |
| 27 | #include "pfs_global.h" |
| 28 | #include "pfs_account.h" |
| 29 | #include "pfs_visitor.h" |
| 30 | |
| 31 | THR_LOCK table_esms_by_user_by_event_name::m_table_lock; |
| 32 | |
| 33 | PFS_engine_table_share |
| 34 | table_esms_by_user_by_event_name::m_share= |
| 35 | { |
| 36 | { C_STRING_WITH_LEN("events_statements_summary_by_user_by_event_name" ) }, |
| 37 | &pfs_truncatable_acl, |
| 38 | table_esms_by_user_by_event_name::create, |
| 39 | NULL, /* write_row */ |
| 40 | table_esms_by_user_by_event_name::delete_all_rows, |
| 41 | NULL, /* get_row_count */ |
| 42 | 1000, /* records */ |
| 43 | sizeof(pos_esms_by_user_by_event_name), |
| 44 | &m_table_lock, |
| 45 | { C_STRING_WITH_LEN("CREATE TABLE events_statements_summary_by_user_by_event_name(" |
| 46 | "USER CHAR(16) collate utf8_bin default null," |
| 47 | "EVENT_NAME VARCHAR(128) not null," |
| 48 | "COUNT_STAR BIGINT unsigned not null," |
| 49 | "SUM_TIMER_WAIT BIGINT unsigned not null," |
| 50 | "MIN_TIMER_WAIT BIGINT unsigned not null," |
| 51 | "AVG_TIMER_WAIT BIGINT unsigned not null," |
| 52 | "MAX_TIMER_WAIT BIGINT unsigned not null," |
| 53 | "SUM_LOCK_TIME BIGINT unsigned not null," |
| 54 | "SUM_ERRORS BIGINT unsigned not null," |
| 55 | "SUM_WARNINGS BIGINT unsigned not null," |
| 56 | "SUM_ROWS_AFFECTED BIGINT unsigned not null," |
| 57 | "SUM_ROWS_SENT BIGINT unsigned not null," |
| 58 | "SUM_ROWS_EXAMINED BIGINT unsigned not null," |
| 59 | "SUM_CREATED_TMP_DISK_TABLES BIGINT unsigned not null," |
| 60 | "SUM_CREATED_TMP_TABLES BIGINT unsigned not null," |
| 61 | "SUM_SELECT_FULL_JOIN BIGINT unsigned not null," |
| 62 | "SUM_SELECT_FULL_RANGE_JOIN BIGINT unsigned not null," |
| 63 | "SUM_SELECT_RANGE BIGINT unsigned not null," |
| 64 | "SUM_SELECT_RANGE_CHECK BIGINT unsigned not null," |
| 65 | "SUM_SELECT_SCAN BIGINT unsigned not null," |
| 66 | "SUM_SORT_MERGE_PASSES BIGINT unsigned not null," |
| 67 | "SUM_SORT_RANGE BIGINT unsigned not null," |
| 68 | "SUM_SORT_ROWS BIGINT unsigned not null," |
| 69 | "SUM_SORT_SCAN BIGINT unsigned not null," |
| 70 | "SUM_NO_INDEX_USED BIGINT unsigned not null," |
| 71 | "SUM_NO_GOOD_INDEX_USED BIGINT unsigned not null)" ) } |
| 72 | }; |
| 73 | |
| 74 | PFS_engine_table* |
| 75 | table_esms_by_user_by_event_name::create(void) |
| 76 | { |
| 77 | return new table_esms_by_user_by_event_name(); |
| 78 | } |
| 79 | |
| 80 | int |
| 81 | table_esms_by_user_by_event_name::delete_all_rows(void) |
| 82 | { |
| 83 | reset_events_statements_by_thread(); |
| 84 | reset_events_statements_by_account(); |
| 85 | reset_events_statements_by_user(); |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | table_esms_by_user_by_event_name::table_esms_by_user_by_event_name() |
| 90 | : PFS_engine_table(&m_share, &m_pos), |
| 91 | m_row_exists(false), m_pos(), m_next_pos() |
| 92 | {} |
| 93 | |
| 94 | void table_esms_by_user_by_event_name::reset_position(void) |
| 95 | { |
| 96 | m_pos.reset(); |
| 97 | m_next_pos.reset(); |
| 98 | } |
| 99 | |
| 100 | int table_esms_by_user_by_event_name::rnd_init(bool scan) |
| 101 | { |
| 102 | m_normalizer= time_normalizer::get(statement_timer); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | int table_esms_by_user_by_event_name::rnd_next(void) |
| 107 | { |
| 108 | PFS_user *user; |
| 109 | PFS_statement_class *statement_class; |
| 110 | |
| 111 | for (m_pos.set_at(&m_next_pos); |
| 112 | m_pos.has_more_user(); |
| 113 | m_pos.next_user()) |
| 114 | { |
| 115 | user= &user_array[m_pos.m_index_1]; |
| 116 | if (user->m_lock.is_populated()) |
| 117 | { |
| 118 | statement_class= find_statement_class(m_pos.m_index_2); |
| 119 | if (statement_class) |
| 120 | { |
| 121 | make_row(user, statement_class); |
| 122 | m_next_pos.set_after(&m_pos); |
| 123 | return 0; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return HA_ERR_END_OF_FILE; |
| 129 | } |
| 130 | |
| 131 | int |
| 132 | table_esms_by_user_by_event_name::rnd_pos(const void *pos) |
| 133 | { |
| 134 | PFS_user *user; |
| 135 | PFS_statement_class *statement_class; |
| 136 | |
| 137 | set_position(pos); |
| 138 | DBUG_ASSERT(m_pos.m_index_1 < user_max); |
| 139 | |
| 140 | user= &user_array[m_pos.m_index_1]; |
| 141 | if (! user->m_lock.is_populated()) |
| 142 | return HA_ERR_RECORD_DELETED; |
| 143 | |
| 144 | statement_class= find_statement_class(m_pos.m_index_2); |
| 145 | if (statement_class) |
| 146 | { |
| 147 | make_row(user, statement_class); |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | return HA_ERR_RECORD_DELETED; |
| 152 | } |
| 153 | |
| 154 | void table_esms_by_user_by_event_name |
| 155 | ::make_row(PFS_user *user, PFS_statement_class *klass) |
| 156 | { |
| 157 | pfs_lock lock; |
| 158 | m_row_exists= false; |
| 159 | |
| 160 | if (klass->is_mutable()) |
| 161 | return; |
| 162 | |
| 163 | user->m_lock.begin_optimistic_lock(&lock); |
| 164 | |
| 165 | if (m_row.m_user.make_row(user)) |
| 166 | return; |
| 167 | |
| 168 | m_row.m_event_name.make_row(klass); |
| 169 | |
| 170 | PFS_connection_statement_visitor visitor(klass); |
| 171 | PFS_connection_iterator::visit_user(user, true, true, & visitor); |
| 172 | |
| 173 | if (! user->m_lock.end_optimistic_lock(&lock)) |
| 174 | return; |
| 175 | |
| 176 | m_row_exists= true; |
| 177 | m_row.m_stat.set(m_normalizer, & visitor.m_stat); |
| 178 | } |
| 179 | |
| 180 | int table_esms_by_user_by_event_name |
| 181 | ::read_row_values(TABLE *table, unsigned char *buf, Field **fields, |
| 182 | bool read_all) |
| 183 | { |
| 184 | Field *f; |
| 185 | |
| 186 | if (unlikely(! m_row_exists)) |
| 187 | return HA_ERR_RECORD_DELETED; |
| 188 | |
| 189 | /* Set the null bits */ |
| 190 | DBUG_ASSERT(table->s->null_bytes == 1); |
| 191 | buf[0]= 0; |
| 192 | |
| 193 | for (; (f= *fields) ; fields++) |
| 194 | { |
| 195 | if (read_all || bitmap_is_set(table->read_set, f->field_index)) |
| 196 | { |
| 197 | switch(f->field_index) |
| 198 | { |
| 199 | case 0: /* USER */ |
| 200 | m_row.m_user.set_field(f); |
| 201 | break; |
| 202 | case 1: /* EVENT_NAME */ |
| 203 | m_row.m_event_name.set_field(f); |
| 204 | break; |
| 205 | default: /* 2, ... COUNT/SUM/MIN/AVG/MAX */ |
| 206 | m_row.m_stat.set_field(f->field_index - 2, f); |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | |