|
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". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // e32test\misc\cpumeter.cpp |
|
15 // |
|
16 // |
|
17 |
|
18 #define __E32TEST_EXTENSION__ |
|
19 |
|
20 #include <e32test.h> |
|
21 #include <e32hal.h> |
|
22 #include <e32svr.h> |
|
23 #include "u32std.h" |
|
24 |
|
25 RTest test(_L("CPU METER")); |
|
26 |
|
27 TBool CpuTimeSupported() |
|
28 { |
|
29 TTimeIntervalMicroSeconds time; |
|
30 TInt err = RThread().GetCpuTime(time); |
|
31 test(err == KErrNone || err == KErrNotSupported); |
|
32 return err == KErrNone; |
|
33 } |
|
34 |
|
35 TInt NumberOfCpus() |
|
36 { |
|
37 TInt r = UserSvr::HalFunction(EHalGroupKernel, EKernelHalNumLogicalCpus, 0, 0); |
|
38 test(r>0); |
|
39 return r; |
|
40 } |
|
41 |
|
42 class CCpuMeter : public CBase |
|
43 { |
|
44 public: |
|
45 CCpuMeter(); |
|
46 ~CCpuMeter(); |
|
47 static CCpuMeter* New(); |
|
48 TInt Construct(); |
|
49 void Measure(); |
|
50 void Display(TInt aInterval); |
|
51 public: |
|
52 TInt iNumCpus; |
|
53 TInt iNextMeas; |
|
54 RThread* iNullThreads; |
|
55 TTimeIntervalMicroSeconds* iMeas[2]; |
|
56 TInt* iDelta; |
|
57 }; |
|
58 |
|
59 CCpuMeter::CCpuMeter() |
|
60 { |
|
61 } |
|
62 |
|
63 CCpuMeter::~CCpuMeter() |
|
64 { |
|
65 TInt i; |
|
66 if (iNullThreads) |
|
67 { |
|
68 for (i=0; i<iNumCpus; ++i) |
|
69 iNullThreads[i].Close(); |
|
70 User::Free(iNullThreads); |
|
71 } |
|
72 User::Free(iMeas[0]); |
|
73 User::Free(iMeas[1]); |
|
74 User::Free(iDelta); |
|
75 } |
|
76 |
|
77 TInt CCpuMeter::Construct() |
|
78 { |
|
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]) |
|
85 return KErrNoMemory; |
|
86 TFullName kname; |
|
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); |
|
93 TInt i; |
|
94 for (i=0; i<iNumCpus; ++i) |
|
95 { |
|
96 TFullName tname(kname); |
|
97 TFullName tname2; |
|
98 if (i>0) |
|
99 tname.AppendNum(i); |
|
100 TFindThread ft(tname); |
|
101 test_KErrNone(ft.Next(tname2)); |
|
102 TInt r = iNullThreads[i].Open(ft); |
|
103 test_KErrNone(r); |
|
104 iNullThreads[i].FullName(tname2); |
|
105 test.Printf(_L("Found and opened %S\n"), &tname2); |
|
106 } |
|
107 for (i=0; i<iNumCpus; ++i) |
|
108 iNullThreads[i].GetCpuTime(iMeas[0][i]); |
|
109 iNextMeas = 1; |
|
110 return KErrNone; |
|
111 } |
|
112 |
|
113 CCpuMeter* CCpuMeter::New() |
|
114 { |
|
115 CCpuMeter* p = new CCpuMeter; |
|
116 if (!p) |
|
117 return 0; |
|
118 TInt r = p->Construct(); |
|
119 if (r!=KErrNone) |
|
120 { |
|
121 delete p; |
|
122 return 0; |
|
123 } |
|
124 return p; |
|
125 } |
|
126 |
|
127 void CCpuMeter::Measure() |
|
128 { |
|
129 TInt i; |
|
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()); |
|
135 iNextMeas = prev; |
|
136 } |
|
137 |
|
138 void CCpuMeter::Display(TInt aInterval) |
|
139 { |
|
140 TBuf<80> buf; |
|
141 TInt i; |
|
142 for (i=0; i<iNumCpus; ++i) |
|
143 { |
|
144 TInt dv = (1000*(aInterval - iDelta[i]))/aInterval; |
|
145 if (dv<0) |
|
146 dv=0; |
|
147 if (dv>1000) |
|
148 dv=1000; |
|
149 buf.AppendFormat(_L(" %4d"),dv); |
|
150 } |
|
151 buf.Append(TChar('\n')); |
|
152 test.Printf(buf); |
|
153 } |
|
154 |
|
155 void UseKernelCpuTime() |
|
156 { |
|
157 test.Start(_L("Create CCpuMeter")); |
|
158 CCpuMeter* m = CCpuMeter::New(); |
|
159 test_NotNull(m); |
|
160 TInt iv = 1000500; // on average 1000.5 ms |
|
161 TRequestStatus s; |
|
162 CConsoleBase* console = test.Console(); |
|
163 console->Read(s); |
|
164 FOREVER |
|
165 { |
|
166 User::AfterHighRes(1000000); |
|
167 m->Measure(); |
|
168 m->Display(iv); |
|
169 while (s!=KRequestPending) |
|
170 { |
|
171 User::WaitForRequest(s); |
|
172 TKeyCode k = console->KeyCode(); |
|
173 if (k == EKeyEscape) |
|
174 { |
|
175 delete m; |
|
176 return; |
|
177 } |
|
178 console->Read(s); |
|
179 } |
|
180 } |
|
181 } |
|
182 |
|
183 |
|
184 |
|
185 TUint32 NopCount=0; |
|
186 TUint MaxCycles; |
|
187 _LIT(KLitThreadName,"IdleThread"); |
|
188 extern TInt CountNops(TAny*); |
|
189 |
|
190 void MeasureByNOPs() |
|
191 { |
|
192 test.Start(_L("Create thread")); |
|
193 RThread t; |
|
194 TInt r=t.Create(KLitThreadName,CountNops,0x1000,NULL,NULL); |
|
195 test(r==KErrNone); |
|
196 t.SetPriority(EPriorityAbsoluteVeryLow); |
|
197 t.Resume(); |
|
198 |
|
199 test.Next(_L("Get processor clock frequency")); |
|
200 TMachineInfoV2Buf buf; |
|
201 TMachineInfoV2& info=buf(); |
|
202 r=UserHal::MachineInfo(buf); |
|
203 test(r==KErrNone); |
|
204 MaxCycles=info.iProcessorClockInKHz*1000; |
|
205 test.Printf(_L("Clock frequency %dHz\n"),MaxCycles); |
|
206 TRequestStatus s; |
|
207 CConsoleBase* console=test.Console(); |
|
208 console->Read(s); |
|
209 #ifdef __WINS__ |
|
210 TInt timerperiod = 5; |
|
211 UserSvr::HalFunction(EHalGroupEmulator,EEmulatorHalIntProperty,(TAny*)"TimerResolution",&timerperiod); |
|
212 #endif |
|
213 |
|
214 FOREVER |
|
215 { |
|
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; |
|
223 #ifdef __WINS__ |
|
224 ms*=timerperiod; |
|
225 #endif |
|
226 while (s!=KRequestPending) |
|
227 { |
|
228 User::WaitForRequest(s); |
|
229 TKeyCode k=console->KeyCode(); |
|
230 if (k==EKeyTab) |
|
231 { |
|
232 // calibrate |
|
233 TInt64 inst64 = MAKE_TINT64(0, cycles); |
|
234 inst64*=1000; |
|
235 inst64/=MAKE_TINT64(0,ms); |
|
236 MaxCycles=I64LOW(inst64); |
|
237 test.Printf(_L("NOPs per second %u\n"),MaxCycles); |
|
238 } |
|
239 else if (k==EKeyEscape) |
|
240 return; |
|
241 console->Read(s); |
|
242 } |
|
243 TInt64 used64=MAKE_TINT64(0, MaxCycles); |
|
244 |
|
245 used64-=MAKE_TINT64(0,cycles); |
|
246 used64*=1000000; |
|
247 used64/=MAKE_TINT64(0,ms); |
|
248 used64/=MAKE_TINT64(0, MaxCycles); |
|
249 test.Printf(_L("%4d\n"),I64INT(used64)); |
|
250 } |
|
251 } |
|
252 |
|
253 |
|
254 GLDEF_C TInt E32Main() |
|
255 { |
|
256 test.SetLogged(EFalse); |
|
257 test.Title(); |
|
258 RThread().SetPriority(EPriorityAbsoluteHigh); |
|
259 |
|
260 if (CpuTimeSupported()) |
|
261 { |
|
262 UseKernelCpuTime(); |
|
263 } |
|
264 if (NumberOfCpus()>1) |
|
265 { |
|
266 test.Printf(_L("Needs RThread::GetCpuTime() on SMP systems\n")); |
|
267 } |
|
268 else |
|
269 MeasureByNOPs(); |
|
270 |
|
271 return 0; |
|
272 } |
|
273 |