author | hgs |
Wed, 05 May 2010 05:11:16 +0100 | |
changeset 131 | e880629062dd |
parent 90 | 947f0dc9f7a8 |
child 109 | b3a1d9898418 |
child 176 | af6ec97d9189 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 2005-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 |
// e32test\misc\t_cputime.cpp |
|
15 |
// Tests User::FastCounter() and RThread::GetCpuTime() |
|
16 |
// Note: This test only works on the emulator when run in textshell mode. The |
|
17 |
// reason for this is that is assumes that it will be able to use 100% of CPU |
|
18 |
// time, but when techview is starting up there are many other threads consuming |
|
19 |
// CPU time. |
|
20 |
// |
|
21 |
// |
|
22 |
||
23 |
#include <e32test.h> |
|
24 |
#include <e32svr.h> |
|
25 |
#include <u32hal.h> |
|
26 |
#include <hal.h> |
|
27 |
#ifdef __WINS__ |
|
28 |
#include <e32wins.h> |
|
29 |
#endif |
|
30 |
||
31 |
RTest test(_L("T_CPUTIME")); |
|
32 |
||
33 |
_LIT(KUp, "up"); |
|
34 |
_LIT(KDown, "down"); |
|
35 |
||
36 |
const TInt KLongWait = 3000000; // 3 seconds |
|
37 |
const TInt KShortWait = 100000; // 0.1 seconds |
|
36
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
38 |
const TInt KTolerance = 1000; // 1 ms |
0 | 39 |
const TInt numCpus = UserSvr::HalFunction(EHalGroupKernel, EKernelHalNumLogicalCpus, 0, 0); |
40 |
||
41 |
#define FailIfError(EXPR) \ |
|
42 |
{ \ |
|
43 |
TInt aErr = (EXPR); \ |
|
44 |
if (aErr != KErrNone) \ |
|
45 |
{ \ |
|
46 |
test.Printf(_L("Return code == %d\n"), aErr); \ |
|
47 |
test(EFalse); \ |
|
48 |
} \ |
|
49 |
} |
|
50 |
||
51 |
class TThreadParam |
|
52 |
{ |
|
53 |
public: |
|
54 |
TInt iCpu; |
|
55 |
RSemaphore iSem; |
|
56 |
}; |
|
57 |
||
58 |
TBool GetCpuTimeIsSupported() |
|
59 |
{ |
|
60 |
RThread thread; |
|
61 |
TTimeIntervalMicroSeconds time; |
|
62 |
TInt err = thread.GetCpuTime(time); |
|
63 |
test(err == KErrNone || err == KErrNotSupported); |
|
64 |
return err == KErrNone; |
|
65 |
} |
|
66 |
||
67 |
TInt SetCpuAffinity(TInt aCore) |
|
68 |
{ |
|
69 |
TInt r = UserSvr::HalFunction(EHalGroupKernel, EKernelHalLockThreadToCpu, (TAny *)aCore, 0); |
|
70 |
test(r==KErrNone); |
|
71 |
return r; |
|
72 |
} |
|
73 |
||
74 |
||
75 |
//! @SYMTestCaseID t_cputime_0 |
|
76 |
//! @SYMTestType CT |
|
77 |
//! @SYMTestCaseDesc Fast counter tests |
|
78 |
//! @SYMREQ CR RFID-66JJKX |
|
79 |
//! @SYMTestActions Compares the high res timer against the nanokernel microsecond tick |
|
80 |
//! @SYMTestExpectedResults The differnce measured should be < 1% |
|
81 |
//! @SYMTestPriority High |
|
82 |
//! @SYMTestStatus Defined |
|
83 |
void TestFastCounter() |
|
84 |
{ |
|
85 |
test.Start(_L("Comparing NTickCount with FastCounter")); |
|
86 |
||
87 |
TInt tickPeriod = 0; |
|
88 |
FailIfError(HAL::Get(HAL::ENanoTickPeriod, tickPeriod)); |
|
89 |
test.Printf(_L(" tick period == %d\n"), tickPeriod); |
|
90 |
||
91 |
TInt countFreq = 0; |
|
92 |
FailIfError(HAL::Get(HAL::EFastCounterFrequency, countFreq)); |
|
93 |
test.Printf(_L(" count freq == %d\n"), countFreq); |
|
94 |
||
95 |
TBool fcCountsUp = 0; |
|
96 |
FailIfError(HAL::Get(HAL::EFastCounterCountsUp, fcCountsUp)); |
|
97 |
test.Printf(_L(" count dir == %S\n"), fcCountsUp ? &KUp : &KDown); |
|
98 |
||
99 |
TUint startTick = User::NTickCount(); |
|
100 |
TUint startCount = User::FastCounter(); |
|
101 |
||
102 |
User::After(KLongWait); |
|
103 |
||
104 |
TUint endTick = User::NTickCount(); |
|
105 |
TUint endCount = User::FastCounter(); |
|
106 |
||
107 |
TInt tickDiff = endTick - startTick; |
|
108 |
TInt countDiff = fcCountsUp ? (endCount - startCount) : (startCount - endCount); |
|
109 |
||
110 |
test.Printf(_L(" tick difference == %d\n"), tickDiff); |
|
111 |
test.Printf(_L(" fast count difference == %d\n"), countDiff); |
|
112 |
||
113 |
TInt elapsedTickUs = tickDiff * tickPeriod; |
|
114 |
TInt elapsedCountUs = (TInt)(((TInt64)1000000 * countDiff) / countFreq); |
|
115 |
||
116 |
test.Printf(_L(" tick time == %d\n"), elapsedTickUs); |
|
117 |
test.Printf(_L(" count time == %d\n"), elapsedCountUs); |
|
118 |
||
119 |
TReal diff = (100.0 * Abs(elapsedCountUs - elapsedTickUs)) / elapsedTickUs; |
|
120 |
||
121 |
test.Printf(_L(" %% difference == %f\n"), diff); |
|
122 |
test(diff < 1.0); |
|
123 |
test.End(); |
|
124 |
} |
|
125 |
||
126 |
TInt ThreadFunction(TAny* aParam) |
|
127 |
{ |
|
128 |
if (numCpus > 1) |
|
129 |
{ |
|
130 |
TInt& core = (static_cast<TThreadParam*>(aParam))->iCpu; |
|
131 |
FailIfError(SetCpuAffinity(core)); |
|
132 |
} |
|
133 |
||
134 |
RSemaphore& semaphore = (static_cast<TThreadParam*>(aParam))->iSem; |
|
135 |
semaphore.Wait(); |
|
136 |
for (;;) |
|
137 |
{ |
|
138 |
// Spin |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
void EnsureSystemIdle() |
|
143 |
{ |
|
144 |
// This test assumes 100% cpu resource is available, so it can fail on |
|
145 |
// windows builds if something else is running in the background. This |
|
146 |
// function attempts to wait for the system to become idle. |
|
147 |
||
148 |
#ifdef __WINS__ |
|
149 |
||
150 |
const TInt KMaxWait = 60 * 1000000; |
|
151 |
const TInt KSampleTime = 1 * 1000000; |
|
152 |
const TInt KWaitTime = 5 * 1000000; |
|
153 |
||
154 |
test.Start(_L("Waiting for system to become idle")); |
|
155 |
TInt totalTime = 0; |
|
156 |
TBool idle; |
|
157 |
do |
|
158 |
{ |
|
159 |
test(totalTime < KMaxWait); |
|
160 |
||
161 |
TThreadParam threadParam; |
|
162 |
FailIfError((threadParam.iSem).CreateLocal(0)); |
|
163 |
threadParam.iCpu = 1; |
|
164 |
||
165 |
RThread thread; |
|
166 |
FailIfError(thread.Create(_L("Thread"), ThreadFunction, 1024, NULL, &threadParam)); |
|
167 |
thread.SetPriority(EPriorityLess); |
|
168 |
thread.Resume(); |
|
169 |
||
170 |
User::After(KShortWait); // Pause to allow thread setup |
|
171 |
||
172 |
(threadParam.iSem).Signal(); |
|
173 |
User::After(KSampleTime); |
|
174 |
thread.Suspend(); |
|
175 |
||
176 |
TTimeIntervalMicroSeconds time; |
|
177 |
FailIfError(thread.GetCpuTime(time)); |
|
178 |
TReal error = (100.0 * Abs(time.Int64() - KSampleTime)) / KSampleTime; |
|
179 |
test.Printf(_L(" time == %ld, error == %f%%\n"), time, error); |
|
180 |
||
181 |
idle = error < 2.0; |
|
182 |
||
183 |
thread.Kill(KErrNone); |
|
184 |
TRequestStatus status; |
|
185 |
thread.Logon(status); |
|
186 |
User::WaitForRequest(status); |
|
187 |
test(status == KErrNone); |
|
188 |
CLOSE_AND_WAIT(thread); |
|
189 |
||
190 |
(threadParam.iSem).Close(); |
|
191 |
||
192 |
if (!idle) |
|
193 |
User::After(KWaitTime); // Allow system to finish whatever it's doing |
|
194 |
||
195 |
totalTime += KShortWait + KSampleTime + KWaitTime; |
|
196 |
} |
|
197 |
while(!idle); |
|
198 |
||
199 |
test.End(); |
|
200 |
||
201 |
#endif |
|
202 |
} |
|
203 |
||
204 |
//! @SYMTestCaseID t_cputime_1 |
|
205 |
//! @SYMTestType CT |
|
206 |
//! @SYMTestCaseDesc Thread CPU time tests |
|
207 |
//! @SYMREQ CR RFID-66JJKX |
|
208 |
//! @SYMTestActions Tests cpu time when a thread is put through the various states |
|
209 |
//! @SYMTestExpectedResults Reported cpu time increses only when the thread is running |
|
210 |
//! @SYMTestPriority High |
|
211 |
//! @SYMTestStatus Defined |
|
212 |
void TestThreadCpuTime() |
|
213 |
{ |
|
214 |
test.Start(_L("CPU thread time unit tests")); |
|
215 |
||
216 |
TThreadParam threadParam; |
|
217 |
FailIfError((threadParam.iSem).CreateLocal(0)); |
|
36
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
218 |
threadParam.iCpu = 0; // Later tests will exercise other CPUs |
0 | 219 |
|
220 |
RThread thread; |
|
221 |
RUndertaker u; |
|
222 |
TInt h; |
|
223 |
TRequestStatus s; |
|
224 |
FailIfError(thread.Create(_L("Thread"), ThreadFunction, 1024, NULL, &threadParam)); |
|
225 |
thread.SetPriority(EPriorityLess); |
|
226 |
FailIfError(u.Create()); |
|
227 |
FailIfError(u.Logon(s,h)); |
|
228 |
test(s==KRequestPending); |
|
229 |
||
230 |
TTimeIntervalMicroSeconds time, time2; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
231 |
TUint us; |
0 | 232 |
|
233 |
// Test time is initially zero |
|
234 |
FailIfError(thread.GetCpuTime(time)); |
|
235 |
test(time == 0); |
|
236 |
||
237 |
// Test not increased while waiting on semaphore |
|
238 |
thread.Resume(); |
|
239 |
User::After(KShortWait); |
|
240 |
FailIfError(thread.GetCpuTime(time)); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
241 |
us = I64LOW(time.Int64()); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
242 |
test.Printf(_L("Time %dus\n"), us); |
0 | 243 |
test(time < KTolerance); // wait happens in less than 0.5ms |
244 |
||
245 |
// Test increases when thread allowed to run |
|
246 |
(threadParam.iSem).Signal(); |
|
247 |
User::After(KShortWait); |
|
248 |
FailIfError(thread.GetCpuTime(time)); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
249 |
us = I64LOW(time.Int64()); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
250 |
test.Printf(_L("Time %dus\n"), us); |
36
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
251 |
test(time > (KShortWait - KTolerance)); |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
252 |
User::After(KLongWait); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
253 |
FailIfError(thread.GetCpuTime(time2)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
254 |
us = I64LOW(time2.Int64()); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
255 |
test.Printf(_L("Time %dus\n"), us); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
256 |
test(time2.Int64() - time.Int64() > (KLongWait - KTolerance)); |
0 | 257 |
|
258 |
// Test not increased while suspended |
|
259 |
thread.Suspend(); |
|
260 |
FailIfError(thread.GetCpuTime(time)); |
|
261 |
User::After(KShortWait); |
|
262 |
FailIfError(thread.GetCpuTime(time2)); |
|
263 |
test(time == time2); |
|
264 |
thread.Resume(); |
|
265 |
||
266 |
// Test not increased while dead |
|
267 |
thread.Kill(KErrNone); |
|
268 |
User::WaitForRequest(s); // wait on undertaker since that completes in supervisor thread |
|
269 |
FailIfError(thread.GetCpuTime(time)); |
|
270 |
User::After(KShortWait); |
|
271 |
FailIfError(thread.GetCpuTime(time2)); |
|
272 |
test(time == time2); |
|
273 |
||
274 |
RThread t; |
|
275 |
t.SetHandle(h); |
|
276 |
test(t.Id()==thread.Id()); |
|
277 |
t.Close(); |
|
278 |
u.Close(); |
|
279 |
thread.Close(); |
|
280 |
(threadParam.iSem).Close(); |
|
281 |
test.End(); |
|
282 |
} |
|
283 |
||
284 |
//! @SYMTestCaseID t_cputime_2 |
|
285 |
//! @SYMTestType CT |
|
286 |
//! @SYMTestCaseDesc Thread CPU time tests |
|
287 |
//! @SYMREQ CR RFID-66JJKX |
|
288 |
//! @SYMTestActions Tests cpu time when multiple threads are running |
|
289 |
//! @SYMTestExpectedResults Total time is divided evenly among running threads |
|
290 |
//! @SYMTestPriority High |
|
291 |
//! @SYMTestStatus Defined |
|
292 |
||
293 |
TBool DoTestThreadCpuTime2() // Returns ETrue if test passed |
|
294 |
{ |
|
295 |
test.Start(_L("Testing time shared between threads")); |
|
296 |
||
297 |
if (numCpus > 1) |
|
298 |
{ |
|
299 |
test.Printf(_L("** SMP system detected - not testing time shared between threads until load balancing optimized **\n")); |
|
300 |
return ETrue; |
|
301 |
} |
|
302 |
||
303 |
const TInt KMaxThreads = 4; |
|
304 |
||
305 |
TThreadParam threadParam; |
|
306 |
||
307 |
RThread* threads = NULL; |
|
308 |
threads = new(ELeave) RThread[numCpus*KMaxThreads]; |
|
309 |
FailIfError((threadParam.iSem).CreateLocal(0)); |
|
310 |
||
311 |
TBool pass = ETrue; |
|
312 |
for (TInt numThreads = 1 ; pass && numThreads <= KMaxThreads ; ++numThreads) |
|
313 |
{ |
|
314 |
test.Printf(_L(" testing with %d threads on each of %d CPUs:\n"), numThreads, numCpus); |
|
315 |
||
316 |
TInt i, j, k; |
|
317 |
for (i = 0 ; i < numThreads ; ++i) |
|
318 |
{ |
|
319 |
for (j = 0 ; j < numCpus ; ++j) |
|
320 |
{ |
|
321 |
TBuf<16> name; |
|
322 |
name.AppendFormat(_L("Thread%d%d"), i, j); |
|
323 |
threadParam.iCpu = j; |
|
324 |
k = i+j*KMaxThreads; |
|
325 |
FailIfError(threads[k].Create(name, ThreadFunction, 1024, NULL, &threadParam)); |
|
326 |
threads[k].SetPriority(EPriorityLess); |
|
327 |
threads[k].Resume(); |
|
328 |
} |
|
329 |
} |
|
330 |
||
331 |
User::After(KShortWait); // Pause to allow thread setup |
|
332 |
||
333 |
(threadParam.iSem).Signal(numThreads*numCpus); |
|
334 |
User::After(KLongWait); |
|
335 |
for (i = 0 ; i < numThreads ; ++i) |
|
336 |
for (j = 0 ; j < numCpus ; ++j) |
|
337 |
threads[i+j*KMaxThreads].Suspend(); |
|
338 |
||
339 |
TInt expected = KLongWait / numThreads; |
|
340 |
for (i = 0 ; i < numThreads ; ++i) |
|
341 |
{ |
|
342 |
for (j = 0 ; j < numCpus ; ++j) |
|
343 |
{ |
|
344 |
k = i+j*KMaxThreads; |
|
345 |
TTimeIntervalMicroSeconds time; |
|
346 |
FailIfError(threads[k].GetCpuTime(time)); |
|
347 |
||
348 |
TReal error = (100.0 * Abs(time.Int64() - expected)) / expected; |
|
349 |
||
350 |
test.Printf(_L(" %d%d: time == %ld, error == %d%%\n"), i, j, time.Int64(), TInt(error)); |
|
351 |
||
352 |
if (error >= 5.0) |
|
353 |
pass = EFalse; |
|
354 |
||
355 |
threads[k].Kill(KErrNone); |
|
356 |
TRequestStatus status; |
|
357 |
threads[k].Logon(status); |
|
358 |
User::WaitForRequest(status); |
|
359 |
test(status == KErrNone); |
|
360 |
CLOSE_AND_WAIT(threads[k]); |
|
361 |
} |
|
362 |
} |
|
363 |
} |
|
364 |
||
365 |
(threadParam.iSem).Close(); |
|
366 |
test.End(); |
|
367 |
||
368 |
return pass; |
|
369 |
} |
|
370 |
||
371 |
void TestThreadCpuTime2() |
|
372 |
{ |
|
373 |
#ifdef __WINS__ |
|
374 |
TBool pass = EFalse; |
|
375 |
for (TInt retry = 0 ; !pass && retry < 5 ; ++retry) |
|
376 |
{ |
|
377 |
if (retry > 0) |
|
378 |
{ |
|
379 |
test.Printf(_L("Test failed, retrying...\n")); |
|
380 |
EnsureSystemIdle(); |
|
381 |
} |
|
382 |
pass = DoTestThreadCpuTime2(); |
|
383 |
} |
|
384 |
test(pass); |
|
385 |
#else |
|
386 |
test(DoTestThreadCpuTime2()); |
|
387 |
#endif |
|
388 |
} |
|
389 |
||
390 |
TInt ThreadFunction2(TAny* aParam) |
|
391 |
{ |
|
392 |
TTimeIntervalMicroSeconds& time = *(TTimeIntervalMicroSeconds*)aParam; |
|
393 |
RThread thread; |
|
394 |
return thread.GetCpuTime(time); |
|
395 |
} |
|
396 |
||
397 |
#ifdef __MARM__ |
|
398 |
||
399 |
void DoTestThreadCpuTime3(TAny* aParam, TExitType aExpectedExitType, TInt aExpectedExitReason) |
|
400 |
{ |
|
401 |
RThread thread; |
|
402 |
FailIfError(thread.Create(_L("TestThread"), ThreadFunction2, 1024, NULL, aParam)); |
|
403 |
thread.Resume(); |
|
404 |
TRequestStatus status; |
|
405 |
thread.Logon(status); |
|
406 |
User::WaitForRequest(status); |
|
407 |
||
408 |
TExitCategoryName exitCat = thread.ExitCategory(); |
|
409 |
test.Printf(_L("Thread exit with type == %d, reason == %d, cat == %S\n"), |
|
410 |
thread.ExitType(), thread.ExitReason(), &exitCat); |
|
411 |
||
412 |
test(thread.ExitType() == aExpectedExitType); |
|
413 |
test(thread.ExitReason() == aExpectedExitReason); |
|
414 |
CLOSE_AND_WAIT(thread); |
|
415 |
} |
|
416 |
||
417 |
void TestThreadCpuTime3() |
|
418 |
{ |
|
419 |
// Test kernel writes the return value back to user-space with the correct permissions |
|
420 |
TTimeIntervalMicroSeconds time; |
|
421 |
DoTestThreadCpuTime3(&time, EExitKill, 0); // ok |
|
422 |
DoTestThreadCpuTime3((TAny*)0, EExitPanic, 3); // null pointer |
|
423 |
DoTestThreadCpuTime3((TAny*)0x64000000, EExitPanic, 3); // start of kernel data on moving memory model |
|
424 |
DoTestThreadCpuTime3((TAny*)0xc8000000, EExitPanic, 3); // start of kernel data on moving multiple model |
|
425 |
} |
|
426 |
||
427 |
#endif |
|
428 |
||
429 |
GLDEF_C TInt E32Main() |
|
430 |
{ |
|
431 |
test.Title(); |
|
432 |
test.Start(_L("T_CPUTIME")); |
|
36
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
433 |
|
0 | 434 |
if (numCpus > 1) |
435 |
FailIfError(SetCpuAffinity(0)); |
|
436 |
||
437 |
TestFastCounter(); |
|
438 |
if (GetCpuTimeIsSupported()) |
|
439 |
{ |
|
440 |
EnsureSystemIdle(); |
|
441 |
TestThreadCpuTime(); |
|
442 |
TestThreadCpuTime2(); |
|
443 |
#ifdef __MARM__ |
|
444 |
TestThreadCpuTime3(); |
|
445 |
#endif |
|
446 |
} |
|
447 |
test.End(); |
|
448 |
return 0; |
|
449 |
} |