summaryrefslogtreecommitdiffstats
path: root/storage/perfschema/pfs_buffer_container.cc
blob: 04f6f5219a33610a33e9a7642905f7b6a6224233 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
/* Copyright (c) 2014, 2023, Oracle and/or its affiliates.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License, version 2.0,
  as published by the Free Software Foundation.

  This program is also distributed with certain software (including
  but not limited to OpenSSL) that is licensed under separate terms,
  as designated in a particular file or component or in included license
  documentation.  The authors of MySQL hereby grant you an additional
  permission to link the program and your derivative works with the
  separately licensed software that they have included with MySQL.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License, version 2.0, for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software Foundation,
  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */

#include "my_global.h"
#include "pfs_global.h"
#include "pfs_lock.h"
#include "pfs_account.h"
#include "pfs_user.h"
#include "pfs_host.h"
#include "pfs_buffer_container.h"
#include "pfs_builtin_memory.h"

PFS_buffer_default_allocator<PFS_mutex> default_mutex_allocator(& builtin_memory_mutex);
PFS_mutex_container global_mutex_container(& default_mutex_allocator);

PFS_buffer_default_allocator<PFS_rwlock> default_rwlock_allocator(& builtin_memory_rwlock);
PFS_rwlock_container global_rwlock_container(& default_rwlock_allocator);

PFS_buffer_default_allocator<PFS_cond> default_cond_allocator(& builtin_memory_cond);
PFS_cond_container global_cond_container(& default_cond_allocator);

PFS_buffer_default_allocator<PFS_file> default_file_allocator(& builtin_memory_file);
PFS_file_container global_file_container(& default_file_allocator);

PFS_buffer_default_allocator<PFS_socket> default_socket_allocator(& builtin_memory_socket);
PFS_socket_container global_socket_container(& default_socket_allocator);

PFS_buffer_default_allocator<PFS_metadata_lock> default_mdl_allocator(& builtin_memory_mdl);
PFS_mdl_container global_mdl_container(& default_mdl_allocator);

PFS_buffer_default_allocator<PFS_setup_actor> default_setup_actor_allocator(& builtin_memory_setup_actor);
PFS_setup_actor_container global_setup_actor_container(& default_setup_actor_allocator);

PFS_buffer_default_allocator<PFS_setup_object> default_setup_object_allocator(& builtin_memory_setup_object);
PFS_setup_object_container global_setup_object_container(& default_setup_object_allocator);

PFS_buffer_default_allocator<PFS_table> default_table_allocator(& builtin_memory_table);
PFS_table_container global_table_container(& default_table_allocator);

PFS_buffer_default_allocator<PFS_table_share> default_table_share_allocator(& builtin_memory_table_share);
PFS_table_share_container global_table_share_container(& default_table_share_allocator);

PFS_buffer_default_allocator<PFS_table_share_index> default_table_share_index_allocator(& builtin_memory_table_share_index);
PFS_table_share_index_container global_table_share_index_container(& default_table_share_index_allocator);

PFS_buffer_default_allocator<PFS_table_share_lock> default_table_share_lock_allocator(& builtin_memory_table_share_lock);
PFS_table_share_lock_container global_table_share_lock_container(& default_table_share_lock_allocator);

PFS_buffer_default_allocator<PFS_program> default_program_allocator(& builtin_memory_program);
PFS_program_container global_program_container(& default_program_allocator);

PFS_buffer_default_allocator<PFS_prepared_stmt> default_prepared_stmt_allocator(& builtin_memory_prepared_stmt);
PFS_prepared_stmt_container global_prepared_stmt_container(& default_prepared_stmt_allocator);

