forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadPoolTest.h
420 lines (351 loc) · 14 KB
/
ThreadPoolTest.h
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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include <cxxtest/TestSuite.h>
#include "MantidKernel/FunctionTask.h"
#include "MantidKernel/ProgressBase.h"
#include "MantidKernel/ThreadPool.h"
#include "MantidKernel/ThreadScheduler.h"
#include "MantidKernel/ThreadSchedulerMutexes.h"
#include "MantidKernel/Timer.h"
#include <Poco/Thread.h>
#include <cstdlib>
#include <memory>
using namespace Mantid::Kernel;
//=======================================================================================
class TimeWaster {
public:
static size_t waste_time(double seconds) {
// Waste time, but use up the CPU!
std::size_t num = 0;
Mantid::Kernel::Timer time;
while (time.elapsed(false) < seconds) {
double x = 1.1;
for (int j = 0; j < 100000; j++) {
x = x * x;
x = x + x;
x = x / 1.1;
}
num += 1;
}
return num;
}
void waste_time_with_lock(double seconds) {
{
std::lock_guard<std::mutex> lock(m_mutex);
std::cout << "waste_time for " << seconds << " seconds.\n";
}
waste_time(seconds);
}
/** Add a number but use a lock to avoid contention */
void add_to_number(size_t adding) {
{
std::lock_guard<std::mutex> lock(m_mutex);
total += adding;
}
}
private:
std::mutex m_mutex;
public:
size_t total;
};
//=======================================================================================
int threadpooltest_check = 0;
void threadpooltest_function() { threadpooltest_check = 12; }
std::vector<int> threadpooltest_vec;
void threadpooltest_adding_stuff(int val) {
// TODO: Mutex
threadpooltest_vec.emplace_back(val);
}
// Counter for the test.
size_t TaskThatAddsTasks_counter;
std::mutex TaskThatAddsTasks_mutex;
//=======================================================================================
/** Class that adds tasks to its scheduler */
class TaskThatAddsTasks : public Task {
public:
// ctor
TaskThatAddsTasks(ThreadScheduler *scheduler, size_t depth) : Task(), m_scheduler(scheduler), depth(depth) {
// Use a randomized cost function; this will have an effect on the sorted
// schedulers.
m_cost = rand();
}
// Run the task
void run() override {
if (depth < 4) {
// Add ten tasks (one level deeper)
for (size_t i = 0; i < 10; i++) {
m_scheduler->push(std::make_shared<TaskThatAddsTasks>(m_scheduler, depth + 1));
}
} else {
// Lock to ensure you don't step on yourself.
std::lock_guard<std::mutex> lock(TaskThatAddsTasks_mutex);
// Increment the counter only at the lowest level.
TaskThatAddsTasks_counter += 1;
}
}
private:
ThreadScheduler *m_scheduler;
size_t depth;
};
int ThreadPoolTest_TaskThatThrows_counter = 0;
//=======================================================================================
class ThreadPoolTest : public CxxTest::TestSuite {
public:
/** Make it waste time, 0 to 16 seconds
* DISABLED because it is (intentionally) slow. */
void xtest_Scheduler_LargestCostFirst_wastetime() {
ThreadPool p(new ThreadSchedulerFIFO(), 0);
threadpooltest_vec.clear();
TS_ASSERT_EQUALS(threadpooltest_vec.size(), 0);
TimeWaster mywaster;
for (int i = 0; i < 16; i++) {
double cost = i; // time is exactly i
// Bind to a member function of mywaster
p.schedule(std::make_shared<FunctionTask>(std::bind(&TimeWaster::waste_time_with_lock, &mywaster, i), cost));
}
Timer overall;
TS_ASSERT_THROWS_NOTHING(p.joinAll());
std::cout << overall.elapsed() << " secs total.\n";
}
void test_Constructor() { ThreadPool p; }
void test_schedule() {
ThreadPool p;
TS_ASSERT_EQUALS(threadpooltest_check, 0);
p.schedule(std::make_shared<FunctionTask>(threadpooltest_function));
TS_ASSERT_EQUALS(threadpooltest_check, 0);
TS_ASSERT_THROWS_NOTHING(p.joinAll());
TS_ASSERT_EQUALS(threadpooltest_check, 12);
}
//=======================================================================================
//=======================================================================================
/** Class for debugging progress reporting */
class MyTestProgress : public ProgressBase {
public:
MyTestProgress(double start, double end, int64_t numSteps, ThreadPoolTest *myParent)
: ProgressBase(start, end, numSteps), parent(myParent) {}
void doReport(const std::string &msg = "") override {
parent->last_report_message = msg;
parent->last_report_counter = m_i;
double p = m_start + m_step * double(m_i - m_ifirst);
parent->last_report_value = p;
}
public:
ThreadPoolTest *parent;
};
/// Index that was last set at doReport
int64_t last_report_counter;
double last_report_value;
std::string last_report_message;
void test_with_progress_reporting() {
last_report_counter = 0;
ThreadPool p(new ThreadSchedulerFIFO(), 1, new MyTestProgress(0.0, 1.0, 10, this));
for (int i = 0; i < 10; i++) {
double cost = i;
p.schedule(std::make_shared<FunctionTask>(threadpooltest_function, cost));
}
TS_ASSERT_THROWS_NOTHING(p.joinAll());
// The test reporter was called
TS_ASSERT_EQUALS(last_report_counter, 10);
}
//=======================================================================================
/** Start a threadpool before adding tasks
* DISABLED because the timing issues make it somewhat unreliable under heavy
* loads. */
void xtest_start_and_wait() {
ThreadPool p; // Makes a default scheduler
threadpooltest_check = 0;
// Start and allow it to wait for 1 second
p.start(1.0);
// Simulate doing some work
Poco::Thread::sleep(40);
// Now you add the task
p.schedule(std::make_shared<FunctionTask>(threadpooltest_function));
// Simulate doing more work (this allows the task to run)
Poco::Thread::sleep(40);
// The task ran before we called joinAll(). Magic!
TS_ASSERT_EQUALS(threadpooltest_check, 12);
// Reset and try again. The threads are still waiting, it has been less than
// 1 second.
threadpooltest_check = 0;
p.schedule(std::make_shared<FunctionTask>(threadpooltest_function));
Poco::Thread::sleep(40);
TS_ASSERT_EQUALS(threadpooltest_check, 12);
// You still need to call joinAll() to clean up everything.
p.joinAll();
// Ok, the task did execute.
TS_ASSERT_EQUALS(threadpooltest_check, 12);
}
/** Start a threadpool before adding tasks. But the wait time was too short!
* DISABLED because the timing issues make it somewhat unreliable under heavy
* loads. */
void xtest_start_and_wait_short_wait_time() {
ThreadPool p; // Makes a default scheduler
threadpooltest_check = 0;
// Start and allow it to wait for a very short time
p.start(0.03);
// But it takes too long before the task is actually added
Poco::Thread::sleep(100);
p.schedule(std::make_shared<FunctionTask>(threadpooltest_function));
Poco::Thread::sleep(30);
// So the task has not run, since the threads exited before!
TS_ASSERT_EQUALS(threadpooltest_check, 0);
// But you can still call joinAll() to run the task that is waiting.
p.joinAll();
// Ok, the task did execute.
TS_ASSERT_EQUALS(threadpooltest_check, 12);
}
//=======================================================================================
/** We schedule a task, run the threads, but don't abort them.
* Then we re-schedule stuff, and re-join.
*/
void test_schedule_resume_tasks() {
ThreadPool p; // Makes a default scheduler
threadpooltest_check = 0;
p.schedule(std::make_shared<FunctionTask>(threadpooltest_function));
TS_ASSERT_THROWS_NOTHING(p.joinAll());
// Ok, the task did execute.
TS_ASSERT_EQUALS(threadpooltest_check, 12);
// Now we reset.
threadpooltest_check = 0;
p.schedule(std::make_shared<FunctionTask>(threadpooltest_function));
TS_ASSERT_THROWS_NOTHING(p.joinAll());
TS_ASSERT_EQUALS(threadpooltest_check, 12);
}
void test_Scheduler_FIFO() {
// Only use one core, it'll make things simpler
ThreadPool p(new ThreadSchedulerFIFO(), 1);
TS_ASSERT_EQUALS(threadpooltest_vec.size(), 0);
for (int i = 0; i < 10; i++) {
double cost = i;
p.schedule(std::make_shared<FunctionTask>(std::bind(threadpooltest_adding_stuff, i), cost));
}
TS_ASSERT_THROWS_NOTHING(p.joinAll());
TS_ASSERT_EQUALS(threadpooltest_vec.size(), 10);
if (threadpooltest_vec.size() < 10)
return;
// The first ones added are the first ones run.
TS_ASSERT_EQUALS(threadpooltest_vec[0], 0);
TS_ASSERT_EQUALS(threadpooltest_vec[1], 1);
TS_ASSERT_EQUALS(threadpooltest_vec[2], 2);
}
void test_Scheduler_LIFO() {
ThreadPool p(new ThreadSchedulerLIFO(), 1);
threadpooltest_vec.clear();
TS_ASSERT_EQUALS(threadpooltest_vec.size(), 0);
for (int i = 0; i < 10; i++) {
double cost = i;
p.schedule(std::make_shared<FunctionTask>(std::bind(threadpooltest_adding_stuff, i), cost));
}
TS_ASSERT_THROWS_NOTHING(p.joinAll());
TS_ASSERT_EQUALS(threadpooltest_vec.size(), 10);
// The last ones added are the first ones run.
TS_ASSERT_EQUALS(threadpooltest_vec[0], 9);
TS_ASSERT_EQUALS(threadpooltest_vec[1], 8);
TS_ASSERT_EQUALS(threadpooltest_vec[2], 7);
}
void test_Scheduler_LargestCostFirst() {
// Only use one core, it'll make things simpler
ThreadPool p(new ThreadSchedulerLargestCost(), 1);
threadpooltest_vec.clear();
TS_ASSERT_EQUALS(threadpooltest_vec.size(), 0);
for (int i = 0; i < 10; i++) {
double cost = i;
p.schedule(std::make_shared<FunctionTask>(std::bind(threadpooltest_adding_stuff, i), cost));
}
TS_ASSERT_THROWS_NOTHING(p.joinAll());
TS_ASSERT_EQUALS(threadpooltest_vec.size(), 10);
// The first ones added are the first ones run.
TS_ASSERT_EQUALS(threadpooltest_vec[0], 9);
TS_ASSERT_EQUALS(threadpooltest_vec[1], 8);
TS_ASSERT_EQUALS(threadpooltest_vec[2], 7);
}
//--------------------------------------------------------------------
/** Perform a stress test on the given scheduler.
* This runs a large number of super-short tasks; enough that the
* queue locking is tested against simultaneous access. A segfault
* results if the queue is improperly accessed.
*/
void do_StressTest_scheduler(ThreadScheduler *sched) {
ThreadPool p(sched, 0);
TimeWaster mywaster;
size_t num = 30000;
mywaster.total = 0;
std::shared_ptr<std::mutex> lastMutex;
for (size_t i = 0; i <= num; i++) {
auto task =
std::make_shared<FunctionTask>(std::bind(&TimeWaster::add_to_number, &mywaster, i), static_cast<double>(i));
// Create a new mutex every 1000 tasks. This is more relevant to the
// ThreadSchedulerMutexes; others ignore it.
if (i % 1000 == 0)
lastMutex = std::make_shared<std::mutex>();
task->setMutex(lastMutex);
p.schedule(task);
}
Timer overall;
TS_ASSERT_THROWS_NOTHING(p.joinAll());
// std::cout << overall.elapsed() << " secs total.\n";
// Expected total
size_t expected = (num * num + num) / 2;
TS_ASSERT_EQUALS(mywaster.total, expected);
}
void test_StressTest_ThreadSchedulerFIFO() { do_StressTest_scheduler(new ThreadSchedulerFIFO()); }
void test_StressTest_ThreadSchedulerLIFO() { do_StressTest_scheduler(new ThreadSchedulerLIFO()); }
void test_StressTest_ThreadSchedulerLargestCost() { do_StressTest_scheduler(new ThreadSchedulerLargestCost()); }
void test_StressTest_ThreadSchedulerMutexes() { do_StressTest_scheduler(new ThreadSchedulerMutexes()); }
//--------------------------------------------------------------------
/** Perform a stress test on the given scheduler.
* This one creates tasks that create new tasks; e.g. 10 tasks each add
* 10 tasks, and so on (up to a certain depth).
* So it tests against possible segfaults of one task
* accessing the queue while another thread is popping it.
*/
void do_StressTest_TasksThatCreateTasks(ThreadScheduler *sched) {
ThreadPool *p = new ThreadPool(sched, 0);
// Create the first task, depth 0, that will recursively create 10000
auto task = std::make_shared<TaskThatAddsTasks>(sched, 0);
p->schedule(task);
// Reset the total
TaskThatAddsTasks_counter = 0;
TS_ASSERT_THROWS_NOTHING(p->joinAll());
// Expected total = the number of lowest level entries
TS_ASSERT_EQUALS(TaskThatAddsTasks_counter, 10000);
delete p;
}
void test_StressTest_TasksThatCreateTasks_ThreadSchedulerFIFO() {
do_StressTest_TasksThatCreateTasks(new ThreadSchedulerFIFO());
}
void test_StressTest_TasksThatCreateTasks_ThreadSchedulerLIFO() {
do_StressTest_TasksThatCreateTasks(new ThreadSchedulerLIFO());
}
void test_StressTest_TasksThatCreateTasks_ThreadSchedulerLargestCost() {
do_StressTest_TasksThatCreateTasks(new ThreadSchedulerLargestCost());
}
void test_StressTest_TasksThatCreateTasks_ThreadSchedulerMutexes() {
do_StressTest_TasksThatCreateTasks(new ThreadSchedulerMutexes());
}
//=======================================================================================
/** Task that throws an exception */
class TaskThatThrows : public Task {
void run() override {
ThreadPoolTest_TaskThatThrows_counter++;
throw Mantid::Kernel::Exception::NotImplementedError("Test exception from TaskThatThrows.");
}
};
//--------------------------------------------------------------------
void test_TaskThatThrows() {
ThreadPool p(new ThreadSchedulerFIFO(), 1); // one core
ThreadPoolTest_TaskThatThrows_counter = 0;
for (int i = 0; i < 10; i++) {
p.schedule(std::make_shared<TaskThatThrows>());
}
// joinAll rethrows
TS_ASSERT_THROWS(p.joinAll(), const std::runtime_error &);
// And only one of the tasks actually ran (since we're on one core)
TS_ASSERT_EQUALS(ThreadPoolTest_TaskThatThrows_counter, 1);
}
};