forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmt.cpp
More file actions
4083 lines (3601 loc) · 108 KB
/
mt.cpp
File metadata and controls
4083 lines (3601 loc) · 108 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2008, 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, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
#include <ndb_global.h>
#include <VMSignal.hpp>
#include <kernel_types.h>
#include <Prio.hpp>
#include <SignalLoggerManager.hpp>
#include <SimulatedBlock.hpp>
#include <ErrorHandlingMacros.hpp>
#include <GlobalData.hpp>
#include <WatchDog.hpp>
#include <TransporterDefinitions.hpp>
#include "FastScheduler.hpp"
#include "mt.hpp"
#include <DebuggerNames.hpp>
#include <signaldata/StopForCrash.hpp>
#include "TransporterCallbackKernel.hpp"
#include <NdbSleep.h>
#include <portlib/ndb_prefetch.h>
#include "mt-asm.h"
inline
SimulatedBlock*
GlobalData::mt_getBlock(BlockNumber blockNo, Uint32 instanceNo)
{
SimulatedBlock* b = getBlock(blockNo);
if (b != 0 && instanceNo != 0)
b = b->getInstance(instanceNo);
return b;
}
#ifdef __GNUC__
/* Provides a small (but noticeable) speedup in benchmarks. */
#define memcpy __builtin_memcpy
#endif
/* size of a cacheline */
#define NDB_CL 64
/* Constants found by benchmarks to be reasonable values. */
/* Maximum number of signals to execute before sending to remote nodes. */
static const Uint32 MAX_SIGNALS_BEFORE_SEND = 200;
/*
* Max. signals to execute from one job buffer before considering other
* possible stuff to do.
*/
static const Uint32 MAX_SIGNALS_PER_JB = 100;
/**
* Max signals written to other thread before calling flush_jbb_write_state
*/
static const Uint32 MAX_SIGNALS_BEFORE_FLUSH_RECEIVER = 2;
static const Uint32 MAX_SIGNALS_BEFORE_FLUSH_OTHER = 20;
static const Uint32 MAX_SIGNALS_BEFORE_WAKEUP = 128;
//#define NDB_MT_LOCK_TO_CPU
#define MAX_BLOCK_INSTANCES (1 + MAX_NDBMT_LQH_WORKERS + 1) //main+lqh+extra
#define NUM_MAIN_THREADS 2 // except receiver
#define MAX_THREADS (NUM_MAIN_THREADS + MAX_NDBMT_LQH_THREADS + 1)
/* If this is too small it crashes before first signal. */
#define MAX_INSTANCES_PER_THREAD (16 + 8 * MAX_NDBMT_LQH_THREADS)
static Uint32 num_lqh_workers = 0;
static Uint32 num_lqh_threads = 0;
static Uint32 num_threads = 0;
static Uint32 receiver_thread_no = 0;
#define NO_SEND_THREAD (MAX_THREADS + 1)
/* max signal is 32 words, 7 for signal header and 25 datawords */
#define MIN_SIGNALS_PER_PAGE (thr_job_buffer::SIZE / 32)
struct mt_lock_stat
{
const void * m_ptr;
char * m_name;
Uint32 m_contended_count;
Uint32 m_spin_count;
};
static void register_lock(const void * ptr, const char * name);
static mt_lock_stat * lookup_lock(const void * ptr);
#if defined(HAVE_LINUX_FUTEX) && defined(NDB_HAVE_XCNG)
#define USE_FUTEX
#endif
#ifdef USE_FUTEX
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#define FUTEX_WAIT 0
#define FUTEX_WAKE 1
#define FUTEX_FD 2
#define FUTEX_REQUEUE 3
#define FUTEX_CMP_REQUEUE 4
#define FUTEX_WAKE_OP 5
static inline
int
futex_wait(volatile unsigned * addr, int val, const struct timespec * timeout)
{
return syscall(SYS_futex,
addr, FUTEX_WAIT, val, timeout, 0, 0) == 0 ? 0 : errno;
}
static inline
int
futex_wake(volatile unsigned * addr)
{
return syscall(SYS_futex, addr, FUTEX_WAKE, 1, 0, 0, 0) == 0 ? 0 : errno;
}
struct thr_wait
{
volatile unsigned m_futex_state;
enum {
FS_RUNNING = 0,
FS_SLEEPING = 1
};
thr_wait() { xcng(&m_futex_state, FS_RUNNING);}
void init () {}
};
/**
* Sleep until woken up or timeout occurs.
*
* Will call check_callback(check_arg) after proper synchronisation, and only
* if that returns true will it actually sleep, else it will return
* immediately. This is needed to avoid races with wakeup.
*
* Returns 'true' if it actually did sleep.
*/
static inline
bool
yield(struct thr_wait* wait, const Uint32 nsec,
bool (*check_callback)(struct thr_data *), struct thr_data *check_arg)
{
volatile unsigned * val = &wait->m_futex_state;
#ifndef NDEBUG
int old =
#endif
xcng(val, thr_wait::FS_SLEEPING);
assert(old == thr_wait::FS_RUNNING);
/**
* At this point, we need to re-check the condition that made us decide to
* sleep, and skip sleeping if it changed..
*
* Otherwise, the condition may have not changed, and the thread making the
* change have already decided not to wake us, as our state was FS_RUNNING
* at the time.
*
* Also need a memory barrier to ensure this extra check is race-free.
* but that is already provided by xcng
*/
bool waited = (*check_callback)(check_arg);
if (waited)
{
struct timespec timeout;
timeout.tv_sec = 0;
timeout.tv_nsec = nsec;
futex_wait(val, thr_wait::FS_SLEEPING, &timeout);
}
xcng(val, thr_wait::FS_RUNNING);
return waited;
}
static inline
int
wakeup(struct thr_wait* wait)
{
volatile unsigned * val = &wait->m_futex_state;
/**
* We must ensure that any state update (new data in buffers...) are visible
* to the other thread before we can look at the sleep state of that other
* thread.
*/
if (xcng(val, thr_wait::FS_RUNNING) == thr_wait::FS_SLEEPING)
{
return futex_wake(val);
}
return 0;
}
#else
#include <NdbMutex.h>
#include <NdbCondition.h>
struct thr_wait
{
bool m_need_wakeup;
NdbMutex *m_mutex;
NdbCondition *m_cond;
thr_wait() : m_need_wakeup(false), m_mutex(0), m_cond(0) {}
void init() {
m_mutex = NdbMutex_Create();
m_cond = NdbCondition_Create();
}
};
static inline
bool
yield(struct thr_wait* wait, const Uint32 nsec,
bool (*check_callback)(struct thr_data *), struct thr_data *check_arg)
{
struct timespec end;
NdbCondition_ComputeAbsTime(&end, nsec/1000000);
NdbMutex_Lock(wait->m_mutex);
Uint32 waits = 0;
/* May have spurious wakeups: Always recheck condition predicate */
while ((*check_callback)(check_arg))
{
wait->m_need_wakeup = true;
waits++;
if (NdbCondition_WaitTimeoutAbs(wait->m_cond,
wait->m_mutex, &end) == ETIMEDOUT)
{
wait->m_need_wakeup = false;
break;
}
}
NdbMutex_Unlock(wait->m_mutex);
return (waits > 0);
}
static inline
int
wakeup(struct thr_wait* wait)
{
NdbMutex_Lock(wait->m_mutex);
// We should avoid signaling when not waiting for wakeup
if (wait->m_need_wakeup)
{
wait->m_need_wakeup = false;
NdbCondition_Signal(wait->m_cond);
}
NdbMutex_Unlock(wait->m_mutex);
return 0;
}
#endif
#ifdef NDB_HAVE_XCNG
template <unsigned SZ>
struct thr_spin_lock
{
thr_spin_lock(const char * name = 0)
{
m_lock = 0;
register_lock(this, name);
}
union {
volatile Uint32 m_lock;
char pad[SZ];
};
};
static
ATTRIBUTE_NOINLINE
void
lock_slow(void * sl, volatile unsigned * val)
{
mt_lock_stat* s = lookup_lock(sl); // lookup before owning lock
loop:
Uint32 spins = 0;
do {
spins++;
cpu_pause();
} while (* val == 1);
if (unlikely(xcng(val, 1) != 0))
goto loop;
if (s)
{
s->m_spin_count += spins;
Uint32 count = ++s->m_contended_count;
Uint32 freq = (count > 10000 ? 5000 : (count > 20 ? 200 : 1));
if ((count % freq) == 0)
printf("%s waiting for lock, contentions: %u spins: %u\n",
s->m_name, count, s->m_spin_count);
}
}
template <unsigned SZ>
static
inline
void
lock(struct thr_spin_lock<SZ>* sl)
{
volatile unsigned* val = &sl->m_lock;
if (likely(xcng(val, 1) == 0))
return;
lock_slow(sl, val);
}
template <unsigned SZ>
static
inline
void
unlock(struct thr_spin_lock<SZ>* sl)
{
/**
* Memory barrier here, to make sure all of our stores are visible before
* the lock release is.
*/
mb();
sl->m_lock = 0;
}
template <unsigned SZ>
static
inline
int
trylock(struct thr_spin_lock<SZ>* sl)
{
volatile unsigned* val = &sl->m_lock;
return xcng(val, 1);
}
#else
#define thr_spin_lock thr_mutex
#endif
template <unsigned SZ>
struct thr_mutex
{
thr_mutex(const char * name = 0) {
NdbMutex_Init(&m_mutex);
register_lock(this, name);
}
union {
NdbMutex m_mutex;
char pad[SZ];
};
};
template <unsigned SZ>
static
inline
void
lock(struct thr_mutex<SZ>* sl)
{
NdbMutex_Lock(&sl->m_mutex);
}
template <unsigned SZ>
static
inline
void
unlock(struct thr_mutex<SZ>* sl)
{
NdbMutex_Unlock(&sl->m_mutex);
}
template <unsigned SZ>
static
inline
int
trylock(struct thr_mutex<SZ> * sl)
{
return NdbMutex_Trylock(&sl->m_mutex);
}
/**
* thr_safe_pool
*/
template<typename T>
struct thr_safe_pool
{
thr_safe_pool(const char * name) : m_free_list(0), m_cnt(0), m_lock(name) {}
T* m_free_list;
Uint32 m_cnt;
thr_spin_lock<NDB_CL - (sizeof(void*) + sizeof(Uint32))> m_lock;
T* seize(Ndbd_mem_manager *mm, Uint32 rg) {
T* ret = 0;
lock(&m_lock);
if (m_free_list)
{
assert(m_cnt);
m_cnt--;
ret = m_free_list;
m_free_list = ret->m_next;
unlock(&m_lock);
}
else
{
Uint32 dummy;
unlock(&m_lock);
ret = reinterpret_cast<T*>
(mm->alloc_page(rg, &dummy,
Ndbd_mem_manager::NDB_ZONE_ANY));
// ToDo: How to deal with failed allocation?!?
// I think in this case we need to start grabbing buffers kept for signal
// trace.
}
return ret;
}
void release(Ndbd_mem_manager *mm, Uint32 rg, T* t) {
lock(&m_lock);
t->m_next = m_free_list;
m_free_list = t;
m_cnt++;
unlock(&m_lock);
}
void release_list(Ndbd_mem_manager *mm, Uint32 rg,
T* head, T* tail, Uint32 cnt) {
lock(&m_lock);
tail->m_next = m_free_list;
m_free_list = head;
m_cnt += cnt;
unlock(&m_lock);
}
};
/**
* thread_local_pool
*/
template<typename T>
class thread_local_pool
{
public:
thread_local_pool(thr_safe_pool<T> *global_pool, unsigned max_free) :
m_max_free(max_free),
m_free(0),
m_freelist(0),
m_global_pool(global_pool)
{
}
T *seize(Ndbd_mem_manager *mm, Uint32 rg) {
T *tmp = m_freelist;
if (tmp)
{
m_freelist = tmp->m_next;
assert(m_free > 0);
m_free--;
}
else
tmp = m_global_pool->seize(mm, rg);
validate();
return tmp;
}
void release(Ndbd_mem_manager *mm, Uint32 rg, T *t) {
unsigned free = m_free;
if (free < m_max_free)
{
m_free = free + 1;
t->m_next = m_freelist;
m_freelist = t;
}
else
m_global_pool->release(mm, rg, t);
validate();
}
/**
* Release to local pool even if it get's "too" full
* (wrt to m_max_free)
*/
void release_local(T *t) {
m_free++;
t->m_next = m_freelist;
m_freelist = t;
validate();
}
void validate() const {
#ifdef VM_TRACE
Uint32 cnt = 0;
T* t = m_freelist;
while (t)
{
cnt++;
t = t->m_next;
}
assert(cnt == m_free);
#endif
}
/**
* Release entries so that m_max_free is honored
* (likely used together with release_local)
*/
void release_global(Ndbd_mem_manager *mm, Uint32 rg) {
validate();
unsigned cnt = 0;
unsigned free = m_free;
Uint32 maxfree = m_max_free;
assert(maxfree > 0);
T* head = m_freelist;
T* tail = m_freelist;
if (free > maxfree)
{
cnt++;
free--;
while (free > maxfree)
{
cnt++;
free--;
tail = tail->m_next;
}
assert(free == maxfree);
m_free = free;
m_freelist = tail->m_next;
m_global_pool->release_list(mm, rg, head, tail, cnt);
}
validate();
}
void release_all(Ndbd_mem_manager *mm, Uint32 rg) {
validate();
T* head = m_freelist;
T* tail = m_freelist;
if (tail)
{
unsigned cnt = 1;
while (tail->m_next != 0)
{
cnt++;
tail = tail->m_next;
}
m_global_pool->release_list(mm, rg, head, tail, cnt);
m_free = 0;
m_freelist = 0;
}
validate();
}
void set_pool(thr_safe_pool<T> * pool) { m_global_pool = pool; }
private:
unsigned m_max_free;
unsigned m_free;
T *m_freelist;
thr_safe_pool<T> *m_global_pool;
};
/**
* Signal buffers.
*
* Each thread job queue contains a list of these buffers with signals.
*
* There is an underlying assumption that the size of this structure is the
* same as the global memory manager page size.
*/
struct thr_job_buffer // 32k
{
static const unsigned SIZE = 8190;
/*
* Amount of signal data currently in m_data buffer.
* Read/written by producer, read by consumer.
*/
Uint32 m_len;
/*
* Whether this buffer contained prio A or prio B signals, used when dumping
* signals from released buffers.
*/
Uint32 m_prioa;
union {
Uint32 m_data[SIZE];
thr_job_buffer * m_next; // For free-list
};
};
static
inline
Uint32
calc_fifo_used(Uint32 ri, Uint32 wi, Uint32 sz)
{
return (wi >= ri) ? wi - ri : (sz - ri) + wi;
}
/**
* thr_job_queue is shared between consumer / producer.
*
* The hot-spot of the thr_job_queue are the read/write indexes.
* As they are updated and read frequently they have been placed
* in its own thr_job_queue_head[] in order to make them fit inside a
* single/few cache lines and thereby avoid complete L1-cache replacement
* every time the job_queue is scanned.
*/
struct thr_job_queue_head
{
unsigned m_read_index; // Read/written by consumer, read by producer
unsigned m_write_index; // Read/written by producer, read by consumer
Uint32 used() const;
};
struct thr_job_queue
{
static const unsigned SIZE = 31;
struct thr_job_queue_head* m_head;
struct thr_job_buffer* m_buffers[SIZE];
};
inline
Uint32
thr_job_queue_head::used() const
{
return calc_fifo_used(m_read_index, m_write_index, thr_job_queue::SIZE);
}
/*
* Two structures tightly associated with thr_job_queue.
*
* There will generally be exactly one thr_jb_read_state and one
* thr_jb_write_state associated with each thr_job_queue.
*
* The reason they are kept separate is to avoid unnecessary inter-CPU
* cache line pollution. All fields shared among producer and consumer
* threads are in thr_job_queue, thr_jb_write_state fields are only
* accessed by the producer thread(s), and thr_jb_read_state fields are
* only accessed by the consumer thread.
*
* For example, on Intel core 2 quad processors, there is a ~33%
* penalty for two cores accessing the same 64-byte cacheline.
*/
struct thr_jb_write_state
{
/*
* The position to insert the next signal into the queue.
*
* m_write_index is the index into thr_job_queue::m_buffers[] of the buffer
* to insert into, and m_write_pos is the index into thr_job_buffer::m_data[]
* at which to store the next signal.
*/
Uint32 m_write_index;
Uint32 m_write_pos;
/* Thread-local copy of thr_job_queue::m_buffers[m_write_index]. */
thr_job_buffer *m_write_buffer;
/* Number of signals inserted since last flush to thr_job_queue. */
Uint32 m_pending_signals;
/* Number of signals inserted since last wakeup */
Uint32 m_pending_signals_wakeup;
};
/*
* This structure is also used when dumping signal traces, to dump executed
* signals from the buffer(s) currently being processed.
*/
struct thr_jb_read_state
{
/*
* Index into thr_job_queue::m_buffers[] of the buffer that we are currently
* executing signals from.
*/
Uint32 m_read_index;
/*
* Index into m_read_buffer->m_data[] of the next signal to execute from the
* current buffer.
*/
Uint32 m_read_pos;
/*
* Thread local copy of thr_job_queue::m_buffers[m_read_index].
*/
thr_job_buffer *m_read_buffer;
/*
* These are thread-local copies of thr_job_queue::m_write_index and
* thr_job_buffer::m_len. They are read once at the start of the signal
* execution loop and used to determine when the end of available signals is
* reached.
*/
Uint32 m_read_end; // End within current thr_job_buffer. (*m_read_buffer)
Uint32 m_write_index; // Last available thr_job_buffer.
bool is_empty() const
{
assert(m_read_index != m_write_index || m_read_pos <= m_read_end);
return (m_read_index == m_write_index) && (m_read_pos >= m_read_end);
}
};
/**
* time-queue
*/
struct thr_tq
{
static const unsigned SQ_SIZE = 512;
static const unsigned LQ_SIZE = 512;
static const unsigned PAGES = 32 * (SQ_SIZE + LQ_SIZE) / 8192;
Uint32 * m_delayed_signals[PAGES];
Uint32 m_next_free;
Uint32 m_next_timer;
Uint32 m_current_time;
Uint32 m_cnt[2];
Uint32 m_short_queue[SQ_SIZE];
Uint32 m_long_queue[LQ_SIZE];
};
/*
* Max number of thread-local job buffers to keep before releasing to
* global pool.
*/
#define THR_FREE_BUF_MAX 32
/* Minimum number of buffers (to ensure useful trace dumps). */
#define THR_FREE_BUF_MIN 12
/*
* 1/THR_FREE_BUF_BATCH is the fraction of job buffers to allocate/free
* at a time from/to global pool.
*/
#define THR_FREE_BUF_BATCH 6
/**
* a page with send data
*/
struct thr_send_page
{
static const Uint32 PGSIZE = 32768;
#if SIZEOF_CHARP == 4
static const Uint32 HEADER_SIZE = 8;
#else
static const Uint32 HEADER_SIZE = 12;
#endif
static Uint32 max_bytes() {
return PGSIZE - offsetof(thr_send_page, m_data);
}
/* Next page */
thr_send_page* m_next;
/* Bytes of send data available in this page. */
Uint16 m_bytes;
/* Start of unsent data */
Uint16 m_start;
/* Data; real size is to the end of one page. */
char m_data[2];
};
/**
* a linked list with thr_send_page
*/
struct thr_send_buffer
{
thr_send_page* m_first_page;
thr_send_page* m_last_page;
};
/**
* a ring buffer with linked list of thr_send_page
*/
struct thr_send_queue
{
unsigned m_write_index;
#if SIZEOF_CHARP == 8
unsigned m_unused;
thr_send_page* m_buffers[7];
static const unsigned SIZE = 7;
#else
thr_send_page* m_buffers[15];
static const unsigned SIZE = 15;
#endif
};
struct thr_data
{
thr_data() : m_jba_write_lock("jbalock"),
m_send_buffer_pool(0, THR_FREE_BUF_MAX) {}
thr_wait m_waiter;
unsigned m_thr_no;
/**
* max signals to execute per JBB buffer
*/
unsigned m_max_signals_per_jb;
/**
* max signals to execute before recomputing m_max_signals_per_jb
*/
unsigned m_max_exec_signals;
Uint64 m_time;
struct thr_tq m_tq;
/* Prio A signal incoming queue. */
struct thr_spin_lock<64> m_jba_write_lock;
struct thr_job_queue m_jba;
struct thr_job_queue_head m_jba_head;
/* Thread-local read state of prio A buffer. */
struct thr_jb_read_state m_jba_read_state;
/*
* There is no m_jba_write_state, as we have multiple writers to the prio A
* queue, so local state becomes invalid as soon as we release the lock.
*/
/*
* In m_next_buffer we keep a free buffer at all times, so that when
* we hold the lock and find we need a new buffer, we can use this and this
* way defer allocation to after releasing the lock.
*/
struct thr_job_buffer* m_next_buffer;
/*
* We keep a small number of buffers in a thread-local cyclic FIFO, so that
* we can avoid going to the global pool in most cases, and so that we have
* recent buffers available for dumping in trace files.
*/
struct thr_job_buffer *m_free_fifo[THR_FREE_BUF_MAX];
/* m_first_free is the index of the entry to return next from seize(). */
Uint32 m_first_free;
/* m_first_unused is the first unused entry in m_free_fifo. */
Uint32 m_first_unused;
/*
* These are the thread input queues, where other threads deliver signals
* into.
*/
struct thr_job_queue_head m_in_queue_head[MAX_THREADS];
struct thr_job_queue m_in_queue[MAX_THREADS];
/* These are the write states of m_in_queue[self] in each thread. */
struct thr_jb_write_state m_write_states[MAX_THREADS];
/* These are the read states of all of our own m_in_queue[]. */
struct thr_jb_read_state m_read_states[MAX_THREADS];
/* Jam buffers for making trace files at crashes. */
EmulatedJamBuffer m_jam;
/* Watchdog counter for this thread. */
Uint32 m_watchdog_counter;
/* Signal delivery statistics. */
Uint32 m_prioa_count;
Uint32 m_prioa_size;
Uint32 m_priob_count;
Uint32 m_priob_size;
/* Array of node ids with pending remote send data. */
Uint8 m_pending_send_nodes[MAX_NTRANSPORTERS];
/* Number of node ids in m_pending_send_nodes. */
Uint32 m_pending_send_count;
/**
* Bitmap of pending node ids with send data.
* Used to quickly check if a node id is already in m_pending_send_nodes.
*/
Bitmask<(MAX_NTRANSPORTERS+31)/32> m_pending_send_mask;
/* pool for send buffers */
class thread_local_pool<thr_send_page> m_send_buffer_pool;
/* Send buffer for this thread, these are not touched by any other thread */
struct thr_send_buffer m_send_buffers[MAX_NTRANSPORTERS];
/* Block instances (main and worker) handled by this thread. */
/* Used for sendpacked (send-at-job-buffer-end). */
Uint32 m_instance_count;
BlockNumber m_instance_list[MAX_INSTANCES_PER_THREAD];
SectionSegmentPool::Cache m_sectionPoolCache;
Uint32 m_cpu;
pthread_t m_thr_id;
NdbThread* m_thread;
};
struct mt_send_handle : public TransporterSendBufferHandle
{
struct thr_data * m_selfptr;
mt_send_handle(thr_data* ptr) : m_selfptr(ptr) {}
virtual ~mt_send_handle() {}
virtual Uint32 *getWritePtr(NodeId node, Uint32 len, Uint32 prio, Uint32 max);
virtual Uint32 updateWritePtr(NodeId node, Uint32 lenBytes, Uint32 prio);
virtual bool forceSend(NodeId node);
};
struct trp_callback : public TransporterCallbackKernel
{
trp_callback() {}
/* Callback interface. */
int checkJobBuffer();
void reportSendLen(NodeId nodeId, Uint32 count, Uint64 bytes);
void lock_transporter(NodeId node);
void unlock_transporter(NodeId node);
Uint32 get_bytes_to_send_iovec(NodeId node, struct iovec *dst, Uint32 max);
Uint32 bytes_sent(NodeId node, Uint32 bytes);
bool has_data_to_send(NodeId node);
void reset_send_buffer(NodeId node, bool should_be_empty);
};
extern trp_callback g_trp_callback; // Forward declaration
extern struct thr_repository g_thr_repository;
#include <NdbMutex.h>
#include <NdbCondition.h>
struct thr_repository
{
thr_repository()
: m_receive_lock("recvlock"),
m_section_lock("sectionlock"),
m_mem_manager_lock("memmanagerlock"),
m_jb_pool("jobbufferpool"),
m_sb_pool("sendbufferpool")
{}
struct thr_spin_lock<64> m_receive_lock;
struct thr_spin_lock<64> m_section_lock;
struct thr_spin_lock<64> m_mem_manager_lock;
struct thr_safe_pool<thr_job_buffer> m_jb_pool;
struct thr_safe_pool<thr_send_page> m_sb_pool;
Ndbd_mem_manager * m_mm;
unsigned m_thread_count;
struct thr_data m_thread[MAX_THREADS];
/**
* send buffer handling
*/
/* The buffers that are to be sent */
struct send_buffer
{
/**
* lock
*/
struct thr_spin_lock<8> m_send_lock;
/**
* pending data
*/
struct thr_send_buffer m_buffer;
/**
* Flag used to coordinate sending to same remote node from different
* threads.
*
* If two threads need to send to the same node at the same time, the
* second thread, rather than wait for the first to finish, will just
* set this flag, and the first thread will do an extra send when done
* with the first.
*/
Uint32 m_force_send;
/**
* Which thread is currently holding the m_send_lock
*/
Uint32 m_send_thread;
/**
* bytes pending for this node
*/
Uint32 m_bytes;
/* read index(es) in thr_send_queue */
Uint32 m_read_index[MAX_THREADS];