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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
|
/* Copyright (c) 2008, 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, Fifth Floor, Boston, MA 02110-1335 USA */
#ifndef PFS_STAT_H
#define PFS_STAT_H
#include <algorithm>
#include "sql_const.h"
/* memcpy */
#include "string.h"
/**
@file storage/perfschema/pfs_stat.h
Statistics (declarations).
*/
/**
@addtogroup Performance_schema_buffers
@{
*/
/** Single statistic. */
struct PFS_single_stat
{
/** Count of values. */
ulonglong m_count;
/** Sum of values. */
ulonglong m_sum;
/** Minimum value. */
ulonglong m_min;
/** Maximum value. */
ulonglong m_max;
PFS_single_stat()
{
m_count= 0;
m_sum= 0;
m_min= ULLONG_MAX;
m_max= 0;
}
inline void reset(void)
{
m_count= 0;
m_sum= 0;
m_min= ULLONG_MAX;
m_max= 0;
}
inline bool has_timed_stats() const
{
return (m_min <= m_max);
}
inline void aggregate(const PFS_single_stat *stat)
{
if (stat->m_count != 0)
{
m_count+= stat->m_count;
m_sum+= stat->m_sum;
if (unlikely(m_min > stat->m_min))
m_min= stat->m_min;
if (unlikely(m_max < stat->m_max))
m_max= stat->m_max;
}
}
inline void aggregate_no_check(const PFS_single_stat *stat)
{
m_count+= stat->m_count;
m_sum+= stat->m_sum;
if (unlikely(m_min > stat->m_min))
m_min= stat->m_min;
if (unlikely(m_max < stat->m_max))
m_max= stat->m_max;
}
inline void aggregate_counted()
{
m_count++;
}
inline void aggregate_counted(ulonglong count)
{
m_count+= count;
}
inline void aggregate_value(ulonglong value)
{
m_count++;
m_sum+= value;
if (unlikely(m_min > value))
m_min= value;
if (unlikely(m_max < value))
m_max= value;
}
inline void aggregate_many_value(ulonglong value, ulonglong count)
{
m_count+= count;
m_sum+= value;
if (unlikely(m_min > value))
m_min= value;
if (unlikely(m_max < value))
m_max= value;
}
};
/** Combined statistic. */
struct PFS_byte_stat : public PFS_single_stat
{
/** Byte count statistics */
ulonglong m_bytes;
/** Aggregate wait stats, event count and byte count */
inline void aggregate(const PFS_byte_stat *stat)
{
if (stat->m_count != 0)
{
PFS_single_stat::aggregate_no_check(stat);
m_bytes+= stat->m_bytes;
}
}
/** Aggregate wait stats, event count and byte count */
inline void aggregate_no_check(const PFS_byte_stat *stat)
{
PFS_single_stat::aggregate_no_check(stat);
m_bytes+= stat->m_bytes;
}
/** Aggregate individual wait time, event count and byte count */
inline void aggregate(ulonglong wait, ulonglong bytes)
{
aggregate_value(wait);
m_bytes+= bytes;
}
/** Aggregate wait stats and event count */
inline void aggregate_waits(const PFS_byte_stat *stat)
{
PFS_single_stat::aggregate(stat);
}
/** Aggregate event count and byte count */
inline void aggregate_counted()
{
PFS_single_stat::aggregate_counted();
}
/** Aggregate event count and byte count */
inline void aggregate_counted(ulonglong bytes)
{
PFS_single_stat::aggregate_counted();
m_bytes+= bytes;
}
PFS_byte_stat()
{
reset();
}
inline void reset(void)
{
PFS_single_stat::reset();
m_bytes= 0;
}
};
/** Statistics for mutex usage. */
struct PFS_mutex_stat
{
/** Wait statistics. */
PFS_single_stat m_wait_stat;
#ifdef PFS_LATER
/**
Lock statistics.
This statistic is not exposed in user visible tables yet.
*/
PFS_single_stat m_lock_stat;
#endif
inline void aggregate(const PFS_mutex_stat *stat)
{
m_wait_stat.aggregate(&stat->m_wait_stat);
#ifdef PFS_LATER
m_lock_stat.aggregate(&stat->m_lock_stat);
#endif
}
inline void reset(void)
{
m_wait_stat.reset();
#ifdef PFS_LATER
m_lock_stat.reset();
#endif
}
};
/** Statistics for rwlock usage. */
struct PFS_rwlock_stat
{
/** Wait statistics. */
PFS_single_stat m_wait_stat;
#ifdef PFS_LATER
/**
RWLock read lock usage statistics.
This statistic is not exposed in user visible tables yet.
*/
PFS_single_stat m_read_lock_stat;
/**
RWLock write lock usage statistics.
This statistic is not exposed in user visible tables yet.
*/
PFS_single_stat m_write_lock_stat;
#endif
inline void aggregate(const PFS_rwlock_stat *stat)
{
m_wait_stat.aggregate(&stat->m_wait_stat);
#ifdef PFS_LATER
m_read_lock_stat.aggregate(&stat->m_read_lock_stat);
m_write_lock_stat.aggregate(&stat->m_write_lock_stat);
#endif
}
inline void reset(void)
{
m_wait_stat.reset();
#ifdef PFS_LATER
m_read_lock_stat.reset();
m_write_lock_stat.reset();
#endif
}
};
/** Statistics for COND usage. */
struct PFS_cond_stat
{
/** Wait statistics. */
PFS_single_stat m_wait_stat;
#ifdef PFS_LATER
/**
Number of times a condition was signalled.
This statistic is not exposed in user visible tables yet.
*/
ulonglong m_signal_count;
/**
Number of times a condition was broadcast.
This statistic is not exposed in user visible tables yet.
*/
ulonglong m_broadcast_count;
#endif
inline void aggregate(const PFS_cond_stat *stat)
{
m_wait_stat.aggregate(&stat->m_wait_stat);
#ifdef PFS_LATER
m_signal_count+= stat->m_signal_count;
m_broadcast_count+= stat->m_broadcast_count;
#endif
}
inline void reset(void)
{
m_wait_stat.reset();
#ifdef PFS_LATER
m_signal_count= 0;
m_broadcast_count= 0;
#endif
}
};
/** Statistics for FILE IO. Used for both waits and byte counts. */
struct PFS_file_io_stat
{
/** READ statistics */
PFS_byte_stat m_read;
/** WRITE statistics */
PFS_byte_stat m_write;
/** Miscellaneous statistics */
PFS_byte_stat m_misc;
inline void reset(void)
{
m_read.reset();
m_write.reset();
m_misc.reset();
}
inline void aggregate(const PFS_file_io_stat *stat)
{
m_read.aggregate(&stat->m_read);
m_write.aggregate(&stat->m_write);
m_misc.aggregate(&stat->m_misc);
}
/* Sum waits and byte counts */
inline void sum(PFS_byte_stat *stat)
{
stat->aggregate(&m_read);
stat->aggregate(&m_write);
stat->aggregate(&m_misc);
}
/* Sum waits only */
inline void sum_waits(PFS_single_stat *stat)
{
stat->aggregate(&m_read);
stat->aggregate(&m_write);
stat->aggregate(&m_misc);
}
};
/** Statistics for FILE usage. */
struct PFS_file_stat
{
/** Number of current open handles. */
ulong m_open_count;
/** File IO statistics. */
PFS_file_io_stat m_io_stat;
inline void aggregate(const PFS_file_stat *stat)
{
m_io_stat.aggregate(&stat->m_io_stat);
}
/** Reset file statistics. */
inline void reset(void)
{
m_io_stat.reset();
}
};
/** Statistics for stage usage. */
struct PFS_stage_stat
{
PFS_single_stat m_timer1_stat;
inline void reset(void)
{ m_timer1_stat.reset(); }
inline void aggregate_counted()
{ m_timer1_stat.aggregate_counted(); }
inline void aggregate_value(ulonglong value)
{ m_timer1_stat.aggregate_value(value); }
inline void aggregate(const PFS_stage_stat *stat)
{ m_timer1_stat.aggregate(& stat->m_timer1_stat); }
};
/** Statistics for stored program usage. */
struct PFS_sp_stat
{
PFS_single_stat m_timer1_stat;
inline void reset(void)
{ m_timer1_stat.reset(); }
inline void aggregate_counted()
{ m_timer1_stat.aggregate_counted(); }
inline void aggregate_value(ulonglong value)
{ m_timer1_stat.aggregate_value(value); }
inline void aggregate(const PFS_stage_stat *stat)
{ m_timer1_stat.aggregate(& stat->m_timer1_stat); }
};
/** Statistics for prepared statement usage. */
struct PFS_prepared_stmt_stat
{
PFS_single_stat m_timer1_stat;
inline void reset(void)
{ m_timer1_stat.reset(); }
inline void aggregate_counted()
{ m_timer1_stat.aggregate_counted(); }
inline void aggregate_value(ulonglong value)
{ m_timer1_stat.aggregate_value(value); }
inline void aggregate(PFS_stage_stat *stat)
{ m_timer1_stat.aggregate(& stat->m_timer1_stat); }
};
/**
Statistics for statement usage.
This structure uses lazy initialization,
controlled by member @c m_timer1_stat.m_count.
*/
struct PFS_statement_stat
{
PFS_single_stat m_timer1_stat;
ulonglong m_error_count;
ulonglong m_warning_count;
ulonglong m_rows_affected;
ulonglong m_lock_time;
ulonglong m_rows_sent;
ulonglong m_rows_examined;
ulonglong m_created_tmp_disk_tables;
ulonglong m_created_tmp_tables;
ulonglong m_select_full_join;
ulonglong m_select_full_range_join;
ulonglong m_select_range;
ulonglong m_select_range_check;
ulonglong m_select_scan;
ulonglong m_sort_merge_passes;
ulonglong m_sort_range;
ulonglong m_sort_rows;
ulonglong m_sort_scan;
ulonglong m_no_index_used;
ulonglong m_no_good_index_used;
PFS_statement_stat()
{
reset();
}
inline void reset()
{
m_timer1_stat.m_count= 0;
}
inline void mark_used()
{
delayed_reset();
}
private:
inline void delayed_reset(void)
{
if (m_timer1_stat.m_count == 0)
{
m_timer1_stat.reset();
m_error_count= 0;
m_warning_count= 0;
m_rows_affected= 0;
m_lock_time= 0;
m_rows_sent= 0;
m_rows_examined= 0;
m_created_tmp_disk_tables= 0;
m_created_tmp_tables= 0;
m_select_full_join= 0;
m_select_full_range_join= 0;
m_select_range= 0;
m_select_range_check= 0;
m_select_scan= 0;
m_sort_merge_passes= 0;
m_sort_range= 0;
m_sort_rows= 0;
m_sort_scan= 0;
m_no_index_used= 0;
m_no_good_index_used= 0;
}
}
public:
inline void aggregate_counted()
{
delayed_reset();
m_timer1_stat.aggregate_counted();
}
inline void aggregate_value(ulonglong value)
{
delayed_reset();
m_timer1_stat.aggregate_value(value);
}
inline void aggregate(const PFS_statement_stat *stat)
{
if (stat->m_timer1_stat.m_count != 0)
{
delayed_reset();
m_timer1_stat.aggregate_no_check(& stat->m_timer1_stat);
m_error_count+= stat->m_error_count;
m_warning_count+= stat->m_warning_count;
m_rows_affected+= stat->m_rows_affected;
m_lock_time+= stat->m_lock_time;
m_rows_sent+= stat->m_rows_sent;
m_rows_examined+= stat->m_rows_examined;
m_created_tmp_disk_tables+= stat->m_created_tmp_disk_tables;
m_created_tmp_tables+= stat->m_created_tmp_tables;
m_select_full_join+= stat->m_select_full_join;
m_select_full_range_join+= stat->m_select_full_range_join;
m_select_range+= stat->m_select_range;
m_select_range_check+= stat->m_select_range_check;
m_select_scan+= stat->m_select_scan;
m_sort_merge_passes+= stat->m_sort_merge_passes;
m_sort_range+= stat->m_sort_range;
m_sort_rows+= stat->m_sort_rows;
m_sort_scan+= stat->m_sort_scan;
m_no_index_used+= stat->m_no_index_used;
m_no_good_index_used+= stat->m_no_good_index_used;
}
}
};
/** Statistics for transaction usage. */
struct PFS_transaction_stat
{
PFS_single_stat m_read_write_stat;
PFS_single_stat m_read_only_stat;
ulonglong m_savepoint_count;
ulonglong m_rollback_to_savepoint_count;
ulonglong m_release_savepoint_count;
PFS_transaction_stat()
{
m_savepoint_count= 0;
m_rollback_to_savepoint_count= 0;
m_release_savepoint_count= 0;
}
ulonglong count(void)
{
return (m_read_write_stat.m_count + m_read_only_stat.m_count);
}
inline void reset(void)
{
m_read_write_stat.reset();
m_read_only_stat.reset();
m_savepoint_count= 0;
m_rollback_to_savepoint_count= 0;
m_release_savepoint_count= 0;
}
inline void aggregate(const PFS_transaction_stat *stat)
{
m_read_write_stat.aggregate(&stat->m_read_write_stat);
m_read_only_stat.aggregate(&stat->m_read_only_stat);
m_savepoint_count+= stat->m_savepoint_count;
m_rollback_to_savepoint_count+= stat->m_rollback_to_savepoint_count;
m_release_savepoint_count+= stat->m_release_savepoint_count;
}
};
/** Single table io statistic. */
struct PFS_table_io_stat
{
bool m_has_data;
/** FETCH statistics */
PFS_single_stat m_fetch;
/** INSERT statistics */
PFS_single_stat m_insert;
/** UPDATE statistics */
PFS_single_stat m_update;
/** DELETE statistics */
PFS_single_stat m_delete;
PFS_table_io_stat()
{
m_has_data= false;
}
inline void reset(void)
{
m_has_data= false;
m_fetch.reset();
m_insert.reset();
m_update.reset();
m_delete.reset();
}
inline void aggregate(const PFS_table_io_stat *stat)
{
if (stat->m_has_data)
{
m_has_data= true;
m_fetch.aggregate(&stat->m_fetch);
m_insert.aggregate(&stat->m_insert);
m_update.aggregate(&stat->m_update);
m_delete.aggregate(&stat->m_delete);
}
}
inline void sum(PFS_single_stat *result)
{
if (m_has_data)
{
result->aggregate(& m_fetch);
result->aggregate(& m_insert);
result->aggregate(& m_update);
result->aggregate(& m_delete);
}
}
};
enum PFS_TL_LOCK_TYPE
{
/* Locks from enum thr_lock */
PFS_TL_READ= 0,
PFS_TL_READ_WITH_SHARED_LOCKS= 1,
PFS_TL_READ_HIGH_PRIORITY= 2,
PFS_TL_READ_NO_INSERT= 3,
PFS_TL_WRITE_ALLOW_WRITE= 4,
PFS_TL_WRITE_CONCURRENT_INSERT= 5,
PFS_TL_WRITE_DELAYED= 6,
PFS_TL_WRITE_LOW_PRIORITY= 7,
PFS_TL_WRITE= 8,
/* Locks for handler::ha_external_lock() */
PFS_TL_READ_EXTERNAL= 9,
PFS_TL_WRITE_EXTERNAL= 10,
PFS_TL_NONE= 99
};
#define COUNT_PFS_TL_LOCK_TYPE 11
/** Statistics for table locks. */
struct PFS_table_lock_stat
{
PFS_single_stat m_stat[COUNT_PFS_TL_LOCK_TYPE];
inline void reset(void)
{
PFS_single_stat *pfs= & m_stat[0];
PFS_single_stat *pfs_last= & m_stat[COUNT_PFS_TL_LOCK_TYPE];
for ( ; pfs < pfs_last ; pfs++)
pfs->reset();
}
inline void aggregate(const PFS_table_lock_stat *stat)
{
PFS_single_stat *pfs= & m_stat[0];
PFS_single_stat *pfs_last= & m_stat[COUNT_PFS_TL_LOCK_TYPE];
const PFS_single_stat *pfs_from= & stat->m_stat[0];
for ( ; pfs < pfs_last ; pfs++, pfs_from++)
pfs->aggregate(pfs_from);
}
inline void sum(PFS_single_stat *result)
{
PFS_single_stat *pfs= & m_stat[0];
PFS_single_stat *pfs_last= & m_stat[COUNT_PFS_TL_LOCK_TYPE];
for ( ; pfs < pfs_last ; pfs++)
result->aggregate(pfs);
}
};
/** Statistics for TABLE usage. */
struct PFS_table_stat
{
/**
Statistics, per index.
Each index stat is in [0, MAX_INDEXES-1],
stats when using no index are in [MAX_INDEXES].
*/
PFS_table_io_stat m_index_stat[MAX_INDEXES + 1];
/**
Statistics, per lock type.
*/
PFS_table_lock_stat m_lock_stat;
/** Reset table io statistic. */
inline void reset_io(void)
{
PFS_table_io_stat *stat= & m_index_stat[0];
PFS_table_io_stat *stat_last= & m_index_stat[MAX_INDEXES + 1];
for ( ; stat < stat_last ; stat++)
stat->reset();
}
/** Reset table lock statistic. */
inline void reset_lock(void)
{
m_lock_stat.reset();
}
/** Reset table statistic. */
inline void reset(void)
{
reset_io();
reset_lock();
}
inline void fast_reset_io(void)
{
memcpy(& m_index_stat, & g_reset_template.m_index_stat, sizeof(m_index_stat));
}
inline void fast_reset_lock(void)
{
memcpy(& m_lock_stat, & g_reset_template.m_lock_stat, sizeof(m_lock_stat));
}
inline void fast_reset(void)
{
memcpy(this, & g_reset_template, sizeof(*this));
}
inline void aggregate_io(const PFS_table_stat *stat, uint key_count)
{
PFS_table_io_stat *to_stat;
PFS_table_io_stat *to_stat_last;
const PFS_table_io_stat *from_stat;
assert(key_count <= MAX_INDEXES);
/* Aggregate stats for each index, if any */
to_stat= & m_index_stat[0];
to_stat_last= to_stat + key_count;
from_stat= & stat->m_index_stat[0];
for ( ; to_stat < to_stat_last ; from_stat++, to_stat++)
to_stat->aggregate(from_stat);
/* Aggregate stats for the table */
to_stat= & m_index_stat[MAX_INDEXES];
from_stat= & stat->m_index_stat[MAX_INDEXES];
to_stat->aggregate(from_stat);
}
inline void aggregate_lock(const PFS_table_stat *stat)
{
m_lock_stat.aggregate(& stat->m_lock_stat);
}
inline void aggregate(const PFS_table_stat *stat, uint key_count)
{
aggregate_io(stat, key_count);
aggregate_lock(stat);
}
inline void sum_io(PFS_single_stat *result, uint key_count)
{
PFS_table_io_stat *stat;
PFS_table_io_stat *stat_last;
assert(key_count <= MAX_INDEXES);
/* Sum stats for each index, if any */
stat= & m_index_stat[0];
stat_last= stat + key_count;
for ( ; stat < stat_last ; stat++)
stat->sum(result);
/* Sum stats for the table */
m_index_stat[MAX_INDEXES].sum(result);
}
inline void sum_lock(PFS_single_stat *result)
{
m_lock_stat.sum(result);
}
inline void sum(PFS_single_stat *result, uint key_count)
{
sum_io(result, key_count);
sum_lock(result);
}
static struct PFS_table_stat g_reset_template;
};
/** Statistics for SOCKET IO. Used for both waits and byte counts. */
struct PFS_socket_io_stat
{
/** READ statistics */
PFS_byte_stat m_read;
/** WRITE statistics */
PFS_byte_stat m_write;
/** Miscellaneous statistics */
PFS_byte_stat m_misc;
inline void reset(void)
{
m_read.reset();
m_write.reset();
m_misc.reset();
}
inline void aggregate(const PFS_socket_io_stat *stat)
{
m_read.aggregate(&stat->m_read);
m_write.aggregate(&stat->m_write);
m_misc.aggregate(&stat->m_misc);
}
/* Sum waits and byte counts */
inline void sum(PFS_byte_stat *stat)
{
stat->aggregate(&m_read);
stat->aggregate(&m_write);
stat->aggregate(&m_misc);
}
/* Sum waits only */
inline void sum_waits(PFS_single_stat *stat)
{
stat->aggregate(&m_read);
stat->aggregate(&m_write);
stat->aggregate(&m_misc);
}
};
/** Statistics for SOCKET usage. */
struct PFS_socket_stat
{
/** Socket timing and byte count statistics per operation */
PFS_socket_io_stat m_io_stat;
/** Reset socket statistics. */
inline void reset(void)
{
m_io_stat.reset();
}
};
struct PFS_memory_stat_delta
{
size_t m_alloc_count_delta;
size_t m_free_count_delta;
size_t m_alloc_size_delta;
size_t m_free_size_delta;
void reset()
{
m_alloc_count_delta= 0;
m_free_count_delta= 0;
m_alloc_size_delta= 0;
m_free_size_delta= 0;
}
};
/**
Memory statistics.
Conceptually, the following statistics are maintained:
- CURRENT_COUNT_USED,
- LOW_COUNT_USED,
- HIGH_COUNT_USED
- CURRENT_SIZE_USED,
- LOW_SIZE_USED,
- HIGH_SIZE_USED
Now, the implementation keeps different counters,
which are easier (less overhead) to maintain while
collecting statistics.
Invariants are as follows:
CURRENT_COUNT_USED = @c m_alloc_count - @c m_free_count
LOW_COUNT_USED + @c m_free_count_capacity = CURRENT_COUNT_USED
CURRENT_COUNT_USED + @c m_alloc_count_capacity = HIGH_COUNT_USED
CURRENT_SIZE_USED = @c m_alloc_size - @c m_free_size
LOW_SIZE_USED + @c m_free_size_capacity = CURRENT_SIZE_USED
CURRENT_SIZE_USED + @c m_alloc_size_capacity = HIGH_SIZE_USED
*/
struct PFS_memory_stat
{
bool m_used;
size_t m_alloc_count;
size_t m_free_count;
size_t m_alloc_size;
size_t m_free_size;
size_t m_alloc_count_capacity;
size_t m_free_count_capacity;
size_t m_alloc_size_capacity;
size_t m_free_size_capacity;
inline void reset(void)
{
m_used= false;
m_alloc_count= 0;
m_free_count= 0;
m_alloc_size= 0;
m_free_size= 0;
m_alloc_count_capacity= 0;
m_free_count_capacity= 0;
m_alloc_size_capacity= 0;
m_free_size_capacity= 0;
}
inline void rebase(void)
{
if (! m_used)
return;
size_t base;
base= std::min<size_t>(m_alloc_count, m_free_count);
m_alloc_count-= base;
m_free_count-= base;
base= std::min<size_t>(m_alloc_size, m_free_size);
m_alloc_size-= base;
m_free_size-= base;
m_alloc_count_capacity= 0;
m_free_count_capacity= 0;
m_alloc_size_capacity= 0;
m_free_size_capacity= 0;
}
inline void partial_aggregate_to(PFS_memory_stat *stat)
{
if (! m_used)
return;
size_t base;
stat->m_used= true;
base= std::min<size_t>(m_alloc_count, m_free_count);
if (base != 0)
{
stat->m_alloc_count+= base;
stat->m_free_count+= base;
m_alloc_count-= base;
m_free_count-= base;
}
base= std::min<size_t>(m_alloc_size, m_free_size);
if (base != 0)
{
stat->m_alloc_size+= base;
stat->m_free_size+= base;
m_alloc_size-= base;
m_free_size-= base;
}
stat->m_alloc_count_capacity+= m_alloc_count_capacity;
stat->m_free_count_capacity+= m_free_count_capacity;
stat->m_alloc_size_capacity+= m_alloc_size_capacity;
stat->m_free_size_capacity+= m_free_size_capacity;
m_alloc_count_capacity= 0;
m_free_count_capacity= 0;
m_alloc_size_capacity= 0;
m_free_size_capacity= 0;
}
inline void full_aggregate_to(PFS_memory_stat *stat) const
{
if (! m_used)
return;
stat->m_used= true;
stat->m_alloc_count+= m_alloc_count;
stat->m_free_count+= m_free_count;
stat->m_alloc_size+= m_alloc_size;
stat->m_free_size+= m_free_size;
stat->m_alloc_count_capacity+= m_alloc_count_capacity;
stat->m_free_count_capacity+= m_free_count_capacity;
stat->m_alloc_size_capacity+= m_alloc_size_capacity;
stat->m_free_size_capacity+= m_free_size_capacity;
}
inline void partial_aggregate_to(PFS_memory_stat *stat1, PFS_memory_stat *stat2)
{
if (! m_used)
return;
size_t base;
stat1->m_used= true;
stat2->m_used= true;
base= std::min<size_t>(m_alloc_count, m_free_count);
if (base != 0)
{
stat1->m_alloc_count+= base;
stat2->m_alloc_count+= base;
stat1->m_free_count+= base;
stat2->m_free_count+= base;
m_alloc_count-= base;
m_free_count-= base;
}
base= std::min<size_t>(m_alloc_size, m_free_size);
if (base != 0)
{
stat1->m_alloc_size+= base;
stat2->m_alloc_size+= base;
stat1->m_free_size+= base;
stat2->m_free_size+= base;
m_alloc_size-= base;
m_free_size-= base;
}
stat1->m_alloc_count_capacity+= m_alloc_count_capacity;
stat2->m_alloc_count_capacity+= m_alloc_count_capacity;
stat1->m_free_count_capacity+= m_free_count_capacity;
stat2->m_free_count_capacity+= m_free_count_capacity;
stat1->m_alloc_size_capacity+= m_alloc_size_capacity;
stat2->m_alloc_size_capacity+= m_alloc_size_capacity;
stat1->m_free_size_capacity+= m_free_size_capacity;
stat2->m_free_size_capacity+= m_free_size_capacity;
m_alloc_count_capacity= 0;
m_free_count_capacity= 0;
m_alloc_size_capacity= 0;
m_free_size_capacity= 0;
}
inline void full_aggregate_to(PFS_memory_stat *stat1, PFS_memory_stat *stat2) const
{
if (! m_used)
return;
stat1->m_used= true;
stat2->m_used= true;
stat1->m_alloc_count+= m_alloc_count;
stat2->m_alloc_count+= m_alloc_count;
stat1->m_free_count+= m_free_count;
stat2->m_free_count+= m_free_count;
stat1->m_alloc_size+= m_alloc_size;
stat2->m_alloc_size+= m_alloc_size;
stat1->m_free_size+= m_free_size;
stat2->m_free_size+= m_free_size;
stat1->m_alloc_count_capacity+= m_alloc_count_capacity;
stat2->m_alloc_count_capacity+= m_alloc_count_capacity;
stat1->m_free_count_capacity+= m_free_count_capacity;
stat2->m_free_count_capacity+= m_free_count_capacity;
stat1->m_alloc_size_capacity+= m_alloc_size_capacity;
stat2->m_alloc_size_capacity+= m_alloc_size_capacity;
stat1->m_free_size_capacity+= m_free_size_capacity;
stat2->m_free_size_capacity+= m_free_size_capacity;
}
void count_builtin_alloc(size_t size)
{
m_used= true;
m_alloc_count++;
m_free_count_capacity++;
m_alloc_size+= size;
m_free_size_capacity+= size;
if (m_alloc_count_capacity >= 1)
{
m_alloc_count_capacity--;
}
if (m_alloc_size_capacity >= size)
{
m_alloc_size_capacity-= size;
}
return;
}
void count_builtin_free(size_t size)
{
m_used= true;
m_free_count++;
m_alloc_count_capacity++;
m_free_size+= size;
m_alloc_size_capacity+= size;
if (m_free_count_capacity >= 1)
{
m_free_count_capacity--;
}
if (m_free_size_capacity >= size)
{
m_free_size_capacity-= size;
}
return;
}
inline PFS_memory_stat_delta *count_alloc(size_t size,
PFS_memory_stat_delta *delta)
{
m_used= true;
m_alloc_count++;
m_free_count_capacity++;
m_alloc_size+= size;
m_free_size_capacity+= size;
if ((m_alloc_count_capacity >= 1) &&
(m_alloc_size_capacity >= size))
{
m_alloc_count_capacity--;
m_alloc_size_capacity-= size;
return NULL;
}
delta->reset();
if (m_alloc_count_capacity >= 1)
{
m_alloc_count_capacity--;
}
else
{
delta->m_alloc_count_delta= 1;
}
if (m_alloc_size_capacity >= size)
{
m_alloc_size_capacity-= size;
}
else
{
delta->m_alloc_size_delta= size - m_alloc_size_capacity;
m_alloc_size_capacity= 0;
}
return delta;
}
inline PFS_memory_stat_delta *count_realloc(size_t old_size, size_t new_size,
PFS_memory_stat_delta *delta)
{
m_used= true;
size_t size_delta= new_size - old_size;
m_alloc_count++;
m_alloc_size+= new_size;
m_free_count++;
m_free_size+= old_size;
if (new_size == old_size)
{
return NULL;
}
if (new_size > old_size)
{
/* Growing */
size_delta= new_size - old_size;
m_free_size_capacity+= size_delta;
if (m_alloc_size_capacity >= size_delta)
{
m_alloc_size_capacity-= size_delta;
return NULL;
}
delta->reset();
delta->m_alloc_size_delta= size_delta - m_alloc_size_capacity;
m_alloc_size_capacity= 0;
}
else
{
/* Shrinking */
size_delta= old_size - new_size;
m_alloc_size_capacity+= size_delta;
if (m_free_size_capacity >= size_delta)
{
m_free_size_capacity-= size_delta;
return NULL;
}
delta->reset();
delta->m_free_size_delta= size_delta - m_free_size_capacity;
m_free_size_capacity= 0;
}
return delta;
}
inline PFS_memory_stat_delta *count_free(size_t size, PFS_memory_stat_delta *delta)
{
m_used= true;
m_free_count++;
m_alloc_count_capacity++;
m_free_size+= size;
m_alloc_size_capacity+= size;
if ((m_free_count_capacity >= 1) &&
(m_free_size_capacity >= size))
{
m_free_count_capacity--;
m_free_size_capacity-= size;
return NULL;
}
delta->reset();
if (m_free_count_capacity >= 1)
{
m_free_count_capacity--;
}
else
{
delta->m_free_count_delta= 1;
}
if (m_free_size_capacity >= size)
{
m_free_size_capacity-= size;
}
else
{
delta->m_free_size_delta= size - m_free_size_capacity;
m_free_size_capacity= 0;
}
return delta;
}
inline PFS_memory_stat_delta *apply_delta(const PFS_memory_stat_delta *delta,
PFS_memory_stat_delta *delta_buffer)
{
size_t val;
size_t remaining_alloc_count;
size_t remaining_alloc_size;
size_t remaining_free_count;
size_t remaining_free_size;
bool has_remaining= false;
m_used= true;
val= delta->m_alloc_count_delta;
if (val <= m_alloc_count_capacity)
{
m_alloc_count_capacity-= val;
remaining_alloc_count= 0;
}
else
{
remaining_alloc_count= val - m_alloc_count_capacity;
m_alloc_count_capacity= 0;
has_remaining= true;
}
val= delta->m_alloc_size_delta;
if (val <= m_alloc_size_capacity)
{
m_alloc_size_capacity-= val;
remaining_alloc_size= 0;
}
else
{
remaining_alloc_size= val - m_alloc_size_capacity;
m_alloc_size_capacity= 0;
has_remaining= true;
}
val= delta->m_free_count_delta;
if (val <= m_free_count_capacity)
{
m_free_count_capacity-= val;
remaining_free_count= 0;
}
else
{
remaining_free_count= val - m_free_count_capacity;
m_free_count_capacity= 0;
has_remaining= true;
}
val= delta->m_free_size_delta;
if (val <= m_free_size_capacity)
{
m_free_size_capacity-= val;
remaining_free_size= 0;
}
else
{
remaining_free_size= val - m_free_size_capacity;
m_free_size_capacity= 0;
has_remaining= true;
}
if (! has_remaining)
return NULL;
delta_buffer->m_alloc_count_delta= remaining_alloc_count;
delta_buffer->m_alloc_size_delta= remaining_alloc_size;
delta_buffer->m_free_count_delta= remaining_free_count;
delta_buffer->m_free_size_delta= remaining_free_size;
return delta_buffer;
}
};
#define PFS_MEMORY_STAT_INITIALIZER { false, 0, 0, 0, 0, 0, 0, 0, 0}
/** Connections statistics. */
struct PFS_connection_stat
{
PFS_connection_stat()
: m_current_connections(0),
m_total_connections(0)
{}
ulonglong m_current_connections;
ulonglong m_total_connections;
inline void aggregate_active(ulonglong active)
{
m_current_connections+= active;
m_total_connections+= active;
}
inline void aggregate_disconnected(ulonglong disconnected)
{
m_total_connections+= disconnected;
}
};
/** @} */
#endif
|