| 1 | /* Copyright (c) 2013, 2023, 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 also distributed 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 included with MySQL. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License, version 2.0, for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program; if not, write to the Free Software Foundation, |
| 21 | 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */ |
| 22 | |
| 23 | /** |
| 24 | @file storage/perfschema/pfs_events_transactions.cc |
| 25 | Events transactions data structures (implementation). |
| 26 | */ |
| 27 | |
| 28 | #include "my_global.h"cxx . . $ `<file>/include/my_global.h`/ |
| 29 | #include "my_sys.h"cxx . . $ `<file>/include/my_sys.h`/ |
| 30 | #include "pfs_global.h"cxx . . $ `<file>/storage/perfschema/pfs_global.h`/ |
| 31 | #include "pfs_instr_class.h"cxx . . $ `<file>/storage/perfschema/pfs_instr_class.h`/ |
| 32 | #include "pfs_instr.h"cxx . . $ `<file>/storage/perfschema/pfs_instr.h`/ |
| 33 | #include "pfs_account.h"cxx . . $ `<file>/storage/perfschema/pfs_account.h`/ |
| 34 | #include "pfs_host.h"cxx . . $ `<file>/storage/perfschema/pfs_host.h`/ |
| 35 | #include "pfs_user.h"cxx . . $ `<file>/storage/perfschema/pfs_user.h`/ |
| 36 | #include "pfs_events_transactions.h"cxx . . $ `<file>/storage/perfschema/pfs_events_transactions.h`/ |
| 37 | #include "pfs_buffer_container.h"cxx . . $ `<file>/storage/perfschema/pfs_buffer_container.h`/ |
| 38 | #include "pfs_builtin_memory.h"cxx . . $ `<file>/storage/perfschema/pfs_builtin_memory.h`/ |
| 39 | #include "m_string.h"cxx . . $ `<file>/include/m_string.h`/ |
| 40 | |
| 41 | PFS_ALIGNEDcxx . . $ `storage/perfschema/pfs_global.h:41:9`! ulong events_transactions_history_long_sizecxx . . $ events_transactions_history_long_size.= 0; |
| 42 | /** Consumer flag for table EVENTS_TRANSACTIONS_CURRENT. */ |
| 43 | PFS_ALIGNEDcxx . . $ `storage/perfschema/pfs_global.h:41:9`! bool flag_events_transactions_currentcxx . . $ flag_events_transactions_current.= false; |
| 44 | /** Consumer flag for table EVENTS_TRANSACTIONS_HISTORY. */ |
| 45 | PFS_ALIGNEDcxx . . $ `storage/perfschema/pfs_global.h:41:9`! bool flag_events_transactions_historycxx . . $ flag_events_transactions_history.= false; |
| 46 | /** Consumer flag for table EVENTS_TRANSACTIONS_HISTORY_LONG. */ |
| 47 | PFS_ALIGNEDcxx . . $ `storage/perfschema/pfs_global.h:41:9`! bool flag_events_transactions_history_longcxx . . $ flag_events_transactions_history_long.= false; |
| 48 | |
| 49 | /** True if EVENTS_TRANSACTIONS_HISTORY_LONG circular buffer is full. */ |
| 50 | PFS_ALIGNEDcxx . . $ `storage/perfschema/pfs_global.h:41:9`! bool events_transactions_history_long_fullcxx . . $ events_transactions_history_long_full.= false; |
| 51 | /** Index in EVENTS_TRANSACTIONS_HISTORY_LONG circular buffer. */ |
| 52 | PFS_ALIGNEDcxx . . $ `storage/perfschema/pfs_global.h:41:9`! PFS_cacheline_uint32cxx . . $ PFS_cacheline_uint32# events_transactions_history_long_indexcxx . . $ events_transactions_history_long_index.; |
| 53 | /** EVENTS_TRANSACTIONS_HISTORY_LONG circular buffer. */ |
| 54 | PFS_ALIGNEDcxx . . $ `storage/perfschema/pfs_global.h:41:9`! PFS_events_transactionscxx . . $ PFS_events_transactions# *events_transactions_history_long_arraycxx . . $ events_transactions_history_long_array.= NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 55 | |
| 56 | /** |
| 57 | Initialize table EVENTS_TRANSACTIONS_HISTORY_LONG. |
| 58 | @param events_transactions_history_long_sizing table sizing |
| 59 | */ |
| 60 | int init_events_transactions_history_longcxx . . $ init_events_transactions_history_long(4c679297bf94b9eb).(uint events_transactions_history_long_sizing) |
| 61 | { |
| 62 | events_transactions_history_long_sizecxx . . $ events_transactions_history_long_size.= events_transactions_history_long_sizing; |
| 63 | events_transactions_history_long_fullcxx . . $ events_transactions_history_long_full.= false; |
| 64 | events_transactions_history_long_indexcxx . . $ events_transactions_history_long_index..m_u32cxx . . $ PFS_cacheline_uint32#m_u32..storecxx . . $ std/__atomic_base#store(3f1f49b30cba5fcc).(0); |
| 65 | |
| 66 | if (events_transactions_history_long_sizecxx . . $ events_transactions_history_long_size. == 0) |
| 67 | return 0; |
| 68 | |
| 69 | events_transactions_history_long_arraycxx . . $ events_transactions_history_long_array.= |
| 70 | PFS_MALLOC_ARRAYcxx . . $ `storage/perfschema/pfs_global.h:87:9`!(& builtin_memory_transactions_history_long, |
| 71 | events_transactions_history_long_size, |
| 72 | sizeof(PFS_events_transactions), PFS_events_transactions, |
| 73 | MYFcxx . . $ `include/my_global.h:986:9`!(MY_ZEROFILLcxx . . $ `include/my_sys.h:87:9`!)); |
| 74 | |
| 75 | return (events_transactions_history_long_arraycxx . . $ events_transactions_history_long_array. ? 0 : 1); |
| 76 | } |
| 77 | |
| 78 | /** Cleanup table EVENTS_TRANSACTIONS_HISTORY_LONG. */ |
| 79 | void cleanup_events_transactions_history_longcxx . . $ cleanup_events_transactions_history_long(49f6e7a06ebc5aa8).(void) |
| 80 | { |
| 81 | PFS_FREE_ARRAYcxx . . $ `storage/perfschema/pfs_global.h:103:9`!(& builtin_memory_transactions_history_long, |
| 82 | events_transactions_history_long_size, sizeof(PFS_events_transactions), |
| 83 | events_transactions_history_long_array); |
| 84 | events_transactions_history_long_arraycxx . . $ events_transactions_history_long_array.= NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 85 | } |
| 86 | |
| 87 | static inline void copy_events_transactionscxx . . $ copy_events_transactions(ee2e3f215b041923).(PFS_events_transactionscxx . . $ PFS_events_transactions# *dest, |
| 88 | const PFS_events_transactionscxx . . $ PFS_events_transactions# *source) |
| 89 | { |
| 90 | memcpy(dest, source, sizeof(PFS_events_transactionscxx . . $ PFS_events_transactions#)); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | Insert a transaction record in table EVENTS_TRANSACTIONS_HISTORY. |
| 95 | @param thread thread that executed the wait |
| 96 | @param transaction record to insert |
| 97 | */ |
| 98 | void insert_events_transactions_historycxx . . $ insert_events_transactions_history(7e34f254cec37eb6).(PFS_threadcxx . . $ PFS_thread# *thread, PFS_events_transactionscxx . . $ PFS_events_transactions# *transaction) |
| 99 | { |
| 100 | if (unlikelycxx . . $ `include/my_global.h:361:9`!(events_transactions_history_per_thread == 0)) |
| 101 | return; |
| 102 | |
| 103 | assertcxx . . $ `/usr/include/assert.h:50:10`!(thread->m_transactions_history != NULL); |
| 104 | |
| 105 | uint index= thread->m_transactions_history_indexcxx . . $ PFS_thread#m_transactions_history_index.; |
| 106 | |
| 107 | /* |
| 108 | A concurrent thread executing TRUNCATE TABLE EVENTS_TRANSACTIONS_CURRENT |
| 109 | could alter the data that this thread is inserting, |
| 110 | causing a potential race condition. |
| 111 | We are not testing for this and insert a possibly empty record, |
| 112 | to make this thread (the writer) faster. |
| 113 | This is ok, the readers of m_transactions_history will filter this out. |
| 114 | */ |
| 115 | copy_events_transactionscxx . . $ copy_events_transactions(ee2e3f215b041923).(&thread->m_transactions_historycxx . . $ PFS_thread#m_transactions_history.[index], transaction); |
| 116 | |
| 117 | index++; |
| 118 | if (index >= events_transactions_history_per_threadcxx . . $ events_transactions_history_per_thread.) |
| 119 | { |
| 120 | index= 0; |
| 121 | thread->m_transactions_history_fullcxx . . $ PFS_thread#m_transactions_history_full.= true; |
| 122 | } |
| 123 | thread->m_transactions_history_indexcxx . . $ PFS_thread#m_transactions_history_index.= index; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | Insert a transaction record in table EVENTS_TRANSACTIONS_HISTORY_LONG. |
| 128 | @param transaction record to insert |
| 129 | */ |
| 130 | void insert_events_transactions_history_longcxx . . $ insert_events_transactions_history_long(ef8107c03f40da8f).(PFS_events_transactionscxx . . $ PFS_events_transactions# *transaction) |
| 131 | { |
| 132 | if (unlikelycxx . . $ `include/my_global.h:361:9`!(events_transactions_history_long_size == 0)) |
| 133 | return ; |
| 134 | |
| 135 | assertcxx . . $ `/usr/include/assert.h:50:10`!(events_transactions_history_long_array != NULL); |
| 136 | |
| 137 | uint index= events_transactions_history_long_indexcxx . . $ events_transactions_history_long_index..m_u32cxx . . $ PFS_cacheline_uint32#m_u32..fetch_addcxx . . $ std/__atomic_base#fetch_add(8de7200f212ddeb7).(1); |
| 138 | |
| 139 | index= index % events_transactions_history_long_sizecxx . . $ events_transactions_history_long_size.; |
| 140 | if (index == 0) |
| 141 | events_transactions_history_long_fullcxx . . $ events_transactions_history_long_full.= true; |
| 142 | |
| 143 | /* See related comment in insert_events_transactions_history. */ |
| 144 | copy_events_transactionscxx . . $ copy_events_transactions(ee2e3f215b041923).(&events_transactions_history_long_arraycxx . . $ events_transactions_history_long_array.[index], transaction); |
| 145 | } |
| 146 | |
| 147 | static void fct_reset_events_transactions_currentcxx . . $ fct_reset_events_transactions_current(15748000876f743a).(PFS_threadcxx . . $ PFS_thread# *pfs) |
| 148 | { |
| 149 | pfs->m_transaction_currentcxx . . $ PFS_thread#m_transaction_current..m_classcxx . . $ PFS_events#m_class.= NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 150 | } |
| 151 | |
| 152 | /** Reset table EVENTS_TRANSACTIONS_CURRENT data. */ |
| 153 | void reset_events_transactions_currentcxx . . $ reset_events_transactions_current(49f6e7a06ebc5aa8).(void) |
| 154 | { |
| 155 | global_thread_containercxx . . $ global_thread_container..apply_allcxx . . $ PFS_buffer_scalable_container#apply_all(56a1e2153553a317).(fct_reset_events_transactions_currentcxx . . $ fct_reset_events_transactions_current(15748000876f743a).); |
| 156 | } |
| 157 | |
| 158 | static void fct_reset_events_transactions_historycxx . . $ fct_reset_events_transactions_history(15748000876f743a).(PFS_threadcxx . . $ PFS_thread# *pfs_thread) |
| 159 | { |
| 160 | PFS_events_transactionscxx . . $ PFS_events_transactions# *pfs= pfs_thread->m_transactions_historycxx . . $ PFS_thread#m_transactions_history.; |
| 161 | PFS_events_transactionscxx . . $ PFS_events_transactions# *pfs_last= pfs + events_transactions_history_per_threadcxx . . $ events_transactions_history_per_thread.; |
| 162 | |
| 163 | pfs_thread->m_transactions_history_indexcxx . . $ PFS_thread#m_transactions_history_index.= 0; |
| 164 | pfs_thread->m_transactions_history_fullcxx . . $ PFS_thread#m_transactions_history_full.= false; |
| 165 | for ( ; pfs < pfs_last; pfs++) |
| 166 | pfs->m_classcxx . . $ PFS_events#m_class.= NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 167 | } |
| 168 | |
| 169 | /** Reset table EVENTS_TRANSACTIONS_HISTORY data. */ |
| 170 | void reset_events_transactions_historycxx . . $ reset_events_transactions_history(49f6e7a06ebc5aa8).(void) |
| 171 | { |
| 172 | global_thread_containercxx . . $ global_thread_container..apply_allcxx . . $ PFS_buffer_scalable_container#apply_all(56a1e2153553a317).(fct_reset_events_transactions_historycxx . . $ fct_reset_events_transactions_history(15748000876f743a).); |
| 173 | } |
| 174 | |
| 175 | /** Reset table EVENTS_TRANSACTIONS_HISTORY_LONG data. */ |
| 176 | void reset_events_transactions_history_longcxx . . $ reset_events_transactions_history_long(49f6e7a06ebc5aa8).(void) |
| 177 | { |
| 178 | events_transactions_history_long_indexcxx . . $ events_transactions_history_long_index..m_u32cxx . . $ PFS_cacheline_uint32#m_u32..storecxx . . $ std/__atomic_base#store(3f1f49b30cba5fcc).(0); |
| 179 | events_transactions_history_long_fullcxx . . $ events_transactions_history_long_full.= false; |
| 180 | |
| 181 | PFS_events_transactionscxx . . $ PFS_events_transactions# *pfs= events_transactions_history_long_arraycxx . . $ events_transactions_history_long_array.; |
| 182 | PFS_events_transactionscxx . . $ PFS_events_transactions# *pfs_last= pfs + events_transactions_history_long_sizecxx . . $ events_transactions_history_long_size.; |
| 183 | for ( ; pfs < pfs_last; pfs++) |
| 184 | pfs->m_classcxx . . $ PFS_events#m_class.= NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 185 | } |
| 186 | |
| 187 | static void fct_reset_events_transactions_by_threadcxx . . $ fct_reset_events_transactions_by_thread(15748000876f743a).(PFS_threadcxx . . $ PFS_thread# *thread) |
| 188 | { |
| 189 | PFS_accountcxx . . $ PFS_account# *account= sanitize_accountcxx . . $ sanitize_account(813b1b87e4a46bfb).(thread->m_accountcxx . . $ PFS_thread#m_account.); |
| 190 | PFS_usercxx . . $ PFS_user# *user= sanitize_usercxx . . $ sanitize_user(7c6db9ba032f8556).(thread->m_usercxx . . $ PFS_thread#m_user.); |
| 191 | PFS_hostcxx . . $ PFS_host# *host= sanitize_hostcxx . . $ sanitize_host(d0f3aaba030f3ffb).(thread->m_hostcxx . . $ PFS_thread#m_host.); |
| 192 | aggregate_thread_transactionscxx . . $ aggregate_thread_transactions(27bffc1cda8ffcdf).(thread, account, user, host); |
| 193 | } |
| 194 | |
| 195 | /** Reset table EVENTS_TRANSACTIONS_SUMMARY_BY_THREAD_BY_EVENT_NAME data. */ |
| 196 | void reset_events_transactions_by_threadcxx . . $ reset_events_transactions_by_thread(49f6e7a06ebc5aa8).() |
| 197 | { |
| 198 | global_thread_containercxx . . $ global_thread_container..applycxx . . $ PFS_buffer_scalable_container#apply(56a1e2153553a317).(fct_reset_events_transactions_by_threadcxx . . $ fct_reset_events_transactions_by_thread(15748000876f743a).); |
| 199 | } |
| 200 | |
| 201 | static void fct_reset_events_transactions_by_accountcxx . . $ fct_reset_events_transactions_by_account(af807f345a081b78).(PFS_accountcxx . . $ PFS_account# *pfs) |
| 202 | { |
| 203 | PFS_usercxx . . $ PFS_user# *user= sanitize_usercxx . . $ sanitize_user(7c6db9ba032f8556).(pfs->m_usercxx . . $ PFS_account#m_user.); |
| 204 | PFS_hostcxx . . $ PFS_host# *host= sanitize_hostcxx . . $ sanitize_host(d0f3aaba030f3ffb).(pfs->m_hostcxx . . $ PFS_account#m_host.); |
| 205 | pfs->aggregate_transactionscxx . . $ PFS_account#aggregate_transactions(5fb91acbdf55a203).(user, host); |
| 206 | } |
| 207 | |
| 208 | /** Reset table EVENTS_TRANSACTIONS_SUMMARY_BY_ACCOUNT_BY_EVENT_NAME data. */ |
| 209 | void reset_events_transactions_by_accountcxx . . $ reset_events_transactions_by_account(49f6e7a06ebc5aa8).() |
| 210 | { |
| 211 | global_account_containercxx . . $ global_account_container..applycxx . . $ PFS_buffer_scalable_container#apply(56a1e2153553a317).(fct_reset_events_transactions_by_accountcxx . . $ fct_reset_events_transactions_by_account(af807f345a081b78).); |
| 212 | } |
| 213 | |
| 214 | static void fct_reset_events_transactions_by_usercxx . . $ fct_reset_events_transactions_by_user(93a5df659a21a697).(PFS_usercxx . . $ PFS_user# *pfs) |
| 215 | { |
| 216 | pfs->aggregate_transactionscxx . . $ PFS_user#aggregate_transactions(49f6e7a06ebc5aa8).(); |
| 217 | } |
| 218 | |
| 219 | /** Reset table EVENTS_TRANSACTIONS_SUMMARY_BY_USER_BY_EVENT_NAME data. */ |
| 220 | void reset_events_transactions_by_usercxx . . $ reset_events_transactions_by_user(49f6e7a06ebc5aa8).() |
| 221 | { |
| 222 | global_user_containercxx . . $ global_user_container..applycxx . . $ PFS_buffer_scalable_container#apply(56a1e2153553a317).(fct_reset_events_transactions_by_usercxx . . $ fct_reset_events_transactions_by_user(93a5df659a21a697).); |
| 223 | } |
| 224 | |
| 225 | static void fct_reset_events_transactions_by_hostcxx . . $ fct_reset_events_transactions_by_host(78a1b153780401fe).(PFS_hostcxx . . $ PFS_host# *pfs) |
| 226 | { |
| 227 | pfs->aggregate_transactionscxx . . $ PFS_host#aggregate_transactions(49f6e7a06ebc5aa8).(); |
| 228 | } |
| 229 | |
| 230 | /** Reset table EVENTS_TRANSACTIONS_SUMMARY_BY_HOST_BY_EVENT_NAME data. */ |
| 231 | void reset_events_transactions_by_hostcxx . . $ reset_events_transactions_by_host(49f6e7a06ebc5aa8).() |
| 232 | { |
| 233 | global_host_containercxx . . $ global_host_container..applycxx . . $ PFS_buffer_scalable_container#apply(56a1e2153553a317).(fct_reset_events_transactions_by_hostcxx . . $ fct_reset_events_transactions_by_host(78a1b153780401fe).); |
| 234 | } |
| 235 | |
| 236 | /** Reset table EVENTS_TRANSACTIONS_GLOBAL_BY_EVENT_NAME data. */ |
| 237 | void reset_events_transactions_globalcxx . . $ reset_events_transactions_global(49f6e7a06ebc5aa8).() |
| 238 | { |
| 239 | global_transaction_statcxx . . $ global_transaction_stat..resetcxx . . $ PFS_transaction_stat#reset(49f6e7a06ebc5aa8).(); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | Check if the XID consists of printable characters, ASCII 32 - 127. |
| 244 | @param xid XID structure |
| 245 | @param offset offset into XID.data[] |
| 246 | @param length number of bytes to process |
| 247 | @return true if all bytes are in printable range |
| 248 | */ |
| 249 | bool xid_printablecxx . . $ xid_printable(d42133c3074b0c39).(PSI_xidcxx . . $ PSI_xid# *xid, size_t offset, size_t length) |
| 250 | { |
| 251 | if (xid->is_nullcxx . . $ PSI_xid#is_null(c9857a73d68e59ba).()) |
| 252 | return false; |
| 253 | |
| 254 | assertcxx . . $ `/usr/include/assert.h:50:10`!(offset + length <= MYSQL_XIDDATASIZE); |
| 255 | |
| 256 | unsigned char *c= (unsigned char*)&xid->datacxx . . $ PSI_xid#data. + offset; |
| 257 | |
| 258 | for (size_t i= 0; i < length; i++, c++) |
| 259 | { |
| 260 | if(*c < 32 || *c > 127) |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | return true; |
| 265 | } |
| 266 |