int PFS_account_allocator::alloc_array(PFS_account_array *array)
{
  size_t size= array->m_max;
  size_t index;
  size_t waits_sizing= size * wait_class_max;
  size_t stages_sizing= size * stage_class_max;
  size_t statements_sizing= size * statement_class_max;
  size_t transactions_sizing= size * transaction_class_max;
  size_t memory_sizing= size * memory_class_max;

  array->m_ptr= NULL;
  array->m_full= true;
  array->m_instr_class_waits_array= NULL;
  array->m_instr_class_stages_array= NULL;
  array->m_instr_class_statements_array= NULL;
  array->m_instr_class_transactions_array= NULL;
  array->m_instr_class_memory_array= NULL;

  if (size > 0)
  {
    array->m_ptr=
      PFS_MALLOC_ARRAY(& builtin_memory_account,
                       size, sizeof(PFS_account), PFS_account, MYF(MY_ZEROFILL));
    if (array->m_ptr == NULL)
      return 1;
  }

  if (waits_sizing > 0)
  {
    array->m_instr_class_waits_array=
      PFS_MALLOC_ARRAY(& builtin_memory_account_waits,
                       waits_sizing, sizeof(PFS_single_stat), PFS_single_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_waits_array == NULL)
      return 1;

    for (index=0; index < waits_sizing; index++)
      array->m_instr_class_waits_array[index].reset();
  }

  if (stages_sizing > 0)
  {
    array->m_instr_class_stages_array=
      PFS_MALLOC_ARRAY(& builtin_memory_account_stages,
                       stages_sizing, sizeof(PFS_stage_stat), PFS_stage_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_stages_array == NULL)
      return 1;

    for (index=0; index < stages_sizing; index++)
      array->m_instr_class_stages_array[index].reset();
  }

  if (statements_sizing > 0)
  {
    array->m_instr_class_statements_array=
      PFS_MALLOC_ARRAY(& builtin_memory_account_statements,
                       statements_sizing, sizeof(PFS_statement_stat), PFS_statement_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_statements_array == NULL)
      return 1;

    for (index=0; index < statements_sizing; index++)
      array->m_instr_class_statements_array[index].reset();
  }

  if (transactions_sizing > 0)
  {
    array->m_instr_class_transactions_array=
      PFS_MALLOC_ARRAY(& builtin_memory_account_transactions,
                       transactions_sizing, sizeof(PFS_transaction_stat), PFS_transaction_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_transactions_array == NULL)
      return 1;

    for (index=0; index < transactions_sizing; index++)
      array->m_instr_class_transactions_array[index].reset();
  }

  if (memory_sizing > 0)
  {
    array->m_instr_class_memory_array=
      PFS_MALLOC_ARRAY(& builtin_memory_account_memory,
                       memory_sizing, sizeof(PFS_memory_stat), PFS_memory_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_memory_array == NULL)
      return 1;

    for (index=0; index < memory_sizing; index++)
      array->m_instr_class_memory_array[index].reset();
  }

  for (index= 0; index < size; index++)
  {
    array->m_ptr[index].set_instr_class_waits_stats(
      & array->m_instr_class_waits_array[index * wait_class_max]);
    array->m_ptr[index].set_instr_class_stages_stats(
      & array->m_instr_class_stages_array[index * stage_class_max]);
    array->m_ptr[index].set_instr_class_statements_stats(
      & array->m_instr_class_statements_array[index * statement_class_max]);
    array->m_ptr[index].set_instr_class_transactions_stats(
      & array->m_instr_class_transactions_array[index * transaction_class_max]);
    array->m_ptr[index].set_instr_class_memory_stats(
      & array->m_instr_class_memory_array[index * memory_class_max]);
  }

  array->m_full= false;
  return 0;
}

void PFS_account_allocator::free_array(PFS_account_array *array)
{
  size_t size= array->m_max;
  size_t waits_sizing= size * wait_class_max;
  size_t stages_sizing= size * stage_class_max;
  size_t statements_sizing= size * statement_class_max;
  size_t transactions_sizing= size * transaction_class_max;
  size_t memory_sizing= size * memory_class_max;

  PFS_FREE_ARRAY(& builtin_memory_account,
                 size, sizeof(PFS_account), array->m_ptr);
  array->m_ptr= NULL;

  PFS_FREE_ARRAY(& builtin_memory_account_waits,
                 waits_sizing, sizeof(PFS_single_stat),
                 array->m_instr_class_waits_array);
  array->m_instr_class_waits_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_account_stages,
                 stages_sizing, sizeof(PFS_stage_stat),
                 array->m_instr_class_stages_array);
  array->m_instr_class_stages_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_account_statements,
                 statements_sizing, sizeof(PFS_statement_stat),
                 array->m_instr_class_statements_array);
  array->m_instr_class_statements_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_account_transactions,
                 transactions_sizing, sizeof(PFS_transaction_stat),
                 array->m_instr_class_transactions_array);
  array->m_instr_class_transactions_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_account_memory,
                 memory_sizing, sizeof(PFS_memory_stat),
                 array->m_instr_class_memory_array);
  array->m_instr_class_memory_array= NULL;
}

