os/kernelhwsrv/kerneltest/e32test/realtime/t_lat2.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32test\realtime\t_lat2.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <e32test.h>
sl@0
    19
#include <e32svr.h>
sl@0
    20
#include <e32property.h>
sl@0
    21
#include <e32atomics.h>
sl@0
    22
#include "runtests.h"
sl@0
    23
#include "d_latncy.h"
sl@0
    24
sl@0
    25
_LIT(KLatencyLddFileName,"D_LATNCY");
sl@0
    26
_LIT(KThreadName,"LatencyThreadU");
sl@0
    27
sl@0
    28
RTest test(_L("Latency"));
sl@0
    29
RThread Main;
sl@0
    30
TUint TicksPerMs;
sl@0
    31
sl@0
    32
struct SFullLatencyResults : public SLatencyResults
sl@0
    33
	{
sl@0
    34
	TUint iKernRetAddr;
sl@0
    35
	TUint iUserRetAddr;
sl@0
    36
	TInt64 iCount;
sl@0
    37
	TInt64 iSumIntTicks;
sl@0
    38
	TInt64 iSumKernTicks;
sl@0
    39
	TInt64 iSumUserTicks;
sl@0
    40
	TUint iIntCpsr;
sl@0
    41
	TUint iKernCpsr;
sl@0
    42
	TUint iKernR14;
sl@0
    43
	TUint iUserCpsr;
sl@0
    44
	TUint iUserR14;
sl@0
    45
sl@0
    46
	void Update(SLatencyResults& aResults);
sl@0
    47
	};
sl@0
    48
sl@0
    49
SFullLatencyResults Latencies;
sl@0
    50
volatile TUint32 UpdateCount=0;
sl@0
    51
sl@0
    52
TUint TimerToMicroseconds(TUint aTimerValue)
sl@0
    53
	{
sl@0
    54
	return (aTimerValue*1000+TicksPerMs-1)/TicksPerMs;
sl@0
    55
	}
sl@0
    56
sl@0
    57
void SFullLatencyResults::Update(SLatencyResults& aResults)
sl@0
    58
	{
sl@0
    59
	__e32_atomic_add_acq32(&UpdateCount, 1);
sl@0
    60
sl@0
    61
	// memory barrier
sl@0
    62
sl@0
    63
	if (aResults.iIntTicks>iIntTicks)
sl@0
    64
		{
sl@0
    65
		iIntTicks=aResults.iIntTicks;
sl@0
    66
		iIntRetAddr=aResults.iIntRetAddr;
sl@0
    67
#ifdef __CAPTURE_EXTRAS
sl@0
    68
		iIntCpsr=aResults.iIntSpsr;
sl@0
    69
		iIntR14=aResults.iIntR14;
sl@0
    70
#endif
sl@0
    71
		}
sl@0
    72
	if (aResults.iKernThreadTicks>iKernThreadTicks)
sl@0
    73
		{
sl@0
    74
		iKernThreadTicks=aResults.iKernThreadTicks;
sl@0
    75
		iKernRetAddr=aResults.iIntRetAddr;
sl@0
    76
#ifdef __CAPTURE_EXTRAS
sl@0
    77
		iKernCpsr=aResults.iIntSpsr;
sl@0
    78
		iKernR14=aResults.iIntR14;
sl@0
    79
#endif
sl@0
    80
		}
sl@0
    81
	if (aResults.iUserThreadTicks>iUserThreadTicks)
sl@0
    82
		{
sl@0
    83
		iUserThreadTicks=aResults.iUserThreadTicks;
sl@0
    84
		iUserRetAddr=aResults.iIntRetAddr;
sl@0
    85
#ifdef __CAPTURE_EXTRAS
sl@0
    86
		iUserCpsr=aResults.iIntSpsr;
sl@0
    87
		iUserR14=aResults.iIntR14;
sl@0
    88
#endif
sl@0
    89
		}
sl@0
    90
	iSumIntTicks+=aResults.iIntTicks;
sl@0
    91
	iSumKernTicks+=aResults.iKernThreadTicks;
sl@0
    92
	iSumUserTicks+=aResults.iUserThreadTicks;
sl@0
    93
	++iCount;
sl@0
    94
sl@0
    95
	// memory barrier
sl@0
    96
sl@0
    97
	__e32_atomic_add_rel32(&UpdateCount, 1);
sl@0
    98
	}
sl@0
    99
sl@0
   100
