| 1 | /* Copyright (c) 2008, 2011, 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 Foundation, |
| 14 | 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */ |
| 15 | |
| 16 | /** |
| 17 | @file storage/perfschema/table_setup_instruments.cc |
| 18 | Table SETUP_INSTRUMENTS (implementation). |
| 19 | */ |
| 20 | |
| 21 | #include "my_global.h" |
| 22 | #include "my_pthread.h" |
| 23 | #include "pfs_instr_class.h" |
| 24 | #include "pfs_instr.h" |
| 25 | #include "pfs_column_types.h" |
| 26 | #include "pfs_column_values.h" |
| 27 | #include "table_setup_instruments.h" |
| 28 | #include "pfs_global.h" |
| 29 | #include "pfs_setup_object.h" |
| 30 | |
| 31 | THR_LOCK table_setup_instruments::m_table_lock; |
| 32 | |
| 33 | PFS_engine_table_share |
| 34 | table_setup_instruments::m_share= |
| 35 | { |
| 36 | { C_STRING_WITH_LEN("setup_instruments" ) }, |
| 37 | &pfs_updatable_acl, |
| 38 | &table_setup_instruments::create, |
| 39 | NULL, /* write_row */ |
| 40 | NULL, /* delete_all_rows */ |
| 41 | NULL, /* get_row_count */ |
| 42 | 1000, /* records */ |
| 43 | sizeof(pos_setup_instruments), |
| 44 | &m_table_lock, |
| 45 | { C_STRING_WITH_LEN("CREATE TABLE setup_instruments(" |
| 46 | "NAME VARCHAR(128) not null," |
| 47 | "ENABLED ENUM ('YES', 'NO') not null," |
| 48 | "TIMED ENUM ('YES', 'NO') not null)" ) } |
| 49 | }; |
| 50 | |
| 51 | PFS_engine_table* table_setup_instruments::create(void) |
| 52 | { |
| 53 | return new table_setup_instruments(); |
| 54 | } |
| 55 | |
| 56 | table_setup_instruments::table_setup_instruments() |
| 57 | : PFS_engine_table(&m_share, &m_pos), |
| 58 | m_pos(), m_next_pos() |
| 59 | {} |
| 60 | |
| 61 | void table_setup_instruments::reset_position(void) |
| 62 | { |
| 63 | m_pos.reset(); |
| 64 | m_next_pos.reset(); |
| 65 | } |
| 66 | |
| 67 | int table_setup_instruments::rnd_next(void) |
| 68 | { |
| 69 | PFS_instr_class *instr_class= NULL; |
| 70 | |
| 71 | /* Do not advertise hard coded instruments when disabled. */ |
| 72 | if (! pfs_initialized) |
| 73 | return HA_ERR_END_OF_FILE; |
| 74 | |
| 75 | for (m_pos.set_at(&m_next_pos); |
| 76 | m_pos.has_more_view(); |
| 77 | m_pos.next_view()) |
| 78 | { |
| 79 | switch (m_pos.m_index_1) |
| 80 | { |
| 81 | case pos_setup_instruments::VIEW_MUTEX: |
| 82 | instr_class= find_mutex_class(m_pos.m_index_2); |
| 83 | break; |
| 84 | case pos_setup_instruments::VIEW_RWLOCK: |
| 85 | instr_class= find_rwlock_class(m_pos.m_index_2); |
| 86 | break; |
| 87 | case pos_setup_instruments::VIEW_COND: |
| 88 | instr_class= find_cond_class(m_pos.m_index_2); |
| 89 | break; |
| 90 | case pos_setup_instruments::VIEW_THREAD: |
| 91 | /* Not used yet */ |
| 92 | break; |
| 93 | case pos_setup_instruments::VIEW_FILE: |
| 94 | instr_class= find_file_class(m_pos.m_index_2); |
| 95 | break; |
| 96 | case pos_setup_instruments::VIEW_TABLE: |
| 97 | instr_class= find_table_class(m_pos.m_index_2); |
| 98 | break; |
| 99 | case pos_setup_instruments::VIEW_STAGE: |
| 100 | instr_class= find_stage_class(m_pos.m_index_2); |
| 101 | break; |
| 102 | case pos_setup_instruments::VIEW_STATEMENT: |
| 103 | instr_class= find_statement_class(m_pos.m_index_2); |
| 104 | break; |
| 105 | case pos_setup_instruments::VIEW_SOCKET: |
| 106 | instr_class= find_socket_class(m_pos.m_index_2); |
| 107 | break; |
| 108 | case pos_setup_instruments::VIEW_IDLE: |
| 109 | instr_class= find_idle_class(m_pos.m_index_2); |
| 110 | break; |
| 111 | } |
| 112 | if (instr_class) |
| 113 | { |
| 114 | make_row(instr_class); |
| 115 | m_next_pos.set_after(&m_pos); |
| 116 | return 0; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return HA_ERR_END_OF_FILE; |
| 121 | } |
| 122 | |
| 123 | int table_setup_instruments::rnd_pos(const void *pos) |
| 124 | { |
| 125 | PFS_instr_class *instr_class= NULL; |
| 126 | |
| 127 | /* Do not advertise hard coded instruments when disabled. */ |
| 128 | if (! pfs_initialized) |
| 129 | return HA_ERR_END_OF_FILE; |
| 130 | |
| 131 | set_position(pos); |
| 132 | |
| 133 | switch (m_pos.m_index_1) |
| 134 | { |
| 135 | case pos_setup_instruments::VIEW_MUTEX: |
| 136 | instr_class= find_mutex_class(m_pos.m_index_2); |
| 137 | break; |
| 138 | case pos_setup_instruments::VIEW_RWLOCK: |
| 139 | instr_class= find_rwlock_class(m_pos.m_index_2); |
| 140 | break; |
| 141 | case pos_setup_instruments::VIEW_COND: |
| 142 | instr_class= find_cond_class(m_pos.m_index_2); |
| 143 | break; |
| 144 | case pos_setup_instruments::VIEW_THREAD: |
| 145 | /* Not used yet */ |
| 146 | break; |
| 147 | case pos_setup_instruments::VIEW_FILE: |
| 148 | instr_class= find_file_class(m_pos.m_index_2); |
| 149 | break; |
| 150 | case pos_setup_instruments::VIEW_TABLE: |
| 151 | instr_class= find_table_class(m_pos.m_index_2); |
| 152 | break; |
| 153 | case pos_setup_instruments::VIEW_STAGE: |
| 154 | instr_class= find_stage_class(m_pos.m_index_2); |
| 155 | break; |
| 156 | case pos_setup_instruments::VIEW_STATEMENT: |
| 157 | instr_class= find_statement_class(m_pos.m_index_2); |
| 158 | break; |
| 159 | case pos_setup_instruments::VIEW_SOCKET: |
| 160 | instr_class= find_socket_class(m_pos.m_index_2); |
| 161 | break; |
| 162 | case pos_setup_instruments::VIEW_IDLE: |
| 163 | instr_class= find_idle_class(m_pos.m_index_2); |
| 164 | break; |
| 165 | } |
| 166 | if (instr_class) |
| 167 | { |
| 168 | make_row(instr_class); |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | return HA_ERR_RECORD_DELETED; |
| 173 | } |
| 174 | |
| 175 | void table_setup_instruments::make_row(PFS_instr_class *klass) |
| 176 | { |
| 177 | m_row.m_instr_class= klass; |
| 178 | } |
| 179 | |
| 180 | int table_setup_instruments::read_row_values(TABLE *table, |
| 181 | unsigned char *, |
| 182 | Field **fields, |
| 183 | bool read_all) |
| 184 | { |
| 185 | Field *f; |
| 186 | |
| 187 | DBUG_ASSERT(table->s->null_bytes == 0); |
| 188 | |
| 189 | /* |
| 190 | The row always exist, the instrument classes |
| 191 | are static and never disappear. |
| 192 | */ |
| 193 | |
| 194 | for (; (f= *fields) ; fields++) |
| 195 | { |
| 196 | if (read_all || bitmap_is_set(table->read_set, f->field_index)) |
| 197 | { |
| 198 | switch(f->field_index) |
| 199 | { |
| 200 | case 0: /* NAME */ |
| 201 | set_field_varchar_utf8(f, m_row.m_instr_class->m_name, m_row.m_instr_class->m_name_length); |
| 202 | break; |
| 203 | case 1: /* ENABLED */ |
| 204 | set_field_enum(f, m_row.m_instr_class->m_enabled ? ENUM_YES : ENUM_NO); |
| 205 | break; |
| 206 | case 2: /* TIMED */ |
| 207 | set_field_enum(f, m_row.m_instr_class->m_timed ? ENUM_YES : ENUM_NO); |
| 208 | break; |
| 209 | default: |
| 210 | DBUG_ASSERT(false); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | int table_setup_instruments::update_row_values(TABLE *table, |
| 219 | const unsigned char *, |
| 220 | const unsigned char *, |
| 221 | Field **fields) |
| 222 | { |
| 223 | Field *f; |
| 224 | enum_yes_no value; |
| 225 | |
| 226 | for (; (f= *fields) ; fields++) |
| 227 | { |
| 228 | if (bitmap_is_set(table->write_set, f->field_index)) |
| 229 | { |
| 230 | switch(f->field_index) |
| 231 | { |
| 232 | case 0: /* NAME */ |
| 233 | return HA_ERR_WRONG_COMMAND; |
| 234 | case 1: /* ENABLED */ |
| 235 | value= (enum_yes_no) get_field_enum(f); |
| 236 | m_row.m_instr_class->m_enabled= (value == ENUM_YES) ? true : false; |
| 237 | break; |
| 238 | case 2: /* TIMED */ |
| 239 | value= (enum_yes_no) get_field_enum(f); |
| 240 | m_row.m_instr_class->m_timed= (value == ENUM_YES) ? true : false; |
| 241 | break; |
| 242 | default: |
| 243 | DBUG_ASSERT(false); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | switch (m_pos.m_index_1) |
| 249 | { |
| 250 | case pos_setup_instruments::VIEW_MUTEX: |
| 251 | update_mutex_derived_flags(); |
| 252 | break; |
| 253 | case pos_setup_instruments::VIEW_RWLOCK: |
| 254 | update_rwlock_derived_flags(); |
| 255 | break; |
| 256 | case pos_setup_instruments::VIEW_COND: |
| 257 | update_cond_derived_flags(); |
| 258 | break; |
| 259 | case pos_setup_instruments::VIEW_THREAD: |
| 260 | /* Not used yet */ |
| 261 | break; |
| 262 | case pos_setup_instruments::VIEW_FILE: |
| 263 | update_file_derived_flags(); |
| 264 | break; |
| 265 | case pos_setup_instruments::VIEW_TABLE: |
| 266 | update_table_derived_flags(); |
| 267 | break; |
| 268 | case pos_setup_instruments::VIEW_STAGE: |
| 269 | case pos_setup_instruments::VIEW_STATEMENT: |
| 270 | /* No flag to update. */ |
| 271 | break; |
| 272 | case pos_setup_instruments::VIEW_SOCKET: |
| 273 | update_socket_derived_flags(); |
| 274 | break; |
| 275 | case pos_setup_instruments::VIEW_IDLE: |
| 276 | /* No flag to update. */ |
| 277 | break; |
| 278 | default: |
| 279 | DBUG_ASSERT(false); |
| 280 | break; |
| 281 | } |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | |