Update contrib.
1 // Copyright (c) 1999-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\realtime\t_lat2.cpp
20 #include <e32property.h>
21 #include <e32atomics.h>
25 _LIT(KLatencyLddFileName,"D_LATNCY");
26 _LIT(KThreadName,"LatencyThreadU");
28 RTest test(_L("Latency"));
32 struct SFullLatencyResults : public SLatencyResults
46 void Update(SLatencyResults& aResults);
49 SFullLatencyResults Latencies;
50 volatile TUint32 UpdateCount=0;
52 TUint TimerToMicroseconds(TUint aTimerValue)
54 return (aTimerValue*1000+TicksPerMs-1)/TicksPerMs;
57 void SFullLatencyResults::Update(SLatencyResults& aResults)
59 __e32_atomic_add_acq32(&UpdateCount, 1);
63 if (aResults.iIntTicks>iIntTicks)
65 iIntTicks=aResults.iIntTicks;
66 iIntRetAddr=aResults.iIntRetAddr;
67 #ifdef __CAPTURE_EXTRAS
68 iIntCpsr=aResults.iIntSpsr;
69 iIntR14=aResults.iIntR14;
72 if (aResults.iKernThreadTicks>iKernThreadTicks)
74 iKernThreadTicks=aResults.iKernThreadTicks;
75 iKernRetAddr=aResults.iIntRetAddr;
76 #ifdef __CAPTURE_EXTRAS
77 iKernCpsr=aResults.iIntSpsr;
78 iKernR14=aResults.iIntR14;
81 if (aResults.iUserThreadTicks>iUserThreadTicks)
83 iUserThreadTicks=aResults.iUserThreadTicks;
84 iUserRetAddr=aResults.iIntRetAddr;
85 #ifdef __CAPTURE_EXTRAS
86 iUserCpsr=aResults.iIntSpsr;
87 iUserR14=aResults.iIntR14;
90 iSumIntTicks+=aResults.iIntTicks;
91 iSumKernTicks+=aResults.iKernThreadTicks;
92 iSumUserTicks+=aResults.iUserThreadTicks;
97 __e32_atomic_add_rel32(&UpdateCount, 1);
100 TInt LatencyThread(TAny* aStatus)
102 TRequestStatus* pS=(TRequestStatus*)aStatus;
107 TicksPerMs=l.TicksPerMs();
108 Mem::FillZ(&Latencies,sizeof(Latencies));
109 Main.RequestComplete(pS,0);
110 SLatencyResults results;
115 User::WaitForAnyRequest();
116 l.GetResults(results);
117 Latencies.Update(results);
122 void GetLatencies(SFullLatencyResults& aResults)
126 TUint32 u1 = UpdateCount;
127 __e32_memory_barrier();
129 __e32_memory_barrier();
130 TUint32 u2 = UpdateCount;
131 if (u1==u2 && !(u1&1)) // no good if it changed partway through or was changing when we started
136 _LIT(KPrefixRuntests, "RUNTESTS: RT");
137 void DisplayMaxValues(const TDesC& aPrefix)
139 SFullLatencyResults v;
141 TUint i=TimerToMicroseconds(v.iIntTicks);
142 TUint k=TimerToMicroseconds(v.iKernThreadTicks);
143 TUint u=TimerToMicroseconds(v.iUserThreadTicks);
144 TUint ia=v.iIntRetAddr;
145 TUint ka=v.iKernRetAddr;
146 TUint ua=v.iUserRetAddr;
147 test.Printf(_L("%SMAX: Int %4d %08x Kern %4d %08x User %4d %08x\n"),&aPrefix,i,ia,k,ka,u,ua);
150 void DisplayAvgValues(const TDesC& aPrefix)
152 SFullLatencyResults v;
154 TUint i=TimerToMicroseconds(I64LOW(v.iSumIntTicks/v.iCount));
155 TUint k=TimerToMicroseconds(I64LOW(v.iSumKernTicks/v.iCount));
156 TUint u=TimerToMicroseconds(I64LOW(v.iSumUserTicks/v.iCount));
157 test.Printf(_L("%SAVG: Int %4d Kern %4d User %4d Count %Ld\n"),&aPrefix,i,k,u,v.iCount);
160 #ifdef __CAPTURE_EXTRAS
161 void DisplayExtras(const TDesC& aPrefix)
163 SFullLatencyResults v;
165 test.Printf(_L("%SInt : Cpsr %08x R14 %08x\n"),&aPrefix,v.iIntCpsr,v.iIntR14);
166 test.Printf(_L("%SKern: Cpsr %08x R14 %08x\n"),&aPrefix,v.iKernCpsr,v.iKernR14);
167 test.Printf(_L("%SUser: Cpsr %08x R14 %08x\n"),&aPrefix,v.iUserCpsr,v.iUserR14);
171 void ClearMaxValues()
173 Mem::FillZ(&Latencies,6*sizeof(TUint));
176 void ClearAvgValues()
178 Mem::FillZ(&Latencies.iCount,4*sizeof(TInt64));
181 _LIT_SECURITY_POLICY_PASS(KPersistencePropReadPolicy);
182 _LIT_SECURITY_POLICY_PASS(KPersistencePropWritePolicy);
183 void AnnouncePersistence()
185 TInt r = RProperty::Define(KRuntestsIntentionalPersistenceKey, RProperty::EInt, KPersistencePropReadPolicy, KPersistencePropWritePolicy);
186 test(r==KErrNone || r==KErrAlreadyExists);
187 r = RProperty::Set(RProcess().SecureId(), KRuntestsIntentionalPersistenceKey, KRuntestsIntentionalPersistenceValue);
191 class CConsoleReader : public CActive
198 virtual void DoCancel();
200 CConsoleBase* iConsole;
203 CConsoleReader::CConsoleReader()
208 void CConsoleReader::RunL()
210 TKeyCode k = iConsole->KeyCode();
214 test.Printf(_L("Clearing Maximum Values\n"));
218 DisplayMaxValues(KNullDesC);
221 test.Printf(_L("Clearing Average Values\n"));
225 DisplayAvgValues(KNullDesC);
227 #ifdef __CAPTURE_EXTRAS
229 DisplayExtras(KNullDesC);
234 CActiveScheduler::Stop();
242 void CConsoleReader::DoCancel()
244 iConsole->ReadCancel();
247 void CConsoleReader::New()
249 CConsoleReader* crdr = new CConsoleReader;
251 crdr->iConsole = test.Console();
252 CActiveScheduler::Add(crdr);
256 void CConsoleReader::Start()
258 iConsole->Read(iStatus);
262 class CPubSubWatcher : public CActive
268 virtual ~CPubSubWatcher();
270 virtual void DoCancel();
275 CPubSubWatcher::CPubSubWatcher()
280 void CPubSubWatcher::RunL()
283 DisplayMaxValues(KPrefixRuntests);
284 DisplayAvgValues(KPrefixRuntests);
287 void CPubSubWatcher::DoCancel()
292 void CPubSubWatcher::New()
294 CPubSubWatcher* psw = new CPubSubWatcher;
296 TInt r = psw->iProperty.Attach(KRuntestsCategory, KRuntestsCurrentTestKey, EOwnerThread);
298 CActiveScheduler::Add(psw);
302 void CPubSubWatcher::Start()
304 iProperty.Subscribe(iStatus);
308 CPubSubWatcher::~CPubSubWatcher()
313 GLDEF_C TInt E32Main()
316 // Don't run automatically on debug builds
317 TUint32 creator_sid = User::CreatorSecureId();
318 if (creator_sid == TUint32(KRuntestsCategoryValue))
321 // disable anything which will interfere, e.g. plat sec diagnostics
322 User::SetDebugMask(UserSvr::DebugMask(2)|4, 2);
326 test.Printf(_L("*** Please note ***\n"));
327 test.Printf(_L("\n"));
328 test.Printf(_L("t_lat2 runs in the backgroud to measure latency while other tests are\n"));
329 test.Printf(_L("running. It should not be run as a standalone test, only as part of a\n"));
330 test.Printf(_L("test run coordinated by runtests. If run on its owm, it will simply wait\n"));
331 test.Printf(_L("forever.\n"));
332 test.Printf(_L("\n"));
334 test.Start(_L("Load LDD"));
335 TInt r=User::LoadLogicalDevice(KLatencyLddFileName);
336 test(r==KErrNone || r==KErrAlreadyExists);
338 test.Next(_L("Duplicate handle"));
339 r=Main.Duplicate(RThread());
342 test.Next(_L("Create thread"));
346 r=t.Create(KThreadName,LatencyThread,0x1000,NULL,&sc);
350 User::WaitForRequest(sx,sc);
351 if (sx!=KRequestPending)
353 if (t.ExitType()==EExitKill && t.ExitReason()==KErrAlreadyExists)
355 test.Printf(_L("T_LAT2 already running.\n"));
359 test.Printf(_L("Initialisation failed, error %d\n"),sx.Int());
364 CTrapCleanup* tcln = CTrapCleanup::New();
366 CActiveScheduler* as = new CActiveScheduler;
368 CActiveScheduler::Install(as);
369 CConsoleReader::New();
370 CPubSubWatcher::New();
371 AnnouncePersistence();
372 RProcess::Rendezvous(KErrNone);
374 CActiveScheduler::Start();
377 User::SetDebugMask(UserSvr::DebugMask(2)&~4, 2);