forked from trdtnguyen/mysql-plnvm
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathha_ndb_index_stat.cc
3004 lines (2694 loc) · 79 KB
/
ha_ndb_index_stat.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) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
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 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
*/
#include "ha_ndbcluster_glue.h"
#include "ha_ndbcluster.h"
#include "ha_ndb_index_stat.h"
#include <mysql/plugin.h>
#include <ctype.h>
/* from other files */
extern struct st_ndb_status g_ndb_status;
extern native_mutex_t ndbcluster_mutex;
// Implementation still uses its own instance
extern Ndb_index_stat_thread ndb_index_stat_thread;
/* Implemented in ha_ndbcluster.cc */
extern bool ndb_index_stat_get_enable(THD *thd);
extern const char* g_ndb_status_index_stat_status;
extern long g_ndb_status_index_stat_cache_query;
extern long g_ndb_status_index_stat_cache_clean;
// Typedefs for long names
typedef NdbDictionary::Table NDBTAB;
typedef NdbDictionary::Index NDBINDEX;
/** ndb_index_stat_thread */
Ndb_index_stat_thread::Ndb_index_stat_thread()
: Ndb_component("Index Stat"),
client_waiting(false)
{
native_mutex_init(&LOCK, MY_MUTEX_INIT_FAST);
native_cond_init(&COND);
native_mutex_init(&stat_mutex, MY_MUTEX_INIT_FAST);
native_cond_init(&stat_cond);
}
Ndb_index_stat_thread::~Ndb_index_stat_thread()
{
native_mutex_destroy(&LOCK);
native_cond_destroy(&COND);
native_mutex_destroy(&stat_mutex);
native_cond_destroy(&stat_cond);
}
void Ndb_index_stat_thread::do_wakeup()
{
// Wakeup from potential wait
log_info("Wakeup");
wakeup();
}
void Ndb_index_stat_thread::wakeup()
{
native_mutex_lock(&LOCK);
client_waiting= true;
native_cond_signal(&COND);
native_mutex_unlock(&LOCK);
}
struct Ndb_index_stat {
enum {
LT_Undef= 0,
LT_New= 1, /* new entry added by a table handler */
LT_Update = 2, /* force kernel update from analyze table */
LT_Read= 3, /* read or reread stats into new query cache */
LT_Idle= 4, /* stats exist */
LT_Check= 5, /* check for new stats */
LT_Delete= 6, /* delete the entry */
LT_Error= 7, /* error, on hold for a while */
LT_Count= 8
};
NdbIndexStat* is;
int index_id;
int index_version;
#ifndef DBUG_OFF
char id[32];
#endif
time_t access_time; /* by any table handler */
time_t update_time; /* latest successful update by us */
time_t load_time; /* when stats were created by kernel */
time_t read_time; /* when stats were read by us (>= load_time) */
uint sample_version; /* goes with read_time */
time_t check_time; /* when checked for updated stats (>= read_time) */
uint query_bytes; /* cache query bytes in use */
uint clean_bytes; /* cache clean bytes waiting to be deleted */
uint drop_bytes; /* cache bytes waiting for drop */
uint evict_bytes; /* cache bytes waiting for evict */
bool force_update; /* one-time force update from analyze table */
bool no_stats; /* have detected that no stats exist */
NdbIndexStat::Error error;
NdbIndexStat::Error client_error;
time_t error_time;
uint error_count; /* forever increasing */
struct Ndb_index_stat *share_next; /* per-share list */
int lt;
int lt_old; /* for info only */
struct Ndb_index_stat *list_next;
struct Ndb_index_stat *list_prev;
struct NDB_SHARE *share;
uint ref_count; /* from client requests */
bool to_delete; /* detached from share and marked for delete */
bool abort_request; /* abort all requests and allow no more */
Ndb_index_stat();
};
struct Ndb_index_stat_list {
const char *name;
int lt;
struct Ndb_index_stat *head;
struct Ndb_index_stat *tail;
uint count;
Ndb_index_stat_list(int the_lt, const char* the_name);
};
extern Ndb_index_stat_list ndb_index_stat_list[];
static time_t ndb_index_stat_time_now= 0;
static time_t
ndb_index_stat_time()
{
time_t now= time(0);
if (unlikely(ndb_index_stat_time_now == 0))
ndb_index_stat_time_now= now;
if (unlikely(now < ndb_index_stat_time_now))
{
DBUG_PRINT("index_stat", ("time moved backwards %d seconds",
int(ndb_index_stat_time_now - now)));
now= ndb_index_stat_time_now;
}
ndb_index_stat_time_now= now;
return now;
}
/*
Return error on stats queries before stats thread starts
and after it exits. This is only a pre-caution since mysqld
should not allow clients at these times.
*/
static bool ndb_index_stat_allow_flag= false;
static bool
ndb_index_stat_allow(int flag= -1)
{
if (flag != -1)
ndb_index_stat_allow_flag= (bool)flag;
return ndb_index_stat_allow_flag;
}
/* Options */
/* Options in string format buffer size */
static const uint ndb_index_stat_option_sz= 512;
static void ndb_index_stat_opt2str(const struct Ndb_index_stat_opt&, char*);
struct Ndb_index_stat_opt {
enum Unit {
Ubool = 1,
Usize = 2,
Utime = 3,
Umsec = 4
};
enum Flag {
Freadonly = (1 << 0),
Fcontrol = (1 << 1)
};
struct Val {
const char* name;
uint val;
uint minval;
uint maxval;
Unit unit;
uint flag;
};
enum Idx {
Iloop_enable = 0,
Iloop_idle = 1,
Iloop_busy = 2,
Iupdate_batch = 3,
Iread_batch = 4,
Iidle_batch = 5,
Icheck_batch = 6,
Icheck_delay = 7,
Idelete_batch = 8,
Iclean_delay = 9,
Ierror_batch = 10,
Ierror_delay = 11,
Ievict_batch = 12,
Ievict_delay = 13,
Icache_limit = 14,
Icache_lowpct = 15,
Izero_total = 16,
Imax = 17
};
Val val[Imax];
/* Options in string format (SYSVAR ndb_index_stat_option) */
char *option;
Ndb_index_stat_opt(char* buf);
uint get(Idx i) const {
assert(i < Imax);
return val[i].val;
}
void set(Idx i, uint the_val) {
assert(i < Imax);
val[i].val = the_val;
}
};
Ndb_index_stat_opt::Ndb_index_stat_opt(char* buf) :
option(buf)
{
#define ival(aname, aval, aminval, amaxval, aunit, aflag) \
val[I##aname].name = #aname; \
val[I##aname].val = aval; \
val[I##aname].minval = aminval; \
val[I##aname].maxval = amaxval; \
val[I##aname].unit = aunit; \
val[I##aname].flag = aflag
ival(loop_enable, 1000, 0, ~(uint)0, Umsec, 0);
ival(loop_idle, 1000, 0, ~(uint)0, Umsec, 0);
ival(loop_busy, 100, 0, ~(uint)0, Umsec, 0);
ival(update_batch, 1, 1, ~(uint)0, Usize, 0);
ival(read_batch, 4, 1, ~(uint)0, Usize, 0);
ival(idle_batch, 32, 1, ~(uint)0, Usize, 0);
ival(check_batch, 8, 1, ~(uint)0, Usize, 0);
ival(check_delay, 600, 0, ~(uint)0, Utime, 0);
ival(clean_delay, 60, 0, ~(uint)0, Utime, 0);
ival(delete_batch, 8, 1, ~(uint)0, Usize, 0);
ival(error_batch, 4, 1, ~(uint)0, Usize, 0);
ival(error_delay, 60, 0, ~(uint)0, Utime, 0);
ival(evict_batch, 8, 1, ~(uint)0, Usize, 0);
ival(evict_delay, 60, 0, ~(uint)0, Utime, 0);
ival(cache_limit, 32*1024*1024, 0, ~(uint)0, Usize, 0);
ival(cache_lowpct, 90, 0, 100, Usize, 0);
ival(zero_total, 0, 0, 1, Ubool, Fcontrol);
#undef ival
ndb_index_stat_opt2str(*this, option);
}
/* Hard limits */
static const uint ndb_index_stat_max_evict_batch = 32;
char ndb_index_stat_option_buf[ndb_index_stat_option_sz];
static Ndb_index_stat_opt ndb_index_stat_opt(ndb_index_stat_option_buf);
/* Copy option struct to string buffer */
static void
ndb_index_stat_opt2str(const Ndb_index_stat_opt& opt, char* str)
{
DBUG_ENTER("ndb_index_stat_opt2str");
char buf[ndb_index_stat_option_sz];
char *const end= &buf[sizeof(buf)];
char* ptr= buf;
*ptr= 0;
const uint imax= Ndb_index_stat_opt::Imax;
for (uint i= 0; i < imax; i++)
{
const Ndb_index_stat_opt::Val& v= opt.val[i];
ptr+= strlen(ptr);
const char* sep= (ptr == buf ? "" : ",");
const uint sz= ptr < end ? (uint)(end - ptr) : 0;
switch (v.unit) {
case Ndb_index_stat_opt::Ubool:
{
DBUG_ASSERT(v.val == 0 || v.val == 1);
if (v.val == 0)
my_snprintf(ptr, sz, "%s%s=0", sep, v.name);
else
my_snprintf(ptr, sz, "%s%s=1", sep, v.name);
}
break;
case Ndb_index_stat_opt::Usize:
{
uint m;
if (v.val == 0)
my_snprintf(ptr, sz, "%s%s=0", sep, v.name);
else if (v.val % (m= 1024*1024*1024) == 0)
my_snprintf(ptr, sz, "%s%s=%uG", sep, v.name, v.val / m);
else if (v.val % (m= 1024*1024) == 0)
my_snprintf(ptr, sz, "%s%s=%uM", sep, v.name, v.val / m);
else if (v.val % (m= 1024) == 0)
my_snprintf(ptr, sz, "%s%s=%uK", sep, v.name, v.val / m);
else
my_snprintf(ptr, sz, "%s%s=%u", sep, v.name, v.val);
}
break;
case Ndb_index_stat_opt::Utime:
{
uint m;
if (v.val == 0)
my_snprintf(ptr, sz, "%s%s=0", sep, v.name);
else if (v.val % (m= 60*60*24) == 0)
my_snprintf(ptr, sz, "%s%s=%ud", sep, v.name, v.val / m);
else if (v.val % (m= 60*60) == 0)
my_snprintf(ptr, sz, "%s%s=%uh", sep, v.name, v.val / m);
else if (v.val % (m= 60) == 0)
my_snprintf(ptr, sz, "%s%s=%um", sep, v.name, v.val / m);
else
my_snprintf(ptr, sz, "%s%s=%us", sep, v.name, v.val);
}
break;
case Ndb_index_stat_opt::Umsec:
{
if (v.val == 0)
my_snprintf(ptr, sz, "%s%s=0", sep, v.name);
else
my_snprintf(ptr, sz, "%s%s=%ums", sep, v.name, v.val);
}
break;
default:
DBUG_ASSERT(false);
break;
}
}
memset(str, 0, ndb_index_stat_option_sz);
strcpy(str, buf);
DBUG_PRINT("index_stat", ("str: \"%s\"", str));
DBUG_VOID_RETURN;
}
static int
ndb_index_stat_option_parse(char* p, Ndb_index_stat_opt& opt)
{
DBUG_ENTER("ndb_index_stat_option_parse");
char *r= strchr(p, '=');
if (r == 0)
DBUG_RETURN(-1);
*r++= 0;
while (isspace(*r))
*r++= 0;
if (*r == 0)
DBUG_RETURN(-1);
bool found= false;
const uint imax= Ndb_index_stat_opt::Imax;
for (uint i= 0; i < imax; i++)
{
Ndb_index_stat_opt::Val& v= opt.val[i];
if (strcmp(p, v.name) != 0)
continue;
found= true;
char *s;
for (s= r; *s != 0; s++)
*s= tolower(*s);
ulonglong val= my_strtoull(r, &s, 10);
switch (v.unit) {
case Ndb_index_stat_opt::Ubool:
{
if ((s > r && *s == 0 && val == 0) ||
strcmp(r, "off") == 0 ||
strcmp(r, "false") == 0)
val= 0;
else if ((s > r && *s == 0 && val == 1) ||
strcmp(r, "on") == 0 ||
strcmp(r, "true") == 0)
val= 1;
else
DBUG_RETURN(-1);
v.val= (uint)val;
}
break;
case Ndb_index_stat_opt::Usize:
{
if (s == r)
DBUG_RETURN(-1);
if (strcmp(s, "") == 0)
;
else if (strcmp(s, "k") == 0)
val*= 1024;
else if (strcmp(s, "m") == 0)
val*= 1024*1024;
else if (strcmp(s, "g") == 0)
val*= 1024*1024*1024;
else
DBUG_RETURN(-1);
if (val < v.minval || val > v.maxval)
DBUG_RETURN(-1);
v.val= (uint)val;
}
break;
case Ndb_index_stat_opt::Utime:
{
if (s == r)
DBUG_RETURN(-1);
if (strcmp(s, "") == 0)
;
else if (strcmp(s, "s") == 0)
;
else if (strcmp(s, "m") == 0)
val*= 60;
else if (strcmp(s, "h") == 0)
val*= 60*60;
else if (strcmp(s, "d") == 0)
val*= 24*60*60;
else
DBUG_RETURN(-1);
if (val < v.minval || val > v.maxval)
DBUG_RETURN(-1);
v.val= (uint)val;
}
break;
case Ndb_index_stat_opt::Umsec:
{
if (s == r)
DBUG_RETURN(-1);
if (strcmp(s, "") == 0)
;
else if (strcmp(s, "ms") == 0)
;
else
DBUG_RETURN(-1);
if (val < v.minval || val > v.maxval)
DBUG_RETURN(-1);
v.val= (uint)val;
}
break;
default:
DBUG_ASSERT(false);
break;
}
}
if (!found)
DBUG_RETURN(-1);
DBUG_RETURN(0);
}
/* Copy option string to option struct */
static int
ndb_index_stat_str2opt(const char *str, Ndb_index_stat_opt& opt)
{
DBUG_ENTER("ndb_index_stat_str2opt");
DBUG_PRINT("index_stat", ("str: \"%s\"", str));
char buf[ndb_index_stat_option_sz];
assert(str != 0);
if (strlen(str) >= sizeof(buf))
DBUG_RETURN(-1);
strcpy(buf, str);
char *p= buf;
while (1)
{
while (isspace(*p))
p++;
if (*p == 0)
break;
char *q= strchr(p, ',');
if (q == p)
DBUG_RETURN(-1);
if (q != 0)
*q= 0;
DBUG_PRINT("index_stat", ("parse: %s", p));
if (ndb_index_stat_option_parse(p, opt) == -1)
DBUG_RETURN(-1);
if (q == 0)
break;
p= q + 1;
}
ndb_index_stat_opt2str(opt, opt.option);
DBUG_RETURN(0);
}
/* Thanks to ha_innodb.cc */
/* Need storage between check and update (assume locked) */
static char ndb_index_stat_option_tmp[ndb_index_stat_option_sz];
int
ndb_index_stat_option_check(MYSQL_THD,
struct st_mysql_sys_var *var,
void *save,
struct st_mysql_value *value)
{
DBUG_ENTER("ndb_index_stat_option_check");
char buf[ndb_index_stat_option_sz];
int len= sizeof(buf);
const char *str= value->val_str(value, buf, &len);
if (str != 0)
{
/* Seems to be nothing in buf */
DBUG_PRINT("index_stat", ("str: %s len: %d", str, len));
char buf2[ndb_index_stat_option_sz];
Ndb_index_stat_opt opt(buf2);
if (ndb_index_stat_str2opt(str, opt) == 0)
{
/* Passed to update */
strcpy(ndb_index_stat_option_tmp, str);
*(const char**)save= ndb_index_stat_option_tmp;
DBUG_RETURN(0);
}
}
DBUG_RETURN(1);
}
void
ndb_index_stat_option_update(MYSQL_THD,
struct st_mysql_sys_var *var,
void *var_ptr,
const void *save)
{
DBUG_ENTER("ndb_index_stat_option_update");
const char *str= *(const char**)save;
DBUG_PRINT("index_stat", ("str: %s", str));
Ndb_index_stat_opt& opt= ndb_index_stat_opt;
int ret= ndb_index_stat_str2opt(str, opt);
assert(ret == 0); NDB_IGNORE_VALUE(ret);
*(const char**)var_ptr= ndb_index_stat_opt.option;
DBUG_VOID_RETURN;
}
/* Global stuff */
struct Ndb_index_stat_glob {
bool th_allow; /* Queries allowed */
bool th_enable; /* Stats thread idea of ndb_index_stat_enable */
bool th_busy; /* Stats thread is busy-looping */
uint th_loop; /* Stats thread current loop wait in ms */
uint force_update;
uint wait_update;
uint no_stats;
uint wait_stats;
/* Accumulating counters */
uint analyze_count; /* Client counters */
uint analyze_error;
uint query_count;
uint query_no_stats;
uint query_error;
uint event_act; /* Events acted on */
uint event_skip; /* Events skipped (likely event-to-self) */
uint event_miss; /* Events received for unknown index */
uint refresh_count; /* Successful cache refreshes */
uint clean_count; /* Times old caches (1 or more) cleaned */
uint pinned_count; /* Times not cleaned due to old cache ref count */
uint drop_count; /* From index drop */
uint evict_count; /* From LRU cleanup */
/* Cache */
uint cache_query_bytes; /* In use */
uint cache_clean_bytes; /* Obsolete versions not yet removed */
uint cache_high_bytes; /* Max ever of above */
uint cache_drop_bytes; /* Part of above waiting to be evicted */
uint cache_evict_bytes; /* Part of above waiting to be evicted */
char status[2][1024];
uint status_i;
Ndb_index_stat_glob();
void set_status();
void zero_total();
};
Ndb_index_stat_glob::Ndb_index_stat_glob()
{
th_allow= false;
th_enable= false;
th_busy= false;
th_loop= 0;
force_update= 0;
wait_update= 0;
no_stats= 0;
wait_stats= 0;
analyze_count= 0;
analyze_error= 0;
query_count= 0;
query_no_stats= 0;
query_error= 0;
event_act= 0;
event_skip= 0;
event_miss= 0;
refresh_count= 0;
clean_count= 0;
pinned_count= 0;
drop_count= 0;
evict_count= 0;
cache_query_bytes= 0;
cache_clean_bytes= 0;
cache_high_bytes= 0;
cache_drop_bytes= 0;
cache_evict_bytes= 0;
memset(status, 0, sizeof(status));
status_i= 0;
}
/* Update status variable (must hold stat_mutex) */
void
Ndb_index_stat_glob::set_status()
{
const Ndb_index_stat_opt &opt= ndb_index_stat_opt;
char* p= status[status_i];
// stats thread
th_allow= ndb_index_stat_allow();
sprintf(p, "allow:%d,enable:%d,busy:%d,loop:%u",
th_allow, th_enable, th_busy, th_loop);
p+= strlen(p);
// entry lists
strcpy(p, ",list:(");
p+= strlen(p);
uint list_count= 0;
for (int lt= 1; lt < Ndb_index_stat::LT_Count; lt++)
{
const Ndb_index_stat_list &list= ndb_index_stat_list[lt];
sprintf(p, "%s:%u,", list.name, list.count);
p+= strlen(p);
list_count+= list.count;
}
sprintf(p, "%s:%u)", "total", list_count);
p+= strlen(p);
// special counters
sprintf(p, ",analyze:(queue:%u,wait:%u)", force_update, wait_update);
p+= strlen(p);
sprintf(p, ",stats:(nostats:%u,wait:%u)", no_stats, wait_stats);
p+= strlen(p);
// accumulating counters
sprintf(p, ",total:(");
p+= strlen(p);
sprintf(p, "analyze:(all:%u,error:%u)", analyze_count, analyze_error);
p+= strlen(p);
sprintf(p, ",query:(all:%u,nostats:%u,error:%u)",
query_count, query_no_stats, query_error);
p+= strlen(p);
sprintf(p, ",event:(act:%u,skip:%u,miss:%u)",
event_act, event_skip, event_miss);
p+= strlen(p);
sprintf(p, ",cache:(refresh:%u,clean:%u,pinned:%u,drop:%u,evict:%u)",
refresh_count, clean_count, pinned_count, drop_count, evict_count);
p+= strlen(p);
sprintf(p, ")");
p+= strlen(p);
// cache size
const uint cache_limit= opt.get(Ndb_index_stat_opt::Icache_limit);
const uint cache_total= cache_query_bytes + cache_clean_bytes;
double cache_pct= (double)0.0;
double cache_high_pct= (double)0.0;
if (cache_limit != 0)
{
cache_pct= (double)100.0 * (double)cache_total / (double)cache_limit;
cache_high_pct= (double)100.0 * (double)cache_high_bytes / (double)cache_limit;
}
sprintf(p, ",cache:(query:%u,clean:%u"
",drop:%u,evict:%u"
",usedpct:%.2f,highpct:%.2f)",
cache_query_bytes, cache_clean_bytes,
cache_drop_bytes, cache_evict_bytes,
cache_pct, cache_high_pct);
p+= strlen(p);
// alternating status buffers to keep this lock short
mysql_mutex_lock(&LOCK_global_system_variables);
g_ndb_status_index_stat_status= status[status_i];
status_i= (status_i + 1) % 2;
g_ndb_status_index_stat_cache_query= cache_query_bytes;
g_ndb_status_index_stat_cache_clean= cache_clean_bytes;
mysql_mutex_unlock(&LOCK_global_system_variables);
}
/* Zero accumulating counters */
void
Ndb_index_stat_glob::zero_total()
{
analyze_count= 0;
analyze_error= 0;
query_count= 0;
query_no_stats= 0;
query_error= 0;
event_act= 0;
event_skip= 0;
event_miss= 0;
refresh_count= 0;
clean_count= 0;
pinned_count= 0;
drop_count= 0;
evict_count= 0;
/* Reset highest use seen to current */
cache_high_bytes= cache_query_bytes + cache_clean_bytes;
}
static Ndb_index_stat_glob ndb_index_stat_glob;
/* Shared index entries */
Ndb_index_stat::Ndb_index_stat()
{
is= 0;
index_id= 0;
index_version= 0;
#ifndef DBUG_OFF
memset(id, 0, sizeof(id));
#endif
access_time= 0;
update_time= 0;
load_time= 0;
read_time= 0;
sample_version= 0;
check_time= 0;
query_bytes= 0;
clean_bytes= 0;
drop_bytes= 0;
evict_bytes= 0;
force_update= false;
no_stats= false;
error_time= 0;
error_count= 0;
share_next= 0;
lt= 0;
lt_old= 0;
list_next= 0;
list_prev= 0;
share= 0;
ref_count= 0;
to_delete= false;
abort_request= false;
}
/*
Called by stats thread and (rarely) by client. Caller must hold
stat_mutex. Client errors currently have no effect on execution
since they are probably local e.g. bad range (internal error).
Argument "from" is 0=stats thread 1=client.
*/
static void
ndb_index_stat_error(Ndb_index_stat *st,
int from, const char* place, int line)
{
time_t now= ndb_index_stat_time();
NdbIndexStat::Error error= st->is->getNdbError();
if (error.code == 0)
{
/* Make sure code is not 0 */
NdbIndexStat::Error error2;
error= error2;
error.code= NdbIndexStat::InternalError;
error.status= NdbError::TemporaryError;
}
if (from == 0)
{
st->error= error;
st->error_time= now; /* Controls proc_error */
}
else
st->client_error= error;
st->error_count++;
DBUG_PRINT("index_stat", ("%s line %d: error %d line %d extra %d",
place, line, error.code, error.line, error.extra));
}
static void
ndb_index_stat_clear_error(Ndb_index_stat *st)
{
st->error.code= 0;
st->error.status= NdbError::Success;
}
/* Lists across shares */
Ndb_index_stat_list::Ndb_index_stat_list(int the_lt, const char* the_name)
{
lt= the_lt;
name= the_name;
head= 0;
tail= 0;
count= 0;
}
Ndb_index_stat_list ndb_index_stat_list[Ndb_index_stat::LT_Count] = {
Ndb_index_stat_list(0, 0),
Ndb_index_stat_list(Ndb_index_stat::LT_New, "new"),
Ndb_index_stat_list(Ndb_index_stat::LT_Update, "update"),
Ndb_index_stat_list(Ndb_index_stat::LT_Read, "read"),
Ndb_index_stat_list(Ndb_index_stat::LT_Idle, "idle"),
Ndb_index_stat_list(Ndb_index_stat::LT_Check, "check"),
Ndb_index_stat_list(Ndb_index_stat::LT_Delete, "delete"),
Ndb_index_stat_list(Ndb_index_stat::LT_Error, "error")
};
static void
ndb_index_stat_list_add(Ndb_index_stat* st, int lt)
{
assert(st != 0 && st->lt == 0);
assert(st->list_next == 0 && st->list_prev == 0);
assert(1 <= lt && lt < Ndb_index_stat::LT_Count);
Ndb_index_stat_list &list= ndb_index_stat_list[lt];
DBUG_PRINT("index_stat", ("st %s -> %s", st->id, list.name));
if (list.count == 0)
{
assert(list.head == 0 && list.tail == 0);
list.head= st;
list.tail= st;
}
else
{
assert(list.tail != 0 && list.tail->list_next == 0);
st->list_prev= list.tail;
list.tail->list_next= st;
list.tail= st;
}
list.count++;
st->lt= lt;
}
static void
ndb_index_stat_list_remove(Ndb_index_stat* st)
{
assert(st != 0);
int lt= st->lt;
assert(1 <= lt && lt < Ndb_index_stat::LT_Count);
Ndb_index_stat_list &list= ndb_index_stat_list[lt];
DBUG_PRINT("index_stat", ("st %s <- %s", st->id, list.name));
Ndb_index_stat* next= st->list_next;
Ndb_index_stat* prev= st->list_prev;
if (list.head == st)
list.head= next;
if (list.tail == st)
list.tail= prev;
assert(list.count != 0);
list.count--;
if (next != 0)
next->list_prev= prev;
if (prev != 0)
prev->list_next= next;
st->lt= 0;
st->lt_old= 0;
st->list_next= 0;
st->list_prev= 0;
}
static void
ndb_index_stat_list_move(Ndb_index_stat *st, int lt)
{
assert(st != 0);
ndb_index_stat_list_remove(st);
ndb_index_stat_list_add(st, lt);
}
/* Stats entry changes (must hold stat_mutex) */
static void
ndb_index_stat_force_update(Ndb_index_stat *st, bool onoff)
{
Ndb_index_stat_glob &glob= ndb_index_stat_glob;
if (onoff)
{
if (!st->force_update)
{
glob.force_update++;
st->force_update= true;
glob.set_status();
}
}
else
{
if (st->force_update)
{
assert(glob.force_update != 0);
glob.force_update--;
st->force_update= false;
glob.set_status();
}
}
}
static void
ndb_index_stat_no_stats(Ndb_index_stat *st, bool flag)
{
Ndb_index_stat_glob &glob= ndb_index_stat_glob;
if (st->no_stats != flag)
{
if (flag)
{
glob.no_stats++;
st->no_stats= true;
}
else
{
assert(glob.no_stats >= 1);
glob.no_stats-= 1;
st->no_stats= false;
}
glob.set_status();
}
}
static void
ndb_index_stat_ref_count(Ndb_index_stat *st, bool flag)
{
uint old_count= st->ref_count;
(void)old_count; // USED
if (flag)
{
st->ref_count++;
}
else
{
assert(st->ref_count != 0);
st->ref_count--;
}
DBUG_PRINT("index_stat", ("st %s ref_count:%u->%u",
st->id, old_count, st->ref_count));
}
/* Find or add entry under the share */
/* Saved in get_share() under stat_mutex */
struct Ndb_index_stat_snap {
time_t load_time;
uint sample_version;
uint error_count;
Ndb_index_stat_snap() { load_time= 0; sample_version= 0; error_count= 0; }
};
/* Subroutine, have lock */
static Ndb_index_stat*
ndb_index_stat_alloc(const NDBINDEX *index,
const NDBTAB *table,
int &err_out)
{
err_out= 0;
Ndb_index_stat *st= new Ndb_index_stat;
NdbIndexStat *is= new NdbIndexStat;
if (st != 0 && is != 0)
{
st->is= is;
st->index_id= index->getObjectId();
st->index_version= index->getObjectVersion();
#ifndef DBUG_OFF
my_snprintf(st->id, sizeof(st->id), "%d.%d", st->index_id, st->index_version);
#endif
if (is->set_index(*index, *table) == 0)
return st;
ndb_index_stat_error(st, 1, "set_index", __LINE__);
err_out= st->client_error.code;
}
else
{
err_out= NdbIndexStat::NoMemError;
}
if (is != 0)
delete is;
if (st != 0)
delete st;
return 0;
}
/* Subroutine, have lock */
static Ndb_index_stat*