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
|
/*
* Copyright (C) 2019 Codership Oy <info@codership.com>
*
* This file is part of wsrep-lib.
*
* Wsrep-lib 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, either version 2 of the License, or
* (at your option) any later version.
*
* Wsrep-lib 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 wsrep-lib. If not, see <https://www.gnu.org/licenses/>.
*/
#include "db_threads.hpp"
#include "wsrep/compiler.hpp"
#include "wsrep/logger.hpp"
#include <cassert>
#include <cstdint>
#include <pthread.h>
#include <algorithm>
#include <atomic>
#include <array>
#include <chrono>
#include <map>
#include <mutex>
#include <ostream>
#include <sstream>
#include <thread>
#include <unordered_map>
#include <vector>
extern "C" { static void* start_thread(void* args_ptr); }
namespace
{
struct ti_obj
{
};
enum ti_opcode
{
oc_thread_create,
oc_thread_destroy,
oc_mutex_create,
oc_mutex_destroy,
oc_mutex_lock,
oc_mutex_trylock,
oc_mutex_unlock,
oc_cond_create,
oc_cond_destroy,
oc_cond_wait,
oc_cond_timedwait,
oc_cond_signal,
oc_cond_broadcast,
oc_max // must be the last
};
static const char* ti_opstring(enum ti_opcode op)
{
switch (op)
{
case oc_thread_create: return "thread_create";
case oc_thread_destroy: return "thread_destroy";
case oc_mutex_create: return "mutex_create";
case oc_mutex_destroy: return "mutex_destroy";
case oc_mutex_lock: return "mutex_lock";
case oc_mutex_trylock: return "mutex_trylock";
case oc_mutex_unlock: return "mutex_unlock";
case oc_cond_create: return "cond_create";
case oc_cond_destroy: return "cond_destroy";
case oc_cond_wait: return "cond_wait";
case oc_cond_timedwait: return "cond_timedwait";
case oc_cond_signal: return "cond_signal";
case oc_cond_broadcast: return "cond_broadcast";
default: return "unknown";
}
}
static std::vector<std::string> key_vec;
static std::atomic<int> key_cnt;
static std::vector<std::vector<size_t>> ops_map;
static std::vector<std::mutex*> ops_map_sync;
static struct ops_map_sync_deleter
{
~ops_map_sync_deleter()
{
std::for_each(ops_map_sync.begin(), ops_map_sync.end(),
[](auto entry) { delete entry; });
}
} ops_map_sync_deleter;
static std::array<std::atomic<size_t>, oc_max> total_ops;
static std::atomic<size_t> total_allocations;
static std::atomic<size_t> mutex_contention;
static std::unordered_map<std::string, size_t> mutex_contention_counts;
static int op_level;
// Check correct condition variable usage:
// - Associated mutex must be locked when waiting for cond
// - There must be at least one waiter when signalling for condition
static bool cond_checks;
static inline void cond_check(bool condition, const char* name,
const char* message)
{
if (cond_checks && !condition)
{
wsrep::log_error() << "Condition variable check failed for '"
<< name << "': " << message;
::abort();
}
}
static inline int append_key(const char* name, const char* type)
{
key_vec.push_back(std::string(name) + "_" + type);
wsrep::log_info() << "Register key " << name << "_" << type
<< " with index " << (key_cnt + 1);
ops_map.push_back(std::vector<size_t>());
ops_map_sync.push_back(new std::mutex());
ops_map.back().resize(oc_max);
return ++key_cnt;
}
template <class Key> static inline size_t get_key_index(const Key* key)
{
size_t index(reinterpret_cast<const size_t>(key) - 1);
assert(index < key_vec.size());
return index;
}
template <class Key>
static inline const char* get_key_name(const Key* key)
{
return key_vec[get_key_index(key)].c_str();
}
static inline const std::string& get_key_name_by_index(size_t index)
{
assert(index < key_vec.size());
return key_vec[index];
}
// Note: Do not refer the obj pointer in this function, it may
// have been deleted before the call.
template <class Key>
static inline void update_ops(const ti_obj* obj,
const Key* key,
enum ti_opcode op)
{
if (op_level < 1)
return;
total_ops[op] += 1;
if (op_level < 2)
return;
if (false && op == oc_mutex_destroy)
{
wsrep::log_info() << "thread: " << std::this_thread::get_id()
<< " object: " << obj
<< ": name: " << get_key_name(key)
<< " op: " << ti_opstring(op);
}
std::lock_guard<std::mutex> lock(*ops_map_sync[get_key_index(key)]);
ops_map[get_key_index(key)][op] += 1;
}
struct thread_args
{
void* this_thread;
void* (*fn)(void*);
void* args;
};
pthread_key_t this_thread_key;
struct this_thread_key_initializer
{
this_thread_key_initializer()
{
pthread_key_create(&this_thread_key, nullptr);
}
~this_thread_key_initializer()
{
pthread_key_delete(this_thread_key);
}
};
class ti_thread : public ti_obj
{
public:
ti_thread(const wsrep::thread_service::thread_key* key)
: key_(key)
, th_()
, retval_()
, detached_()
{
update_ops(this, key_, oc_thread_create);
}
~ti_thread()
{
update_ops(this, key_, oc_thread_destroy);
}
ti_thread(const ti_thread&) = delete;
ti_thread& operator=(const ti_thread&) = delete;
int run(void* (*fn)(void *), void* args)
{
auto ta(new thread_args{this, fn, args});
return pthread_create(&th_, nullptr, start_thread, ta);
}
int detach()
{
detached_ = true;
return pthread_detach(th_);
}
int join(void** retval)
{
return pthread_join(th_, retval);
}
bool detached() const { return detached_; }
void retval(void* retval) { retval_ = retval; }
static ti_thread* self()
{
return reinterpret_cast<ti_thread*>(
pthread_getspecific(this_thread_key));
}
int setschedparam(int policy, const struct sched_param* param)
{
return pthread_setschedparam(th_, policy, param);
}
int getschedparam(int* policy, struct sched_param* param)
{
return pthread_getschedparam(th_, policy, param);
}
int equal(ti_thread* other)
{
return pthread_equal(th_, other->th_);
}
private:
const wsrep::thread_service::thread_key* key_;
pthread_t th_;
void* retval_;
bool detached_;
};
class ti_mutex : public ti_obj
{
public:
ti_mutex(const wsrep::thread_service::mutex_key* key, bool inplace)
: mutex_(PTHREAD_MUTEX_INITIALIZER)
, key_(key)
, inplace_(inplace)
#ifndef NDEBUG
, locked_()
, owner_()
#endif // ! NDEBUG
{
update_ops(this, key_, oc_mutex_create);
if (not inplace) total_allocations++;
}
~ti_mutex() { update_ops(this, key_, oc_mutex_destroy); }
ti_mutex& operator=(const ti_mutex&) = delete;
ti_mutex(const ti_mutex&) = delete;
int lock()
{
update_ops(this, key_, oc_mutex_lock);
int ret(pthread_mutex_trylock(&mutex_));
if (ret == EBUSY)
{
mutex_contention++;
{
std::lock_guard<std::mutex> lock(*ops_map_sync[get_key_index(key_)]);
mutex_contention_counts[get_key_name(key_)] += 1;
}
ret = pthread_mutex_lock(&mutex_);
}
#ifndef NDEBUG
if (ret == 0)
{
assert(owner_ == std::thread::id());
locked_ = true;
owner_ = std::this_thread::get_id();
}
#endif // ! NDEBUG
return ret;
}
int trylock()
{
update_ops(this, key_, oc_mutex_trylock);
int ret(pthread_mutex_trylock(&mutex_));
#ifndef NDEBUG
if (ret == 0)
{
assert(owner_ == std::thread::id());
locked_ = true;
owner_ = std::this_thread::get_id();
}
#endif // ! NDEBUG
return ret;
}
int unlock()
{
assert(locked_);
#ifndef NDEBUG
assert(owner_ == std::this_thread::get_id());
owner_ = std::thread::id();
#endif // ! NDEBUG
// Use temporary object. After mutex is unlocked it may be
// destroyed before this update_ops() finishes.
auto key(key_);
int ret(pthread_mutex_unlock(&mutex_));
update_ops(this, key, oc_mutex_unlock);
return ret;
}
struct condwait_context
{
#ifndef NDEBUG
bool locked;
std::thread::id owner;
#endif // ! NDEBUG
};
condwait_context save_for_condwait()
{
#ifndef NDEBUG
return condwait_context{ locked_, owner_ };
#else
return condwait_context{};
#endif // ! NDEBUG
}
void reset()
{
#ifndef NDEBUG
locked_ = false;
owner_ = std::thread::id();
#endif // ! NDEBUG
}
void restore_from_condwait(const condwait_context& ctx WSREP_UNUSED)
{
#ifndef NDEBUG
locked_ = ctx.locked;
owner_ = ctx.owner;
#endif // ! NDEBUG
}
pthread_mutex_t* native_handle() { return &mutex_; }
const wsrep::thread_service::mutex_key* key() const { return key_; }
bool inplace() const { return inplace_; }
private:
pthread_mutex_t mutex_;
const wsrep::thread_service::mutex_key* key_;
const bool inplace_;
#ifndef NDEBUG
bool locked_;
std::atomic<std::thread::id> owner_;
#endif // ! NDEBU
};
class ti_cond : public ti_obj
{
public:
ti_cond(const wsrep::thread_service::cond_key* key, bool inplace)
: cond_(PTHREAD_COND_INITIALIZER)
, key_(key)
, inplace_(inplace)
, waiter_()
{
update_ops(this, key_, oc_cond_create);
if (not inplace) total_allocations++;
}
~ti_cond() { update_ops(this, key_, oc_cond_destroy); }
ti_cond& operator=(const ti_cond&) = delete;
ti_cond(const ti_cond&) = delete;
int wait(ti_mutex& mutex)
{
cond_check(pthread_mutex_trylock(mutex.native_handle()),
get_key_name(key_), "Mutex not locked in cond wait");
waiter_ = true;
update_ops(this, key_, oc_cond_wait);
// update_ops(&mutex, mutex.key(), oc_mutex_unlock);
auto condwait_ctx(mutex.save_for_condwait());
mutex.reset();
int ret(pthread_cond_wait(&cond_, mutex.native_handle()));
// update_ops(&mutex, mutex.key(), oc_mutex_lock);
mutex.restore_from_condwait(condwait_ctx);
waiter_ = false;
return ret;
}
int timedwait(ti_mutex& mutex, const struct timespec* ts)
{
cond_check(pthread_mutex_trylock(mutex.native_handle()),
get_key_name(key_), "Mutex not locked in cond wait");
waiter_ = true;
update_ops(this, key_, oc_cond_timedwait);
// update_ops(&mutex, mutex.key(), oc_mutex_unlock);
auto condwait_ctx(mutex.save_for_condwait());
mutex.reset();
int ret(pthread_cond_timedwait(&cond_, mutex.native_handle(), ts));
// update_ops(&mutex, mutex.key(), oc_mutex_lock);
mutex.restore_from_condwait(condwait_ctx);
waiter_ = false;
return ret;
}
int signal()
{
update_ops(this, key_, oc_cond_signal);
cond_check(waiter_, get_key_name(key_),
"Signalling condition variable without waiter");
return pthread_cond_signal(&cond_);
}
int broadcast()
{
update_ops(this, key_, oc_cond_broadcast);
return pthread_cond_broadcast(&cond_);
}
bool inplace() const { return inplace_; }
private:
pthread_cond_t cond_;
const wsrep::thread_service::cond_key* key_;
const bool inplace_;
bool waiter_;
};
}
int db::ti::before_init()
{
wsrep::log_info() << "db::ti::before_init()";
return 0;
}
int db::ti::after_init()
{
wsrep::log_info() << "db::ti::after_init()";
return 0;
}
//////////////////////////////////////////////////////////////////////////////
// Thread //
//////////////////////////////////////////////////////////////////////////////
extern "C"
{
static void* start_thread(void* args_ptr)
{
thread_args* ta(reinterpret_cast<thread_args*>(args_ptr));
ti_thread* thread = reinterpret_cast<ti_thread*>(ta->this_thread);
pthread_setspecific(this_thread_key, thread);
void* (*fn)(void*) = ta->fn;
void* args = ta->args;
delete ta;
void* ret = (*fn)(args);
pthread_setspecific(this_thread_key, nullptr);
// If we end here the thread returned instead of calling
// pthread_exit()
if (thread->detached())
delete thread;
return ret;
}
WSREP_NORETURN
static void exit_thread(wsrep::thread_service::thread* thread, void* retval)
{
pthread_setspecific(this_thread_key, nullptr);
ti_thread* th(reinterpret_cast<ti_thread*>(thread));
th->retval(retval);
if (th->detached())
delete th;
pthread_exit(retval);
}
} // extern "C"
db::ti::ti()
{
thread_service::exit = exit_thread;
}
const wsrep::thread_service::thread_key*
db::ti::create_thread_key(const char* name) WSREP_NOEXCEPT
{
assert(name);
return reinterpret_cast<const wsrep::thread_service::thread_key*>(
append_key(name, "thread"));
}
int db::ti::create_thread(const wsrep::thread_service::thread_key* key,
wsrep::thread_service::thread** thread,
void* (*fn)(void*), void* args) WSREP_NOEXCEPT
{
auto pit(new ti_thread(key));
total_allocations++;
int ret;
if ((ret = pit->run(fn, args)))
{
delete pit;
}
else
{
*thread = reinterpret_cast<wsrep::thread_service::thread*>(pit);
}
return ret;
}
int db::ti::detach(wsrep::thread_service::thread* thread) WSREP_NOEXCEPT
{
return reinterpret_cast<ti_thread*>(thread)->detach();
}
int db::ti::equal(wsrep::thread_service::thread* thread_1,
wsrep::thread_service::thread* thread_2) WSREP_NOEXCEPT
{
return (reinterpret_cast<ti_thread*>(thread_1)->equal(
reinterpret_cast<ti_thread*>(thread_2)));
}
int db::ti::join(wsrep::thread_service::thread* thread, void** retval) WSREP_NOEXCEPT
{
ti_thread* th(reinterpret_cast<ti_thread*>(thread));
int ret(th->join(retval));
if (not th->detached())
{
delete th;
}
return ret;
}
wsrep::thread_service::thread* db::ti::self() WSREP_NOEXCEPT
{
return reinterpret_cast<wsrep::thread_service::thread*>(ti_thread::self());
}
int db::ti::setschedparam(wsrep::thread_service::thread* thread,
int policy, const struct sched_param* param) WSREP_NOEXCEPT
{
return reinterpret_cast<ti_thread*>(thread)->setschedparam(policy, param);
}
int db::ti::getschedparam(wsrep::thread_service::thread* thread,
int* policy, struct sched_param* param) WSREP_NOEXCEPT
{
return reinterpret_cast<ti_thread*>(thread)->getschedparam(policy, param);
}
//////////////////////////////////////////////////////////////////////////////
// Mutex //
//////////////////////////////////////////////////////////////////////////////
const wsrep::thread_service::mutex_key*
db::ti::create_mutex_key(const char* name) WSREP_NOEXCEPT
{
assert(name);
return reinterpret_cast<const wsrep::thread_service::mutex_key*>(
append_key(name, "mutex"));
}
wsrep::thread_service::mutex*
db::ti::init_mutex(const wsrep::thread_service::mutex_key* key, void* memblock,
size_t memblock_size) WSREP_NOEXCEPT
{
return reinterpret_cast<wsrep::thread_service::mutex*>(
memblock_size >= sizeof(ti_mutex) ? new (memblock) ti_mutex(key, true)
: new ti_mutex(key, false));
}
int db::ti::destroy(wsrep::thread_service::mutex* mutex) WSREP_NOEXCEPT
{
ti_mutex* m(reinterpret_cast<ti_mutex*>(mutex));
if (m->inplace())
{
m->~ti_mutex();
}
else
{
delete m;
}
return 0;
}
int db::ti::lock(wsrep::thread_service::mutex* mutex) WSREP_NOEXCEPT
{
return reinterpret_cast<ti_mutex*>(mutex)->lock();
}
int db::ti::trylock(wsrep::thread_service::mutex* mutex) WSREP_NOEXCEPT
{
return reinterpret_cast<ti_mutex*>(mutex)->trylock();
}
int db::ti::unlock(wsrep::thread_service::mutex* mutex) WSREP_NOEXCEPT
{
return reinterpret_cast<ti_mutex*>(mutex)->unlock();
}
//////////////////////////////////////////////////////////////////////////////
// Cond //
//////////////////////////////////////////////////////////////////////////////
const wsrep::thread_service::cond_key* db::ti::create_cond_key(const char* name) WSREP_NOEXCEPT
{
assert(name);
return reinterpret_cast<const wsrep::thread_service::cond_key*>(
append_key(name, "cond"));
}
wsrep::thread_service::cond*
db::ti::init_cond(const wsrep::thread_service::cond_key* key, void* memblock,
size_t memblock_size) WSREP_NOEXCEPT
{
return reinterpret_cast<wsrep::thread_service::cond*>(
memblock_size >= sizeof(ti_cond) ? new (memblock) ti_cond(key, true)
: new ti_cond(key, false));
}
int db::ti::destroy(wsrep::thread_service::cond* cond) WSREP_NOEXCEPT
{
ti_cond* c(reinterpret_cast<ti_cond*>(cond));
if (c->inplace())
{
c->~ti_cond();
}
else
{
delete c;
}
return 0;
}
int db::ti::wait(wsrep::thread_service::cond* cond,
wsrep::thread_service::mutex* mutex) WSREP_NOEXCEPT
{
return reinterpret_cast<ti_cond*>(cond)->wait(
*reinterpret_cast<ti_mutex*>(mutex));
}
int db::ti::timedwait(wsrep::thread_service::cond* cond,
wsrep::thread_service::mutex* mutex,
const struct timespec* ts) WSREP_NOEXCEPT
{
return reinterpret_cast<ti_cond*>(cond)->timedwait(
*reinterpret_cast<ti_mutex*>(mutex), ts);
}
int db::ti::signal(wsrep::thread_service::cond* cond) WSREP_NOEXCEPT
{
return reinterpret_cast<ti_cond*>(cond)->signal();
}
int db::ti::broadcast(wsrep::thread_service::cond* cond) WSREP_NOEXCEPT
{
return reinterpret_cast<ti_cond*>(cond)->broadcast();
}
void db::ti::level(int level)
{
::op_level = level;
}
void db::ti::cond_checks(bool cond_checks)
{
if (cond_checks)
wsrep::log_info() << "Enabling condition variable checking";
::cond_checks = cond_checks;
}
std::string db::ti::stats()
{
std::ostringstream os;
os << "Totals:\n";
for (size_t i(0); i < total_ops.size(); ++i)
{
if (total_ops[i] > 0)
{
os << " " << ti_opstring(static_cast<enum ti_opcode>(i)) << ": "
<< total_ops[i] << "\n";
}
}
os << "Total allocations: " << total_allocations << "\n";
os << "Mutex contention: " << mutex_contention << "\n";
for (auto i : mutex_contention_counts)
{
os << " " << i.first << ": " << i.second << "\n";
}
os << "Per key:\n";
std::map<std::string, std::vector<size_t>> sorted;
for (size_t i(0); i < ops_map.size(); ++i)
{
sorted.insert(std::make_pair(get_key_name_by_index(i), ops_map[i]));
}
for (auto i : sorted)
{
for (size_t j(0); j < i.second.size(); ++j)
{
if (i.second[j])
{
os << " " << i.first << ": "
<< ti_opstring(static_cast<enum ti_opcode>(j)) << ": "
<< i.second[j] << "\n";
}
}
}
return os.str();
}
|