-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathpfs_instr_class.cc
2772 lines (2354 loc) · 86.3 KB
/
pfs_instr_class.cc
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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2008, 2025, 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 designed to work 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 either included with
the program or referenced in the documentation.
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, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@file storage/perfschema/pfs_instr_class.cc
Performance schema instruments metadata (implementation).
*/
#include "storage/perfschema/pfs_instr_class.h"
#include <assert.h>
#include <string.h>
#include <algorithm>
#include <atomic>
#include "lex_string.h"
#include "lf.h"
#include "my_dbug.h"
#include "my_macros.h"
#include "my_sys.h"
#include "my_systime.h"
#include "mysql/psi/mysql_thread.h"
#include "mysql/strings/m_ctype.h"
#include "sql/mysqld.h" // lower_case_table_names
#include "sql/table.h"
#include "storage/perfschema/pfs_buffer_container.h"
#include "storage/perfschema/pfs_builtin_memory.h"
#include "storage/perfschema/pfs_column_values.h"
#include "storage/perfschema/pfs_events_waits.h"
#include "storage/perfschema/pfs_global.h"
#include "storage/perfschema/pfs_instr.h"
#include "storage/perfschema/pfs_program.h"
#include "storage/perfschema/pfs_setup_object.h"
#include "storage/perfschema/pfs_timer.h"
#include "storage/perfschema/terminology_use_previous.h"
#include <mysql/components/services/mysql_server_telemetry_logs_service.h>
extern std::atomic<log_delivery_callback_t> g_telemetry_log;
/**
@defgroup performance_schema_buffers Performance Schema Buffers
@ingroup performance_schema_implementation
@{
*/
/**
Global performance schema flag.
Indicate if the performance schema is enabled.
This flag is set at startup, and never changes.
*/
bool pfs_enabled = true;
/**
Global performance schema reference count for plugin and component events.
Incremented when a shared library is being unloaded, decremented when
the performance schema is finished processing the event.
*/
std::atomic<uint32> pfs_unload_plugin_ref_count(0);
/**
PFS_INSTRUMENT option settings array
*/
Pfs_instr_config_array *pfs_instr_config_array = nullptr;
static void configure_instr_class(PFS_instr_class *entry);
static void init_instr_class(PFS_instr_class *klass, const char *name,
uint name_length, int flags, int volatility,
const char *documentation,
PFS_class_type class_type);
/**
PFS_METER option settings array
*/
Pfs_meter_config_array *pfs_meter_config_array = nullptr;
static void configure_meter_class(PFS_meter_class *entry);
/**
PFS_LOGGER option settings array
*/
Pfs_logger_config_array *pfs_logger_config_array = nullptr;
static void configure_logger_class(PFS_logger_class *entry);
/**
Current number of elements in mutex_class_array.
This global variable is written to during:
- the performance schema initialization
- a plugin initialization
*/
static std::atomic<uint32> mutex_class_dirty_count{0};
static std::atomic<uint32> mutex_class_allocated_count{0};
static std::atomic<uint32> rwlock_class_dirty_count{0};
static std::atomic<uint32> rwlock_class_allocated_count{0};
static std::atomic<uint32> cond_class_dirty_count{0};
static std::atomic<uint32> cond_class_allocated_count{0};
static std::atomic<uint32> meter_class_dirty_count{0};
static std::atomic<uint32> meter_class_allocated_count{0};
static std::atomic<uint32> metric_class_dirty_count{0};
static std::atomic<uint32> metric_class_allocated_count{0};
static std::atomic<uint32> logger_class_dirty_count{0};
static std::atomic<uint32> logger_class_allocated_count{0};
/** Size of the mutex class array. @sa mutex_class_array */
ulong mutex_class_max = 0;
/** Number of mutex class lost. @sa mutex_class_array */
ulong mutex_class_lost = 0;
/** Size of the rwlock class array. @sa rwlock_class_array */
ulong rwlock_class_max = 0;
/** Number of rwlock class lost. @sa rwlock_class_array */
ulong rwlock_class_lost = 0;
/** Size of the condition class array. @sa cond_class_array */
ulong cond_class_max = 0;
/** Number of condition class lost. @sa cond_class_array */
ulong cond_class_lost = 0;
/** Size of the thread class array. @sa thread_class_array */
ulong thread_class_max = 0;
/** Number of thread class lost. @sa thread_class_array */
ulong thread_class_lost = 0;
/** Size of the file class array. @sa file_class_array */
ulong file_class_max = 0;
/** Number of file class lost. @sa file_class_array */
ulong file_class_lost = 0;
/** Size of the stage class array. @sa stage_class_array */
ulong stage_class_max = 0;
/** Number of stage class lost. @sa stage_class_array */
ulong stage_class_lost = 0;
/** Size of the statement class array. @sa statement_class_array */
ulong statement_class_max = 0;
/** Number of statement class lost. @sa statement_class_array */
ulong statement_class_lost = 0;
/** Size of the socket class array. @sa socket_class_array */
ulong socket_class_max = 0;
/** Number of socket class lost. @sa socket_class_array */
ulong socket_class_lost = 0;
/** Size of the memory class array. @sa memory_class_array */
ulong memory_class_max = 0;
/** Number of memory class lost. @sa memory_class_array */
ulong memory_class_lost = 0;
/** Size of the meter class array. @sa meter_class_array */
ulong meter_class_max = 0;
/** Number of meter class lost. @sa meter_class_array */
ulong meter_class_lost = 0;
/** Size of the metric class array. @sa metric_class_array */
ulong metric_class_max = 0;
/** Number of metric class lost. @sa metric_class_array */
ulong metric_class_lost = 0;
/** Size of the logger class array. @sa logger_class_array */
ulong logger_class_max = 0;
/** Number of logger class lost. @sa logger_class_array */
ulong logger_class_lost = 0;
/**
Number of transaction classes. Although there is only one transaction class,
this is used for sizing by other event classes.
@sa global_transaction_class
*/
ulong transaction_class_max = 0;
/**
Number of error classes. Although there is only one error class,
this is kept for future use if there is more error classification required.
@sa global_error_class
*/
ulong error_class_max = 0;
PFS_mutex_class *mutex_class_array = nullptr;
PFS_rwlock_class *rwlock_class_array = nullptr;
PFS_cond_class *cond_class_array = nullptr;
PFS_meter_class *meter_class_array = nullptr;
PFS_metric_class *metric_class_array = nullptr;
PFS_logger_class *logger_class_array = nullptr;
/**
Current number or elements in thread_class_array.
This global variable is written to during:
- the performance schema initialization
- a plugin initialization
*/
static std::atomic<uint32> thread_class_dirty_count{0};
static std::atomic<uint32> thread_class_allocated_count{0};
static PFS_thread_class *thread_class_array = nullptr;
PFS_ALIGNED PFS_single_stat global_idle_stat;
PFS_ALIGNED PFS_table_io_stat global_table_io_stat;
PFS_ALIGNED PFS_table_lock_stat global_table_lock_stat;
PFS_ALIGNED PFS_single_stat global_metadata_stat;
PFS_ALIGNED PFS_transaction_stat global_transaction_stat;
PFS_ALIGNED PFS_error_stat global_error_stat;
PFS_ALIGNED PFS_instr_class global_table_io_class;
PFS_ALIGNED PFS_instr_class global_table_lock_class;
PFS_ALIGNED PFS_instr_class global_idle_class;
PFS_ALIGNED PFS_instr_class global_metadata_class;
PFS_ALIGNED PFS_error_class global_error_class;
PFS_ALIGNED PFS_transaction_class global_transaction_class;
PFS_ALIGNED PFS_meter_class global_meter_class;
PFS_ALIGNED PFS_metric_class global_metric_class;
PFS_ALIGNED PFS_logger_class global_logger_class;
/**
Hash index for instrumented table shares.
This index is searched by table fully qualified name (@c PFS_table_share_key),
and points to instrumented table shares (@c PFS_table_share).
@sa PFS_table_share_key
@sa PFS_table_share
@sa table_share_hash_get_key
@sa get_table_share_hash_pins
*/
LF_HASH table_share_hash;
/** True if table_share_hash is initialized. */
static bool table_share_hash_inited = false;
static std::atomic<uint32> file_class_dirty_count{0};
static std::atomic<uint32> file_class_allocated_count{0};
PFS_file_class *file_class_array = nullptr;
static std::atomic<uint32> stage_class_dirty_count{0};
static std::atomic<uint32> stage_class_allocated_count{0};
static PFS_stage_class *stage_class_array = nullptr;
static std::atomic<uint32> statement_class_dirty_count{0};
static std::atomic<uint32> statement_class_allocated_count{0};
static PFS_statement_class *statement_class_array = nullptr;
static std::atomic<uint32> socket_class_dirty_count{0};
static std::atomic<uint32> socket_class_allocated_count{0};
static PFS_socket_class *socket_class_array = nullptr;
static std::atomic<uint32> memory_class_dirty_count{0};
static std::atomic<uint32> memory_class_allocated_count{0};
static std::atomic<PFS_memory_class *> memory_class_array{nullptr};
uint mutex_class_start = 0;
uint rwlock_class_start = 0;
uint cond_class_start = 0;
uint file_class_start = 0;
uint wait_class_max = 0;
uint socket_class_start = 0;
const char *PFS_instr_name::str() const {
DBUG_TRACE;
if (m_private_old_name != nullptr &&
terminology_use_previous::is_older_required(m_private_version))
return m_private_old_name;
return m_private_name;
}
uint PFS_instr_name::length() const {
if (m_private_old_name != nullptr &&
terminology_use_previous::is_older_required(m_private_version))
return m_private_old_name_length;
return m_private_name_length;
}
/**
Like strlen (or POSIX strnlen), but don't read past the max_len'th
character.
This is useful when the string may be terminated without '\0' at the
end of the buffer.
@param s The string
@param max_len The maxmium length
@return The length of the string, or max_len if the string is longer
than that.
*/
static uint safe_strlen(const char *s, uint max_len) {
const char *end =
static_cast<const char *>(memchr(s, '\0', static_cast<size_t>(max_len)));
return end == nullptr ? max_len : static_cast<uint>(end - s);
}
void PFS_instr_name::set(PFS_class_type class_type, const char *name,
uint max_length_arg) {
// Copy the given name to the member.
const uint length = safe_strlen(name, std::min(max_length, max_length_arg));
memcpy(m_private_name, name, length);
m_private_name[length] = '\0';
m_private_name_length = length;
// Check if there is an alternative name to use when
// @@terminology_use_previous is enabled.
const auto compatible_name =
terminology_use_previous::lookup(class_type, std::string{name, length});
m_private_old_name = compatible_name.old_name;
m_private_old_name_length =
compatible_name.old_name ? strlen(compatible_name.old_name) : 0;
m_private_version = compatible_name.version;
}
void init_event_name_sizing(const PFS_global_param *param) {
/* global table I/O, table lock, idle, metadata */
mutex_class_start = COUNT_GLOBAL_EVENT_INDEX;
rwlock_class_start = mutex_class_start + param->m_mutex_class_sizing;
cond_class_start = rwlock_class_start + param->m_rwlock_class_sizing;
file_class_start = cond_class_start + param->m_cond_class_sizing;
socket_class_start = file_class_start + param->m_file_class_sizing;
wait_class_max = socket_class_start + param->m_socket_class_sizing;
}
void register_global_classes() {
/* Table I/O class */
init_instr_class(&global_table_io_class, table_io_class_name.str,
(uint)table_io_class_name.length, 0, 0, PSI_DOCUMENT_ME,
PFS_CLASS_TABLE_IO);
global_table_io_class.m_event_name_index = GLOBAL_TABLE_IO_EVENT_INDEX;
configure_instr_class(&global_table_io_class);
/* Table lock class */
init_instr_class(&global_table_lock_class, table_lock_class_name.str,
(uint)table_lock_class_name.length, 0, 0, PSI_DOCUMENT_ME,
PFS_CLASS_TABLE_LOCK);
global_table_lock_class.m_event_name_index = GLOBAL_TABLE_LOCK_EVENT_INDEX;
configure_instr_class(&global_table_lock_class);
/* Idle class */
init_instr_class(&global_idle_class, idle_class_name.str,
(uint)idle_class_name.length, PSI_FLAG_USER,
0, /* no volatility */
PSI_DOCUMENT_ME, PFS_CLASS_IDLE);
global_idle_class.m_event_name_index = GLOBAL_IDLE_EVENT_INDEX;
configure_instr_class(&global_idle_class);
/* Metadata class */
init_instr_class(&global_metadata_class, metadata_lock_class_name.str,
(uint)metadata_lock_class_name.length, 0, 0, PSI_DOCUMENT_ME,
PFS_CLASS_METADATA);
global_metadata_class.m_event_name_index = GLOBAL_METADATA_EVENT_INDEX;
configure_instr_class(&global_metadata_class);
/* Error class */
init_instr_class(&global_error_class, error_class_name.str,
(uint)error_class_name.length, 0, 0, PSI_DOCUMENT_ME,
PFS_CLASS_ERROR);
global_error_class.m_event_name_index = GLOBAL_ERROR_INDEX;
global_error_class.m_enabled = true; /* Enabled by default */
configure_instr_class(&global_error_class);
global_error_class.m_timed = false; /* Not applicable */
error_class_max = 1; /* only one error class as of now. */
/* Transaction class */
init_instr_class(&global_transaction_class, transaction_instrument_prefix.str,
(uint)transaction_instrument_prefix.length, 0, 0,
PSI_DOCUMENT_ME, PFS_CLASS_TRANSACTION);
global_transaction_class.m_event_name_index = GLOBAL_TRANSACTION_INDEX;
configure_instr_class(&global_transaction_class);
transaction_class_max = 1; /* used for sizing by other event classes */
}
/**
Initialize the instrument synch class buffers.
@param mutex_class_sizing max number of mutex class
@param rwlock_class_sizing max number of rwlock class
@param cond_class_sizing max number of condition class
@return 0 on success
*/
int init_sync_class(uint mutex_class_sizing, uint rwlock_class_sizing,
uint cond_class_sizing) {
mutex_class_dirty_count = mutex_class_allocated_count = 0;
rwlock_class_dirty_count = rwlock_class_allocated_count = 0;
cond_class_dirty_count = cond_class_allocated_count = 0;
mutex_class_max = mutex_class_sizing;
rwlock_class_max = rwlock_class_sizing;
cond_class_max = cond_class_sizing;
mutex_class_lost = rwlock_class_lost = cond_class_lost = 0;
mutex_class_array = nullptr;
rwlock_class_array = nullptr;
cond_class_array = nullptr;
if (mutex_class_max > 0) {
mutex_class_array = PFS_MALLOC_ARRAY(
&builtin_memory_mutex_class, mutex_class_max, sizeof(PFS_mutex_class),
PFS_mutex_class, MYF(MY_ZEROFILL));
if (unlikely(mutex_class_array == nullptr)) {
mutex_class_max = 0;
return 1;
}
}
if (rwlock_class_max > 0) {
rwlock_class_array = PFS_MALLOC_ARRAY(
&builtin_memory_rwlock_class, rwlock_class_max,
sizeof(PFS_rwlock_class), PFS_rwlock_class, MYF(MY_ZEROFILL));
if (unlikely(rwlock_class_array == nullptr)) {
rwlock_class_max = 0;
return 1;
}
}
if (cond_class_max > 0) {
cond_class_array = PFS_MALLOC_ARRAY(&builtin_memory_cond_class,
cond_class_max, sizeof(PFS_cond_class),
PFS_cond_class, MYF(MY_ZEROFILL));
if (unlikely(cond_class_array == nullptr)) {
cond_class_max = 0;
return 1;
}
}
return 0;
}
/** Cleanup the instrument synch class buffers. */
void cleanup_sync_class() {
unsigned int i;
if (mutex_class_array != nullptr) {
for (i = 0; i < mutex_class_max; i++) {
my_free(mutex_class_array[i].m_documentation);
}
}
PFS_FREE_ARRAY(&builtin_memory_mutex_class, mutex_class_max,
sizeof(PFS_mutex_class), mutex_class_array);
mutex_class_array = nullptr;
mutex_class_dirty_count = mutex_class_allocated_count = mutex_class_max = 0;
if (rwlock_class_array != nullptr) {
for (i = 0; i < rwlock_class_max; i++) {
my_free(rwlock_class_array[i].m_documentation);
}
}
PFS_FREE_ARRAY(&builtin_memory_rwlock_class, rwlock_class_max,
sizeof(PFS_rwlock_class), rwlock_class_array);
rwlock_class_array = nullptr;
rwlock_class_dirty_count = rwlock_class_allocated_count = rwlock_class_max =
0;
if (cond_class_array != nullptr) {
for (i = 0; i < cond_class_max; i++) {
my_free(cond_class_array[i].m_documentation);
}
}
PFS_FREE_ARRAY(&builtin_memory_cond_class, cond_class_max,
sizeof(PFS_cond_class), cond_class_array);
cond_class_array = nullptr;
cond_class_dirty_count = cond_class_allocated_count = cond_class_max = 0;
}
/**
Initialize the thread class buffer.
@param thread_class_sizing max number of thread class
@return 0 on success
*/
int init_thread_class(uint thread_class_sizing) {
int result = 0;
thread_class_dirty_count = thread_class_allocated_count = 0;
thread_class_max = thread_class_sizing;
thread_class_lost = 0;
if (thread_class_max > 0) {
thread_class_array = PFS_MALLOC_ARRAY(
&builtin_memory_thread_class, thread_class_max,
sizeof(PFS_thread_class), PFS_thread_class, MYF(MY_ZEROFILL));
if (unlikely(thread_class_array == nullptr)) {
thread_class_max = 0;
result = 1;
}
} else {
thread_class_array = nullptr;
}
return result;
}
/** Cleanup the thread class buffers. */
void cleanup_thread_class() {
if (thread_class_array != nullptr) {
for (unsigned int i = 0; i < thread_class_max; i++) {
my_free(thread_class_array[i].m_documentation);
}
}
PFS_FREE_ARRAY(&builtin_memory_thread_class, thread_class_max,
sizeof(PFS_thread_class), thread_class_array);
thread_class_array = nullptr;
thread_class_dirty_count = thread_class_allocated_count = 0;
thread_class_max = 0;
}
/**
Initialize the table share buffer.
@param table_share_sizing max number of table share
@return 0 on success
*/
int init_table_share(uint table_share_sizing) {
if (global_table_share_container.init(table_share_sizing)) {
return 1;
}
return 0;
}
/**
Initialize the meter class buffer.
@param meter_class_sizing max number of meter class
@return 0 on success
*/
int init_meter_class(uint meter_class_sizing) {
int result = 0;
meter_class_dirty_count = meter_class_allocated_count = 0;
meter_class_max = meter_class_sizing;
meter_class_lost = 0;
if (meter_class_max > 0) {
meter_class_array = PFS_MALLOC_ARRAY(
&builtin_memory_meter_class, meter_class_max, sizeof(PFS_meter_class),
PFS_meter_class, MYF(MY_ZEROFILL));
if (unlikely(meter_class_array == nullptr)) {
meter_class_max = 0;
result = 1;
}
} else {
meter_class_array = nullptr;
}
return result;
}
/** Cleanup the meter class buffers. */
void cleanup_meter_class() {
if (meter_class_array != nullptr) {
for (unsigned int i = 0; i < meter_class_max; i++) {
my_free(meter_class_array[i].m_documentation);
PFS_FREE_ARRAY(&builtin_memory_meter_class, metric_class_max,
sizeof(PFS_metric_key), meter_class_array[i].m_metrics);
meter_class_array[i].m_metrics = nullptr;
meter_class_array[i].m_metrics_size = 0;
}
}
PFS_FREE_ARRAY(&builtin_memory_meter_class, meter_class_max,
sizeof(PFS_meter_class), meter_class_array);
meter_class_array = nullptr;
meter_class_dirty_count = meter_class_allocated_count = 0;
meter_class_max = 0;
}
/**
Initialize the metric class buffer.
@param metric_class_sizing max number of metric class
@return 0 on success
*/
int init_metric_class(uint metric_class_sizing) {
int result = 0;
metric_class_dirty_count = metric_class_allocated_count = 0;
metric_class_max = metric_class_sizing;
metric_class_lost = 0;
if (metric_class_max > 0) {
metric_class_array = PFS_MALLOC_ARRAY(
&builtin_memory_metric_class, metric_class_max,
sizeof(PFS_metric_class), PFS_metric_class, MYF(MY_ZEROFILL));
if (unlikely(metric_class_array == nullptr)) {
metric_class_max = 0;
result = 1;
}
} else {
metric_class_array = nullptr;
}
return result;
}
/** Cleanup the metric class buffers. */
void cleanup_metric_class() {
if (metric_class_array != nullptr) {
for (unsigned int i = 0; i < metric_class_max; i++) {
my_free(metric_class_array[i].m_documentation);
}
}
PFS_FREE_ARRAY(&builtin_memory_metric_class, metric_class_max,
sizeof(PFS_metric_class), metric_class_array);
metric_class_array = nullptr;
metric_class_dirty_count = metric_class_allocated_count = 0;
metric_class_max = 0;
}
/**
Initialize the logger class buffer.
@param logger_class_sizing max number of logger class
@return 0 on success
*/
int init_logger_class(uint logger_class_sizing) {
int result = 0;
logger_class_dirty_count = logger_class_allocated_count = 0;
logger_class_max = logger_class_sizing;
logger_class_lost = 0;
if (logger_class_max > 0) {
logger_class_array = PFS_MALLOC_ARRAY(
&builtin_memory_logger_class, logger_class_max,
sizeof(PFS_logger_class), PFS_logger_class, MYF(MY_ZEROFILL));
if (unlikely(logger_class_array == nullptr)) {
logger_class_max = 0;
result = 1;
}
} else {
logger_class_array = nullptr;
}
return result;
}
/** Cleanup the logger class buffers. */
void cleanup_logger_class() {
PFS_FREE_ARRAY(&builtin_memory_logger_class, logger_class_max,
sizeof(PFS_logger_class), logger_class_array);
logger_class_array = nullptr;
logger_class_dirty_count = logger_class_allocated_count = 0;
logger_class_max = 0;
}
/** Cleanup the table share buffers. */
void cleanup_table_share() { global_table_share_container.cleanup(); }
/** get_key function for @c table_share_hash. */
static const uchar *table_share_hash_get_key(const uchar *entry,
size_t *length) {
const PFS_table_share *const *typed_entry;
const PFS_table_share *share;
const void *result;
typed_entry = reinterpret_cast<const PFS_table_share *const *>(entry);
assert(typed_entry != nullptr);
share = *typed_entry;
assert(share != nullptr);
*length = sizeof(share->m_key);
result = &share->m_key;
return reinterpret_cast<const uchar *>(result);
}
static uint table_share_hash_func(const LF_HASH *, const uchar *key,
size_t key_len [[maybe_unused]]) {
const PFS_table_share_key *share_key;
uint64 nr1;
uint64 nr2;
assert(key_len == sizeof(PFS_table_share_key));
share_key = reinterpret_cast<const PFS_table_share_key *>(key);
assert(share_key != nullptr);
nr1 = share_key->m_type;
nr2 = 0;
share_key->m_schema_name.hash(&nr1, &nr2);
share_key->m_table_name.hash(&nr1, &nr2);
return nr1;
}
static int table_share_hash_cmp_func(const uchar *key1,
size_t key_len1 [[maybe_unused]],
const uchar *key2,
size_t key_len2 [[maybe_unused]]) {
const PFS_table_share_key *share_key1;
const PFS_table_share_key *share_key2;
int cmp;
assert(key_len1 == sizeof(PFS_table_share_key));
assert(key_len2 == sizeof(PFS_table_share_key));
share_key1 = reinterpret_cast<const PFS_table_share_key *>(key1);
share_key2 = reinterpret_cast<const PFS_table_share_key *>(key2);
assert(share_key1 != nullptr);
assert(share_key2 != nullptr);
if (share_key1->m_type > share_key2->m_type) {
return +1;
}
if (share_key1->m_type < share_key2->m_type) {
return -1;
}
cmp = share_key1->m_schema_name.sort(&share_key2->m_schema_name);
if (cmp != 0) {
return cmp;
}
cmp = share_key1->m_table_name.sort(&share_key2->m_table_name);
return cmp;
}
/** Initialize the table share hash table. */
int init_table_share_hash(const PFS_global_param *param) {
if ((!table_share_hash_inited) && (param->m_table_share_sizing != 0)) {
lf_hash_init3(&table_share_hash, sizeof(PFS_table_share *), LF_HASH_UNIQUE,
table_share_hash_get_key, table_share_hash_func,
table_share_hash_cmp_func, nullptr, nullptr, nullptr);
table_share_hash_inited = true;
}
return 0;
}
/** Cleanup the table share hash table. */
void cleanup_table_share_hash() {
if (table_share_hash_inited) {
lf_hash_destroy(&table_share_hash);
table_share_hash_inited = false;
}
}
/**
Get the hash pins for @sa table_share_hash.
@param thread The running thread.
@returns The LF_HASH pins for the thread.
*/
static LF_PINS *get_table_share_hash_pins(PFS_thread *thread) {
if (unlikely(thread->m_table_share_hash_pins == nullptr)) {
if (!table_share_hash_inited) {
return nullptr;
}
thread->m_table_share_hash_pins = lf_hash_get_pins(&table_share_hash);
}
return thread->m_table_share_hash_pins;
}
/**
Set a table share hash key.
@param [out] key The key to populate.
@param temporary True for TEMPORARY TABLE.
@param schema_name The table schema name.
@param schema_name_length The table schema name length.
@param table_name The table name.
@param table_name_length The table name length.
*/
static void set_table_share_key(PFS_table_share_key *key, bool temporary,
const char *schema_name,
size_t schema_name_length,
const char *table_name,
size_t table_name_length) {
assert(schema_name_length <= NAME_LEN);
assert(table_name_length <= NAME_LEN);
key->m_type = (temporary ? OBJECT_TYPE_TEMPORARY_TABLE : OBJECT_TYPE_TABLE);
key->m_schema_name.set(schema_name, schema_name_length);
key->m_table_name.set(table_name, table_name_length);
}
/**
Find an existing table share lock instrumentation.
@return a table share lock.
*/
PFS_table_share_lock *PFS_table_share::find_lock_stat() const {
const auto *that = const_cast<PFS_table_share *>(this);
return that->m_race_lock_stat.load();
}
/**
Find or create a table share lock instrumentation.
@return a table share lock, or NULL.
*/
PFS_table_share_lock *PFS_table_share::find_or_create_lock_stat() {
PFS_table_share_lock *pfs = this->m_race_lock_stat.load();
if (pfs != nullptr) {
return pfs;
}
/* (2) Create a lock stat */
PFS_table_share_lock *new_pfs = create_table_share_lock_stat();
if (new_pfs == nullptr) {
return nullptr;
}
new_pfs->m_owner = this;
/* (3) Atomic CAS */
if (atomic_compare_exchange_strong(&this->m_race_lock_stat, &pfs, new_pfs)) {
/* Ok. */
return new_pfs;
}
/* Collision with another thread that also executed (2) and (3). */
release_table_share_lock_stat(new_pfs);
return pfs;
}
/** Destroy a table share lock instrumentation. */
void PFS_table_share::destroy_lock_stat() {
PFS_table_share_lock *new_ptr = nullptr;
PFS_table_share_lock *old_ptr = this->m_race_lock_stat.exchange(new_ptr);
if (old_ptr != nullptr) {
release_table_share_lock_stat(old_ptr);
}
}
/**
Find an existing table share index instrumentation.
@return a table share index
*/
PFS_table_share_index *PFS_table_share::find_index_stat(uint index) const {
assert(index <= MAX_INDEXES);
return this->m_race_index_stat[index].load();
}
/**
Find or create a table share index instrumentation.
@param server_share the server TABLE_SHARE structure
@param index the index
@return a table share index, or NULL
*/
PFS_table_share_index *PFS_table_share::find_or_create_index_stat(
const TABLE_SHARE *server_share, uint index) {
assert(index <= MAX_INDEXES);
/* (1) Atomic Load */
PFS_table_share_index *pfs = this->m_race_index_stat[index].load();
if (pfs != nullptr) {
return pfs;
}
/* (2) Create an index stat */
PFS_table_share_index *new_pfs =
create_table_share_index_stat(server_share, index);
if (new_pfs == nullptr) {
return nullptr;
}
new_pfs->m_owner = this;
/* (3) Atomic CAS */
if (atomic_compare_exchange_strong(&this->m_race_index_stat[index], &pfs,
new_pfs)) {
/* Ok. */
return new_pfs;
}
/* Collision with another thread that also executed (2) and (3). */
release_table_share_index_stat(new_pfs);
return pfs;
}
/** Destroy table share index instrumentation. */
void PFS_table_share::destroy_index_stats() {
for (uint index = 0; index <= MAX_INDEXES; index++) {
PFS_table_share_index *new_ptr = nullptr;
PFS_table_share_index *old_ptr =
this->m_race_index_stat[index].exchange(new_ptr);
if (old_ptr != nullptr) {
release_table_share_index_stat(old_ptr);
}
}
}
void PFS_table_share::refresh_setup_object_flags(PFS_thread *thread) {
lookup_setup_object_table(thread, OBJECT_TYPE_TABLE, &m_key.m_schema_name,
&m_key.m_table_name, &m_enabled, &m_timed);
}
/**
Initialize the table lock stat buffer.
@param table_stat_sizing max number of table lock statistics
@return 0 on success
*/
int init_table_share_lock_stat(uint table_stat_sizing) {
if (global_table_share_lock_container.init(table_stat_sizing)) {
return 1;
}
return 0;
}
/**
Create a table share lock instrumentation.
@return table share lock instrumentation, or NULL
*/
PFS_table_share_lock *create_table_share_lock_stat() {
pfs_dirty_state dirty_state;
/* Create a new record in table stat array. */
PFS_table_share_lock *pfs =
global_table_share_lock_container.allocate(&dirty_state);
if (pfs != nullptr) {
/* Reset the stats. */
pfs->m_stat.reset();
/* Use this stat buffer. */
pfs->m_lock.dirty_to_allocated(&dirty_state);
}
return pfs;
}
/** Release a table share lock instrumentation. */
void release_table_share_lock_stat(PFS_table_share_lock *pfs) {
pfs->m_owner = nullptr;
global_table_share_lock_container.deallocate(pfs);
}
/** Cleanup the table stat buffers. */
void cleanup_table_share_lock_stat() {
global_table_share_lock_container.cleanup();
}
/**
Initialize table index stat buffer.
@param index_stat_sizing max number of index statistics
@return 0 on success
*/
int init_table_share_index_stat(uint index_stat_sizing) {
if (global_table_share_index_container.init(index_stat_sizing)) {
return 1;
}
return 0;
}
/**
Create a table share index instrumentation.
@return table share index instrumentation, or NULL
*/
PFS_table_share_index *create_table_share_index_stat(
const TABLE_SHARE *server_share, uint server_index) {
assert((server_share != nullptr) || (server_index == MAX_INDEXES));
pfs_dirty_state dirty_state;
/* Create a new record in index stat array. */
PFS_table_share_index *pfs =
global_table_share_index_container.allocate(&dirty_state);
if (pfs != nullptr) {
if (server_index == MAX_INDEXES) {
pfs->m_key.m_name_length = 0;
} else {
const KEY *key_info = server_share->key_info + server_index;
const size_t len = strlen(key_info->name);
memcpy(pfs->m_key.m_name, key_info->name, len);
pfs->m_key.m_name_length = len;
}
/* Reset the stats. */
pfs->m_stat.reset();
/* Use this stat buffer. */
pfs->m_lock.dirty_to_allocated(&dirty_state);
}
return pfs;
}
/** Release a table share index instrumentation. */
void release_table_share_index_stat(PFS_table_share_index *pfs) {
pfs->m_owner = nullptr;
global_table_share_index_container.deallocate(pfs);
}
/** Cleanup the table stat buffers. */
void cleanup_table_share_index_stat() {
global_table_share_index_container.cleanup();
}
/**
Initialize the file class buffer.
@param file_class_sizing max number of file class
@return 0 on success
*/
int init_file_class(uint file_class_sizing) {
file_class_dirty_count = file_class_allocated_count = 0;
file_class_max = file_class_sizing;
file_class_lost = 0;
if (file_class_max > 0) {
file_class_array = PFS_MALLOC_ARRAY(&builtin_memory_file_class,
file_class_max, sizeof(PFS_file_class),