PFS_account_allocator account_allocator;
PFS_account_container global_account_container(& account_allocator);

int PFS_host_allocator::alloc_array(PFS_host_array *array)
{
  size_t size= array->m_max;
  PFS_host *pfs;
  size_t index;
  size_t waits_sizing= size * wait_class_max;
  size_t stages_sizing= size * stage_class_max;
  size_t statements_sizing= size * statement_class_max;
  size_t transactions_sizing= size * transaction_class_max;
  size_t memory_sizing= size * memory_class_max;

  array->m_ptr= NULL;
  array->m_full= true;
  array->m_instr_class_waits_array= NULL;
  array->m_instr_class_stages_array= NULL;
  array->m_instr_class_statements_array= NULL;
  array->m_instr_class_transactions_array= NULL;
  array->m_instr_class_memory_array= NULL;

  if (size > 0)
  {
    array->m_ptr=
      PFS_MALLOC_ARRAY(& builtin_memory_host,
                       size, sizeof(PFS_host), PFS_host, MYF(MY_ZEROFILL));
    if (array->m_ptr == NULL)
      return 1;
  }

  if (waits_sizing > 0)
  {
    array->m_instr_class_waits_array=
      PFS_MALLOC_ARRAY(& builtin_memory_host_waits,
                       waits_sizing, sizeof(PFS_single_stat), PFS_single_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_waits_array == NULL)
      return 1;

    for (index=0; index < waits_sizing; index++)
      array->m_instr_class_waits_array[index].reset();
  }

  if (stages_sizing > 0)
  {
    array->m_instr_class_stages_array=
      PFS_MALLOC_ARRAY(& builtin_memory_host_stages,
                       stages_sizing, sizeof(PFS_stage_stat), PFS_stage_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_stages_array == NULL)
      return 1;

    for (index=0; index < stages_sizing; index++)
      array->m_instr_class_stages_array[index].reset();
  }

  if (statements_sizing > 0)
  {
    array->m_instr_class_statements_array=
      PFS_MALLOC_ARRAY(& builtin_memory_host_statements,
                       statements_sizing, sizeof(PFS_statement_stat), PFS_statement_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_statements_array == NULL)
      return 1;

    for (index=0; index < statements_sizing; index++)
      array->m_instr_class_statements_array[index].reset();
  }

  if (transactions_sizing > 0)
  {
    array->m_instr_class_transactions_array=
      PFS_MALLOC_ARRAY(& builtin_memory_host_transactions,
                       transactions_sizing, sizeof(PFS_transaction_stat), PFS_transaction_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_transactions_array == NULL)
      return 1;

    for (index=0; index < transactions_sizing; index++)
      array->m_instr_class_transactions_array[index].reset();
  }

  if (memory_sizing > 0)
  {
    array->m_instr_class_memory_array=
      PFS_MALLOC_ARRAY(& builtin_memory_host_memory,
                       memory_sizing, sizeof(PFS_memory_stat), PFS_memory_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_memory_array == NULL)
      return 1;

    for (index=0; index < memory_sizing; index++)
      array->m_instr_class_memory_array[index].reset();
  }

  for (index= 0; index < size; index++)
  {
    pfs= & array->m_ptr[index];

    pfs->set_instr_class_waits_stats(
      & array->m_instr_class_waits_array[index * wait_class_max]);
    pfs->set_instr_class_stages_stats(
      & array->m_instr_class_stages_array[index * stage_class_max]);
    pfs->set_instr_class_statements_stats(
      & array->m_instr_class_statements_array[index * statement_class_max]);
    pfs->set_instr_class_transactions_stats(
      & array->m_instr_class_transactions_array[index * transaction_class_max]);
    pfs->set_instr_class_memory_stats(
      & array->m_instr_class_memory_array[index * memory_class_max]);
  }

  array->m_full= false;
  return 0;
}

