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