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