void PFS_host_allocator::free_array(PFS_host_array *array)
{
  size_t size= array->m_max;
  size_t waits_sizing= size * wait_class_max;
  size_t stages_sizing= size * stage_class_max;
  size_t statements_sizing= size * statement_class_max;
  size_t transactions_sizing= size * transaction_class_max;
  size_t memory_sizing= size * memory_class_max;

  PFS_FREE_ARRAY(& builtin_memory_host,
                 size, sizeof(PFS_host), array->m_ptr);
  array->m_ptr= NULL;

  PFS_FREE_ARRAY(& builtin_memory_host_waits,
                 waits_sizing, sizeof(PFS_single_stat),
                 array->m_instr_class_waits_array);
  array->m_instr_class_waits_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_host_stages,
                 stages_sizing, sizeof(PFS_stage_stat),
                 array->m_instr_class_stages_array);
  array->m_instr_class_stages_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_host_statements,
                 statements_sizing, sizeof(PFS_statement_stat),
                 array->m_instr_class_statements_array);
  array->m_instr_class_statements_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_host_transactions,
                 transactions_sizing, sizeof(PFS_transaction_stat),
                 array->m_instr_class_transactions_array);
  array->m_instr_class_transactions_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_host_memory,
                 memory_sizing, sizeof(PFS_memory_stat),
                 array->m_instr_class_memory_array);
  array->m_instr_class_memory_array= NULL;
}

PFS_host_allocator host_allocator;
PFS_host_container global_host_container(& host_allocator);