TInt LatencyThread(TAny* aStatus)
sl@0
   101
	{
sl@0
   102
	TRequestStatus* pS=(TRequestStatus*)aStatus;
sl@0
   103
	RLatency l;
sl@0
   104
	TInt r=l.Open();
sl@0
   105
	if (r!=KErrNone)
sl@0
   106
		return r;
sl@0
   107
	TicksPerMs=l.TicksPerMs();
sl@0
   108
	Mem::FillZ(&Latencies,sizeof(Latencies));
sl@0
   109
	Main.RequestComplete(pS,0);
sl@0
   110
	SLatencyResults results;
sl@0
   111
sl@0
   112
	l.Start();
sl@0
   113
	FOREVER
sl@0
   114
		{
sl@0
   115
		User::WaitForAnyRequest();
sl@0
   116
		l.GetResults(results);
sl@0
   117
		Latencies.Update(results);
sl@0
   118
		}
sl@0
   119
	return r;
sl@0
   120
	}
sl@0
   121
sl@0
   122
void GetLatencies(SFullLatencyResults& aResults)
sl@0
   123
	{
sl@0
   124
	FOREVER
sl@0
   125
		{
sl@0
   126
		TUint32 u1 = UpdateCount;
sl@0
   127
		__e32_memory_barrier();
sl@0
   128
		aResults=Latencies;
sl@0
   129
		__e32_memory_barrier();
sl@0
   130
		TUint32 u2 = UpdateCount;
sl@0
   131
		if (u1==u2 && !(u1&1))	// no good if it changed partway through or was changing when we started
sl@0
   132
			break;
sl@0
   133
		}
sl@0
   134
	}
sl@0
   135
sl@0
   136
_LIT(KPrefixRuntests, "RUNTESTS: RT");
sl@0
   137
void DisplayMaxValues(const TDesC& aPrefix)
sl@0
   138
	{
sl@0
   139
	SFullLatencyResults v;
sl@0
   140
	GetLatencies(v);
sl@0
   141
	TUint i=TimerToMicroseconds(v.iIntTicks);
sl@0
   142
	TUint k=TimerToMicroseconds(v.iKernThreadTicks);
sl@0
   143
	TUint u=TimerToMicroseconds(v.iUserThreadTicks);
sl@0
   144
	TUint ia=v.iIntRetAddr;
sl@0
   145
	TUint ka=v.iKernRetAddr;
sl@0
   146
	TUint ua=v.iUserRetAddr;
sl@0
   147
	test.Printf(_L("%SMAX: Int %4d %08x Kern %4d %08x User %4d %08x\n"),&aPrefix,i,ia,k,ka,u,ua);
sl@0
   148
	}
sl@0
   149
sl@0
   150
void DisplayAvgValues(const TDesC& aPrefix)
sl@0
   151
	{
sl@0
   152
	SFullLatencyResults v;
sl@0
   153
	GetLatencies(v);
sl@0
   154
	TUint i=TimerToMicroseconds(I64LOW(v.iSumIntTicks/v.iCount));
sl@0
   155
	TUint k=TimerToMicroseconds(I64LOW(v.iSumKernTicks/v.iCount));
sl@0
   156
	TUint u=TimerToMicroseconds(I64LOW(v.iSumUserTicks/v.iCount));
sl@0
   157
	test.Printf(_L("%SAVG: Int %4d Kern %4d User %4d Count %Ld\n"),&aPrefix,i,k,u,v.iCount);
sl@0
   158
	}
sl@0
   159
sl@0
   160
#ifdef __CAPTURE_EXTRAS
sl@0
   161
void DisplayExtras(const TDesC& aPrefix)
sl@0
   162
	{
sl@0
   163
	SFullLatencyResults v;
sl@0
   164
	GetLatencies(v);
sl@0
   165
	test.Printf(_L("%SInt : Cpsr %08x R14 %08x\n"),&aPrefix,v.iIntCpsr,v.iIntR14);
sl@0
   166
	test.Printf(_L("%SKern: Cpsr %08x R14 %08x\n"),&aPrefix,v.iKernCpsr,v.iKernR14);
sl@0
   167
	test.Printf(_L("%SUser: Cpsr %08x R14 %08x\n"),&aPrefix,v.iUserCpsr,v.iUserR14);
sl@0
   168
	}
sl@0
   169
#endif
sl@0
   170
sl@0
   171
void ClearMaxValues()
sl@0
   172
	{
sl@0
   173
	Mem::FillZ(&Latencies,6*sizeof(TUint));
sl@0
   174
	}
sl@0
   175
sl@0
   176
void ClearAvgValues()
sl@0
   177
	{
sl@0
   178
	Mem::FillZ(&Latencies.iCount,4*sizeof(TInt64));
sl@0
   179
	}
sl@0
   180
sl@0
   181
_LIT_SECURITY_POLICY_PASS(KPersistencePropReadPolicy);
sl@0
   182
_LIT_SECURITY_POLICY_PASS(KPersistencePropWritePolicy);
sl@0
   183
