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