1 /* Copyright (c) 2008, 2021, Oracle and/or its affiliates.
2 
3   This program is free software; you can redistribute it and/or modify
4   it under the terms of the GNU General Public License, version 2.0,
5   as published by the Free Software Foundation.
6 
7   This program is also distributed with certain software (including
8   but not limited to OpenSSL) that is licensed under separate terms,
9   as designated in a particular file or component or in included license
10   documentation.  The authors of MySQL hereby grant you an additional
11   permission to link the program and your derivative works with the
12   separately licensed software that they have included with MySQL.
13 
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License, version 2.0, for more details.
18 
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software Foundation,
21   51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22 
23 #include <my_global.h>
24 #include <my_thread.h>
25 #include <pfs_server.h>
26 #include <pfs_instr_class.h>
27 #include <pfs_instr.h>
28 #include <pfs_global.h>
29 #include <pfs_buffer_container.h>
30 #include <tap.h>
31 
32 #include <string.h>
33 #include <memory.h>
34 
35 #include "stub_print_error.h"
36 #include "stub_pfs_defaults.h"
37 #include "stub_global_status_var.h"
38 
39 /* test helpers, to simulate the setup */
40 
setup_thread(PSI_thread * t,bool enabled)41 void setup_thread(PSI_thread *t, bool enabled)
42 {
43   PFS_thread *t2= (PFS_thread*) t;
44   t2->m_enabled= enabled;
45 }
46 
47 /* test helpers, to inspect data */
48 
lookup_file_by_name(const char * name)49 PFS_file* lookup_file_by_name(const char* name)
50 {
51   PFS_file *pfs;
52   size_t len= strlen(name);
53   size_t dirlen;
54   const char *filename;
55   size_t filename_length;
56 
57   PFS_file_iterator it= global_file_container.iterate();
58   pfs= it.scan_next();
59 
60   while (pfs != NULL)
61   {
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 NULL;
78 }
79 
80 /* tests */
81 
test_bootstrap()82 void test_bootstrap()
83 {
84   void *psi;
85   void *psi_2;
86   PSI_bootstrap *boot;
87   PFS_global_param param;
88 
89   diag("test_bootstrap");
90 
91   memset(& param, 0xFF, sizeof(param));
92   param.m_enabled= true;
93   param.m_mutex_class_sizing= 0;
94   param.m_rwlock_class_sizing= 0;
95   param.m_cond_class_sizing= 0;
96   param.m_thread_class_sizing= 0;
97   param.m_table_share_sizing= 0;
98   param.m_file_class_sizing= 0;
99   param.m_socket_class_sizing= 0;
100   param.m_mutex_sizing= 0;
101   param.m_rwlock_sizing= 0;
102   param.m_cond_sizing= 0;
103   param.m_thread_sizing= 0;
104   param.m_table_sizing= 0;
105   param.m_file_sizing= 0;
106   param.m_file_handle_sizing= 0;
107   param.m_socket_sizing= 0;
108   param.m_events_waits_history_sizing= 0;
109   param.m_events_waits_history_long_sizing= 0;
110   param.m_setup_actor_sizing= 0;
111   param.m_setup_object_sizing= 0;
112   param.m_user_sizing= 0;
113   param.m_account_sizing= 0;
114   param.m_host_sizing= 0;
115   param.m_stage_class_sizing= 0;
116   param.m_events_stages_history_sizing= 0;
117   param.m_events_stages_history_long_sizing= 0;
118   param.m_statement_class_sizing= 0;
119   param.m_events_statements_history_sizing= 0;
120   param.m_events_statements_history_long_sizing= 0;
121   param.m_events_transactions_history_sizing= 0;
122   param.m_events_transactions_history_long_sizing= 0;
123   param.m_digest_sizing= 0;
124   param.m_session_connect_attrs_sizing= 0;
125   param.m_program_sizing= 0;
126   param.m_statement_stack_sizing= 0;
127   param.m_memory_class_sizing= 0;
128   param.m_metadata_lock_sizing= 0;
129   param.m_max_digest_length= 0;
130   param.m_max_sql_text_length= 0;
131 
132   param.m_hints.m_table_definition_cache = 100;
133   param.m_hints.m_table_open_cache       = 100;
134   param.m_hints.m_max_connections        = 100;
135   param.m_hints.m_open_files_limit       = 100;
136   param.m_hints.m_max_prepared_stmt_count= 100;
137 
138   pre_initialize_performance_schema();
139   boot= initialize_performance_schema(& param);
140   ok(boot != NULL, "boot");
141   ok(boot->get_interface != NULL, "boot->get_interface");
142 
143   psi= boot->get_interface(0);
144   ok(psi == NULL, "no version 0");
145 
146   psi= boot->get_interface(PSI_VERSION_1);
147   ok(psi != NULL, "version 1");
148 
149   psi_2= boot->get_interface(PSI_VERSION_2);
150   ok(psi_2 == NULL, "version 2");
151 
152   shutdown_performance_schema();
153 }
154 
155 /*
156   Not a test, helper for testing pfs.cc
157 */
load_perfschema()158 PSI * load_perfschema()
159 {
160   PSI *psi;
161   PSI_bootstrap *boot;
162   PFS_global_param param;
163 
164   memset(& param, 0xFF, sizeof(param));
165   param.m_enabled= true;
166   param.m_mutex_class_sizing= 10;
167   param.m_rwlock_class_sizing= 10;
168   param.m_cond_class_sizing= 10;
169   param.m_thread_class_sizing= 10;
170   param.m_table_share_sizing= 10;
171   param.m_file_class_sizing= 10;
172   param.m_socket_class_sizing= 10;
173   param.m_mutex_sizing= 10;
174   param.m_rwlock_sizing= 10;
175   param.m_cond_sizing= 10;
176   param.m_thread_sizing= 10;
177   param.m_table_sizing= 10;
178   param.m_file_sizing= 10;
179   param.m_file_handle_sizing= 50;
180   param.m_socket_sizing= 10;
181   param.m_events_waits_history_sizing= 10;
182   param.m_events_waits_history_long_sizing= 10;
183   param.m_setup_actor_sizing= 0;
184   param.m_setup_object_sizing= 0;
185   param.m_user_sizing= 0;
186   param.m_account_sizing= 0;
187   param.m_host_sizing= 0;
188   param.m_stage_class_sizing= 0;
189   param.m_events_stages_history_sizing= 0;
190   param.m_events_stages_history_long_sizing= 0;
191   param.m_statement_class_sizing= 0;
192   param.m_events_statements_history_sizing= 0;
193   param.m_events_statements_history_long_sizing= 0;
194   param.m_events_transactions_history_sizing= 0;
195   param.m_events_transactions_history_long_sizing= 0;
196   param.m_digest_sizing= 0;
197   param.m_session_connect_attrs_sizing= 0;
198   param.m_program_sizing= 0;
199   param.m_statement_stack_sizing= 10;
200   param.m_memory_class_sizing= 10;
201   param.m_metadata_lock_sizing= 10;
202   param.m_max_digest_length= 0;
203   param.m_max_sql_text_length= 1000;
204 
205   param.m_hints.m_table_definition_cache = 100;
206   param.m_hints.m_table_open_cache       = 100;
207   param.m_hints.m_max_connections        = 100;
208   param.m_hints.m_open_files_limit       = 100;
209   param.m_hints.m_max_prepared_stmt_count= 100;
210 
211   pre_initialize_performance_schema();
212   /* test_bootstrap() covered this, assuming it just works */
213   boot= initialize_performance_schema(& param);
214   psi= (PSI *)boot->get_interface(PSI_VERSION_1);
215 
216   /* Reset every consumer to a known state */
217   flag_global_instrumentation= true;
218   flag_thread_instrumentation= true;
219 
220   return (PSI*) psi;
221 }
222 
test_bad_registration()223 void test_bad_registration()
224 {
225   PSI *psi;
226 
227   diag("test_bad_registration");
228 
229   psi= load_perfschema();
230 
231   /*
232     Test that length('wait/synch/mutex/' (17) + category + '/' (1)) < 32
233     --> category can be up to 13 chars for a mutex.
234   */
235 
236   PSI_mutex_key dummy_mutex_key= 9999;
237   PSI_mutex_info bad_mutex_1[]=
238   {
239     { & dummy_mutex_key, "X", 0}
240   };
241 
242   psi->register_mutex("/", bad_mutex_1, 1);
243   ok(dummy_mutex_key == 0, "zero key");
244   dummy_mutex_key= 9999;
245   psi->register_mutex("a/", bad_mutex_1, 1);
246   ok(dummy_mutex_key == 0, "zero key");
247   dummy_mutex_key= 9999;
248   psi->register_mutex("/b", bad_mutex_1, 1);
249   ok(dummy_mutex_key == 0, "zero key");
250   dummy_mutex_key= 9999;
251   psi->register_mutex("a/b", bad_mutex_1, 1);
252   ok(dummy_mutex_key == 0, "zero key");
253   dummy_mutex_key= 9999;
254   psi->register_mutex("12345678901234", bad_mutex_1, 1);
255   ok(dummy_mutex_key == 0, "zero key");
256   dummy_mutex_key= 9999;
257   psi->register_mutex("1234567890123", bad_mutex_1, 1);
258   ok(dummy_mutex_key == 1, "assigned key");
259 
260   /*
261     Test that length('wait/synch/mutex/' (17) + category + '/' (1) + name) <= 128
262     --> category + name can be up to 110 chars for a mutex.
263   */
264 
265   dummy_mutex_key= 9999;
266   PSI_mutex_info bad_mutex_2[]=
267   {
268     { & dummy_mutex_key,
269       /* 110 chars name */
270       "12345678901234567890123456789012345678901234567890"
271       "12345678901234567890123456789012345678901234567890"
272       "1234567890",
273       0}
274   };
275 
276   psi->register_mutex("X", bad_mutex_2, 1);
277   ok(dummy_mutex_key == 0, "zero key");
278 
279   dummy_mutex_key= 9999;
280   PSI_mutex_info bad_mutex_3[]=
281   {
282     { & dummy_mutex_key,
283       /* 109 chars name */
284       "12345678901234567890123456789012345678901234567890"
285       "12345678901234567890123456789012345678901234567890"
286       "123456789",
287       0}
288   };
289 
290   psi->register_mutex("XX", bad_mutex_3, 1);
291   ok(dummy_mutex_key == 0, "zero key");
292 
293   psi->register_mutex("X", bad_mutex_3, 1);
294   ok(dummy_mutex_key == 2, "assigned key");
295 
296   /*
297     Test that length('wait/synch/rwlock/' (18) + category + '/' (1)) < 32
298     --> category can be up to 12 chars for a rwlock.
299   */
300 
301   PSI_rwlock_key dummy_rwlock_key= 9999;
302   PSI_rwlock_info bad_rwlock_1[]=
303   {
304     { & dummy_rwlock_key, "X", 0}
305   };
306 
307   psi->register_rwlock("/", bad_rwlock_1, 1);
308   ok(dummy_rwlock_key == 0, "zero key");
309   dummy_rwlock_key= 9999;
310   psi->register_rwlock("a/", bad_rwlock_1, 1);
311   ok(dummy_rwlock_key == 0, "zero key");
312   dummy_rwlock_key= 9999;
313   psi->register_rwlock("/b", bad_rwlock_1, 1);
314   ok(dummy_rwlock_key == 0, "zero key");
315   dummy_rwlock_key= 9999;
316   psi->register_rwlock("a/b", bad_rwlock_1, 1);
317   ok(dummy_rwlock_key == 0, "zero key");
318   dummy_rwlock_key= 9999;
319   psi->register_rwlock("1234567890123", bad_rwlock_1, 1);
320   ok(dummy_rwlock_key == 0, "zero key");
321   dummy_rwlock_key= 9999;
322   psi->register_rwlock("123456789012", bad_rwlock_1, 1);
323   ok(dummy_rwlock_key == 1, "assigned key");
324 
325   /*
326     Test that length('wait/synch/rwlock/' (18) + category + '/' (1) + name) <= 128
327     --> category + name can be up to 109 chars for a rwlock.
328   */
329 
330   dummy_rwlock_key= 9999;
331   PSI_rwlock_info bad_rwlock_2[]=
332   {
333     { & dummy_rwlock_key,
334       /* 109 chars name */
335       "12345678901234567890123456789012345678901234567890"
336       "12345678901234567890123456789012345678901234567890"
337       "123456789",
338       0}
339   };
340 
341   psi->register_rwlock("X", bad_rwlock_2, 1);
342   ok(dummy_rwlock_key == 0, "zero key");
343 
344   dummy_rwlock_key= 9999;
345   PSI_rwlock_info bad_rwlock_2_sx[]=
346   {
347     { & dummy_rwlock_key,
348       /* 109 chars name */
349       "12345678901234567890123456789012345678901234567890"
350       "12345678901234567890123456789012345678901234567890"
351       "123456789",
352       PSI_RWLOCK_FLAG_SX}
353   };
354 
355   psi->register_rwlock("Y", bad_rwlock_2_sx, 1);
356   ok(dummy_rwlock_key == 0, "zero key SX");
357 
358   dummy_rwlock_key= 9999;
359   PSI_rwlock_info bad_rwlock_3[]=
360   {
361     { & dummy_rwlock_key,
362       /* 108 chars name */
363       "12345678901234567890123456789012345678901234567890"
364       "12345678901234567890123456789012345678901234567890"
365       "12345678",
366       0}
367   };
368 
369   psi->register_rwlock("XX", bad_rwlock_3, 1);
370   ok(dummy_rwlock_key == 0, "zero key");
371 
372   psi->register_rwlock("X", bad_rwlock_3, 1);
373   ok(dummy_rwlock_key == 2, "assigned key");
374 
375   dummy_rwlock_key= 9999;
376   PSI_rwlock_info bad_rwlock_3_sx[]=
377   {
378     { & dummy_rwlock_key,
379       /* 108 chars name */
380       "12345678901234567890123456789012345678901234567890"
381       "12345678901234567890123456789012345678901234567890"
382       "12345678",
383       PSI_RWLOCK_FLAG_SX}
384   };
385 
386   psi->register_rwlock("YY", bad_rwlock_3_sx, 1);
387   ok(dummy_rwlock_key == 0, "zero key SX");
388 
389   psi->register_rwlock("Y", bad_rwlock_3_sx, 1);
390   ok(dummy_rwlock_key == 3, "assigned key SX");
391 
392   /*
393     Test that length('wait/synch/cond/' (16) + category + '/' (1)) < 32
394     --> category can be up to 14 chars for a cond.
395   */
396 
397   PSI_cond_key dummy_cond_key= 9999;
398   PSI_cond_info bad_cond_1[]=
399   {
400     { & dummy_cond_key, "X", 0}
401   };
402 
403   psi->register_cond("/", bad_cond_1, 1);
404   ok(dummy_cond_key == 0, "zero key");
405   dummy_cond_key= 9999;
406   psi->register_cond("a/", bad_cond_1, 1);
407   ok(dummy_cond_key == 0, "zero key");
408   dummy_cond_key= 9999;
409   psi->register_cond("/b", bad_cond_1, 1);
410   ok(dummy_cond_key == 0, "zero key");
411   dummy_cond_key= 9999;
412   psi->register_cond("a/b", bad_cond_1, 1);
413   ok(dummy_cond_key == 0, "zero key");
414   dummy_cond_key= 9999;
415   psi->register_cond("123456789012345", bad_cond_1, 1);
416   ok(dummy_cond_key == 0, "zero key");
417   dummy_cond_key= 9999;
418   psi->register_cond("12345678901234", bad_cond_1, 1);
419   ok(dummy_cond_key == 1, "assigned key");
420 
421   /*
422     Test that length('wait/synch/cond/' (16) + category + '/' (1) + name) <= 128
423     --> category + name can be up to 111 chars for a cond.
424   */
425 
426   dummy_cond_key= 9999;
427   PSI_cond_info bad_cond_2[]=
428   {
429     { & dummy_cond_key,
430       /* 111 chars name */
431       "12345678901234567890123456789012345678901234567890"
432       "12345678901234567890123456789012345678901234567890"
433       "12345678901",
434       0}
435   };
436 
437   psi->register_cond("X", bad_cond_2, 1);
438   ok(dummy_cond_key == 0, "zero key");
439 
440   dummy_cond_key= 9999;
441   PSI_cond_info bad_cond_3[]=
442   {
443     { & dummy_cond_key,
444       /* 110 chars name */
445       "12345678901234567890123456789012345678901234567890"
446       "12345678901234567890123456789012345678901234567890"
447       "1234567890",
448       0}
449   };
450 
451   psi->register_cond("XX", bad_cond_3, 1);
452   ok(dummy_cond_key == 0, "zero key");
453 
454   psi->register_cond("X", bad_cond_3, 1);
455   ok(dummy_cond_key == 2, "assigned key");
456 
457   /*
458     Test that length('thread/' (7) + category + '/' (1)) < 32
459     --> category can be up to 23 chars for a thread.
460   */
461 
462   PSI_thread_key dummy_thread_key= 9999;
463   PSI_thread_info bad_thread_1[]=
464   {
465     { & dummy_thread_key, "X", 0}
466   };
467 
468   psi->register_thread("/", bad_thread_1, 1);
469   ok(dummy_thread_key == 0, "zero key");
470   dummy_thread_key= 9999;
471   psi->register_thread("a/", bad_thread_1, 1);
472   ok(dummy_thread_key == 0, "zero key");
473   dummy_thread_key= 9999;
474   psi->register_thread("/b", bad_thread_1, 1);
475   ok(dummy_thread_key == 0, "zero key");
476   dummy_thread_key= 9999;
477   psi->register_thread("a/b", bad_thread_1, 1);
478   ok(dummy_thread_key == 0, "zero key");
479   dummy_thread_key= 9999;
480   psi->register_thread("123456789012345678901234", bad_thread_1, 1);
481   ok(dummy_thread_key == 0, "zero key");
482   dummy_thread_key= 9999;
483   psi->register_thread("12345678901234567890123", bad_thread_1, 1);
484   ok(dummy_thread_key == 1, "assigned key");
485 
486   /*
487     Test that length('thread/' (7) + category + '/' (1) + name) <= 128
488     --> category + name can be up to 120 chars for a thread.
489   */
490 
491   dummy_thread_key= 9999;
492   PSI_thread_info bad_thread_2[]=
493   {
494     { & dummy_thread_key,
495       /* 120 chars name */
496       "12345678901234567890123456789012345678901234567890"
497       "12345678901234567890123456789012345678901234567890"
498       "12345678901234567890",
499       0}
500   };
501 
502   psi->register_thread("X", bad_thread_2, 1);
503   ok(dummy_thread_key == 0, "zero key");
504 
505   dummy_thread_key= 9999;
506   PSI_thread_info bad_thread_3[]=
507   {
508     { & dummy_thread_key,
509       /* 119 chars name */
510       "12345678901234567890123456789012345678901234567890"
511       "12345678901234567890123456789012345678901234567890"
512       "1234567890123456789",
513       0}
514   };
515 
516   psi->register_thread("XX", bad_thread_3, 1);
517   ok(dummy_thread_key == 0, "zero key");
518 
519   psi->register_thread("X", bad_thread_3, 1);
520   ok(dummy_thread_key == 2, "assigned key");
521 
522   /*
523     Test that length('wait/io/file/' (13) + category + '/' (1)) < 32
524     --> category can be up to 17 chars for a file.
525   */
526 
527   PSI_file_key dummy_file_key= 9999;
528   PSI_file_info bad_file_1[]=
529   {
530     { & dummy_file_key, "X", 0}
531   };
532 
533   psi->register_file("/", bad_file_1, 1);
534   ok(dummy_file_key == 0, "zero key");
535   dummy_file_key= 9999;
536   psi->register_file("a/", bad_file_1, 1);
537   ok(dummy_file_key == 0, "zero key");
538   dummy_file_key= 9999;
539   psi->register_file("/b", bad_file_1, 1);
540   ok(dummy_file_key == 0, "zero key");
541   dummy_file_key= 9999;
542   psi->register_file("a/b", bad_file_1, 1);
543   ok(dummy_file_key == 0, "zero key");
544   dummy_file_key= 9999;
545   psi->register_file("123456789012345678", bad_file_1, 1);
546   ok(dummy_file_key == 0, "zero key");
547   dummy_file_key= 9999;
548   psi->register_file("12345678901234567", bad_file_1, 1);
549   ok(dummy_file_key == 1, "assigned key");
550 
551   /*
552     Test that length('wait/io/file/' (13) + category + '/' (1) + name) <= 128
553     --> category + name can be up to 114 chars for a file.
554   */
555 
556   dummy_file_key= 9999;
557   PSI_file_info bad_file_2[]=
558   {
559     { & dummy_file_key,
560       /* 114 chars name */
561       "12345678901234567890123456789012345678901234567890"
562       "12345678901234567890123456789012345678901234567890"
563       "12345678901234",
564       0}
565   };
566 
567   psi->register_file("X", bad_file_2, 1);
568   ok(dummy_file_key == 0, "zero key");
569 
570   dummy_file_key= 9999;
571   PSI_file_info bad_file_3[]=
572   {
573     { & dummy_file_key,
574       /* 113 chars name */
575       "12345678901234567890123456789012345678901234567890"
576       "12345678901234567890123456789012345678901234567890"
577       "1234567890123",
578       0}
579   };
580 
581   psi->register_file("XX", bad_file_3, 1);
582   ok(dummy_file_key == 0, "zero key");
583 
584   psi->register_file("X", bad_file_3, 1);
585   ok(dummy_file_key == 2, "assigned key");
586 
587  /*
588     Test that length('wait/io/socket/' (15) + category + '/' (1)) < 32
589     --> category can be up to 15 chars for a socket.
590   */
591 
592   PSI_socket_key dummy_socket_key= 9999;
593   PSI_socket_info bad_socket_1[]=
594   {
595     { & dummy_socket_key, "X", 0}
596   };
597 
598   psi->register_socket("/", bad_socket_1, 1);
599   ok(dummy_socket_key == 0, "zero key");
600   dummy_socket_key= 9999;
601   psi->register_socket("a/", bad_socket_1, 1);
602   ok(dummy_socket_key == 0, "zero key");
603   dummy_socket_key= 9999;
604   psi->register_socket("/b", bad_socket_1, 1);
605   ok(dummy_socket_key == 0, "zero key");
606   dummy_socket_key= 9999;
607   psi->register_socket("a/b", bad_socket_1, 1);
608   ok(dummy_socket_key == 0, "zero key");
609   dummy_socket_key= 9999;
610   psi->register_socket("1234567890123456", bad_socket_1, 1);
611   ok(dummy_socket_key == 0, "zero key");
612   dummy_socket_key= 9999;
613   psi->register_socket("123456789012345", bad_socket_1, 1);
614   ok(dummy_socket_key == 1, "assigned key");
615 
616   /*
617     Test that length('wait/io/socket/' (15) + category + '/' (1) + name) <= 128
618     --> category + name can be up to 112 chars for a socket.
619   */
620 
621   dummy_socket_key= 9999;
622   PSI_socket_info bad_socket_2[]=
623   {
624     { & dummy_socket_key,
625       /* 112 chars name */
626       "12345678901234567890123456789012345678901234567890"
627       "12345678901234567890123456789012345678901234567890"
628       "123456789012",
629       0}
630   };
631 
632   psi->register_socket("X", bad_socket_2, 1);
633   ok(dummy_socket_key == 0, "zero key");
634 
635   dummy_socket_key= 9999;
636   PSI_socket_info bad_socket_3[]=
637   {
638     { & dummy_socket_key,
639       /* 111 chars name */
640       "12345678901234567890123456789012345678901234567890"
641       "12345678901234567890123456789012345678901234567890"
642       "12345678901",
643       0}
644   };
645 
646   psi->register_socket("XX", bad_socket_3, 1);
647   ok(dummy_socket_key == 0, "zero key");
648 
649   psi->register_socket("X", bad_socket_3, 1);
650   ok(dummy_socket_key == 2, "assigned key");
651 
652 
653   shutdown_performance_schema();
654 }
655 
test_init_disabled()656 void test_init_disabled()
657 {
658   PSI *psi;
659 
660   diag("test_init_disabled");
661 
662   psi= load_perfschema();
663 
664   PSI_mutex_key mutex_key_A;
665   PSI_mutex_info all_mutex[]=
666   {
667     { & mutex_key_A, "M-A", 0}
668   };
669 
670   PSI_rwlock_key rwlock_key_A;
671   PSI_rwlock_info all_rwlock[]=
672   {
673     { & rwlock_key_A, "RW-A", 0}
674   };
675 
676   PSI_cond_key cond_key_A;
677   PSI_cond_info all_cond[]=
678   {
679     { & cond_key_A, "C-A", 0}
680   };
681 
682   PSI_file_key file_key_A;
683   PSI_file_info all_file[]=
684   {
685     { & file_key_A, "F-A", 0}
686   };
687 
688   PSI_socket_key socket_key_A;
689   PSI_socket_info all_socket[]=
690   {
691     { & socket_key_A, "S-A", 0}
692   };
693 
694   PSI_thread_key thread_key_1;
695   PSI_thread_info all_thread[]=
696   {
697     { & thread_key_1, "T-1", 0}
698   };
699 
700   psi->register_mutex("test", all_mutex, 1);
701   psi->register_rwlock("test", all_rwlock, 1);
702   psi->register_cond("test", all_cond, 1);
703   psi->register_file("test", all_file, 1);
704   psi->register_socket("test", all_socket, 1);
705   psi->register_thread("test", all_thread, 1);
706 
707   PFS_mutex_class *mutex_class_A;
708   PFS_rwlock_class *rwlock_class_A;
709   PFS_cond_class *cond_class_A;
710   PFS_file_class *file_class_A;
711   PFS_socket_class *socket_class_A;
712   PSI_mutex *mutex_A1;
713   PSI_rwlock *rwlock_A1;
714   PSI_cond *cond_A1;
715   PFS_file *file_A1;
716   PSI_socket *socket_A1;
717   PSI_thread *thread_1;
718 
719   /* Preparation */
720 
721   thread_1= psi->new_thread(thread_key_1, NULL, 0);
722   ok(thread_1 != NULL, "T-1");
723   psi->set_thread_id(thread_1, 1);
724 
725   mutex_class_A= find_mutex_class(mutex_key_A);
726   ok(mutex_class_A != NULL, "mutex class A");
727 
728   rwlock_class_A= find_rwlock_class(rwlock_key_A);
729   ok(rwlock_class_A != NULL, "rwlock class A");
730 
731   cond_class_A= find_cond_class(cond_key_A);
732   ok(cond_class_A != NULL, "cond class A");
733 
734   file_class_A= find_file_class(file_key_A);
735   ok(file_class_A != NULL, "file class A");
736 
737   socket_class_A= find_socket_class(socket_key_A);
738   ok(socket_class_A != NULL, "socket class A");
739 
740   /*
741     Pretend thread T-1 is running, and disabled, with thread_instrumentation.
742     Disabled instruments are still created so they can be enabled later.
743   */
744 
745   /* ------------------------------------------------------------------------ */
746 
747   psi->set_thread(thread_1);
748   setup_thread(thread_1, false);
749 
750   /* disabled M-A + disabled T-1: instrumentation */
751 
752   mutex_class_A->m_enabled= false;
753   mutex_A1= psi->init_mutex(mutex_key_A, NULL);
754   ok(mutex_A1 != NULL, "mutex_A1 disabled, instrumented");
755 
756   /* enabled M-A + disabled T-1: instrumentation (for later) */
757 
758   mutex_class_A->m_enabled= true;
759   mutex_A1= psi->init_mutex(mutex_key_A, NULL);
760   ok(mutex_A1 != NULL, "mutex_A1 enabled, instrumented");
761 
762   /* broken key + disabled T-1: no instrumentation */
763 
764   mutex_class_A->m_enabled= true;
765   mutex_A1= psi->init_mutex(0, NULL);
766   ok(mutex_A1 == NULL, "mutex key 0 not instrumented");
767   mutex_A1= psi->init_mutex(99, NULL);
768   ok(mutex_A1 == NULL, "broken mutex key not instrumented");
769 
770   /* disabled RW-A + disabled T-1: no instrumentation */
771 
772   rwlock_class_A->m_enabled= false;
773   rwlock_A1= psi->init_rwlock(rwlock_key_A, NULL);
774   ok(rwlock_A1 != NULL, "rwlock_A1 disabled, instrumented");
775 
776   /* enabled RW-A + disabled T-1: instrumentation (for later) */
777 
778   rwlock_class_A->m_enabled= true;
779   rwlock_A1= psi->init_rwlock(rwlock_key_A, NULL);
780   ok(rwlock_A1 != NULL, "rwlock_A1 enabled, instrumented");
781 
782   /* broken key + disabled T-1: no instrumentation */
783 
784   rwlock_class_A->m_enabled= true;
785   rwlock_A1= psi->init_rwlock(0, NULL);
786   ok(rwlock_A1 == NULL, "rwlock key 0 not instrumented");
787   rwlock_A1= psi->init_rwlock(99, NULL);
788   ok(rwlock_A1 == NULL, "broken rwlock key not instrumented");
789 
790   /* disabled C-A + disabled T-1: no instrumentation */
791 
792   cond_class_A->m_enabled= false;
793   cond_A1= psi->init_cond(cond_key_A, NULL);
794   ok(cond_A1 != NULL, "cond_A1 disabled, instrumented");
795 
796   /* enabled C-A + disabled T-1: instrumentation (for later) */
797 
798   cond_class_A->m_enabled= true;
799   cond_A1= psi->init_cond(cond_key_A, NULL);
800   ok(cond_A1 != NULL, "cond_A1 enabled, instrumented");
801 
802   /* broken key + disabled T-1: no instrumentation */
803 
804   cond_class_A->m_enabled= true;
805   cond_A1= psi->init_cond(0, NULL);
806   ok(cond_A1 == NULL, "cond key 0 not instrumented");
807   cond_A1= psi->init_cond(99, NULL);
808   ok(cond_A1 == NULL, "broken cond key not instrumented");
809 
810   /* disabled F-A + disabled T-1: no instrumentation */
811 
812   file_class_A->m_enabled= false;
813   psi->create_file(file_key_A, "foo", (File) 12);
814   file_A1= lookup_file_by_name("foo");
815   ok(file_A1 == NULL, "not instrumented");
816 
817   /* enabled F-A + disabled T-1: no instrumentation */
818 
819   file_class_A->m_enabled= true;
820   psi->create_file(file_key_A, "foo", (File) 12);
821   file_A1= lookup_file_by_name("foo");
822   ok(file_A1 == NULL, "not instrumented");
823 
824   /* broken key + disabled T-1: no instrumentation */
825 
826   file_class_A->m_enabled= true;
827   psi->create_file(0, "foo", (File) 12);
828   file_A1= lookup_file_by_name("foo");
829   ok(file_A1 == NULL, "file_A1 not instrumented");
830   psi->create_file(99, "foo", (File) 12);
831   file_A1= lookup_file_by_name("foo");
832   ok(file_A1 == NULL, "file_A1 not instrumented");
833 
834   /* disabled S-A + disabled T-1: no instrumentation */
835 
836   socket_class_A->m_enabled= false;
837   socket_A1= psi->init_socket(socket_key_A, NULL, NULL, 0);
838   ok(socket_A1 != NULL, "socket_A1 disabled, instrumented");
839 
840   /* enabled S-A + disabled T-1: instrumentation (for later) */
841 
842   socket_class_A->m_enabled= true;
843   socket_A1= psi->init_socket(socket_key_A, NULL, NULL, 0);
844   ok(socket_A1 != NULL, "socket_A1 enabled, instrumented");
845 
846   /* broken key + disabled T-1: no instrumentation */
847 
848   socket_class_A->m_enabled= true;
849   socket_A1= psi->init_socket(0, NULL, NULL, 0);
850   ok(socket_A1 == NULL, "socket key 0 not instrumented");
851   socket_A1= psi->init_socket(99, NULL, NULL, 0);
852   ok(socket_A1 == NULL, "broken socket key not instrumented");
853 
854   /* Pretend thread T-1 is enabled */
855   /* ----------------------------- */
856 
857   setup_thread(thread_1, true);
858 
859   /* disabled M-A + enabled T-1: no instrumentation */
860 
861   mutex_class_A->m_enabled= false;
862   mutex_A1= psi->init_mutex(mutex_key_A, NULL);
863   ok(mutex_A1 != NULL, "mutex_A1 disabled, instrumented");
864 
865   /* enabled M-A + enabled T-1: instrumentation */
866 
867   mutex_class_A->m_enabled= true;
868   mutex_A1= psi->init_mutex(mutex_key_A, NULL);
869   ok(mutex_A1 != NULL, "mutex_A1 enabled, instrumented");
870   psi->destroy_mutex(mutex_A1);
871 
872   /* broken key + enabled T-1: no instrumentation */
873 
874   mutex_class_A->m_enabled= true;
875   mutex_A1= psi->init_mutex(0, NULL);
876   ok(mutex_A1 == NULL, "mutex_A1 not instrumented");
877   mutex_A1= psi->init_mutex(99, NULL);
878   ok(mutex_A1 == NULL, "mutex_A1 not instrumented");
879 
880   /* disabled RW-A + enabled T-1: no instrumentation */
881 
882   rwlock_class_A->m_enabled= false;
883   rwlock_A1= psi->init_rwlock(rwlock_key_A, NULL);
884   ok(rwlock_A1 != NULL, "rwlock_A1 disabled, instrumented");
885 
886   /* enabled RW-A + enabled T-1: instrumentation */
887 
888   rwlock_class_A->m_enabled= true;
889   rwlock_A1= psi->init_rwlock(rwlock_key_A, NULL);
890   ok(rwlock_A1 != NULL, "rwlock_A1 enabled, instrumented");
891   psi->destroy_rwlock(rwlock_A1);
892 
893   /* broken key + enabled T-1: no instrumentation */
894 
895   rwlock_class_A->m_enabled= true;
896   rwlock_A1= psi->init_rwlock(0, NULL);
897   ok(rwlock_A1 == NULL, "rwlock_A1 not instrumented");
898   rwlock_A1= psi->init_rwlock(99, NULL);
899   ok(rwlock_A1 == NULL, "rwlock_A1 not instrumented");
900 
901   /* disabled C-A + enabled T-1: no instrumentation */
902 
903   cond_class_A->m_enabled= false;
904   cond_A1= psi->init_cond(cond_key_A, NULL);
905   ok(cond_A1 != NULL, "cond_A1 disabled, instrumented");
906 
907   /* enabled C-A + enabled T-1: instrumentation */
908 
909   cond_class_A->m_enabled= true;
910   cond_A1= psi->init_cond(cond_key_A, NULL);
911   ok(cond_A1 != NULL, "cond_A1 enabled, instrumented");
912   psi->destroy_cond(cond_A1);
913 
914   /* broken key + enabled T-1: no instrumentation */
915 
916   cond_class_A->m_enabled= true;
917   cond_A1= psi->init_cond(0, NULL);
918   ok(cond_A1 == NULL, "cond_A1 not instrumented");
919   cond_A1= psi->init_cond(99, NULL);
920   ok(cond_A1 == NULL, "cond_A1 not instrumented");
921 
922   /* disabled F-A + enabled T-1: no instrumentation */
923 
924   file_class_A->m_enabled= false;
925   psi->create_file(file_key_A, "foo", (File) 12);
926   file_A1= lookup_file_by_name("foo");
927   ok(file_A1 == NULL, "file_A1 not instrumented");
928 
929   /* enabled F-A + open failed + enabled T-1: no instrumentation */
930 
931   file_class_A->m_enabled= true;
932   psi->create_file(file_key_A, "foo", (File) -1);
933   file_A1= lookup_file_by_name("foo");
934   ok(file_A1 == NULL, "file_A1 not instrumented");
935 
936   /* enabled F-A + out-of-descriptors + enabled T-1: no instrumentation */
937 
938   file_class_A->m_enabled= true;
939   psi->create_file(file_key_A, "foo", (File) 65000);
940   file_A1= lookup_file_by_name("foo");
941   ok(file_A1 == NULL, "file_A1 not instrumented");
942   ok(file_handle_lost == 1, "lost a file handle");
943   file_handle_lost= 0;
944 
945   /* enabled F-A + enabled T-1: instrumentation */
946 
947   file_class_A->m_enabled= true;
948   psi->create_file(file_key_A, "foo-instrumented", (File) 12);
949   file_A1= lookup_file_by_name("foo-instrumented");
950   ok(file_A1 != NULL, "file_A1 instrumented");
951 
952   /* broken key + enabled T-1: no instrumentation */
953 
954   file_class_A->m_enabled= true;
955   psi->create_file(0, "foo", (File) 12);
956   file_A1= lookup_file_by_name("foo");
957   ok(file_A1 == NULL, "file key 0 not instrumented");
958   psi->create_file(99, "foo", (File) 12);
959   file_A1= lookup_file_by_name("foo");
960   ok(file_A1 == NULL, "broken file key not instrumented");
961 
962   /* disabled S-A + enabled T-1: no instrumentation */
963 
964   socket_class_A->m_enabled= false;
965   ok(socket_A1 == NULL, "socket_A1 not instrumented");
966 
967   /* enabled S-A + enabled T-1: instrumentation */
968 
969   socket_class_A->m_enabled= true;
970   socket_A1= psi->init_socket(socket_key_A, NULL, NULL, 0);
971   ok(socket_A1 != NULL, "socket_A1 instrumented");
972   psi->destroy_socket(socket_A1);
973 
974   /* broken key + enabled T-1: no instrumentation */
975 
976   socket_class_A->m_enabled= true;
977   socket_A1= psi->init_socket(0, NULL, NULL, 0);
978   ok(socket_A1 == NULL, "socket_A1 not instrumented");
979   socket_A1= psi->init_socket(99, NULL, NULL, 0);
980   ok(socket_A1 == NULL, "socket_A1 not instrumented");
981 
982   /* Pretend the running thread is not instrumented */
983   /* ---------------------------------------------- */
984 
985   psi->delete_current_thread();
986 
987   /* disabled M-A + unknown thread: no instrumentation */
988 
989   mutex_class_A->m_enabled= false;
990   mutex_A1= psi->init_mutex(mutex_key_A, NULL);
991   ok(mutex_A1 != NULL, "mutex_A1 disabled, instrumented");
992 
993   /* enabled M-A + unknown thread: instrumentation (for later) */
994 
995   mutex_class_A->m_enabled= true;
996   mutex_A1= psi->init_mutex(mutex_key_A, NULL);
997   ok(mutex_A1 != NULL, "mutex_A1 enabled, instrumented");
998 
999   /* broken key + unknown thread: no instrumentation */
1000 
1001   mutex_class_A->m_enabled= true;
1002   mutex_A1= psi->init_mutex(0, NULL);
1003   ok(mutex_A1 == NULL, "mutex key 0 not instrumented");
1004   mutex_A1= psi->init_mutex(99, NULL);
1005   ok(mutex_A1 == NULL, "broken mutex key not instrumented");
1006 
1007   /* disabled RW-A + unknown thread: no instrumentation */
1008 
1009   rwlock_class_A->m_enabled= false;
1010   rwlock_A1= psi->init_rwlock(rwlock_key_A, NULL);
1011   ok(rwlock_A1 != NULL, "rwlock_A1 disabled, instrumented");
1012 
1013   /* enabled RW-A + unknown thread: instrumentation (for later) */
1014 
1015   rwlock_class_A->m_enabled= true;
1016   rwlock_A1= psi->init_rwlock(rwlock_key_A, NULL);
1017   ok(rwlock_A1 != NULL, "rwlock_A1 enabled, instrumented");
1018 
1019   /* broken key + unknown thread: no instrumentation */
1020 
1021   rwlock_class_A->m_enabled= true;
1022   rwlock_A1= psi->init_rwlock(0, NULL);
1023   ok(rwlock_A1 == NULL, "rwlock key 0 not instrumented");
1024   rwlock_A1= psi->init_rwlock(99, NULL);
1025   ok(rwlock_A1 == NULL, "broken rwlock key not instrumented");
1026 
1027   /* disabled C-A + unknown thread: no instrumentation */
1028 
1029   cond_class_A->m_enabled= false;
1030   cond_A1= psi->init_cond(cond_key_A, NULL);
1031   ok(cond_A1 != NULL, "cond_A1 disabled, instrumented");
1032 
1033   /* enabled C-A + unknown thread: instrumentation (for later) */
1034 
1035   cond_class_A->m_enabled= true;
1036   cond_A1= psi->init_cond(cond_key_A, NULL);
1037   ok(cond_A1 != NULL, "cond_A1 enabled, instrumented");
1038 
1039   /* broken key + unknown thread: no instrumentation */
1040 
1041   cond_class_A->m_enabled= true;
1042   cond_A1= psi->init_cond(0, NULL);
1043   ok(cond_A1 == NULL, "cond key 0 not instrumented");
1044   cond_A1= psi->init_cond(99, NULL);
1045   ok(cond_A1 == NULL, "broken cond key not instrumented");
1046 
1047   /* disabled F-A + unknown thread: no instrumentation */
1048 
1049   file_class_A->m_enabled= false;
1050   psi->create_file(file_key_A, "foo", (File) 12);
1051   file_A1= lookup_file_by_name("foo");
1052   ok(file_A1 == NULL, "file_A1 not instrumented");
1053 
1054   /* enabled F-A + unknown thread: no instrumentation */
1055 
1056   file_class_A->m_enabled= true;
1057   psi->create_file(file_key_A, "foo", (File) 12);
1058   file_A1= lookup_file_by_name("foo");
1059   ok(file_A1 == NULL, "file_A1 not instrumented");
1060 
1061   /* broken key + unknown thread: no instrumentation */
1062 
1063   file_class_A->m_enabled= true;
1064   psi->create_file(0, "foo", (File) 12);
1065   file_A1= lookup_file_by_name("foo");
1066   ok(file_A1 == NULL, "not instrumented");
1067   psi->create_file(99, "foo", (File) 12);
1068   file_A1= lookup_file_by_name("foo");
1069   ok(file_A1 == NULL, "not instrumented");
1070 
1071   /* disabled S-A + unknown thread: no instrumentation */
1072 
1073   socket_class_A->m_enabled= false;
1074   socket_A1= psi->init_socket(socket_key_A, NULL, NULL, 0);
1075   ok(socket_A1 != NULL, "socket_A1 disabled, instrumented");
1076 
1077   /* enabled S-A + unknown thread: instrumentation (for later) */
1078 
1079   socket_class_A->m_enabled= true;
1080   socket_A1= psi->init_socket(socket_key_A, NULL, NULL, 0);
1081   ok(socket_A1 != NULL, "socket_A1 enabled, instrumented");
1082 
1083   /* broken key + unknown thread: no instrumentation */
1084 
1085   socket_class_A->m_enabled= true;
1086   socket_A1= psi->init_socket(0, NULL, NULL, 0);
1087   ok(socket_A1 == NULL, "socket key 0 not instrumented");
1088   socket_A1= psi->init_socket(99, NULL, NULL, 0);
1089   ok(socket_A1 == NULL, "broken socket key not instrumented");
1090 
1091   shutdown_performance_schema();
1092 }
1093 
test_locker_disabled()1094 void test_locker_disabled()
1095 {
1096   PSI *psi;
1097 
1098   diag("test_locker_disabled");
1099 
1100   psi= load_perfschema();
1101 
1102   PSI_mutex_key mutex_key_A;
1103   PSI_mutex_info all_mutex[]=
1104   {
1105     { & mutex_key_A, "M-A", 0}
1106   };
1107 
1108   PSI_rwlock_key rwlock_key_A;
1109   PSI_rwlock_info all_rwlock[]=
1110   {
1111     { & rwlock_key_A, "RW-A", 0}
1112   };
1113 
1114   PSI_cond_key cond_key_A;
1115   PSI_cond_info all_cond[]=
1116   {
1117     { & cond_key_A, "C-A", 0}
1118   };
1119 
1120   PSI_file_key file_key_A;
1121   PSI_file_info all_file[]=
1122   {
1123     { & file_key_A, "F-A", 0}
1124   };
1125 
1126   PSI_socket_key socket_key_A;
1127   PSI_socket_info all_socket[]=
1128   {
1129     { & socket_key_A, "S-A", 0}
1130   };
1131 
1132   PSI_thread_key thread_key_1;
1133   PSI_thread_info all_thread[]=
1134   {
1135     { & thread_key_1, "T-1", 0}
1136   };
1137 
1138   psi->register_mutex("test", all_mutex, 1);
1139   psi->register_rwlock("test", all_rwlock, 1);
1140   psi->register_cond("test", all_cond, 1);
1141   psi->register_file("test", all_file, 1);
1142   psi->register_socket("test", all_socket, 1);
1143   psi->register_thread("test", all_thread, 1);
1144 
1145   PFS_mutex_class *mutex_class_A;
1146   PFS_rwlock_class *rwlock_class_A;
1147   PFS_cond_class *cond_class_A;
1148   PFS_file_class *file_class_A;
1149   PFS_socket_class *socket_class_A;
1150   PSI_mutex *mutex_A1;
1151   PSI_rwlock *rwlock_A1;
1152   PSI_cond *cond_A1;
1153   PSI_file *file_A1;
1154   PSI_socket *socket_A1;
1155   PSI_thread *thread_1;
1156 
1157   /* Preparation */
1158 
1159   thread_1= psi->new_thread(thread_key_1, NULL, 0);
1160   ok(thread_1 != NULL, "T-1");
1161   psi->set_thread_id(thread_1, 1);
1162 
1163   mutex_class_A= find_mutex_class(mutex_key_A);
1164   ok(mutex_class_A != NULL, "mutex info A");
1165 
1166   rwlock_class_A= find_rwlock_class(rwlock_key_A);
1167   ok(rwlock_class_A != NULL, "rwlock info A");
1168 
1169   cond_class_A= find_cond_class(cond_key_A);
1170   ok(cond_class_A != NULL, "cond info A");
1171 
1172   file_class_A= find_file_class(file_key_A);
1173   ok(file_class_A != NULL, "file info A");
1174 
1175   socket_class_A= find_socket_class(socket_key_A);
1176   ok(socket_class_A != NULL, "socket info A");
1177 
1178   /* Pretend thread T-1 is running, and enabled */
1179   /* ------------------------------------------ */
1180 
1181   psi->set_thread(thread_1);
1182   setup_thread(thread_1, true);
1183 
1184   /* Enable all instruments, instantiate objects */
1185 
1186   mutex_class_A->m_enabled= true;
1187   mutex_A1= psi->init_mutex(mutex_key_A, NULL);
1188   ok(mutex_A1 != NULL, "instrumented");
1189 
1190   rwlock_class_A->m_enabled= true;
1191   rwlock_A1= psi->init_rwlock(rwlock_key_A, NULL);
1192   ok(rwlock_A1 != NULL, "instrumented");
1193 
1194   cond_class_A->m_enabled= true;
1195   cond_A1= psi->init_cond(cond_key_A, NULL);
1196   ok(cond_A1 != NULL, "instrumented");
1197 
1198   file_class_A->m_enabled= true;
1199   psi->create_file(file_key_A, "foo", (File) 12);
1200   file_A1= (PSI_file*) lookup_file_by_name("foo");
1201   ok(file_A1 != NULL, "instrumented");
1202 
1203   socket_class_A->m_enabled= true;
1204   socket_A1= psi->init_socket(socket_key_A, NULL, NULL, 0);
1205   ok(socket_A1 != NULL, "instrumented");
1206 
1207   /* Socket lockers require a thread owner */
1208   psi->set_socket_thread_owner(socket_A1);
1209 
1210   PSI_mutex_locker *mutex_locker;
1211   PSI_mutex_locker_state mutex_state;
1212   PSI_rwlock_locker *rwlock_locker;
1213   PSI_rwlock_locker_state rwlock_state;
1214   PSI_cond_locker *cond_locker;
1215   PSI_cond_locker_state cond_state;
1216   PSI_file_locker *file_locker;
1217   PSI_file_locker_state file_state;
1218   PSI_socket_locker *socket_locker;
1219   PSI_socket_locker_state socket_state;
1220 
1221   /* Pretend thread T-1 is disabled */
1222   /* ------------------------------ */
1223 
1224   setup_thread(thread_1, false);
1225   flag_events_waits_current= true;
1226   mutex_class_A->m_enabled= true;
1227   rwlock_class_A->m_enabled= true;
1228   cond_class_A->m_enabled= true;
1229   file_class_A->m_enabled= true;
1230   socket_class_A->m_enabled= true;
1231 
1232   mutex_locker= psi->start_mutex_wait(&mutex_state, mutex_A1, PSI_MUTEX_LOCK, "foo.cc", 12);
1233   ok(mutex_locker == NULL, "no locker (T-1 disabled)");
1234   rwlock_locker= psi->start_rwlock_rdwait(&rwlock_state, rwlock_A1, PSI_RWLOCK_READLOCK, "foo.cc", 12);
1235   ok(rwlock_locker == NULL, "no locker (T-1 disabled)");
1236   cond_locker= psi->start_cond_wait(&cond_state, cond_A1, mutex_A1, PSI_COND_WAIT, "foo.cc", 12);
1237   ok(cond_locker == NULL, "no locker (T-1 disabled)");
1238   file_locker= psi->get_thread_file_name_locker(&file_state, file_key_A, PSI_FILE_OPEN, "xxx", NULL);
1239   ok(file_locker == NULL, "no locker (T-1 disabled)");
1240   file_locker= psi->get_thread_file_stream_locker(&file_state, file_A1, PSI_FILE_READ);
1241   ok(file_locker == NULL, "no locker (T-1 disabled)");
1242   file_locker= psi->get_thread_file_descriptor_locker(&file_state, (File) 12, PSI_FILE_READ);
1243   ok(file_locker == NULL, "no locker (T-1 disabled)");
1244   socket_locker= psi->start_socket_wait(&socket_state, socket_A1, PSI_SOCKET_SEND, 12, "foo.cc", 12);
1245   ok(socket_locker == NULL, "no locker (T-1 disabled)");
1246 
1247   /* Pretend the global consumer is disabled */
1248   /* --------------------------------------- */
1249 
1250   setup_thread(thread_1, true);
1251   flag_global_instrumentation= false;
1252   mutex_class_A->m_enabled= true;
1253   rwlock_class_A->m_enabled= true;
1254   cond_class_A->m_enabled= true;
1255   file_class_A->m_enabled= true;
1256   socket_class_A->m_enabled= true;
1257   update_instruments_derived_flags();
1258 
1259   mutex_locker= psi->start_mutex_wait(&mutex_state, mutex_A1, PSI_MUTEX_LOCK, "foo.cc", 12);
1260   ok(mutex_locker == NULL, "no locker (global disabled)");
1261   rwlock_locker= psi->start_rwlock_rdwait(&rwlock_state, rwlock_A1, PSI_RWLOCK_READLOCK, "foo.cc", 12);
1262   ok(rwlock_locker == NULL, "no locker (global disabled)");
1263   cond_locker= psi->start_cond_wait(&cond_state, cond_A1, mutex_A1, PSI_COND_WAIT, "foo.cc", 12);
1264   ok(cond_locker == NULL, "no locker (global disabled)");
1265   file_locker= psi->get_thread_file_name_locker(&file_state, file_key_A, PSI_FILE_OPEN, "xxx", NULL);
1266   ok(file_locker == NULL, "no locker (global disabled)");
1267   file_locker= psi->get_thread_file_stream_locker(&file_state, file_A1, PSI_FILE_READ);
1268   ok(file_locker == NULL, "no locker (global disabled)");
1269   file_locker= psi->get_thread_file_descriptor_locker(&file_state, (File) 12, PSI_FILE_READ);
1270   ok(file_locker == NULL, "no locker (global disabled)");
1271   socket_locker= psi->start_socket_wait(&socket_state, socket_A1, PSI_SOCKET_SEND, 12, "foo.cc", 12);
1272   ok(socket_locker == NULL, "no locker (global disabled)");
1273 
1274   /* Pretend the mode is global, counted only */
1275   /* ---------------------------------------- */
1276 
1277   setup_thread(thread_1, true);
1278   flag_global_instrumentation= true;
1279   flag_thread_instrumentation= false;
1280   mutex_class_A->m_enabled= true;
1281   mutex_class_A->m_timed= false;
1282   rwlock_class_A->m_enabled= true;
1283   rwlock_class_A->m_timed= false;
1284   cond_class_A->m_enabled= true;
1285   cond_class_A->m_timed= false;
1286   file_class_A->m_enabled= true;
1287   file_class_A->m_timed= false;
1288   socket_class_A->m_enabled= true;
1289   socket_class_A->m_timed= false;
1290   update_instruments_derived_flags();
1291 
1292   mutex_locker= psi->start_mutex_wait(&mutex_state, mutex_A1, PSI_MUTEX_LOCK, "foo.cc", 12);
1293   ok(mutex_locker == NULL, "no locker (global counted)");
1294   rwlock_locker= psi->start_rwlock_rdwait(&rwlock_state, rwlock_A1, PSI_RWLOCK_READLOCK, "foo.cc", 12);
1295   ok(rwlock_locker == NULL, "no locker (global counted)");
1296   cond_locker= psi->start_cond_wait(&cond_state, cond_A1, mutex_A1, PSI_COND_WAIT, "foo.cc", 12);
1297   ok(cond_locker == NULL, "no locker (global counted)");
1298   file_locker= psi->get_thread_file_name_locker(&file_state, file_key_A, PSI_FILE_OPEN, "xxx", NULL);
1299   ok(file_locker != NULL, "locker (global counted)");
1300   psi->start_file_wait(file_locker, 10, __FILE__, __LINE__);
1301   psi->end_file_wait(file_locker, 10);
1302   file_locker= psi->get_thread_file_stream_locker(&file_state, file_A1, PSI_FILE_READ);
1303   ok(file_locker != NULL, "locker (global counted)");
1304   psi->start_file_wait(file_locker, 10, __FILE__, __LINE__);
1305   psi->end_file_wait(file_locker, 10);
1306   file_locker= psi->get_thread_file_descriptor_locker(&file_state, (File) 12, PSI_FILE_READ);
1307   ok(file_locker != NULL, "locker (global counted)");
1308   psi->start_file_wait(file_locker, 10, __FILE__, __LINE__);
1309   psi->end_file_wait(file_locker, 10);
1310   /* The null locker shortcut applies only to socket ops with no byte count */
1311   socket_locker= psi->start_socket_wait(&socket_state, socket_A1, PSI_SOCKET_BIND, 0, "foo.cc", 12);
1312   ok(socket_locker == NULL, "no locker (global counted)");
1313 
1314   /* TODO */
1315 
1316   /* Pretend the instrument is disabled */
1317   /* ---------------------------------- */
1318 
1319   setup_thread(thread_1, true);
1320   flag_global_instrumentation= true;
1321   flag_events_waits_current= true;
1322   mutex_class_A->m_enabled= false;
1323   rwlock_class_A->m_enabled= false;
1324   cond_class_A->m_enabled= false;
1325   file_class_A->m_enabled= false;
1326   socket_class_A->m_enabled= false;
1327   update_instruments_derived_flags();
1328 
1329   mutex_locker= psi->start_mutex_wait(&mutex_state, mutex_A1, PSI_MUTEX_LOCK, "foo.cc", 12);
1330   ok(mutex_locker == NULL, "no locker");
1331   rwlock_locker= psi->start_rwlock_rdwait(&rwlock_state, rwlock_A1, PSI_RWLOCK_READLOCK, "foo.cc", 12);
1332   ok(rwlock_locker == NULL, "no locker");
1333   cond_locker= psi->start_cond_wait(&cond_state, cond_A1, mutex_A1, PSI_COND_WAIT, "foo.cc", 12);
1334   ok(cond_locker == NULL, "no locker");
1335   file_locker= psi->get_thread_file_name_locker(&file_state, file_key_A, PSI_FILE_OPEN, "xxx", NULL);
1336   ok(file_locker == NULL, "no locker");
1337   file_locker= psi->get_thread_file_stream_locker(&file_state, file_A1, PSI_FILE_READ);
1338   ok(file_locker == NULL, "no locker");
1339   file_locker= psi->get_thread_file_descriptor_locker(&file_state, (File) 12, PSI_FILE_READ);
1340   ok(file_locker == NULL, "no locker");
1341   socket_locker= psi->start_socket_wait(&socket_state, socket_A1, PSI_SOCKET_SEND, 12, "foo.cc", 12);
1342   ok(socket_locker == NULL, "no locker");
1343 
1344   /* Pretend everything is enabled and timed */
1345   /* --------------------------------------- */
1346 
1347   setup_thread(thread_1, true);
1348   flag_global_instrumentation= true;
1349   flag_thread_instrumentation= true;
1350   flag_events_waits_current= true;
1351   mutex_class_A->m_enabled= true;
1352   mutex_class_A->m_timed= true;
1353   rwlock_class_A->m_enabled= true;
1354   rwlock_class_A->m_timed= true;
1355   cond_class_A->m_enabled= true;
1356   cond_class_A->m_timed= true;
1357   file_class_A->m_enabled= true;
1358   file_class_A->m_timed= true;
1359   socket_class_A->m_enabled= true;
1360   socket_class_A->m_timed= true;
1361   update_instruments_derived_flags();
1362 
1363   mutex_locker= psi->start_mutex_wait(&mutex_state, mutex_A1, PSI_MUTEX_LOCK, __FILE__, __LINE__);
1364   ok(mutex_locker != NULL, "locker");
1365   psi->end_mutex_wait(mutex_locker, 0);
1366   rwlock_locker= psi->start_rwlock_rdwait(&rwlock_state, rwlock_A1, PSI_RWLOCK_READLOCK, __FILE__, __LINE__);
1367   ok(rwlock_locker != NULL, "locker");
1368   psi->end_rwlock_rdwait(rwlock_locker, 0);
1369   cond_locker= psi->start_cond_wait(&cond_state, cond_A1, mutex_A1, PSI_COND_WAIT, __FILE__, __LINE__);
1370   ok(cond_locker != NULL, "locker");
1371   psi->end_cond_wait(cond_locker, 0);
1372   file_locker= psi->get_thread_file_name_locker(&file_state, file_key_A, PSI_FILE_STREAM_OPEN, "xxx", NULL);
1373   ok(file_locker != NULL, "locker");
1374   psi->start_file_open_wait(file_locker, __FILE__, __LINE__);
1375   psi->end_file_open_wait(file_locker, NULL);
1376   file_locker= psi->get_thread_file_stream_locker(&file_state, file_A1, PSI_FILE_READ);
1377   ok(file_locker != NULL, "locker");
1378   psi->start_file_wait(file_locker, 10, __FILE__, __LINE__);
1379   psi->end_file_wait(file_locker, 10);
1380   file_locker= psi->get_thread_file_descriptor_locker(&file_state, (File) 12, PSI_FILE_READ);
1381   ok(file_locker != NULL, "locker");
1382   psi->start_file_wait(file_locker, 10, __FILE__, __LINE__);
1383   psi->end_file_wait(file_locker, 10);
1384   socket_locker= psi->start_socket_wait(&socket_state, socket_A1, PSI_SOCKET_SEND, 12, "foo.cc", 12);
1385   ok(socket_locker != NULL, "locker");
1386   psi->end_socket_wait(socket_locker, 10);
1387 
1388   /* Pretend the socket does not have a thread owner */
1389   /* ---------------------------------------------- */
1390 
1391   socket_class_A->m_enabled= true;
1392   socket_A1= psi->init_socket(socket_key_A, NULL, NULL, 0);
1393   ok(socket_A1 != NULL, "instrumented");
1394   /* Socket thread owner has not been set */
1395   socket_locker= psi->start_socket_wait(&socket_state, socket_A1, PSI_SOCKET_SEND, 12, "foo.cc", 12);
1396   ok(socket_locker != NULL, "locker (owner not used)");
1397   psi->end_socket_wait(socket_locker, 10);
1398 
1399   /* Pretend the running thread is not instrumented */
1400   /* ---------------------------------------------- */
1401 
1402   psi->delete_current_thread();
1403   flag_events_waits_current= true;
1404   mutex_class_A->m_enabled= true;
1405   rwlock_class_A->m_enabled= true;
1406   cond_class_A->m_enabled= true;
1407   file_class_A->m_enabled= true;
1408   socket_class_A->m_enabled= true;
1409   update_instruments_derived_flags();
1410 
1411   mutex_locker= psi->start_mutex_wait(&mutex_state, mutex_A1, PSI_MUTEX_LOCK, "foo.cc", 12);
1412   ok(mutex_locker == NULL, "no locker");
1413   rwlock_locker= psi->start_rwlock_rdwait(&rwlock_state, rwlock_A1, PSI_RWLOCK_READLOCK, "foo.cc", 12);
1414   ok(rwlock_locker == NULL, "no locker");
1415   cond_locker= psi->start_cond_wait(&cond_state, cond_A1, mutex_A1, PSI_COND_WAIT, "foo.cc", 12);
1416   ok(cond_locker == NULL, "no locker");
1417   file_locker= psi->get_thread_file_name_locker(&file_state, file_key_A, PSI_FILE_OPEN, "xxx", NULL);
1418   ok(file_locker == NULL, "no locker");
1419   file_locker= psi->get_thread_file_stream_locker(&file_state, file_A1, PSI_FILE_READ);
1420   ok(file_locker == NULL, "no locker");
1421   file_locker= psi->get_thread_file_descriptor_locker(&file_state, (File) 12, PSI_FILE_READ);
1422   ok(file_locker == NULL, "no locker");
1423   socket_locker= psi->start_socket_wait(&socket_state, socket_A1, PSI_SOCKET_SEND, 12, "foo.cc", 12);
1424   ok(socket_locker == NULL, "no locker");
1425 
1426   shutdown_performance_schema();
1427 }
1428 
test_file_instrumentation_leak()1429 void test_file_instrumentation_leak()
1430 {
1431   PSI *psi;
1432 
1433   diag("test_file_instrumentation_leak");
1434 
1435   psi= load_perfschema();
1436 
1437   PSI_file_key file_key_A;
1438   PSI_file_key file_key_B;
1439   PSI_file_info all_file[]=
1440   {
1441     { & file_key_A, "F-A", 0},
1442     { & file_key_B, "F-B", 0}
1443   };
1444 
1445   PSI_thread_key thread_key_1;
1446   PSI_thread_info all_thread[]=
1447   {
1448     { & thread_key_1, "T-1", 0}
1449   };
1450 
1451   psi->register_file("test", all_file, 2);
1452   psi->register_thread("test", all_thread, 1);
1453 
1454   PFS_file_class *file_class_A;
1455   PFS_file_class *file_class_B;
1456   PSI_file_locker_state file_state;
1457   PSI_thread *thread_1;
1458 
1459   /* Preparation */
1460 
1461   thread_1= psi->new_thread(thread_key_1, NULL, 0);
1462   ok(thread_1 != NULL, "T-1");
1463   psi->set_thread_id(thread_1, 1);
1464 
1465   file_class_A= find_file_class(file_key_A);
1466   ok(file_class_A != NULL, "file info A");
1467 
1468   file_class_B= find_file_class(file_key_B);
1469   ok(file_class_B != NULL, "file info B");
1470 
1471   psi->set_thread(thread_1);
1472 
1473   /* Pretend everything is enabled */
1474   /* ----------------------------- */
1475 
1476   setup_thread(thread_1, true);
1477   flag_events_waits_current= true;
1478   file_class_A->m_enabled= true;
1479   file_class_B->m_enabled= true;
1480 
1481   PSI_file_locker *file_locker;
1482 
1483   /* Simulate OPEN + READ of 100 bytes + CLOSE on descriptor 12 */
1484 
1485   file_locker= psi->get_thread_file_name_locker(&file_state, file_key_A, PSI_FILE_OPEN, "AAA", NULL);
1486   ok(file_locker != NULL, "locker");
1487   psi->start_file_open_wait(file_locker, __FILE__, __LINE__);
1488   psi->end_file_open_wait_and_bind_to_descriptor(file_locker, 12);
1489 
1490   file_locker= psi->get_thread_file_descriptor_locker(&file_state, (File) 12, PSI_FILE_READ);
1491   ok(file_locker != NULL, "locker");
1492   psi->start_file_wait(file_locker, 100, __FILE__, __LINE__);
1493   psi->end_file_wait(file_locker, 100);
1494 
1495   file_locker= psi->get_thread_file_descriptor_locker(&file_state, (File) 12, PSI_FILE_CLOSE);
1496   ok(file_locker != NULL, "locker");
1497   psi->start_file_wait(file_locker, 0, __FILE__, __LINE__);
1498   psi->end_file_wait(file_locker, 0);
1499 
1500   /* Simulate uninstrumented-OPEN + WRITE on descriptor 24 */
1501 
1502   file_locker= psi->get_thread_file_descriptor_locker(&file_state, (File) 24, PSI_FILE_WRITE);
1503   ok(file_locker == NULL, "no locker, since the open was not instrumented");
1504 
1505   /*
1506     Simulate uninstrumented-OPEN + WRITE on descriptor 12 :
1507     the instrumentation should not leak (don't charge the file io on unknown B to "AAA")
1508   */
1509 
1510   file_locker= psi->get_thread_file_descriptor_locker(&file_state, (File) 12, PSI_FILE_WRITE);
1511   ok(file_locker == NULL, "no locker, no leak");
1512 
1513   shutdown_performance_schema();
1514 }
1515 
test_enabled()1516 void test_enabled()
1517 {
1518 #ifdef LATER
1519   PSI *psi;
1520 
1521   diag("test_enabled");
1522 
1523   psi= load_perfschema();
1524 
1525   PSI_mutex_key mutex_key_A;
1526   PSI_mutex_key mutex_key_B;
1527   PSI_mutex_info all_mutex[]=
1528   {
1529     { & mutex_key_A, "M-A", 0},
1530     { & mutex_key_B, "M-B", 0}
1531   };
1532 
1533   PSI_rwlock_key rwlock_key_A;
1534   PSI_rwlock_key rwlock_key_B;
1535   PSI_rwlock_info all_rwlock[]=
1536   {
1537     { & rwlock_key_A, "RW-A", 0},
1538     { & rwlock_key_B, "RW-B", 0}
1539   };
1540 
1541   PSI_cond_key cond_key_A;
1542   PSI_cond_key cond_key_B;
1543   PSI_cond_info all_cond[]=
1544   {
1545     { & cond_key_A, "C-A", 0},
1546     { & cond_key_B, "C-B", 0}
1547   };
1548 
1549   shutdown_performance_schema();
1550 #endif
1551 }
1552 
test_event_name_index()1553 void test_event_name_index()
1554 {
1555   PSI *psi;
1556   PSI_bootstrap *boot;
1557   PFS_global_param param;
1558 
1559   diag("test_event_name_index");
1560 
1561   memset(& param, 0xFF, sizeof(param));
1562   param.m_enabled= true;
1563 
1564   /* NOTE: Need to add 4 to each index: table io, table lock, idle, metadata lock */
1565 
1566   /* Per mutex info waits should be at [0..9] */
1567   param.m_mutex_class_sizing= 10;
1568   /* Per rwlock info waits should be at [10..29] */
1569   param.m_rwlock_class_sizing= 20;
1570   /* Per cond info waits should be at [30..69] */
1571   param.m_cond_class_sizing= 40;
1572   /* Per file info waits should be at [70..149] */
1573   param.m_file_class_sizing= 80;
1574   /* Per socket info waits should be at [150..309] */
1575   param.m_socket_class_sizing= 160;
1576   /* Per table info waits should be at [310] */
1577   param.m_table_share_sizing= 320;
1578 
1579   param.m_thread_class_sizing= 0;
1580   param.m_user_sizing= 0;
1581   param.m_account_sizing= 0;
1582   param.m_host_sizing= 0;
1583   param.m_stage_class_sizing= 0;
1584   param.m_events_stages_history_sizing= 0;
1585   param.m_events_stages_history_long_sizing= 0;
1586   param.m_statement_class_sizing= 0;
1587   param.m_events_statements_history_sizing= 0;
1588   param.m_events_statements_history_long_sizing= 0;
1589   param.m_events_transactions_history_sizing= 0;
1590   param.m_events_transactions_history_long_sizing= 0;
1591   param.m_digest_sizing= 0;
1592   param.m_session_connect_attrs_sizing= 0;
1593   param.m_program_sizing= 0;
1594   param.m_statement_stack_sizing= 10;
1595   param.m_memory_class_sizing= 12;
1596   param.m_metadata_lock_sizing= 10;
1597   param.m_max_digest_length= 0;
1598   param.m_max_sql_text_length= 1000;
1599 
1600   param.m_mutex_sizing= 0;
1601   param.m_rwlock_sizing= 0;
1602   param.m_cond_sizing= 0;
1603   param.m_thread_sizing= 0;
1604   param.m_table_sizing= 0;
1605   param.m_file_sizing= 0;
1606   param.m_file_handle_sizing= 0;
1607   param.m_socket_sizing= 0;
1608   param.m_events_waits_history_sizing= 0;
1609   param.m_events_waits_history_long_sizing= 0;
1610   param.m_setup_actor_sizing= 0;
1611   param.m_setup_object_sizing= 0;
1612 
1613   param.m_hints.m_table_definition_cache = 100;
1614   param.m_hints.m_table_open_cache       = 100;
1615   param.m_hints.m_max_connections        = 100;
1616   param.m_hints.m_open_files_limit       = 100;
1617   param.m_hints.m_max_prepared_stmt_count= 100;
1618 
1619   pre_initialize_performance_schema();
1620   boot= initialize_performance_schema(& param);
1621   ok(boot != NULL, "bootstrap");
1622   psi= (PSI*) boot->get_interface(PSI_VERSION_1);
1623   ok(psi != NULL, "psi");
1624 
1625   PFS_mutex_class *mutex_class;
1626   PSI_mutex_key dummy_mutex_key_1;
1627   PSI_mutex_key dummy_mutex_key_2;
1628   PSI_mutex_info dummy_mutexes[]=
1629   {
1630     { & dummy_mutex_key_1, "M-1", 0},
1631     { & dummy_mutex_key_2, "M-2", 0}
1632   };
1633 
1634   psi->register_mutex("X", dummy_mutexes, 2);
1635   mutex_class= find_mutex_class(dummy_mutex_key_1);
1636   ok(mutex_class != NULL, "mutex class 1");
1637   ok(mutex_class->m_event_name_index == 4, "index 4");
1638   mutex_class= find_mutex_class(dummy_mutex_key_2);
1639   ok(mutex_class != NULL, "mutex class 2");
1640   ok(mutex_class->m_event_name_index == 5, "index 5");
1641 
1642   PFS_rwlock_class *rwlock_class;
1643   PSI_rwlock_key dummy_rwlock_key_1;
1644   PSI_rwlock_key dummy_rwlock_key_2;
1645   PSI_rwlock_info dummy_rwlocks[]=
1646   {
1647     { & dummy_rwlock_key_1, "RW-1", 0},
1648     { & dummy_rwlock_key_2, "RW-2", 0}
1649   };
1650 
1651   psi->register_rwlock("X", dummy_rwlocks, 2);
1652   rwlock_class= find_rwlock_class(dummy_rwlock_key_1);
1653   ok(rwlock_class != NULL, "rwlock class 1");
1654   ok(rwlock_class->m_event_name_index == 14, "index 14");
1655   rwlock_class= find_rwlock_class(dummy_rwlock_key_2);
1656   ok(rwlock_class != NULL, "rwlock class 2");
1657   ok(rwlock_class->m_event_name_index == 15, "index 15");
1658 
1659   PFS_cond_class *cond_class;
1660   PSI_cond_key dummy_cond_key_1;
1661   PSI_cond_key dummy_cond_key_2;
1662   PSI_cond_info dummy_conds[]=
1663   {
1664     { & dummy_cond_key_1, "C-1", 0},
1665     { & dummy_cond_key_2, "C-2", 0}
1666   };
1667 
1668   psi->register_cond("X", dummy_conds, 2);
1669   cond_class= find_cond_class(dummy_cond_key_1);
1670   ok(cond_class != NULL, "cond class 1");
1671   ok(cond_class->m_event_name_index == 34, "index 34");
1672   cond_class= find_cond_class(dummy_cond_key_2);
1673   ok(cond_class != NULL, "cond class 2");
1674   ok(cond_class->m_event_name_index == 35, "index 35");
1675 
1676   PFS_file_class *file_class;
1677   PSI_file_key dummy_file_key_1;
1678   PSI_file_key dummy_file_key_2;
1679   PSI_file_info dummy_files[]=
1680   {
1681     { & dummy_file_key_1, "F-1", 0},
1682     { & dummy_file_key_2, "F-2", 0}
1683   };
1684 
1685   psi->register_file("X", dummy_files, 2);
1686   file_class= find_file_class(dummy_file_key_1);
1687   ok(file_class != NULL, "file class 1");
1688   ok(file_class->m_event_name_index == 74, "index 74");
1689   file_class= find_file_class(dummy_file_key_2);
1690   ok(file_class != NULL, "file class 2");
1691   ok(file_class->m_event_name_index == 75, "index 75");
1692 
1693   PFS_socket_class *socket_class;
1694   PSI_socket_key dummy_socket_key_1;
1695   PSI_socket_key dummy_socket_key_2;
1696   PSI_socket_info dummy_sockets[]=
1697   {
1698     { & dummy_socket_key_1, "S-1", 0},
1699     { & dummy_socket_key_2, "S-2", 0}
1700   };
1701 
1702   psi->register_socket("X", dummy_sockets, 2);
1703   socket_class= find_socket_class(dummy_socket_key_1);
1704   ok(socket_class != NULL, "socket class 1");
1705   ok(socket_class->m_event_name_index == 154, "index 154");
1706   socket_class= find_socket_class(dummy_socket_key_2);
1707   ok(socket_class != NULL, "socket class 2");
1708   ok(socket_class->m_event_name_index == 155, "index 155");
1709 
1710   ok(global_table_io_class.m_event_name_index == 0, "index 0");
1711   ok(global_table_lock_class.m_event_name_index == 1, "index 1");
1712   ok(wait_class_max= 314, "314 event names"); // 4 global classes
1713 
1714   shutdown_performance_schema();
1715 }
1716 
test_memory_instruments()1717 void test_memory_instruments()
1718 {
1719   PSI *psi;
1720   PSI_thread *owner;
1721 
1722   diag("test_memory_instruments");
1723 
1724   psi= load_perfschema();
1725 
1726   PSI_memory_key memory_key_A;
1727   PSI_memory_info all_memory[]=
1728   {
1729     { & memory_key_A, "M-A", 0}
1730   };
1731 
1732   PSI_thread_key thread_key_1;
1733   PSI_thread_info all_thread[]=
1734   {
1735     { & thread_key_1, "T-1", 0}
1736   };
1737 
1738   psi->register_memory("test", all_memory, 1);
1739   psi->register_thread("test", all_thread, 1);
1740 
1741   PFS_memory_class *memory_class_A;
1742   PSI_thread *thread_1;
1743   PSI_memory_key key;
1744 
1745   /* Preparation */
1746 
1747   thread_1= psi->new_thread(thread_key_1, NULL, 0);
1748   ok(thread_1 != NULL, "T-1");
1749   psi->set_thread_id(thread_1, 1);
1750 
1751   memory_class_A= find_memory_class(memory_key_A);
1752   ok(memory_class_A != NULL, "memory info A");
1753 
1754   /* Pretend thread T-1 is running, and enabled */
1755   /* ------------------------------------------ */
1756 
1757   psi->set_thread(thread_1);
1758   setup_thread(thread_1, true);
1759 
1760   /* Enable all instruments */
1761 
1762   memory_class_A->m_enabled= true;
1763 
1764   /* for coverage, need to print stats collected. */
1765 
1766   key= psi->memory_alloc(memory_key_A, 100, & owner);
1767   ok(key == memory_key_A, "alloc memory info A");
1768   key= psi->memory_realloc(memory_key_A, 100, 200, & owner);
1769   ok(key == memory_key_A, "realloc memory info A");
1770   key= psi->memory_realloc(memory_key_A, 200, 300, & owner);
1771   ok(key == memory_key_A, "realloc up memory info A");
1772   key= psi->memory_realloc(memory_key_A, 300, 50, & owner);
1773   ok(key == memory_key_A, "realloc down memory info A");
1774   psi->memory_free(memory_key_A, 50, owner);
1775 
1776   /* Use global instrumentation only */
1777   /* ------------------------------- */
1778 
1779   flag_thread_instrumentation= false;
1780 
1781   key= psi->memory_alloc(memory_key_A, 100, & owner);
1782   ok(key == memory_key_A, "alloc memory info A");
1783   key= psi->memory_realloc(memory_key_A, 100, 200, & owner);
1784   ok(key == memory_key_A, "realloc memory info A");
1785   key= psi->memory_realloc(memory_key_A, 200, 300, & owner);
1786   ok(key == memory_key_A, "realloc up memory info A");
1787   key= psi->memory_realloc(memory_key_A, 300, 50, & owner);
1788   ok(key == memory_key_A, "realloc down memory info A");
1789   psi->memory_free(memory_key_A, 50, owner);
1790 
1791   /* Garbage, for robustness */
1792   /* ----------------------- */
1793 
1794   key= psi->memory_alloc(9999, 100, & owner);
1795   ok(key == PSI_NOT_INSTRUMENTED, "alloc with unknown key");
1796   key= psi->memory_realloc(PSI_NOT_INSTRUMENTED, 100, 200, & owner);
1797   ok(key == PSI_NOT_INSTRUMENTED, "realloc with unknown key");
1798   psi->memory_free(PSI_NOT_INSTRUMENTED, 200, owner);
1799 
1800   shutdown_performance_schema();
1801 }
1802 
test_leaks()1803 void test_leaks()
1804 {
1805   PSI_bootstrap *boot;
1806   PFS_global_param param;
1807 
1808   /* Allocate everything, to make sure cleanup does not forget anything. */
1809 
1810   memset(& param, 0xFF, sizeof(param));
1811   param.m_enabled= true;
1812   param.m_mutex_class_sizing= 10;
1813   param.m_rwlock_class_sizing= 10;
1814   param.m_cond_class_sizing= 10;
1815   param.m_thread_class_sizing= 10;
1816   param.m_table_share_sizing= 10;
1817   param.m_file_class_sizing= 10;
1818   param.m_socket_class_sizing= 10;
1819   param.m_mutex_sizing= 1000;
1820   param.m_rwlock_sizing= 1000;
1821   param.m_cond_sizing= 1000;
1822   param.m_thread_sizing= 1000;
1823   param.m_table_sizing= 1000;
1824   param.m_file_sizing= 1000;
1825   param.m_file_handle_sizing= 1000;
1826   param.m_socket_sizing= 1000;
1827   param.m_events_waits_history_sizing= 10;
1828   param.m_events_waits_history_long_sizing= 1000;
1829   param.m_setup_actor_sizing= 1000;
1830   param.m_setup_object_sizing= 1000;
1831   param.m_host_sizing= 1000;
1832   param.m_user_sizing= 1000;
1833   param.m_account_sizing= 1000;
1834   param.m_stage_class_sizing= 10;
1835   param.m_events_stages_history_sizing= 10;
1836   param.m_events_stages_history_long_sizing= 1000;
1837   param.m_statement_class_sizing= 10;
1838   param.m_events_statements_history_sizing= 10;
1839   param.m_events_statements_history_long_sizing= 1000;
1840   param.m_session_connect_attrs_sizing= 1000;
1841   param.m_memory_class_sizing= 10;
1842   param.m_metadata_lock_sizing= 1000;
1843   param.m_digest_sizing= 1000;
1844   param.m_program_sizing= 1000;
1845   param.m_statement_stack_sizing= 10;
1846   param.m_max_digest_length= 1000;
1847   param.m_max_sql_text_length= 1000;
1848 
1849   param.m_hints.m_table_definition_cache = 100;
1850   param.m_hints.m_table_open_cache       = 100;
1851   param.m_hints.m_max_connections        = 100;
1852   param.m_hints.m_open_files_limit       = 100;
1853   param.m_hints.m_max_prepared_stmt_count= 100;
1854 
1855   pre_initialize_performance_schema();
1856   boot= initialize_performance_schema(& param);
1857   ok(boot != NULL, "bootstrap");
1858   shutdown_performance_schema();
1859 
1860   /* Leaks will be reported with valgrind */
1861 }
1862 
do_all_tests()1863 void do_all_tests()
1864 {
1865   /* Using initialize_performance_schema(), no partial init needed. */
1866 
1867   test_bootstrap();
1868   test_bad_registration();
1869   test_init_disabled();
1870   test_locker_disabled();
1871   test_file_instrumentation_leak();
1872   test_event_name_index();
1873   test_memory_instruments();
1874   test_leaks();
1875 }
1876 
main(int,char **)1877 int main(int, char **)
1878 {
1879   plan(232);
1880 
1881   MY_INIT("pfs-t");
1882   do_all_tests();
1883   return (exit_status());
1884 }
1885 
1886