int PFS_thread_allocator::alloc_array(PFS_thread_array *array)
{
  size_t size= array->m_max;
  PFS_thread *pfs;
  PFS_events_statements *pfs_stmt;
  unsigned char *pfs_tokens;

  size_t index;
  size_t waits_sizing= size * wait_class_max;
  size_t stages_sizing= size * stage_class_max;
  size_t statements_sizing= size * statement_class_max;
  size_t transactions_sizing= size * transaction_class_max;
  size_t memory_sizing= size * memory_class_max;

  size_t waits_history_sizing= size * events_waits_history_per_thread;
  size_t stages_history_sizing= size * events_stages_history_per_thread;
  size_t statements_history_sizing= size * events_statements_history_per_thread;
  size_t statements_stack_sizing= size * statement_stack_max;
  size_t transactions_history_sizing= size * events_transactions_history_per_thread;
  size_t session_connect_attrs_sizing= size * session_connect_attrs_size_per_thread;

  size_t current_sqltext_sizing= size * pfs_max_sqltext * statement_stack_max;
  size_t history_sqltext_sizing= size * pfs_max_sqltext * events_statements_history_per_thread;
  size_t current_digest_tokens_sizing= size * pfs_max_digest_length * statement_stack_max;
  size_t history_digest_tokens_sizing= size * pfs_max_digest_length * events_statements_history_per_thread;

  array->m_ptr= NULL;
  array->m_full= true;
  array->m_instr_class_waits_array= NULL;
  array->m_instr_class_stages_array= NULL;
  array->m_instr_class_statements_array= NULL;
  array->m_instr_class_transactions_array= NULL;
  array->m_instr_class_memory_array= NULL;

  array->m_waits_history_array= NULL;
  array->m_stages_history_array= NULL;
  array->m_statements_history_array= NULL;
  array->m_statements_stack_array= NULL;
  array->m_transactions_history_array= NULL;
  array->m_session_connect_attrs_array= NULL;

  array->m_current_stmts_text_array= NULL;
  array->m_current_stmts_digest_token_array= NULL;
  array->m_history_stmts_text_array= NULL;
  array->m_history_stmts_digest_token_array= NULL;

  if (size > 0)
  {
    array->m_ptr=
      PFS_MALLOC_ARRAY(& builtin_memory_thread,
                       size, sizeof(PFS_thread), PFS_thread, MYF(MY_ZEROFILL));
    if (array->m_ptr == NULL)
      return 1;
  }

  if (waits_sizing > 0)
  {
    array->m_instr_class_waits_array=
      PFS_MALLOC_ARRAY(& builtin_memory_thread_waits,
                       waits_sizing, sizeof(PFS_single_stat), PFS_single_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_waits_array == NULL)
      return 1;

    for (index=0; index < waits_sizing; index++)
      array->m_instr_class_waits_array[index].reset();
  }

  if (stages_sizing > 0)
  {
    array->m_instr_class_stages_array=
      PFS_MALLOC_ARRAY(& builtin_memory_thread_stages,
                       stages_sizing, sizeof(PFS_stage_stat), PFS_stage_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_stages_array == NULL)
      return 1;

    for (index=0; index < stages_sizing; index++)
      array->m_instr_class_stages_array[index].reset();
  }

  if (statements_sizing > 0)
  {
    array->m_instr_class_statements_array=
      PFS_MALLOC_ARRAY(& builtin_memory_thread_statements,
                       statements_sizing, sizeof(PFS_statement_stat), PFS_statement_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_statements_array == NULL)
      return 1;

    for (index=0; index < statements_sizing; index++)
      array->m_instr_class_statements_array[index].reset();
  }

  if (transactions_sizing > 0)
  {
    array->m_instr_class_transactions_array=
      PFS_MALLOC_ARRAY(& builtin_memory_thread_transactions,
                       transactions_sizing, sizeof(PFS_transaction_stat), PFS_transaction_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_transactions_array == NULL)
      return 1;

    for (index=0; index < transactions_sizing; index++)
      array->m_instr_class_transactions_array[index].reset();
  }

  if (memory_sizing > 0)
  {
    array->m_instr_class_memory_array=
      PFS_MALLOC_ARRAY(& builtin_memory_thread_memory,
                       memory_sizing, sizeof(PFS_memory_stat), PFS_memory_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_memory_array == NULL)
      return 1;

    for (index=0; index < memory_sizing; index++)
      array->m_instr_class_memory_array[index].reset();
  }

  if (waits_history_sizing > 0)
  {
    array->m_waits_history_array=
      PFS_MALLOC_ARRAY(& builtin_memory_thread_waits_history,
                       waits_history_sizing, sizeof(PFS_events_waits), PFS_events_waits, MYF(MY_ZEROFILL));
    if (unlikely(array->m_waits_history_array == NULL))
      return 1;
  }

  if (stages_history_sizing > 0)
  {
    array->m_stages_history_array=
      PFS_MALLOC_ARRAY(& builtin_memory_thread_stages_history,
                       stages_history_sizing, sizeof(PFS_events_stages), PFS_events_stages, MYF(MY_ZEROFILL));
    if (unlikely(array->m_stages_history_array == NULL))
      return 1;
  }

  if (statements_history_sizing > 0)
  {
    array->m_statements_history_array=
      PFS_MALLOC_ARRAY(& builtin_memory_thread_statements_history,
                       statements_history_sizing, sizeof(PFS_events_statements), PFS_events_statements, MYF(MY_ZEROFILL));
    if (unlikely(array->m_statements_history_array == NULL))
      return 1;
  }

  if (statements_stack_sizing > 0)
  {
    array->m_statements_stack_array=
      PFS_MALLOC_ARRAY(& builtin_memory_thread_statements_stack,
                       statements_stack_sizing, sizeof(PFS_events_statements), PFS_events_statements, MYF(MY_ZEROFILL));
    if (unlikely(array->m_statements_stack_array == NULL))
      return 1;
  }

  if (transactions_history_sizing > 0)
  {
    array->m_transactions_history_array=
      PFS_MALLOC_ARRAY(& builtin_memory_thread_transaction_history,
                       transactions_history_sizing, sizeof(PFS_events_transactions), PFS_events_transactions, MYF(MY_ZEROFILL));
    if (unlikely(array->m_transactions_history_array == NULL))
      return 1;
  }

  if (session_connect_attrs_sizing > 0)
  {
    array->m_session_connect_attrs_array=
      (char *)pfs_malloc(& builtin_memory_thread_session_connect_attrs,
                         session_connect_attrs_sizing, MYF(MY_ZEROFILL));
    if (unlikely(array->m_session_connect_attrs_array == NULL))
      return 1;
  }

  if (current_sqltext_sizing > 0)
  {
    array->m_current_stmts_text_array=
      (char *)pfs_malloc(& builtin_memory_thread_statements_stack_sqltext,
                         current_sqltext_sizing, MYF(MY_ZEROFILL));
    if (unlikely(array->m_current_stmts_text_array == NULL))
      return 1;
  }

  if (history_sqltext_sizing > 0)
  {
    array->m_history_stmts_text_array=
      (char *)pfs_malloc(& builtin_memory_thread_statements_history_sqltext,
                         history_sqltext_sizing, MYF(MY_ZEROFILL));
    if (unlikely(array->m_history_stmts_text_array == NULL))
      return 1;
  }

  if (current_digest_tokens_sizing > 0)
  {
    array->m_current_stmts_digest_token_array=
      (unsigned char *)pfs_malloc(& builtin_memory_thread_statements_stack_tokens,
                                  current_digest_tokens_sizing, MYF(MY_ZEROFILL));
    if (unlikely(array->m_current_stmts_digest_token_array == NULL))
      return 1;
  }

  if (history_digest_tokens_sizing > 0)
  {
    array->m_history_stmts_digest_token_array=
      (unsigned char *)pfs_malloc(& builtin_memory_thread_statements_history_tokens,
                                  history_digest_tokens_sizing, MYF(MY_ZEROFILL));
    if (unlikely(array->m_history_stmts_digest_token_array == NULL))
      return 1;
  }

  for (index= 0; index < size; index++)
  {
    pfs= & array->m_ptr[index];

    pfs->set_instr_class_waits_stats(
      & array->m_instr_class_waits_array[index * wait_class_max]);
    pfs->set_instr_class_stages_stats(
      & array->m_instr_class_stages_array[index * stage_class_max]);
    pfs->set_instr_class_statements_stats(
      & array->m_instr_class_statements_array[index * statement_class_max]);
    pfs->set_instr_class_transactions_stats(
      & array->m_instr_class_transactions_array[index * transaction_class_max]);
    pfs->set_instr_class_memory_stats(
      & array->m_instr_class_memory_array[index * memory_class_max]);

    pfs->m_waits_history=
      & array->m_waits_history_array[index * events_waits_history_per_thread];
    pfs->m_stages_history=
      & array->m_stages_history_array[index * events_stages_history_per_thread];
    pfs->m_statements_history=
      & array->m_statements_history_array[index * events_statements_history_per_thread];
    pfs->m_statement_stack=
      & array->m_statements_stack_array[index * statement_stack_max];
    pfs->m_transactions_history=
      & array->m_transactions_history_array[index * events_transactions_history_per_thread];
    pfs->m_session_connect_attrs=
      & array->m_session_connect_attrs_array[index * session_connect_attrs_size_per_thread];
  }

  for (index= 0; index < statements_stack_sizing; index++)
  {
    pfs_stmt= & array->m_statements_stack_array[index];

    pfs_stmt->m_sqltext= & array->m_current_stmts_text_array[index * pfs_max_sqltext];

    pfs_tokens= & array->m_current_stmts_digest_token_array[index * pfs_max_digest_length];
    pfs_stmt->m_digest_storage.reset(pfs_tokens, pfs_max_digest_length);
  }

  for (index= 0; index < statements_history_sizing; index++)
  {
    pfs_stmt= & array->m_statements_history_array[index];

    pfs_stmt->m_sqltext= & array->m_history_stmts_text_array[index * pfs_max_sqltext];

    pfs_tokens= & array->m_history_stmts_digest_token_array[index * pfs_max_digest_length];
    pfs_stmt->m_digest_storage.reset(pfs_tokens, pfs_max_digest_length);
  }

  array->m_full= false;
  return 0;
}

