0
|
1 |
// Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
2 |
// All rights reserved.
|
|
3 |
// This component and the accompanying materials are made available
|
|
4 |
// under the terms of the License "Eclipse Public License v1.0"
|
|
5 |
// which accompanies this distribution, and is available
|
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
//
|
|
15 |
|
|
16 |
#include <e32test.h>
|
|
17 |
#include <e32msgqueue.h>
|
|
18 |
|
|
19 |
#include "bm_suite.h"
|
|
20 |
|
|
21 |
class Sync : public BMProgram
|
|
22 |
{
|
|
23 |
public :
|
|
24 |
Sync() : BMProgram(_L("Synchronization Primitives"))
|
|
25 |
{}
|
|
26 |
virtual TBMResult* Run(TBMUInt64 aIter, TInt* aCount);
|
|
27 |
|
|
28 |
typedef void (*MeasurementFunc)(TBMResult*, TBMUInt64 aIter, TBool aRemote);
|
|
29 |
struct Measurement
|
|
30 |
{
|
|
31 |
MeasurementFunc iFunc;
|
|
32 |
TPtrC iName;
|
|
33 |
TBool iRemote;
|
|
34 |
|
|
35 |
Measurement(MeasurementFunc aFunc, const TDesC& aName, TBool aRemote = EFalse) :
|
|
36 |
iFunc(aFunc), iName(aName), iRemote(aRemote) {}
|
|
37 |
};
|
|
38 |
|
|
39 |
static TBMResult iResults[];
|
|
40 |
static Measurement iMeasurements[];
|
|
41 |
|
|
42 |
static void MutexPassing(TBMResult*, TBMUInt64 aIter, TBool aRemote);
|
|
43 |
static void MutexContentionParent(TBMResult* aResult, TBMUInt64 aIter, TBool aRemote);
|
|
44 |
static TInt MutexContentionChild(TAny*);
|
|
45 |
static void SemaphoreLatencyParent(TBMResult* aResult, TBMUInt64 aIter, TBool aRemote);
|
|
46 |
static TInt SemaphoreLatencyChild(TAny*);
|
|
47 |
static void ThreadSemaphoreLatencyParent(TBMResult* aResult, TBMUInt64 aIter, TBool aRemote);
|
|
48 |
static TInt ThreadSemaphoreLatencyChild(TAny*);
|
|
49 |
};
|
|
50 |
|
|
51 |
Sync::Measurement Sync::iMeasurements[] =
|
|
52 |
{
|
|
53 |
Measurement(&Sync::MutexPassing, _L("Mutex Passing Case")),
|
|
54 |
Measurement(&Sync::MutexContentionParent, _L("Local Mutex Contention")),
|
|
55 |
Measurement(&Sync::MutexContentionParent, _L("Remote Mutex Contention"), ETrue),
|
|
56 |
Measurement(&Sync::SemaphoreLatencyParent, _L("Local Semaphore Latency")),
|
|
57 |
Measurement(&Sync::SemaphoreLatencyParent, _L("Remote Semaphore Latency"), ETrue),
|
|
58 |
Measurement(&Sync::ThreadSemaphoreLatencyParent, _L("Local Thread Semaphore Latency")),
|
|
59 |
};
|
|
60 |
TBMResult Sync::iResults[sizeof(Sync::iMeasurements)/sizeof(Sync::iMeasurements[0])];
|
|
61 |
|
|
62 |
static Sync sync;
|
|
63 |
|
|
64 |
void Sync::MutexPassing(TBMResult* aResult, TBMUInt64 aIter, TBool)
|
|
65 |
{
|
|
66 |
RMutex mutex;
|
|
67 |
mutex.CreateLocal();
|
|
68 |
|
|
69 |
TBMTimeInterval ti;
|
|
70 |
ti.Begin();
|
|
71 |
for (TBMUInt64 i = 0; i < aIter; ++i)
|
|
72 |
{
|
|
73 |
mutex.Wait();
|
|
74 |
mutex.Signal();
|
|
75 |
}
|
|
76 |
TBMTicks t = ti.End();
|
|
77 |
|
|
78 |
mutex.Close();
|
|
79 |
|
|
80 |
aResult->Cumulate(t, aIter);
|
|
81 |
}
|
|
82 |
|
|
83 |
class MutexContentionArgs : public TBMSpawnArgs
|
|
84 |
{
|
|
85 |
public:
|
|
86 |
|
|
87 |
RMutex iMutexA;
|
|
88 |
RMutex iMutexB;
|
|
89 |
RSemaphore iSem;
|
|
90 |
TBMUInt64 iIterationCount;
|
|
91 |
|
|
92 |
MutexContentionArgs(TInt aRemote, TBMUInt64 aIter);
|
|
93 |
|
|
94 |
void ChildOpen();
|
|
95 |
void ChildClose();
|
|
96 |
|
|
97 |
void Close();
|
|
98 |
};
|
|
99 |
|
|
100 |
MutexContentionArgs::MutexContentionArgs(TInt aRemote, TBMUInt64 aIter) :
|
|
101 |
TBMSpawnArgs(Sync::MutexContentionChild, KBMPriorityLow, aRemote, sizeof(*this)),
|
|
102 |
iIterationCount(aIter)
|
|
103 |
{
|
|
104 |
TInt r;
|
|
105 |
if (aRemote)
|
|
106 |
{
|
|
107 |
r = iMutexA.CreateGlobal(_L("MutexA"));
|
|
108 |
BM_ERROR(r, r == KErrNone);
|
|
109 |
r = iMutexB.CreateGlobal(_L("MutexB"));
|
|
110 |
BM_ERROR(r, r == KErrNone);
|
|
111 |
r = iSem.CreateGlobal(_L("Semaphore"), 0);
|
|
112 |
BM_ERROR(r, r == KErrNone);
|
|
113 |
}
|
|
114 |
else
|
|
115 |
{
|
|
116 |
r = iMutexA.CreateLocal();
|
|
117 |
BM_ERROR(r, r == KErrNone);
|
|
118 |
r = iMutexB.CreateLocal();
|
|
119 |
BM_ERROR(r, r == KErrNone);
|
|
120 |
r = iSem.CreateLocal(0);
|
|
121 |
BM_ERROR(r, r == KErrNone);
|
|
122 |
}
|
|
123 |
}
|
|
124 |
|
|
125 |
void MutexContentionArgs::ChildOpen()
|
|
126 |
{
|
|
127 |
if (iRemote)
|
|
128 |
{
|
|
129 |
iMutexA.Duplicate(iParent);
|
|
130 |
iMutexB.Duplicate(iParent);
|
|
131 |
iSem.Duplicate(iParent);
|
|
132 |
}
|
|
133 |
}
|
|
134 |
|
|
135 |
void MutexContentionArgs::ChildClose()
|
|
136 |
{
|
|
137 |
if (iRemote)
|
|
138 |
{
|
|
139 |
iMutexA.Close();
|
|
140 |
iMutexB.Close();
|
|
141 |
iSem.Close();
|
|
142 |
}
|
|
143 |
}
|
|
144 |
|
|
145 |
void MutexContentionArgs::Close()
|
|
146 |
{
|
|
147 |
iMutexA.Close();
|
|
148 |
iMutexB.Close();
|
|
149 |
iSem.Close();
|
|
150 |
}
|
|
151 |
|
|
152 |
void Sync::MutexContentionParent(TBMResult* aResult, TBMUInt64 aIter, TBool aRemote)
|
|
153 |
{
|
|
154 |
MutexContentionArgs mc(aRemote, aIter);
|
|
155 |
|
|
156 |
MBMChild* child = sync.SpawnChild(&mc);
|
|
157 |
|
|
158 |
mc.iSem.Wait();
|
|
159 |
|
|
160 |
TBMTimeInterval ti;
|
|
161 |
ti.Begin();
|
|
162 |
for (TBMUInt64 i = 0; i < aIter; ++i)
|
|
163 |
{
|
|
164 |
mc.iMutexA.Wait();
|
|
165 |
mc.iMutexA.Signal();
|
|
166 |
mc.iMutexB.Wait();
|
|
167 |
mc.iMutexB.Signal();
|
|
168 |
}
|
|
169 |
TBMTicks t = ti.End();
|
|
170 |
|
|
171 |
child->WaitChildExit();
|
|
172 |
mc.Close();
|
|
173 |
|
|
174 |
aResult->Cumulate(t/2, aIter);
|
|
175 |
}
|
|
176 |
|
|
177 |
TInt Sync::MutexContentionChild(TAny* ptr)
|
|
178 |
{
|
|
179 |
MutexContentionArgs* mc = (MutexContentionArgs*) ptr;
|
|
180 |
mc->ChildOpen();
|
|
181 |
|
|
182 |
mc->iMutexA.Wait();
|
|
183 |
mc->iSem.Signal();
|
|
184 |
for (TBMUInt64 i = 0; i < mc->iIterationCount; ++i)
|
|
185 |
{
|
|
186 |
mc->iMutexB.Wait();
|
|
187 |
mc->iMutexA.Signal();
|
|
188 |
mc->iMutexA.Wait();
|
|
189 |
mc->iMutexB.Signal();
|
|
190 |
}
|
|
191 |
mc->iMutexA.Signal();
|
|
192 |
|
|
193 |
mc->ChildClose();
|
|
194 |
return KErrNone;
|
|
195 |
}
|
|
196 |
|
|
197 |
class SemaphoreLatencyArgs : public TBMSpawnArgs
|
|
198 |
{
|
|
199 |
public:
|
|
200 |
|
|
201 |
RSemaphore iSem;
|
|
202 |
TBMUInt64 iIterationCount;
|
|
203 |
RMsgQueue<TBMTicks> iSignalTimeQue;
|
|
204 |
|
|
205 |
SemaphoreLatencyArgs(TInt aRemote, TBMUInt64 aIter);
|
|
206 |
|
|
207 |
void ChildOpen();
|
|
208 |
void ChildClose();
|
|
209 |
|
|
210 |
TBMTicks SignalTime();
|
|
211 |
void ChildSignalTime(TBMTicks);
|
|
212 |
|
|
213 |
void Close();
|
|
214 |
};
|
|
215 |
|
|
216 |
SemaphoreLatencyArgs::SemaphoreLatencyArgs(TInt aRemote, TBMUInt64 aIter) :
|
|
217 |
TBMSpawnArgs(Sync::SemaphoreLatencyChild, KBMPriorityLow, aRemote, sizeof(*this)),
|
|
218 |
iIterationCount(aIter)
|
|
219 |
{
|
|
220 |
TInt r;
|
|
221 |
if (aRemote)
|
|
222 |
{
|
|
223 |
r = iSem.CreateGlobal(_L("BM Semaphore"), 0);
|
|
224 |
BM_ERROR(r, r == KErrNone);
|
|
225 |
}
|
|
226 |
else
|
|
227 |
{
|
|
228 |
r = iSem.CreateLocal(0);
|
|
229 |
BM_ERROR(r, r == KErrNone);
|
|
230 |
}
|
|
231 |
r = iSignalTimeQue.CreateGlobal(_L("BM Queue"), 1);
|
|
232 |
BM_ERROR(r, r == KErrNone);
|
|
233 |
}
|
|
234 |
|
|
235 |
void SemaphoreLatencyArgs::ChildOpen()
|
|
236 |
{
|
|
237 |
if (iRemote)
|
|
238 |
{
|
|
239 |
iSem.Duplicate(iParent);
|
|
240 |
TInt r = iSignalTimeQue.OpenGlobal(_L("BM Queue"));
|
|
241 |
BM_ERROR(r, r == KErrNone);
|
|
242 |
}
|
|
243 |
}
|
|
244 |
|
|
245 |
void SemaphoreLatencyArgs::ChildSignalTime(TBMTicks aTime)
|
|
246 |
{
|
|
247 |
TInt r = iSignalTimeQue.Send(aTime);
|
|
248 |
BM_ERROR(r, r == KErrNone);
|
|
249 |
}
|
|
250 |
|
|
251 |
TBMTicks SemaphoreLatencyArgs::SignalTime()
|
|
252 |
{
|
|
253 |
TBMTicks time;
|
|
254 |
iSignalTimeQue.ReceiveBlocking(time);
|
|
255 |
return time;
|
|
256 |
}
|
|
257 |
|
|
258 |
void SemaphoreLatencyArgs::ChildClose()
|
|
259 |
{
|
|
260 |
if (iRemote)
|
|
261 |
{
|
|
262 |
iSem.Close();
|
|
263 |
iSignalTimeQue.Close();
|
|
264 |
}
|
|
265 |
}
|
|
266 |
|
|
267 |
void SemaphoreLatencyArgs::Close()
|
|
268 |
{
|
|
269 |
iSem.Close();
|
|
270 |
iSignalTimeQue.Close();
|
|
271 |
}
|
|
272 |
|
|
273 |
void Sync::SemaphoreLatencyParent(TBMResult* aResult, TBMUInt64 aIter, TBool aRemote)
|
|
274 |
{
|
148
|
275 |
RSemaphore slSync;
|
|
276 |
TInt r = slSync.CreateGlobal(_L("slSync"), 0);
|
|
277 |
BM_ERROR(r, r == KErrNone);
|
|
278 |
|
0
|
279 |
SemaphoreLatencyArgs sl(aRemote, aIter);
|
|
280 |
MBMChild* child = sync.SpawnChild(&sl);
|
|
281 |
for (TBMUInt64 i = 0; i < aIter; ++i)
|
|
282 |
{
|
148
|
283 |
slSync.Signal();
|
0
|
284 |
sl.iSem.Wait();
|
|
285 |
TBMTicks now;
|
|
286 |
::bmTimer.Stamp(&now);
|
|
287 |
aResult->Cumulate(TBMTicksDelta(sl.SignalTime(), now));
|
|
288 |
}
|
|
289 |
child->WaitChildExit();
|
|
290 |
sl.Close();
|
148
|
291 |
slSync.Close();
|
0
|
292 |
}
|
|
293 |
|
|
294 |
TInt Sync::SemaphoreLatencyChild(TAny* ptr)
|
|
295 |
{
|
148
|
296 |
RSemaphore slSync;
|
|
297 |
TInt r = slSync.OpenGlobal(_L("slSync"));
|
|
298 |
BM_ERROR(r, r == KErrNone);
|
|
299 |
|
0
|
300 |
SemaphoreLatencyArgs* sl = (SemaphoreLatencyArgs*) ptr;
|
|
301 |
sl->ChildOpen();
|
|
302 |
for (TBMUInt64 i = 0; i < sl->iIterationCount; ++i)
|
|
303 |
{
|
148
|
304 |
slSync.Wait();
|
0
|
305 |
TBMTicks sigTime;
|
|
306 |
::bmTimer.Stamp(&sigTime);
|
|
307 |
sl->iSem.Signal();
|
|
308 |
sl->ChildSignalTime(sigTime);
|
|
309 |
}
|
|
310 |
sl->ChildClose();
|
148
|
311 |
slSync.Close();
|
0
|
312 |
return KErrNone;
|
|
313 |
}
|
|
314 |
|
|
315 |
class ThreadSemaphoreLatencyArgs : public TBMSpawnArgs
|
|
316 |
{
|
|
317 |
public:
|
|
318 |
|
|
319 |
TBMUInt64 iIterationCount;
|
|
320 |
TBMTicks iSignalTime;
|
|
321 |
TRequestStatus iStatus;
|
|
322 |
TRequestStatus* iStatusPtr;
|
|
323 |
RMsgQueue<TBMTicks> iSignalTimeQue;
|
|
324 |
|
|
325 |
ThreadSemaphoreLatencyArgs(TInt aRemote, TBMUInt64 aIter);
|
|
326 |
|
|
327 |
void ChildOpen();
|
|
328 |
void ChildClose();
|
|
329 |
|
|
330 |
TBMTicks SignalTime();
|
|
331 |
void ChildSignalTime(TBMTicks);
|
|
332 |
|
|
333 |
void Close();
|
|
334 |
};
|
|
335 |
|
|
336 |
ThreadSemaphoreLatencyArgs::ThreadSemaphoreLatencyArgs(TInt aRemote, TBMUInt64 aIter) :
|
|
337 |
TBMSpawnArgs(Sync::ThreadSemaphoreLatencyChild, KBMPriorityLow, aRemote, sizeof(*this)),
|
|
338 |
iIterationCount(aIter),
|
|
339 |
iStatusPtr(&iStatus)
|
|
340 |
|
|
341 |
{
|
|
342 |
TInt r = iSignalTimeQue.CreateGlobal(_L("BM Queue"), 1);
|
|
343 |
BM_ERROR(r, r == KErrNone);
|
|
344 |
}
|
|
345 |
|
|
346 |
void ThreadSemaphoreLatencyArgs::ChildOpen()
|
|
347 |
{
|
|
348 |
if (iRemote)
|
|
349 |
{
|
|
350 |
TInt r = iSignalTimeQue.OpenGlobal(_L("BM Queue"));
|
|
351 |
BM_ERROR(r, r == KErrNone);
|
|
352 |
}
|
|
353 |
}
|
|
354 |
|
|
355 |
void ThreadSemaphoreLatencyArgs::ChildSignalTime(TBMTicks aTime)
|
|
356 |
{
|
|
357 |
TInt r = iSignalTimeQue.Send(aTime);
|
|
358 |
BM_ERROR(r, r == KErrNone);
|
|
359 |
}
|
|
360 |
|
|
361 |
TBMTicks ThreadSemaphoreLatencyArgs::SignalTime()
|
|
362 |
{
|
|
363 |
TBMTicks time;
|
|
364 |
iSignalTimeQue.ReceiveBlocking(time);
|
|
365 |
return time;
|
|
366 |
}
|
|
367 |
|
|
368 |
void ThreadSemaphoreLatencyArgs::ChildClose()
|
|
369 |
{
|
|
370 |
if (iRemote)
|
|
371 |
{
|
|
372 |
iSignalTimeQue.Close();
|
|
373 |
}
|
|
374 |
}
|
|
375 |
|
|
376 |
void ThreadSemaphoreLatencyArgs::Close()
|
|
377 |
{
|
|
378 |
iSignalTimeQue.Close();
|
|
379 |
}
|
|
380 |
|
|
381 |
void Sync::ThreadSemaphoreLatencyParent(TBMResult* aResult, TBMUInt64 aIter, TBool aRemote)
|
|
382 |
{
|
148
|
383 |
RSemaphore tslSync;
|
|
384 |
TInt r = tslSync.CreateGlobal(_L("tslSync"), 0);
|
|
385 |
BM_ERROR(r, r == KErrNone);
|
|
386 |
|
0
|
387 |
ThreadSemaphoreLatencyArgs sl(aRemote, aIter);
|
|
388 |
MBMChild* child = sync.SpawnChild(&sl);
|
|
389 |
for (TBMUInt64 i = 0; i < aIter; ++i)
|
|
390 |
{
|
|
391 |
sl.iStatus = KRequestPending;
|
148
|
392 |
tslSync.Signal();
|
0
|
393 |
User::WaitForRequest(sl.iStatus);
|
|
394 |
BM_ASSERT(sl.iStatus == KErrNone);
|
|
395 |
TBMTicks now;
|
|
396 |
::bmTimer.Stamp(&now);
|
|
397 |
aResult->Cumulate(TBMTicksDelta(sl.SignalTime(), now));
|
|
398 |
}
|
|
399 |
child->WaitChildExit();
|
|
400 |
sl.Close();
|
148
|
401 |
tslSync.Close();
|
0
|
402 |
}
|
|
403 |
|
|
404 |
TInt Sync::ThreadSemaphoreLatencyChild(TAny* ptr)
|
|
405 |
{
|
148
|
406 |
RSemaphore tslSync;
|
|
407 |
TInt r = tslSync.OpenGlobal(_L("tslSync"));
|
|
408 |
BM_ERROR(r, r == KErrNone);
|
|
409 |
|
0
|
410 |
ThreadSemaphoreLatencyArgs* sl = (ThreadSemaphoreLatencyArgs*) ptr;
|
|
411 |
sl->ChildOpen();
|
|
412 |
for (TBMUInt64 i = 0; i < sl->iIterationCount; ++i)
|
|
413 |
{
|
148
|
414 |
tslSync.Wait();
|
0
|
415 |
TRequestStatus* sptr = sl->iStatusPtr;
|
|
416 |
TBMTicks sigTime;
|
|
417 |
::bmTimer.Stamp(&sigTime);
|
|
418 |
sl->iParent.RequestComplete(sptr, KErrNone);
|
|
419 |
sl->ChildSignalTime(sigTime);
|
|
420 |
}
|
|
421 |
sl->ChildClose();
|
148
|
422 |
tslSync.Close();
|
0
|
423 |
return KErrNone;
|
|
424 |
}
|
|
425 |
|
|
426 |
|
|
427 |
TBMResult* Sync::Run(TBMUInt64 aIter, TInt* aCount)
|
|
428 |
{
|
|
429 |
TInt count = sizeof(iResults)/sizeof(iResults[0]);
|
|
430 |
|
|
431 |
for (TInt i = 0; i < count; ++i)
|
|
432 |
{
|
|
433 |
iResults[i].Reset(iMeasurements[i].iName);
|
|
434 |
iMeasurements[i].iFunc(&iResults[i], aIter, iMeasurements[i].iRemote);
|
|
435 |
iResults[i].Update();
|
|
436 |
}
|
|
437 |
|
|
438 |
*aCount = count;
|
|
439 |
return iResults;
|
|
440 |
}
|
|
441 |
|
|
442 |
void AddSync()
|
|
443 |
{
|
|
444 |
BMProgram* next = bmSuite;
|
|
445 |
bmSuite=(BMProgram*)&sync;
|
|
446 |
bmSuite->Next()=next;
|
|
447 |
}
|