void AnnouncePersistence()
sl@0
   184
	{
sl@0
   185
	TInt r = RProperty::Define(KRuntestsIntentionalPersistenceKey, RProperty::EInt, KPersistencePropReadPolicy, KPersistencePropWritePolicy);
sl@0
   186
	test(r==KErrNone || r==KErrAlreadyExists);
sl@0
   187
	r = RProperty::Set(RProcess().SecureId(), KRuntestsIntentionalPersistenceKey, KRuntestsIntentionalPersistenceValue);
sl@0
   188
	test(r==KErrNone);
sl@0
   189
	}
sl@0
   190
sl@0
   191
class CConsoleReader : public CActive
sl@0
   192
	{
sl@0
   193
public:
sl@0
   194
	CConsoleReader();
sl@0
   195
	static void New();
sl@0
   196
	void Start();
sl@0
   197
	virtual void RunL();
sl@0
   198
	virtual void DoCancel();
sl@0
   199
public:
sl@0
   200
	CConsoleBase* iConsole;
sl@0
   201
	};
sl@0
   202
sl@0
   203
CConsoleReader::CConsoleReader()
sl@0
   204
	:	CActive(0)
sl@0
   205
	{
sl@0
   206
	}
sl@0
   207
sl@0
   208
void CConsoleReader::RunL()
sl@0
   209
	{
sl@0
   210
	TKeyCode k = iConsole->KeyCode();
sl@0
   211
	switch(k)
sl@0
   212
		{
sl@0
   213
		case '1':
sl@0
   214
			test.Printf(_L("Clearing Maximum Values\n"));
sl@0
   215
			ClearMaxValues();
sl@0
   216
			break;
sl@0
   217
		case '2':
sl@0
   218
			DisplayMaxValues(KNullDesC);
sl@0
   219
			break;
sl@0
   220
		case '3':
sl@0
   221
			test.Printf(_L("Clearing Average Values\n"));
sl@0
   222
			ClearAvgValues();
sl@0
   223
			break;
sl@0
   224
		case '4':
sl@0
   225
			DisplayAvgValues(KNullDesC);
sl@0
   226
			break;
sl@0
   227
#ifdef __CAPTURE_EXTRAS
sl@0
   228
		case '5':
sl@0
   229
			DisplayExtras(KNullDesC);
sl@0
   230
			break;
sl@0
   231
#endif
sl@0
   232
		case 'x':
sl@0
   233
		case 'X':
sl@0
   234
			CActiveScheduler::Stop();
sl@0
   235
			return;
sl@0
   236
		default:
sl@0
   237
			break;
sl@0
   238
		}
sl@0
   239
	Start();
sl@0
   240
	}
sl@0
   241
sl@0
   242
void CConsoleReader::DoCancel()
sl@0
   243
	{
sl@0
   244
	iConsole->ReadCancel();
sl@0
   245
	}
sl@0
   246
sl@0
   247
void CConsoleReader::New()
sl@0
   248
	{
sl@0
   249
	CConsoleReader* crdr = new CConsoleReader;
sl@0
   250
	test(crdr != NULL);
sl@0
   251
	crdr->iConsole = test.Console();
sl@0
   252
	CActiveScheduler::Add(crdr);
sl@0
   253
	crdr->Start();
sl@0
   254
	}
sl@0
   255
sl@0
   256
void CConsoleReader::Start()
sl@0
   257
	{
sl@0
   258
	iConsole->Read(iStatus);
sl@0
   259
	SetActive();
sl@0
   260
	}
sl@0
   261
sl@0
   262
class CPubSubWatcher : public CActive
sl@0
   263
	{
sl@0
   264
public:
sl@0
   265
	CPubSubWatcher();
sl@0
   266
	static void New();
sl@0
   267
	void Start();
sl@0
   268
	virtual ~CPubSubWatcher();
sl@0
   269
	virtual void RunL();
sl@0
   270
	virtual void DoCancel();
sl@0
   271
public:
sl@0
   272
	RProperty iProperty;
sl@0
   273
	};
sl@0
   274
sl@0
   275
CPubSubWatcher::CPubSubWatcher()
sl@0
   276
	:	CActive(0)
sl@0
   277
	{
sl@0
   278
	}
sl@0
   279
sl@0
   280
void CPubSubWatcher::RunL()
sl@0
   281
	{
sl@0
   282
	Start();
sl@0
   283
	DisplayMaxValues(KPrefixRuntests);
sl@0
   284
	DisplayAvgValues(KPrefixRuntests);
sl@0
   285
	}
sl@0
   286
sl@0
   287
void CPubSubWatcher::DoCancel()
sl@0
   288
	{
sl@0
   289
	iProperty.Cancel();
sl@0
   290
	}
sl@0
   291
sl@0
   292
