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
|
// **********************************************************************
//
// Copyright (c) 2003-2018 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <PriorityInversion.h>
#include <IceUtil/Thread.h>
#include <IceUtil/Shared.h>
#include <IceUtil/Mutex.h>
#include <IceUtil/Monitor.h>
#include <IceUtil/RecMutex.h>
#include <sstream>
#include <TestHelper.h>
#include <vector>
#include <map>
#ifndef _WIN32
#include <unistd.h>
#endif
using namespace std;
using namespace IceUtil;
class TaskCollector : public IceUtil::Shared
{
public:
TaskCollector(int cores, int high, int medium, int low, Monitor<Mutex>& monitor) :
_lowBegin(0),
_lowEnd(0),
_mediumBegin(0),
_mediumEnd(0),
_highBegin(0),
_cores(cores),
_high(high),
_medium(medium),
_low(low),
_acquired(0),
_monitor(monitor)
{
}
void waitAcquired()
{
Monitor<Mutex>::Lock lock(_monitor);
while(_acquired == 0)
{
_monitor.wait();
}
}
void acquired()
{
Monitor<Mutex>::Lock lock(_monitor);
++_acquired;
_monitor.notifyAll();
}
void waitAll()
{
Monitor<Mutex>::Lock lock(_monitor);
while(_mediumBegin < _cores || _highBegin == 0)
{
//Wait until all task are ready to compete by processors
_monitor.wait();
}
}
void taskBegin(int priority)
{
Monitor<Mutex>::Lock lock(_monitor);
if(priority == _low)
{
_lowBegin++;
}
else if(priority == _medium)
{
_mediumBegin++;
}
else if(priority == _high)
{
_highBegin++;
}
_monitor.notifyAll();
}
void taskEnd(int priority)
{
Monitor<Mutex>::Lock lock(_monitor);
//
// Test all task begin run before any task ends.
//
test(_lowBegin == 1);
test(_highBegin == 1);
test(_mediumBegin == _cores);
if(priority == _low)
{
//
// Low priority thread should end before all medium priority threads.
//
test(_mediumEnd == 0);
_lowEnd++;
}
else if(priority == _medium)
{
//
// When the first medium priority task end the
// low priority task completed.
//
test(_lowEnd > 0);
_mediumEnd++;
}
}
private:
int _lowBegin;
int _lowEnd;
int _mediumBegin;
int _mediumEnd;
int _highBegin;
int _cores;
int _high;
int _medium;
int _low;
int _acquired;
Monitor<Mutex>& _monitor;
IceUtil::Mutex _mutex;
};
typedef IceUtil::Handle<TaskCollector> TaskCollectorPtr;
class SharedResource : public IceUtil::Shared
{
public:
SharedResource(const TaskCollectorPtr& taskCollector) :
_taskCollector(taskCollector)
{
}
TaskCollectorPtr taskCollector() const { return _taskCollector; }
virtual void run(int priority) = 0;
private:
TaskCollectorPtr _taskCollector;
};
typedef IceUtil::Handle<SharedResource> SharedResourcePtr;
class SharedResourceMutex : public SharedResource
{
public:
SharedResourceMutex(const TaskCollectorPtr& taskCollector) :
SharedResource(taskCollector)
{
}
virtual void run(int priority)
{
taskCollector()->taskBegin(priority);
Mutex::Lock lock(_mutex);
taskCollector()->acquired();
taskCollector()->waitAll();
//
// If this is the low priority thread we ensure the test runs at least timeout
// seconds this ensure that it doesn't terminate righ away and medium priority
// threads will take over all available cores
//
IceUtil::Time t = IceUtil::Time::now(IceUtil::Time::Monotonic);
IceUtil::Time timeout = IceUtil::Time::seconds(2);
if(priority == 1)
{
while(true)
{
if(IceUtil::Time::now(IceUtil::Time::Monotonic) - t > timeout)
{
break;
}
}
}
taskCollector()->taskEnd(priority);
}
private:
IceUtil::Mutex _mutex;
};
class SharedResourceRecMutex : public SharedResource
{
public:
SharedResourceRecMutex(const TaskCollectorPtr& taskCollector) :
SharedResource(taskCollector)
{
}
void run(int priority)
{
taskCollector()->taskBegin(priority);
RecMutex::Lock lock(_mutex);
taskCollector()->acquired();
taskCollector()->waitAll();
//
// If this is the low priority thread we ensure the test runs at least timeout
// seconds this ensure that it doesn't terminate righ away and medium priority
// threads will take over all available cores
//
IceUtil::Time t = IceUtil::Time::now(IceUtil::Time::Monotonic);
IceUtil::Time timeout = IceUtil::Time::seconds(2);
if(priority == 1)
{
while(true)
{
if(IceUtil::Time::now(IceUtil::Time::Monotonic) - t > timeout)
{
break;
}
}
}
taskCollector()->taskEnd(priority);
}
private:
IceUtil::RecMutex _mutex;
};
class ThreadCommon : public IceUtil::Thread
{
public:
virtual void run() = 0;
int getPriority()
{
#ifdef _WIN32_WCE
return CeGetThreadPriority(GetCurrentThread());
#elif defined _WIN32
return GetThreadPriority(GetCurrentThread());
#else
sched_param param;
int sched_policy;
pthread_t thread = pthread_self();
pthread_getschedparam(thread, &sched_policy, ¶m);
return param.sched_priority;
#endif
}
};
class Task : public ThreadCommon
{
public:
Task(const SharedResourcePtr& shared) :
_shared(shared)
{
}
virtual void run()
{
_shared->run(getPriority());
}
void waitAcquired()
{
_shared->taskCollector()->waitAcquired();
}
private:
SharedResourcePtr _shared;
};
typedef IceUtil::Handle<Task> TaskPtr;
class MediumPriorityThread : public ThreadCommon
{
public:
MediumPriorityThread(const TaskCollectorPtr& taskCollector, const ThreadPtr& highPriorityThread, int timeout) :
_taskCollector(taskCollector),
_highPriorityThread(highPriorityThread),
_timeout(IceUtil::Time::seconds(timeout))
{
}
virtual void run()
{
IceUtil::Time timestamp = IceUtil::Time::now(IceUtil::Time::Monotonic);
_taskCollector->taskBegin(getPriority());
while(true)
{
if(IceUtil::Time::now(IceUtil::Time::Monotonic) - timestamp > _timeout)
{
// If high priority task do not end with the specific timeout means
// that the low priority task priority was not bosted so we are having
// the clasic priority inversion issue.
test(false);
}
if(!_highPriorityThread->isAlive())
{
break;
}
}
_taskCollector->taskEnd(getPriority());
}
private:
const TaskCollectorPtr _taskCollector;
const ThreadPtr _highPriorityThread;
const IceUtil::Time _timeout;
};
static const string priorityTestName("priority inversion");
PriorityInversionTest::PriorityInversionTest() :
TestBase(priorityTestName)
{
}
void
PriorityInversionTest::run()
{
int cores, high, medium, low, timeout;
timeout = 30;
#ifdef _WIN32
return; //Priority inversion is not supported by WIN32
#else
try
{
IceUtil::Mutex m;
}
catch(const IceUtil::ThreadSyscallException&)
{
return; // Mutex protocol PrioInherit not supported
}
cores = static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
high = 45;
medium = 35;
low = 1;
#endif
{
Monitor<Mutex> monitor;
TaskCollectorPtr collector = new TaskCollector(cores, high, medium, low, monitor);
vector<ThreadControl> threads;
SharedResourcePtr shared = new SharedResourceMutex(collector);
//
// Create one low priority thread.
//
TaskPtr lowThread = new Task(shared);
threads.push_back(lowThread->start(128, low));
lowThread->waitAcquired();
//
// Create one high priority thread that use the same shared resource
// as the previous low priority thread
//
TaskPtr highThread = new Task(shared);
threads.push_back(highThread->start(128, high));
//
// Create one medium priority thread per core.
//
for(int cont = 0; cont < cores; ++cont)
{
ThreadPtr t = new MediumPriorityThread(collector, highThread, timeout);
threads.push_back(t->start(128, medium));
}
//
// Join with all the threads.
//
vector<ThreadControl>::iterator it;
for(it = threads.begin(); it != threads.end(); ++it)
{
try
{
(*it).join();
}
catch(...)
{
}
}
}
//
// Same test with a recursive mutex.
//
{
Monitor<Mutex> monitor;
TaskCollectorPtr collector = new TaskCollector(cores, high, medium, low, monitor);
SharedResourcePtr shared = new SharedResourceRecMutex(collector);
vector<ThreadControl> threads;
//
// Create one low priority thread.
//
TaskPtr lowThread = new Task(shared);
threads.push_back(lowThread->start(128, low));
lowThread->waitAcquired();
//
// Create one high priority thread that use the same shared resource
// as the previous low priority thread.
//
ThreadPtr highThread = new Task(shared);
threads.push_back(highThread->start(128, high));
//
// Create one medium priority tasks per core that runs until
// the high priority thread is running.
//
for(int cont = 0; cont < cores; ++cont)
{
ThreadPtr t = new MediumPriorityThread(collector, highThread, timeout);
threads.push_back(t->start(128, medium));
}
//
// Join with all the threads.
//
vector<ThreadControl>::iterator it;
for(it = threads.begin(); it != threads.end(); ++it)
{
try
{
(*it).join();
}
catch(...)
{
}
}
}
}
|