os/kernelhwsrv/kerneltest/e32test/misc/t_stres1.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) 1996-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\misc\t_stres1.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <e32test.h>
sl@0
    19
#include "u32std.h"
sl@0
    20
#include <e32atomics.h>
sl@0
    21
#include "../misc/prbs.h"
sl@0
    22
sl@0
    23
RTest test(_L("T_STRES1"));
sl@0
    24
sl@0
    25
_LIT(KLibraryFileName,"STRES1");
sl@0
    26
sl@0
    27
LOCAL_D RMutex Mutex;
sl@0
    28
LOCAL_D RSemaphore Semaphore;
sl@0
    29
LOCAL_D RChunk Chunk;
sl@0
    30
LOCAL_D RLibrary Library;
sl@0
    31
sl@0
    32
LOCAL_D TBool CheckerHasRun=EFalse;
sl@0
    33
LOCAL_D TInt TotalChecks=0;
sl@0
    34
LOCAL_D TInt CheckerBumped=0;
sl@0
    35
LOCAL_D TInt HeapUserIterations=0;
sl@0
    36
LOCAL_D TInt CreatorIterations=0;
sl@0
    37
LOCAL_D RSemaphore KillerSem;
sl@0
    38
sl@0
    39
LOCAL_C TInt HeapChecker(TAny*)
sl@0
    40
	{
sl@0
    41
	TInt n=0;
sl@0
    42
	FOREVER
sl@0
    43
		{
sl@0
    44
		User::Allocator().Check();
sl@0
    45
		TotalChecks++;
sl@0
    46
		CheckerHasRun=ETrue;
sl@0
    47
		if (++n==4)
sl@0
    48
			{
sl@0
    49
			n=0;
sl@0
    50
			User::CompressAllHeaps();
sl@0
    51
			}
sl@0
    52
		RThread().SetPriority(EPriorityMuchLess);
sl@0
    53
		}
sl@0
    54
	}
sl@0
    55
sl@0
    56
LOCAL_C TAny* HeapAlloc(TUint* aSeed)
sl@0
    57
	{
sl@0
    58
	TInt size=(Random(aSeed)&0x3ffc)+8;
sl@0
    59
	TUint32* p=(TUint32*)User::Alloc(size);
sl@0
    60
	if (p)
sl@0
    61
		{
sl@0
    62
		size=User::Allocator().AllocLen(p);
sl@0
    63
		p[0]=aSeed[0];
sl@0
    64
		p[1]=aSeed[1];
sl@0
    65
		TInt i;
sl@0
    66
		for (i=2; i<(size>>2); i++)
sl@0
    67
			p[i]=Random(aSeed);
sl@0
    68
		}
sl@0
    69
	return p;
sl@0
    70
	}
sl@0
    71
sl@0
    72
LOCAL_C void HeapFree(TAny* aPtr)
sl@0
    73
	{
sl@0
    74
	if (!aPtr)
sl@0
    75
		return;
sl@0
    76
	TInt size=User::Allocator().AllocLen(aPtr);
sl@0
    77
	TUint32* p=(TUint32*)aPtr;
sl@0
    78
	TUint seed[2];
sl@0
    79
	seed[0]=p[0];
sl@0
    80
	seed[1]=p[1];
sl@0
    81
	TInt i;
sl@0
    82
	for (i=2; i<(size>>2); i++)
sl@0
    83
		{
sl@0
    84
		if (p[i]!=Random(seed))
sl@0
    85
			User::Panic(_L("VERIFY"),i);
sl@0
    86
		}
sl@0
    87
	User::Free(p);
sl@0
    88
	}
sl@0
    89
sl@0
    90
LOCAL_C TInt HeapUser(TAny* aSeed)
sl@0
    91
	{
sl@0
    92
	TUint seed[2];
sl@0
    93
	seed[0]=(TUint)aSeed;
sl@0
    94
	seed[1]=0;
sl@0
    95
	FOREVER
sl@0
    96
		{
sl@0
    97
		TAny* p0=HeapAlloc(seed);
sl@0
    98
		TAny* p1=HeapAlloc(seed);
sl@0
    99
		TAny* p2=HeapAlloc(seed);
sl@0
   100
		HeapFree(p1);
sl@0
   101
		TAny* p3=HeapAlloc(seed);
sl@0
   102
		TAny* p4=HeapAlloc(seed);
sl@0
   103
		HeapFree(p4);
sl@0
   104
		HeapFree(p0);
sl@0
   105
		TAny* p5=HeapAlloc(seed);
sl@0
   106
		HeapFree(p2);
sl@0
   107
		TAny* p6=HeapAlloc(seed);
sl@0
   108
		TAny* p7=HeapAlloc(seed);
sl@0
   109
		HeapFree(p3);
sl@0
   110
		HeapFree(p7);
sl@0
   111
		HeapFree(p5);
sl@0
   112
		HeapFree(p6);
sl@0
   113
		__e32_atomic_add_ord32(&HeapUserIterations, 1);
sl@0
   114
		TInt ms=Random(seed)&63;
sl@0
   115
		TTimeIntervalMicroSeconds32 wait(1000*ms);
sl@0
   116
		User::AfterHighRes(wait);
sl@0
   117
		}
sl@0
   118
	}
