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
|
/* IBM_PROLOG_BEGIN_TAG */
/* This is an automatically generated prolog. */
/* */
/* $Source: src/usr/util/test/threadpool.H $ */
/* */
/* OpenPOWER HostBoot Project */
/* */
/* Contributors Listed Below - COPYRIGHT 2012,2019 */
/* [+] International Business Machines Corp. */
/* */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or */
/* implied. See the License for the specific language governing */
/* permissions and limitations under the License. */
/* */
/* IBM_PROLOG_END_TAG */
#ifndef __UTIL_TEST_THREADPOOL_H
#define __UTIL_TEST_THREADPOOL_H
#include <cxxtest/TestSuite.H>
#include <util/threadpool.H>
#include <errl/errlentry.H>
#include <errl/errlmanager.H>
#include <util/util_reasoncodes.H>
#include <hbotcompid.H>
#include <kernel/console.H>
// Thread pool constructor flags
#define DISABLE_CHILD_RC_CHECKING false
#define CHECK_CHILD_RC true
namespace __ThreadPoolTest
{
/** WorkItem that simply modifies a pointer. */
struct Simple
{
explicit Simple(uint64_t* _v) : iv_value(_v) {};
void operator()() { (*iv_value) = 0xABCD; };
private:
uint64_t* iv_value;
};
/** WorkItem that modifies a value and has an order function. */
struct SimpleOrdered : public Simple
{
explicit SimpleOrdered(uint64_t* _v) : Simple(_v) {};
bool operator<(const SimpleOrdered& rhs) { return false; };
};
/** WorkItem that touches a barrier to ensure at least a certain number
* of threads have been spawned. */
struct EnsureThreads
{
explicit EnsureThreads(barrier_t* _b) : iv_barrier(_b) {};
void operator()() { barrier_wait(iv_barrier); };
private:
barrier_t* iv_barrier;
};
/** WorkItem with an order function that modifies a value in order
* sequence.
*
* An EnsureOrder workitem is created with 0, 1, 2, etc. The order
* function ensures that item-0 should run before item-1, etc. To
* perform the test, a read-modify-write of a shared value is performed.
* The value should be at the item-n value and the WorkItem increments
* the value.
*
* There is no issues with weak consistency with this object because only
* a single thread is executing against the pool at this time.
*/
struct EnsureOrder
{
EnsureOrder(uint64_t _i, uint64_t* _v) : iv_id(_i), iv_value(_v) {};
void operator()()
{
if ((*iv_value) != iv_id)
{
TS_FAIL("ID number wrong. %d:%d", (*iv_value), iv_id);
}
(*iv_value) = iv_id + 1;
}
bool operator<(const EnsureOrder& rhs) { return iv_id < rhs.iv_id; };
private:
uint64_t iv_id;
uint64_t* iv_value;
};
/** A simple work item that causes the task to crash */
struct CrashedTask
{
CrashedTask() {};
void operator()()
{
uint8_t* l_ptr = nullptr;
*l_ptr = 1;
}
};
};
class ThreadPoolTest: public CxxTest::TestSuite
{
public:
/** Test that we can construct a ThreadPool for both FIFO and non-FIFO
* ordered pools. */
void testSimpleWorkItem()
{
Util::ThreadPool<__ThreadPoolTest::Simple>
instance(DISABLE_CHILD_RC_CHECKING);
uint64_t value = 0;
instance.insert(new __ThreadPoolTest::Simple(&value));
instance.start();
instance.shutdown();
if (value == 0)
{
TS_FAIL("Value was not changed by Simple worker thread.");
}
Util::ThreadPool<__ThreadPoolTest::SimpleOrdered>
instance2(DISABLE_CHILD_RC_CHECKING);
value = 0;
instance2.insert(new __ThreadPoolTest::SimpleOrdered(&value));
instance2.start();
instance2.shutdown();
if (value == 0)
{
TS_FAIL("Value was not changed by SimpleOrdered"
"worker thread.");
}
}
/** Test that the thread manager function creates at least as many
* threads as directed. */
void testThreadManager()
{
Util::ThreadPool<__ThreadPoolTest::EnsureThreads>
instance(DISABLE_CHILD_RC_CHECKING);
barrier_t barrier;
for (size_t count = 1; count < 5; count++)
{
Util::ThreadPoolManager::setThreadCount(count);
barrier_init(&barrier, count);
for (size_t i = 0; i < count; i++)
{
instance.insert(
new __ThreadPoolTest::EnsureThreads(&barrier));
}
instance.start();
instance.shutdown();
barrier_destroy(&barrier);
}
}
/** Test that the order functions work on the thread pool. */
void testThreadOrder()
{
Util::ThreadPool<__ThreadPoolTest::EnsureOrder>
instance(DISABLE_CHILD_RC_CHECKING);
uint64_t value = 0;
// Ensure that adding work items in order works.
Util::ThreadPoolManager::setThreadCount(1);
for (uint64_t i = 0; i < 29; i++)
{
instance.insert(new __ThreadPoolTest::EnsureOrder(i, &value));
}
instance.start();
instance.shutdown();
// Ensure that adding work items in reverse order works.
value = 0;
for (uint64_t i = 0; i < 29; i++)
{
instance.insert(
new __ThreadPoolTest::EnsureOrder(28 - i, &value));
}
instance.start();
instance.shutdown();
// Ensure that adding work items in a unsorted order works.
//
// Since 29 is a prime, i*n mod 29 generates the additive group of
// all integers (mod 29). Use this to create a pseudo-random
// ordering of the integers 0-28: 0, 23, 17, 11, 5, 28, 22, ...
value = 0;
for (uint64_t i = 0; i < 29; i++)
{
instance.insert(
new __ThreadPoolTest::EnsureOrder((i*23) % 29, &value));
}
instance.start();
instance.shutdown();
}
/** Test a good child task that doesn't return an error. */
void testChildRCGoodTask()
{
TS_INFO(ENTER_MRK"testChildRCGoodTask");
Util::ThreadPool<__ThreadPoolTest::Simple>
l_threadPool(CHECK_CHILD_RC);
uint64_t l_value = 0;
do {
l_threadPool.insert(new __ThreadPoolTest::Simple(&l_value));
l_threadPool.start();
errlHndl_t l_errl = l_threadPool.shutdown();
if(l_errl)
{
TS_FAIL("testChildRCGoodTask: an unexpected error log (EID 0x%.8%x) returned from the thread pool", l_errl->eid());
errlCommit(l_errl, CXXTEST_COMP_ID);
break;
}
if(l_value == 0)
{
TS_FAIL("testChildRCGoodTask: the test value was not changed by the child task");
break;
}
}while(0);
TS_INFO(EXIT_MRK"testChildRCGoodTask");
}
/** Test that the crashed task's error log is returned to thread pool
correctly */
void testChildRCCrashedTask()
{
TS_INFO(ENTER_MRK"testChildRCCrashedTask");
Util::ThreadPool<__ThreadPoolTest::CrashedTask>
l_threadPool(CHECK_CHILD_RC);
errlHndl_t l_errl = nullptr;
do {
printk("testChildRCCrashedTask: Expect to see uncaught exception\n");
l_threadPool.insert(new __ThreadPoolTest::CrashedTask());
l_threadPool.start();
l_errl = l_threadPool.shutdown();
if(!l_errl)
{
TS_FAIL("testChildRCCrashedTask: the thread pool did not return an error log as was expected");
break;
}
if(l_errl->moduleId() != Util::UTIL_MOD_TP_SHUTDOWN)
{
TS_FAIL("testChildRCCrashedTask: unexpected moduleId returned from EID 0x%.8x; expected: 0x%x; actual: 0x%x",
l_errl->eid(),
Util::UTIL_MOD_TP_SHUTDOWN,
l_errl->moduleId());
break;
}
if(l_errl->reasonCode() != Util::UTIL_RC_CHILD_TASK_FAILED)
{
TS_FAIL("testChildRCCrashedTask: unexpected return code from EID 0x%.8x; expected: 0x%x; actual 0x%x",
l_errl->eid(),
Util::UTIL_RC_CHILD_TASK_FAILED,
l_errl->reasonCode());
break;
}
}while(0);
if(l_errl)
{
ERRORLOG::errlCommit(l_errl, CXXTEST_COMP_ID);
}
TS_INFO(EXIT_MRK"testChildRCCrashedTask");
}
/* Test that error is not returned by crashed task when explicitly
not requested by thread pool */
void testChildNoRCCrashedTask()
{
TS_INFO(ENTER_MRK"testChildNoRCCrashedTask");
Util::ThreadPool<__ThreadPoolTest::CrashedTask>
l_threadPool(DISABLE_CHILD_RC_CHECKING);
errlHndl_t l_errl = nullptr;
printk("testChildNoRCCrashedTask: Expect to see uncaught exception\n");
l_threadPool.insert(new __ThreadPoolTest::CrashedTask());
l_threadPool.start();
l_errl = l_threadPool.shutdown();
if(l_errl)
{
TS_FAIL("testChildNoRCCrashedTask: unexpected error returned from the thread pool (EID 0x%.8x)", l_errl->eid());
ERRORLOG::errlCommit(l_errl, CXXTEST_COMP_ID);
}
TS_INFO(EXIT_MRK"testChildNoRCCrashedTask");
}
};
#endif
|