forked from dearcode/inception2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug_sync.cc
1870 lines (1481 loc) · 63.1 KB
/
debug_sync.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) 2009, 2011, 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,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
/**
== Debug Sync Facility ==
The Debug Sync Facility allows placement of synchronization points in
the server code by using the DEBUG_SYNC macro:
open_tables(...)
DEBUG_SYNC(thd, "after_open_tables");
lock_tables(...)
When activated, a sync point can
- Emit a signal and/or
- Wait for a signal
Nomenclature:
- signal: A value of a global variable that persists
until overwritten by a new signal. The global
variable can also be seen as a "signal post"
or "flag mast". Then the signal is what is
attached to the "signal post" or "flag mast".
- emit a signal: Assign the value (the signal) to the global
variable ("set a flag") and broadcast a
global condition to wake those waiting for
a signal.
- wait for a signal: Loop over waiting for the global condition until
the global value matches the wait-for signal.
By default, all sync points are inactive. They do nothing (except to
burn a couple of CPU cycles for checking if they are active).
A sync point becomes active when an action is requested for it.
To do so, put a line like this in the test case file:
SET DEBUG_SYNC= 'after_open_tables SIGNAL opened WAIT_FOR flushed';
This activates the sync point 'after_open_tables'. It requests it to
emit the signal 'opened' and wait for another thread to emit the signal
'flushed' when the thread's execution runs through the sync point.
For every sync point there can be one action per thread only. Every
thread can request multiple actions, but only one per sync point. In
other words, a thread can activate multiple sync points.
Here is an example how to activate and use the sync points:
--connection conn1
SET DEBUG_SYNC= 'after_open_tables SIGNAL opened WAIT_FOR flushed';
send INSERT INTO t1 VALUES(1);
--connection conn2
SET DEBUG_SYNC= 'now WAIT_FOR opened';
SET DEBUG_SYNC= 'after_abort_locks SIGNAL flushed';
FLUSH TABLE t1;
When conn1 runs through the INSERT statement, it hits the sync point
'after_open_tables'. It notices that it is active and executes its
action. It emits the signal 'opened' and waits for another thread to
emit the signal 'flushed'.
conn2 waits immediately at the special sync point 'now' for another
thread to emit the 'opened' signal.
A signal remains in effect until it is overwritten. If conn1 signals
'opened' before conn2 reaches 'now', conn2 will still find the 'opened'
signal. It does not wait in this case.
When conn2 reaches 'after_abort_locks', it signals 'flushed', which lets
conn1 awake.
Normally the activation of a sync point is cleared when it has been
executed. Sometimes it is necessary to keep the sync point active for
another execution. You can add an execute count to the action:
SET DEBUG_SYNC= 'name SIGNAL sig EXECUTE 3';
This sets the signal point's activation counter to 3. Each execution
decrements the counter. After the third execution the sync point
becomes inactive.
One of the primary goals of this facility is to eliminate sleeps from
the test suite. In most cases it should be possible to rewrite test
cases so that they do not need to sleep. (But this facility cannot
synchronize multiple processes.) However, to support test development,
and as a last resort, sync point waiting times out. There is a default
timeout, but it can be overridden:
SET DEBUG_SYNC= 'name WAIT_FOR sig TIMEOUT 10 EXECUTE 2';
TIMEOUT 0 is special: If the signal is not present, the wait times out
immediately.
When a wait timed out (even on TIMEOUT 0), a warning is generated so
that it shows up in the test result.
You can throw an error message and kill the query when a synchronization
point is hit a certain number of times:
SET DEBUG_SYNC= 'name HIT_LIMIT 3';
Or combine it with signal and/or wait:
SET DEBUG_SYNC= 'name SIGNAL sig EXECUTE 2 HIT_LIMIT 3';
Here the first two hits emit the signal, the third hit returns the error
message and kills the query.
For cases where you are not sure that an action is taken and thus
cleared in any case, you can force to clear (deactivate) a sync point:
SET DEBUG_SYNC= 'name CLEAR';
If you want to clear all actions and clear the global signal, use:
SET DEBUG_SYNC= 'RESET';
This is the only way to reset the global signal to an empty string.
For testing of the facility itself you can execute a sync point just
as if it had been hit:
SET DEBUG_SYNC= 'name TEST';
=== Formal Syntax ===
The string to "assign" to the DEBUG_SYNC variable can contain:
{RESET |
<sync point name> TEST |
<sync point name> CLEAR |
<sync point name> {{SIGNAL <signal name> |
WAIT_FOR <signal name> [TIMEOUT <seconds>]}
[EXECUTE <count>] &| HIT_LIMIT <count>}
Here '&|' means 'and/or'. This means that one of the sections
separated by '&|' must be present or both of them.
=== Activation/Deactivation ===
The facility is an optional part of the MySQL server.
It is enabled in a debug server by default.
./configure --enable-debug-sync
The Debug Sync Facility, when compiled in, is disabled by default. It
can be enabled by a mysqld command line option:
--debug-sync-timeout[=default_wait_timeout_value_in_seconds]
'default_wait_timeout_value_in_seconds' is the default timeout for the
WAIT_FOR action. If set to zero, the facility stays disabled.
The facility is enabled by default in the test suite, but can be
disabled with:
mysql-test-run.pl ... --debug-sync-timeout=0 ...
Likewise the default wait timeout can be set:
mysql-test-run.pl ... --debug-sync-timeout=10 ...
The command line option influences the readable value of the system
variable 'debug_sync'.
* If the facility is not compiled in, the system variable does not exist.
* If --debug-sync-timeout=0 the value of the variable reads as "OFF".
* Otherwise the value reads as "ON - current signal: " followed by the
current signal string, which can be empty.
The readable variable value is the same, regardless if read as global
or session value.
Setting the 'debug-sync' system variable requires 'SUPER' privilege.
You can never read back the string that you assigned to the variable,
unless you assign the value that the variable does already have. But
that would give a parse error. A syntactically correct string is
parsed into a debug sync action and stored apart from the variable value.
=== Implementation ===
Pseudo code for a sync point:
#define DEBUG_SYNC(thd, sync_point_name)
if (unlikely(opt_debug_sync_timeout))
debug_sync(thd, STRING_WITH_LEN(sync_point_name))
The sync point performs a binary search in a sorted array of actions
for this thread.
The SET DEBUG_SYNC statement adds a requested action to the array or
overwrites an existing action for the same sync point. When it adds a
new action, the array is sorted again.
=== A typical synchronization pattern ===
There are quite a few places in MySQL, where we use a synchronization
pattern like this:
mysql_mutex_lock(&mutex);
thd->enter_cond(&condition_variable, &mutex, new_message);
#if defined(ENABLE_DEBUG_SYNC)
if (!thd->killed && !end_of_wait_condition)
DEBUG_SYNC(thd, "sync_point_name");
#endif
while (!thd->killed && !end_of_wait_condition)
mysql_cond_wait(&condition_variable, &mutex);
thd->exit_cond(old_message);
Here some explanations:
thd->enter_cond() is used to register the condition variable and the
mutex in thd->mysys_var. This is done to allow the thread to be
interrupted (killed) from its sleep. Another thread can find the
condition variable to signal and mutex to use for synchronization in
this thread's THD::mysys_var.
thd->enter_cond() requires the mutex to be acquired in advance.
thd->exit_cond() unregisters the condition variable and mutex and
releases the mutex.
If you want to have a Debug Sync point with the wait, please place it
behind enter_cond(). Only then you can safely decide, if the wait will
be taken. Also you will have THD::proc_info correct when the sync
point emits a signal. DEBUG_SYNC sets its own proc_info, but restores
the previous one before releasing its internal mutex. As soon as
another thread sees the signal, it does also see the proc_info from
before entering the sync point. In this case it will be "new_message",
which is associated with the wait that is to be synchronized.
In the example above, the wait condition is repeated before the sync
point. This is done to skip the sync point, if no wait takes place.
The sync point is before the loop (not inside the loop) to have it hit
once only. It is possible that the condition variable is signaled
multiple times without the wait condition to be true.
A bit off-topic: At some places, the loop is taken around the whole
synchronization pattern:
while (!thd->killed && !end_of_wait_condition)
{
mysql_mutex_lock(&mutex);
thd->enter_cond(&condition_variable, &mutex, new_message);
if (!thd->killed [&& !end_of_wait_condition])
{
[DEBUG_SYNC(thd, "sync_point_name");]
mysql_cond_wait(&condition_variable, &mutex);
}
thd->exit_cond(old_message);
}
Note that it is important to repeat the test for thd->killed after
enter_cond(). Otherwise the killing thread may kill this thread after
it tested thd->killed in the loop condition and before it registered
the condition variable and mutex in enter_cond(). In this case, the
killing thread does not know that this thread is going to wait on a
condition variable. It would just set THD::killed. But if we would not
test it again, we would go asleep though we are killed. If the killing
thread would kill us when we are after the second test, but still
before sleeping, we hold the mutex, which is registered in mysys_var.
The killing thread would try to acquire the mutex before signaling
the condition variable. Since the mutex is only released implicitly in
mysql_cond_wait(), the signaling happens at the right place. We
have a safe synchronization.
=== Co-work with the DBUG facility ===
When running the MySQL test suite with the --debug command line
option, the Debug Sync Facility writes trace messages to the DBUG
trace. The following shell commands proved very useful in extracting
relevant information:
egrep 'query:|debug_sync_exec:' mysql-test/var/log/mysqld.1.trace
It shows all executed SQL statements and all actions executed by
synchronization points.
Sometimes it is also useful to see, which synchronization points have
been run through (hit) with or without executing actions. Then add
"|debug_sync_point:" to the egrep pattern.
=== Further reading ===
For a discussion of other methods to synchronize threads see
http://forge.mysql.com/wiki/MySQL_Internals_Test_Synchronization
For complete syntax tests, functional tests, and examples see the test
case debug_sync.test.
See also worklog entry WL#4259 - Test Synchronization Facility
*/
#include "debug_sync.h"
#if defined(ENABLED_DEBUG_SYNC)
/*
Due to weaknesses in our include files, we need to include
sql_priv.h here. To have THD declared, we need to include
sql_class.h. This includes log_event.h, which in turn requires
declarations from sql_priv.h (e.g. OPTION_AUTO_IS_NULL).
sql_priv.h includes almost everything, so is sufficient here.
*/
#include "sql_priv.h"
#include "sql_parse.h"
using std::max;
using std::min;
/*
Action to perform at a synchronization point.
NOTE: This structure is moved around in memory by realloc(), qsort(),
and memmove(). Do not add objects with non-trivial constuctors
or destructors, which might prevent moving of this structure
with these functions.
*/
struct st_debug_sync_action
{
ulong activation_count; /* max(hit_limit, execute) */
ulong hit_limit; /* hits before kill query */
ulong execute; /* executes before self-clear */
ulong timeout; /* wait_for timeout */
String signal; /* signal to emit */
String wait_for; /* signal to wait for */
String sync_point; /* sync point name */
bool need_sort; /* if new action, array needs sort */
};
/* Debug sync control. Referenced by THD. */
struct st_debug_sync_control
{
st_debug_sync_action *ds_action; /* array of actions */
uint ds_active; /* # active actions */
uint ds_allocated; /* # allocated actions */
ulonglong dsp_hits; /* statistics */
ulonglong dsp_executed; /* statistics */
ulonglong dsp_max_active; /* statistics */
/*
thd->proc_info points at unsynchronized memory.
It must not go away as long as the thread exists.
*/
char ds_proc_info[80]; /* proc_info string */
};
/**
Definitions for the debug sync facility.
1. Global string variable to hold a "signal" ("signal post", "flag mast").
2. Global condition variable for signaling and waiting.
3. Global mutex to synchronize access to the above.
*/
struct st_debug_sync_globals
{
String ds_signal; /* signal variable */
mysql_cond_t ds_cond; /* condition variable */
mysql_mutex_t ds_mutex; /* mutex variable */
ulonglong dsp_hits; /* statistics */
ulonglong dsp_executed; /* statistics */
ulonglong dsp_max_active; /* statistics */
};
static st_debug_sync_globals debug_sync_global; /* All globals in one object */
/**
Callback pointer for C files.
*/
extern "C" void (*debug_sync_C_callback_ptr)(const char *, size_t);
/**
Callbacks from C files.
*/
C_MODE_START
static void debug_sync_C_callback(const char *, size_t);
static int debug_sync_qsort_cmp(const void *, const void *);
C_MODE_END
/**
Callback for debug sync, to be used by C files. See thr_lock.c for example.
@description
We cannot place a sync point directly in C files (like those in mysys or
certain storage engines written mostly in C like MyISAM or Maria). Because
they are C code and do not include sql_priv.h. So they do not know the
macro DEBUG_SYNC(thd, sync_point_name). The macro needs a 'thd' argument.
Hence it cannot be used in files outside of the sql/ directory.
The workaround is to call back simple functions like this one from
non-sql/ files.
We want to allow modules like thr_lock to be used without sql/ and
especially without Debug Sync. So we cannot just do a simple call
of the callback function. Instead we provide a global pointer in
the other file, which is to be set to the callback by Debug Sync.
If the pointer is not set, no call back will be done. If Debug
Sync sets the pointer to a callback function like this one, it will
be called. That way thr_lock.c does not have an undefined reference
to Debug Sync and can be used without it. Debug Sync, in contrast,
has an undefined reference to that pointer and thus requires
thr_lock to be linked too. But this is not a problem as it is part
of the MySQL server anyway.
@note
The callback pointer in C files is set only if debug sync is
initialized. And this is done only if opt_debug_sync_timeout is set.
*/
static void debug_sync_C_callback(const char *sync_point_name,
size_t name_len)
{
if (unlikely(opt_debug_sync_timeout))
debug_sync(current_thd, sync_point_name, name_len);
}
#ifdef HAVE_PSI_INTERFACE
static PSI_mutex_key key_debug_sync_globals_ds_mutex;
static PSI_mutex_info all_debug_sync_mutexes[] = {
{ &key_debug_sync_globals_ds_mutex, "DEBUG_SYNC::mutex", PSI_FLAG_GLOBAL}
};
static PSI_cond_key key_debug_sync_globals_ds_cond;
static PSI_cond_info all_debug_sync_conds[] = {
{ &key_debug_sync_globals_ds_cond, "DEBUG_SYNC::cond", PSI_FLAG_GLOBAL}
};
static void init_debug_sync_psi_keys(void)
{
const char *category = "sql";
int count;
count = array_elements(all_debug_sync_mutexes);
mysql_mutex_register(category, all_debug_sync_mutexes, count);
count = array_elements(all_debug_sync_conds);
mysql_cond_register(category, all_debug_sync_conds, count);
}
#endif /* HAVE_PSI_INTERFACE */
/**
Initialize the debug sync facility at server start.
@return status
@retval 0 ok
@retval != 0 error
*/
int debug_sync_init(void)
{
DBUG_ENTER("debug_sync_init");
#ifdef HAVE_PSI_INTERFACE
init_debug_sync_psi_keys();
#endif
if (opt_debug_sync_timeout) {
int rc;
/* Initialize the global variables. */
debug_sync_global.ds_signal.length(0);
if ((rc = mysql_cond_init(key_debug_sync_globals_ds_cond,
&debug_sync_global.ds_cond, NULL)) ||
(rc = mysql_mutex_init(key_debug_sync_globals_ds_mutex,
&debug_sync_global.ds_mutex,
MY_MUTEX_INIT_FAST)))
DBUG_RETURN(rc); /* purecov: inspected */
/* Set the call back pointer in C files. */
debug_sync_C_callback_ptr = debug_sync_C_callback;
}
DBUG_RETURN(0);
}
/**
End the debug sync facility.
@description
This is called at server shutdown or after a thread initialization error.
*/
void debug_sync_end(void)
{
DBUG_ENTER("debug_sync_end");
/* End the facility only if it had been initialized. */
if (debug_sync_C_callback_ptr) {
/* Clear the call back pointer in C files. */
debug_sync_C_callback_ptr = NULL;
/* Destroy the global variables. */
debug_sync_global.ds_signal.free();
mysql_cond_destroy(&debug_sync_global.ds_cond);
mysql_mutex_destroy(&debug_sync_global.ds_mutex);
/* Print statistics. */
{
char llbuff[22];
sql_print_information("Debug sync points hit: %22s",
llstr(debug_sync_global.dsp_hits, llbuff));
sql_print_information("Debug sync points executed: %22s",
llstr(debug_sync_global.dsp_executed, llbuff));
sql_print_information("Debug sync points max active per thread: %22s",
llstr(debug_sync_global.dsp_max_active, llbuff));
}
}
DBUG_VOID_RETURN;
}
/* purecov: begin tested */
/**
Disable the facility after lack of memory if no error can be returned.
@note
Do not end the facility here because the global variables can
be in use by other threads.
*/
static void debug_sync_emergency_disable(void)
{
DBUG_ENTER("debug_sync_emergency_disable");
opt_debug_sync_timeout = 0;
DBUG_PRINT("debug_sync",
("Debug Sync Facility disabled due to lack of memory."));
sql_print_error("Debug Sync Facility disabled due to lack of memory.");
DBUG_VOID_RETURN;
}
/* purecov: end */
/**
Initialize the debug sync facility at thread start.
@param[in] thd thread handle
*/
void debug_sync_init_thread(THD *thd)
{
DBUG_ENTER("debug_sync_init_thread");
DBUG_ASSERT(thd);
if (opt_debug_sync_timeout) {
thd->debug_sync_control = (st_debug_sync_control *)
my_malloc(sizeof(st_debug_sync_control), MYF(MY_WME | MY_ZEROFILL));
if (!thd->debug_sync_control) {
/*
Error is reported by my_malloc().
We must disable the facility. We have no way to return an error.
*/
debug_sync_emergency_disable(); /* purecov: tested */
}
}
DBUG_VOID_RETURN;
}
/**
End the debug sync facility at thread end.
@param[in] thd thread handle
*/
void debug_sync_end_thread(THD *thd)
{
DBUG_ENTER("debug_sync_end_thread");
DBUG_ASSERT(thd);
if (thd->debug_sync_control) {
st_debug_sync_control *ds_control = thd->debug_sync_control;
/*
This synchronization point can be used to synchronize on thread end.
This is the latest point in a THD's life, where this can be done.
*/
DEBUG_SYNC(thd, "thread_end");
if (ds_control->ds_action) {
st_debug_sync_action *action = ds_control->ds_action;
st_debug_sync_action *action_end = action + ds_control->ds_allocated;
for (; action < action_end; action++) {
action->signal.free();
action->wait_for.free();
action->sync_point.free();
}
my_free(ds_control->ds_action);
}
/* Statistics. */
mysql_mutex_lock(&debug_sync_global.ds_mutex);
debug_sync_global.dsp_hits += ds_control->dsp_hits;
debug_sync_global.dsp_executed += ds_control->dsp_executed;
if (debug_sync_global.dsp_max_active < ds_control->dsp_max_active)
debug_sync_global.dsp_max_active = ds_control->dsp_max_active;
mysql_mutex_unlock(&debug_sync_global.ds_mutex);
my_free(ds_control);
thd->debug_sync_control = NULL;
}
DBUG_VOID_RETURN;
}
/**
Move a string by length.
@param[out] to buffer for the resulting string
@param[in] to_end end of buffer
@param[in] from source string
@param[in] length number of bytes to copy
@return pointer to end of copied string
*/
static char *debug_sync_bmove_len(char *to, char *to_end,
const char *from, size_t length)
{
DBUG_ASSERT(to);
DBUG_ASSERT(to_end);
DBUG_ASSERT(!length || from);
set_if_smaller(length, (size_t) (to_end - to));
memcpy(to, from, length);
return (to + length);
}
#if !defined(DBUG_OFF)
/**
Create a string that describes an action.
@param[out] result buffer for the resulting string
@param[in] size size of result buffer
@param[in] action action to describe
*/
static void debug_sync_action_string(char *result, uint size,
st_debug_sync_action *action)
{
char *wtxt = result;
char *wend = wtxt + size - 1; /* Allow emergency '\0'. */
DBUG_ASSERT(result);
DBUG_ASSERT(action);
/* If an execute count is present, signal or wait_for are needed too. */
DBUG_ASSERT(!action->execute ||
action->signal.length() || action->wait_for.length());
if (action->execute) {
if (action->signal.length()) {
wtxt = debug_sync_bmove_len(wtxt, wend, STRING_WITH_LEN("SIGNAL "));
wtxt = debug_sync_bmove_len(wtxt, wend, action->signal.ptr(),
action->signal.length());
}
if (action->wait_for.length()) {
if ((wtxt == result) && (wtxt < wend))
*(wtxt++) = ' ';
wtxt = debug_sync_bmove_len(wtxt, wend, STRING_WITH_LEN(" WAIT_FOR "));
wtxt = debug_sync_bmove_len(wtxt, wend, action->wait_for.ptr(),
action->wait_for.length());
if (action->timeout != opt_debug_sync_timeout)
wtxt += my_snprintf(wtxt, wend - wtxt, " TIMEOUT %lu", action->timeout);
}
if (action->execute != 1)
wtxt += my_snprintf(wtxt, wend - wtxt, " EXECUTE %lu", action->execute);
}
if (action->hit_limit) {
wtxt += my_snprintf(wtxt, wend - wtxt, "%sHIT_LIMIT %lu",
(wtxt == result) ? "" : " ", action->hit_limit);
}
/*
If (wtxt == wend) string may not be terminated.
There is one byte left for an emergency termination.
*/
*wtxt = '\0';
}
/**
Print actions.
@param[in] thd thread handle
*/
static void debug_sync_print_actions(THD *thd)
{
st_debug_sync_control *ds_control = thd->debug_sync_control;
uint idx;
DBUG_ENTER("debug_sync_print_actions");
DBUG_ASSERT(thd);
if (!ds_control)
DBUG_VOID_RETURN;
for (idx = 0; idx < ds_control->ds_active; idx++) {
const char *dsp_name = ds_control->ds_action[idx].sync_point.c_ptr();
char action_string[256];
debug_sync_action_string(action_string, sizeof(action_string),
ds_control->ds_action + idx);
DBUG_PRINT("debug_sync_list", ("%s %s", dsp_name, action_string));
}
DBUG_VOID_RETURN;
}
#endif /* !defined(DBUG_OFF) */
/**
Compare two actions by sync point name length, string.
@param[in] arg1 reference to action1
@param[in] arg2 reference to action2
@return difference
@retval == 0 length1/string1 is same as length2/string2
@retval < 0 length1/string1 is smaller
@retval > 0 length1/string1 is bigger
*/
static int debug_sync_qsort_cmp(const void *arg1, const void *arg2)
{
st_debug_sync_action *action1 = (st_debug_sync_action *) arg1;
st_debug_sync_action *action2 = (st_debug_sync_action *) arg2;
int diff;
DBUG_ASSERT(action1);
DBUG_ASSERT(action2);
if (!(diff = action1->sync_point.length() - action2->sync_point.length()))
diff = memcmp(action1->sync_point.ptr(), action2->sync_point.ptr(),
action1->sync_point.length());
return diff;
}
/**
Find a debug sync action.
@param[in] actionarr array of debug sync actions
@param[in] quantity number of actions in array
@param[in] dsp_name name of debug sync point to find
@param[in] name_len length of name of debug sync point
@return action
@retval != NULL found sync point in array
@retval NULL not found
@description
Binary search. Array needs to be sorted by length, sync point name.
*/
static st_debug_sync_action *debug_sync_find(st_debug_sync_action *actionarr,
int quantity,
const char *dsp_name,
uint name_len)
{
st_debug_sync_action *action;
int low ;
int high ;
int mid ;
int diff ;
DBUG_ASSERT(actionarr);
DBUG_ASSERT(dsp_name);
DBUG_ASSERT(name_len);
low = 0;
high = quantity;
while (low < high) {
mid = (low + high) / 2;
action = actionarr + mid;
if (!(diff = name_len - action->sync_point.length()) &&
!(diff = memcmp(dsp_name, action->sync_point.ptr(), name_len)))
return action;
if (diff > 0)
low = mid + 1;
else
high = mid - 1;
}
if (low < quantity) {
action = actionarr + low;
if ((name_len == action->sync_point.length()) &&
!memcmp(dsp_name, action->sync_point.ptr(), name_len))
return action;
}
return NULL;
}
/**
Reset the debug sync facility.
@param[in] thd thread handle
@description
Remove all actions of this thread.
Clear the global signal.
*/
static void debug_sync_reset(THD *thd)
{
st_debug_sync_control *ds_control = thd->debug_sync_control;
DBUG_ENTER("debug_sync_reset");
DBUG_ASSERT(thd);
DBUG_ASSERT(ds_control);
/* Remove all actions of this thread. */
ds_control->ds_active = 0;
/* Clear the global signal. */
mysql_mutex_lock(&debug_sync_global.ds_mutex);
debug_sync_global.ds_signal.length(0);
mysql_mutex_unlock(&debug_sync_global.ds_mutex);
DBUG_VOID_RETURN;
}
/**
Remove a debug sync action.
@param[in] ds_control control object
@param[in] action action to be removed
@description
Removing an action mainly means to decrement the ds_active counter.
But if the action is between other active action in the array, then
the array needs to be shrinked. The active actions above the one to
be removed have to be moved down by one slot.
*/
static void debug_sync_remove_action(st_debug_sync_control *ds_control,
st_debug_sync_action *action)
{
uint dsp_idx = action - ds_control->ds_action;
DBUG_ENTER("debug_sync_remove_action");
DBUG_ASSERT(ds_control);
DBUG_ASSERT(ds_control == current_thd->debug_sync_control);
DBUG_ASSERT(action);
DBUG_ASSERT(dsp_idx < ds_control->ds_active);
/* Decrement the number of currently active actions. */
ds_control->ds_active--;
/*
If this was not the last active action in the array, we need to
shift remaining active actions down to keep the array gap-free.
Otherwise binary search might fail or take longer than necessary at
least. Also new actions are always put to the end of the array.
*/
if (ds_control->ds_active > dsp_idx) {
/*
Do not make save_action an object of class st_debug_sync_action.
Its destructor would tamper with the String pointers.
*/
uchar save_action[sizeof(st_debug_sync_action)];
/*
Copy the to-be-removed action object to temporary storage before
the shift copies the string pointers over. Do not use assignment
because it would use assignment operator methods for the Strings.
This would copy the strings. The shift below overwrite the string
pointers without freeing them first. By using memmove() we save
the pointers, which are overwritten by the shift.
*/
memmove(save_action, action, sizeof(st_debug_sync_action));
/* Move actions down. */
memmove(ds_control->ds_action + dsp_idx,
ds_control->ds_action + dsp_idx + 1,
(ds_control->ds_active - dsp_idx) *
sizeof(st_debug_sync_action));
/*
Copy back the saved action object to the now free array slot. This
replaces the double references of String pointers that have been
produced by the shift. Again do not use an assignment operator to
avoid string allocation/copy.
*/
memmove(ds_control->ds_action + ds_control->ds_active, save_action,
sizeof(st_debug_sync_action));
}
DBUG_VOID_RETURN;
}
/**
Get a debug sync action.
@param[in] thd thread handle
@param[in] dsp_name debug sync point name
@param[in] name_len length of sync point name
@return action
@retval != NULL ok
@retval NULL error
@description
Find the debug sync action for a debug sync point or make a new one.
*/
static st_debug_sync_action *debug_sync_get_action(THD *thd,
const char *dsp_name,
uint name_len)
{
st_debug_sync_control *ds_control = thd->debug_sync_control;
st_debug_sync_action *action;
DBUG_ENTER("debug_sync_get_action");
DBUG_ASSERT(thd);
DBUG_ASSERT(dsp_name);
DBUG_ASSERT(name_len);
DBUG_ASSERT(ds_control);
DBUG_PRINT("debug_sync", ("sync_point: '%.*s'", (int) name_len, dsp_name));
DBUG_PRINT("debug_sync", ("active: %u allocated: %u",
ds_control->ds_active, ds_control->ds_allocated));
/* There cannot be more active actions than allocated. */
DBUG_ASSERT(ds_control->ds_active <= ds_control->ds_allocated);
/* If there are active actions, the action array must be present. */
DBUG_ASSERT(!ds_control->ds_active || ds_control->ds_action);
/* Try to reuse existing action if there is one for this sync point. */
if (ds_control->ds_active &&
(action = debug_sync_find(ds_control->ds_action, ds_control->ds_active,
dsp_name, name_len))) {
/* Reuse an already active sync point action. */
DBUG_ASSERT((uint)(action - ds_control->ds_action) < ds_control->ds_active);
DBUG_PRINT("debug_sync", ("reuse action idx: %ld",
(long) (action - ds_control->ds_action)));
} else {
/* Create a new action. */
int dsp_idx = ds_control->ds_active++;
set_if_bigger(ds_control->dsp_max_active, ds_control->ds_active);
if (ds_control->ds_active > ds_control->ds_allocated) {
uint new_alloc = ds_control->ds_active + 3;
void *new_action = my_realloc(ds_control->ds_action,
new_alloc * sizeof(st_debug_sync_action),
MYF(MY_WME | MY_ALLOW_ZERO_PTR));
if (!new_action) {
/* Error is reported by my_malloc(). */
goto err; /* purecov: tested */
}
ds_control->ds_action = (st_debug_sync_action *) new_action;
ds_control->ds_allocated = new_alloc;
/* Clear memory as we do not run string constructors here. */
memset((ds_control->ds_action + dsp_idx), 0,
(new_alloc - dsp_idx) * sizeof(st_debug_sync_action));
}
DBUG_PRINT("debug_sync", ("added action idx: %u", dsp_idx));
action = ds_control->ds_action + dsp_idx;
if (action->sync_point.copy(dsp_name, name_len, system_charset_info)) {
/* Error is reported by my_malloc(). */
goto err; /* purecov: tested */
}
action->need_sort = TRUE;
}
DBUG_ASSERT(action >= ds_control->ds_action);
DBUG_ASSERT(action < ds_control->ds_action + ds_control->ds_active);
DBUG_PRINT("debug_sync", ("action: 0x%lx array: 0x%lx count: %u",