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 <e32svr.h>
|
|
18 |
|
|
19 |
#include "bm_suite.h"
|
|
20 |
|
|
21 |
class Thread : public BMProgram
|
|
22 |
{
|
|
23 |
public :
|
|
24 |
Thread() : BMProgram(_L("Threads"))
|
|
25 |
{}
|
|
26 |
virtual TBMResult* Run(TBMUInt64 aIter, TInt* aCount);
|
|
27 |
|
|
28 |
typedef void (*MeasurementFunc)(TBMResult*, TBMUInt64 aIter);
|
|
29 |
struct Measurement
|
|
30 |
{
|
|
31 |
MeasurementFunc iFunc;
|
|
32 |
TPtrC iName;
|
|
33 |
|
|
34 |
Measurement(MeasurementFunc aFunc, const TDesC& aName) :
|
|
35 |
iFunc(aFunc), iName(aName) {}
|
|
36 |
};
|
|
37 |
|
|
38 |
static TBMResult iResults[];
|
|
39 |
static Measurement iMeasurements[];
|
|
40 |
|
|
41 |
static TBMTicks iChildTime;
|
|
42 |
|
|
43 |
static void Creation(TBMResult*, TBMUInt64 aIter);
|
|
44 |
static TInt CreationChild(TAny*);
|
|
45 |
static void CreationSuicide(TBMResult*, TBMUInt64 aIter);
|
|
46 |
static TInt CreationSuicideChild(TAny*);
|
|
47 |
static void Suicide(TBMResult*, TBMUInt64 aIter);
|
|
48 |
static TInt SuicideChild(TAny*);
|
|
49 |
static void Killing(TBMResult*, TBMUInt64 aIter);
|
|
50 |
static TInt KillingChild(TAny*);
|
|
51 |
static void SetTls(TBMResult*, TBMUInt64 aIter);
|
|
52 |
static void GetTls(TBMResult*, TBMUInt64 aIter);
|
|
53 |
|
|
54 |
void EnableCleanup()
|
|
55 |
{
|
|
56 |
TInt prio = BMProgram::SetAbsPriority(RThread(), iOrigAbsPriority);
|
|
57 |
BMProgram::SetAbsPriority(RThread(), prio);
|
|
58 |
}
|
|
59 |
};
|
|
60 |
|
|
61 |
Thread::Measurement Thread::iMeasurements[] =
|
|
62 |
{
|
|
63 |
Measurement(&Thread::Creation, _L("Thread Creation Latency")),
|
|
64 |
Measurement(&Thread::CreationSuicide, _L("Thread Creation Suicide")),
|
|
65 |
Measurement(&Thread::Suicide, _L("Thread Suicide")),
|
|
66 |
Measurement(&Thread::Killing, _L("Thread Killing")),
|
|
67 |
Measurement(&Thread::SetTls, _L("Setting per-thread data")),
|
|
68 |
Measurement(&Thread::GetTls, _L("Getting per-thread data"))
|
|
69 |
};
|
|
70 |
TBMResult Thread::iResults[sizeof(Thread::iMeasurements)/sizeof(Thread::iMeasurements[0])];
|
|
71 |
|
|
72 |
TBMTicks Thread::iChildTime;
|
|
73 |
|
|
74 |
static Thread prog;
|
|
75 |
|
|
76 |
void Thread::Creation(TBMResult* aResult, TBMUInt64 aIter)
|
|
77 |
{
|
|
78 |
for (TBMUInt64 i = 0; i < aIter; ++i)
|
|
79 |
{
|
|
80 |
RThread child;
|
|
81 |
TRequestStatus st;
|
|
82 |
TBMTicks t1;
|
|
83 |
::bmTimer.Stamp(&t1);
|
|
84 |
TInt r = child.Create(KNullDesC, Thread::CreationChild, 0x2000, NULL, NULL);
|
|
85 |
BM_ERROR(r, r == KErrNone);
|
|
86 |
child.Logon(st);
|
|
87 |
BMProgram::SetAbsPriority(RThread(), KBMPriorityHigh);
|
|
88 |
child.Resume();
|
|
89 |
User::WaitForRequest(st);
|
|
90 |
BM_ERROR(st.Int(), st == KErrNone);
|
|
91 |
aResult->Cumulate(TBMTicksDelta(t1, iChildTime));
|
|
92 |
CLOSE_AND_WAIT(child);
|
|
93 |
prog.EnableCleanup();
|
|
94 |
}
|
|
95 |
}
|
|
96 |
|
|
97 |
TInt Thread::CreationChild(TAny*)
|
|
98 |
{
|
|
99 |
::bmTimer.Stamp(&iChildTime);
|
|
100 |
return KErrNone;
|
|
101 |
}
|
|
102 |
|
|
103 |
void Thread::CreationSuicide(TBMResult* aResult, TBMUInt64 aIter)
|
|
104 |
{
|
|
105 |
for (TBMUInt64 i = 0; i < aIter; ++i)
|
|
106 |
{
|
|
107 |
RThread child;
|
|
108 |
TRequestStatus st;
|
|
109 |
TBMTicks t1;
|
|
110 |
::bmTimer.Stamp(&t1);
|
|
111 |
TInt r = child.Create(KNullDesC, Thread::CreationSuicideChild, 0x2000, NULL, NULL);
|
|
112 |
BM_ERROR(r, r == KErrNone);
|
|
113 |
child.Logon(st);
|
|
114 |
BMProgram::SetAbsPriority(RThread(), KBMPriorityLow);
|
|
115 |
child.Resume();
|
|
116 |
User::WaitForRequest(st);
|
|
117 |
BM_ERROR(st.Int(), st == KErrNone);
|
|
118 |
TBMTicks t2;
|
|
119 |
::bmTimer.Stamp(&t2);
|
|
120 |
aResult->Cumulate(TBMTicksDelta(t1, t2));
|
|
121 |
CLOSE_AND_WAIT(child);
|
|
122 |
prog.EnableCleanup();
|
|
123 |
}
|
|
124 |
}
|
|
125 |
|
|
126 |
TInt Thread::CreationSuicideChild(TAny*)
|
|
127 |
{
|
|
128 |
return KErrNone;
|
|
129 |
}
|
|
130 |
|
|
131 |
void Thread::Suicide(TBMResult* aResult, TBMUInt64 aIter)
|
|
132 |
{
|
|
133 |
for (TBMUInt64 i = 0; i < aIter; ++i)
|
|
134 |
{
|
|
135 |
RThread child;
|
|
136 |
TRequestStatus st;
|
|
137 |
TInt r = child.Create(KNullDesC, Thread::SuicideChild, 0x2000, NULL, NULL);
|
|
138 |
BM_ERROR(r, r == KErrNone);
|
|
139 |
child.Logon(st);
|
|
140 |
BMProgram::SetAbsPriority(RThread(), KBMPriorityLow);
|
|
141 |
child.Resume();
|
|
142 |
User::WaitForRequest(st);
|
|
143 |
BM_ERROR(st.Int(), st == KErrNone);
|
|
144 |
TBMTicks t2;
|
|
145 |
::bmTimer.Stamp(&t2);
|
|
146 |
aResult->Cumulate(TBMTicksDelta(iChildTime, t2));
|
|
147 |
CLOSE_AND_WAIT(child);
|
|
148 |
prog.EnableCleanup();
|
|
149 |
}
|
|
150 |
}
|
|
151 |
|
|
152 |
TInt Thread::SuicideChild(TAny*)
|
|
153 |
{
|
|
154 |
::bmTimer.Stamp(&iChildTime);
|
|
155 |
return KErrNone;
|
|
156 |
}
|
|
157 |
|
|
158 |
void Thread::Killing(TBMResult* aResult, TBMUInt64 aIter)
|
|
159 |
{
|
|
160 |
for (TBMUInt64 i = 0; i < aIter; ++i)
|
|
161 |
{
|
|
162 |
RThread child;
|
|
163 |
TRequestStatus st;
|
|
164 |
TInt r = child.Create(KNullDesC, Thread::KillingChild, 0x2000, NULL, NULL);
|
|
165 |
BM_ERROR(r, r == KErrNone);
|
|
166 |
child.Logon(st);
|
148
|
167 |
BMProgram::SetAbsPriority(RThread(), KBMPriorityLow);
|
|
168 |
child.Resume();
|
|
169 |
User::After(1000); // Give the child thread a chance to run - killing it too earlier can leave the heap locked
|
0
|
170 |
BMProgram::SetAbsPriority(RThread(), KBMPriorityHigh);
|
|
171 |
TBMTicks t1;
|
|
172 |
::bmTimer.Stamp(&t1);
|
|
173 |
child.Kill(KErrCancel);
|
|
174 |
User::WaitForRequest(st);
|
|
175 |
BM_ERROR(st.Int(), st == KErrCancel);
|
|
176 |
TBMTicks t2;
|
|
177 |
::bmTimer.Stamp(&t2);
|
|
178 |
aResult->Cumulate(TBMTicksDelta(t1, t2));
|
|
179 |
CLOSE_AND_WAIT(child);
|
|
180 |
prog.EnableCleanup();
|
|
181 |
}
|
|
182 |
}
|
|
183 |
|
|
184 |
TInt Thread::KillingChild(TAny*)
|
|
185 |
{
|
|
186 |
User::WaitForAnyRequest();
|
|
187 |
return KErrNone;
|
|
188 |
}
|
|
189 |
|
|
190 |
#define TLS_KEY ((TInt32) &Thread::SetTls)
|
|
191 |
|
|
192 |
void Thread::SetTls(TBMResult* aResult, TBMUInt64 aIter)
|
|
193 |
{
|
|
194 |
aIter <<= 4;
|
|
195 |
|
|
196 |
TBMTimeInterval ti;
|
|
197 |
ti.Begin();
|
|
198 |
for (TBMUInt64 i = 0; i < aIter; ++i)
|
|
199 |
{
|
|
200 |
TInt r = UserSvr::DllSetTls(TLS_KEY, 0);
|
|
201 |
BM_ERROR(r, r == KErrNone);
|
|
202 |
}
|
|
203 |
TBMTicks t = ti.End();
|
|
204 |
aResult->Cumulate(t, aIter);
|
|
205 |
|
|
206 |
UserSvr::DllFreeTls(TLS_KEY);
|
|
207 |
}
|
|
208 |
|
|
209 |
void Thread::GetTls(TBMResult* aResult, TBMUInt64 aIter)
|
|
210 |
{
|
|
211 |
aIter <<= 4;
|
|
212 |
|
|
213 |
TInt r = UserSvr::DllSetTls(TLS_KEY, 0);
|
|
214 |
BM_ERROR(r, r == KErrNone);
|
|
215 |
|
|
216 |
TBMTimeInterval ti;
|
|
217 |
ti.Begin();
|
|
218 |
for (TBMUInt64 i = 0; i < aIter; ++i)
|
|
219 |
{
|
|
220 |
UserSvr::DllTls(TLS_KEY);
|
|
221 |
}
|
|
222 |
TBMTicks t = ti.End();
|
|
223 |
aResult->Cumulate(t, aIter);
|
|
224 |
|
|
225 |
UserSvr::DllFreeTls(TLS_KEY);
|
|
226 |
}
|
|
227 |
|
|
228 |
TBMResult* Thread::Run(TBMUInt64 aIter, TInt* aCount)
|
|
229 |
{
|
|
230 |
TInt count = sizeof(iResults)/sizeof(iResults[0]);
|
|
231 |
|
|
232 |
for (TInt i = 0; i < count; ++i)
|
|
233 |
{
|
|
234 |
iResults[i].Reset(iMeasurements[i].iName);
|
|
235 |
iMeasurements[i].iFunc(&iResults[i], aIter);
|
|
236 |
iResults[i].Update();
|
|
237 |
}
|
|
238 |
|
|
239 |
*aCount = count;
|
|
240 |
return iResults;
|
|
241 |
}
|
|
242 |
|
|
243 |
void AddThread()
|
|
244 |
{
|
|
245 |
BMProgram* next = bmSuite;
|
|
246 |
bmSuite=(BMProgram*)&prog;
|
|
247 |
bmSuite->Next()=next;
|
|
248 |
}
|