-
Notifications
You must be signed in to change notification settings - Fork 357
/
Copy pathtest_thread_utility.cpp
executable file
·203 lines (170 loc) · 5.16 KB
/
test_thread_utility.cpp
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
#include <type_traits>
#include <iostream>
#include <shared_mutex> // std::shared_lock
#include <utility>
#include <thread>
#include <future> // std::async
#include <atomic>
#include <string> // std::string
#include "capo/spin_lock.hpp"
#include "capo/type_name.hpp"
#include "libipc/rw_lock.h"
#include "test.h"
#include "thread_pool.h"
namespace {
constexpr int LoopCount = 100000;
constexpr int ThreadMax = 8;
template <typename T>
constexpr T acc(T b, T e) noexcept {
return (e + b) * (e - b + 1) / 2;
}
template <typename Mutex>
struct lc_wrapper : Mutex {
void lock_shared () { Mutex::lock (); }
void unlock_shared() { Mutex::unlock(); }
};
template <typename Lc, int Loops = LoopCount>
void benchmark_lc(int w, int r, char const * message) {
ipc_ut::sender().start(static_cast<std::size_t>(w));
ipc_ut::reader().start(static_cast<std::size_t>(r));
ipc_ut::test_stopwatch sw;
std::uint64_t data = 0;
std::uint64_t sum = acc<std::uint64_t>(1, Loops) * w;
Lc lc;
for (int k = 0; k < r; ++k) {
ipc_ut::reader() << [sum, &lc, &data] {
while (1) {
{
std::shared_lock<Lc> guard { lc };
if (data == sum) break;
}
std::this_thread::yield();
}
};
}
for (int k = 0; k < w; ++k) {
ipc_ut::sender() << [&sw, &lc, &data] {
sw.start();
for (int i = 1; i <= Loops; ++i) {
{
std::lock_guard<Lc> guard { lc };
data += i;
}
std::this_thread::yield();
}
};
}
ipc_ut::sender().wait_for_done();
sw.print_elapsed(w, r, Loops, message);
ipc_ut::reader().wait_for_done();
}
void test_lock_performance(int w, int r) {
std::cout << "test_lock_performance: [" << w << "-" << r << "]" << std::endl;
benchmark_lc<ipc::rw_lock >(w, r, "ipc::rw_lock");
benchmark_lc<lc_wrapper< ipc::spin_lock>>(w, r, "ipc::spin_lock");
benchmark_lc<lc_wrapper<capo::spin_lock>>(w, r, "capo::spin_lock");
benchmark_lc<lc_wrapper<std::mutex> >(w, r, "std::mutex");
// benchmark_lc<std::shared_mutex >(w, r, "std::shared_mutex");
}
} // internal-linkage
//TEST(Thread, rw_lock) {
// for (int i = 1; i <= ThreadMax; ++i) test_lock_performance(i, 0);
// for (int i = 1; i <= ThreadMax; ++i) test_lock_performance(1, i);
// for (int i = 2; i <= ThreadMax; ++i) test_lock_performance(i, i);
//}
#if 0 // disable ipc::tls
TEST(Thread, tls_main_thread) {
ipc::tls::pointer<int> p;
EXPECT_FALSE(p);
EXPECT_NE(p.create(123), nullptr);
EXPECT_EQ(*p, 123);
}
namespace {
struct Foo {
std::atomic<int> * px_;
Foo(std::atomic<int> * px) : px_(px) {}
~Foo() {
px_->fetch_add(1, std::memory_order_relaxed);
}
};
} // namespace
TEST(Thread, tls_create_once) {
std::atomic<int> x { 0 };
Foo { &x };
EXPECT_EQ(x, 1);
{
ipc::tls::pointer<Foo> foo;
EXPECT_NE(foo.create(&x), nullptr);
EXPECT_EQ(x, 1);
std::thread {[&foo, &x] {
auto foo1 = foo.create_once(&x);
auto foo2 = foo.create_once(&x);
EXPECT_EQ(foo1, foo2);
}}.join();
EXPECT_EQ(x, 2);
}
EXPECT_EQ(x, 3);
}
TEST(Thread, tls_multi_thread) {
std::atomic<int> x { 0 };
{
ipc::tls::pointer<Foo> foo; // no create
EXPECT_EQ(x, 0);
ipc_ut::thread_pool pool { 10 };
for (int i = 0; i < 1000; ++i) {
pool << [&foo, &x] {
// foo.create_once(&x);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
};
}
pool.wait_for_done();
EXPECT_EQ(x, 0); // thread_pool hasn't destructed.
}
// EXPECT_EQ(x, 10);
}
namespace {
template <typename Tls, typename T>
void benchmark_tls(char const * message) {
ipc_ut::thread_pool pool { static_cast<std::size_t>(ThreadMax) };
ipc_ut::test_stopwatch sw;
pool.wait_for_started();
for (int k = 0; k < ThreadMax; ++k) {
pool << [&sw] {
sw.start();
for (int i = 0; i < LoopCount * 10; ++i) {
*Tls::template get<T>() = i;
}
};
}
pool.wait_for_done();
sw.print_elapsed(ThreadMax, LoopCount * 10, message);
}
struct ipc_tls {
template <typename T>
static T * get() {
static ipc::tls::pointer<T> p;
return p.create_once();
}
};
struct std_tls {
template <typename T>
static T * get() {
thread_local T p;
return &p;
}
};
struct Str {
std::string str_;
Str & operator=(int i) {
str_ = std::to_string(i);
return *this;
}
};
} // internal-linkage
TEST(Thread, tls_benchmark) {
benchmark_tls<std_tls, int>("std_tls: int");
benchmark_tls<ipc_tls, int>("ipc_tls: int");
benchmark_tls<std_tls, Str>("std_tls: Str");
benchmark_tls<ipc_tls, Str>("ipc_tls: Str");
}
#endif