1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
#ifdef TOKU_MYSQL_WITH_PFS
#include "toku_portability.h"
#include "toku_pthread.h"
toku_instr_probe_pfs::toku_instr_probe_pfs(const toku_instr_key &key)
: mutex(new toku_mutex_t) {
toku_mutex_init(key, mutex.get(), nullptr);
}
toku_instr_probe_pfs::~toku_instr_probe_pfs() {
toku_mutex_destroy(mutex.get());
}
// Thread instrumentation
int toku_pthread_create(const toku_instr_key &key,
pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine)(void *),
void *arg) {
#if (MYSQL_VERSION_ID >= 50700)
return PSI_THREAD_CALL(spawn_thread)(
key.id(), reinterpret_cast<my_thread_handle *>(thread),
attr, start_routine, arg);
#else
return PSI_THREAD_CALL(spawn_thread)(
key.id(), thread, attr, start_routine, arg);
#endif
}
void toku_instr_register_current_thread(const toku_instr_key &key) {
struct PSI_thread *psi_thread =
PSI_THREAD_CALL(new_thread)(key.id(), nullptr, 0);
PSI_THREAD_CALL(set_thread)(psi_thread);
}
void toku_instr_delete_current_thread() {
PSI_THREAD_CALL(delete_current_thread)();
}
// I/O instrumentation
void toku_instr_file_open_begin(toku_io_instrumentation &io_instr,
const toku_instr_key &key,
toku_instr_file_op op,
const char *name,
const char *src_file,
int src_line) {
io_instr.locker =
PSI_FILE_CALL(get_thread_file_name_locker)(
&io_instr.state, key.id(), static_cast<PSI_file_operation>(op),
name, io_instr.locker);
if (io_instr.locker != nullptr) {
PSI_FILE_CALL(start_file_open_wait)
(io_instr.locker, src_file, src_line);
}
}
void toku_instr_file_stream_open_end(toku_io_instrumentation &io_instr,
TOKU_FILE &file) {
file.key = nullptr;
if (FT_LIKELY(io_instr.locker)) {
file.key =
PSI_FILE_CALL(end_file_open_wait)(io_instr.locker, file.file);
}
}
void toku_instr_file_open_end(toku_io_instrumentation &io_instr, int fd) {
if (FT_LIKELY(io_instr.locker))
PSI_FILE_CALL(end_file_open_wait_and_bind_to_descriptor)
(io_instr.locker, fd);
}
void toku_instr_file_name_close_begin(toku_io_instrumentation &io_instr,
const toku_instr_key &key,
toku_instr_file_op op,
const char *name,
const char *src_file,
int src_line) {
io_instr.locker =
PSI_FILE_CALL(get_thread_file_name_locker)(
&io_instr.state, key.id(), static_cast<PSI_file_operation>(op),
name,
io_instr.locker);
if (FT_LIKELY(io_instr.locker)) {
PSI_FILE_CALL(start_file_close_wait)
(io_instr.locker, src_file, src_line);
}
}
void toku_instr_file_stream_close_begin(toku_io_instrumentation &io_instr,
toku_instr_file_op op,
const TOKU_FILE &file,
const char *src_file,
int src_line) {
io_instr.locker = nullptr;
if (FT_LIKELY(file.key)) {
io_instr.locker = PSI_FILE_CALL(get_thread_file_stream_locker)(
&io_instr.state, file.key, (PSI_file_operation)op);
if (FT_LIKELY(io_instr.locker)) {
PSI_FILE_CALL(start_file_close_wait)
(io_instr.locker, src_file, src_line);
}
}
}
void toku_instr_file_fd_close_begin(toku_io_instrumentation &io_instr,
toku_instr_file_op op,
int fd,
const char *src_file,
int src_line) {
io_instr.locker = PSI_FILE_CALL(get_thread_file_descriptor_locker)(
&io_instr.state, fd, (PSI_file_operation)op);
if (FT_LIKELY(io_instr.locker)) {
PSI_FILE_CALL(start_file_close_wait)
(io_instr.locker, src_file, src_line);
}
}
void toku_instr_file_close_end(const toku_io_instrumentation &io_instr,
int result) {
if (FT_LIKELY(io_instr.locker))
PSI_FILE_CALL(end_file_close_wait)
(io_instr.locker, result);
}
void toku_instr_file_io_begin(toku_io_instrumentation &io_instr,
toku_instr_file_op op,
int fd,
ssize_t count,
const char *src_file,
int src_line) {
io_instr.locker = PSI_FILE_CALL(get_thread_file_descriptor_locker)(
&io_instr.state, fd, (PSI_file_operation)op);
if (FT_LIKELY(io_instr.locker)) {
PSI_FILE_CALL(start_file_wait)
(io_instr.locker, count, src_file, src_line);
}
}
void toku_instr_file_name_io_begin(toku_io_instrumentation &io_instr,
const toku_instr_key &key,
toku_instr_file_op op,
const char *name,
ssize_t count,
const char *src_file,
int src_line) {
io_instr.locker =
PSI_FILE_CALL(get_thread_file_name_locker)(&io_instr.state,
key.id(),
(PSI_file_operation)op,
name,
&io_instr.locker);
if (FT_LIKELY(io_instr.locker)) {
PSI_FILE_CALL(start_file_wait)
(io_instr.locker, count, src_file, src_line);
}
}
void toku_instr_file_stream_io_begin(toku_io_instrumentation &io_instr,
toku_instr_file_op op,
const TOKU_FILE &file,
ssize_t count,
const char *src_file,
int src_line) {
io_instr.locker = nullptr;
if (FT_LIKELY(file.key)) {
io_instr.locker = PSI_FILE_CALL(get_thread_file_stream_locker)(
&io_instr.state, file.key, (PSI_file_operation)op);
if (FT_LIKELY(io_instr.locker)) {
PSI_FILE_CALL(start_file_wait)
(io_instr.locker, count, src_file, src_line);
}
}
}
void toku_instr_file_io_end(toku_io_instrumentation &io_instr, ssize_t count) {
if (FT_LIKELY(io_instr.locker))
PSI_FILE_CALL(end_file_wait)
(io_instr.locker, count);
}
// Mutex instrumentation
void toku_instr_mutex_init(const toku_instr_key &key, toku_mutex_t &mutex) {
mutex.psi_mutex = PSI_MUTEX_CALL(init_mutex)(key.id(), &mutex.pmutex);
#if TOKU_PTHREAD_DEBUG
mutex.instr_key_id = key.id();
#endif
}
void toku_instr_mutex_destroy(PSI_mutex *&mutex_instr) {
if (mutex_instr != nullptr) {
PSI_MUTEX_CALL(destroy_mutex)(mutex_instr);
mutex_instr = nullptr;
}
}
void toku_instr_mutex_lock_start(toku_mutex_instrumentation &mutex_instr,
toku_mutex_t &mutex,
const char *src_file,
int src_line) {
mutex_instr.locker = nullptr;
if (mutex.psi_mutex) {
mutex_instr.locker =
PSI_MUTEX_CALL(start_mutex_wait)(&mutex_instr.state,
mutex.psi_mutex,
PSI_MUTEX_LOCK,
src_file,
src_line);
}
}
void toku_instr_mutex_trylock_start(toku_mutex_instrumentation &mutex_instr,
toku_mutex_t &mutex,
const char *src_file,
int src_line) {
mutex_instr.locker = nullptr;
if (mutex.psi_mutex) {
mutex_instr.locker =
PSI_MUTEX_CALL(start_mutex_wait)(&mutex_instr.state,
mutex.psi_mutex,
PSI_MUTEX_TRYLOCK,
src_file,
src_line);
}
}
void toku_instr_mutex_lock_end(toku_mutex_instrumentation &mutex_instr,
int pthread_mutex_lock_result) {
if (mutex_instr.locker)
PSI_MUTEX_CALL(end_mutex_wait)
(mutex_instr.locker, pthread_mutex_lock_result);
}
void toku_instr_mutex_unlock(PSI_mutex *mutex_instr) {
if (mutex_instr)
PSI_MUTEX_CALL(unlock_mutex)(mutex_instr);
}
// Condvar instrumentation
void toku_instr_cond_init(const toku_instr_key &key, toku_cond_t &cond) {
cond.psi_cond = PSI_COND_CALL(init_cond)(key.id(), &cond.pcond);
#if TOKU_PTHREAD_DEBUG
cond.instr_key_id = key.id();
#endif
}
void toku_instr_cond_destroy(PSI_cond *&cond_instr) {
if (cond_instr != nullptr) {
PSI_COND_CALL(destroy_cond)(cond_instr);
cond_instr = nullptr;
}
}
void toku_instr_cond_wait_start(toku_cond_instrumentation &cond_instr,
toku_instr_cond_op op,
toku_cond_t &cond,
toku_mutex_t &mutex,
const char *src_file,
int src_line) {
cond_instr.locker = nullptr;
if (cond.psi_cond) {
/* Instrumentation start */
cond_instr.locker =
PSI_COND_CALL(start_cond_wait)(&cond_instr.state,
cond.psi_cond,
mutex.psi_mutex,
(PSI_cond_operation)op,
src_file,
src_line);
}
}
void toku_instr_cond_wait_end(toku_cond_instrumentation &cond_instr,
int pthread_cond_wait_result) {
if (cond_instr.locker)
PSI_COND_CALL(end_cond_wait)
(cond_instr.locker, pthread_cond_wait_result);
}
void toku_instr_cond_signal(const toku_cond_t &cond) {
if (cond.psi_cond)
PSI_COND_CALL(signal_cond)(cond.psi_cond);
}
void toku_instr_cond_broadcast(const toku_cond_t &cond) {
if (cond.psi_cond)
PSI_COND_CALL(broadcast_cond)(cond.psi_cond);
}
// rwlock instrumentation
void toku_instr_rwlock_init(const toku_instr_key &key,
toku_pthread_rwlock_t &rwlock) {
rwlock.psi_rwlock = PSI_RWLOCK_CALL(init_rwlock)(key.id(), &rwlock.rwlock);
#if TOKU_PTHREAD_DEBUG
rwlock.instr_key_id = key.id();
#endif
}
void toku_instr_rwlock_destroy(PSI_rwlock *&rwlock_instr) {
if (rwlock_instr != nullptr) {
PSI_RWLOCK_CALL(destroy_rwlock)(rwlock_instr);
rwlock_instr = nullptr;
}
}
void toku_instr_rwlock_rdlock_wait_start(
toku_rwlock_instrumentation &rwlock_instr,
toku_pthread_rwlock_t &rwlock,
const char *src_file,
int src_line) {
rwlock_instr.locker = nullptr;
if (rwlock.psi_rwlock) {
/* Instrumentation start */
rwlock_instr.locker =
PSI_RWLOCK_CALL(start_rwlock_rdwait)(&rwlock_instr.state,
rwlock.psi_rwlock,
PSI_RWLOCK_READLOCK,
src_file,
src_line);
}
}
void toku_instr_rwlock_wrlock_wait_start(
toku_rwlock_instrumentation &rwlock_instr,
toku_pthread_rwlock_t &rwlock,
const char *src_file,
int src_line) {
rwlock_instr.locker = nullptr;
if (rwlock.psi_rwlock) {
/* Instrumentation start */
rwlock_instr.locker =
PSI_RWLOCK_CALL(start_rwlock_wrwait)(&rwlock_instr.state,
rwlock.psi_rwlock,
PSI_RWLOCK_WRITELOCK,
src_file,
src_line);
}
}
void toku_instr_rwlock_rdlock_wait_end(
toku_rwlock_instrumentation &rwlock_instr,
int pthread_rwlock_wait_result) {
if (rwlock_instr.locker)
PSI_RWLOCK_CALL(end_rwlock_rdwait)
(rwlock_instr.locker, pthread_rwlock_wait_result);
}
void toku_instr_rwlock_wrlock_wait_end(
toku_rwlock_instrumentation &rwlock_instr,
int pthread_rwlock_wait_result) {
if (rwlock_instr.locker)
PSI_RWLOCK_CALL(end_rwlock_wrwait)
(rwlock_instr.locker, pthread_rwlock_wait_result);
}
void toku_instr_rwlock_unlock(toku_pthread_rwlock_t &rwlock) {
if (rwlock.psi_rwlock)
PSI_RWLOCK_CALL(unlock_rwlock)(rwlock.psi_rwlock);
}
#endif // TOKU_MYSQL_WITH_PFS
|