1 /* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
2    Copyright (c) 2017, 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
22   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
23 
24 #ifndef MYSQL_STATEMENT_H
25 #define MYSQL_STATEMENT_H
26 
27 /**
28   @file mysql/psi/mysql_statement.h
29   Instrumentation helpers for statements.
30 */
31 
32 #include "mysql/psi/psi.h"
33 
34 /**
35   @defgroup Statement_instrumentation Statement Instrumentation
36   @ingroup Instrumentation_interface
37   @{
38 */
39 
40 /**
41   @def mysql_statement_register(P1, P2, P3)
42   Statement registration.
43 */
44 #ifdef HAVE_PSI_STATEMENT_INTERFACE
45 #define mysql_statement_register(P1, P2, P3) \
46   inline_mysql_statement_register(P1, P2, P3)
47 #else
48 #define mysql_statement_register(P1, P2, P3) \
49   do {} while (0)
50 #endif
51 
52 #ifdef HAVE_PSI_STATEMENT_DIGEST_INTERFACE
53   #define MYSQL_DIGEST_START(LOCKER) \
54     inline_mysql_digest_start(LOCKER)
55 #else
56   #define MYSQL_DIGEST_START(LOCKER) \
57     NULL
58 #endif
59 
60 #ifdef HAVE_PSI_STATEMENT_DIGEST_INTERFACE
61   #define MYSQL_DIGEST_END(LOCKER, DIGEST) \
62     inline_mysql_digest_end(LOCKER, DIGEST)
63 #else
64   #define MYSQL_DIGEST_END(LOCKER, DIGEST) \
65     do {} while (0)
66 #endif
67 
68 #ifdef HAVE_PSI_STATEMENT_INTERFACE
69   #define MYSQL_START_STATEMENT(STATE, K, DB, DB_LEN, CS) \
70     inline_mysql_start_statement(STATE, K, DB, DB_LEN, CS, __FILE__, __LINE__)
71 #else
72   #define MYSQL_START_STATEMENT(STATE, K, DB, DB_LEN, CS) \
73     NULL
74 #endif
75 
76 #ifdef HAVE_PSI_STATEMENT_INTERFACE
77   #define MYSQL_REFINE_STATEMENT(LOCKER, K) \
78     inline_mysql_refine_statement(LOCKER, K)
79 #else
80   #define MYSQL_REFINE_STATEMENT(LOCKER, K) \
81     NULL
82 #endif
83 
84 #ifdef HAVE_PSI_STATEMENT_INTERFACE
85   #define MYSQL_SET_STATEMENT_TEXT(LOCKER, P1, P2) \
86     inline_mysql_set_statement_text(LOCKER, P1, P2)
87 #else
88   #define MYSQL_SET_STATEMENT_TEXT(LOCKER, P1, P2) \
89     do {} while (0)
90 #endif
91 
92 #ifdef HAVE_PSI_STATEMENT_INTERFACE
93   #define MYSQL_SET_STATEMENT_LOCK_TIME(LOCKER, P1) \
94     inline_mysql_set_statement_lock_time(LOCKER, P1)
95 #else
96   #define MYSQL_SET_STATEMENT_LOCK_TIME(LOCKER, P1) \
97     do {} while (0)
98 #endif
99 
100 #ifdef HAVE_PSI_STATEMENT_INTERFACE
101   #define MYSQL_SET_STATEMENT_ROWS_SENT(LOCKER, P1) \
102     inline_mysql_set_statement_rows_sent(LOCKER, P1)
103 #else
104   #define MYSQL_SET_STATEMENT_ROWS_SENT(LOCKER, P1) \
105     do {} while (0)
106 #endif
107 
108 #ifdef HAVE_PSI_STATEMENT_INTERFACE
109   #define MYSQL_SET_STATEMENT_ROWS_EXAMINED(LOCKER, P1) \
110     inline_mysql_set_statement_rows_examined(LOCKER, P1)
111 #else
112   #define MYSQL_SET_STATEMENT_ROWS_EXAMINED(LOCKER, P1) \
113     do {} while (0)
114 #endif
115 
116 #ifdef HAVE_PSI_STATEMENT_INTERFACE
117   #define MYSQL_END_STATEMENT(LOCKER, DA) \
118     inline_mysql_end_statement(LOCKER, DA)
119 #else
120   #define MYSQL_END_STATEMENT(LOCKER, DA) \
121     do {} while (0)
122 #endif
123 
124 #ifdef HAVE_PSI_STATEMENT_INTERFACE
inline_mysql_statement_register(const char * category,PSI_statement_info * info,int count)125 static inline void inline_mysql_statement_register(
126   const char *category, PSI_statement_info *info, int count)
127 {
128   PSI_STATEMENT_CALL(register_statement)(category, info, count);
129 }
130 
131 #ifdef HAVE_PSI_STATEMENT_DIGEST_INTERFACE
132 static inline struct PSI_digest_locker *
inline_mysql_digest_start(PSI_statement_locker * locker)133 inline_mysql_digest_start(PSI_statement_locker *locker)
134 {
135   PSI_digest_locker* digest_locker= NULL;
136 
137   if (psi_likely(locker != NULL))
138     digest_locker= PSI_DIGEST_CALL(digest_start)(locker);
139   return digest_locker;
140 }
141 #endif
142 
143 #ifdef HAVE_PSI_STATEMENT_DIGEST_INTERFACE
144 static inline void
inline_mysql_digest_end(PSI_digest_locker * locker,const sql_digest_storage * digest)145 inline_mysql_digest_end(PSI_digest_locker *locker, const sql_digest_storage *digest)
146 {
147   if (psi_likely(locker != NULL))
148     PSI_DIGEST_CALL(digest_end)(locker, digest);
149 }
150 #endif
151 
152 static inline struct PSI_statement_locker *
inline_mysql_start_statement(PSI_statement_locker_state * state,PSI_statement_key key,const char * db,size_t db_len,const CHARSET_INFO * charset,const char * src_file,uint src_line)153 inline_mysql_start_statement(PSI_statement_locker_state *state,
154                              PSI_statement_key key,
155                              const char *db, size_t db_len,
156                              const CHARSET_INFO *charset,
157                              const char *src_file, uint src_line)
158 {
159   PSI_statement_locker *locker;
160   locker= PSI_STATEMENT_CALL(get_thread_statement_locker)(state, key, charset);
161   if (psi_likely(locker != NULL))
162     PSI_STATEMENT_CALL(start_statement)(locker, db, (uint)db_len, src_file, src_line);
163   return locker;
164 }
165 
166 static inline struct PSI_statement_locker *
inline_mysql_refine_statement(PSI_statement_locker * locker,PSI_statement_key key)167 inline_mysql_refine_statement(PSI_statement_locker *locker,
168                               PSI_statement_key key)
169 {
170   if (psi_likely(locker != NULL))
171   {
172     locker= PSI_STATEMENT_CALL(refine_statement)(locker, key);
173   }
174   return locker;
175 }
176 
177 static inline void
inline_mysql_set_statement_text(PSI_statement_locker * locker,const char * text,uint text_len)178 inline_mysql_set_statement_text(PSI_statement_locker *locker,
179                                 const char *text, uint text_len)
180 {
181   if (psi_likely(locker != NULL))
182   {
183     PSI_STATEMENT_CALL(set_statement_text)(locker, text, text_len);
184   }
185 }
186 
187 static inline void
inline_mysql_set_statement_lock_time(PSI_statement_locker * locker,ulonglong count)188 inline_mysql_set_statement_lock_time(PSI_statement_locker *locker,
189                                      ulonglong count)
190 {
191   if (psi_likely(locker != NULL))
192   {
193     PSI_STATEMENT_CALL(set_statement_lock_time)(locker, count);
194   }
195 }
196 
197 static inline void
inline_mysql_set_statement_rows_sent(PSI_statement_locker * locker,ulonglong count)198 inline_mysql_set_statement_rows_sent(PSI_statement_locker *locker,
199                                      ulonglong count)
200 {
201   if (psi_likely(locker != NULL))
202   {
203     PSI_STATEMENT_CALL(set_statement_rows_sent)(locker, count);
204   }
205 }
206 
207 static inline void
inline_mysql_set_statement_rows_examined(PSI_statement_locker * locker,ulonglong count)208 inline_mysql_set_statement_rows_examined(PSI_statement_locker *locker,
209                                          ulonglong count)
210 {
211   if (psi_likely(locker != NULL))
212   {
213     PSI_STATEMENT_CALL(set_statement_rows_examined)(locker, count);
214   }
215 }
216 
217 static inline void
inline_mysql_end_statement(struct PSI_statement_locker * locker,Diagnostics_area * stmt_da)218 inline_mysql_end_statement(struct PSI_statement_locker *locker,
219                            Diagnostics_area *stmt_da)
220 {
221   PSI_STAGE_CALL(end_stage)();
222   if (psi_likely(locker != NULL))
223     PSI_STATEMENT_CALL(end_statement)(locker, stmt_da);
224 }
225 #endif
226 
227 /** @} (end of group Statement_instrumentation) */
228 
229 #endif
230 
231