sl@0
   119
sl@0
   120
LOCAL_C TInt KernelObjectCreator(TAny* aSeed)
sl@0
   121
	{
sl@0
   122
	TUint seed[2];
sl@0
   123
	seed[0]=(TUint)aSeed;
sl@0
   124
	seed[1]=0;
sl@0
   125
	FOREVER
sl@0
   126
		{
sl@0
   127
		TInt rm=Mutex.CreateLocal(EOwnerThread);
sl@0
   128
		TInt rs=Semaphore.CreateLocal(0,EOwnerThread);
sl@0
   129
		TInt rc=Chunk.CreateLocal(0x10000,0x100000,EOwnerThread);
sl@0
   130
		TInt rl=Library.Load(KLibraryFileName);
sl@0
   131
		if (rm==KErrNone)
sl@0
   132
			{
sl@0
   133
			Mutex.Close();
sl@0
   134
			}
sl@0
   135
		if (rs==KErrNone)
sl@0
   136
			{
sl@0
   137
			Semaphore.Close();
sl@0
   138
			}
sl@0
   139
		if (rc==KErrNone)
sl@0
   140
			{
sl@0
   141
			Chunk.Close();
sl@0
   142
			}
sl@0
   143
		if (rl==KErrNone)
sl@0
   144
			{
sl@0
   145
			Library.Close();
sl@0
   146
			}
sl@0
   147
		CreatorIterations++;
sl@0
   148
		TInt ms=Random(seed)&63;
sl@0
   149
		TTimeIntervalMicroSeconds32 wait(1000*ms);
sl@0
   150
		User::AfterHighRes(wait);
sl@0
   151
		}
sl@0
   152
	}
sl@0
   153
sl@0
   154
LOCAL_C TInt KillerThread(TAny* aSeed)
sl@0
   155
	{
sl@0
   156
	TUint seed[2];
sl@0
   157
	seed[0]=(TUint)aSeed;
sl@0
   158
	seed[1]=0;
sl@0
   159
	FOREVER
sl@0
   160
		{
sl@0
   161
		KillerSem.Wait();
sl@0
   162
		TInt ms=Random(seed)&127;
sl@0
   163
		ms+=32;
sl@0
   164
		TTimeIntervalMicroSeconds32 wait(1000*ms);
sl@0
   165
		User::AfterHighRes(wait);
sl@0
   166
		RThread t;
sl@0
   167
		TFindThread ft(_L("*Creator"));
sl@0
   168
		TFullName fn;
sl@0
   169
		TInt r=ft.Next(fn);
sl@0
   170
		if (r!=KErrNone)
sl@0
   171
			User::Panic(_L("FindErr"),r);
sl@0
   172
		r=t.Open(ft);
sl@0
   173
		if (r!=KErrNone)
sl@0
   174
			User::Panic(_L("OpenErr"),r);
sl@0
   175
		t.Kill(1);
sl@0
   176
		t.Close();
sl@0
   177
		}
sl@0
   178
	}
sl@0
   179
sl@0
   180
LOCAL_C TInt StartThread(const TDesC& aName, TThreadFunction aFunction, TAny* aPtr, TThreadPriority aPriority, RThread* aThread)
sl@0
   181
	{
sl@0
   182
	RThread t;
sl@0
   183
	TInt r=t.Create(aName,aFunction,0x1000,NULL,aPtr);
sl@0
   184
	if (r!=KErrNone)
sl@0
   185
		return r;
sl@0
   186
	t.SetPriority(aPriority);
sl@0
   187
	t.Resume();
sl@0
   188
	if (aThread)
sl@0
   189
		aThread->SetHandle(t.Handle());
sl@0
   190
	else
sl@0
   191
		t.Close();
sl@0
   192
	return r;
sl@0
   193
	}
sl@0
   194
sl@0
   195
