kerneltest/f32test/demandpaging/loader/t_pageldrtst.cpp
changeset 43 96e5fb8b040d
child 44 36bfc973b146
equal deleted inserted replaced
-1:000000000000 43:96e5fb8b040d
       
     1 // Copyright (c) 2006-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 // f32test\demandpaging\loader\t_pageldrtst.cpp
       
    15 // Demand Paging Loader Stress Tests
       
    16 // Demand Paging Loader stress tests attempt to cause as much paging as possible
       
    17 // whilst putting the system various types of load.
       
    18 // t_pageldrtst.exe is the root of the tests, it in turn will start copies of 
       
    19 // itself stored in various types of media (t_pageldrtst_rom.exe for example).
       
    20 // It also loads DLLs from various media, each DLL containing simple functions 
       
    21 // that are aligned on page boundaries, so each function call is likely to 
       
    22 // cause a page fault.
       
    23 // Usage:
       
    24 // t_pageldrtst and t_pageldrtst_rom
       
    25 // Common command lines:
       
    26 // t_pageldrtst - run the auto test suite
       
    27 // t_pageldrtst lowmem - run the low memory tests
       
    28 // t_pageldrtst chunks - run the chunk tests
       
    29 // t_pageldrtst chunks+ - run the chunk tests (same as used in autotest)
       
    30 // t_pageldrtst echunks - run the really stressful chunk tests
       
    31 // t_pageldrtst auto debug - run the autotest but with debug output to the serial port
       
    32 // t_pageldrtst d_exc - run the d_exc tests
       
    33 // Arguments:
       
    34 // single - run the tests in a single thread
       
    35 // multiple <numThreads> - run the tests in multiple threads where <numThreads>
       
    36 // auto - dummy param to trick the tests into running the auto test suite with extra params
       
    37 // fullauto - param to make the tests perform the full automatic stress test
       
    38 // interleave - force thread interleaving
       
    39 // prio - each thread reschedules in between each function call, causes lots of context changes
       
    40 // media - perform media access during the tests, very stressful
       
    41 // mmc - only use the mmc media for media access to test file caching
       
    42 // min - min cache size in pages
       
    43 // max - max cache size in pages
       
    44 // chunks - simple chunk stress tests
       
    45 // chunks+ - the chunk auto tests
       
    46 // echunks - extremem chunks tests
       
    47 // nochunkdata - don't check the integrity of the data in the chunks
       
    48 // lowmem - low memory tests
       
    49 // dll - only load dll's
       
    50 // exe - only start exe's (t_pagestress)
       
    51 // self - only start copies of self (t_pageldrtst from various media)
       
    52 // complete - dll, exe and self.
       
    53 // rom - only load from ROM
       
    54 // base - only load the base DLL and exe's (from code)
       
    55 // mixed - rom and base.
       
    56 // all_media - load dlls and exes from all media
       
    57 // debug - switch on debugging information
       
    58 // silent - no output to the screen or serial port
       
    59 // noclean - don't delete copied files on exit
       
    60 // d_exc - run the d_exc tests
       
    61 // global - load dlls once globally
       
    62 // thread - load dlls once per thread
       
    63 // func - load dlls in the test function (default and most stressful)
       
    64 // forward - patern in which to execute function calls 
       
    65 // backward - patern in which to execute function calls 			
       
    66 // random - patern in which to execute function calls 			
       
    67 // all - patern in which to execute function calls (forward, backward and random)
       
    68 // inst - for debugging a parameter passed to a spawned exe to give it an id.
       
    69 // iters <count> - the number of times to loop (a '-' means run forever)
       
    70 // reaper - test the reaper.
       
    71 // btrace - test the btrace code.
       
    72 // defrag - test the ram defrag code.
       
    73 // stressfree - set the page cache to stress free size and run tests.
       
    74 // t_pageldrtst causes a large ammount of paging by repeatedly calling 
       
    75 // functions from multiple DLLs which include 64 functions which have 
       
    76 // been aligned on page boundaries from multiple threads, whilst causing 
       
    77 // background paging by spawning copies of itself and t_pagestress.
       
    78 // The test also endeavours to stress the loader by loading and unloading
       
    79 // DLLs from multiple threads from various types of media at the same 
       
    80 // time as stressing the media, testing chunks, the reaper and changing
       
    81 // thread priorities.
       
    82 // 002 Load thrashing, test rapid loading and unloading of DLLs from 
       
    83 // multiple threads (DEF100158)
       
    84 // 003 Multiple threads loading DLLs in random pattern
       
    85 // 004 Multiple threads loading EXE, SELF and DLLs in random pattern with
       
    86 // all media, loaded in thread with prio change
       
    87 // 005 Multiple threads loading EXE, SELF and DLLs in random pattern with
       
    88 // all media, loaded globally with prio change
       
    89 // 006 Multiple threads loading EXE, SELF and DLLs in random pattern with
       
    90 // all media, loaded in func with process interleaving
       
    91 // 007 Multiple threads loading EXE, SELF and DLLs in random pattern with
       
    92 // all media, loaded in func with process interleaving, prio change
       
    93 // and media access
       
    94 // 008 Low Memory setup test
       
    95 // 009 Low Memory, Multiple threads loading EXE, SELF and DLLs in random 
       
    96 // pattern, loaded in func.
       
    97 // 010 Low Memory setup test
       
    98 // 011 Low Memory, Multiple threads loading EXE, SELF and DLLs in random 
       
    99 // pattern, loaded in func with process interleaving, 
       
   100 // prio change and media access
       
   101 // 012 Close test driver
       
   102 // 013 Chunk tests, Multiple threads loading EXE, SELF and DLLs in random 
       
   103 // pattern with ROM / ROFS media, loaded in func with prio change 
       
   104 // 014 Reaper tests with Multiple threads loading EXE, SELF and DLLs in random 
       
   105 // pattern with all media 
       
   106 // 015 Reaper tests with Multiple threads loading EXE, SELF and DLLs in random 
       
   107 // pattern with all media, prio change and process interleaving
       
   108 // 016 d_exc check test
       
   109 // 
       
   110 //
       
   111 
       
   112 //! @SYMTestCaseID			KBASE-T_PAGELDRTST-0326
       
   113 //! @SYMTestType			UT
       
   114 //! @SYMPREQ				PREQ1110
       
   115 //! @SYMTestCaseDesc		Demand Paging Loader Stress Tests
       
   116 //! @SYMTestActions			001 Demand Paging loader stress tests...
       
   117 //! @SYMTestExpectedResults All tests should pass.
       
   118 //! @SYMTestPriority        High
       
   119 //! @SYMTestStatus          Implemented
       
   120 
       
   121 #include <e32test.h>
       
   122 #include <e32rom.h>
       
   123 #include <u32hal.h>
       
   124 #include <f32file.h>
       
   125 #include <f32dbg.h>
       
   126 #include <e32msgqueue.h>
       
   127 #include <e32math.h>
       
   128 #include <e32btrace.h>
       
   129 #include <d32btrace.h>
       
   130 #include <hal.h>
       
   131 
       
   132 #include "t_hash.h"
       
   133 #include "paging_info.h"
       
   134 
       
   135 #ifndef TEST_AUTOTEST
       
   136 #define TEST_RUN_REAPERTEST
       
   137 #define TEST_RUN_LOWMEMTEST
       
   138 #define TEST_RUN_DEFRAGTEST
       
   139 #define TEST_RUN_D_EXCTEST
       
   140 #define TEST_RUN_CHUNKTEST
       
   141 #define TEST_RUN_AUTOTEST
       
   142 RTest test(_L("T_PAGELDRTST"));
       
   143 #else
       
   144 #ifdef TEST_RUN_REAPERTEST
       
   145 RTest test(_L("T_PAGELDRTST_REAPER"));
       
   146 #endif
       
   147 #ifdef TEST_RUN_LOWMEMTEST
       
   148 RTest test(_L("T_PAGELDRTST_LOWMEM"));
       
   149 #endif
       
   150 #ifdef TEST_RUN_DEFRAGTEST
       
   151 RTest test(_L("T_PAGELDRTST_DEFRAG"));
       
   152 #endif
       
   153 #ifdef TEST_RUN_D_EXCTEST
       
   154 RTest test(_L("T_PAGELDRTST_D_EXC"));
       
   155 #endif
       
   156 #ifdef TEST_RUN_CHUNKTEST
       
   157 RTest test(_L("T_PAGELDRTST_CHUNK"));
       
   158 #endif
       
   159 #ifdef TEST_RUN_AUTOTEST
       
   160 RTest test(_L("T_PAGELDRTST_AUTO"));
       
   161 #endif
       
   162 #endif //TEST_AUTOTEST
       
   163 
       
   164 const TInt KMessageBufSize = 80;
       
   165 typedef TBuf<KMessageBufSize> TMessageBuf;
       
   166 
       
   167 //#define TEST_SHORT_TEST
       
   168 //#define TEST_THRASHING_TEST
       
   169 //#define TEST_ADD_FAT_MEDIA
       
   170 #define TEST_DONT_RESET_STATS
       
   171 #define TEST_MINIMAL_STATS
       
   172 //#define TEST_KERN_HEAP
       
   173 #define TEST_ADD_FRAGD_MEDIA
       
   174 #ifdef TEST_ADD_FRAGD_MEDIA
       
   175 #endif
       
   176 
       
   177 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
       
   178 //#define WANT_FS_CACHE_STATS 
       
   179 #endif
       
   180 
       
   181 #ifdef __X86__
       
   182 #define TEST_ON_UNPAGED
       
   183 #define TEST_NO_DEXC_IN_AUTO
       
   184 #endif
       
   185 
       
   186 
       
   187 #include "t_pagestress.h"
       
   188 #include "t_pageldrtstdll.h"
       
   189 
       
   190 #include "t_ramstress.h"
       
   191 
       
   192 TBool		TestDebug					= EFalse;
       
   193 TBool		TestSilent					= EFalse;
       
   194 TBool		TestExit					= EFalse;
       
   195 #define TEST_EXE			0x01
       
   196 #define TEST_DLL			0x02
       
   197 #define TEST_SELF			0x04
       
   198 #define TEST_EXE_SELF		(TEST_EXE | TEST_SELF)
       
   199 #define TEST_EXE_SELF_DLL	(TEST_EXE | TEST_SELF | TEST_DLL)
       
   200 TInt		TestLoading				    = TEST_EXE_SELF_DLL;
       
   201 
       
   202 #define TEST_MEDIA_BASE			(1 << KTestMediaBase)
       
   203 #define TEST_MEDIA_ROM			(1 << KTestMediaRom)
       
   204 #define TEST_MEDIA_ROFS			(1 << KTestMediaRofs)
       
   205 #define TEST_MEDIA_EXT			(1 << KTestMediaExt)
       
   206 #define TEST_MEDIA_FAT			(1 << KTestMediaFat)
       
   207 #define TEST_MEDIA_MMC			(1 << KTestMediaMmc)
       
   208 #define TEST_MEDIA_ROM_BASE		(TEST_MEDIA_ROM | TEST_MEDIA_BASE)
       
   209 #define TEST_MEDIA_ALL		(TEST_MEDIA_ROM | TEST_MEDIA_BASE | TEST_MEDIA_ROFS | TEST_MEDIA_EXT | TEST_MEDIA_MMC)
       
   210 
       
   211 typedef enum
       
   212 {
       
   213 	KTestMediaBase = 0,
       
   214 	KTestMediaRom,
       
   215 	KTestMediaExt,
       
   216 	KTestMediaRofs,
       
   217 #ifdef TEST_ADD_FAT_MEDIA
       
   218 	KTestMediaFat,  // this is the last one that is always present.
       
   219 #endif
       
   220 	KTestMediaMmc,
       
   221 #ifdef TEST_ADD_FRAGD_MEDIA
       
   222 	KTestMediaNandFrag,
       
   223 	KTestMediaMmcFrag,
       
   224 #endif
       
   225 	KTestMediaCOUNT,
       
   226 }ETestMediaType;
       
   227 #ifdef TEST_ADD_FAT_MEDIA
       
   228 #define TEST_MEDIA_COUNT_HACK   (KTestMediaFat + 1)
       
   229 #else
       
   230 #define TEST_MEDIA_COUNT_HACK   (KTestMediaRofs + 1)
       
   231 #endif
       
   232 
       
   233 typedef enum
       
   234 {
       
   235 	KTestMediaAccessNone = 0,
       
   236 	KTestMediaAccessBasic,
       
   237 	KTestMediaAccessMultipleThreads,
       
   238 	KTestMediaAccessMultiplePattern,
       
   239 	KTestMediaAccessMixed,
       
   240 	KTestMediaAccessCOUNT,
       
   241 }ETestMediaAccess;
       
   242 
       
   243 TInt		TestWhichMedia			    = TEST_MEDIA_ROM_BASE;
       
   244 TInt		DriveNumber=-1;   // Parameter - Which drive?  -1 = autodetect.
       
   245 TBool		TestSingle					= EFalse;
       
   246 TBool		TestMultiple				= EFalse;
       
   247 TInt		TestMaxLoops				= 20;
       
   248 #define TEST_2MEDIA_THREADS		20
       
   249 #define TEST_ALLMEDIA_THREADS	20
       
   250 TInt		TestMultipleThreadCount		= TEST_2MEDIA_THREADS;
       
   251 TInt		TestInstanceId				= 0;
       
   252 TBool		TestWeAreTheTestBase        = EFalse;
       
   253 TBool		TestBootedFromMmc			= EFalse;
       
   254 TBool		TestOnlyFromMmc				= EFalse;
       
   255 TBool		TestD_Exc					= EFalse;
       
   256 TBool		TestNoClean					= EFalse;
       
   257 TBool		TestFullAutoTest			= EFalse;
       
   258 #define TEST_DLL_GLOBAL		0x01
       
   259 #define TEST_DLL_THREAD		0x02
       
   260 #define TEST_DLL_FUNC		0x04
       
   261 TInt		TestLoadDllHow				= TEST_DLL_FUNC;
       
   262 TBool		TestIsAutomated				= EFalse;
       
   263 
       
   264 #define TEST_INTERLEAVE_PRIO		EPriorityMore//EPriorityRealTime //23 // KNandThreadPriority - 1
       
   265 TBool		TestInterleave				= EFalse;
       
   266 TFileName	TestNameBuffer;
       
   267 TBool		TestPrioChange				= EFalse;
       
   268 
       
   269 volatile TBool		TestStopMedia				= EFalse;
       
   270 ETestMediaAccess TestMediaAccess        = KTestMediaAccessNone;
       
   271 #define TEST_NUM_FILES		5
       
   272 
       
   273 RSemaphore	TestMultiSem;
       
   274 RMsgQueue<TMessageBuf> TestMsgQueue;
       
   275 
       
   276 #define TEST_LM_NUM_FREE	0
       
   277 #define TEST_LM_BLOCKSIZE	1
       
   278 #define TEST_LM_BLOCKS_FREE	4
       
   279 TBool		TestLowMem					= EFalse;
       
   280 TBool		TestingLowMem				= EFalse;
       
   281 RPageStressTestLdd PagestressLdd;
       
   282 RRamStressTestLdd  RamstressLdd;
       
   283 
       
   284 TBool		TestBtrace					= EFalse;
       
   285 TBool		TestDefrag					= EFalse;
       
   286 TBool		TestChunks					= EFalse;
       
   287 TBool		TestChunksPlus				= EFalse;
       
   288 TBool		TestExtremeChunks			= EFalse;
       
   289 TBool		TestChunkData				= ETrue;
       
   290 TBool		TestingChunks				= EFalse;
       
   291 volatile TBool		TestDefragTestEnd			= EFalse;
       
   292 TBool		TestingDefrag				= EFalse;
       
   293 volatile TBool		TestThreadsExit				= EFalse;
       
   294 TInt		TestPageSize				= 4096;
       
   295 RChunk		TestChunk;
       
   296 TInt		TestCommitEnd = 0;
       
   297 TUint8*		TestChunkBase = NULL;
       
   298 #define TEST_NUM_PAGES			64
       
   299 #define TEST_NUM_CHUNK_PAGES	(TEST_NUM_PAGES * 2)
       
   300 TBool		TestChunkPageState[TEST_NUM_CHUNK_PAGES];
       
   301 
       
   302 TBool		TestReaper					= EFalse;
       
   303 TBool		TestingReaper				= EFalse;
       
   304 TBool		TestingReaperCleaningFiles  = EFalse;
       
   305 #define TEST_REAPER_ITERS			20
       
   306 #define TEST_DOT_PERIOD				30
       
   307 TBool		TestStressFree				= EFalse;
       
   308 TInt		TestMinCacheSize = 64 * 4096;
       
   309 TInt		TestMaxCacheSize = 128 * 4096;
       
   310 TBool		TestIsDemandPaged = ETrue;
       
   311 #define TEST_MAX_ZONE_THREADS		8
       
   312 TUint		TestZoneCount = 0;
       
   313 TInt TickPeriod = 15625;
       
   314 
       
   315 #define TEST_NONE		0x0
       
   316 #define TEST_THRASH		0x1
       
   317 #define TEST_FORWARD	0x2
       
   318 #define TEST_BACKWARD	0x4
       
   319 #define TEST_RANDOM		0x8
       
   320 #define TEST_ALL		(TEST_RANDOM | TEST_BACKWARD | TEST_FORWARD)
       
   321 TUint32	TestWhichTests				= TEST_ALL;
       
   322 _LIT(KRomPath, "z:\\sys\\bin\\");
       
   323 _LIT(KMmcDefaultPath, "d:\\sys\\bin\\");
       
   324 
       
   325 #define EXISTS(__val) ((__val == KErrNone) ? &KFileExists : &KFileMissing)
       
   326 _LIT(KSysHash,"?:\\Sys\\Hash\\");
       
   327 _LIT(KTestBlank, "");
       
   328 _LIT(KFileExists, "Exists");
       
   329 _LIT(KFileMissing, "Missing");
       
   330 _LIT(KMultipleTest, "Multiple");
       
   331 _LIT(KSingleTest,   "Single  ");
       
   332 _LIT(KTestExe, "Exe ");
       
   333 _LIT(KTestDll, "Dll ");
       
   334 _LIT(KTestSelf, "Self ");
       
   335 _LIT(KTestBase, "Base ");
       
   336 _LIT(KTestRom, "ROM ");
       
   337 _LIT(KTestAll, "All ");
       
   338 _LIT(KTestGlobal, "Global");
       
   339 _LIT(KTestThread, "Thread");
       
   340 _LIT(KTestFunc,  "Func");
       
   341 _LIT(KTestInter, "Interleave ");
       
   342 _LIT(KTestPrio, "Prio ");
       
   343 _LIT(KTestMedia, "Media ");
       
   344 _LIT(KTestLowMem, "LowMem ");
       
   345 _LIT(KTestChunking, "Chunks ");
       
   346 _LIT(KTestEChunking, "EChunks ");
       
   347 _LIT(KTestChunkingPlus, "Chunks+ ");
       
   348 _LIT(KTestReaper, "Reaper ");
       
   349 _LIT(KTestThrash, "Thrash ");
       
   350 _LIT(KTestForward, "Forward ");
       
   351 _LIT(KTestBackward, "Backward ");
       
   352 _LIT(KTestRandom, "Random ");
       
   353 
       
   354 typedef struct 
       
   355 	{
       
   356 	TBool				testFullAutoOnly;
       
   357 	TInt				testLoading;
       
   358 	TInt				testWhichMedia;
       
   359 	TBool				testMultiple;
       
   360 	TInt				testMaxLoops;
       
   361 	TInt				testMultipleThreadCount;
       
   362 	TBool				testLoadDllHow;
       
   363 	TBool				testInterleave;
       
   364 	TBool				testPrioChange;
       
   365 	ETestMediaAccess	testMediaAccess;
       
   366 	TUint32				testWhichTests;
       
   367 	TBool				testLowMem;
       
   368 	TInt				testFreeRam;
       
   369 	}TTheTests; 
       
   370 
       
   371 typedef struct
       
   372 	{
       
   373 	TInt	ok;
       
   374 	TInt	fail;
       
   375 	}TChunkTestPair;
       
   376 
       
   377 typedef struct
       
   378 	{
       
   379 	TChunkTestPair	lock;
       
   380 	TChunkTestPair	unlock;
       
   381 	TChunkTestPair	decommit;
       
   382 	TChunkTestPair	commit;
       
   383 	TChunkTestPair	check;
       
   384 	}
       
   385 TChunkTestStats;
       
   386 
       
   387 TChunkTestStats	TestChunkStats[TEST_NUM_CHUNK_PAGES];
       
   388 
       
   389 
       
   390 TPtrC TestPsExeNames[KTestMediaCOUNT] = {	_L("t_pagestress.exe"), 
       
   391 											_L("t_pagestress_rom.exe"), 
       
   392 											_L("t_pagestress_ext.exe"), 
       
   393 											_L("t_pagestress_rofs.exe"), 
       
   394 #ifdef TEST_ADD_FAT_MEDIA
       
   395 											_L("t_pagestress_fat.exe"),
       
   396 #endif
       
   397 											_L("t_pagestress_mmc.exe"),
       
   398 #ifdef TEST_ADD_FRAGD_MEDIA
       
   399 											_L("t_pagestress_nfr.exe"),
       
   400 											_L("t_pagestress_mfr.exe"),
       
   401 #endif
       
   402 											};
       
   403 
       
   404 TPtrC TestPlExeNames[KTestMediaCOUNT] = {	_L("t_pageldrtst.exe"), 
       
   405 											_L("t_pageldrtst_rom.exe"), 
       
   406 											_L("t_pageldrtst_ext.exe"), 
       
   407 											_L("t_pageldrtst_rofs.exe"), 
       
   408 #ifdef TEST_ADD_FAT_MEDIA
       
   409 											_L("t_pageldrtst_fat.exe"),
       
   410 #endif
       
   411 											_L("t_pageldrtst_mmc.exe"),
       
   412 #ifdef TEST_ADD_FRAGD_MEDIA
       
   413 											_L("t_pageldrtst_nfr.exe"),
       
   414 											_L("t_pageldrtst_mfr.exe"),
       
   415 #endif
       
   416 											};
       
   417 
       
   418 _LIT(KDllBaseName,   "t_pageldrtst");
       
   419 
       
   420 TPtrC TestPlExtNames[KTestMediaCOUNT] = {	_L(".dll"),
       
   421 											_L("_rom.dll"),
       
   422 											_L("_ext.dll"),
       
   423 											_L("_rofs.dll"),
       
   424 #ifdef TEST_ADD_FAT_MEDIA
       
   425 											_L("_fat.dll"),
       
   426 #endif
       
   427 											_L("_mmc.dll"),
       
   428 #ifdef TEST_ADD_FRAGD_MEDIA
       
   429 											_L("_nfr.dll"),
       
   430 											_L("_mfr.dll"),
       
   431 #endif
       
   432 											};
       
   433 
       
   434 
       
   435 TBool TestDllExesExist[KTestMediaCOUNT] = { EFalse, 
       
   436 											EFalse,
       
   437 											EFalse,
       
   438 											EFalse,
       
   439 #ifdef TEST_ADD_FAT_MEDIA
       
   440 											EFalse,
       
   441 #endif
       
   442 											EFalse,
       
   443 #ifdef TEST_ADD_FRAGD_MEDIA
       
   444 											EFalse,
       
   445 											EFalse,
       
   446 #endif
       
   447 											};
       
   448 #define DBGS_PRINT(__args)\
       
   449 	if (!TestSilent) test.Printf __args;
       
   450 
       
   451 #define DBGD_PRINT(__args)\
       
   452 	if (TestDebug) test.Printf __args;
       
   453 
       
   454 void SendDebugMessage(RMsgQueue<TMessageBuf> *aMsgQueue = NULL, 
       
   455 					  TMessageBuf            *aBuffer = NULL,
       
   456 					  RSemaphore	 		 *aTheSem = NULL)
       
   457 	{
       
   458 	for (;;)
       
   459 		{
       
   460 		aTheSem->Wait();
       
   461 		TInt r = aMsgQueue->Send(*aBuffer);
       
   462 		aTheSem->Signal();
       
   463 		if (r != KErrOverflow)
       
   464 			return;
       
   465 		User::After(0);
       
   466 		}
       
   467 	}
       
   468 
       
   469 #define DEBUG_PRINT(__args)\
       
   470 if (!TestSilent) \
       
   471 	{\
       
   472 	if (aMsgQueue && aBuffer && aTheSem)\
       
   473 		{\
       
   474 		aBuffer->Zero();\
       
   475 		aBuffer->Format __args ;\
       
   476 		SendDebugMessage(aMsgQueue, aBuffer, aTheSem);\
       
   477 		}\
       
   478 	else\
       
   479 		{\
       
   480 		test.Printf __args ;\
       
   481 		}\
       
   482 	}
       
   483 
       
   484 #define RUNTEST(__test, __error)\
       
   485 	if (!TestSilent)\
       
   486 		test(__test == __error);\
       
   487 	else\
       
   488 		__test;
       
   489 
       
   490 #define RUNTEST1(__test)\
       
   491 	if (!TestSilent)\
       
   492 		test(__test);
       
   493 
       
   494 
       
   495 #define DEBUG_PRINT1(__args)\
       
   496 if (TestDebug)\
       
   497 	{\
       
   498 	DEBUG_PRINT(__args)\
       
   499 	}
       
   500 
       
   501 #define DOTEST(__operation, __condition)\
       
   502 	if (aLowMem) \
       
   503 		{\
       
   504 		__operation;\
       
   505 		while (!__condition)\
       
   506 			{\
       
   507 			DBGD_PRINT((_L("Releasing some memory on line %d\n"), __LINE__));\
       
   508 			if (pTheSem)\
       
   509 				pTheSem->Wait();\
       
   510 			PagestressLdd.DoReleaseSomeRam(TEST_LM_BLOCKS_FREE);\
       
   511 			if (pTheSem)\
       
   512 				pTheSem->Signal();\
       
   513 			__operation;\
       
   514 			}\
       
   515 		RUNTEST1(__condition);\
       
   516 		}\
       
   517 	else\
       
   518 		{\
       
   519 		__operation;\
       
   520 		RUNTEST1(__condition);\
       
   521 		}
       
   522 
       
   523 #define DOTEST1(__var, __func, __ok, __fail)\
       
   524 	if (aLowMem) \
       
   525 		{\
       
   526 		__var = __func;\
       
   527 		while (__var == __fail)\
       
   528 			{\
       
   529 			DBGD_PRINT((_L("Releasing some memory on line %d\n"), __LINE__));\
       
   530 			if (pTheSem)\
       
   531 				pTheSem->Wait();\
       
   532 			PagestressLdd.DoReleaseSomeRam(TEST_LM_BLOCKS_FREE);\
       
   533 			if (pTheSem)\
       
   534 				pTheSem->Signal();\
       
   535 			__var = __func;\
       
   536 			}\
       
   537 		if (__var != __ok)\
       
   538 			DBGS_PRINT((_L("Failing on line %d with error %d\n"), __LINE__, __var));\
       
   539 		RUNTEST1(__var == __ok);\
       
   540 		}\
       
   541 	else\
       
   542 		{\
       
   543 		__var = __func;\
       
   544 		RUNTEST1(__var == __ok);\
       
   545 		}
       
   546 
       
   547 #define DOLOADALLOC(__numDlls, __pTheLibs, __theSem)\
       
   548 	if (TestingLowMem)\
       
   549 		{\
       
   550 		__pTheLibs = (PageLdrRLibrary *)User::AllocZ(sizeof(PageLdrRLibrary) * __numDlls);\
       
   551 		while (__pTheLibs == NULL)\
       
   552 			{\
       
   553 			DEBUG_PRINT1((_L("Releasing some memory for alloc on line %d\n"), __LINE__));\
       
   554 			if (__theSem)\
       
   555 				__theSem->Wait();\
       
   556 			PagestressLdd.DoReleaseSomeRam(TEST_LM_BLOCKS_FREE);\
       
   557 			if (__theSem)\
       
   558 				__theSem->Signal();\
       
   559 			__pTheLibs = (PageLdrRLibrary *)User::AllocZ(sizeof(PageLdrRLibrary) * __numDlls);\
       
   560 			}\
       
   561 		}\
       
   562 	else\
       
   563 		{\
       
   564 		__pTheLibs = (PageLdrRLibrary *)User::AllocZ(sizeof(PageLdrRLibrary) * __numDlls);\
       
   565 		if (__pTheLibs == NULL)\
       
   566 			return KErrGeneral;\
       
   567 		}
       
   568 
       
   569 #define TEST_NEXT(__args) \
       
   570 	if (!TestSilent)\
       
   571 		test.Next __args;
       
   572 
       
   573 void DoStats();
       
   574 void CheckFilePresence(TBool aDoFileCopy);
       
   575 void CleanupFiles(TBool silent);
       
   576 typedef TInt (*TCallFunction)(TUint32 funcIndex, TInt param1, TInt param2);
       
   577 
       
   578 class PageLdrRLibrary : public RLibrary
       
   579 	{
       
   580 public:
       
   581 	TInt TestLoadLibrary(const TDesC& aFileName, TInt aThreadIndex, RMsgQueue<TMessageBuf> *aMsgQueue, TMessageBuf *aBuffer, RSemaphore  *aTheSem);
       
   582 	TInt CloseLibrary();
       
   583 	
       
   584 public:	
       
   585 	TBool				iInUse;
       
   586 	TUint32				iFuncCount;
       
   587 	TLibraryFunction	iInitFunc;
       
   588 	TLibraryFunction	iFunctionCountFunc;
       
   589 	TCallFunction       iCallFunctionFunc;
       
   590 	TLibraryFunction	iSetCloseFunc;
       
   591 	};
       
   592 
       
   593 TInt PageLdrRLibrary::CloseLibrary()
       
   594 	{
       
   595 	if (iInUse)
       
   596 		{
       
   597 		if (iSetCloseFunc)
       
   598 			(iSetCloseFunc)();
       
   599 		Close();
       
   600 		iFuncCount = 0;
       
   601 		iInitFunc = NULL;
       
   602 		iFunctionCountFunc = NULL;
       
   603 		iCallFunctionFunc = NULL;
       
   604 		iSetCloseFunc = NULL;
       
   605 		iInUse = EFalse;
       
   606 		}
       
   607 	return KErrNone;
       
   608 	}
       
   609 
       
   610 PageLdrRLibrary		theGlobalLibs[PAGELDRTST_MAX_DLLS * KTestMediaCOUNT];
       
   611 
       
   612 ////////////////////////////////////////////////////////////
       
   613 // Template functions encapsulating ControlIo magic
       
   614 //
       
   615 GLDEF_D template <class C>
       
   616 GLDEF_C TInt controlIo(RFs &fs, TInt drv, TInt fkn, C &c)
       
   617 {
       
   618     TPtr8 ptrC((TUint8 *)&c, sizeof(C), sizeof(C));
       
   619 
       
   620     TInt r = fs.ControlIo(drv, fkn, ptrC);
       
   621 
       
   622     return r;
       
   623 }
       
   624 
       
   625 //
       
   626 // FreeRam
       
   627 //
       
   628 // Get available free ram.
       
   629 //
       
   630 
       
   631 TInt FreeRam()
       
   632 	{
       
   633 	// wait for any async cleanup in the supervisor to finish first...
       
   634 	UserSvr::HalFunction(EHalGroupKernel, EKernelHalSupervisorBarrier, 0, 0);
       
   635 
       
   636 	TMemoryInfoV1Buf meminfo;
       
   637 	TInt r=UserHal::MemoryInfo(meminfo);
       
   638 	test (r==KErrNone);
       
   639 	return meminfo().iFreeRamInBytes;
       
   640 	}
       
   641 
       
   642 //
       
   643 // FindFsNANDDrive
       
   644 //
       
   645 // Find the NAND drive
       
   646 //
       
   647 
       
   648 static TInt FindFsNANDDrive(RFs& aFs)
       
   649 	{
       
   650 	TDriveList driveList;
       
   651 	TDriveInfo driveInfo;
       
   652 	TInt r=aFs.DriveList(driveList);
       
   653     if (r == KErrNone)
       
   654 		{
       
   655 		for (TInt drvNum= (DriveNumber<0)?0:DriveNumber; drvNum<KMaxDrives; ++drvNum)
       
   656 			{
       
   657 			if(!driveList[drvNum])
       
   658 				continue;   //-- skip unexisting drive
       
   659 
       
   660 			if (aFs.Drive(driveInfo, drvNum) == KErrNone)
       
   661 				{
       
   662 				if(driveInfo.iMediaAtt&KMediaAttPageable)
       
   663 					{
       
   664 					TBool readOnly = driveInfo.iMediaAtt & KMediaAttWriteProtected;		// skip ROFS partitions
       
   665 					if(!readOnly && (driveInfo.iType != EMediaHardDisk))
       
   666 						{
       
   667 						if ((drvNum==DriveNumber) || (DriveNumber<0))		// only test if running on this drive
       
   668 							{
       
   669 							return (drvNum);
       
   670 							}
       
   671 						}
       
   672 					}
       
   673 				}
       
   674 			}
       
   675 		}
       
   676 	return (-1);
       
   677 	}
       
   678 
       
   679 //
       
   680 // FindMMCDriveNumber
       
   681 // 
       
   682 // Find the first read write drive.
       
   683 //
       
   684 
       
   685 TInt FindMMCDriveNumber(RFs& aFs)
       
   686 	{
       
   687 	TDriveInfo driveInfo;
       
   688 	for (TInt drvNum=0; drvNum<KMaxDrives; ++drvNum)
       
   689 		{
       
   690 		TInt r = aFs.Drive(driveInfo, drvNum);
       
   691 		if (r >= 0)
       
   692 			{
       
   693 			if (driveInfo.iType == EMediaHardDisk)
       
   694 				return (drvNum);
       
   695 			}
       
   696 		}
       
   697 	return -1;
       
   698 	}
       
   699 
       
   700 
       
   701 //
       
   702 // PageLdrRLibrary::TestLoadLibrary
       
   703 //
       
   704 // Load a library and initialise information about that library
       
   705 //
       
   706 
       
   707 TInt PageLdrRLibrary::TestLoadLibrary(const TDesC&           aFileName,
       
   708 									  TInt					 aThreadIndex,
       
   709 									  RMsgQueue<TMessageBuf> *aMsgQueue = NULL, 
       
   710 									  TMessageBuf           *aBuffer = NULL,
       
   711 									  RSemaphore			*aTheSem = NULL)
       
   712 	{
       
   713 	TInt retVal = KErrNone;
       
   714 	if (TestingLowMem)
       
   715 		{
       
   716 		TBool whinged = EFalse;
       
   717 		TInt initialFreeRam = 0;
       
   718 		TInt freeRam = 0;
       
   719 
       
   720 		while (1)
       
   721 			{
       
   722 			initialFreeRam = FreeRam();
       
   723 			retVal = Load(aFileName);
       
   724 			freeRam = FreeRam();
       
   725 			if (retVal == KErrNoMemory)
       
   726 				{
       
   727 				if (!whinged && (freeRam > (4 * TestPageSize)))
       
   728 					{
       
   729 					whinged = ETrue;
       
   730 					DEBUG_PRINT1((_L("Load() %d pages %S\n"), (freeRam / TestPageSize), &aFileName));
       
   731 					if (TestIsDemandPaged)
       
   732 						{
       
   733 						SVMCacheInfo  tempPages;
       
   734 						UserSvr::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0);
       
   735 
       
   736 						DEBUG_PRINT1((_L("DPC : min %d max %d curr %d\n"), 
       
   737 									tempPages.iMinSize, tempPages.iMaxSize, tempPages.iCurrentSize));
       
   738 						DEBUG_PRINT1((_L("    : maxFree %d freeRam %d\n"),
       
   739 									tempPages.iMaxFreeSize, FreeRam()));
       
   740 						}
       
   741 					}
       
   742 				DEBUG_PRINT1((_L("Load() releasing some memory for %S (%d)\n"), &aFileName, retVal));
       
   743 				if (aTheSem)
       
   744 					aTheSem->Wait();
       
   745 				PagestressLdd.DoReleaseSomeRam(TEST_LM_BLOCKS_FREE);
       
   746 				if (aTheSem)
       
   747 					aTheSem->Signal();
       
   748 				}
       
   749 			else
       
   750 				{
       
   751 				if (whinged)
       
   752 					{
       
   753 					DEBUG_PRINT((_L("Load() Ok %d pages (%d) %S\n"), ((initialFreeRam - freeRam) / TestPageSize), (freeRam / TestPageSize), &aFileName));
       
   754 					}
       
   755 				break;
       
   756 				}
       
   757 			}
       
   758 		}
       
   759 	else
       
   760 		{
       
   761 		DEBUG_PRINT1((_L("Loading %S (%d)\n"), &aFileName, aThreadIndex));	 
       
   762 		retVal = Load(aFileName);
       
   763 		if (retVal != KErrNone)
       
   764 			{
       
   765 			DEBUG_PRINT1((_L("Load failed %S (%d)\n"), &aFileName, aThreadIndex));	 
       
   766 			if (TestingReaper )
       
   767 				{
       
   768 				TInt tempIndex = 0;
       
   769 				TBool whinged = EFalse;
       
   770 				while (    (   (retVal == KErrNotFound) 
       
   771 							|| (retVal == KErrPermissionDenied) 
       
   772 							|| (retVal == KErrCorrupt) 
       
   773 							|| (retVal == KErrInUse)) 
       
   774 						&& (    TestingReaperCleaningFiles
       
   775 							|| (tempIndex < TEST_REAPER_ITERS)))
       
   776 					{
       
   777 					User::After(2000000);
       
   778 					if (!whinged)
       
   779 						{
       
   780 						DEBUG_PRINT((_L("Load() retrying load for %S (%d)\n"), &aFileName, retVal));
       
   781 						whinged = ETrue;
       
   782 						}
       
   783 					retVal = Load(aFileName);
       
   784 					if (!TestingReaperCleaningFiles)
       
   785 						{
       
   786 						tempIndex ++;
       
   787 						}
       
   788 					}
       
   789 				if (retVal != KErrNone)
       
   790 					{
       
   791 					DEBUG_PRINT((_L("Load() failing for %S (%d) idx %d\n"), &aFileName, retVal, tempIndex));
       
   792 					}
       
   793 				}
       
   794 			else if (TestingDefrag)
       
   795 				{
       
   796 				TInt tempIndex = 0;
       
   797 				TBool whinged = EFalse;
       
   798 				while ((retVal == KErrGeneral) && (tempIndex < 10))
       
   799 					{
       
   800 					User::After(20000);
       
   801 					if (!whinged)
       
   802 						{
       
   803 						DEBUG_PRINT((_L("Load() retrying load for %S (%d)\n"), &aFileName, retVal));
       
   804 						whinged = ETrue;
       
   805 						}
       
   806 					retVal = Load(aFileName);
       
   807 					tempIndex ++;
       
   808 					}
       
   809 				if (retVal != KErrNone)
       
   810 					{
       
   811 					DEBUG_PRINT((_L("Load() failing for %S (%d) idx %d\n"), &aFileName, retVal, tempIndex));
       
   812 					}
       
   813 				}
       
   814 			}
       
   815 		}
       
   816 	DEBUG_PRINT1((_L("Loaded %S (%d)\n"), &aFileName, aThreadIndex));	 
       
   817 	if (retVal == KErrNone)
       
   818 		{
       
   819 		iInUse = ETrue;
       
   820 		iInitFunc = Lookup(PAGELDRTST_FUNC_Init);
       
   821 		iFunctionCountFunc = Lookup(PAGELDRTST_FUNC_FunctionCount);
       
   822 		iCallFunctionFunc = (TCallFunction)Lookup(PAGELDRTST_FUNC_CallFunction);
       
   823 		iSetCloseFunc = Lookup(PAGELDRTST_FUNC_SetClose);
       
   824 		if (   (iInitFunc != NULL)
       
   825 			&& (iFunctionCountFunc != NULL)
       
   826 			&& (iCallFunctionFunc != NULL)
       
   827 			&& (iSetCloseFunc != NULL))
       
   828 			{
       
   829 			retVal = (iInitFunc)();
       
   830 			if (retVal == KErrNone)
       
   831 				{
       
   832 				iFuncCount = (iFunctionCountFunc)();
       
   833 				if (iFuncCount != 0)
       
   834 					{
       
   835 					DEBUG_PRINT1((_L("Loaded ok %S (%d)\n"), &aFileName, aThreadIndex));	 
       
   836 					return KErrNone;	
       
   837 					}
       
   838 				retVal = KErrGeneral;
       
   839 				DEBUG_PRINT((_L("!!! bad count %S (%d)\n"), &aFileName, aThreadIndex));	 
       
   840 				}
       
   841 			else
       
   842 				{
       
   843 				DEBUG_PRINT((_L("!!! init failed %S (%d)\n"), &aFileName, aThreadIndex));	 
       
   844 				retVal = KErrGeneral;
       
   845 				}
       
   846 			}
       
   847 		else
       
   848 			{
       
   849 			DEBUG_PRINT((_L("!!! missing %S (%d)\n"), &aFileName, aThreadIndex));	 
       
   850 			retVal = KErrGeneral;
       
   851 			}
       
   852 		}
       
   853 	else
       
   854 		{
       
   855 		DEBUG_PRINT((_L("Load() failed %S %d\n"), &aFileName, retVal));
       
   856 #ifdef WANT_FS_CACHE_STATS
       
   857 		RFs			 fs;
       
   858 		if (KErrNone != fs.Connect())
       
   859 			{
       
   860 			DEBUG_PRINT(_L("TestLoadLibrary : Can't connect to the FS\n"));
       
   861 			}
       
   862 		else
       
   863 			{
       
   864 			TFileCacheStats stats1;
       
   865 			TInt drvNum = FindMMCDriveNumber(fs); 
       
   866 			controlIo(fs,drvNum, KControlIoFileCacheStats, stats1);
       
   867 		
       
   868 			DEBUG_PRINT((_L("FSC: drv %d %c free %d used %d locked %d\n"),
       
   869 						drvNum, 'a' + drvNum,
       
   870 						stats1.iFreeCount,
       
   871 						stats1.iUsedCount,
       
   872 						stats1.iLockedSegmentCount));
       
   873 			DEBUG_PRINT((_L("   : alloc %d lock %d closed %d\n"),
       
   874 						stats1.iAllocatedSegmentCount,
       
   875 						stats1.iFileCount,
       
   876 						stats1.iFilesOnClosedQueue));
       
   877 			fs.Close();
       
   878 			}
       
   879 #endif //WANT_FS_CACHE_STATS 
       
   880 
       
   881 		if (TestIsDemandPaged)
       
   882 			{
       
   883 			SVMCacheInfo  tempPages;
       
   884 			UserSvr::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0);
       
   885 
       
   886 			DEBUG_PRINT((_L("DPC : min %d max %d curr %d\n"), 
       
   887 						tempPages.iMinSize, tempPages.iMaxSize, tempPages.iCurrentSize));
       
   888 			DEBUG_PRINT((_L("    : maxFree %d freeRam %d\n"),
       
   889 						tempPages.iMaxFreeSize, FreeRam()));
       
   890 			}
       
   891 		}
       
   892 	return retVal;
       
   893 	}
       
   894 
       
   895 //
       
   896 // GetNumDlls
       
   897 //
       
   898 // Work out how many Dlls we will play with
       
   899 //
       
   900 TInt GetNumDlls()
       
   901 	{
       
   902 	TInt maxDllIndex;
       
   903 
       
   904 	switch (TestWhichMedia)
       
   905 		{
       
   906 		default:
       
   907 		case TEST_MEDIA_BASE:	
       
   908 		case TEST_MEDIA_ROM:
       
   909 			maxDllIndex = PAGELDRTST_MAX_DLLS;
       
   910 		break;
       
   911 
       
   912 		case TEST_MEDIA_ROM_BASE:
       
   913 			maxDllIndex = PAGELDRTST_MAX_DLLS * 2;
       
   914 		break;
       
   915 
       
   916 		case TEST_MEDIA_ALL:
       
   917 			maxDllIndex = PAGELDRTST_MAX_DLLS * KTestMediaCOUNT;
       
   918 		break;
       
   919 		}
       
   920 	return maxDllIndex;
       
   921 	}
       
   922 
       
   923 //
       
   924 // LoadTheLibs
       
   925 //
       
   926 // Open DLLs for use in the tests.
       
   927 //
       
   928 
       
   929 TInt LoadTheLibs(PageLdrRLibrary       *aTheLibs,
       
   930                  TInt                   aLibCount,
       
   931 				 TInt				    aThreadIndex, 
       
   932                  RMsgQueue<TMessageBuf> *aMsgQueue = NULL, 
       
   933 				 TMessageBuf           *aBuffer = NULL, 
       
   934 				 RSemaphore			   *aTheSem = NULL)
       
   935 	{
       
   936 	TBuf<128>			nameBuffer;
       
   937 	TInt				dllIndex = 0;
       
   938 	TInt				realDllIndex = 0;
       
   939 	TInt				dllOffset = -1;
       
   940 	TInt				testWhich;
       
   941 	RThread				thisThread;
       
   942 
       
   943 	memset(aTheLibs, 0, sizeof(*aTheLibs) * aLibCount);
       
   944 	for (dllIndex = 0; dllIndex < aLibCount; dllIndex ++)
       
   945 		{
       
   946 		realDllIndex = (dllIndex + aThreadIndex) % PAGELDRTST_MAX_DLLS;
       
   947 //		realDllIndex = (dllIndex) % PAGELDRTST_MAX_DLLS;
       
   948 		if (realDllIndex == 0)
       
   949 			dllOffset ++;
       
   950 
       
   951 		if ((TestWhichMedia & TEST_MEDIA_ALL) == TEST_MEDIA_ALL)
       
   952 			testWhich = (dllIndex + dllOffset) % KTestMediaCOUNT;
       
   953 		else if ((TestWhichMedia & TEST_MEDIA_ALL) == TEST_MEDIA_ROM_BASE)
       
   954 			testWhich = ((dllIndex + dllOffset) & 1) ? KTestMediaBase : KTestMediaRom;
       
   955 		else if (TestWhichMedia & TEST_MEDIA_BASE )
       
   956 			testWhich = KTestMediaBase;
       
   957 		else
       
   958 			testWhich = KTestMediaRom;
       
   959 		
       
   960 		if (!TestDllExesExist[testWhich])
       
   961 			testWhich = KTestMediaBase;
       
   962 
       
   963 		nameBuffer.Format(_L("%S%d%S"), &KDllBaseName, realDllIndex, &TestPlExtNames[testWhich]);
       
   964 		
       
   965 		DEBUG_PRINT1((_L("LoadTheLibs[%02d] - loading %S\n"), aThreadIndex, &nameBuffer));
       
   966 		TInt theErr = aTheLibs[dllIndex].TestLoadLibrary(nameBuffer, aThreadIndex, aMsgQueue, aBuffer, aTheSem);
       
   967 		if (theErr != KErrNone)
       
   968 			{
       
   969 			DEBUG_PRINT((_L("LoadTheLibs[%02d] - fail %S %d\n"), aThreadIndex, &nameBuffer, theErr));
       
   970 			return KErrGeneral;
       
   971 			}
       
   972 		else
       
   973 			{
       
   974 			DEBUG_PRINT1((_L("LoadTheLibs[%02d] - loaded %S OK\n"), aThreadIndex, &nameBuffer));
       
   975 			}
       
   976 		if (TestThreadsExit)
       
   977 			{
       
   978 			DEBUG_PRINT((_L("LoadTheLibs[%02d] - cancelled\n"), aThreadIndex));
       
   979 			return KErrCancel;
       
   980 			}
       
   981 		if (TestPrioChange)
       
   982 			{
       
   983 			TThreadPriority originalThreadPriority = thisThread.Priority();
       
   984 			DEBUG_PRINT1((_L("LoadTheLibs[%02d] before priority change\n"), aThreadIndex));
       
   985 			thisThread.SetPriority(EPriorityLess);
       
   986 			User::AfterHighRes(0);
       
   987 			thisThread.SetPriority(originalThreadPriority);
       
   988 			DEBUG_PRINT1((_L("LoadTheLibs[%02d] after priority change\n"), aThreadIndex));
       
   989 			}
       
   990 		}
       
   991 	DEBUG_PRINT((_L("LoadTheLibs[%02d] done\n"), aThreadIndex));
       
   992 	return KErrNone;
       
   993 	}
       
   994 
       
   995 //
       
   996 // CloseTheLibs
       
   997 //
       
   998 // Close the DLLs that we have previously opened
       
   999 //
       
  1000 
       
  1001 void CloseTheLibs (PageLdrRLibrary       *aTheLibs,
       
  1002                    TInt                   aLibCount)
       
  1003 	{
       
  1004 	TInt				dllIndex = 0;
       
  1005 	
       
  1006 	for (dllIndex = 0; dllIndex < aLibCount; dllIndex ++)
       
  1007 		{
       
  1008 		aTheLibs[dllIndex].CloseLibrary();
       
  1009 		}
       
  1010 	memset(aTheLibs, 0, sizeof(*aTheLibs) * aLibCount);
       
  1011 	}
       
  1012 
       
  1013 //
       
  1014 // RunThreadForward
       
  1015 //
       
  1016 // Walk through the function pointer array (forwards) calling each function
       
  1017 //
       
  1018 
       
  1019 TInt RunThreadForward(TInt				     aThreadIndex, 
       
  1020 					  PageLdrRLibrary		*aTheLibs,
       
  1021 					  TInt					 aMaxDllIndex,
       
  1022 					  RMsgQueue<TMessageBuf> *aMsgQueue = NULL, 
       
  1023 					  TMessageBuf			*aBuffer = NULL, 
       
  1024 					  RSemaphore			*aTheSem = NULL)
       
  1025 	{
       
  1026 	TInt				seed = 1;
       
  1027 	TUint32				index = 0;
       
  1028 	RThread				thisThread;
       
  1029 	PageLdrRLibrary    *pTheLibs = NULL;
       
  1030 	TInt				dllIndex = 0;
       
  1031 
       
  1032 	if (TestLoadDllHow == TEST_DLL_FUNC)
       
  1033 		{
       
  1034 		DOLOADALLOC(aMaxDllIndex, pTheLibs, aTheSem);
       
  1035 		if (pTheLibs)
       
  1036 			{
       
  1037 			TInt retVal = LoadTheLibs(pTheLibs, aMaxDllIndex, aThreadIndex, aMsgQueue, aBuffer, aTheSem);
       
  1038 			if (retVal != KErrNone)
       
  1039 				{
       
  1040 				DEBUG_PRINT((_L("Forward[%d] - load fail\n"), aThreadIndex));
       
  1041 				CloseTheLibs (pTheLibs, aMaxDllIndex);
       
  1042 				User::Free(pTheLibs);
       
  1043 				return retVal;
       
  1044 				}
       
  1045 			}
       
  1046 		else
       
  1047 			{
       
  1048 			DEBUG_PRINT((_L("Forward[%d] - alloc fail\n"), aThreadIndex));
       
  1049 			return KErrGeneral;
       
  1050 			}
       
  1051 		}
       
  1052 	else
       
  1053 		{
       
  1054 		pTheLibs = aTheLibs;
       
  1055 		}
       
  1056 	
       
  1057 	for (dllIndex = 0; dllIndex < aMaxDllIndex; dllIndex ++)
       
  1058 		{
       
  1059 		index = 0;
       
  1060 		while (index < pTheLibs[dllIndex].iFuncCount)
       
  1061 			{
       
  1062 			if (TestPrioChange)
       
  1063 				{
       
  1064 				TThreadPriority originalThreadPriority = thisThread.Priority();
       
  1065 				thisThread.SetPriority(EPriorityLess);
       
  1066 				User::AfterHighRes(0);
       
  1067 				thisThread.SetPriority(originalThreadPriority);
       
  1068 				}
       
  1069 			if (pTheLibs[dllIndex].iCallFunctionFunc)
       
  1070 				seed = pTheLibs[dllIndex].iCallFunctionFunc(index, seed, index);
       
  1071 			else
       
  1072 				DEBUG_PRINT((_L("Forward[%d] : dll %d was NULL\n"), aThreadIndex, dllIndex));
       
  1073 			index ++;
       
  1074 			if (TestThreadsExit)
       
  1075 				break;
       
  1076 			}
       
  1077 		if (TestThreadsExit)
       
  1078 			break;
       
  1079 		}
       
  1080 	if (TestLoadDllHow == TEST_DLL_FUNC)
       
  1081 		{
       
  1082 		CloseTheLibs(pTheLibs, aMaxDllIndex);
       
  1083 		User::Free(pTheLibs);
       
  1084 		}
       
  1085 	return KErrNone;
       
  1086 	}
       
  1087 
       
  1088 //
       
  1089 // RunThreadBackward
       
  1090 //
       
  1091 // Walk through the function pointer array (backwards) calling each function
       
  1092 //
       
  1093 
       
  1094 TInt RunThreadBackward(TInt				      aThreadIndex, 
       
  1095 					   PageLdrRLibrary		 *aTheLibs,
       
  1096 					   TInt					  aMaxDllIndex,
       
  1097 					   RMsgQueue<TMessageBuf> *aMsgQueue = NULL, 
       
  1098 					   TMessageBuf			 *aBuffer = NULL,
       
  1099 					   RSemaphore			 *aTheSem = NULL)
       
  1100 	{
       
  1101 	TInt				seed = 1;
       
  1102 	TUint32				index = 0;
       
  1103 	RThread				thisThread;
       
  1104 	PageLdrRLibrary    *pTheLibs = NULL;
       
  1105 	TInt				dllIndex = 0;
       
  1106 
       
  1107 	if (TestLoadDllHow == TEST_DLL_FUNC)
       
  1108 		{
       
  1109 		DOLOADALLOC(aMaxDllIndex, pTheLibs, aTheSem);
       
  1110 		if (pTheLibs)
       
  1111 			{
       
  1112 			TInt retVal = LoadTheLibs(pTheLibs, aMaxDllIndex, aThreadIndex, aMsgQueue, aBuffer, aTheSem);
       
  1113 			if (retVal != KErrNone)
       
  1114 				{
       
  1115 				DEBUG_PRINT((_L("Backward[%d] - load fail\n"), aThreadIndex));
       
  1116 				CloseTheLibs (pTheLibs, aMaxDllIndex);
       
  1117 				User::Free(pTheLibs);
       
  1118 				return retVal;
       
  1119 				}
       
  1120 			}
       
  1121 		else
       
  1122 			{
       
  1123 			DEBUG_PRINT((_L("Backward[%d] - alloc fail\n"), aThreadIndex));
       
  1124 			return KErrGeneral;
       
  1125 			}
       
  1126 		}	
       
  1127 	else
       
  1128 		{
       
  1129 		pTheLibs = aTheLibs;
       
  1130 		}
       
  1131 
       
  1132 	for (dllIndex = aMaxDllIndex - 1; dllIndex >= 0; dllIndex --)
       
  1133 		{
       
  1134 		index = pTheLibs[dllIndex].iFuncCount;
       
  1135 		while (index > 0)
       
  1136 			{
       
  1137 			if (TestPrioChange)
       
  1138 				{
       
  1139 				TThreadPriority originalThreadPriority = thisThread.Priority();
       
  1140 				thisThread.SetPriority(EPriorityLess);
       
  1141 				User::AfterHighRes(0);
       
  1142 				thisThread.SetPriority(originalThreadPriority);
       
  1143 				}
       
  1144 			if (pTheLibs[dllIndex].iCallFunctionFunc)
       
  1145 				seed = pTheLibs[dllIndex].iCallFunctionFunc(index, seed, index);
       
  1146 			else
       
  1147 				DEBUG_PRINT((_L("Backward[%d] : dll %d was NULL\n"), aThreadIndex, dllIndex));
       
  1148 			index --;
       
  1149 			if (TestThreadsExit)
       
  1150 				break;
       
  1151 			}
       
  1152 		if (TestThreadsExit)
       
  1153 			break;
       
  1154 		}
       
  1155 	if (TestLoadDllHow == TEST_DLL_FUNC)
       
  1156 		{
       
  1157 		CloseTheLibs(pTheLibs, aMaxDllIndex);
       
  1158 		User::Free(pTheLibs);
       
  1159 		}
       
  1160 	return KErrNone;
       
  1161 	}
       
  1162 
       
  1163 //
       
  1164 // RunThreadRandom
       
  1165 //
       
  1166 // Walk through the function pointer array in a random order a number of times calling each function
       
  1167 //
       
  1168 
       
  1169 TInt RunThreadRandom(TInt				    aThreadIndex, 
       
  1170 					 PageLdrRLibrary	   *aTheLibs,
       
  1171 					 TInt				    aMaxDllIndex,
       
  1172 					 RMsgQueue<TMessageBuf> *aMsgQueue = NULL, 
       
  1173 					 TMessageBuf		   *aBuffer = NULL,
       
  1174 					 RSemaphore			   *aTheSem = NULL)
       
  1175 	{
       
  1176 	TInt				seed = 1;
       
  1177 	TUint				randNum;
       
  1178 	RThread				thisThread;
       
  1179 	PageLdrRLibrary    *pTheLibs = NULL;
       
  1180 	TUint				dllIndex = 0;
       
  1181 	
       
  1182 	if (TestLoadDllHow == TEST_DLL_FUNC)
       
  1183 		{
       
  1184 		DOLOADALLOC(aMaxDllIndex, pTheLibs, aTheSem);
       
  1185 		if (pTheLibs)
       
  1186 			{
       
  1187 			TInt retVal = LoadTheLibs(pTheLibs, aMaxDllIndex, aThreadIndex, aMsgQueue, aBuffer, aTheSem);
       
  1188 			if (retVal != KErrNone)
       
  1189 				{
       
  1190 				DEBUG_PRINT((_L("Random[%d] - load fail\n"), aThreadIndex));
       
  1191 				CloseTheLibs (pTheLibs, aMaxDllIndex);
       
  1192 				User::Free(pTheLibs);
       
  1193 				return retVal;
       
  1194 				}
       
  1195 			}
       
  1196 		else
       
  1197 			{
       
  1198 			DEBUG_PRINT((_L("Random[%d] - alloc fail\n"), aThreadIndex));
       
  1199 			return KErrGeneral;
       
  1200 			}
       
  1201 		}
       
  1202 	else
       
  1203 		{
       
  1204 		pTheLibs = aTheLibs;
       
  1205 		}
       
  1206 
       
  1207 	
       
  1208 	TUint funcCount = (TUint)pTheLibs[0].iFuncCount;
       
  1209 	TInt iterCount = aMaxDllIndex * funcCount;
       
  1210 	
       
  1211 	// reduce the time for auto tests by reducing the number of cycles.
       
  1212 	if (TestIsAutomated)
       
  1213 		iterCount /= 4;
       
  1214 
       
  1215 	while (iterCount > 0)
       
  1216 		{
       
  1217 		if (TestPrioChange)
       
  1218 			{
       
  1219 			TThreadPriority originalThreadPriority = thisThread.Priority();
       
  1220 			thisThread.SetPriority(EPriorityLess);
       
  1221 			User::AfterHighRes(0);
       
  1222 			thisThread.SetPriority(originalThreadPriority);
       
  1223 			}
       
  1224 		
       
  1225 		randNum = (TUint)Math::Random();
       
  1226 		dllIndex = randNum % (TUint)aMaxDllIndex;
       
  1227 
       
  1228 		randNum %= funcCount;
       
  1229 
       
  1230 		if (   (randNum < funcCount)
       
  1231 		    && ((TInt)dllIndex < aMaxDllIndex))
       
  1232 			{
       
  1233 			if (pTheLibs[dllIndex].iCallFunctionFunc)
       
  1234 				{
       
  1235 				seed = pTheLibs[dllIndex].iCallFunctionFunc(randNum, seed, randNum);
       
  1236 				}
       
  1237 			else
       
  1238 				DEBUG_PRINT((_L("Random[%d] : dll %d was NULL\n"), aThreadIndex, dllIndex));
       
  1239 			}
       
  1240 		else
       
  1241 			{
       
  1242 			DEBUG_PRINT((_L("Random[%d] : %d ERROR dllIndex %u rand %u\n"), aThreadIndex, iterCount, dllIndex, randNum));
       
  1243 			}
       
  1244 		
       
  1245 		--iterCount;
       
  1246 		if (TestThreadsExit)
       
  1247 			break;
       
  1248 		}
       
  1249 
       
  1250 	if (TestLoadDllHow == TEST_DLL_FUNC)
       
  1251 		{
       
  1252 		CloseTheLibs(pTheLibs, aMaxDllIndex);
       
  1253 		User::Free(pTheLibs);
       
  1254 		}
       
  1255 	return KErrNone;
       
  1256 	}
       
  1257 
       
  1258 
       
  1259 //
       
  1260 // ThrashThreadLoad
       
  1261 //
       
  1262 // Load and unload the DLLs rapidly to show up a timing window in the kernel.
       
  1263 //
       
  1264 
       
  1265 TInt ThrashThreadLoad (TInt				      aThreadIndex, 
       
  1266 					   PageLdrRLibrary		 *aTheLibs,
       
  1267 					   TInt					  aMaxDllIndex,
       
  1268 					   RMsgQueue<TMessageBuf> *aMsgQueue = NULL, 
       
  1269 					   TMessageBuf			 *aBuffer = NULL,
       
  1270 					   RSemaphore			 *aTheSem = NULL)
       
  1271 	{
       
  1272 	if (TestLoadDllHow == TEST_DLL_FUNC)
       
  1273 		{
       
  1274 		PageLdrRLibrary    *pTheLibs = NULL;
       
  1275 		DOLOADALLOC(aMaxDllIndex, pTheLibs, aTheSem);
       
  1276 		if (pTheLibs)
       
  1277 			{
       
  1278 			TInt retVal = LoadTheLibs(pTheLibs, aMaxDllIndex, aThreadIndex, aMsgQueue, aBuffer, aTheSem);
       
  1279 			if (retVal != KErrNone)
       
  1280 				{
       
  1281 				DEBUG_PRINT((_L("Thrash[%d] - load fail\n"), aThreadIndex));
       
  1282 				CloseTheLibs (pTheLibs, aMaxDllIndex);
       
  1283 				User::Free(pTheLibs);
       
  1284 				return retVal;
       
  1285 				}
       
  1286 			}
       
  1287 		else
       
  1288 			{
       
  1289 			DEBUG_PRINT((_L("Thrash[%d] - alloc fail\n"), aThreadIndex));
       
  1290 			return KErrGeneral;
       
  1291 			}
       
  1292 
       
  1293 		CloseTheLibs(pTheLibs, aMaxDllIndex);
       
  1294 		User::Free(pTheLibs);
       
  1295 		}
       
  1296 	return KErrNone;
       
  1297 	}
       
  1298 
       
  1299 
       
  1300 //
       
  1301 // PerformTestThread
       
  1302 //
       
  1303 // This is the function that actually does the work.
       
  1304 // It is complicated a little because test.Printf can only be called from the first thread that calls it 
       
  1305 // so if we are using multiple threads we need to use a message queue to pass the debug info from the
       
  1306 // child threads back to the parent for the parent to then call printf.
       
  1307 //
       
  1308 //
       
  1309 
       
  1310 LOCAL_C TInt PerformTestThread(TInt					  aThreadIndex, 
       
  1311 							   RMsgQueue<TMessageBuf> *aMsgQueue = NULL, 
       
  1312 							   TMessageBuf			 *aBuffer = NULL,
       
  1313 							   RSemaphore			 *aTheSem = NULL)
       
  1314 	{
       
  1315 	TUint start = User::TickCount();
       
  1316 
       
  1317 	TFullName n(RThread().Name());
       
  1318 
       
  1319 	DEBUG_PRINT((_L("%S : thread %d Executing %S\n"), &TestNameBuffer, aThreadIndex, &n));
       
  1320 	
       
  1321 	// now select how we do the test...
       
  1322 	TInt	iterIndex;
       
  1323 
       
  1324 	PageLdrRLibrary    *pTheLibs = theGlobalLibs;
       
  1325 	TInt				maxDllIndex = GetNumDlls();
       
  1326 
       
  1327 	switch (TestLoadDllHow)
       
  1328 		{
       
  1329 		case TEST_DLL_THREAD:
       
  1330 			pTheLibs = NULL;
       
  1331 			DOLOADALLOC(maxDllIndex, pTheLibs, aTheSem);
       
  1332 			if (pTheLibs)
       
  1333 				{
       
  1334 				TInt retVal = LoadTheLibs(pTheLibs, maxDllIndex, aThreadIndex, aMsgQueue, aBuffer, aTheSem);
       
  1335 				if (retVal != KErrNone)
       
  1336 					{
       
  1337 					DEBUG_PRINT((_L("Perform[%d] - load fail\n"), aThreadIndex));
       
  1338 					CloseTheLibs (pTheLibs, maxDllIndex);
       
  1339 					User::Free(pTheLibs);
       
  1340 					return retVal;
       
  1341 					}
       
  1342 				}
       
  1343 			else
       
  1344 				{
       
  1345 				DEBUG_PRINT((_L("Perform[%d] - alloc fail\n"), aThreadIndex));
       
  1346 				return KErrGeneral;
       
  1347 				}
       
  1348 		break;
       
  1349 
       
  1350 		case TEST_DLL_GLOBAL:
       
  1351 			pTheLibs = theGlobalLibs;
       
  1352 		break;
       
  1353 
       
  1354 		case TEST_DLL_FUNC:
       
  1355 		default:
       
  1356 		// do nowt
       
  1357 		break;
       
  1358 		}
       
  1359 
       
  1360 	TInt    retVal = KErrNone;
       
  1361 	if (TEST_ALL == (TestWhichTests & TEST_ALL))
       
  1362 		{
       
  1363 		#define LOCAL_ORDER_INDEX1	6
       
  1364 		#define LOCAL_ORDER_INDEX2	3
       
  1365 		TInt	order[LOCAL_ORDER_INDEX1][LOCAL_ORDER_INDEX2] = {	{TEST_FORWARD, TEST_BACKWARD,TEST_RANDOM},
       
  1366 																	{TEST_FORWARD, TEST_RANDOM,  TEST_BACKWARD},
       
  1367 																	{TEST_BACKWARD,TEST_FORWARD, TEST_RANDOM},
       
  1368 																	{TEST_BACKWARD,TEST_RANDOM,  TEST_FORWARD},
       
  1369 																	{TEST_RANDOM,  TEST_FORWARD, TEST_BACKWARD},
       
  1370 																	{TEST_RANDOM,  TEST_BACKWARD,TEST_FORWARD}};
       
  1371 		TInt	whichOrder = 0;
       
  1372 
       
  1373 		for (iterIndex = 0; ; )
       
  1374 			{
       
  1375 			TInt    selOrder = ((aThreadIndex + 1) * (iterIndex + 1)) % LOCAL_ORDER_INDEX1;
       
  1376 			for (whichOrder = 0; whichOrder < LOCAL_ORDER_INDEX2; whichOrder ++)
       
  1377 				{
       
  1378 				switch (order[selOrder][whichOrder])
       
  1379 					{
       
  1380 						case TEST_FORWARD:
       
  1381 						DEBUG_PRINT((_L("%S : %d Iter %d.%d Forward\n"),
       
  1382 							&TestNameBuffer, aThreadIndex, iterIndex, whichOrder));
       
  1383 						retVal = RunThreadForward(aThreadIndex, pTheLibs, maxDllIndex, aMsgQueue, aBuffer, aTheSem);
       
  1384 						break;
       
  1385 
       
  1386 						case TEST_BACKWARD:
       
  1387 						DEBUG_PRINT((_L("%S : %d Iter %d.%d Backward\n"),
       
  1388 							&TestNameBuffer, aThreadIndex, iterIndex, whichOrder));
       
  1389 						retVal = RunThreadBackward(aThreadIndex, pTheLibs, maxDllIndex, aMsgQueue, aBuffer, aTheSem);
       
  1390 						break;
       
  1391 
       
  1392 						case TEST_RANDOM:
       
  1393 						DEBUG_PRINT((_L("%S : %d Iter %d.%d Random\n"),
       
  1394 							&TestNameBuffer, aThreadIndex, iterIndex, whichOrder));
       
  1395 						retVal = RunThreadRandom(aThreadIndex, pTheLibs, maxDllIndex, aMsgQueue, aBuffer, aTheSem);
       
  1396 						break;
       
  1397 						
       
  1398 						default: // this is really an error.
       
  1399 						break;
       
  1400 					}
       
  1401 				DEBUG_PRINT((_L("%S : %d Iter %d.%d finished %d\n"),
       
  1402 					&TestNameBuffer, aThreadIndex, iterIndex, whichOrder, retVal));
       
  1403 				if ((retVal == KErrCancel) && iterIndex > 0)
       
  1404 					retVal = KErrNone;
       
  1405 				if ((retVal != KErrNone) || TestThreadsExit)
       
  1406 					break;
       
  1407 				}
       
  1408 			if ((retVal != KErrNone) || TestThreadsExit)
       
  1409 				break;
       
  1410 			if (++iterIndex >= TestMaxLoops)
       
  1411 				break;
       
  1412 			User::AfterHighRes(TEST_DOT_PERIOD/3*1000000);
       
  1413 			}
       
  1414 		}
       
  1415 	else
       
  1416 		{
       
  1417 		if (TestWhichTests & TEST_FORWARD)
       
  1418 			{
       
  1419 			for (iterIndex = 0; ; )
       
  1420 				{
       
  1421 				DEBUG_PRINT((_L("%S : %d Iter %d Forward\n"), &TestNameBuffer, aThreadIndex, iterIndex));
       
  1422 				retVal = RunThreadForward(aThreadIndex, pTheLibs, maxDllIndex, aMsgQueue, aBuffer, aTheSem);
       
  1423 				DEBUG_PRINT((_L("%S : %d Iter %d finished %d\n"), &TestNameBuffer, aThreadIndex, iterIndex, retVal));
       
  1424 				if ((retVal == KErrCancel) && iterIndex > 0)
       
  1425 					retVal = KErrNone;
       
  1426 				if ((retVal != KErrNone) || TestThreadsExit)
       
  1427 					break;
       
  1428 				if (++iterIndex >= TestMaxLoops)
       
  1429 					break;
       
  1430 				User::AfterHighRes(TEST_DOT_PERIOD/3*1000000);
       
  1431 				}
       
  1432 			}
       
  1433 			
       
  1434 		if (TestWhichTests & TEST_BACKWARD)
       
  1435 			{
       
  1436 			for (iterIndex = 0; ; )
       
  1437 				{
       
  1438 				DEBUG_PRINT((_L("%S : %d Iter %d Backward\n"), &TestNameBuffer, aThreadIndex, iterIndex));
       
  1439 				retVal = RunThreadBackward(aThreadIndex, pTheLibs, maxDllIndex, aMsgQueue, aBuffer, aTheSem);
       
  1440 				DEBUG_PRINT((_L("%S : %d Iter %d finished %d\n"), &TestNameBuffer, aThreadIndex, iterIndex, retVal));
       
  1441 				if ((retVal == KErrCancel) && iterIndex > 0)
       
  1442 					retVal = KErrNone;
       
  1443 				if ((retVal != KErrNone) || TestThreadsExit)
       
  1444 					break;
       
  1445 				if (++iterIndex >= TestMaxLoops)
       
  1446 					break;
       
  1447 				User::AfterHighRes(TEST_DOT_PERIOD/3*1000000);
       
  1448 				}
       
  1449 			}
       
  1450 
       
  1451 		if (TestWhichTests & TEST_RANDOM)
       
  1452 			{
       
  1453 			for (iterIndex = 0; ; )
       
  1454 				{
       
  1455 				DEBUG_PRINT((_L("%S : %d Iter %d Random\n"), &TestNameBuffer, aThreadIndex, iterIndex));
       
  1456 				retVal = RunThreadRandom(aThreadIndex, pTheLibs, maxDllIndex, aMsgQueue, aBuffer, aTheSem);
       
  1457 				DEBUG_PRINT((_L("%S : %d Iter %d finished %d\n"), &TestNameBuffer, aThreadIndex, iterIndex, retVal));
       
  1458 				if ((retVal == KErrCancel) && iterIndex > 0)
       
  1459 					retVal = KErrNone;
       
  1460 				if ((retVal != KErrNone) || TestThreadsExit)
       
  1461 					break;
       
  1462 				if (++iterIndex >= TestMaxLoops)
       
  1463 					break;
       
  1464 				User::AfterHighRes(TEST_DOT_PERIOD/3*1000000);
       
  1465 				}
       
  1466 			}
       
  1467 		
       
  1468 		if (TestWhichTests & TEST_THRASH)
       
  1469 			{
       
  1470 			for (iterIndex = 0; ; )
       
  1471 				{
       
  1472 				DEBUG_PRINT((_L("%S : %d Iter %d Thrash Load\n"), &TestNameBuffer, aThreadIndex, iterIndex));
       
  1473 				retVal = ThrashThreadLoad(aThreadIndex, pTheLibs, maxDllIndex, aMsgQueue, aBuffer, aTheSem);
       
  1474 				DEBUG_PRINT((_L("%S : %d Iter %d finished %d\n"), &TestNameBuffer, aThreadIndex, iterIndex, retVal));
       
  1475 				if ((retVal == KErrCancel) && iterIndex > 0)
       
  1476 					retVal = KErrNone;
       
  1477 				if ((retVal != KErrNone) || TestThreadsExit)
       
  1478 					break;
       
  1479 				if (++iterIndex >= TestMaxLoops)
       
  1480 					break;
       
  1481 				User::AfterHighRes(TEST_DOT_PERIOD/3*1000000);
       
  1482 				}
       
  1483 			}
       
  1484 		}
       
  1485 
       
  1486 	if (TestLoadDllHow == TEST_DLL_THREAD)
       
  1487 		{
       
  1488 		CloseTheLibs(pTheLibs, maxDllIndex);
       
  1489 		User::Free(pTheLibs);
       
  1490 		}
       
  1491 
       
  1492 	DEBUG_PRINT((_L("%S : thread %d Exit (tick %u)\n"), &TestNameBuffer, aThreadIndex, User::TickCount() - start));
       
  1493 	return retVal;
       
  1494 	}
       
  1495 
       
  1496 
       
  1497 //
       
  1498 // MultipleTestThread
       
  1499 //
       
  1500 // Thread function, one created for each thread in a multiple thread test.
       
  1501 //
       
  1502 
       
  1503 LOCAL_C TInt MultipleTestThread(TAny* aUseTb)
       
  1504 	{
       
  1505 	TInt			ret;
       
  1506 	TMessageBuf		localBuffer;
       
  1507 
       
  1508 	if (TestInterleave)	
       
  1509 		{
       
  1510 		RThread				thisThread;
       
  1511 		thisThread.SetPriority((TThreadPriority) TEST_INTERLEAVE_PRIO);
       
  1512 		}
       
  1513 
       
  1514 	ret = PerformTestThread((TInt) aUseTb, &TestMsgQueue, &localBuffer, &TestMultiSem);
       
  1515 	if (!TestingChunks)
       
  1516 		{
       
  1517 		if (ret != KErrNone) 
       
  1518 			User::Panic(_L("LOAD"), KErrGeneral);
       
  1519 		}
       
  1520 	return KErrNone;
       
  1521 	}
       
  1522 
       
  1523 //
       
  1524 // StartExe
       
  1525 //
       
  1526 // Start an executable.
       
  1527 //
       
  1528 
       
  1529 TInt StartExe(RProcess& aTheProcesses, TRequestStatus* aPrStatus, TInt aIndex, TBool aLoadSelf, TBool aLowMem, RSemaphore *pTheSem = NULL)
       
  1530 	{
       
  1531 	TBuf<256>		buffer;
       
  1532 	TInt			testWhich = KTestMediaRom;
       
  1533 	//y_LIT(KTestDebug, "debug");
       
  1534 	_LIT(KTestSilent, "silent");
       
  1535 
       
  1536 	if ((TestWhichMedia & TEST_MEDIA_ALL) == TEST_MEDIA_ALL)
       
  1537 		testWhich = aIndex % KTestMediaCOUNT;
       
  1538 	else if ((TestWhichMedia & TEST_MEDIA_ALL) == TEST_MEDIA_ROM_BASE)
       
  1539 		testWhich = (aIndex & 1) ? KTestMediaBase : KTestMediaRom;
       
  1540 	else if (TestWhichMedia & TEST_MEDIA_BASE )
       
  1541 		testWhich = KTestMediaBase;
       
  1542 	else
       
  1543 		testWhich = KTestMediaRom;
       
  1544 
       
  1545 	if (!TestDllExesExist[testWhich])
       
  1546 		testWhich = KTestMediaBase;
       
  1547 
       
  1548 	buffer.Zero();
       
  1549 	TInt ret;
       
  1550 	if (aLoadSelf)
       
  1551 		{
       
  1552 		buffer.Format(_L("single random dll %S iters %d inst %d"),
       
  1553 			/* TestDebug ? &KTestDebug : */ &KTestSilent, TestMaxLoops, aIndex);
       
  1554 		if (TestExtremeChunks)
       
  1555 			buffer.Append(_L(" echunks"));
       
  1556 		else if (TestChunksPlus)
       
  1557 			buffer.Append(_L(" chunks prio"));
       
  1558 		if (TestChunkData == EFalse)
       
  1559 			buffer.Append(_L(" nochunkdata"));
       
  1560 		DBGS_PRINT((_L("%S : Starting Process %d %S %S\n"),
       
  1561 			&TestNameBuffer, aIndex, &TestPlExeNames[testWhich], &buffer));
       
  1562 		DOTEST1(ret,aTheProcesses.Create(TestPlExeNames[testWhich],buffer),KErrNone, KErrNoMemory);
       
  1563 		}
       
  1564 	else
       
  1565 		{
       
  1566 		buffer.Format(_L("single random %S iters %d inst %d"),
       
  1567 			/* TestDebug ? &KTestDebug : */ &KTestSilent, TestMaxLoops, aIndex);
       
  1568 		DBGS_PRINT((_L("%S : Starting Process %d %S %S\n"),
       
  1569 			&TestNameBuffer, aIndex, &TestPsExeNames[testWhich], &buffer));
       
  1570 		DOTEST1(ret,aTheProcesses.Create(TestPsExeNames[testWhich],buffer),KErrNone, KErrNoMemory);
       
  1571 		}
       
  1572 	if (ret == KErrNone)
       
  1573 		{
       
  1574 		if(aPrStatus)
       
  1575 			{
       
  1576 			aTheProcesses.Logon(*aPrStatus);
       
  1577 			RUNTEST1(*aPrStatus == KRequestPending);	
       
  1578 			}
       
  1579 		aTheProcesses.Resume();
       
  1580 		}
       
  1581 	return ret;
       
  1582 	}
       
  1583 
       
  1584 //
       
  1585 // PerformRomAndFileSystemAccessThread
       
  1586 // 
       
  1587 // Access the rom and dump it out to one of the writeable partitions...
       
  1588 // really just to make the media server a little busy during the test.
       
  1589 //
       
  1590 TInt PerformRomAndFileSystemAccessThread(TInt					aThreadId,
       
  1591 										 RMsgQueue<TMessageBuf> *aMsgQueue, 
       
  1592 										 TMessageBuf		   *aBuffer,
       
  1593 										 RSemaphore			   *aTheSem,
       
  1594 										 TBool					aLowMem)
       
  1595 	{
       
  1596 	RThread		 thisThread;
       
  1597 	TUint		 maxBytes = KMaxTUint;
       
  1598 	TInt		 startTime = User::TickCount();
       
  1599 	RSemaphore	*pTheSem = aTheSem;
       
  1600 	RFs fs;
       
  1601 	RFile file;
       
  1602 
       
  1603 	if (KErrNone != fs.Connect())
       
  1604 		{
       
  1605 		DEBUG_PRINT(_L("PerformRomAndFileSystemAccessThread : Can't connect to the FS\n"));
       
  1606 		return KErrGeneral;
       
  1607 		}
       
  1608 
       
  1609 	// get info about the ROM...
       
  1610 	TRomHeader* romHeader = (TRomHeader*)UserSvr::RomHeaderAddress();
       
  1611 	TUint8* start;
       
  1612 	TUint8* end;
       
  1613 	if(romHeader->iPageableRomStart)
       
  1614 		{
       
  1615 		start = (TUint8*)romHeader + romHeader->iPageableRomStart;
       
  1616 		end = start + romHeader->iPageableRomSize;
       
  1617 		}
       
  1618 	else
       
  1619 		{
       
  1620 		start = (TUint8*)romHeader;
       
  1621 		end = start + romHeader->iUncompressedSize;
       
  1622 		}
       
  1623 	if (end <= start)
       
  1624 		return KErrGeneral;
       
  1625 
       
  1626 	// read all ROM pages in a random order...and write out to file in ROFs
       
  1627 	TInt pageSize = 0;
       
  1628 	UserSvr::HalFunction(EHalGroupKernel,EKernelHalPageSizeInBytes,&pageSize,0);
       
  1629 
       
  1630 	TUint size = end - start - pageSize;
       
  1631 	if(size > maxBytes)
       
  1632 		size = maxBytes;
       
  1633 
       
  1634 	TUint32 random = 1 + aThreadId;
       
  1635 	TPtrC8  sourceData;
       
  1636 	TUint8* theAddr;
       
  1637 	HBufC8* checkData;
       
  1638 
       
  1639 	DOTEST((checkData = HBufC8::New(pageSize + 10)),
       
  1640 	       (checkData != NULL));
       
  1641 	
       
  1642 	if (!checkData)
       
  1643 		{
       
  1644 		DEBUG_PRINT((_L("RomAndFSThread %S : failed to alloc read buffer\n"), &TestNameBuffer));
       
  1645 		}
       
  1646 
       
  1647 	TInt		drvNum = (TestBootedFromMmc || TestOnlyFromMmc) ? FindMMCDriveNumber(fs) : FindFsNANDDrive(fs);
       
  1648 	TBuf<32>	filename;
       
  1649 	
       
  1650 	filename.Format(_L("?:\\Pageldrtst%d.tmp"), aThreadId);
       
  1651 	if (drvNum >= 0)
       
  1652 		{
       
  1653 		DEBUG_PRINT((_L("%S : Filename %S\n"), &TestNameBuffer, &filename));
       
  1654 		}
       
  1655 	else
       
  1656 		{
       
  1657 		DEBUG_PRINT((_L("RomAndFSThread : error getting drive num\n")));
       
  1658 		drvNum = 3; //make it 'd' by default.
       
  1659 		}
       
  1660 	filename[0] = 'a' + drvNum;
       
  1661 
       
  1662 #ifdef WANT_FS_CACHE_STATS 
       
  1663 	TInt allocatedSegmentCount = 0;
       
  1664 	TInt filesOnClosedQueue = 0;
       
  1665 #endif
       
  1666 	TInt ret;
       
  1667 	while(1)
       
  1668 		{
       
  1669 		for(TInt i = size / (pageSize); i>0; --i)
       
  1670 			{
       
  1671 			DEBUG_PRINT1((_L("%S : Opening the file\n"), &TestNameBuffer));
       
  1672 			DOTEST((ret = file.Replace(fs, filename, EFileWrite)),
       
  1673 				   (KErrNone == ret));
       
  1674 
       
  1675 			random = random * 69069 + 1;
       
  1676 			theAddr = (TUint8 *)(start + ((TInt64(random) * TInt64(size - pageSize)) >> 32));
       
  1677 			sourceData.Set(theAddr,pageSize);
       
  1678 			DEBUG_PRINT1((_L("%S : Writing the file\n"), &TestNameBuffer));
       
  1679 			ret = file.Write(sourceData);
       
  1680 			if (ret != KErrNone)
       
  1681 				{
       
  1682 				DEBUG_PRINT((_L("%S : Write returned error %d\n"), &TestNameBuffer, ret));
       
  1683 				}
       
  1684 			DEBUG_PRINT1((_L("%S : Closing the file\n"), &TestNameBuffer));
       
  1685 			file.Close();
       
  1686 			
       
  1687 			if (checkData)
       
  1688 				{
       
  1689 				TPtr8  theBuf = checkData->Des();
       
  1690 
       
  1691 #ifdef WANT_FS_CACHE_STATS 
       
  1692 				// Page cache
       
  1693 				TFileCacheStats stats1;
       
  1694 				TFileCacheStats stats2;
       
  1695 				ret = controlIo(fs,drvNum, KControlIoFileCacheStats, stats1);
       
  1696 				if ((ret != KErrNone) && (ret != KErrNotSupported))
       
  1697 					{
       
  1698 					DEBUG_PRINT((_L("%S : KControlIoFileCacheStats 1 failed %d\n"), &TestNameBuffer, ret));
       
  1699 					}
       
  1700 
       
  1701 				if (aThreadId & 1)
       
  1702 					{
       
  1703 					// flush closed files queue
       
  1704 					ret = fs.ControlIo(drvNum, KControlIoFlushClosedFiles);
       
  1705 					if (ret != KErrNone)
       
  1706 						{
       
  1707 						DEBUG_PRINT((_L("%S : KControlIoFlushClosedFiles failed %d\n"), &TestNameBuffer, ret));
       
  1708 						}
       
  1709 					}
       
  1710 				else
       
  1711 #endif //WANT_FS_CACHE_STATS 
       
  1712 					{
       
  1713 					// rename file to make sure it has cleared the cache.				
       
  1714 					TBuf<32>	newname;
       
  1715 					newname.Format(_L("d:\\Pageldrtst%d.temp"), aThreadId);
       
  1716 					if (drvNum >= 0)
       
  1717 						{
       
  1718 						newname[0] = 'a' + drvNum;
       
  1719 						}
       
  1720 					fs.Rename(filename, newname);
       
  1721 					filename = newname;
       
  1722 					}
       
  1723 #ifdef WANT_FS_CACHE_STATS 
       
  1724 				ret = controlIo(fs,drvNum, KControlIoFileCacheStats, stats2);
       
  1725 				if (ret != KErrNone && ret != KErrNotSupported)
       
  1726 					{
       
  1727 					DEBUG_PRINT((_L("%S : KControlIoFileCacheStats2 failed %d\n"), &TestNameBuffer, ret));
       
  1728 					}
       
  1729 
       
  1730 				allocatedSegmentCount = (allocatedSegmentCount > stats1.iAllocatedSegmentCount) ? allocatedSegmentCount : stats1.iAllocatedSegmentCount;
       
  1731 				filesOnClosedQueue = (filesOnClosedQueue > stats1.iFilesOnClosedQueue) ? filesOnClosedQueue : stats1.iFilesOnClosedQueue;
       
  1732 #endif //WANT_FS_CACHE_STATS 
       
  1733 
       
  1734 				DOTEST((ret = file.Open(fs, filename, EFileRead)),
       
  1735 					   (KErrNone == ret));
       
  1736 				// now read back the page that we wrote and compare with the source.
       
  1737 				ret = file.Read(0, theBuf, pageSize);
       
  1738 				if (ret == KErrNone)
       
  1739 					{		
       
  1740 					ret = sourceData.Compare(theBuf);
       
  1741 					if (ret != 0)
       
  1742 						{
       
  1743 						DEBUG_PRINT((_L("%S : read compare error %d\n"), &TestNameBuffer, ret));
       
  1744 						}
       
  1745 					}
       
  1746 				else
       
  1747 					{
       
  1748 					DEBUG_PRINT((_L("%S : failed read compare, error %d\n"), &TestNameBuffer, ret));
       
  1749 					}
       
  1750 				file.Close();
       
  1751 				}
       
  1752 			DEBUG_PRINT1((_L("%S : Deleting the file\n"), &TestNameBuffer));
       
  1753 			ret = fs.Delete(filename);
       
  1754 			if (KErrNone != ret)
       
  1755 				{
       
  1756 				DEBUG_PRINT((_L("%S [%d] Delete %S Failed %d!\n"), &TestNameBuffer, aThreadId, &filename, ret));
       
  1757 				}
       
  1758 		
       
  1759 			if (TestPrioChange)
       
  1760 				{
       
  1761 				TThreadPriority originalThreadPriority = thisThread.Priority();
       
  1762 				DEBUG_PRINT1((_L("%S [%d] media thread before priority change, stop = %d\n"), &TestNameBuffer, aThreadId, TestStopMedia));
       
  1763 				thisThread.SetPriority(EPriorityLess);
       
  1764 				User::AfterHighRes(0);
       
  1765 				thisThread.SetPriority(originalThreadPriority);
       
  1766 				DEBUG_PRINT1((_L("%S [%d] media thread after priority change, stop = %d\n"), &TestNameBuffer, aThreadId, TestStopMedia));
       
  1767 				}
       
  1768 			if (TestStopMedia)
       
  1769 				break;
       
  1770 			}
       
  1771 		if (TestStopMedia)
       
  1772 			break;
       
  1773 		}
       
  1774 
       
  1775 #ifdef WANT_FS_CACHE_STATS 
       
  1776 	DEBUG_PRINT((_L("%S : [%d] allocPageCount %d filesClosedQueue %d \n"),&TestNameBuffer, aThreadId,allocatedSegmentCount,filesOnClosedQueue));
       
  1777 #endif //WANT_FS_CACHE_STATS 
       
  1778 
       
  1779 	if (checkData)
       
  1780 		{
       
  1781 		delete checkData;
       
  1782 		}
       
  1783 	fs.Close();
       
  1784 	DEBUG_PRINT1((_L("Done in %d ticks\n"), User::TickCount() - startTime));
       
  1785 	return KErrNone;
       
  1786 	}
       
  1787 
       
  1788 //
       
  1789 // PerformFileSystemAccessThread
       
  1790 // 
       
  1791 // Access the rom and dump it out to one of the writeable partitions...
       
  1792 // really just to make the media server a little busy during the test.
       
  1793 //
       
  1794 TInt PerformFileSystemAccessThread(TInt					    aThreadId,
       
  1795 								   RMsgQueue<TMessageBuf>   *aMsgQueue, 
       
  1796 								   TMessageBuf	           *aBuffer,
       
  1797 								   RSemaphore			   *aTheSem,
       
  1798 								   TBool					aLowMem)
       
  1799 	{
       
  1800 	RThread		 thisThread;
       
  1801 	TInt		 startTime = User::TickCount();
       
  1802 	RSemaphore	*pTheSem = aTheSem;
       
  1803 	RFs			 fs;
       
  1804 	RFile		 file;
       
  1805 	if (KErrNone != fs.Connect())
       
  1806 		{
       
  1807 		DEBUG_PRINT(_L("PerformFileSystemAccessThread : Can't connect to the FS\n"));
       
  1808 		return KErrGeneral;
       
  1809 		}
       
  1810 
       
  1811 	// read all ROM pages in a random order...and write out to file in ROFs
       
  1812 	TInt pageSize = 0;
       
  1813 	UserSvr::HalFunction(EHalGroupKernel,EKernelHalPageSizeInBytes,&pageSize,0);
       
  1814 
       
  1815 	HBufC8* checkData;
       
  1816 	HBufC8* sourceData;
       
  1817 	TUint32 random = 1 + aThreadId;
       
  1818 	TInt	dataSize = pageSize + (pageSize / 2);
       
  1819 	
       
  1820 	DOTEST((sourceData = HBufC8::New(dataSize)),
       
  1821 	       (sourceData != NULL));
       
  1822 	if (!sourceData)
       
  1823 		{
       
  1824 		DEBUG_PRINT((_L("RomAndFSThread %S : failed to alloc read buffer\n"), &TestNameBuffer));
       
  1825 		fs.Close();
       
  1826 		return KErrGeneral;
       
  1827 		}
       
  1828 
       
  1829 	DOTEST((checkData = HBufC8::New(dataSize)),
       
  1830 	       (checkData != NULL));
       
  1831 	if (!checkData)
       
  1832 		{
       
  1833 		DEBUG_PRINT((_L("RomAndFSThread %S : failed to alloc read buffer\n"), &TestNameBuffer));
       
  1834 		}
       
  1835 
       
  1836 	TInt		drvNum = (TestBootedFromMmc || TestOnlyFromMmc) ? FindMMCDriveNumber(fs) : FindFsNANDDrive(fs);
       
  1837 	TBuf<32>	filename;
       
  1838 	
       
  1839 	if (drvNum < 0)
       
  1840 		{
       
  1841 		drvNum = 3; //make it 'd' by default.
       
  1842 		DEBUG_PRINT((_L("FSAccessThread : error getting drive num\n")));
       
  1843 		}
       
  1844 
       
  1845 #ifdef WANT_FS_CACHE_STATS 
       
  1846 	TInt allocatedSegmentCount = 0;
       
  1847 	TInt filesOnClosedQueue = 0;
       
  1848 #endif
       
  1849 	TInt fileIndex;
       
  1850 	TInt ret;
       
  1851 
       
  1852 	TPtr8  pBuf = sourceData->Des();
       
  1853 	
       
  1854 	while (1)
       
  1855 		{
       
  1856 		TUint32 randomStart = random;
       
  1857 		// write the file
       
  1858 		for (fileIndex = 0; fileIndex < TEST_NUM_FILES; fileIndex ++)
       
  1859 			{
       
  1860 			filename.Format(_L("%c:\\pldrtst%d_%d.tmp"), 'a' + drvNum, aThreadId, fileIndex);
       
  1861 
       
  1862 			DEBUG_PRINT1((_L("%S : Opening the file\n"), &TestNameBuffer));
       
  1863 
       
  1864 			DOTEST ((ret = file.Replace(fs, filename, EFileWrite)),
       
  1865 				   (KErrNone == ret));
       
  1866 
       
  1867 			pBuf.Zero();			
       
  1868 			if (fileIndex & 1)
       
  1869 				{
       
  1870 				TInt fillSize = dataSize / sizeof(TUint32);
       
  1871 				while (fillSize > 0)
       
  1872 					{
       
  1873 					random = random * 69069 + 1;
       
  1874 					pBuf.Append((const TUint8 *) &random, sizeof(random));
       
  1875 					fillSize --;
       
  1876 					}
       
  1877 				}
       
  1878 			else
       
  1879 				{
       
  1880 				pBuf.Fill('x',dataSize);
       
  1881 				}
       
  1882 		
       
  1883 
       
  1884 			DEBUG_PRINT1((_L("%S : Writing the file\n"), &TestNameBuffer));
       
  1885 			ret = file.Write(sourceData->Des());
       
  1886 			if (ret != KErrNone)
       
  1887 				{
       
  1888 				DEBUG_PRINT((_L("%S : Write returned error %d\n"), &TestNameBuffer, ret));
       
  1889 				}
       
  1890 			DEBUG_PRINT1((_L("%S : Closing the file\n"), &TestNameBuffer));
       
  1891 			file.Close();
       
  1892 			}
       
  1893 
       
  1894 		random = randomStart;
       
  1895 		// check the file
       
  1896 		for (fileIndex = 0; fileIndex < TEST_NUM_FILES; fileIndex ++)
       
  1897 			{
       
  1898 			filename.Format(_L("%c:\\pldrtst%d_%d.tmp"), 'a' + drvNum, aThreadId, fileIndex);
       
  1899 			
       
  1900 			if (checkData)
       
  1901 				{
       
  1902 				TPtr8  theBuf = checkData->Des();
       
  1903 
       
  1904 #ifdef WANT_FS_CACHE_STATS 
       
  1905 				// Page cache
       
  1906 				TFileCacheStats stats1;
       
  1907 				TFileCacheStats stats2;
       
  1908 				ret = controlIo(fs,drvNum, KControlIoFileCacheStats, stats1);
       
  1909 				if ((ret != KErrNone) && (ret != KErrNotSupported))
       
  1910 					{
       
  1911 					DEBUG_PRINT((_L("%S : KControlIoFileCacheStats 1 failed %d\n"), &TestNameBuffer, ret));
       
  1912 					}
       
  1913 
       
  1914 				if (aThreadId & 1)
       
  1915 					{
       
  1916 					// flush closed files queue
       
  1917 					ret = fs.ControlIo(drvNum, KControlIoFlushClosedFiles);
       
  1918 					if (ret != KErrNone)
       
  1919 						{
       
  1920 						DEBUG_PRINT((_L("%S : KControlIoFlushClosedFiles failed %d\n"), &TestNameBuffer, ret));
       
  1921 						}
       
  1922 					}
       
  1923 				else
       
  1924 #endif //WANT_FS_CACHE_STATS 
       
  1925 					{
       
  1926 					// rename file to make sure it has cleared the cache.				
       
  1927 					TBuf<32>	newname;
       
  1928 					newname.Format(_L("%c:\\pldrtst%d_%d.temp"), 'a' + drvNum, aThreadId, fileIndex);
       
  1929 					fs.Rename(filename, newname);
       
  1930 					filename = newname;
       
  1931 					}
       
  1932 #ifdef WANT_FS_CACHE_STATS 
       
  1933 				ret = controlIo(fs,drvNum, KControlIoFileCacheStats, stats2);
       
  1934 				if (ret != KErrNone && ret != KErrNotSupported)
       
  1935 					{
       
  1936 					DEBUG_PRINT((_L("%S : KControlIoFileCacheStats2 failed %d\n"), &TestNameBuffer, ret));
       
  1937 					}
       
  1938 				allocatedSegmentCount = (allocatedSegmentCount > stats1.iAllocatedSegmentCount) ? allocatedSegmentCount : stats1.iAllocatedSegmentCount;
       
  1939 				filesOnClosedQueue = (filesOnClosedQueue > stats1.iFilesOnClosedQueue) ? filesOnClosedQueue : stats1.iFilesOnClosedQueue;
       
  1940 #endif //WANT_FS_CACHE_STATS 
       
  1941 
       
  1942 				DOTEST((ret = file.Open(fs, filename, EFileRead)),
       
  1943 					   (KErrNone == ret));
       
  1944 				// now read back the page that we wrote and compare with the source.
       
  1945 				ret = file.Read(0, theBuf, dataSize);
       
  1946 				if (ret == KErrNone)
       
  1947 					{
       
  1948 					pBuf.Zero();			
       
  1949 					if (fileIndex & 1)
       
  1950 						{
       
  1951 						TInt fillSize = dataSize / sizeof(TUint32);
       
  1952 						while (fillSize > 0)
       
  1953 							{
       
  1954 							random = random * 69069 + 1;
       
  1955 							pBuf.Append((const TUint8 *) &random, sizeof(random));
       
  1956 							fillSize --;
       
  1957 							}
       
  1958 						}
       
  1959 					else
       
  1960 						{
       
  1961 						pBuf.Fill('x',dataSize);
       
  1962 						}
       
  1963 
       
  1964 					ret = sourceData->Des().Compare(theBuf);
       
  1965 					if (ret != 0)
       
  1966 						{
       
  1967 						DEBUG_PRINT((_L("%S :compare error %S %d\n"), &TestNameBuffer, &filename, ret));
       
  1968 						}
       
  1969 					}
       
  1970 				else
       
  1971 					{
       
  1972 					DEBUG_PRINT((_L("%S : failed read compare, error %d\n"), &TestNameBuffer, ret));
       
  1973 					}
       
  1974 				file.Close();
       
  1975 				}
       
  1976 			DEBUG_PRINT1((_L("%S : Deleting the file\n"), &TestNameBuffer));
       
  1977 			ret = fs.Delete(filename);
       
  1978 			if (KErrNone != ret)
       
  1979 				{
       
  1980 				DEBUG_PRINT((_L("%S [%d] Delete %S Failed %d!\n"), &TestNameBuffer, aThreadId, &filename, ret));
       
  1981 				}
       
  1982 			if (TestPrioChange)
       
  1983 				{
       
  1984 				TThreadPriority originalThreadPriority = thisThread.Priority();
       
  1985 				thisThread.SetPriority(EPriorityLess);
       
  1986 				User::AfterHighRes(0);
       
  1987 				thisThread.SetPriority(originalThreadPriority);
       
  1988 				}
       
  1989 			if (TestStopMedia)
       
  1990 				break;
       
  1991 			}
       
  1992 		if (TestStopMedia)
       
  1993 			break;
       
  1994 		}
       
  1995 #ifdef WANT_FS_CACHE_STATS 
       
  1996 	DEBUG_PRINT((_L("%S : [%d] allocPageCount %d filesClosedQueue %d \n"),&TestNameBuffer, aThreadId,allocatedSegmentCount,filesOnClosedQueue));
       
  1997 #endif //WANT_FS_CACHE_STATS 
       
  1998 
       
  1999 	if (checkData)
       
  2000 		{
       
  2001 		delete checkData;
       
  2002 		}
       
  2003 	delete sourceData;
       
  2004 	fs.Close();
       
  2005 	DEBUG_PRINT1((_L("Done in %d ticks\n"), User::TickCount() - startTime));
       
  2006 	return KErrNone;
       
  2007 	}
       
  2008 
       
  2009 //
       
  2010 // PerformRomAndFileSystemAccess
       
  2011 //
       
  2012 // Thread function, kicks off the file system access.
       
  2013 //
       
  2014 
       
  2015 LOCAL_C TInt PerformRomAndFileSystemAccess(TAny* aParam)
       
  2016 	{
       
  2017 	TMessageBuf			localBuffer;
       
  2018 	TInt				threadId = (TInt) aParam;
       
  2019 	TInt				retVal = KErrGeneral;
       
  2020 
       
  2021 	if (TestInterleave)	
       
  2022 		{
       
  2023 		RThread				thisThread;
       
  2024 		thisThread.SetPriority((TThreadPriority) TEST_INTERLEAVE_PRIO);
       
  2025 		}
       
  2026 
       
  2027 	switch (TestMediaAccess)
       
  2028 		{
       
  2029 		default:
       
  2030 		break;
       
  2031 		
       
  2032 		case KTestMediaAccessBasic:
       
  2033 		case KTestMediaAccessMultipleThreads:
       
  2034 			retVal = PerformRomAndFileSystemAccessThread(threadId, &TestMsgQueue, &localBuffer, &TestMultiSem, TestingLowMem);	
       
  2035 		break;
       
  2036 				
       
  2037 		case KTestMediaAccessMultiplePattern:
       
  2038 			retVal = PerformFileSystemAccessThread(threadId, &TestMsgQueue, &localBuffer, &TestMultiSem, TestingLowMem);
       
  2039 		break;
       
  2040 
       
  2041 		case KTestMediaAccessMixed:
       
  2042 			if (threadId < ((TestMultipleThreadCount + 1) / 2))
       
  2043 				retVal = PerformRomAndFileSystemAccessThread(threadId, &TestMsgQueue, &localBuffer, &TestMultiSem, TestingLowMem);	
       
  2044 			else
       
  2045 				retVal = PerformFileSystemAccessThread(threadId, &TestMsgQueue, &localBuffer, &TestMultiSem, TestingLowMem);
       
  2046 		break;
       
  2047 		}
       
  2048 	return retVal;
       
  2049 	}
       
  2050 
       
  2051 
       
  2052 //
       
  2053 // DisplayTestBanner
       
  2054 // 
       
  2055 // Output a header showing the test parameters.
       
  2056 //
       
  2057 
       
  2058 void DisplayTestBanner(TBool aMultiple)
       
  2059 	{
       
  2060 	DBGS_PRINT((_L("%S : what = %S%S%S(0x%x), media = %S%S%S(0x%x)\n"),
       
  2061 				aMultiple ? &KMultipleTest : &KSingleTest,
       
  2062 				TestLoading & TEST_EXE ? &KTestExe : &KTestBlank,
       
  2063  				TestLoading & TEST_DLL ? &KTestDll : &KTestBlank,
       
  2064  				TestLoading & TEST_SELF ? &KTestSelf : &KTestBlank,
       
  2065 				TestLoading,
       
  2066 				TestWhichMedia & TEST_MEDIA_BASE ? &KTestBase : &KTestBlank,
       
  2067 				TestWhichMedia & TEST_MEDIA_ROM ? &KTestRom : &KTestBlank,
       
  2068 				(TestWhichMedia & TEST_MEDIA_ALL) == TEST_MEDIA_ALL ? &KTestAll : &KTestBlank,
       
  2069 				TestWhichMedia));
       
  2070 	DBGS_PRINT((_L("         : maxLoops = %d, threads = %d, loadHow = %S (0x%x)\n"),
       
  2071 				TestMaxLoops,
       
  2072 				TestMultipleThreadCount,
       
  2073 				TestLoadDllHow == TEST_DLL_GLOBAL ? &KTestGlobal : TestLoadDllHow == TEST_DLL_THREAD ? &KTestThread : &KTestFunc, TestLoadDllHow));
       
  2074 	DBGS_PRINT((_L("         : options = %S%S%S%S%S%S, which = %S%S%S%S (0x%x)\n"),
       
  2075 				TestInterleave ? &KTestInter : &KTestBlank,
       
  2076 				TestPrioChange ? &KTestPrio: &KTestBlank,
       
  2077 				(TestMediaAccess == KTestMediaAccessNone) ? &KTestBlank : &KTestMedia,
       
  2078 				TestingLowMem ? &KTestLowMem : &KTestBlank, 
       
  2079 				TestExtremeChunks ? &KTestEChunking : TestChunksPlus ? &KTestChunkingPlus : TestingChunks ? &KTestChunking : &KTestBlank, 
       
  2080 				TestingReaper ? &KTestReaper : &KTestBlank, 
       
  2081 				TestWhichTests & TEST_THRASH ? &KTestThrash : &KTestBlank,
       
  2082 				TestWhichTests & TEST_FORWARD ? &KTestForward : &KTestBlank,
       
  2083 				TestWhichTests & TEST_BACKWARD ? &KTestBackward : &KTestBlank,
       
  2084 				TestWhichTests & TEST_RANDOM ? &KTestRandom : &KTestBlank,
       
  2085 				TestWhichTests));
       
  2086 	}
       
  2087 
       
  2088 //
       
  2089 // DoSingleTest
       
  2090 // 
       
  2091 // Perform the single thread test, spawning a number of threads.
       
  2092 //
       
  2093 
       
  2094 LOCAL_C TInt DoSingleTest(TBool aLowMem = EFalse)
       
  2095 	{
       
  2096 	TUint        start = User::TickCount();
       
  2097 	RSemaphore	*pTheSem = NULL;
       
  2098 	TInt ret = KErrNone;
       
  2099 	DisplayTestBanner(EFalse);
       
  2100 
       
  2101 	if (aLowMem)
       
  2102 		{
       
  2103 		DOTEST1(ret,TestMultiSem.CreateLocal(1),KErrNone, KErrNoMemory);
       
  2104 		pTheSem = &TestMultiSem;
       
  2105 		}
       
  2106 	if (TestLoading & TEST_EXE)
       
  2107 		{
       
  2108 		RProcess		theProcess;
       
  2109 		TRequestStatus	status;
       
  2110 		
       
  2111 		if (StartExe(theProcess, &status, 0, EFalse, aLowMem, pTheSem) == KErrNone)
       
  2112 			{
       
  2113 			User::WaitForRequest(status);
       
  2114 			if (theProcess.ExitType() == EExitPanic)
       
  2115 				{
       
  2116 				DBGS_PRINT((_L("%S : Process Panic'd...\n"), &TestNameBuffer));	
       
  2117 				}
       
  2118 			theProcess.Close();
       
  2119 			}
       
  2120 		}
       
  2121 
       
  2122 	if (TestLoading & TEST_SELF)
       
  2123 		{
       
  2124 		RProcess		theProcess;
       
  2125 		TRequestStatus	status;
       
  2126 		
       
  2127 		if (StartExe(theProcess, &status, 0, ETrue, aLowMem,pTheSem) == KErrNone)
       
  2128 			{
       
  2129 			User::WaitForRequest(status);
       
  2130 			if (theProcess.ExitType() == EExitPanic)
       
  2131 				{
       
  2132 				DBGS_PRINT((_L("%S : Process Panic'd...\n"), &TestNameBuffer));
       
  2133 				}
       
  2134 			theProcess.Close();
       
  2135 			}
       
  2136 		}
       
  2137 
       
  2138 	if (TestLoading	& TEST_DLL)
       
  2139 		{
       
  2140 		TInt maxDlls = GetNumDlls();
       
  2141 		if (TestLoadDllHow == TEST_DLL_GLOBAL)
       
  2142 			{
       
  2143 			TInt retVal = LoadTheLibs(theGlobalLibs, maxDlls, TestInstanceId, NULL, NULL, pTheSem);
       
  2144 			if (retVal != KErrNone)
       
  2145 				{
       
  2146 				DBGS_PRINT((_L("DoSingleTest - unable to load libs\n") ));
       
  2147 				CloseTheLibs (theGlobalLibs, PAGELDRTST_MAX_DLLS);
       
  2148 				if (aLowMem)
       
  2149 					{
       
  2150 					TestMultiSem.Close();
       
  2151 					}
       
  2152 				return KErrGeneral;
       
  2153 				}
       
  2154 			}
       
  2155 
       
  2156 		ret = PerformTestThread((TInt) TestInstanceId, NULL, NULL, pTheSem);
       
  2157 
       
  2158 		if (TestLoadDllHow == TEST_DLL_GLOBAL)
       
  2159 			{
       
  2160 			CloseTheLibs(theGlobalLibs, maxDlls);
       
  2161 			}
       
  2162 		}
       
  2163 	if (aLowMem)
       
  2164 		{
       
  2165 		TestMultiSem.Close();
       
  2166 		}
       
  2167 
       
  2168 	if (!TestSilent)
       
  2169 		{
       
  2170 		TInt end = User::TickCount();
       
  2171 		TInt time = TUint((TUint64)(end-start)*(TUint64)TickPeriod/(TUint64)1000000);
       
  2172 		DBGS_PRINT((_L("\n%S : Single Test : (%u seconds)\n"), &TestNameBuffer, time));
       
  2173 		}
       
  2174 
       
  2175 	return ret;
       
  2176 	}
       
  2177 
       
  2178 //
       
  2179 // FillPage
       
  2180 //
       
  2181 // Fill a page with test data
       
  2182 //
       
  2183 
       
  2184 void FillPage(TUint aOffset)
       
  2185 	{
       
  2186 	if (TestChunkData)
       
  2187 		{
       
  2188 		TUint32* ptr = (TUint32 *)((TUint8 *)TestChunkBase+aOffset);
       
  2189 		TUint32* ptrEnd = (TUint32 *)((TUint8 *)ptr + TestPageSize);
       
  2190 		do 
       
  2191 			{
       
  2192 			*ptr = 0x55000000 + aOffset;
       
  2193 			ptr ++;
       
  2194 			aOffset += 4;
       
  2195 			}
       
  2196 		while(ptr<ptrEnd);
       
  2197 		}
       
  2198 	}
       
  2199 
       
  2200 //
       
  2201 // CheckPage
       
  2202 //
       
  2203 // Check a page matches test data....
       
  2204 //
       
  2205 
       
  2206 TBool CheckPage(TUint index, TUint aOffset)
       
  2207 	{
       
  2208 	TBool ret = ETrue;
       
  2209 	if (TestChunkData)
       
  2210 		{
       
  2211 		TUint32* ptr = (TUint32 *)((TUint8 *)TestChunkBase+aOffset);
       
  2212 		TUint32* ptrEnd = (TUint32 *)((TUint8 *)ptr + TestPageSize);
       
  2213 		do
       
  2214 			{
       
  2215 			if (*ptr != (0x55000000 + aOffset)) 
       
  2216 				break;
       
  2217 			ptr ++;
       
  2218 			aOffset += 4;
       
  2219 			}
       
  2220 		while(ptr<ptrEnd);
       
  2221 		if (ptr==ptrEnd)
       
  2222 			{
       
  2223 			TestChunkStats[index].check.ok ++;
       
  2224 			}
       
  2225 		else
       
  2226 			{
       
  2227 			TestChunkStats[index].check.fail ++;
       
  2228 			ret = EFalse;
       
  2229 			}
       
  2230 		}
       
  2231 	return ret;
       
  2232 	}
       
  2233 
       
  2234 //
       
  2235 // DoSomeChunking
       
  2236 //
       
  2237 // Lock and unlock various pages in a chunk...
       
  2238 //
       
  2239 TUint   TestChunkingIndex = 0;
       
  2240 TUint   TestChunkingIndexFails = 0;
       
  2241 
       
  2242 void DoSomeChunking()
       
  2243 	{
       
  2244 	TUint       iters = TEST_NUM_CHUNK_PAGES / 4;
       
  2245 	TBool		lockit = EFalse;
       
  2246 	TBool		decomit = EFalse;
       
  2247 	TUint		index;
       
  2248 	TInt		ret;
       
  2249 	TInt		theOffset;
       
  2250 	
       
  2251 	while (iters)
       
  2252 		{
       
  2253 		TestChunkingIndex = TestChunkingIndex * 69069 + 1;
       
  2254 		index = TUint64((TUint64)TestChunkingIndex*(TUint64)TEST_NUM_CHUNK_PAGES)>>32;
       
  2255 		if (index >= TEST_NUM_CHUNK_PAGES)
       
  2256 			TestChunkingIndexFails ++;
       
  2257 
       
  2258 		theOffset = index * TestPageSize;
       
  2259 		if (theOffset < TestCommitEnd)
       
  2260 			{
       
  2261 			if (lockit)
       
  2262 				{
       
  2263 				if (decomit)
       
  2264 					{
       
  2265 					ret = TestChunk.Decommit(theOffset,TestPageSize);
       
  2266 					if (KErrNone == ret)
       
  2267 						TestChunkStats[index].decommit.ok ++;
       
  2268 					else
       
  2269 						TestChunkStats[index].decommit.fail ++;
       
  2270 					ret = TestChunk.Commit(theOffset,TestPageSize);
       
  2271 					if (KErrNone == ret)
       
  2272 						{
       
  2273 						TestChunkStats[index].commit.ok ++;
       
  2274 						FillPage(theOffset);
       
  2275 						TestChunkPageState[index] = ETrue;
       
  2276 						}
       
  2277 					else
       
  2278 						{
       
  2279 						TestChunkStats[index].commit.fail ++;
       
  2280 						TestChunkPageState[index] = EFalse;
       
  2281 						}
       
  2282 					ret = KErrNone;
       
  2283 					}
       
  2284 				else
       
  2285 					{
       
  2286 					ret = TestChunk.Lock(theOffset,TestPageSize);
       
  2287 					if (KErrNone == ret)
       
  2288 						{
       
  2289 						TestChunkStats[index].lock.ok ++;
       
  2290 						if (!CheckPage(index, theOffset))
       
  2291 							FillPage(theOffset);
       
  2292 						TestChunkPageState[index] = ETrue;
       
  2293 						}
       
  2294 					else
       
  2295 						{
       
  2296 						TestChunkStats[index].lock.fail ++;
       
  2297 						TestChunkPageState[index] = EFalse;
       
  2298 						}
       
  2299 					}
       
  2300 				decomit = !decomit;
       
  2301 				}
       
  2302 			else
       
  2303 				{
       
  2304 				if (TestChunkPageState[index])
       
  2305 					{
       
  2306 					// this one should still be locked so the data should be ok.
       
  2307 					if (KErrNone == TestChunk.Lock(theOffset,TestPageSize))
       
  2308 						{				
       
  2309 						TestChunkStats[index].lock.ok ++;
       
  2310 						CheckPage(index, theOffset);
       
  2311 						}
       
  2312 					else
       
  2313 						TestChunkStats[index].lock.fail ++;
       
  2314 					}
       
  2315 				ret = TestChunk.Unlock(theOffset,TestPageSize);
       
  2316 				if (KErrNone == ret)
       
  2317 					TestChunkStats[index].unlock.ok ++;
       
  2318 				else
       
  2319 					TestChunkStats[index].unlock.fail ++;
       
  2320 				TestChunkPageState[index] = EFalse;
       
  2321 				}
       
  2322 			if (KErrNone != ret)			
       
  2323 				{
       
  2324 				// so now we need to commit another page in this pages place.
       
  2325 				ret = TestChunk.Commit(theOffset,TestPageSize);
       
  2326 				if (KErrNone != ret)
       
  2327 					{
       
  2328 					TestChunkStats[index].commit.fail ++;
       
  2329 					//DBGS_PRINT((_L("%S : DoSomeChunking[%03d] index %03d failed to commit a page  %d\n"), &TestNameBuffer, iters, index, ret));
       
  2330 					TestChunkPageState[index] = EFalse;
       
  2331 					}
       
  2332 				else
       
  2333 					{
       
  2334 					TestChunkStats[index].commit.ok ++;
       
  2335 					FillPage(theOffset);
       
  2336 					TestChunkPageState[index] = ETrue;
       
  2337 					}
       
  2338 				}
       
  2339 			lockit = !lockit;
       
  2340 			}
       
  2341 		else
       
  2342 			{
       
  2343 			RDebug::Printf("DoSomeChunking - offset was bad %d / %d", theOffset, TestCommitEnd);
       
  2344 			}
       
  2345 		iters --;
       
  2346 		}
       
  2347 	}
       
  2348 
       
  2349 //
       
  2350 // DoMultipleTest
       
  2351 // 
       
  2352 // Perform the multiple thread test, spawning a number of threads.
       
  2353 // It is complicated a little because test.Printf can only be called from the first thread that calls it 
       
  2354 // so if we are using multiple threads we need to use a message queue to pass the debug info from the
       
  2355 // child threads back to the parent for the parent to then call printf.
       
  2356 //
       
  2357 
       
  2358 TInt DoMultipleTest(TBool aLowMem = EFalse)
       
  2359 	{
       
  2360 	TInt			 index;
       
  2361 	TUint            start = User::TickCount();
       
  2362 	RThread			*pTheThreads = NULL;
       
  2363 	TInt			*pThreadInUse = NULL;
       
  2364 
       
  2365 	RProcess		*pTheProcesses = NULL;
       
  2366 	TInt			*pProcessInUse = NULL;
       
  2367 
       
  2368 	RThread			*pMedThreads = NULL;
       
  2369 	TInt			*pMedInUse = NULL;
       
  2370 
       
  2371 	TRequestStatus	mediaStatus;
       
  2372 	RThread			mediaThread;
       
  2373 	TInt ret;
       
  2374 
       
  2375 	RSemaphore	*pTheSem = NULL;
       
  2376 
       
  2377 	DisplayTestBanner(ETrue);
       
  2378 	
       
  2379 	TestThreadsExit = EFalse;
       
  2380 
       
  2381 	DOTEST1(ret,TestMultiSem.CreateLocal(1),KErrNone, KErrNoMemory);
       
  2382 	
       
  2383 	pTheSem = &TestMultiSem;
       
  2384 	if (TestLoading & TEST_DLL)
       
  2385 		{
       
  2386 		DOTEST((pTheThreads  = (RThread *)User::AllocZ(sizeof(RThread) * TestMultipleThreadCount)),
       
  2387 		       (pTheThreads != NULL))
       
  2388 		DOTEST((pThreadInUse = (TInt *)User::AllocZ(sizeof(TInt) * TestMultipleThreadCount)),
       
  2389 		       (pThreadInUse != NULL));
       
  2390 		RUNTEST1(pTheThreads && pThreadInUse);
       
  2391 		if (!(pTheThreads && pThreadInUse))
       
  2392 			return KErrGeneral;
       
  2393 		}
       
  2394 
       
  2395 	if (TestLoading & TEST_EXE_SELF)
       
  2396 		{
       
  2397 		DOTEST((pTheProcesses = (RProcess *)User::AllocZ(sizeof(RProcess) * TestMultipleThreadCount)),
       
  2398 		       (pTheProcesses != NULL));
       
  2399 		DOTEST((pProcessInUse = (TInt *)User::AllocZ(sizeof(TInt) * TestMultipleThreadCount)),
       
  2400 		       (pProcessInUse != NULL));
       
  2401 		RUNTEST1(pTheProcesses && pProcessInUse);
       
  2402 		if (!(pTheProcesses && pProcessInUse))
       
  2403 			return KErrGeneral;
       
  2404 		}
       
  2405 	
       
  2406 	if (!TestSilent)
       
  2407 		{
       
  2408 		DOTEST1(ret,TestMsgQueue.CreateLocal(TestMultipleThreadCount * 10, EOwnerProcess),KErrNone, KErrNoMemory);
       
  2409 		if (ret != KErrNone)
       
  2410 			return KErrGeneral;
       
  2411 		}
       
  2412 
       
  2413 	if (TestMediaAccess != KTestMediaAccessNone)
       
  2414 		{
       
  2415 		if (TestMediaAccess != KTestMediaAccessBasic)
       
  2416 			{
       
  2417 			TestStopMedia = EFalse;
       
  2418 			DOTEST((pMedThreads  = (RThread *)User::AllocZ(sizeof(RThread) * TestMultipleThreadCount)),
       
  2419 				   (pMedThreads != NULL))
       
  2420 			DOTEST((pMedInUse = (TInt *)User::AllocZ(sizeof(TInt) * TestMultipleThreadCount)),
       
  2421 				   (pMedInUse != NULL));
       
  2422 			RUNTEST1(pMedThreads && pMedInUse);
       
  2423 			if (!(pMedThreads && pMedInUse))
       
  2424 				return KErrGeneral;
       
  2425 
       
  2426 			for (index = 0; index < TestMultipleThreadCount; index++)
       
  2427 				{
       
  2428 				DBGS_PRINT((_L("%S : Starting Media Thread %d\n"), &TestNameBuffer, index));
       
  2429 				DOTEST1(ret,pMedThreads[index].Create(KTestBlank,PerformRomAndFileSystemAccess,KDefaultStackSize,NULL,(TAny*) index),KErrNone, KErrNoMemory);
       
  2430 				if (ret == KErrNone)
       
  2431 					{
       
  2432 					pMedThreads[index].Resume();
       
  2433 					pMedInUse[index] = 1;
       
  2434 					}
       
  2435 				User::AfterHighRes(0);
       
  2436 				}
       
  2437 			}
       
  2438 		else
       
  2439 			{
       
  2440 			TestStopMedia = EFalse;
       
  2441 			DOTEST1(ret,mediaThread.Create(KTestBlank,PerformRomAndFileSystemAccess,KDefaultStackSize,NULL,(TAny *) 0),KErrNone, KErrNoMemory);
       
  2442 			if (ret == KErrNone)
       
  2443 				{
       
  2444 				mediaThread.Logon(mediaStatus);
       
  2445 				RUNTEST1(mediaStatus == KRequestPending);	
       
  2446 				mediaThread.Resume();
       
  2447 				}
       
  2448 			}
       
  2449 		}
       
  2450 
       
  2451 	TInt maxDlls = GetNumDlls();
       
  2452 	if (TestLoadDllHow == TEST_DLL_GLOBAL)
       
  2453 		{
       
  2454 		TInt retVal = LoadTheLibs(theGlobalLibs, maxDlls, 0, NULL, NULL, NULL);
       
  2455 		if (retVal != KErrNone)
       
  2456 			{
       
  2457 			DBGS_PRINT((_L("DoMultipleTest - unable to load libs\n")));
       
  2458 			CloseTheLibs (theGlobalLibs, maxDlls);
       
  2459 			if (!TestSilent)
       
  2460 				{
       
  2461 				TestMsgQueue.Close();
       
  2462 				}
       
  2463 			TestMultiSem.Close();
       
  2464 			return KErrGeneral;
       
  2465 			}
       
  2466 		}
       
  2467 
       
  2468 	// make sure we have a priority higher than that of the threads we spawn...
       
  2469 	RThread thisThread;
       
  2470 	TThreadPriority savedThreadPriority = thisThread.Priority();
       
  2471 	const TThreadPriority KMainThreadPriority = EPriorityMuchMore;
       
  2472 	__ASSERT_COMPILE(KMainThreadPriority>TEST_INTERLEAVE_PRIO);
       
  2473 	thisThread.SetPriority(KMainThreadPriority);
       
  2474 	
       
  2475 
       
  2476 	for (index = 0; index < TestMultipleThreadCount; index++)
       
  2477 		{
       
  2478 		if (TestLoading & TEST_EXE_SELF)
       
  2479 			{
       
  2480 			if (KErrNone == StartExe(pTheProcesses[index], 0, index + ((TestLoading & TEST_DLL) ? TestMultipleThreadCount : 0), ((TestLoading & TEST_EXE_SELF) == TEST_EXE_SELF) ? (index & 2) : (TestLoading & TEST_SELF), aLowMem, pTheSem))
       
  2481 				{
       
  2482 				User::AfterHighRes(0);
       
  2483 				pProcessInUse[index] = 1;
       
  2484 				}
       
  2485 			}
       
  2486 		
       
  2487 	
       
  2488 		if (TestLoading & TEST_DLL)
       
  2489 			{
       
  2490 			DBGS_PRINT((_L("%S : Starting Thread %d\n"), &TestNameBuffer, index));
       
  2491 			DOTEST1(ret,pTheThreads[index].Create(KTestBlank,MultipleTestThread,KDefaultStackSize,NULL,(TAny*) index),KErrNone, KErrNoMemory);
       
  2492 			if (ret == KErrNone)
       
  2493 				{
       
  2494 				pTheThreads[index].Resume();
       
  2495 				User::AfterHighRes(0);
       
  2496 				pThreadInUse[index] = 1;
       
  2497 				}
       
  2498 			}
       
  2499 		}
       
  2500 
       
  2501 	// wait for any child threads to exit and process any debug messages they pass back to the parent.
       
  2502 	TBool		anyUsed = ETrue;
       
  2503 	TMessageBuf	localBuffer;
       
  2504 	
       
  2505 	TInt		processOk = 0;
       
  2506 	TInt		threadOk = 0;
       
  2507 	TInt		processPanic = 0;
       
  2508 	TInt		threadPanic = 0;
       
  2509 	TUint		end = start;
       
  2510 	TUint		now;
       
  2511 	TUint		time;
       
  2512 	TUint		killNext = 0;
       
  2513 	TUint		numDots = 0;
       
  2514 	TUint		maxDots = (10*60)/TEST_DOT_PERIOD;	// No individual test should take longer than 10 minutes!
       
  2515 													// Most have been tuned to take between 2 and 8 minutes.
       
  2516 													// The autotests should not take more than 120 minutes total.
       
  2517 
       
  2518 	while(anyUsed)
       
  2519 		{
       
  2520 		TInt threadCount = 0;
       
  2521 		TInt processCount = 0;
       
  2522 		anyUsed = EFalse;
       
  2523 
       
  2524 		// check the message queue and call printf if we get a message.
       
  2525 		if (!TestSilent)
       
  2526 			{
       
  2527 			while (KErrNone == TestMsgQueue.Receive(localBuffer))
       
  2528 				{
       
  2529 				DBGS_PRINT((localBuffer));
       
  2530 				}
       
  2531 			}
       
  2532 
       
  2533 		// walk through the thread list to check which are still alive.
       
  2534 		for (index = 0; index < TestMultipleThreadCount; index++)
       
  2535 			{
       
  2536 			if (TestLoading & TEST_DLL)
       
  2537 				{
       
  2538 				if (pThreadInUse[index])
       
  2539 					{
       
  2540 					if (pTheThreads[index].ExitType() != EExitPending)
       
  2541 						{
       
  2542 						if (pTheThreads[index].ExitType() == EExitPanic)
       
  2543 							{
       
  2544 							DBGS_PRINT((_L("%S : Thread %d Panic'd after %u ticks \n"),
       
  2545 								&TestNameBuffer, index, User::TickCount() - start));	
       
  2546 							threadPanic ++;
       
  2547 							}
       
  2548 						else
       
  2549 							{
       
  2550 							DBGS_PRINT((_L("%S : Thread %d Exited after %u ticks \n"),
       
  2551 								&TestNameBuffer, index, User::TickCount() - start));	
       
  2552 							threadOk ++;
       
  2553 							}
       
  2554 						pTheThreads[index].Close();
       
  2555 						pThreadInUse[index] = EFalse;
       
  2556 						}
       
  2557 					else
       
  2558 						{
       
  2559 						threadCount += 1;
       
  2560 						anyUsed = ETrue;
       
  2561 						if (TestThreadsExit)
       
  2562 							{
       
  2563 							now = User::TickCount();
       
  2564 							time = TUint((TUint64)(now-end)*(TUint64)TickPeriod/(TUint64)1000000);
       
  2565 							if (time > TEST_DOT_PERIOD)
       
  2566 								{
       
  2567 								DBGS_PRINT((_L("%S : Thread %d still running\n"), &TestNameBuffer, index));	
       
  2568 								}
       
  2569 							time = TUint((TUint64)(now-killNext)*(TUint64)TickPeriod/(TUint64)1000000);
       
  2570 							const TUint killTimeStep = (TEST_DOT_PERIOD+9)/10; // 1/10th of a dot
       
  2571 							if(time>TEST_DOT_PERIOD+killTimeStep)
       
  2572 								{
       
  2573 								killNext += killTimeStep*1000000/TickPeriod;
       
  2574 								DBGS_PRINT((_L("%S : killing Thread %d\n"), &TestNameBuffer, index));	
       
  2575 								pTheThreads[index].Kill(KErrNone);
       
  2576 								pTheThreads[index].Close();
       
  2577 								pThreadInUse[index] = EFalse;
       
  2578 								}
       
  2579 							}
       
  2580 						}
       
  2581 					}
       
  2582 				}
       
  2583 			if (TestLoading & TEST_EXE_SELF)
       
  2584 				{
       
  2585 				if (pProcessInUse[index])
       
  2586 					{
       
  2587 					if (pTheProcesses[index].ExitType() != EExitPending)
       
  2588 						{
       
  2589 						if (pTheProcesses[index].ExitType() == EExitPanic)
       
  2590 							{
       
  2591 							DBGS_PRINT((_L("%S : Process %d Panic'd after %u ticks \n"),
       
  2592 								&TestNameBuffer,
       
  2593 								index + ((TestLoading & TEST_DLL) ? TestMultipleThreadCount : 0),
       
  2594 								User::TickCount() - start));	
       
  2595 							processPanic ++;
       
  2596 							}
       
  2597 						else
       
  2598 							{
       
  2599 							DBGS_PRINT((_L("%S : Process %d Exited after %u ticks \n"),
       
  2600 								&TestNameBuffer,
       
  2601 								index + ((TestLoading & TEST_DLL) ? TestMultipleThreadCount : 0),
       
  2602 								User::TickCount() - start));	
       
  2603 							processOk ++;
       
  2604 							}
       
  2605 
       
  2606 						pTheProcesses[index].Close();
       
  2607 						pProcessInUse[index] = EFalse;
       
  2608 						}
       
  2609 					else
       
  2610 						{
       
  2611 						processCount += 1;
       
  2612 						anyUsed = ETrue;
       
  2613 						if (TestThreadsExit)
       
  2614 							{
       
  2615 							now = User::TickCount();
       
  2616 							time = TUint((TUint64)(now-end)*(TUint64)TickPeriod/(TUint64)1000000);
       
  2617 							if (time > TEST_DOT_PERIOD)
       
  2618 								{
       
  2619 								DBGS_PRINT((_L("%S : Process %d still running; killing it.\n"),
       
  2620 									&TestNameBuffer, index));
       
  2621 								pTheProcesses[index].Kill(EExitKill);
       
  2622 								pTheProcesses[index].Close();
       
  2623 								pProcessInUse[index] = EFalse;
       
  2624 								}
       
  2625 							
       
  2626 							}
       
  2627 						}
       
  2628 					}
       
  2629 				}
       
  2630 			}
       
  2631 
       
  2632 		now = User::TickCount();
       
  2633 		time = TUint((TUint64)(now-end)*(TUint64)TickPeriod/(TUint64)1000000);
       
  2634 
       
  2635 		DBGD_PRINT((_L("%S : %d seconds (%d ticks) %d threads, %d processes still alive\n"),
       
  2636 			&TestNameBuffer, time, now, threadCount, processCount));
       
  2637 
       
  2638 		if (time > TEST_DOT_PERIOD)
       
  2639 			{
       
  2640 			DBGS_PRINT((_L(".")));
       
  2641 			numDots ++;
       
  2642 			end += TEST_DOT_PERIOD*1000000/TickPeriod;
       
  2643 			if (TestingReaper)
       
  2644 				{
       
  2645 				TestingReaperCleaningFiles = ETrue;
       
  2646 				CleanupFiles(EFalse);
       
  2647 				CheckFilePresence(ETrue);
       
  2648 				TestingReaperCleaningFiles = EFalse;
       
  2649 				}
       
  2650 			if ((numDots >= maxDots) && (!TestThreadsExit))
       
  2651 				{
       
  2652 				DBGS_PRINT((_L("Taking longer than %d dots...exiting test case."), maxDots));
       
  2653 				TestThreadsExit = ETrue;
       
  2654 				killNext = end;
       
  2655 				}
       
  2656 			}
       
  2657 
       
  2658 		if (TestingChunks)
       
  2659 			{
       
  2660 			DoSomeChunking();
       
  2661 			}
       
  2662 
       
  2663 #ifdef TEST_THRASHING_TEST
       
  2664 		User::AfterHighRes(1000);
       
  2665 #else
       
  2666 		User::AfterHighRes(TickPeriod);
       
  2667 #endif
       
  2668 		}
       
  2669 
       
  2670 	DBGD_PRINT((_L("%S : all test threads presumably gone now\n"), &TestNameBuffer));
       
  2671 
       
  2672 	if (TestMediaAccess != KTestMediaAccessNone)
       
  2673 		{
       
  2674 		if (TestMediaAccess != KTestMediaAccessBasic)
       
  2675 			{
       
  2676 			TBool killMedia = EFalse;
       
  2677 			TestStopMedia = ETrue;
       
  2678 			anyUsed = ETrue;
       
  2679 			DBGS_PRINT((_L("%S : Waiting for media threads to exit...\n"), &TestNameBuffer));	
       
  2680 			end = User::TickCount();
       
  2681 			while (anyUsed)
       
  2682 				{
       
  2683 				anyUsed = EFalse;
       
  2684 
       
  2685 				// check the message queue and call printf if we get a message.
       
  2686 				if (!TestSilent)
       
  2687 					{
       
  2688 					while (KErrNone == TestMsgQueue.Receive(localBuffer))
       
  2689 						{
       
  2690 						DBGS_PRINT((localBuffer));
       
  2691 						}
       
  2692 					}
       
  2693 
       
  2694 				for (index = 0; index < TestMultipleThreadCount; index++)
       
  2695 					{
       
  2696 					if (pMedInUse[index])
       
  2697 						{
       
  2698 						if (pMedThreads[index].ExitType() != EExitPending)
       
  2699 							{
       
  2700 							if (pMedThreads[index].ExitType() == EExitPanic)
       
  2701 								{
       
  2702 								DBGS_PRINT((_L("%S : Media Thread %d Panic'd after %u ticks \n"),
       
  2703 									&TestNameBuffer, index, User::TickCount() - start));	
       
  2704 								threadPanic ++;
       
  2705 								}
       
  2706 							else
       
  2707 								{
       
  2708 								DBGS_PRINT((_L("%S : Media Thread %d Exited after %u ticks \n"),
       
  2709 									&TestNameBuffer, index, User::TickCount() - start));	
       
  2710 								threadOk ++;
       
  2711 								}
       
  2712 							pMedInUse[index] = EFalse;
       
  2713 							}
       
  2714 						else
       
  2715 							{
       
  2716 							anyUsed = ETrue;
       
  2717 							if (killMedia)
       
  2718 								{
       
  2719 								DBGS_PRINT((_L("%S : Media Thread %d still going after %u ticks; killing it!\n"),
       
  2720 									&TestNameBuffer, index, User::TickCount() - start));	
       
  2721 								pMedThreads[index].Kill(EExitKill);
       
  2722 								}
       
  2723 							}
       
  2724 						}
       
  2725 					}
       
  2726 				now = User::TickCount();
       
  2727 				time = TUint((TUint64)(now-end)*(TUint64)TickPeriod/(TUint64)1000000);
       
  2728 				if (time > TEST_DOT_PERIOD)
       
  2729 					{
       
  2730 					DBGS_PRINT((_L(".")));
       
  2731 					end += TEST_DOT_PERIOD*1000000/TickPeriod;
       
  2732 					killMedia = ETrue;
       
  2733 					}
       
  2734 
       
  2735 				User::AfterHighRes(50000);
       
  2736 
       
  2737 				}
       
  2738 			DBGS_PRINT((_L("%S : Media threads exited...\n"), &TestNameBuffer));	
       
  2739 			User::Free(pMedThreads);
       
  2740 			User::Free(pMedInUse);
       
  2741 			}
       
  2742 		else
       
  2743 			{
       
  2744 			TestStopMedia = ETrue;
       
  2745 			DBGS_PRINT((_L("%S : Waiting for media thread to exit...\n"), &TestNameBuffer));	
       
  2746 			end = User::TickCount();
       
  2747 			while (mediaThread.ExitType() == EExitPending)
       
  2748 				{
       
  2749 				now = User::TickCount();
       
  2750 				time = TUint((TUint64)(now-end)*(TUint64)TickPeriod/(TUint64)1000000);
       
  2751 				if (time > TEST_DOT_PERIOD)
       
  2752 					{
       
  2753 					DBGS_PRINT((_L("%S : Media thread still going after %u seconds; killing it!\n"),
       
  2754 						&TestNameBuffer, time));
       
  2755 					mediaThread.Kill(EExitKill);
       
  2756 					}
       
  2757 				User::AfterHighRes(50000);
       
  2758 				}
       
  2759 			User::WaitForRequest(mediaStatus);
       
  2760 			mediaThread.Close();
       
  2761 			DBGS_PRINT((_L("%S : Media thread exited...\n"), &TestNameBuffer));	
       
  2762 			}
       
  2763 		}
       
  2764 
       
  2765 	DBGD_PRINT((_L("%S : all media threads presumably gone now\n"), &TestNameBuffer));
       
  2766 
       
  2767 	if (!TestSilent)
       
  2768 		{
       
  2769 		TestMsgQueue.Close();
       
  2770 		}
       
  2771 	TestMultiSem.Close();
       
  2772 
       
  2773 	DBGD_PRINT((_L("%S : about to close the libraries\n"), &TestNameBuffer));
       
  2774 
       
  2775 	if (TestLoadDllHow == TEST_DLL_GLOBAL)
       
  2776 		{
       
  2777 		CloseTheLibs(theGlobalLibs, maxDlls);
       
  2778 		}
       
  2779 
       
  2780 	TestThreadsExit = EFalse;
       
  2781 
       
  2782 	DBGD_PRINT((_L("%S : cleaning up\n"), &TestNameBuffer));
       
  2783 
       
  2784 	// cleanup the resources and exit.
       
  2785 	if (TestLoading & TEST_EXE_SELF)
       
  2786 		{
       
  2787 		User::Free(pTheProcesses);
       
  2788 		User::Free(pProcessInUse);
       
  2789 		}
       
  2790 
       
  2791 	// cleanup the resources and exit.
       
  2792 	if (TestLoading & TEST_DLL)
       
  2793 		{
       
  2794 		User::Free(pTheThreads);
       
  2795 		User::Free(pThreadInUse);
       
  2796 		}
       
  2797 
       
  2798 	if (!TestSilent)
       
  2799 		{
       
  2800 		end = User::TickCount();
       
  2801 		time = TUint((TUint64)(end-start)*(TUint64)TickPeriod/(TUint64)1000000);
       
  2802 		DBGS_PRINT((_L("\n%S : Multiple Test : (%u seconds)\n\tThreads panic'd = %d Ok = %d\n\tProcess panic'd = %d Ok = %d\n"), &TestNameBuffer, time, threadPanic, threadOk, processPanic, processOk));
       
  2803 		}
       
  2804 
       
  2805 	thisThread.SetPriority(savedThreadPriority);
       
  2806 
       
  2807 	return (threadPanic | processPanic) ? KErrGeneral : KErrNone;
       
  2808 	}
       
  2809 
       
  2810 //
       
  2811 // DoChunkTests
       
  2812 //
       
  2813 // Allocate a chunk and assign some pages to it...
       
  2814 // Then do a multiple test.
       
  2815 //
       
  2816 
       
  2817 void DoChunkTests()
       
  2818 	{
       
  2819 	SVMCacheInfo  tempPages;
       
  2820 	memset(&tempPages, 0, sizeof(tempPages));
       
  2821 	if (TestIsDemandPaged)
       
  2822 		{
       
  2823 		// Shrink the page cache down to the minimum.
       
  2824 		UserSvr::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0);
       
  2825 
       
  2826 		DBGS_PRINT((_L("Start : min %d max %d current %d maxFree %d freeRam %d\n"),
       
  2827 					 tempPages.iMinSize, tempPages.iMaxSize, tempPages.iCurrentSize ,tempPages.iMaxFreeSize, FreeRam()));
       
  2828 
       
  2829 		// set the cache small 
       
  2830 		TInt minSize = 16 * TestPageSize;
       
  2831 		TInt maxSize = TEST_NUM_PAGES * TestPageSize;
       
  2832 		UserSvr::HalFunction(EHalGroupVM,EVMHalSetCacheSize,(TAny*)minSize,(TAny*)maxSize);
       
  2833 		}
       
  2834 
       
  2835 	if (KErrNone != TestChunk.CreateDisconnectedLocal(0,0,TEST_NUM_CHUNK_PAGES *TestPageSize))
       
  2836 		{
       
  2837 		DBGS_PRINT((_L("DoChunkTests - create failed.\n")));
       
  2838 		return;
       
  2839 		}
       
  2840 	TestChunkBase = TestChunk.Base();
       
  2841 	if (TestChunkBase == NULL)
       
  2842 		{
       
  2843 		RDebug::Printf("DoChunkTests - TestChunkBase was NULL");
       
  2844 		TestChunk.Close();
       
  2845 		return;
       
  2846 		}
       
  2847 	TInt retVal = KErrNone;
       
  2848 	TUint index = 0;
       
  2849 	TestCommitEnd = 0;
       
  2850 	memset(TestChunkPageState, 0, sizeof(TestChunkPageState));
       
  2851 	memset(TestChunkStats,0,sizeof(TestChunkStats));
       
  2852 	while(index < TEST_NUM_CHUNK_PAGES)
       
  2853 		{
       
  2854 		retVal = TestChunk.Commit(TestCommitEnd,TestPageSize);
       
  2855 		if (KErrNone != retVal)
       
  2856 			{
       
  2857 			DBGS_PRINT((_L("%S : TestChunk.Commit returned %d for 0x%08x...\n"), &TestNameBuffer, retVal, TestCommitEnd));	
       
  2858 			break;
       
  2859 			}
       
  2860 		TestChunkPageState[index] = ETrue;
       
  2861 		FillPage(TestCommitEnd);
       
  2862 		TestCommitEnd += TestPageSize;
       
  2863 		index ++;
       
  2864 		}
       
  2865 	RUNTEST1(retVal == KErrNone);
       
  2866 	
       
  2867 	// now do some testing....
       
  2868 	TestingChunks = ETrue;
       
  2869 	TestInterleave = EFalse;
       
  2870 	TestPrioChange = ETrue;
       
  2871 	TestMediaAccess = KTestMediaAccessNone;
       
  2872 	// temp
       
  2873 	TestWhichMedia = TEST_MEDIA_ROM_BASE;
       
  2874 
       
  2875 	if (TestChunksPlus)
       
  2876 		{
       
  2877 		TestMaxLoops = 1;
       
  2878 		TestMultipleThreadCount	= 40;
       
  2879 		}
       
  2880 	else if (TestExtremeChunks)
       
  2881 		{
       
  2882 		TestMaxLoops = 10;
       
  2883 		TestMultipleThreadCount	= 12;
       
  2884 		}
       
  2885 	else
       
  2886 		{
       
  2887 		TestMaxLoops = 3;
       
  2888 		TestMultipleThreadCount	= 20;
       
  2889 		}
       
  2890 	TestWhichTests = TEST_RANDOM;
       
  2891 
       
  2892 	TestLoading = TEST_EXE_SELF_DLL;
       
  2893 	TestLoadDllHow = TEST_DLL_FUNC;
       
  2894 	TestChunkingIndexFails = 0;
       
  2895 
       
  2896 	TEST_NEXT((_L("Multiple threads random with chunks.")));
       
  2897 	RUNTEST(DoMultipleTest(), KErrNone);
       
  2898 	
       
  2899 	TestingChunks = EFalse;
       
  2900 
       
  2901 	// clean up.
       
  2902 	UserSvr::HalFunction(EHalGroupVM,EVMHalFlushCache,0,0);
       
  2903 	TestChunk.Close();
       
  2904 
       
  2905 	if (TestIsDemandPaged)
       
  2906 		{
       
  2907 		// put the cache back to the the original values.
       
  2908 		TInt minSize = tempPages.iMinSize;
       
  2909 		TInt maxSize = tempPages.iMaxSize;
       
  2910 
       
  2911 		UserSvr::HalFunction(EHalGroupVM,EVMHalSetCacheSize,(TAny*)minSize,(TAny*)maxSize);
       
  2912 
       
  2913 		UserSvr::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0);
       
  2914 
       
  2915 		DBGS_PRINT((_L("Finish : min %d max %d current %d maxFree %d freeRam %d\n"),
       
  2916 					 tempPages.iMinSize, tempPages.iMaxSize, tempPages.iCurrentSize ,tempPages.iMaxFreeSize, FreeRam()));
       
  2917 		}
       
  2918 	TChunkTestStats  stats;
       
  2919 
       
  2920 	memset(&stats, 0, sizeof(stats));
       
  2921 	DBGS_PRINT((_L("Stats : (pass/fail) \nindex\t\tlock\t\tunlock\t\tcommit\t\tdecommit\t\tcheck\n")));
       
  2922 	for (index = 0; index < TEST_NUM_CHUNK_PAGES; index ++)
       
  2923 		{
       
  2924 		DBGS_PRINT((_L("%u\t\t%d/%d\t\t%d/%d\t\t%d/%d\t\t%d/%d\t\t%d/%d\n"), 
       
  2925 					index,
       
  2926 					TestChunkStats[index].lock.ok, TestChunkStats[index].lock.fail,
       
  2927 					TestChunkStats[index].unlock.ok, TestChunkStats[index].unlock.fail,
       
  2928 					TestChunkStats[index].commit.ok, TestChunkStats[index].commit.fail,
       
  2929 					TestChunkStats[index].decommit.ok, TestChunkStats[index].decommit.fail,
       
  2930 					TestChunkStats[index].check.ok, TestChunkStats[index].check.fail));
       
  2931 
       
  2932 		stats.lock.ok += TestChunkStats[index].lock.ok;
       
  2933 		stats.lock.fail += TestChunkStats[index].lock.fail;
       
  2934 		stats.unlock.ok += TestChunkStats[index].unlock.ok;
       
  2935 		stats.unlock.fail += TestChunkStats[index].unlock.fail;
       
  2936 		stats.decommit.ok += TestChunkStats[index].decommit.ok;
       
  2937 		stats.decommit.fail += TestChunkStats[index].decommit.fail;
       
  2938 		stats.commit.ok += TestChunkStats[index].commit.ok;
       
  2939 		stats.commit.fail += TestChunkStats[index].commit.fail;
       
  2940 		stats.check.ok += TestChunkStats[index].check.ok;
       
  2941 		stats.check.fail += TestChunkStats[index].check.fail;
       
  2942 		}
       
  2943 
       
  2944 	DBGS_PRINT((_L("Total Stats (p/f): \n\t lock %d / %d\n\t unlock  %d / %d\n\t commit %d / %d\n\t decommit %d / %d\n\t check %d / %d\n"), 
       
  2945 				stats.lock.ok, stats.lock.fail,
       
  2946 				stats.unlock.ok, stats.unlock.fail,
       
  2947 				stats.commit.ok, stats.commit.fail,
       
  2948 				stats.decommit.ok, stats.decommit.fail,
       
  2949 				stats.check.ok, stats.check.fail));
       
  2950 	DBGS_PRINT((_L("TestChunkingIndexFails %d\n"), TestChunkingIndexFails));
       
  2951 
       
  2952 	}
       
  2953 
       
  2954 //
       
  2955 // DoReaperTests
       
  2956 //
       
  2957 // Test the reaper by deleting the transient files and re-creating them.
       
  2958 //
       
  2959 
       
  2960 void DoReaperTests(void)
       
  2961 	{
       
  2962 	// make sure we have the full complement of files.
       
  2963 	CheckFilePresence(ETrue);
       
  2964 	
       
  2965 	// now do some testing....
       
  2966 	TestInterleave = EFalse;
       
  2967 	TestPrioChange = EFalse;
       
  2968 	TestMediaAccess = KTestMediaAccessNone;
       
  2969 	// temp
       
  2970 	TestWhichMedia = TEST_MEDIA_ALL;
       
  2971 	TestMaxLoops = 3;
       
  2972 	TestMultipleThreadCount	= 12;
       
  2973 	TestWhichTests = TEST_RANDOM;
       
  2974 
       
  2975 	TestLoading = TEST_EXE_SELF_DLL;
       
  2976 	TestLoadDllHow = TEST_DLL_FUNC;
       
  2977 	
       
  2978 	TestingReaper = ETrue;
       
  2979 
       
  2980 	TEST_NEXT((_L("Reaper tests.")));
       
  2981 	RUNTEST(DoMultipleTest(), KErrNone);
       
  2982 	TestInterleave = ETrue;
       
  2983 	TestPrioChange = ETrue;
       
  2984 	TEST_NEXT((_L("Reaper tests 2.")));
       
  2985 	RUNTEST(DoMultipleTest(), KErrNone);
       
  2986 	
       
  2987 	TestingReaper = EFalse;
       
  2988 	}
       
  2989 
       
  2990 //
       
  2991 // DoBtraceTest
       
  2992 //
       
  2993 // Test the paging BTrace function.
       
  2994 //
       
  2995 
       
  2996 void DoBtraceTest(void)
       
  2997 	{
       
  2998 #define LE4(a) ((*((a) + 3) << 24) + (*((a) + 2) << 16) + (*((a) + 1) << 8) + *(a))
       
  2999 
       
  3000 	RBTrace bTraceHandle;
       
  3001 	
       
  3002 	TInt r = bTraceHandle.Open();
       
  3003 	test(r == KErrNone);
       
  3004 	
       
  3005 	r = bTraceHandle.ResizeBuffer(0x200000); 
       
  3006 	test(r == KErrNone);
       
  3007 	bTraceHandle.SetFilter(BTrace::EPaging, ETrue);
       
  3008 
       
  3009 	// Enable trace
       
  3010 	bTraceHandle.Empty();
       
  3011 	bTraceHandle.SetMode(RBTrace::EEnable);
       
  3012 	
       
  3013 	TestLoading             = TEST_EXE_SELF_DLL;
       
  3014 	TestWhichMedia          = TEST_MEDIA_ROM_BASE;
       
  3015 	TestMaxLoops            = 2;
       
  3016 	TestMultipleThreadCount = 10;
       
  3017 	TestLoadDllHow          = TEST_DLL_FUNC;
       
  3018 	TestInterleave          = ETrue;
       
  3019 	TestPrioChange          = ETrue;
       
  3020 	TestMediaAccess         = KTestMediaAccessNone;
       
  3021 	TestWhichTests          = TEST_RANDOM;		
       
  3022 	TestingLowMem			= EFalse;
       
  3023 
       
  3024 	RUNTEST(DoMultipleTest(TestingLowMem), KErrNone);
       
  3025 
       
  3026 	bTraceHandle.SetMode(0);
       
  3027 
       
  3028 	// analyse the btrace logs and display on the serial port.
       
  3029 	TUint8* pDataStart;
       
  3030 	TInt	dataSize;
       
  3031 	TUint8* pTemp;
       
  3032 	TUint8* pThis;
       
  3033 	TUint8* pEnd;
       
  3034 	TBuf<128>	data;
       
  3035 	while (1)
       
  3036 		{
       
  3037 		dataSize = bTraceHandle.GetData(pDataStart);
       
  3038 		if (dataSize <= 0)
       
  3039 			{
       
  3040 			break;
       
  3041 			}
       
  3042 		pEnd = pDataStart + dataSize;
       
  3043 		pTemp = pDataStart;
       
  3044 		while (pTemp < pEnd)
       
  3045 			{
       
  3046 			TUint8	recSize		= pTemp[BTrace::ESizeIndex];
       
  3047 			TUint8	recFlags	= pTemp[BTrace::EFlagsIndex];
       
  3048 			TUint8	recCat		= pTemp[BTrace::ECategoryIndex];
       
  3049 			TUint8	recSub		= pTemp[BTrace::ESubCategoryIndex];
       
  3050 			TUint32 addr[4];
       
  3051 			pThis = pTemp;
       
  3052 
       
  3053 			data.Zero();
       
  3054 			// step over the header.
       
  3055 			data.Format(_L("size %d cat %d sub %d flg 0x%02x "), recSize, recCat, recSub, recFlags);
       
  3056 			pTemp += 4; 
       
  3057 					
       
  3058 			if (recFlags & BTrace::EHeader2Present)
       
  3059 				{
       
  3060 				data.AppendFormat(_L("h2 0x%08x "), LE4(pTemp));
       
  3061 				pTemp += 4;
       
  3062 				}
       
  3063 			if (recFlags & BTrace::ETimestampPresent)
       
  3064 				{
       
  3065 				data.AppendFormat(_L("ts 0x%08x "), LE4(pTemp));
       
  3066 				pTemp += 4;
       
  3067 				}
       
  3068 			if (recFlags & BTrace::ETimestamp2Present)
       
  3069 				{
       
  3070 				data.AppendFormat(_L("ts2 0x%08x "), LE4(pTemp));
       
  3071 				pTemp += 4;
       
  3072 				}
       
  3073 			if (recFlags & BTrace::EContextIdPresent)
       
  3074 				{
       
  3075 				data.AppendFormat(_L("cId 0x%08x "), LE4(pTemp));
       
  3076 				pTemp += 4;
       
  3077 				}
       
  3078 			TInt index;
       
  3079 			for (index = 0; index < 4; index ++)
       
  3080 				{
       
  3081 				if (recSize > pTemp - pThis)
       
  3082 					{
       
  3083 					addr[index] = LE4(pTemp);
       
  3084 					pTemp += 4;
       
  3085 					}
       
  3086 				else
       
  3087 					addr[index] = 0;
       
  3088 				}
       
  3089 
       
  3090 			switch(recCat)
       
  3091 				{
       
  3092 				case BTrace::EPaging:
       
  3093 					{
       
  3094 					switch (recSub)
       
  3095 						{
       
  3096 						case BTrace::EPagingPageInBegin:
       
  3097 						/**
       
  3098 						- 4 bytes containing the virtual address which was accessed, causing this paging event.
       
  3099 						- 4 bytes containing the virtual address of the instuction which caused this paging event.
       
  3100 						  (The PC value.)
       
  3101 						**/
       
  3102 						test.Printf(_L("PageInBegin    : %S addr 0x%08x inst 0x%08x\n"), &data, addr[0], addr[1]);
       
  3103 						break;
       
  3104 
       
  3105 						/**
       
  3106 						- 0 bytes. (No extra data.)
       
  3107 						*/
       
  3108 						case BTrace::EPagingPageInUnneeded:
       
  3109 						test.Printf(_L("PageInUnneeded : %S\n"), &data);
       
  3110 						break;
       
  3111 
       
  3112 						/**
       
  3113 						- 4 bytes containing the physical address of the page 'paged in'.
       
  3114 						- 4 bytes containing the virtual address of the page 'paged in'.
       
  3115 						*/
       
  3116 						case BTrace::EPagingPageInROM:
       
  3117 						test.Printf(_L("PageInROM      : %S phys 0x%08x virt 0x%08x\n"), &data, addr[0], addr[1]);
       
  3118 						break;
       
  3119 
       
  3120 						/**
       
  3121 						- 4 bytes containing the physical address of the page being 'paged out'.
       
  3122 						- 4 bytes containing the virtual address of the page being 'paged out'.
       
  3123 						*/
       
  3124 						case BTrace::EPagingPageOutROM:
       
  3125 						test.Printf(_L("PageOutROM     : %S phys 0x%08x virt 0x%08x\n"), &data, addr[0], addr[1]);
       
  3126 						break;
       
  3127 
       
  3128 						/**
       
  3129 						- 4 bytes containing the physical address of the page being 'paged in'.
       
  3130 						*/
       
  3131 						case BTrace::EPagingPageInFree:
       
  3132 						test.Printf(_L("PageInFree     : %S phys 0x%08x\n"), &data, addr[0]);
       
  3133 						break;
       
  3134 
       
  3135 						/**
       
  3136 						- 4 bytes containing the physical address of the page being 'paged out'.
       
  3137 						*/
       
  3138 						case BTrace::EPagingPageOutFree:
       
  3139 						test.Printf(_L("PageOutFree    : %S phys 0x%08x\n"), &data, addr[0]);
       
  3140 						break;
       
  3141 
       
  3142 						/**
       
  3143 						- 4 bytes containing the physical address of the page being rejuvenated, (made young).
       
  3144 						- 4 bytes containing the virtual address which was accessed, causing this paging event.
       
  3145 						- 4 bytes containing the virtual address of the instuction which caused this paging event.
       
  3146 						  (The PC value.)
       
  3147 						*/
       
  3148 						case BTrace::EPagingRejuvenate:
       
  3149 						test.Printf(_L("Rejuvenate     : %S phys 0x%08x virt 0x%08x inst 0x%08x\n"), &data, addr[0], addr[1], addr[2]);
       
  3150 						break;
       
  3151 
       
  3152 						/**
       
  3153 						- 4 bytes containing the physical address of the page accessed.
       
  3154 						- 4 bytes containing the virtual address which was accessed, causing this paging event.
       
  3155 						- 4 bytes containing the virtual address of the instuction which caused this paging event.
       
  3156 						  (The PC value.)
       
  3157 						*/
       
  3158 						case BTrace::EPagingPageNop:
       
  3159 						test.Printf(_L("PageNop        : %S phys 0x%08x virt 0x%08x inst 0x%08x\n"), &data, addr[0], addr[1], addr[2]);
       
  3160 						break;
       
  3161 
       
  3162 						/**
       
  3163 						- 4 bytes containing the physical address of the page being locked.
       
  3164 						- 4 bytes containing the value of the lock count after the paged was locked.
       
  3165 						*/
       
  3166 						case BTrace::EPagingPageLock:
       
  3167 						test.Printf(_L("PageLock       : %S phys 0x%08x lock 0x%08x\n"), &data, addr[0], addr[1]);
       
  3168 						break;
       
  3169 
       
  3170 						/**
       
  3171 						- 4 bytes containing the physical address of the page being unlocked.
       
  3172 						- 4 bytes containing the value of the lock count before the paged was unlocked.
       
  3173 						*/
       
  3174 						case BTrace::EPagingPageUnlock:
       
  3175 						test.Printf(_L("PageUnlock     : %S phys 0x%08x lock 0x%08x\n"), &data, addr[0], addr[1]);
       
  3176 						break;
       
  3177 		
       
  3178 						/**
       
  3179 						- 4 bytes containing the physical address of the page being 'paged out'.
       
  3180 						- 4 bytes containing the virtual address of the page being 'paged out'.
       
  3181 						*/
       
  3182 						case BTrace::EPagingPageOutCache:
       
  3183 						test.Printf(_L("PageOutCache   : %S phys 0x%08x virt 0x%08x\n"), &data, addr[0], addr[1]);
       
  3184 						break;
       
  3185 		
       
  3186 						/**
       
  3187 						- 4 bytes containing the physical address of the page 'paged in'.
       
  3188 						- 4 bytes containing the virtual address of the page 'paged in'.
       
  3189 						*/
       
  3190 						case BTrace::EPagingPageInCode:
       
  3191 						test.Printf(_L("PageInCode     : %S phys 0x%08x virt 0x%08x\n"), &data, addr[0], addr[1]);
       
  3192 						break;
       
  3193 
       
  3194 						/**
       
  3195 						- 4 bytes containing the physical address of the page being 'paged out'.
       
  3196 						- 4 bytes containing the virtual address of the page being 'paged out'.
       
  3197 						*/
       
  3198 						case BTrace::EPagingPageOutCode:
       
  3199 						test.Printf(_L("PageOutCode    : %S phys 0x%08x virt 0x%08x\n"), &data, addr[0], addr[1]);
       
  3200 						break;
       
  3201 		
       
  3202 						/**
       
  3203 						- 4 bytes containing the physical address of the page 'paged in'.
       
  3204 						- 4 bytes containing the virtual address of the page 'paged in'.
       
  3205 						*/
       
  3206 						case BTrace::EPagingMapCode:
       
  3207 						test.Printf(_L("MapCode        : %S phys 0x%08x virt 0x%08x\n"), &data, addr[0], addr[1]);
       
  3208 						break;
       
  3209 						
       
  3210 						/**
       
  3211 						- 4 bytes containing the physical address of the page being aged, (made old).
       
  3212 						*/
       
  3213 						case BTrace::EPagingAged:
       
  3214 						test.Printf(_L("Aged           : %S phys 0x%08x\n"), &data, addr[0]);
       
  3215 						break;
       
  3216 						}
       
  3217 					}
       
  3218 				break;
       
  3219 
       
  3220 				default:
       
  3221 				
       
  3222 				break;
       
  3223 				}
       
  3224 			pTemp = BTrace::NextRecord(pThis);
       
  3225 			}
       
  3226 		bTraceHandle.DataUsed();
       
  3227 		}
       
  3228 	bTraceHandle.Close();
       
  3229 	}
       
  3230 
       
  3231 //
       
  3232 // ParseCommandLine 
       
  3233 //
       
  3234 // read the arguments passed from the command line and set global variables to 
       
  3235 // control the tests.
       
  3236 //
       
  3237 
       
  3238 TBool ParseCommandLine()
       
  3239 	{
       
  3240 	TBuf<256> args;
       
  3241 	User::CommandLine(args);
       
  3242 	TLex	lex(args);
       
  3243 	TBool	retVal = ETrue;
       
  3244 	
       
  3245 	// initially test for arguments, the parse them, if not apply some sensible defaults.
       
  3246 	TBool	foundArgs = EFalse;	
       
  3247 		
       
  3248 	FOREVER
       
  3249 		{
       
  3250 		TPtrC  token=lex.NextToken();
       
  3251 		if(token.Length()!=0)
       
  3252 			{
       
  3253 			if ((token == _L("help")) || (token == _L("-h")) || (token == _L("-?")))
       
  3254 				{
       
  3255 				DBGS_PRINT((_L("\nUsage: [ single | multiple <numThreads>] [ dll | exe | self | complete ] [func | thread | global ] [ rom | base | mixed | mall ] [reaper] [chunks|echunks|chunks+ {nochunkdata}] [prio] [media] [lowmem] [forward | backward | random | all] [loadGlobal | loadThread | loadFunc] [interleave] [d_exc] [btrace] [defrag] [noclean] [min <pages>] [max <pages>] [stressfree] [iters <iters>]\n'-' indicated infinity.\n\n")));
       
  3256 				test.Getch();
       
  3257 				}
       
  3258 			else  if (token == _L("mmc"))
       
  3259 				{
       
  3260 				TestOnlyFromMmc = ETrue;
       
  3261 				}
       
  3262 			else  if (token == _L("min"))
       
  3263 				{
       
  3264 				TPtrC val=lex.NextToken();
       
  3265 				TLex lexv(val);
       
  3266 				TInt value;
       
  3267 				lexv.Val(value);
       
  3268 				TestMinCacheSize = value * 4096;
       
  3269 				}
       
  3270 			else  if (token == _L("max"))
       
  3271 				{
       
  3272 				TPtrC val=lex.NextToken();
       
  3273 				TLex lexv(val);
       
  3274 				TInt value;
       
  3275 				lexv.Val(value);
       
  3276 				TestMaxCacheSize = value * 4096;
       
  3277 				}
       
  3278 			else  if (token == _L("interleave"))
       
  3279 				{
       
  3280 				TestInterleave = ETrue;
       
  3281 				}
       
  3282 			else if (token == _L("auto"))
       
  3283 				{
       
  3284 				TestFullAutoTest = EFalse;
       
  3285 				retVal = EFalse;
       
  3286 				}
       
  3287 			else if (token == _L("stressfree"))
       
  3288 				{
       
  3289 				TestStressFree = !TestStressFree;
       
  3290 				retVal = EFalse;
       
  3291 				}
       
  3292 			else if (token == _L("fullauto"))
       
  3293 				{
       
  3294 				TestFullAutoTest = ETrue;
       
  3295 				retVal = EFalse;
       
  3296 				}
       
  3297 			else if (token == _L("prio"))
       
  3298 				{
       
  3299 				TestPrioChange = !TestPrioChange;
       
  3300 				}
       
  3301 			else if (token == _L("media"))
       
  3302 				{
       
  3303 				TestMediaAccess = KTestMediaAccessBasic;
       
  3304 				}
       
  3305 			else if (token == _L("reaper"))
       
  3306 				{
       
  3307 				TestReaper = ETrue;
       
  3308 				}
       
  3309 			else if (token == _L("btrace"))
       
  3310 				{
       
  3311 				TestBtrace = ETrue;
       
  3312 				}
       
  3313 			else if (token == _L("defrag"))
       
  3314 				{
       
  3315 				TestDefrag = ETrue;
       
  3316 				}
       
  3317 			else if (token == _L("echunks"))
       
  3318 				{
       
  3319 				TestChunks = ETrue;
       
  3320 				TestExtremeChunks = ETrue;
       
  3321 				}
       
  3322 			else if (token == _L("chunks+"))
       
  3323 				{
       
  3324 				TestChunks = ETrue;
       
  3325 				TestChunksPlus = ETrue;
       
  3326 				}
       
  3327 			else if (token == _L("chunks"))
       
  3328 				{
       
  3329 				TestChunks = ETrue;
       
  3330 				}
       
  3331 			else if (token == _L("nochunkdata"))
       
  3332 				{
       
  3333 				TestChunkData = EFalse;
       
  3334 				}
       
  3335 			else if (token == _L("lowmem"))
       
  3336 				{
       
  3337 				TestLowMem = ETrue;
       
  3338 				}
       
  3339 			else if (token == _L("dll"))
       
  3340 				{
       
  3341 				TestLoading = TEST_DLL;
       
  3342 				}
       
  3343 			else if (token == _L("exe"))
       
  3344 				{
       
  3345 				TestLoading = TEST_EXE;
       
  3346 				}
       
  3347 			else if (token == _L("self"))
       
  3348 				{
       
  3349 				TestLoading = TEST_SELF;
       
  3350 				}
       
  3351 			else if (token == _L("complete"))
       
  3352 				{
       
  3353 				TestLoading |= TEST_EXE_SELF_DLL;
       
  3354 				}
       
  3355 			else if (token == _L("rom"))
       
  3356 				{
       
  3357 				TestWhichMedia = TEST_MEDIA_ROM;
       
  3358 				}
       
  3359 			else if (token == _L("base"))
       
  3360 				{
       
  3361 				TestWhichMedia = TEST_MEDIA_BASE;
       
  3362 				}
       
  3363 			else if (token == _L("mixed"))
       
  3364 				{
       
  3365 				TestWhichMedia |= TEST_MEDIA_ROM_BASE;
       
  3366 				}
       
  3367 			else if (token == _L("all_media"))
       
  3368 				{
       
  3369 				TestWhichMedia |= TEST_MEDIA_ALL;
       
  3370 				}
       
  3371 			else if (token == _L("debug"))
       
  3372 				{
       
  3373 				if (!TestSilent)
       
  3374 					{
       
  3375 					TestDebug = ETrue;
       
  3376 					TestPrioChange = ETrue;
       
  3377 					}
       
  3378 				}
       
  3379 			else if (token == _L("silent"))
       
  3380 				{
       
  3381 				TestSilent = ETrue;
       
  3382 				TestDebug = EFalse;
       
  3383 				}
       
  3384 			else if (token == _L("noclean"))
       
  3385 				{
       
  3386 				TestNoClean = ETrue;
       
  3387 				}
       
  3388 			else if (token == _L("d_exc"))
       
  3389 				{
       
  3390 				TestD_Exc = ETrue;
       
  3391 				}
       
  3392 			else if (token == _L("global"))
       
  3393 				{
       
  3394 				TestLoadDllHow = TEST_DLL_GLOBAL;
       
  3395 				}	
       
  3396 			else if (token == _L("thread"))
       
  3397 				{
       
  3398 				TestLoadDllHow = TEST_DLL_THREAD;
       
  3399 				}	
       
  3400 			else if (token == _L("func"))
       
  3401 				{
       
  3402 				TestLoadDllHow = TEST_DLL_FUNC;
       
  3403 				}	
       
  3404 			else if (token == _L("single"))
       
  3405 				{
       
  3406 				TestSingle = ETrue;
       
  3407 				}
       
  3408 			else if (token == _L("multiple"))
       
  3409 				{
       
  3410 				TPtrC val=lex.NextToken();
       
  3411 				TLex lexv(val);
       
  3412 				TInt value;
       
  3413 
       
  3414 				if (lexv.Val(value)==KErrNone)
       
  3415 					{
       
  3416 					if ((value <= 0) || (value > 100))
       
  3417 						{
       
  3418 						TestMultipleThreadCount = 10;
       
  3419 						}
       
  3420 					else
       
  3421 						{
       
  3422 						TestMultipleThreadCount = value;
       
  3423 						}
       
  3424 					}
       
  3425 				else
       
  3426 					{
       
  3427 					DBGS_PRINT((_L("Bad value for thread count '%S' was ignored.\n"), &val));
       
  3428 					retVal = EFalse;
       
  3429 					break;
       
  3430 					}
       
  3431 				TestMultiple = ETrue;
       
  3432 				}
       
  3433 			else if (token == _L("forward"))
       
  3434 				{
       
  3435 				TestWhichTests = TEST_FORWARD;
       
  3436 				}
       
  3437 			else if (token == _L("backward"))
       
  3438 				{
       
  3439 				TestWhichTests = TEST_BACKWARD;
       
  3440 				}
       
  3441 			else if (token == _L("random"))
       
  3442 				{
       
  3443 				TestWhichTests = TEST_RANDOM;
       
  3444 				}
       
  3445 			else if (token == _L("all"))
       
  3446 				{
       
  3447 				TestWhichTests = TEST_ALL;
       
  3448 				}
       
  3449 			else  if (token == _L("inst"))
       
  3450 				{
       
  3451 				TPtrC val=lex.NextToken();
       
  3452 				TLex lexv(val);
       
  3453 				TInt value;
       
  3454 
       
  3455 				if (lexv.Val(value)==KErrNone)
       
  3456 					{
       
  3457 					TestInstanceId = value;
       
  3458 					}
       
  3459 				}
       
  3460 			else  if (token == _L("iters"))
       
  3461 				{
       
  3462 				TPtrC val=lex.NextToken();
       
  3463 				TLex lexv(val);
       
  3464 				TInt value;
       
  3465 
       
  3466 				if (val==_L("-"))
       
  3467 					{
       
  3468 					TestMaxLoops = KMaxTInt;
       
  3469 					}
       
  3470 				else
       
  3471 					{
       
  3472 					if (lexv.Val(value)==KErrNone)
       
  3473 						{
       
  3474 						TestMaxLoops = value;
       
  3475 						}
       
  3476 					else
       
  3477 						{
       
  3478 						DBGS_PRINT((_L("Bad value for thread count '%S' was ignored.\n"), &val));
       
  3479 						retVal = EFalse;
       
  3480 						break;
       
  3481 						}
       
  3482 					}
       
  3483 				}
       
  3484 			else
       
  3485 				{
       
  3486 				if ((foundArgs == EFalse) && (token.Length() == 1))
       
  3487 					{
       
  3488 					// Single letter argument...only run on 'd'
       
  3489 					if (token.CompareF(_L("d")) == 0)
       
  3490 						{
       
  3491 
       
  3492 						TestFullAutoTest = EFalse;
       
  3493 						TestIsAutomated = ETrue;
       
  3494 						break;
       
  3495 						}
       
  3496 					else
       
  3497 						{
       
  3498 						if (!TestSilent)
       
  3499 							{
       
  3500 							test.Title();
       
  3501 							test.Start(_L("Skipping non drive 'd' - Test Exiting."));
       
  3502 							test.End();
       
  3503 							}
       
  3504 						foundArgs = ETrue;
       
  3505 						TestExit = ETrue;
       
  3506 						break;
       
  3507 						}
       
  3508 					}
       
  3509 				DBGS_PRINT((_L("Unknown argument '%S' was ignored.\n"), &token));
       
  3510 				break;
       
  3511 				}
       
  3512 			foundArgs = ETrue;
       
  3513 			}
       
  3514 		else
       
  3515 			{
       
  3516 			break;
       
  3517 			}
       
  3518 		}
       
  3519 	if (!foundArgs)
       
  3520 		{
       
  3521 		retVal = EFalse;
       
  3522 		}
       
  3523 	return retVal;
       
  3524 	}
       
  3525 
       
  3526 //
       
  3527 // AreWeTheTestBase
       
  3528 //
       
  3529 // Test whether we are the root of the tests.
       
  3530 //
       
  3531 void AreWeTheTestBase()
       
  3532 	{
       
  3533 	if (!TestSilent)
       
  3534 		{
       
  3535 		TFileName  filename(RProcess().FileName());
       
  3536 
       
  3537 		TParse	myParse;
       
  3538 		myParse.Set(filename, NULL, NULL);
       
  3539 		TestNameBuffer.Zero();
       
  3540 		TestNameBuffer.Append(myParse.Name());
       
  3541 		TestNameBuffer.Append(_L(".exe"));
       
  3542 
       
  3543 		TestWeAreTheTestBase = !TestNameBuffer.Compare(TestPlExeNames[KTestMediaBase]);
       
  3544 
       
  3545 		RFs fs;
       
  3546 		if (KErrNone == fs.Connect())
       
  3547 			{
       
  3548 			TEntry  anEntry;
       
  3549 			TInt retVal = fs.Entry(_L("z:\\test\\mmcdemandpaginge32tests.bat"), anEntry);
       
  3550 			if (retVal == KErrNone)
       
  3551 				{
       
  3552 				TestBootedFromMmc = ETrue;
       
  3553 				}
       
  3554 			else
       
  3555 				{
       
  3556 				TestBootedFromMmc = EFalse;
       
  3557 				}
       
  3558 			fs.Close();
       
  3559 			}
       
  3560 		}
       
  3561 	else
       
  3562 		{
       
  3563 		TestNameBuffer.Zero();
       
  3564 		TestNameBuffer.Append(_L("t_pageldrtst.exe"));
       
  3565 		}
       
  3566 	}
       
  3567 #define  MEDNONE	KTestMediaAccessNone
       
  3568 #define MEDBASIC	KTestMediaAccessBasic
       
  3569 #define MEDMTHRE	KTestMediaAccessMultipleThreads
       
  3570 #define MEDMPATT	KTestMediaAccessMultiplePattern
       
  3571 #define MEDMIX		KTestMediaAccessMixed
       
  3572 
       
  3573 TTheTests TheAutoTests[] =
       
  3574 	{// fullOnly,           loading,               media,  multi, loops, threads,         loadHow,  inter,   prio,    media,  whichTests, lowmem, free, testName
       
  3575 #ifdef TEST_SHORT_TEST
       
  3576 		{ EFalse,          TEST_DLL,      TEST_MEDIA_ALL,  ETrue,     2,      24,   TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("DLL Load (ALL Media) Multiple thread all."), },
       
  3577 #else
       
  3578 		{ EFalse,          TEST_DLL,     TEST_MEDIA_BASE,  ETrue,     5,      24,   TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_THRASH, EFalse,    0, }, //_L("DLL Load (ROM) Multiple thread Thrash."), },
       
  3579 		{  ETrue,          TEST_DLL, TEST_MEDIA_ROM_BASE, EFalse,     5,      20,   TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_ALL,    EFalse,    0, }, //_L("DLL Load (ROM/ROFS) Single thread all."), },
       
  3580 		{  ETrue,          TEST_EXE, TEST_MEDIA_ROM_BASE, EFalse,     5,      20,   TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_ALL,    EFalse,    0, }, //_L("Exe Load (ROM/ROFS) Single thread."), },
       
  3581 		{  ETrue,         TEST_SELF, TEST_MEDIA_ROM_BASE, EFalse,     5,      20,   TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_ALL,    EFalse,    0, }, //_L("Self Load (ROM/ROFS) Single thread."), },
       
  3582 		{  ETrue,          TEST_DLL, TEST_MEDIA_ROM_BASE,  ETrue,     5,      20,   TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("DLL Load (ROM/ROFS) Multiple thread all."), },
       
  3583 		{ EFalse,          TEST_DLL,      TEST_MEDIA_ALL,  ETrue,     3,      20,   TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("DLL Load (ALL Media) Multiple thread all."), },
       
  3584 		{  ETrue, TEST_EXE_SELF_DLL,      TEST_MEDIA_ALL,  ETrue,     2,      16,   TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("DLL/EXE/SELF Load (All Media) Multiple threads."), },
       
  3585 		{ EFalse, TEST_EXE_SELF_DLL,      TEST_MEDIA_ALL,  ETrue,     2,      16,   TEST_DLL_FUNC, EFalse,  ETrue,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("DLL/EXE/SELF Load (All Media) Multiple threads with prio."), },
       
  3586 		{ EFalse, TEST_EXE_SELF_DLL,      TEST_MEDIA_ALL,  ETrue,     2,      10,   TEST_DLL_FUNC, EFalse, EFalse, MEDBASIC, TEST_RANDOM, EFalse,    0, }, //_L("DLL/EXE/SELF Load (All Media) Multiple threads with media access."), },
       
  3587 		{ EFalse, TEST_EXE_SELF_DLL,      TEST_MEDIA_ALL,  ETrue,     2,      12,   TEST_DLL_FUNC, EFalse,  ETrue, MEDBASIC, TEST_RANDOM, EFalse,    0, }, //_L("DLL/EXE/SELF Load (All Media) Multiple threads with media access and prio."), },
       
  3588 		{  ETrue, TEST_EXE_SELF_DLL,      TEST_MEDIA_ALL,  ETrue,     2,      16, TEST_DLL_THREAD, EFalse, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("DLL/EXE/SELF Load thread (All Media) Multiple threads."), },
       
  3589 		{ EFalse, TEST_EXE_SELF_DLL,      TEST_MEDIA_ALL,  ETrue,     2,      16, TEST_DLL_THREAD, EFalse,  ETrue,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("DLL/EXE/SELF Load thread (All Media) Multiple threads with prio."), },
       
  3590 		{  ETrue, TEST_EXE_SELF_DLL,      TEST_MEDIA_ALL,  ETrue,     2,      12, TEST_DLL_THREAD, EFalse,  ETrue, MEDBASIC, TEST_RANDOM, EFalse,    0, }, //_L("DLL/EXE/SELF Load thread (All Media) Multiple threads with media access and prio."), },
       
  3591 		{  ETrue, TEST_EXE_SELF_DLL,      TEST_MEDIA_ALL,  ETrue,     2,      16, TEST_DLL_GLOBAL, EFalse, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("DLL/EXE/SELF Load global (All Media) Multiple threads."), },
       
  3592 		{ EFalse, TEST_EXE_SELF_DLL,      TEST_MEDIA_ALL,  ETrue,     2,      16, TEST_DLL_GLOBAL, EFalse,  ETrue,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("DLL/EXE/SELF Load global (All Media) Multiple threads with prio."), },
       
  3593 		{  ETrue, TEST_EXE_SELF_DLL,      TEST_MEDIA_ALL,  ETrue,     2,      12, TEST_DLL_GLOBAL, EFalse,  ETrue, MEDBASIC, TEST_RANDOM, EFalse,    0, }, //_L("DLL/EXE/SELF Load global (All Media) Multiple threads with media access and prio."), },
       
  3594 		{ EFalse, TEST_EXE_SELF_DLL,      TEST_MEDIA_ALL,  ETrue,     2,      16,   TEST_DLL_FUNC,  ETrue, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("DLL/EXE/SELF Load (All Media) Multiple threads with interleave."), },
       
  3595 		{  ETrue, TEST_EXE_SELF_DLL,      TEST_MEDIA_ALL,  ETrue,     2,      16,   TEST_DLL_FUNC,  ETrue,  ETrue,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("DLL/EXE/SELF Load (All Media) Multiple threads with interleave, prio."), },
       
  3596 
       
  3597 		{  ETrue, TEST_EXE_SELF_DLL,      TEST_MEDIA_ALL,  ETrue,     2,      16,   TEST_DLL_FUNC,  ETrue,  ETrue, MEDBASIC, TEST_RANDOM, EFalse,    0, }, //_L("DLL/EXE/SELF Load (All Media) Multiple threads with interleave, media and prio."), },
       
  3598 		{  ETrue, TEST_EXE_SELF_DLL,      TEST_MEDIA_ALL,  ETrue,     2,      12,   TEST_DLL_FUNC,  ETrue,  ETrue, MEDMTHRE, TEST_RANDOM, EFalse,    0, }, //_L("DLL/EXE/SELF Load (All Media) Multiple threads with interleave, multi media and prio."), },
       
  3599 		{  ETrue, TEST_EXE_SELF_DLL,      TEST_MEDIA_ALL,  ETrue,     2,      12,   TEST_DLL_FUNC,  ETrue,  ETrue, MEDMPATT, TEST_RANDOM, EFalse,    0, }, //_L("DLL/EXE/SELF Load (All Media) Multiple threads with interleave, media and prio."), },
       
  3600 		{  ETrue, TEST_EXE_SELF_DLL,      TEST_MEDIA_ALL,  ETrue,     2,      12,   TEST_DLL_FUNC,  ETrue,  ETrue,   MEDMIX, TEST_RANDOM, EFalse,    0, }, //_L("DLL/EXE/SELF Load (All Media) Multiple threads with interleave, media and prio."), },
       
  3601 		{ EFalse, TEST_EXE_SELF_DLL, TEST_MEDIA_ROM_BASE,  ETrue,     2,      10,   TEST_DLL_FUNC,  ETrue,  ETrue,   MEDMIX, TEST_RANDOM, EFalse,    0, }, //_L("DLL/EXE/SELF Load Multiple threads with interleave, media and prio."), },
       
  3602 #endif // TEST_SHORT_TEST
       
  3603 	};
       
  3604 #define NUM_AUTO_TESTS (TInt)(sizeof(TheAutoTests) / sizeof(TTheTests))
       
  3605 
       
  3606 //
       
  3607 // PerformAutoTest
       
  3608 //
       
  3609 // The autotest.
       
  3610 //
       
  3611 
       
  3612 void PerformAutoTest(TBool aReduceTime = EFalse)
       
  3613 	{
       
  3614 	TInt        testIndex;
       
  3615 	TTheTests  *pTest = &TheAutoTests[0];
       
  3616 	
       
  3617 	DoStats();
       
  3618 
       
  3619 	for (testIndex = 0; testIndex < NUM_AUTO_TESTS; testIndex ++, pTest++)
       
  3620 		{
       
  3621 		if (   (   !TestWeAreTheTestBase 
       
  3622 			    && (   (pTest->testLoadDllHow != TEST_DLL_FUNC)
       
  3623 				    || !pTest->testMultiple))
       
  3624 			|| ((TestFullAutoTest == EFalse) && (pTest->testFullAutoOnly)))
       
  3625 			{
       
  3626 			continue;
       
  3627 			}
       
  3628 		
       
  3629 		TestLoading             = pTest->testLoading;
       
  3630 		TestWhichMedia          = pTest->testWhichMedia;
       
  3631 		TestMaxLoops            = aReduceTime ? 1 : pTest->testMaxLoops;
       
  3632 		TestMultipleThreadCount = aReduceTime ? 10 : pTest->testMultipleThreadCount;
       
  3633 		TestLoadDllHow          = pTest->testLoadDllHow;
       
  3634 		TestInterleave          = pTest->testInterleave;
       
  3635 		TestPrioChange          = pTest->testPrioChange;
       
  3636 		TestMediaAccess         = pTest->testMediaAccess;
       
  3637 		if (aReduceTime && (TestMediaAccess != MEDBASIC) && (TestMediaAccess != MEDNONE))
       
  3638 			{
       
  3639 			continue;
       
  3640 			}
       
  3641 		TestWhichTests          = pTest->testWhichTests;		
       
  3642 		TestingLowMem			= pTest->testLowMem;
       
  3643 		if (!TestSilent)
       
  3644 			{
       
  3645 			test.Next(_L("Auto Test"));
       
  3646 			}
       
  3647 		if (pTest->testMultiple)
       
  3648 			{
       
  3649 			RUNTEST(DoMultipleTest(ETrue), KErrNone);
       
  3650 			}
       
  3651 		else
       
  3652 			{
       
  3653 			RUNTEST(DoSingleTest(ETrue), KErrNone);
       
  3654 			}
       
  3655 
       
  3656 		DoStats();
       
  3657 
       
  3658 #ifdef TEST_KERN_HEAP
       
  3659 		__KHEAP_MARK;
       
  3660 		__KHEAP_CHECK(0);
       
  3661 		__KHEAP_MARKEND;
       
  3662 #endif
       
  3663 		}
       
  3664 #ifdef TEST_KERN_HEAP
       
  3665 	__KHEAP_MARK;
       
  3666 	__KHEAP_CHECK(0);
       
  3667 	__KHEAP_MARKEND;
       
  3668 #endif
       
  3669 	}
       
  3670 
       
  3671 TTheTests TheLowMemTests[] =
       
  3672 	{// fullOnly,           loading,               media,  multi, loops, threads,       loadHow,  inter,   prio,    media,  whichTests, lowmem, free, testName
       
  3673 #ifndef TEST_SHORT_TEST
       
  3674 		{  ETrue, TEST_EXE_SELF_DLL, TEST_MEDIA_ROM_BASE, EFalse,     1,       1, TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("Single thread with Low memory (init)."), },
       
  3675 		{  ETrue, TEST_EXE_SELF_DLL, TEST_MEDIA_ROM_BASE, EFalse,     5,       1, TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM,  ETrue,    0, }, //_L("Single thread with Low memory."), },
       
  3676 		{  ETrue,          TEST_DLL, TEST_MEDIA_ROM_BASE, EFalse,     1,       1, TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("Multiple thread with Low memory (init)."), },
       
  3677 		{  ETrue, TEST_EXE_SELF_DLL, TEST_MEDIA_ROM_BASE,  ETrue,     2,      16, TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM,  ETrue,    0, }, //_L("Multiple thread with Low memory ."), },
       
  3678 		{ EFalse,          TEST_DLL, TEST_MEDIA_ALL,      EFalse,     5,       1, TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("Multiple thread with Low memory and All media(init)."), },
       
  3679 		{ EFalse, TEST_EXE_SELF_DLL, TEST_MEDIA_ALL,       ETrue,     2,      12, TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM,  ETrue,    0, }, //_L("Multiple thread with Low memory and All media."), },
       
  3680 		{  ETrue,          TEST_DLL, TEST_MEDIA_ROM_BASE, EFalse,     1,       1, TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("Multiple thread with Low memory, with starting free ram (init)."), },
       
  3681 		{  ETrue, TEST_EXE_SELF_DLL, TEST_MEDIA_ROM_BASE,  ETrue,     2,      16, TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM,  ETrue,   32, }, //_L("Multiple thread with Low memory, with starting free ram."), },
       
  3682 		{  ETrue,          TEST_DLL, TEST_MEDIA_ROM_BASE, EFalse,     1,      16, TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("Multiple thread with Low memory and prio and media access(init)."), },
       
  3683 		{  ETrue, TEST_EXE_SELF_DLL, TEST_MEDIA_ROM_BASE,  ETrue,     2,      16, TEST_DLL_FUNC, EFalse,  ETrue, MEDBASIC, TEST_RANDOM,  ETrue,    0, }, //_L("Multiple thread with Low memory and prio and media access."), },
       
  3684 		{  ETrue,          TEST_DLL, TEST_MEDIA_ROM_BASE, EFalse,     1,       1, TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("Multiple thread with Low memory interleave, prio and media access(init)."), },
       
  3685 		{  ETrue, TEST_EXE_SELF_DLL, TEST_MEDIA_ROM_BASE,  ETrue,     2,      16, TEST_DLL_FUNC,  ETrue,  ETrue, MEDBASIC, TEST_RANDOM,  ETrue,    0, }, //_L("Multiple thread with Low memory interleave, prio and media access."), },
       
  3686 		{  ETrue,          TEST_DLL, TEST_MEDIA_ROM_BASE, EFalse,     1,       1, TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("Multiple thread with Low memory interleave, media access and All media (init)."), },
       
  3687 		{  ETrue, TEST_EXE_SELF_DLL, TEST_MEDIA_ALL,       ETrue,     2,      16, TEST_DLL_FUNC,  ETrue, EFalse, MEDBASIC, TEST_RANDOM,  ETrue,    0, }, //_L("Multiple thread with Low memory interleave, media access and All media + loading."), },
       
  3688 		{ EFalse,		   TEST_DLL, TEST_MEDIA_ROM_BASE, EFalse,    10,       1, TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("Single thread with Low memory (init)."), },
       
  3689 		{ EFalse,          TEST_DLL, TEST_MEDIA_ROM_BASE, EFalse,     5,       1, TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM,  ETrue,    0, }, //_L("Single thread with Low memory."), },
       
  3690 #endif //TEST_SHORT_TEST
       
  3691 		{ EFalse,          TEST_DLL, TEST_MEDIA_ROM_BASE, EFalse,    10,       1, TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("Multiple thread with Low memory interleave, prio, media access and All media (init)."), },
       
  3692 		{ EFalse, TEST_EXE_SELF_DLL, TEST_MEDIA_ROM_BASE,  ETrue,     2,      16, TEST_DLL_FUNC,  ETrue,  ETrue, MEDBASIC, TEST_RANDOM,  ETrue,    0, }, //_L("Multiple thread with Low memory interleave, prio, media access and All media + loading."), },
       
  3693 		{ EFalse,          TEST_DLL, TEST_MEDIA_ROM_BASE, EFalse,     5,       1, TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("Multiple thread with Low memory interleave, prio, media access and All media (init)."), },
       
  3694 		{ EFalse, TEST_EXE_SELF_DLL, TEST_MEDIA_ROM_BASE,  ETrue,     2,      10, TEST_DLL_FUNC,  ETrue,  ETrue, MEDMTHRE, TEST_RANDOM,  ETrue,    0, }, //_L("Multiple thread with Low memory interleave, prio, multi media access and All media + loading."), },
       
  3695 		{  ETrue,          TEST_DLL, TEST_MEDIA_ROM_BASE, EFalse,     1,       1, TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("Multiple thread with Low memory interleave, prio, media access and All media (init)."), },
       
  3696 		{  ETrue, TEST_EXE_SELF_DLL, TEST_MEDIA_ALL,       ETrue,     2,      16, TEST_DLL_FUNC,  ETrue,  ETrue, MEDBASIC, TEST_RANDOM,  ETrue,    0, }, //_L("Multiple thread with Low memory interleave, prio, media access and All media + loading."), },
       
  3697 		{  ETrue,          TEST_DLL, TEST_MEDIA_ROM_BASE, EFalse,     1,       1, TEST_DLL_FUNC, EFalse, EFalse,  MEDNONE, TEST_RANDOM, EFalse,    0, }, //_L("Multiple thread with Low memory interleave, prio, media access and All media (init)."), },
       
  3698 		{  ETrue, TEST_EXE_SELF_DLL, TEST_MEDIA_ALL,       ETrue,     2,      16, TEST_DLL_FUNC,  ETrue,  ETrue, MEDMTHRE, TEST_RANDOM,  ETrue,    0, }, //_L("Multiple thread with Low memory interleave, prio, multi media access and All media + loading."), },
       
  3699 
       
  3700 	};
       
  3701 #define NUM_LOWMEM_TESTS (TInt)(sizeof(TheLowMemTests) / sizeof(TTheTests))
       
  3702 
       
  3703 //
       
  3704 // DoLowMemTest
       
  3705 //
       
  3706 // Low Memory Test
       
  3707 //
       
  3708 void DoLowMemTest(TBool aEnableAllMedia = EFalse)
       
  3709 	{
       
  3710 	TInt r = User::LoadLogicalDevice(KPageStressTestLddName);
       
  3711 	RUNTEST1(r==KErrNone || r==KErrAlreadyExists);
       
  3712 	RUNTEST(PagestressLdd.Open(),KErrNone);
       
  3713 	RUNTEST(PagestressLdd.DoSetDebugFlag((TInt)TestDebug),KErrNone);
       
  3714 	
       
  3715 	SVMCacheInfo  tempPages;
       
  3716 	memset(&tempPages, 0, sizeof(tempPages));
       
  3717 	if (TestIsDemandPaged)
       
  3718 		{
       
  3719 		// get the old cache info
       
  3720 		UserSvr::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0);
       
  3721 		TInt minSize = 8 * 4096;
       
  3722 		TInt maxSize = 256 * 4096;
       
  3723 		UserSvr::HalFunction(EHalGroupVM,EVMHalSetCacheSize,(TAny*)minSize,(TAny*)maxSize);
       
  3724 		}
       
  3725 
       
  3726 	TInt		testIndex;
       
  3727 	TTheTests  *pTest = &TheLowMemTests[0];
       
  3728 	for (testIndex = 0; testIndex < NUM_LOWMEM_TESTS; testIndex ++, pTest++)
       
  3729 		{
       
  3730 		if (   (!aEnableAllMedia && (pTest->testWhichMedia == TEST_MEDIA_ALL))
       
  3731 		    || ((TestFullAutoTest == EFalse) && (pTest->testFullAutoOnly)))
       
  3732 			{
       
  3733 			continue;
       
  3734 			}
       
  3735 
       
  3736 		TestLoading             = pTest->testLoading;
       
  3737 		TestWhichMedia          = pTest->testWhichMedia;
       
  3738 		TestMaxLoops            = pTest->testMaxLoops;
       
  3739 		TestMultipleThreadCount = pTest->testMultipleThreadCount;
       
  3740 		TestLoadDllHow          = pTest->testLoadDllHow;
       
  3741 		TestInterleave          = pTest->testInterleave;
       
  3742 		TestPrioChange          = pTest->testPrioChange;
       
  3743 		TestMediaAccess         = pTest->testMediaAccess;
       
  3744 		TestWhichTests          = pTest->testWhichTests;		
       
  3745 		TestingLowMem			= pTest->testLowMem;
       
  3746 		if (!TestSilent)
       
  3747 			{
       
  3748 			test.Next(_L("Low Memory"));
       
  3749 			}
       
  3750 		if (pTest->testLowMem)
       
  3751 			{
       
  3752 			PagestressLdd.DoConsumeRamSetup(pTest->testFreeRam, TEST_LM_BLOCKSIZE);
       
  3753 			}
       
  3754 
       
  3755 		if (pTest->testMultiple)
       
  3756 			{
       
  3757 			RUNTEST(DoMultipleTest(pTest->testLowMem), KErrNone);
       
  3758 			}
       
  3759 		else
       
  3760 			{
       
  3761 			RUNTEST(DoSingleTest(pTest->testLowMem), KErrNone);
       
  3762 			}
       
  3763 
       
  3764 		if (pTest->testLowMem)
       
  3765 			{
       
  3766 			PagestressLdd.DoConsumeRamFinish();
       
  3767 			}
       
  3768 
       
  3769 		DoStats();
       
  3770 #ifdef TEST_KERN_HEAP
       
  3771 		__KHEAP_MARK;
       
  3772 		__KHEAP_CHECK(0);
       
  3773 		__KHEAP_MARKEND;
       
  3774 #endif
       
  3775 		}
       
  3776 
       
  3777 	if (!TestSilent)
       
  3778 		{
       
  3779 		test.Next(_L("Close test driver"));
       
  3780 		}
       
  3781 	PagestressLdd.Close();
       
  3782 	RUNTEST(User::FreeLogicalDevice(KPageStressTestLddName), KErrNone);
       
  3783 
       
  3784 	if (TestIsDemandPaged)
       
  3785 		{
       
  3786 		TInt minSize = tempPages.iMinSize;
       
  3787 		TInt maxSize = tempPages.iMaxSize;
       
  3788 		UserSvr::HalFunction(EHalGroupVM,EVMHalSetCacheSize,(TAny*)minSize,(TAny*)maxSize);
       
  3789 		}
       
  3790 
       
  3791 #ifdef TEST_KERN_HEAP
       
  3792 	__KHEAP_MARK;
       
  3793 	__KHEAP_CHECK(0);
       
  3794 	__KHEAP_MARKEND;
       
  3795 #endif
       
  3796 	TestingLowMem = EFalse;
       
  3797 
       
  3798 	}
       
  3799 
       
  3800 //
       
  3801 // MultipleDefragThread
       
  3802 //
       
  3803 // Thread function, one created for each zone in a multiple thread test.
       
  3804 //
       
  3805 
       
  3806 LOCAL_C TInt MultipleDefragThread(TAny* aUseTb)
       
  3807 	{
       
  3808 	TInt numZones = 1;
       
  3809 	TInt zoneId = (TInt)aUseTb;
       
  3810 
       
  3811 	if (TestZoneCount > TEST_MAX_ZONE_THREADS)
       
  3812 		{
       
  3813 		numZones = TestZoneCount / TEST_MAX_ZONE_THREADS;
       
  3814 		}
       
  3815 
       
  3816 	while (1)
       
  3817 		{
       
  3818 		TInt index = 0;
       
  3819 		TInt tempy = 0;
       
  3820 		for (; index < numZones; index ++)
       
  3821 			{
       
  3822 			User::AfterHighRes(TEST_MAX_ZONE_THREADS*TickPeriod/4);
       
  3823 			tempy = zoneId + (TEST_MAX_ZONE_THREADS * index);
       
  3824 			if (tempy < (TInt)TestZoneCount)
       
  3825 				{
       
  3826 				RamstressLdd.DoMovePagesInZone(tempy);
       
  3827 				}
       
  3828 			if (TestDefragTestEnd)
       
  3829 				break;
       
  3830 			}
       
  3831 		if (TestDefragTestEnd)
       
  3832 			break;
       
  3833 		}	
       
  3834 	return KErrNone;
       
  3835 	}
       
  3836 
       
  3837 //
       
  3838 // DoDefragAutoTest
       
  3839 //
       
  3840 // Call the auto tests whilst defraging in the background.
       
  3841 //
       
  3842 
       
  3843 void DoDefragAutoTest()
       
  3844 	{
       
  3845 	TUint	localZoneCount = TestZoneCount;
       
  3846 	if (TestZoneCount > TEST_MAX_ZONE_THREADS)
       
  3847 		{
       
  3848 		localZoneCount = TEST_MAX_ZONE_THREADS;
       
  3849 		}
       
  3850 	TInt			size =    (sizeof(RThread) * localZoneCount) 
       
  3851 							+ (sizeof(TInt) * localZoneCount);
       
  3852 	TUint8*			pBuf = (TUint8*)User::AllocZ(size);
       
  3853 
       
  3854 	test(pBuf != NULL);
       
  3855 	RThread			*pTheThreads = (RThread*)pBuf;
       
  3856 	TInt			*pThreadInUse = (TInt*)(pTheThreads + localZoneCount);
       
  3857 	TInt			 ret;
       
  3858 	TUint			 index;
       
  3859 	for (index = 0; index < localZoneCount; index ++)
       
  3860 		{
       
  3861 		DBGS_PRINT((_L("%S : Starting Defrag Thread %d\n"), &TestNameBuffer, index));
       
  3862 		ret = pTheThreads[index].Create(KTestBlank,MultipleDefragThread,KDefaultStackSize,NULL,(TAny*) index);
       
  3863 		if (ret == KErrNone)
       
  3864 			{
       
  3865 			pTheThreads[index].Resume();
       
  3866 			pThreadInUse[index] = 1;
       
  3867 			}
       
  3868 		else
       
  3869 			{
       
  3870 			DBGS_PRINT((_L("%S : Starting Defrag Thread Failed %d\n"), &TestNameBuffer, index));
       
  3871 			}
       
  3872 		}
       
  3873 
       
  3874 	// Do the full auto tests...
       
  3875 	PerformAutoTest(TestIsDemandPaged);
       
  3876 
       
  3877 	TestDefragTestEnd = ETrue;
       
  3878 	RamstressLdd.DoSetEndFlag(1);
       
  3879 	TBool	anyUsed = ETrue;
       
  3880 
       
  3881 	DBGS_PRINT((_L("%S : Waiting for Defrag Threads to exit...\n"), &TestNameBuffer));	
       
  3882 	TUint killNext = User::TickCount();
       
  3883 	while(anyUsed)
       
  3884 		{
       
  3885 		anyUsed = EFalse;
       
  3886 		
       
  3887 		// walk through the thread list to check which are still alive.
       
  3888 		for (index = 0; index < localZoneCount; index++)
       
  3889 			{
       
  3890 			if (pThreadInUse[index])
       
  3891 				{
       
  3892 				if (pTheThreads[index].ExitType() != EExitPending)
       
  3893 					{
       
  3894 					if (pTheThreads[index].ExitType() == EExitPanic)
       
  3895 						{
       
  3896 						DBGS_PRINT((_L("%S : Defrag Thread %d Panic'd\n"), &TestNameBuffer, index));	
       
  3897 						}
       
  3898 					else
       
  3899 						{
       
  3900 						DBGS_PRINT((_L("%S : Defrag Thread %d Exited\n"), &TestNameBuffer, index));	
       
  3901 						}
       
  3902 					pTheThreads[index].Close();
       
  3903 					pThreadInUse[index] = EFalse;
       
  3904 					}
       
  3905 				else
       
  3906 					{
       
  3907 					anyUsed = ETrue;
       
  3908 					TUint now = User::TickCount();
       
  3909 					TUint time = TUint((TUint64)(now-killNext)*(TUint64)TickPeriod/(TUint64)1000000);
       
  3910 					const TUint killTimeStep = (TEST_DOT_PERIOD+9)/10; // 1/10th of a dot
       
  3911 					if(time>TEST_DOT_PERIOD+killTimeStep)
       
  3912 						{
       
  3913 						killNext += killTimeStep*1000000/TickPeriod;
       
  3914 						DBGS_PRINT((_L("%S : killing Defrag Thread %d\n"), &TestNameBuffer, index));	
       
  3915 						pTheThreads[index].Kill(KErrNone);
       
  3916 						pTheThreads[index].Close();
       
  3917 						pThreadInUse[index] = EFalse;
       
  3918 						}
       
  3919 					}
       
  3920 				}
       
  3921 			}
       
  3922 		User::After(500000);
       
  3923 		}
       
  3924 	DBGS_PRINT((_L("%S : Defrag Threads exited...\n"), &TestNameBuffer));	
       
  3925 	RamstressLdd.DoSetEndFlag(0);
       
  3926 	User::Free(pBuf);
       
  3927 	}
       
  3928 
       
  3929 //
       
  3930 // DoDefragTest
       
  3931 //
       
  3932 // Test the ram defrag code.
       
  3933 //
       
  3934 
       
  3935 void DoDefragTest(void)
       
  3936 	{
       
  3937 	SVMCacheInfo  tempPages;
       
  3938 	memset(&tempPages, 0, sizeof(tempPages));
       
  3939 
       
  3940 	test.Next(_L("Ram Defrag : Get the number of zones"));
       
  3941 	// first get the number of zones
       
  3942 	TInt ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneCount,&TestZoneCount,0);
       
  3943 	if(ret==KErrNotSupported)
       
  3944 		{
       
  3945 		test.Next(_L("TESTS NOT RUN - Ram Defrag appears to not be supported.\n"));
       
  3946 		return;
       
  3947 		}
       
  3948 	test(ret == KErrNone);
       
  3949 	test(TestZoneCount != 0);
       
  3950 	test.Printf(_L("RAM Zones (count=%u)\n"),TestZoneCount);
       
  3951 
       
  3952 	// now get the config of each of the zones.
       
  3953 	TUint						index;
       
  3954 	struct SRamZoneConfig		config;
       
  3955 	struct SRamZoneUtilisation	util;
       
  3956 	test.Next(_L("Ram Defrag : Get info about the zones"));
       
  3957 	for (index = 0; index < TestZoneCount; index ++)
       
  3958 		{
       
  3959 		ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneConfig,(TAny*)index, (TAny*)&config);
       
  3960 		test(ret == KErrNone);
       
  3961 		test.Printf(_L("config : id=%d index=%d base=0x%08x end=0x%08x pages=%d pref=%d flags=0x%x\n"),
       
  3962 					config.iZoneId,config.iZoneIndex,config.iPhysBase,config.iPhysEnd,config.iPhysPages, 
       
  3963 					config.iPref,config.iFlags);
       
  3964 
       
  3965 		ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneUtilisation,(TAny*)index, (TAny*)&util);
       
  3966 		test(ret == KErrNone);
       
  3967 		test.Printf(_L("usage  : id=%d index=%d pages=%d free=%d unknown=%d fixed=%d move=%d discard=%d other=%d\n"),
       
  3968 					util.iZoneId,util.iZoneIndex,util.iPhysPages,util.iFreePages,
       
  3969 					util.iAllocUnknown,util.iAllocFixed,util.iAllocMovable,util.iAllocDiscardable,util.iAllocOther);
       
  3970 		}
       
  3971 	// Now test for zones out of range.
       
  3972 	test.Next(_L("Ram Defrag : test out of range indexes"));
       
  3973 	ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneConfig,(TAny*)(TestZoneCount + 1), (TAny*)&config);
       
  3974 	test(ret != KErrNone);
       
  3975 	ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneUtilisation,(TAny*)(TestZoneCount + 1), (TAny*)&util);
       
  3976 	test(ret != KErrNone);
       
  3977 
       
  3978 	ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneConfig,(TAny*)-1, (TAny*)&config);
       
  3979 	test(ret != KErrNone);
       
  3980 	ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneUtilisation,(TAny*)-1, (TAny*)&util);
       
  3981 	test(ret != KErrNone);
       
  3982 	test.Next(_L("Ram Defrag : test out of range enums"));
       
  3983 	ret = UserSvr::HalFunction(EHalGroupRam,-1, 0, 0);
       
  3984 	test(ret != KErrNone);
       
  3985 	ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneUtilisation + 1,0, 0);
       
  3986 	test(ret != KErrNone);
       
  3987 
       
  3988 	TInt r = User::LoadLogicalDevice(KRamStressTestLddName);
       
  3989 	RUNTEST1(r==KErrNone || r==KErrAlreadyExists);
       
  3990 	RUNTEST(RamstressLdd.Open(),KErrNone);
       
  3991 	//TestDebug = ETrue;
       
  3992 	RUNTEST(RamstressLdd.DoSetDebugFlag((TInt)TestDebug),KErrNone);
       
  3993 
       
  3994 	test.Next(_L("Ram Defrag : set VM cache to stress free..."));
       
  3995 
       
  3996 	if (TestIsDemandPaged)
       
  3997 		{
       
  3998 		// get the old cache info
       
  3999 		UserSvr::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0);
       
  4000 
       
  4001 		TInt minSize = 512 * 4096;
       
  4002 		TInt maxSize = 32767 * 4096;
       
  4003 		UserSvr::HalFunction(EHalGroupVM,EVMHalSetCacheSize,(TAny*)minSize,(TAny*)maxSize);
       
  4004 		}
       
  4005 
       
  4006 	test.Next(_L("Ram Defrag : move all pages in all zone in 1 thread..."));
       
  4007 
       
  4008 	for (index = 0; index < TestZoneCount; index ++)
       
  4009 		{
       
  4010 		test.Printf(_L("Ram Defrag : moving pages in zone %u\n"),index);
       
  4011 		ret = RamstressLdd.DoMovePagesInZone(index);
       
  4012 		if (ret != KErrNone)
       
  4013 			{
       
  4014 			test.Printf(_L("Ram Defrag : moving pages in zone failed %u err=%d\n"), index, ret);
       
  4015 			}
       
  4016 		}
       
  4017 
       
  4018 
       
  4019 	test.Next(_L("Ram Defrag : Get info after test"));
       
  4020 	for (index = 0; index < TestZoneCount; index ++)
       
  4021 		{
       
  4022 		ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneUtilisation,(TAny*)index, (TAny*)&util);
       
  4023 		test(ret == KErrNone);
       
  4024 		test.Printf(_L("usage  : id=%d index=%d pages=%d free=%d unknown=%d fixed=%d move=%d discard=%d other=%d\n"),
       
  4025 					util.iZoneId,util.iZoneIndex,util.iPhysPages,util.iFreePages,
       
  4026 					util.iAllocUnknown,util.iAllocFixed,util.iAllocMovable,util.iAllocDiscardable,util.iAllocOther);
       
  4027 		}
       
  4028 
       
  4029 	test.Next(_L("Ram Defrag : Page moving on multiple threads with auto test running."));
       
  4030 
       
  4031 	TestingDefrag = ETrue;
       
  4032 	TestDefragTestEnd = EFalse;
       
  4033 
       
  4034 	DoDefragAutoTest();
       
  4035 	TestingDefrag = EFalse;
       
  4036 	/*
       
  4037 	 * End of test cleanup.
       
  4038 	 */
       
  4039 
       
  4040 	test.Next(_L("Ram Defrag : reset VM cache back to stressed."));
       
  4041 	if (TestIsDemandPaged)
       
  4042 		{
       
  4043 		TInt minSize = tempPages.iMinSize;
       
  4044 		TInt maxSize = tempPages.iMaxSize;
       
  4045 		UserSvr::HalFunction(EHalGroupVM,EVMHalSetCacheSize,(TAny*)minSize,(TAny*)maxSize);
       
  4046 		}
       
  4047 	RamstressLdd.Close();
       
  4048 	test.Next(_L("Ram Defrag : Done"));
       
  4049 	}
       
  4050 
       
  4051 //
       
  4052 // PerformExceptionThread
       
  4053 //
       
  4054 // Generate a Panic
       
  4055 //
       
  4056 
       
  4057 LOCAL_C TInt PerformExceptionThread(TAny* )
       
  4058 	{
       
  4059 	User::AfterHighRes(1000000);
       
  4060 	// this line will cause a Kern::Exec 0 !!!
       
  4061 	test.Printf(_L("Hello World\n"));
       
  4062 
       
  4063 	return KErrNone;
       
  4064 	}
       
  4065 
       
  4066 //
       
  4067 // DoExceptionInAnotherThread
       
  4068 //
       
  4069 // Test the d_exc and minkda functionality with faulting processes.
       
  4070 //
       
  4071 
       
  4072 void DoExceptionInAnotherThread(void)
       
  4073 	{
       
  4074 	TRequestStatus	theStatus;
       
  4075 	RThread			theThread;
       
  4076 	
       
  4077 	TInt ret = theThread.Create(KTestBlank,PerformExceptionThread,KDefaultStackSize,NULL,NULL);
       
  4078 	test(ret == KErrNone);
       
  4079 	theThread.Logon(theStatus);
       
  4080 	RUNTEST1(theStatus == KRequestPending);	
       
  4081 	theThread.Resume();
       
  4082 	theThread.Close();
       
  4083 	User::WaitForRequest(theStatus);
       
  4084 	}
       
  4085 
       
  4086 //
       
  4087 // DoTestD_Exc
       
  4088 //
       
  4089 // Test the d_exc and minkda functionality with faulting processes.
       
  4090 //
       
  4091 
       
  4092 TInt DoTestD_Exc()
       
  4093 	{
       
  4094 	if (!TestSilent)
       
  4095 		{
       
  4096 		test.Next(_L("DoTestD_Exc : d_exc check test."));
       
  4097 		}
       
  4098 	DBGS_PRINT((_L("%S : DoTestD_Exc start...\n"), &TestNameBuffer));	
       
  4099 	// first we need to spawn d_exc.exe
       
  4100 	RProcess dexcProcess;
       
  4101 	TInt ret = dexcProcess.Create(_L("d_exc.exe"),_L("-b"));
       
  4102 	RUNTEST1(KErrNone == ret);
       
  4103 	TRequestStatus dexcStatus;
       
  4104 	dexcProcess.Logon(dexcStatus);
       
  4105 	RUNTEST1(dexcStatus == KRequestPending);	
       
  4106 	dexcProcess.Resume();
       
  4107 
       
  4108 	DBGS_PRINT((_L("%S : DoTestD_Exc started d_exc.exe\n"), &TestNameBuffer));	
       
  4109 
       
  4110 	DoExceptionInAnotherThread();
       
  4111 
       
  4112 	DBGS_PRINT((_L("%S : DoTestD_Exc test completed\n"), &TestNameBuffer));	
       
  4113 	// check that d_exc and minkda don't die!
       
  4114 	RUNTEST1(dexcProcess.ExitType() == EExitPending);
       
  4115 
       
  4116 	DBGS_PRINT((_L("%S : DoTestD_Exc d_exc still running\n"), &TestNameBuffer));	
       
  4117 	
       
  4118 	// kill off d_exc!
       
  4119 	dexcProcess.Kill(KErrNone);
       
  4120 	dexcProcess.Close();
       
  4121 	User::WaitForRequest(dexcStatus);
       
  4122 	DBGS_PRINT((_L("%S : DoTestD_Exc d_exc killed and exiting\n"), &TestNameBuffer));	
       
  4123 	return KErrNone;
       
  4124 	}
       
  4125 
       
  4126 /**
       
  4127 	Get name of the hash file used for an EXE or DLL which has been
       
  4128 	copied to writable media.
       
  4129 
       
  4130 	@param	aOrigName		Name of EXE or DLL which has been copied to
       
  4131 							writable media.  This does not have to be
       
  4132 							qualified because only the name and extension
       
  4133 							are used.
       
  4134 	@param	aHashName		On return this is set to the absolute filename
       
  4135 							which should contain the file's hash.  This
       
  4136 							function does not create the file, or its containing
       
  4137 							directory.
       
  4138  */
       
  4139 
       
  4140 static void GetHashFileName(const TDesC& aOrigName, TDes& aHashName)
       
  4141 	{
       
  4142 	aHashName.Copy(KSysHash);
       
  4143 	aHashName[0] = (TUint8) RFs::GetSystemDriveChar();
       
  4144 	const TParsePtrC ppc(aOrigName);
       
  4145 	aHashName.Append(ppc.NameAndExt());
       
  4146 	}
       
  4147 
       
  4148 //
       
  4149 // HashFile
       
  4150 // take hash of files require full drive:/path/name.ext
       
  4151 //
       
  4152 
       
  4153 void HashFile(const TDesC& aFileName, RFs& aFs)
       
  4154 	{
       
  4155 	CSHA1* sha1 = CSHA1::NewL();
       
  4156 	CleanupStack::PushL(sha1);
       
  4157 	
       
  4158 	TBuf<50> hashfile;
       
  4159 	hashfile = KSysHash;
       
  4160 	hashfile[0] = (TUint8) RFs::GetSystemDriveChar();
       
  4161 	
       
  4162 	TInt r = aFs.MkDirAll(hashfile);
       
  4163 	RUNTEST1(r==KErrNone || r==KErrAlreadyExists);
       
  4164 
       
  4165 	RFile fDest;
       
  4166 	r = fDest.Open(aFs, aFileName, EFileRead | EFileStream);
       
  4167 	if (r != KErrNone)
       
  4168 		{
       
  4169 		if (TestingReaper && (r == KErrInUse))
       
  4170 			{
       
  4171 			TBool whinged = EFalse;
       
  4172 			while (r == KErrInUse)
       
  4173 				{
       
  4174 				User::After(2000000);
       
  4175 				if (!whinged)
       
  4176 					{
       
  4177 					DBGS_PRINT((_L("HashFile() retrying Open for %S (%d)\n"), &aFileName, r));
       
  4178 					whinged = ETrue;
       
  4179 					}
       
  4180 				r = fDest.Open(aFs, aFileName, EFileRead | EFileStream);
       
  4181 				}
       
  4182 
       
  4183 			}
       
  4184 		else
       
  4185 			{
       
  4186 			DBGS_PRINT((_L("fDest.Open returned %d\n"), r));
       
  4187 			}
       
  4188 		}
       
  4189 	User::LeaveIfError(r);
       
  4190 	CleanupClosePushL(fDest);
       
  4191 
       
  4192 	TBool done;
       
  4193 	TBuf8<512> content;
       
  4194 	do
       
  4195 		{
       
  4196 		r = fDest.Read(content);
       
  4197 		if (r!=KErrNone)
       
  4198 			DBGS_PRINT((_L("fDest.Read returned %d\n"), r));	
       
  4199 		User::LeaveIfError(r);
       
  4200 		done = (content.Length() == 0);
       
  4201 		if (! done)
       
  4202 			sha1->Update(content);
       
  4203 		} while (! done);
       
  4204 	CleanupStack::PopAndDestroy(&fDest);
       
  4205 
       
  4206 	// write hash to \sys\hash
       
  4207 	TBuf8<SHA1_HASH> hashVal = sha1->Final();
       
  4208 
       
  4209 	TFileName fnSrc(aFileName);
       
  4210 	GetHashFileName(aFileName, fnSrc);
       
  4211 	RFile fHash;
       
  4212 	r = fHash.Replace(aFs, fnSrc, EFileWrite | EFileStream);
       
  4213 	if (r != KErrNone)
       
  4214 		DBGS_PRINT((_L("fHash.Replace returned %d\n"), r));
       
  4215 	User::LeaveIfError(r);
       
  4216 	CleanupClosePushL(fHash);
       
  4217 	r = fHash.Write(hashVal);
       
  4218 	if (r != KErrNone)
       
  4219 		DBGS_PRINT((_L("fHash.Write returned %d\n"), r));
       
  4220 	User::LeaveIfError(r);
       
  4221 
       
  4222 	CleanupStack::PopAndDestroy(2, sha1);
       
  4223 	}
       
  4224 
       
  4225 //
       
  4226 // CopyFileToMMc
       
  4227 //
       
  4228 // Copy a file to the MMC card and create a hash of it.
       
  4229 //
       
  4230 
       
  4231 TInt CopyFileToMMc(RFs& aFs,CFileMan* aFileMan, TPtrC aPath, TPtrC  aOldFilename, TPtrC  aNewFilename)
       
  4232 	{
       
  4233 	TInt retVal = aFs.MkDirAll(aPath);
       
  4234 	RUNTEST1(retVal==KErrNone || retVal==KErrAlreadyExists);
       
  4235 
       
  4236 	TFileName newPath;
       
  4237 	TFileName oldPath;
       
  4238 
       
  4239 	oldPath.Format(_L("%S%S"),&KRomPath, &aOldFilename);
       
  4240 	newPath.Format(_L("%S%S"),&aPath, &aNewFilename);
       
  4241 	DBGD_PRINT((_L("Copying %S to %S\n"), &oldPath, &newPath));
       
  4242 	retVal = aFileMan->Copy(oldPath, newPath, CFileMan::EOverWrite);
       
  4243 	if (retVal == KErrNone)
       
  4244 		{
       
  4245 		retVal = aFileMan->Attribs(newPath, KEntryAttNormal, KEntryAttReadOnly, 0);
       
  4246 		if (retVal != KErrNone)
       
  4247 			{
       
  4248 			DBGS_PRINT((_L("%S :   Attribs failed (%d)\n"), &newPath, retVal));
       
  4249 			}
       
  4250 		TEntry  anEntry;
       
  4251 		retVal = aFs.Entry(newPath, anEntry);
       
  4252 		if (retVal != KErrNone)
       
  4253 			{
       
  4254 			DBGS_PRINT((_L("%S : aFs.Entry failed (%d)\n"), &newPath, retVal));
       
  4255 			}
       
  4256 		TRAPD(r, HashFile(newPath, aFs));
       
  4257 		RUNTEST1(r == KErrNone);
       
  4258 		}
       
  4259 	else
       
  4260 		DBGS_PRINT((_L("Failed to copy file %d\n"), retVal));
       
  4261 	DBGD_PRINT((_L("%S : now %S (%d)\n"), &newPath, EXISTS(retVal), retVal));
       
  4262 	return retVal;
       
  4263 	}
       
  4264 
       
  4265 //
       
  4266 // CopyAndFragmentFiles
       
  4267 //
       
  4268 // Copy the test files to a specified location edeavouring to fragment as much as possible.
       
  4269 //
       
  4270 
       
  4271 TBool CopyAndFragmentFiles(RFs& aFs,CFileMan* aFileMan, TPtrC aPath, ETestMediaType aMediaType)
       
  4272 	{
       
  4273 	TInt retVal = aFs.MkDirAll(aPath);
       
  4274 	RUNTEST1(retVal==KErrNone || retVal==KErrAlreadyExists);
       
  4275 #define FILECOUNTMAX (PAGELDRTST_MAX_DLLS + 2)
       
  4276 	RFile	theInFiles[FILECOUNTMAX];
       
  4277 	RFile	theOutFiles[FILECOUNTMAX];
       
  4278 	TInt	inFileSize[FILECOUNTMAX];
       
  4279 	TInt	inFilePos[FILECOUNTMAX];
       
  4280 	TBool	fileOk[FILECOUNTMAX];
       
  4281 
       
  4282 	TInt	  index;
       
  4283 	TFileName newPath;
       
  4284 	TFileName oldPath;
       
  4285 
       
  4286 	for (index = 0; index < FILECOUNTMAX; index ++)
       
  4287 		{
       
  4288 		inFileSize[index] = 0;
       
  4289 		inFilePos[index] = 0;
       
  4290 		fileOk[index] = EFalse;
       
  4291 
       
  4292 		if (index < PAGELDRTST_MAX_DLLS)
       
  4293 			{
       
  4294 			oldPath.Format(_L("%S%S%d%S"), &KRomPath, &KDllBaseName, index, &TestPlExtNames[KTestMediaBase]);
       
  4295 			newPath.Format(_L("%S%S%d%S"), &aPath, &KDllBaseName, index, &TestPlExtNames[aMediaType]);
       
  4296 			}
       
  4297 		else if (index < (PAGELDRTST_MAX_DLLS + 1))
       
  4298 			{
       
  4299 			oldPath.Format(_L("%S%S"), &KRomPath, &TestPsExeNames[KTestMediaBase]);
       
  4300 			newPath.Format(_L("%S%S"), &aPath, &TestPsExeNames[aMediaType]);
       
  4301 			}
       
  4302 		else
       
  4303 			{
       
  4304 			oldPath.Format(_L("%S%S"), &KRomPath, &TestPlExeNames[KTestMediaBase]);
       
  4305 			newPath.Format(_L("%S%S"), &aPath, &TestPlExeNames[aMediaType]);
       
  4306 			}
       
  4307 
       
  4308 		retVal = theInFiles[index].Open(aFs, oldPath, EFileRead);
       
  4309 		if (retVal != KErrNone)
       
  4310 			{
       
  4311 			DBGS_PRINT((_L("%S : Failed to open for read (%d)\n"), &oldPath, retVal));
       
  4312 			break;
       
  4313 			}
       
  4314 		retVal = theInFiles[index].Size(inFileSize[index]);
       
  4315 		if (retVal != KErrNone)
       
  4316 			{
       
  4317 			theInFiles[index].Close();
       
  4318 			DBGS_PRINT((_L("%S : Failed to get file size (%d)\n"), &newPath, retVal));
       
  4319 			break;
       
  4320 			}
       
  4321 		retVal = theOutFiles[index].Replace(aFs, newPath, EFileWrite);
       
  4322 		if (retVal != KErrNone)
       
  4323 			{
       
  4324 			theInFiles[index].Close();
       
  4325 			DBGS_PRINT((_L("%S : Failed to open for write (%d)\n"), &newPath, retVal));
       
  4326 			break;
       
  4327 			}
       
  4328 
       
  4329 		fileOk[index] = ETrue;
       
  4330 		}
       
  4331 
       
  4332 	const TInt KBufferSize = 3333;
       
  4333 	TBuf8<KBufferSize> buffer;
       
  4334 	TBool stillGoing;
       
  4335 
       
  4336 	do
       
  4337 		{
       
  4338 		stillGoing = EFalse;
       
  4339 		for (index = 0; index < FILECOUNTMAX; index ++)
       
  4340 			{
       
  4341 			if (!fileOk[index])
       
  4342 				break;
       
  4343 			if (inFilePos[index] < inFileSize[index])
       
  4344 				{
       
  4345 				retVal = theInFiles[index].Read(buffer);
       
  4346 				if (retVal != KErrNone)
       
  4347 					{
       
  4348 					DBGS_PRINT((_L("theInFiles[%d] read failed (%d)\n"), index, retVal));
       
  4349 					break;
       
  4350 					}
       
  4351 				retVal = theOutFiles[index].Write(buffer);
       
  4352 				if (retVal != KErrNone)
       
  4353 					{
       
  4354 					DBGS_PRINT((_L("theOutFiles[%d] Write failed (%d)\n"), index, retVal));
       
  4355 					break;
       
  4356 					}
       
  4357 				retVal = theOutFiles[index].Flush();
       
  4358 				if (retVal != KErrNone)
       
  4359 					{
       
  4360 					DBGS_PRINT((_L("theOutFiles[%d] flush failed (%d)\n"), index, retVal));
       
  4361 					break;
       
  4362 					}
       
  4363 				inFilePos[index] += buffer.Length();
       
  4364 				if (inFilePos[index] < inFileSize[index])
       
  4365 					stillGoing = ETrue;
       
  4366 				}
       
  4367 			}
       
  4368 		}
       
  4369 	while (stillGoing);
       
  4370 
       
  4371 	TBool allOk = retVal == KErrNone;
       
  4372 	for (index = 0; index < FILECOUNTMAX; index ++)
       
  4373 		{
       
  4374 		if (!fileOk[index])
       
  4375 			{
       
  4376 			allOk = EFalse;
       
  4377 			break;
       
  4378 			}
       
  4379 		theInFiles[index].Close();
       
  4380 		theOutFiles[index].Close();
       
  4381 		if (index < PAGELDRTST_MAX_DLLS)
       
  4382 			{
       
  4383 			newPath.Format(_L("%S%S%d%S"), &aPath, &KDllBaseName, index, &TestPlExtNames[aMediaType]);
       
  4384 			}
       
  4385 		else if (index < (PAGELDRTST_MAX_DLLS + 1))
       
  4386 			{
       
  4387 			newPath.Format(_L("%S%S"), &aPath, &TestPsExeNames[aMediaType]);
       
  4388 			}
       
  4389 		else
       
  4390 			{
       
  4391 			newPath.Format(_L("%S%S"), &aPath, &TestPlExeNames[aMediaType]);
       
  4392 			}
       
  4393 
       
  4394 		retVal = aFileMan->Attribs(newPath, KEntryAttNormal, KEntryAttReadOnly, 0);
       
  4395 		if (retVal != KErrNone)
       
  4396 			{
       
  4397 			DBGS_PRINT((_L("%S : Attribs failed (%d)\n"), &newPath, retVal));
       
  4398 			allOk = EFalse;
       
  4399 			}
       
  4400 		TEntry  anEntry;
       
  4401 		retVal = aFs.Entry(newPath, anEntry);
       
  4402 		if (retVal != KErrNone)
       
  4403 			{
       
  4404 			DBGS_PRINT((_L("%S : aFs.Entry failed (%d)\n"), &newPath, retVal));
       
  4405 			allOk = EFalse;
       
  4406 			}
       
  4407 		TRAPD(r, HashFile(newPath, aFs));
       
  4408 		if (r != KErrNone)
       
  4409 			{
       
  4410 			allOk = EFalse;
       
  4411 			}
       
  4412 		DBGD_PRINT((_L("%S : %S!\n"), &newPath, EXISTS(!allOk)));
       
  4413 		}
       
  4414 	return allOk;
       
  4415 	}
       
  4416 
       
  4417 //
       
  4418 // CheckFilePresence
       
  4419 //
       
  4420 // Checks all the files required for the test are present and copies some tests to the MMC card
       
  4421 //
       
  4422 
       
  4423 void CheckFilePresence(TBool aDoFileCopy)
       
  4424 	{
       
  4425 	TUint start = User::TickCount();
       
  4426 
       
  4427 	RFs fs;
       
  4428 	if (KErrNone != fs.Connect())
       
  4429 		{
       
  4430 		DBGS_PRINT(_L("CheckFilePresence : Can't connect to the FS\n"));
       
  4431 		return ;
       
  4432 		}
       
  4433 
       
  4434 	TFileName filename;
       
  4435 	TFileName newFilename;
       
  4436 	TEntry anEntry;
       
  4437 	TInt   index;
       
  4438 	TInt   retVal;
       
  4439 	TInt   dllIndex;
       
  4440 
       
  4441 	// now we need to add the MMC files
       
  4442 	TInt drvNum = FindMMCDriveNumber(fs);
       
  4443 	TBuf<32>	mmcPath;
       
  4444 	mmcPath.Format(_L("%S"),&KMmcDefaultPath);
       
  4445 	if (drvNum >= 0)
       
  4446 		mmcPath[0] = 'a' + drvNum;
       
  4447 
       
  4448 	TBool	allOk;
       
  4449 	//TInt  indexMax = aDoFileCopy ? KTestMediaMmc : KTestMediaCOUNT; 
       
  4450 	for (index = 0; index < TEST_MEDIA_COUNT_HACK; index ++)
       
  4451 		{
       
  4452 		allOk = ETrue;	
       
  4453 		filename.Format(_L("%S%S"),(index == KTestMediaMmc) ? & mmcPath : &KRomPath, &TestPsExeNames[index]);
       
  4454 		if (KErrNone != fs.Entry(filename, anEntry))
       
  4455 			allOk = EFalse;
       
  4456 
       
  4457 		filename.Format(_L("%S%S"),(index == KTestMediaMmc) ? & mmcPath : &KRomPath, &TestPlExeNames[index]);
       
  4458 		if (KErrNone != fs.Entry(filename, anEntry))
       
  4459 			allOk = EFalse;
       
  4460 
       
  4461 		for (dllIndex = 0; dllIndex < PAGELDRTST_MAX_DLLS; dllIndex ++)
       
  4462 			{
       
  4463 			filename.Format(_L("%S%S%d%S"), (index == KTestMediaMmc) ? & mmcPath : &KRomPath, &KDllBaseName, dllIndex, &TestPlExtNames[index]);
       
  4464 			if (KErrNone != fs.Entry(filename, anEntry))
       
  4465 				allOk = EFalse;
       
  4466 			}
       
  4467 		TestDllExesExist[index] = allOk;
       
  4468 		DBGS_PRINT((_L("%S : %S!\n"), &TestPsExeNames[index], EXISTS(!TestDllExesExist[index])));
       
  4469 		}
       
  4470 	TInt nandDrvNum = FindFsNANDDrive(fs);
       
  4471 	if (aDoFileCopy && (drvNum >= 0) && (nandDrvNum >= 0))
       
  4472 		{
       
  4473 		CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
  4474 		if(!cleanupStack)
       
  4475 			DBGS_PRINT((_L("Cleanup stack failed\n")));	
       
  4476 		CFileMan* pFileMan = NULL;
       
  4477 		TRAP(retVal, pFileMan = CFileMan::NewL(fs));
       
  4478 	
       
  4479 		// First make a clean copy of the DLLs to the MMC card.
       
  4480 		allOk = ETrue;			
       
  4481 		if (KErrNone != CopyFileToMMc(fs, pFileMan, mmcPath, TestPsExeNames[KTestMediaBase], TestPsExeNames[KTestMediaMmc]))
       
  4482 			allOk = EFalse;
       
  4483 		if (KErrNone != CopyFileToMMc(fs, pFileMan, mmcPath, TestPlExeNames[KTestMediaBase], TestPlExeNames[KTestMediaMmc]))
       
  4484 			allOk = EFalse;
       
  4485 		for (dllIndex = 0; dllIndex < PAGELDRTST_MAX_DLLS; dllIndex ++)
       
  4486 			{
       
  4487 			filename.Format(_L("%S%d%S"), &KDllBaseName, dllIndex, &TestPlExtNames[KTestMediaBase]);
       
  4488 			newFilename.Format(_L("%S%d%S"), &KDllBaseName, dllIndex, &TestPlExtNames[KTestMediaMmc]);
       
  4489 			if (KErrNone != CopyFileToMMc(fs, pFileMan, mmcPath, filename, newFilename))
       
  4490 				allOk = EFalse;
       
  4491 			}
       
  4492 		TestDllExesExist[KTestMediaMmc] = allOk;
       
  4493 		DBGS_PRINT((_L("%S : %S! (Drive %c)\n"), &TestPsExeNames[index], EXISTS(!TestDllExesExist[index]), mmcPath[0]));
       
  4494 #ifdef TEST_ADD_FRAGD_MEDIA
       
  4495 		//now make some fragmented files on the MMC card.
       
  4496 		TestDllExesExist[KTestMediaMmcFrag] = CopyAndFragmentFiles(fs, pFileMan, mmcPath, KTestMediaMmcFrag);
       
  4497 		DBGS_PRINT((_L("%S : %S! (Drive %c)\n"), &TestPsExeNames[KTestMediaMmcFrag], EXISTS(!TestDllExesExist[KTestMediaMmcFrag]), mmcPath[0]));
       
  4498 
       
  4499 		//now make some fragmented files on the NAND card.
       
  4500 		if (nandDrvNum >= 0)
       
  4501 			{
       
  4502 			mmcPath[0] = 'a' + nandDrvNum;
       
  4503 			TestDllExesExist[KTestMediaNandFrag] = CopyAndFragmentFiles(fs, pFileMan, mmcPath, KTestMediaNandFrag);
       
  4504 			DBGS_PRINT((_L("%S : %S! (Drive %c)\n"), &TestPsExeNames[KTestMediaNandFrag], EXISTS(!TestDllExesExist[KTestMediaNandFrag]), mmcPath[0]));
       
  4505 			}
       
  4506 		else
       
  4507 			DBGS_PRINT((_L("CheckFilePresence : Failed to get NAND drive number\n")));
       
  4508 #endif // TEST_ADD_FRAGD_MEDIA
       
  4509 		delete pFileMan; pFileMan = NULL;
       
  4510 		delete cleanupStack; cleanupStack = NULL;
       
  4511 		}
       
  4512 
       
  4513 	fs.Close();
       
  4514 
       
  4515 	TUint end = User::TickCount();
       
  4516 	TUint time = TUint((TUint64)(end-start)*(TUint64)TickPeriod/(TUint64)1000000);
       
  4517 	DBGS_PRINT((_L("CheckFilePresence : %d secs elapsed\n"), time));
       
  4518 	}
       
  4519 
       
  4520 //
       
  4521 // DoDeleteFile
       
  4522 //
       
  4523 // Delete a file and remove the hash
       
  4524 //
       
  4525 
       
  4526 void DoDeleteFile(CFileMan* aFileMan, TBool aSilent,TFileName& aFileName )
       
  4527 	{
       
  4528 	TFileName hashName;
       
  4529 	RLoader l;
       
  4530 	test(l.Connect() == KErrNone);
       
  4531 
       
  4532 	DBGD_PRINT((_L("Deleting %S ...\n"), &aFileName));
       
  4533 	if (!aSilent)
       
  4534 		DBGD_PRINT((_L("Deleting %S\n"), &aFileName));
       
  4535 	TInt retVal = aFileMan->Delete(aFileName);
       
  4536 	if (retVal != KErrNone)
       
  4537 		{
       
  4538 		if (TestingReaper)
       
  4539 			{
       
  4540 			aFileMan->Attribs(aFileName, KEntryAttNormal, KEntryAttReadOnly, 0);
       
  4541 			retVal = l.Delete(aFileName);
       
  4542 			if (retVal != KErrNone)
       
  4543 				{
       
  4544 				DBGS_PRINT((_L("RLoader::Delete %S Failed %d\n"), &aFileName, retVal));
       
  4545 				}
       
  4546 			}
       
  4547 		else
       
  4548 			{
       
  4549 			if (!aSilent)
       
  4550 				DBGS_PRINT((_L("Deleting %S Failed %d\n"), &aFileName, retVal));
       
  4551 			}
       
  4552 		}
       
  4553 	GetHashFileName(aFileName, hashName);
       
  4554 	retVal = aFileMan->Delete(hashName);
       
  4555 	if (retVal != KErrNone)
       
  4556 		{
       
  4557 		if (TestingReaper && (retVal == KErrInUse))
       
  4558 			{
       
  4559 			retVal = l.Delete(hashName);
       
  4560 			if (retVal != KErrNone)
       
  4561 				{
       
  4562 				DBGS_PRINT((_L("RLoader::Delete %S Failed %d\n"), &hashName, retVal));
       
  4563 				}
       
  4564 			}
       
  4565 		else
       
  4566 			{
       
  4567 			if (!aSilent)
       
  4568 				DBGS_PRINT((_L("Deleting %S Failed %d\n"), &hashName, retVal));
       
  4569 			}
       
  4570 		}
       
  4571 	l.Close();
       
  4572 	}
       
  4573 
       
  4574 //
       
  4575 // CleanupFiles
       
  4576 //
       
  4577 // Remove any copied files and created directories.
       
  4578 //
       
  4579 
       
  4580 void CleanupFiles(TBool silent)
       
  4581 	{
       
  4582 	TUint start = User::TickCount();
       
  4583 
       
  4584 	RFs fs;
       
  4585 	if (KErrNone != fs.Connect())
       
  4586 		{
       
  4587 		DBGS_PRINT(_L("CleanupFiles : Can't connect to the FS\n"));
       
  4588 		return ;
       
  4589 		}
       
  4590 
       
  4591 	CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
  4592 	if(!cleanupStack)
       
  4593 		if (!silent)
       
  4594 			DBGS_PRINT((_L("Cleanup stack failed\n")));	
       
  4595 	
       
  4596 	CFileMan* pFileMan = NULL;
       
  4597 	TInt retVal;
       
  4598 	TRAP(retVal, pFileMan = CFileMan::NewL(fs));
       
  4599 	
       
  4600 	TFileName newPath;
       
  4601 	TInt index;
       
  4602 	TInt dllIndex;
       
  4603 
       
  4604 	TBuf<32>	path;
       
  4605 	path.Format(_L("%S"),&KMmcDefaultPath);
       
  4606 	TInt mmcDrvNum = FindMMCDriveNumber(fs);
       
  4607 	TInt nandDrvNum = FindFsNANDDrive(fs);
       
  4608 	for (index = KTestMediaMmc; index < KTestMediaCOUNT; index ++)
       
  4609 		{
       
  4610 #ifdef TEST_ADD_FRAGD_MEDIA
       
  4611 		if (index == KTestMediaNandFrag)
       
  4612 			{
       
  4613 			if (nandDrvNum < 0)
       
  4614 				continue;
       
  4615 			path[0] = 'a' + nandDrvNum;
       
  4616 			}
       
  4617 		else
       
  4618 			{
       
  4619 			if (mmcDrvNum < 0)
       
  4620 				continue;
       
  4621 			path[0] = 'a' + mmcDrvNum;
       
  4622 			}
       
  4623 #else
       
  4624 		path[0] = 'a' + mmcDrvNum;
       
  4625 #endif
       
  4626 		newPath.Format(_L("%S%S"),&path, &TestPsExeNames[index]);
       
  4627 		DoDeleteFile(pFileMan, silent,  newPath);
       
  4628 
       
  4629 		newPath.Format(_L("%S%S"),&path, &TestPlExeNames[index]);
       
  4630 		DoDeleteFile(pFileMan, silent,  newPath);
       
  4631 		
       
  4632 		for (dllIndex = 0; dllIndex < PAGELDRTST_MAX_DLLS; dllIndex ++)
       
  4633 			{
       
  4634 			newPath.Format(_L("%S%S%d%S"), &path, &KDllBaseName, dllIndex, &TestPlExtNames[index]);
       
  4635 			DoDeleteFile(pFileMan, silent,  newPath);
       
  4636 			}
       
  4637 		}
       
  4638 	if (nandDrvNum >= 0)
       
  4639 		{
       
  4640 		path[0] = 'a' + nandDrvNum;
       
  4641 		fs.RmDir(path);
       
  4642 		}
       
  4643 	if (mmcDrvNum >= 0)
       
  4644 		{
       
  4645 		path[0] = 'a' + mmcDrvNum;
       
  4646 		fs.RmDir(path);
       
  4647 		}
       
  4648 
       
  4649 	delete pFileMan; pFileMan = NULL;
       
  4650 	delete cleanupStack; cleanupStack = NULL;
       
  4651 	fs.Close();
       
  4652 	TUint end = User::TickCount();
       
  4653 	TUint time = TUint((TUint64)(end-start)*(TUint64)TickPeriod/(TUint64)1000000);
       
  4654 	DBGS_PRINT((_L("CleanupFiles : %d secs elapsed\n"), time));
       
  4655 	}
       
  4656 
       
  4657 #ifdef _DEBUG
       
  4658 
       
  4659 //
       
  4660 // FindLocalDriveNumber
       
  4661 //
       
  4662 // Find the local drive
       
  4663 //
       
  4664 
       
  4665 TInt FindLocalDriveNumber(RFs &aFs, TInt aFsDrvNum)
       
  4666 	{
       
  4667 	RFile file;
       
  4668 	TBuf<256> fileName;	
       
  4669 	fileName.Append((TChar)('A' + aFsDrvNum));
       
  4670 	fileName+=_L(":\\f32-tst\\");
       
  4671 	TInt r=aFs.MkDirAll(fileName);
       
  4672 	TInt locDriveNumber = -1;
       
  4673 	if (r==KErrNone || r== KErrAlreadyExists)
       
  4674 		{
       
  4675 		fileName += _L("tempy.txt");
       
  4676 		r=file.Replace(aFs,fileName,EFileWrite);
       
  4677 		if (r!=KErrNone)
       
  4678 			DBGS_PRINT((_L("FindLocalDriveNumber : Error %d: file '%S' could not be created\n"),r,&fileName));
       
  4679 		RUNTEST1(r==KErrNone);
       
  4680 		r=file.Write(_L8("Flies as big as sparrows indoletly buzzing in the warm air, heavy with the stench of rotting carcasses"));
       
  4681 		if (r!=KErrNone)
       
  4682 			{
       
  4683 			DBGS_PRINT((_L("FindLocalDriveNumber : Error %d: could not write to file %d (%S)\n"),r,aFsDrvNum, &fileName));
       
  4684 			}
       
  4685 		else
       
  4686 			{
       
  4687 			// write caching may be enabled to flush the cache...
       
  4688 			TRequestStatus flushStatus;
       
  4689 			file.Flush(flushStatus);
       
  4690 			User::WaitForRequest(flushStatus);
       
  4691 			// get the block map
       
  4692 			SBlockMapInfo info;
       
  4693 			TInt64 start=0;
       
  4694 			r=file.BlockMap(info, start, -1,ETestDebug);
       
  4695 			if (r==KErrNone || r==KErrCompletion)
       
  4696 				{
       
  4697 				locDriveNumber=info.iLocalDriveNumber;
       
  4698 				DBGD_PRINT((_L("FindLocalDriveNumber : locDriveNumber  %d\n"), locDriveNumber));
       
  4699 				}
       
  4700 			else
       
  4701 				DBGS_PRINT((_L("FindLocalDriveNumber : Error %d: error getting blockmap for drive %d (%S)\n"),r,aFsDrvNum, &fileName));
       
  4702 			}
       
  4703 		aFs.Delete(fileName);
       
  4704 		file.Close();
       
  4705 		}
       
  4706 	else
       
  4707 		DBGS_PRINT((_L("FindLocalDriveNumber : Error %d: error creating dir	\n"),r));
       
  4708 	return locDriveNumber;
       
  4709 	}
       
  4710 
       
  4711 //
       
  4712 // ResetConcurrencyStats
       
  4713 //
       
  4714 // Reset the stats
       
  4715 //
       
  4716 
       
  4717 void ResetConcurrencyStats(RFs& aFs)
       
  4718 	{
       
  4719 	if(TestBootedFromMmc)
       
  4720 		{
       
  4721 		TInt fsDriveNum = FindMMCDriveNumber(aFs);
       
  4722 		if (fsDriveNum >= 0)
       
  4723 			{
       
  4724 			TInt locDriveNumber = FindLocalDriveNumber(aFs, fsDriveNum);
       
  4725 			if (locDriveNumber >= 0)
       
  4726 				{
       
  4727 				RUNTEST(PagingInfo::ResetConcurrency(locDriveNumber,EMediaPagingStatsRomAndCode),KErrNone);
       
  4728 				}
       
  4729 			else
       
  4730 				DBGS_PRINT((_L("ResetConcurrencyStats MMC : Failed to get locDriveNumber %d (%d)\n"), locDriveNumber, fsDriveNum));
       
  4731 			}
       
  4732 		else
       
  4733 			DBGS_PRINT((_L("ResetConcurrencyStats MMC : Failed to get fsDriveNum %d\n"), fsDriveNum));
       
  4734 		}
       
  4735 	else
       
  4736 		{
       
  4737 		TInt fsDriveNum = FindFsNANDDrive(aFs);
       
  4738 		if (fsDriveNum >= 0)
       
  4739 			{
       
  4740 			TInt locDriveNumber = FindLocalDriveNumber(aFs, fsDriveNum);
       
  4741 			if (locDriveNumber >= 0)
       
  4742 				{
       
  4743 				RUNTEST(PagingInfo::ResetConcurrency(locDriveNumber,EMediaPagingStatsRomAndCode),KErrNone);
       
  4744 				}
       
  4745 			else
       
  4746 				DBGS_PRINT((_L("ResetConcurrencyStats NAND : Failed to get locDriveNumber %d (%d)\n"), locDriveNumber, fsDriveNum));
       
  4747 			}
       
  4748 		else
       
  4749 			DBGS_PRINT((_L("ResetConcurrencyStats NAND : Failed to get fsDriveNum %d\n"), fsDriveNum));
       
  4750 		}
       
  4751 	}
       
  4752 
       
  4753 //
       
  4754 // ResetBenchmarks
       
  4755 //
       
  4756 // Reset the stats
       
  4757 //
       
  4758 
       
  4759 void ResetBenchmarks(RFs& aFs)
       
  4760 	{
       
  4761 	if(TestBootedFromMmc)
       
  4762 		{
       
  4763 		TInt fsDriveNum = FindMMCDriveNumber(aFs);
       
  4764 		if (fsDriveNum >= 0)
       
  4765 			{
       
  4766 			TInt locDriveNumber = FindLocalDriveNumber(aFs, fsDriveNum);
       
  4767 			if (locDriveNumber >= 0)
       
  4768 				{
       
  4769 				RUNTEST(PagingInfo::ResetBenchmarks(locDriveNumber,EMediaPagingStatsRomAndCode),KErrNone);
       
  4770 				}
       
  4771 			else
       
  4772 				DBGS_PRINT((_L("ResetBenchmarks MMC : Failed to get locDriveNumber %d (%d)\n"), locDriveNumber, fsDriveNum));
       
  4773 			}
       
  4774 		else
       
  4775 			DBGS_PRINT((_L("ResetBenchmarks MMC : Failed to get fsDriveNum %d\n"), fsDriveNum));
       
  4776 		}
       
  4777 	else
       
  4778 		{
       
  4779 		TInt fsDriveNum = FindFsNANDDrive(aFs);
       
  4780 		if (fsDriveNum >= 0)
       
  4781 			{
       
  4782 			TInt locDriveNumber = FindLocalDriveNumber(aFs, fsDriveNum);
       
  4783 			if (locDriveNumber >= 0)
       
  4784 				{
       
  4785 				RUNTEST(PagingInfo::ResetBenchmarks(locDriveNumber,EMediaPagingStatsRomAndCode),KErrNone);
       
  4786 				}
       
  4787 			else
       
  4788 				DBGS_PRINT((_L("ResetBenchmarks NAND : Failed to get locDriveNumber %d (%d)\n"), locDriveNumber, fsDriveNum));
       
  4789 			}
       
  4790 		else
       
  4791 			DBGS_PRINT((_L("ResetBenchmarks NAND : Failed to get fsDriveNum %d\n"), fsDriveNum));
       
  4792 		}
       
  4793 	}
       
  4794 
       
  4795 //
       
  4796 // DisplayConcurrencyStats
       
  4797 //
       
  4798 // Display the stats
       
  4799 //
       
  4800 
       
  4801 void DisplayConcurrencyStats(RFs& aFs)
       
  4802 	{
       
  4803 	if(TestBootedFromMmc)
       
  4804 		{
       
  4805 		TInt fsDriveNum = FindMMCDriveNumber(aFs);
       
  4806 		if (fsDriveNum >= 0)
       
  4807 			{
       
  4808 			TInt locDriveNumber = FindLocalDriveNumber(aFs, fsDriveNum);
       
  4809 			if (locDriveNumber >= 0)
       
  4810 				{
       
  4811 				DBGS_PRINT((_L("MMC stats\n")));
       
  4812 				RUNTEST1(PagingInfo::PrintConcurrency(locDriveNumber,EMediaPagingStatsRomAndCode)==KErrNone);
       
  4813 				}
       
  4814 			else
       
  4815 				DBGS_PRINT((_L("DisplayConcurrencyStats MMC : Failed to get locDriveNumber %d (%d)\n"), locDriveNumber, fsDriveNum));
       
  4816 			}
       
  4817 		else
       
  4818 			DBGS_PRINT((_L("DisplayConcurrencyStats MMC : Failed to get fsDriveNum %d\n"), fsDriveNum));
       
  4819 		}
       
  4820 	else
       
  4821 		{
       
  4822 		TInt fsDriveNum = FindFsNANDDrive(aFs);
       
  4823 		if (fsDriveNum >= 0)
       
  4824 			{
       
  4825 			TInt locDriveNumber = FindLocalDriveNumber(aFs, fsDriveNum);
       
  4826 
       
  4827 			if (locDriveNumber >= 0)
       
  4828 				{
       
  4829 				DBGS_PRINT((_L("NAND stats\n")));
       
  4830 				RUNTEST1(PagingInfo::PrintConcurrency(locDriveNumber,EMediaPagingStatsRomAndCode)==KErrNone);
       
  4831 				}
       
  4832 			else
       
  4833 				DBGS_PRINT((_L("DisplayConcurrencyStats NAND : Failed to get locDriveNumber %d (%d)\n"), locDriveNumber, fsDriveNum));
       
  4834 			}
       
  4835 		else
       
  4836 			DBGS_PRINT((_L("DisplayConcurrencyStats NAND : Failed to get fsDriveNum %d\n"), fsDriveNum));
       
  4837 		}
       
  4838 	}
       
  4839 
       
  4840 void DisplayBenchmarks(RFs& aFs)
       
  4841 	{
       
  4842 	if(TestBootedFromMmc)
       
  4843 		{
       
  4844 		TInt fsDriveNum = FindMMCDriveNumber(aFs);
       
  4845 		if (fsDriveNum >= 0)
       
  4846 			{
       
  4847 			TInt locDriveNumber = FindLocalDriveNumber(aFs, fsDriveNum);
       
  4848 			if(locDriveNumber>=0)
       
  4849 				{
       
  4850 				DBGS_PRINT((_L("MMC benchmarks\n")));
       
  4851 				RUNTEST1(PagingInfo::PrintBenchmarks(locDriveNumber,EMediaPagingStatsRomAndCode)==KErrNone);
       
  4852 				}
       
  4853 			else
       
  4854 				DBGS_PRINT((_L("DisplayBenchmarks MMC : Failed to get locDriveNumber %d (%d)\n"), locDriveNumber, fsDriveNum));
       
  4855 			}
       
  4856 		else
       
  4857 			DBGS_PRINT((_L("DisplayBenchmarks MMC : Failed to get fsDriveNum %d\n"), fsDriveNum));
       
  4858 		}
       
  4859 	else
       
  4860 		{
       
  4861 		TInt fsDriveNum = FindFsNANDDrive(aFs);
       
  4862 		if (fsDriveNum >= 0)
       
  4863 			{
       
  4864 			TInt locDriveNumber = FindLocalDriveNumber(aFs, fsDriveNum);
       
  4865 			if(locDriveNumber>=0)
       
  4866 				{
       
  4867 				DBGS_PRINT((_L("NAND benchmarks\n")));
       
  4868 				RUNTEST1(PagingInfo::PrintBenchmarks(locDriveNumber,EMediaPagingStatsRomAndCode)==KErrNone);
       
  4869 				}
       
  4870 			else
       
  4871 				DBGS_PRINT((_L("DisplayBenchmarks NAND : Failed to get locDriveNumber %d (%d)\n"), locDriveNumber, fsDriveNum));
       
  4872 			}
       
  4873 		else
       
  4874 			DBGS_PRINT((_L("DisplayBenchmarks NAND : Failed to get fsDriveNum %d\n"), fsDriveNum));
       
  4875 		}
       
  4876 	}
       
  4877 
       
  4878 #endif
       
  4879 
       
  4880 void DoStats()
       
  4881 	{
       
  4882 	if (TestIsDemandPaged)
       
  4883 		{
       
  4884 		SVMCacheInfo  tempPages;
       
  4885 		UserSvr::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0);
       
  4886 		DBGS_PRINT((_L("DPC : min %d max %d curr %d\n"), 
       
  4887 					tempPages.iMinSize, tempPages.iMaxSize, tempPages.iCurrentSize));
       
  4888 		DBGS_PRINT((_L("    : maxFree %d freeRam %d\n"),
       
  4889 					tempPages.iMaxFreeSize, FreeRam()));
       
  4890 		}
       
  4891 
       
  4892 #ifdef _DEBUG
       
  4893 	if (TestWeAreTheTestBase && !TestSilent)
       
  4894 		{
       
  4895 		RFs fs;
       
  4896 		if (KErrNone != fs.Connect())
       
  4897 			{
       
  4898 			DBGS_PRINT(_L("ResetConcurrencyStats : Can't connect to the FS\n"));
       
  4899 			return;
       
  4900 			}
       
  4901 
       
  4902 #ifndef TEST_MINIMAL_STATS
       
  4903 		DisplayConcurrencyStats(fs);
       
  4904 		DisplayBenchmarks(fs);
       
  4905 #endif
       
  4906 #ifndef TEST_DONT_RESET_STATS
       
  4907 		ResetConcurrencyStats(fs);
       
  4908 		ResetBenchmarks(fs);
       
  4909 #endif
       
  4910 		fs.Close();
       
  4911 		}
       
  4912 #endif
       
  4913 	}
       
  4914 
       
  4915 
       
  4916 //
       
  4917 // E32Main
       
  4918 //
       
  4919 // Main entry point.
       
  4920 //
       
  4921 
       
  4922 TInt E32Main()
       
  4923 	{
       
  4924 #ifndef TEST_ON_UNPAGED
       
  4925 	TRomHeader* romHeader = (TRomHeader*)UserSvr::RomHeaderAddress();
       
  4926 	if(!romHeader->iPageableRomStart)
       
  4927 		{
       
  4928 		TestIsDemandPaged = EFalse;
       
  4929 		}
       
  4930 #endif
       
  4931 	// Turn off lazy dll unloading
       
  4932 	RLoader l;
       
  4933 	if (l.Connect() == KErrNone)
       
  4934 		{
       
  4935 		l.CancelLazyDllUnload();
       
  4936 		l.Close();
       
  4937 		}
       
  4938 	
       
  4939 	HAL::Get(HAL::ESystemTickPeriod, TickPeriod);
       
  4940 
       
  4941 	SVMCacheInfo  tempPages;
       
  4942 	memset(&tempPages, 0, sizeof(tempPages));
       
  4943 
       
  4944 	TBool parseResult = ParseCommandLine();
       
  4945 
       
  4946 	if (TestExit)
       
  4947 		{
       
  4948 		return KErrNone;
       
  4949 		}
       
  4950 
       
  4951 	TUint start = User::TickCount();
       
  4952 	
       
  4953 	AreWeTheTestBase();	
       
  4954 
       
  4955 	if (TestIsDemandPaged)
       
  4956 		{
       
  4957 		TInt  minSize = TestMinCacheSize;
       
  4958 		TInt  maxSize = TestMaxCacheSize;
       
  4959 
       
  4960 		SVMCacheInfo  tempPages;
       
  4961 
       
  4962 		// get the old cache info
       
  4963 		UserSvr::HalFunction(EHalGroupVM,EVMHalGetCacheSize,&tempPages,0);
       
  4964 		// set the cache to our test value
       
  4965 		UserSvr::HalFunction(EHalGroupVM,EVMHalSetCacheSize,(TAny*)minSize,(TAny*)maxSize);
       
  4966 		}
       
  4967 	if (!TestSilent)
       
  4968 		{
       
  4969 		test.Title();
       
  4970 		test.Start(_L("Demand Paging loader stress tests..."));
       
  4971 		test.Printf(_L("%S (%d)\n"), &TestNameBuffer, TestWeAreTheTestBase);
       
  4972 		test.Printf(_L("TestBootedFromMmc %d\n"), TestBootedFromMmc);
       
  4973 
       
  4974 		if (TestWeAreTheTestBase)
       
  4975 			CleanupFiles(ETrue);
       
  4976 
       
  4977 		CheckFilePresence(TestWeAreTheTestBase);
       
  4978 		}
       
  4979 
       
  4980 	if (parseResult)
       
  4981 		{
       
  4982 		if (TestLowMem)
       
  4983 			{
       
  4984 			DoLowMemTest(ETrue);
       
  4985 			}
       
  4986 		if (TestSingle)
       
  4987 			{
       
  4988 			RUNTEST(DoSingleTest(),KErrNone);
       
  4989 			}
       
  4990 		if (TestMultiple)
       
  4991 			{
       
  4992 			RUNTEST(DoMultipleTest(),KErrNone);
       
  4993 			}
       
  4994 		if (TestD_Exc)
       
  4995 			{
       
  4996 			RUNTEST(DoTestD_Exc(),KErrNone);
       
  4997 			}
       
  4998 		if (TestChunks)
       
  4999 			{
       
  5000 			DoChunkTests();
       
  5001 			}
       
  5002 		if (TestReaper)
       
  5003 			{
       
  5004 			DoReaperTests();
       
  5005 			}
       
  5006 		if (TestBtrace)
       
  5007 			{
       
  5008 			DoBtraceTest();
       
  5009 			}
       
  5010 		if (TestDefrag)
       
  5011 			{
       
  5012 			DoDefragTest();
       
  5013 			}
       
  5014 		}
       
  5015 	else
       
  5016 		{
       
  5017 #ifdef _DEBUG
       
  5018 		if (TestWeAreTheTestBase)
       
  5019 			{
       
  5020 			RFs fs;
       
  5021 			if (KErrNone == fs.Connect())
       
  5022 				{
       
  5023 				//fs.SetDebugRegister(KCACHE);
       
  5024 				ResetConcurrencyStats(fs);
       
  5025 				ResetBenchmarks(fs);
       
  5026 				fs.Close();
       
  5027 				}
       
  5028 			}
       
  5029 #endif
       
  5030 
       
  5031 		while (1)
       
  5032 			{
       
  5033 			if (TestIsDemandPaged)
       
  5034 				{
       
  5035 #ifdef TEST_RUN_AUTOTEST
       
  5036 				PerformAutoTest();
       
  5037 #endif //TEST_RUN_AUTOTEST
       
  5038 
       
  5039 #ifndef	TEST_SHORT_TEST
       
  5040 #ifdef TEST_RUN_LOWMEMTEST
       
  5041 				DoLowMemTest(ETrue);
       
  5042 #endif //TEST_RUN_LOWMEMTEST
       
  5043 #ifdef TEST_RUN_CHUNKTEST
       
  5044 				DoChunkTests();
       
  5045 #endif //TEST_RUN_CHUNKTEST
       
  5046 #ifdef TEST_RUN_REAPERTEST
       
  5047 				DoReaperTests();
       
  5048 #endif //TEST_RUN_REAPERTEST
       
  5049 #endif //TEST_SHORT_TEST
       
  5050 				}
       
  5051 
       
  5052 #ifdef TEST_RUN_DEFRAGTEST
       
  5053 			DoDefragTest();
       
  5054 #endif //TEST_RUN_DEFRAGTEST
       
  5055 
       
  5056 			if (TestStressFree)
       
  5057 				{
       
  5058 				TInt minSize = 512 * 4096;
       
  5059 				TInt maxSize = 32767 * 4096;
       
  5060 				UserSvr::HalFunction(EHalGroupVM,EVMHalSetCacheSize,(TAny*)minSize,(TAny*)maxSize);
       
  5061 
       
  5062 				test.Printf(_L("%S Stress Free!!\n"), &TestNameBuffer, TestWeAreTheTestBase);
       
  5063 				TestStressFree = EFalse;
       
  5064 				}
       
  5065 			else
       
  5066 				{
       
  5067 				break;
       
  5068 				}
       
  5069 			}
       
  5070 
       
  5071 #ifndef TEST_SHORT_TEST
       
  5072 #ifndef TEST_NO_DEXC_IN_AUTO
       
  5073 #ifdef TEST_RUN_D_EXCTEST
       
  5074 		RUNTEST(DoTestD_Exc(),KErrNone);
       
  5075 #endif //TEST_RUN_D_EXCTEST
       
  5076 #endif //TEST_NO_DEXC_IN_AUTO
       
  5077 		if (TestWeAreTheTestBase && TestFullAutoTest && TestIsDemandPaged)
       
  5078 			{
       
  5079 			RProcess		theProcess;
       
  5080 			TRequestStatus	status;
       
  5081 
       
  5082 			TInt retVal = theProcess.Create(_L("t_pageldrtst_rom.exe"),_L("fullauto"));
       
  5083 			if (retVal != KErrNotFound)
       
  5084 				{
       
  5085 				RUNTEST1(KErrNone == retVal);
       
  5086 				theProcess.Logon(status);
       
  5087 				RUNTEST1(status == KRequestPending);	
       
  5088 				theProcess.Resume();
       
  5089 #ifdef TEST_THRASHING_TEST
       
  5090 				while (1)
       
  5091 					{
       
  5092 					if (theProcess.ExitType() != EExitPending)
       
  5093 						{
       
  5094 						RUNTEST1(theProcess.ExitType() != EExitPanic);
       
  5095 						break;
       
  5096 						}
       
  5097 					User::AfterHighRes(1);
       
  5098 					}
       
  5099 				User::WaitForRequest(status);
       
  5100 #else
       
  5101 				User::WaitForRequest(status);
       
  5102 				if (theProcess.ExitType() != EExitPending)
       
  5103 					{
       
  5104 					RUNTEST1(theProcess.ExitType() != EExitPanic);
       
  5105 					}
       
  5106 #endif //TEST_THRASHING_TEST
       
  5107 				theProcess.Close();
       
  5108 				}
       
  5109 			}
       
  5110 #endif //TEST_SHORT_TEST
       
  5111 #ifdef _DEBUG
       
  5112 		if (TestWeAreTheTestBase && !TestSilent)
       
  5113 			{
       
  5114 			RFs fs;
       
  5115 			if (KErrNone == fs.Connect())
       
  5116 				{
       
  5117 				DisplayConcurrencyStats(fs);
       
  5118 				DisplayBenchmarks(fs);
       
  5119 				fs.Close();
       
  5120 				}
       
  5121 			}
       
  5122 #endif
       
  5123 		}
       
  5124 
       
  5125 	if (TestWeAreTheTestBase && !TestNoClean)
       
  5126 		CleanupFiles(EFalse);
       
  5127 
       
  5128 	if (TestIsDemandPaged)
       
  5129 		{
       
  5130 		TInt minSize = tempPages.iMinSize;
       
  5131 		TInt maxSize = tempPages.iMaxSize;
       
  5132 		// put the cache back to the the original values.
       
  5133 		UserSvr::HalFunction(EHalGroupVM,EVMHalSetCacheSize,(TAny*)minSize,(TAny*)maxSize);
       
  5134 		}
       
  5135 	if (!TestSilent)
       
  5136 		{
       
  5137 		TUint end = User::TickCount();
       
  5138 		TUint time = TUint((TUint64)(end-start)*(TUint64)TickPeriod/(TUint64)1000000);
       
  5139 		test.Printf(_L("%S : Complete (%u seconds)\n"), &TestNameBuffer, time);	
       
  5140 		test.End();
       
  5141 		}
       
  5142 	return KErrNone;
       
  5143 	}