author | hgs |
Fri, 23 Apr 2010 22:08:41 +0100 | |
changeset 121 | 661475905584 |
parent 90 | 947f0dc9f7a8 |
child 257 | 3e88ff8f41d5 |
permissions | -rw-r--r-- |
0 | 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); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
51 |
void DisplayCoreControlInfo(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
52 |
void ChangeNumberOfCores(TInt aNum); |
0 | 53 |
public: |
54 |
TInt iNumCpus; |
|
55 |
TInt iNextMeas; |
|
56 |
RThread* iNullThreads; |
|
57 |
TTimeIntervalMicroSeconds* iMeas[2]; |
|
58 |
TInt* iDelta; |
|
59 |
}; |
|
60 |
||
61 |
CCpuMeter::CCpuMeter() |
|
62 |
{ |
|
63 |
} |
|
64 |
||
65 |
CCpuMeter::~CCpuMeter() |
|
66 |
{ |
|
67 |
TInt i; |
|
68 |
if (iNullThreads) |
|
69 |
{ |
|
70 |
for (i=0; i<iNumCpus; ++i) |
|
71 |
iNullThreads[i].Close(); |
|
72 |
User::Free(iNullThreads); |
|
73 |
} |
|
74 |
User::Free(iMeas[0]); |
|
75 |
User::Free(iMeas[1]); |
|
76 |
User::Free(iDelta); |
|
77 |
} |
|
78 |
||
79 |
TInt CCpuMeter::Construct() |
|
80 |
{ |
|
81 |
iNumCpus = NumberOfCpus(); |
|
82 |
iNullThreads = (RThread*)User::AllocZ(iNumCpus*sizeof(RThread)); |
|
83 |
iDelta = (TInt*)User::AllocZ(iNumCpus*sizeof(TInt)); |
|
84 |
iMeas[0] = (TTimeIntervalMicroSeconds*)User::AllocZ(iNumCpus*sizeof(TTimeIntervalMicroSeconds)); |
|
85 |
iMeas[1] = (TTimeIntervalMicroSeconds*)User::AllocZ(iNumCpus*sizeof(TTimeIntervalMicroSeconds)); |
|
86 |
if (!iNullThreads || !iDelta || !iMeas[0] || !iMeas[1]) |
|
87 |
return KErrNoMemory; |
|
88 |
TFullName kname; |
|
89 |
_LIT(KLitKernelName, "ekern.exe*"); |
|
90 |
_LIT(KLitNull, "::Null"); |
|
91 |
TFindProcess fp(KLitKernelName); |
|
92 |
test_KErrNone(fp.Next(kname)); |
|
93 |
test.Printf(_L("Found kernel process: %S\n"), &kname); |
|
94 |
kname.Append(KLitNull); |
|
95 |
TInt i; |
|
96 |
for (i=0; i<iNumCpus; ++i) |
|
97 |
{ |
|
98 |
TFullName tname(kname); |
|
99 |
TFullName tname2; |
|
100 |
if (i>0) |
|
101 |
tname.AppendNum(i); |
|
102 |
TFindThread ft(tname); |
|
103 |
test_KErrNone(ft.Next(tname2)); |
|
104 |
TInt r = iNullThreads[i].Open(ft); |
|
105 |
test_KErrNone(r); |
|
106 |
iNullThreads[i].FullName(tname2); |
|
107 |
test.Printf(_L("Found and opened %S\n"), &tname2); |
|
108 |
} |
|
109 |
for (i=0; i<iNumCpus; ++i) |
|
110 |
iNullThreads[i].GetCpuTime(iMeas[0][i]); |
|
111 |
iNextMeas = 1; |
|
112 |
return KErrNone; |
|
113 |
} |
|
114 |
||
115 |
CCpuMeter* CCpuMeter::New() |
|
116 |
{ |
|
117 |
CCpuMeter* p = new CCpuMeter; |
|
118 |
if (!p) |
|
119 |
return 0; |
|
120 |
TInt r = p->Construct(); |
|
121 |
if (r!=KErrNone) |
|
122 |
{ |
|
123 |
delete p; |
|
124 |
return 0; |
|
125 |
} |
|
126 |
return p; |
|
127 |
} |
|
128 |
||
129 |
void CCpuMeter::Measure() |
|
130 |
{ |
|
131 |
TInt i; |
|
132 |
for (i=0; i<iNumCpus; ++i) |
|
133 |
iNullThreads[i].GetCpuTime(iMeas[iNextMeas][i]); |
|
134 |
TInt prev = 1 - iNextMeas; |
|
135 |
for (i=0; i<iNumCpus; ++i) |
|
136 |
iDelta[i] = TInt(iMeas[iNextMeas][i].Int64() - iMeas[prev][i].Int64()); |
|
137 |
iNextMeas = prev; |
|
138 |
} |
|
139 |
||
140 |
void CCpuMeter::Display(TInt aInterval) |
|
141 |
{ |
|
142 |
TBuf<80> buf; |
|
143 |
TInt i; |
|
144 |
for (i=0; i<iNumCpus; ++i) |
|
145 |
{ |
|
146 |
TInt dv = (1000*(aInterval - iDelta[i]))/aInterval; |
|
147 |
if (dv<0) |
|
148 |
dv=0; |
|
149 |
if (dv>1000) |
|
150 |
dv=1000; |
|
151 |
buf.AppendFormat(_L(" %4d"),dv); |
|
152 |
} |
|
153 |
buf.Append(TChar('\n')); |
|
154 |
test.Printf(buf); |
|
155 |
} |
|
156 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
157 |
void CCpuMeter::DisplayCoreControlInfo() |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
158 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
159 |
SCpuStates s; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
160 |
TInt r = UserSvr::HalFunction(EHalGroupKernel, EKernelHalCpuStates, &s, 0); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
161 |
if (r != KErrNone) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
162 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
163 |
test.Printf(_L("Error %d\n"), r); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
164 |
return; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
165 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
166 |
test.SetLogged(ETrue); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
167 |
test.Printf(_L(" TA=%08x IA=%08x CU=%08x GD=%08x DC=%08x\n"), s.iTA, s.iIA, s.iCU, s.iGD, s.iDC); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
168 |
test.Printf(_L(" SC=%08x RC=%08x PO=%02x CCS=%08x PODC=%08x\n"), s.iSC, s.iRC, s.iPO, s.iCCS, s.iPODC); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
169 |
TInt i; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
170 |
for (i=0; i<iNumCpus; ++i) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
171 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
172 |
test.Printf(_L("%1d:DS=%08x UDC=%08x UAC=%08x OP=%08x F=%08x\n"), i, s.iDS[i], s.iUDC[i], s.iUAC[i], s.iOP[i], s.iF[i]); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
173 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
174 |
test.SetLogged(EFalse); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
175 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
176 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
177 |
void CCpuMeter::ChangeNumberOfCores(TInt aNum) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
178 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
179 |
test.SetLogged(ETrue); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
180 |
test.Printf(_L("#CORES->%d\n"), aNum); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
181 |
TInt r = UserSvr::HalFunction(EHalGroupKernel, EKernelHalSetNumberOfCpus, (TAny*)aNum, 0); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
182 |
if (r != KErrNone) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
183 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
184 |
test.Printf(_L("Error %d\n"), r); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
185 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
186 |
test.SetLogged(EFalse); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
187 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
188 |
|
0 | 189 |
void UseKernelCpuTime() |
190 |
{ |
|
191 |
test.Start(_L("Create CCpuMeter")); |
|
192 |
CCpuMeter* m = CCpuMeter::New(); |
|
193 |
test_NotNull(m); |
|
194 |
TInt iv = 1000500; // on average 1000.5 ms |
|
195 |
TRequestStatus s; |
|
196 |
CConsoleBase* console = test.Console(); |
|
197 |
console->Read(s); |
|
198 |
FOREVER |
|
199 |
{ |
|
200 |
User::AfterHighRes(1000000); |
|
201 |
m->Measure(); |
|
202 |
m->Display(iv); |
|
203 |
while (s!=KRequestPending) |
|
204 |
{ |
|
205 |
User::WaitForRequest(s); |
|
206 |
TKeyCode k = console->KeyCode(); |
|
207 |
if (k == EKeyEscape) |
|
208 |
{ |
|
209 |
delete m; |
|
210 |
return; |
|
211 |
} |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
212 |
if (m->iNumCpus > 1) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
213 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
214 |
// SMP only options |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
215 |
if (k == EKeySpace) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
216 |
m->DisplayCoreControlInfo(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
217 |
else if (k>='1' && k<=('0'+m->iNumCpus)) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
218 |
m->ChangeNumberOfCores(k - '0'); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
219 |
} |
0 | 220 |
console->Read(s); |
221 |
} |
|
222 |
} |
|
223 |
} |
|
224 |
||
225 |
||
226 |
||
227 |
TUint32 NopCount=0; |
|
228 |
TUint MaxCycles; |
|
229 |
_LIT(KLitThreadName,"IdleThread"); |
|
230 |
extern TInt CountNops(TAny*); |
|
231 |
||
232 |
void MeasureByNOPs() |
|
233 |
{ |
|
234 |
test.Start(_L("Create thread")); |
|
235 |
RThread t; |
|
236 |
TInt r=t.Create(KLitThreadName,CountNops,0x1000,NULL,NULL); |
|
237 |
test(r==KErrNone); |
|
238 |
t.SetPriority(EPriorityAbsoluteVeryLow); |
|
239 |
t.Resume(); |
|
240 |
||
241 |
test.Next(_L("Get processor clock frequency")); |
|
242 |
TMachineInfoV2Buf buf; |
|
243 |
TMachineInfoV2& info=buf(); |
|
244 |
r=UserHal::MachineInfo(buf); |
|
245 |
test(r==KErrNone); |
|
246 |
MaxCycles=info.iProcessorClockInKHz*1000; |
|
247 |
test.Printf(_L("Clock frequency %dHz\n"),MaxCycles); |
|
248 |
TRequestStatus s; |
|
249 |
CConsoleBase* console=test.Console(); |
|
250 |
console->Read(s); |
|
251 |
#ifdef __WINS__ |
|
252 |
TInt timerperiod = 5; |
|
253 |
UserSvr::HalFunction(EHalGroupEmulator,EEmulatorHalIntProperty,(TAny*)"TimerResolution",&timerperiod); |
|
254 |
#endif |
|
255 |
||
256 |
FOREVER |
|
257 |
{ |
|
258 |
TUint32 init_count=NopCount; |
|
259 |
TUint32 init_ms=User::NTickCount(); |
|
260 |
User::After(1000000); |
|
261 |
TUint32 final_count=NopCount; |
|
262 |
TUint32 final_ms=User::NTickCount(); |
|
263 |
TUint32 cycles=final_count-init_count; |
|
264 |
TUint32 ms=final_ms-init_ms; |
|
265 |
#ifdef __WINS__ |
|
266 |
ms*=timerperiod; |
|
267 |
#endif |
|
268 |
while (s!=KRequestPending) |
|
269 |
{ |
|
270 |
User::WaitForRequest(s); |
|
271 |
TKeyCode k=console->KeyCode(); |
|
272 |
if (k==EKeyTab) |
|
273 |
{ |
|
274 |
// calibrate |
|
275 |
TInt64 inst64 = MAKE_TINT64(0, cycles); |
|
276 |
inst64*=1000; |
|
277 |
inst64/=MAKE_TINT64(0,ms); |
|
278 |
MaxCycles=I64LOW(inst64); |
|
279 |
test.Printf(_L("NOPs per second %u\n"),MaxCycles); |
|
280 |
} |
|
281 |
else if (k==EKeyEscape) |
|
282 |
return; |
|
283 |
console->Read(s); |
|
284 |
} |
|
285 |
TInt64 used64=MAKE_TINT64(0, MaxCycles); |
|
286 |
||
287 |
used64-=MAKE_TINT64(0,cycles); |
|
288 |
used64*=1000000; |
|
289 |
used64/=MAKE_TINT64(0,ms); |
|
290 |
used64/=MAKE_TINT64(0, MaxCycles); |
|
291 |
test.Printf(_L("%4d\n"),I64INT(used64)); |
|
292 |
} |
|
293 |
} |
|
294 |
||
295 |
||
296 |
GLDEF_C TInt E32Main() |
|
297 |
{ |
|
298 |
test.SetLogged(EFalse); |
|
299 |
test.Title(); |
|
300 |
RThread().SetPriority(EPriorityAbsoluteHigh); |
|
301 |
||
302 |
if (CpuTimeSupported()) |
|
303 |
{ |
|
304 |
UseKernelCpuTime(); |
|
305 |
} |
|
306 |
if (NumberOfCpus()>1) |
|
307 |
{ |
|
308 |
test.Printf(_L("Needs RThread::GetCpuTime() on SMP systems\n")); |
|
309 |
} |
|
310 |
else |
|
311 |
MeasureByNOPs(); |
|
312 |
||
313 |
return 0; |
|
314 |
} |
|
315 |