void CPubSubWatcher::New()
sl@0
   293
	{
sl@0
   294
	CPubSubWatcher* psw = new CPubSubWatcher;
sl@0
   295
	test(psw != NULL);
sl@0
   296
	TInt r = psw->iProperty.Attach(KRuntestsCategory, KRuntestsCurrentTestKey, EOwnerThread);
sl@0
   297
	test(r==KErrNone);
sl@0
   298
	CActiveScheduler::Add(psw);
sl@0
   299
	psw->Start();
sl@0
   300
	}
sl@0
   301
sl@0
   302
void CPubSubWatcher::Start()
sl@0
   303
	{
sl@0
   304
	iProperty.Subscribe(iStatus);
sl@0
   305
	SetActive();
sl@0
   306
	}
sl@0
   307
sl@0
   308
CPubSubWatcher::~CPubSubWatcher()
sl@0
   309
	{
sl@0
   310
	iProperty.Close();
sl@0
   311
	}
sl@0
   312
sl@0
   313
GLDEF_C TInt E32Main()
sl@0
   314
	{
sl@0
   315
#ifdef _DEBUG
sl@0
   316
	// Don't run automatically on debug builds
sl@0
   317
	TUint32 creator_sid = User::CreatorSecureId();
sl@0
   318
	if (creator_sid == TUint32(KRuntestsCategoryValue))
sl@0
   319
		return KErrNone;
sl@0
   320
#endif
sl@0
   321
	// disable anything which will interfere, e.g. plat sec diagnostics
sl@0
   322
	User::SetDebugMask(UserSvr::DebugMask(2)|4, 2);
sl@0
   323
sl@0
   324
	test.Title();
sl@0
   325
	
sl@0
   326
	test.Printf(_L("*** Please note ***\n"));
sl@0
   327
	test.Printf(_L("\n"));
sl@0
   328
	test.Printf(_L("t_lat2 runs in the backgroud to measure latency while other tests are\n"));
sl@0
   329
	test.Printf(_L("running.  It should not be run as a standalone test, only as part of a\n"));
sl@0
   330
	test.Printf(_L("test run coordinated by runtests.  If run on its owm, it will simply wait\n"));
sl@0
   331
	test.Printf(_L("forever.\n"));
sl@0
   332
	test.Printf(_L("\n"));
sl@0
   333
	
sl@0
   334
	test.Start(_L("Load LDD"));
sl@0
   335
	TInt r=User::LoadLogicalDevice(KLatencyLddFileName);
sl@0
   336
	test(r==KErrNone || r==KErrAlreadyExists);
sl@0
   337
sl@0
   338
	test.Next(_L("Duplicate handle"));
sl@0
   339
	r=Main.Duplicate(RThread());
sl@0
   340
	test(r==KErrNone);
sl@0
   341
sl@0
   342
	test.Next(_L("Create thread"));
sl@0
   343
	RThread t;
sl@0
   344
	TRequestStatus sx;
sl@0
   345
	TRequestStatus sc;
sl@0
   346
	r=t.Create(KThreadName,LatencyThread,0x1000,NULL,&sc);
sl@0
   347
	test(r==KErrNone);
sl@0
   348
	t.Logon(sx);
sl@0
   349
	t.Resume();
sl@0
   350
	User::WaitForRequest(sx,sc);
sl@0
   351
	if (sx!=KRequestPending)
sl@0
   352
		{
sl@0
   353
		if (t.ExitType()==EExitKill && t.ExitReason()==KErrAlreadyExists)
sl@0
   354
			{
sl@0
   355
			test.Printf(_L("T_LAT2 already running.\n"));
sl@0
   356
			test.End();
sl@0
   357
			return 0;
sl@0
   358
			}
sl@0
   359
		test.Printf(_L("Initialisation failed, error %d\n"),sx.Int());
sl@0
   360
		test(0);
sl@0
   361
		}
sl@0
   362
	test(sc==KErrNone);
sl@0
   363
sl@0
   364
	CTrapCleanup* tcln = CTrapCleanup::New();
sl@0
   365
	test(tcln != NULL);
sl@0
   366
	CActiveScheduler* as = new CActiveScheduler;
sl@0
   367
	test(as != NULL);
sl@0
   368
	CActiveScheduler::Install(as);
sl@0
   369
	CConsoleReader::New();
sl@0
   370
	CPubSubWatcher::New();
sl@0
   371
	AnnouncePersistence();
sl@0
   372
	RProcess::Rendezvous(KErrNone);
sl@0
   373
sl@0
   374
	CActiveScheduler::Start();
sl@0
   375
sl@0
   376
	// latency test over
sl@0
   377
	User::SetDebugMask(UserSvr::DebugMask(2)&~4, 2);
sl@0
   378
sl@0
   379
	test.End();
sl@0
   380
	return 0;
sl@0
   381
	}