void PFS_thread_allocator::free_array(PFS_thread_array *array)
{
  size_t size= array->m_max;
  size_t waits_sizing= size * wait_class_max;
  size_t stages_sizing= size * stage_class_max;
  size_t statements_sizing= size * statement_class_max;
  size_t transactions_sizing= size * transaction_class_max;
  size_t memory_sizing= size * memory_class_max;

  size_t waits_history_sizing= size * events_waits_history_per_thread;
  size_t stages_history_sizing= size * events_stages_history_per_thread;
  size_t statements_history_sizing= size * events_statements_history_per_thread;
  size_t statements_stack_sizing= size * statement_stack_max;
  size_t transactions_history_sizing= size * events_transactions_history_per_thread;
  size_t session_connect_attrs_sizing= size * session_connect_attrs_size_per_thread;

  size_t current_sqltext_sizing= size * pfs_max_sqltext * statement_stack_max;
  size_t history_sqltext_sizing= size * pfs_max_sqltext * events_statements_history_per_thread;
  size_t current_digest_tokens_sizing= size * pfs_max_digest_length * statement_stack_max;
  size_t history_digest_tokens_sizing= size * pfs_max_digest_length * events_statements_history_per_thread;

  PFS_FREE_ARRAY(& builtin_memory_thread,
                 size, sizeof(PFS_thread), array->m_ptr);
  array->m_ptr= NULL;

  PFS_FREE_ARRAY(& builtin_memory_thread_waits,
                 waits_sizing, sizeof(PFS_single_stat),
                 array->m_instr_class_waits_array);
  array->m_instr_class_waits_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_thread_stages,
                 stages_sizing, sizeof(PFS_stage_stat),
                 array->m_instr_class_stages_array);
  array->m_instr_class_stages_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_thread_statements,
                 statements_sizing, sizeof(PFS_statement_stat),
                 array->m_instr_class_statements_array);
  array->m_instr_class_statements_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_thread_transactions,
                 transactions_sizing, sizeof(PFS_transaction_stat),
                 array->m_instr_class_transactions_array);
  array->m_instr_class_transactions_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_thread_memory,
                 memory_sizing, sizeof(PFS_memory_stat),
                 array->m_instr_class_memory_array);
  array->m_instr_class_memory_array= NULL;


  PFS_FREE_ARRAY(& builtin_memory_thread_waits_history,
                 waits_history_sizing, sizeof(PFS_events_waits),
                 array->m_waits_history_array);
  array->m_waits_history_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_thread_stages_history,
                 stages_history_sizing, sizeof(PFS_events_stages),
                 array->m_stages_history_array);
  array->m_stages_history_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_thread_statements_history,
                 statements_history_sizing, sizeof(PFS_events_statements),
                 array->m_statements_history_array);
  array->m_statements_history_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_thread_statements_stack,
                 statements_stack_sizing, sizeof(PFS_events_statements),
                 array->m_statements_stack_array);
  array->m_statements_stack_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_thread_transaction_history,
                 transactions_history_sizing, sizeof(PFS_events_transactions),
                 array->m_transactions_history_array);
  array->m_transactions_history_array= NULL;

  pfs_free(& builtin_memory_thread_session_connect_attrs,
           session_connect_attrs_sizing,
           array->m_session_connect_attrs_array);
  array->m_session_connect_attrs_array= NULL;

  pfs_free(& builtin_memory_thread_statements_stack_sqltext,
           current_sqltext_sizing,
           array->m_current_stmts_text_array);
  array->m_current_stmts_text_array= NULL;

  pfs_free(& builtin_memory_thread_statements_history_sqltext,
           history_sqltext_sizing,
          array->m_history_stmts_text_array);
  array->m_history_stmts_text_array= NULL;

  pfs_free(& builtin_memory_thread_statements_stack_tokens,
           current_digest_tokens_sizing,
           array->m_current_stmts_digest_token_array);
  array->m_current_stmts_digest_token_array= NULL;

  pfs_free(& builtin_memory_thread_statements_history_tokens,
           history_digest_tokens_sizing,
           array->m_history_stmts_digest_token_array);
  array->m_history_stmts_digest_token_array= NULL;
}

