author | hgs |
Thu, 10 Jun 2010 11:48:01 +0100 | |
changeset 149 | d9f1e5bfe28c |
parent 90 | 947f0dc9f7a8 |
child 201 | 43365a9b78a3 |
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; |
|
149 | 231 |
TInt64 us; |
232 |
||
233 |
// Test cpu time is initially zero |
|
0 | 234 |
FailIfError(thread.GetCpuTime(time)); |
235 |
test(time == 0); |
|
236 |
||
149 | 237 |
// Test cpu time is not increased while thread is waiting on semaphore |
0 | 238 |
thread.Resume(); |
239 |
User::After(KShortWait); |
|
149 | 240 |
FailIfError(thread.GetCpuTime(time2)); |
241 |
us = time2.Int64(); |
|
90
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); |
149 | 243 |
test(us < KTolerance); // wait should happen in less than 1ms |
0 | 244 |
|
149 | 245 |
// Test cpu time increases when thread allowed to run |
246 |
// We want to allow 2% tolerance for the thread's CPU time, as there could be |
|
247 |
// something else running on the system during that time which would result lower CPU time than the |
|
248 |
// actual KShortPeriod or KLongPeriod wait time. |
|
249 |
// Also User::After(t) might return within the range of <t, t + 1000000/64 + 2*NanoKarnelTickPeriod>. |
|
250 |
// Given all that - we expect that the the cpu time should be within the range of: |
|
251 |
// <t - 0.02*t, t + 15625 + 2*NanoKernelTickPeriod> |
|
252 |
// or <0.98*t, t + 15625 + 2*NanoKernelTickPeriod> |
|
253 |
TInt user_after_tolerance = 0; |
|
254 |
HAL::Get(HAL::ENanoTickPeriod, user_after_tolerance); |
|
255 |
user_after_tolerance += user_after_tolerance + 15625; |
|
256 |
||
0 | 257 |
(threadParam.iSem).Signal(); |
258 |
User::After(KShortWait); |
|
259 |
FailIfError(thread.GetCpuTime(time)); |
|
149 | 260 |
us = time.Int64() - time2.Int64(); |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
261 |
test.Printf(_L("Time %dus\n"), us); |
149 | 262 |
test(100*us >= 98*KShortWait); // left limit |
263 |
test(us - KShortWait <= user_after_tolerance); // right limit |
|
264 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
265 |
User::After(KLongWait); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
266 |
FailIfError(thread.GetCpuTime(time2)); |
149 | 267 |
us = time2.Int64() - time.Int64(); |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
268 |
test.Printf(_L("Time %dus\n"), us); |
149 | 269 |
test(100*us >= 98*KLongWait); // left limit |
270 |
test(us - KLongWait <= user_after_tolerance); // right limit |
|
0 | 271 |
|
272 |
// Test not increased while suspended |
|
273 |
thread.Suspend(); |
|
274 |
FailIfError(thread.GetCpuTime(time)); |
|
275 |
User::After(KShortWait); |
|
276 |
FailIfError(thread.GetCpuTime(time2)); |
|
277 |
test(time == time2); |
|
278 |
thread.Resume(); |
|
149 | 279 |
|
0 | 280 |
// Test not increased while dead |
281 |
thread.Kill(KErrNone); |
|
282 |
User::WaitForRequest(s); // wait on undertaker since that completes in supervisor thread |
|
283 |
FailIfError(thread.GetCpuTime(time)); |
|
284 |
User::After(KShortWait); |
|
285 |
FailIfError(thread.GetCpuTime(time2)); |
|
286 |
test(time == time2); |
|
287 |
||
288 |
RThread t; |
|
289 |
t.SetHandle(h); |
|
290 |
test(t.Id()==thread.Id()); |
|
291 |
t.Close(); |
|
292 |
u.Close(); |
|
293 |
thread.Close(); |
|
294 |
(threadParam.iSem).Close(); |
|
295 |
test.End(); |
|
296 |
} |
|
297 |
||
298 |
//! @SYMTestCaseID t_cputime_2 |
|
299 |
//! @SYMTestType CT |
|
300 |
//! @SYMTestCaseDesc Thread CPU time tests |
|
301 |
//! @SYMREQ CR RFID-66JJKX |
|
302 |
//! @SYMTestActions Tests cpu time when multiple threads are running |
|
303 |
//! @SYMTestExpectedResults Total time is divided evenly among running threads |
|
304 |
//! @SYMTestPriority High |
|
305 |
//! @SYMTestStatus Defined |
|
306 |
||
307 |
TBool DoTestThreadCpuTime2() // Returns ETrue if test passed |
|
308 |
{ |
|
309 |
test.Start(_L("Testing time shared between threads")); |
|
310 |
||
311 |
if (numCpus > 1) |
|
312 |
{ |
|
313 |
test.Printf(_L("** SMP system detected - not testing time shared between threads until load balancing optimized **\n")); |
|
314 |
return ETrue; |
|
315 |
} |
|
316 |
||
317 |
const TInt KMaxThreads = 4; |
|
318 |
||
319 |
TThreadParam threadParam; |
|
320 |
||
321 |
RThread* threads = NULL; |
|
322 |
threads = new(ELeave) RThread[numCpus*KMaxThreads]; |
|
323 |
FailIfError((threadParam.iSem).CreateLocal(0)); |
|
324 |
||
325 |
TBool pass = ETrue; |
|
326 |
for (TInt numThreads = 1 ; pass && numThreads <= KMaxThreads ; ++numThreads) |
|
327 |
{ |
|
328 |
test.Printf(_L(" testing with %d threads on each of %d CPUs:\n"), numThreads, numCpus); |
|
329 |
||
330 |
TInt i, j, k; |
|
331 |
for (i = 0 ; i < numThreads ; ++i) |
|
332 |
{ |
|
333 |
for (j = 0 ; j < numCpus ; ++j) |
|
334 |
{ |
|
335 |
TBuf<16> name; |
|
336 |
name.AppendFormat(_L("Thread%d%d"), i, j); |
|
337 |
threadParam.iCpu = j; |
|
338 |
k = i+j*KMaxThreads; |
|
339 |
FailIfError(threads[k].Create(name, ThreadFunction, 1024, NULL, &threadParam)); |
|
340 |
threads[k].SetPriority(EPriorityLess); |
|
341 |
threads[k].Resume(); |
|
342 |
} |
|
343 |
} |
|
344 |
||
345 |
User::After(KShortWait); // Pause to allow thread setup |
|
346 |
||
347 |
(threadParam.iSem).Signal(numThreads*numCpus); |
|
348 |
User::After(KLongWait); |
|
349 |
for (i = 0 ; i < numThreads ; ++i) |
|
350 |
for (j = 0 ; j < numCpus ; ++j) |
|
351 |
threads[i+j*KMaxThreads].Suspend(); |
|
352 |
||
353 |
TInt expected = KLongWait / numThreads; |
|
354 |
for (i = 0 ; i < numThreads ; ++i) |
|
355 |
{ |
|
356 |
for (j = 0 ; j < numCpus ; ++j) |
|
357 |
{ |
|
358 |
k = i+j*KMaxThreads; |
|
359 |
TTimeIntervalMicroSeconds time; |
|
360 |
FailIfError(threads[k].GetCpuTime(time)); |
|
361 |
||
362 |
TReal error = (100.0 * Abs(time.Int64() - expected)) / expected; |
|
363 |
||
364 |
test.Printf(_L(" %d%d: time == %ld, error == %d%%\n"), i, j, time.Int64(), TInt(error)); |
|
365 |
||
366 |
if (error >= 5.0) |
|
367 |
pass = EFalse; |
|
368 |
||
369 |
threads[k].Kill(KErrNone); |
|
370 |
TRequestStatus status; |
|
371 |
threads[k].Logon(status); |
|
372 |
User::WaitForRequest(status); |
|
373 |
test(status == KErrNone); |
|
374 |
CLOSE_AND_WAIT(threads[k]); |
|
375 |
} |
|
376 |
} |
|
377 |
} |
|
378 |
||
379 |
(threadParam.iSem).Close(); |
|
380 |
test.End(); |
|
381 |
||
382 |
return pass; |
|
383 |
} |
|
384 |
||
385 |
void TestThreadCpuTime2() |
|
386 |
{ |
|
387 |
#ifdef __WINS__ |
|
388 |
TBool pass = EFalse; |
|
389 |
for (TInt retry = 0 ; !pass && retry < 5 ; ++retry) |
|
390 |
{ |
|
391 |
if (retry > 0) |
|
392 |
{ |
|
393 |
test.Printf(_L("Test failed, retrying...\n")); |
|
394 |
EnsureSystemIdle(); |
|
395 |
} |
|
396 |
pass = DoTestThreadCpuTime2(); |
|
397 |
} |
|
398 |
test(pass); |
|
399 |
#else |
|
400 |
test(DoTestThreadCpuTime2()); |
|
401 |
#endif |
|
402 |
} |
|
403 |
||
404 |
TInt ThreadFunction2(TAny* aParam) |
|
405 |
{ |
|
406 |
TTimeIntervalMicroSeconds& time = *(TTimeIntervalMicroSeconds*)aParam; |
|
407 |
RThread thread; |
|
408 |
return thread.GetCpuTime(time); |
|
409 |
} |
|
410 |
||
411 |
#ifdef __MARM__ |
|
412 |
||
413 |
void DoTestThreadCpuTime3(TAny* aParam, TExitType aExpectedExitType, TInt aExpectedExitReason) |
|
414 |
{ |
|
415 |
RThread thread; |
|
416 |
FailIfError(thread.Create(_L("TestThread"), ThreadFunction2, 1024, NULL, aParam)); |
|
417 |
thread.Resume(); |
|
418 |
TRequestStatus status; |
|
419 |
thread.Logon(status); |
|
420 |
User::WaitForRequest(status); |
|
421 |
||
422 |
TExitCategoryName exitCat = thread.ExitCategory(); |
|
423 |
test.Printf(_L("Thread exit with type == %d, reason == %d, cat == %S\n"), |
|
424 |
thread.ExitType(), thread.ExitReason(), &exitCat); |
|
425 |
||
426 |
test(thread.ExitType() == aExpectedExitType); |
|
427 |
test(thread.ExitReason() == aExpectedExitReason); |
|
428 |
CLOSE_AND_WAIT(thread); |
|
429 |
} |
|
430 |
||
431 |
void TestThreadCpuTime3() |
|
432 |
{ |
|
433 |
// Test kernel writes the return value back to user-space with the correct permissions |
|
434 |
TTimeIntervalMicroSeconds time; |
|
435 |
DoTestThreadCpuTime3(&time, EExitKill, 0); // ok |
|
436 |
DoTestThreadCpuTime3((TAny*)0, EExitPanic, 3); // null pointer |
|
437 |
DoTestThreadCpuTime3((TAny*)0x64000000, EExitPanic, 3); // start of kernel data on moving memory model |
|
438 |
DoTestThreadCpuTime3((TAny*)0xc8000000, EExitPanic, 3); // start of kernel data on moving multiple model |
|
439 |
} |
|
440 |
||
441 |
#endif |
|
442 |
||
443 |
GLDEF_C TInt E32Main() |
|
444 |
{ |
|
445 |
test.Title(); |
|
446 |
test.Start(_L("T_CPUTIME")); |
|
36
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
447 |
|
0 | 448 |
if (numCpus > 1) |
449 |
FailIfError(SetCpuAffinity(0)); |
|
450 |
||
451 |
TestFastCounter(); |
|
452 |
if (GetCpuTimeIsSupported()) |
|
453 |
{ |
|
454 |
EnsureSystemIdle(); |
|
455 |
TestThreadCpuTime(); |
|
456 |
TestThreadCpuTime2(); |
|
457 |
#ifdef __MARM__ |
|
458 |
TestThreadCpuTime3(); |
|
459 |
#endif |
|
460 |
} |
|
461 |
test.End(); |
|
462 |
return 0; |
|
463 |
} |