LOCAL_C void Initialise(RThread& aChecker)
sl@0
   196
	{
sl@0
   197
	TInt r=StartThread(_L("HeapChecker"),HeapChecker,NULL,EPriorityMuchLess,&aChecker);
sl@0
   198
	test(r==KErrNone);
sl@0
   199
	r=StartThread(_L("HeapUser1"),HeapUser,(TAny*)0xb504f333,EPriorityNormal,NULL);
sl@0
   200
	test(r==KErrNone);
sl@0
   201
	r=StartThread(_L("HeapUser2"),HeapUser,(TAny*)0xddb3d743,EPriorityNormal,NULL);
sl@0
   202
	test(r==KErrNone);
sl@0
   203
	r=StartThread(_L("Creator"),KernelObjectCreator,(TAny*)0xadf85458,EPriorityNormal,NULL);
sl@0
   204
	test(r==KErrNone);
sl@0
   205
	r=KillerSem.CreateLocal(1);
sl@0
   206
	test(r==KErrNone);
sl@0
   207
	r=StartThread(_L("Killer"),KillerThread,(TAny*)0xb17217f8,EPriorityMore,NULL);
sl@0
   208
	test(r==KErrNone);
sl@0
   209
	}
sl@0
   210
sl@0
   211
LOCAL_C void Restart(const TDesC&)
sl@0
   212
	{
sl@0
   213
	FOREVER
sl@0
   214
		{
sl@0
   215
		TInt r=StartThread(_L("Creator"),KernelObjectCreator,(TAny*)0xadf85458,EPriorityNormal,NULL);
sl@0
   216
		if (r==KErrNone)
sl@0
   217
			break;
sl@0
   218
		User::After(15000);
sl@0
   219
		}
sl@0
   220
	KillerSem.Signal();
sl@0
   221
	}
sl@0
   222
sl@0
   223
GLDEF_C TInt E32Main()
sl@0
   224
	{
sl@0
   225
	test.Title();
sl@0
   226
sl@0
   227
	RThread().SetPriority(EPriorityMuchMore);
sl@0
   228
	RThread checker;
sl@0
   229
sl@0
   230
	test.Start(_L("Initialise"));
sl@0
   231
	Initialise(checker);
sl@0
   232
sl@0
   233
	test.Next(_L("Create undertaker"));
sl@0
   234
	RUndertaker u;
sl@0
   235
	TInt r=u.Create();
sl@0
   236
	test(r==KErrNone);
sl@0
   237
	test.Next(_L("Create timer"));
sl@0
   238
	RTimer timer;
sl@0
   239
	r=timer.CreateLocal();
sl@0
   240
	test(r==KErrNone);
sl@0
   241
	TInt tick=0;
sl@0
   242
	TRequestStatus su;
sl@0
   243
	TRequestStatus st;
sl@0
   244
	TInt h=0;
sl@0
   245
	test.Next(_L("Logon to undertaker"));
sl@0
   246
	u.Logon(su,h);
sl@0
   247
	test.Next(_L("Start timer"));
sl@0
   248
	timer.After(st,500000);
sl@0
   249
	FOREVER
sl@0
   250
		{
sl@0
   251
		User::WaitForRequest(su,st);
sl@0
   252
		if (su!=KRequestPending)
sl@0
   253
			{
sl@0
   254
			RThread t;
sl@0
   255
			t.SetHandle(h);
sl@0
   256
			TName n=t.Name();
sl@0
   257
			TExitType exitType=t.ExitType();
sl@0
   258
			TInt exitReason=t.ExitReason();
sl@0
   259
			TName exitCategory=t.ExitCategory();
sl@0
   260
			t.Close();
sl@0
   261
			if (exitType==EExitPanic)
sl@0
   262
				{
sl@0
   263
				test.Printf(_L("Thread %S Panic %S %d\n"),&n,&exitCategory,exitReason);
sl@0
   264
				test(0);
sl@0
   265
				}
sl@0
   266
			if (exitType==EExitKill)
sl@0
   267
				{
sl@0
   268
				Restart(n);
sl@0
   269
				}
sl@0
   270
			h=0;
sl@0
   271
			u.Logon(su,h);
sl@0
   272
			}
sl@0
   273
		if (st!=KRequestPending)
sl@0
   274
			{
sl@0
   275
			if (!CheckerHasRun)
sl@0
   276
				{
sl@0
   277
				checker.SetPriority(EPriorityMuchMore);
sl@0
   278
				CheckerBumped++;
sl@0
   279
				}
sl@0
   280
			CheckerHasRun=EFalse;
sl@0
   281
			if (++tick==4)
sl@0
   282
				{
sl@0
   283
				tick=0;
sl@0
   284
				test.Printf(_L("Heap user iterations %d Creator iterations %d\n"),HeapUserIterations,CreatorIterations);
sl@0
   285
				test.Printf(_L("Checks %d Bumped %d\n"),TotalChecks,CheckerBumped);
sl@0
   286
				}
sl@0
   287
			timer.After(st,500000);
sl@0
   288
			}
sl@0
   289
		}
sl@0
   290
	}