PFS_thread_allocator thread_allocator;
PFS_thread_container global_thread_container(& thread_allocator);

int PFS_user_allocator::alloc_array(PFS_user_array *array)
{
  size_t size= array->m_max;
  PFS_user *pfs;
  size_t index;
  size_t waits_sizing= size * wait_class_max;
  size_t stages_sizing= size * stage_class_max;
  size_t statements_sizing= size * statement_class_max;
  size_t transactions_sizing= size * transaction_class_max;
  size_t memory_sizing= size * memory_class_max;

  array->m_ptr= NULL;
  array->m_full= true;
  array->m_instr_class_waits_array= NULL;
  array->m_instr_class_stages_array= NULL;
  array->m_instr_class_statements_array= NULL;
  array->m_instr_class_transactions_array= NULL;
  array->m_instr_class_memory_array= NULL;

  if (size > 0)
  {
    array->m_ptr=
      PFS_MALLOC_ARRAY(& builtin_memory_user,
                       size, sizeof(PFS_user), PFS_user, MYF(MY_ZEROFILL));
    if (array->m_ptr == NULL)
      return 1;
  }

  if (waits_sizing > 0)
  {
    array->m_instr_class_waits_array=
      PFS_MALLOC_ARRAY(& builtin_memory_user_waits,
                       waits_sizing, sizeof(PFS_single_stat), PFS_single_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_waits_array == NULL)
      return 1;

    for (index=0; index < waits_sizing; index++)
      array->m_instr_class_waits_array[index].reset();
  }

  if (stages_sizing > 0)
  {
    array->m_instr_class_stages_array=
      PFS_MALLOC_ARRAY(& builtin_memory_user_stages,
                       stages_sizing, sizeof(PFS_stage_stat), PFS_stage_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_stages_array == NULL)
      return 1;

    for (index=0; index < stages_sizing; index++)
      array->m_instr_class_stages_array[index].reset();
  }

  if (statements_sizing > 0)
  {
    array->m_instr_class_statements_array=
      PFS_MALLOC_ARRAY(& builtin_memory_user_statements,
                       statements_sizing, sizeof(PFS_statement_stat), PFS_statement_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_statements_array == NULL)
      return 1;

    for (index=0; index < statements_sizing; index++)
      array->m_instr_class_statements_array[index].reset();
  }

  if (transactions_sizing > 0)
  {
    array->m_instr_class_transactions_array=
      PFS_MALLOC_ARRAY(& builtin_memory_user_transactions,
                       transactions_sizing, sizeof(PFS_transaction_stat), PFS_transaction_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_transactions_array == NULL)
      return 1;

    for (index=0; index < transactions_sizing; index++)
      array->m_instr_class_transactions_array[index].reset();
  }

  if (memory_sizing > 0)
  {
    array->m_instr_class_memory_array=
      PFS_MALLOC_ARRAY(& builtin_memory_user_memory,
                       memory_sizing, sizeof(PFS_memory_stat), PFS_memory_stat, MYF(MY_ZEROFILL));
    if (array->m_instr_class_memory_array == NULL)
      return 1;

    for (index=0; index < memory_sizing; index++)
      array->m_instr_class_memory_array[index].reset();
  }

  for (index= 0; index < size; index++)
  {
    pfs= & array->m_ptr[index];

    pfs->set_instr_class_waits_stats(
      & array->m_instr_class_waits_array[index * wait_class_max]);
    pfs->set_instr_class_stages_stats(
      & array->m_instr_class_stages_array[index * stage_class_max]);
    pfs->set_instr_class_statements_stats(
      & array->m_instr_class_statements_array[index * statement_class_max]);
    pfs->set_instr_class_transactions_stats(
      & array->m_instr_class_transactions_array[index * transaction_class_max]);
    pfs->set_instr_class_memory_stats(
      & array->m_instr_class_memory_array[index * memory_class_max]);
  }

  array->m_full= false;
  return 0;
}

