1 /* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.
2    Copyright (c) 2017, 2019, MariaDB Corporation.
3 
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2.0,
6   as published by the Free Software Foundation.
7 
8   This program is also distributed with certain software (including
9   but not limited to OpenSSL) that is licensed under separate terms,
10   as designated in a particular file or component or in included license
11   documentation.  The authors of MySQL hereby grant you an additional
12   permission to link the program and your derivative works with the
13   separately licensed software that they have included with MySQL.
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 Foundation,
22   51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
23 
24 #ifndef MYSQL_TABLE_H
25 #define MYSQL_TABLE_H
26 
27 /**
28   @file mysql/psi/mysql_table.h
29   Instrumentation helpers for table io.
30 */
31 
32 #include "mysql/psi/psi.h"
33 
34 #ifndef PSI_TABLE_CALL
35 #define PSI_TABLE_CALL(M) PSI_DYNAMIC_CALL(M)
36 #endif
37 
38 /**
39   @defgroup Table_instrumentation Table Instrumentation
40   @ingroup Instrumentation_interface
41   @{
42 */
43 
44 #ifdef HAVE_PSI_TABLE_INTERFACE
45 #define MYSQL_UNBIND_TABLE(handler) (handler)->unbind_psi()
46 
47 #define PSI_CALL_unbind_table           PSI_TABLE_CALL(unbind_table)
48 #define PSI_CALL_rebind_table           PSI_TABLE_CALL(rebind_table)
49 #define PSI_CALL_open_table             PSI_TABLE_CALL(open_table)
50 #define PSI_CALL_close_table            PSI_TABLE_CALL(close_table)
51 #define PSI_CALL_get_table_share        PSI_TABLE_CALL(get_table_share)
52 #define PSI_CALL_release_table_share    PSI_TABLE_CALL(release_table_share)
53 #define PSI_CALL_drop_table_share       PSI_TABLE_CALL(drop_table_share)
54 #else
55 #define MYSQL_UNBIND_TABLE(handler)                     do { } while(0)
56 
57 #define PSI_CALL_unbind_table(A1)                       do { } while(0)
58 #define PSI_CALL_rebind_table(A1,A2,A3)                 NULL
59 #define PSI_CALL_close_table(A1,A2)                     do { } while(0)
60 #define PSI_CALL_open_table(A1,A2)                      NULL
61 #define PSI_CALL_get_table_share(A1,A2)                 NULL
62 #define PSI_CALL_release_table_share(A1)                do { } while(0)
63 #define PSI_CALL_drop_table_share(A1,A2,A3,A4,A5)       do { } while(0)
64 #endif
65 
66 /**
67   @def MYSQL_TABLE_WAIT_VARIABLES
68   Instrumentation helper for table waits.
69   This instrumentation declares local variables.
70   Do not use a ';' after this macro
71   @param LOCKER the locker
72   @param STATE the locker state
73   @sa MYSQL_START_TABLE_IO_WAIT.
74   @sa MYSQL_END_TABLE_IO_WAIT.
75   @sa MYSQL_START_TABLE_LOCK_WAIT.
76   @sa MYSQL_END_TABLE_LOCK_WAIT.
77 */
78 #ifdef HAVE_PSI_TABLE_INTERFACE
79   #define MYSQL_TABLE_WAIT_VARIABLES(LOCKER, STATE) \
80     struct PSI_table_locker* LOCKER; \
81     PSI_table_locker_state STATE;
82 #else
83   #define MYSQL_TABLE_WAIT_VARIABLES(LOCKER, STATE)
84 #endif
85 
86 /**
87   @def MYSQL_START_TABLE_LOCK_WAIT
88   Instrumentation helper for table lock waits.
89   This instrumentation marks the start of a wait event.
90   @param LOCKER the locker
91   @param STATE the locker state
92   @param PSI the instrumented table
93   @param OP the table operation to be performed
94   @param FLAGS per table operation flags.
95   @sa MYSQL_END_TABLE_LOCK_WAIT.
96 */
97 #ifdef HAVE_PSI_TABLE_INTERFACE
98   #define MYSQL_START_TABLE_LOCK_WAIT(LOCKER, STATE, PSI, OP, FLAGS) \
99     LOCKER= inline_mysql_start_table_lock_wait(STATE, PSI, \
100                                                OP, FLAGS, __FILE__, __LINE__)
101 #else
102   #define MYSQL_START_TABLE_LOCK_WAIT(LOCKER, STATE, PSI, OP, FLAGS) \
103     do {} while (0)
104 #endif
105 
106 /**
107   @def MYSQL_END_TABLE_LOCK_WAIT
108   Instrumentation helper for table lock waits.
109   This instrumentation marks the end of a wait event.
110   @param LOCKER the locker
111   @sa MYSQL_START_TABLE_LOCK_WAIT.
112 */
113 #ifdef HAVE_PSI_TABLE_INTERFACE
114   #define MYSQL_END_TABLE_LOCK_WAIT(LOCKER) \
115     inline_mysql_end_table_lock_wait(LOCKER)
116 #else
117   #define MYSQL_END_TABLE_LOCK_WAIT(LOCKER) \
118     do {} while (0)
119 #endif
120 
121 #ifdef HAVE_PSI_TABLE_INTERFACE
122   #define MYSQL_UNLOCK_TABLE(T) \
123     inline_mysql_unlock_table(T)
124 #else
125   #define MYSQL_UNLOCK_TABLE(T) \
126     do {} while (0)
127 #endif
128 
129 #ifdef HAVE_PSI_TABLE_INTERFACE
130 /**
131   Instrumentation calls for MYSQL_START_TABLE_LOCK_WAIT.
132   @sa MYSQL_END_TABLE_LOCK_WAIT.
133 */
134 static inline struct PSI_table_locker *
inline_mysql_start_table_lock_wait(PSI_table_locker_state * state,struct PSI_table * psi,enum PSI_table_lock_operation op,ulong flags,const char * src_file,uint src_line)135 inline_mysql_start_table_lock_wait(PSI_table_locker_state *state,
136                                    struct PSI_table *psi,
137                                    enum PSI_table_lock_operation op,
138                                    ulong flags, const char *src_file, uint src_line)
139 {
140   if (psi_likely(psi != NULL))
141   {
142     struct PSI_table_locker *locker;
143     locker= PSI_TABLE_CALL(start_table_lock_wait)
144       (state, psi, op, flags, src_file, src_line);
145     return locker;
146   }
147   return NULL;
148 }
149 
150 /**
151   Instrumentation calls for MYSQL_END_TABLE_LOCK_WAIT.
152   @sa MYSQL_START_TABLE_LOCK_WAIT.
153 */
154 static inline void
inline_mysql_end_table_lock_wait(struct PSI_table_locker * locker)155 inline_mysql_end_table_lock_wait(struct PSI_table_locker *locker)
156 {
157   if (psi_likely(locker != NULL))
158     PSI_TABLE_CALL(end_table_lock_wait)(locker);
159 }
160 
161 static inline void
inline_mysql_unlock_table(struct PSI_table * table)162 inline_mysql_unlock_table(struct PSI_table *table)
163 {
164   if (table != NULL)
165     PSI_TABLE_CALL(unlock_table)(table);
166 }
167 #endif
168 
169 /** @} (end of group Table_instrumentation) */
170 
171 #endif
172 
173