sl@0: // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32test\realtime\t_lat2.cpp sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "runtests.h" sl@0: #include "d_latncy.h" sl@0: sl@0: _LIT(KLatencyLddFileName,"D_LATNCY"); sl@0: _LIT(KThreadName,"LatencyThreadU"); sl@0: sl@0: RTest test(_L("Latency")); sl@0: RThread Main; sl@0: TUint TicksPerMs; sl@0: sl@0: struct SFullLatencyResults : public SLatencyResults sl@0: { sl@0: TUint iKernRetAddr; sl@0: TUint iUserRetAddr; sl@0: TInt64 iCount; sl@0: TInt64 iSumIntTicks; sl@0: TInt64 iSumKernTicks; sl@0: TInt64 iSumUserTicks; sl@0: TUint iIntCpsr; sl@0: TUint iKernCpsr; sl@0: TUint iKernR14; sl@0: TUint iUserCpsr; sl@0: TUint iUserR14; sl@0: sl@0: void Update(SLatencyResults& aResults); sl@0: }; sl@0: sl@0: SFullLatencyResults Latencies; sl@0: volatile TUint32 UpdateCount=0; sl@0: sl@0: TUint TimerToMicroseconds(TUint aTimerValue) sl@0: { sl@0: return (aTimerValue*1000+TicksPerMs-1)/TicksPerMs; sl@0: } sl@0: sl@0: void SFullLatencyResults::Update(SLatencyResults& aResults) sl@0: { sl@0: __e32_atomic_add_acq32(&UpdateCount, 1); sl@0: sl@0: // memory barrier sl@0: sl@0: if (aResults.iIntTicks>iIntTicks) sl@0: { sl@0: iIntTicks=aResults.iIntTicks; sl@0: iIntRetAddr=aResults.iIntRetAddr; sl@0: #ifdef __CAPTURE_EXTRAS sl@0: iIntCpsr=aResults.iIntSpsr; sl@0: iIntR14=aResults.iIntR14; sl@0: #endif sl@0: } sl@0: if (aResults.iKernThreadTicks>iKernThreadTicks) sl@0: { sl@0: iKernThreadTicks=aResults.iKernThreadTicks; sl@0: iKernRetAddr=aResults.iIntRetAddr; sl@0: #ifdef __CAPTURE_EXTRAS sl@0: iKernCpsr=aResults.iIntSpsr; sl@0: iKernR14=aResults.iIntR14; sl@0: #endif sl@0: } sl@0: if (aResults.iUserThreadTicks>iUserThreadTicks) sl@0: { sl@0: iUserThreadTicks=aResults.iUserThreadTicks; sl@0: iUserRetAddr=aResults.iIntRetAddr; sl@0: #ifdef __CAPTURE_EXTRAS sl@0: iUserCpsr=aResults.iIntSpsr; sl@0: iUserR14=aResults.iIntR14; sl@0: #endif sl@0: } sl@0: iSumIntTicks+=aResults.iIntTicks; sl@0: iSumKernTicks+=aResults.iKernThreadTicks; sl@0: iSumUserTicks+=aResults.iUserThreadTicks; sl@0: ++iCount; sl@0: sl@0: // memory barrier sl@0: sl@0: __e32_atomic_add_rel32(&UpdateCount, 1); sl@0: } sl@0: sl@0: TInt LatencyThread(TAny* aStatus) sl@0: { sl@0: TRequestStatus* pS=(TRequestStatus*)aStatus; sl@0: RLatency l; sl@0: TInt r=l.Open(); sl@0: if (r!=KErrNone) sl@0: return r; sl@0: TicksPerMs=l.TicksPerMs(); sl@0: Mem::FillZ(&Latencies,sizeof(Latencies)); sl@0: Main.RequestComplete(pS,0); sl@0: SLatencyResults results; sl@0: sl@0: l.Start(); sl@0: FOREVER sl@0: { sl@0: User::WaitForAnyRequest(); sl@0: l.GetResults(results); sl@0: Latencies.Update(results); sl@0: } sl@0: return r; sl@0: } sl@0: sl@0: void GetLatencies(SFullLatencyResults& aResults) sl@0: { sl@0: FOREVER sl@0: { sl@0: TUint32 u1 = UpdateCount; sl@0: __e32_memory_barrier(); sl@0: aResults=Latencies; sl@0: __e32_memory_barrier(); sl@0: TUint32 u2 = UpdateCount; sl@0: if (u1==u2 && !(u1&1)) // no good if it changed partway through or was changing when we started sl@0: break; sl@0: } sl@0: } sl@0: sl@0: _LIT(KPrefixRuntests, "RUNTESTS: RT"); sl@0: void DisplayMaxValues(const TDesC& aPrefix) sl@0: { sl@0: SFullLatencyResults v; sl@0: GetLatencies(v); sl@0: TUint i=TimerToMicroseconds(v.iIntTicks); sl@0: TUint k=TimerToMicroseconds(v.iKernThreadTicks); sl@0: TUint u=TimerToMicroseconds(v.iUserThreadTicks); sl@0: TUint ia=v.iIntRetAddr; sl@0: TUint ka=v.iKernRetAddr; sl@0: TUint ua=v.iUserRetAddr; sl@0: test.Printf(_L("%SMAX: Int %4d %08x Kern %4d %08x User %4d %08x\n"),&aPrefix,i,ia,k,ka,u,ua); sl@0: } sl@0: sl@0: void DisplayAvgValues(const TDesC& aPrefix) sl@0: { sl@0: SFullLatencyResults v; sl@0: GetLatencies(v); sl@0: TUint i=TimerToMicroseconds(I64LOW(v.iSumIntTicks/v.iCount)); sl@0: TUint k=TimerToMicroseconds(I64LOW(v.iSumKernTicks/v.iCount)); sl@0: TUint u=TimerToMicroseconds(I64LOW(v.iSumUserTicks/v.iCount)); sl@0: test.Printf(_L("%SAVG: Int %4d Kern %4d User %4d Count %Ld\n"),&aPrefix,i,k,u,v.iCount); sl@0: } sl@0: sl@0: #ifdef __CAPTURE_EXTRAS sl@0: void DisplayExtras(const TDesC& aPrefix) sl@0: { sl@0: SFullLatencyResults v; sl@0: GetLatencies(v); sl@0: test.Printf(_L("%SInt : Cpsr %08x R14 %08x\n"),&aPrefix,v.iIntCpsr,v.iIntR14); sl@0: test.Printf(_L("%SKern: Cpsr %08x R14 %08x\n"),&aPrefix,v.iKernCpsr,v.iKernR14); sl@0: test.Printf(_L("%SUser: Cpsr %08x R14 %08x\n"),&aPrefix,v.iUserCpsr,v.iUserR14); sl@0: } sl@0: #endif sl@0: sl@0: void ClearMaxValues() sl@0: { sl@0: Mem::FillZ(&Latencies,6*sizeof(TUint)); sl@0: } sl@0: sl@0: void ClearAvgValues() sl@0: { sl@0: Mem::FillZ(&Latencies.iCount,4*sizeof(TInt64)); sl@0: } sl@0: sl@0: _LIT_SECURITY_POLICY_PASS(KPersistencePropReadPolicy); sl@0: _LIT_SECURITY_POLICY_PASS(KPersistencePropWritePolicy); sl@0: void AnnouncePersistence() sl@0: { sl@0: TInt r = RProperty::Define(KRuntestsIntentionalPersistenceKey, RProperty::EInt, KPersistencePropReadPolicy, KPersistencePropWritePolicy); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: r = RProperty::Set(RProcess().SecureId(), KRuntestsIntentionalPersistenceKey, KRuntestsIntentionalPersistenceValue); sl@0: test(r==KErrNone); sl@0: } sl@0: sl@0: class CConsoleReader : public CActive sl@0: { sl@0: public: sl@0: CConsoleReader(); sl@0: static void New(); sl@0: void Start(); sl@0: virtual void RunL(); sl@0: virtual void DoCancel(); sl@0: public: sl@0: CConsoleBase* iConsole; sl@0: }; sl@0: sl@0: CConsoleReader::CConsoleReader() sl@0: : CActive(0) sl@0: { sl@0: } sl@0: sl@0: void CConsoleReader::RunL() sl@0: { sl@0: TKeyCode k = iConsole->KeyCode(); sl@0: switch(k) sl@0: { sl@0: case '1': sl@0: test.Printf(_L("Clearing Maximum Values\n")); sl@0: ClearMaxValues(); sl@0: break; sl@0: case '2': sl@0: DisplayMaxValues(KNullDesC); sl@0: break; sl@0: case '3': sl@0: test.Printf(_L("Clearing Average Values\n")); sl@0: ClearAvgValues(); sl@0: break; sl@0: case '4': sl@0: DisplayAvgValues(KNullDesC); sl@0: break; sl@0: #ifdef __CAPTURE_EXTRAS sl@0: case '5': sl@0: DisplayExtras(KNullDesC); sl@0: break; sl@0: #endif sl@0: case 'x': sl@0: case 'X': sl@0: CActiveScheduler::Stop(); sl@0: return; sl@0: default: sl@0: break; sl@0: } sl@0: Start(); sl@0: } sl@0: sl@0: void CConsoleReader::DoCancel() sl@0: { sl@0: iConsole->ReadCancel(); sl@0: } sl@0: sl@0: void CConsoleReader::New() sl@0: { sl@0: CConsoleReader* crdr = new CConsoleReader; sl@0: test(crdr != NULL); sl@0: crdr->iConsole = test.Console(); sl@0: CActiveScheduler::Add(crdr); sl@0: crdr->Start(); sl@0: } sl@0: sl@0: void CConsoleReader::Start() sl@0: { sl@0: iConsole->Read(iStatus); sl@0: SetActive(); sl@0: } sl@0: sl@0: class CPubSubWatcher : public CActive sl@0: { sl@0: public: sl@0: CPubSubWatcher(); sl@0: static void New(); sl@0: void Start(); sl@0: virtual ~CPubSubWatcher(); sl@0: virtual void RunL(); sl@0: virtual void DoCancel(); sl@0: public: sl@0: RProperty iProperty; sl@0: }; sl@0: sl@0: CPubSubWatcher::CPubSubWatcher() sl@0: : CActive(0) sl@0: { sl@0: } sl@0: sl@0: void CPubSubWatcher::RunL() sl@0: { sl@0: Start(); sl@0: DisplayMaxValues(KPrefixRuntests); sl@0: DisplayAvgValues(KPrefixRuntests); sl@0: } sl@0: sl@0: void CPubSubWatcher::DoCancel() sl@0: { sl@0: iProperty.Cancel(); sl@0: } sl@0: sl@0: void CPubSubWatcher::New() sl@0: { sl@0: CPubSubWatcher* psw = new CPubSubWatcher; sl@0: test(psw != NULL); sl@0: TInt r = psw->iProperty.Attach(KRuntestsCategory, KRuntestsCurrentTestKey, EOwnerThread); sl@0: test(r==KErrNone); sl@0: CActiveScheduler::Add(psw); sl@0: psw->Start(); sl@0: } sl@0: sl@0: void CPubSubWatcher::Start() sl@0: { sl@0: iProperty.Subscribe(iStatus); sl@0: SetActive(); sl@0: } sl@0: sl@0: CPubSubWatcher::~CPubSubWatcher() sl@0: { sl@0: iProperty.Close(); sl@0: } sl@0: sl@0: GLDEF_C TInt E32Main() sl@0: { sl@0: #ifdef _DEBUG sl@0: // Don't run automatically on debug builds sl@0: TUint32 creator_sid = User::CreatorSecureId(); sl@0: if (creator_sid == TUint32(KRuntestsCategoryValue)) sl@0: return KErrNone; sl@0: #endif sl@0: // disable anything which will interfere, e.g. plat sec diagnostics sl@0: User::SetDebugMask(UserSvr::DebugMask(2)|4, 2); sl@0: sl@0: test.Title(); sl@0: sl@0: test.Printf(_L("*** Please note ***\n")); sl@0: test.Printf(_L("\n")); sl@0: test.Printf(_L("t_lat2 runs in the backgroud to measure latency while other tests are\n")); sl@0: test.Printf(_L("running. It should not be run as a standalone test, only as part of a\n")); sl@0: test.Printf(_L("test run coordinated by runtests. If run on its owm, it will simply wait\n")); sl@0: test.Printf(_L("forever.\n")); sl@0: test.Printf(_L("\n")); sl@0: sl@0: test.Start(_L("Load LDD")); sl@0: TInt r=User::LoadLogicalDevice(KLatencyLddFileName); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: sl@0: test.Next(_L("Duplicate handle")); sl@0: r=Main.Duplicate(RThread()); sl@0: test(r==KErrNone); sl@0: sl@0: test.Next(_L("Create thread")); sl@0: RThread t; sl@0: TRequestStatus sx; sl@0: TRequestStatus sc; sl@0: r=t.Create(KThreadName,LatencyThread,0x1000,NULL,&sc); sl@0: test(r==KErrNone); sl@0: t.Logon(sx); sl@0: t.Resume(); sl@0: User::WaitForRequest(sx,sc); sl@0: if (sx!=KRequestPending) sl@0: { sl@0: if (t.ExitType()==EExitKill && t.ExitReason()==KErrAlreadyExists) sl@0: { sl@0: test.Printf(_L("T_LAT2 already running.\n")); sl@0: test.End(); sl@0: return 0; sl@0: } sl@0: test.Printf(_L("Initialisation failed, error %d\n"),sx.Int()); sl@0: test(0); sl@0: } sl@0: test(sc==KErrNone); sl@0: sl@0: CTrapCleanup* tcln = CTrapCleanup::New(); sl@0: test(tcln != NULL); sl@0: CActiveScheduler* as = new CActiveScheduler; sl@0: test(as != NULL); sl@0: CActiveScheduler::Install(as); sl@0: CConsoleReader::New(); sl@0: CPubSubWatcher::New(); sl@0: AnnouncePersistence(); sl@0: RProcess::Rendezvous(KErrNone); sl@0: sl@0: CActiveScheduler::Start(); sl@0: sl@0: // latency test over sl@0: User::SetDebugMask(UserSvr::DebugMask(2)&~4, 2); sl@0: sl@0: test.End(); sl@0: return 0; sl@0: }