void PFS_user_allocator::free_array(PFS_user_array *array)
{
  size_t size= array->m_max;
  size_t waits_sizing= size * wait_class_max;
  size_t stages_sizing= size * stage_class_max;
  size_t statements_sizing= size * statement_class_max;
  size_t transactions_sizing= size * transaction_class_max;
  size_t memory_sizing= size * memory_class_max;

  PFS_FREE_ARRAY(& builtin_memory_user,
                 size, sizeof(PFS_user), array->m_ptr);
  array->m_ptr= NULL;

  PFS_FREE_ARRAY(& builtin_memory_user_waits,
                 waits_sizing, sizeof(PFS_single_stat),
                 array->m_instr_class_waits_array);
  array->m_instr_class_waits_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_user_stages,
                 stages_sizing, sizeof(PFS_stage_stat),
                 array->m_instr_class_stages_array);
  array->m_instr_class_stages_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_user_statements,
                 statements_sizing, sizeof(PFS_statement_stat),
                 array->m_instr_class_statements_array);
  array->m_instr_class_statements_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_user_transactions,
                 transactions_sizing, sizeof(PFS_transaction_stat),
                 array->m_instr_class_transactions_array);
  array->m_instr_class_transactions_array= NULL;

  PFS_FREE_ARRAY(& builtin_memory_user_memory,
                 memory_sizing, sizeof(PFS_memory_stat),
                 array->m_instr_class_memory_array);
  array->m_instr_class_memory_array= NULL;
}

PFS_user_allocator user_allocator;
PFS_user_container global_user_container(& user_allocator);