-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathsanityTestHelper.cpp
380 lines (318 loc) · 8.98 KB
/
sanityTestHelper.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
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
/*******************************************************************************
* Copyright IBM Corp. and others 1991
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#include "threadTestLib.hpp"
#include "sanityTestHelper.hpp"
#if __cplusplus < 201103L
#define snprintf(buf, buf_size, format, ...) sprintf(buf, format, __VA_ARGS__)
#endif /* __cplusplus < 201103L */
static bool TestLoopingWaitNotify(int runTime);
/*
* Tests operations on a single thread
*/
bool
SimpleSanity(void)
{
bool ok = true;
omrTestEnv->changeIndent(1);
CMonitor mon1(0, "monitor1");
omrTestEnv->log("Enter exit on one thread\n");
mon1.Enter();
mon1.Exit();
omrTestEnv->log("ok\n");
{
CMonitor mon2(0, "monitor2");
omrTestEnv->log("Enter on 1 and hold, contended enter on 2\n");
omrTestEnv->changeIndent(1);
CEnterExit thread1(mon2, 1000);
CEnterExit thread2(mon2, 0);
omrTestEnv->log("Starting thread 1\n");
thread1.Start();
omrthread_sleep(2000);
omrTestEnv->log("starting thread 2\n");
thread2.Start();
omrTestEnv->log("waiting for thread 1 to terminate\n");
while (!thread1.Terminated()) {
omrthread_sleep(1);
}
omrTestEnv->log("waiting for thread 2 to terminate\n");
while (!thread2.Terminated()) {
omrthread_sleep(1);
}
omrTestEnv->changeIndent(-1);
omrTestEnv->log("ok\n");
}
{
CMonitor mon3(0, "monitor3");
omrTestEnv->log("Enter, notify, notify all - on one thread\n");
mon3.Enter();
mon3.Notify();
mon3.NotifyAll();
mon3.Exit();
omrTestEnv->log("ok\n");
}
/* just testing if we return .. not if we're accurate
* on the time
*/
omrTestEnv->log("Enter, wait(1 second); Exit\n");
CMonitor mon4(0, "monitor4");
mon4.Enter();
mon4.Wait(1000);
mon4.Exit();
omrTestEnv->log("ok\n");
/*
* See if there's any concept of fairness.
* Launch two tight loopers and see if the current
* thread can get in.
*/
omrTestEnv->log("Three thread enter/exit testing\n");
CMonitor mon6(0, "monitor6");
CEnterExitLooper looper1(mon6, 0);
CEnterExitLooper looper2(mon6, 0);
looper1.Start();
looper2.Start();
omrthread_sleep(1000);
unsigned long countBefore = looper1.LoopCount() + looper2.LoopCount();
mon6.Enter();
looper1.ResetLoopCount();
looper2.ResetLoopCount();
mon6.Exit();
unsigned long countAfter = looper1.LoopCount() + looper2.LoopCount();
if (countAfter >= countBefore) {
omrTestEnv->log(LEVEL_ERROR, "Something's wrong!\n");
ok = false;
}
looper1.StopAndWaitForDeath();
looper2.StopAndWaitForDeath();
omrTestEnv->changeIndent(-1);
return ok;
}
bool
TestNThreadsLooping(unsigned int numThreads, unsigned int sleepInterval,
unsigned int runTime, bool keepCount)
{
CMonitor mon(0, "monitor");
unsigned int i;
omrTestEnv->changeIndent(+1);
CEnterExitLooper **threads = new CEnterExitLooper *[numThreads];
for (i = 0; i < numThreads; i++) {
threads[i] = new CEnterExitLooper(mon, sleepInterval);
}
/*
* kick all of them off at (approx) the same time
*/
for (i = 0; i < numThreads; i++) {
threads[i]->Start();
}
omrTestEnv->log("");
for (i = runTime; i > 0; i--) {
omrTestEnv->logRaw("{");
omrthread_sleep(1 * 1000);
omrTestEnv->logRaw("}");
}
if (keepCount) {
char buf[256];
unsigned long count = 0;
for (i = 0; i < numThreads; i++) {
count += threads[i]->LoopCount();
}
snprintf(buf, sizeof(buf), "Performance = %lu", count);
omrTestEnv->log("\n");
omrTestEnv->log("%s\n", buf);
}
for (i = 0; i < numThreads; i++) {
omrTestEnv->log("stopping thread\n");
threads[i]->StopRunning();
}
for (i = 0; i < numThreads; i++) {
omrTestEnv->log("waiting for a thread to terminate\n");
threads[i]->WaitForTermination();
}
delete[] threads;
omrTestEnv->changeIndent(-1);
return true;
}
/*
* Tests N threads contending on a single monitor with varying hold times
*/
void
SanityTestNThreads(unsigned int numThreads, unsigned int runTime)
{
omrTestEnv->changeIndent(1);
omrTestEnv->log("100ms\n");
TestNThreadsLooping(numThreads, 100, runTime, false);
omrTestEnv->log("10ms\n");
TestNThreadsLooping(numThreads, 10, runTime, false);
omrTestEnv->log("1ms\n");
TestNThreadsLooping(numThreads, 1, runTime, false);
omrTestEnv->log("0ms\n");
TestNThreadsLooping(numThreads, 0, runTime, false);
omrTestEnv->changeIndent(-1);
}
void
QuickNDirtyPerformanceTest(unsigned int runTime)
{
omrTestEnv->changeIndent(1);
omrTestEnv->log("One thread\n");
TestNThreadsLooping(1, 0, runTime, true);
omrTestEnv->log("Two threads\n");
TestNThreadsLooping(2, 0, runTime, true);
omrTestEnv->log("Four threads\n");
TestNThreadsLooping(4, 0, runTime, true);
omrTestEnv->changeIndent(-1);
return;
}
/*
* Tests queuing and dequeuing from blocking queue
*/
bool
TestBlockingQueue(CThread& self, const unsigned int numThreads)
{
#if !defined(OMR_THR_THREE_TIER_LOCKING)
return true;
#else /* !defined(OMR_THR_THREE_TIER_LOCKING) */
unsigned int i;
bool ok = true;
CMonitor mon(0, "TestBlockingQueue");
mon.Enter();
CEnterExit **pThreads = new CEnterExit*[numThreads];
for (i = 0; i < numThreads; i++) {
pThreads[i] = new CEnterExit(mon, i * 1000);
pThreads[i]->Start();
self.Sleep(10);
}
/* Hold the monitor until threads have spun out */
while (mon.numBlocking() < numThreads) {
self.Sleep(100);
}
for (i = 0; i < numThreads; i++) {
if (!mon.isThreadBlocking(*pThreads[i])) {
omrTestEnv->log(LEVEL_ERROR, "%08x not in queue\n", pThreads[i]->getThread());
ok = false;
}
}
mon.Exit();
for (i = 0; i < numThreads; i++) {
while (!pThreads[i]->Terminated()) {
self.Sleep(1000);
}
delete pThreads[i];
}
if (mon.numBlocking() != 0) {
omrTestEnv->log(LEVEL_ERROR, "Blocking queue is not empty\n");
ok = false;
}
return ok;
#endif /* !defined(OMR_THR_THREE_TIER_LOCKING) */
}
#if 0
static bool
TestSimpleWaitNotify()
{
/* just testing if we return .. not if we're accurate
* on the time
*/
omrTestEnv->log("Wait notify testing\n");
omrTestEnv->changeIndent(+1);
/* Thread 1 Thread 2
* enter
* wait()
* ...
* enter
* notify
* exit
*/
CMonitor mon(0, "simple wait notify");
mon.Enter();
CNotifier thread2(mon, false, 2000, 0, 0);
thread2.Start();
omrTestEnv->log("Thread 1 waiting\n");
mon.Wait();
omrTestEnv->log("Thread 1 done waiting\n");
/* main thread should now still be able to
* do things on the monitor
*/
mon.Notify();
mon.Wait(100);
mon.Exit();
mon.Enter();
mon.Exit();
omrTestEnv->changeIndent(-1);
omrTestEnv->log("ok\n");
return true;
}
#endif
static bool
TestLoopingWaitNotify(int runTime)
{
char numBuf[64];
CMonitor mon(0, "monitor");
volatile unsigned int doneRunningCount = 0;
volatile unsigned int notifyCount = 0;
CWaitNotifyLooper thread1(mon, (unsigned int *)¬ifyCount,
(unsigned int *)&doneRunningCount);
CWaitNotifyLooper thread2(mon, (unsigned int *)¬ifyCount,
(unsigned int *)&doneRunningCount);
thread1.Start();
thread2.Start();
omrTestEnv->log("Letting threads run for ");
snprintf(numBuf, sizeof(numBuf), "%d", runTime);
omrTestEnv->logRaw(numBuf);
omrTestEnv->logRaw(" seconds...");
omrTestEnv->log("\n");
omrTestEnv->log("");
for (int i = runTime; i > 0; i--) {
snprintf(numBuf, sizeof(numBuf), "%d ", i);
omrTestEnv->logRaw(numBuf);
omrthread_sleep(1000);
}
omrTestEnv->log("\n");
omrTestEnv->log("Telling threads to stop running...\n");
thread1.StopRunning();
omrthread_sleep(1000);
thread2.StopRunning();
/* give them a chance to exit */
omrthread_sleep(1000);
/* notify the one thread still waiting that it's over */
omrTestEnv->log("Entering the monitor to notify the last person...\n");
mon.Enter();
omrTestEnv->log("Notifying...\n");
mon.Notify();
omrTestEnv->log("done notifying...\n");
mon.Exit();
omrTestEnv->log("Exiting and waiting for threads to die...\n");
while (doneRunningCount != 2) {
omrthread_sleep(10);
}
assert(doneRunningCount == 2);
omrTestEnv->log("ok\n");
return true;
}
bool
TestWaitNotify(unsigned int runTime)
{
omrTestEnv->changeIndent(+1);
#if 0
TestSimpleWaitNotify();
#endif
TestLoopingWaitNotify(runTime);
omrTestEnv->changeIndent(-1);
return true;
}