1 /* Copyright (c) 2008, 2020, 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, 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
21   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #include "storage/perfschema/unittest/pfs_unit_test_conf.h"
24 
25 #include <memory.h>
26 #include <mysql/psi/psi_file.h>
27 #include <string.h>
28 
29 #include "m_ctype.h"
30 #include "my_io.h"
31 #include "my_thread.h"
32 #include "storage/perfschema/pfs_buffer_container.h"
33 #include "storage/perfschema/pfs_global.h"
34 #include "storage/perfschema/pfs_instr.h"
35 #include "storage/perfschema/pfs_instr_class.h"
36 #include "storage/perfschema/pfs_server.h"
37 #include "storage/perfschema/unittest/stub_pfs_defaults.h"
38 #include "storage/perfschema/unittest/stub_pfs_plugin_table.h"
39 #include "storage/perfschema/unittest/stub_print_error.h"
40 #include "unittest/mytap/tap.h"
41 
42 /* test helpers, to simulate the setup */
43 
setup_thread(PSI_thread * t,bool enabled)44 static void setup_thread(PSI_thread *t, bool enabled) {
45   PFS_thread *t2 = (PFS_thread *)t;
46   t2->m_enabled = enabled;
47 }
48 
49 /* test helpers, to inspect data */
50 
lookup_file_by_name(const char * name)51 static PFS_file *lookup_file_by_name(const char *name) {
52   PFS_file *pfs;
53   size_t len = strlen(name);
54   size_t dirlen;
55   const char *filename;
56   size_t filename_length;
57 
58   PFS_file_iterator it = global_file_container.iterate();
59   pfs = it.scan_next();
60 
61   while (pfs != nullptr) {
62     /*
63       When a file "foo" is instrumented, the name is normalized
64       to "/path/to/current/directory/foo", so we remove the
65       directory name here to find it back.
66     */
67     dirlen = dirname_length(pfs->m_filename);
68     filename = pfs->m_filename + dirlen;
69     filename_length = pfs->m_filename_length - dirlen;
70     if ((len == filename_length) &&
71         (strncmp(name, filename, filename_length) == 0))
72       return pfs;
73 
74     pfs = it.scan_next();
75   }
76 
77   return nullptr;
78 }
79 
80 /* tests */
81 
test_bootstrap()82 static void test_bootstrap() {
83   void *psi;
84   void *psi_2;
85   PSI_thread_bootstrap *thread_boot;
86   PSI_mutex_bootstrap *mutex_boot;
87   PSI_rwlock_bootstrap *rwlock_boot;
88   PSI_cond_bootstrap *cond_boot;
89   PSI_file_bootstrap *file_boot;
90   PSI_socket_bootstrap *socket_boot;
91   PSI_table_bootstrap *table_boot;
92   PSI_mdl_bootstrap *mdl_boot;
93   PSI_idle_bootstrap *idle_boot;
94   PSI_stage_bootstrap *stage_boot;
95   PSI_statement_bootstrap *statement_boot;
96   PSI_transaction_bootstrap *transaction_boot;
97   PSI_memory_bootstrap *memory_boot;
98   PSI_error_bootstrap *error_boot;
99   PSI_data_lock_bootstrap *data_lock_boot;
100   PSI_system_bootstrap *system_boot;
101   PSI_tls_channel_bootstrap *tls_channel_boot;
102   PFS_global_param param;
103 
104   diag("test_bootstrap");
105 
106   memset(&param, 0xFF, sizeof(param));
107   param.m_enabled = true;
108   param.m_mutex_class_sizing = 0;
109   param.m_rwlock_class_sizing = 0;
110   param.m_cond_class_sizing = 0;
111   param.m_thread_class_sizing = 0;
112   param.m_table_share_sizing = 0;
113   param.m_file_class_sizing = 0;
114   param.m_socket_class_sizing = 0;
115   param.m_mutex_sizing = 0;
116   param.m_rwlock_sizing = 0;
117   param.m_cond_sizing = 0;
118   param.m_thread_sizing = 0;
119   param.m_table_sizing = 0;
120   param.m_file_sizing = 0;
121   param.m_file_handle_sizing = 0;
122   param.m_socket_sizing = 0;
123   param.m_events_waits_history_sizing = 0;
124   param.m_events_waits_history_long_sizing = 0;
125   param.m_setup_actor_sizing = 0;
126   param.m_setup_object_sizing = 0;
127   param.m_user_sizing = 0;
128   param.m_account_sizing = 0;
129   param.m_host_sizing = 0;
130   param.m_stage_class_sizing = 0;
131   param.m_events_stages_history_sizing = 0;
132   param.m_events_stages_history_long_sizing = 0;
133   param.m_statement_class_sizing = 0;
134   param.m_events_statements_history_sizing = 0;
135   param.m_events_statements_history_long_sizing = 0;
136   param.m_events_transactions_history_sizing = 0;
137   param.m_events_transactions_history_long_sizing = 0;
138   param.m_digest_sizing = 0;
139   param.m_session_connect_attrs_sizing = 0;
140   param.m_program_sizing = 0;
141   param.m_statement_stack_sizing = 0;
142   param.m_memory_class_sizing = 0;
143   param.m_metadata_lock_sizing = 0;
144   param.m_max_digest_length = 0;
145   param.m_max_sql_text_length = 0;
146   param.m_error_sizing = 0;
147 
148   param.m_hints.m_table_definition_cache = 100;
149   param.m_hints.m_table_open_cache = 100;
150   param.m_hints.m_max_connections = 100;
151   param.m_hints.m_open_files_limit = 100;
152   param.m_hints.m_max_prepared_stmt_count = 100;
153 
154   pre_initialize_performance_schema();
155   initialize_performance_schema(
156       &param, &thread_boot, &mutex_boot, &rwlock_boot, &cond_boot, &file_boot,
157       &socket_boot, &table_boot, &mdl_boot, &idle_boot, &stage_boot,
158       &statement_boot, &transaction_boot, &memory_boot, &error_boot,
159       &data_lock_boot, &system_boot, &tls_channel_boot);
160   ok(thread_boot != nullptr, "thread_boot");
161   ok(mutex_boot != nullptr, "mutex_boot");
162   ok(rwlock_boot != nullptr, "rwlock_boot");
163   ok(cond_boot != nullptr, "cond_boot");
164   ok(file_boot != nullptr, "file_boot");
165   ok(socket_boot != nullptr, "socket_boot");
166   ok(table_boot != nullptr, "table_boot");
167   ok(mdl_boot != nullptr, "mdl_boot");
168   ok(idle_boot != nullptr, "idle_boot");
169   ok(stage_boot != nullptr, "stage_boot");
170   ok(statement_boot != nullptr, "statement_boot");
171   ok(transaction_boot != nullptr, "transaction_boot");
172   ok(memory_boot != nullptr, "memory_boot");
173   ok(error_boot != nullptr, "error_boot");
174   ok(data_lock_boot != nullptr, "data_lock_boot");
175   ok(tls_channel_boot != nullptr, "tls_channel_boot");
176 
177   ok(thread_boot->get_interface != nullptr, "thread_boot->get_interface");
178   ok(mutex_boot->get_interface != nullptr, "mutex_boot->get_interface");
179   ok(rwlock_boot->get_interface != nullptr, "rwlock_boot->get_interface");
180   ok(cond_boot->get_interface != nullptr, "cond_boot->get_interface");
181   ok(file_boot->get_interface != nullptr, "file_boot->get_interface");
182   ok(socket_boot->get_interface != nullptr, "socket_boot->get_interface");
183   ok(table_boot->get_interface != nullptr, "table_boot->get_interface");
184   ok(mdl_boot->get_interface != nullptr, "mdl_boot->get_interface");
185   ok(idle_boot->get_interface != nullptr, "idle_boot->get_interface");
186   ok(stage_boot->get_interface != nullptr, "stage_boot->get_interface");
187   ok(statement_boot->get_interface != nullptr, "statement_boot->get_interface");
188   ok(transaction_boot->get_interface != nullptr,
189      "transaction_boot->get_interface");
190   ok(memory_boot->get_interface != nullptr, "memory_boot->get_interface");
191   ok(error_boot->get_interface != nullptr, "error_boot->get_interface");
192   ok(data_lock_boot->get_interface != nullptr, "data_lock_boot->get_interface");
193   ok(tls_channel_boot->get_interface != nullptr,
194      "tls_channel_boot->get_interface");
195 
196   psi = thread_boot->get_interface(0);
197   ok(psi == nullptr, "no thread version 0");
198   psi = thread_boot->get_interface(PSI_THREAD_VERSION_1);
199   ok(psi == nullptr, "no thread version 1");
200   psi = thread_boot->get_interface(PSI_THREAD_VERSION_2);
201   ok(psi == nullptr, "no thread version 2");
202 
203   psi = mutex_boot->get_interface(0);
204   ok(psi == nullptr, "no mutex version 0");
205   psi = mutex_boot->get_interface(PSI_MUTEX_VERSION_1);
206   ok(psi != nullptr, "mutex version 1");
207 
208   psi = rwlock_boot->get_interface(0);
209   ok(psi == nullptr, "no rwlock version 0");
210   psi = rwlock_boot->get_interface(PSI_RWLOCK_VERSION_1);
211   ok(psi == nullptr, "no rwlock version 1");
212   psi = rwlock_boot->get_interface(PSI_RWLOCK_VERSION_2);
213   ok(psi != nullptr, "rwlock version 2");
214 
215   psi = cond_boot->get_interface(0);
216   ok(psi == nullptr, "no cond version 0");
217   psi = cond_boot->get_interface(PSI_COND_VERSION_1);
218   ok(psi != nullptr, "cond version 1");
219 
220   psi = file_boot->get_interface(0);
221   ok(psi == nullptr, "no file version 0");
222   psi = file_boot->get_interface(PSI_FILE_VERSION_2);
223   ok(psi != nullptr, "file version 2");
224 
225   psi = socket_boot->get_interface(0);
226   ok(psi == nullptr, "no socket version 0");
227   psi = socket_boot->get_interface(PSI_SOCKET_VERSION_1);
228   ok(psi != nullptr, "socket version 1");
229 
230   psi = table_boot->get_interface(0);
231   ok(psi == nullptr, "no table version 0");
232   psi = table_boot->get_interface(PSI_TABLE_VERSION_1);
233   ok(psi != nullptr, "table version 1");
234 
235   psi = mdl_boot->get_interface(0);
236   ok(psi == nullptr, "no mdl version 0");
237   psi = mdl_boot->get_interface(PSI_MDL_VERSION_1);
238   ok(psi != nullptr, "mdl version 1");
239 
240   psi = idle_boot->get_interface(0);
241   ok(psi == nullptr, "no idle version 0");
242   psi = idle_boot->get_interface(PSI_IDLE_VERSION_1);
243   ok(psi != nullptr, "idle version 1");
244 
245   psi = stage_boot->get_interface(0);
246   ok(psi == nullptr, "no stage version 0");
247   psi = stage_boot->get_interface(PSI_STAGE_VERSION_1);
248   ok(psi != nullptr, "stage version 1");
249 
250   psi = statement_boot->get_interface(0);
251   ok(psi == nullptr, "no statement version 0");
252   psi = statement_boot->get_interface(PSI_STATEMENT_VERSION_1);
253   ok(psi == nullptr, "no statement version 1");
254   psi = statement_boot->get_interface(PSI_STATEMENT_VERSION_2);
255   ok(psi != nullptr, "statement version 2");
256 
257   psi = transaction_boot->get_interface(0);
258   ok(psi == nullptr, "no transaction version 0");
259   psi = transaction_boot->get_interface(PSI_TRANSACTION_VERSION_1);
260   ok(psi != nullptr, "transaction version 1");
261 
262   psi = memory_boot->get_interface(0);
263   ok(psi == nullptr, "no memory version 0");
264   psi = memory_boot->get_interface(PSI_MEMORY_VERSION_1);
265   ok(psi != nullptr, "memory version 1");
266 
267   psi = error_boot->get_interface(0);
268   ok(psi == nullptr, "no error version 0");
269   psi = error_boot->get_interface(PSI_ERROR_VERSION_1);
270   ok(psi != nullptr, "error version 1");
271 
272   psi = data_lock_boot->get_interface(0);
273   ok(psi == nullptr, "no data_lock version 0");
274   psi = data_lock_boot->get_interface(PSI_DATA_LOCK_VERSION_1);
275   ok(psi != nullptr, "data_lock version 1");
276   psi_2 = data_lock_boot->get_interface(PSI_DATA_LOCK_VERSION_2);
277   ok(psi_2 == nullptr, "data_lock version 2");
278 
279   psi = tls_channel_boot->get_interface(0);
280   ok(psi == nullptr, "no tls channel version 0");
281   psi = tls_channel_boot->get_interface(PSI_TLS_CHANNEL_VERSION_1);
282   ok(psi != nullptr, "tls channel version 1");
283 
284   shutdown_performance_schema();
285 }
286 
287 /*
288   Not a test, helper for testing pfs.cc
289 */
load_perfschema(PSI_thread_service_t ** thread_service,PSI_mutex_service_t ** mutex_service,PSI_rwlock_service_t ** rwlock_service,PSI_cond_service_t ** cond_service,PSI_file_service_t ** file_service,PSI_socket_service_t ** socket_service,PSI_table_service_t ** table_service,PSI_mdl_service_t ** mdl_service,PSI_idle_service_t ** idle_service,PSI_stage_service_t ** stage_service,PSI_statement_service_t ** statement_service,PSI_transaction_service_t ** transaction_service,PSI_memory_service_t ** memory_service,PSI_error_service_t ** error_service,PSI_data_lock_service_t ** data_lock_service,PSI_system_service_t ** system_service,PSI_tls_channel_service_t ** tls_channel_service)290 static void load_perfschema(
291     PSI_thread_service_t **thread_service, PSI_mutex_service_t **mutex_service,
292     PSI_rwlock_service_t **rwlock_service, PSI_cond_service_t **cond_service,
293     PSI_file_service_t **file_service, PSI_socket_service_t **socket_service,
294     PSI_table_service_t **table_service, PSI_mdl_service_t **mdl_service,
295     PSI_idle_service_t **idle_service, PSI_stage_service_t **stage_service,
296     PSI_statement_service_t **statement_service,
297     PSI_transaction_service_t **transaction_service,
298     PSI_memory_service_t **memory_service, PSI_error_service_t **error_service,
299     PSI_data_lock_service_t **data_lock_service,
300     PSI_system_service_t **system_service,
301     PSI_tls_channel_service_t **tls_channel_service) {
302   PSI_thread_bootstrap *thread_boot;
303   PSI_mutex_bootstrap *mutex_boot;
304   PSI_rwlock_bootstrap *rwlock_boot;
305   PSI_cond_bootstrap *cond_boot;
306   PSI_file_bootstrap *file_boot;
307   PSI_socket_bootstrap *socket_boot;
308   PSI_table_bootstrap *table_boot;
309   PSI_mdl_bootstrap *mdl_boot;
310   PSI_idle_bootstrap *idle_boot;
311   PSI_stage_bootstrap *stage_boot;
312   PSI_statement_bootstrap *statement_boot;
313   PSI_transaction_bootstrap *transaction_boot;
314   PSI_memory_bootstrap *memory_boot;
315   PSI_error_bootstrap *error_boot;
316   PSI_data_lock_bootstrap *data_lock_boot;
317   PSI_system_bootstrap *system_boot;
318   PSI_tls_channel_bootstrap *tls_channel_boot;
319   PFS_global_param param;
320 
321   memset(&param, 0xFF, sizeof(param));
322   param.m_enabled = true;
323   param.m_mutex_class_sizing = 10;
324   param.m_rwlock_class_sizing = 10;
325   param.m_cond_class_sizing = 10;
326   param.m_thread_class_sizing = 10;
327   param.m_table_share_sizing = 10;
328   param.m_file_class_sizing = 10;
329   param.m_socket_class_sizing = 10;
330   param.m_mutex_sizing = 10;
331   param.m_rwlock_sizing = 10;
332   param.m_cond_sizing = 10;
333   param.m_thread_sizing = 10;
334   param.m_table_sizing = 10;
335   param.m_file_sizing = 10;
336   param.m_file_handle_sizing = 50;
337   param.m_socket_sizing = 10;
338   param.m_events_waits_history_sizing = 10;
339   param.m_events_waits_history_long_sizing = 10;
340   param.m_setup_actor_sizing = 0;
341   param.m_setup_object_sizing = 0;
342   param.m_user_sizing = 0;
343   param.m_account_sizing = 0;
344   param.m_host_sizing = 0;
345   param.m_stage_class_sizing = 0;
346   param.m_events_stages_history_sizing = 0;
347   param.m_events_stages_history_long_sizing = 0;
348   param.m_statement_class_sizing = 0;
349   param.m_events_statements_history_sizing = 0;
350   param.m_events_statements_history_long_sizing = 0;
351   param.m_events_transactions_history_sizing = 0;
352   param.m_events_transactions_history_long_sizing = 0;
353   param.m_digest_sizing = 0;
354   param.m_session_connect_attrs_sizing = 0;
355   param.m_program_sizing = 0;
356   param.m_statement_stack_sizing = 10;
357   param.m_memory_class_sizing = 10;
358   param.m_metadata_lock_sizing = 10;
359   param.m_max_digest_length = 0;
360   param.m_max_sql_text_length = 1000;
361   param.m_error_sizing = 0;
362 
363   param.m_hints.m_table_definition_cache = 100;
364   param.m_hints.m_table_open_cache = 100;
365   param.m_hints.m_max_connections = 100;
366   param.m_hints.m_open_files_limit = 100;
367   param.m_hints.m_max_prepared_stmt_count = 100;
368 
369   pre_initialize_performance_schema();
370   /* test_bootstrap() covered this, assuming it just works */
371   initialize_performance_schema(
372       &param, &thread_boot, &mutex_boot, &rwlock_boot, &cond_boot, &file_boot,
373       &socket_boot, &table_boot, &mdl_boot, &idle_boot, &stage_boot,
374       &statement_boot, &transaction_boot, &memory_boot, &error_boot,
375       &data_lock_boot, &system_boot, &tls_channel_boot);
376   *thread_service = (PSI_thread_service_t *)thread_boot->get_interface(
377       PSI_CURRENT_THREAD_VERSION);
378   *mutex_service =
379       (PSI_mutex_service_t *)mutex_boot->get_interface(PSI_MUTEX_VERSION_1);
380   *rwlock_service =
381       (PSI_rwlock_service_t *)rwlock_boot->get_interface(PSI_RWLOCK_VERSION_2);
382   *cond_service =
383       (PSI_cond_service_t *)cond_boot->get_interface(PSI_COND_VERSION_1);
384   *file_service =
385       (PSI_file_service_t *)file_boot->get_interface(PSI_FILE_VERSION_2);
386   *socket_service =
387       (PSI_socket_service_t *)socket_boot->get_interface(PSI_SOCKET_VERSION_1);
388   *table_service =
389       (PSI_table_service_t *)table_boot->get_interface(PSI_TABLE_VERSION_1);
390   *mdl_service =
391       (PSI_mdl_service_t *)mdl_boot->get_interface(PSI_MDL_VERSION_1);
392   *idle_service =
393       (PSI_idle_service_t *)idle_boot->get_interface(PSI_IDLE_VERSION_1);
394   *stage_service =
395       (PSI_stage_service_t *)stage_boot->get_interface(PSI_SOCKET_VERSION_1);
396   *statement_service = (PSI_statement_service_t *)statement_boot->get_interface(
397       PSI_STATEMENT_VERSION_2);
398   *system_service =
399       (PSI_system_service_t *)system_boot->get_interface(PSI_SYSTEM_VERSION_1);
400   *transaction_service =
401       (PSI_transaction_service_t *)transaction_boot->get_interface(
402           PSI_TRANSACTION_VERSION_1);
403   *memory_service =
404       (PSI_memory_service_t *)memory_boot->get_interface(PSI_MEMORY_VERSION_1);
405   *error_service =
406       (PSI_error_service_t *)error_boot->get_interface(PSI_ERROR_VERSION_1);
407   *data_lock_service = (PSI_data_lock_service_t *)data_lock_boot->get_interface(
408       PSI_DATA_LOCK_VERSION_1);
409   *tls_channel_service =
410       (PSI_tls_channel_service_t *)tls_channel_boot->get_interface(
411           PSI_TLS_CHANNEL_VERSION_1);
412 
413   /* Reset every consumer to a known state */
414   flag_global_instrumentation = true;
415   flag_thread_instrumentation = true;
416 }
417 
test_bad_registration()418 static void test_bad_registration() {
419   PSI_thread_service_t *thread_service;
420   PSI_mutex_service_t *mutex_service;
421   PSI_rwlock_service_t *rwlock_service;
422   PSI_cond_service_t *cond_service;
423   PSI_file_service_t *file_service;
424   PSI_socket_service_t *socket_service;
425   PSI_table_service_t *table_service;
426   PSI_mdl_service_t *mdl_service;
427   PSI_idle_service_t *idle_service;
428   PSI_stage_service_t *stage_service;
429   PSI_statement_service_t *statement_service;
430   PSI_transaction_service_t *transaction_service;
431   PSI_memory_service_t *memory_service;
432   PSI_error_service_t *error_service;
433   PSI_data_lock_service_t *data_lock_service;
434   PSI_system_service_t *system_service;
435   PSI_tls_channel_service_t *tls_channel_service;
436 
437   diag("test_bad_registration");
438 
439   load_perfschema(&thread_service, &mutex_service, &rwlock_service,
440                   &cond_service, &file_service, &socket_service, &table_service,
441                   &mdl_service, &idle_service, &stage_service,
442                   &statement_service, &transaction_service, &memory_service,
443                   &error_service, &data_lock_service, &system_service,
444                   &tls_channel_service);
445 
446   /*
447     Test that length('wait/synch/mutex/' (17) + category + '/' (1)) < 32
448     --> category can be up to 13 chars for a mutex.
449   */
450 
451   PSI_mutex_key dummy_mutex_key = 9999;
452   PSI_mutex_info bad_mutex_1[] = {{&dummy_mutex_key, "X", 0, 0, ""}};
453 
454   mutex_service->register_mutex("/", bad_mutex_1, 1);
455   ok(dummy_mutex_key == 0, "zero key");
456   dummy_mutex_key = 9999;
457   mutex_service->register_mutex("a/", bad_mutex_1, 1);
458   ok(dummy_mutex_key == 0, "zero key");
459   dummy_mutex_key = 9999;
460   mutex_service->register_mutex("/b", bad_mutex_1, 1);
461   ok(dummy_mutex_key == 0, "zero key");
462   dummy_mutex_key = 9999;
463   mutex_service->register_mutex("a/b", bad_mutex_1, 1);
464   ok(dummy_mutex_key == 0, "zero key");
465   dummy_mutex_key = 9999;
466   mutex_service->register_mutex("12345678901234", bad_mutex_1, 1);
467   ok(dummy_mutex_key == 0, "zero key");
468   dummy_mutex_key = 9999;
469   mutex_service->register_mutex("1234567890123", bad_mutex_1, 1);
470   ok(dummy_mutex_key == 1, "assigned key");
471 
472   /*
473     Test that length('wait/synch/mutex/' (17) + category + '/' (1) + name) <=
474     128
475     --> category + name can be up to 110 chars for a mutex.
476   */
477 
478   dummy_mutex_key = 9999;
479   PSI_mutex_info bad_mutex_2[] = {
480       {&dummy_mutex_key,
481        /* 110 chars name */
482        "12345678901234567890123456789012345678901234567890"
483        "12345678901234567890123456789012345678901234567890"
484        "1234567890",
485        0, 0, ""}};
486 
487   mutex_service->register_mutex("X", bad_mutex_2, 1);
488   ok(dummy_mutex_key == 0, "zero key");
489 
490   dummy_mutex_key = 9999;
491   PSI_mutex_info bad_mutex_3[] = {
492       {&dummy_mutex_key,
493        /* 109 chars name */
494        "12345678901234567890123456789012345678901234567890"
495        "12345678901234567890123456789012345678901234567890"
496        "123456789",
497        0, 0, ""}};
498 
499   mutex_service->register_mutex("XX", bad_mutex_3, 1);
500   ok(dummy_mutex_key == 0, "zero key");
501 
502   mutex_service->register_mutex("X", bad_mutex_3, 1);
503   ok(dummy_mutex_key == 2, "assigned key");
504 
505   /*
506     Test that length('wait/synch/rwlock/' (18) + category + '/' (1)) < 32
507     --> category can be up to 12 chars for a rwlock.
508   */
509 
510   PSI_rwlock_key dummy_rwlock_key = 9999;
511   PSI_rwlock_info bad_rwlock_1[] = {{&dummy_rwlock_key, "X", 0, 0, ""}};
512 
513   rwlock_service->register_rwlock("/", bad_rwlock_1, 1);
514   ok(dummy_rwlock_key == 0, "zero key");
515   dummy_rwlock_key = 9999;
516   rwlock_service->register_rwlock("a/", bad_rwlock_1, 1);
517   ok(dummy_rwlock_key == 0, "zero key");
518   dummy_rwlock_key = 9999;
519   rwlock_service->register_rwlock("/b", bad_rwlock_1, 1);
520   ok(dummy_rwlock_key == 0, "zero key");
521   dummy_rwlock_key = 9999;
522   rwlock_service->register_rwlock("a/b", bad_rwlock_1, 1);
523   ok(dummy_rwlock_key == 0, "zero key");
524   dummy_rwlock_key = 9999;
525   rwlock_service->register_rwlock("1234567890123", bad_rwlock_1, 1);
526   ok(dummy_rwlock_key == 0, "zero key");
527   dummy_rwlock_key = 9999;
528   rwlock_service->register_rwlock("123456789012", bad_rwlock_1, 1);
529   ok(dummy_rwlock_key == 2, "assigned key");
530 
531   /*
532     Test that length('wait/synch/rwlock/' (18) + category + '/' (1) + name) <=
533     128
534     --> category + name can be up to 109 chars for a rwlock.
535   */
536 
537   dummy_rwlock_key = 9999;
538   PSI_rwlock_info bad_rwlock_2[] = {
539       {&dummy_rwlock_key,
540        /* 109 chars name */
541        "12345678901234567890123456789012345678901234567890"
542        "12345678901234567890123456789012345678901234567890"
543        "123456789",
544        0, 0, ""}};
545 
546   rwlock_service->register_rwlock("X", bad_rwlock_2, 1);
547   ok(dummy_rwlock_key == 0, "zero key");
548 
549   dummy_rwlock_key = 9999;
550   PSI_rwlock_info bad_rwlock_2_sx[] = {
551       {&dummy_rwlock_key,
552        /* 109 chars name */
553        "12345678901234567890123456789012345678901234567890"
554        "12345678901234567890123456789012345678901234567890"
555        "123456789",
556        PSI_FLAG_RWLOCK_SX, 0, ""}};
557 
558   rwlock_service->register_rwlock("Y", bad_rwlock_2_sx, 1);
559   ok(dummy_rwlock_key == 0, "zero key SX");
560 
561   dummy_rwlock_key = 9999;
562   PSI_rwlock_info bad_rwlock_3[] = {
563       {&dummy_rwlock_key,
564        /* 108 chars name */
565        "12345678901234567890123456789012345678901234567890"
566        "12345678901234567890123456789012345678901234567890"
567        "12345678",
568        0, 0, ""}};
569 
570   rwlock_service->register_rwlock("XX", bad_rwlock_3, 1);
571   ok(dummy_rwlock_key == 0, "zero key");
572 
573   rwlock_service->register_rwlock("X", bad_rwlock_3, 1);
574   ok(dummy_rwlock_key == 3, "assigned key");
575 
576   dummy_rwlock_key = 9999;
577   PSI_rwlock_info bad_rwlock_3_sx[] = {
578       {&dummy_rwlock_key,
579        /* 108 chars name */
580        "12345678901234567890123456789012345678901234567890"
581        "12345678901234567890123456789012345678901234567890"
582        "12345678",
583        PSI_FLAG_RWLOCK_SX, 0, ""}};
584 
585   rwlock_service->register_rwlock("YY", bad_rwlock_3_sx, 1);
586   ok(dummy_rwlock_key == 0, "zero key SX");
587 
588   rwlock_service->register_rwlock("Y", bad_rwlock_3_sx, 1);
589   ok(dummy_rwlock_key == 4, "assigned key SX");
590 
591   /*
592     Test that length('wait/synch/cond/' (16) + category + '/' (1)) < 32
593     --> category can be up to 14 chars for a cond.
594   */
595 
596   PSI_cond_key dummy_cond_key = 9999;
597   PSI_cond_info bad_cond_1[] = {{&dummy_cond_key, "X", 0, 0, ""}};
598 
599   cond_service->register_cond("/", bad_cond_1, 1);
600   ok(dummy_cond_key == 0, "zero key");
601   dummy_cond_key = 9999;
602   cond_service->register_cond("a/", bad_cond_1, 1);
603   ok(dummy_cond_key == 0, "zero key");
604   dummy_cond_key = 9999;
605   cond_service->register_cond("/b", bad_cond_1, 1);
606   ok(dummy_cond_key == 0, "zero key");
607   dummy_cond_key = 9999;
608   cond_service->register_cond("a/b", bad_cond_1, 1);
609   ok(dummy_cond_key == 0, "zero key");
610   dummy_cond_key = 9999;
611   cond_service->register_cond("123456789012345", bad_cond_1, 1);
612   ok(dummy_cond_key == 0, "zero key");
613   dummy_cond_key = 9999;
614   cond_service->register_cond("12345678901234", bad_cond_1, 1);
615   ok(dummy_cond_key == 1, "assigned key");
616 
617   /*
618     Test that length('wait/synch/cond/' (16) + category + '/' (1) + name) <= 128
619     --> category + name can be up to 111 chars for a cond.
620   */
621 
622   dummy_cond_key = 9999;
623   PSI_cond_info bad_cond_2[] = {
624       {&dummy_cond_key,
625        /* 111 chars name */
626        "12345678901234567890123456789012345678901234567890"
627        "12345678901234567890123456789012345678901234567890"
628        "12345678901",
629        0, 0, ""}};
630 
631   cond_service->register_cond("X", bad_cond_2, 1);
632   ok(dummy_cond_key == 0, "zero key");
633 
634   dummy_cond_key = 9999;
635   PSI_cond_info bad_cond_3[] = {
636       {&dummy_cond_key,
637        /* 110 chars name */
638        "12345678901234567890123456789012345678901234567890"
639        "12345678901234567890123456789012345678901234567890"
640        "1234567890",
641        0, 0, ""}};
642 
643   cond_service->register_cond("XX", bad_cond_3, 1);
644   ok(dummy_cond_key == 0, "zero key");
645 
646   cond_service->register_cond("X", bad_cond_3, 1);
647   ok(dummy_cond_key == 2, "assigned key");
648 
649   /*
650     Test that length('thread/' (7) + category + '/' (1)) < 32
651     --> category can be up to 23 chars for a thread.
652   */
653 
654   PSI_thread_key dummy_thread_key = 9999;
655   PSI_thread_info bad_thread_1[] = {{&dummy_thread_key, "X", 0, 0, ""}};
656 
657   thread_service->register_thread("/", bad_thread_1, 1);
658   ok(dummy_thread_key == 0, "zero key");
659   dummy_thread_key = 9999;
660   thread_service->register_thread("a/", bad_thread_1, 1);
661   ok(dummy_thread_key == 0, "zero key");
662   dummy_thread_key = 9999;
663   thread_service->register_thread("/b", bad_thread_1, 1);
664   ok(dummy_thread_key == 0, "zero key");
665   dummy_thread_key = 9999;
666   thread_service->register_thread("a/b", bad_thread_1, 1);
667   ok(dummy_thread_key == 0, "zero key");
668   dummy_thread_key = 9999;
669   thread_service->register_thread("123456789012345678901234", bad_thread_1, 1);
670   ok(dummy_thread_key == 0, "zero key");
671   dummy_thread_key = 9999;
672   thread_service->register_thread("12345678901234567890123", bad_thread_1, 1);
673   ok(dummy_thread_key == 1, "assigned key");
674 
675   /*
676     Test that length('thread/' (7) + category + '/' (1) + name) <= 128
677     --> category + name can be up to 120 chars for a thread.
678   */
679 
680   dummy_thread_key = 9999;
681   PSI_thread_info bad_thread_2[] = {
682       {&dummy_thread_key,
683        /* 120 chars name */
684        "12345678901234567890123456789012345678901234567890"
685        "12345678901234567890123456789012345678901234567890"
686        "12345678901234567890",
687        0, 0, ""}};
688 
689   thread_service->register_thread("X", bad_thread_2, 1);
690   ok(dummy_thread_key == 0, "zero key");
691 
692   dummy_thread_key = 9999;
693   PSI_thread_info bad_thread_3[] = {
694       {&dummy_thread_key,
695        /* 119 chars name */
696        "12345678901234567890123456789012345678901234567890"
697        "12345678901234567890123456789012345678901234567890"
698        "1234567890123456789",
699        0, 0, ""}};
700 
701   thread_service->register_thread("XX", bad_thread_3, 1);
702   ok(dummy_thread_key == 0, "zero key");
703 
704   thread_service->register_thread("X", bad_thread_3, 1);
705   ok(dummy_thread_key == 2, "assigned key");
706 
707   /*
708     Test that length('wait/io/file/' (13) + category + '/' (1)) < 32
709     --> category can be up to 17 chars for a file.
710   */
711 
712   PSI_file_key dummy_file_key = 9999;
713   PSI_file_info bad_file_1[] = {{&dummy_file_key, "X", 0, 0, ""}};
714 
715   file_service->register_file("/", bad_file_1, 1);
716   ok(dummy_file_key == 0, "zero key");
717   dummy_file_key = 9999;
718   file_service->register_file("a/", bad_file_1, 1);
719   ok(dummy_file_key == 0, "zero key");
720   dummy_file_key = 9999;
721   file_service->register_file("/b", bad_file_1, 1);
722   ok(dummy_file_key == 0, "zero key");
723   dummy_file_key = 9999;
724   file_service->register_file("a/b", bad_file_1, 1);
725   ok(dummy_file_key == 0, "zero key");
726   dummy_file_key = 9999;
727   file_service->register_file("123456789012345678", bad_file_1, 1);
728   ok(dummy_file_key == 0, "zero key");
729   dummy_file_key = 9999;
730   file_service->register_file("12345678901234567", bad_file_1, 1);
731   ok(dummy_file_key == 1, "assigned key");
732 
733   /*
734     Test that length('wait/io/file/' (13) + category + '/' (1) + name) <= 128
735     --> category + name can be up to 114 chars for a file.
736   */
737 
738   dummy_file_key = 9999;
739   PSI_file_info bad_file_2[] = {
740       {&dummy_file_key,
741        /* 114 chars name */
742        "12345678901234567890123456789012345678901234567890"
743        "12345678901234567890123456789012345678901234567890"
744        "12345678901234",
745        0, 0, ""}};
746 
747   file_service->register_file("X", bad_file_2, 1);
748   ok(dummy_file_key == 0, "zero key");
749 
750   dummy_file_key = 9999;
751   PSI_file_info bad_file_3[] = {
752       {&dummy_file_key,
753        /* 113 chars name */
754        "12345678901234567890123456789012345678901234567890"
755        "12345678901234567890123456789012345678901234567890"
756        "1234567890123",
757        0, 0, ""}};
758 
759   file_service->register_file("XX", bad_file_3, 1);
760   ok(dummy_file_key == 0, "zero key");
761 
762   file_service->register_file("X", bad_file_3, 1);
763   ok(dummy_file_key == 2, "assigned key");
764 
765   /*
766      Test that length('wait/io/socket/' (15) + category + '/' (1)) < 32
767      --> category can be up to 15 chars for a socket.
768    */
769 
770   PSI_socket_key dummy_socket_key = 9999;
771   PSI_socket_info bad_socket_1[] = {{&dummy_socket_key, "X", 0, 0, ""}};
772 
773   socket_service->register_socket("/", bad_socket_1, 1);
774   ok(dummy_socket_key == 0, "zero key");
775   dummy_socket_key = 9999;
776   socket_service->register_socket("a/", bad_socket_1, 1);
777   ok(dummy_socket_key == 0, "zero key");
778   dummy_socket_key = 9999;
779   socket_service->register_socket("/b", bad_socket_1, 1);
780   ok(dummy_socket_key == 0, "zero key");
781   dummy_socket_key = 9999;
782   socket_service->register_socket("a/b", bad_socket_1, 1);
783   ok(dummy_socket_key == 0, "zero key");
784   dummy_socket_key = 9999;
785   socket_service->register_socket("1234567890123456", bad_socket_1, 1);
786   ok(dummy_socket_key == 0, "zero key");
787   dummy_socket_key = 9999;
788   socket_service->register_socket("123456789012345", bad_socket_1, 1);
789   ok(dummy_socket_key == 1, "assigned key");
790 
791   /*
792     Test that length('wait/io/socket/' (15) + category + '/' (1) + name) <= 128
793     --> category + name can be up to 112 chars for a socket.
794   */
795 
796   dummy_socket_key = 9999;
797   PSI_socket_info bad_socket_2[] = {
798       {&dummy_socket_key,
799        /* 112 chars name */
800        "12345678901234567890123456789012345678901234567890"
801        "12345678901234567890123456789012345678901234567890"
802        "123456789012",
803        0, 0, ""}};
804 
805   socket_service->register_socket("X", bad_socket_2, 1);
806   ok(dummy_socket_key == 0, "zero key");
807 
808   dummy_socket_key = 9999;
809   PSI_socket_info bad_socket_3[] = {
810       {&dummy_socket_key,
811        /* 111 chars name */
812        "12345678901234567890123456789012345678901234567890"
813        "12345678901234567890123456789012345678901234567890"
814        "12345678901",
815        0, 0, ""}};
816 
817   socket_service->register_socket("XX", bad_socket_3, 1);
818   ok(dummy_socket_key == 0, "zero key");
819 
820   socket_service->register_socket("X", bad_socket_3, 1);
821   ok(dummy_socket_key == 2, "assigned key");
822 
823   shutdown_performance_schema();
824 }
825 
test_init_disabled()826 static void test_init_disabled() {
827   PSI_thread_service_t *thread_service;
828   PSI_mutex_service_t *mutex_service;
829   PSI_rwlock_service_t *rwlock_service;
830   PSI_cond_service_t *cond_service;
831   PSI_file_service_t *file_service;
832   PSI_socket_service_t *socket_service;
833   PSI_table_service_t *table_service;
834   PSI_mdl_service_t *mdl_service;
835   PSI_idle_service_t *idle_service;
836   PSI_stage_service_t *stage_service;
837   PSI_statement_service_t *statement_service;
838   PSI_transaction_service_t *transaction_service;
839   PSI_memory_service_t *memory_service;
840   PSI_error_service_t *error_service;
841   PSI_data_lock_service_t *data_lock_service;
842   PSI_system_service_t *system_service;
843   PSI_tls_channel_service_t *tls_channel_service;
844 
845   diag("test_init_disabled");
846 
847   load_perfschema(&thread_service, &mutex_service, &rwlock_service,
848                   &cond_service, &file_service, &socket_service, &table_service,
849                   &mdl_service, &idle_service, &stage_service,
850                   &statement_service, &transaction_service, &memory_service,
851                   &error_service, &data_lock_service, &system_service,
852                   &tls_channel_service);
853 
854   PSI_mutex_key mutex_key_A;
855   PSI_mutex_info all_mutex[] = {{&mutex_key_A, "M-A", 0, 0, ""}};
856 
857   PSI_rwlock_key rwlock_key_A;
858   PSI_rwlock_info all_rwlock[] = {{&rwlock_key_A, "RW-A", 0, 0, ""}};
859 
860   PSI_cond_key cond_key_A;
861   PSI_cond_info all_cond[] = {{&cond_key_A, "C-A", 0, 0, ""}};
862 
863   PSI_file_key file_key_A;
864   PSI_file_info all_file[] = {{&file_key_A, "F-A", 0, 0, ""}};
865 
866   PSI_socket_key socket_key_A;
867   PSI_socket_info all_socket[] = {{&socket_key_A, "S-A", 0, 0, ""}};
868 
869   PSI_thread_key thread_key_1;
870   PSI_thread_info all_thread[] = {{&thread_key_1, "T-1", 0, 0, ""}};
871 
872   mutex_service->register_mutex("test", all_mutex, 1);
873   rwlock_service->register_rwlock("test", all_rwlock, 1);
874   cond_service->register_cond("test", all_cond, 1);
875   file_service->register_file("test", all_file, 1);
876   socket_service->register_socket("test", all_socket, 1);
877   thread_service->register_thread("test", all_thread, 1);
878 
879   PFS_mutex_class *mutex_class_A;
880   PFS_rwlock_class *rwlock_class_A;
881   PFS_cond_class *cond_class_A;
882   PFS_file_class *file_class_A;
883   PFS_socket_class *socket_class_A;
884   PSI_mutex *mutex_A1;
885   PSI_rwlock *rwlock_A1;
886   PSI_cond *cond_A1;
887   PFS_file *file_A1;
888   PSI_socket *socket_A1;
889   PSI_thread *thread_1;
890 
891   /* Preparation */
892 
893   thread_1 = thread_service->new_thread(thread_key_1, nullptr, 0);
894   ok(thread_1 != nullptr, "T-1");
895   thread_service->set_thread_id(thread_1, 1);
896 
897   mutex_class_A = find_mutex_class(mutex_key_A);
898   ok(mutex_class_A != nullptr, "mutex class A");
899 
900   rwlock_class_A = find_rwlock_class(rwlock_key_A);
901   ok(rwlock_class_A != nullptr, "rwlock class A");
902 
903   cond_class_A = find_cond_class(cond_key_A);
904   ok(cond_class_A != nullptr, "cond class A");
905 
906   file_class_A = find_file_class(file_key_A);
907   ok(file_class_A != nullptr, "file class A");
908 
909   socket_class_A = find_socket_class(socket_key_A);
910   ok(socket_class_A != nullptr, "socket class A");
911 
912   /*
913     Pretend thread T-1 is running, and disabled, with thread_instrumentation.
914     Disabled instruments are still created so they can be enabled later.
915   */
916 
917   /* ------------------------------------------------------------------------ */
918 
919   thread_service->set_thread(thread_1);
920   setup_thread(thread_1, false);
921 
922   /* disabled M-A + disabled T-1: instrumentation */
923 
924   mutex_class_A->m_enabled = false;
925   mutex_A1 = mutex_service->init_mutex(mutex_key_A, nullptr);
926   ok(mutex_A1 != nullptr, "mutex_A1 disabled, instrumented");
927 
928   /* enabled M-A + disabled T-1: instrumentation (for later) */
929 
930   mutex_class_A->m_enabled = true;
931   mutex_A1 = mutex_service->init_mutex(mutex_key_A, nullptr);
932   ok(mutex_A1 != nullptr, "mutex_A1 enabled, instrumented");
933 
934   /* broken key + disabled T-1: no instrumentation */
935 
936   mutex_class_A->m_enabled = true;
937   mutex_A1 = mutex_service->init_mutex(0, nullptr);
938   ok(mutex_A1 == nullptr, "mutex key 0 not instrumented");
939   mutex_A1 = mutex_service->init_mutex(99, nullptr);
940   ok(mutex_A1 == nullptr, "broken mutex key not instrumented");
941 
942   /* disabled RW-A + disabled T-1: no instrumentation */
943 
944   rwlock_class_A->m_enabled = false;
945   rwlock_A1 = rwlock_service->init_rwlock(rwlock_key_A, nullptr);
946   ok(rwlock_A1 != nullptr, "rwlock_A1 disabled, instrumented");
947 
948   /* enabled RW-A + disabled T-1: instrumentation (for later) */
949 
950   rwlock_class_A->m_enabled = true;
951   rwlock_A1 = rwlock_service->init_rwlock(rwlock_key_A, nullptr);
952   ok(rwlock_A1 != nullptr, "rwlock_A1 enabled, instrumented");
953 
954   /* broken key + disabled T-1: no instrumentation */
955 
956   rwlock_class_A->m_enabled = true;
957   rwlock_A1 = rwlock_service->init_rwlock(0, nullptr);
958   ok(rwlock_A1 == nullptr, "rwlock key 0 not instrumented");
959   rwlock_A1 = rwlock_service->init_rwlock(99, nullptr);
960   ok(rwlock_A1 == nullptr, "broken rwlock key not instrumented");
961 
962   /* disabled C-A + disabled T-1: no instrumentation */
963 
964   cond_class_A->m_enabled = false;
965   cond_A1 = cond_service->init_cond(cond_key_A, nullptr);
966   ok(cond_A1 != nullptr, "cond_A1 disabled, instrumented");
967 
968   /* enabled C-A + disabled T-1: instrumentation (for later) */
969 
970   cond_class_A->m_enabled = true;
971   cond_A1 = cond_service->init_cond(cond_key_A, nullptr);
972   ok(cond_A1 != nullptr, "cond_A1 enabled, instrumented");
973 
974   /* broken key + disabled T-1: no instrumentation */
975 
976   cond_class_A->m_enabled = true;
977   cond_A1 = cond_service->init_cond(0, nullptr);
978   ok(cond_A1 == nullptr, "cond key 0 not instrumented");
979   cond_A1 = cond_service->init_cond(99, nullptr);
980   ok(cond_A1 == nullptr, "broken cond key not instrumented");
981 
982   /* disabled F-A + disabled T-1: no instrumentation */
983 
984   file_class_A->m_enabled = false;
985   file_service->create_file(file_key_A, "foo", (File)12);
986   file_A1 = lookup_file_by_name("foo");
987   ok(file_A1 == nullptr, "file_A1 disabled, not instrumented");
988 
989   /* enabled F-A + disabled T-1: no instrumentation */
990 
991   file_class_A->m_enabled = true;
992   file_service->create_file(file_key_A, "foo", (File)12);
993   file_A1 = lookup_file_by_name("foo");
994   ok(file_A1 == nullptr, "file_A1 enabled, not instrumented");
995 
996   /* broken key + disabled T-1: no instrumentation */
997 
998   file_class_A->m_enabled = true;
999   file_service->create_file(0, "foo", (File)12);
1000   file_A1 = lookup_file_by_name("foo");
1001   ok(file_A1 == nullptr, "file_A1 not instrumented");
1002   file_service->create_file(99, "foo", (File)12);
1003   file_A1 = lookup_file_by_name("foo");
1004   ok(file_A1 == nullptr, "file_A1 not instrumented");
1005 
1006   /* disabled S-A + disabled T-1: no instrumentation */
1007 
1008   socket_class_A->m_enabled = false;
1009   socket_A1 = socket_service->init_socket(socket_key_A, nullptr, nullptr, 0);
1010   ok(socket_A1 != nullptr, "socket_A1 disabled, instrumented");
1011 
1012   /* enabled S-A + disabled T-1: instrumentation (for later) */
1013 
1014   socket_class_A->m_enabled = true;
1015   socket_A1 = socket_service->init_socket(socket_key_A, nullptr, nullptr, 0);
1016   ok(socket_A1 != nullptr, "socket_A1 enabled, instrumented");
1017 
1018   /* broken key + disabled T-1: no instrumentation */
1019 
1020   socket_class_A->m_enabled = true;
1021   socket_A1 = socket_service->init_socket(0, nullptr, nullptr, 0);
1022   ok(socket_A1 == nullptr, "socket key 0 not instrumented");
1023   socket_A1 = socket_service->init_socket(99, nullptr, nullptr, 0);
1024   ok(socket_A1 == nullptr, "broken socket key not instrumented");
1025 
1026   /* Pretend thread T-1 is enabled */
1027   /* ----------------------------- */
1028 
1029   setup_thread(thread_1, true);
1030 
1031   /* disabled M-A + enabled T-1: no instrumentation */
1032 
1033   mutex_class_A->m_enabled = false;
1034   mutex_A1 = mutex_service->init_mutex(mutex_key_A, nullptr);
1035   ok(mutex_A1 != nullptr, "mutex_A1 disabled, instrumented");
1036 
1037   /* enabled M-A + enabled T-1: instrumentation */
1038 
1039   mutex_class_A->m_enabled = true;
1040   mutex_A1 = mutex_service->init_mutex(mutex_key_A, nullptr);
1041   ok(mutex_A1 != nullptr, "mutex_A1 enabled, instrumented");
1042   mutex_service->destroy_mutex(mutex_A1);
1043 
1044   /* broken key + enabled T-1: no instrumentation */
1045 
1046   mutex_class_A->m_enabled = true;
1047   mutex_A1 = mutex_service->init_mutex(0, nullptr);
1048   ok(mutex_A1 == nullptr, "mutex_A1 not instrumented");
1049   mutex_A1 = mutex_service->init_mutex(99, nullptr);
1050   ok(mutex_A1 == nullptr, "mutex_A1 not instrumented");
1051 
1052   /* disabled RW-A + enabled T-1: no instrumentation */
1053 
1054   rwlock_class_A->m_enabled = false;
1055   rwlock_A1 = rwlock_service->init_rwlock(rwlock_key_A, nullptr);
1056   ok(rwlock_A1 != nullptr, "rwlock_A1 disabled, instrumented");
1057 
1058   /* enabled RW-A + enabled T-1: instrumentation */
1059 
1060   rwlock_class_A->m_enabled = true;
1061   rwlock_A1 = rwlock_service->init_rwlock(rwlock_key_A, nullptr);
1062   ok(rwlock_A1 != nullptr, "rwlock_A1 enabled, instrumented");
1063   rwlock_service->destroy_rwlock(rwlock_A1);
1064 
1065   /* broken key + enabled T-1: no instrumentation */
1066 
1067   rwlock_class_A->m_enabled = true;
1068   rwlock_A1 = rwlock_service->init_rwlock(0, nullptr);
1069   ok(rwlock_A1 == nullptr, "rwlock_A1 not instrumented");
1070   rwlock_A1 = rwlock_service->init_rwlock(99, nullptr);
1071   ok(rwlock_A1 == nullptr, "rwlock_A1 not instrumented");
1072 
1073   /* disabled C-A + enabled T-1: no instrumentation */
1074 
1075   cond_class_A->m_enabled = false;
1076   cond_A1 = cond_service->init_cond(cond_key_A, nullptr);
1077   ok(cond_A1 != nullptr, "cond_A1 disabled, instrumented");
1078 
1079   /* enabled C-A + enabled T-1: instrumentation */
1080 
1081   cond_class_A->m_enabled = true;
1082   cond_A1 = cond_service->init_cond(cond_key_A, nullptr);
1083   ok(cond_A1 != nullptr, "cond_A1 enabled, instrumented");
1084   cond_service->destroy_cond(cond_A1);
1085 
1086   /* broken key + enabled T-1: no instrumentation */
1087 
1088   cond_class_A->m_enabled = true;
1089   cond_A1 = cond_service->init_cond(0, nullptr);
1090   ok(cond_A1 == nullptr, "cond_A1 not instrumented");
1091   cond_A1 = cond_service->init_cond(99, nullptr);
1092   ok(cond_A1 == nullptr, "cond_A1 not instrumented");
1093 
1094   /* disabled F-A + enabled T-1: no instrumentation */
1095 
1096   file_class_A->m_enabled = false;
1097   file_service->create_file(file_key_A, "foo", (File)12);
1098   file_A1 = lookup_file_by_name("foo");
1099   ok(file_A1 == nullptr, "file_A1 not instrumented");
1100 
1101   /* enabled F-A + open failed + enabled T-1: no instrumentation */
1102 
1103   file_class_A->m_enabled = true;
1104   file_service->create_file(file_key_A, "foo", (File)-1);
1105   file_A1 = lookup_file_by_name("foo");
1106   ok(file_A1 == nullptr, "file_A1 not instrumented");
1107 
1108   /* enabled F-A + out-of-descriptors + enabled T-1: no instrumentation */
1109 
1110   file_class_A->m_enabled = true;
1111   file_service->create_file(file_key_A, "foo", (File)65000);
1112   file_A1 = lookup_file_by_name("foo");
1113   ok(file_A1 == nullptr, "file_A1 not instrumented");
1114   ok(file_handle_lost == 1, "lost a file handle");
1115   file_handle_lost = 0;
1116 
1117   /* enabled F-A + enabled T-1: instrumentation */
1118 
1119   file_class_A->m_enabled = true;
1120   file_service->create_file(file_key_A, "foo-instrumented", (File)12);
1121   file_A1 = lookup_file_by_name("foo-instrumented");
1122   ok(file_A1 != nullptr, "file_A1 instrumented");
1123 
1124   /* broken key + enabled T-1: no instrumentation */
1125 
1126   file_class_A->m_enabled = true;
1127   file_service->create_file(0, "foo", (File)12);
1128   file_A1 = lookup_file_by_name("foo");
1129   ok(file_A1 == nullptr, "file key 0 not instrumented");
1130   file_service->create_file(99, "foo", (File)12);
1131   file_A1 = lookup_file_by_name("foo");
1132   ok(file_A1 == nullptr, "broken file key not instrumented");
1133 
1134   /* disabled S-A + enabled T-1: no instrumentation */
1135 
1136   socket_class_A->m_enabled = false;
1137   ok(socket_A1 == nullptr, "socket_A1 not instrumented");
1138 
1139   /* enabled S-A + enabled T-1: instrumentation */
1140 
1141   socket_class_A->m_enabled = true;
1142   socket_A1 = socket_service->init_socket(socket_key_A, nullptr, nullptr, 0);
1143   ok(socket_A1 != nullptr, "socket_A1 instrumented");
1144   socket_service->destroy_socket(socket_A1);
1145 
1146   /* broken key + enabled T-1: no instrumentation */
1147 
1148   socket_class_A->m_enabled = true;
1149   socket_A1 = socket_service->init_socket(0, nullptr, nullptr, 0);
1150   ok(socket_A1 == nullptr, "socket_A1 not instrumented");
1151   socket_A1 = socket_service->init_socket(99, nullptr, nullptr, 0);
1152   ok(socket_A1 == nullptr, "socket_A1 not instrumented");
1153 
1154   /* Pretend the running thread is not instrumented */
1155   /* ---------------------------------------------- */
1156 
1157   thread_service->delete_current_thread();
1158 
1159   /* disabled M-A + unknown thread: no instrumentation */
1160 
1161   mutex_class_A->m_enabled = false;
1162   mutex_A1 = mutex_service->init_mutex(mutex_key_A, nullptr);
1163   ok(mutex_A1 != nullptr, "mutex_A1 disabled, instrumented");
1164 
1165   /* enabled M-A + unknown thread: instrumentation (for later) */
1166 
1167   mutex_class_A->m_enabled = true;
1168   mutex_A1 = mutex_service->init_mutex(mutex_key_A, nullptr);
1169   ok(mutex_A1 != nullptr, "mutex_A1 enabled, instrumented");
1170 
1171   /* broken key + unknown thread: no instrumentation */
1172 
1173   mutex_class_A->m_enabled = true;
1174   mutex_A1 = mutex_service->init_mutex(0, nullptr);
1175   ok(mutex_A1 == nullptr, "mutex key 0 not instrumented");
1176   mutex_A1 = mutex_service->init_mutex(99, nullptr);
1177   ok(mutex_A1 == nullptr, "broken mutex key not instrumented");
1178 
1179   /* disabled RW-A + unknown thread: no instrumentation */
1180 
1181   rwlock_class_A->m_enabled = false;
1182   rwlock_A1 = rwlock_service->init_rwlock(rwlock_key_A, nullptr);
1183   ok(rwlock_A1 != nullptr, "rwlock_A1 disabled, instrumented");
1184 
1185   /* enabled RW-A + unknown thread: instrumentation (for later) */
1186 
1187   rwlock_class_A->m_enabled = true;
1188   rwlock_A1 = rwlock_service->init_rwlock(rwlock_key_A, nullptr);
1189   ok(rwlock_A1 != nullptr, "rwlock_A1 enabled, instrumented");
1190 
1191   /* broken key + unknown thread: no instrumentation */
1192 
1193   rwlock_class_A->m_enabled = true;
1194   rwlock_A1 = rwlock_service->init_rwlock(0, nullptr);
1195   ok(rwlock_A1 == nullptr, "rwlock key 0 not instrumented");
1196   rwlock_A1 = rwlock_service->init_rwlock(99, nullptr);
1197   ok(rwlock_A1 == nullptr, "broken rwlock key not instrumented");
1198 
1199   /* disabled C-A + unknown thread: no instrumentation */
1200 
1201   cond_class_A->m_enabled = false;
1202   cond_A1 = cond_service->init_cond(cond_key_A, nullptr);
1203   ok(cond_A1 != nullptr, "cond_A1 disabled, instrumented");
1204 
1205   /* enabled C-A + unknown thread: instrumentation (for later) */
1206 
1207   cond_class_A->m_enabled = true;
1208   cond_A1 = cond_service->init_cond(cond_key_A, nullptr);
1209   ok(cond_A1 != nullptr, "cond_A1 enabled, instrumented");
1210 
1211   /* broken key + unknown thread: no instrumentation */
1212 
1213   cond_class_A->m_enabled = true;
1214   cond_A1 = cond_service->init_cond(0, nullptr);
1215   ok(cond_A1 == nullptr, "cond key 0 not instrumented");
1216   cond_A1 = cond_service->init_cond(99, nullptr);
1217   ok(cond_A1 == nullptr, "broken cond key not instrumented");
1218 
1219   /* disabled F-A + unknown thread: no instrumentation */
1220 
1221   file_class_A->m_enabled = false;
1222   file_service->create_file(file_key_A, "foo", (File)12);
1223   file_A1 = lookup_file_by_name("foo");
1224   ok(file_A1 == nullptr, "file_A1 not instrumented");
1225 
1226   /* enabled F-A + unknown thread: no instrumentation */
1227 
1228   file_class_A->m_enabled = true;
1229   file_service->create_file(file_key_A, "foo", (File)12);
1230   file_A1 = lookup_file_by_name("foo");
1231   ok(file_A1 == nullptr, "file_A1 not instrumented");
1232 
1233   /* broken key + unknown thread: no instrumentation */
1234 
1235   file_class_A->m_enabled = true;
1236   file_service->create_file(0, "foo", (File)12);
1237   file_A1 = lookup_file_by_name("foo");
1238   ok(file_A1 == nullptr, "not instrumented");
1239   file_service->create_file(99, "foo", (File)12);
1240   file_A1 = lookup_file_by_name("foo");
1241   ok(file_A1 == nullptr, "not instrumented");
1242 
1243   /* disabled S-A + unknown thread: no instrumentation */
1244 
1245   socket_class_A->m_enabled = false;
1246   socket_A1 = socket_service->init_socket(socket_key_A, nullptr, nullptr, 0);
1247   ok(socket_A1 != nullptr, "socket_A1 disabled, instrumented");
1248 
1249   /* enabled S-A + unknown thread: instrumentation (for later) */
1250 
1251   socket_class_A->m_enabled = true;
1252   socket_A1 = socket_service->init_socket(socket_key_A, nullptr, nullptr, 0);
1253   ok(socket_A1 != nullptr, "socket_A1 enabled, instrumented");
1254 
1255   /* broken key + unknown thread: no instrumentation */
1256 
1257   socket_class_A->m_enabled = true;
1258   socket_A1 = socket_service->init_socket(0, nullptr, nullptr, 0);
1259   ok(socket_A1 == nullptr, "socket key 0 not instrumented");
1260   socket_A1 = socket_service->init_socket(99, nullptr, nullptr, 0);
1261   ok(socket_A1 == nullptr, "broken socket key not instrumented");
1262 
1263   shutdown_performance_schema();
1264 }
1265 
test_locker_disabled()1266 static void test_locker_disabled() {
1267   PSI_thread_service_t *thread_service;
1268   PSI_mutex_service_t *mutex_service;
1269   PSI_rwlock_service_t *rwlock_service;
1270   PSI_cond_service_t *cond_service;
1271   PSI_file_service_t *file_service;
1272   PSI_socket_service_t *socket_service;
1273   PSI_table_service_t *table_service;
1274   PSI_mdl_service_t *mdl_service;
1275   PSI_idle_service_t *idle_service;
1276   PSI_stage_service_t *stage_service;
1277   PSI_statement_service_t *statement_service;
1278   PSI_transaction_service_t *transaction_service;
1279   PSI_memory_service_t *memory_service;
1280   PSI_error_service_t *error_service;
1281   PSI_data_lock_service_t *data_lock_service;
1282   PSI_system_service_t *system_service;
1283   PSI_tls_channel_service_t *tls_channel_service;
1284 
1285   diag("test_locker_disabled");
1286 
1287   load_perfschema(&thread_service, &mutex_service, &rwlock_service,
1288                   &cond_service, &file_service, &socket_service, &table_service,
1289                   &mdl_service, &idle_service, &stage_service,
1290                   &statement_service, &transaction_service, &memory_service,
1291                   &error_service, &data_lock_service, &system_service,
1292                   &tls_channel_service);
1293 
1294   PSI_mutex_key mutex_key_A;
1295   PSI_mutex_info all_mutex[] = {{&mutex_key_A, "M-A", 0, 0, ""}};
1296 
1297   PSI_rwlock_key rwlock_key_A;
1298   PSI_rwlock_info all_rwlock[] = {{&rwlock_key_A, "RW-A", 0, 0, ""}};
1299 
1300   PSI_cond_key cond_key_A;
1301   PSI_cond_info all_cond[] = {{&cond_key_A, "C-A", 0, 0, ""}};
1302 
1303   PSI_file_key file_key_A;
1304   PSI_file_info all_file[] = {{&file_key_A, "F-A", 0, 0, ""}};
1305 
1306   PSI_socket_key socket_key_A;
1307   PSI_socket_info all_socket[] = {{&socket_key_A, "S-A", 0, 0, ""}};
1308 
1309   PSI_thread_key thread_key_1;
1310   PSI_thread_info all_thread[] = {{&thread_key_1, "T-1", 0, 0, ""}};
1311 
1312   mutex_service->register_mutex("test", all_mutex, 1);
1313   rwlock_service->register_rwlock("test", all_rwlock, 1);
1314   cond_service->register_cond("test", all_cond, 1);
1315   file_service->register_file("test", all_file, 1);
1316   socket_service->register_socket("test", all_socket, 1);
1317   thread_service->register_thread("test", all_thread, 1);
1318 
1319   PFS_mutex_class *mutex_class_A;
1320   PFS_rwlock_class *rwlock_class_A;
1321   PFS_cond_class *cond_class_A;
1322   PFS_file_class *file_class_A;
1323   PFS_socket_class *socket_class_A;
1324   PSI_mutex *mutex_A1;
1325   PSI_rwlock *rwlock_A1;
1326   PSI_cond *cond_A1;
1327   PSI_file *file_A1;
1328   PSI_socket *socket_A1;
1329   PSI_thread *thread_1;
1330 
1331   /* Preparation */
1332 
1333   thread_1 = thread_service->new_thread(thread_key_1, nullptr, 0);
1334   ok(thread_1 != nullptr, "T-1");
1335   thread_service->set_thread_id(thread_1, 1);
1336 
1337   mutex_class_A = find_mutex_class(mutex_key_A);
1338   ok(mutex_class_A != nullptr, "mutex info A");
1339 
1340   rwlock_class_A = find_rwlock_class(rwlock_key_A);
1341   ok(rwlock_class_A != nullptr, "rwlock info A");
1342 
1343   cond_class_A = find_cond_class(cond_key_A);
1344   ok(cond_class_A != nullptr, "cond info A");
1345 
1346   file_class_A = find_file_class(file_key_A);
1347   ok(file_class_A != nullptr, "file info A");
1348 
1349   socket_class_A = find_socket_class(socket_key_A);
1350   ok(socket_class_A != nullptr, "socket info A");
1351 
1352   /* Pretend thread T-1 is running, and enabled */
1353   /* ------------------------------------------ */
1354 
1355   thread_service->set_thread(thread_1);
1356   setup_thread(thread_1, true);
1357 
1358   /* Enable all instruments, instantiate objects */
1359 
1360   mutex_class_A->m_enabled = true;
1361   mutex_A1 = mutex_service->init_mutex(mutex_key_A, nullptr);
1362   ok(mutex_A1 != nullptr, "instrumented");
1363 
1364   rwlock_class_A->m_enabled = true;
1365   rwlock_A1 = rwlock_service->init_rwlock(rwlock_key_A, nullptr);
1366   ok(rwlock_A1 != nullptr, "instrumented");
1367 
1368   cond_class_A->m_enabled = true;
1369   cond_A1 = cond_service->init_cond(cond_key_A, nullptr);
1370   ok(cond_A1 != nullptr, "instrumented");
1371 
1372   file_class_A->m_enabled = true;
1373   file_service->create_file(file_key_A, "foo", (File)12);
1374   file_A1 = (PSI_file *)lookup_file_by_name("foo");
1375   ok(file_A1 != nullptr, "instrumented");
1376 
1377   socket_class_A->m_enabled = true;
1378   socket_A1 = socket_service->init_socket(socket_key_A, nullptr, nullptr, 0);
1379   ok(socket_A1 != nullptr, "instrumented");
1380 
1381   /* Socket lockers require a thread owner */
1382   socket_service->set_socket_thread_owner(socket_A1);
1383 
1384   PSI_mutex_locker *mutex_locker;
1385   PSI_mutex_locker_state mutex_state;
1386   PSI_rwlock_locker *rwlock_locker;
1387   PSI_rwlock_locker_state rwlock_state;
1388   PSI_cond_locker *cond_locker;
1389   PSI_cond_locker_state cond_state;
1390   PSI_file_locker *file_locker;
1391   PSI_file_locker_state file_state;
1392   PSI_socket_locker *socket_locker;
1393   PSI_socket_locker_state socket_state;
1394 
1395   /* Pretend thread T-1 is disabled */
1396   /* ------------------------------ */
1397 
1398   setup_thread(thread_1, false);
1399   flag_events_waits_current = true;
1400   mutex_class_A->m_enabled = true;
1401   rwlock_class_A->m_enabled = true;
1402   cond_class_A->m_enabled = true;
1403   file_class_A->m_enabled = true;
1404   socket_class_A->m_enabled = true;
1405 
1406   mutex_locker = mutex_service->start_mutex_wait(&mutex_state, mutex_A1,
1407                                                  PSI_MUTEX_LOCK, "foo.cc", 12);
1408   ok(mutex_locker == nullptr, "no locker (T-1 disabled)");
1409   rwlock_locker = rwlock_service->start_rwlock_rdwait(
1410       &rwlock_state, rwlock_A1, PSI_RWLOCK_READLOCK, "foo.cc", 12);
1411   ok(rwlock_locker == nullptr, "no locker (T-1 disabled)");
1412   cond_locker = cond_service->start_cond_wait(&cond_state, cond_A1, mutex_A1,
1413                                               PSI_COND_WAIT, "foo.cc", 12);
1414   ok(cond_locker == nullptr, "no locker (T-1 disabled)");
1415   file_locker = file_service->get_thread_file_name_locker(
1416       &file_state, file_key_A, PSI_FILE_OPEN, "xxx", nullptr);
1417   ok(file_locker == nullptr, "no locker (T-1 disabled)");
1418   file_locker = file_service->get_thread_file_stream_locker(
1419       &file_state, file_A1, PSI_FILE_READ);
1420   ok(file_locker == nullptr, "no locker (T-1 disabled)");
1421   file_locker = file_service->get_thread_file_descriptor_locker(
1422       &file_state, (File)12, PSI_FILE_READ);
1423   ok(file_locker == nullptr, "no locker (T-1 disabled)");
1424   socket_locker = socket_service->start_socket_wait(
1425       &socket_state, socket_A1, PSI_SOCKET_SEND, 12, "foo.cc", 12);
1426   ok(socket_locker == nullptr, "no locker (T-1 disabled)");
1427 
1428   /* Pretend the global consumer is disabled */
1429   /* --------------------------------------- */
1430 
1431   setup_thread(thread_1, true);
1432   flag_global_instrumentation = false;
1433   mutex_class_A->m_enabled = true;
1434   rwlock_class_A->m_enabled = true;
1435   cond_class_A->m_enabled = true;
1436   file_class_A->m_enabled = true;
1437   socket_class_A->m_enabled = true;
1438   update_instruments_derived_flags();
1439 
1440   mutex_locker = mutex_service->start_mutex_wait(&mutex_state, mutex_A1,
1441                                                  PSI_MUTEX_LOCK, "foo.cc", 12);
1442   ok(mutex_locker == nullptr, "no locker (global disabled)");
1443   rwlock_locker = rwlock_service->start_rwlock_rdwait(
1444       &rwlock_state, rwlock_A1, PSI_RWLOCK_READLOCK, "foo.cc", 12);
1445   ok(rwlock_locker == nullptr, "no locker (global disabled)");
1446   cond_locker = cond_service->start_cond_wait(&cond_state, cond_A1, mutex_A1,
1447                                               PSI_COND_WAIT, "foo.cc", 12);
1448   ok(cond_locker == nullptr, "no locker (global disabled)");
1449   file_locker = file_service->get_thread_file_name_locker(
1450       &file_state, file_key_A, PSI_FILE_OPEN, "xxx", nullptr);
1451   ok(file_locker == nullptr, "no locker (global disabled)");
1452   file_locker = file_service->get_thread_file_stream_locker(
1453       &file_state, file_A1, PSI_FILE_READ);
1454   ok(file_locker == nullptr, "no locker (global disabled)");
1455   file_locker = file_service->get_thread_file_descriptor_locker(
1456       &file_state, (File)12, PSI_FILE_READ);
1457   ok(file_locker == nullptr, "no locker (global disabled)");
1458   socket_locker = socket_service->start_socket_wait(
1459       &socket_state, socket_A1, PSI_SOCKET_SEND, 12, "foo.cc", 12);
1460   ok(socket_locker == nullptr, "no locker (global disabled)");
1461 
1462   /* Pretend the mode is global, counted only */
1463   /* ---------------------------------------- */
1464 
1465   setup_thread(thread_1, true);
1466   flag_global_instrumentation = true;
1467   flag_thread_instrumentation = false;
1468   mutex_class_A->m_enabled = true;
1469   mutex_class_A->m_timed = false;
1470   rwlock_class_A->m_enabled = true;
1471   rwlock_class_A->m_timed = false;
1472   cond_class_A->m_enabled = true;
1473   cond_class_A->m_timed = false;
1474   file_class_A->m_enabled = true;
1475   file_class_A->m_timed = false;
1476   socket_class_A->m_enabled = true;
1477   socket_class_A->m_timed = false;
1478   update_instruments_derived_flags();
1479 
1480   mutex_locker = mutex_service->start_mutex_wait(&mutex_state, mutex_A1,
1481                                                  PSI_MUTEX_LOCK, "foo.cc", 12);
1482   ok(mutex_locker == nullptr, "no locker (global counted)");
1483   rwlock_locker = rwlock_service->start_rwlock_rdwait(
1484       &rwlock_state, rwlock_A1, PSI_RWLOCK_READLOCK, "foo.cc", 12);
1485   ok(rwlock_locker == nullptr, "no locker (global counted)");
1486   cond_locker = cond_service->start_cond_wait(&cond_state, cond_A1, mutex_A1,
1487                                               PSI_COND_WAIT, "foo.cc", 12);
1488   ok(cond_locker == nullptr, "no locker (global counted)");
1489   file_locker = file_service->get_thread_file_name_locker(
1490       &file_state, file_key_A, PSI_FILE_OPEN, "xxx", nullptr);
1491   ok(file_locker != nullptr, "locker (global counted)");
1492   file_service->start_file_wait(file_locker, 10, __FILE__, __LINE__);
1493   file_service->end_file_wait(file_locker, 10);
1494   file_locker = file_service->get_thread_file_stream_locker(
1495       &file_state, file_A1, PSI_FILE_READ);
1496   ok(file_locker != nullptr, "locker (global counted)");
1497   file_service->start_file_wait(file_locker, 10, __FILE__, __LINE__);
1498   file_service->end_file_wait(file_locker, 10);
1499   file_locker = file_service->get_thread_file_descriptor_locker(
1500       &file_state, (File)12, PSI_FILE_READ);
1501   ok(file_locker != nullptr, "locker (global counted)");
1502   file_service->start_file_wait(file_locker, 10, __FILE__, __LINE__);
1503   file_service->end_file_wait(file_locker, 10);
1504   /* The null locker shortcut applies only to socket ops with no byte count */
1505   socket_locker = socket_service->start_socket_wait(
1506       &socket_state, socket_A1, PSI_SOCKET_BIND, 0, "foo.cc", 12);
1507   ok(socket_locker == nullptr, "no locker (global counted)");
1508 
1509   /* TODO */
1510 
1511   /* Pretend the instrument is disabled */
1512   /* ---------------------------------- */
1513 
1514   setup_thread(thread_1, true);
1515   flag_global_instrumentation = true;
1516   flag_events_waits_current = true;
1517   mutex_class_A->m_enabled = false;
1518   rwlock_class_A->m_enabled = false;
1519   cond_class_A->m_enabled = false;
1520   file_class_A->m_enabled = false;
1521   socket_class_A->m_enabled = false;
1522   update_instruments_derived_flags();
1523 
1524   mutex_locker = mutex_service->start_mutex_wait(&mutex_state, mutex_A1,
1525                                                  PSI_MUTEX_LOCK, "foo.cc", 12);
1526   ok(mutex_locker == nullptr, "no locker");
1527   rwlock_locker = rwlock_service->start_rwlock_rdwait(
1528       &rwlock_state, rwlock_A1, PSI_RWLOCK_READLOCK, "foo.cc", 12);
1529   ok(rwlock_locker == nullptr, "no locker");
1530   cond_locker = cond_service->start_cond_wait(&cond_state, cond_A1, mutex_A1,
1531                                               PSI_COND_WAIT, "foo.cc", 12);
1532   ok(cond_locker == nullptr, "no locker");
1533   file_locker = file_service->get_thread_file_name_locker(
1534       &file_state, file_key_A, PSI_FILE_OPEN, "xxx", nullptr);
1535   ok(file_locker == nullptr, "no locker");
1536   file_locker = file_service->get_thread_file_stream_locker(
1537       &file_state, file_A1, PSI_FILE_READ);
1538   ok(file_locker == nullptr, "no locker");
1539   file_locker = file_service->get_thread_file_descriptor_locker(
1540       &file_state, (File)12, PSI_FILE_READ);
1541   ok(file_locker == nullptr, "no locker");
1542   socket_locker = socket_service->start_socket_wait(
1543       &socket_state, socket_A1, PSI_SOCKET_SEND, 12, "foo.cc", 12);
1544   ok(socket_locker == nullptr, "no locker");
1545 
1546   /* Pretend everything is enabled and timed */
1547   /* --------------------------------------- */
1548 
1549   setup_thread(thread_1, true);
1550   flag_global_instrumentation = true;
1551   flag_thread_instrumentation = true;
1552   flag_events_waits_current = true;
1553   mutex_class_A->m_enabled = true;
1554   mutex_class_A->m_timed = true;
1555   rwlock_class_A->m_enabled = true;
1556   rwlock_class_A->m_timed = true;
1557   cond_class_A->m_enabled = true;
1558   cond_class_A->m_timed = true;
1559   file_class_A->m_enabled = true;
1560   file_class_A->m_timed = true;
1561   socket_class_A->m_enabled = true;
1562   socket_class_A->m_timed = true;
1563   update_instruments_derived_flags();
1564 
1565   mutex_locker = mutex_service->start_mutex_wait(
1566       &mutex_state, mutex_A1, PSI_MUTEX_LOCK, __FILE__, __LINE__);
1567   ok(mutex_locker != nullptr, "locker");
1568   mutex_service->end_mutex_wait(mutex_locker, 0);
1569   rwlock_locker = rwlock_service->start_rwlock_rdwait(
1570       &rwlock_state, rwlock_A1, PSI_RWLOCK_READLOCK, __FILE__, __LINE__);
1571   ok(rwlock_locker != nullptr, "locker");
1572   rwlock_service->end_rwlock_rdwait(rwlock_locker, 0);
1573   cond_locker = cond_service->start_cond_wait(
1574       &cond_state, cond_A1, mutex_A1, PSI_COND_WAIT, __FILE__, __LINE__);
1575   ok(cond_locker != nullptr, "locker");
1576   cond_service->end_cond_wait(cond_locker, 0);
1577   file_locker = file_service->get_thread_file_name_locker(
1578       &file_state, file_key_A, PSI_FILE_STREAM_OPEN, "xxx", nullptr);
1579   ok(file_locker != nullptr, "locker");
1580   file_service->start_file_open_wait(file_locker, __FILE__, __LINE__);
1581   file_service->end_file_open_wait(file_locker, nullptr);
1582   file_locker = file_service->get_thread_file_stream_locker(
1583       &file_state, file_A1, PSI_FILE_READ);
1584   ok(file_locker != nullptr, "locker");
1585   file_service->start_file_wait(file_locker, 10, __FILE__, __LINE__);
1586   file_service->end_file_wait(file_locker, 10);
1587   file_locker = file_service->get_thread_file_descriptor_locker(
1588       &file_state, (File)12, PSI_FILE_READ);
1589   ok(file_locker != nullptr, "locker");
1590   file_service->start_file_wait(file_locker, 10, __FILE__, __LINE__);
1591   file_service->end_file_wait(file_locker, 10);
1592   socket_locker = socket_service->start_socket_wait(
1593       &socket_state, socket_A1, PSI_SOCKET_SEND, 12, "foo.cc", 12);
1594   ok(socket_locker != nullptr, "locker");
1595   socket_service->end_socket_wait(socket_locker, 10);
1596 
1597   /* Pretend the socket does not have a thread owner */
1598   /* ---------------------------------------------- */
1599 
1600   socket_class_A->m_enabled = true;
1601   socket_A1 = socket_service->init_socket(socket_key_A, nullptr, nullptr, 0);
1602   ok(socket_A1 != nullptr, "instrumented");
1603   /* Socket thread owner has not been set */
1604   socket_locker = socket_service->start_socket_wait(
1605       &socket_state, socket_A1, PSI_SOCKET_SEND, 12, "foo.cc", 12);
1606   ok(socket_locker != nullptr, "locker (owner not used)");
1607   socket_service->end_socket_wait(socket_locker, 10);
1608 
1609   /* Pretend the running thread is not instrumented */
1610   /* ---------------------------------------------- */
1611 
1612   thread_service->delete_current_thread();
1613   flag_events_waits_current = true;
1614   mutex_class_A->m_enabled = true;
1615   rwlock_class_A->m_enabled = true;
1616   cond_class_A->m_enabled = true;
1617   file_class_A->m_enabled = true;
1618   socket_class_A->m_enabled = true;
1619   update_instruments_derived_flags();
1620 
1621   mutex_locker = mutex_service->start_mutex_wait(&mutex_state, mutex_A1,
1622                                                  PSI_MUTEX_LOCK, "foo.cc", 12);
1623   ok(mutex_locker == nullptr, "no locker");
1624   rwlock_locker = rwlock_service->start_rwlock_rdwait(
1625       &rwlock_state, rwlock_A1, PSI_RWLOCK_READLOCK, "foo.cc", 12);
1626   ok(rwlock_locker == nullptr, "no locker");
1627   cond_locker = cond_service->start_cond_wait(&cond_state, cond_A1, mutex_A1,
1628                                               PSI_COND_WAIT, "foo.cc", 12);
1629   ok(cond_locker == nullptr, "no locker");
1630   file_locker = file_service->get_thread_file_name_locker(
1631       &file_state, file_key_A, PSI_FILE_OPEN, "xxx", nullptr);
1632   ok(file_locker == nullptr, "no locker");
1633   file_locker = file_service->get_thread_file_stream_locker(
1634       &file_state, file_A1, PSI_FILE_READ);
1635   ok(file_locker == nullptr, "no locker");
1636   file_locker = file_service->get_thread_file_descriptor_locker(
1637       &file_state, (File)12, PSI_FILE_READ);
1638   ok(file_locker == nullptr, "no locker");
1639   socket_locker = socket_service->start_socket_wait(
1640       &socket_state, socket_A1, PSI_SOCKET_SEND, 12, "foo.cc", 12);
1641   ok(socket_locker == nullptr, "no locker");
1642 
1643   shutdown_performance_schema();
1644 }
1645 
test_file_instrumentation_leak()1646 static void test_file_instrumentation_leak() {
1647   PSI_thread_service_t *thread_service;
1648   PSI_mutex_service_t *mutex_service;
1649   PSI_rwlock_service_t *rwlock_service;
1650   PSI_cond_service_t *cond_service;
1651   PSI_file_service_t *file_service;
1652   PSI_socket_service_t *socket_service;
1653   PSI_table_service_t *table_service;
1654   PSI_mdl_service_t *mdl_service;
1655   PSI_idle_service_t *idle_service;
1656   PSI_stage_service_t *stage_service;
1657   PSI_statement_service_t *statement_service;
1658   PSI_transaction_service_t *transaction_service;
1659   PSI_memory_service_t *memory_service;
1660   PSI_error_service_t *error_service;
1661   PSI_data_lock_service_t *data_lock_service;
1662   PSI_system_service_t *system_service;
1663   PSI_tls_channel_service_t *tls_channel_service;
1664 
1665   diag("test_file_instrumentation_leak");
1666 
1667   load_perfschema(&thread_service, &mutex_service, &rwlock_service,
1668                   &cond_service, &file_service, &socket_service, &table_service,
1669                   &mdl_service, &idle_service, &stage_service,
1670                   &statement_service, &transaction_service, &memory_service,
1671                   &error_service, &data_lock_service, &system_service,
1672                   &tls_channel_service);
1673 
1674   PSI_file_key file_key_A;
1675   PSI_file_key file_key_B;
1676   PSI_file_info all_file[] = {{&file_key_A, "F-A", 0, 0, ""},
1677                               {&file_key_B, "F-B", 0, 0, ""}};
1678 
1679   PSI_thread_key thread_key_1;
1680   PSI_thread_info all_thread[] = {{&thread_key_1, "T-1", 0, 0, ""}};
1681 
1682   file_service->register_file("test", all_file, 2);
1683   thread_service->register_thread("test", all_thread, 1);
1684 
1685   PFS_file_class *file_class_A;
1686   PFS_file_class *file_class_B;
1687   PSI_file_locker_state file_state;
1688   PSI_thread *thread_1;
1689 
1690   /* Preparation */
1691 
1692   thread_1 = thread_service->new_thread(thread_key_1, nullptr, 0);
1693   ok(thread_1 != nullptr, "T-1");
1694   thread_service->set_thread_id(thread_1, 1);
1695 
1696   file_class_A = find_file_class(file_key_A);
1697   ok(file_class_A != nullptr, "file info A");
1698 
1699   file_class_B = find_file_class(file_key_B);
1700   ok(file_class_B != nullptr, "file info B");
1701 
1702   thread_service->set_thread(thread_1);
1703 
1704   /* Pretend everything is enabled */
1705   /* ----------------------------- */
1706 
1707   setup_thread(thread_1, true);
1708   flag_events_waits_current = true;
1709   file_class_A->m_enabled = true;
1710   file_class_B->m_enabled = true;
1711 
1712   PSI_file_locker *file_locker;
1713 
1714   /* Simulate OPEN + READ of 100 bytes + CLOSE on descriptor 12 */
1715 
1716   file_locker = file_service->get_thread_file_name_locker(
1717       &file_state, file_key_A, PSI_FILE_OPEN, "AAA", nullptr);
1718   ok(file_locker != nullptr, "locker");
1719   file_service->start_file_open_wait(file_locker, __FILE__, __LINE__);
1720   file_service->end_file_open_wait_and_bind_to_descriptor(file_locker, 12);
1721 
1722   file_locker = file_service->get_thread_file_descriptor_locker(
1723       &file_state, (File)12, PSI_FILE_READ);
1724   ok(file_locker != nullptr, "locker");
1725   file_service->start_file_wait(file_locker, 100, __FILE__, __LINE__);
1726   file_service->end_file_wait(file_locker, 100);
1727 
1728   file_locker = file_service->get_thread_file_descriptor_locker(
1729       &file_state, (File)12, PSI_FILE_CLOSE);
1730   ok(file_locker != nullptr, "locker");
1731   file_service->start_file_wait(file_locker, 0, __FILE__, __LINE__);
1732   file_service->end_file_wait(file_locker, 0);
1733 
1734   /* Simulate uninstrumented-OPEN + WRITE on descriptor 24 */
1735 
1736   file_locker = file_service->get_thread_file_descriptor_locker(
1737       &file_state, (File)24, PSI_FILE_WRITE);
1738   ok(file_locker == nullptr, "no locker, since the open was not instrumented");
1739 
1740   /*
1741     Simulate uninstrumented-OPEN + WRITE on descriptor 12 :
1742     the instrumentation should not leak (don't charge the file io on unknown B
1743     to "AAA")
1744   */
1745 
1746   file_locker = file_service->get_thread_file_descriptor_locker(
1747       &file_state, (File)12, PSI_FILE_WRITE);
1748   ok(file_locker == nullptr, "no locker, no leak");
1749 
1750   shutdown_performance_schema();
1751 }
1752 
1753 #ifdef LATER
test_enabled()1754 static void test_enabled() {
1755   PSI *psi;
1756 
1757   diag("test_enabled");
1758 
1759   psi = load_perfschema();
1760 
1761   PSI_mutex_key mutex_key_A;
1762   PSI_mutex_key mutex_key_B;
1763   PSI_mutex_info all_mutex[] = {{&mutex_key_A, "M-A", 0, 0, ""},
1764                                 {&mutex_key_B, "M-B", 0, 0, ""}};
1765 
1766   PSI_rwlock_key rwlock_key_A;
1767   PSI_rwlock_key rwlock_key_B;
1768   PSI_rwlock_info all_rwlock[] = {{&rwlock_key_A, "RW-A", 0, 0, ""},
1769                                   {&rwlock_key_B, "RW-B", 0, 0, ""}};
1770 
1771   PSI_cond_key cond_key_A;
1772   PSI_cond_key cond_key_B;
1773   PSI_cond_info all_cond[] = {{&cond_key_A, "C-A", 0, 0, ""},
1774                               {&cond_key_B, "C-B", 0, 0, ""}};
1775 
1776   shutdown_performance_schema();
1777 }
1778 #endif
1779 
test_event_name_index()1780 static void test_event_name_index() {
1781   PSI_thread_service_t *thread_service;
1782   PSI_mutex_service_t *mutex_service;
1783   PSI_rwlock_service_t *rwlock_service;
1784   PSI_cond_service_t *cond_service;
1785   PSI_file_service_t *file_service;
1786   PSI_socket_service_t *socket_service;
1787   PSI_table_service_t *table_service;
1788   PSI_mdl_service_t *mdl_service;
1789   PSI_idle_service_t *idle_service;
1790   PSI_stage_service_t *stage_service;
1791   PSI_statement_service_t *statement_service;
1792   PSI_transaction_service_t *transaction_service;
1793   PSI_memory_service_t *memory_service;
1794   PSI_error_service_t *error_service;
1795   PSI_data_lock_service_t *data_lock_service;
1796   PSI_tls_channel_service_t *tls_channel_service;
1797 
1798   PSI_thread_bootstrap *thread_boot;
1799   PSI_mutex_bootstrap *mutex_boot;
1800   PSI_rwlock_bootstrap *rwlock_boot;
1801   PSI_cond_bootstrap *cond_boot;
1802   PSI_file_bootstrap *file_boot;
1803   PSI_socket_bootstrap *socket_boot;
1804   PSI_table_bootstrap *table_boot;
1805   PSI_mdl_bootstrap *mdl_boot;
1806   PSI_idle_bootstrap *idle_boot;
1807   PSI_stage_bootstrap *stage_boot;
1808   PSI_statement_bootstrap *statement_boot;
1809   PSI_transaction_bootstrap *transaction_boot;
1810   PSI_memory_bootstrap *memory_boot;
1811   PSI_error_bootstrap *error_boot;
1812   PSI_data_lock_bootstrap *data_lock_boot;
1813   PSI_system_bootstrap *system_boot;
1814   PSI_tls_channel_bootstrap *tls_channel_boot;
1815   PFS_global_param param;
1816 
1817   diag("test_event_name_index");
1818 
1819   memset(&param, 0xFF, sizeof(param));
1820   param.m_enabled = true;
1821 
1822   /* NOTE: Need to add 4 to each index: table io, table lock, idle, metadata
1823    * lock */
1824 
1825   /* Per mutex info waits should be at [0..9] */
1826   param.m_mutex_class_sizing = 10;
1827   /* Per rwlock info waits should be at [10..29] */
1828   param.m_rwlock_class_sizing = 20;
1829   /* Per cond info waits should be at [30..69] */
1830   param.m_cond_class_sizing = 40;
1831   /* Per file info waits should be at [70..149] */
1832   param.m_file_class_sizing = 80;
1833   /* Per socket info waits should be at [150..309] */
1834   param.m_socket_class_sizing = 160;
1835   /* Per table info waits should be at [310] */
1836   param.m_table_share_sizing = 320;
1837 
1838   param.m_thread_class_sizing = 0;
1839   param.m_user_sizing = 0;
1840   param.m_account_sizing = 0;
1841   param.m_host_sizing = 0;
1842   param.m_stage_class_sizing = 0;
1843   param.m_events_stages_history_sizing = 0;
1844   param.m_events_stages_history_long_sizing = 0;
1845   param.m_statement_class_sizing = 0;
1846   param.m_events_statements_history_sizing = 0;
1847   param.m_events_statements_history_long_sizing = 0;
1848   param.m_events_transactions_history_sizing = 0;
1849   param.m_events_transactions_history_long_sizing = 0;
1850   param.m_digest_sizing = 0;
1851   param.m_session_connect_attrs_sizing = 0;
1852   param.m_program_sizing = 0;
1853   param.m_statement_stack_sizing = 10;
1854   param.m_memory_class_sizing = 12;
1855   param.m_metadata_lock_sizing = 10;
1856   param.m_max_digest_length = 0;
1857   param.m_max_sql_text_length = 1000;
1858   param.m_error_sizing = 0;
1859 
1860   param.m_mutex_sizing = 0;
1861   param.m_rwlock_sizing = 0;
1862   param.m_cond_sizing = 0;
1863   param.m_thread_sizing = 0;
1864   param.m_table_sizing = 0;
1865   param.m_file_sizing = 0;
1866   param.m_file_handle_sizing = 0;
1867   param.m_socket_sizing = 0;
1868   param.m_events_waits_history_sizing = 0;
1869   param.m_events_waits_history_long_sizing = 0;
1870   param.m_setup_actor_sizing = 0;
1871   param.m_setup_object_sizing = 0;
1872 
1873   param.m_hints.m_table_definition_cache = 100;
1874   param.m_hints.m_table_open_cache = 100;
1875   param.m_hints.m_max_connections = 100;
1876   param.m_hints.m_open_files_limit = 100;
1877   param.m_hints.m_max_prepared_stmt_count = 100;
1878 
1879   pre_initialize_performance_schema();
1880   initialize_performance_schema(
1881       &param, &thread_boot, &mutex_boot, &rwlock_boot, &cond_boot, &file_boot,
1882       &socket_boot, &table_boot, &mdl_boot, &idle_boot, &stage_boot,
1883       &statement_boot, &transaction_boot, &memory_boot, &error_boot,
1884       &data_lock_boot, &system_boot, &tls_channel_boot);
1885   ok(thread_boot != nullptr, "thread_bootstrap");
1886   ok(mutex_boot != nullptr, "mutex_bootstrap");
1887   ok(rwlock_boot != nullptr, "rwlock_bootstrap");
1888   ok(cond_boot != nullptr, "cond_bootstrap");
1889   ok(file_boot != nullptr, "file_bootstrap");
1890   ok(socket_boot != nullptr, "socket_bootstrap");
1891   ok(table_boot != nullptr, "table_bootstrap");
1892   ok(mdl_boot != nullptr, "mdl_bootstrap");
1893   ok(idle_boot != nullptr, "idle_bootstrap");
1894   ok(stage_boot != nullptr, "stage_bootstrap");
1895   ok(statement_boot != nullptr, "statement_bootstrap");
1896   ok(transaction_boot != nullptr, "transaction_bootstrap");
1897   ok(memory_boot != nullptr, "memory_bootstrap");
1898   ok(error_boot != nullptr, "error_bootstrap");
1899   ok(data_lock_boot != nullptr, "data_lock_bootstrap");
1900   ok(tls_channel_boot != nullptr, "tls_channel_bootstrap");
1901 
1902   thread_service = (PSI_thread_service_t *)thread_boot->get_interface(
1903       PSI_CURRENT_THREAD_VERSION);
1904   ok(thread_service != nullptr, "thread_service");
1905   mutex_service =
1906       (PSI_mutex_service_t *)mutex_boot->get_interface(PSI_MUTEX_VERSION_1);
1907   ok(mutex_service != nullptr, "mutex_service");
1908   rwlock_service =
1909       (PSI_rwlock_service_t *)rwlock_boot->get_interface(PSI_RWLOCK_VERSION_2);
1910   ok(rwlock_service != nullptr, "rwlock_service");
1911   cond_service =
1912       (PSI_cond_service_t *)cond_boot->get_interface(PSI_COND_VERSION_1);
1913   ok(cond_service != nullptr, "cond_service");
1914   file_service =
1915       (PSI_file_service_t *)file_boot->get_interface(PSI_FILE_VERSION_2);
1916   ok(file_service != nullptr, "file_service");
1917   socket_service =
1918       (PSI_socket_service_t *)socket_boot->get_interface(PSI_SOCKET_VERSION_1);
1919   ok(socket_service != nullptr, "socket_service");
1920   table_service =
1921       (PSI_table_service_t *)table_boot->get_interface(PSI_TABLE_VERSION_1);
1922   ok(table_service != nullptr, "table_service");
1923   mdl_service = (PSI_mdl_service_t *)mdl_boot->get_interface(PSI_MDL_VERSION_1);
1924   ok(mdl_service != nullptr, "mdl_service");
1925   idle_service =
1926       (PSI_idle_service_t *)idle_boot->get_interface(PSI_IDLE_VERSION_1);
1927   ok(idle_service != nullptr, "idle_service");
1928   stage_service =
1929       (PSI_stage_service_t *)stage_boot->get_interface(PSI_STAGE_VERSION_1);
1930   ok(stage_service != nullptr, "stage_service");
1931   statement_service = (PSI_statement_service_t *)statement_boot->get_interface(
1932       PSI_STATEMENT_VERSION_2);
1933   ok(statement_service != nullptr, "statement_service");
1934   transaction_service =
1935       (PSI_transaction_service_t *)transaction_boot->get_interface(
1936           PSI_TRANSACTION_VERSION_1);
1937   ok(transaction_service != nullptr, "transaction_service");
1938   memory_service =
1939       (PSI_memory_service_t *)memory_boot->get_interface(PSI_MEMORY_VERSION_1);
1940   ok(memory_service != nullptr, "memory_service");
1941   error_service =
1942       (PSI_error_service_t *)error_boot->get_interface(PSI_MEMORY_VERSION_1);
1943   ok(error_service != nullptr, "error_service");
1944   data_lock_service = (PSI_data_lock_service_t *)data_lock_boot->get_interface(
1945       PSI_DATA_LOCK_VERSION_1);
1946   ok(data_lock_service != nullptr, "data_lock_service");
1947   tls_channel_service =
1948       (PSI_tls_channel_service_t *)tls_channel_boot->get_interface(
1949           PSI_TLS_CHANNEL_VERSION_1);
1950   ok(tls_channel_service != nullptr, "tls_channel_service");
1951 
1952   PFS_mutex_class *mutex_class;
1953   PSI_mutex_key dummy_mutex_key_1;
1954   PSI_mutex_key dummy_mutex_key_2;
1955   PSI_mutex_info dummy_mutexes[] = {{&dummy_mutex_key_1, "M-1", 0, 0, ""},
1956                                     {&dummy_mutex_key_2, "M-2", 0, 0, ""}};
1957 
1958   mutex_service->register_mutex("X", dummy_mutexes, 2);
1959   mutex_class = find_mutex_class(dummy_mutex_key_1);
1960   ok(mutex_class != nullptr, "mutex class 1");
1961   ok(mutex_class->m_event_name_index == 4, "index 4");
1962   mutex_class = find_mutex_class(dummy_mutex_key_2);
1963   ok(mutex_class != nullptr, "mutex class 2");
1964   ok(mutex_class->m_event_name_index == 5, "index 5");
1965 
1966   PFS_rwlock_class *rwlock_class;
1967   PSI_rwlock_key dummy_rwlock_key_1;
1968   PSI_rwlock_key dummy_rwlock_key_2;
1969   PSI_rwlock_info dummy_rwlocks[] = {{&dummy_rwlock_key_1, "RW-1", 0, 0, ""},
1970                                      {&dummy_rwlock_key_2, "RW-2", 0, 0, ""}};
1971 
1972   rwlock_service->register_rwlock("X", dummy_rwlocks, 2);
1973   rwlock_class = find_rwlock_class(dummy_rwlock_key_1);
1974   ok(rwlock_class != nullptr, "rwlock class 1");
1975   ok(rwlock_class->m_event_name_index == 15, "index 15");
1976   rwlock_class = find_rwlock_class(dummy_rwlock_key_2);
1977   ok(rwlock_class != nullptr, "rwlock class 2");
1978   ok(rwlock_class->m_event_name_index == 16, "index 16");
1979 
1980   PFS_cond_class *cond_class;
1981   PSI_cond_key dummy_cond_key_1;
1982   PSI_cond_key dummy_cond_key_2;
1983   PSI_cond_info dummy_conds[] = {{&dummy_cond_key_1, "C-1", 0, 0, ""},
1984                                  {&dummy_cond_key_2, "C-2", 0, 0, ""}};
1985 
1986   cond_service->register_cond("X", dummy_conds, 2);
1987   cond_class = find_cond_class(dummy_cond_key_1);
1988   ok(cond_class != nullptr, "cond class 1");
1989   ok(cond_class->m_event_name_index == 34, "index 34");
1990   cond_class = find_cond_class(dummy_cond_key_2);
1991   ok(cond_class != nullptr, "cond class 2");
1992   ok(cond_class->m_event_name_index == 35, "index 35");
1993 
1994   PFS_file_class *file_class;
1995   PSI_file_key dummy_file_key_1;
1996   PSI_file_key dummy_file_key_2;
1997   PSI_file_info dummy_files[] = {{&dummy_file_key_1, "F-1", 0, 0, ""},
1998                                  {&dummy_file_key_2, "F-2", 0, 0, ""}};
1999 
2000   file_service->register_file("X", dummy_files, 2);
2001   file_class = find_file_class(dummy_file_key_1);
2002   ok(file_class != nullptr, "file class 1");
2003   ok(file_class->m_event_name_index == 74, "index 74");
2004   file_class = find_file_class(dummy_file_key_2);
2005   ok(file_class != nullptr, "file class 2");
2006   ok(file_class->m_event_name_index == 75, "index 75");
2007 
2008   PFS_socket_class *socket_class;
2009   PSI_socket_key dummy_socket_key_1;
2010   PSI_socket_key dummy_socket_key_2;
2011   PSI_socket_info dummy_sockets[] = {{&dummy_socket_key_1, "S-1", 0, 0, ""},
2012                                      {&dummy_socket_key_2, "S-2", 0, 0, ""}};
2013 
2014   socket_service->register_socket("X", dummy_sockets, 2);
2015   socket_class = find_socket_class(dummy_socket_key_1);
2016   ok(socket_class != nullptr, "socket class 1");
2017   ok(socket_class->m_event_name_index == 154, "index 154");
2018   socket_class = find_socket_class(dummy_socket_key_2);
2019   ok(socket_class != nullptr, "socket class 2");
2020   ok(socket_class->m_event_name_index == 155, "index 155");
2021 
2022   ok(global_table_io_class.m_event_name_index == 0, "index 0");
2023   ok(global_table_lock_class.m_event_name_index == 1, "index 1");
2024   ok(wait_class_max = 314, "314 event names");  // 4 global classes
2025 
2026   shutdown_performance_schema();
2027 }
2028 
test_memory_instruments()2029 static void test_memory_instruments() {
2030   PSI_thread_service_t *thread_service;
2031   PSI_mutex_service_t *mutex_service;
2032   PSI_rwlock_service_t *rwlock_service;
2033   PSI_cond_service_t *cond_service;
2034   PSI_file_service_t *file_service;
2035   PSI_socket_service_t *socket_service;
2036   PSI_table_service_t *table_service;
2037   PSI_mdl_service_t *mdl_service;
2038   PSI_idle_service_t *idle_service;
2039   PSI_stage_service_t *stage_service;
2040   PSI_statement_service_t *statement_service;
2041   PSI_transaction_service_t *transaction_service;
2042   PSI_memory_service_t *memory_service;
2043   PSI_error_service_t *error_service;
2044   PSI_data_lock_service_t *data_lock_service;
2045   PSI_system_service_t *system_service;
2046   PSI_tls_channel_service_t *tls_channel_service;
2047   PSI_thread *owner;
2048 
2049   diag("test_memory_instruments");
2050 
2051   load_perfschema(&thread_service, &mutex_service, &rwlock_service,
2052                   &cond_service, &file_service, &socket_service, &table_service,
2053                   &mdl_service, &idle_service, &stage_service,
2054                   &statement_service, &transaction_service, &memory_service,
2055                   &error_service, &data_lock_service, &system_service,
2056                   &tls_channel_service);
2057 
2058   PSI_memory_key memory_key_A;
2059   PSI_memory_info all_memory[] = {{&memory_key_A, "M-A", 0, 0, ""}};
2060 
2061   PSI_thread_key thread_key_1;
2062   PSI_thread_info all_thread[] = {{&thread_key_1, "T-1", 0, 0, ""}};
2063 
2064   memory_service->register_memory("test", all_memory, 1);
2065   thread_service->register_thread("test", all_thread, 1);
2066 
2067   PFS_memory_class *memory_class_A;
2068   PSI_thread *thread_1;
2069   PSI_memory_key key;
2070 
2071   /* Preparation */
2072 
2073   thread_1 = thread_service->new_thread(thread_key_1, nullptr, 0);
2074   ok(thread_1 != nullptr, "T-1");
2075   thread_service->set_thread_id(thread_1, 1);
2076 
2077   memory_class_A = find_memory_class(memory_key_A);
2078   ok(memory_class_A != nullptr, "memory info A");
2079 
2080   /* Pretend thread T-1 is running, and enabled */
2081   /* ------------------------------------------ */
2082 
2083   thread_service->set_thread(thread_1);
2084   setup_thread(thread_1, true);
2085 
2086   /* Enable all instruments */
2087 
2088   memory_class_A->m_enabled = true;
2089 
2090   /* for coverage, need to print stats collected. */
2091 
2092   key = memory_service->memory_alloc(memory_key_A, 100, &owner);
2093   ok(key == memory_key_A, "alloc memory info A");
2094   key = memory_service->memory_realloc(memory_key_A, 100, 200, &owner);
2095   ok(key == memory_key_A, "realloc memory info A");
2096   key = memory_service->memory_realloc(memory_key_A, 200, 300, &owner);
2097   ok(key == memory_key_A, "realloc up memory info A");
2098   key = memory_service->memory_realloc(memory_key_A, 300, 50, &owner);
2099   ok(key == memory_key_A, "realloc down memory info A");
2100   memory_service->memory_free(memory_key_A, 50, owner);
2101 
2102   /* Use global instrumentation only */
2103   /* ------------------------------- */
2104 
2105   flag_thread_instrumentation = false;
2106 
2107   key = memory_service->memory_alloc(memory_key_A, 100, &owner);
2108   ok(key == memory_key_A, "alloc memory info A");
2109   key = memory_service->memory_realloc(memory_key_A, 100, 200, &owner);
2110   ok(key == memory_key_A, "realloc memory info A");
2111   key = memory_service->memory_realloc(memory_key_A, 200, 300, &owner);
2112   ok(key == memory_key_A, "realloc up memory info A");
2113   key = memory_service->memory_realloc(memory_key_A, 300, 50, &owner);
2114   ok(key == memory_key_A, "realloc down memory info A");
2115   memory_service->memory_free(memory_key_A, 50, owner);
2116 
2117   /* Garbage, for robustness */
2118   /* ----------------------- */
2119 
2120   key = memory_service->memory_alloc(9999, 100, &owner);
2121   ok(key == PSI_NOT_INSTRUMENTED, "alloc with unknown key");
2122   key = memory_service->memory_realloc(PSI_NOT_INSTRUMENTED, 100, 200, &owner);
2123   ok(key == PSI_NOT_INSTRUMENTED, "realloc with unknown key");
2124   memory_service->memory_free(PSI_NOT_INSTRUMENTED, 200, owner);
2125 
2126   shutdown_performance_schema();
2127 }
2128 
test_leaks()2129 static void test_leaks() {
2130   PSI_thread_bootstrap *thread_boot;
2131   PSI_mutex_bootstrap *mutex_boot;
2132   PSI_rwlock_bootstrap *rwlock_boot;
2133   PSI_cond_bootstrap *cond_boot;
2134   PSI_file_bootstrap *file_boot;
2135   PSI_socket_bootstrap *socket_boot;
2136   PSI_table_bootstrap *table_boot;
2137   PSI_mdl_bootstrap *mdl_boot;
2138   PSI_idle_bootstrap *idle_boot;
2139   PSI_stage_bootstrap *stage_boot;
2140   PSI_statement_bootstrap *statement_boot;
2141   PSI_transaction_bootstrap *transaction_boot;
2142   PSI_memory_bootstrap *memory_boot;
2143   PSI_data_lock_bootstrap *data_lock_boot;
2144   PSI_error_bootstrap *error_boot;
2145   PSI_system_bootstrap *system_boot;
2146   PSI_tls_channel_bootstrap *tls_channel_boot;
2147   PFS_global_param param;
2148 
2149   /* Allocate everything, to make sure cleanup does not forget anything. */
2150 
2151   memset(&param, 0xFF, sizeof(param));
2152   param.m_enabled = true;
2153   param.m_mutex_class_sizing = 10;
2154   param.m_rwlock_class_sizing = 10;
2155   param.m_cond_class_sizing = 10;
2156   param.m_thread_class_sizing = 10;
2157   param.m_table_share_sizing = 10;
2158   param.m_file_class_sizing = 10;
2159   param.m_socket_class_sizing = 10;
2160   param.m_mutex_sizing = 1000;
2161   param.m_rwlock_sizing = 1000;
2162   param.m_cond_sizing = 1000;
2163   param.m_thread_sizing = 1000;
2164   param.m_table_sizing = 1000;
2165   param.m_file_sizing = 1000;
2166   param.m_file_handle_sizing = 1000;
2167   param.m_socket_sizing = 1000;
2168   param.m_events_waits_history_sizing = 10;
2169   param.m_events_waits_history_long_sizing = 1000;
2170   param.m_setup_actor_sizing = 1000;
2171   param.m_setup_object_sizing = 1000;
2172   param.m_host_sizing = 1000;
2173   param.m_user_sizing = 1000;
2174   param.m_account_sizing = 1000;
2175   param.m_stage_class_sizing = 10;
2176   param.m_events_stages_history_sizing = 10;
2177   param.m_events_stages_history_long_sizing = 1000;
2178   param.m_statement_class_sizing = 10;
2179   param.m_events_statements_history_sizing = 10;
2180   param.m_events_statements_history_long_sizing = 1000;
2181   param.m_session_connect_attrs_sizing = 1000;
2182   param.m_memory_class_sizing = 10;
2183   param.m_metadata_lock_sizing = 1000;
2184   param.m_digest_sizing = 1000;
2185   param.m_program_sizing = 1000;
2186   param.m_statement_stack_sizing = 10;
2187   param.m_max_digest_length = 1000;
2188   param.m_max_sql_text_length = 1000;
2189   param.m_error_sizing = 1000;
2190 
2191   param.m_hints.m_table_definition_cache = 100;
2192   param.m_hints.m_table_open_cache = 100;
2193   param.m_hints.m_max_connections = 100;
2194   param.m_hints.m_open_files_limit = 100;
2195   param.m_hints.m_max_prepared_stmt_count = 100;
2196 
2197   pre_initialize_performance_schema();
2198   initialize_performance_schema(
2199       &param, &thread_boot, &mutex_boot, &rwlock_boot, &cond_boot, &file_boot,
2200       &socket_boot, &table_boot, &mdl_boot, &idle_boot, &stage_boot,
2201       &statement_boot, &transaction_boot, &memory_boot, &error_boot,
2202       &data_lock_boot, &system_boot, &tls_channel_boot);
2203   ok(thread_boot != nullptr, "thread bootstrap");
2204   ok(mutex_boot != nullptr, "mutex bootstrap");
2205   ok(rwlock_boot != nullptr, "rwlock bootstrap");
2206   ok(cond_boot != nullptr, "cond bootstrap");
2207   ok(file_boot != nullptr, "file bootstrap");
2208   ok(socket_boot != nullptr, "socket bootstrap");
2209   ok(table_boot != nullptr, "table bootstrap");
2210   ok(mdl_boot != nullptr, "mdl bootstrap");
2211   ok(idle_boot != nullptr, "idle bootstrap");
2212   ok(stage_boot != nullptr, "stage bootstrap");
2213   ok(statement_boot != nullptr, "statement bootstrap");
2214   ok(transaction_boot != nullptr, "transaction bootstrap");
2215   ok(memory_boot != nullptr, "memory bootstrap");
2216   ok(error_boot != nullptr, "error bootstrap");
2217   shutdown_performance_schema();
2218 
2219   /* Leaks will be reported with valgrind */
2220 }
2221 
2222 const char *temp_filename1 = "MLfd=12";
2223 const char *temp_filename2 = "MLfd=13";
2224 
2225 /* Simulated my_create_temp_file() */
my_create_temp_file(const char ** filename)2226 File my_create_temp_file(const char **filename) {
2227   *filename = temp_filename1;
2228   return 12;
2229 }
2230 
2231 /* Simulated my_close() */
my_close(File fd MY_ATTRIBUTE ((unused)),bool success)2232 int my_close(File fd MY_ATTRIBUTE((unused)), bool success) {
2233   return (success ? 0 : 1);
2234 }
2235 
2236 /* Simulated my_delete() */
my_delete(const char * filename MY_ATTRIBUTE ((unused)),bool success)2237 int my_delete(const char *filename MY_ATTRIBUTE((unused)), bool success) {
2238   return (success ? 0 : 1);
2239 }
2240 
2241 /* Simulated my_rename() */
my_rename(const char * from MY_ATTRIBUTE ((unused)),const char * to MY_ATTRIBUTE ((unused)),bool success)2242 int my_rename(const char *from MY_ATTRIBUTE((unused)),
2243               const char *to MY_ATTRIBUTE((unused)), bool success) {
2244   return (success ? 0 : 1);
2245 }
2246 
test_file_operations()2247 static void test_file_operations() {
2248   PSI_thread_service_t *thread_service;
2249   PSI_mutex_service_t *mutex_service;
2250   PSI_rwlock_service_t *rwlock_service;
2251   PSI_cond_service_t *cond_service;
2252   PSI_file_service_t *file_service;
2253   PSI_socket_service_t *socket_service;
2254   PSI_table_service_t *table_service;
2255   PSI_mdl_service_t *mdl_service;
2256   PSI_idle_service_t *idle_service;
2257   PSI_stage_service_t *stage_service;
2258   PSI_statement_service_t *statement_service;
2259   PSI_transaction_service_t *transaction_service;
2260   PSI_memory_service_t *memory_service;
2261   PSI_error_service_t *error_service;
2262   PSI_data_lock_service_t *data_lock_service;
2263   PSI_system_service_t *system_service;
2264   PSI_tls_channel_service_t *tls_channel_service;
2265 
2266   diag("test_file_operations SETUP");
2267 
2268   load_perfschema(&thread_service, &mutex_service, &rwlock_service,
2269                   &cond_service, &file_service, &socket_service, &table_service,
2270                   &mdl_service, &idle_service, &stage_service,
2271                   &statement_service, &transaction_service, &memory_service,
2272                   &error_service, &data_lock_service, &system_service,
2273                   &tls_channel_service);
2274 
2275   PFS_file_class *file_class;
2276   PSI_thread *thread_A, *thread_B;
2277   PSI_file_locker *locker_A, *locker_B;
2278   PSI_file_locker_state state_A, state_B;
2279   File fd1, fd2;
2280   const char *filename1, *filename2;
2281   int rc = 0;
2282 
2283   PSI_file_key file_key;
2284   PSI_file_info all_file[] = {{&file_key, "File Class", 0, 0, ""}};
2285   PSI_thread_key thread_key;
2286   PSI_thread_info all_thread[] = {{&thread_key, "Thread Class", 0, 0, ""}};
2287 
2288   file_service->register_file("test", all_file, 1);
2289   thread_service->register_thread("test", all_thread, 1);
2290 
2291   /* Create Thread A and B to simulate operations from different threads. */
2292   thread_A = thread_service->new_thread(thread_key, NULL, 0);
2293   ok(thread_A != NULL, "Thread A");
2294   thread_service->set_thread_id(thread_A, 1);
2295 
2296   thread_B = thread_service->new_thread(thread_key, NULL, 0);
2297   ok(thread_B != NULL, "Thread B");
2298   thread_service->set_thread_id(thread_B, 1);
2299 
2300   file_class = find_file_class(file_key);
2301   ok(file_class != NULL, "File Class");
2302 
2303   flag_global_instrumentation = true;
2304   flag_thread_instrumentation = true;
2305   file_class->m_enabled = true;
2306   file_class->m_timed = true;
2307   update_instruments_derived_flags();
2308 
2309   setup_thread(thread_A, true);
2310   setup_thread(thread_B, true);
2311   flag_events_waits_current = true;
2312   file_class->m_enabled = true;
2313 
2314   /*
2315     TEST 1: Simulate race of mysql_file_close() on Thread A and
2316             mysql_file_create_temp() on Thread B
2317   */
2318   diag("test_file_operations TEST 1");
2319 
2320   /* THREAD A */
2321   thread_service->set_thread(thread_A);
2322   /* Create a temporary file */
2323   locker_A = file_service->get_thread_file_name_locker(
2324       &state_A, file_key, PSI_FILE_CREATE, NULL, &locker_A);
2325   ok(locker_A != NULL, "locker A");
2326   file_service->start_file_open_wait(locker_A, __FILE__, __LINE__);
2327   /* Returns filename with embedded FD */
2328   fd1 = my_create_temp_file(&filename1);
2329   file_service->end_temp_file_open_wait_and_bind_to_descriptor(locker_A, fd1,
2330                                                                filename1);
2331   /* THREAD A */
2332   /* Start mysql_file_close */
2333   locker_A = file_service->get_thread_file_descriptor_locker(&state_A, fd1,
2334                                                              PSI_FILE_CLOSE);
2335   ok(locker_A != NULL, "locker A");
2336   file_service->start_file_close_wait(locker_A, __FILE__, __LINE__);
2337   rc = my_close(fd1, true); /* successful close, FD released */
2338 
2339   /* THREAD B */
2340   thread_service->set_thread(thread_B);
2341   /* Create a temporary file with the same FD before Thread A completes
2342      mysql_file_close()
2343   */
2344   locker_B = file_service->get_thread_file_name_locker(
2345       &state_B, file_key, PSI_FILE_CREATE, NULL, &locker_B);
2346   ok(locker_B != NULL, "locker B");
2347   file_service->start_file_open_wait(locker_B, __FILE__, __LINE__);
2348   /* Returns same FD and filename as Thread A */
2349   fd2 = my_create_temp_file(&filename2);
2350   file_service->end_temp_file_open_wait_and_bind_to_descriptor(locker_B, fd2,
2351                                                                filename2);
2352   /* THREAD A */
2353   thread_service->set_thread(thread_A);
2354   /* Complete mysql_file_close() */
2355   file_service->end_file_close_wait(locker_A, rc);
2356 
2357   /* THREAD B */
2358   /* Close the file and clean up */
2359   locker_B = file_service->get_thread_file_descriptor_locker(&state_B, fd2,
2360                                                              PSI_FILE_CLOSE);
2361   ok(locker_B != NULL, "locker A");
2362   file_service->start_file_close_wait(locker_B, __FILE__, __LINE__);
2363   rc = my_close(fd2, true); /* successful close, FD released */
2364   file_service->end_file_close_wait(locker_B, rc);
2365 
2366   /*
2367     TEST 2: Disable file instrumentation after a file has been created and
2368             before it is closed. Re-enable the instrumentation, then create the
2369             and close the file again.
2370   */
2371   diag("test_file_operations TEST 2");
2372 
2373   /* Create a temporary file */
2374   thread_service->set_thread(thread_A);
2375   locker_A = file_service->get_thread_file_name_locker(
2376       &state_A, file_key, PSI_FILE_CREATE, NULL, &locker_A);
2377   ok(locker_A != NULL, "locker A");
2378   file_service->start_file_open_wait(locker_A, __FILE__, __LINE__);
2379   /* Returns filename with embedded FD */
2380   fd1 = my_create_temp_file(&filename1);
2381   file_service->end_temp_file_open_wait_and_bind_to_descriptor(locker_A, fd1,
2382                                                                filename1);
2383   /* Disable file instrumentation */
2384   file_class->m_enabled = false;
2385   update_instruments_derived_flags();
2386 
2387   /* mysql_file_close() */
2388   locker_A = file_service->get_thread_file_descriptor_locker(&state_A, fd1,
2389                                                              PSI_FILE_CLOSE);
2390   /* File instrumentation should be deleted for temporary files. */
2391   ok(locker_A == NULL, "locker A is NULL");
2392   rc = my_close(fd1, true); /* successful close, FD released */
2393 
2394   /* Re-enable the file instrumentation */
2395   file_class->m_enabled = true;
2396   update_instruments_derived_flags();
2397 
2398   /* Open the same temporary file with the same FD */
2399   locker_A = file_service->get_thread_file_name_locker(
2400       &state_A, file_key, PSI_FILE_CREATE, NULL, &locker_A);
2401   ok(locker_A != NULL, "locker A");
2402   file_service->start_file_open_wait(locker_A, __FILE__, __LINE__);
2403   /* Returns filename with embedded FD */
2404   fd1 = my_create_temp_file(&filename1);
2405   file_service->end_temp_file_open_wait_and_bind_to_descriptor(locker_A, fd1,
2406                                                                filename1);
2407   /* mysql_file_close() */
2408   locker_A = file_service->get_thread_file_descriptor_locker(&state_A, fd1,
2409                                                              PSI_FILE_CLOSE);
2410   ok(locker_A != NULL, "locker A");
2411   file_service->start_file_close_wait(locker_A, __FILE__, __LINE__);
2412   rc = my_close(fd1, true); /* successful close, FD released */
2413   /* Checks for correct open count */
2414   file_service->end_file_close_wait(locker_A, rc);
2415 
2416   /*
2417     TEST 3: Disable file instrumentation after a file has been created and
2418             before it is deleted. Re-enable the instrumentation, then create
2419             and delete the file again.
2420   */
2421   diag("test_file_operations TEST 3");
2422 
2423   /* Create a temporary file */
2424   thread_service->set_thread(thread_A);
2425   locker_A = file_service->get_thread_file_name_locker(
2426       &state_A, file_key, PSI_FILE_CREATE, NULL, &locker_A);
2427   ok(locker_A != NULL, "locker A");
2428   file_service->start_file_open_wait(locker_A, __FILE__, __LINE__);
2429   /* Returns filename with embedded FD */
2430   fd1 = my_create_temp_file(&filename1);
2431   file_service->end_temp_file_open_wait_and_bind_to_descriptor(locker_A, fd1,
2432                                                                filename1);
2433   /* Disable file instrumentation */
2434   file_class->m_enabled = false;
2435   update_instruments_derived_flags();
2436 
2437   /* mysql_file_delete() */
2438   locker_A = file_service->get_thread_file_name_locker(
2439       &state_A, file_key, PSI_FILE_DELETE, temp_filename1, &locker_A);
2440   /* Locker should be NULL if instrumentation disabled. */
2441   ok(locker_A == NULL, "locker A");
2442   rc = my_delete(temp_filename1, true); /* successful delete */
2443 
2444   /* Re-enable the file instrumentation */
2445   file_class->m_enabled = true;
2446   update_instruments_derived_flags();
2447 
2448   /* Open the same temporary file with the same FD */
2449   locker_A = file_service->get_thread_file_name_locker(
2450       &state_A, file_key, PSI_FILE_CREATE, NULL, &locker_A);
2451   ok(locker_A != NULL, "locker A");
2452   file_service->start_file_open_wait(locker_A, __FILE__, __LINE__);
2453   /* Returns filename with embedded FD */
2454   fd1 = my_create_temp_file(&filename1);
2455   file_service->end_temp_file_open_wait_and_bind_to_descriptor(locker_A, fd1,
2456                                                                filename1);
2457 
2458   /* mysql_file_delete() */
2459   locker_A = file_service->get_thread_file_name_locker(
2460       &state_A, file_key, PSI_FILE_DELETE, temp_filename1, &locker_A);
2461   ok(locker_A != NULL, "locker A");
2462   file_service->start_file_close_wait(locker_A, __FILE__, __LINE__);
2463   rc = my_delete(temp_filename1, true); /* successful delete */
2464   file_service->end_file_close_wait(locker_A, rc);
2465 
2466   /*
2467     TEST 4: Disable file instrumentation after a file has been created and
2468             before it is renamed. Re-enable the instrumentation, then delete,
2469             create and delete the file again.
2470   */
2471   diag("test_file_operations TEST 4");
2472 
2473   /* Create a temporary file */
2474   thread_service->set_thread(thread_A);
2475   locker_A = file_service->get_thread_file_name_locker(
2476       &state_A, file_key, PSI_FILE_CREATE, NULL, &locker_A);
2477   ok(locker_A != NULL, "locker A");
2478   file_service->start_file_open_wait(locker_A, __FILE__, __LINE__);
2479   /* Returns filename with embedded FD */
2480   fd1 = my_create_temp_file(&filename1);
2481   file_service->end_temp_file_open_wait_and_bind_to_descriptor(locker_A, fd1,
2482                                                                filename1);
2483   /* Disable file instrumentation */
2484   file_class->m_enabled = false;
2485   update_instruments_derived_flags();
2486 
2487   /* mysql_file_rename() */
2488   locker_A = file_service->get_thread_file_name_locker(
2489       &state_A, file_key, PSI_FILE_RENAME, temp_filename1, &locker_A);
2490   /* Locker should be NULL if file instrumentation disabled. */
2491   ok(locker_A == NULL, "locker A");
2492   rc = my_rename(temp_filename1, temp_filename2, true); /* success */
2493 
2494   /* Re-enable the file instrumentation */
2495   file_class->m_enabled = true;
2496   update_instruments_derived_flags();
2497 
2498   /* mysql_file_delete() */
2499   locker_A = file_service->get_thread_file_name_locker(
2500       &state_A, file_key, PSI_FILE_DELETE, temp_filename2, &locker_A);
2501   ok(locker_A != NULL, "locker A");
2502   file_service->start_file_close_wait(locker_A, __FILE__, __LINE__);
2503   rc = my_delete(temp_filename2, true); /* success */
2504   file_service->end_file_close_wait(locker_A, rc);
2505 
2506   /* Open the original file with the same FD */
2507   locker_A = file_service->get_thread_file_name_locker(
2508       &state_A, file_key, PSI_FILE_CREATE, NULL, &locker_A);
2509   ok(locker_A != NULL, "locker A");
2510   file_service->start_file_open_wait(locker_A, __FILE__, __LINE__);
2511   /* Returns filename with embedded FD */
2512   fd1 = my_create_temp_file(&filename1);
2513   file_service->end_temp_file_open_wait_and_bind_to_descriptor(locker_A, fd1,
2514                                                                filename1);
2515   /* mysql_file_delete() */
2516   locker_A = file_service->get_thread_file_name_locker(
2517       &state_A, file_key, PSI_FILE_DELETE, temp_filename1, &locker_A);
2518   ok(locker_A != NULL, "locker A");
2519   file_service->start_file_close_wait(locker_A, __FILE__, __LINE__);
2520   rc = my_delete(temp_filename1, true); /* successful delete */
2521   file_service->end_file_close_wait(locker_A, rc);
2522 
2523   thread_service->delete_thread(thread_A);
2524   thread_service->delete_thread(thread_B);
2525   shutdown_performance_schema();
2526 }
2527 
do_all_tests()2528 static void do_all_tests() {
2529   /* system charset needed by pfs_statements_digest */
2530   system_charset_info = &my_charset_latin1;
2531 
2532   /* Using initialize_performance_schema(), no partial init needed. */
2533   test_bootstrap();
2534   test_bad_registration();
2535   test_init_disabled();
2536   test_locker_disabled();
2537   test_file_instrumentation_leak();
2538   test_event_name_index();
2539   test_memory_instruments();
2540   test_leaks();
2541   test_file_operations();
2542 }
2543 
main(int,char **)2544 int main(int, char **) {
2545   plan(358);
2546 
2547   MY_INIT("pfs-t");
2548   do_all_tests();
2549   my_end(0);
2550   return (exit_status());
2551 }
2552