Update contrib.
1 // Copyright (c) 1996-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\misc\cpumeter.cpp
18 #define __E32TEST_EXTENSION__
25 RTest test(_L("CPU METER"));
27 TBool CpuTimeSupported()
29 TTimeIntervalMicroSeconds time;
30 TInt err = RThread().GetCpuTime(time);
31 test(err == KErrNone || err == KErrNotSupported);
32 return err == KErrNone;
37 TInt r = UserSvr::HalFunction(EHalGroupKernel, EKernelHalNumLogicalCpus, 0, 0);
42 class CCpuMeter : public CBase
47 static CCpuMeter* New();
50 void Display(TInt aInterval);
54 RThread* iNullThreads;
55 TTimeIntervalMicroSeconds* iMeas[2];
59 CCpuMeter::CCpuMeter()
63 CCpuMeter::~CCpuMeter()
68 for (i=0; i<iNumCpus; ++i)
69 iNullThreads[i].Close();
70 User::Free(iNullThreads);
77 TInt CCpuMeter::Construct()
79 iNumCpus = NumberOfCpus();
80 iNullThreads = (RThread*)User::AllocZ(iNumCpus*sizeof(RThread));
81 iDelta = (TInt*)User::AllocZ(iNumCpus*sizeof(TInt));
82 iMeas[0] = (TTimeIntervalMicroSeconds*)User::AllocZ(iNumCpus*sizeof(TTimeIntervalMicroSeconds));
83 iMeas[1] = (TTimeIntervalMicroSeconds*)User::AllocZ(iNumCpus*sizeof(TTimeIntervalMicroSeconds));
84 if (!iNullThreads || !iDelta || !iMeas[0] || !iMeas[1])
87 _LIT(KLitKernelName, "ekern.exe*");
88 _LIT(KLitNull, "::Null");
89 TFindProcess fp(KLitKernelName);
90 test_KErrNone(fp.Next(kname));
91 test.Printf(_L("Found kernel process: %S\n"), &kname);
92 kname.Append(KLitNull);
94 for (i=0; i<iNumCpus; ++i)
96 TFullName tname(kname);
100 TFindThread ft(tname);
101 test_KErrNone(ft.Next(tname2));
102 TInt r = iNullThreads[i].Open(ft);
104 iNullThreads[i].FullName(tname2);
105 test.Printf(_L("Found and opened %S\n"), &tname2);
107 for (i=0; i<iNumCpus; ++i)
108 iNullThreads[i].GetCpuTime(iMeas[0][i]);
113 CCpuMeter* CCpuMeter::New()
115 CCpuMeter* p = new CCpuMeter;
118 TInt r = p->Construct();
127 void CCpuMeter::Measure()
130 for (i=0; i<iNumCpus; ++i)
131 iNullThreads[i].GetCpuTime(iMeas[iNextMeas][i]);
132 TInt prev = 1 - iNextMeas;
133 for (i=0; i<iNumCpus; ++i)
134 iDelta[i] = TInt(iMeas[iNextMeas][i].Int64() - iMeas[prev][i].Int64());
138 void CCpuMeter::Display(TInt aInterval)
142 for (i=0; i<iNumCpus; ++i)
144 TInt dv = (1000*(aInterval - iDelta[i]))/aInterval;
149 buf.AppendFormat(_L(" %4d"),dv);
151 buf.Append(TChar('\n'));
155 void UseKernelCpuTime()
157 test.Start(_L("Create CCpuMeter"));
158 CCpuMeter* m = CCpuMeter::New();
160 TInt iv = 1000500; // on average 1000.5 ms
162 CConsoleBase* console = test.Console();
166 User::AfterHighRes(1000000);
169 while (s!=KRequestPending)
171 User::WaitForRequest(s);
172 TKeyCode k = console->KeyCode();
187 _LIT(KLitThreadName,"IdleThread");
188 extern TInt CountNops(TAny*);
192 test.Start(_L("Create thread"));
194 TInt r=t.Create(KLitThreadName,CountNops,0x1000,NULL,NULL);
196 t.SetPriority(EPriorityAbsoluteVeryLow);
199 test.Next(_L("Get processor clock frequency"));
200 TMachineInfoV2Buf buf;
201 TMachineInfoV2& info=buf();
202 r=UserHal::MachineInfo(buf);
204 MaxCycles=info.iProcessorClockInKHz*1000;
205 test.Printf(_L("Clock frequency %dHz\n"),MaxCycles);
207 CConsoleBase* console=test.Console();
210 TInt timerperiod = 5;
211 UserSvr::HalFunction(EHalGroupEmulator,EEmulatorHalIntProperty,(TAny*)"TimerResolution",&timerperiod);
216 TUint32 init_count=NopCount;
217 TUint32 init_ms=User::NTickCount();
218 User::After(1000000);
219 TUint32 final_count=NopCount;
220 TUint32 final_ms=User::NTickCount();
221 TUint32 cycles=final_count-init_count;
222 TUint32 ms=final_ms-init_ms;
226 while (s!=KRequestPending)
228 User::WaitForRequest(s);
229 TKeyCode k=console->KeyCode();
233 TInt64 inst64 = MAKE_TINT64(0, cycles);
235 inst64/=MAKE_TINT64(0,ms);
236 MaxCycles=I64LOW(inst64);
237 test.Printf(_L("NOPs per second %u\n"),MaxCycles);
239 else if (k==EKeyEscape)
243 TInt64 used64=MAKE_TINT64(0, MaxCycles);
245 used64-=MAKE_TINT64(0,cycles);
247 used64/=MAKE_TINT64(0,ms);
248 used64/=MAKE_TINT64(0, MaxCycles);
249 test.Printf(_L("%4d\n"),I64INT(used64));
254 GLDEF_C TInt E32Main()
256 test.SetLogged(EFalse);
258 RThread().SetPriority(EPriorityAbsoluteHigh);
260 if (CpuTimeSupported())
264 if (NumberOfCpus()>1)
266 test.Printf(_L("Needs RThread::GetCpuTime() on SMP systems\n"));