1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/misc/t_stres1.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,290 @@
1.4 +// Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32test\misc\t_stres1.cpp
1.18 +//
1.19 +//
1.20 +
1.21 +#include <e32test.h>
1.22 +#include "u32std.h"
1.23 +#include <e32atomics.h>
1.24 +#include "../misc/prbs.h"
1.25 +
1.26 +RTest test(_L("T_STRES1"));
1.27 +
1.28 +_LIT(KLibraryFileName,"STRES1");
1.29 +
1.30 +LOCAL_D RMutex Mutex;
1.31 +LOCAL_D RSemaphore Semaphore;
1.32 +LOCAL_D RChunk Chunk;
1.33 +LOCAL_D RLibrary Library;
1.34 +
1.35 +LOCAL_D TBool CheckerHasRun=EFalse;
1.36 +LOCAL_D TInt TotalChecks=0;
1.37 +LOCAL_D TInt CheckerBumped=0;
1.38 +LOCAL_D TInt HeapUserIterations=0;
1.39 +LOCAL_D TInt CreatorIterations=0;
1.40 +LOCAL_D RSemaphore KillerSem;
1.41 +
1.42 +LOCAL_C TInt HeapChecker(TAny*)
1.43 + {
1.44 + TInt n=0;
1.45 + FOREVER
1.46 + {
1.47 + User::Allocator().Check();
1.48 + TotalChecks++;
1.49 + CheckerHasRun=ETrue;
1.50 + if (++n==4)
1.51 + {
1.52 + n=0;
1.53 + User::CompressAllHeaps();
1.54 + }
1.55 + RThread().SetPriority(EPriorityMuchLess);
1.56 + }
1.57 + }
1.58 +
1.59 +LOCAL_C TAny* HeapAlloc(TUint* aSeed)
1.60 + {
1.61 + TInt size=(Random(aSeed)&0x3ffc)+8;
1.62 + TUint32* p=(TUint32*)User::Alloc(size);
1.63 + if (p)
1.64 + {
1.65 + size=User::Allocator().AllocLen(p);
1.66 + p[0]=aSeed[0];
1.67 + p[1]=aSeed[1];
1.68 + TInt i;
1.69 + for (i=2; i<(size>>2); i++)
1.70 + p[i]=Random(aSeed);
1.71 + }
1.72 + return p;
1.73 + }
1.74 +
1.75 +LOCAL_C void HeapFree(TAny* aPtr)
1.76 + {
1.77 + if (!aPtr)
1.78 + return;
1.79 + TInt size=User::Allocator().AllocLen(aPtr);
1.80 + TUint32* p=(TUint32*)aPtr;
1.81 + TUint seed[2];
1.82 + seed[0]=p[0];
1.83 + seed[1]=p[1];
1.84 + TInt i;
1.85 + for (i=2; i<(size>>2); i++)
1.86 + {
1.87 + if (p[i]!=Random(seed))
1.88 + User::Panic(_L("VERIFY"),i);
1.89 + }
1.90 + User::Free(p);
1.91 + }
1.92 +
1.93 +LOCAL_C TInt HeapUser(TAny* aSeed)
1.94 + {
1.95 + TUint seed[2];
1.96 + seed[0]=(TUint)aSeed;
1.97 + seed[1]=0;
1.98 + FOREVER
1.99 + {
1.100 + TAny* p0=HeapAlloc(seed);
1.101 + TAny* p1=HeapAlloc(seed);
1.102 + TAny* p2=HeapAlloc(seed);
1.103 + HeapFree(p1);
1.104 + TAny* p3=HeapAlloc(seed);
1.105 + TAny* p4=HeapAlloc(seed);
1.106 + HeapFree(p4);
1.107 + HeapFree(p0);
1.108 + TAny* p5=HeapAlloc(seed);
1.109 + HeapFree(p2);
1.110 + TAny* p6=HeapAlloc(seed);
1.111 + TAny* p7=HeapAlloc(seed);
1.112 + HeapFree(p3);
1.113 + HeapFree(p7);
1.114 + HeapFree(p5);
1.115 + HeapFree(p6);
1.116 + __e32_atomic_add_ord32(&HeapUserIterations, 1);
1.117 + TInt ms=Random(seed)&63;
1.118 + TTimeIntervalMicroSeconds32 wait(1000*ms);
1.119 + User::AfterHighRes(wait);
1.120 + }
1.121 + }
1.122 +
1.123 +LOCAL_C TInt KernelObjectCreator(TAny* aSeed)
1.124 + {
1.125 + TUint seed[2];
1.126 + seed[0]=(TUint)aSeed;
1.127 + seed[1]=0;
1.128 + FOREVER
1.129 + {
1.130 + TInt rm=Mutex.CreateLocal(EOwnerThread);
1.131 + TInt rs=Semaphore.CreateLocal(0,EOwnerThread);
1.132 + TInt rc=Chunk.CreateLocal(0x10000,0x100000,EOwnerThread);
1.133 + TInt rl=Library.Load(KLibraryFileName);
1.134 + if (rm==KErrNone)
1.135 + {
1.136 + Mutex.Close();
1.137 + }
1.138 + if (rs==KErrNone)
1.139 + {
1.140 + Semaphore.Close();
1.141 + }
1.142 + if (rc==KErrNone)
1.143 + {
1.144 + Chunk.Close();
1.145 + }
1.146 + if (rl==KErrNone)
1.147 + {
1.148 + Library.Close();
1.149 + }
1.150 + CreatorIterations++;
1.151 + TInt ms=Random(seed)&63;
1.152 + TTimeIntervalMicroSeconds32 wait(1000*ms);
1.153 + User::AfterHighRes(wait);
1.154 + }
1.155 + }
1.156 +
1.157 +LOCAL_C TInt KillerThread(TAny* aSeed)
1.158 + {
1.159 + TUint seed[2];
1.160 + seed[0]=(TUint)aSeed;
1.161 + seed[1]=0;
1.162 + FOREVER
1.163 + {
1.164 + KillerSem.Wait();
1.165 + TInt ms=Random(seed)&127;
1.166 + ms+=32;
1.167 + TTimeIntervalMicroSeconds32 wait(1000*ms);
1.168 + User::AfterHighRes(wait);
1.169 + RThread t;
1.170 + TFindThread ft(_L("*Creator"));
1.171 + TFullName fn;
1.172 + TInt r=ft.Next(fn);
1.173 + if (r!=KErrNone)
1.174 + User::Panic(_L("FindErr"),r);
1.175 + r=t.Open(ft);
1.176 + if (r!=KErrNone)
1.177 + User::Panic(_L("OpenErr"),r);
1.178 + t.Kill(1);
1.179 + t.Close();
1.180 + }
1.181 + }
1.182 +
1.183 +LOCAL_C TInt StartThread(const TDesC& aName, TThreadFunction aFunction, TAny* aPtr, TThreadPriority aPriority, RThread* aThread)
1.184 + {
1.185 + RThread t;
1.186 + TInt r=t.Create(aName,aFunction,0x1000,NULL,aPtr);
1.187 + if (r!=KErrNone)
1.188 + return r;
1.189 + t.SetPriority(aPriority);
1.190 + t.Resume();
1.191 + if (aThread)
1.192 + aThread->SetHandle(t.Handle());
1.193 + else
1.194 + t.Close();
1.195 + return r;
1.196 + }
1.197 +
1.198 +LOCAL_C void Initialise(RThread& aChecker)
1.199 + {
1.200 + TInt r=StartThread(_L("HeapChecker"),HeapChecker,NULL,EPriorityMuchLess,&aChecker);
1.201 + test(r==KErrNone);
1.202 + r=StartThread(_L("HeapUser1"),HeapUser,(TAny*)0xb504f333,EPriorityNormal,NULL);
1.203 + test(r==KErrNone);
1.204 + r=StartThread(_L("HeapUser2"),HeapUser,(TAny*)0xddb3d743,EPriorityNormal,NULL);
1.205 + test(r==KErrNone);
1.206 + r=StartThread(_L("Creator"),KernelObjectCreator,(TAny*)0xadf85458,EPriorityNormal,NULL);
1.207 + test(r==KErrNone);
1.208 + r=KillerSem.CreateLocal(1);
1.209 + test(r==KErrNone);
1.210 + r=StartThread(_L("Killer"),KillerThread,(TAny*)0xb17217f8,EPriorityMore,NULL);
1.211 + test(r==KErrNone);
1.212 + }
1.213 +
1.214 +LOCAL_C void Restart(const TDesC&)
1.215 + {
1.216 + FOREVER
1.217 + {
1.218 + TInt r=StartThread(_L("Creator"),KernelObjectCreator,(TAny*)0xadf85458,EPriorityNormal,NULL);
1.219 + if (r==KErrNone)
1.220 + break;
1.221 + User::After(15000);
1.222 + }
1.223 + KillerSem.Signal();
1.224 + }
1.225 +
1.226 +GLDEF_C TInt E32Main()
1.227 + {
1.228 + test.Title();
1.229 +
1.230 + RThread().SetPriority(EPriorityMuchMore);
1.231 + RThread checker;
1.232 +
1.233 + test.Start(_L("Initialise"));
1.234 + Initialise(checker);
1.235 +
1.236 + test.Next(_L("Create undertaker"));
1.237 + RUndertaker u;
1.238 + TInt r=u.Create();
1.239 + test(r==KErrNone);
1.240 + test.Next(_L("Create timer"));
1.241 + RTimer timer;
1.242 + r=timer.CreateLocal();
1.243 + test(r==KErrNone);
1.244 + TInt tick=0;
1.245 + TRequestStatus su;
1.246 + TRequestStatus st;
1.247 + TInt h=0;
1.248 + test.Next(_L("Logon to undertaker"));
1.249 + u.Logon(su,h);
1.250 + test.Next(_L("Start timer"));
1.251 + timer.After(st,500000);
1.252 + FOREVER
1.253 + {
1.254 + User::WaitForRequest(su,st);
1.255 + if (su!=KRequestPending)
1.256 + {
1.257 + RThread t;
1.258 + t.SetHandle(h);
1.259 + TName n=t.Name();
1.260 + TExitType exitType=t.ExitType();
1.261 + TInt exitReason=t.ExitReason();
1.262 + TName exitCategory=t.ExitCategory();
1.263 + t.Close();
1.264 + if (exitType==EExitPanic)
1.265 + {
1.266 + test.Printf(_L("Thread %S Panic %S %d\n"),&n,&exitCategory,exitReason);
1.267 + test(0);
1.268 + }
1.269 + if (exitType==EExitKill)
1.270 + {
1.271 + Restart(n);
1.272 + }
1.273 + h=0;
1.274 + u.Logon(su,h);
1.275 + }
1.276 + if (st!=KRequestPending)
1.277 + {
1.278 + if (!CheckerHasRun)
1.279 + {
1.280 + checker.SetPriority(EPriorityMuchMore);
1.281 + CheckerBumped++;
1.282 + }
1.283 + CheckerHasRun=EFalse;
1.284 + if (++tick==4)
1.285 + {
1.286 + tick=0;
1.287 + test.Printf(_L("Heap user iterations %d Creator iterations %d\n"),HeapUserIterations,CreatorIterations);
1.288 + test.Printf(_L("Checks %d Bumped %d\n"),TotalChecks,CheckerBumped);
1.289 + }
1.290 + timer.After(st,500000);
1.291 + }
1.292 + }
1.293 + }