sl@0: // Copyright (c) 1996-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\misc\t_stres1.cpp sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include "u32std.h" sl@0: #include sl@0: #include "../misc/prbs.h" sl@0: sl@0: RTest test(_L("T_STRES1")); sl@0: sl@0: _LIT(KLibraryFileName,"STRES1"); sl@0: sl@0: LOCAL_D RMutex Mutex; sl@0: LOCAL_D RSemaphore Semaphore; sl@0: LOCAL_D RChunk Chunk; sl@0: LOCAL_D RLibrary Library; sl@0: sl@0: LOCAL_D TBool CheckerHasRun=EFalse; sl@0: LOCAL_D TInt TotalChecks=0; sl@0: LOCAL_D TInt CheckerBumped=0; sl@0: LOCAL_D TInt HeapUserIterations=0; sl@0: LOCAL_D TInt CreatorIterations=0; sl@0: LOCAL_D RSemaphore KillerSem; sl@0: sl@0: LOCAL_C TInt HeapChecker(TAny*) sl@0: { sl@0: TInt n=0; sl@0: FOREVER sl@0: { sl@0: User::Allocator().Check(); sl@0: TotalChecks++; sl@0: CheckerHasRun=ETrue; sl@0: if (++n==4) sl@0: { sl@0: n=0; sl@0: User::CompressAllHeaps(); sl@0: } sl@0: RThread().SetPriority(EPriorityMuchLess); sl@0: } sl@0: } sl@0: sl@0: LOCAL_C TAny* HeapAlloc(TUint* aSeed) sl@0: { sl@0: TInt size=(Random(aSeed)&0x3ffc)+8; sl@0: TUint32* p=(TUint32*)User::Alloc(size); sl@0: if (p) sl@0: { sl@0: size=User::Allocator().AllocLen(p); sl@0: p[0]=aSeed[0]; sl@0: p[1]=aSeed[1]; sl@0: TInt i; sl@0: for (i=2; i<(size>>2); i++) sl@0: p[i]=Random(aSeed); sl@0: } sl@0: return p; sl@0: } sl@0: sl@0: LOCAL_C void HeapFree(TAny* aPtr) sl@0: { sl@0: if (!aPtr) sl@0: return; sl@0: TInt size=User::Allocator().AllocLen(aPtr); sl@0: TUint32* p=(TUint32*)aPtr; sl@0: TUint seed[2]; sl@0: seed[0]=p[0]; sl@0: seed[1]=p[1]; sl@0: TInt i; sl@0: for (i=2; i<(size>>2); i++) sl@0: { sl@0: if (p[i]!=Random(seed)) sl@0: User::Panic(_L("VERIFY"),i); sl@0: } sl@0: User::Free(p); sl@0: } sl@0: sl@0: LOCAL_C TInt HeapUser(TAny* aSeed) sl@0: { sl@0: TUint seed[2]; sl@0: seed[0]=(TUint)aSeed; sl@0: seed[1]=0; sl@0: FOREVER sl@0: { sl@0: TAny* p0=HeapAlloc(seed); sl@0: TAny* p1=HeapAlloc(seed); sl@0: TAny* p2=HeapAlloc(seed); sl@0: HeapFree(p1); sl@0: TAny* p3=HeapAlloc(seed); sl@0: TAny* p4=HeapAlloc(seed); sl@0: HeapFree(p4); sl@0: HeapFree(p0); sl@0: TAny* p5=HeapAlloc(seed); sl@0: HeapFree(p2); sl@0: TAny* p6=HeapAlloc(seed); sl@0: TAny* p7=HeapAlloc(seed); sl@0: HeapFree(p3); sl@0: HeapFree(p7); sl@0: HeapFree(p5); sl@0: HeapFree(p6); sl@0: __e32_atomic_add_ord32(&HeapUserIterations, 1); sl@0: TInt ms=Random(seed)&63; sl@0: TTimeIntervalMicroSeconds32 wait(1000*ms); sl@0: User::AfterHighRes(wait); sl@0: } sl@0: } sl@0: sl@0: LOCAL_C TInt KernelObjectCreator(TAny* aSeed) sl@0: { sl@0: TUint seed[2]; sl@0: seed[0]=(TUint)aSeed; sl@0: seed[1]=0; sl@0: FOREVER sl@0: { sl@0: TInt rm=Mutex.CreateLocal(EOwnerThread); sl@0: TInt rs=Semaphore.CreateLocal(0,EOwnerThread); sl@0: TInt rc=Chunk.CreateLocal(0x10000,0x100000,EOwnerThread); sl@0: TInt rl=Library.Load(KLibraryFileName); sl@0: if (rm==KErrNone) sl@0: { sl@0: Mutex.Close(); sl@0: } sl@0: if (rs==KErrNone) sl@0: { sl@0: Semaphore.Close(); sl@0: } sl@0: if (rc==KErrNone) sl@0: { sl@0: Chunk.Close(); sl@0: } sl@0: if (rl==KErrNone) sl@0: { sl@0: Library.Close(); sl@0: } sl@0: CreatorIterations++; sl@0: TInt ms=Random(seed)&63; sl@0: TTimeIntervalMicroSeconds32 wait(1000*ms); sl@0: User::AfterHighRes(wait); sl@0: } sl@0: } sl@0: sl@0: LOCAL_C TInt KillerThread(TAny* aSeed) sl@0: { sl@0: TUint seed[2]; sl@0: seed[0]=(TUint)aSeed; sl@0: seed[1]=0; sl@0: FOREVER sl@0: { sl@0: KillerSem.Wait(); sl@0: TInt ms=Random(seed)&127; sl@0: ms+=32; sl@0: TTimeIntervalMicroSeconds32 wait(1000*ms); sl@0: User::AfterHighRes(wait); sl@0: RThread t; sl@0: TFindThread ft(_L("*Creator")); sl@0: TFullName fn; sl@0: TInt r=ft.Next(fn); sl@0: if (r!=KErrNone) sl@0: User::Panic(_L("FindErr"),r); sl@0: r=t.Open(ft); sl@0: if (r!=KErrNone) sl@0: User::Panic(_L("OpenErr"),r); sl@0: t.Kill(1); sl@0: t.Close(); sl@0: } sl@0: } sl@0: sl@0: LOCAL_C TInt StartThread(const TDesC& aName, TThreadFunction aFunction, TAny* aPtr, TThreadPriority aPriority, RThread* aThread) sl@0: { sl@0: RThread t; sl@0: TInt r=t.Create(aName,aFunction,0x1000,NULL,aPtr); sl@0: if (r!=KErrNone) sl@0: return r; sl@0: t.SetPriority(aPriority); sl@0: t.Resume(); sl@0: if (aThread) sl@0: aThread->SetHandle(t.Handle()); sl@0: else sl@0: t.Close(); sl@0: return r; sl@0: } sl@0: sl@0: LOCAL_C void Initialise(RThread& aChecker) sl@0: { sl@0: TInt r=StartThread(_L("HeapChecker"),HeapChecker,NULL,EPriorityMuchLess,&aChecker); sl@0: test(r==KErrNone); sl@0: r=StartThread(_L("HeapUser1"),HeapUser,(TAny*)0xb504f333,EPriorityNormal,NULL); sl@0: test(r==KErrNone); sl@0: r=StartThread(_L("HeapUser2"),HeapUser,(TAny*)0xddb3d743,EPriorityNormal,NULL); sl@0: test(r==KErrNone); sl@0: r=StartThread(_L("Creator"),KernelObjectCreator,(TAny*)0xadf85458,EPriorityNormal,NULL); sl@0: test(r==KErrNone); sl@0: r=KillerSem.CreateLocal(1); sl@0: test(r==KErrNone); sl@0: r=StartThread(_L("Killer"),KillerThread,(TAny*)0xb17217f8,EPriorityMore,NULL); sl@0: test(r==KErrNone); sl@0: } sl@0: sl@0: LOCAL_C void Restart(const TDesC&) sl@0: { sl@0: FOREVER sl@0: { sl@0: TInt r=StartThread(_L("Creator"),KernelObjectCreator,(TAny*)0xadf85458,EPriorityNormal,NULL); sl@0: if (r==KErrNone) sl@0: break; sl@0: User::After(15000); sl@0: } sl@0: KillerSem.Signal(); sl@0: } sl@0: sl@0: GLDEF_C TInt E32Main() sl@0: { sl@0: test.Title(); sl@0: sl@0: RThread().SetPriority(EPriorityMuchMore); sl@0: RThread checker; sl@0: sl@0: test.Start(_L("Initialise")); sl@0: Initialise(checker); sl@0: sl@0: test.Next(_L("Create undertaker")); sl@0: RUndertaker u; sl@0: TInt r=u.Create(); sl@0: test(r==KErrNone); sl@0: test.Next(_L("Create timer")); sl@0: RTimer timer; sl@0: r=timer.CreateLocal(); sl@0: test(r==KErrNone); sl@0: TInt tick=0; sl@0: TRequestStatus su; sl@0: TRequestStatus st; sl@0: TInt h=0; sl@0: test.Next(_L("Logon to undertaker")); sl@0: u.Logon(su,h); sl@0: test.Next(_L("Start timer")); sl@0: timer.After(st,500000); sl@0: FOREVER sl@0: { sl@0: User::WaitForRequest(su,st); sl@0: if (su!=KRequestPending) sl@0: { sl@0: RThread t; sl@0: t.SetHandle(h); sl@0: TName n=t.Name(); sl@0: TExitType exitType=t.ExitType(); sl@0: TInt exitReason=t.ExitReason(); sl@0: TName exitCategory=t.ExitCategory(); sl@0: t.Close(); sl@0: if (exitType==EExitPanic) sl@0: { sl@0: test.Printf(_L("Thread %S Panic %S %d\n"),&n,&exitCategory,exitReason); sl@0: test(0); sl@0: } sl@0: if (exitType==EExitKill) sl@0: { sl@0: Restart(n); sl@0: } sl@0: h=0; sl@0: u.Logon(su,h); sl@0: } sl@0: if (st!=KRequestPending) sl@0: { sl@0: if (!CheckerHasRun) sl@0: { sl@0: checker.SetPriority(EPriorityMuchMore); sl@0: CheckerBumped++; sl@0: } sl@0: CheckerHasRun=EFalse; sl@0: if (++tick==4) sl@0: { sl@0: tick=0; sl@0: test.Printf(_L("Heap user iterations %d Creator iterations %d\n"),HeapUserIterations,CreatorIterations); sl@0: test.Printf(_L("Checks %d Bumped %d\n"),TotalChecks,CheckerBumped); sl@0: } sl@0: timer.After(st,500000); sl@0: } sl@0: } sl@0: }