os/kernelhwsrv/kerneltest/e32test/defrag/t_ramdefrag.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32test\defrag\t_ramdefrag.cpp
sl@0
    15
// RAM Defragmentation Functional Tests
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
//#define RUN_ALL_TESTS			// Uncomment to ensure that all tests are run regardless of test failures
sl@0
    20
//#define DEBUG_VER				// Uncomment for information output from tests
sl@0
    21
sl@0
    22
sl@0
    23
sl@0
    24
#define __E32TEST_EXTENSION__
sl@0
    25
#include <e32test.h>
sl@0
    26
RTest test(_L("T_RAMDEFRAG"));
sl@0
    27
#include <e32rom.h>
sl@0
    28
#include <u32hal.h>
sl@0
    29
#include <f32file.h>
sl@0
    30
#include <f32dbg.h>
sl@0
    31
#include <e32svr.h>
sl@0
    32
#include <e32msgqueue.h>
sl@0
    33
#include <e32math.h>
sl@0
    34
#include <hal.h>
sl@0
    35
#include "testdefs.h"
sl@0
    36
sl@0
    37
sl@0
    38
#include <dptest.h>
sl@0
    39
sl@0
    40
#include "t_ramdefrag.h"
sl@0
    41
sl@0
    42
#define READ(a) ReadByte((volatile TUint8*)(a))
sl@0
    43
sl@0
    44
sl@0
    45
#ifdef RUN_ALL_TESTS
sl@0
    46
#define TEST_FAIL {gTestStepFailed++;}
sl@0
    47
#define CLEANUP(a) {}
sl@0
    48
#else
sl@0
    49
#define TEST_FAIL {TestCleanup(); test(EFalse);}
sl@0
    50
#define CLEANUP(a) {if (!gFailPrintPageInfo) \
sl@0
    51
						{ \
sl@0
    52
						PrintPageInfo(); \
sl@0
    53
						gFailPrintPageInfo = ETrue; \
sl@0
    54
						} \
sl@0
    55
					a;}
sl@0
    56
#endif
sl@0
    57
sl@0
    58
sl@0
    59
sl@0
    60
#define TEST_DRIVER_OPEN		1
sl@0
    61
#define TEST_DRIVER_CLOSE		0
sl@0
    62
sl@0
    63
#define BEST_MOVABLE			1
sl@0
    64
#define BEST_DISCARDABLE		2
sl@0
    65
#define BEST_FIXED				3
sl@0
    66
sl@0
    67
#define Z_ALLOC_CONTIG			1
sl@0
    68
#define Z_ALLOC_DISC			2
sl@0
    69
sl@0
    70
LOCAL_D TUint gTestStarted = EFalse;								// Used to ensure matching TestStart() and TestEnd().
sl@0
    71
LOCAL_D TBool gPagedRom = ETrue;									// Stores whether or not is a paged ROM
sl@0
    72
LOCAL_D TInt gTestStepFailed = 0;									// Stores the number of test steps failed
sl@0
    73
LOCAL_D TBool gFailPrintPageInfo = EFalse;							// Set to ETrue once CLEANUP has been invoked once.
sl@0
    74
LOCAL_D TBool gFileCacheRun = EFalse;								// Set to ETrue whe FSCaching tests have been run
sl@0
    75
sl@0
    76
LOCAL_D TInt gRamSize;												// The total RAM size in bytes
sl@0
    77
LOCAL_D TInt gFreeRam;												// The amount of free RAM available in bytes
sl@0
    78
LOCAL_D TInt gPageSize;												// The number of bytes per page
sl@0
    79
LOCAL_D TUint gPageShift;
sl@0
    80
#ifdef DEBUG_VER
sl@0
    81
LOCAL_D TInt gRamUsed; 												// The amount of RAM used in bytes
sl@0
    82
#endif
sl@0
    83
LOCAL_D TInt gInitialRam;											// The initial free RAM before a test starts
sl@0
    84
LOCAL_D TInt gEndRam;												// The end free RAM when a test finishes
sl@0
    85
LOCAL_D TUint gOriginalMinCacheSize;									// The original DP minSize
sl@0
    86
LOCAL_D TUint gOriginalMaxCacheSize;									// The original DP maxSize
sl@0
    87
LOCAL_D TInt gTotalRamLost;											// The total amount of RAM lost during the test
sl@0
    88
sl@0
    89
LOCAL_D TUint gZoneCount = 0;										// Number of zones
sl@0
    90
LOCAL_D const TUint KInvalidZoneID = 0xffffffff;					// Invalid value for a zone ID
sl@0
    91
LOCAL_D STestPageCount gTotalPageCount;
sl@0
    92
sl@0
    93
sl@0
    94
LOCAL_D struct SRamZoneConfig*	gZoneConfigArray;					// Contains the configurations of all the zones
sl@0
    95
LOCAL_D struct SRamZoneUtilisation*	gZoneUtilArray;					// Contains the utilisations of all the zones
sl@0
    96
LOCAL_D struct SRamZoneUtilisation* gOriginalPageCountArray;		// Contains the original utilisations of the zones
sl@0
    97
LOCAL_D TInt* gPrefArray;											// Contains the preference order of the zones 
sl@0
    98
LOCAL_D TUint8* gOrigFlagArray;										// Contains the orignal values for the zone flags
sl@0
    99
sl@0
   100
LOCAL_D TInt gDefragMaxPages = 0;
sl@0
   101
sl@0
   102
const TInt KFillAllMovable = -1;
sl@0
   103
sl@0
   104
LOCAL_D RChunk* gChunkArray1 = NULL;								// Stores reference to all the chunks that have been created
sl@0
   105
LOCAL_D RChunk* gChunkArray2 = NULL;								// Stores reference to all the chunks that have been created
sl@0
   106
LOCAL_D TUint gChunkArraySize1 = 0;									// The size of the array gChunkArray
sl@0
   107
LOCAL_D TUint gChunkArraySize2 = 0;									// The size of the array gChunkArray
sl@0
   108
const TUint KChunkDefaultSize = 0x300000;	
sl@0
   109
const TUint KMaxChunks = 14;
sl@0
   110
LOCAL_D const TUint KNumAllocChunks = 10;							// The number of chunks to be allocd for some tests.
sl@0
   111
	
sl@0
   112
LOCAL_D RRamDefragFuncTestLdd Ldd;									// Main Ldd used to call into device driver
sl@0
   113
sl@0
   114
LOCAL_D TBuf<20> gTestThreadName =_L("TestThread");	
sl@0
   115
LOCAL_D RThread gTestThread;
sl@0
   116
LOCAL_D TRequestStatus status;
sl@0
   117
sl@0
   118
LOCAL_D TInt gDrive;												// The removable media drive
sl@0
   119
LOCAL_D RFs gTheFs;									
sl@0
   120
LOCAL_D TFileName gSessionPath;
sl@0
   121
sl@0
   122
const TInt KNoRemovableDrive = -1;									// gDrive is set to this when no suitable drive can be found.
sl@0
   123
const TInt KDefaultCacheSize = (128 + 12) * 1024;					// The default file system cache size 
sl@0
   124
const TUint KNumFilesOrig = (32 * 1024 * 1024) / KDefaultCacheSize;	// The number of files that are needed to fill the file system cache
sl@0
   125
LOCAL_D TInt gFilesNeededToFillCache = KNumFilesOrig ;				// Not constant as can change depending on the size of the disk
sl@0
   126
LOCAL_D RFile gFile[KNumFilesOrig];
sl@0
   127
sl@0
   128
LOCAL_D TInt* gCandList1;											// Array of zones that have the same preference and the same
sl@0
   129
LOCAL_D TInt* gCandList2;											// amount of free pages
sl@0
   130
const TInt KInvalidCandIndex = -1;
sl@0
   131
sl@0
   132
//
sl@0
   133
// GetDrive
sl@0
   134
//
sl@0
   135
// Gets the removable drive number
sl@0
   136
//
sl@0
   137
TInt GetDrive()
sl@0
   138
	{
sl@0
   139
	RFs theFs;
sl@0
   140
	TInt r = theFs.Connect();
sl@0
   141
	test_KErrNone(r);
sl@0
   142
sl@0
   143
	TInt driveLet = KNoRemovableDrive;
sl@0
   144
sl@0
   145
	TInt i = EDriveA;
sl@0
   146
	for (; i <= EDriveZ; i++)
sl@0
   147
		{
sl@0
   148
		TVolumeInfo volInfo;
sl@0
   149
		r = theFs.Volume(volInfo, i);
sl@0
   150
		if (r == KErrNone)
sl@0
   151
			{// This drive no. exists so determine if it is removable and 
sl@0
   152
			//  formattable media.
sl@0
   153
			if ((volInfo.iDrive.iDriveAtt & KDriveAttRemovable) &&
sl@0
   154
				(volInfo.iDrive.iMediaAtt & KMediaAttFormattable))	
sl@0
   155
				{
sl@0
   156
				driveLet = i;
sl@0
   157
				break;
sl@0
   158
				}
sl@0
   159
			}
sl@0
   160
		}
sl@0
   161
	theFs.Close();
sl@0
   162
	return driveLet;
sl@0
   163
	}
sl@0
   164
sl@0
   165
sl@0
   166
//
sl@0
   167
// DeviceDriver
sl@0
   168
//
sl@0
   169
// Opens or closes the device driver used
sl@0
   170
//
sl@0
   171
TInt DeviceDriver(TInt aFunctionNum)
sl@0
   172
	{
sl@0
   173
	TInt r = 0;
sl@0
   174
	switch (aFunctionNum)
sl@0
   175
		{
sl@0
   176
		case TEST_DRIVER_OPEN:
sl@0
   177
			{
sl@0
   178
			r = User::LoadLogicalDevice(KRamDefragFuncTestLddName);
sl@0
   179
			test(r==KErrNone || r==KErrAlreadyExists);
sl@0
   180
			r = Ldd.Open();
sl@0
   181
			test_KErrNone(r);
sl@0
   182
			}
sl@0
   183
		break;
sl@0
   184
		
sl@0
   185
		case TEST_DRIVER_CLOSE:
sl@0
   186
			{
sl@0
   187
			Ldd.Close();
sl@0
   188
			r = User::FreeLogicalDevice(KRamDefragFuncTestLddName);
sl@0
   189
			test_KErrNone(r);
sl@0
   190
			}
sl@0
   191
		break;
sl@0
   192
		
sl@0
   193
		default:
sl@0
   194
		break;
sl@0
   195
		
sl@0
   196
		}
sl@0
   197
	return r;
sl@0
   198
	}
sl@0
   199
sl@0
   200
sl@0
   201
//
sl@0
   202
// GetOriginalPageCount
sl@0
   203
//
sl@0
   204
// Obtains the orginal types of pages in each of the zones
sl@0
   205
//
sl@0
   206
void GetOriginalPageCount()
sl@0
   207
	{
sl@0
   208
	TUint index;
sl@0
   209
	TInt ret = 0;
sl@0
   210
	TESTDEBUG(test.Printf(_L("ram defrag : Get info about the zones\n")));
sl@0
   211
	for (index = 0; index < gZoneCount; index ++)
sl@0
   212
		{
sl@0
   213
		ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneUtilisation,(TAny*)index, (TAny*)&gOriginalPageCountArray[index]);
sl@0
   214
		test(ret == KErrNone);
sl@0
   215
		}
sl@0
   216
	}
sl@0
   217
sl@0
   218
sl@0
   219
//
sl@0
   220
// PrintPageInfo
sl@0
   221
//
sl@0
   222
// Prints various page information to the screen
sl@0
   223
//
sl@0
   224
void PrintPageInfo()
sl@0
   225
	{
sl@0
   226
	test.Printf(_L("\nZONE CONFIGURATIONS:\n"));
sl@0
   227
	for (TUint index = 0; index < gZoneCount; index ++)
sl@0
   228
		{
sl@0
   229
		TInt ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneConfig,(TAny*)index, (TAny*)&gZoneConfigArray[index]);
sl@0
   230
		test(ret == KErrNone);
sl@0
   231
		test.Printf(_L("config : id=0x%08x  index=%-2d  base=0x%08x  end=0x%08x  pages=0x%08x  pref=%-2d  flags=0x%x\n"),
sl@0
   232
					gZoneConfigArray[index].iZoneId,gZoneConfigArray[index].iZoneIndex,
sl@0
   233
					gZoneConfigArray[index].iPhysBase,gZoneConfigArray[index].iPhysEnd,
sl@0
   234
					gZoneConfigArray[index].iPhysPages,gZoneConfigArray[index].iPref,gZoneConfigArray[index].iFlags);
sl@0
   235
		}
sl@0
   236
	test.Printf(_L("\nZONE UTILISATIONS:\n"));
sl@0
   237
	for (TUint index = 0; index < gZoneCount; index ++)
sl@0
   238
		{
sl@0
   239
		TInt ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneUtilisation,(TAny*)index, (TAny*)&gZoneUtilArray[index]);
sl@0
   240
		test(ret == KErrNone);
sl@0
   241
		test.Printf(_L("usage  : id=0x%08x  index=%-2d  pref=%d  pages=0x%08x free=0x%08x  unknown=0x%08x  fixed=0x%08x  move=0x%08x  discard=0x%08x  other=0x%08x\n"),
sl@0
   242
					gZoneUtilArray[index].iZoneId,gZoneUtilArray[index].iZoneIndex,gZoneConfigArray[index].iPref, 
sl@0
   243
					gZoneUtilArray[index].iPhysPages,gZoneUtilArray[index].iFreePages,
sl@0
   244
					gZoneUtilArray[index].iAllocUnknown,gZoneUtilArray[index].iAllocFixed,gZoneUtilArray[index].iAllocMovable,
sl@0
   245
					gZoneUtilArray[index].iAllocDiscardable,gZoneUtilArray[index].iAllocOther);
sl@0
   246
		}
sl@0
   247
	}
sl@0
   248
sl@0
   249
sl@0
   250
//
sl@0
   251
// GetAllPageInfo
sl@0
   252
//
sl@0
   253
// Get various different page information for all zones
sl@0
   254
// Also updates the total page count
sl@0
   255
//
sl@0
   256
void GetAllPageInfo()
sl@0
   257
	{
sl@0
   258
	TInt ret = 0;
sl@0
   259
	gTotalPageCount.iFreePages = 0;
sl@0
   260
	gTotalPageCount.iUnknownPages = 0;
sl@0
   261
	gTotalPageCount.iFixedPages = 0;
sl@0
   262
	gTotalPageCount.iMovablePages = 0;
sl@0
   263
	gTotalPageCount.iDiscardablePages = 0;
sl@0
   264
	gTotalPageCount.iOtherPages = 0;
sl@0
   265
sl@0
   266
	// now get the config of each of the zones.
sl@0
   267
	TUint	index;
sl@0
   268
	TESTDEBUG(test.Printf(_L("ram defrag : Get info about the zones\n")));
sl@0
   269
	for (index = 0; index < gZoneCount; index ++)
sl@0
   270
		{
sl@0
   271
		ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneConfig,(TAny*)index, (TAny*)&gZoneConfigArray[index]);
sl@0
   272
		test(ret == KErrNone);
sl@0
   273
sl@0
   274
		ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneUtilisation,(TAny*)index, (TAny*)&gZoneUtilArray[index]);
sl@0
   275
		test(ret == KErrNone);
sl@0
   276
		
sl@0
   277
		gTotalPageCount.iFreePages += gZoneUtilArray[index].iFreePages;
sl@0
   278
		gTotalPageCount.iUnknownPages += gZoneUtilArray[index].iAllocUnknown;
sl@0
   279
		gTotalPageCount.iFixedPages += gZoneUtilArray[index].iAllocFixed;
sl@0
   280
		gTotalPageCount.iMovablePages += gZoneUtilArray[index].iAllocMovable;
sl@0
   281
		gTotalPageCount.iDiscardablePages += gZoneUtilArray[index].iAllocDiscardable;
sl@0
   282
		gTotalPageCount.iOtherPages += gZoneUtilArray[index].iAllocOther;
sl@0
   283
		}
sl@0
   284
	TESTDEBUG(test.Printf(_L("free=0x%x unknown=0x%x fixed=0x%x move=0x%x discard=0x%x other=0x%x\n"),
sl@0
   285
					gTotalPageCount.iFreePages, gTotalPageCount.iUnknownPages, gTotalPageCount.iFixedPages, 
sl@0
   286
					gTotalPageCount.iMovablePages, gTotalPageCount.iDiscardablePages,gTotalPageCount.iOtherPages));
sl@0
   287
sl@0
   288
	TESTDEBUG(PrintPageInfo());
sl@0
   289
	}
sl@0
   290
sl@0
   291
void RestoreRamZoneFlags()
sl@0
   292
	{
sl@0
   293
	GetAllPageInfo(); // Update the current set of RAM zone flag data.
sl@0
   294
	for (TUint index=0; index < gZoneCount; index++)
sl@0
   295
		{
sl@0
   296
		TUint zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
   297
		Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ORIG_FLAG, gOrigFlagArray[index]);
sl@0
   298
		}
sl@0
   299
	}
sl@0
   300
sl@0
   301
void ResetRamZoneFlags()
sl@0
   302
	{
sl@0
   303
	GetAllPageInfo(); // Update the current set of RAM zone flag data.
sl@0
   304
	for (TUint index=0; index < gZoneCount; index++)
sl@0
   305
		{
sl@0
   306
		TUint zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
   307
		Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, RESET_FLAG);
sl@0
   308
		}
sl@0
   309
	}
sl@0
   310
sl@0
   311
void RemoveChunkAlloc(RChunk*& aChunkArray, TUint& aChunkArraySize);
sl@0
   312
void ResetDPCache();
sl@0
   313
sl@0
   314
void FSCachCleanUp()
sl@0
   315
	{
sl@0
   316
	// If the File System Caching tests have been run, 
sl@0
   317
	// ensure that they are cleaned up
sl@0
   318
	if (gFileCacheRun)
sl@0
   319
		{
sl@0
   320
		TUint i = 0;
sl@0
   321
		// First close all the open handles to the RFile objects open
sl@0
   322
		for (i = 0; i < KNumFilesOrig; i++) 
sl@0
   323
			{
sl@0
   324
			gFile[i].Close();
sl@0
   325
			}
sl@0
   326
sl@0
   327
		// Now call EmptyRamZone on every zone to ensure that 
sl@0
   328
		// discardable pages are cleaned up
sl@0
   329
		
sl@0
   330
		GetAllPageInfo();
sl@0
   331
		for (i = 0; i < gZoneCount; i++)
sl@0
   332
			{
sl@0
   333
			TUint zoneID = gZoneConfigArray[i].iZoneId;
sl@0
   334
			Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, zoneID);
sl@0
   335
			}
sl@0
   336
		gFileCacheRun = EFalse;
sl@0
   337
		}
sl@0
   338
	}
sl@0
   339
sl@0
   340
//
sl@0
   341
// TestCleanup
sl@0
   342
//
sl@0
   343
// Cleans up all the allocations made at the beginning of the test
sl@0
   344
//
sl@0
   345
void TestCleanup()
sl@0
   346
	{
sl@0
   347
sl@0
   348
	Ldd.ResetDriver();
sl@0
   349
sl@0
   350
	// Revert the cleared flags to their original values before the tests were carried out
sl@0
   351
	RestoreRamZoneFlags();
sl@0
   352
sl@0
   353
	// Reset the DP cache and remove any allocated chunks and fixed pages.
sl@0
   354
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
   355
	RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
sl@0
   356
	ResetDPCache();
sl@0
   357
	Ldd.FreeAllFixedPages();
sl@0
   358
	Ldd.FreeFromAllZones();
sl@0
   359
	FSCachCleanUp();
sl@0
   360
sl@0
   361
	User::Free(gPrefArray);
sl@0
   362
	gPrefArray = NULL;
sl@0
   363
sl@0
   364
	User::Free(gOrigFlagArray);
sl@0
   365
	gOrigFlagArray = NULL;
sl@0
   366
sl@0
   367
	User::Free(gCandList1);
sl@0
   368
	gCandList1 = NULL;
sl@0
   369
sl@0
   370
	User::Free(gCandList2);
sl@0
   371
	gCandList2 = NULL;
sl@0
   372
sl@0
   373
	User::Free(gOriginalPageCountArray);
sl@0
   374
	gOriginalPageCountArray = NULL;
sl@0
   375
sl@0
   376
	// Output the last possible state of memory
sl@0
   377
	if (!gFailPrintPageInfo)
sl@0
   378
		PrintPageInfo();
sl@0
   379
sl@0
   380
	User::Free(gZoneConfigArray);
sl@0
   381
	gZoneConfigArray = NULL;
sl@0
   382
sl@0
   383
	User::Free(gZoneUtilArray);
sl@0
   384
	gZoneUtilArray = NULL;
sl@0
   385
sl@0
   386
	}
sl@0
   387
sl@0
   388
sl@0
   389
// TestSetup
sl@0
   390
//
sl@0
   391
// Get the necessary information needed to carry out the tests
sl@0
   392
//
sl@0
   393
TInt TestSetup()
sl@0
   394
	{
sl@0
   395
	// Get the MMC drive
sl@0
   396
	gDrive = GetDrive();
sl@0
   397
sl@0
   398
	// first get the number of zones
sl@0
   399
	TInt ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneCount,&gZoneCount,0);
sl@0
   400
	if (ret != KErrNone)
sl@0
   401
		{
sl@0
   402
		test.Printf(_L("Cannot obtain the number of zones\n"));
sl@0
   403
		return ret;
sl@0
   404
		}
sl@0
   405
	test.Printf(_L("RAM Zones (count=%u)\n"),gZoneCount);
sl@0
   406
sl@0
   407
	// Obtain the size of the RAM and the size of a page	
sl@0
   408
	ret = HAL::Get(HAL::EMemoryRAM, gRamSize);
sl@0
   409
	if (ret != KErrNone)
sl@0
   410
		{
sl@0
   411
		test.Printf(_L("Cannot obtain the size of RAM\n"));
sl@0
   412
		return ret;
sl@0
   413
		}
sl@0
   414
sl@0
   415
	// Retrieve the page size and use it to detemine the page shift (assumes 32-bit system).
sl@0
   416
	ret = HAL::Get(HAL::EMemoryPageSize, gPageSize);
sl@0
   417
	if (ret != KErrNone)
sl@0
   418
		{
sl@0
   419
		test.Printf(_L("Cannot obtain the page size\n"));
sl@0
   420
		return ret;
sl@0
   421
		}
sl@0
   422
sl@0
   423
	TUint32 pageMask = gPageSize;
sl@0
   424
	TUint i = 0;
sl@0
   425
	for (; i < 32; i++)
sl@0
   426
		{
sl@0
   427
		if (pageMask & 1)
sl@0
   428
			{
sl@0
   429
			if (pageMask & ~1u)
sl@0
   430
				{
sl@0
   431
				test.Printf(_L("ERROR - page size not a power of 2"));
sl@0
   432
				return KErrNotSupported;
sl@0
   433
				}
sl@0
   434
			gPageShift = i;
sl@0
   435
			break;
sl@0
   436
			}
sl@0
   437
		pageMask >>= 1;
sl@0
   438
		}
sl@0
   439
sl@0
   440
	gZoneConfigArray = (SRamZoneConfig *)User::AllocZ(sizeof(SRamZoneConfig) * gZoneCount);
sl@0
   441
	if (gZoneConfigArray == NULL)
sl@0
   442
		return KErrNoMemory;
sl@0
   443
	gZoneUtilArray = (SRamZoneUtilisation *)User::AllocZ(sizeof(SRamZoneUtilisation) * gZoneCount);
sl@0
   444
	if (gZoneUtilArray == NULL)
sl@0
   445
		return KErrNoMemory;
sl@0
   446
	gPrefArray = (TInt *)User::AllocZ(sizeof(TInt) * gZoneCount);
sl@0
   447
	if (gPrefArray == NULL)
sl@0
   448
		return KErrNoMemory;
sl@0
   449
	gOrigFlagArray = (TUint8 *)User::AllocZ(sizeof(TUint8) * gZoneCount);
sl@0
   450
	if (gOrigFlagArray == NULL)
sl@0
   451
		return KErrNoMemory;
sl@0
   452
	gOriginalPageCountArray = (SRamZoneUtilisation *)User::AllocZ(sizeof(SRamZoneUtilisation) * gZoneCount);
sl@0
   453
	if (gOriginalPageCountArray == NULL)
sl@0
   454
		return KErrNoMemory;
sl@0
   455
sl@0
   456
	gCandList1 = (TInt *)User::AllocZ(sizeof(TInt) * gZoneCount);
sl@0
   457
	if (gCandList1 == NULL)
sl@0
   458
		return KErrNoMemory;
sl@0
   459
	gCandList2 = (TInt *)User::AllocZ(sizeof(TInt) * gZoneCount);
sl@0
   460
	if (gCandList2 == NULL)
sl@0
   461
		return KErrNoMemory;
sl@0
   462
sl@0
   463
	GetAllPageInfo();
sl@0
   464
	PrintPageInfo();
sl@0
   465
	
sl@0
   466
	// Store the original flags
sl@0
   467
	for (i=0; i < gZoneCount; i++)
sl@0
   468
		gOrigFlagArray[i] = gZoneConfigArray[i].iFlags;
sl@0
   469
	
sl@0
   470
	// Now clear the flags for carrying out tests
sl@0
   471
	Ldd.ResetDriver();
sl@0
   472
	ResetRamZoneFlags();
sl@0
   473
	
sl@0
   474
	
sl@0
   475
	// Check whether the ROM is paged or not
sl@0
   476
	TRomHeader* romHeader = (TRomHeader*)UserSvr::RomHeaderAddress();
sl@0
   477
	if(!romHeader->iPageableRomStart)
sl@0
   478
		{
sl@0
   479
		test.Printf(_L("Not a Paged ROM - Skipping all discardable page tests.\n"));
sl@0
   480
		gPagedRom = EFalse;
sl@0
   481
		}
sl@0
   482
	else
sl@0
   483
		{// Save the current state of the DP cache so it can be restored when required and
sl@0
   484
		// after the test has finished.
sl@0
   485
		TUint currentCacheSize;
sl@0
   486
		DPTest::CacheSize(gOriginalMinCacheSize, gOriginalMaxCacheSize, currentCacheSize);
sl@0
   487
		TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
sl@0
   488
									gOriginalMinCacheSize >> gPageShift, gOriginalMaxCacheSize >> gPageShift, 
sl@0
   489
									currentCacheSize >> gPageShift));
sl@0
   490
		}
sl@0
   491
sl@0
   492
	return KErrNone;
sl@0
   493
	}
sl@0
   494
sl@0
   495
sl@0
   496
// 
sl@0
   497
// UpdateRamInfo
sl@0
   498
//
sl@0
   499
// Updating the various RAM information
sl@0
   500
//
sl@0
   501
void UpdateRamInfo()
sl@0
   502
	{
sl@0
   503
	HAL::Get(HAL::EMemoryRAMFree, gFreeRam);
sl@0
   504
	TESTDEBUG(gRamUsed = gRamSize - gFreeRam);
sl@0
   505
	}
sl@0
   506
	
sl@0
   507
sl@0
   508
// 
sl@0
   509
// CheckRamDifference
sl@0
   510
//
sl@0
   511
// Checks the difference between the initial free RAM and the end free RAM
sl@0
   512
//
sl@0
   513
void CheckRamDifference()
sl@0
   514
	{
sl@0
   515
	if (gInitialRam == gEndRam)
sl@0
   516
		{
sl@0
   517
		TESTDEBUG(test.Printf(_L("No RAM was lost during this test\n")));
sl@0
   518
		}
sl@0
   519
	else
sl@0
   520
		{
sl@0
   521
		TInt diff = gInitialRam - gEndRam;
sl@0
   522
sl@0
   523
sl@0
   524
		gTotalRamLost = gTotalRamLost + diff;
sl@0
   525
		}
sl@0
   526
	}
sl@0
   527
sl@0
   528
TInt VerifyMovDisAlloc();
sl@0
   529
//
sl@0
   530
// TestStart
sl@0
   531
//
sl@0
   532
// Updates the RAM information at the beginning of a test step	
sl@0
   533
//
sl@0
   534
void TestStart()
sl@0
   535
	{
sl@0
   536
	test(!gTestStarted);
sl@0
   537
	gTestStarted = ETrue;
sl@0
   538
sl@0
   539
	Ldd.ResetDriver();
sl@0
   540
	
sl@0
   541
	Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
   542
	if (VerifyMovDisAlloc() != KErrNone)
sl@0
   543
		{
sl@0
   544
		CLEANUP(;);
sl@0
   545
		TEST_FAIL;
sl@0
   546
		}
sl@0
   547
	
sl@0
   548
	UpdateRamInfo();
sl@0
   549
	gInitialRam = gFreeRam;
sl@0
   550
	TESTDEBUG(test.Printf(_L("Initial Free RAM = 0x%x, Initial RAM Used = 0x%x\n"), gFreeRam, gRamUsed));
sl@0
   551
	}
sl@0
   552
sl@0
   553
sl@0
   554
void RemoveChunkAlloc(RChunk*& aChunkArray, TUint& aChunkArraySize);
sl@0
   555
//
sl@0
   556
// TestEnd
sl@0
   557
//
sl@0
   558
// Updates RAM information at end of test step and checks the RAM delta
sl@0
   559
//
sl@0
   560
void TestEnd()
sl@0
   561
	{
sl@0
   562
	test(gTestStarted);
sl@0
   563
	gTestStarted = EFalse;
sl@0
   564
sl@0
   565
	gDefragMaxPages = 0;
sl@0
   566
sl@0
   567
	// Clean up anything that may need to be cleaned.
sl@0
   568
	ResetRamZoneFlags();
sl@0
   569
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
   570
	RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
sl@0
   571
	ResetDPCache();
sl@0
   572
	Ldd.FreeAllFixedPages();
sl@0
   573
	FSCachCleanUp();
sl@0
   574
	
sl@0
   575
	UpdateRamInfo();
sl@0
   576
	gEndRam = gFreeRam;
sl@0
   577
	TESTDEBUG(test.Printf(_L("End RAM Free = 0x%x, End RAM Used = 0x%x\n"), gEndRam, gRamUsed));
sl@0
   578
	CheckRamDifference();
sl@0
   579
	test.Printf(_L(" \n"));
sl@0
   580
	}
sl@0
   581
sl@0
   582
//
sl@0
   583
// CheckZonesSwitchedOff
sl@0
   584
//
sl@0
   585
// Checks that zones have been switched off
sl@0
   586
//
sl@0
   587
TBool CheckZonesSwitchedOff()
sl@0
   588
	{
sl@0
   589
	GetAllPageInfo();
sl@0
   590
	for (TUint i = 0; i < gZoneCount; i++)
sl@0
   591
		{
sl@0
   592
		if (gOriginalPageCountArray[i].iFreePages != gOriginalPageCountArray[i].iPhysPages &&
sl@0
   593
			gZoneUtilArray[i].iFreePages == gZoneUtilArray[i].iPhysPages)
sl@0
   594
			{
sl@0
   595
			return ETrue;
sl@0
   596
			}
sl@0
   597
		}
sl@0
   598
	return EFalse;
sl@0
   599
	}
sl@0
   600
	
sl@0
   601
sl@0
   602
//
sl@0
   603
// CheckZoneIsOff
sl@0
   604
//
sl@0
   605
// Checks if a particular zone is empty 
sl@0
   606
//
sl@0
   607
TBool CheckZoneIsOff(TUint aZoneIndex)
sl@0
   608
	{
sl@0
   609
	GetAllPageInfo();
sl@0
   610
	if (gZoneUtilArray[aZoneIndex].iFreePages == gZoneUtilArray[aZoneIndex].iPhysPages)
sl@0
   611
		{
sl@0
   612
		TESTDEBUG(test.Printf(_L("Zone index %d is Empty\n"), aZoneIndex));
sl@0
   613
		return ETrue;
sl@0
   614
		}
sl@0
   615
	else
sl@0
   616
		{
sl@0
   617
		TESTDEBUG(test.Printf(_L("Zone index %d is Not empty\n"), aZoneIndex));
sl@0
   618
		return EFalse;
sl@0
   619
		}
sl@0
   620
	}
sl@0
   621
sl@0
   622
sl@0
   623
//
sl@0
   624
// GetPrefOrder
sl@0
   625
//
sl@0
   626
// Go through each zone ordering them in preference order
sl@0
   627
//
sl@0
   628
void GetPrefOrder()
sl@0
   629
	{
sl@0
   630
	GetAllPageInfo();
sl@0
   631
	TESTDEBUG(PrintPageInfo());
sl@0
   632
sl@0
   633
	for (TUint i=0; i < (TUint)gZoneCount; i++)
sl@0
   634
		gPrefArray[i] = KErrNotFound;
sl@0
   635
sl@0
   636
	for (TUint curIndex = 0; curIndex < gZoneCount; curIndex++)
sl@0
   637
		{
sl@0
   638
		TBool currentEmpty = gZoneUtilArray[curIndex].iPhysPages == gZoneUtilArray[curIndex].iFreePages;
sl@0
   639
		TUint currentPref = gZoneConfigArray[curIndex].iPref;
sl@0
   640
		TUint currentImmovPages = 	gZoneUtilArray[curIndex].iAllocFixed + 
sl@0
   641
									gZoneUtilArray[curIndex].iAllocUnknown;
sl@0
   642
		TUint morePrefCnt = 0;
sl@0
   643
		for (TUint index = 0; index < gZoneCount; index++)
sl@0
   644
			{// A RAM zone with the same iPref is more preferable if it has 
sl@0
   645
			// more immovable pages.
sl@0
   646
			if (gZoneConfigArray[index].iPref < currentPref || 
sl@0
   647
				(gZoneConfigArray[index].iPref == currentPref && 
sl@0
   648
				(currentImmovPages < gZoneUtilArray[index].iAllocFixed + gZoneUtilArray[index].iAllocUnknown ||
sl@0
   649
				(currentEmpty &&
sl@0
   650
				gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages))))
sl@0
   651
				{
sl@0
   652
				morePrefCnt++;
sl@0
   653
				}
sl@0
   654
			}
sl@0
   655
sl@0
   656
		while (gPrefArray[morePrefCnt] != KErrNotFound)
sl@0
   657
			{// Zone(s) of this preference and size already exist so 
sl@0
   658
			 // place this one after it/them
sl@0
   659
			morePrefCnt++;
sl@0
   660
			}
sl@0
   661
		gPrefArray[morePrefCnt] = curIndex;
sl@0
   662
		}
sl@0
   663
	}
sl@0
   664
sl@0
   665
//
sl@0
   666
// ZonesSamePref
sl@0
   667
//
sl@0
   668
// Return ETrue if the RAM zones are of the same preference.
sl@0
   669
//
sl@0
   670
// NOTE - This requires GetAllPageInfo() to have already been called.
sl@0
   671
//
sl@0
   672
TBool ZonesSamePref(TUint aZoneIndex1, TUint aZoneIndex2)
sl@0
   673
	{
sl@0
   674
	TBool zoneEmpty1 = gZoneUtilArray[aZoneIndex1].iFreePages == gZoneUtilArray[aZoneIndex1].iPhysPages;
sl@0
   675
	TBool zoneEmpty2 = gZoneUtilArray[aZoneIndex2].iFreePages == gZoneUtilArray[aZoneIndex2].iPhysPages;
sl@0
   676
	if (gZoneConfigArray[aZoneIndex1].iPref == gZoneConfigArray[aZoneIndex2].iPref &&
sl@0
   677
		(gZoneUtilArray[aZoneIndex1].iAllocFixed + gZoneUtilArray[aZoneIndex1].iAllocUnknown == 
sl@0
   678
		gZoneUtilArray[aZoneIndex2].iAllocFixed + gZoneUtilArray[aZoneIndex2].iAllocUnknown &&
sl@0
   679
		(zoneEmpty1 == zoneEmpty2)))
sl@0
   680
		{
sl@0
   681
		return ETrue;
sl@0
   682
		}
sl@0
   683
sl@0
   684
	return EFalse;	
sl@0
   685
	}
sl@0
   686
sl@0
   687
//
sl@0
   688
// FindMostPrefEmpty
sl@0
   689
//
sl@0
   690
// Checks all zones and returns the most preferable RAM zone which 
sl@0
   691
// is completely emtpy
sl@0
   692
//
sl@0
   693
// @param aZoneIndex On return this will contain the index into gZoneUtilArray of the most preferable empty RAM zone.
sl@0
   694
// @param aPrefIndex On return this will contain the index into gPrefArray of the most preferable empty RAM zone.
sl@0
   695
//
sl@0
   696
// @return KErrNotFound if a zone cannot be found, else KErrNone
sl@0
   697
//
sl@0
   698
TInt FindMostPrefEmpty(TUint& aZoneIndex, TUint* aPrefIndex = NULL)
sl@0
   699
	{
sl@0
   700
	// Get the most pref zone which is completely free to use as a test zone
sl@0
   701
	GetPrefOrder();
sl@0
   702
	TUint prefIndex = 0;
sl@0
   703
	for (; prefIndex < gZoneCount; prefIndex++)
sl@0
   704
		{
sl@0
   705
		TUint zoneIndex = gPrefArray[prefIndex];
sl@0
   706
		if (gZoneUtilArray[zoneIndex].iFreePages == gZoneUtilArray[zoneIndex].iPhysPages)
sl@0
   707
			{
sl@0
   708
			aZoneIndex = zoneIndex;
sl@0
   709
			if (aPrefIndex)
sl@0
   710
				{
sl@0
   711
				*aPrefIndex = prefIndex;
sl@0
   712
				}
sl@0
   713
			return KErrNone;
sl@0
   714
			}
sl@0
   715
		}
sl@0
   716
	return KErrNotFound;
sl@0
   717
	}
sl@0
   718
sl@0
   719
//
sl@0
   720
// FindLeastPrefEmpty
sl@0
   721
//
sl@0
   722
// Checks all zones and returns the least preferable RAM zone which 
sl@0
   723
// is completely emtpy
sl@0
   724
//
sl@0
   725
// @param aZoneIndex On return this will contain the index into gZoneUtilArray of the least preferable empty RAM zone.
sl@0
   726
// @param aPrefIndex On return this will contain the index into gPrefArray of the least preferable empty RAM zone.
sl@0
   727
//
sl@0
   728
// @return KErrNotFound if a zone cannot be found, else KErrNone
sl@0
   729
//
sl@0
   730
TInt FindLeastPrefEmpty(TUint& aZoneIndex, TUint* aPrefIndex = NULL)
sl@0
   731
	{
sl@0
   732
	// Get the most pref zone which is completely free to use as a test zone
sl@0
   733
	GetPrefOrder();
sl@0
   734
	TInt prefIndex = gZoneCount - 1;
sl@0
   735
	for (; prefIndex >= 0; prefIndex--)
sl@0
   736
		{
sl@0
   737
		TUint zoneIndex = gPrefArray[prefIndex];
sl@0
   738
		if (gZoneUtilArray[zoneIndex].iFreePages == gZoneUtilArray[zoneIndex].iPhysPages)
sl@0
   739
			{
sl@0
   740
			aZoneIndex = zoneIndex;
sl@0
   741
			if (aPrefIndex)
sl@0
   742
				{
sl@0
   743
				*aPrefIndex = (TUint)prefIndex;
sl@0
   744
				}
sl@0
   745
			return KErrNone;
sl@0
   746
			}
sl@0
   747
		}
sl@0
   748
	return KErrNotFound;
sl@0
   749
	}
sl@0
   750
sl@0
   751
//
sl@0
   752
// FindMostPrefWithFree
sl@0
   753
//
sl@0
   754
// Checks all zones and returns the most preferable RAM zone which 
sl@0
   755
// has at least 1 free page 
sl@0
   756
//
sl@0
   757
// @param aZoneIndex On return this will contain the index into gZoneUtilArray of the most preferable RAM zone with free pages.
sl@0
   758
// @param aPrefIndex On return this will contain the index into gPrefArray of the most preferable RAM zone with free pages.
sl@0
   759
//
sl@0
   760
// @return KErrNotFound if a zone cannot be found, else KErrNone
sl@0
   761
//
sl@0
   762
TInt FindMostPrefWithFree(TUint& aZoneIndex, TUint* aPrefIndex = NULL)
sl@0
   763
	{
sl@0
   764
	// Get the most pref zone which has free pages
sl@0
   765
	GetPrefOrder();
sl@0
   766
	TUint prefIndex = 0;
sl@0
   767
	for (; prefIndex < gZoneCount; prefIndex++)
sl@0
   768
		{
sl@0
   769
		TUint zoneIndex = gPrefArray[prefIndex];
sl@0
   770
		if (gZoneUtilArray[zoneIndex].iFreePages)
sl@0
   771
			{
sl@0
   772
			aZoneIndex = zoneIndex;
sl@0
   773
			if (aPrefIndex)
sl@0
   774
				{
sl@0
   775
				*aPrefIndex = prefIndex;
sl@0
   776
				}
sl@0
   777
			return KErrNone;
sl@0
   778
			}
sl@0
   779
		}
sl@0
   780
	return KErrNotFound;
sl@0
   781
	}
sl@0
   782
//
sl@0
   783
// CanGenSucceed
sl@0
   784
//
sl@0
   785
// Check whether a call to TRamDefragRequest::DefragRam() would be able to 
sl@0
   786
// succeed or not. 
sl@0
   787
//
sl@0
   788
TBool CanGenSucceed()
sl@0
   789
	{
sl@0
   790
	GetPrefOrder();
sl@0
   791
	TBool genSucceed = EFalse;
sl@0
   792
	// Work out if general has anything to do
sl@0
   793
	for(TInt prefIndex = (TInt)gZoneCount-1; prefIndex >= 0; prefIndex--)
sl@0
   794
		{
sl@0
   795
		TUint zoneIndex = gPrefArray[prefIndex];
sl@0
   796
		TESTDEBUG(test.Printf(_L("prefIndex = %d zoneIndex = 0x%x\n"), prefIndex, zoneIndex));
sl@0
   797
sl@0
   798
		TUint samePrefCount = 1;
sl@0
   799
		TUint samePrefSucceed = 0;
sl@0
   800
		// The number of zones of this preference that will be skipped by the general defrag
sl@0
   801
		TUint samePrefEmptyImmovable = 0;
sl@0
   802
sl@0
   803
		// Determine how many zones have the same preference as this one
sl@0
   804
		TInt prevPrefIndex = (prefIndex != 0)? (prefIndex - 1) : -1;
sl@0
   805
		for (; prevPrefIndex >= 0; prevPrefIndex--)
sl@0
   806
			{
sl@0
   807
			TUint prevIndex = gPrefArray[prevPrefIndex];
sl@0
   808
			if (ZonesSamePref(zoneIndex, prevIndex))
sl@0
   809
				{
sl@0
   810
				samePrefCount++;
sl@0
   811
				}
sl@0
   812
			else // no more zones with this preference
sl@0
   813
				break;
sl@0
   814
			}
sl@0
   815
		TESTDEBUG(test.Printf(_L("samePrefCount = %d\n"), samePrefCount));
sl@0
   816
sl@0
   817
		for (TInt l = prefIndex - (samePrefCount-1); l <= prefIndex; l++)
sl@0
   818
			{
sl@0
   819
			TUint curPrefIndex = gPrefArray[l];
sl@0
   820
			TESTDEBUG(test.Printf(_L("curPrefIndex = %d\n"), curPrefIndex));
sl@0
   821
			if (gZoneUtilArray[curPrefIndex].iFreePages != gZoneConfigArray[curPrefIndex].iPhysPages)
sl@0
   822
				{
sl@0
   823
				TBool clearMovable = EFalse;
sl@0
   824
				TBool clearDiscardable = EFalse;
sl@0
   825
sl@0
   826
				if (gZoneUtilArray[curPrefIndex].iAllocUnknown || gZoneUtilArray[curPrefIndex].iAllocFixed)
sl@0
   827
					{
sl@0
   828
					TESTDEBUG(test.Printf(_L("unknown or fixed\n")));
sl@0
   829
					samePrefEmptyImmovable++;
sl@0
   830
					continue;
sl@0
   831
					}
sl@0
   832
				if (gZoneUtilArray[curPrefIndex].iAllocMovable)
sl@0
   833
					{// determine if movable can potentially be cleared from this zone
sl@0
   834
					TUint freeInLower = 0;
sl@0
   835
					for (TInt j=0; j <= prefIndex; j++)
sl@0
   836
						{
sl@0
   837
						TUint idx = gPrefArray[j];
sl@0
   838
						if (idx == curPrefIndex)
sl@0
   839
							continue;
sl@0
   840
						freeInLower += gZoneUtilArray[idx].iFreePages;
sl@0
   841
						}
sl@0
   842
					if (gZoneUtilArray[curPrefIndex].iAllocMovable <= freeInLower)
sl@0
   843
						{
sl@0
   844
						clearMovable = ETrue;
sl@0
   845
						TESTDEBUG(test.Printf(_L("Can clear movable, curPrefIndex = %d\n"), curPrefIndex));
sl@0
   846
						}
sl@0
   847
					}
sl@0
   848
				else
sl@0
   849
					{
sl@0
   850
					TESTDEBUG(test.Printf(_L("Can clear movable, curPrefIndex = %d\n"), curPrefIndex));
sl@0
   851
					clearMovable = ETrue;
sl@0
   852
					}
sl@0
   853
				if (gZoneUtilArray[curPrefIndex].iAllocDiscardable)
sl@0
   854
					{
sl@0
   855
					if (gPagedRom)
sl@0
   856
						{
sl@0
   857
						TUint minCacheSize = 0;
sl@0
   858
						TUint maxCacheSize = 0;
sl@0
   859
						TUint currentCacheSize = 0;
sl@0
   860
sl@0
   861
						DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
sl@0
   862
						TUint spareCache = currentCacheSize - minCacheSize;
sl@0
   863
						if (spareCache >= gZoneUtilArray[curPrefIndex].iAllocDiscardable )
sl@0
   864
							{
sl@0
   865
							clearDiscardable = ETrue;
sl@0
   866
							TESTDEBUG(test.Printf(_L("Paged: Can clear discardable\n")));
sl@0
   867
							}
sl@0
   868
						else
sl@0
   869
							{// determine space for discardable in more preferable zones
sl@0
   870
							TUint freeInLower = 0;
sl@0
   871
							for (TInt j=0; j <= prefIndex; j++)
sl@0
   872
								{
sl@0
   873
								TUint idx = gPrefArray[j];
sl@0
   874
								if (idx == curPrefIndex)
sl@0
   875
									continue;
sl@0
   876
								freeInLower += gZoneUtilArray[idx].iFreePages;
sl@0
   877
								}
sl@0
   878
							if (gZoneUtilArray[curPrefIndex].iAllocDiscardable - spareCache <= freeInLower)
sl@0
   879
								{
sl@0
   880
								clearDiscardable = ETrue;	
sl@0
   881
								TESTDEBUG(test.Printf(_L("Paged: Can clear discardable curPrefIndex = %d\n"), curPrefIndex));
sl@0
   882
								}
sl@0
   883
							}
sl@0
   884
						}
sl@0
   885
					else
sl@0
   886
						{//Should always be OK to discard as no min cache size on non-paged ROMS
sl@0
   887
						clearDiscardable = ETrue;
sl@0
   888
						test.Printf(_L("Can clear discardable curPrefIndex = %d\n"), curPrefIndex);
sl@0
   889
						}
sl@0
   890
					}
sl@0
   891
				else
sl@0
   892
					{
sl@0
   893
					clearDiscardable = ETrue;
sl@0
   894
					}
sl@0
   895
sl@0
   896
				if (clearDiscardable && clearMovable)
sl@0
   897
					{
sl@0
   898
					samePrefSucceed++;
sl@0
   899
					TESTDEBUG(test.Printf(_L("General should succeed ID=%x\n"), gZoneConfigArray[curPrefIndex].iZoneId));
sl@0
   900
					}
sl@0
   901
				}
sl@0
   902
			else
sl@0
   903
				{//zone already empty
sl@0
   904
				samePrefEmptyImmovable++;
sl@0
   905
				}
sl@0
   906
			}
sl@0
   907
		if (samePrefSucceed == 0 && samePrefEmptyImmovable == 0)
sl@0
   908
			{// no zones can be defragged and none are already empty/have immovable.
sl@0
   909
			break;
sl@0
   910
			}
sl@0
   911
		if (samePrefEmptyImmovable != samePrefCount)
sl@0
   912
			{// Have reached some zones with allocated pages in them.
sl@0
   913
			if (samePrefSucceed + samePrefEmptyImmovable == samePrefCount)
sl@0
   914
				{// general should definitely succeed as each of the zones of this preference 
sl@0
   915
				// can be emptied or are already empty/have immovable pages allocated.
sl@0
   916
				TESTDEBUG(test.Printf(_L("General should succeed \n")));
sl@0
   917
				genSucceed = ETrue;
sl@0
   918
				}
sl@0
   919
			break;
sl@0
   920
			}
sl@0
   921
		prefIndex -= samePrefCount - 1;
sl@0
   922
		}
sl@0
   923
	return genSucceed;
sl@0
   924
	}
sl@0
   925
sl@0
   926
sl@0
   927
//
sl@0
   928
// ReadByte
sl@0
   929
//
sl@0
   930
// Read a particular byte 
sl@0
   931
//
sl@0
   932
TUint8 ReadByte(volatile TUint8* aPtr)
sl@0
   933
	{
sl@0
   934
	return *aPtr;
sl@0
   935
	}
sl@0
   936
sl@0
   937
sl@0
   938
//
sl@0
   939
// AllocDiscardable
sl@0
   940
//
sl@0
   941
// Allocate Discardable pages in the form of demand paged pages
sl@0
   942
//
sl@0
   943
// @param aNumDiscardableBytes On return this will contain the number of discardable bytes above the min cache size.
sl@0
   944
// @param aMaxBytes The new limit for maximum number of bytes in the DP cache, set to KMaxTUInt64 to fill RAM.
sl@0
   945
// @param aMinOffsetBytes When not set to KMaxTUint64, this sets the min cache size to be the max cache size - aMinOffsetBytes.
sl@0
   946
//
sl@0
   947
TInt AllocDiscardable(TInt& aNumDiscardableBytes, TUint64 aMaxBytes = KMaxTUint64, TUint64 aMinOffsetBytes = KMaxTUint64)
sl@0
   948
	{
sl@0
   949
	TUint minCacheSize = 0;
sl@0
   950
	TUint maxCacheSize = 0;
sl@0
   951
	TUint currentCacheSize = 0;
sl@0
   952
	DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
sl@0
   953
	TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
sl@0
   954
								minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
sl@0
   955
sl@0
   956
	TESTDEBUG(test.Printf(_L("SetCacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x\n"), minCacheSize >> gPageShift, aMaxBytes >> gPageShift));
sl@0
   957
sl@0
   958
	if (aMaxBytes == KMaxTUint64)
sl@0
   959
		{// Need to fill all of free memory with discardable pages
sl@0
   960
		UpdateRamInfo();
sl@0
   961
		maxCacheSize = minCacheSize + gFreeRam;
sl@0
   962
		if (aMinOffsetBytes != KMaxTUint64)
sl@0
   963
			{// Set the min cache size relative to the max cache size.
sl@0
   964
			minCacheSize = maxCacheSize - aMinOffsetBytes;
sl@0
   965
			}
sl@0
   966
		TESTDEBUG(test.Printf(_L("free 0x%x max 0x%x min 0x%x\n"), gFreeRam, maxCacheSize, minCacheSize));
sl@0
   967
		}
sl@0
   968
	else
sl@0
   969
		{
sl@0
   970
		maxCacheSize = aMaxBytes;
sl@0
   971
		}
sl@0
   972
sl@0
   973
	TInt r = DPTest::SetCacheSize(maxCacheSize, maxCacheSize);
sl@0
   974
	if (r != KErrNone)
sl@0
   975
		return r;
sl@0
   976
	r = DPTest::SetCacheSize(minCacheSize, maxCacheSize);
sl@0
   977
	if (r != KErrNone)
sl@0
   978
		return r;
sl@0
   979
	DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
sl@0
   980
	TESTDEBUG(test.Printf(_L("After CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
sl@0
   981
					minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
sl@0
   982
sl@0
   983
	aNumDiscardableBytes = currentCacheSize - minCacheSize;
sl@0
   984
	TESTDEBUG(test.Printf(_L("Number of discardable bytes 0x%x\n"), aNumDiscardableBytes));
sl@0
   985
sl@0
   986
	if (aMaxBytes == KMaxTUint64)
sl@0
   987
		{
sl@0
   988
		UpdateRamInfo();
sl@0
   989
		if (gFreeRam != aNumDiscardableBytes)
sl@0
   990
			{// The only free RAM should be that of the DP cache.
sl@0
   991
			test.Printf(_L("gFreeRam 0x%x aNumDiscardableBytes 0x%x\n"), gFreeRam, aNumDiscardableBytes);
sl@0
   992
			return KErrGeneral;
sl@0
   993
			}
sl@0
   994
		}
sl@0
   995
	return KErrNone;
sl@0
   996
	}	
sl@0
   997
	
sl@0
   998
sl@0
   999
//
sl@0
  1000
// ResetDPCache
sl@0
  1001
//
sl@0
  1002
// Flush the cache and set the boundaries back to their original values
sl@0
  1003
//
sl@0
  1004
void ResetDPCache()
sl@0
  1005
	{
sl@0
  1006
	if (gPagedRom)
sl@0
  1007
		{
sl@0
  1008
		TUint minCacheSize = 0;
sl@0
  1009
		TUint maxCacheSize = 0;
sl@0
  1010
		TUint currentCacheSize = 0;
sl@0
  1011
		
sl@0
  1012
		TESTDEBUG(test.Printf(_L("FlushCache\n")));
sl@0
  1013
		TInt r = DPTest::FlushCache();
sl@0
  1014
sl@0
  1015
		DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
sl@0
  1016
		TESTDEBUG(test.Printf(_L("After CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
sl@0
  1017
						minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
sl@0
  1018
		
sl@0
  1019
		
sl@0
  1020
		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
sl@0
  1021
		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMaxCacheSize);
sl@0
  1022
		TESTDEBUG(test.Printf(_L("SetCacheSize returns r = %d\n"), r));
sl@0
  1023
		DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
sl@0
  1024
		TESTDEBUG(test.Printf(_L("After CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
sl@0
  1025
						minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
sl@0
  1026
		}
sl@0
  1027
	}
sl@0
  1028
	
sl@0
  1029
//
sl@0
  1030
// WriteToChunk
sl@0
  1031
//
sl@0
  1032
// Write to a number of chunks
sl@0
  1033
//
sl@0
  1034
TInt WriteToChunk(RChunk* aChunkArray, TUint aChunkArraySize, TInt aChunk, TUint8 aStart=0)
sl@0
  1035
	{
sl@0
  1036
	for (TUint i=0; i<10; i++) // Write to all chunks 10 times
sl@0
  1037
		{
sl@0
  1038
		for (TUint j=0; j < aChunkArraySize; j++) //Write to all open chunks except aChunk.
sl@0
  1039
			{
sl@0
  1040
			if (aChunkArray[j].Handle() != NULL)
sl@0
  1041
				{
sl@0
  1042
				if ((TInt)j == aChunk) // Don't write to specified chunk
sl@0
  1043
					{	
sl@0
  1044
					continue;	
sl@0
  1045
					}
sl@0
  1046
				TUint8* base = aChunkArray[j].Base();
sl@0
  1047
				for (TUint8 k = aStart; k < aStart + 10; k++)
sl@0
  1048
					{
sl@0
  1049
					*base++ = k; // write 0 - 9 to the chunk
sl@0
  1050
					}
sl@0
  1051
				}		
sl@0
  1052
			}
sl@0
  1053
		}
sl@0
  1054
	return KErrNone;
sl@0
  1055
	}
sl@0
  1056
sl@0
  1057
sl@0
  1058
//
sl@0
  1059
// ReadChunk
sl@0
  1060
//
sl@0
  1061
// Read chunks - If a chunk is specified, that particular chunk is not read
sl@0
  1062
//
sl@0
  1063
TInt ReadChunk(RChunk* aChunkArray, TUint aChunkArraySize, TInt aChunk=-1)
sl@0
  1064
	{
sl@0
  1065
	for (TUint j=0; j < aChunkArraySize; j++) //Read all open chunks
sl@0
  1066
		{
sl@0
  1067
		if (aChunkArray[j].Handle() != NULL)
sl@0
  1068
			{
sl@0
  1069
			if ((TInt)j == aChunk) // Don't read specified chunk
sl@0
  1070
				{	
sl@0
  1071
				continue;	
sl@0
  1072
				}
sl@0
  1073
			TUint8* base = aChunkArray[j].Base();
sl@0
  1074
			while ((aChunkArray[j].Base() + aChunkArray[j].Size()) != base)
sl@0
  1075
				{
sl@0
  1076
				READ(base++);				
sl@0
  1077
				}
sl@0
  1078
			}		
sl@0
  1079
		}
sl@0
  1080
	return KErrNone;
sl@0
  1081
	}
sl@0
  1082
	
sl@0
  1083
sl@0
  1084
//
sl@0
  1085
// RemoveChunkAlloc
sl@0
  1086
//
sl@0
  1087
// Remove ALL chunks allocated
sl@0
  1088
//
sl@0
  1089
// @param aChunkArray The array that stores a reference to the chunks created.
sl@0
  1090
// @param aChunkArraySize The size of aChunkArray.
sl@0
  1091
//
sl@0
  1092
void RemoveChunkAlloc(RChunk*& aChunkArray, TUint& aChunkArraySize)
sl@0
  1093
	{
sl@0
  1094
	TInt closedChunks = 0;
sl@0
  1095
sl@0
  1096
	if (aChunkArray == NULL)
sl@0
  1097
		{// The chunk array has already been deleted.
sl@0
  1098
		return;
sl@0
  1099
		}
sl@0
  1100
sl@0
  1101
	for (TUint i = 0; i < aChunkArraySize; i++)
sl@0
  1102
		{
sl@0
  1103
		if (aChunkArray[i].Handle() != NULL)
sl@0
  1104
			{
sl@0
  1105
			aChunkArray[i].Close();
sl@0
  1106
			closedChunks ++;
sl@0
  1107
			}
sl@0
  1108
		}
sl@0
  1109
	delete[] aChunkArray;
sl@0
  1110
	aChunkArray = NULL;
sl@0
  1111
	aChunkArraySize = 0;
sl@0
  1112
	UpdateRamInfo();
sl@0
  1113
	test.Printf(_L("Free RAM after closing %d chunks = 0x%x\n"),closedChunks,gFreeRam);
sl@0
  1114
	}	
sl@0
  1115
sl@0
  1116
sl@0
  1117
TBool SpaceAvailForPageTables(TUint aZoneIndex, TInt aNumPages)
sl@0
  1118
	{
sl@0
  1119
	// Every 1MB allocated needs a new page table
sl@0
  1120
	const TUint KDataBytesPerPageTable = 1024 * 1024; 
sl@0
  1121
	
sl@0
  1122
	// 1 Page can fit 4 page tables
sl@0
  1123
	const TUint KPageTablesPerPage = 4;
sl@0
  1124
	
sl@0
  1125
	
sl@0
  1126
	GetAllPageInfo();
sl@0
  1127
	if (aNumPages == KFillAllMovable)
sl@0
  1128
		{
sl@0
  1129
		aNumPages = gTotalPageCount.iFreePages;
sl@0
  1130
		}
sl@0
  1131
sl@0
  1132
	TUint allocBytes = aNumPages << gPageShift;
sl@0
  1133
sl@0
  1134
	// Add 1 as you always require at least 1 page table
sl@0
  1135
	TUint pageTablesRequired = (allocBytes / KDataBytesPerPageTable) + 1;
sl@0
  1136
	// Add 1 as the first 1-3 page tables may require a new page.
sl@0
  1137
	TUint pageTablePagesRequired = (pageTablesRequired / KPageTablesPerPage) + 1;
sl@0
  1138
	
sl@0
  1139
	// Determine the number of free pages in the other zones
sl@0
  1140
	TUint freeInOther = 0;
sl@0
  1141
	for (TUint index = 0; index < gZoneCount; index++)
sl@0
  1142
		{
sl@0
  1143
		if (index != aZoneIndex)
sl@0
  1144
			{
sl@0
  1145
			freeInOther += gZoneUtilArray[index].iFreePages;
sl@0
  1146
			}
sl@0
  1147
		}
sl@0
  1148
sl@0
  1149
	// Need an overhead for the heap to grow (5 pages)
sl@0
  1150
	const TUint KOverhead = 5;
sl@0
  1151
	if (freeInOther < pageTablePagesRequired + KOverhead)
sl@0
  1152
		{// Not enough space in other zones to fit all page tables
sl@0
  1153
		test.Printf(_L("No space in other zones for page table pages\n"));
sl@0
  1154
		return EFalse;
sl@0
  1155
		}
sl@0
  1156
	// There is space available in the other zones to fit all the page tables
sl@0
  1157
	return ETrue;
sl@0
  1158
	}
sl@0
  1159
//
sl@0
  1160
// AllocMovable
sl@0
  1161
//
sl@0
  1162
// Allocate movable memory in the form of chunks
sl@0
  1163
//
sl@0
  1164
// @param aChunkArray The array to store a reference to the chunks created.
sl@0
  1165
// @param aChunkArraySize The size of aChunkArray.
sl@0
  1166
// @param aNumChunks The number of chunks to create.
sl@0
  1167
// @param aNumPages The size of each chunk.
sl@0
  1168
//
sl@0
  1169
TInt AllocMovable(RChunk*& aChunkArray, TUint& aChunkArraySize, TInt aNumChunks, TUint aChunkSize=KChunkDefaultSize, TBool aForceFill = ETrue)
sl@0
  1170
	{
sl@0
  1171
	TUint i = 0;
sl@0
  1172
	TInt r = 0;
sl@0
  1173
	TUint chunksAllocd = 0;
sl@0
  1174
	TBool fillAll = EFalse;
sl@0
  1175
sl@0
  1176
	TInt numChunks = aNumChunks;
sl@0
  1177
	UpdateRamInfo();
sl@0
  1178
	// Allocate chunks to take up all of memory with the maximum number of chunks
sl@0
  1179
	if (aNumChunks == KFillAllMovable)
sl@0
  1180
		{
sl@0
  1181
		fillAll = ETrue;
sl@0
  1182
		if (aChunkSize == KChunkDefaultSize)
sl@0
  1183
			{
sl@0
  1184
			numChunks = KMaxChunks;
sl@0
  1185
			aChunkSize = gFreeRam / numChunks; 
sl@0
  1186
			}
sl@0
  1187
		else
sl@0
  1188
			{
sl@0
  1189
			numChunks = gFreeRam  / aChunkSize;
sl@0
  1190
			}
sl@0
  1191
		}
sl@0
  1192
sl@0
  1193
	test.Printf(_L("gFreeRam = 0x%x, aChunkSize = 0x%x, numChunks = %d\n"), gFreeRam, aChunkSize, numChunks);
sl@0
  1194
	
sl@0
  1195
	// Allocate as many chunks as is specified, either with the default chunk size or a specified chunk size
sl@0
  1196
	if (aChunkArray == NULL)
sl@0
  1197
		{
sl@0
  1198
		aChunkArraySize = numChunks;
sl@0
  1199
		aChunkArray = new RChunk[aChunkArraySize];
sl@0
  1200
		if (aChunkArray == NULL)
sl@0
  1201
			return KErrNoMemory;
sl@0
  1202
		}
sl@0
  1203
sl@0
  1204
	// Create chunks for each RChunk with a NULL handle.
sl@0
  1205
	for (i = 0; i < aChunkArraySize; i++)
sl@0
  1206
		{
sl@0
  1207
		if (aChunkArray[i].Handle() == NULL)
sl@0
  1208
			{
sl@0
  1209
			// Keep going even if a chunk creation fails as the flag tests rely 
sl@0
  1210
			// on this.
sl@0
  1211
			r = aChunkArray[i].CreateLocal(aChunkSize, aChunkSize);
sl@0
  1212
			if (r != KErrNone && fillAll && aForceFill)
sl@0
  1213
				{
sl@0
  1214
				while (aChunkArray[i].CreateLocal(aChunkSize, aChunkSize) != KErrNone)
sl@0
  1215
					{
sl@0
  1216
					aChunkSize -= gPageSize;
sl@0
  1217
					}
sl@0
  1218
				}
sl@0
  1219
			if (r == KErrNone)	
sl@0
  1220
				{
sl@0
  1221
				chunksAllocd++;
sl@0
  1222
				}
sl@0
  1223
			}
sl@0
  1224
		User::After(10); // Wait so that the next chunk gets allocated in the next time slice
sl@0
  1225
		}
sl@0
  1226
	test.Printf(_L("Number of chunks allocd = %d\n"),chunksAllocd);
sl@0
  1227
	return r;
sl@0
  1228
	}
sl@0
  1229
sl@0
  1230
//
sl@0
  1231
// ZoneAllocMovable
sl@0
  1232
//
sl@0
  1233
// Allocate the specified number of movable pages to a specific zone
sl@0
  1234
// If the number of pages is not specified, then fill the specified zone with
sl@0
  1235
// movable pages
sl@0
  1236
//
sl@0
  1237
// @param aChunkArray The array to store a reference to the chunks created.
sl@0
  1238
// @param aChunkArraySize The size of aChunkArray.
sl@0
  1239
// @param aZoneIndex The zone index to allocate movable pages to.
sl@0
  1240
// @param aNumPages The number of movable pages to allocate.
sl@0
  1241
//
sl@0
  1242
TInt ZoneAllocMovable(RChunk*& aChunkArray, TUint& aChunkArraySize, TUint aZoneIndex, TUint aNumPages = KMaxTUint)
sl@0
  1243
	{
sl@0
  1244
	ResetRamZoneFlags();
sl@0
  1245
	TInt r = KErrNone;
sl@0
  1246
	TUint allocBytes = 0;
sl@0
  1247
sl@0
  1248
	if (aNumPages == KMaxTUint)
sl@0
  1249
		{
sl@0
  1250
		aNumPages = gZoneUtilArray[aZoneIndex].iFreePages;
sl@0
  1251
		}
sl@0
  1252
sl@0
  1253
	allocBytes = aNumPages << gPageShift;
sl@0
  1254
sl@0
  1255
	if (!SpaceAvailForPageTables(aZoneIndex, aNumPages))
sl@0
  1256
		{
sl@0
  1257
		return KErrGeneral;
sl@0
  1258
		}
sl@0
  1259
sl@0
  1260
	// Block all other zones from allocation
sl@0
  1261
	for(TUint index = 0; index < gZoneCount; index++)
sl@0
  1262
		{
sl@0
  1263
		if (index == aZoneIndex)
sl@0
  1264
			{
sl@0
  1265
			r = Ldd.SetZoneFlag(gZoneConfigArray[index].iZoneId, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
sl@0
  1266
			}
sl@0
  1267
		else
sl@0
  1268
			{
sl@0
  1269
			r = Ldd.SetZoneFlag(gZoneConfigArray[index].iZoneId, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
sl@0
  1270
			}
sl@0
  1271
		if (r != KErrNone)
sl@0
  1272
			{
sl@0
  1273
			test.Printf(_L("Failed to set RAM zone flag on zone %d r = %d\n"), index, r);
sl@0
  1274
			return r;
sl@0
  1275
			}
sl@0
  1276
		}
sl@0
  1277
sl@0
  1278
	// Allocate the movable pages
sl@0
  1279
	r = AllocMovable(aChunkArray, aChunkArraySize, 1, allocBytes);
sl@0
  1280
sl@0
  1281
	ResetRamZoneFlags();
sl@0
  1282
	return r;
sl@0
  1283
	}
sl@0
  1284
sl@0
  1285
//
sl@0
  1286
// ZoneAllocDiscard
sl@0
  1287
//
sl@0
  1288
// Allocate the specified number of discardable pages to a specific zone
sl@0
  1289
//
sl@0
  1290
// @param aZoneIndex The zone index to allocate discardable pages to.
sl@0
  1291
// @param aNumPages The number of discardable pages to allocate.
sl@0
  1292
// @param aDisPages On return this will contain the number of discardable pages allocated
sl@0
  1293
//
sl@0
  1294
TInt ZoneAllocDiscard(TUint aZoneIndex, TUint aNumPages, TInt& aDisPages)
sl@0
  1295
	{
sl@0
  1296
	TInt r = KErrNone;
sl@0
  1297
	ResetRamZoneFlags();
sl@0
  1298
	
sl@0
  1299
	for (TUint index = 0; index < gZoneCount; index++)
sl@0
  1300
		{
sl@0
  1301
		TUint zoneID = gZoneConfigArray[index].iZoneId;
sl@0
  1302
		if (index == aZoneIndex)
sl@0
  1303
			{
sl@0
  1304
			r = Ldd.SetZoneFlag(zoneID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
sl@0
  1305
			}
sl@0
  1306
		else
sl@0
  1307
			{
sl@0
  1308
			r = Ldd.SetZoneFlag(zoneID, gZoneConfigArray[index].iFlags, NO_DISCARD_FLAG);
sl@0
  1309
			}
sl@0
  1310
sl@0
  1311
		if (r != KErrNone)
sl@0
  1312
			{			
sl@0
  1313
			test.Printf(_L("Failed to set flag r = %d\n"), r);
sl@0
  1314
			return r;
sl@0
  1315
			}
sl@0
  1316
		}
sl@0
  1317
	
sl@0
  1318
	TUint disBytes = gTotalPageCount.iDiscardablePages + aNumPages << gPageShift;
sl@0
  1319
	r = AllocDiscardable(aDisPages, disBytes);
sl@0
  1320
	aDisPages = aDisPages >> gPageShift;
sl@0
  1321
	if (r != KErrNone)
sl@0
  1322
		{
sl@0
  1323
		test.Printf(_L("Discardable pages not allocated r = %d\n"), r);
sl@0
  1324
		}
sl@0
  1325
	ResetRamZoneFlags();
sl@0
  1326
	return r;
sl@0
  1327
	}
sl@0
  1328
sl@0
  1329
//
sl@0
  1330
// FreeMovable
sl@0
  1331
//
sl@0
  1332
// Free movable pages by closing chunks. 
sl@0
  1333
// The function will close every other chunk so that the movable pages are scattered in every zone
sl@0
  1334
//
sl@0
  1335
// @param aChunkArray The array that stores reference to the chunks created.
sl@0
  1336
// @param aChunkArraySize The size of aChunkArray.
sl@0
  1337
//
sl@0
  1338
TInt FreeMovable(RChunk* aChunkArray, TUint aChunkArraySize)
sl@0
  1339
	{
sl@0
  1340
	TUint i;
sl@0
  1341
	TInt closedChunks = 0;
sl@0
  1342
		
sl@0
  1343
	for (i=0; i < aChunkArraySize; i+=2) // Close every other chunk
sl@0
  1344
		{
sl@0
  1345
		if (aChunkArray[i].Handle() != NULL)
sl@0
  1346
			{
sl@0
  1347
			aChunkArray[i].Close();
sl@0
  1348
			closedChunks ++;
sl@0
  1349
			}
sl@0
  1350
		}
sl@0
  1351
	UpdateRamInfo();
sl@0
  1352
	test.Printf(_L("Free RAM after closing %d chunks = 0x%x\n"),closedChunks,gFreeRam);
sl@0
  1353
	return KErrNone;
sl@0
  1354
	}
sl@0
  1355
sl@0
  1356
//
sl@0
  1357
// GetBestZone
sl@0
  1358
//
sl@0
  1359
// Obtains the most preferable zone for allocating a specific type of page
sl@0
  1360
//
sl@0
  1361
// @param aPageType The page type that we are interested in
sl@0
  1362
// @param aBestPrefIndex The index into the preference array for the zone.
sl@0
  1363
//
sl@0
  1364
// @return KErrNotFound if a zone cannot be found, else the zone index into gZoneUtilArray
sl@0
  1365
//
sl@0
  1366
TInt GetBestZone(TInt aPageType, TUint* aBestPrefIndex = NULL)
sl@0
  1367
	{
sl@0
  1368
	GetPrefOrder();
sl@0
  1369
sl@0
  1370
sl@0
  1371
	switch (aPageType)
sl@0
  1372
		{
sl@0
  1373
		case BEST_MOVABLE:			
sl@0
  1374
		case BEST_DISCARDABLE:
sl@0
  1375
			{
sl@0
  1376
			TInt bestIndex = KErrNotFound;
sl@0
  1377
			TUint bestPrefIndex = 0;
sl@0
  1378
			TUint curIndex = 0;
sl@0
  1379
			TInt startIndex = KErrNotFound;
sl@0
  1380
			TBool zoneFound = EFalse;
sl@0
  1381
			// Find the least preferable zone that has movable or discardable pages
sl@0
  1382
			for (TInt prefIndex = gZoneCount - 1; prefIndex >= 0; prefIndex--)
sl@0
  1383
				{
sl@0
  1384
				curIndex = gPrefArray[prefIndex];
sl@0
  1385
				if (gZoneUtilArray[curIndex].iAllocMovable || gZoneUtilArray[curIndex].iAllocDiscardable)
sl@0
  1386
					{
sl@0
  1387
					startIndex = prefIndex;
sl@0
  1388
					break;
sl@0
  1389
					}
sl@0
  1390
				}
sl@0
  1391
			if (startIndex == KErrNotFound)
sl@0
  1392
				return startIndex;
sl@0
  1393
				
sl@0
  1394
			// Work up the preference list to look for the best zone
sl@0
  1395
			for (TInt prefIndex = startIndex; prefIndex >= 0; prefIndex--)
sl@0
  1396
				{
sl@0
  1397
				curIndex = gPrefArray[prefIndex];
sl@0
  1398
				if (gZoneUtilArray[curIndex].iFreePages)
sl@0
  1399
					{
sl@0
  1400
					bestIndex = curIndex;
sl@0
  1401
					bestPrefIndex = prefIndex;
sl@0
  1402
					zoneFound = ETrue;
sl@0
  1403
					break;
sl@0
  1404
					}
sl@0
  1405
				}
sl@0
  1406
			
sl@0
  1407
			// If the zone still isn't found, look down the preference list
sl@0
  1408
			if (!zoneFound)
sl@0
  1409
				{
sl@0
  1410
				for (TUint prefIndex = startIndex; prefIndex < gZoneCount; prefIndex++)
sl@0
  1411
					{
sl@0
  1412
					curIndex = gPrefArray[prefIndex];
sl@0
  1413
					if (gZoneUtilArray[curIndex].iFreePages)
sl@0
  1414
						{
sl@0
  1415
						bestIndex = curIndex;
sl@0
  1416
						bestPrefIndex = prefIndex;
sl@0
  1417
						zoneFound = ETrue;
sl@0
  1418
						break;
sl@0
  1419
						}
sl@0
  1420
					}
sl@0
  1421
				}
sl@0
  1422
				
sl@0
  1423
			test.Printf(_L("leastPref = %d\n"), bestIndex);
sl@0
  1424
			if (aBestPrefIndex)
sl@0
  1425
				*aBestPrefIndex = bestPrefIndex;
sl@0
  1426
			return bestIndex;
sl@0
  1427
			}
sl@0
  1428
sl@0
  1429
		case BEST_FIXED:
sl@0
  1430
			for (TUint prefIndex = 0; prefIndex < gZoneCount; prefIndex++)
sl@0
  1431
				{
sl@0
  1432
				TUint mostPref = gPrefArray[prefIndex];
sl@0
  1433
				if (gZoneUtilArray[mostPref].iFreePages != 0 || 
sl@0
  1434
					gZoneUtilArray[mostPref].iAllocMovable != 0 ||
sl@0
  1435
					gZoneUtilArray[mostPref].iAllocDiscardable != 0)
sl@0
  1436
					{
sl@0
  1437
					test.Printf(_L("mostPref = %d\n"), mostPref);
sl@0
  1438
					if (aBestPrefIndex)
sl@0
  1439
						*aBestPrefIndex = prefIndex;
sl@0
  1440
					return mostPref;
sl@0
  1441
					}
sl@0
  1442
				}
sl@0
  1443
			break;
sl@0
  1444
		}
sl@0
  1445
	
sl@0
  1446
	test.Printf(_L("Cannot find zone\n"));
sl@0
  1447
	return KErrNotFound;
sl@0
  1448
	}
sl@0
  1449
sl@0
  1450
void GetCandList1(TInt aIndex);
sl@0
  1451
//
sl@0
  1452
// VerifyMovDisAlloc
sl@0
  1453
//
sl@0
  1454
// Checks that all movable and discardable pages are in the correct RAM zones.
sl@0
  1455
// Should only be invoked after a general defragmentation or a general 
sl@0
  1456
// defragmentation followed by an allocation.
sl@0
  1457
//
sl@0
  1458
//	NOTE - This shouldn't be used to verify RAM if a RAM specific allocation or
sl@0
  1459
//	zone claim operation has been performed.
sl@0
  1460
//
sl@0
  1461
// @return KErrNone if RAM layout is good, KErrGeneral if not, KErrNotFound if no
sl@0
  1462
// movable or discardable pages are allocated.
sl@0
  1463
//
sl@0
  1464
TInt VerifyMovDisAlloc()
sl@0
  1465
	{
sl@0
  1466
	GetPrefOrder();
sl@0
  1467
	TInt leastInUse = KErrNotFound;
sl@0
  1468
	TUint requiredMovDis = 0;
sl@0
  1469
	TUint totalMorePrefInUse = 0;
sl@0
  1470
	TBool verifySpread = ETrue;
sl@0
  1471
	TBool prevZoneNotFull = EFalse;
sl@0
  1472
sl@0
  1473
	// Determine which is the least prefable RAM zone in use and how many pages
sl@0
  1474
	// are allocated of each type.
sl@0
  1475
	for (TInt prefIndex = gZoneCount - 1; prefIndex >= 0; prefIndex--)
sl@0
  1476
		{
sl@0
  1477
		TUint index = gPrefArray[prefIndex];
sl@0
  1478
		TUint allocMov = gZoneUtilArray[index].iAllocMovable;
sl@0
  1479
		TUint allocDis = gZoneUtilArray[index].iAllocDiscardable;
sl@0
  1480
		TUint allocFixed = gZoneUtilArray[index].iAllocFixed;
sl@0
  1481
		TESTDEBUG(test.Printf(_L("pref ID 0x%x\n"), gZoneConfigArray[index].iZoneId));
sl@0
  1482
		if (allocMov || allocDis || allocFixed)
sl@0
  1483
			{
sl@0
  1484
			TBool zoneNotFull = EFalse;
sl@0
  1485
			GetCandList1(index);
sl@0
  1486
			TUint candIndex = 0;
sl@0
  1487
			for (; candIndex < gZoneCount; candIndex++)
sl@0
  1488
				{
sl@0
  1489
				TInt zoneIndexCand = gCandList1[candIndex];
sl@0
  1490
				if (zoneIndexCand == KInvalidCandIndex)
sl@0
  1491
					{
sl@0
  1492
					break;
sl@0
  1493
					}
sl@0
  1494
				allocMov  += gZoneUtilArray[zoneIndexCand].iAllocMovable;
sl@0
  1495
				allocDis  += gZoneUtilArray[zoneIndexCand].iAllocDiscardable;
sl@0
  1496
				if (gZoneUtilArray[zoneIndexCand].iFreePages)
sl@0
  1497
					{
sl@0
  1498
					zoneNotFull = ETrue;
sl@0
  1499
					}
sl@0
  1500
				}
sl@0
  1501
			prefIndex -= candIndex - 1;
sl@0
  1502
			if (leastInUse == KErrNotFound)
sl@0
  1503
				{// Have found least preferable RAM zone that is in use.
sl@0
  1504
				leastInUse = index;
sl@0
  1505
				if (allocFixed)
sl@0
  1506
					{// The least preferable RAM zone has fixed pages in it so
sl@0
  1507
					// RAM may be more spread out than is necessary.
sl@0
  1508
					verifySpread = EFalse;
sl@0
  1509
					}
sl@0
  1510
				}
sl@0
  1511
			else
sl@0
  1512
				{
sl@0
  1513
				if ((allocMov || allocDis) && prevZoneNotFull)
sl@0
  1514
					{// The previous least preferable RAM zones were not full so shouldn't
sl@0
  1515
					// be any movable or discardable pages in the RAM zones of this preference.
sl@0
  1516
					test.Printf(_L("Movable or discardable pages in more preferable RAM zones unnecessarily\n"));
sl@0
  1517
					return KErrGeneral;
sl@0
  1518
					}
sl@0
  1519
				prevZoneNotFull = zoneNotFull;
sl@0
  1520
sl@0
  1521
				// Not the least preferable RAM zone so add to total allocatable.
sl@0
  1522
				totalMorePrefInUse += allocMov + allocDis + gZoneUtilArray[index].iFreePages;
sl@0
  1523
				}
sl@0
  1524
			requiredMovDis += allocMov + allocDis;
sl@0
  1525
			}
sl@0
  1526
		}
sl@0
  1527
sl@0
  1528
	if (leastInUse == KErrNotFound)
sl@0
  1529
		{// No movable or discardable pages are allocated.
sl@0
  1530
		test.Printf(_L("No in use RAM zones found????\n"));
sl@0
  1531
		return KErrNotFound;
sl@0
  1532
		}
sl@0
  1533
sl@0
  1534
	if (totalMorePrefInUse > requiredMovDis)
sl@0
  1535
		{// There enough allocatable pages in the RAM zones below the currently 
sl@0
  1536
		// least preferable RAM in use.
sl@0
  1537
		test.Printf(_L("Memory is spread out totalMorePref 0x%x required 0x%x\n"), totalMorePrefInUse, requiredMovDis);
sl@0
  1538
		if (verifySpread)
sl@0
  1539
			return KErrGeneral;
sl@0
  1540
		}
sl@0
  1541
	return KErrNone;
sl@0
  1542
	}
sl@0
  1543
sl@0
  1544
sl@0
  1545
//
sl@0
  1546
// GetCandList1
sl@0
  1547
//
sl@0
  1548
// Populates a list of all zones that have the same preference and the same amount 
sl@0
  1549
// of immovable pages
sl@0
  1550
//
sl@0
  1551
void GetCandList1(TInt aIndex)
sl@0
  1552
	{
sl@0
  1553
	for (TUint i=0; i<gZoneCount; i++)
sl@0
  1554
		{
sl@0
  1555
		gCandList1[i] = KInvalidCandIndex;
sl@0
  1556
		}
sl@0
  1557
sl@0
  1558
	TInt candListIndex = 0;
sl@0
  1559
	GetAllPageInfo();
sl@0
  1560
	for (TUint i=0; i<gZoneCount; i++)
sl@0
  1561
		{
sl@0
  1562
		if (ZonesSamePref(aIndex, i))
sl@0
  1563
			{
sl@0
  1564
			gCandList1[candListIndex] = i;
sl@0
  1565
			candListIndex ++;
sl@0
  1566
			}
sl@0
  1567
		}
sl@0
  1568
	}
sl@0
  1569
sl@0
  1570
//
sl@0
  1571
// GetCandList2
sl@0
  1572
//
sl@0
  1573
// Populates a list of all zones that have the same preference and the same amount 
sl@0
  1574
// of immovable pages
sl@0
  1575
//
sl@0
  1576
void GetCandList2(TInt aIndex)
sl@0
  1577
	{
sl@0
  1578
	for (TUint i=0; i<gZoneCount; i++)
sl@0
  1579
		{
sl@0
  1580
		gCandList2[i] = KInvalidCandIndex;
sl@0
  1581
		}
sl@0
  1582
sl@0
  1583
	TInt candListIndex = 0;
sl@0
  1584
	GetAllPageInfo();
sl@0
  1585
	for (TUint i=0; i<gZoneCount; i++)
sl@0
  1586
		{
sl@0
  1587
		if (ZonesSamePref(aIndex, i))
sl@0
  1588
			{
sl@0
  1589
			gCandList2[candListIndex] = i;
sl@0
  1590
			candListIndex ++;
sl@0
  1591
			}
sl@0
  1592
		}
sl@0
  1593
	}
sl@0
  1594
sl@0
  1595
//
sl@0
  1596
// GetCandListFixed1
sl@0
  1597
//
sl@0
  1598
// Populates a list of all zones that have the same preference and the same 
sl@0
  1599
// amount of immovable pages, it will ignore RAM zones that are full of 
sl@0
  1600
// immovable pages as these are the only RAM zones that fixed pages can't be 
sl@0
  1601
// allocated into.
sl@0
  1602
//
sl@0
  1603
void GetCandListFixed1(TInt aIndex)
sl@0
  1604
	{
sl@0
  1605
	for (TUint i=0; i<gZoneCount; i++)
sl@0
  1606
		{
sl@0
  1607
		gCandList1[i] = KInvalidCandIndex;
sl@0
  1608
		}
sl@0
  1609
sl@0
  1610
	TInt candListIndex = 0;
sl@0
  1611
	GetAllPageInfo();
sl@0
  1612
	for (TUint i=0; i<gZoneCount; i++)
sl@0
  1613
		{
sl@0
  1614
		if (ZonesSamePref(aIndex, i) &&
sl@0
  1615
			gZoneUtilArray[i].iAllocFixed + gZoneUtilArray[i].iAllocUnknown !=
sl@0
  1616
			gZoneConfigArray[i].iPhysPages) 
sl@0
  1617
			{
sl@0
  1618
			gCandList1[candListIndex] = i;
sl@0
  1619
			candListIndex ++;
sl@0
  1620
			}
sl@0
  1621
		}
sl@0
  1622
	}
sl@0
  1623
sl@0
  1624
//
sl@0
  1625
// MultiGenDefragThreadFunc
sl@0
  1626
//
sl@0
  1627
// Called when a general defrag is called to run at the same time as another operation
sl@0
  1628
//
sl@0
  1629
TInt MultiGenDefragThreadFunc(TAny* /*aPtr*/)
sl@0
  1630
	{
sl@0
  1631
	RRamDefragFuncTestLdd Ldd2;
sl@0
  1632
	TInt r = Ldd2.Open();
sl@0
  1633
	if (r != KErrNone)
sl@0
  1634
		{
sl@0
  1635
		RDebug::Printf("Unable to open Ldd2 in MultiGenDefragThreadFunc, r = %d\n", r);
sl@0
  1636
		return r;
sl@0
  1637
		}
sl@0
  1638
sl@0
  1639
	RThread thisThread = RThread();
sl@0
  1640
	thisThread.SetPriority(EPriorityLess);
sl@0
  1641
	r = Ldd2.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  1642
	Ldd2.Close();
sl@0
  1643
	thisThread.Close();
sl@0
  1644
	return r;
sl@0
  1645
	}
sl@0
  1646
sl@0
  1647
TBool gTestLoop = EFalse;
sl@0
  1648
//
sl@0
  1649
// MultiLoopGenDefragThreadFunc
sl@0
  1650
//
sl@0
  1651
// Called when a general defrag is called to run at the same time as another operation
sl@0
  1652
//
sl@0
  1653
TInt MultiLoopGenDefragThreadFunc(TAny* /*aPtr*/)
sl@0
  1654
	{
sl@0
  1655
	RRamDefragFuncTestLdd Ldd2;
sl@0
  1656
	TInt r = Ldd2.Open();
sl@0
  1657
	if (r != KErrNone)
sl@0
  1658
		{
sl@0
  1659
		RDebug::Printf("Unable to open Ldd2 in MultiLoopGenDefragThreadFunc, r = %d\n", r);
sl@0
  1660
		return r;
sl@0
  1661
		}
sl@0
  1662
sl@0
  1663
	RThread thisThread = RThread();
sl@0
  1664
	thisThread.SetPriority(EPriorityLess);
sl@0
  1665
	while (gTestLoop)
sl@0
  1666
		{
sl@0
  1667
		r = Ldd2.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  1668
		if (r != KErrNone)
sl@0
  1669
			goto threadExit;
sl@0
  1670
		}
sl@0
  1671
threadExit:
sl@0
  1672
	Ldd2.Close();
sl@0
  1673
	thisThread.Close();
sl@0
  1674
	return r;
sl@0
  1675
	}
sl@0
  1676
sl@0
  1677
//
sl@0
  1678
// MultiEmptyZoneThreadFunc
sl@0
  1679
//
sl@0
  1680
// Called when a zone defrag is called to run at the same time as another operation
sl@0
  1681
//
sl@0
  1682
TInt MultiEmptyZoneThreadFunc(TAny* zoneID)
sl@0
  1683
	{
sl@0
  1684
	RRamDefragFuncTestLdd Ldd2;
sl@0
  1685
	TInt r = Ldd2.Open();
sl@0
  1686
	if (r != KErrNone)
sl@0
  1687
		{
sl@0
  1688
		RDebug::Printf("Unable to open Ldd2 in MultiEmptyZoneThreadFunc, r = %d\n", r);
sl@0
  1689
		return r;
sl@0
  1690
		}
sl@0
  1691
sl@0
  1692
	RThread thisThread = RThread();
sl@0
  1693
	thisThread.SetPriority(EPriorityLess);
sl@0
  1694
	r = Ldd2.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, (TUint)zoneID);
sl@0
  1695
	Ldd2.Close();
sl@0
  1696
	thisThread.Close();
sl@0
  1697
	return r;
sl@0
  1698
	}
sl@0
  1699
sl@0
  1700
sl@0
  1701
//
sl@0
  1702
// MultiClaimZoneThreadFunc
sl@0
  1703
//
sl@0
  1704
// Called when a ClaimRamZone is called to run at the same time as another operation
sl@0
  1705
//
sl@0
  1706
TInt MultiClaimZoneThreadFunc(TAny* zoneID)
sl@0
  1707
	{
sl@0
  1708
	RRamDefragFuncTestLdd Ldd2;
sl@0
  1709
	TInt r = Ldd2.Open();
sl@0
  1710
	if (r != KErrNone)
sl@0
  1711
		{
sl@0
  1712
		RDebug::Printf("Unable to open Ldd2 in MultiClaimZoneThreadFunc, r = %d\n", r);
sl@0
  1713
		return r;
sl@0
  1714
		}
sl@0
  1715
sl@0
  1716
	RThread thisThread = RThread();
sl@0
  1717
	thisThread.SetPriority(EPriorityLess);
sl@0
  1718
	r = Ldd2.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, (TUint)zoneID);
sl@0
  1719
	Ldd2.Close();
sl@0
  1720
	thisThread.Close();
sl@0
  1721
	return r;
sl@0
  1722
	}
sl@0
  1723
sl@0
  1724
sl@0
  1725
//
sl@0
  1726
// TestAllocStrategies
sl@0
  1727
//
sl@0
  1728
// Verifying that pages are allocated correctly.  All tests rely on a general defragmentation occuring
sl@0
  1729
// in TestStart() so that memory is in a defragmented state before the allocations.
sl@0
  1730
//
sl@0
  1731
//---------------------------------------------------------------------------------------------------------------------
sl@0
  1732
//! @SYMTestCaseID				KBASE-t_ramdefrag-0525
sl@0
  1733
//! @SYMTestCaseDesc			Verifying that pages are allocated correctly
sl@0
  1734
//! @SYMTestType				CIT
sl@0
  1735
//! @SYMPREQ					PREQ308
sl@0
  1736
//! @SYMTestPriority			High
sl@0
  1737
//! @SYMTestActions				
sl@0
  1738
//! 	1.	Allocate one chunk to memory. 
sl@0
  1739
//! 		Check which zone the chunk has been allocated to by checking the 
sl@0
  1740
//! 		number of movable pages before and after the allocation has taken place. 
sl@0
  1741
//!		2.	Allocate one chunk to memory that is large enough to cause an empty RAM 
sl@0
  1742
//!			zone to be used.
sl@0
  1743
//! 	3.	Allocate a certain number of fixed pages to memory. 
sl@0
  1744
//! 		Check which zone the chunk has been allocated to by checking the 
sl@0
  1745
//! 		number of fixed pages before and after the allocation has taken place. 
sl@0
  1746
//!		4.	Allocate fixed pages when it is known that discardable pages will need 
sl@0
  1747
//!			to be discarded for the allocation to succeed.
sl@0
  1748
//!		5.	Allocate fixed pages when it is known that movable pages will need 
sl@0
  1749
//!			to be moved for the allocation to succeed.
sl@0
  1750
//! 	6.	Allocate fixed pages when it is known that movable pages will need 
sl@0
  1751
//!			to be moved for the allocation to succeed, Determine the "best" zone for fixed pages and allocate 1 more
sl@0
  1752
//!			than that. 
sl@0
  1753
//!		7.	Allocate discardable pages by loading pages that are demand paged. 
sl@0
  1754
//! 		Check which zone the memory has been allocated to by checking the number of 
sl@0
  1755
//! 		discardable pages before and after the allocation has taken place. 
sl@0
  1756
//! 
sl@0
  1757
//! @SYMTestExpectedResults
sl@0
  1758
//! 	1.	The memory has been allocated to the most preferred zone with the least amount of 
sl@0
  1759
//! 		space accounting for the zone threshold for movable pages. 
sl@0
  1760
//!		2.	The new RAM zone is used and the movable page are allocated into it first then the other
sl@0
  1761
//!			more preferable RAM zones.
sl@0
  1762
//! 	3.	The fixed pages are allocated to the most preferred zone
sl@0
  1763
//!		4.	The fixed pages are allocatted to the most preferred zone with free, movable or discardable pages in it.
sl@0
  1764
//!		5.	The fixed pages are allocatted to the most preferred zone with free, movable or discardable pages in it.
sl@0
  1765
//! 	6.	Extra page is placed in the next preferable to the "best"zone 
sl@0
  1766
//!		7.	Memory is allocated to the most preferred zone with the least amount of 
sl@0
  1767
//! 		space accounting for the zone threshold for discardable pages. 
sl@0
  1768
//---------------------------------------------------------------------------------------------------------------------
sl@0
  1769
TInt TestAllocStrategies()
sl@0
  1770
	{
sl@0
  1771
	
sl@0
  1772
	test.Start(_L("Test1: Check which zone a movable page has been allocated to "));	
sl@0
  1773
	TestStart(); //This will perform a general defrag which should tidy up RAM for us
sl@0
  1774
	gChunkArray1 = new RChunk;
sl@0
  1775
	gChunkArraySize1 = 1;
sl@0
  1776
	TBool zoneFound = EFalse;
sl@0
  1777
sl@0
  1778
	// Determine if suitable RAM zone exists for testing
sl@0
  1779
	TInt best = GetBestZone(BEST_MOVABLE);
sl@0
  1780
	if (best == KErrNotFound)
sl@0
  1781
		{
sl@0
  1782
		test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
sl@0
  1783
		}
sl@0
  1784
	else
sl@0
  1785
		{
sl@0
  1786
		test.Printf(_L("best = %d\n"), best);
sl@0
  1787
		// Populate the candidate list
sl@0
  1788
		GetCandList2(best);
sl@0
  1789
		// Allocate 1 movable page
sl@0
  1790
		GetOriginalPageCount();
sl@0
  1791
		if (AllocMovable(gChunkArray1, gChunkArraySize1, 1, gPageSize) != KErrNone)
sl@0
  1792
			{
sl@0
  1793
			test.Printf(_L("Not enough free RAM for test - Skipping...\n"));
sl@0
  1794
			}
sl@0
  1795
		else
sl@0
  1796
			{
sl@0
  1797
			GetAllPageInfo();
sl@0
  1798
			TInt r = VerifyMovDisAlloc();
sl@0
  1799
			// Need to check all candidates to see if page has gone into any one of them
sl@0
  1800
			for (TUint i=0; i < gZoneCount; i++)
sl@0
  1801
				{
sl@0
  1802
				if (gCandList2[i] == KInvalidCandIndex)
sl@0
  1803
					{
sl@0
  1804
					break;
sl@0
  1805
					}
sl@0
  1806
				TUint zoneIndex = gCandList2[i];
sl@0
  1807
				if (gOriginalPageCountArray[zoneIndex].iAllocMovable < gZoneUtilArray[zoneIndex].iAllocMovable)
sl@0
  1808
					{
sl@0
  1809
					zoneFound = ETrue;
sl@0
  1810
					break;
sl@0
  1811
					}
sl@0
  1812
				}
sl@0
  1813
sl@0
  1814
			if (r == KErrNone && zoneFound)
sl@0
  1815
				{
sl@0
  1816
				test.Printf(_L("Pass: Movable allocated to the zone expected\n"));
sl@0
  1817
				}
sl@0
  1818
			else
sl@0
  1819
				{
sl@0
  1820
				test.Printf(_L("Fail: Movable allocated to a zone that was not expected, r %d\n"), r);
sl@0
  1821
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  1822
				TEST_FAIL;
sl@0
  1823
				}	
sl@0
  1824
			}
sl@0
  1825
		}
sl@0
  1826
	TestEnd();
sl@0
  1827
sl@0
  1828
	test.Next(_L("Test2: Check that movable pages allocated from least preferable RAM zone to be used\n"));
sl@0
  1829
	TestStart();
sl@0
  1830
	gChunkArray1 = new RChunk;
sl@0
  1831
	gChunkArraySize1 = 1;
sl@0
  1832
	// Determine if suitable RAM zone exists for testing.
sl@0
  1833
	TUint bestPrefIndex;
sl@0
  1834
	best = GetBestZone(BEST_MOVABLE, &bestPrefIndex); 
sl@0
  1835
	TUint morePrefIndex = bestPrefIndex - 1;
sl@0
  1836
	TUint nextLeastPrefIndex = bestPrefIndex + 1;
sl@0
  1837
	
sl@0
  1838
sl@0
  1839
	// Check number of free pages in the more preferable zones
sl@0
  1840
	TUint freeInMorePref = 0;
sl@0
  1841
	for (TUint i = 0; i < bestPrefIndex; i++)
sl@0
  1842
		{
sl@0
  1843
		freeInMorePref += gZoneUtilArray[gPrefArray[i]].iFreePages;
sl@0
  1844
		}
sl@0
  1845
	
sl@0
  1846
	test.Printf(_L("freeInMorePref = 0x%x\n"), freeInMorePref);
sl@0
  1847
	const TUint KHeapOverhead = 5; // Room for kernel heap allocations so they don't affect the page count
sl@0
  1848
	if (best == KErrNotFound || nextLeastPrefIndex >= gZoneCount ||
sl@0
  1849
		(gZoneUtilArray[gPrefArray[nextLeastPrefIndex]].iFreePages < KHeapOverhead && 
sl@0
  1850
		freeInMorePref < KHeapOverhead)||
sl@0
  1851
		gZoneConfigArray[gPrefArray[nextLeastPrefIndex]].iPref == gZoneConfigArray[best].iPref ||
sl@0
  1852
		gZoneConfigArray[gPrefArray[morePrefIndex]].iPref == gZoneConfigArray[best].iPref)
sl@0
  1853
		{// No less preferable RAM zone or there are more or less preferable of
sl@0
  1854
		// same preference so re-ordering potential makes verification too complex.
sl@0
  1855
		test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
sl@0
  1856
		}
sl@0
  1857
	else
sl@0
  1858
		{
sl@0
  1859
		// Ensure the zone is almost full as chunks will get allocated in blocks
sl@0
  1860
		// by almost filling the best zone with fixed pages, leaving a space (e.g. 20 pages)
sl@0
  1861
		const TUint KSpaceNeeded = 20;
sl@0
  1862
		if (gZoneUtilArray[best].iFreePages > KSpaceNeeded)
sl@0
  1863
			{
sl@0
  1864
			TUint zoneID = gZoneUtilArray[best].iZoneId;
sl@0
  1865
			TUint diffAlloc = gZoneUtilArray[best].iFreePages - KSpaceNeeded;
sl@0
  1866
			TInt r = Ldd.ZoneAllocDiscontiguous(zoneID, diffAlloc);
sl@0
  1867
			if (r != KErrNone)
sl@0
  1868
				{
sl@0
  1869
				CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  1870
				TEST_FAIL;
sl@0
  1871
				}
sl@0
  1872
			}
sl@0
  1873
		
sl@0
  1874
sl@0
  1875
		
sl@0
  1876
		GetAllPageInfo();
sl@0
  1877
		// Update the number of free pages in the more preferable zones
sl@0
  1878
		freeInMorePref = 0;
sl@0
  1879
		for (TUint i = 0; i < bestPrefIndex; i++)
sl@0
  1880
			{
sl@0
  1881
			freeInMorePref += gZoneUtilArray[gPrefArray[i]].iFreePages;
sl@0
  1882
			}
sl@0
  1883
		TUint origFixed = gTotalPageCount.iFixedPages;
sl@0
  1884
sl@0
  1885
		GetOriginalPageCount();
sl@0
  1886
sl@0
  1887
		// Allocate enough movable pages that the next least preferable RAM zone 
sl@0
  1888
		// will need to be used.
sl@0
  1889
		TUint movPages = gZoneUtilArray[best].iFreePages + 1;
sl@0
  1890
		TUint movBytes = movPages << gPageShift;
sl@0
  1891
		TInt r = AllocMovable(gChunkArray1, gChunkArraySize1, 1, movBytes);
sl@0
  1892
		if (r != KErrNone)
sl@0
  1893
			{
sl@0
  1894
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  1895
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  1896
			TEST_FAIL;
sl@0
  1897
			}
sl@0
  1898
sl@0
  1899
		GetAllPageInfo();
sl@0
  1900
		TUint curFixed = gTotalPageCount.iFixedPages;
sl@0
  1901
		TInt difFixed = curFixed - origFixed;	
sl@0
  1902
		
sl@0
  1903
		// If there is space in the more preferable zones then they should be allocated starting from
sl@0
  1904
		// the "best" zone to the more preferable zones, else from the next least preferable zone to the more preferable zones
sl@0
  1905
		TUint prefIndex;		
sl@0
  1906
		if ((TInt)freeInMorePref > difFixed)
sl@0
  1907
			{//No new zones should be turned on, allocation should have gone into bestPrefIndex then down towards most preferred zones
sl@0
  1908
			TUint nextIndex = gPrefArray[nextLeastPrefIndex];
sl@0
  1909
			if (gOriginalPageCountArray[best].iAllocMovable >= gZoneUtilArray[best].iAllocMovable ||
sl@0
  1910
				gOriginalPageCountArray[nextIndex].iAllocMovable < gZoneUtilArray[nextIndex].iAllocMovable)
sl@0
  1911
				{
sl@0
  1912
				test.Printf(_L("Movable page allocated incorrectly origFree 0x%x curFree 0x%x nxtPrefFree 0x%x\n"), 
sl@0
  1913
								gOriginalPageCountArray[best].iFreePages, gZoneUtilArray[best].iFreePages, 
sl@0
  1914
								gZoneUtilArray[nextLeastPrefIndex].iFreePages);
sl@0
  1915
				CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  1916
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  1917
				TEST_FAIL;
sl@0
  1918
				}
sl@0
  1919
			prefIndex = morePrefIndex;
sl@0
  1920
			}
sl@0
  1921
		else
sl@0
  1922
			{// If there are enough free pages in "nextIndex" to fit all movable pages allocated then the movable 
sl@0
  1923
			// page count in "best" should stay the same, else they should be allocated between "nextIndex" and "best".
sl@0
  1924
			TUint nextIndex = gPrefArray[nextLeastPrefIndex];
sl@0
  1925
			test.Printf(_L("nextIndex = %d\n"), nextIndex);
sl@0
  1926
			if (gOriginalPageCountArray[nextIndex].iAllocMovable >= gZoneUtilArray[nextIndex].iAllocMovable ||
sl@0
  1927
				(gOriginalPageCountArray[nextIndex].iFreePages >= movPages && 
sl@0
  1928
				gOriginalPageCountArray[best].iAllocMovable != gZoneUtilArray[best].iAllocMovable) ||
sl@0
  1929
				(gOriginalPageCountArray[nextIndex].iFreePages < movPages && 
sl@0
  1930
				gOriginalPageCountArray[best].iAllocMovable == gZoneUtilArray[best].iAllocMovable))
sl@0
  1931
				{
sl@0
  1932
				test.Printf(_L("Movable page allocated incorrectly origFree 0x%x curFree 0x%x nxtPrefFree 0x%x\n"), 
sl@0
  1933
								gOriginalPageCountArray[nextIndex].iFreePages, gZoneUtilArray[nextIndex].iFreePages, 
sl@0
  1934
								gZoneUtilArray[nextIndex].iFreePages);
sl@0
  1935
				CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  1936
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  1937
				TEST_FAIL;
sl@0
  1938
				}
sl@0
  1939
			prefIndex = bestPrefIndex;
sl@0
  1940
			}
sl@0
  1941
		
sl@0
  1942
		// Check that movable pages have only been allocated into the more 
sl@0
  1943
		// preferable RAM zones if the less preferable ram zones in use are full.
sl@0
  1944
		prefIndex++;
sl@0
  1945
		do
sl@0
  1946
			{
sl@0
  1947
			prefIndex--;
sl@0
  1948
			TUint indexCurrent = gPrefArray[prefIndex];			
sl@0
  1949
			TUint indexLessPref = gPrefArray[prefIndex+1];
sl@0
  1950
sl@0
  1951
			if (gOriginalPageCountArray[indexCurrent].iAllocMovable < gZoneUtilArray[indexCurrent].iAllocMovable &&
sl@0
  1952
				gZoneUtilArray[indexLessPref].iFreePages)
sl@0
  1953
				{// Current in use zone or less preferable than current has free pages so fail
sl@0
  1954
				test.Printf(_L("Movable page allocated incorrectly origFree 0x%x curFree 0x%x nxtPrefFree 0x%x\n"), 
sl@0
  1955
								gOriginalPageCountArray[best].iFreePages, gZoneUtilArray[best].iFreePages, 
sl@0
  1956
								gZoneUtilArray[nextLeastPrefIndex].iFreePages);
sl@0
  1957
				CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  1958
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  1959
				TEST_FAIL;
sl@0
  1960
				}
sl@0
  1961
			}
sl@0
  1962
		while(prefIndex);
sl@0
  1963
		test.Printf(_L("Pass: Pages allocated to the zone expected\n"));
sl@0
  1964
		}
sl@0
  1965
	TestEnd();
sl@0
  1966
sl@0
  1967
	test.Next(_L("Test3: Check which zone a fixed page has been allocated to "));	
sl@0
  1968
	for (TUint testStep = 0; testStep < 2; testStep++)
sl@0
  1969
		{
sl@0
  1970
		switch (testStep)
sl@0
  1971
			{
sl@0
  1972
			case 0:
sl@0
  1973
				test.Printf(_L("Testing discontiguous allocations\n"));
sl@0
  1974
				break;
sl@0
  1975
				
sl@0
  1976
			case 1:
sl@0
  1977
				test.Printf(_L("Testing contiguous allocations\n"));
sl@0
  1978
				break;
sl@0
  1979
			}
sl@0
  1980
		TestStart();
sl@0
  1981
		zoneFound = EFalse;
sl@0
  1982
		GetOriginalPageCount();
sl@0
  1983
		
sl@0
  1984
		best = GetBestZone(BEST_FIXED);
sl@0
  1985
		TESTDEBUG(test.Printf(_L("best = %d\n"), best));
sl@0
  1986
		if (best == KErrNotFound)
sl@0
  1987
			{
sl@0
  1988
			test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
sl@0
  1989
			}
sl@0
  1990
		else
sl@0
  1991
			{
sl@0
  1992
			GetCandList1(best);
sl@0
  1993
			TInt allocFixedPages = 1; // Try and allocate just 1 fixed page
sl@0
  1994
			switch (testStep)
sl@0
  1995
				{
sl@0
  1996
				case 0:
sl@0
  1997
					Ldd.AllocateFixed(allocFixedPages); 
sl@0
  1998
					break;
sl@0
  1999
					
sl@0
  2000
				case 1:
sl@0
  2001
					TUint allocFixedBytes = allocFixedPages << gPageShift;
sl@0
  2002
					Ldd.AllocContiguous(allocFixedBytes); 
sl@0
  2003
					break;
sl@0
  2004
				}
sl@0
  2005
			
sl@0
  2006
			
sl@0
  2007
			GetAllPageInfo();
sl@0
  2008
			for (TUint i=0; i<gZoneCount; i++)
sl@0
  2009
				{
sl@0
  2010
				if (gCandList1[i] == KInvalidCandIndex)
sl@0
  2011
					{
sl@0
  2012
					break;
sl@0
  2013
					}
sl@0
  2014
				TUint zoneIndex = gCandList1[i];
sl@0
  2015
				if (gOriginalPageCountArray[zoneIndex].iAllocFixed + allocFixedPages <= gZoneUtilArray[zoneIndex].iAllocFixed)
sl@0
  2016
					{
sl@0
  2017
					zoneFound = ETrue;
sl@0
  2018
					break;
sl@0
  2019
					}
sl@0
  2020
				}
sl@0
  2021
sl@0
  2022
			if (zoneFound)
sl@0
  2023
				{
sl@0
  2024
				test.Printf(_L("Pass: Chunk has been allocated to the zone expected\n"));
sl@0
  2025
				}
sl@0
  2026
			else
sl@0
  2027
				{
sl@0
  2028
				test.Printf(_L("Fail: Fixed been allocated to a zone that was not expected\n"));
sl@0
  2029
				CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  2030
				TEST_FAIL;
sl@0
  2031
				}	
sl@0
  2032
			
sl@0
  2033
			Ldd.FreeAllFixedPages();
sl@0
  2034
			}
sl@0
  2035
		TestEnd();
sl@0
  2036
		}
sl@0
  2037
sl@0
  2038
	test.Next(_L("Test4: Check fixed page allocations will discard pages"));
sl@0
  2039
	for (TUint testStep = 0; testStep < 2; testStep++)
sl@0
  2040
		{
sl@0
  2041
		switch (testStep)
sl@0
  2042
			{
sl@0
  2043
			case 0:
sl@0
  2044
				test.Printf(_L("Testing discontiguous allocations\n"));
sl@0
  2045
				break;
sl@0
  2046
				
sl@0
  2047
			case 1:
sl@0
  2048
				test.Printf(_L("Testing contiguous allocations\n"));
sl@0
  2049
				break;
sl@0
  2050
			}
sl@0
  2051
		TestStart();
sl@0
  2052
		TInt discardBytes;
sl@0
  2053
		TInt r;
sl@0
  2054
sl@0
  2055
		if (!gPagedRom)
sl@0
  2056
			{
sl@0
  2057
			test.Printf(_L("Not a paged ROM - Skipping test step\n"));
sl@0
  2058
			goto SkipTest4;
sl@0
  2059
			}
sl@0
  2060
sl@0
  2061
		best = GetBestZone(BEST_FIXED);
sl@0
  2062
		TESTDEBUG(test.Printf(_L("best = %d\n"), best));
sl@0
  2063
		if (best == KErrNotFound)
sl@0
  2064
			{
sl@0
  2065
			test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
sl@0
  2066
			goto SkipTest4;
sl@0
  2067
			}
sl@0
  2068
		// Ensure discardable pages in the preferred RAM zone.
sl@0
  2069
		r = AllocDiscardable(discardBytes);
sl@0
  2070
		if (r != KErrNone)
sl@0
  2071
			{
sl@0
  2072
			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  2073
			TEST_FAIL;
sl@0
  2074
			}
sl@0
  2075
		if (discardBytes < gPageSize)
sl@0
  2076
			{// Can't discard any pages so test can't run.
sl@0
  2077
			test.Printf(_L("Memory too full to perform test - Skipping...\n"));
sl@0
  2078
			goto SkipTest4;
sl@0
  2079
			}
sl@0
  2080
sl@0
  2081
		// Make sure all RAM zones that the fixed page allocation could potentially 
sl@0
  2082
		// go to have discardable pages allocated in it.
sl@0
  2083
		GetOriginalPageCount();
sl@0
  2084
		GetCandListFixed1(best);
sl@0
  2085
		for (TUint i = 0; i < gZoneCount; i++)
sl@0
  2086
			{
sl@0
  2087
			if (gCandList1[i] == KInvalidCandIndex)
sl@0
  2088
				{
sl@0
  2089
				break;
sl@0
  2090
				}
sl@0
  2091
			TUint zoneIndex = gCandList1[i];
sl@0
  2092
			if (gOriginalPageCountArray[zoneIndex].iAllocDiscardable == 0)
sl@0
  2093
				{
sl@0
  2094
				test.Printf(_L("No dicardable pages in one of the candidates RAM zones - Skipping...\n"));
sl@0
  2095
				goto SkipTest4;
sl@0
  2096
				}
sl@0
  2097
			if (gOriginalPageCountArray[zoneIndex].iFreePages != 0)
sl@0
  2098
				{
sl@0
  2099
				test.Printf(_L("Some free pages in candidate RAM zone ID%x\n"), 
sl@0
  2100
							gZoneConfigArray[zoneIndex].iZoneId);
sl@0
  2101
				CLEANUP(ResetDPCache());
sl@0
  2102
				TEST_FAIL;
sl@0
  2103
				}
sl@0
  2104
			}
sl@0
  2105
sl@0
  2106
		// Allocate 1 fixed page and ensure that it discards a page.
sl@0
  2107
		switch (testStep)
sl@0
  2108
			{
sl@0
  2109
			case 0:
sl@0
  2110
				r = Ldd.AllocateFixed(1); 
sl@0
  2111
				break;
sl@0
  2112
				
sl@0
  2113
			case 1:
sl@0
  2114
				r = Ldd.AllocContiguous(gPageSize); 
sl@0
  2115
				break;
sl@0
  2116
			}
sl@0
  2117
sl@0
  2118
		// Find RAM zone the fixed page was allocated into.
sl@0
  2119
		GetAllPageInfo();
sl@0
  2120
		zoneFound = EFalse;
sl@0
  2121
		for (TUint i = 0; i < gZoneCount; i++)
sl@0
  2122
			{
sl@0
  2123
			if (gCandList1[i] == KInvalidCandIndex)
sl@0
  2124
				{
sl@0
  2125
				break;
sl@0
  2126
				}
sl@0
  2127
			TUint zoneIndex = gCandList1[i];
sl@0
  2128
			if (gOriginalPageCountArray[zoneIndex].iAllocFixed < gZoneUtilArray[zoneIndex].iAllocFixed)
sl@0
  2129
				{
sl@0
  2130
				zoneFound = ETrue;
sl@0
  2131
				if (gOriginalPageCountArray[zoneIndex].iAllocDiscardable <= gZoneUtilArray[zoneIndex].iAllocDiscardable &&
sl@0
  2132
					gOriginalPageCountArray[zoneIndex].iAllocMovable <= gZoneUtilArray[zoneIndex].iAllocMovable)
sl@0
  2133
					{
sl@0
  2134
					test.Printf(_L("Fixed pages allocated but no pages discarded in RAM zone ID 0x%x\n"), 
sl@0
  2135
						gZoneConfigArray[zoneIndex].iZoneId);
sl@0
  2136
					CLEANUP(ResetDPCache());
sl@0
  2137
					TEST_FAIL;
sl@0
  2138
					}
sl@0
  2139
				}
sl@0
  2140
			}
sl@0
  2141
sl@0
  2142
		if (!zoneFound || r != KErrNone)
sl@0
  2143
			{
sl@0
  2144
			test.Printf(_L("No fixed pages were allocated r = %d\n"), r);
sl@0
  2145
			CLEANUP(ResetDPCache());
sl@0
  2146
			TEST_FAIL;
sl@0
  2147
			}
sl@0
  2148
		else
sl@0
  2149
			{
sl@0
  2150
			test.Printf(_L("Pass: Pages been allocated to the zone expected\n"));
sl@0
  2151
			}
sl@0
  2152
	SkipTest4 :
sl@0
  2153
		// This will free any DP cache pages and fixed pages allocated.
sl@0
  2154
		TestEnd();
sl@0
  2155
		}
sl@0
  2156
sl@0
  2157
sl@0
  2158
	for (TUint testIndex = 0; testIndex < 2; testIndex++)
sl@0
  2159
		{
sl@0
  2160
		switch (testIndex)
sl@0
  2161
			{
sl@0
  2162
			case 0:
sl@0
  2163
				test.Next(_L("Test5: Check fixed page allocations (discontiguous) will move pages"));
sl@0
  2164
				break;
sl@0
  2165
sl@0
  2166
			case 1:
sl@0
  2167
				test.Next(_L("Test6: Check fixed page will only go into a new zone if all other fix page zones are full of fix"));
sl@0
  2168
				break;
sl@0
  2169
			}
sl@0
  2170
sl@0
  2171
		TestStart();
sl@0
  2172
		gChunkArray1 = new RChunk;
sl@0
  2173
		gChunkArraySize1 = 1;
sl@0
  2174
		TInt r = KErrNone;
sl@0
  2175
		TUint freeInOtherZones = 0;
sl@0
  2176
		TUint allocatablePages;
sl@0
  2177
		TUint allocImmovPages;
sl@0
  2178
		TUint bestPrefIndex;
sl@0
  2179
		TUint nextBestIndex = 0;
sl@0
  2180
		const TUint KMovAllocOverhead = 5;	// need pages for page tables and kernel heap expansion.
sl@0
  2181
		
sl@0
  2182
		best = GetBestZone(BEST_FIXED, &bestPrefIndex);
sl@0
  2183
		TESTDEBUG(test.Printf(_L("best = %d\n"), best));
sl@0
  2184
		GetCandListFixed1(best);
sl@0
  2185
		TUint candidates = 0;
sl@0
  2186
		for (TUint i = 0; i < gZoneCount; i++)
sl@0
  2187
			{
sl@0
  2188
			if (gCandList1[i] == KInvalidCandIndex)
sl@0
  2189
				{
sl@0
  2190
				break;
sl@0
  2191
				}
sl@0
  2192
			candidates++;
sl@0
  2193
			}
sl@0
  2194
		if (best == KErrNotFound || 
sl@0
  2195
			(gZoneUtilArray[best].iAllocMovable == 0 && gZoneUtilArray[best].iFreePages < KMovAllocOverhead) ||
sl@0
  2196
			candidates != 1)
sl@0
  2197
			{
sl@0
  2198
			test.Printf(_L("Cannot find zone or too many equal pref zones to perform test - Skipping...\n"));
sl@0
  2199
			goto SkipTest5;
sl@0
  2200
			}
sl@0
  2201
sl@0
  2202
		if (testIndex == 1)
sl@0
  2203
			{// need to work out what the next best zone would be
sl@0
  2204
			GetPrefOrder();
sl@0
  2205
			if (bestPrefIndex + 1 >= gZoneCount)
sl@0
  2206
				{
sl@0
  2207
				test.Printf(_L("Cannot find next best zone - Skipping...\n"));
sl@0
  2208
				goto SkipTest5;
sl@0
  2209
				}
sl@0
  2210
			nextBestIndex = gPrefArray[bestPrefIndex + 1];
sl@0
  2211
			test.Printf(_L("nextBestIndex= %d\n"), nextBestIndex);
sl@0
  2212
			GetCandListFixed1(nextBestIndex);
sl@0
  2213
			candidates = 0;
sl@0
  2214
			for (TUint i = 0; i < gZoneCount; i++)
sl@0
  2215
				{
sl@0
  2216
				if (gCandList1[i] == KInvalidCandIndex)
sl@0
  2217
					{
sl@0
  2218
					break;
sl@0
  2219
					}
sl@0
  2220
				candidates++;
sl@0
  2221
				}
sl@0
  2222
			if (gZoneUtilArray[nextBestIndex].iPhysPages == gZoneUtilArray[nextBestIndex].iAllocFixed + 
sl@0
  2223
															gZoneUtilArray[nextBestIndex].iAllocUnknown ||
sl@0
  2224
				candidates != 1)
sl@0
  2225
				{
sl@0
  2226
				test.Printf(_L("Cannot find zone or too many equal pref zones to perform test - Skipping...\n"));
sl@0
  2227
				goto SkipTest5;
sl@0
  2228
				}
sl@0
  2229
			}
sl@0
  2230
sl@0
  2231
sl@0
  2232
sl@0
  2233
		for (TUint i = 0; i < gZoneCount; i++)
sl@0
  2234
			{
sl@0
  2235
			if (i != (TUint)best)
sl@0
  2236
				{
sl@0
  2237
				freeInOtherZones += gZoneUtilArray[i].iFreePages;
sl@0
  2238
				}
sl@0
  2239
			}
sl@0
  2240
		allocatablePages = 	gZoneUtilArray[best].iFreePages + 
sl@0
  2241
							gZoneUtilArray[best].iAllocMovable + 
sl@0
  2242
							gZoneUtilArray[best].iAllocDiscardable;
sl@0
  2243
		
sl@0
  2244
		if (allocatablePages > freeInOtherZones)
sl@0
  2245
			{
sl@0
  2246
			test.Printf(_L("Not enough free RAM for test - Skipping...\n"));
sl@0
  2247
			goto SkipTest5;
sl@0
  2248
			}
sl@0
  2249
		
sl@0
  2250
		// Allocate the fixed array before getting any page counts
sl@0
  2251
		r = Ldd.AllocFixedArray(allocatablePages + 1);
sl@0
  2252
		if (r != KErrNone)
sl@0
  2253
			{
sl@0
  2254
			test.Printf(_L("Failed to allocate fixed array r = %d - Skipping...\n"), r);
sl@0
  2255
			goto SkipTest5;
sl@0
  2256
			}
sl@0
  2257
		
sl@0
  2258
		// Fill the RAM zone with movable pages if none already in it.
sl@0
  2259
		GetAllPageInfo();
sl@0
  2260
		if (gZoneUtilArray[best].iAllocMovable == 0)
sl@0
  2261
			{
sl@0
  2262
			if (!gZoneUtilArray[best].iFreePages)
sl@0
  2263
				{
sl@0
  2264
				test.Printf(_L("RAM zone ID %x too full for test - Skipping...\n"), gZoneConfigArray[best].iZoneId);
sl@0
  2265
				goto SkipTest5;
sl@0
  2266
				}
sl@0
  2267
			// Fill the zone with movable pages
sl@0
  2268
			r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, best);
sl@0
  2269
			if (r != KErrNone)
sl@0
  2270
				{
sl@0
  2271
				test.Printf(_L("Failed to fill zone index %d with movable r = %d\n"), best, r);
sl@0
  2272
				CLEANUP(ResetRamZoneFlags());
sl@0
  2273
				TEST_FAIL;
sl@0
  2274
				}
sl@0
  2275
			
sl@0
  2276
			if (GetBestZone(BEST_FIXED) != best)
sl@0
  2277
				{
sl@0
  2278
				test.Printf(_L("Unable to complete test; RAM zone reordering - Skipping...\n"));
sl@0
  2279
				goto SkipTest5;
sl@0
  2280
				}
sl@0
  2281
			}	
sl@0
  2282
		// Allocate fixed pages after reseting the allocation flags.
sl@0
  2283
		GetAllPageInfo();
sl@0
  2284
		ResetRamZoneFlags();
sl@0
  2285
		allocatablePages = 	gZoneUtilArray[best].iFreePages + 
sl@0
  2286
							gZoneUtilArray[best].iAllocMovable + 
sl@0
  2287
							gZoneUtilArray[best].iAllocDiscardable;
sl@0
  2288
		switch (testIndex)
sl@0
  2289
			{
sl@0
  2290
			case 0:
sl@0
  2291
				r = Ldd.AllocateFixed2(allocatablePages);
sl@0
  2292
				GetAllPageInfo();
sl@0
  2293
				allocImmovPages = gZoneUtilArray[best].iAllocFixed + gZoneUtilArray[best].iAllocUnknown;
sl@0
  2294
				if (r != KErrNone || gZoneConfigArray[best].iPhysPages != allocImmovPages)
sl@0
  2295
					{
sl@0
  2296
					test.Printf(_L("RAM zone ID 0x%x not full of immovable pages\n"), gZoneConfigArray[best].iZoneId);
sl@0
  2297
					CLEANUP(ResetRamZoneFlags());
sl@0
  2298
					TEST_FAIL;
sl@0
  2299
					}
sl@0
  2300
				else
sl@0
  2301
					{
sl@0
  2302
					test.Printf(_L("Pass: Pages allocated to the zone expected\n"));
sl@0
  2303
					}
sl@0
  2304
				break;
sl@0
  2305
sl@0
  2306
			case 1:
sl@0
  2307
				GetOriginalPageCount();
sl@0
  2308
				r = Ldd.AllocateFixed2(allocatablePages + 1);
sl@0
  2309
				GetAllPageInfo();
sl@0
  2310
				allocImmovPages = gZoneUtilArray[best].iAllocFixed + gZoneUtilArray[best].iAllocUnknown;
sl@0
  2311
				if (r != KErrNone || gZoneUtilArray[best].iPhysPages != allocImmovPages ||
sl@0
  2312
					gZoneUtilArray[nextBestIndex].iAllocFixed <= gOriginalPageCountArray[nextBestIndex].iAllocFixed)
sl@0
  2313
					{
sl@0
  2314
					test.Printf(_L("RAM zone ID 0x%x not full of immovable pages or next best ID 0x%x no fix pages\n"), 
sl@0
  2315
									gZoneConfigArray[best].iZoneId, gZoneConfigArray[nextBestIndex].iZoneId);
sl@0
  2316
					test.Printf(_L("nextBest %d origFix 0x%x curFix 0x%x\n"), 
sl@0
  2317
									nextBestIndex, gOriginalPageCountArray[nextBestIndex].iAllocFixed, 
sl@0
  2318
									gZoneUtilArray[nextBestIndex].iAllocFixed);
sl@0
  2319
					CLEANUP(ResetRamZoneFlags());
sl@0
  2320
					TEST_FAIL;
sl@0
  2321
					}
sl@0
  2322
				// Go through every other zone and check that fixed pages haven't increased
sl@0
  2323
				for (TUint i = 0; i < gZoneCount; i++)
sl@0
  2324
					{
sl@0
  2325
					if (i != (TUint)best && i != (TUint)nextBestIndex &&
sl@0
  2326
						gZoneUtilArray[i].iAllocFixed > gOriginalPageCountArray[i].iAllocFixed)
sl@0
  2327
						{
sl@0
  2328
						test.Printf(_L("FAIL: Fix page count increased zoneIndex %d orig 0x%x current 0x%x\n"), 
sl@0
  2329
										i, gOriginalPageCountArray[i].iAllocFixed, gZoneUtilArray[i].iAllocFixed);
sl@0
  2330
						CLEANUP(ResetRamZoneFlags());
sl@0
  2331
						TEST_FAIL;
sl@0
  2332
						}
sl@0
  2333
					}
sl@0
  2334
sl@0
  2335
				test.Printf(_L("Pass: Pages allocated to the zone expected\n"));
sl@0
  2336
				break;
sl@0
  2337
			}
sl@0
  2338
	SkipTest5 :
sl@0
  2339
		// This will perform any required clean up.
sl@0
  2340
		ResetRamZoneFlags();
sl@0
  2341
		TestEnd();
sl@0
  2342
	}
sl@0
  2343
sl@0
  2344
	test.Next(_L("Test7: Check which zone a discardable page has been allocated to"));	
sl@0
  2345
	TestStart();
sl@0
  2346
	if (gPagedRom)
sl@0
  2347
		{
sl@0
  2348
		GetAllPageInfo();
sl@0
  2349
sl@0
  2350
		// Try to allocate just one more discardable page
sl@0
  2351
		TUint allocBytes = (gTotalPageCount.iDiscardablePages + 1) << gPageShift;
sl@0
  2352
		TInt discardablePages;
sl@0
  2353
sl@0
  2354
		best = GetBestZone(BEST_DISCARDABLE);
sl@0
  2355
		if (best == KErrNotFound)
sl@0
  2356
			{
sl@0
  2357
			test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
sl@0
  2358
			}
sl@0
  2359
		else
sl@0
  2360
			{
sl@0
  2361
			// Populate the candidate list
sl@0
  2362
			GetCandList2(best);
sl@0
  2363
			zoneFound = EFalse;
sl@0
  2364
			GetOriginalPageCount();
sl@0
  2365
			TInt r = AllocDiscardable(discardablePages, allocBytes);
sl@0
  2366
			if (r != KErrNone)
sl@0
  2367
				{
sl@0
  2368
				test.Printf(_L("Fail: r %d\n"), r);
sl@0
  2369
				CLEANUP(ResetDPCache());
sl@0
  2370
				TEST_FAIL;
sl@0
  2371
				}
sl@0
  2372
sl@0
  2373
			r = VerifyMovDisAlloc();
sl@0
  2374
			// Need to check all candidates to see if page has gone into any one of them
sl@0
  2375
			for (TUint i=0; i < gZoneCount; i++)
sl@0
  2376
				{
sl@0
  2377
				if (gCandList2[i] == KInvalidCandIndex)
sl@0
  2378
					{
sl@0
  2379
					break;
sl@0
  2380
					}
sl@0
  2381
				TUint zoneIndex = gCandList2[i];
sl@0
  2382
				if (gOriginalPageCountArray[zoneIndex].iAllocDiscardable < gZoneUtilArray[zoneIndex].iAllocDiscardable)
sl@0
  2383
					{
sl@0
  2384
					zoneFound = ETrue;
sl@0
  2385
					break;
sl@0
  2386
					}
sl@0
  2387
				}
sl@0
  2388
sl@0
  2389
			if (r == KErrNone && zoneFound)
sl@0
  2390
				{
sl@0
  2391
				test.Printf(_L("Pass: Discardable allocated to the zone expected\n"));
sl@0
  2392
				}
sl@0
  2393
			else
sl@0
  2394
				{
sl@0
  2395
				test.Printf(_L("Fail: Discardable been allocated to a zone that was not expected r %d\n"), r);
sl@0
  2396
				CLEANUP(ResetDPCache());
sl@0
  2397
				TEST_FAIL;
sl@0
  2398
				}	
sl@0
  2399
			}
sl@0
  2400
		}
sl@0
  2401
	else
sl@0
  2402
		{
sl@0
  2403
		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
sl@0
  2404
		}
sl@0
  2405
	TestEnd();
sl@0
  2406
sl@0
  2407
	test.Next(_L("Test8: Check fixed page allocations (contiguous) will move pages"));
sl@0
  2408
	TestStart();
sl@0
  2409
	gChunkArray1 = new RChunk;
sl@0
  2410
	gChunkArraySize1 = 1;
sl@0
  2411
	
sl@0
  2412
	TInt r = KErrNone;
sl@0
  2413
	TUint testZoneIndex = 0;
sl@0
  2414
sl@0
  2415
	// Get the most pref zone which is completely free to use as a test zone
sl@0
  2416
	r = FindMostPrefEmpty(testZoneIndex);
sl@0
  2417
	if (r != KErrNone)
sl@0
  2418
		{
sl@0
  2419
		test.Printf(_L("Cannot find empty zone - Skipping...\n"));
sl@0
  2420
		goto skipTest8;
sl@0
  2421
		}
sl@0
  2422
sl@0
  2423
		
sl@0
  2424
	// fill the test zone with movable pages
sl@0
  2425
	r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, testZoneIndex);
sl@0
  2426
	GetAllPageInfo();
sl@0
  2427
	if (r != KErrNone ||
sl@0
  2428
		gZoneUtilArray[testZoneIndex].iAllocMovable != gZoneUtilArray[testZoneIndex].iPhysPages)
sl@0
  2429
		{
sl@0
  2430
		test.Printf(_L("Failed to allocate movable r = %d - Skipping...\n"), r);
sl@0
  2431
		goto skipTest8;
sl@0
  2432
		}
sl@0
  2433
sl@0
  2434
	if (gTotalPageCount.iFreePages < 1)
sl@0
  2435
		{
sl@0
  2436
		test.Printf(_L("Insufficient memory - totalFreePages = 0x%x - Skipping...\n"), gTotalPageCount.iFreePages);
sl@0
  2437
		goto skipTest8;
sl@0
  2438
		}
sl@0
  2439
sl@0
  2440
	for (TUint zoneIndex = 0; zoneIndex < gZoneCount; zoneIndex++)
sl@0
  2441
		{
sl@0
  2442
		TUint zoneId = gZoneConfigArray[zoneIndex].iZoneId;
sl@0
  2443
		if (zoneIndex != testZoneIndex)
sl@0
  2444
			{
sl@0
  2445
			r = Ldd.SetZoneFlag(zoneId, gZoneConfigArray[zoneIndex].iFlags, NO_FIXED_FLAG);
sl@0
  2446
			if (r != KErrNone)
sl@0
  2447
				{
sl@0
  2448
				test.Printf(_L("Failed to set flag on zone index %d r = %d- Skipping...\n"), zoneIndex, r);
sl@0
  2449
				goto skipTest8;
sl@0
  2450
				}
sl@0
  2451
			}
sl@0
  2452
		}
sl@0
  2453
	
sl@0
  2454
	//attempt to alloc 1 contiguous fixed page
sl@0
  2455
	r = Ldd.AllocContiguous(gPageSize);
sl@0
  2456
	GetAllPageInfo();
sl@0
  2457
	if (r != KErrNone ||
sl@0
  2458
		!gZoneUtilArray[testZoneIndex].iAllocFixed)
sl@0
  2459
		{
sl@0
  2460
		test.Printf(_L("FAIL: no fixed pages in testZoneIndex(%d) r = %d\n"), testZoneIndex, r);
sl@0
  2461
		CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  2462
		TEST_FAIL;
sl@0
  2463
		}
sl@0
  2464
	else
sl@0
  2465
		{
sl@0
  2466
		test.Printf(_L("Passed...\n"));
sl@0
  2467
		}
sl@0
  2468
	
sl@0
  2469
skipTest8:
sl@0
  2470
	TestEnd();
sl@0
  2471
sl@0
  2472
	test.End();
sl@0
  2473
	return KErrNone;
sl@0
  2474
	}
sl@0
  2475
sl@0
  2476
	
sl@0
  2477
//
sl@0
  2478
// TestMovingPages
sl@0
  2479
//
sl@0
  2480
//---------------------------------------------------------------------------------------------------------------------
sl@0
  2481
//! @SYMTestCaseID				KBASE-t_ramdefrag-0526
sl@0
  2482
//! @SYMTestType				CIT
sl@0
  2483
//! @SYMTestCaseDesc			Verifying that pages are moved correctly
sl@0
  2484
//! @SYMPREQ					PREQ308
sl@0
  2485
//! @SYMTestPriority			High
sl@0
  2486
//! @SYMTestActions				
sl@0
  2487
//! 	1.	Fragment the memory. Write to all chunks apart from chunk 9. 
sl@0
  2488
//! 		Following this start a RAM defrag and whilst defrag is running write to chunk 9. 
sl@0
  2489
//! 	2.	Fragment the memory. Start a RAM defrag and whilst defrag is running 
sl@0
  2490
//! 		continuously write different values to the chunks. 
sl@0
  2491
//! 	3.	Fragment the memory. Following this start a RAM defrag and whilst this 
sl@0
  2492
//! 		is happening, continuously read from all chunks. 
sl@0
  2493
//! 	4.	Allocate some movable pages. Call a device driver that will allocate fixed 
sl@0
  2494
//! 		pages and write values to these fixed pages. 
sl@0
  2495
//! 		Close every other chunk so that only 5 chunks are now still open. 
sl@0
  2496
//! 		Following this perform a RAM defrag. 
sl@0
  2497
//! 		Read from the fixed pages that were originally written to, ensuring 
sl@0
  2498
//! 		that they have not changed.   
sl@0
  2499
//! 	5.	Without starting any processes, allocate discardable pages by loading 
sl@0
  2500
//! 		pages that are demand paged. 
sl@0
  2501
//! 		Read each of the constants from beginning to end. 
sl@0
  2502
//! 		Following this perform a RAM defrag. 
sl@0
  2503
//! 
sl@0
  2504
//! @SYMTestExpectedResults
sl@0
  2505
//! 	1.	Zones are emptied
sl@0
  2506
//! 	2.	Zones are emptied	
sl@0
  2507
//! 	3.	Zones are emptied
sl@0
  2508
//! 	4.	The values written to the fixed pages have not changed. 
sl@0
  2509
//! 	5.	Zones are emptied
sl@0
  2510
//---------------------------------------------------------------------------------------------------------------------
sl@0
  2511
TInt TestMovingPages()
sl@0
  2512
	{
sl@0
  2513
	const TInt KAllChunks = -1;  // Specifies that all chunks should be written to
sl@0
  2514
	
sl@0
  2515
	test.Start(_L("Test1: Whilst moving page, change the usage "));	
sl@0
  2516
	TestStart();	
sl@0
  2517
	
sl@0
  2518
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks, KChunkDefaultSize);
sl@0
  2519
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  2520
sl@0
  2521
	// Find a chunk that exists
sl@0
  2522
	TInt chunkIndex = gChunkArraySize1 - 1;
sl@0
  2523
	for (; chunkIndex >= 0 && gChunkArray1[chunkIndex].Handle() == NULL; chunkIndex--);
sl@0
  2524
	if (chunkIndex < 0)
sl@0
  2525
		{
sl@0
  2526
		test.Printf(_L("No chunks were allocated\n"));
sl@0
  2527
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  2528
		TEST_FAIL;
sl@0
  2529
		}
sl@0
  2530
sl@0
  2531
	WriteToChunk(gChunkArray1, gChunkArraySize1, chunkIndex);
sl@0
  2532
sl@0
  2533
	TInt r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
sl@0
  2534
	if (r != KErrNone)
sl@0
  2535
		{
sl@0
  2536
		test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  2537
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  2538
		TEST_FAIL;
sl@0
  2539
		}	
sl@0
  2540
	gTestThread.Logon(status);
sl@0
  2541
	gTestThread.Resume();	
sl@0
  2542
sl@0
  2543
	TUint8* base = gChunkArray1[chunkIndex].Base();
sl@0
  2544
	while (status.Int() == KRequestPending)
sl@0
  2545
		{
sl@0
  2546
		User::After(10000);
sl@0
  2547
		for (TInt8 k = 0; k < 10; k ++)
sl@0
  2548
			{
sl@0
  2549
			if (base == gChunkArray1[chunkIndex].Base() + gChunkArray1[chunkIndex].Size())
sl@0
  2550
				{
sl@0
  2551
				base = gChunkArray1[chunkIndex].Base();
sl@0
  2552
				}
sl@0
  2553
			*base++ = k; // write 0 - 9 to the chunk
sl@0
  2554
			}
sl@0
  2555
		}
sl@0
  2556
	
sl@0
  2557
	User::WaitForRequest(status);
sl@0
  2558
	r = status.Int();
sl@0
  2559
	TESTDEBUG(test.Printf(_L("defrag running on another thread returns %d\n"), r));
sl@0
  2560
sl@0
  2561
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  2562
sl@0
  2563
	if (r == KErrNone)
sl@0
  2564
		{
sl@0
  2565
		test.Printf(_L("Correct return value\n"));
sl@0
  2566
		test.Printf(_L("Passed...\n"));
sl@0
  2567
		}
sl@0
  2568
	else
sl@0
  2569
		{
sl@0
  2570
		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
sl@0
  2571
		TEST_FAIL;	
sl@0
  2572
		}
sl@0
  2573
		
sl@0
  2574
	gTestThread.Close();
sl@0
  2575
	TestEnd();
sl@0
  2576
	
sl@0
  2577
	
sl@0
  2578
	test.Next(_L("Test2: Whilst moving page, change the contents of the page"));	
sl@0
  2579
	TestStart();	
sl@0
  2580
		
sl@0
  2581
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks, KChunkDefaultSize);
sl@0
  2582
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  2583
	WriteToChunk(gChunkArray1, gChunkArraySize1, KAllChunks);
sl@0
  2584
	
sl@0
  2585
	r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
sl@0
  2586
	if (r != KErrNone)
sl@0
  2587
		{
sl@0
  2588
		test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  2589
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  2590
		TEST_FAIL;
sl@0
  2591
		}	
sl@0
  2592
	gTestThread.Logon(status);
sl@0
  2593
	gTestThread.Resume();
sl@0
  2594
sl@0
  2595
	TUint8 startValue = 0;
sl@0
  2596
	while (status.Int() == KRequestPending)
sl@0
  2597
		{
sl@0
  2598
		User::After(10000);
sl@0
  2599
		WriteToChunk(gChunkArray1, gChunkArraySize1, KAllChunks, startValue);
sl@0
  2600
		if (++startValue > 245)
sl@0
  2601
			{
sl@0
  2602
			startValue = 0;
sl@0
  2603
			}
sl@0
  2604
		}
sl@0
  2605
		
sl@0
  2606
	User::WaitForRequest(status);
sl@0
  2607
	r = status.Int();
sl@0
  2608
	TESTDEBUG(test.Printf(_L("defrag running on another thread returns %d\n"), r));
sl@0
  2609
	
sl@0
  2610
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  2611
sl@0
  2612
	if (r == KErrNone)
sl@0
  2613
		{
sl@0
  2614
		test.Printf(_L("Correct return value\n"));
sl@0
  2615
		test.Printf(_L("Passed...\n"));	
sl@0
  2616
		}
sl@0
  2617
	else
sl@0
  2618
		{
sl@0
  2619
		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
sl@0
  2620
		TEST_FAIL;	
sl@0
  2621
		}
sl@0
  2622
sl@0
  2623
	gTestThread.Close();
sl@0
  2624
	TestEnd();
sl@0
  2625
	
sl@0
  2626
sl@0
  2627
	test.Next(_L("Test3: Whilst moving page, read pages"));	
sl@0
  2628
	TestStart();	
sl@0
  2629
	
sl@0
  2630
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks, KChunkDefaultSize);
sl@0
  2631
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  2632
	WriteToChunk(gChunkArray1, gChunkArraySize1, KAllChunks);
sl@0
  2633
	
sl@0
  2634
	r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
sl@0
  2635
	if (r != KErrNone)
sl@0
  2636
		{
sl@0
  2637
		test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  2638
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  2639
		TEST_FAIL;
sl@0
  2640
		}	
sl@0
  2641
	gTestThread.Logon(status);
sl@0
  2642
	gTestThread.Resume();
sl@0
  2643
sl@0
  2644
	while (status.Int() == KRequestPending)
sl@0
  2645
		{
sl@0
  2646
		User::After(100000000);
sl@0
  2647
		ReadChunk(gChunkArray1, gChunkArraySize1);
sl@0
  2648
		}		
sl@0
  2649
	
sl@0
  2650
	User::WaitForRequest(status);
sl@0
  2651
	r = status.Int();
sl@0
  2652
	TESTDEBUG(test.Printf(_L("defrag running on another thread returns %d\n"), r));
sl@0
  2653
sl@0
  2654
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  2655
sl@0
  2656
	if (r == KErrNone)
sl@0
  2657
		{
sl@0
  2658
		test.Printf(_L("Correct return value\n"));
sl@0
  2659
		test.Printf(_L("Passed...\n"));	
sl@0
  2660
		}
sl@0
  2661
	else
sl@0
  2662
		{
sl@0
  2663
		test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  2664
		TEST_FAIL;	
sl@0
  2665
		}
sl@0
  2666
		
sl@0
  2667
	gTestThread.Close();
sl@0
  2668
	TestEnd();
sl@0
  2669
sl@0
  2670
	test.Next(_L("Test4: Allocate fixed pages and then perform a defrag"));	
sl@0
  2671
	TestStart();	
sl@0
  2672
	
sl@0
  2673
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  2674
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  2675
	
sl@0
  2676
	Ldd.AllocateFixedWrite(FILL_ALL_FIXED);
sl@0
  2677
sl@0
  2678
	GetOriginalPageCount();
sl@0
  2679
	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  2680
	test.Printf(_L("defrag returns %d\n"), r);
sl@0
  2681
sl@0
  2682
	if (CheckZonesSwitchedOff() != EFalse)
sl@0
  2683
		{
sl@0
  2684
		test.Printf(_L("Fail: Zones were switched off when they shouldn't have been\n"));
sl@0
  2685
		CLEANUP(r = Ldd.FreeAllFixedPagesRead());
sl@0
  2686
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  2687
		TEST_FAIL;
sl@0
  2688
		}
sl@0
  2689
	else
sl@0
  2690
		{
sl@0
  2691
		test.Printf(_L("Passed...\n"));	
sl@0
  2692
		}
sl@0
  2693
	r = Ldd.FreeAllFixedPagesRead();
sl@0
  2694
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);	
sl@0
  2695
	TestEnd();
sl@0
  2696
	
sl@0
  2697
	test.Next(_L("Test5: Allocate discardable pages and then perform a defrag"));	
sl@0
  2698
	TestStart();	
sl@0
  2699
	if (gPagedRom)
sl@0
  2700
		{
sl@0
  2701
		TInt discardablePages;
sl@0
  2702
				
sl@0
  2703
		UpdateRamInfo();
sl@0
  2704
		r = AllocDiscardable(discardablePages);
sl@0
  2705
		if (r != KErrNone)
sl@0
  2706
			{
sl@0
  2707
			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  2708
			TEST_FAIL;
sl@0
  2709
			}
sl@0
  2710
		TESTDEBUG(test.Printf(_L("Number of discardable pages = 0x%x\n"), discardablePages >> gPageShift));
sl@0
  2711
sl@0
  2712
		GetOriginalPageCount();
sl@0
  2713
		TBool genSucceed = CanGenSucceed();
sl@0
  2714
		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  2715
sl@0
  2716
		TESTDEBUG(test.Printf(_L("defrag returns %d\n"), r));
sl@0
  2717
		
sl@0
  2718
		if (genSucceed && CheckZonesSwitchedOff() == EFalse)
sl@0
  2719
			{
sl@0
  2720
			test.Printf(_L("Fail: Zones were not switched off when they should have been\n"));
sl@0
  2721
			CLEANUP(ResetDPCache());
sl@0
  2722
			TEST_FAIL;
sl@0
  2723
			}
sl@0
  2724
		else
sl@0
  2725
			{
sl@0
  2726
			test.Printf(_L("Passed...\n"));	
sl@0
  2727
			}
sl@0
  2728
		ResetDPCache();
sl@0
  2729
		
sl@0
  2730
		}
sl@0
  2731
	else
sl@0
  2732
		{
sl@0
  2733
		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
sl@0
  2734
		}
sl@0
  2735
	TestEnd();
sl@0
  2736
		
sl@0
  2737
	test.End();
sl@0
  2738
	return 0;
sl@0
  2739
	}
sl@0
  2740
		
sl@0
  2741
sl@0
  2742
//
sl@0
  2743
// TestMovPgsDefrag
sl@0
  2744
//
sl@0
  2745
//---------------------------------------------------------------------------------------------------------------------
sl@0
  2746
//! @SYMTestCaseID				KBASE-t_ramdefrag-0527
sl@0
  2747
//! @SYMTestType				CIT
sl@0
  2748
//! @SYMTestCaseDesc			Verifying the moving of pages in the defrag implmentation
sl@0
  2749
//! @SYMPREQ					PREQ308
sl@0
  2750
//! @SYMTestPriority			High
sl@0
  2751
//! @SYMTestActions				
sl@0
  2752
//! 	1.	Fragment the memory. Following this perform a RAM defrag. 
sl@0
  2753
//! 		Now that all movable memory has now been moved and the relevant source
sl@0
  2754
//! 		zones have been switched off, again attempt to perform a RAM defrag. 
sl@0
  2755
//! 	2.	Fragment the memory. Following this perform a RAM defrag and whilst 
sl@0
  2756
//! 		this is happening, continuously create more chunks. 
sl@0
  2757
//!		3.	Set up memory so that there are a mix of movable and free pages in a low preference zone, with there
sl@0
  2758
//!			being movable pages in the higher preference zones. Following this call a general defrag
sl@0
  2759
//!		4.	Set up memory so that there are a mix of movable and free pages and a couple of free pages 
sl@0
  2760
//!			in a low preference zone, with there being enough free pages in the higher preference zones for the 
sl@0
  2761
//!			pages to be moved to. Following this call a general defrag
sl@0
  2762
//!		5.	Set up memory so that there is a fixed page and a movable page in an empty zone (testZone). Also ensure that 
sl@0
  2763
//!			there are movable and free pages in more preferable zones than the test zone. Following this call a general
sl@0
  2764
//!			defrag
sl@0
  2765
//!		6.	Set up memory so that the mostPrefZone contains movable and free pages. Allocate 1 movable and 1 fixed
sl@0
  2766
//!			page into an empty zone (testZone). Following this call a general defrag. 
sl@0
  2767
//! 
sl@0
  2768
//! @SYMTestExpectedResults
sl@0
  2769
//! 	1.	Second defrag does not empty any zones
sl@0
  2770
//! 	2.	No zones have been emptied
sl@0
  2771
//!		3.	Zones are emptied if the general defrag can succeed
sl@0
  2772
//! 	4.	Defrag fills up the less preferable zones first
sl@0
  2773
//!		5.	movable pages are moved from the testZone
sl@0
  2774
//!		6.	testZone is not emptied
sl@0
  2775
//---------------------------------------------------------------------------------------------------------------------
sl@0
  2776
TInt TestMovPgsDefrag()
sl@0
  2777
	{
sl@0
  2778
	TInt r = KErrNone;
sl@0
  2779
sl@0
  2780
	test.Start(_L("Test1: Performing a defrag twice"));
sl@0
  2781
	TestStart();
sl@0
  2782
	
sl@0
  2783
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  2784
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  2785
	
sl@0
  2786
	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  2787
	TESTDEBUG(test.Printf(_L("After calling general defrag r = %d\n"), r));
sl@0
  2788
sl@0
  2789
	r = VerifyMovDisAlloc();	
sl@0
  2790
	// The first defrag should empty zones if the general can succeed 
sl@0
  2791
	if (r != KErrNone)
sl@0
  2792
		{
sl@0
  2793
		test.Printf(_L("Fail: r = %d, memory is not laid out as expected\n"), r);
sl@0
  2794
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  2795
		TEST_FAIL;
sl@0
  2796
		}
sl@0
  2797
sl@0
  2798
	GetOriginalPageCount();
sl@0
  2799
	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  2800
	
sl@0
  2801
	TESTDEBUG(test.Printf(_L("After calling general defrag again r = %d\n"), r));
sl@0
  2802
	
sl@0
  2803
	// The second call to general defrag should have nothing further to do
sl@0
  2804
	if (r != KErrNone || CheckZonesSwitchedOff() != EFalse)
sl@0
  2805
		{
sl@0
  2806
		test.Printf(_L("Fail: r = %d, expected = %d, or zones have been switched off \n"), 
sl@0
  2807
						r, KErrNone);
sl@0
  2808
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  2809
		TEST_FAIL;
sl@0
  2810
		}
sl@0
  2811
	else
sl@0
  2812
		{
sl@0
  2813
		test.Printf(_L("Passed...\n"));	
sl@0
  2814
		}
sl@0
  2815
	
sl@0
  2816
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);	
sl@0
  2817
	TestEnd();	
sl@0
  2818
	
sl@0
  2819
	
sl@0
  2820
	
sl@0
  2821
	test.Next(_L("Test2: Ensure new memory allocations can occur successfully during a general defrag"));
sl@0
  2822
	TestStart();
sl@0
  2823
	
sl@0
  2824
	// Create a thread to call Defrag continuously and resume it
sl@0
  2825
	r = gTestThread.Create(gTestThreadName,MultiLoopGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
sl@0
  2826
	if (r != KErrNone)
sl@0
  2827
		{
sl@0
  2828
		test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  2829
		TEST_FAIL;
sl@0
  2830
		}		
sl@0
  2831
	gTestThread.Logon(status);
sl@0
  2832
	gTestLoop = ETrue;
sl@0
  2833
	gTestThread.Resume();
sl@0
  2834
	
sl@0
  2835
	// Whilst the defrag loop is taking place, continuously allocate and free memory
sl@0
  2836
	for (TInt i = 0; i < 100; i++)
sl@0
  2837
		{
sl@0
  2838
		r = AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  2839
		if (r != KErrNone)
sl@0
  2840
			break;
sl@0
  2841
		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  2842
		}
sl@0
  2843
	gTestLoop = EFalse;
sl@0
  2844
	User::WaitForRequest(status);
sl@0
  2845
	TESTDEBUG(test.Printf(_L("defrag running on another thread returns %d\n"), r));
sl@0
  2846
		
sl@0
  2847
	if (r != KErrNone || status.Int() != KErrNone)
sl@0
  2848
		{
sl@0
  2849
		test.Printf(_L("Fail: r = %d, status.Int() = %d\n"), r, status.Int());	
sl@0
  2850
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  2851
		TEST_FAIL;
sl@0
  2852
		}
sl@0
  2853
	else
sl@0
  2854
		{
sl@0
  2855
		test.Printf(_L("Passed...\n"));	
sl@0
  2856
		}
sl@0
  2857
sl@0
  2858
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  2859
	RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
sl@0
  2860
	gTestThread.Close();	
sl@0
  2861
	TestEnd();
sl@0
  2862
	
sl@0
  2863
	
sl@0
  2864
	test.Next(_L("Test3: Check whether RAM defrag switches off any zones"));
sl@0
  2865
	TestStart();
sl@0
  2866
	
sl@0
  2867
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  2868
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  2869
sl@0
  2870
	GetOriginalPageCount();
sl@0
  2871
	TBool genSucceed = CanGenSucceed();
sl@0
  2872
	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  2873
	
sl@0
  2874
	TESTDEBUG(test.Printf(_L("After calling zone defrag r = %d\n"), r));
sl@0
  2875
	
sl@0
  2876
	if (r != KErrNone || (genSucceed && CheckZonesSwitchedOff() == EFalse))
sl@0
  2877
		{
sl@0
  2878
		test.Printf(_L("Fail: No zones were switched off or r = %d, expected = %d\n"), r, KErrNone);
sl@0
  2879
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  2880
		TEST_FAIL;
sl@0
  2881
		}
sl@0
  2882
	else
sl@0
  2883
		{
sl@0
  2884
		test.Printf(_L("Passed...\n"));	
sl@0
  2885
		}
sl@0
  2886
sl@0
  2887
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  2888
	TestEnd();
sl@0
  2889
sl@0
  2890
sl@0
  2891
	test.Next(_L("Test4: Verify that a general defrag moves pages to the least preferable RAM zone in use"));
sl@0
  2892
	TestStart();
sl@0
  2893
	gChunkArray1 = new RChunk;
sl@0
  2894
	gChunkArraySize1 = 1;
sl@0
  2895
	// Get the preference order
sl@0
  2896
	GetPrefOrder();
sl@0
  2897
sl@0
  2898
	TUint mostPrefIndex = 0;
sl@0
  2899
	TUint mostPrefArrayIndex = 0;
sl@0
  2900
	TUint totMov = 0;
sl@0
  2901
	TUint lastPrefIndex = 0;
sl@0
  2902
	TUint lastPrefArrayIndex = 0;
sl@0
  2903
	TUint movPages = 0;
sl@0
  2904
	TUint movBytes = 0;
sl@0
  2905
	TUint totFreeInTestZones = 0;
sl@0
  2906
	TBool zoneNotEmptyOrFull = EFalse;
sl@0
  2907
sl@0
  2908
sl@0
  2909
	// Find the first most pref zone that has free pages in it
sl@0
  2910
	r = FindMostPrefWithFree(mostPrefIndex, &mostPrefArrayIndex);
sl@0
  2911
	if (r != KErrNone)
sl@0
  2912
		{
sl@0
  2913
		test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
sl@0
  2914
		goto skipTest4;
sl@0
  2915
		}
sl@0
  2916
sl@0
  2917
	// Find the next most preferable free zone
sl@0
  2918
	lastPrefArrayIndex = mostPrefArrayIndex + 1;
sl@0
  2919
sl@0
  2920
	while (lastPrefArrayIndex < gZoneCount &&
sl@0
  2921
		gZoneUtilArray[gPrefArray[lastPrefArrayIndex]].iFreePages != gZoneUtilArray[gPrefArray[lastPrefArrayIndex]].iPhysPages)
sl@0
  2922
		{
sl@0
  2923
		lastPrefArrayIndex++;
sl@0
  2924
		}
sl@0
  2925
sl@0
  2926
	if (lastPrefArrayIndex >= gZoneCount)
sl@0
  2927
		{
sl@0
  2928
		test.Printf(_L("Skipping...\n"));
sl@0
  2929
		goto skipTest4;
sl@0
  2930
		}
sl@0
  2931
sl@0
  2932
	// Block all other zones
sl@0
  2933
	for (TUint prefIndex = lastPrefArrayIndex + 1; prefIndex < gZoneCount; prefIndex++)
sl@0
  2934
		{
sl@0
  2935
		TUint zoneIndex = gPrefArray[prefIndex];
sl@0
  2936
		Ldd.SetZoneFlag(gZoneConfigArray[zoneIndex].iZoneId, gZoneConfigArray[zoneIndex].iFlags, NO_ALLOC_FLAG);
sl@0
  2937
		}
sl@0
  2938
sl@0
  2939
sl@0
  2940
	// Zone alloc to fill half of the last zone under test
sl@0
  2941
	GetAllPageInfo();
sl@0
  2942
	lastPrefIndex = gPrefArray[lastPrefArrayIndex];
sl@0
  2943
	r = Ldd.ZoneAllocDiscontiguous(gZoneConfigArray[lastPrefIndex].iZoneId, gZoneUtilArray[lastPrefIndex].iFreePages >> 1);
sl@0
  2944
	if (r != KErrNone)
sl@0
  2945
		{
sl@0
  2946
		test.Printf(_L("Failed to allocate fixed pages r = %d - Skipping...\n"), r);
sl@0
  2947
		goto skipTest4;
sl@0
  2948
		}
sl@0
  2949
sl@0
  2950
sl@0
  2951
	// Go through the zones under test and determine the number of free pages
sl@0
  2952
	GetAllPageInfo();
sl@0
  2953
	for(TUint prefIndex = mostPrefArrayIndex; prefIndex <= lastPrefArrayIndex; prefIndex++)
sl@0
  2954
		{
sl@0
  2955
		TUint zoneIndex = gPrefArray[prefIndex];
sl@0
  2956
		totFreeInTestZones += gZoneUtilArray[zoneIndex].iFreePages;
sl@0
  2957
		}
sl@0
  2958
sl@0
  2959
	// Allocate movable pages to almost fill zones under test
sl@0
  2960
	movPages = totFreeInTestZones;
sl@0
  2961
	movBytes = movPages << gPageShift;
sl@0
  2962
	while (movBytes && AllocMovable(gChunkArray1, gChunkArraySize1, 1, movBytes) != KErrNone)
sl@0
  2963
		{
sl@0
  2964
		movBytes -= gPageSize;
sl@0
  2965
		movPages--;
sl@0
  2966
		}
sl@0
  2967
	if (!movBytes)
sl@0
  2968
		{
sl@0
  2969
		test.Printf(_L("Failed to allocate 0x%x movable pages - Skipping...\n"), movPages);
sl@0
  2970
		goto skipTest4;
sl@0
  2971
		}
sl@0
  2972
sl@0
  2973
sl@0
  2974
	// Free the allocated fixed pages
sl@0
  2975
	Ldd.FreeAllFixedPages();
sl@0
  2976
sl@0
  2977
	// Reset all the flags
sl@0
  2978
	ResetRamZoneFlags();
sl@0
  2979
	
sl@0
  2980
	// Check that the most preferable zone still has movable pages in it
sl@0
  2981
	// and also check that the last zone under test has free pages in it
sl@0
  2982
	GetAllPageInfo();
sl@0
  2983
	if (gTotalPageCount.iMovablePages < movPages ||
sl@0
  2984
		gZoneUtilArray[mostPrefIndex].iAllocMovable == 0 ||
sl@0
  2985
		gZoneUtilArray[lastPrefIndex].iFreePages == 0)
sl@0
  2986
		{
sl@0
  2987
		test.Printf(_L("Setup failed - Skipping...\n"));
sl@0
  2988
		goto skipTest4;
sl@0
  2989
		}
sl@0
  2990
sl@0
  2991
	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  2992
	// Check that defrag allocated the movable from the less preferable to the more preferable
sl@0
  2993
	GetPrefOrder();
sl@0
  2994
	totMov = gTotalPageCount.iMovablePages;
sl@0
  2995
	for(TInt prefIndex = gZoneCount - 1; prefIndex >= 0 ; prefIndex--)
sl@0
  2996
		{
sl@0
  2997
		TUint zoneIndex = gPrefArray[prefIndex];
sl@0
  2998
		GetCandList1(zoneIndex);
sl@0
  2999
		TUint candIndex = 0;
sl@0
  3000
		for (; candIndex < gZoneCount; candIndex++)
sl@0
  3001
			{// Check all the zones of this preference
sl@0
  3002
			TInt zoneIndexCand = gCandList1[candIndex];
sl@0
  3003
			if (zoneIndexCand == KInvalidCandIndex)
sl@0
  3004
				{
sl@0
  3005
				break;
sl@0
  3006
				}
sl@0
  3007
			totMov -= gZoneUtilArray[zoneIndexCand].iAllocMovable;
sl@0
  3008
			if (gZoneUtilArray[zoneIndexCand].iFreePages != gZoneUtilArray[zoneIndexCand].iPhysPages &&
sl@0
  3009
				gZoneUtilArray[zoneIndexCand].iFreePages != 0)
sl@0
  3010
				{
sl@0
  3011
				zoneNotEmptyOrFull = ETrue;
sl@0
  3012
				}
sl@0
  3013
			}
sl@0
  3014
		prefIndex -= candIndex - 1 ;
sl@0
  3015
sl@0
  3016
		if (zoneNotEmptyOrFull && totMov != 0)
sl@0
  3017
			{
sl@0
  3018
			test.Printf(_L("FAIL: index = %d free = 0x%x totMov = 0x%x\n"), 
sl@0
  3019
							zoneIndex, gZoneUtilArray[zoneIndex].iFreePages, totMov);
sl@0
  3020
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  3021
			CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
sl@0
  3022
			TEST_FAIL;
sl@0
  3023
			}
sl@0
  3024
		}
sl@0
  3025
sl@0
  3026
skipTest4:
sl@0
  3027
	TestEnd();
sl@0
  3028
sl@0
  3029
sl@0
  3030
	test.Next(_L("Test5: Verify that a general defrag tidies up if RAM zone to be emptied contains fixed pages"));
sl@0
  3031
	TestStart();
sl@0
  3032
	gChunkArray1 = new RChunk;
sl@0
  3033
	gChunkArraySize1 = 1;
sl@0
  3034
	gChunkArray2 = new RChunk;
sl@0
  3035
	gChunkArraySize2 = 1;
sl@0
  3036
sl@0
  3037
	// find 2 empty zones - put 1 movable in 1 of them and 1 movable and 1 fixed in the other
sl@0
  3038
	TUint testZoneIndex1;
sl@0
  3039
	TUint testZoneIndex2;
sl@0
  3040
	TUint testZoneID2;
sl@0
  3041
	TUint numFreeInUse = 0;
sl@0
  3042
sl@0
  3043
	r = FindMostPrefEmpty(testZoneIndex1);
sl@0
  3044
	if (r != KErrNone)
sl@0
  3045
		{
sl@0
  3046
		test.Printf(_L("Skipping...\n"));
sl@0
  3047
		goto skipTest5;
sl@0
  3048
		}
sl@0
  3049
	// Allocate 1 movable page to the test zone
sl@0
  3050
	r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, testZoneIndex1, 1);
sl@0
  3051
	if (r != KErrNone)
sl@0
  3052
		{
sl@0
  3053
		test.Printf(_L("Failed to allocate 1 movable page to zone index %d, r = %d - Skipping...\n"), testZoneIndex1, r);
sl@0
  3054
		goto skipTest5;
sl@0
  3055
		}
sl@0
  3056
	
sl@0
  3057
	r = FindMostPrefEmpty(testZoneIndex2);
sl@0
  3058
	if (r != KErrNone)
sl@0
  3059
		{
sl@0
  3060
		test.Printf(_L("Skipping...\n"));
sl@0
  3061
		goto skipTest5;
sl@0
  3062
		}
sl@0
  3063
	// Allocate 1 movable page to the test zone
sl@0
  3064
	r = ZoneAllocMovable(gChunkArray2, gChunkArraySize2, testZoneIndex2, 1);
sl@0
  3065
	if (r != KErrNone)
sl@0
  3066
		{
sl@0
  3067
		test.Printf(_L("Failed to allocate 1 movable page to zone index %d, r = %d - Skipping...\n"), testZoneIndex2, r);
sl@0
  3068
		goto skipTest5;
sl@0
  3069
		}
sl@0
  3070
sl@0
  3071
	// Zone alloc to put 1 fixed page last zone under test
sl@0
  3072
	testZoneID2 = gZoneConfigArray[testZoneIndex2].iZoneId;
sl@0
  3073
	r = Ldd.ZoneAllocContiguous(testZoneID2, gPageSize);
sl@0
  3074
	if (r != KErrNone)
sl@0
  3075
		{
sl@0
  3076
		test.Printf(_L("Failed to allocate 1 fixed page to zone %d index, r = %d - Skipping...\n"), testZoneIndex2, r);
sl@0
  3077
		goto skipTest5;
sl@0
  3078
		}
sl@0
  3079
	
sl@0
  3080
	// Allocate 2 fixed (as there is 1 in our test zone) to prevent reordering
sl@0
  3081
	GetAllPageInfo();
sl@0
  3082
	for (TUint index = 0; index < gZoneCount; index++)
sl@0
  3083
		{
sl@0
  3084
		TUint totImmovable = gZoneUtilArray[index].iAllocFixed + gZoneUtilArray[index].iAllocUnknown ;
sl@0
  3085
		if (index != testZoneIndex1 &&
sl@0
  3086
			index != testZoneIndex2 &&
sl@0
  3087
			gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
sl@0
  3088
			totImmovable < 2)
sl@0
  3089
			{
sl@0
  3090
			r = Ldd.ZoneAllocToMany(index, 2 - totImmovable);
sl@0
  3091
			if (r != KErrNone)
sl@0
  3092
				{
sl@0
  3093
				test.Printf(_L("Failed to alloc 0x%x fixed to zone index %d r = %d - Skipping...\n"), 
sl@0
  3094
								2, index, r);
sl@0
  3095
				goto skipTest5; 
sl@0
  3096
				}
sl@0
  3097
			}
sl@0
  3098
		}
sl@0
  3099
sl@0
  3100
	// Check that the number of free pages in the other in-use zones >= movable pages in test zone
sl@0
  3101
	GetAllPageInfo();
sl@0
  3102
	for (TUint index = 0; index < gZoneCount; index++)
sl@0
  3103
		{
sl@0
  3104
		if (index != testZoneIndex1 &&
sl@0
  3105
			index != testZoneIndex2 &&
sl@0
  3106
			gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages)
sl@0
  3107
			{
sl@0
  3108
			numFreeInUse += gZoneUtilArray[index].iFreePages;
sl@0
  3109
			}
sl@0
  3110
		}
sl@0
  3111
	if (gZoneUtilArray[testZoneIndex1].iAllocMovable + gZoneUtilArray[testZoneIndex1].iAllocDiscardable + 
sl@0
  3112
		gZoneUtilArray[testZoneIndex2].iAllocMovable + gZoneUtilArray[testZoneIndex2].iAllocDiscardable > numFreeInUse ||
sl@0
  3113
		gZoneUtilArray[testZoneIndex2].iAllocFixed == 0)
sl@0
  3114
		{
sl@0
  3115
		test.Printf(_L("Setup failed - Skipping...\n"));
sl@0
  3116
		goto skipTest5;
sl@0
  3117
		}
sl@0
  3118
sl@0
  3119
	// Added extra tracing to debug random failures
sl@0
  3120
	PrintPageInfo();
sl@0
  3121
sl@0
  3122
	// Call a general defrag
sl@0
  3123
	GetOriginalPageCount();
sl@0
  3124
	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  3125
sl@0
  3126
	// Check that the defrag tidies up
sl@0
  3127
	GetAllPageInfo();
sl@0
  3128
	if(gZoneUtilArray[testZoneIndex1].iAllocMovable ||
sl@0
  3129
		gZoneUtilArray[testZoneIndex2].iAllocMovable)
sl@0
  3130
		{
sl@0
  3131
		test.Printf(_L("FAIL: testZoneIndex1(%d): mov orig 0x%x cur 0x%x, testZoneIndex2(%d) mov orig 0x%x cur 0x%x \n"), 
sl@0
  3132
						testZoneIndex1, gOriginalPageCountArray[testZoneIndex1].iAllocMovable, gZoneUtilArray[testZoneIndex1].iAllocMovable, 
sl@0
  3133
						testZoneIndex2, gOriginalPageCountArray[testZoneIndex2].iAllocMovable, gZoneUtilArray[testZoneIndex2].iAllocMovable);
sl@0
  3134
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  3135
		CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
sl@0
  3136
		CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  3137
		TEST_FAIL;
sl@0
  3138
		}
sl@0
  3139
sl@0
  3140
skipTest5:
sl@0
  3141
	TestEnd();
sl@0
  3142
sl@0
  3143
sl@0
  3144
	test.Next(_L("Test6: Verify that a general defrag will not move pages if RAM zone to be emptied contains fixed pages"));
sl@0
  3145
	TestStart();
sl@0
  3146
	gChunkArray1 = new RChunk;
sl@0
  3147
	gChunkArraySize1 = 1;
sl@0
  3148
	// Find the first most pref zone that has free pages in it
sl@0
  3149
	r = FindMostPrefWithFree(mostPrefIndex, &mostPrefArrayIndex);
sl@0
  3150
	if (r != KErrNone)
sl@0
  3151
		{
sl@0
  3152
		test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
sl@0
  3153
		goto skipTest6;
sl@0
  3154
		}
sl@0
  3155
sl@0
  3156
	// Find the next most preferable free zone
sl@0
  3157
	lastPrefArrayIndex = mostPrefArrayIndex + 1;
sl@0
  3158
	while (lastPrefArrayIndex < gZoneCount &&
sl@0
  3159
		gZoneUtilArray[gPrefArray[lastPrefArrayIndex]].iFreePages != gZoneUtilArray[gPrefArray[lastPrefArrayIndex]].iPhysPages)
sl@0
  3160
		{
sl@0
  3161
		lastPrefArrayIndex++;
sl@0
  3162
		}
sl@0
  3163
	if (lastPrefArrayIndex >= gZoneCount)
sl@0
  3164
		{
sl@0
  3165
		test.Printf(_L("Skipping...\n"));
sl@0
  3166
		goto skipTest6;
sl@0
  3167
		}
sl@0
  3168
sl@0
  3169
sl@0
  3170
	// Zone alloc to put 1 fixed page last zone under test
sl@0
  3171
	lastPrefIndex = gPrefArray[lastPrefArrayIndex];
sl@0
  3172
	r = Ldd.ZoneAllocDiscontiguous(gZoneConfigArray[lastPrefIndex].iZoneId, 1);
sl@0
  3173
	if (r != KErrNone)
sl@0
  3174
		{
sl@0
  3175
		test.Printf(_L("Failed to allocate 1 fixed page to zone index %d r = %d - Skipping...\n"), lastPrefIndex, r);
sl@0
  3176
		goto skipTest6;
sl@0
  3177
		}
sl@0
  3178
	
sl@0
  3179
	GetAllPageInfo();
sl@0
  3180
	if (!gZoneUtilArray[mostPrefIndex].iFreePages)
sl@0
  3181
		{
sl@0
  3182
		test.Printf(_L("Not enough space in zone under test  mostPrefIndex = %d - Skipping...\n"), mostPrefIndex);
sl@0
  3183
		goto skipTest6;
sl@0
  3184
		}
sl@0
  3185
sl@0
  3186
	// Block all zones apart from the last zone under test from allocation
sl@0
  3187
	for (TUint prefIndex = 0; prefIndex < gZoneCount; prefIndex++)
sl@0
  3188
		{
sl@0
  3189
		TUint zoneIndex = gPrefArray[prefIndex];
sl@0
  3190
		if (zoneIndex != lastPrefIndex)
sl@0
  3191
			{
sl@0
  3192
			Ldd.SetZoneFlag(gZoneConfigArray[zoneIndex].iZoneId, gZoneConfigArray[zoneIndex].iFlags, NO_ALLOC_FLAG);
sl@0
  3193
			}
sl@0
  3194
		}
sl@0
  3195
	
sl@0
  3196
	TESTDEBUG(test.Printf(_L("mostPrefIndex = %d lastPrefIndex = %d\n"), mostPrefIndex, lastPrefIndex));
sl@0
  3197
	
sl@0
  3198
	// Allocate movable pages to the lastPrefZone that will fit into the most pref zone
sl@0
  3199
	movPages = gZoneUtilArray[mostPrefIndex].iFreePages;
sl@0
  3200
	movBytes = movPages << gPageShift;
sl@0
  3201
	while (movBytes && AllocMovable(gChunkArray1, gChunkArraySize1, 1, movBytes) != KErrNone)
sl@0
  3202
		{
sl@0
  3203
		movBytes -= gPageSize;
sl@0
  3204
		}
sl@0
  3205
	if (!movBytes)
sl@0
  3206
		{
sl@0
  3207
		test.Printf(_L("Failed to allocate 0x%x movable pages r = %d - Skipping...\n"), movPages, r);
sl@0
  3208
		goto skipTest6;
sl@0
  3209
		}
sl@0
  3210
sl@0
  3211
	// Reset all the flags
sl@0
  3212
	ResetRamZoneFlags();
sl@0
  3213
	
sl@0
  3214
sl@0
  3215
	// Check that the number of movable pages in the least pref will fit
sl@0
  3216
	// into the most preferable zone
sl@0
  3217
	GetAllPageInfo();
sl@0
  3218
	if (gZoneUtilArray[lastPrefIndex].iAllocMovable > gZoneUtilArray[mostPrefIndex].iFreePages)
sl@0
  3219
		{
sl@0
  3220
		test.Printf(_L("Setup failed - Skipping...\n"));
sl@0
  3221
		PrintPageInfo();
sl@0
  3222
		goto skipTest6;
sl@0
  3223
		}
sl@0
  3224
	
sl@0
  3225
	// Added extra tracing to debug random failures
sl@0
  3226
	PrintPageInfo();	
sl@0
  3227
sl@0
  3228
	// Call a general defrag
sl@0
  3229
	GetOriginalPageCount();
sl@0
  3230
	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  3231
sl@0
  3232
	// Check that the defrag didn't move any pages from the least pref zone
sl@0
  3233
	GetAllPageInfo();
sl@0
  3234
	if(gOriginalPageCountArray[lastPrefIndex].iAllocMovable > gZoneUtilArray[lastPrefIndex].iAllocMovable)
sl@0
  3235
		{
sl@0
  3236
		test.Printf(_L("FAIL: mostPref(index %d): origMov = 0x%x curMov = 0x%x, lastPref(index %d): origMov = 0x%x curMov = 0x%x \n"), 
sl@0
  3237
						mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocMovable, gZoneUtilArray[mostPrefIndex].iAllocMovable, 
sl@0
  3238
						lastPrefIndex, 	gOriginalPageCountArray[lastPrefIndex].iAllocMovable, gZoneUtilArray[lastPrefIndex].iAllocMovable);
sl@0
  3239
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  3240
		CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
sl@0
  3241
		CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  3242
		TEST_FAIL;
sl@0
  3243
		}
sl@0
  3244
sl@0
  3245
skipTest6:
sl@0
  3246
	TestEnd();
sl@0
  3247
sl@0
  3248
	test.End();
sl@0
  3249
	return KErrNone;
sl@0
  3250
	}
sl@0
  3251
sl@0
  3252
//
sl@0
  3253
// GenDefragTest123Setup
sl@0
  3254
//
sl@0
  3255
// Used to set up the memory ready for testing in TestGenDefrag, Test Steps 1, 2, 3 and 4
sl@0
  3256
//
sl@0
  3257
// @param aMovAlloc The number of movable pages to allocate.
sl@0
  3258
// @param aFixAlloc The number of fixed pages to allocate.
sl@0
  3259
// @param aDisAlloc The number of discardable pages to allocate.
sl@0
  3260
// @param aMovIndex On return contains the test zone index.
sl@0
  3261
// @param aNumFreeInUse On return contains the number of free pages in the inuse zones.
sl@0
  3262
// @param aTestNo The test number that is using this setup.
sl@0
  3263
//
sl@0
  3264
TInt GenDefragTest123Setup(TUint aMovAlloc, TUint aFixAlloc, TUint aDisAlloc, TUint& aMovIndex, TUint& aNumFreeInUse, TUint aTestNo)
sl@0
  3265
	{
sl@0
  3266
	TInt r = KErrNone;
sl@0
  3267
	aNumFreeInUse = 0;
sl@0
  3268
	const TUint KFillUpTo = 5;	// Rough estimate of how full the in-use zones should be
sl@0
  3269
	const TUint KSpaceNeeded = 2;
sl@0
  3270
	TUint movPrefIndex = 0;
sl@0
  3271
	if (aTestNo == 4)
sl@0
  3272
		{// Test 4 we need to look for the LEAST preferable RAM zone which is empty
sl@0
  3273
		TUint mostPrefIndex;
sl@0
  3274
		r = FindLeastPrefEmpty(aMovIndex, &movPrefIndex);
sl@0
  3275
		TInt r2 = FindMostPrefEmpty(mostPrefIndex);
sl@0
  3276
		if (r != KErrNone || r2 != KErrNone || !gPagedRom ||
sl@0
  3277
			gZoneConfigArray[aMovIndex].iPref == gZoneConfigArray[mostPrefIndex].iPref)
sl@0
  3278
			{
sl@0
  3279
			test.Printf(_L("r %d r2 %d or not a paged ROM or Equal zone preferences- Skipping test step...\n"), 
sl@0
  3280
							r, r2);
sl@0
  3281
			return KErrGeneral;
sl@0
  3282
			}
sl@0
  3283
		}
sl@0
  3284
	else
sl@0
  3285
		{// All other tests look the MOST preferable RAM zone which is empty
sl@0
  3286
		r = FindMostPrefEmpty(aMovIndex, &movPrefIndex);
sl@0
  3287
		if (r != KErrNone || !gPagedRom)
sl@0
  3288
			{
sl@0
  3289
			test.Printf(_L("No suitable RAM zone found or not a paged ROM - Skipping test step...\n"));
sl@0
  3290
			return KErrGeneral;
sl@0
  3291
			}
sl@0
  3292
		}
sl@0
  3293
sl@0
  3294
	// Go through all the in-use zones and check how many free pages there are
sl@0
  3295
	// Allocate fixed so there is only KFillUpTo pages in each in-use zone to
sl@0
  3296
	// ensure that the total number of free pages in the in-use zones can fit into
sl@0
  3297
	// the zone under test
sl@0
  3298
	GetAllPageInfo();
sl@0
  3299
	for (TUint index = 0; index < gZoneCount; index++)
sl@0
  3300
		{
sl@0
  3301
		if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
sl@0
  3302
			gZoneUtilArray[index].iFreePages > KFillUpTo)
sl@0
  3303
			{
sl@0
  3304
			r = Ldd.ZoneAllocToMany(index, gZoneUtilArray[index].iFreePages - KFillUpTo);
sl@0
  3305
			if (r != KErrNone)
sl@0
  3306
				{
sl@0
  3307
				test.Printf(_L("Failed to alloc 0x%x fixed to zone index %d r = %d - Ending setup...\n"), 
sl@0
  3308
								gZoneUtilArray[index].iFreePages - KFillUpTo, index, r);
sl@0
  3309
				return KErrGeneral; 
sl@0
  3310
				}
sl@0
  3311
			}
sl@0
  3312
		}
sl@0
  3313
sl@0
  3314
	GetAllPageInfo();
sl@0
  3315
	// If any of the in-use zones doesn't contain any fixed and unknown pages allocate one fixed
sl@0
  3316
	for (TUint index = 0; index < gZoneCount; index++)
sl@0
  3317
		{
sl@0
  3318
		if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
sl@0
  3319
			!gZoneUtilArray[index].iAllocFixed && !gZoneUtilArray[index].iAllocUnknown)
sl@0
  3320
			{
sl@0
  3321
			r = Ldd.ZoneAllocToMany(index, 1);
sl@0
  3322
			if (r != KErrNone)
sl@0
  3323
				{
sl@0
  3324
				test.Printf(_L("Failed to alloc 0x%x fixed to zone index %d r = %d - Ending setup...\n"), 
sl@0
  3325
								1, index, r);
sl@0
  3326
				return KErrGeneral;
sl@0
  3327
				}
sl@0
  3328
			}
sl@0
  3329
		}
sl@0
  3330
sl@0
  3331
	GetAllPageInfo();
sl@0
  3332
	// Check the total number of free pages in the in-use zones
sl@0
  3333
	for (TUint index = 0; index < gZoneCount; index++)
sl@0
  3334
		{
sl@0
  3335
		if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages)
sl@0
  3336
			{
sl@0
  3337
			aNumFreeInUse += gZoneUtilArray[index].iFreePages;
sl@0
  3338
			}
sl@0
  3339
		}
sl@0
  3340
	TESTDEBUG(test.Printf(_L("aNumFreeInUse = 0x%x\n"), aNumFreeInUse));
sl@0
  3341
sl@0
  3342
	test.Printf(_L("aMovIndex = %d\n"), aMovIndex);
sl@0
  3343
	if (aNumFreeInUse < KSpaceNeeded)
sl@0
  3344
		{
sl@0
  3345
		test.Printf(_L("RAM zone to be tested is too full - Skipping test step...\n"));
sl@0
  3346
		return KErrGeneral;
sl@0
  3347
		}
sl@0
  3348
sl@0
  3349
sl@0
  3350
	// Ensure that there are movable pages in the zone under test
sl@0
  3351
	// ensuring that there is also room for discardable pages
sl@0
  3352
	GetAllPageInfo();
sl@0
  3353
	if (aMovAlloc != 0 && gZoneUtilArray[aMovIndex].iAllocMovable == 0)
sl@0
  3354
		{
sl@0
  3355
		// Allocate just aMovAlloc movable pages
sl@0
  3356
		TUint movPagesAlloc = aMovAlloc;
sl@0
  3357
		if (aTestNo == 2 || aTestNo == 4)
sl@0
  3358
			{// Tests 2 and 4 require that movable pages can't be moved from zone
sl@0
  3359
			movPagesAlloc = aNumFreeInUse + 1;
sl@0
  3360
			if (movPagesAlloc > gZoneUtilArray[aMovIndex].iFreePages)
sl@0
  3361
				{
sl@0
  3362
				test.Printf(_L("Insufficiant space in zone %d to allocate 0x%x pages - Ending setup...\n"), 
sl@0
  3363
								aMovIndex, movPagesAlloc);
sl@0
  3364
				return KErrGeneral;
sl@0
  3365
				}
sl@0
  3366
			}
sl@0
  3367
sl@0
  3368
		r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, aMovIndex, movPagesAlloc);
sl@0
  3369
		if (gZoneUtilArray[aMovIndex].iAllocMovable != movPagesAlloc ||
sl@0
  3370
			gZoneUtilArray[aMovIndex].iFreePages == 0 ||
sl@0
  3371
			r != KErrNone)
sl@0
  3372
			{// Movable page didn't go into RAM, or RAM zone is full.
sl@0
  3373
			test.Printf(_L("Can't complete test with current RAM layout. - Skipping...\n"));
sl@0
  3374
			test.Printf(_L("zone mov 0x%x free 0x%x r=%d\n"), 
sl@0
  3375
						gZoneUtilArray[aMovIndex].iAllocMovable,	
sl@0
  3376
						gZoneUtilArray[aMovIndex].iFreePages, r);
sl@0
  3377
			return KErrGeneral;
sl@0
  3378
			}
sl@0
  3379
		}
sl@0
  3380
sl@0
  3381
	// Block all other zones from allocation
sl@0
  3382
	for(TUint i = 0; i < gZoneCount; i++)
sl@0
  3383
		{
sl@0
  3384
		if (i != aMovIndex)
sl@0
  3385
			{
sl@0
  3386
			r = Ldd.SetZoneFlag(gZoneConfigArray[i].iZoneId, gZoneConfigArray[i].iFlags, NO_ALLOC_FLAG);
sl@0
  3387
			if (r != KErrNone)
sl@0
  3388
				{
sl@0
  3389
				test.Printf(_L("Failed to set flag on zone index %d r = %d- Ending setup...\n"), i, r);
sl@0
  3390
				return KErrGeneral;
sl@0
  3391
				}
sl@0
  3392
			}
sl@0
  3393
		}
sl@0
  3394
sl@0
  3395
	// Now ensure that there are fixed pages in the zone under test
sl@0
  3396
	// ensuring that there is also room for discardable pages
sl@0
  3397
	GetAllPageInfo();
sl@0
  3398
	if (aFixAlloc != 0 && gZoneUtilArray[aMovIndex].iAllocFixed == 0)
sl@0
  3399
		{//Allocate just aFixAlloc fixed pages 
sl@0
  3400
		r = Ldd.ZoneAllocToMany(aMovIndex, aFixAlloc);
sl@0
  3401
		GetAllPageInfo();
sl@0
  3402
		if (gZoneUtilArray[aMovIndex].iAllocMovable == 0 ||
sl@0
  3403
			gZoneUtilArray[aMovIndex].iFreePages == 0 ||
sl@0
  3404
			gZoneUtilArray[aMovIndex].iAllocFixed == 0 ||
sl@0
  3405
			r != KErrNone)
sl@0
  3406
			{// Fixed page didn't go into RAM or RAM zone is full.
sl@0
  3407
			test.Printf(_L("Can't complete test with current RAM layout. - Skipping...\n"));
sl@0
  3408
			test.Printf(_L("zone mov 0x%x fixed 0x%x free 0x%x r=%d\n"), 
sl@0
  3409
						gZoneUtilArray[aMovIndex].iAllocMovable,	
sl@0
  3410
						gZoneUtilArray[aMovIndex].iAllocFixed,	
sl@0
  3411
						gZoneUtilArray[aMovIndex].iFreePages, r);
sl@0
  3412
			return KErrGeneral;
sl@0
  3413
			}
sl@0
  3414
		}
sl@0
  3415
sl@0
  3416
sl@0
  3417
	
sl@0
  3418
	// Allocate aDisAlloc number of discardable pages
sl@0
  3419
	if (aDisAlloc != 0 && gZoneUtilArray[aMovIndex].iAllocDiscardable == 0)
sl@0
  3420
		{//Allocate just aDisAlloc discardable pages 
sl@0
  3421
		TInt discard;
sl@0
  3422
		r = ZoneAllocDiscard(aMovIndex, aDisAlloc, discard);
sl@0
  3423
		GetAllPageInfo();
sl@0
  3424
		if (r != KErrNone || gZoneUtilArray[aMovIndex].iAllocDiscardable == 0)
sl@0
  3425
			{// Discardable page didn't go into RAM or RAM zone is full.
sl@0
  3426
			test.Printf(_L("Can't complete test with current RAM layout. - Skipping...\n"));
sl@0
  3427
			test.Printf(_L("zone mov 0x%x discardable 0x%x free 0x%x r=%d\n"), 
sl@0
  3428
						gZoneUtilArray[aMovIndex].iAllocDiscardable,	
sl@0
  3429
						gZoneUtilArray[aMovIndex].iAllocFixed,	
sl@0
  3430
						gZoneUtilArray[aMovIndex].iFreePages, r);
sl@0
  3431
			return KErrGeneral;
sl@0
  3432
			}
sl@0
  3433
		}
sl@0
  3434
	
sl@0
  3435
	// Update the total number of free pages in the in-use zones
sl@0
  3436
	aNumFreeInUse = 0;
sl@0
  3437
	for (TUint index = 0; index < gZoneCount; index++)
sl@0
  3438
		{
sl@0
  3439
		if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
sl@0
  3440
			index != aMovIndex)
sl@0
  3441
			{
sl@0
  3442
			aNumFreeInUse += gZoneUtilArray[index].iFreePages;
sl@0
  3443
			}
sl@0
  3444
		}
sl@0
  3445
	TESTDEBUG(test.Printf(_L("aNumFreeInUse = 0x%x\n"), aNumFreeInUse));
sl@0
  3446
sl@0
  3447
	// now reset all the flags 
sl@0
  3448
	ResetRamZoneFlags();
sl@0
  3449
	
sl@0
  3450
	return KErrNone;
sl@0
  3451
	}
sl@0
  3452
//
sl@0
  3453
// TestGenDefrag
sl@0
  3454
//
sl@0
  3455
//---------------------------------------------------------------------------------------------------------------------
sl@0
  3456
//! @SYMTestCaseID				KBASE-t_ramdefrag-0528
sl@0
  3457
//! @SYMTestType				CIT
sl@0
  3458
//! @SYMTestCaseDesc			Verifying general scenarios when RAM defrag would take place
sl@0
  3459
//! @SYMPREQ					PREQ308
sl@0
  3460
//! @SYMTestPriority			High
sl@0
  3461
//! @SYMTestActions				
sl@0
  3462
//! 	1.	Allocate 1 movable, 1 fixed and 1 discardable pages into the most preferable empty RAM zone
sl@0
  3463
//!			ensuring that there is enough free space in the more preferable RAM zones for the
sl@0
  3464
//!			movable pages in the RAM zone under test. Following this call a general defrag.
sl@0
  3465
//!		2.	Allocate 1 movable and 1 discardable page into into the most preferable empty RAM zone
sl@0
  3466
//!			ensuring that there is not enough free space in the more preferable RAM zones for the
sl@0
  3467
//!			movable pages in the RAM zone under test. Following this call a general defrag. 
sl@0
  3468
//!		3.	Allocate 1 movable and 1 discardable and 1 fixed page into the most preferable empty RAM zone(a). 
sl@0
  3469
//!			Following this, place 1 movable page in an empty zone(b) ensuring that there is enough free space in 
sl@0
  3470
//!			the more preferable RAM zones for the movable pages in both zones (a) and (b). 
sl@0
  3471
//!			Following this call a general defrag. 
sl@0
  3472
//!		4.	Allocate 1 movable and 1 discardable page into the least preferable empty RAM zone
sl@0
  3473
//!			ensuring that there is not enough free space in the more preferable RAM zones for the
sl@0
  3474
//!			movable pages in the RAM zone under test. Following this call a general defrag. 
sl@0
  3475
//! 	5.	Fragment the memory. 
sl@0
  3476
//! 		Following this allocate discardable pages by loading pages that are demand paged. 
sl@0
  3477
//! 		Read each of the constants from beginning to end. 
sl@0
  3478
//! 		Following this perform a RAM defrag. 
sl@0
  3479
//! 	6.	Call a device driver that will continuously allocate fixed pages to the
sl@0
  3480
//! 		memory until it reports out of memory. Following this perform a RAM defrag. 
sl@0
  3481
//!		7.	Defrag memory filled with discardable pages when the min cache size is reached
sl@0
  3482
//!		8.	Fragment the memory. Following this, continuously call a general defrag, each time 
sl@0
  3483
//!			reducing the size of the chunks that are allocated  
sl@0
  3484
//! 
sl@0
  3485
//! @SYMTestExpectedResults
sl@0
  3486
//! 	1.	The RAM zone under test is not emptied and all the discardable pages contained in it
sl@0
  3487
//!			have not been discarded.
sl@0
  3488
//! 	2.	The RAM zone under test is not emptied and all the discardable pages contained in it
sl@0
  3489
//!			have not been discarded.
sl@0
  3490
//! 	3.	RAM zones (a) and (b) have been emptied and all the discardable pages contained in them
sl@0
  3491
//!			have not been discarded.
sl@0
  3492
//!		4.	The RAM zone under test is emptied
sl@0
  3493
//! 	5.	Pages are discarded
sl@0
  3494
//! 	6.	No zones are emptied
sl@0
  3495
//!		7.	The least preferable zone is skipped but the other zones are defragmented as required.
sl@0
  3496
//! 	8.	Zones are emptied if the general defrag can succeed
sl@0
  3497
//---------------------------------------------------------------------------------------------------------------------
sl@0
  3498
TInt TestGenDefrag()
sl@0
  3499
	{
sl@0
  3500
	TInt r = KErrNone;
sl@0
  3501
	
sl@0
  3502
	test.Start(_L("Test1: Test General Defrag doesn't discard pages when fixed page in zone to be emptied"));	
sl@0
  3503
	TestStart();
sl@0
  3504
	gChunkArray1 = new RChunk;
sl@0
  3505
	gChunkArraySize1 = 1;
sl@0
  3506
	TUint numFreeInUse = 0;
sl@0
  3507
	TUint testNo = 1;
sl@0
  3508
sl@0
  3509
	// We just need 1 movable, 1 discardable and 1 fixed in the zone to ensure it cannot be emptied
sl@0
  3510
	// as the fixed should block the defrag
sl@0
  3511
	TInt movAllocPages = 1;
sl@0
  3512
	TInt fixAllocPages = 1;
sl@0
  3513
	TInt disAllocPages = 1;
sl@0
  3514
	TUint zoneIndex;
sl@0
  3515
sl@0
  3516
	if (GenDefragTest123Setup(movAllocPages, fixAllocPages, disAllocPages, zoneIndex, numFreeInUse, testNo) != KErrNone)
sl@0
  3517
		{
sl@0
  3518
		test.Printf(_L("Setup failed - Skipping..\n"));
sl@0
  3519
		goto skipTest1;
sl@0
  3520
		}
sl@0
  3521
sl@0
  3522
	// Call a general defrag and check that no discardable pages were freed.
sl@0
  3523
	GetOriginalPageCount();
sl@0
  3524
	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  3525
	GetAllPageInfo();
sl@0
  3526
	if (r != KErrNone || 
sl@0
  3527
		gZoneUtilArray[zoneIndex].iAllocMovable < gOriginalPageCountArray[zoneIndex].iAllocMovable ||
sl@0
  3528
		gOriginalPageCountArray[zoneIndex].iAllocDiscardable > gZoneUtilArray[zoneIndex].iAllocDiscardable)
sl@0
  3529
		{
sl@0
  3530
		test.Printf(_L("Fail: r = %d zoneIndex %d origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), r, zoneIndex, 
sl@0
  3531
						gOriginalPageCountArray[zoneIndex].iAllocMovable, gZoneUtilArray[zoneIndex].iAllocMovable, 
sl@0
  3532
						gOriginalPageCountArray[zoneIndex].iAllocDiscardable, gZoneUtilArray[zoneIndex].iAllocDiscardable);
sl@0
  3533
		CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  3534
		TEST_FAIL;
sl@0
  3535
		}
sl@0
  3536
	else
sl@0
  3537
		{
sl@0
  3538
		test.Printf(_L("Passed...\n"));	
sl@0
  3539
		}
sl@0
  3540
sl@0
  3541
skipTest1:
sl@0
  3542
	// TestEnd() will perform any required clean up.
sl@0
  3543
	TestEnd();
sl@0
  3544
sl@0
  3545
	test.Next(_L("Test2: Test General Defrag doesn't discard pages when zone can't be emptied"));	
sl@0
  3546
	TestStart();	
sl@0
  3547
	gChunkArray1 = new RChunk;
sl@0
  3548
	gChunkArraySize1 = 1;
sl@0
  3549
	testNo++;
sl@0
  3550
	// No fixed, but there should be no space for the movable to go to
sl@0
  3551
	// GenDefragTest123Setup(), will ensure that this is the case
sl@0
  3552
	// by allocating 1 more movable page than there is free in the in-use zones
sl@0
  3553
	movAllocPages = 1;
sl@0
  3554
	fixAllocPages = 0;
sl@0
  3555
	disAllocPages = 1;
sl@0
  3556
sl@0
  3557
	if (GenDefragTest123Setup(movAllocPages, fixAllocPages, disAllocPages, zoneIndex, numFreeInUse, testNo) != KErrNone)
sl@0
  3558
		{
sl@0
  3559
		test.Printf(_L("Setup failed - Skipping..\n"));
sl@0
  3560
		goto skipTest2;
sl@0
  3561
		}
sl@0
  3562
sl@0
  3563
	// Call a general defrag and check that no discardable pages were freed.
sl@0
  3564
	GetOriginalPageCount();
sl@0
  3565
	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  3566
	
sl@0
  3567
	GetAllPageInfo();
sl@0
  3568
	if (r != KErrNone || 
sl@0
  3569
		gZoneUtilArray[zoneIndex].iAllocMovable < gOriginalPageCountArray[zoneIndex].iAllocMovable ||
sl@0
  3570
		gOriginalPageCountArray[zoneIndex].iAllocDiscardable > gZoneUtilArray[zoneIndex].iAllocDiscardable)
sl@0
  3571
		{
sl@0
  3572
		test.Printf(_L("Fail: r = %d zoneIndex %d origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), r, zoneIndex, 
sl@0
  3573
						gOriginalPageCountArray[zoneIndex].iAllocMovable, gZoneUtilArray[zoneIndex].iAllocMovable, 
sl@0
  3574
						gOriginalPageCountArray[zoneIndex].iAllocDiscardable, gZoneUtilArray[zoneIndex].iAllocDiscardable);
sl@0
  3575
		CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  3576
		TEST_FAIL;
sl@0
  3577
		}
sl@0
  3578
	else
sl@0
  3579
		{
sl@0
  3580
		test.Printf(_L("Passed...\n"));	
sl@0
  3581
		}
sl@0
  3582
sl@0
  3583
skipTest2:
sl@0
  3584
	// TestEnd() will perform any required clean up.
sl@0
  3585
	TestEnd();
sl@0
  3586
sl@0
  3587
	test.Next(_L("Test3: Test General Defrag tidies when fixed page in zone to be emptied but other can be emptied"));	
sl@0
  3588
	TestStart();
sl@0
  3589
	gChunkArray1 = new RChunk;
sl@0
  3590
	gChunkArraySize1 = 1;
sl@0
  3591
	gChunkArray2 = new RChunk;
sl@0
  3592
	gChunkArraySize2 = 1;
sl@0
  3593
	TUint totalToMove;
sl@0
  3594
	testNo++;
sl@0
  3595
sl@0
  3596
	// First use GenDefragTest123Setup() to ensure we place 1 fixed, 1 movable and 1 discardable
sl@0
  3597
	// in the least preferable zone
sl@0
  3598
	movAllocPages = 1;
sl@0
  3599
	fixAllocPages = 1;
sl@0
  3600
	disAllocPages = 1;
sl@0
  3601
	TBool zoneFound = EFalse;	
sl@0
  3602
	TUint emptyZoneIndex = 0;
sl@0
  3603
sl@0
  3604
	if (GenDefragTest123Setup(movAllocPages, fixAllocPages, disAllocPages, zoneIndex, numFreeInUse, testNo) != KErrNone)
sl@0
  3605
		{
sl@0
  3606
		test.Printf(_L("Setup failed - Skipping..\n"));
sl@0
  3607
		goto skipTest3;
sl@0
  3608
		}
sl@0
  3609
sl@0
  3610
	// Find an empty zone and place a movable page in it
sl@0
  3611
	GetAllPageInfo();
sl@0
  3612
	for (TUint i = 0; i < gZoneCount; i++)
sl@0
  3613
		{
sl@0
  3614
		if (gZoneUtilArray[i].iFreePages == gZoneUtilArray[i].iPhysPages)
sl@0
  3615
			{
sl@0
  3616
			zoneFound = ETrue;
sl@0
  3617
			emptyZoneIndex = i;
sl@0
  3618
			break;
sl@0
  3619
			}
sl@0
  3620
		}
sl@0
  3621
	if (!zoneFound)
sl@0
  3622
		{
sl@0
  3623
		test.Printf(_L("Can't find empty zone - Skipping..\n"));
sl@0
  3624
		goto skipTest3;
sl@0
  3625
		}
sl@0
  3626
sl@0
  3627
	// Allocate 1 movable page into the empty zone
sl@0
  3628
	r = ZoneAllocMovable(gChunkArray2, gChunkArraySize2, emptyZoneIndex, 1);
sl@0
  3629
	GetAllPageInfo();
sl@0
  3630
	if (r != KErrNone ||
sl@0
  3631
		gZoneUtilArray[emptyZoneIndex].iAllocMovable == 0 ||
sl@0
  3632
		gZoneUtilArray[emptyZoneIndex].iAllocFixed != 0)
sl@0
  3633
		{
sl@0
  3634
		test.Printf(_L("Movable pages not allocated or fixed pages allocated. - Skipping...\n"));
sl@0
  3635
		test.Printf(_L("zone mov 0x%x free 0x%x r =% d\n"), 
sl@0
  3636
					gZoneUtilArray[emptyZoneIndex].iAllocMovable,	
sl@0
  3637
					gZoneUtilArray[emptyZoneIndex].iFreePages, r);
sl@0
  3638
		goto skipTest3;
sl@0
  3639
		}
sl@0
  3640
sl@0
  3641
	// Check that the amount we are allocating can actually be moved into the in-use zones
sl@0
  3642
	totalToMove = gZoneUtilArray[zoneIndex].iAllocMovable + gZoneUtilArray[zoneIndex].iAllocDiscardable + 
sl@0
  3643
					gZoneUtilArray[emptyZoneIndex].iAllocMovable + gZoneUtilArray[emptyZoneIndex].iAllocDiscardable;
sl@0
  3644
	
sl@0
  3645
	// Check the total number of free pages in the in-use zones
sl@0
  3646
	numFreeInUse = 0;
sl@0
  3647
	for (TUint index = 0; index < gZoneCount; index++)
sl@0
  3648
		{
sl@0
  3649
		if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
sl@0
  3650
			index != zoneIndex && index != emptyZoneIndex)
sl@0
  3651
			{
sl@0
  3652
			numFreeInUse += gZoneUtilArray[index].iFreePages;
sl@0
  3653
			}
sl@0
  3654
		}
sl@0
  3655
	if(numFreeInUse < totalToMove)
sl@0
  3656
		{
sl@0
  3657
		test.Printf(_L("No space to move pages numFreeInUse = 0x%x totalToMove = 0x%x - Skipping..\n"), 
sl@0
  3658
						numFreeInUse, totalToMove);
sl@0
  3659
		goto skipTest3;
sl@0
  3660
		}
sl@0
  3661
sl@0
  3662
	// Call a general defrag and check that zone is emptied.
sl@0
  3663
	GetOriginalPageCount();
sl@0
  3664
	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  3665
	
sl@0
  3666
	GetAllPageInfo();
sl@0
  3667
	if (r != KErrNone || 
sl@0
  3668
		gZoneUtilArray[zoneIndex].iAllocMovable ||
sl@0
  3669
		gZoneUtilArray[zoneIndex].iAllocDiscardable ||
sl@0
  3670
		gZoneUtilArray[emptyZoneIndex].iAllocMovable)
sl@0
  3671
		{
sl@0
  3672
		test.Printf(_L("Fail: r = %d zoneIndex %d origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), r, zoneIndex, 
sl@0
  3673
						gOriginalPageCountArray[zoneIndex].iAllocMovable, gZoneUtilArray[zoneIndex].iAllocMovable, 
sl@0
  3674
						gOriginalPageCountArray[zoneIndex].iAllocDiscardable, gZoneUtilArray[zoneIndex].iAllocDiscardable);
sl@0
  3675
		CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  3676
		TEST_FAIL;
sl@0
  3677
		}
sl@0
  3678
	else
sl@0
  3679
		{
sl@0
  3680
		test.Printf(_L("Passed...\n"));	
sl@0
  3681
		}
sl@0
  3682
sl@0
  3683
skipTest3:
sl@0
  3684
	// TestEnd() will perform any required clean up.
sl@0
  3685
	TestEnd();
sl@0
  3686
sl@0
  3687
	test.Next(_L("Test4: Test General Defrag moves pages into the next most pref if they don't fit into the most pref"));	
sl@0
  3688
	TestStart();
sl@0
  3689
	gChunkArray1 = new RChunk;	
sl@0
  3690
	gChunkArraySize1 = 1;
sl@0
  3691
	testNo++;
sl@0
  3692
	
sl@0
  3693
	// No fixed, but there should be no space for the movable to go to
sl@0
  3694
	// GenDefragTest123Setup(), will ensure that this is the case
sl@0
  3695
	// by allocating 1 more movable page than there is free in the in-use zones
sl@0
  3696
	movAllocPages = 1;
sl@0
  3697
	fixAllocPages = 0;
sl@0
  3698
	disAllocPages = 1;
sl@0
  3699
sl@0
  3700
	if (GenDefragTest123Setup(movAllocPages, fixAllocPages, disAllocPages, zoneIndex, numFreeInUse, testNo) != KErrNone)
sl@0
  3701
		{
sl@0
  3702
		test.Printf(_L("Setup failed - Skipping..\n"));
sl@0
  3703
		goto skipTest4;
sl@0
  3704
		}
sl@0
  3705
sl@0
  3706
	// Call a general defrag and check that the test zone is emptied.
sl@0
  3707
	GetOriginalPageCount();
sl@0
  3708
	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  3709
	
sl@0
  3710
	GetAllPageInfo();
sl@0
  3711
	if (r != KErrNone || 
sl@0
  3712
		gZoneUtilArray[zoneIndex].iFreePages != gZoneUtilArray[zoneIndex].iPhysPages)
sl@0
  3713
		{
sl@0
  3714
		test.Printf(_L("Fail: r = %d zoneIndex %d origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), r, zoneIndex, 
sl@0
  3715
						gOriginalPageCountArray[zoneIndex].iAllocMovable, gZoneUtilArray[zoneIndex].iAllocMovable, 
sl@0
  3716
						gOriginalPageCountArray[zoneIndex].iAllocDiscardable, gZoneUtilArray[zoneIndex].iAllocDiscardable);
sl@0
  3717
		CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  3718
		TEST_FAIL;
sl@0
  3719
		}
sl@0
  3720
	else
sl@0
  3721
		{
sl@0
  3722
		test.Printf(_L("Passed...\n"));	
sl@0
  3723
		}
sl@0
  3724
sl@0
  3725
skipTest4:
sl@0
  3726
	// TestEnd() will perform any required clean up.
sl@0
  3727
	TestEnd();
sl@0
  3728
sl@0
  3729
	test.Next(_L("Test5: Defrag memory filled with discardable pages\n"));
sl@0
  3730
	TestStart();
sl@0
  3731
	if (gPagedRom)
sl@0
  3732
		{
sl@0
  3733
		AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  3734
		FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  3735
		
sl@0
  3736
		GetAllPageInfo();	
sl@0
  3737
		TInt discardablePages;
sl@0
  3738
		UpdateRamInfo();
sl@0
  3739
		
sl@0
  3740
		r = AllocDiscardable(discardablePages);
sl@0
  3741
		if (r != KErrNone)
sl@0
  3742
			{
sl@0
  3743
			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  3744
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  3745
			TEST_FAIL;
sl@0
  3746
			}
sl@0
  3747
		TESTDEBUG(test.Printf(_L("Number of discardable pages = 0x%x\n"), discardablePages >> gPageShift));
sl@0
  3748
		
sl@0
  3749
		GetOriginalPageCount();
sl@0
  3750
		TBool genSucceed = CanGenSucceed();
sl@0
  3751
		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  3752
		TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
sl@0
  3753
		
sl@0
  3754
		if (r != KErrNone)
sl@0
  3755
			{
sl@0
  3756
			test.Printf(_L("Fail: r = %d, expected = %d\n"),r,KErrNone);
sl@0
  3757
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  3758
			TEST_FAIL;
sl@0
  3759
			}
sl@0
  3760
		else if (genSucceed && CheckZonesSwitchedOff() == EFalse)
sl@0
  3761
			{
sl@0
  3762
			test.Printf(_L("No Zones Switched off and should have been\n"));
sl@0
  3763
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  3764
			TEST_FAIL;
sl@0
  3765
			}
sl@0
  3766
		else
sl@0
  3767
			{
sl@0
  3768
			test.Printf(_L("Passed...\n"));	
sl@0
  3769
			}
sl@0
  3770
sl@0
  3771
		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  3772
		ResetDPCache();	
sl@0
  3773
		}
sl@0
  3774
	else
sl@0
  3775
		{
sl@0
  3776
		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
sl@0
  3777
		}
sl@0
  3778
	TestEnd();
sl@0
  3779
		
sl@0
  3780
	
sl@0
  3781
	test.Next(_L("Test6: Defrag memory filled with fixed pages"));	
sl@0
  3782
	TestStart();
sl@0
  3783
	
sl@0
  3784
	TESTDEBUG(test.Printf(_L("Filling memory with fixed pages, r = %d\n")));
sl@0
  3785
	r = Ldd.AllocateFixed(FILL_ALL_FIXED);
sl@0
  3786
sl@0
  3787
	GetOriginalPageCount();
sl@0
  3788
	TBool genSucceed = CanGenSucceed();
sl@0
  3789
	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  3790
	TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
sl@0
  3791
sl@0
  3792
	// The zones should be full of fixed pages so the general should do nothing.
sl@0
  3793
	if (r != KErrNone || 
sl@0
  3794
		genSucceed || 
sl@0
  3795
		CheckZonesSwitchedOff())
sl@0
  3796
		{
sl@0
  3797
		test.Printf(_L("Fail: r = %d, expected = %d, or zone have been emptied\n"), r, KErrNone);
sl@0
  3798
		CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  3799
		TEST_FAIL;
sl@0
  3800
		}
sl@0
  3801
	else
sl@0
  3802
		{
sl@0
  3803
		test.Printf(_L("Passed...\n"));	
sl@0
  3804
		}
sl@0
  3805
	Ldd.FreeAllFixedPages();
sl@0
  3806
	TestEnd();
sl@0
  3807
sl@0
  3808
	test.Next(_L("Test7: Defrag memory filled with discardable pages when the min cache size is reached\n"));
sl@0
  3809
	TestStart();
sl@0
  3810
	if (gPagedRom)
sl@0
  3811
		{
sl@0
  3812
		AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  3813
		FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  3814
		
sl@0
  3815
		GetAllPageInfo();	
sl@0
  3816
		TInt discardablePages;
sl@0
  3817
		UpdateRamInfo();
sl@0
  3818
		
sl@0
  3819
		r = AllocDiscardable(discardablePages);
sl@0
  3820
		if (r != KErrNone)
sl@0
  3821
			{
sl@0
  3822
			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  3823
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  3824
			TEST_FAIL;
sl@0
  3825
			}
sl@0
  3826
		TESTDEBUG(test.Printf(_L("Number of discardable pages = 0x%x\n"), discardablePages >> gPageShift));
sl@0
  3827
sl@0
  3828
		TUint minCacheSize = 0;
sl@0
  3829
		TUint maxCacheSize = 0;
sl@0
  3830
		TUint currentCacheSize = 0;
sl@0
  3831
sl@0
  3832
		DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
sl@0
  3833
sl@0
  3834
		TUint setMin = maxCacheSize;
sl@0
  3835
		TUint64 setMax = maxCacheSize;
sl@0
  3836
		TInt r = DPTest::SetCacheSize(setMin, setMax);
sl@0
  3837
		if (r != KErrNone)
sl@0
  3838
			{
sl@0
  3839
			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  3840
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  3841
			CLEANUP(ResetDPCache());
sl@0
  3842
			TEST_FAIL;
sl@0
  3843
			}			
sl@0
  3844
		TESTDEBUG(test.Printf(_L("After SetCacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x\n"), 
sl@0
  3845
															setMin >> gPageShift, setMax >> gPageShift));
sl@0
  3846
		
sl@0
  3847
		GetOriginalPageCount();
sl@0
  3848
		genSucceed = CanGenSucceed();
sl@0
  3849
		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  3850
		TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
sl@0
  3851
sl@0
  3852
		if (r != KErrNone)
sl@0
  3853
			{
sl@0
  3854
			test.Printf(_L("Fail: r = %d, expected = %d\n"),r,KErrNone);
sl@0
  3855
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  3856
			CLEANUP(ResetDPCache());
sl@0
  3857
			TEST_FAIL;
sl@0
  3858
			}
sl@0
  3859
		else if (genSucceed && CheckZonesSwitchedOff() == EFalse)
sl@0
  3860
			{
sl@0
  3861
			test.Printf(_L("No Zones Switched off and should have been\n"));
sl@0
  3862
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  3863
			CLEANUP(ResetDPCache());
sl@0
  3864
			TEST_FAIL;
sl@0
  3865
			}
sl@0
  3866
		else
sl@0
  3867
			{
sl@0
  3868
			test.Printf(_L("Passed...\n"));	
sl@0
  3869
			}
sl@0
  3870
sl@0
  3871
		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  3872
		ResetDPCache();
sl@0
  3873
		
sl@0
  3874
		
sl@0
  3875
		}
sl@0
  3876
	else
sl@0
  3877
		{
sl@0
  3878
		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
sl@0
  3879
		}
sl@0
  3880
	TestEnd();
sl@0
  3881
sl@0
  3882
	test.Next(_L("Test8: Defrag fragmented memory for various smaller chunk sizes"));	
sl@0
  3883
	TInt chunkSize = 0x80000;
sl@0
  3884
	while (chunkSize >= 0x4000)
sl@0
  3885
		{
sl@0
  3886
		test.Printf(_L("chunkSize = %dKB\n"), chunkSize/1024);
sl@0
  3887
		TestStart();
sl@0
  3888
		r = AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable, chunkSize, EFalse);
sl@0
  3889
		r = FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  3890
		
sl@0
  3891
		GetOriginalPageCount();
sl@0
  3892
		TBool genSucceed = CanGenSucceed();
sl@0
  3893
		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  3894
		TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
sl@0
  3895
		
sl@0
  3896
		if (r != KErrNone)
sl@0
  3897
			{
sl@0
  3898
			test.Printf(_L("Fail: r = %d, expected = %d\n"),r,KErrNone);
sl@0
  3899
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  3900
			TEST_FAIL;
sl@0
  3901
			}
sl@0
  3902
		else if (genSucceed && CheckZonesSwitchedOff() == EFalse)
sl@0
  3903
			{
sl@0
  3904
			test.Printf(_L("No Zones Switched off and should have been\n"));
sl@0
  3905
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  3906
			TEST_FAIL;
sl@0
  3907
			}
sl@0
  3908
		else
sl@0
  3909
			{
sl@0
  3910
			test.Printf(_L("Passed...\n"));	
sl@0
  3911
			}
sl@0
  3912
sl@0
  3913
		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  3914
		TestEnd();
sl@0
  3915
		chunkSize = chunkSize>>1; 
sl@0
  3916
		}
sl@0
  3917
sl@0
  3918
	test.Next(_L("Test9: Defrag fragmented memory "));	
sl@0
  3919
	TestStart();
sl@0
  3920
	
sl@0
  3921
	r = AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
sl@0
  3922
	r = FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  3923
	
sl@0
  3924
	GetOriginalPageCount();
sl@0
  3925
	genSucceed = CanGenSucceed();
sl@0
  3926
	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  3927
	TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
sl@0
  3928
sl@0
  3929
	if (r != KErrNone)
sl@0
  3930
		{
sl@0
  3931
		test.Printf(_L("Fail: r = %d, expected = %d\n"),r,KErrNone);
sl@0
  3932
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  3933
		TEST_FAIL;
sl@0
  3934
		}
sl@0
  3935
	else if (genSucceed && CheckZonesSwitchedOff() == EFalse)
sl@0
  3936
		{
sl@0
  3937
		test.Printf(_L("No Zones Switched off and should have been\n"));
sl@0
  3938
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  3939
		TEST_FAIL;
sl@0
  3940
		}
sl@0
  3941
	else
sl@0
  3942
		{
sl@0
  3943
		test.Printf(_L("Passed...\n"));	
sl@0
  3944
		}
sl@0
  3945
sl@0
  3946
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  3947
	TestEnd();
sl@0
  3948
sl@0
  3949
	test.End();
sl@0
  3950
	return KErrNone;
sl@0
  3951
	}
sl@0
  3952
sl@0
  3953
sl@0
  3954
//
sl@0
  3955
// TestGetRamZonePageCount
sl@0
  3956
//
sl@0
  3957
//---------------------------------------------------------------------------------------------------------------------
sl@0
  3958
//! @SYMTestCaseID				KBASE-t_ramdefrag-0529
sl@0
  3959
//! @SYMTestType				CIT
sl@0
  3960
//! @SYMTestCaseDesc			Verifying the implementation of the function GetRamZonePageCount()
sl@0
  3961
//! @SYMPREQ					PREQ308
sl@0
  3962
//! @SYMTestPriority			High
sl@0
  3963
//! @SYMTestActions				
sl@0
  3964
//! 	1.	Fragment the memory. Following this, call GetRamZonePageCount() on every zone and verify
sl@0
  3965
//!			the values with the test HAL functions. 
sl@0
  3966
//! 	2.	Fragment the memory. Following this, call function with an valid aID
sl@0
  3967
//! 
sl@0
  3968
//! @SYMTestExpectedResults
sl@0
  3969
//! 	1.	KErrNone
sl@0
  3970
//! 	2.	KErrArgument
sl@0
  3971
//---------------------------------------------------------------------------------------------------------------------
sl@0
  3972
TInt TestGetRamZonePageCount()
sl@0
  3973
	{
sl@0
  3974
	test.Start(_L("Test1: Call GetRamZonePageCount() on every zone one after the other"));	
sl@0
  3975
	TestStart();	
sl@0
  3976
		
sl@0
  3977
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  3978
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  3979
	
sl@0
  3980
	for (TUint index = 0; index < gZoneCount; index++)
sl@0
  3981
		{
sl@0
  3982
		GetAllPageInfo();
sl@0
  3983
		TUint zoneID = gZoneConfigArray[index].iZoneId;
sl@0
  3984
sl@0
  3985
		STestUserSidePageCount pageData;
sl@0
  3986
		TInt r = Ldd.PageCount(zoneID, &pageData);
sl@0
  3987
		TESTDEBUG(test.Printf(_L("Page count function r = %d\n"), r));
sl@0
  3988
		if (r != KErrNone)
sl@0
  3989
			{
sl@0
  3990
			test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
sl@0
  3991
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  3992
			TEST_FAIL;
sl@0
  3993
			}
sl@0
  3994
		
sl@0
  3995
		if (pageData.iFreePages != gZoneUtilArray[index].iFreePages ||
sl@0
  3996
			pageData.iFixedPages != gZoneUtilArray[index].iAllocFixed ||
sl@0
  3997
			pageData.iMovablePages != gZoneUtilArray[index].iAllocMovable ||
sl@0
  3998
			pageData.iDiscardablePages != gZoneUtilArray[index].iAllocDiscardable)
sl@0
  3999
			{
sl@0
  4000
			test.Printf(_L("RAM zone page count does not match test HAL page count, Zone %d\n"), zoneID);
sl@0
  4001
			test.Printf(_L("PgCnt: free = 0x%x, fixed = 0x%x, movable= 0x%x, discard = 0x%x\n"),
sl@0
  4002
									pageData.iFreePages, pageData.iFixedPages, pageData.iMovablePages, pageData.iDiscardablePages);
sl@0
  4003
			test.Printf(_L("HalFunc: free = 0x%x, fixed = 0x%x, movable= 0x%x, discard = 0x%x\n"),
sl@0
  4004
									gZoneUtilArray[index].iFreePages, gZoneUtilArray[index].iAllocFixed, 
sl@0
  4005
									gZoneUtilArray[index].iAllocMovable, gZoneUtilArray[index].iAllocDiscardable);
sl@0
  4006
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  4007
			TEST_FAIL;
sl@0
  4008
			}
sl@0
  4009
		else
sl@0
  4010
			{
sl@0
  4011
			test.Printf(_L("Zone %d Passed...\n"), zoneID);	
sl@0
  4012
			}
sl@0
  4013
		TESTDEBUG(test.Printf(_L("iFreePages = 0x%x, iFixedPages = 0x%x, iMovablePages = 0x%x, iDiscardablePages = 0x%x\n"),
sl@0
  4014
						pageData.iFreePages, pageData.iFixedPages, pageData.iMovablePages, pageData.iDiscardablePages));
sl@0
  4015
		}	
sl@0
  4016
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  4017
	TestEnd();
sl@0
  4018
	
sl@0
  4019
	test.Next(_L("Test2: Call GetRamZonePageCount() with an invalid aID "));	
sl@0
  4020
	TestStart();	
sl@0
  4021
		
sl@0
  4022
	TUint zoneID = KInvalidZoneID;
sl@0
  4023
	STestUserSidePageCount pageData;
sl@0
  4024
	TInt r = Ldd.PageCount(zoneID, &pageData);
sl@0
  4025
	TESTDEBUG(test.Printf(_L("Page count function r = %d\n"), r));
sl@0
  4026
	if (r != KErrArgument)
sl@0
  4027
		{
sl@0
  4028
		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
sl@0
  4029
		TEST_FAIL;
sl@0
  4030
		}
sl@0
  4031
	else
sl@0
  4032
		{
sl@0
  4033
		test.Printf(_L("Passed...\n"));	
sl@0
  4034
		}
sl@0
  4035
	TestEnd();
sl@0
  4036
	
sl@0
  4037
	test.End();
sl@0
  4038
	return 0;
sl@0
  4039
	}
sl@0
  4040
sl@0
  4041
const TUint KIndex2Alloc = 10;
sl@0
  4042
const TUint KTestMaxPages = 6;
sl@0
  4043
sl@0
  4044
//
sl@0
  4045
// DefragMaxPagesSetup
sl@0
  4046
//
sl@0
  4047
// Arranges the memory layout in preparation for TestDefragRamMaxPages()
sl@0
  4048
//
sl@0
  4049
// WARNING THIS WILL BE UNRELIABLE IF aMaxPages > KIndex2Alloc
sl@0
  4050
// 
sl@0
  4051
TInt DefragMaxPagesSetup(TInt aPageType, TUint aMaxPages, TUint& aIndex, TUint& aIndex2)
sl@0
  4052
	{
sl@0
  4053
	TInt r = KErrNoMemory;
sl@0
  4054
	
sl@0
  4055
	// Get the preference order and determine the 2 most preferable zones
sl@0
  4056
	// that are currently not in use.
sl@0
  4057
	GetPrefOrder();
sl@0
  4058
	TUint freeInUsePages = 0;
sl@0
  4059
	TBool zonesFound = EFalse;
sl@0
  4060
	for (TUint i = 1; i < gZoneCount; i++)
sl@0
  4061
		{
sl@0
  4062
		aIndex = gPrefArray[i];
sl@0
  4063
		aIndex2 = gPrefArray[i-1];
sl@0
  4064
		TUint indexFree = gZoneUtilArray[aIndex].iFreePages;
sl@0
  4065
		TUint index2Free = gZoneUtilArray[aIndex2].iFreePages;
sl@0
  4066
		if (indexFree == gZoneUtilArray[aIndex].iPhysPages &&
sl@0
  4067
			index2Free == gZoneUtilArray[aIndex2].iPhysPages &&
sl@0
  4068
			indexFree >= aMaxPages && index2Free >= KIndex2Alloc &&
sl@0
  4069
			freeInUsePages >= KIndex2Alloc + aMaxPages)
sl@0
  4070
			{
sl@0
  4071
			zonesFound = ETrue;
sl@0
  4072
			break;
sl@0
  4073
			}
sl@0
  4074
		freeInUsePages += index2Free;
sl@0
  4075
		}	
sl@0
  4076
	
sl@0
  4077
	// Could suitable RAM zones be found.
sl@0
  4078
	if (!zonesFound)
sl@0
  4079
		{
sl@0
  4080
		test.Printf(_L("Insufficient memory - Skipping test...\n"));
sl@0
  4081
		return KErrNoMemory;
sl@0
  4082
		}
sl@0
  4083
sl@0
  4084
	if (aPageType == BEST_MOVABLE)
sl@0
  4085
		{		
sl@0
  4086
		// Allocate KIndex2Alloc movable pages to aIndex2
sl@0
  4087
		r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, aIndex2, KIndex2Alloc);
sl@0
  4088
		if (r != KErrNone)
sl@0
  4089
			{
sl@0
  4090
			test.Printf(_L("Insufficient memory - Skipping test...\n"));
sl@0
  4091
			r = KErrNoMemory;
sl@0
  4092
			goto error;
sl@0
  4093
			}
sl@0
  4094
sl@0
  4095
			
sl@0
  4096
sl@0
  4097
		// Now allow movable pages to be allocated into the least preferable RAM zone under test only.
sl@0
  4098
		GetAllPageInfo();
sl@0
  4099
		if (aMaxPages >= gZoneUtilArray[aIndex].iFreePages)
sl@0
  4100
			{
sl@0
  4101
			test.Printf(_L("Insufficient memory available - Skipping test...\n"));
sl@0
  4102
			r = KErrNoMemory;
sl@0
  4103
			goto error;
sl@0
  4104
			}
sl@0
  4105
		// Allocate aMaxPages movable pages to aIndex
sl@0
  4106
		r = ZoneAllocMovable(gChunkArray2, gChunkArraySize2, aIndex, aMaxPages);
sl@0
  4107
		if (r != KErrNone)
sl@0
  4108
			{
sl@0
  4109
			test.Printf(_L("Insufficient memory - Skipping test...\n"));
sl@0
  4110
			r = KErrNoMemory;
sl@0
  4111
			goto error;
sl@0
  4112
			}
sl@0
  4113
		
sl@0
  4114
		// Determine how many free pages there are in the RAM zones more preferable
sl@0
  4115
		// than the RAM zones under test.
sl@0
  4116
		GetPrefOrder();
sl@0
  4117
		freeInUsePages = 0;
sl@0
  4118
		for (TUint i = 0; i < gZoneCount; i++)
sl@0
  4119
			{
sl@0
  4120
			TUint tmpIndex = gPrefArray[i];
sl@0
  4121
			if (tmpIndex == aIndex2)
sl@0
  4122
				break;
sl@0
  4123
			freeInUsePages += gZoneUtilArray[tmpIndex].iFreePages;
sl@0
  4124
			}
sl@0
  4125
		// Verify that the RAM layout is still suitable for the test.
sl@0
  4126
		if (gZoneUtilArray[aIndex].iAllocMovable != aMaxPages ||
sl@0
  4127
			gZoneUtilArray[aIndex2].iAllocMovable != KIndex2Alloc ||
sl@0
  4128
			gZoneUtilArray[aIndex].iAllocDiscardable || gZoneUtilArray[aIndex].iAllocFixed || 
sl@0
  4129
			gZoneUtilArray[aIndex2].iAllocDiscardable || gZoneUtilArray[aIndex2].iAllocFixed ||
sl@0
  4130
			freeInUsePages < KIndex2Alloc + aMaxPages)
sl@0
  4131
			{
sl@0
  4132
			test.Printf(_L("Insufficient memory - Skipping test...\n"));
sl@0
  4133
			r = KErrNoMemory;
sl@0
  4134
			goto error;
sl@0
  4135
			}
sl@0
  4136
sl@0
  4137
		// now reset all the flags
sl@0
  4138
		ResetRamZoneFlags();
sl@0
  4139
sl@0
  4140
		// Perform a general defrag
sl@0
  4141
		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, KTestMaxPages);
sl@0
  4142
		goto exit;
sl@0
  4143
		}
sl@0
  4144
		
sl@0
  4145
	if (aPageType == BEST_DISCARDABLE)
sl@0
  4146
		{
sl@0
  4147
sl@0
  4148
		TUint minCacheSize = 0;
sl@0
  4149
		TUint maxCacheSize = 0;
sl@0
  4150
		TUint currentCacheSize = 0;
sl@0
  4151
		DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
sl@0
  4152
		test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
sl@0
  4153
									minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift);
sl@0
  4154
					
sl@0
  4155
sl@0
  4156
	
sl@0
  4157
		// Allocate a number of discardable pages to the 2nd least preferable zone under test
sl@0
  4158
		TInt disPages;
sl@0
  4159
		r = ZoneAllocDiscard(aIndex2, KIndex2Alloc, disPages);
sl@0
  4160
		if (r != KErrNone)
sl@0
  4161
			{
sl@0
  4162
			test.Printf(_L("ZoneAllocDiscard() r = %d KIndex2Alloc = 0x%x disPages = 0x%x aIndex2 = %d - Skipping test...\n"),
sl@0
  4163
							r, KIndex2Alloc, disPages, aIndex2);
sl@0
  4164
			GetAllPageInfo();
sl@0
  4165
			PrintPageInfo();
sl@0
  4166
			r = KErrNoMemory;
sl@0
  4167
			goto error;
sl@0
  4168
			}
sl@0
  4169
		TUint discFillBytes = KIndex2Alloc << gPageShift;
sl@0
  4170
		r = DPTest::SetCacheSize(currentCacheSize + discFillBytes, currentCacheSize + discFillBytes);
sl@0
  4171
		if (r != KErrNone)
sl@0
  4172
			{
sl@0
  4173
			test.Printf(_L("SetCacheSize r = 0x%x currentCacheSize + discFillBytes = 0x%x - Skipping test...\n"),
sl@0
  4174
							r, currentCacheSize + discFillBytes);
sl@0
  4175
			GetAllPageInfo();
sl@0
  4176
			PrintPageInfo();
sl@0
  4177
			r = KErrNoMemory;
sl@0
  4178
			goto error;
sl@0
  4179
			}
sl@0
  4180
sl@0
  4181
	
sl@0
  4182
		// Allocate a discardable pages equal to aMaxPages to the least preferable zone under test
sl@0
  4183
		GetAllPageInfo();
sl@0
  4184
		if(aMaxPages >= gTotalPageCount.iFreePages)
sl@0
  4185
			{
sl@0
  4186
			test.Printf(_L("Insufficient memory available - Skipping test...\n"));
sl@0
  4187
			r = KErrNoMemory;
sl@0
  4188
			goto error;
sl@0
  4189
			}
sl@0
  4190
		TUint allocPages = aMaxPages + KIndex2Alloc;
sl@0
  4191
		r = ZoneAllocDiscard(aIndex, aMaxPages, disPages);
sl@0
  4192
		if (r != KErrNone)
sl@0
  4193
			{
sl@0
  4194
			test.Printf(_L("ZoneAllocDiscard() r = %d aMaxPages = 0x%x disPages = 0x%x aIndex = %d - Skipping test...\n"),
sl@0
  4195
							r, aMaxPages, disPages, aIndex);
sl@0
  4196
			GetAllPageInfo();
sl@0
  4197
			PrintPageInfo();
sl@0
  4198
			r = KErrNoMemory;
sl@0
  4199
			goto error;
sl@0
  4200
			}
sl@0
  4201
		discFillBytes = allocPages << gPageShift;
sl@0
  4202
		r = DPTest::SetCacheSize(currentCacheSize + discFillBytes, currentCacheSize + discFillBytes);
sl@0
  4203
		if (r != KErrNone)
sl@0
  4204
			{
sl@0
  4205
			test.Printf(_L("SetCacheSize r = %d currentCacheSize + discFillBytes = 0x%x - Skipping test...\n"),
sl@0
  4206
							r, currentCacheSize + discFillBytes);
sl@0
  4207
			GetAllPageInfo();
sl@0
  4208
			PrintPageInfo();
sl@0
  4209
			r = KErrNoMemory;
sl@0
  4210
			goto error;
sl@0
  4211
			}
sl@0
  4212
sl@0
  4213
sl@0
  4214
		// Determine how many free pages there are in the RAM zones more preferable
sl@0
  4215
		// than the RAM zones under test.
sl@0
  4216
		GetPrefOrder();
sl@0
  4217
		freeInUsePages = 0;
sl@0
  4218
		for (TUint i = 0; i < gZoneCount; i++)
sl@0
  4219
			{
sl@0
  4220
			TUint tmpIndex = gPrefArray[i];
sl@0
  4221
			if (tmpIndex == aIndex2)
sl@0
  4222
				break;
sl@0
  4223
			freeInUsePages += gZoneUtilArray[tmpIndex].iFreePages;
sl@0
  4224
			}
sl@0
  4225
		// Verify that the RAM layout is still suitable for the test.
sl@0
  4226
		if (r != KErrNone || gZoneUtilArray[aIndex].iAllocDiscardable != aMaxPages ||
sl@0
  4227
			gZoneUtilArray[aIndex2].iAllocDiscardable != KIndex2Alloc ||
sl@0
  4228
			gZoneUtilArray[aIndex].iAllocMovable || gZoneUtilArray[aIndex].iAllocFixed || 
sl@0
  4229
			gZoneUtilArray[aIndex2].iAllocMovable || gZoneUtilArray[aIndex2].iAllocFixed ||
sl@0
  4230
			freeInUsePages < KIndex2Alloc + aMaxPages)
sl@0
  4231
			{
sl@0
  4232
			test.Printf(_L("Insufficient memory - Skipping test...\n"));
sl@0
  4233
			r = KErrNoMemory;
sl@0
  4234
			goto error;
sl@0
  4235
			}
sl@0
  4236
sl@0
  4237
		// now reset all the flags
sl@0
  4238
		ResetRamZoneFlags();
sl@0
  4239
sl@0
  4240
		// Perform a general defrag with maxPages = KTestMaxPages
sl@0
  4241
		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, KTestMaxPages);
sl@0
  4242
		goto exit;
sl@0
  4243
sl@0
  4244
		}
sl@0
  4245
error:
sl@0
  4246
	// Reset all the flags
sl@0
  4247
	ResetRamZoneFlags();
sl@0
  4248
exit:
sl@0
  4249
	return r;
sl@0
  4250
	}
sl@0
  4251
sl@0
  4252
//
sl@0
  4253
// TestDefragRamMaxPages
sl@0
  4254
//
sl@0
  4255
//---------------------------------------------------------------------------------------------------------------------
sl@0
  4256
//! @SYMTestCaseID				KBASE-t_ramdefrag-0530
sl@0
  4257
//! @SYMTestType				CIT
sl@0
  4258
//! @SYMTestCaseDesc			Verifying the implementation of the function TRamDefragRequest::DefragRam()
sl@0
  4259
//! @SYMPREQ					PREQ308
sl@0
  4260
//! @SYMTestPriority			High
sl@0
  4261
//! @SYMTestActions				
sl@0
  4262
//! 	1.	Call function with invalid aMaxPages 
sl@0
  4263
//! 	2.	Call DefragRam when aMaxPages < number of movable pages in least preferable zone 
sl@0
  4264
//! 	3.	Call DefragRam when aMaxPages > number of movable pages in least preferable zone
sl@0
  4265
//! 	4.	Call DefragRam when aMaxPages = number of movable pages in least preferable zone
sl@0
  4266
//! 	5.	Call DefragRam when aMaxPages < number of discardable pages in least preferable zone 
sl@0
  4267
//! 	6.	Call DefragRam when aMaxPages > number of discardable pages in least preferable zone
sl@0
  4268
//! 	7.	Call DefragRam when aMaxPages = number of discardable pages in least preferable zone
sl@0
  4269
//! 
sl@0
  4270
//! @SYMTestExpectedResults
sl@0
  4271
//! 	1.	KErrArgument
sl@0
  4272
//! 	2.	Least preferable zone and 2nd least preferable zone have not been emptied
sl@0
  4273
//! 	3.	Least preferable zone has been emptied and 2nd least preferable zone has not been emptied
sl@0
  4274
//! 	4.	Least preferable zone has been emptied and 2nd least preferable zone not not been emptied
sl@0
  4275
//! 	5.	Least preferable zone and 2nd least preferable zone have not been emptied
sl@0
  4276
//! 	6.	Least preferable zone has been emptied and 2nd least preferable zone has not been emptied
sl@0
  4277
//! 	7.	Least preferable zone has been emptied and 2nd least preferable zone not not been emptied
sl@0
  4278
//---------------------------------------------------------------------------------------------------------------------
sl@0
  4279
TInt TestDefragRamMaxPages()
sl@0
  4280
	{
sl@0
  4281
	test.Start(_L("Test1: Call DefragRam with invalid aMaxPages "));	
sl@0
  4282
	TestStart();	
sl@0
  4283
	
sl@0
  4284
	gDefragMaxPages = -1;
sl@0
  4285
	
sl@0
  4286
	TInt r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
sl@0
  4287
	TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
sl@0
  4288
	if (r != KErrArgument)
sl@0
  4289
		{
sl@0
  4290
		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
sl@0
  4291
		TEST_FAIL;
sl@0
  4292
		}
sl@0
  4293
	else
sl@0
  4294
		{
sl@0
  4295
		test.Printf(_L("Passed...\n"));	
sl@0
  4296
		}	
sl@0
  4297
	TestEnd();
sl@0
  4298
	
sl@0
  4299
	test.Next(_L("Test2: Call DefragRam aMaxPages < number of movable pages in least preferable zone "));	
sl@0
  4300
	TestStart();	
sl@0
  4301
	gChunkArray1 = new RChunk;
sl@0
  4302
	gChunkArraySize1 = 1;
sl@0
  4303
	gChunkArray2 = new RChunk;
sl@0
  4304
	gChunkArraySize2 = 1;
sl@0
  4305
	TUint index = 0;
sl@0
  4306
	TUint index2 = 0;
sl@0
  4307
	r = DefragMaxPagesSetup(BEST_MOVABLE, KTestMaxPages + 1, index, index2);	
sl@0
  4308
	if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
sl@0
  4309
		{// Cannot perform test with fixed pages in least preferable zones
sl@0
  4310
		test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
sl@0
  4311
		}
sl@0
  4312
	else
sl@0
  4313
		{
sl@0
  4314
		if (r == KErrNone)
sl@0
  4315
			{
sl@0
  4316
			GetAllPageInfo();
sl@0
  4317
			if (gZoneUtilArray[index].iAllocMovable < KTestMaxPages + 1 ||
sl@0
  4318
				gZoneUtilArray[index2].iAllocMovable < KIndex2Alloc)
sl@0
  4319
				{
sl@0
  4320
				test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
sl@0
  4321
										index, KTestMaxPages + 1, index2, KIndex2Alloc);
sl@0
  4322
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  4323
				CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
sl@0
  4324
				CLEANUP(Ldd.FreeFromAllZones());
sl@0
  4325
				TEST_FAIL;
sl@0
  4326
				}
sl@0
  4327
sl@0
  4328
			// Double check that if DefragRam is called with maxPages = 0, these zones are emptied
sl@0
  4329
			// if there is free space elsewhere
sl@0
  4330
			r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
sl@0
  4331
			GetAllPageInfo();
sl@0
  4332
			if (gTotalPageCount.iFreePages > KIndex2Alloc + KTestMaxPages + 1 &&
sl@0
  4333
				(gZoneUtilArray[index].iAllocMovable != 0 || gZoneUtilArray[index2].iAllocMovable != 0))
sl@0
  4334
				{
sl@0
  4335
				test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
sl@0
  4336
										index, 0, index2, 0);
sl@0
  4337
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  4338
				CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
sl@0
  4339
				CLEANUP(Ldd.FreeFromAllZones());
sl@0
  4340
				TEST_FAIL;
sl@0
  4341
				}
sl@0
  4342
			else
sl@0
  4343
				{
sl@0
  4344
				test.Printf(_L("Passed...\n"));
sl@0
  4345
				}
sl@0
  4346
			}
sl@0
  4347
		else 
sl@0
  4348
			{
sl@0
  4349
			test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
sl@0
  4350
			}
sl@0
  4351
		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  4352
		RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
sl@0
  4353
		}
sl@0
  4354
	Ldd.FreeFromAllZones();
sl@0
  4355
	TestEnd();
sl@0
  4356
sl@0
  4357
sl@0
  4358
	test.Next(_L("Test3: Call DefragRam aMaxPages > number of movable pages in least preferable zone "));	
sl@0
  4359
	TestStart();	
sl@0
  4360
	gChunkArray1 = new RChunk;
sl@0
  4361
	gChunkArraySize1 = 1;
sl@0
  4362
	gChunkArray2 = new RChunk;
sl@0
  4363
	gChunkArraySize2 = 1;
sl@0
  4364
	r = DefragMaxPagesSetup(BEST_MOVABLE, KTestMaxPages - 1, index, index2);
sl@0
  4365
	if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
sl@0
  4366
		{// Cannot perform test with fixed pages in least preferable zones
sl@0
  4367
		test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
sl@0
  4368
		}
sl@0
  4369
	else
sl@0
  4370
		{		
sl@0
  4371
		if (r == KErrNone)
sl@0
  4372
			{
sl@0
  4373
			GetAllPageInfo();
sl@0
  4374
			if (gZoneUtilArray[index].iAllocMovable != 0)
sl@0
  4375
				{
sl@0
  4376
				test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
sl@0
  4377
										index, 0, index2, KIndex2Alloc);
sl@0
  4378
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  4379
				CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
sl@0
  4380
				CLEANUP(Ldd.FreeFromAllZones());
sl@0
  4381
				TEST_FAIL;		
sl@0
  4382
				}
sl@0
  4383
			
sl@0
  4384
			// Double check that if DefragRam is called with maxPages = 0, these zones are emptied
sl@0
  4385
			// if there is free space elsewhere
sl@0
  4386
			r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
sl@0
  4387
			
sl@0
  4388
			GetAllPageInfo();
sl@0
  4389
			if (gTotalPageCount.iFreePages > KIndex2Alloc + KTestMaxPages - 1 &&
sl@0
  4390
				(gZoneUtilArray[index].iAllocMovable != 0 || gZoneUtilArray[index2].iAllocMovable != 0))
sl@0
  4391
				{
sl@0
  4392
				test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
sl@0
  4393
										index, 0, index2, 0);
sl@0
  4394
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  4395
				CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
sl@0
  4396
				CLEANUP(Ldd.FreeFromAllZones());
sl@0
  4397
				TEST_FAIL;		
sl@0
  4398
				}
sl@0
  4399
			else
sl@0
  4400
				{
sl@0
  4401
				test.Printf(_L("Passed...\n"));
sl@0
  4402
				}
sl@0
  4403
			}
sl@0
  4404
		else 
sl@0
  4405
			{
sl@0
  4406
			test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
sl@0
  4407
			}	
sl@0
  4408
		
sl@0
  4409
		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  4410
		RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
sl@0
  4411
		}
sl@0
  4412
	Ldd.FreeFromAllZones();
sl@0
  4413
	TestEnd();
sl@0
  4414
sl@0
  4415
sl@0
  4416
	test.Next(_L("Test4: Call DefragRam aMaxPages = number of movable pages in least preferable zone "));	
sl@0
  4417
	TestStart();	
sl@0
  4418
	gChunkArray1 = new RChunk;
sl@0
  4419
	gChunkArraySize1 = 1;
sl@0
  4420
	gChunkArray2 = new RChunk;
sl@0
  4421
	gChunkArraySize2 = 1;
sl@0
  4422
	r = DefragMaxPagesSetup(BEST_MOVABLE, KTestMaxPages, index, index2);
sl@0
  4423
	if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
sl@0
  4424
		{// Cannot perform test with fixed pages in least preferable zones
sl@0
  4425
		test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
sl@0
  4426
		}
sl@0
  4427
	else
sl@0
  4428
		{		
sl@0
  4429
		if (r == KErrNone)
sl@0
  4430
			{
sl@0
  4431
			GetAllPageInfo();
sl@0
  4432
			if (gZoneUtilArray[index].iAllocMovable != 0)
sl@0
  4433
				{
sl@0
  4434
				test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
sl@0
  4435
										index, 0, index2, KIndex2Alloc);
sl@0
  4436
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  4437
				CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
sl@0
  4438
				CLEANUP(Ldd.FreeFromAllZones());
sl@0
  4439
				TEST_FAIL;		
sl@0
  4440
				}
sl@0
  4441
sl@0
  4442
			// Double check that if DefragRam is called with maxPages = 0, these zones are emptied
sl@0
  4443
			// if there is free space elsewhere
sl@0
  4444
			r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
sl@0
  4445
			
sl@0
  4446
			GetAllPageInfo();
sl@0
  4447
			if (gTotalPageCount.iFreePages > KIndex2Alloc + KTestMaxPages &&
sl@0
  4448
				(gZoneUtilArray[index].iAllocMovable != 0 || gZoneUtilArray[index2].iAllocMovable != 0))
sl@0
  4449
				{
sl@0
  4450
				test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
sl@0
  4451
										index, 0, index2, 0);
sl@0
  4452
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  4453
				CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
sl@0
  4454
				CLEANUP(Ldd.FreeFromAllZones());
sl@0
  4455
				TEST_FAIL;		
sl@0
  4456
				}
sl@0
  4457
			else 
sl@0
  4458
				{
sl@0
  4459
				test.Printf(_L("Passed...\n"));
sl@0
  4460
				}
sl@0
  4461
			}
sl@0
  4462
		else 
sl@0
  4463
			{
sl@0
  4464
			test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
sl@0
  4465
			}
sl@0
  4466
		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  4467
		RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
sl@0
  4468
		}
sl@0
  4469
	Ldd.FreeFromAllZones();
sl@0
  4470
	TestEnd();
sl@0
  4471
	
sl@0
  4472
	test.Next(_L("Test5: Call DefragRam aMaxPages < number of discardable pages in least preferable zone "));	
sl@0
  4473
	TestStart();	
sl@0
  4474
	if (gPagedRom)
sl@0
  4475
		{
sl@0
  4476
		r = DefragMaxPagesSetup(BEST_DISCARDABLE, KTestMaxPages + 1, index, index2);
sl@0
  4477
		if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
sl@0
  4478
			{// Cannot perform test with fixed pages in least preferable zones
sl@0
  4479
			test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
sl@0
  4480
			}
sl@0
  4481
		else
sl@0
  4482
			{
sl@0
  4483
			if (r == KErrNone)
sl@0
  4484
				{
sl@0
  4485
				GetAllPageInfo();
sl@0
  4486
				if (gZoneUtilArray[index].iAllocDiscardable < KTestMaxPages + 1 ||
sl@0
  4487
					gZoneUtilArray[index2].iAllocDiscardable < KIndex2Alloc)
sl@0
  4488
					{
sl@0
  4489
					test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
sl@0
  4490
											index, KTestMaxPages + 1, index2, KIndex2Alloc);
sl@0
  4491
					CLEANUP(ResetDPCache());
sl@0
  4492
					CLEANUP(Ldd.FreeFromAllZones());
sl@0
  4493
					TEST_FAIL;		
sl@0
  4494
					}
sl@0
  4495
				
sl@0
  4496
				// Double check that if DefragRam is called with maxPages = 0, these zones are emptied
sl@0
  4497
				r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
sl@0
  4498
				
sl@0
  4499
				GetAllPageInfo();
sl@0
  4500
				if (gZoneUtilArray[index].iAllocDiscardable != 0 ||
sl@0
  4501
					gZoneUtilArray[index2].iAllocDiscardable != 0)
sl@0
  4502
					{
sl@0
  4503
					test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
sl@0
  4504
											index, 0, index2, 0);
sl@0
  4505
					CLEANUP(ResetDPCache());
sl@0
  4506
					CLEANUP(Ldd.FreeFromAllZones());
sl@0
  4507
					TEST_FAIL;		
sl@0
  4508
					}
sl@0
  4509
				else
sl@0
  4510
					{
sl@0
  4511
					test.Printf(_L("Passed...\n"));
sl@0
  4512
					}
sl@0
  4513
				}
sl@0
  4514
			else 
sl@0
  4515
				{
sl@0
  4516
				test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
sl@0
  4517
				}
sl@0
  4518
			ResetDPCache();
sl@0
  4519
			}
sl@0
  4520
		Ldd.FreeFromAllZones();
sl@0
  4521
		}
sl@0
  4522
	else
sl@0
  4523
		{
sl@0
  4524
		test.Printf(_L("Not a Paged Rom - Skipping...\n"));
sl@0
  4525
		}
sl@0
  4526
	TestEnd();
sl@0
  4527
sl@0
  4528
	test.Next(_L("Test6: Call DefragRam aMaxPages > number of discardable pages in least preferable zone "));	
sl@0
  4529
	TestStart();	
sl@0
  4530
	
sl@0
  4531
	if (gPagedRom)
sl@0
  4532
		{
sl@0
  4533
		r = DefragMaxPagesSetup(BEST_DISCARDABLE, KTestMaxPages - 1, index, index2);
sl@0
  4534
		
sl@0
  4535
		if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
sl@0
  4536
			{// Cannot perform test with fixed pages in least preferable zones
sl@0
  4537
			test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
sl@0
  4538
			}
sl@0
  4539
		else
sl@0
  4540
			{	
sl@0
  4541
			if (r == KErrNone)
sl@0
  4542
				{
sl@0
  4543
				GetAllPageInfo();
sl@0
  4544
				if (gZoneUtilArray[index].iAllocDiscardable != 0||
sl@0
  4545
					gZoneUtilArray[index2].iAllocDiscardable != KIndex2Alloc)
sl@0
  4546
					{
sl@0
  4547
					test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
sl@0
  4548
											index, 0, index2, KIndex2Alloc);
sl@0
  4549
					CLEANUP(ResetDPCache());
sl@0
  4550
					CLEANUP(Ldd.FreeFromAllZones());
sl@0
  4551
					TEST_FAIL;		
sl@0
  4552
					}
sl@0
  4553
				
sl@0
  4554
				// Double check that if DefragRam is called with maxPages = 0, these zones are emptied
sl@0
  4555
				r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
sl@0
  4556
				GetAllPageInfo();
sl@0
  4557
				if (gZoneUtilArray[index].iAllocDiscardable != 0||
sl@0
  4558
					gZoneUtilArray[index2].iAllocDiscardable != 0)
sl@0
  4559
					{
sl@0
  4560
					test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
sl@0
  4561
											index, 0, index2, 0);
sl@0
  4562
					CLEANUP(ResetDPCache());
sl@0
  4563
					CLEANUP(Ldd.FreeFromAllZones());
sl@0
  4564
					TEST_FAIL;		
sl@0
  4565
					}
sl@0
  4566
				else
sl@0
  4567
					{
sl@0
  4568
					test.Printf(_L("Passed...\n"));
sl@0
  4569
					}
sl@0
  4570
				}
sl@0
  4571
			else 
sl@0
  4572
				{
sl@0
  4573
				test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
sl@0
  4574
				}	
sl@0
  4575
			ResetDPCache();
sl@0
  4576
			}
sl@0
  4577
		Ldd.FreeFromAllZones();
sl@0
  4578
		}
sl@0
  4579
	else
sl@0
  4580
		{
sl@0
  4581
		test.Printf(_L("Not a Paged Rom - Skipping...\n"));
sl@0
  4582
		}
sl@0
  4583
	TestEnd();
sl@0
  4584
sl@0
  4585
sl@0
  4586
	test.Next(_L("Test4: Call DefragRam aMaxPages = number of discardable pages in least preferable zone "));	
sl@0
  4587
	TestStart();	
sl@0
  4588
	
sl@0
  4589
	if(gPagedRom)
sl@0
  4590
		{
sl@0
  4591
		r = DefragMaxPagesSetup(BEST_DISCARDABLE, KTestMaxPages, index, index2);
sl@0
  4592
		
sl@0
  4593
		if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
sl@0
  4594
			{// Cannot perform test with fixed pages in least preferable zones
sl@0
  4595
			test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
sl@0
  4596
			}
sl@0
  4597
		else
sl@0
  4598
			{	
sl@0
  4599
			if (r == KErrNone)
sl@0
  4600
				{
sl@0
  4601
				GetAllPageInfo();
sl@0
  4602
				if (gZoneUtilArray[index].iAllocDiscardable != 0||
sl@0
  4603
					gZoneUtilArray[index2].iAllocDiscardable != KIndex2Alloc)
sl@0
  4604
					{
sl@0
  4605
					test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
sl@0
  4606
											index, 0, index2, KIndex2Alloc);
sl@0
  4607
					CLEANUP(ResetDPCache());
sl@0
  4608
					CLEANUP(Ldd.FreeFromAllZones());
sl@0
  4609
					TEST_FAIL;		
sl@0
  4610
					}
sl@0
  4611
sl@0
  4612
				// Double check that if DefragRam is called with maxPages = 0, these zones are emptied
sl@0
  4613
				r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
sl@0
  4614
				GetAllPageInfo();
sl@0
  4615
				if (gZoneUtilArray[index].iAllocDiscardable != 0||
sl@0
  4616
					gZoneUtilArray[index2].iAllocDiscardable != 0)
sl@0
  4617
					{
sl@0
  4618
					test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
sl@0
  4619
											index, 0, index2, 0);
sl@0
  4620
					CLEANUP(ResetDPCache());
sl@0
  4621
					CLEANUP(Ldd.FreeFromAllZones());
sl@0
  4622
					TEST_FAIL;		
sl@0
  4623
					}
sl@0
  4624
				else
sl@0
  4625
					{
sl@0
  4626
					test.Printf(_L("Passed...\n"));
sl@0
  4627
					}
sl@0
  4628
				}
sl@0
  4629
			else 
sl@0
  4630
				{
sl@0
  4631
				test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
sl@0
  4632
				}
sl@0
  4633
			ResetDPCache();
sl@0
  4634
			}
sl@0
  4635
		Ldd.FreeFromAllZones();
sl@0
  4636
		}
sl@0
  4637
	else
sl@0
  4638
		{
sl@0
  4639
		test.Printf(_L("Not a Paged Rom - Skipping...\n"));
sl@0
  4640
		}
sl@0
  4641
	TestEnd();
sl@0
  4642
sl@0
  4643
	test.End();
sl@0
  4644
	return KErrNone;
sl@0
  4645
	}
sl@0
  4646
sl@0
  4647
sl@0
  4648
//
sl@0
  4649
// TestEmptyRamZone
sl@0
  4650
//
sl@0
  4651
//---------------------------------------------------------------------------------------------------------------------
sl@0
  4652
//! @SYMTestCaseID				KBASE-t_ramdefrag-0531
sl@0
  4653
//! @SYMTestType				CIT
sl@0
  4654
//! @SYMTestCaseDesc			Verifying the implementation of the function TRamDefragRequest::EmptyRamZone()
sl@0
  4655
//! @SYMPREQ					PREQ308
sl@0
  4656
//! @SYMTestPriority			High
sl@0
  4657
//! @SYMTestActions				
sl@0
  4658
//! 	1.	Fragment the memory. Call function with a valid aID, 
sl@0
  4659
//! 		i.e. a zone which lies within the range of zones. 
sl@0
  4660
//! 	2.	Fragment the memory. Call function with an invalid aID
sl@0
  4661
//! 		i.e. a zone which does not exist within the range of zones. 
sl@0
  4662
//! 	3.	Fragment the memory. Following this, call a zone specific defrag on a particular zone. 
sl@0
  4663
//! 		Call the zone specific defrag again on the same zone whilst the other defrag is still 
sl@0
  4664
//!			running on that zone. 
sl@0
  4665
//! 	4.	Fragment the memory. Following this, call the TRamDefragRequest::DefragRam() to perform general defrag. 
sl@0
  4666
//! 		Following this call zone specific defrag whilst the general defrag is still running. 
sl@0
  4667
//! 	5.	Fragment the memory. Following this, call a zone specific defrag on a particular zone. 
sl@0
  4668
//! 		Whilst the zone defrag is running, call a general defrag
sl@0
  4669
//! 	6:  Fragment the memory. Following this, call the function on specifc zone and 
sl@0
  4670
//! 		at the same time allocate pages to the zone
sl@0
  4671
//! 	7.	Fragment the memory. Call EmptyRamZone() on every zone one after the other. 
sl@0
  4672
//! 	8.	Fragment the memory. Allocate a couple of fixed pages to a zone that contains movable pages. 
sl@0
  4673
//! 		Following this, Call EmptyRamZone() on that zone. 
sl@0
  4674
//! 
sl@0
  4675
//! @SYMTestExpectedResults
sl@0
  4676
//! 	1.	KErrNone
sl@0
  4677
//! 	2.	KErrArgument
sl@0
  4678
//! 	3.	KErrNone and the zone has been emptied
sl@0
  4679
//! 	4.	Zone has not been emptied, as the general defrag has already placed pages in the zone
sl@0
  4680
//! 	5.	Zone has been emptied
sl@0
  4681
//! 	6.	KErrNone and pages have been allocated in the zone
sl@0
  4682
//! 	7.	One of the following scenarios should occur:
sl@0
  4683
//!			a.	If the cache has not reached its minimum size, then all the movable pages should 
sl@0
  4684
//!				be moved from the zone if there are enough free pages in the other zones. 
sl@0
  4685
//!				However, when demand paging is off, all movable pages should be moved from the zone, again if there 
sl@0
  4686
//!				are enough free pages in the other zones. 
sl@0
  4687
//!			b.	If the current size of the cache minus the number of discardable pages in the zone being emptied is 
sl@0
  4688
//!				not less than the minimum cache size, then all the discardable pages should be discarded of. 
sl@0
  4689
//!				However, when demand paging is off, all discardable pages should be removed from 
sl@0
  4690
//!				the zone. 
sl@0
  4691
//! 	8.	KErrNoMemory, however all movable and discardable pages have been moved from the zone 
sl@0
  4692
//---------------------------------------------------------------------------------------------------------------------
sl@0
  4693
TInt TestEmptyRamZone()
sl@0
  4694
	{
sl@0
  4695
	TInt r = KErrNone;
sl@0
  4696
	TInt r2 = KErrNone;
sl@0
  4697
sl@0
  4698
	test.Start(_L("Test1: Call EmptyRamZone with valid aID "));	
sl@0
  4699
	TestStart();	
sl@0
  4700
	
sl@0
  4701
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  4702
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  4703
	
sl@0
  4704
	GetAllPageInfo();
sl@0
  4705
	TUint index = gZoneCount - 1;
sl@0
  4706
	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
sl@0
  4707
		{
sl@0
  4708
		-- index;
sl@0
  4709
		}
sl@0
  4710
	TUint defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  4711
	test.Printf(_L("Zone ID = %d\n"), defragZoneID);
sl@0
  4712
	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
sl@0
  4713
		{
sl@0
  4714
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  4715
		}
sl@0
  4716
	else
sl@0
  4717
		{
sl@0
  4718
		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
sl@0
  4719
		GetAllPageInfo();
sl@0
  4720
		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  4721
		if (freeInOthers && (r != KErrNone || !CheckZoneIsOff(index)))
sl@0
  4722
			{
sl@0
  4723
			test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
sl@0
  4724
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  4725
			TEST_FAIL;
sl@0
  4726
			}
sl@0
  4727
		else
sl@0
  4728
			{
sl@0
  4729
			test.Printf(_L("Passed...\n"));	
sl@0
  4730
			}
sl@0
  4731
		}
sl@0
  4732
sl@0
  4733
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
sl@0
  4734
	TestEnd();
sl@0
  4735
	
sl@0
  4736
	
sl@0
  4737
	test.Next(_L("Test2: Call EmptyRamZone with invalid aID "));	
sl@0
  4738
	TestStart();	
sl@0
  4739
	gChunkArray1 = new RChunk;
sl@0
  4740
	gChunkArraySize1 = 1;
sl@0
  4741
	defragZoneID = KInvalidZoneID; 
sl@0
  4742
sl@0
  4743
	r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
sl@0
  4744
	
sl@0
  4745
	if (r != KErrArgument)
sl@0
  4746
		{
sl@0
  4747
		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
sl@0
  4748
		TEST_FAIL;
sl@0
  4749
		}
sl@0
  4750
	else
sl@0
  4751
		{
sl@0
  4752
		test.Printf(_L("Passed...\n"));	
sl@0
  4753
		}
sl@0
  4754
	
sl@0
  4755
	TestEnd();
sl@0
  4756
	
sl@0
  4757
	
sl@0
  4758
	test.Next(_L("Test3: Call EmptyRamZone twice at the same time "));	
sl@0
  4759
	TestStart();	
sl@0
  4760
	
sl@0
  4761
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  4762
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  4763
sl@0
  4764
	GetAllPageInfo();
sl@0
  4765
	index = gZoneCount - 1;
sl@0
  4766
	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
sl@0
  4767
		{
sl@0
  4768
		-- index;
sl@0
  4769
		}
sl@0
  4770
	defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  4771
	
sl@0
  4772
	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
sl@0
  4773
		{
sl@0
  4774
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  4775
		}
sl@0
  4776
	else
sl@0
  4777
		{
sl@0
  4778
		r = gTestThread.Create(gTestThreadName,MultiEmptyZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny *)defragZoneID);
sl@0
  4779
		if (r != KErrNone)
sl@0
  4780
			{
sl@0
  4781
			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  4782
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  4783
			TEST_FAIL;
sl@0
  4784
			}		
sl@0
  4785
				
sl@0
  4786
		gTestThread.Logon(status);
sl@0
  4787
		gTestThread.Resume();
sl@0
  4788
		
sl@0
  4789
		User::After(10);
sl@0
  4790
sl@0
  4791
		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
sl@0
  4792
		
sl@0
  4793
		User::WaitForRequest(status);
sl@0
  4794
		r2 = status.Int();
sl@0
  4795
sl@0
  4796
		GetAllPageInfo();
sl@0
  4797
		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  4798
		if (r != r2 || 
sl@0
  4799
			(freeInOthers && (r != KErrNone || r2 != KErrNone || !CheckZoneIsOff(index))))
sl@0
  4800
			{
sl@0
  4801
			test.Printf(_L("Fail: r = %d, r2 = %d, expected = %d, or zone %d has not been emptied\n"), 
sl@0
  4802
									r, r2, KErrNone, defragZoneID);
sl@0
  4803
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));	
sl@0
  4804
			TEST_FAIL;
sl@0
  4805
			}	
sl@0
  4806
		else
sl@0
  4807
			{
sl@0
  4808
			test.Printf(_L("Passed...\n"));	
sl@0
  4809
			}
sl@0
  4810
		
sl@0
  4811
		gTestThread.Close();	
sl@0
  4812
		}
sl@0
  4813
	
sl@0
  4814
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
sl@0
  4815
	TestEnd();
sl@0
  4816
sl@0
  4817
		
sl@0
  4818
	test.Next(_L("Test4: Call general defrag and zone defrag at the same time "));	
sl@0
  4819
	TestStart();	
sl@0
  4820
		
sl@0
  4821
	TInt waitTime = 10000;
sl@0
  4822
	
sl@0
  4823
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  4824
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  4825
sl@0
  4826
	GetAllPageInfo();
sl@0
  4827
	index = gZoneCount - 1;
sl@0
  4828
	while (index > 0 && gZoneUtilArray[index].iAllocFixed > 0)
sl@0
  4829
		{
sl@0
  4830
		index--;
sl@0
  4831
		}
sl@0
  4832
	defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  4833
	
sl@0
  4834
	if (index == 0 && gZoneUtilArray[index].iAllocFixed > 0)
sl@0
  4835
		{
sl@0
  4836
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  4837
		}
sl@0
  4838
	else
sl@0
  4839
		{
sl@0
  4840
		r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
sl@0
  4841
		if (r != KErrNone)
sl@0
  4842
			{
sl@0
  4843
			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  4844
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  4845
			TEST_FAIL;
sl@0
  4846
			}		
sl@0
  4847
		gTestThread.Logon(status);
sl@0
  4848
				
sl@0
  4849
		gTestThread.Resume();
sl@0
  4850
		User::After(waitTime);
sl@0
  4851
		
sl@0
  4852
		TESTDEBUG(test.Printf(_L("Zone defrag running on main thread\n")));
sl@0
  4853
sl@0
  4854
		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
sl@0
  4855
sl@0
  4856
		User::WaitForRequest(status);
sl@0
  4857
		r2 = status.Int();
sl@0
  4858
sl@0
  4859
		GetAllPageInfo();
sl@0
  4860
		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  4861
		if (r2 != KErrNone || 
sl@0
  4862
			(freeInOthers && (r != KErrNone || !CheckZoneIsOff(index))))
sl@0
  4863
			{
sl@0
  4864
			test.Printf(_L("Fail: r = %d, r2 = %d, expected = %d, or zone %d is on unexpectedly\n"),
sl@0
  4865
							r, r2, KErrNone, defragZoneID);
sl@0
  4866
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));	
sl@0
  4867
			TEST_FAIL;
sl@0
  4868
			}	
sl@0
  4869
		else
sl@0
  4870
			{
sl@0
  4871
			test.Printf(_L("Passed...\n"));	
sl@0
  4872
			}	
sl@0
  4873
sl@0
  4874
		gTestThread.Close();	
sl@0
  4875
		}
sl@0
  4876
sl@0
  4877
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  4878
	TestEnd();
sl@0
  4879
	
sl@0
  4880
	
sl@0
  4881
	test.Next(_L("Test5: Call zone defrag and general defrag at the same time "));	
sl@0
  4882
	TestStart();	
sl@0
  4883
	
sl@0
  4884
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  4885
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  4886
sl@0
  4887
	GetAllPageInfo();
sl@0
  4888
	index = gZoneCount - 1;
sl@0
  4889
	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
sl@0
  4890
		{
sl@0
  4891
		-- index;
sl@0
  4892
		}
sl@0
  4893
	defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  4894
sl@0
  4895
	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
sl@0
  4896
		{
sl@0
  4897
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  4898
		}
sl@0
  4899
	else
sl@0
  4900
		{
sl@0
  4901
		r = gTestThread.Create(gTestThreadName,MultiEmptyZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)defragZoneID);
sl@0
  4902
		if (r != KErrNone)
sl@0
  4903
			{
sl@0
  4904
			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  4905
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  4906
			TEST_FAIL;
sl@0
  4907
			}		
sl@0
  4908
		gTestThread.Logon(status);
sl@0
  4909
				
sl@0
  4910
		gTestThread.Resume();
sl@0
  4911
		User::After(0);
sl@0
  4912
		
sl@0
  4913
		TESTDEBUG(test.Printf(_L("Zone defrag running on main thread\n")));
sl@0
  4914
		
sl@0
  4915
		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
sl@0
  4916
sl@0
  4917
		User::WaitForRequest(status);
sl@0
  4918
		r2 = status.Int();
sl@0
  4919
sl@0
  4920
		GetAllPageInfo();
sl@0
  4921
		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  4922
		if (r != KErrNone ||
sl@0
  4923
			(freeInOthers && (r2 != KErrNone || !CheckZoneIsOff(index))))
sl@0
  4924
			{
sl@0
  4925
			test.Printf(_L("Fail: r = %d, r2 = %d, expected = %d, or zone 0x%x is on unexpectedly\n"),
sl@0
  4926
							r, r2, KErrNone, defragZoneID);
sl@0
  4927
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  4928
			TEST_FAIL;
sl@0
  4929
			}
sl@0
  4930
		else
sl@0
  4931
			{
sl@0
  4932
			test.Printf(_L("Passed...\n"));	
sl@0
  4933
			}
sl@0
  4934
sl@0
  4935
		gTestThread.Close();	
sl@0
  4936
		}
sl@0
  4937
			
sl@0
  4938
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  4939
	TestEnd();
sl@0
  4940
sl@0
  4941
sl@0
  4942
	test.Next(_L("Test6: Call function TRamDefragRequest::EmptyRamZone on specifc zone and at the same time allocate pages to the zone"));	
sl@0
  4943
	TestStart();	
sl@0
  4944
	
sl@0
  4945
	TInt pagesAlloc = 1; // Try and allocate just one page whilst trying to empty the zone. 
sl@0
  4946
sl@0
  4947
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  4948
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  4949
sl@0
  4950
	GetAllPageInfo();
sl@0
  4951
	index = gZoneCount - 1;
sl@0
  4952
	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
sl@0
  4953
		{
sl@0
  4954
		-- index;
sl@0
  4955
		}
sl@0
  4956
	defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  4957
	
sl@0
  4958
	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
sl@0
  4959
		{
sl@0
  4960
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  4961
		}
sl@0
  4962
	else
sl@0
  4963
		{
sl@0
  4964
		TInt numPassed = 0;
sl@0
  4965
		for (waitTime = 1000; waitTime > 0; waitTime-=10)
sl@0
  4966
			{
sl@0
  4967
			r = gTestThread.Create(gTestThreadName,MultiEmptyZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)defragZoneID);
sl@0
  4968
			if (r != KErrNone)
sl@0
  4969
				{
sl@0
  4970
				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  4971
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  4972
				TEST_FAIL;
sl@0
  4973
				}
sl@0
  4974
			gTestThread.Logon(status);
sl@0
  4975
			
sl@0
  4976
			gTestThread.Resume();
sl@0
  4977
			User::After(waitTime);
sl@0
  4978
			
sl@0
  4979
			r = Ldd.ZoneAllocDiscontiguous(defragZoneID, pagesAlloc);
sl@0
  4980
sl@0
  4981
			User::WaitForRequest(status);
sl@0
  4982
			r2 = status.Int();
sl@0
  4983
			
sl@0
  4984
			if (r2 != KErrNone)
sl@0
  4985
				{
sl@0
  4986
				test.Printf(_L("Empty was unsuccsessful: r2 = %d, expected = %d\n"), r2, KErrNone);
sl@0
  4987
				}
sl@0
  4988
			else if (r != KErrNone)
sl@0
  4989
				{
sl@0
  4990
				test.Printf(_L("Fail: r = %d, expected = %d, r2 = %d, expected = %d, zone = 0x%x\n"), 
sl@0
  4991
										r, KErrNone, r2, KErrNone, defragZoneID);
sl@0
  4992
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  4993
				CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  4994
				TEST_FAIL;
sl@0
  4995
				}
sl@0
  4996
			else
sl@0
  4997
				{
sl@0
  4998
				numPassed ++;
sl@0
  4999
				}
sl@0
  5000
							
sl@0
  5001
			Ldd.FreeAllFixedPages();
sl@0
  5002
sl@0
  5003
			gTestThread.Close();	
sl@0
  5004
			}
sl@0
  5005
		if (numPassed > 0)
sl@0
  5006
			{
sl@0
  5007
			test.Printf(_L("Passed...\n"));	
sl@0
  5008
			}
sl@0
  5009
		}
sl@0
  5010
			
sl@0
  5011
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  5012
	TestEnd();
sl@0
  5013
sl@0
  5014
sl@0
  5015
	test.Next(_L("Test7: Call function TRamDefragRequest::EmptyRamZone on every zone one after the other"));	
sl@0
  5016
	TestStart();
sl@0
  5017
		
sl@0
  5018
	Ldd.ResetDriver();
sl@0
  5019
			
sl@0
  5020
	for (index = 0; index < gZoneCount; index ++)
sl@0
  5021
		{
sl@0
  5022
		// Variables for DP ROM cache sizes
sl@0
  5023
		TUint minCacheSize = 0;
sl@0
  5024
		TUint maxCacheSize = 0;
sl@0
  5025
		TUint currentCacheSize = 0;
sl@0
  5026
sl@0
  5027
		if (gPagedRom)
sl@0
  5028
			{
sl@0
  5029
			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
sl@0
  5030
			TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
sl@0
  5031
										minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
sl@0
  5032
			// Calculate the maximum size available for the DP cache.
sl@0
  5033
			TUint minZoneFree = KMaxTUint;
sl@0
  5034
			TUint maxDisFillBytes = 0;
sl@0
  5035
			GetAllPageInfo();
sl@0
  5036
			for (TUint i = 0; i < gZoneCount; i++)
sl@0
  5037
				{
sl@0
  5038
				maxDisFillBytes += gZoneUtilArray[i].iFreePages;
sl@0
  5039
				maxDisFillBytes += gZoneUtilArray[i].iAllocDiscardable;
sl@0
  5040
				if (minZoneFree > gZoneUtilArray[i].iFreePages)
sl@0
  5041
					minZoneFree = gZoneUtilArray[i].iFreePages;
sl@0
  5042
				}
sl@0
  5043
			test.Printf(_L("Free pages 0x%x maxDisFillBytes 0x%x\n"), gTotalPageCount.iFreePages, maxDisFillBytes);
sl@0
  5044
			maxDisFillBytes <<= gPageShift;
sl@0
  5045
sl@0
  5046
			r = DPTest::SetCacheSize(maxDisFillBytes, maxDisFillBytes);
sl@0
  5047
			if (r != KErrNone)
sl@0
  5048
				{
sl@0
  5049
				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  5050
				CLEANUP(ResetDPCache());
sl@0
  5051
				TEST_FAIL;
sl@0
  5052
				}
sl@0
  5053
			
sl@0
  5054
			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
sl@0
  5055
			TESTDEBUG(test.Printf(_L("CacheSize2: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
sl@0
  5056
										minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
sl@0
  5057
			if (currentCacheSize != maxDisFillBytes)
sl@0
  5058
				{
sl@0
  5059
				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  5060
				CLEANUP(ResetDPCache());
sl@0
  5061
				TEST_FAIL;
sl@0
  5062
				}
sl@0
  5063
			
sl@0
  5064
			GetAllPageInfo();
sl@0
  5065
			test.Printf(_L("Free pages after alloc discardable1 = 0x%x\n"), gTotalPageCount.iFreePages);
sl@0
  5066
sl@0
  5067
			r = DPTest::SetCacheSize(minZoneFree << gPageShift, maxDisFillBytes);
sl@0
  5068
			if (r != KErrNone)
sl@0
  5069
				{
sl@0
  5070
				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  5071
				CLEANUP(ResetDPCache());
sl@0
  5072
				TEST_FAIL;
sl@0
  5073
				}
sl@0
  5074
			
sl@0
  5075
			// Check all zones have no free pages.
sl@0
  5076
			GetAllPageInfo();
sl@0
  5077
			for (TUint i = 0; i < gZoneCount; i++)
sl@0
  5078
				{
sl@0
  5079
				if (gZoneUtilArray[i].iFreePages != 0)
sl@0
  5080
					{
sl@0
  5081
					test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  5082
					CLEANUP(ResetDPCache());
sl@0
  5083
					TEST_FAIL;
sl@0
  5084
					}
sl@0
  5085
				}
sl@0
  5086
			}
sl@0
  5087
		else
sl@0
  5088
			{
sl@0
  5089
			// Fragment the RAM with some movable pages
sl@0
  5090
			AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
sl@0
  5091
			FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  5092
			}
sl@0
  5093
		
sl@0
  5094
		TUint zoneID = gZoneConfigArray[index].iZoneId;
sl@0
  5095
		
sl@0
  5096
		GetAllPageInfo();
sl@0
  5097
		TUint origUnknownInZone = gZoneUtilArray[index].iAllocUnknown;
sl@0
  5098
		TUint origMovInZone = gZoneUtilArray[index].iAllocMovable;
sl@0
  5099
		TUint origFreeInZone = gZoneUtilArray[index].iFreePages;
sl@0
  5100
sl@0
  5101
		test.Printf(_L("Zone ID 0x%x - fixedPages = 0x%x, unknownPages = 0x%x, discardablePages = 0x%x movable = 0x%x free = 0x%x\n"), 
sl@0
  5102
						zoneID, gZoneUtilArray[index].iAllocFixed, origUnknownInZone, gZoneUtilArray[index].iAllocDiscardable, 
sl@0
  5103
						origMovInZone, origFreeInZone);
sl@0
  5104
sl@0
  5105
sl@0
  5106
		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, zoneID);
sl@0
  5107
sl@0
  5108
		GetAllPageInfo();		
sl@0
  5109
		TUint unknownPages = gZoneUtilArray[index].iAllocUnknown;
sl@0
  5110
		TUint discPages = gZoneUtilArray[index].iAllocDiscardable;
sl@0
  5111
		TUint movablePages = gZoneUtilArray[index].iAllocMovable;
sl@0
  5112
	
sl@0
  5113
		TUint freeInOtherZones = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  5114
		if(movablePages && freeInOtherZones)
sl@0
  5115
			{
sl@0
  5116
			test.Printf(_L("Fail: Zone ID %x all the movable pages haven't been moved\n"), zoneID);
sl@0
  5117
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5118
			CLEANUP(ResetDPCache());
sl@0
  5119
			TEST_FAIL;
sl@0
  5120
			}
sl@0
  5121
sl@0
  5122
		if (origUnknownInZone != unknownPages)
sl@0
  5123
			{
sl@0
  5124
			test.Printf(_L("Fail: Zone ID %x unknown pages before and after are not equal\n"), zoneID);
sl@0
  5125
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5126
			CLEANUP(ResetDPCache());
sl@0
  5127
			TEST_FAIL;
sl@0
  5128
			}
sl@0
  5129
sl@0
  5130
		if (!gPagedRom)
sl@0
  5131
			{
sl@0
  5132
			if (discPages && freeInOtherZones)
sl@0
  5133
				{
sl@0
  5134
				test.Printf(_L("Fail: Zone ID %x all the discardable pages haven't been moved\n"), zoneID);
sl@0
  5135
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5136
				CLEANUP(ResetDPCache());
sl@0
  5137
				TEST_FAIL;
sl@0
  5138
				}
sl@0
  5139
			}
sl@0
  5140
		else
sl@0
  5141
			{
sl@0
  5142
			DPTest::CacheSize(minCacheSize, maxCacheSize, currentCacheSize);
sl@0
  5143
			TESTDEBUG(test.Printf(_L("CacheSize3: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
sl@0
  5144
										minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
sl@0
  5145
			// The the discardable pages should have been discarded or moved unless 
sl@0
  5146
			// there is no room in the other zones and the cache size is already at its minimum
sl@0
  5147
			if (discPages && (freeInOtherZones || currentCacheSize != minCacheSize))
sl@0
  5148
				{
sl@0
  5149
				test.Printf(_L("Fail: Zone 0x%x all the discardable pages haven't been moved\n"), zoneID);
sl@0
  5150
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5151
				CLEANUP(ResetDPCache());
sl@0
  5152
				TEST_FAIL;
sl@0
  5153
				}
sl@0
  5154
			}
sl@0
  5155
		test.Printf(_L("Passed...\n"));
sl@0
  5156
		}
sl@0
  5157
	// TestEnd() willl cleanup what's required
sl@0
  5158
	TestEnd();
sl@0
  5159
sl@0
  5160
	test.Next(_L("Test8: Call function TRamDefragRequest::EmptyRamZone on a zone that contains fixed and movable pages"));
sl@0
  5161
	TestStart();
sl@0
  5162
	
sl@0
  5163
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  5164
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  5165
sl@0
  5166
	GetAllPageInfo();	
sl@0
  5167
	index = gZoneCount - 1;
sl@0
  5168
	while (index > 0 && (gZoneUtilArray[index].iAllocMovable < 10 || gZoneUtilArray[index].iFreePages < 3))
sl@0
  5169
		{	
sl@0
  5170
		-- index;
sl@0
  5171
		}
sl@0
  5172
	defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  5173
	
sl@0
  5174
	if (index == 0 && (gZoneUtilArray[index].iAllocMovable < 10 || gZoneUtilArray[index].iFreePages < 3))
sl@0
  5175
		{
sl@0
  5176
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  5177
		}
sl@0
  5178
	else
sl@0
  5179
		{
sl@0
  5180
		test.Printf(_L("defragZoneID = 0x%x\n"), defragZoneID);		
sl@0
  5181
		test.Printf(_L("movable = 0x%x discardable = 0x%x\n"), 
sl@0
  5182
						gZoneUtilArray[index].iAllocMovable, gZoneUtilArray[index].iAllocDiscardable);	
sl@0
  5183
sl@0
  5184
		
sl@0
  5185
		Ldd.ZoneAllocDiscontiguous(defragZoneID, 2); // Allocated 2 fixed pages to ensure that zone cannot be emptied
sl@0
  5186
		
sl@0
  5187
		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
sl@0
  5188
		
sl@0
  5189
		GetAllPageInfo();
sl@0
  5190
		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  5191
		if (r != KErrNoMemory || 
sl@0
  5192
			(freeInOthers && 
sl@0
  5193
			(gZoneUtilArray[index].iAllocMovable ||	
sl@0
  5194
			gZoneUtilArray[index].iAllocDiscardable)))
sl@0
  5195
			{
sl@0
  5196
			test.Printf(_L("r = %d, expected = %d, or all movable/discardable pages have not been moved"), 
sl@0
  5197
							r, KErrNoMemory);
sl@0
  5198
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  5199
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5200
			TEST_FAIL;
sl@0
  5201
			}
sl@0
  5202
		else
sl@0
  5203
			{
sl@0
  5204
			test.Printf(_L("Passed..."));
sl@0
  5205
			}
sl@0
  5206
		
sl@0
  5207
		Ldd.FreeAllFixedPages();
sl@0
  5208
		}
sl@0
  5209
	
sl@0
  5210
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
sl@0
  5211
	TestEnd();
sl@0
  5212
sl@0
  5213
	test.End();
sl@0
  5214
	return r;
sl@0
  5215
	}
sl@0
  5216
sl@0
  5217
sl@0
  5218
//
sl@0
  5219
// TestClaimRamZone
sl@0
  5220
//
sl@0
  5221
//---------------------------------------------------------------------------------------------------------------------
sl@0
  5222
//! @SYMTestCaseID				KBASE-t_ramdefrag-0532
sl@0
  5223
//! @SYMTestType				CIT
sl@0
  5224
//! @SYMTestCaseDesc			Verifying the implemntation of the function TRamDefragRequest::ClaimRamZone()
sl@0
  5225
//! @SYMPREQ					PREQ308
sl@0
  5226
//! @SYMTestPriority			High
sl@0
  5227
//! @SYMTestActions				
sl@0
  5228
//! 	1.	Fragment the memory. Following this, call function with a valid aID
sl@0
  5229
//! 	2.	Fragment the memory. Following this, call function with an invalid aID
sl@0
  5230
//! 	3.	Fragment the memory. Following this, start a zone defrag and whilst this 
sl@0
  5231
//! 		is running, call function on the same zone. 
sl@0
  5232
//! 	4.	Fragment the memory. Following this, start a general defrag and whilst this 
sl@0
  5233
//! 		is running, call function on the same zone.
sl@0
  5234
//! 	5.	Fragment the memory. Following this, call ClaimRamZone() on a specific zone, whilst at the
sl@0
  5235
//! 		same time calling ClaimRamZone() on another zone. 
sl@0
  5236
//! 	6:	Fragment the memory. Call function TRamDefragRequest::ClaimRamZone on specifc zone and at the 
sl@0
  5237
//! 		same time allocate pages to the zone
sl@0
  5238
//!		7.	Fragment the memory. Following this allocate fixed pages to a zone and attempt to claim
sl@0
  5239
//!			the zone. 
sl@0
  5240
//!		8.	Fragment the memory. Following this call ClaimRamZone() on every zone, one after the other
sl@0
  5241
//! 
sl@0
  5242
//! @SYMTestExpectedResults
sl@0
  5243
//! 	1.	KErrNone
sl@0
  5244
//! 	2.	KErrArgument
sl@0
  5245
//! 	3.	KErrNone and the zone has been claimed
sl@0
  5246
//! 	4.	KErrNone and the zone has been claimed
sl@0
  5247
//! 	5.	KErrNone and both zones have been claimed
sl@0
  5248
//! 	6.	KErrNoMemory
sl@0
  5249
//!		7.	KErrNoMemory, all movable and discardable pages have been moved
sl@0
  5250
//!		8.	One of the following scenarios should occur:
sl@0
  5251
//!			a.	If the cache has not reached its minimum size and there are no unknown pages in 
sl@0
  5252
//!				the zone, then all the movable pages should be moved from the zone if there are enough free pages
sl@0
  5253
//!				in the other zones. 
sl@0
  5254
//!				However, when demand paging is off, all movable pages should be moved from the zone, again if there 
sl@0
  5255
//!				are enough free pages in the other zones and if there are no unknown pages in the zone. 
sl@0
  5256
//!			b.	If the current size of the cache minus the number of discardable pages in the zone being emptied is not 
sl@0
  5257
//!				less than the minimum cache size and there are no unknown pages in the zone, 
sl@0
  5258
//!				then all the discardable pages should be discarded of. 
sl@0
  5259
//!				However, when demand paging is off, all discardable pages should be removed from 
sl@0
  5260
//!				the zone if there are no unknown pages in the zone. 
sl@0
  5261
//---------------------------------------------------------------------------------------------------------------------
sl@0
  5262
TInt TestClaimRamZone()
sl@0
  5263
	{
sl@0
  5264
	TInt r = 0;
sl@0
  5265
	TInt r2 = 0;
sl@0
  5266
	TUint32 addr = 0;
sl@0
  5267
	TInt waitTime = 1000;
sl@0
  5268
	
sl@0
  5269
	test.Start(_L("Test1: Call function TRamDefragRequest::ClaimRamZone with a valid aID"));	
sl@0
  5270
	TestStart();	
sl@0
  5271
	
sl@0
  5272
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  5273
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  5274
sl@0
  5275
	GetAllPageInfo();
sl@0
  5276
	TUint index = gZoneCount - 1;
sl@0
  5277
	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
sl@0
  5278
		{
sl@0
  5279
		-- index;
sl@0
  5280
		}
sl@0
  5281
	TUint defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  5282
	
sl@0
  5283
	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
sl@0
  5284
		{
sl@0
  5285
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  5286
		}
sl@0
  5287
	else
sl@0
  5288
		{
sl@0
  5289
		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
sl@0
  5290
		GetAllPageInfo();
sl@0
  5291
		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  5292
		if (freeInOthers && r != KErrNone && 
sl@0
  5293
			gZoneUtilArray[index].iAllocFixed != gZoneUtilArray[index].iPhysPages)
sl@0
  5294
			{
sl@0
  5295
			test.Printf(_L("Fail: Zone 0x%x has not been claimed r %d expected %d\n"), defragZoneID, r, KErrNone);
sl@0
  5296
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5297
			TEST_FAIL;
sl@0
  5298
			}
sl@0
  5299
		else
sl@0
  5300
			{
sl@0
  5301
			test.Printf(_L("Passed...\n"));	
sl@0
  5302
			}
sl@0
  5303
sl@0
  5304
		addr = gZoneConfigArray[index].iPhysBase;
sl@0
  5305
		if (r == KErrNone)
sl@0
  5306
			{
sl@0
  5307
			r = Ldd.FreeAllFixedPages();
sl@0
  5308
			}
sl@0
  5309
		}
sl@0
  5310
	
sl@0
  5311
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
sl@0
  5312
	TestEnd();
sl@0
  5313
sl@0
  5314
sl@0
  5315
	test.Next(_L("Test2: Call function TRamDefragRequest::ClaimRamZone with an invalid aID"));	
sl@0
  5316
	TestStart();	
sl@0
  5317
	gChunkArray1 = new RChunk;
sl@0
  5318
	gChunkArraySize1 = 1;
sl@0
  5319
	defragZoneID = KInvalidZoneID;
sl@0
  5320
	
sl@0
  5321
	r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);	
sl@0
  5322
	if (r != KErrArgument)
sl@0
  5323
		{
sl@0
  5324
		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
sl@0
  5325
		TEST_FAIL;
sl@0
  5326
		}
sl@0
  5327
	else
sl@0
  5328
		{
sl@0
  5329
		test.Printf(_L("Passed...\n"));	
sl@0
  5330
		}	
sl@0
  5331
	
sl@0
  5332
	TestEnd();
sl@0
  5333
sl@0
  5334
sl@0
  5335
	test.Next(_L("Test3: Call function TRamDefragRequest::ClaimRamZone when a EmptyRamZone is already running"));	
sl@0
  5336
	TestStart();	
sl@0
  5337
	
sl@0
  5338
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  5339
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  5340
sl@0
  5341
	GetAllPageInfo();
sl@0
  5342
	index = gZoneCount - 1;
sl@0
  5343
	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
sl@0
  5344
		{
sl@0
  5345
		-- index;
sl@0
  5346
		}
sl@0
  5347
	defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  5348
sl@0
  5349
	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
sl@0
  5350
		{
sl@0
  5351
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  5352
		}
sl@0
  5353
	else
sl@0
  5354
		{
sl@0
  5355
		TInt numPassed = 0;
sl@0
  5356
		for (waitTime = 1000; waitTime > 0; waitTime-=10)
sl@0
  5357
			{
sl@0
  5358
			r = gTestThread.Create(gTestThreadName,MultiEmptyZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)defragZoneID);
sl@0
  5359
			if (r != KErrNone)
sl@0
  5360
				{
sl@0
  5361
				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  5362
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5363
				TEST_FAIL;
sl@0
  5364
				}
sl@0
  5365
sl@0
  5366
			gTestThread.Logon(status);
sl@0
  5367
			gTestThread.Resume();
sl@0
  5368
			User::After(waitTime);
sl@0
  5369
			
sl@0
  5370
			r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
sl@0
  5371
sl@0
  5372
			User::WaitForRequest(status);
sl@0
  5373
			r2 = status.Int();
sl@0
  5374
			
sl@0
  5375
			GetAllPageInfo();
sl@0
  5376
			TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  5377
			if (freeInOthers && r != KErrNone && r2 != KErrNone)
sl@0
  5378
				{
sl@0
  5379
				test.Printf(_L("Fail: r = %d, r2 = %d, expected = %d, or zone ID 0x%x has not been claimed\n"), 
sl@0
  5380
										r, r2, KErrNone, defragZoneID);
sl@0
  5381
				if (r == KErrNone)
sl@0
  5382
					{
sl@0
  5383
					CLEANUP(r = Ldd.FreeAllFixedPages());
sl@0
  5384
					}	
sl@0
  5385
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5386
				TEST_FAIL;
sl@0
  5387
				}
sl@0
  5388
			else
sl@0
  5389
				{
sl@0
  5390
				numPassed ++;
sl@0
  5391
				}
sl@0
  5392
			
sl@0
  5393
			if (r == KErrNone)
sl@0
  5394
				{
sl@0
  5395
				r = Ldd.FreeAllFixedPages();
sl@0
  5396
				}		
sl@0
  5397
			
sl@0
  5398
			gTestThread.Close();	
sl@0
  5399
			}
sl@0
  5400
		if (numPassed > 0)
sl@0
  5401
			{
sl@0
  5402
			test.Printf(_L("Passed...\n"));	
sl@0
  5403
			}
sl@0
  5404
		}
sl@0
  5405
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  5406
	TestEnd();
sl@0
  5407
sl@0
  5408
sl@0
  5409
	test.Next(_L("Test4: Call function TRamDefragRequest::ClaimRamZone when a general defrag is already running"));	
sl@0
  5410
	TestStart();	
sl@0
  5411
	
sl@0
  5412
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  5413
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  5414
sl@0
  5415
	GetAllPageInfo();
sl@0
  5416
	index = gZoneCount - 1;
sl@0
  5417
	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
sl@0
  5418
		{
sl@0
  5419
		-- index;
sl@0
  5420
		}
sl@0
  5421
sl@0
  5422
	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
sl@0
  5423
		{
sl@0
  5424
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  5425
		}
sl@0
  5426
	else
sl@0
  5427
		{
sl@0
  5428
		defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  5429
sl@0
  5430
		r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
sl@0
  5431
		if (r != KErrNone)
sl@0
  5432
			{
sl@0
  5433
			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  5434
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5435
			TEST_FAIL;
sl@0
  5436
			}
sl@0
  5437
		
sl@0
  5438
		gTestThread.Logon(status);
sl@0
  5439
		gTestThread.Resume();
sl@0
  5440
		User::After(waitTime);
sl@0
  5441
sl@0
  5442
		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
sl@0
  5443
		
sl@0
  5444
		User::WaitForRequest(status);
sl@0
  5445
		r2 = status.Int();
sl@0
  5446
		
sl@0
  5447
		if (r != KErrNone)
sl@0
  5448
			{
sl@0
  5449
			test.Printf(_L("ClaimZone: r = %d, expected = %d\n"), r, KErrNone);
sl@0
  5450
			}	
sl@0
  5451
		
sl@0
  5452
		if (r2 != KErrNone)
sl@0
  5453
			{
sl@0
  5454
			test.Printf(_L("General: r2 = %d, expected = %d\n"), r, KErrNone);
sl@0
  5455
			}
sl@0
  5456
sl@0
  5457
		GetAllPageInfo();
sl@0
  5458
		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  5459
		if (freeInOthers && gZoneUtilArray[index].iAllocFixed != gZoneUtilArray[index].iPhysPages)
sl@0
  5460
			{
sl@0
  5461
			test.Printf(_L("Fail: Zone ID 0x%x has not been claimed, r = %d, r2 = %d, expected = %d\n"), 
sl@0
  5462
										defragZoneID, r, r2, KErrNone);
sl@0
  5463
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));	
sl@0
  5464
			TEST_FAIL;
sl@0
  5465
			}
sl@0
  5466
		else
sl@0
  5467
			{
sl@0
  5468
			test.Printf(_L("Passed...\n"));	
sl@0
  5469
			}
sl@0
  5470
sl@0
  5471
		if (r == KErrNone)
sl@0
  5472
			{
sl@0
  5473
			r = Ldd.FreeAllFixedPages();
sl@0
  5474
			}
sl@0
  5475
					
sl@0
  5476
		gTestThread.Close();	
sl@0
  5477
		}
sl@0
  5478
	
sl@0
  5479
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
sl@0
  5480
	TestEnd();
sl@0
  5481
sl@0
  5482
sl@0
  5483
sl@0
  5484
	test.Next(_L("Test5: Call function TRamDefragRequest::ClaimRamZone on specifc zone at the same time as calling on another zone"));	
sl@0
  5485
	TestStart();	
sl@0
  5486
	
sl@0
  5487
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  5488
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  5489
	
sl@0
  5490
	GetAllPageInfo();
sl@0
  5491
	index = gZoneCount - 1;
sl@0
  5492
	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
sl@0
  5493
		{
sl@0
  5494
		-- index;
sl@0
  5495
		}
sl@0
  5496
	defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  5497
	test.Printf(_L("index = 0x%x "), index);
sl@0
  5498
sl@0
  5499
	TUint index2 = gZoneCount - 1;
sl@0
  5500
	while (	index2 > 0 &&  
sl@0
  5501
			(index == index2 || gZoneUtilArray[index2].iAllocFixed != 0 || gZoneUtilArray[index2].iAllocUnknown != 0))
sl@0
  5502
		{
sl@0
  5503
		-- index2;
sl@0
  5504
		}
sl@0
  5505
	TUint defragZoneID2 = gZoneConfigArray[index2].iZoneId;
sl@0
  5506
	test.Printf(_L("index2 = %d\n"), index2);
sl@0
  5507
sl@0
  5508
	if ((index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) || 
sl@0
  5509
		(index2 == 0 && (index == index2 || gZoneUtilArray[index2].iAllocFixed != 0 || gZoneUtilArray[index2].iAllocUnknown != 0)))
sl@0
  5510
		{
sl@0
  5511
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  5512
		}
sl@0
  5513
	else
sl@0
  5514
		{
sl@0
  5515
		r = gTestThread.Create(gTestThreadName,MultiClaimZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)(defragZoneID2));
sl@0
  5516
		if (r != KErrNone)
sl@0
  5517
			{
sl@0
  5518
			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  5519
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5520
			TEST_FAIL;
sl@0
  5521
			}
sl@0
  5522
sl@0
  5523
		gTestThread.Logon(status);
sl@0
  5524
		gTestThread.Resume();
sl@0
  5525
		User::After(waitTime);
sl@0
  5526
		
sl@0
  5527
		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
sl@0
  5528
		TESTDEBUG(test.Printf(_L("r = %d\n"), r));
sl@0
  5529
sl@0
  5530
		User::WaitForRequest(status);
sl@0
  5531
		r2 = status.Int();
sl@0
  5532
		
sl@0
  5533
		GetAllPageInfo();
sl@0
  5534
		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  5535
		TUint freeInOthers2 = gTotalPageCount.iFreePages - gZoneUtilArray[index2].iFreePages;
sl@0
  5536
		if ((freeInOthers && r != KErrNone && 
sl@0
  5537
			gZoneUtilArray[index].iAllocFixed != gZoneUtilArray[index].iPhysPages) ||
sl@0
  5538
			(freeInOthers2 && r != KErrNone && 
sl@0
  5539
			gZoneUtilArray[index2].iAllocFixed != gZoneUtilArray[index2].iPhysPages))
sl@0
  5540
			{
sl@0
  5541
			test.Printf(_L("Fail: Zone ID 0x%x or Zone ID 0x%x has not been claimed, r = %d, r2 = %d, expected = %d\n"), 
sl@0
  5542
									defragZoneID, defragZoneID2, r, r2, KErrNone);
sl@0
  5543
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5544
			TEST_FAIL;
sl@0
  5545
			}
sl@0
  5546
		else
sl@0
  5547
			{
sl@0
  5548
			test.Printf(_L("Passed...\n"));	
sl@0
  5549
			}
sl@0
  5550
		
sl@0
  5551
		if (r == KErrNone)
sl@0
  5552
			{
sl@0
  5553
			r = Ldd.FreeAllFixedPages();
sl@0
  5554
			}
sl@0
  5555
sl@0
  5556
		if (r2 == KErrNone)
sl@0
  5557
			{// Have to free from specific address as RAM zone claimed by other channel.
sl@0
  5558
			addr = gZoneConfigArray[index2].iPhysBase;
sl@0
  5559
			r = Ldd.FreeFromAddr(gZoneUtilArray[index2].iAllocFixed, addr);
sl@0
  5560
			}		
sl@0
  5561
sl@0
  5562
		gTestThread.Close();	
sl@0
  5563
		}
sl@0
  5564
	
sl@0
  5565
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
sl@0
  5566
	TestEnd();
sl@0
  5567
sl@0
  5568
sl@0
  5569
	test.Next(_L("Test6: Call function TRamDefragRequest::ClaimRamZone on specifc zone and at the same time allocate pages to the zone"));	
sl@0
  5570
	TestStart();		
sl@0
  5571
	
sl@0
  5572
	TInt pagesAlloc = 1; // Try and allocate just one page whilst attempting to claim the zone
sl@0
  5573
sl@0
  5574
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  5575
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  5576
sl@0
  5577
	GetAllPageInfo();
sl@0
  5578
	index = gZoneCount - 1;
sl@0
  5579
	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
sl@0
  5580
		{
sl@0
  5581
		-- index;
sl@0
  5582
		}
sl@0
  5583
	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
sl@0
  5584
		{
sl@0
  5585
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  5586
		}
sl@0
  5587
	else
sl@0
  5588
		{
sl@0
  5589
		GetAllPageInfo();
sl@0
  5590
		defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  5591
		TInt numPassed = 0;
sl@0
  5592
		for (waitTime = 1000; waitTime > 0; waitTime-=10)
sl@0
  5593
			{
sl@0
  5594
			r = gTestThread.Create(gTestThreadName,MultiClaimZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)defragZoneID);
sl@0
  5595
			if (r != KErrNone)
sl@0
  5596
			{
sl@0
  5597
			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  5598
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5599
			TEST_FAIL;
sl@0
  5600
			}
sl@0
  5601
sl@0
  5602
			gTestThread.Logon(status);
sl@0
  5603
			gTestThread.Resume();
sl@0
  5604
			test(status.Int() == KRequestPending);
sl@0
  5605
			User::After(waitTime);
sl@0
  5606
			
sl@0
  5607
			r = Ldd.ZoneAllocDiscontiguous(defragZoneID, pagesAlloc);
sl@0
  5608
sl@0
  5609
			TESTDEBUG(test.Printf(_L("r = %d\n"), r));
sl@0
  5610
			
sl@0
  5611
			User::WaitForRequest(status);
sl@0
  5612
			r2 = status.Int();
sl@0
  5613
				
sl@0
  5614
			GetAllPageInfo();
sl@0
  5615
			TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  5616
			if (freeInOthers && r2 != KErrNone)
sl@0
  5617
				{
sl@0
  5618
				test.Printf(_L("Claim was unsuccsessful: r2 = %d, expected = %d\n"), r2, KErrNone);
sl@0
  5619
				}
sl@0
  5620
			else if (r2 == KErrNone && r != KErrNoMemory)
sl@0
  5621
				{
sl@0
  5622
				test.Printf(_L("Fail: r = %d, expected = %d, r2 = %d, expected = %d, zone ID = 0x%x\n"), 
sl@0
  5623
										r, KErrNoMemory, r2, KErrNone, defragZoneID);
sl@0
  5624
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5625
				CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  5626
				// Free from address as a different channel claimed the RAM zone.
sl@0
  5627
				addr = gZoneConfigArray[index].iPhysBase;
sl@0
  5628
				CLEANUP(Ldd.FreeFromAddr(gZoneUtilArray[index].iAllocFixed, addr));
sl@0
  5629
				TEST_FAIL;
sl@0
  5630
				}
sl@0
  5631
			else
sl@0
  5632
				{
sl@0
  5633
				numPassed ++;
sl@0
  5634
				}
sl@0
  5635
			
sl@0
  5636
			Ldd.FreeAllFixedPages();
sl@0
  5637
		
sl@0
  5638
			GetAllPageInfo();
sl@0
  5639
			if (r2 == KErrNone)
sl@0
  5640
				{// Free from address as a different channel claimed the RAM zone.
sl@0
  5641
				addr = gZoneConfigArray[index].iPhysBase;
sl@0
  5642
				r = Ldd.FreeFromAddr(gZoneUtilArray[index].iAllocFixed, addr);
sl@0
  5643
				}
sl@0
  5644
			gTestThread.Close();
sl@0
  5645
			}
sl@0
  5646
		if (numPassed > 0)
sl@0
  5647
			{
sl@0
  5648
			test.Printf(_L("Passed...\n"));	
sl@0
  5649
			}	
sl@0
  5650
		}	
sl@0
  5651
	
sl@0
  5652
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  5653
	TestEnd();
sl@0
  5654
sl@0
  5655
sl@0
  5656
sl@0
  5657
	test.Next(_L("Test7: Call function TRamDefragRequest::ClaimRamZone on a zone that contains fixed and movable pages"));
sl@0
  5658
	TestStart();
sl@0
  5659
	
sl@0
  5660
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  5661
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  5662
sl@0
  5663
	GetAllPageInfo();
sl@0
  5664
	index = gZoneCount - 1;
sl@0
  5665
	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0) &&
sl@0
  5666
			(gZoneUtilArray[index].iAllocMovable < 10 || gZoneUtilArray[index].iFreePages < 3))
sl@0
  5667
			{
sl@0
  5668
			-- index;
sl@0
  5669
			}
sl@0
  5670
sl@0
  5671
	if (index == 0 && ((gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0) &&
sl@0
  5672
			(gZoneUtilArray[index].iAllocMovable < 10 || gZoneUtilArray[index].iFreePages < 3)))
sl@0
  5673
		{
sl@0
  5674
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  5675
		}
sl@0
  5676
	else
sl@0
  5677
		{
sl@0
  5678
		defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  5679
		TESTDEBUG(test.Printf(_L("defragZoneID = 0x%x\n"), defragZoneID));		
sl@0
  5680
		TESTDEBUG(test.Printf(_L("movable = 0x%x discardable = 0x%x\n"), 
sl@0
  5681
						gZoneUtilArray[index].iAllocMovable, gZoneUtilArray[index].iAllocDiscardable));	
sl@0
  5682
sl@0
  5683
		
sl@0
  5684
		Ldd.ZoneAllocDiscontiguous(defragZoneID, 2); // Allocated 2 fixed pages to ensure that zone cannot be emptied
sl@0
  5685
		
sl@0
  5686
		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
sl@0
  5687
		GetAllPageInfo();
sl@0
  5688
		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  5689
		if (r != KErrNoMemory || 
sl@0
  5690
			(freeInOthers && 
sl@0
  5691
			(gZoneUtilArray[index].iAllocMovable || 
sl@0
  5692
			gZoneUtilArray[index].iAllocDiscardable)))
sl@0
  5693
			{
sl@0
  5694
			test.Printf(_L("r = %d, expected = %d, or all movable/discardable pages have not been moved"), 
sl@0
  5695
							r, KErrNoMemory);
sl@0
  5696
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  5697
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5698
			TEST_FAIL;
sl@0
  5699
			}
sl@0
  5700
		else
sl@0
  5701
			{
sl@0
  5702
			test.Printf(_L("Passed...\n"));
sl@0
  5703
			}
sl@0
  5704
		
sl@0
  5705
		Ldd.FreeAllFixedPages();
sl@0
  5706
		}
sl@0
  5707
sl@0
  5708
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  5709
	TestEnd();
sl@0
  5710
sl@0
  5711
	test.Next(_L("Test8: Call function TRamDefragRequest::ClaimRamZone on every zone one after the other"));
sl@0
  5712
	TestStart();
sl@0
  5713
		
sl@0
  5714
	Ldd.ResetDriver();
sl@0
  5715
			
sl@0
  5716
	for (index = 0; index < gZoneCount; index ++)
sl@0
  5717
		{
sl@0
  5718
		// Variables for DP ROM cache sizes
sl@0
  5719
		TUint minCacheSize = 0;
sl@0
  5720
		TUint maxCacheSize = 0;
sl@0
  5721
		TUint currentCacheSize = 0;
sl@0
  5722
sl@0
  5723
		if (gPagedRom)
sl@0
  5724
			{
sl@0
  5725
			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
sl@0
  5726
			TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
sl@0
  5727
										minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
sl@0
  5728
			// Calculate the maximum size of the DP cache 
sl@0
  5729
			TUint minZoneFree = KMaxTUint;
sl@0
  5730
			TUint maxDisFillBytes = 0;
sl@0
  5731
			GetAllPageInfo();
sl@0
  5732
			for (TUint i = 0; i < gZoneCount; i++)
sl@0
  5733
				{
sl@0
  5734
				maxDisFillBytes += gZoneUtilArray[i].iFreePages;
sl@0
  5735
				maxDisFillBytes += gZoneUtilArray[i].iAllocDiscardable;
sl@0
  5736
				if (minZoneFree > gZoneUtilArray[i].iFreePages)
sl@0
  5737
					minZoneFree = gZoneUtilArray[i].iFreePages;
sl@0
  5738
				}
sl@0
  5739
			test.Printf(_L("Free pages 0x%x maxDisFillBytes 0x%x\n"), gTotalPageCount.iFreePages, maxDisFillBytes);
sl@0
  5740
			maxDisFillBytes <<= gPageShift;
sl@0
  5741
sl@0
  5742
			r = DPTest::SetCacheSize(maxDisFillBytes, maxDisFillBytes);
sl@0
  5743
			if (r != KErrNone)
sl@0
  5744
				{
sl@0
  5745
				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  5746
				CLEANUP(ResetDPCache());
sl@0
  5747
				TEST_FAIL;
sl@0
  5748
				}
sl@0
  5749
			
sl@0
  5750
			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
sl@0
  5751
			TESTDEBUG(test.Printf(_L("CacheSize2: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
sl@0
  5752
										minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
sl@0
  5753
			if (currentCacheSize != maxDisFillBytes)
sl@0
  5754
				{
sl@0
  5755
				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  5756
				CLEANUP(ResetDPCache());
sl@0
  5757
				TEST_FAIL;
sl@0
  5758
				}
sl@0
  5759
			
sl@0
  5760
			GetAllPageInfo();
sl@0
  5761
			test.Printf(_L("Free pages after alloc discardable1 = 0x%x\n"), gTotalPageCount.iFreePages);
sl@0
  5762
sl@0
  5763
			r = DPTest::SetCacheSize(minZoneFree << gPageShift, maxDisFillBytes);
sl@0
  5764
			if (r != KErrNone)
sl@0
  5765
				{
sl@0
  5766
				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  5767
				CLEANUP(ResetDPCache());
sl@0
  5768
				TEST_FAIL;
sl@0
  5769
				}
sl@0
  5770
			
sl@0
  5771
			// Check all zones have no free pages.
sl@0
  5772
			GetAllPageInfo();
sl@0
  5773
			for (TUint i = 0; i < gZoneCount; i++)
sl@0
  5774
				{
sl@0
  5775
				if (gZoneUtilArray[i].iFreePages != 0)
sl@0
  5776
					{
sl@0
  5777
					test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  5778
					CLEANUP(ResetDPCache());
sl@0
  5779
					TEST_FAIL;
sl@0
  5780
					}
sl@0
  5781
				}
sl@0
  5782
			}
sl@0
  5783
		else
sl@0
  5784
			{
sl@0
  5785
			// Fragment the RAM with some movable pages
sl@0
  5786
			AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
sl@0
  5787
			FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  5788
			}
sl@0
  5789
		
sl@0
  5790
		TUint zoneID = gZoneConfigArray[index].iZoneId;
sl@0
  5791
		
sl@0
  5792
		GetAllPageInfo();
sl@0
  5793
		TUint origUnknownInZone = gZoneUtilArray[index].iAllocUnknown;
sl@0
  5794
		TUint origMovInZone = gZoneUtilArray[index].iAllocMovable;
sl@0
  5795
		TUint origFreeInZone = gZoneUtilArray[index].iFreePages;
sl@0
  5796
sl@0
  5797
		test.Printf(_L("Zone ID 0x%x - fixedPages = 0x%x, unknownPages = 0x%x, discardablePages = 0x%x movable = 0x%x free = 0x%x\n"), 
sl@0
  5798
						zoneID, gZoneUtilArray[index].iAllocFixed, origUnknownInZone, gZoneUtilArray[index].iAllocDiscardable, 
sl@0
  5799
						origMovInZone, origFreeInZone);
sl@0
  5800
sl@0
  5801
sl@0
  5802
		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneID);
sl@0
  5803
sl@0
  5804
		GetAllPageInfo();		
sl@0
  5805
		TUint unknownPages = gZoneUtilArray[index].iAllocUnknown;
sl@0
  5806
		TUint discPages = gZoneUtilArray[index].iAllocDiscardable;
sl@0
  5807
		TUint movablePages = gZoneUtilArray[index].iAllocMovable;
sl@0
  5808
	
sl@0
  5809
		if (r == KErrNone)
sl@0
  5810
			{
sl@0
  5811
			r = Ldd.FreeAllFixedPages();
sl@0
  5812
			}
sl@0
  5813
		TUint freeInOtherZones = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  5814
		if (!unknownPages && movablePages && freeInOtherZones)
sl@0
  5815
			{
sl@0
  5816
			test.Printf(_L("Fail: Zone ID %x all the movable pages haven't been moved\n"), zoneID);
sl@0
  5817
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5818
			CLEANUP(ResetDPCache());
sl@0
  5819
			TEST_FAIL;
sl@0
  5820
			}
sl@0
  5821
sl@0
  5822
		if (origUnknownInZone != unknownPages)
sl@0
  5823
			{
sl@0
  5824
			test.Printf(_L("Fail: Zone ID %x unknown pages before and after are not equal\n"), zoneID);
sl@0
  5825
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5826
			CLEANUP(ResetDPCache());
sl@0
  5827
			TEST_FAIL;
sl@0
  5828
			}
sl@0
  5829
sl@0
  5830
		if (!gPagedRom)
sl@0
  5831
			{
sl@0
  5832
			if (!unknownPages && freeInOtherZones && discPages)
sl@0
  5833
				{
sl@0
  5834
				test.Printf(_L("Fail: Zone ID %x all the discardable pages haven't been moved\n"), zoneID);
sl@0
  5835
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5836
				CLEANUP(ResetDPCache());
sl@0
  5837
				TEST_FAIL;
sl@0
  5838
				}
sl@0
  5839
			}
sl@0
  5840
		else
sl@0
  5841
			{
sl@0
  5842
			DPTest::CacheSize(minCacheSize, maxCacheSize, currentCacheSize);
sl@0
  5843
			TESTDEBUG(test.Printf(_L("CacheSize3: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
sl@0
  5844
										minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
sl@0
  5845
			// The the discardable pages should have been discarded or moved unless 
sl@0
  5846
			// there is no room in the other zones and the cache size is already at its minimum
sl@0
  5847
			if (!unknownPages && discPages && (freeInOtherZones || currentCacheSize != minCacheSize))
sl@0
  5848
				{
sl@0
  5849
				test.Printf(_L("Fail: Zone ID 0x%x all the discardable pages haven't been moved\n"), zoneID);
sl@0
  5850
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5851
				CLEANUP(ResetDPCache());
sl@0
  5852
				TEST_FAIL;
sl@0
  5853
				}
sl@0
  5854
			}
sl@0
  5855
		test.Printf(_L("Passed...\n"));
sl@0
  5856
		}
sl@0
  5857
	// TestEnd() will cleanup what's required.
sl@0
  5858
	TestEnd();
sl@0
  5859
	test.End();
sl@0
  5860
	return 0;
sl@0
  5861
	}
sl@0
  5862
sl@0
  5863
sl@0
  5864
//
sl@0
  5865
// TestCancelDefrag
sl@0
  5866
//
sl@0
  5867
//-----------------------------------------------------------------------------------------------------------------------------------------
sl@0
  5868
//! @SYMTestCaseID				KBASE-t_ramdefrag-0533
sl@0
  5869
//! @SYMTestType				CIT
sl@0
  5870
//! @SYMTestCaseDesc			Verifying the implementation of the function TRamDefragRequest::CancelDefrag()
sl@0
  5871
//! @SYMPREQ					PREQ308
sl@0
  5872
//! @SYMTestPriority			High
sl@0
  5873
//! @SYMTestActions				
sl@0
  5874
//! 	1.	Fragment the memory. Following this, start a general defrag and cancel it
sl@0
  5875
//! 	2.	Fragment the memory. Following this, start a zone defrag and cancel it 
sl@0
  5876
//! 	3.	Fragment the memory. Following this, start a claim zone and cancel it
sl@0
  5877
//! 
sl@0
  5878
//! @SYMTestExpectedResults
sl@0
  5879
//! 	1.	KErrCancel
sl@0
  5880
//! 	2.	KErrCancel
sl@0
  5881
//! 	3.	KErrCancel
sl@0
  5882
//-----------------------------------------------------------------------------------------------------------------------------------------
sl@0
  5883
TInt TestCancelDefrag()
sl@0
  5884
	{
sl@0
  5885
	TInt r = 0;
sl@0
  5886
	TUint defragZoneID = 0;
sl@0
  5887
sl@0
  5888
	test.Start(_L("Test1: Call general defrag and cancel it"));	
sl@0
  5889
	TestStart();	
sl@0
  5890
	
sl@0
  5891
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  5892
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  5893
	
sl@0
  5894
	r = Ldd.CheckCancel(DEFRAG_TYPE_GEN);
sl@0
  5895
sl@0
  5896
	TESTDEBUG(test.Printf(_L("r = %d\n"), r));
sl@0
  5897
	if (r != KErrCancel)
sl@0
  5898
		{
sl@0
  5899
		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrCancel);
sl@0
  5900
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5901
		TEST_FAIL;
sl@0
  5902
		}
sl@0
  5903
	else
sl@0
  5904
		{
sl@0
  5905
		test.Printf(_L("Passed...\n"));	
sl@0
  5906
		}
sl@0
  5907
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  5908
	TestEnd();
sl@0
  5909
sl@0
  5910
sl@0
  5911
	test.Next(_L("Test2: Call zone defrag and cancel it"));	
sl@0
  5912
	TestStart();		
sl@0
  5913
	
sl@0
  5914
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  5915
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  5916
sl@0
  5917
	GetAllPageInfo();
sl@0
  5918
	TUint index = gZoneCount - 1;
sl@0
  5919
	while (index > 0 && 
sl@0
  5920
			(gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
sl@0
  5921
			gZoneUtilArray[index].iAllocFixed !=0 ||
sl@0
  5922
			gZoneUtilArray[index].iAllocUnknown !=0))
sl@0
  5923
		{	
sl@0
  5924
		-- index; 
sl@0
  5925
		}
sl@0
  5926
	if (index == 0 && 
sl@0
  5927
		(gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
sl@0
  5928
		gZoneUtilArray[index].iAllocFixed !=0 ||
sl@0
  5929
		gZoneUtilArray[index].iAllocUnknown !=0))
sl@0
  5930
		{
sl@0
  5931
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  5932
		}
sl@0
  5933
	else
sl@0
  5934
		{		
sl@0
  5935
		defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  5936
		TESTDEBUG(test.Printf(_L("defragZoneID = 0x%x\n"),defragZoneID));
sl@0
  5937
sl@0
  5938
		r = Ldd.CheckCancel(DEFRAG_TYPE_EMPTY, defragZoneID);
sl@0
  5939
		
sl@0
  5940
		TESTDEBUG(test.Printf(_L("r = %d\n"), r));
sl@0
  5941
		if (r != KErrCancel)
sl@0
  5942
			{
sl@0
  5943
			test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrCancel);
sl@0
  5944
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5945
			TEST_FAIL;
sl@0
  5946
			}	
sl@0
  5947
		else
sl@0
  5948
			{
sl@0
  5949
			test.Printf(_L("Passed...\n"));	
sl@0
  5950
			}
sl@0
  5951
		}	
sl@0
  5952
sl@0
  5953
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  5954
	TestEnd();
sl@0
  5955
sl@0
  5956
sl@0
  5957
	test.Next(_L("Test3: Call Claim RAM Zone and cancel it"));	
sl@0
  5958
	TestStart();
sl@0
  5959
	
sl@0
  5960
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  5961
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  5962
sl@0
  5963
	GetAllPageInfo();
sl@0
  5964
	index = gZoneCount - 1;	
sl@0
  5965
	while (index > 0 && 
sl@0
  5966
			(gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
sl@0
  5967
			gZoneUtilArray[index].iAllocFixed !=0 ||
sl@0
  5968
			gZoneUtilArray[index].iAllocUnknown !=0))
sl@0
  5969
		{
sl@0
  5970
		-- index;
sl@0
  5971
		}
sl@0
  5972
	defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  5973
	TESTDEBUG(test.Printf(_L("defragZoneID = 0x%x\n"),defragZoneID));
sl@0
  5974
sl@0
  5975
	if (index == 0 && 
sl@0
  5976
			(gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
sl@0
  5977
			gZoneUtilArray[index].iAllocFixed !=0 ||
sl@0
  5978
			gZoneUtilArray[index].iAllocUnknown !=0))
sl@0
  5979
		{
sl@0
  5980
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  5981
		}
sl@0
  5982
	else
sl@0
  5983
		{
sl@0
  5984
		r = Ldd.CheckCancel(DEFRAG_TYPE_CLAIM, defragZoneID);
sl@0
  5985
sl@0
  5986
		TESTDEBUG(test.Printf(_L("r = %d\n"), r));
sl@0
  5987
		if (r != KErrCancel)
sl@0
  5988
			{
sl@0
  5989
			test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrCancel);
sl@0
  5990
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  5991
			TEST_FAIL;
sl@0
  5992
			}	
sl@0
  5993
		else
sl@0
  5994
			{
sl@0
  5995
			test.Printf(_L("Passed...\n"));	
sl@0
  5996
			}
sl@0
  5997
		}
sl@0
  5998
sl@0
  5999
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  6000
	TestEnd(); 
sl@0
  6001
sl@0
  6002
	test.End();
sl@0
  6003
	return 0;
sl@0
  6004
	}
sl@0
  6005
sl@0
  6006
//
sl@0
  6007
// TestZoneAllocNoAffect
sl@0
  6008
//
sl@0
  6009
// Ensures that fixed page allocations do not affect the movable
sl@0
  6010
// or discardable page allocations
sl@0
  6011
//
sl@0
  6012
TInt TestZoneAllocNoAffect(TInt aZoneAllocType, TInt aPageType)
sl@0
  6013
	{
sl@0
  6014
	TInt retVal = KErrNone;
sl@0
  6015
	TInt r = KErrNone;
sl@0
  6016
	TInt mostPrefIndex = 0;
sl@0
  6017
	TUint leastPrefIndex = 0;
sl@0
  6018
	TUint leastPrefZoneID = 0;
sl@0
  6019
sl@0
  6020
	TestStart();
sl@0
  6021
	gChunkArray1 = new RChunk;
sl@0
  6022
	gChunkArraySize1 = 1;
sl@0
  6023
	switch(aPageType)
sl@0
  6024
		{
sl@0
  6025
		case BEST_MOVABLE:
sl@0
  6026
			test.Printf(_L("Test Epoc::ZoneAllocPhysicalRam() doesn't affect the allocation of movable pages\n"));
sl@0
  6027
			break;
sl@0
  6028
sl@0
  6029
		case BEST_DISCARDABLE:
sl@0
  6030
			test.Printf(_L("Test Epoc::ZoneAllocPhysicalRam() doesn't affect the allocation of discardable pages\n"));
sl@0
  6031
			if (!gPagedRom)
sl@0
  6032
				{
sl@0
  6033
				test.Printf(_L("Not a paged ROM - Skipping...\n"));
sl@0
  6034
				goto skipSetup;
sl@0
  6035
				}
sl@0
  6036
			break;
sl@0
  6037
		}
sl@0
  6038
	// Fist find the zone that movable page allocations should go into
sl@0
  6039
	// Getting the best movable will be the same for discardable as well
sl@0
  6040
	mostPrefIndex = GetBestZone(aPageType);
sl@0
  6041
	if (mostPrefIndex == KErrNotFound)
sl@0
  6042
		{
sl@0
  6043
		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  6044
		goto skipSetup;
sl@0
  6045
		}
sl@0
  6046
sl@0
  6047
	// Get the least pref zone to zone alloc into and ensure it has free pages
sl@0
  6048
	GetPrefOrder();
sl@0
  6049
	leastPrefIndex = gPrefArray[gZoneCount - 1];
sl@0
  6050
	if (gZoneConfigArray[mostPrefIndex].iPref == gZoneConfigArray[leastPrefIndex].iPref)
sl@0
  6051
		{
sl@0
  6052
		test.Printf(_L("Zones with same preference - Skipping...\n"));
sl@0
  6053
		goto skipSetup;
sl@0
  6054
		}
sl@0
  6055
	if (leastPrefIndex == (TUint)mostPrefIndex ||
sl@0
  6056
		gZoneUtilArray[leastPrefIndex].iFreePages == 0)
sl@0
  6057
		{
sl@0
  6058
		test.Printf(_L("leastPrefIndex = mostPrefIndex or leastPrefIndex(%d) / mostPrefIndex(%d) has 0 free - Skipping test step...\n"), 
sl@0
  6059
						leastPrefIndex, mostPrefIndex);
sl@0
  6060
		goto skipSetup;
sl@0
  6061
		}
sl@0
  6062
sl@0
  6063
sl@0
  6064
	// Zone alloc 1 fixed page into the least preferable zone
sl@0
  6065
	leastPrefZoneID = gZoneConfigArray[leastPrefIndex].iZoneId;	
sl@0
  6066
	GetOriginalPageCount();
sl@0
  6067
	switch(aZoneAllocType)
sl@0
  6068
		{
sl@0
  6069
		case Z_ALLOC_DISC:
sl@0
  6070
			{
sl@0
  6071
			r = Ldd.ZoneAllocDiscontiguous(leastPrefZoneID, 1);
sl@0
  6072
			break;
sl@0
  6073
			}
sl@0
  6074
sl@0
  6075
		case Z_ALLOC_CONTIG:
sl@0
  6076
			{			
sl@0
  6077
			r = Ldd.ZoneAllocContiguous(leastPrefZoneID, gPageSize);
sl@0
  6078
			break;
sl@0
  6079
			}
sl@0
  6080
		}
sl@0
  6081
	GetAllPageInfo();
sl@0
  6082
	if (r != KErrNone ||
sl@0
  6083
		gZoneUtilArray[leastPrefIndex].iAllocFixed <= gOriginalPageCountArray[leastPrefIndex].iAllocFixed)
sl@0
  6084
		{
sl@0
  6085
		test.Printf(_L("Failed to allocate 1 fixed page to zone index %d r = %d\n"), leastPrefIndex, r);
sl@0
  6086
		goto skipSetup;
sl@0
  6087
		}
sl@0
  6088
	test.Printf(_L("leastPrefIndex = %d mostPrefIndex = %d\n"), leastPrefIndex, mostPrefIndex);
sl@0
  6089
	switch(aPageType)
sl@0
  6090
		{
sl@0
  6091
		case BEST_MOVABLE:
sl@0
  6092
			// now allocate 1 movable page
sl@0
  6093
			r = AllocMovable(gChunkArray1, gChunkArraySize1, 1, gPageSize);
sl@0
  6094
			if (r != KErrNone)
sl@0
  6095
				{
sl@0
  6096
				test.Printf(_L("Failed to allocate 1 movable page r = %d\n"), r);
sl@0
  6097
				goto skipSetup;
sl@0
  6098
				}
sl@0
  6099
			break;
sl@0
  6100
			
sl@0
  6101
		case BEST_DISCARDABLE:
sl@0
  6102
			// now allocate 1 discardable page
sl@0
  6103
			TInt disPages;					
sl@0
  6104
			TUint disBytes = (gTotalPageCount.iDiscardablePages + 1) << gPageShift;
sl@0
  6105
			r = AllocDiscardable(disPages, disBytes);
sl@0
  6106
			if (r != KErrNone)
sl@0
  6107
				{
sl@0
  6108
				test.Printf(_L("Failed to allocate 1 discardable page r = %d\n"), r);
sl@0
  6109
				goto skipSetup;
sl@0
  6110
				}
sl@0
  6111
			break;
sl@0
  6112
		}
sl@0
  6113
	
sl@0
  6114
	GetAllPageInfo();
sl@0
  6115
	switch(aPageType)
sl@0
  6116
		{
sl@0
  6117
		case BEST_MOVABLE:
sl@0
  6118
			if (gOriginalPageCountArray[mostPrefIndex].iAllocMovable >= gZoneUtilArray[mostPrefIndex].iAllocMovable ||
sl@0
  6119
				gOriginalPageCountArray[leastPrefIndex].iAllocMovable < gZoneUtilArray[leastPrefIndex].iAllocMovable)
sl@0
  6120
				{
sl@0
  6121
				test.Printf(_L("FAIL: mostPrefIndex(%d) origMov 0x%x curMov 0x%x leastPrefIndex(%d) origMov 0x%x curMov 0x%x\n"), 
sl@0
  6122
								mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocMovable, gZoneUtilArray[mostPrefIndex].iAllocMovable, 
sl@0
  6123
								leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocMovable, gZoneUtilArray[leastPrefIndex].iAllocMovable);
sl@0
  6124
				CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  6125
				retVal = KErrGeneral;
sl@0
  6126
				}
sl@0
  6127
			else
sl@0
  6128
				{
sl@0
  6129
				test.Printf(_L("Passed...\n"));
sl@0
  6130
				}
sl@0
  6131
			break;
sl@0
  6132
sl@0
  6133
			
sl@0
  6134
		case BEST_DISCARDABLE:
sl@0
  6135
			if (gOriginalPageCountArray[mostPrefIndex].iAllocDiscardable >= gZoneUtilArray[mostPrefIndex].iAllocDiscardable ||
sl@0
  6136
				gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable < gZoneUtilArray[leastPrefIndex].iAllocDiscardable)
sl@0
  6137
				{
sl@0
  6138
				test.Printf(_L("FAIL: mostPrefIndex(%d) origDis 0x%x curDis 0x%x leastPrefIndex(%d) origDis 0x%x curDis 0x%x\n"), 
sl@0
  6139
								mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocDiscardable, gZoneUtilArray[mostPrefIndex].iAllocDiscardable, 
sl@0
  6140
								leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable, gZoneUtilArray[leastPrefIndex].iAllocDiscardable);
sl@0
  6141
				CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  6142
				retVal = KErrGeneral;
sl@0
  6143
				}
sl@0
  6144
			else
sl@0
  6145
				{
sl@0
  6146
				test.Printf(_L("Passed...\n"));
sl@0
  6147
				}
sl@0
  6148
			break;
sl@0
  6149
		}
sl@0
  6150
	
sl@0
  6151
	// This will clean up any fixed pages allocated.
sl@0
  6152
skipSetup:
sl@0
  6153
	TestEnd();
sl@0
  6154
	return retVal;
sl@0
  6155
	}
sl@0
  6156
		
sl@0
  6157
//
sl@0
  6158
// TestZoneAllocContiguous
sl@0
  6159
//
sl@0
  6160
//---------------------------------------------------------------------------------------------------------------------
sl@0
  6161
//! @SYMTestCaseID				KBASE-t_ramdefrag-0535
sl@0
  6162
//! @SYMTestType				CIT
sl@0
  6163
//! @SYMTestCaseDesc			Verifying the contiguous overload of Epoc::ZoneAllocPhysicalRam().
sl@0
  6164
//! @SYMPREQ					PREQ308
sl@0
  6165
//! @SYMTestPriority			High
sl@0
  6166
//! @SYMTestActions				
sl@0
  6167
//! 	1.	Call function with a valid aZoneID.  
sl@0
  6168
//! 	2.	Call function with an invalid aZoneID
sl@0
  6169
//! 	3.	Call function with aSize > zone size 
sl@0
  6170
//!		4.	Call multiple RAM zone overload of the function with a mix of valid and invalid IDs.
sl@0
  6171
//!		5.	Call multiple RAM zone overload of the function with contiguous RAM zones and attempt
sl@0
  6172
//!			to allocate over both RAM zones.
sl@0
  6173
//!		6.	Call function on a RAM zone that has some non-DP pages allocated into it and ask
sl@0
  6174
//!			for the whole RAM zone to be filled with fixed pages.
sl@0
  6175
//!		7.	Get the most preferable zone for movable page allocations (mostPref). Allocate 1 contiguous fixed page 
sl@0
  6176
//!			in the least preferable zone. Following this allocate 1 movable page
sl@0
  6177
//!		8.	Get the most preferable zone for discardable page allocations (mostPref). Allocate 1 contiguous fixed 
sl@0
  6178
//!			page in the least preferable zone. Following this allocate 1 discardable page
sl@0
  6179
//! 
sl@0
  6180
//! @SYMTestExpectedResults
sl@0
  6181
//! 	1.	KErrNone
sl@0
  6182
//! 	2.	KErrArgument 
sl@0
  6183
//! 	3.	KErrArgument
sl@0
  6184
//!		4.	KErrArgument
sl@0
  6185
//!		5.	KErrNone
sl@0
  6186
//!		6.	KErrNoMemory
sl@0
  6187
//!		7.	Movable pages are allocated into zone mostPref.
sl@0
  6188
//!		8.	Discardable pages are allocated into zone mostPref
sl@0
  6189
//---------------------------------------------------------------------------------------------------------------------
sl@0
  6190
TInt TestZoneAllocContiguous()
sl@0
  6191
	{
sl@0
  6192
	TInt r = 0;
sl@0
  6193
	test.Start(_L("Test1: Call function Epoc::ZoneAllocPhysicalRam() with a valid aZoneID"));	
sl@0
  6194
	TestStart();	
sl@0
  6195
		
sl@0
  6196
	const TUint KAllocPages = 2;
sl@0
  6197
	
sl@0
  6198
	GetOriginalPageCount();
sl@0
  6199
	GetAllPageInfo();
sl@0
  6200
	TUint index = gZoneCount - 1;
sl@0
  6201
	while (index > 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
sl@0
  6202
		{
sl@0
  6203
		-- index;
sl@0
  6204
		}
sl@0
  6205
	TUint zoneID = gZoneConfigArray[index].iZoneId;
sl@0
  6206
	if (index == 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
sl@0
  6207
		{
sl@0
  6208
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  6209
		}
sl@0
  6210
	else
sl@0
  6211
		{
sl@0
  6212
		TUint allocBytes = KAllocPages << gPageShift;
sl@0
  6213
		r = Ldd.ZoneAllocContiguous(zoneID, allocBytes);
sl@0
  6214
		GetAllPageInfo();
sl@0
  6215
		if (r == KErrNone && 
sl@0
  6216
			gZoneUtilArray[index].iAllocFixed - gOriginalPageCountArray[index].iAllocFixed == KAllocPages)
sl@0
  6217
			{
sl@0
  6218
			test.Printf(_L("Pass: Correct number of fixed pages allocated to zone ID 0x%x\n"),zoneID);
sl@0
  6219
			}
sl@0
  6220
		else
sl@0
  6221
			{
sl@0
  6222
			test.Printf(_L("Fail: r = %d, expected = %d, or number of pages allocated is not expected\n"), 
sl@0
  6223
							r, KErrNone);
sl@0
  6224
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  6225
			TEST_FAIL;
sl@0
  6226
			}
sl@0
  6227
sl@0
  6228
		Ldd.FreeAllFixedPages();
sl@0
  6229
		}
sl@0
  6230
	TestEnd();
sl@0
  6231
sl@0
  6232
sl@0
  6233
	test.Next(_L("Test2: Call function Epoc::ZoneAllocPhysicalRam() with an invalid aZoneID"));	
sl@0
  6234
	TestStart();	
sl@0
  6235
	
sl@0
  6236
	zoneID = KInvalidZoneID; 
sl@0
  6237
 
sl@0
  6238
	TESTDEBUG(test.Printf(_L("zoneID = 0x%x\n"), zoneID));
sl@0
  6239
	
sl@0
  6240
	r = Ldd.ZoneAllocContiguous(zoneID, gPageSize);
sl@0
  6241
	GetAllPageInfo();
sl@0
  6242
	if (r == KErrArgument)
sl@0
  6243
		{
sl@0
  6244
		test.Printf(_L("Pass: Correct return value\n"));
sl@0
  6245
		}
sl@0
  6246
	else
sl@0
  6247
		{
sl@0
  6248
		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
sl@0
  6249
		CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  6250
		TEST_FAIL;
sl@0
  6251
		}
sl@0
  6252
	Ldd.FreeAllFixedPages();
sl@0
  6253
	TestEnd();
sl@0
  6254
sl@0
  6255
sl@0
  6256
	test.Next(_L("Test3: Call function Epoc::ZoneAllocPhysicalRam() with aSize > zone size"));	
sl@0
  6257
	TestStart();	
sl@0
  6258
	
sl@0
  6259
	index = gZoneCount - 1;
sl@0
  6260
	zoneID = gZoneConfigArray[index].iZoneId;
sl@0
  6261
sl@0
  6262
	TUint allocBytes = (gZoneUtilArray[index].iPhysPages + 5)  << gPageShift;
sl@0
  6263
	
sl@0
  6264
	r = Ldd.ZoneAllocContiguous(zoneID, allocBytes);
sl@0
  6265
	GetAllPageInfo();
sl@0
  6266
	if (r == KErrArgument)
sl@0
  6267
		{
sl@0
  6268
		test.Printf(_L("Pass: Correct return value and number of pages allocated is correct\n"));
sl@0
  6269
		}
sl@0
  6270
	else
sl@0
  6271
		{
sl@0
  6272
		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
sl@0
  6273
		CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  6274
		TEST_FAIL;
sl@0
  6275
		}
sl@0
  6276
	Ldd.FreeAllFixedPages();
sl@0
  6277
	TestEnd();
sl@0
  6278
sl@0
  6279
	const TUint KMultiZoneIds = 10;
sl@0
  6280
	TUint* zoneIdArray = new TUint[KMultiZoneIds];
sl@0
  6281
	test_NotNull(zoneIdArray);
sl@0
  6282
sl@0
  6283
	test.Next(_L("Test4: Test Epoc::ZoneAllocPhysicaRam() always fails when at least one ID is invalid"));
sl@0
  6284
	TestStart();
sl@0
  6285
sl@0
  6286
	index = gZoneCount - 1;
sl@0
  6287
	while (index > 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
sl@0
  6288
		{
sl@0
  6289
		-- index;
sl@0
  6290
		}
sl@0
  6291
	if (index == 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
sl@0
  6292
		{
sl@0
  6293
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  6294
		}
sl@0
  6295
	else
sl@0
  6296
		{
sl@0
  6297
		TUint zoneIdSize = 2;
sl@0
  6298
		zoneIdArray[0] = gZoneConfigArray[index].iZoneId;
sl@0
  6299
		zoneIdArray[1] = KInvalidZoneID;
sl@0
  6300
		TUint allocBytes = KAllocPages << gPageShift;
sl@0
  6301
		r = Ldd.MultiZoneAllocContiguous(zoneIdArray, zoneIdSize, allocBytes);
sl@0
  6302
		if (r != KErrArgument)
sl@0
  6303
			{
sl@0
  6304
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  6305
			TEST_FAIL;
sl@0
  6306
			}
sl@0
  6307
		else
sl@0
  6308
			{
sl@0
  6309
			test.Printf(_L("Pass: Correct return value\n"));
sl@0
  6310
			}
sl@0
  6311
		}
sl@0
  6312
sl@0
  6313
	TestEnd();
sl@0
  6314
sl@0
  6315
	test.Next(_L("Test5: Test Epoc::ZoneAllocPhysicalRam() can span multiple RAM zones"));
sl@0
  6316
	TestStart();
sl@0
  6317
	// Attempt to find to physically contiguous RAM zones where higher addressed
sl@0
  6318
	// one is empty, relies on RAM zones are returned by HAL functions in 
sl@0
  6319
	// ascending physical address order.
sl@0
  6320
	GetAllPageInfo();
sl@0
  6321
	TBool zonesFound = EFalse;
sl@0
  6322
	index = gZoneCount - 1;
sl@0
  6323
	for (; index > 1; index--)
sl@0
  6324
		{
sl@0
  6325
		if (gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages &&
sl@0
  6326
			gZoneUtilArray[index-1].iFreePages == gZoneUtilArray[index-1].iPhysPages &&
sl@0
  6327
			gZoneConfigArray[index].iPhysBase - 1 == gZoneConfigArray[index-1].iPhysEnd)
sl@0
  6328
			{
sl@0
  6329
			zonesFound = ETrue;
sl@0
  6330
			break;
sl@0
  6331
			}
sl@0
  6332
		}
sl@0
  6333
sl@0
  6334
	if (!zonesFound)
sl@0
  6335
		{
sl@0
  6336
		test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
sl@0
  6337
		}
sl@0
  6338
	else
sl@0
  6339
		{
sl@0
  6340
		// Allocate one page more than the first RAM zone to force the allocation
sl@0
  6341
		// to spread over both RAM zones.
sl@0
  6342
		TUint allocPages = gZoneUtilArray[index-1].iPhysPages + 1;
sl@0
  6343
		TUint allocBytes = allocPages << gPageShift;
sl@0
  6344
sl@0
  6345
		// Attempt to find a zone that has less free pages than the allocation
sl@0
  6346
		// size so we can test that the method continues past too full RAM zones.
sl@0
  6347
		zonesFound = EFalse;
sl@0
  6348
		TUint noAllocZone = 0;
sl@0
  6349
		for (; noAllocZone < gZoneCount; noAllocZone++)
sl@0
  6350
			{
sl@0
  6351
			if (allocPages > gZoneUtilArray[noAllocZone].iFreePages &&
sl@0
  6352
				noAllocZone != index && noAllocZone != index-1)
sl@0
  6353
				{
sl@0
  6354
				zonesFound = ETrue;
sl@0
  6355
				break;
sl@0
  6356
				} 
sl@0
  6357
			}
sl@0
  6358
		TUint zoneIds = 2;
sl@0
  6359
		if (!zonesFound)
sl@0
  6360
			{
sl@0
  6361
			zoneIdArray[0] = gZoneConfigArray[index-1].iZoneId;
sl@0
  6362
			zoneIdArray[1] = gZoneConfigArray[index].iZoneId;
sl@0
  6363
			}
sl@0
  6364
		else
sl@0
  6365
			{// Have a zone that won't meet the allocation so use it
sl@0
  6366
			TESTDEBUG(test.Printf(_L("noAllocZone ID %x\n"), gZoneConfigArray[noAllocZone].iZoneId));
sl@0
  6367
			zoneIds++;
sl@0
  6368
			zoneIdArray[0] = gZoneConfigArray[noAllocZone].iZoneId;
sl@0
  6369
			zoneIdArray[1] = gZoneConfigArray[index-1].iZoneId;
sl@0
  6370
			zoneIdArray[2] = gZoneConfigArray[index].iZoneId;
sl@0
  6371
			}
sl@0
  6372
sl@0
  6373
		r = Ldd.MultiZoneAllocContiguous(zoneIdArray, zoneIds, allocBytes);
sl@0
  6374
sl@0
  6375
		GetAllPageInfo();
sl@0
  6376
		if (r != KErrNone || 
sl@0
  6377
			gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
sl@0
  6378
			gZoneUtilArray[index-1].iFreePages == gZoneUtilArray[index-1].iPhysPages)
sl@0
  6379
			{// The allocation failed.
sl@0
  6380
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  6381
			TEST_FAIL;
sl@0
  6382
			}
sl@0
  6383
		else
sl@0
  6384
			{
sl@0
  6385
			test.Printf(_L("Passed...\n"));
sl@0
  6386
			}
sl@0
  6387
		}
sl@0
  6388
	// TestEnd() will free the allocation of fixed pages.
sl@0
  6389
	TestEnd();
sl@0
  6390
sl@0
  6391
	test.Next(_L("Test6: Test Epoc::ZoneAllocPhysicalRam() returns KErrNoMemory when appropriate"));
sl@0
  6392
	TestStart();
sl@0
  6393
	// Attempt to find a RAM zone with some non-discarable pages allocated into it.
sl@0
  6394
	// (At time of writing discardable pages wouldn't be discarded on demand by 
sl@0
  6395
	// this function but this may be changed in the future as discontiguous case does that).
sl@0
  6396
	GetAllPageInfo();
sl@0
  6397
	zonesFound = EFalse;
sl@0
  6398
	index = gZoneCount - 1;
sl@0
  6399
	for (; index > 0; index--)
sl@0
  6400
		{
sl@0
  6401
		if (gZoneUtilArray[index].iFreePages && 
sl@0
  6402
			(gZoneUtilArray[index].iAllocMovable || gZoneUtilArray[index].iAllocFixed))
sl@0
  6403
			{
sl@0
  6404
			zonesFound = ETrue;
sl@0
  6405
			break;
sl@0
  6406
			}
sl@0
  6407
		}
sl@0
  6408
	
sl@0
  6409
	if (!zonesFound)
sl@0
  6410
		{
sl@0
  6411
		test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));		
sl@0
  6412
		}
sl@0
  6413
	else
sl@0
  6414
		{
sl@0
  6415
		// Attempt to allocate the whole of the RAM zone.
sl@0
  6416
		GetOriginalPageCount();
sl@0
  6417
		TUint allocBytes = gZoneConfigArray[index].iPhysPages << gPageShift;
sl@0
  6418
		r = Ldd.ZoneAllocContiguous(gZoneConfigArray[index].iZoneId, allocBytes);
sl@0
  6419
sl@0
  6420
		// The allocation should have failed and no pages should have
sl@0
  6421
		// been allocated.
sl@0
  6422
		GetAllPageInfo();
sl@0
  6423
		if (r != KErrNoMemory ||
sl@0
  6424
			gOriginalPageCountArray[index].iFreePages > gZoneUtilArray[index].iFreePages)
sl@0
  6425
			{
sl@0
  6426
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  6427
			TEST_FAIL;
sl@0
  6428
			}
sl@0
  6429
		else
sl@0
  6430
			{
sl@0
  6431
			test.Printf(_L("Pass: Correct return value\n"));
sl@0
  6432
			}
sl@0
  6433
		}
sl@0
  6434
	// TestEnd() will free the allocation of fixed pages.
sl@0
  6435
	TestEnd();
sl@0
  6436
	delete[] zoneIdArray;
sl@0
  6437
sl@0
  6438
	
sl@0
  6439
	test.Next(_L("Test7: Test Epoc::ZoneAllocPhysicalRam() (Contiguous) doesn't affect the allocation of movable pages"));
sl@0
  6440
	r = TestZoneAllocNoAffect(Z_ALLOC_CONTIG, BEST_MOVABLE);
sl@0
  6441
	if (r != KErrNone)
sl@0
  6442
		{
sl@0
  6443
		TEST_FAIL;
sl@0
  6444
		}
sl@0
  6445
sl@0
  6446
	test.Next(_L("Test8: Test Epoc::ZoneAllocPhysicalRam() (Contiguous) doesn't affect the allocation of discardable pages"));
sl@0
  6447
	r = TestZoneAllocNoAffect(Z_ALLOC_CONTIG, BEST_DISCARDABLE);
sl@0
  6448
	if (r != KErrNone)
sl@0
  6449
		{
sl@0
  6450
		TEST_FAIL;
sl@0
  6451
		}
sl@0
  6452
sl@0
  6453
	test.End();
sl@0
  6454
	return KErrNone;
sl@0
  6455
	}
sl@0
  6456
sl@0
  6457
sl@0
  6458
//
sl@0
  6459
// TestZoneAllocDiscontiguous
sl@0
  6460
//
sl@0
  6461
//---------------------------------------------------------------------------------------------------------------------
sl@0
  6462
//! @SYMTestCaseID				KBASE-t_ramdefrag-0536
sl@0
  6463
//! @SYMTestType				CIT
sl@0
  6464
//! @SYMTestCaseDesc			Verifying the discontiguous overload of Epoc::ZoneAllocPhysicalRam().
sl@0
  6465
//! @SYMPREQ					PREQ308
sl@0
  6466
//! @SYMTestPriority			High
sl@0
  6467
//! @SYMTestActions				
sl@0
  6468
//! 	1.	Call function with a valid aZoneID.  
sl@0
  6469
//! 	2.	Call function with an invalid aZoneID
sl@0
  6470
//! 	3.	Call function with aNumPages > zone size 
sl@0
  6471
//!		4.	Call multiple RAM zone overload of function with a mix of valid and invalid IDs
sl@0
  6472
//!		5.	Call multiple RAM zone overload of function so that the allocation will have to
sl@0
  6473
//!			span multiple RAM zones.
sl@0
  6474
//!		6.	Call function with memory full with DP cache that has reached it's 
sl@0
  6475
//!			minimum cache size.
sl@0
  6476
//!		7.	Call function with memory not quite full with DP cache that has reached it's 
sl@0
  6477
//!			minimum cache size and with the specified RAM zone full.
sl@0
  6478
//!		8.	Call function to allocate a whole RAM zone on a RAM zone that has non-discardable
sl@0
  6479
//!			pages already allocated into it.
sl@0
  6480
//!		9.	Call function to allocate one less than the whole RAM zone on a RAM zone that has movable 
sl@0
  6481
//!			pages allocated.
sl@0
  6482
//!		10.	Get the most preferable zone for movable page allocations (mostPref). Allocate 1 discontiguous fixed page 
sl@0
  6483
//!			in the least preferable zone. Following this allocate 1 movable page
sl@0
  6484
//!		11.	Get the most preferable zone for discardable page allocations (mostPref). Allocate 1 discontiguous fixed 
sl@0
  6485
//!			page in the least preferable zone. Following this allocate 1 discardable page
sl@0
  6486
//! 
sl@0
  6487
//! @SYMTestExpectedResults
sl@0
  6488
//! 	1.	KErrNone
sl@0
  6489
//! 	2.	KErrArgument
sl@0
  6490
//! 	3.	KErrArgument
sl@0
  6491
//!		4.	KErrArgument
sl@0
  6492
//!		5.	KErrNone
sl@0
  6493
//!		6.	KErrNoMemory
sl@0
  6494
//!		7.	KErrNone
sl@0
  6495
//!		8. 	KErrNoMemory
sl@0
  6496
//!		9.	KErrNone (i.e. the movable pages are shifted out of the way but only those that need to be moved).
sl@0
  6497
//!		10.	Movable pages are allocated into zone mostPref.
sl@0
  6498
//!		11.	Discardable pages are allocated into zone mostPref
sl@0
  6499
//---------------------------------------------------------------------------------------------------------------------
sl@0
  6500
TInt TestZoneAllocDiscontiguous()
sl@0
  6501
	{
sl@0
  6502
	TInt r = KErrNone;
sl@0
  6503
	test.Start(_L("Test1: Call function Epoc::ZoneAllocPhysicalRam() with a valid aZoneID"));	
sl@0
  6504
	TestStart();	
sl@0
  6505
	
sl@0
  6506
	const TUint KAllocPages = 5;
sl@0
  6507
	// Detemine how many extra pages the kernel heap may grow by
sl@0
  6508
	// as these may need to be accounted for.
sl@0
  6509
	TUint fixedOverhead = Ldd.GetAllocDiff(KAllocPages);
sl@0
  6510
sl@0
  6511
	GetOriginalPageCount();
sl@0
  6512
	GetAllPageInfo();
sl@0
  6513
	TUint index = gZoneCount - 1;
sl@0
  6514
	while (index > 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
sl@0
  6515
		{
sl@0
  6516
		-- index;
sl@0
  6517
		}
sl@0
  6518
sl@0
  6519
	if (gZoneUtilArray[index].iFreePages < KAllocPages || gTotalPageCount.iFreePages < KAllocPages + fixedOverhead)
sl@0
  6520
		{
sl@0
  6521
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  6522
		}
sl@0
  6523
	else
sl@0
  6524
		{
sl@0
  6525
		// Allocate KAllocPages discontiguous fixed pages into RAM zone zoneID 
sl@0
  6526
		// and verfiy that the pages were allocated to the correct zone, allow for
sl@0
  6527
		// extra fixed pages to be allocated as the kernel heap may grow.
sl@0
  6528
		TUint zoneID = gZoneConfigArray[index].iZoneId;
sl@0
  6529
		r = Ldd.ZoneAllocDiscontiguous(zoneID, KAllocPages);
sl@0
  6530
sl@0
  6531
		GetAllPageInfo();
sl@0
  6532
		if ((r == KErrNone) && 
sl@0
  6533
			(gZoneUtilArray[index].iAllocFixed >= gOriginalPageCountArray[index].iAllocFixed + KAllocPages))
sl@0
  6534
			{
sl@0
  6535
			test.Printf(_L("Pass: Correct return value and number of pages allocated is correct\n"));
sl@0
  6536
			}
sl@0
  6537
		else
sl@0
  6538
			{
sl@0
  6539
			test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
sl@0
  6540
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  6541
			TEST_FAIL;
sl@0
  6542
			}
sl@0
  6543
		Ldd.FreeAllFixedPages();
sl@0
  6544
		}	
sl@0
  6545
	TestEnd();
sl@0
  6546
sl@0
  6547
sl@0
  6548
	test.Next(_L("Test2: Call function Epoc::ZoneAllocPhysicalRam() with an invalid aZoneID"));	
sl@0
  6549
	TestStart();	
sl@0
  6550
	
sl@0
  6551
	TUint zoneID = KInvalidZoneID;
sl@0
  6552
	
sl@0
  6553
	r = Ldd.ZoneAllocDiscontiguous(zoneID, KAllocPages);
sl@0
  6554
	GetAllPageInfo();
sl@0
  6555
	if (r == KErrArgument)
sl@0
  6556
		{
sl@0
  6557
		test.Printf(_L("Pass: Correct return value\n"));
sl@0
  6558
		}
sl@0
  6559
	else
sl@0
  6560
		{
sl@0
  6561
		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
sl@0
  6562
		CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  6563
		TEST_FAIL;
sl@0
  6564
		}
sl@0
  6565
	Ldd.FreeAllFixedPages();
sl@0
  6566
	TestEnd();
sl@0
  6567
sl@0
  6568
	test.Next(_L("Test3: Call function Epoc::ZoneAllocPhysicalRam() when aNumPages > zone size"));	
sl@0
  6569
	TestStart();	
sl@0
  6570
	
sl@0
  6571
	GetAllPageInfo();
sl@0
  6572
	index = gZoneCount - 1;
sl@0
  6573
	zoneID = gZoneConfigArray[index].iZoneId;
sl@0
  6574
	
sl@0
  6575
	TUint allocPages = gZoneUtilArray[index].iPhysPages + 1;
sl@0
  6576
	r = Ldd.ZoneAllocDiscontiguous(zoneID, allocPages);
sl@0
  6577
	
sl@0
  6578
	GetAllPageInfo();
sl@0
  6579
	if (r == KErrArgument)
sl@0
  6580
		{
sl@0
  6581
		test.Printf(_L("Pass: Correct return value and number of pages allocated is correct\n"));
sl@0
  6582
		}
sl@0
  6583
	else
sl@0
  6584
		{
sl@0
  6585
		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
sl@0
  6586
		CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  6587
		TEST_FAIL;
sl@0
  6588
		}
sl@0
  6589
	Ldd.FreeAllFixedPages();
sl@0
  6590
	TestEnd();
sl@0
  6591
sl@0
  6592
	const TUint KMultiZoneIds = 10;
sl@0
  6593
	TUint* zoneIdArray = new TUint[KMultiZoneIds];
sl@0
  6594
	test_NotNull(zoneIdArray);
sl@0
  6595
sl@0
  6596
	test.Next(_L("Test4: Test Epoc::ZoneAllocPhysicaRam() always fails when at least one ID is invalid"));
sl@0
  6597
	TestStart();
sl@0
  6598
sl@0
  6599
	TBool zonesFound = EFalse;
sl@0
  6600
	index = gZoneCount - 1;
sl@0
  6601
	for (; index > 0; index--)
sl@0
  6602
		{
sl@0
  6603
		if (gZoneUtilArray[index].iFreePages >= KAllocPages)
sl@0
  6604
			{
sl@0
  6605
			zonesFound = ETrue;
sl@0
  6606
			break;
sl@0
  6607
			}
sl@0
  6608
		}
sl@0
  6609
	if (!zonesFound)
sl@0
  6610
		{
sl@0
  6611
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  6612
		}
sl@0
  6613
	else
sl@0
  6614
		{
sl@0
  6615
		TUint zoneIdSize = 2;
sl@0
  6616
		zoneIdArray[0] = gZoneConfigArray[index].iZoneId;
sl@0
  6617
		zoneIdArray[1] = KInvalidZoneID;
sl@0
  6618
		r = Ldd.MultiZoneAllocDiscontiguous(zoneIdArray, zoneIdSize, KAllocPages);
sl@0
  6619
		if (r != KErrArgument)
sl@0
  6620
			{// Make sure we cleanup.
sl@0
  6621
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  6622
			TEST_FAIL;
sl@0
  6623
			}
sl@0
  6624
		else
sl@0
  6625
			{
sl@0
  6626
			test.Printf(_L("Passed...\n"));	
sl@0
  6627
			}
sl@0
  6628
		}
sl@0
  6629
sl@0
  6630
	TestEnd();
sl@0
  6631
sl@0
  6632
	test.Next(_L("Test5: Test Epoc::ZoneAllocPhysicalRam() can span multiple RAM zones"));
sl@0
  6633
	TestStart();
sl@0
  6634
	zonesFound = EFalse;
sl@0
  6635
	TUint zonesCount = 0;
sl@0
  6636
	const TUint KTest5Zones = 2;
sl@0
  6637
	TUint zoneIndices[KTest5Zones];
sl@0
  6638
	allocPages = 0;
sl@0
  6639
sl@0
  6640
	// Attempt to find KTest5Zones RAM zones with some free pages,
sl@0
  6641
	// search in reverse preference order to reduce chances of kernel heap pages
sl@0
  6642
	// being allocated into the RAM zones under test.
sl@0
  6643
	GetPrefOrder();
sl@0
  6644
	index = gZoneCount - 1;
sl@0
  6645
	for (; index > 0; index--)
sl@0
  6646
		{
sl@0
  6647
		TUint prefIndex = gPrefArray[index];
sl@0
  6648
		if (gZoneUtilArray[prefIndex].iFreePages != 0)
sl@0
  6649
			{
sl@0
  6650
			allocPages += gZoneUtilArray[prefIndex].iFreePages;
sl@0
  6651
			zoneIndices[zonesCount++] = prefIndex;
sl@0
  6652
			if (zonesCount == KTest5Zones)
sl@0
  6653
				{
sl@0
  6654
				zonesFound = ETrue;
sl@0
  6655
				break;
sl@0
  6656
				}
sl@0
  6657
			}
sl@0
  6658
		}
sl@0
  6659
sl@0
  6660
	if (!zonesFound)
sl@0
  6661
		{
sl@0
  6662
		test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
sl@0
  6663
		}
sl@0
  6664
	else
sl@0
  6665
		{
sl@0
  6666
		// Attempt to find a zone that has less free pages than the allocation
sl@0
  6667
		// size so we can test that the method continues past too full RAM zones.
sl@0
  6668
		zonesFound = EFalse;
sl@0
  6669
		TUint noAllocZone = 0;
sl@0
  6670
		for (; noAllocZone < gZoneCount; noAllocZone++)
sl@0
  6671
			{
sl@0
  6672
			if (allocPages > gZoneUtilArray[noAllocZone].iFreePages &&
sl@0
  6673
				noAllocZone != zoneIndices[0] && noAllocZone != zoneIndices[1])
sl@0
  6674
				{
sl@0
  6675
				zonesFound = ETrue;
sl@0
  6676
				break;
sl@0
  6677
				}
sl@0
  6678
			}
sl@0
  6679
		
sl@0
  6680
		if (gPagedRom)
sl@0
  6681
			{// Fill memory with DP pages to test the allocation will discard
sl@0
  6682
			// pages when necessary.
sl@0
  6683
			TInt discard;
sl@0
  6684
			r = AllocDiscardable(discard);
sl@0
  6685
			GetAllPageInfo();
sl@0
  6686
			if (r != KErrNone ||
sl@0
  6687
				gZoneUtilArray[zoneIndices[0]].iFreePages != 0 || 
sl@0
  6688
				gZoneUtilArray[zoneIndices[1]].iFreePages != 0)
sl@0
  6689
				{
sl@0
  6690
				test.Printf(_L("r %d\n"), r);
sl@0
  6691
				CLEANUP(ResetDPCache());
sl@0
  6692
				TEST_FAIL;
sl@0
  6693
				}			
sl@0
  6694
			}
sl@0
  6695
sl@0
  6696
		TUint zoneIds = KTest5Zones;
sl@0
  6697
		if (!zonesFound)
sl@0
  6698
			{
sl@0
  6699
			zoneIdArray[0] = gZoneConfigArray[zoneIndices[0]].iZoneId;
sl@0
  6700
			zoneIdArray[1] = gZoneConfigArray[zoneIndices[1]].iZoneId;
sl@0
  6701
			}
sl@0
  6702
		else
sl@0
  6703
			{// Have a zone that won't meet the allocation so use it
sl@0
  6704
			TESTDEBUG(test.Printf(_L("noAllocZone ID %x\n"), gZoneConfigArray[noAllocZone].iZoneId));
sl@0
  6705
			zoneIds++;
sl@0
  6706
			zoneIdArray[0] = gZoneConfigArray[noAllocZone].iZoneId;
sl@0
  6707
			zoneIdArray[1] = gZoneConfigArray[zoneIndices[0]].iZoneId;
sl@0
  6708
			zoneIdArray[2] = gZoneConfigArray[zoneIndices[1]].iZoneId;
sl@0
  6709
			}
sl@0
  6710
sl@0
  6711
		// Adjust the allocation size for any kernel heap pages that may be
sl@0
  6712
		// required as they may get allocated into the RAM zones under test.
sl@0
  6713
		allocPages -= Ldd.GetAllocDiff(allocPages);
sl@0
  6714
sl@0
  6715
		GetOriginalPageCount();
sl@0
  6716
		r = Ldd.MultiZoneAllocDiscontiguous(zoneIdArray, zoneIds, allocPages);
sl@0
  6717
sl@0
  6718
		GetAllPageInfo();
sl@0
  6719
		if (r != KErrNone ||
sl@0
  6720
			gZoneUtilArray[zoneIndices[0]].iAllocFixed <= gOriginalPageCountArray[zoneIndices[0]].iAllocFixed ||
sl@0
  6721
			gZoneUtilArray[zoneIndices[1]].iAllocFixed <= gOriginalPageCountArray[zoneIndices[1]].iAllocFixed)
sl@0
  6722
			{// The allocation failed.
sl@0
  6723
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  6724
			TEST_FAIL;
sl@0
  6725
			}
sl@0
  6726
		else
sl@0
  6727
			{
sl@0
  6728
			test.Printf(_L("Passed...\n"));	
sl@0
  6729
			}
sl@0
  6730
		}
sl@0
  6731
	// TestEnd() will free the allocation of fixed pages.
sl@0
  6732
	TestEnd();
sl@0
  6733
sl@0
  6734
sl@0
  6735
	if (gPagedRom)
sl@0
  6736
		{
sl@0
  6737
		test.Next(_L("Test6: Test Epoc::ZoneAllocPhysicalRam() returns KErrNoMemory when DP minimum cache size is hit"));
sl@0
  6738
		TestStart();
sl@0
  6739
		GetPrefOrder();
sl@0
  6740
		TUint zoneFreePages = 0;
sl@0
  6741
		index = gZoneCount - 1;
sl@0
  6742
		for (; index > 0; index--)
sl@0
  6743
			{
sl@0
  6744
			TUint prefIndex = gPrefArray[index];
sl@0
  6745
			zoneFreePages = gZoneUtilArray[prefIndex].iFreePages;
sl@0
  6746
			if (zoneFreePages > 2)
sl@0
  6747
				{
sl@0
  6748
				index = prefIndex;
sl@0
  6749
				zonesFound = ETrue;
sl@0
  6750
				break;
sl@0
  6751
				}
sl@0
  6752
			}
sl@0
  6753
sl@0
  6754
		if (!zonesFound)
sl@0
  6755
			{
sl@0
  6756
			test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
sl@0
  6757
			}
sl@0
  6758
		else
sl@0
  6759
			{
sl@0
  6760
			// Fill the RAM with DP cache pages and up the minimum cache size so
sl@0
  6761
			// that the allocation will fail.
sl@0
  6762
			TInt discardBytes;
sl@0
  6763
			TInt r = AllocDiscardable(discardBytes, KMaxTUint64, (zoneFreePages - 1) << gPageShift);
sl@0
  6764
			test_KErrNone(r);
sl@0
  6765
sl@0
  6766
			// Ensure that the RAM zone under test is full.
sl@0
  6767
			GetAllPageInfo();
sl@0
  6768
			if (gZoneUtilArray[index].iFreePages != 0)
sl@0
  6769
				{
sl@0
  6770
				CLEANUP(ResetDPCache());
sl@0
  6771
				TEST_FAIL;
sl@0
  6772
				}
sl@0
  6773
sl@0
  6774
			r = Ldd.ZoneAllocDiscontiguous(gZoneConfigArray[index].iZoneId, zoneFreePages);
sl@0
  6775
sl@0
  6776
			if (r != KErrNoMemory)
sl@0
  6777
				{
sl@0
  6778
				CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  6779
				TEST_FAIL;
sl@0
  6780
				}
sl@0
  6781
			else
sl@0
  6782
				{
sl@0
  6783
				test.Printf(_L("Passed...\n"));	
sl@0
  6784
				}
sl@0
  6785
			}
sl@0
  6786
		TestEnd();
sl@0
  6787
sl@0
  6788
		test.Next(_L("Test7: Test Epoc::ZoneAllocPhysicalRam() replaces DP cache when DP minimum cache size is hit"));
sl@0
  6789
		TestStart();
sl@0
  6790
		gChunkArray1 = new RChunk;
sl@0
  6791
		gChunkArraySize1 = 1;
sl@0
  6792
		GetPrefOrder();
sl@0
  6793
		zonesFound = EFalse;
sl@0
  6794
		for (index = gZoneCount - 1; index > 0 && !zonesFound; index--)
sl@0
  6795
			{
sl@0
  6796
			TUint prefIndex = gPrefArray[index];
sl@0
  6797
			zoneFreePages = gZoneUtilArray[prefIndex].iFreePages;
sl@0
  6798
			if (zoneFreePages > 1)
sl@0
  6799
				{
sl@0
  6800
				// Check there is at least one free page in the other RAM zones.
sl@0
  6801
				TUint i = 0;
sl@0
  6802
				for (; i < gZoneCount; i++)
sl@0
  6803
					{
sl@0
  6804
					if (i != prefIndex && gZoneUtilArray[i].iFreePages != 0)
sl@0
  6805
						{
sl@0
  6806
						index = prefIndex;
sl@0
  6807
						zonesFound = ETrue;
sl@0
  6808
						break;
sl@0
  6809
						}
sl@0
  6810
					}
sl@0
  6811
				}
sl@0
  6812
			}
sl@0
  6813
		if (!zonesFound)
sl@0
  6814
			{
sl@0
  6815
			test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
sl@0
  6816
			}
sl@0
  6817
		else
sl@0
  6818
			{
sl@0
  6819
			index++;
sl@0
  6820
			// Attempt to allocate a movable page to create a gap for the DP cache 
sl@0
  6821
			// page to be replaced with.
sl@0
  6822
			GetOriginalPageCount();
sl@0
  6823
			r = AllocMovable(gChunkArray1, gChunkArraySize1, 1, gPageSize);
sl@0
  6824
sl@0
  6825
			GetAllPageInfo();
sl@0
  6826
			TESTDEBUG(test.Printf(_L("index %d prev free 0x%x cur free 0x%x\n"), 
sl@0
  6827
										index, gOriginalPageCountArray[index].iFreePages, gZoneUtilArray[index].iFreePages));
sl@0
  6828
sl@0
  6829
			if (r != KErrNone || 
sl@0
  6830
				gOriginalPageCountArray[index].iFreePages != gZoneUtilArray[index].iFreePages)
sl@0
  6831
				{// The gap was allocated into the RAM zone under test so can't continue as
sl@0
  6832
				// the DP cache will attempt to be reallocated into the same RAM zone.
sl@0
  6833
				test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
sl@0
  6834
				}
sl@0
  6835
			else
sl@0
  6836
				{
sl@0
  6837
				// Get the ldd to create the array for the fixed page addresses here
sl@0
  6838
				// so that any kernel heap allocations have already occurred before 
sl@0
  6839
				// memory is filled etc.  Make allocation really large so it will always be enough.
sl@0
  6840
				r = Ldd.AllocFixedArray(50);
sl@0
  6841
sl@0
  6842
				// Fill RAM with DP cache pages and free the gap.
sl@0
  6843
				TInt discardBytes;
sl@0
  6844
				r = AllocDiscardable(discardBytes, KMaxTUint64, 0);
sl@0
  6845
				if (r != KErrNone)	
sl@0
  6846
					{
sl@0
  6847
					CLEANUP(ResetDPCache());
sl@0
  6848
					TEST_FAIL;
sl@0
  6849
					}
sl@0
  6850
				UpdateRamInfo();
sl@0
  6851
				TInt prevFreeBytes = gFreeRam;
sl@0
  6852
				RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  6853
				UpdateRamInfo();
sl@0
  6854
				TInt freedPages = (gFreeRam - prevFreeBytes) >> gPageShift;
sl@0
  6855
				if (freedPages < 1)
sl@0
  6856
					{// Something went wrong as should have freed at least one page
sl@0
  6857
					CLEANUP(ResetDPCache());
sl@0
  6858
					TEST_FAIL;
sl@0
  6859
					}
sl@0
  6860
				TUint extraFreePages = freedPages - 1;
sl@0
  6861
sl@0
  6862
				// Ensure that the RAM zone under test is full.
sl@0
  6863
				GetAllPageInfo();
sl@0
  6864
				if (gZoneUtilArray[index].iFreePages != 0)
sl@0
  6865
					{
sl@0
  6866
					CLEANUP(ResetDPCache());
sl@0
  6867
					TEST_FAIL;
sl@0
  6868
					}
sl@0
  6869
sl@0
  6870
				// Allocate from the RAM zone which should force a DP cache 
sl@0
  6871
				// page to be allocated.
sl@0
  6872
				GetOriginalPageCount();
sl@0
  6873
				TUint fixedAllocPages = 1 + extraFreePages;
sl@0
  6874
				r = Ldd.ZoneAllocDiscontiguous2(gZoneConfigArray[index].iZoneId, fixedAllocPages);
sl@0
  6875
			
sl@0
  6876
				GetAllPageInfo();
sl@0
  6877
				if (r != KErrNone ||
sl@0
  6878
					gOriginalPageCountArray[index].iAllocFixed + fixedAllocPages != gZoneUtilArray[index].iAllocFixed)
sl@0
  6879
					{
sl@0
  6880
					test.Printf(_L("r %d index %d alloc 0x%x prevFixed 0x%x curFixed 0x%x\n"), r, index, fixedAllocPages, 
sl@0
  6881
								gOriginalPageCountArray[index].iAllocFixed, gZoneUtilArray[index].iAllocFixed);
sl@0
  6882
					CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  6883
					TEST_FAIL;
sl@0
  6884
					}
sl@0
  6885
				else
sl@0
  6886
					{
sl@0
  6887
					test.Printf(_L("Passed...\n"));						
sl@0
  6888
					}
sl@0
  6889
				}
sl@0
  6890
			}
sl@0
  6891
		// This should cleanup any fixed pages allocated.
sl@0
  6892
		TestEnd();
sl@0
  6893
		}
sl@0
  6894
sl@0
  6895
	test.Next(_L("Test8: Test Epoc::ZoneAllocPhysicalRam() return KErrNoMemory when appropriate"));
sl@0
  6896
	TestStart();
sl@0
  6897
	// Search for a RAM zone that has some immovable pages allocated into 
sl@0
  6898
	// it but isn't totally full.
sl@0
  6899
	GetAllPageInfo();
sl@0
  6900
	zonesFound = EFalse;
sl@0
  6901
	for (index = 0; index < gZoneCount; index++)
sl@0
  6902
		{
sl@0
  6903
		if (gZoneUtilArray[index].iFreePages && 
sl@0
  6904
			(gZoneUtilArray[index].iAllocUnknown || gZoneUtilArray[index].iAllocFixed))
sl@0
  6905
			{
sl@0
  6906
			zonesFound = ETrue;
sl@0
  6907
			break;
sl@0
  6908
			}
sl@0
  6909
		}
sl@0
  6910
	if (!zonesFound)
sl@0
  6911
		{
sl@0
  6912
		test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
sl@0
  6913
		}
sl@0
  6914
	else
sl@0
  6915
		{
sl@0
  6916
		// Attempt to allocate the whole RAM zone.
sl@0
  6917
		r = Ldd.ZoneAllocDiscontiguous(gZoneConfigArray[index].iZoneId, gZoneUtilArray[index].iPhysPages);
sl@0
  6918
		
sl@0
  6919
		if (r != KErrNoMemory)
sl@0
  6920
			{
sl@0
  6921
			test.Printf(_L("FAIL: r %d index %d\n"), r, index);
sl@0
  6922
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  6923
			TEST_FAIL;
sl@0
  6924
			}
sl@0
  6925
		else
sl@0
  6926
			{
sl@0
  6927
			test.Printf(_L("Passed...\n"));
sl@0
  6928
			}
sl@0
  6929
		}
sl@0
  6930
	TestEnd();
sl@0
  6931
sl@0
  6932
	test.Next(_L("Test9: Test Epoc::ZoneAllocPhysicalRam() moves the required number of movable pages"));
sl@0
  6933
	TestStart();
sl@0
  6934
	TUint allocFixedPages;
sl@0
  6935
	// Search for a RAM zone that has at least 2 movable pages allocated into it.
sl@0
  6936
	// Need 2 so that we can move one and leave one.
sl@0
  6937
	GetAllPageInfo();
sl@0
  6938
	zonesFound = EFalse;
sl@0
  6939
	for (index = 0; index < gZoneCount; index++)
sl@0
  6940
		{
sl@0
  6941
		if (gZoneUtilArray[index].iAllocMovable > 1)
sl@0
  6942
			{
sl@0
  6943
			// Only use this zone if the other RAM zones have enough free space for 
sl@0
  6944
			// the movable page in this zone to be moved to.
sl@0
  6945
			TUint freeInOther = 0;
sl@0
  6946
			for (TUint i = 0; i < gZoneCount && !zonesFound; i++)
sl@0
  6947
				{
sl@0
  6948
				if (i != index)
sl@0
  6949
					{
sl@0
  6950
					freeInOther += gZoneUtilArray[i].iFreePages;
sl@0
  6951
					}
sl@0
  6952
				}
sl@0
  6953
			if (freeInOther >= gZoneUtilArray[index].iAllocMovable)
sl@0
  6954
				{
sl@0
  6955
				zonesFound = ETrue;
sl@0
  6956
				break;
sl@0
  6957
				}
sl@0
  6958
			}
sl@0
  6959
		}
sl@0
  6960
	
sl@0
  6961
	if (!zonesFound)
sl@0
  6962
		{
sl@0
  6963
		test.Printf(_L("No suitable RAM zone could be found - Skipping...\n"));
sl@0
  6964
		goto skipTest9;
sl@0
  6965
		}
sl@0
  6966
sl@0
  6967
	// Allocate up to one less than the RAM zone size. Do 2 stage fixed allocation
sl@0
  6968
	// to avoid kernel heap allocations spoiling test setup.
sl@0
  6969
	r = Ldd.AllocFixedArray(gZoneConfigArray[index].iPhysPages);
sl@0
  6970
	if (r != KErrNone)
sl@0
  6971
		{
sl@0
  6972
		test.Printf(_L("Not enough free RAM to perform test - Skipping...\n"));
sl@0
  6973
		goto skipTest9;
sl@0
  6974
		}
sl@0
  6975
sl@0
  6976
	GetAllPageInfo();
sl@0
  6977
	if (gZoneUtilArray[index].iAllocMovable < 2)
sl@0
  6978
		{
sl@0
  6979
		test.Printf(_L("Expanding kernel heap for phys address array spoiled RAM zone - Skipping...\n"));
sl@0
  6980
		goto skipTest9;
sl@0
  6981
		}
sl@0
  6982
	allocFixedPages =	gZoneUtilArray[index].iAllocMovable + 
sl@0
  6983
						gZoneUtilArray[index].iAllocDiscardable +
sl@0
  6984
						gZoneUtilArray[index].iFreePages - 1;
sl@0
  6985
	r = Ldd.ZoneAllocDiscontiguous2(gZoneConfigArray[index].iZoneId, allocFixedPages);
sl@0
  6986
sl@0
  6987
	if (r != KErrNone || !gZoneUtilArray[index].iAllocMovable)
sl@0
  6988
		{
sl@0
  6989
		test.Printf(_L("Fixed not allocated or too many movable moved RAM zone ID%x\n"), 
sl@0
  6990
					gZoneConfigArray[index].iZoneId);
sl@0
  6991
		CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  6992
		TEST_FAIL;
sl@0
  6993
		}
sl@0
  6994
sl@0
  6995
skipTest9 :
sl@0
  6996
	// This will clean up any fixed pages allocated.
sl@0
  6997
	TestEnd();
sl@0
  6998
sl@0
  6999
	delete[] zoneIdArray;
sl@0
  7000
	
sl@0
  7001
	test.Next(_L("Test10: Test Epoc::ZoneAllocPhysicalRam() (Discontiguous) doesn't affect the allocation of movable pages"));
sl@0
  7002
	r = TestZoneAllocNoAffect(Z_ALLOC_DISC, BEST_MOVABLE);
sl@0
  7003
	if (r != KErrNone)
sl@0
  7004
		{
sl@0
  7005
		TEST_FAIL;
sl@0
  7006
		}
sl@0
  7007
sl@0
  7008
	test.Next(_L("Test11: Test Epoc::ZoneAllocPhysicalRam() (Discontiguous) doesn't affect the allocation of discardable pages"));
sl@0
  7009
	r = TestZoneAllocNoAffect(Z_ALLOC_DISC, BEST_DISCARDABLE);
sl@0
  7010
	if (r != KErrNone)
sl@0
  7011
		{
sl@0
  7012
		TEST_FAIL;
sl@0
  7013
		}
sl@0
  7014
sl@0
  7015
	test.End();
sl@0
  7016
	return KErrNone;
sl@0
  7017
	}
sl@0
  7018
sl@0
  7019
sl@0
  7020
//
sl@0
  7021
// TestFreeZone
sl@0
  7022
//
sl@0
  7023
//---------------------------------------------------------------------------------------------------------------------
sl@0
  7024
//! @SYMTestCaseID				KBASE-t_ramdefrag-0537
sl@0
  7025
//! @SYMTestType				CIT
sl@0
  7026
//! @SYMTestCaseDesc			Verifying the function Epoc::FreePhysicalRam()
sl@0
  7027
//! @SYMPREQ					PREQ308
sl@0
  7028
//! @SYMTestPriority			High
sl@0
  7029
//! @SYMTestActions				
sl@0
  7030
//! 	1.	Allocate fixed pages and call function to free all fixed pages allocated.  
sl@0
  7031
//! 
sl@0
  7032
//! @SYMTestExpectedResults
sl@0
  7033
//! 	1.	KErrNone
sl@0
  7034
//---------------------------------------------------------------------------------------------------------------------
sl@0
  7035
TInt TestFreeZone()
sl@0
  7036
	{
sl@0
  7037
	TInt r = 0;
sl@0
  7038
	TUint zoneID = 0;
sl@0
  7039
	test.Start(_L("Test1: Free allocated pages"));	
sl@0
  7040
	TestStart();	
sl@0
  7041
	
sl@0
  7042
	TInt pages = 50;
sl@0
  7043
sl@0
  7044
	GetAllPageInfo();
sl@0
  7045
	TUint index = gZoneCount - 1;
sl@0
  7046
	while (index > 0 && 
sl@0
  7047
			(gZoneUtilArray[index].iAllocFixed != 0 || 
sl@0
  7048
			gZoneUtilArray[index].iAllocUnknown != 0 || 
sl@0
  7049
			(TInt)gZoneUtilArray[index].iFreePages < pages)) 
sl@0
  7050
		{
sl@0
  7051
		-- index;
sl@0
  7052
		}
sl@0
  7053
	zoneID = gZoneConfigArray[index].iZoneId;
sl@0
  7054
	
sl@0
  7055
	if (index == 0 && 
sl@0
  7056
		(gZoneUtilArray[index].iAllocFixed != 0 || 
sl@0
  7057
		gZoneUtilArray[index].iAllocUnknown != 0 || 
sl@0
  7058
		(TInt)gZoneUtilArray[index].iFreePages < pages))
sl@0
  7059
		{
sl@0
  7060
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  7061
		}
sl@0
  7062
	else
sl@0
  7063
		{
sl@0
  7064
		TESTDEBUG(test.Printf(_L("Allocating 0x%x pages to zone ID 0x%x\n"), pages, zoneID));
sl@0
  7065
		r = Ldd.ZoneAllocDiscontiguous(zoneID, pages);
sl@0
  7066
		GetAllPageInfo();
sl@0
  7067
sl@0
  7068
		TESTDEBUG(test.Printf(_L("Freeing 0x%x fixed pages\n"), pages));
sl@0
  7069
		if (r == KErrNone)
sl@0
  7070
			{
sl@0
  7071
			r = Ldd.FreeZone(pages);
sl@0
  7072
			}
sl@0
  7073
		TESTDEBUG(test.Printf(_L("r = %d\n"), r));
sl@0
  7074
		if (r == KErrNone)
sl@0
  7075
			{
sl@0
  7076
			test.Printf(_L("Pass: Correct return value\n"));
sl@0
  7077
			}
sl@0
  7078
		else
sl@0
  7079
			{
sl@0
  7080
			test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
sl@0
  7081
			TEST_FAIL;
sl@0
  7082
			}
sl@0
  7083
		}
sl@0
  7084
	TestEnd();
sl@0
  7085
sl@0
  7086
	test.End();
sl@0
  7087
	return KErrNone;
sl@0
  7088
	}
sl@0
  7089
sl@0
  7090
sl@0
  7091
//
sl@0
  7092
// TestDefragSemMethod
sl@0
  7093
//
sl@0
  7094
//---------------------------------------------------------------------------------------------------------------------
sl@0
  7095
//! @SYMTestCaseID				KBASE-t_ramdefrag-0538
sl@0
  7096
//! @SYMTestType				CIT
sl@0
  7097
//! @SYMTestCaseDesc			Verifying the semaphore versions of the various defrag methods
sl@0
  7098
//! @SYMPREQ					PREQ308
sl@0
  7099
//! @SYMTestPriority			High
sl@0
  7100
//! @SYMTestActions				
sl@0
  7101
//! 	1.	Fragment the memory. Following this, call the semaphore variation of DefragRam.  
sl@0
  7102
//! 	2.	Fragment the memory. Following this, call the semaphore variation of EmptyRamZone 
sl@0
  7103
//! 	3.	Fragment the memory. Following this, call the semaphore variation of ClaimRamZone 
sl@0
  7104
//! 
sl@0
  7105
//! @SYMTestExpectedResults
sl@0
  7106
//! 	1.	1 or more zones have been emptied
sl@0
  7107
//! 	2.	Zone specified has been emptied
sl@0
  7108
//! 	3.	Zone has been claimed
sl@0
  7109
//---------------------------------------------------------------------------------------------------------------------
sl@0
  7110
TInt TestDefragSemMethod()
sl@0
  7111
	{
sl@0
  7112
	TInt r = 0;
sl@0
  7113
	
sl@0
  7114
	test.Start(_L("Test1: Call semaphore method of DefragRam"));	
sl@0
  7115
	TestStart();	
sl@0
  7116
	
sl@0
  7117
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  7118
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  7119
sl@0
  7120
	GetOriginalPageCount();
sl@0
  7121
	TBool genSucceed = CanGenSucceed();
sl@0
  7122
	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SEM);
sl@0
  7123
	
sl@0
  7124
	
sl@0
  7125
	if (r != KErrNone ||(genSucceed && CheckZonesSwitchedOff() == EFalse))
sl@0
  7126
		{
sl@0
  7127
		test.Printf(_L("Fail: r = %d, or zones have not been swtiched off\n"), r);
sl@0
  7128
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  7129
		TEST_FAIL;
sl@0
  7130
		}
sl@0
  7131
	else
sl@0
  7132
		{
sl@0
  7133
		test.Printf(_L("Passed...\n"));	
sl@0
  7134
		}
sl@0
  7135
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  7136
	TestEnd();
sl@0
  7137
sl@0
  7138
sl@0
  7139
	test.Next(_L("Test2: Call semaphore method of EmptyRamZone"));	
sl@0
  7140
	TestStart();	
sl@0
  7141
	
sl@0
  7142
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  7143
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  7144
sl@0
  7145
	GetAllPageInfo();
sl@0
  7146
	TUint index = gZoneCount - 1;
sl@0
  7147
	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
sl@0
  7148
		{
sl@0
  7149
		-- index;
sl@0
  7150
		}
sl@0
  7151
	TUint defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  7152
	
sl@0
  7153
	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
sl@0
  7154
		{
sl@0
  7155
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  7156
		}
sl@0
  7157
	else
sl@0
  7158
		{
sl@0
  7159
		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SEM, defragZoneID);
sl@0
  7160
		
sl@0
  7161
		GetAllPageInfo();
sl@0
  7162
		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  7163
		if (freeInOthers && (r != KErrNone || !CheckZoneIsOff(index)))
sl@0
  7164
			{
sl@0
  7165
			test.Printf(_L("Fail: r = %d, or zones has not been swtiched off\n"), r);
sl@0
  7166
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  7167
			TEST_FAIL;
sl@0
  7168
			}
sl@0
  7169
		else
sl@0
  7170
			{
sl@0
  7171
			test.Printf(_L("Passed...\n"));	
sl@0
  7172
			}
sl@0
  7173
		}
sl@0
  7174
	// This will free any allocated memory.			
sl@0
  7175
	TestEnd();
sl@0
  7176
sl@0
  7177
	test.Next(_L("Test3: Call semaphore method of ClaimRamZone"));	
sl@0
  7178
	TestStart();	
sl@0
  7179
	
sl@0
  7180
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  7181
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  7182
sl@0
  7183
	GetAllPageInfo();
sl@0
  7184
	index = gZoneCount - 2;
sl@0
  7185
	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
sl@0
  7186
		{
sl@0
  7187
		-- index;
sl@0
  7188
		}
sl@0
  7189
	defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  7190
	
sl@0
  7191
	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
sl@0
  7192
		{
sl@0
  7193
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  7194
		}
sl@0
  7195
	else
sl@0
  7196
		{
sl@0
  7197
		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SEM, defragZoneID);
sl@0
  7198
sl@0
  7199
		GetAllPageInfo();
sl@0
  7200
		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  7201
		if (CheckZoneIsOff(index) ||
sl@0
  7202
			(freeInOthers && ( r != KErrNone ||
sl@0
  7203
			gZoneUtilArray[index].iAllocFixed != gZoneConfigArray[index].iPhysPages)))
sl@0
  7204
			{
sl@0
  7205
			test.Printf(_L("Fail: r = %d, or zone ID 0x%x has not been claimed\n"), r, defragZoneID);
sl@0
  7206
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  7207
			TEST_FAIL;
sl@0
  7208
			}
sl@0
  7209
		else
sl@0
  7210
			{
sl@0
  7211
			test.Printf(_L("Passed...\n"));	
sl@0
  7212
			}
sl@0
  7213
sl@0
  7214
		r = Ldd.FreeAllFixedPages();
sl@0
  7215
		}
sl@0
  7216
sl@0
  7217
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  7218
	TestEnd();
sl@0
  7219
sl@0
  7220
	test.End();
sl@0
  7221
	return KErrNone;
sl@0
  7222
sl@0
  7223
	}
sl@0
  7224
sl@0
  7225
sl@0
  7226
//
sl@0
  7227
// TestDefragDfcMethod
sl@0
  7228
//
sl@0
  7229
//---------------------------------------------------------------------------------------------------------------------
sl@0
  7230
//! @SYMTestCaseID				KBASE-t_ramdefrag-0539
sl@0
  7231
//! @SYMTestType				CIT
sl@0
  7232
//! @SYMTestCaseDesc			Verifying the Dfc versions of the various defrag methods
sl@0
  7233
//! @SYMPREQ					PREQ308
sl@0
  7234
//! @SYMTestPriority			High
sl@0
  7235
//! @SYMTestActions				
sl@0
  7236
//! 	1.	Fragment the memory. Following this, call the Dfc variation of DefragRam.  
sl@0
  7237
//! 	2.	Fragment the memory. Following this, call the Dfc variation of EmptyRamZone 
sl@0
  7238
//! 	3.	Fragment the memory. Following this, call the Dfc variation of ClaimRamZone 
sl@0
  7239
//! 
sl@0
  7240
//! @SYMTestExpectedResults
sl@0
  7241
//! 	1.	1 or more zones have been emptied
sl@0
  7242
//! 	2.	Zone specified has been emptied
sl@0
  7243
//! 	3.	Zone has been claimed
sl@0
  7244
//---------------------------------------------------------------------------------------------------------------------
sl@0
  7245
TInt TestDefragDfcMethod()
sl@0
  7246
	{
sl@0
  7247
	TInt r = 0;
sl@0
  7248
	TRequestStatus req;
sl@0
  7249
	test.Start(_L("Test1: Call Dfc method of DefragRam"));	
sl@0
  7250
	TestStart();	
sl@0
  7251
	
sl@0
  7252
	GetAllPageInfo();
sl@0
  7253
	
sl@0
  7254
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  7255
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  7256
sl@0
  7257
	GetOriginalPageCount();
sl@0
  7258
	TBool genSucceed = CanGenSucceed();
sl@0
  7259
	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_DFC, 0, 0, -1, &req);
sl@0
  7260
	TESTDEBUG(test.Printf(_L("After queueing defrag r = %d\n"), r));
sl@0
  7261
	User::WaitForRequest(req);
sl@0
  7262
	r = req.Int();
sl@0
  7263
	
sl@0
  7264
	if (r != KErrNone || (genSucceed && CheckZonesSwitchedOff() == EFalse))
sl@0
  7265
		{
sl@0
  7266
		test.Printf(_L("Fail: r = %d, or zones have not been swtiched off\n"), r);
sl@0
  7267
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  7268
		TEST_FAIL;
sl@0
  7269
		}
sl@0
  7270
	else
sl@0
  7271
		{
sl@0
  7272
		test.Printf(_L("Passed...\n"));	
sl@0
  7273
		}
sl@0
  7274
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  7275
	TestEnd();
sl@0
  7276
sl@0
  7277
sl@0
  7278
	test.Next(_L("Test2: Call Dfc method of EmptyRamZone"));	
sl@0
  7279
	TestStart();	
sl@0
  7280
	
sl@0
  7281
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  7282
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  7283
sl@0
  7284
	GetAllPageInfo();
sl@0
  7285
	TUint index = gZoneCount - 1;
sl@0
  7286
	while (index > 0 && 
sl@0
  7287
			(gZoneUtilArray[index].iAllocMovable < 10 ||
sl@0
  7288
			gZoneUtilArray[index].iAllocFixed != 0 || 
sl@0
  7289
			gZoneUtilArray[index].iAllocUnknown != 0)) 
sl@0
  7290
		{
sl@0
  7291
		-- index;
sl@0
  7292
		}
sl@0
  7293
sl@0
  7294
	TUint defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  7295
	test.Printf(_L("zone ID = 0x%x\n"), defragZoneID);
sl@0
  7296
	if (index == 0 && 
sl@0
  7297
		(gZoneUtilArray[index].iAllocMovable < 10 ||
sl@0
  7298
		gZoneUtilArray[index].iAllocFixed != 0 || 
sl@0
  7299
		gZoneUtilArray[index].iAllocUnknown != 0))
sl@0
  7300
		{
sl@0
  7301
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  7302
		}
sl@0
  7303
	else
sl@0
  7304
		{
sl@0
  7305
		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_DFC, defragZoneID, 0, -1, &req);
sl@0
  7306
		
sl@0
  7307
		User::WaitForRequest(req);
sl@0
  7308
		r = req.Int();
sl@0
  7309
sl@0
  7310
		GetAllPageInfo();
sl@0
  7311
		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  7312
		if (freeInOthers && (r != KErrNone || CheckZoneIsOff(index) == EFalse))
sl@0
  7313
			{
sl@0
  7314
			test.Printf(_L("Fail: r = %d, or zones have not been swtiched off\n"), r);
sl@0
  7315
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  7316
			TEST_FAIL;
sl@0
  7317
			}
sl@0
  7318
		else
sl@0
  7319
			{
sl@0
  7320
			test.Printf(_L("Passed...\n"));	
sl@0
  7321
			}
sl@0
  7322
		}
sl@0
  7323
	// This will free any allocated memory
sl@0
  7324
	TestEnd();
sl@0
  7325
	
sl@0
  7326
sl@0
  7327
	test.Next(_L("Test3: Call Dfc method of ClaimRamZone"));	
sl@0
  7328
	TestStart();	
sl@0
  7329
	
sl@0
  7330
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  7331
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  7332
sl@0
  7333
	GetAllPageInfo();
sl@0
  7334
	index = gZoneCount - 1;
sl@0
  7335
	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
sl@0
  7336
		{
sl@0
  7337
		-- index;
sl@0
  7338
		}
sl@0
  7339
	defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  7340
sl@0
  7341
	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
sl@0
  7342
		{
sl@0
  7343
		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
sl@0
  7344
		}
sl@0
  7345
	else
sl@0
  7346
		{
sl@0
  7347
		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_DFC, defragZoneID, 0, -1, &req);
sl@0
  7348
		User::WaitForRequest(req);
sl@0
  7349
		r = req.Int();
sl@0
  7350
		
sl@0
  7351
		GetAllPageInfo();
sl@0
  7352
		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  7353
		if (CheckZoneIsOff(index) || 
sl@0
  7354
			(freeInOthers && (r != KErrNone || 
sl@0
  7355
			gZoneUtilArray[index].iAllocFixed != gZoneConfigArray[index].iPhysPages)))
sl@0
  7356
			{
sl@0
  7357
			test.Printf(_L("Fail: r = %d, or zone ID 0x%x has not been claimed\n"), r, defragZoneID);
sl@0
  7358
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  7359
			if (r == KErrNone)
sl@0
  7360
				{
sl@0
  7361
				CLEANUP(Ldd.FreeFromAddr(	gZoneUtilArray[index].iAllocFixed, 
sl@0
  7362
											gZoneConfigArray[index].iPhysBase));
sl@0
  7363
				}
sl@0
  7364
			TEST_FAIL;
sl@0
  7365
			}
sl@0
  7366
		else
sl@0
  7367
			{
sl@0
  7368
			test.Printf(_L("Passed...\n"));	
sl@0
  7369
			}
sl@0
  7370
		
sl@0
  7371
		if (r == KErrNone)
sl@0
  7372
			{
sl@0
  7373
			Ldd.FreeFromAddr(	gZoneUtilArray[index].iAllocFixed, 
sl@0
  7374
								gZoneConfigArray[index].iPhysBase);
sl@0
  7375
			}
sl@0
  7376
		}
sl@0
  7377
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
sl@0
  7378
	TestEnd();
sl@0
  7379
sl@0
  7380
	test.End();
sl@0
  7381
	return KErrNone;
sl@0
  7382
	}
sl@0
  7383
sl@0
  7384
sl@0
  7385
//
sl@0
  7386
// TestPriorities
sl@0
  7387
//
sl@0
  7388
//---------------------------------------------------------------------------------------------------------------------
sl@0
  7389
//! @SYMTestCaseID				KBASE-t_ramdefrag-0540
sl@0
  7390
//! @SYMTestType				CIT
sl@0
  7391
//! @SYMTestCaseDesc			Verifying the priorities of the defrag methods
sl@0
  7392
//! @SYMPREQ					PREQ308
sl@0
  7393
//! @SYMTestPriority			High
sl@0
  7394
//! @SYMTestActions				
sl@0
  7395
//! 	1.	Call DefragRam with a lower invalid priority value, e.g. -2  
sl@0
  7396
//! 	2.	Call DefragRam with a lower invalid priority value, e.g. 100
sl@0
  7397
//! 	3.	Queue three asynchronous defrags using the Dfc method:
sl@0
  7398
//! 			a.	First one with the lowest priority, this will start the straight away 
sl@0
  7399
//! 				and will busy the defrag method, causing any other defrag requests to 
sl@0
  7400
//! 				be queued. 
sl@0
  7401
//! 			b.	Queue a defrag with a relatively low priority
sl@0
  7402
//! 			c.	Queue a defrag with a higher priority than the one queued in (b)
sl@0
  7403
//! 		Record the order in which the defrags are completed
sl@0
  7404
//! 
sl@0
  7405
//! @SYMTestExpectedResults
sl@0
  7406
//! 	1.	KErrArgument
sl@0
  7407
//! 	2.	KErrArgument
sl@0
  7408
//! 	3.	(a) will complete first as it started straight away. 
sl@0
  7409
//! 		(b) and (c) were both queued whilst (a) was running, 
sl@0
  7410
//! 		however as (c) has a higher priority, it will complete first - 
sl@0
  7411
//! 		therefore the order returned would be "a,c,b"
sl@0
  7412
//---------------------------------------------------------------------------------------------------------------------
sl@0
  7413
TInt TestPriorities()
sl@0
  7414
	{
sl@0
  7415
	test.Start(_L("Test1: Call defrag with an invalid lower priority"));	
sl@0
  7416
	TestStart();	
sl@0
  7417
	
sl@0
  7418
	TInt r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, 0, -2);
sl@0
  7419
	TESTDEBUG(test.Printf(_L("r = %d\n"), r));
sl@0
  7420
	if (r != KErrArgument)
sl@0
  7421
		{
sl@0
  7422
		test.Printf(_L("Fail: r = %d, not expected\n"), r);
sl@0
  7423
		TEST_FAIL;
sl@0
  7424
		}
sl@0
  7425
	else
sl@0
  7426
		{
sl@0
  7427
		test.Printf(_L("Passed...\n"));	
sl@0
  7428
		}
sl@0
  7429
	
sl@0
  7430
	TestEnd();
sl@0
  7431
sl@0
  7432
	test.Next(_L("Test2: Call defrag with an invalid higher priority"));	
sl@0
  7433
	TestStart();	
sl@0
  7434
	
sl@0
  7435
	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, 0, 100);
sl@0
  7436
	TESTDEBUG(test.Printf(_L("r = %d\n"), r));
sl@0
  7437
	if (r != KErrArgument)
sl@0
  7438
		{
sl@0
  7439
		test.Printf(_L("Fail: r = %d, not expected\n"), r);
sl@0
  7440
		TEST_FAIL;
sl@0
  7441
		}
sl@0
  7442
	else
sl@0
  7443
		{
sl@0
  7444
		test.Printf(_L("Passed...\n"));	
sl@0
  7445
		}
sl@0
  7446
	TestEnd();
sl@0
  7447
sl@0
  7448
	if (UserSvr::HalFunction(EHalGroupKernel, EKernelHalNumLogicalCpus, 0, 0) == 1)
sl@0
  7449
		{// Only test priorities on single core system as this test is not smp safe.
sl@0
  7450
		test.Next(_L("Test3: Call Dfc method of EmptyRamZone to test priorities"));	
sl@0
  7451
		TestStart();
sl@0
  7452
		
sl@0
  7453
		
sl@0
  7454
		TRequestStatus req;
sl@0
  7455
		TRequestStatus req2;
sl@0
  7456
		TRequestStatus req3;	
sl@0
  7457
		TInt expectedOrder = 132; // Priorities set in Device driver 
sl@0
  7458
		
sl@0
  7459
		AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  7460
		FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  7461
sl@0
  7462
		TUint index = (gZoneCount + 1) / 2;
sl@0
  7463
		TUint defragZoneID = gZoneConfigArray[index].iZoneId;
sl@0
  7464
sl@0
  7465
		r = Ldd.CheckPriorities(DEFRAG_TYPE_EMPTY, defragZoneID, &req, &req2, &req3);
sl@0
  7466
sl@0
  7467
		User::WaitForRequest(req);
sl@0
  7468
		User::WaitForRequest(req2);
sl@0
  7469
		User::WaitForRequest(req3);
sl@0
  7470
sl@0
  7471
		TInt order = Ldd.GetDefragOrder();
sl@0
  7472
		if (order != expectedOrder)
sl@0
  7473
			{
sl@0
  7474
			test.Printf(_L("Fail: order = %d. expected = %d\n"), order, expectedOrder);
sl@0
  7475
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  7476
			TEST_FAIL;
sl@0
  7477
			}
sl@0
  7478
		else
sl@0
  7479
			{
sl@0
  7480
			test.Printf(_L("Passed...\n"));	
sl@0
  7481
			}
sl@0
  7482
		// This will free any allocated memory
sl@0
  7483
		TestEnd();
sl@0
  7484
		}
sl@0
  7485
sl@0
  7486
	test.End();
sl@0
  7487
	return KErrNone;
sl@0
  7488
	}
sl@0
  7489
sl@0
  7490
sl@0
  7491
//
sl@0
  7492
// TestFlags
sl@0
  7493
//
sl@0
  7494
//---------------------------------------------------------------------------------------------------------------------
sl@0
  7495
//! @SYMTestCaseID				KBASE-t_ramdefrag-0541
sl@0
  7496
//! @SYMTestType				CIT
sl@0
  7497
//! @SYMTestCaseDesc			Verifying that when certain flags are set, 
sl@0
  7498
//! 							only certain types of pages can be allocated to the zone. 
sl@0
  7499
//! @SYMPREQ					PREQ308
sl@0
  7500
//! @SYMTestPriority			High
sl@0
  7501
//! @SYMTestActions				
sl@0
  7502
//! 	1.	Set the NoFixed flag in a zone and allocate movable pages
sl@0
  7503
//! 	2.	Set the NoFixed flag in a zone and allocate fixed pages
sl@0
  7504
//! 	3.	Set the NoFixed flag in a zone and allocate discardable pages
sl@0
  7505
//! 	4.	Set the NoFixed flag in a zone and allocate fixed pages to that zone using Epoc::ZoneAllocPhysicalRam()
sl@0
  7506
//! 	5.	Set the NoFixed flag in a zone and allocate fixed pages by calling TRamDefragRequest::ClaimRamZone()
sl@0
  7507
//! 	6.	Set the NoMovable flag in a zone and allocate movable pages
sl@0
  7508
//! 	7.	Set the NoMovable flag in a zone and allocate fixed pages
sl@0
  7509
//! 	8.	Set the NoMovable flag in a zone and allocate discardable pages
sl@0
  7510
//! 	9.	Set the NoDiscardable flag in a zone and allocate movable pages
sl@0
  7511
//! 	10.	Set the NoDiscardable flag in a zone and allocate fixed pages
sl@0
  7512
//! 	11.	Set the NoDiscardable flag in a zone and allocate discardable pages
sl@0
  7513
//! 	12.	Set the OnlyDiscardable flag in a zone and allocate movable pages
sl@0
  7514
//! 	13.	Set the OnlyDiscardable flag in a zone and allocate fixed pages
sl@0
  7515
//! 	14.	Set the OnlyDiscardable flag in a zone and allocate discardable pages
sl@0
  7516
//! 	15.	Set the OnlyDiscardable flag in a zone and allocate fixed pages to that zone using Epoc::ZoneAllocPhysicalRam()
sl@0
  7517
//! 	16.	Set the OnlyDiscardable flag in a zone and allocate fixed pages by calling TRamDefragRequest::ClaimRamZone()
sl@0
  7518
//! 	17.	Set the NoFurtherAlloc flag in a zone and allocate movable pages
sl@0
  7519
//! 	18.	Set the NoFurtherAlloc flag in a zone and allocate fixed pages
sl@0
  7520
//! 	19.	Set the NoFurtherAlloc flag in a zone and allocate discardable pages
sl@0
  7521
//! 	20.	Set the NoFurtherAlloc flag in a zone and allocate fixed pages to that zone using Epoc::ZoneAllocPhysicalRam()
sl@0
  7522
//! 	21.	Set the NoFurtherAlloc flag in a zone and allocate fixed pages by calling TRamDefragRequest::ClaimRamZone()
sl@0
  7523
//!		22.	Set up memory so that the least preferable RAM zone has movable pages + discardable pages > free pages in the most  
sl@0
  7524
//!			preferable zone. Ensure that the discardable pages cannot be discarded and so must be moved. Now set the flag on  
sl@0
  7525
//!			all zones barring the most preferable zone to KRamZoneFlagNoMovable, ensuring that most pref has no flags set. 
sl@0
  7526
//!			Following this call a general defrag. 
sl@0
  7527
//!		23.	Set up memory so that the least preferable RAM zone has movable pages + discardable pages > free pages in the most 
sl@0
  7528
//!			preferable RAM zone. Ensure that the discardable pages cannot be discarded and so must be moved. 
sl@0
  7529
//!			Now set the flag on all zones barring the most preferable zone to KRamZoneFlagNoDiscard, ensuring that 
sl@0
  7530
//!			most preferable RAM zone has no flags set. Following this call a general defrag. 
sl@0
  7531
//!		24. Set up memory so that the least preferable RAM zone has movable pages and discardable pages. Set all the zone 
sl@0
  7532
//!			flags to KRamZoneFlagNoMovable. Following this call a general defrag. 
sl@0
  7533
//!
sl@0
  7534
//! @SYMTestExpectedResults
sl@0
  7535
//! 	1.	Movable pages are allocated and no fixed pages allocated
sl@0
  7536
//! 	2.	No fixed pages have been allocated
sl@0
  7537
//! 	3.	Discardable pages are allocated and no fixed pages allocated
sl@0
  7538
//!		4.	KErrNone, flag is ignored with zone specific allocation
sl@0
  7539
//!		5.	KErrNone, flag is ignored when claiming a zone
sl@0
  7540
//! 	6.	No movable pages have been allocated
sl@0
  7541
//! 	7.	Fixed pages allocated, no movable allocated
sl@0
  7542
//! 	8.	Discardable pages allocated, no movable pages allocated
sl@0
  7543
//! 	9.	Movable pages allocated, no discardable pages allocated
sl@0
  7544
//! 	10.	Fixed pages allocated, no discardable allocated
sl@0
  7545
//! 	11.	No discardable pages allocated
sl@0
  7546
//! 	12.	No movable pages allocated
sl@0
  7547
//! 	13.	No fixed pages allocated
sl@0
  7548
//! 	14.	Discardable pages allocated, no movable or fixed allocated
sl@0
  7549
//!		15.	KErrNone, flag is ignored with zone specific allocation
sl@0
  7550
//!		16.	KErrNone, flag is ignored when claiming a zone
sl@0
  7551
//! 	17.	No moving, fixed or discardable pages allocated
sl@0
  7552
//! 	18.	No moving, fixed or discardable pages allocated
sl@0
  7553
//! 	19.	No moving, fixed or discardable pages allocated
sl@0
  7554
//!		20.	KErrNoMemory, flag is obeyed with zone specific allocation
sl@0
  7555
//!		21.	KErrNone, flag is ignored when claiming a zone
sl@0
  7556
//!		22.	Movable pages moved to the most preferable zone, discardable pages moved to next most preferable zone
sl@0
  7557
//!		23.	Discardable pages moved to most preferable zone, movable pages moved to next next most preferable zone
sl@0
  7558
//!		24.	No pages are moved from the least preferable zone zone
sl@0
  7559
//---------------------------------------------------------------------------------------------------------------------
sl@0
  7560
TInt TestFlags()
sl@0
  7561
	{
sl@0
  7562
	TInt r = 0;
sl@0
  7563
	TUint zoneDefragID = 0;
sl@0
  7564
sl@0
  7565
	test.Start(_L("Test1: No Fixed Flag, Alloc Movable"));	
sl@0
  7566
	TestStart();	
sl@0
  7567
	
sl@0
  7568
	TInt index = GetBestZone(BEST_MOVABLE);
sl@0
  7569
sl@0
  7570
	if (index == KErrNotFound)
sl@0
  7571
		{
sl@0
  7572
		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  7573
		}
sl@0
  7574
	else
sl@0
  7575
		{
sl@0
  7576
		zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  7577
		if (SpaceAvailForPageTables(index, KFillAllMovable))
sl@0
  7578
			{
sl@0
  7579
			GetOriginalPageCount();
sl@0
  7580
			r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
sl@0
  7581
			test_KErrNone(r);		
sl@0
  7582
sl@0
  7583
			AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
sl@0
  7584
			GetAllPageInfo();
sl@0
  7585
			if (gZoneUtilArray[index].iAllocMovable == gOriginalPageCountArray[index].iAllocMovable || 
sl@0
  7586
				gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
sl@0
  7587
				{
sl@0
  7588
				test.Printf(_L("Fail: Either fixed pages have been allocated or movable pages have not been allocated to zone ID %x\n"), gZoneConfigArray[index].iZoneId);
sl@0
  7589
				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  7590
				TEST_FAIL;
sl@0
  7591
				}
sl@0
  7592
			else
sl@0
  7593
				{
sl@0
  7594
				test.Printf(_L("Passed...\n"));	
sl@0
  7595
				}
sl@0
  7596
			}
sl@0
  7597
		else
sl@0
  7598
			{
sl@0
  7599
			test.Printf(_L("Skipping...\n"));
sl@0
  7600
			}
sl@0
  7601
		}
sl@0
  7602
	TestEnd();
sl@0
  7603
sl@0
  7604
sl@0
  7605
	test.Next(_L("Test2: No Fixed Flag, Alloc Fixed"));	
sl@0
  7606
	TestStart();
sl@0
  7607
sl@0
  7608
	if(gPagedRom)
sl@0
  7609
		{
sl@0
  7610
		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
sl@0
  7611
		test_KErrNone(r);
sl@0
  7612
		}
sl@0
  7613
sl@0
  7614
	// find a zone that has free pages in it to block it
sl@0
  7615
	GetAllPageInfo();
sl@0
  7616
	TUint i = 0;
sl@0
  7617
	for (; i < gZoneCount; i++)
sl@0
  7618
		{
sl@0
  7619
		if (gZoneUtilArray[i].iFreePages > 0)
sl@0
  7620
			{
sl@0
  7621
			index = i;
sl@0
  7622
			break;
sl@0
  7623
			}
sl@0
  7624
		}
sl@0
  7625
	
sl@0
  7626
	if (i == gZoneCount)
sl@0
  7627
		{
sl@0
  7628
		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  7629
		}
sl@0
  7630
	else
sl@0
  7631
		{
sl@0
  7632
		zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  7633
sl@0
  7634
		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
sl@0
  7635
		test_KErrNone(r);
sl@0
  7636
sl@0
  7637
		GetOriginalPageCount();
sl@0
  7638
		r = Ldd.AllocateFixed(FILL_ALL_FIXED);
sl@0
  7639
sl@0
  7640
		GetAllPageInfo();
sl@0
  7641
		// Ensure that either zone does not contain extra fixed pages 
sl@0
  7642
		if (gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
sl@0
  7643
			{
sl@0
  7644
			test.Printf(_L("Fail: Fixed pages have been allocated into the zone ID 0x%x r = %d\n"), zoneDefragID, r);
sl@0
  7645
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  7646
			TEST_FAIL;
sl@0
  7647
			}
sl@0
  7648
		else
sl@0
  7649
			{
sl@0
  7650
			test.Printf(_L("Passed...\n"));	
sl@0
  7651
			}
sl@0
  7652
		}	
sl@0
  7653
	TestEnd();
sl@0
  7654
sl@0
  7655
sl@0
  7656
	test.Next(_L("Test3: No Fixed Flag, Alloc Discardable"));	
sl@0
  7657
	TestStart();
sl@0
  7658
	if (gPagedRom)
sl@0
  7659
		{
sl@0
  7660
		index = GetBestZone(BEST_DISCARDABLE);
sl@0
  7661
			
sl@0
  7662
		if (index == KErrNotFound)
sl@0
  7663
			{
sl@0
  7664
			test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  7665
			}
sl@0
  7666
		else
sl@0
  7667
			{
sl@0
  7668
			zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  7669
			GetOriginalPageCount();
sl@0
  7670
			
sl@0
  7671
			r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
sl@0
  7672
			test_KErrNone(r);
sl@0
  7673
sl@0
  7674
			UpdateRamInfo();
sl@0
  7675
			TInt discardablePages;
sl@0
  7676
			r = AllocDiscardable(discardablePages);
sl@0
  7677
			if (r != KErrNone)
sl@0
  7678
				{
sl@0
  7679
				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  7680
				TEST_FAIL;
sl@0
  7681
				}
sl@0
  7682
			
sl@0
  7683
			GetAllPageInfo();
sl@0
  7684
			if (gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
sl@0
  7685
				{
sl@0
  7686
				test.Printf(_L("Fail: Fixed pages have been allocated\n"));
sl@0
  7687
				CLEANUP(ResetDPCache());
sl@0
  7688
				TEST_FAIL;
sl@0
  7689
				}
sl@0
  7690
			else
sl@0
  7691
				{
sl@0
  7692
				test.Printf(_L("Passed...\n"));	
sl@0
  7693
				}
sl@0
  7694
			}		
sl@0
  7695
		}
sl@0
  7696
	else
sl@0
  7697
		{
sl@0
  7698
		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
sl@0
  7699
		}
sl@0
  7700
	TestEnd();
sl@0
  7701
	
sl@0
  7702
	test.Next(_L("Test4: No Fixed Flag, Alloc Fixed using ZoneAllocPhyicalRam"));	
sl@0
  7703
	TestStart();
sl@0
  7704
	
sl@0
  7705
	index = GetBestZone(BEST_FIXED);
sl@0
  7706
	
sl@0
  7707
	if (index == KErrNotFound)
sl@0
  7708
		{
sl@0
  7709
		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  7710
		}
sl@0
  7711
	else
sl@0
  7712
		{
sl@0
  7713
		zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  7714
sl@0
  7715
		GetOriginalPageCount();
sl@0
  7716
		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
sl@0
  7717
		test_KErrNone(r);		
sl@0
  7718
sl@0
  7719
		// Just need to try and allocate one page
sl@0
  7720
		r = Ldd.ZoneAllocDiscontiguous(zoneDefragID, 1);
sl@0
  7721
sl@0
  7722
		GetAllPageInfo();
sl@0
  7723
		if (r != KErrNone)
sl@0
  7724
			{
sl@0
  7725
			test.Printf(_L("Fail: Fixed pages have not been allocated into the zone\n"));
sl@0
  7726
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  7727
			TEST_FAIL;
sl@0
  7728
			}
sl@0
  7729
		else
sl@0
  7730
			{
sl@0
  7731
			test.Printf(_L("Passed...\n"));	
sl@0
  7732
			}
sl@0
  7733
		}	
sl@0
  7734
	TestEnd();
sl@0
  7735
sl@0
  7736
	test.Next(_L("Test5: No Fixed Flag, Alloc Fixed by attempting to claim zone"));	
sl@0
  7737
	TestStart();
sl@0
  7738
	
sl@0
  7739
	GetAllPageInfo();
sl@0
  7740
	index = 0;	
sl@0
  7741
	while ((TUint)index < gZoneCount && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
sl@0
  7742
		{
sl@0
  7743
		index++;
sl@0
  7744
		}
sl@0
  7745
	
sl@0
  7746
	if ((TUint)index == gZoneCount)
sl@0
  7747
		{
sl@0
  7748
		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  7749
		}
sl@0
  7750
	else
sl@0
  7751
		{
sl@0
  7752
		zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  7753
		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
sl@0
  7754
		test_KErrNone(r);		
sl@0
  7755
sl@0
  7756
		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneDefragID);
sl@0
  7757
		GetAllPageInfo();
sl@0
  7758
		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  7759
		if (freeInOthers && r != KErrNone)
sl@0
  7760
			{
sl@0
  7761
			test.Printf(_L("Fail: Claim zone ID 0x%x was unsuccessful, r = %d\n"), zoneDefragID, r);
sl@0
  7762
			TEST_FAIL;
sl@0
  7763
			}
sl@0
  7764
		else
sl@0
  7765
			{
sl@0
  7766
			test.Printf(_L("Passed...\n"));	
sl@0
  7767
			}			
sl@0
  7768
		}	
sl@0
  7769
	TestEnd();
sl@0
  7770
	
sl@0
  7771
sl@0
  7772
sl@0
  7773
//------------------------------------------------------------
sl@0
  7774
	test.Next(_L("Test6: No Movable Flag, Alloc Movable"));	
sl@0
  7775
	TestStart();
sl@0
  7776
	
sl@0
  7777
	index = GetBestZone(BEST_MOVABLE);
sl@0
  7778
	
sl@0
  7779
	if (index == KErrNotFound)
sl@0
  7780
		{
sl@0
  7781
		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  7782
		}
sl@0
  7783
	else
sl@0
  7784
		{
sl@0
  7785
		zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  7786
sl@0
  7787
		GetOriginalPageCount();
sl@0
  7788
		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
sl@0
  7789
		test_KErrNone(r);
sl@0
  7790
		
sl@0
  7791
		AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable, KChunkDefaultSize, EFalse);
sl@0
  7792
sl@0
  7793
		GetAllPageInfo();
sl@0
  7794
		if (gZoneUtilArray[index].iAllocMovable >  gOriginalPageCountArray[index].iAllocMovable)
sl@0
  7795
			{
sl@0
  7796
			test.Printf(_L("Fail: Movable pages have been allocated in the zone\n"));
sl@0
  7797
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  7798
			TEST_FAIL;
sl@0
  7799
			}
sl@0
  7800
		else
sl@0
  7801
			{
sl@0
  7802
			test.Printf(_L("Passed...\n"));	
sl@0
  7803
			}		
sl@0
  7804
		}
sl@0
  7805
	TestEnd();
sl@0
  7806
sl@0
  7807
sl@0
  7808
	test.Next(_L("Test7: No Movable Flag, Alloc Fixed"));	
sl@0
  7809
	TestStart();
sl@0
  7810
	
sl@0
  7811
	if(gPagedRom)
sl@0
  7812
		{
sl@0
  7813
		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
sl@0
  7814
		test_KErrNone(r);
sl@0
  7815
		}
sl@0
  7816
sl@0
  7817
	index = GetBestZone(BEST_FIXED);
sl@0
  7818
		
sl@0
  7819
	if (index == KErrNotFound)
sl@0
  7820
		{
sl@0
  7821
		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  7822
		}
sl@0
  7823
	else
sl@0
  7824
		{ 
sl@0
  7825
		zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  7826
sl@0
  7827
		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
sl@0
  7828
		test_KErrNone(r);
sl@0
  7829
sl@0
  7830
		GetOriginalPageCount();
sl@0
  7831
		// Fill up all of RAM with fixed pages.
sl@0
  7832
		r = Ldd.AllocateFixed(FILL_ALL_FIXED);
sl@0
  7833
		test.Printf(_L("r = %d\n"), r);
sl@0
  7834
sl@0
  7835
		GetAllPageInfo();
sl@0
  7836
		if (gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable || 
sl@0
  7837
			gZoneUtilArray[index].iAllocFixed <= gOriginalPageCountArray[index].iAllocFixed)
sl@0
  7838
			{
sl@0
  7839
			test.Printf(_L("Fail: orig mov 0x%x fix 0x%x current mov 0x%x fix 0x%x\n"), gOriginalPageCountArray[index].iAllocMovable,
sl@0
  7840
						gOriginalPageCountArray[index].iAllocFixed, gZoneUtilArray[index].iAllocMovable, gZoneUtilArray[index].iAllocFixed);
sl@0
  7841
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  7842
			TEST_FAIL;
sl@0
  7843
			}
sl@0
  7844
		else
sl@0
  7845
			{
sl@0
  7846
			test.Printf(_L("Passed...\n"));	
sl@0
  7847
			}
sl@0
  7848
		}
sl@0
  7849
	TestEnd();
sl@0
  7850
sl@0
  7851
sl@0
  7852
	test.Next(_L("Test8: No Movable Flag, Alloc Discardable"));	
sl@0
  7853
	TestStart();
sl@0
  7854
	if (gPagedRom)
sl@0
  7855
		{	
sl@0
  7856
		index = GetBestZone(BEST_DISCARDABLE);
sl@0
  7857
		
sl@0
  7858
		if (index == KErrNotFound)
sl@0
  7859
			{
sl@0
  7860
			test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  7861
			}
sl@0
  7862
		else
sl@0
  7863
			{
sl@0
  7864
			zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  7865
sl@0
  7866
			GetOriginalPageCount();
sl@0
  7867
			r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
sl@0
  7868
			test_KErrNone(r)
sl@0
  7869
			UpdateRamInfo();
sl@0
  7870
			TInt discardablePages;
sl@0
  7871
			r = AllocDiscardable(discardablePages);
sl@0
  7872
			if (r != KErrNone)
sl@0
  7873
				{
sl@0
  7874
				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  7875
				TEST_FAIL;
sl@0
  7876
				}
sl@0
  7877
sl@0
  7878
			GetAllPageInfo();
sl@0
  7879
			if (gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable)
sl@0
  7880
				{
sl@0
  7881
				test.Printf(_L("Fail: Movable pages have been allocated into the zone \n"));
sl@0
  7882
				CLEANUP(ResetDPCache());
sl@0
  7883
				TEST_FAIL;
sl@0
  7884
				}
sl@0
  7885
			else
sl@0
  7886
				{
sl@0
  7887
				test.Printf(_L("Passed...\n"));	
sl@0
  7888
				}
sl@0
  7889
			}
sl@0
  7890
		}
sl@0
  7891
	else
sl@0
  7892
		{
sl@0
  7893
		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
sl@0
  7894
		}
sl@0
  7895
	TestEnd();
sl@0
  7896
sl@0
  7897
//-----------------------------------------------------------------------------------------------
sl@0
  7898
sl@0
  7899
	test.Next(_L("Test9: No Discardable Flag, Alloc Movable"));	
sl@0
  7900
	TestStart();
sl@0
  7901
	
sl@0
  7902
	index = GetBestZone(BEST_MOVABLE);
sl@0
  7903
sl@0
  7904
	if (index == KErrNotFound)
sl@0
  7905
		{
sl@0
  7906
		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  7907
		}
sl@0
  7908
	else
sl@0
  7909
		{
sl@0
  7910
		zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  7911
sl@0
  7912
		GetOriginalPageCount();
sl@0
  7913
		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_DISCARD_FLAG);
sl@0
  7914
		test_KErrNone(r);
sl@0
  7915
sl@0
  7916
		AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
sl@0
  7917
		GetAllPageInfo();
sl@0
  7918
sl@0
  7919
		if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable || 
sl@0
  7920
			(gZoneUtilArray[index].iAllocMovable <=  gOriginalPageCountArray[index].iAllocMovable &&
sl@0
  7921
			gZoneUtilArray[index].iAllocFixed ==  gOriginalPageCountArray[index].iAllocFixed))
sl@0
  7922
			{
sl@0
  7923
			test.Printf(_L("Fail: Either discardable pages have been allocated or movable pages have not been allocated\n"));
sl@0
  7924
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  7925
			TEST_FAIL;
sl@0
  7926
			}
sl@0
  7927
		else
sl@0
  7928
			{
sl@0
  7929
			test.Printf(_L("Passed...\n"));	
sl@0
  7930
			}
sl@0
  7931
		}
sl@0
  7932
	TestEnd();
sl@0
  7933
sl@0
  7934
sl@0
  7935
	test.Next(_L("Test10: No Discardable Flag, Alloc Fixed"));	
sl@0
  7936
	TestStart();
sl@0
  7937
	
sl@0
  7938
	if(gPagedRom)
sl@0
  7939
		{
sl@0
  7940
		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
sl@0
  7941
		test_KErrNone(r);
sl@0
  7942
		}
sl@0
  7943
sl@0
  7944
	index = GetBestZone(BEST_FIXED);
sl@0
  7945
			
sl@0
  7946
	if (index == KErrNotFound)
sl@0
  7947
		{
sl@0
  7948
		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  7949
		}
sl@0
  7950
	else
sl@0
  7951
		{ 
sl@0
  7952
		zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  7953
sl@0
  7954
		GetOriginalPageCount();
sl@0
  7955
		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_DISCARD_FLAG);
sl@0
  7956
		test_KErrNone(r);
sl@0
  7957
		// Fill up all of RAM with fixed pages.
sl@0
  7958
		r = Ldd.AllocateFixed(FILL_ALL_FIXED);
sl@0
  7959
sl@0
  7960
		GetAllPageInfo();
sl@0
  7961
		if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable || 
sl@0
  7962
			gZoneUtilArray[index].iAllocFixed <= gOriginalPageCountArray[index].iAllocFixed)
sl@0
  7963
			{
sl@0
  7964
			test.Printf(_L("Fail: Discardable pages have been allocated or fixed pages have not been allocated\n"));
sl@0
  7965
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  7966
			TEST_FAIL;
sl@0
  7967
			}
sl@0
  7968
		else
sl@0
  7969
			{
sl@0
  7970
			test.Printf(_L("Passed...\n"));	
sl@0
  7971
			}
sl@0
  7972
		}
sl@0
  7973
	TestEnd();
sl@0
  7974
sl@0
  7975
sl@0
  7976
	test.Next(_L("Test11: No Discardable Flag, Alloc Discardable"));	
sl@0
  7977
	TestStart();
sl@0
  7978
	if (gPagedRom)
sl@0
  7979
		{
sl@0
  7980
		index = GetBestZone(BEST_DISCARDABLE);
sl@0
  7981
			
sl@0
  7982
		if (index == KErrNotFound)
sl@0
  7983
			{
sl@0
  7984
			test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  7985
			}
sl@0
  7986
		else
sl@0
  7987
			{
sl@0
  7988
			zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  7989
			GetOriginalPageCount();
sl@0
  7990
			r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_DISCARD_FLAG);
sl@0
  7991
			test_KErrNone(r);
sl@0
  7992
			UpdateRamInfo();
sl@0
  7993
			
sl@0
  7994
			TInt discardablePages;
sl@0
  7995
			r = AllocDiscardable(discardablePages);
sl@0
  7996
			if (r != KErrNoMemory)
sl@0
  7997
				{// Allocation should fail as no dis flag is set
sl@0
  7998
				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  7999
				TEST_FAIL;
sl@0
  8000
				}
sl@0
  8001
			GetAllPageInfo();
sl@0
  8002
sl@0
  8003
			if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable)
sl@0
  8004
				{
sl@0
  8005
				test.Printf(_L("Fail: Discardable pages have been allocated into the zone\n"));
sl@0
  8006
				CLEANUP(ResetDPCache());
sl@0
  8007
				TEST_FAIL;
sl@0
  8008
				}
sl@0
  8009
			else
sl@0
  8010
				{
sl@0
  8011
				test.Printf(_L("Passed...\n"));	
sl@0
  8012
				}
sl@0
  8013
			}
sl@0
  8014
		}
sl@0
  8015
	else
sl@0
  8016
		{
sl@0
  8017
		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
sl@0
  8018
		}
sl@0
  8019
	TestEnd();
sl@0
  8020
sl@0
  8021
//-----------------------------------------------------------------------------------------------
sl@0
  8022
sl@0
  8023
	test.Next(_L("Test12: Only Discardable Flag, Alloc Movable"));	
sl@0
  8024
	TestStart();
sl@0
  8025
	
sl@0
  8026
	index = GetBestZone(BEST_MOVABLE);
sl@0
  8027
sl@0
  8028
	if (index == KErrNotFound)
sl@0
  8029
		{
sl@0
  8030
		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  8031
		}
sl@0
  8032
	else
sl@0
  8033
		{
sl@0
  8034
		zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  8035
sl@0
  8036
		GetOriginalPageCount();
sl@0
  8037
		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
sl@0
  8038
		test_KErrNone(r);
sl@0
  8039
		AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable, KChunkDefaultSize, EFalse);
sl@0
  8040
		GetAllPageInfo();
sl@0
  8041
sl@0
  8042
		if (gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable)
sl@0
  8043
			{
sl@0
  8044
			test.Printf(_L("Fail: Movable pages have been allocated\n"));
sl@0
  8045
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  8046
			TEST_FAIL;
sl@0
  8047
			}
sl@0
  8048
		else
sl@0
  8049
			{
sl@0
  8050
			test.Printf(_L("Passed...\n"));	
sl@0
  8051
			}
sl@0
  8052
		}
sl@0
  8053
	TestEnd();
sl@0
  8054
sl@0
  8055
sl@0
  8056
	test.Next(_L("Test13: Only Discardable Flag, Alloc Fixed"));	
sl@0
  8057
	TestStart();
sl@0
  8058
	
sl@0
  8059
	if(gPagedRom)
sl@0
  8060
		{
sl@0
  8061
		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
sl@0
  8062
		test_KErrNone(r);
sl@0
  8063
		}
sl@0
  8064
sl@0
  8065
	index = GetBestZone(BEST_FIXED);
sl@0
  8066
sl@0
  8067
	if (index == KErrNotFound)
sl@0
  8068
		{
sl@0
  8069
		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  8070
		}
sl@0
  8071
	else
sl@0
  8072
		{
sl@0
  8073
		zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  8074
sl@0
  8075
		GetOriginalPageCount();
sl@0
  8076
		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
sl@0
  8077
		test_KErrNone(r);
sl@0
  8078
sl@0
  8079
		// Fill up all of RAM with fixed pages.
sl@0
  8080
		r = Ldd.AllocateFixed(FILL_ALL_FIXED);
sl@0
  8081
sl@0
  8082
		GetAllPageInfo();
sl@0
  8083
		if (gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
sl@0
  8084
			{
sl@0
  8085
			test.Printf(_L("Fail: Fixed pages have been allocated\n"));
sl@0
  8086
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  8087
			TEST_FAIL;
sl@0
  8088
			}
sl@0
  8089
		else
sl@0
  8090
			{
sl@0
  8091
			test.Printf(_L("Passed...\n"));	
sl@0
  8092
			}	
sl@0
  8093
		}
sl@0
  8094
	TestEnd();
sl@0
  8095
sl@0
  8096
sl@0
  8097
	test.Next(_L("Test14: Only Discardable Flag, Alloc Discardable"));	
sl@0
  8098
	TestStart();
sl@0
  8099
	if (gPagedRom)
sl@0
  8100
		{
sl@0
  8101
		index = GetBestZone(BEST_DISCARDABLE);
sl@0
  8102
sl@0
  8103
		if (index == KErrNotFound)
sl@0
  8104
			{
sl@0
  8105
			test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  8106
			}
sl@0
  8107
		else
sl@0
  8108
			{
sl@0
  8109
			zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  8110
sl@0
  8111
			GetOriginalPageCount();
sl@0
  8112
			r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
sl@0
  8113
			test_KErrNone(r);
sl@0
  8114
			UpdateRamInfo();
sl@0
  8115
			
sl@0
  8116
			TInt discardablePages;
sl@0
  8117
			r = AllocDiscardable(discardablePages);
sl@0
  8118
			if (r != KErrNone)
sl@0
  8119
				{
sl@0
  8120
				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  8121
				TEST_FAIL;
sl@0
  8122
				}
sl@0
  8123
			GetAllPageInfo();
sl@0
  8124
sl@0
  8125
			if (gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable ||
sl@0
  8126
				gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
sl@0
  8127
				{
sl@0
  8128
				test.Printf(_L("Fail: Pages other than discardable have been allocated\n"));
sl@0
  8129
				CLEANUP(ResetDPCache());
sl@0
  8130
				TEST_FAIL;
sl@0
  8131
				}
sl@0
  8132
			else
sl@0
  8133
				{
sl@0
  8134
				test.Printf(_L("Passed...\n"));	
sl@0
  8135
				}
sl@0
  8136
			}
sl@0
  8137
		}
sl@0
  8138
	else
sl@0
  8139
		{
sl@0
  8140
		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
sl@0
  8141
		}
sl@0
  8142
	TestEnd();
sl@0
  8143
sl@0
  8144
	test.Next(_L("Test15: Only Discardable Flag, Alloc Fixed using ZoneAllocPhyicalRam"));	
sl@0
  8145
	TestStart();
sl@0
  8146
	
sl@0
  8147
	index = GetBestZone(BEST_FIXED);
sl@0
  8148
	
sl@0
  8149
	if (index == KErrNotFound)
sl@0
  8150
		{
sl@0
  8151
		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  8152
		}
sl@0
  8153
	else
sl@0
  8154
		{
sl@0
  8155
		zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  8156
sl@0
  8157
		GetOriginalPageCount();
sl@0
  8158
		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
sl@0
  8159
		test_KErrNone(r);		
sl@0
  8160
sl@0
  8161
		// Just need to try and allocate one page
sl@0
  8162
		r = Ldd.ZoneAllocDiscontiguous(zoneDefragID, 1);
sl@0
  8163
sl@0
  8164
		GetAllPageInfo();
sl@0
  8165
		if (r != KErrNone)
sl@0
  8166
			{
sl@0
  8167
			test.Printf(_L("Fail: Fixed pages have not been allocated into zone ID 0x%x\n"), zoneDefragID);
sl@0
  8168
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  8169
			TEST_FAIL;
sl@0
  8170
			}
sl@0
  8171
		else
sl@0
  8172
			{
sl@0
  8173
			test.Printf(_L("Passed...\n"));	
sl@0
  8174
			}	
sl@0
  8175
		}	
sl@0
  8176
	TestEnd();
sl@0
  8177
sl@0
  8178
	test.Next(_L("Test16: Only Discardable Flag, Alloc Fixed by attempting to claim zone"));	
sl@0
  8179
	TestStart();
sl@0
  8180
	
sl@0
  8181
	GetAllPageInfo();
sl@0
  8182
	index = 0;	
sl@0
  8183
	while ((TUint)index < gZoneCount && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
sl@0
  8184
		{
sl@0
  8185
		index++;
sl@0
  8186
		}
sl@0
  8187
	
sl@0
  8188
	if ((TUint)index == gZoneCount)
sl@0
  8189
		{
sl@0
  8190
		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  8191
		}
sl@0
  8192
	else
sl@0
  8193
		{
sl@0
  8194
		zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  8195
		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
sl@0
  8196
		test_KErrNone(r);		
sl@0
  8197
sl@0
  8198
		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneDefragID);
sl@0
  8199
		GetAllPageInfo();
sl@0
  8200
		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  8201
		if (freeInOthers && r != KErrNone)
sl@0
  8202
			{
sl@0
  8203
			test.Printf(_L("Fail: Claim zone ID 0x%x was unsuccessful, r = %d\n"), zoneDefragID, r);
sl@0
  8204
			TEST_FAIL;
sl@0
  8205
			}
sl@0
  8206
		else
sl@0
  8207
			{
sl@0
  8208
			test.Printf(_L("Passed...\n"));	
sl@0
  8209
			}		
sl@0
  8210
		}	
sl@0
  8211
	TestEnd();
sl@0
  8212
//-----------------------------------------------------------------------------------------------
sl@0
  8213
sl@0
  8214
	test.Next(_L("Test17: No further alloc Flag, Alloc Movable"));	
sl@0
  8215
	TestStart();
sl@0
  8216
	
sl@0
  8217
	index = GetBestZone(BEST_MOVABLE);
sl@0
  8218
sl@0
  8219
	
sl@0
  8220
	if (index == KErrNotFound)
sl@0
  8221
		{
sl@0
  8222
		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  8223
		}
sl@0
  8224
	else
sl@0
  8225
		{
sl@0
  8226
		zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  8227
sl@0
  8228
		GetOriginalPageCount();
sl@0
  8229
		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
sl@0
  8230
		test_KErrNone(r);
sl@0
  8231
		AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable, KChunkDefaultSize, EFalse);
sl@0
  8232
		GetAllPageInfo();
sl@0
  8233
sl@0
  8234
		if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable || 
sl@0
  8235
			gZoneUtilArray[index].iAllocMovable >  gOriginalPageCountArray[index].iAllocMovable || 
sl@0
  8236
			gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
sl@0
  8237
			{
sl@0
  8238
			test.Printf(_L("Fail: Pages been allocated\n"));
sl@0
  8239
			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  8240
			TEST_FAIL;
sl@0
  8241
			}
sl@0
  8242
		else
sl@0
  8243
			{
sl@0
  8244
			test.Printf(_L("Passed...\n"));	
sl@0
  8245
			}
sl@0
  8246
		}
sl@0
  8247
	TestEnd();
sl@0
  8248
sl@0
  8249
sl@0
  8250
	test.Next(_L("Test18: No further alloc Flag, Alloc Fixed"));	
sl@0
  8251
	TestStart();
sl@0
  8252
	
sl@0
  8253
	if(gPagedRom)
sl@0
  8254
		{
sl@0
  8255
		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
sl@0
  8256
		test_KErrNone(r);
sl@0
  8257
		}
sl@0
  8258
sl@0
  8259
	// Find a zone with free pages and set the flag on this zone 
sl@0
  8260
	// as it will ensure that you cannot fill all of free RAM with fixed pages
sl@0
  8261
	index = KErrNotFound;
sl@0
  8262
	GetAllPageInfo();
sl@0
  8263
	for (TUint i = 0; i < gZoneCount; i++)
sl@0
  8264
		{
sl@0
  8265
		if (gZoneUtilArray[i].iFreePages != 0)
sl@0
  8266
			{
sl@0
  8267
			index = i;
sl@0
  8268
			break;
sl@0
  8269
			}
sl@0
  8270
		}
sl@0
  8271
	if (index == KErrNotFound)
sl@0
  8272
		{
sl@0
  8273
		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  8274
		}
sl@0
  8275
	else
sl@0
  8276
		{ 
sl@0
  8277
		zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  8278
sl@0
  8279
		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
sl@0
  8280
		test_KErrNone(r);
sl@0
  8281
sl@0
  8282
		GetOriginalPageCount();
sl@0
  8283
		r = Ldd.AllocateFixed(FILL_ALL_FIXED);
sl@0
  8284
		
sl@0
  8285
		// Ensure memory wasn't filled as it should have hit the blocked zone.
sl@0
  8286
		GetAllPageInfo();
sl@0
  8287
		if (r != KErrNoMemory || gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
sl@0
  8288
			{
sl@0
  8289
			test.Printf(_L("Fail: orig fix 0x%x current fix 0x%x\n"), gOriginalPageCountArray[index].iAllocFixed, gZoneUtilArray[index].iAllocFixed);
sl@0
  8290
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  8291
			TEST_FAIL;
sl@0
  8292
			}
sl@0
  8293
		else
sl@0
  8294
			{
sl@0
  8295
			test.Printf(_L("Passed...\n"));	
sl@0
  8296
			}
sl@0
  8297
		}
sl@0
  8298
	TestEnd();
sl@0
  8299
sl@0
  8300
sl@0
  8301
	test.Next(_L("Test19: No further alloc Flag, Alloc Discardable"));	
sl@0
  8302
	TestStart();
sl@0
  8303
	if (gPagedRom)
sl@0
  8304
		{
sl@0
  8305
		index = GetBestZone(BEST_DISCARDABLE);
sl@0
  8306
		
sl@0
  8307
		if (index == KErrNotFound)
sl@0
  8308
			{
sl@0
  8309
			test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  8310
			}
sl@0
  8311
		else
sl@0
  8312
			{
sl@0
  8313
			zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  8314
sl@0
  8315
			GetOriginalPageCount();
sl@0
  8316
			r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
sl@0
  8317
			test_KErrNone(r);
sl@0
  8318
			UpdateRamInfo();
sl@0
  8319
			TInt discardablePages;
sl@0
  8320
			r = AllocDiscardable(discardablePages);
sl@0
  8321
			if (r != KErrNoMemory)
sl@0
  8322
				{// Allocation should fail as no alloc flag is set
sl@0
  8323
				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  8324
				TEST_FAIL;
sl@0
  8325
				}
sl@0
  8326
			GetAllPageInfo();
sl@0
  8327
sl@0
  8328
			if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable || 
sl@0
  8329
				gZoneUtilArray[index].iAllocMovable >  gOriginalPageCountArray[index].iAllocMovable || 
sl@0
  8330
				gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
sl@0
  8331
				{
sl@0
  8332
				test.Printf(_L("Fail: Pages have been allocated into the zone\n"));
sl@0
  8333
				CLEANUP(ResetDPCache());
sl@0
  8334
				TEST_FAIL;
sl@0
  8335
				}
sl@0
  8336
			else
sl@0
  8337
				{
sl@0
  8338
				test.Printf(_L("Passed...\n"));	
sl@0
  8339
				}
sl@0
  8340
			}
sl@0
  8341
		}
sl@0
  8342
	else
sl@0
  8343
		{
sl@0
  8344
		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
sl@0
  8345
		}
sl@0
  8346
	TestEnd();
sl@0
  8347
sl@0
  8348
	
sl@0
  8349
	test.Next(_L("Test20: No Further Alloc Flag, Alloc Fixed using ZoneAllocPhyicalRam"));	
sl@0
  8350
	TestStart();
sl@0
  8351
	
sl@0
  8352
	GetAllPageInfo();
sl@0
  8353
	index = 0;
sl@0
  8354
	while ((TUint)index < gZoneCount && (gZoneUtilArray[index].iFreePages == 0 || 
sl@0
  8355
			gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
sl@0
  8356
		{
sl@0
  8357
		index++;
sl@0
  8358
		}
sl@0
  8359
	
sl@0
  8360
	if ((TUint)index == gZoneCount)
sl@0
  8361
		{
sl@0
  8362
		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  8363
		}
sl@0
  8364
	else
sl@0
  8365
		{
sl@0
  8366
		zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  8367
sl@0
  8368
		GetOriginalPageCount();
sl@0
  8369
		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
sl@0
  8370
		test_KErrNone(r);		
sl@0
  8371
sl@0
  8372
		// Just need to try and allocate one page
sl@0
  8373
		r = Ldd.ZoneAllocDiscontiguous(zoneDefragID, 1);
sl@0
  8374
sl@0
  8375
		GetAllPageInfo();
sl@0
  8376
		if (r != KErrNoMemory)
sl@0
  8377
			{
sl@0
  8378
			test.Printf(_L("Fail: Fixed pages have been allocated into zone ID 0x%x\n"), zoneDefragID);
sl@0
  8379
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  8380
			TEST_FAIL;
sl@0
  8381
			}
sl@0
  8382
		else
sl@0
  8383
			{
sl@0
  8384
			test.Printf(_L("Passed...\n"));	
sl@0
  8385
			}
sl@0
  8386
		}	
sl@0
  8387
	TestEnd();
sl@0
  8388
sl@0
  8389
	test.Next(_L("Test21: No Further Alloc Flag, Alloc Fixed by attempting to claim zone"));	
sl@0
  8390
	TestStart();
sl@0
  8391
	
sl@0
  8392
	GetAllPageInfo();
sl@0
  8393
	index = 0;	
sl@0
  8394
	while ((TUint)index < gZoneCount && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
sl@0
  8395
		{
sl@0
  8396
		index++;
sl@0
  8397
		}
sl@0
  8398
	
sl@0
  8399
	if ((TUint)index == gZoneCount)
sl@0
  8400
		{
sl@0
  8401
		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
sl@0
  8402
		}
sl@0
  8403
	else
sl@0
  8404
		{
sl@0
  8405
		zoneDefragID = gZoneConfigArray[index].iZoneId;
sl@0
  8406
		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
sl@0
  8407
		test_KErrNone(r);		
sl@0
  8408
sl@0
  8409
		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneDefragID);
sl@0
  8410
		GetAllPageInfo();
sl@0
  8411
		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
sl@0
  8412
		if (freeInOthers && r != KErrNone)
sl@0
  8413
			{
sl@0
  8414
			test.Printf(_L("Fail: Claim zone ID 0x%x was unsuccessful, r = %d\n"), zoneDefragID, r);
sl@0
  8415
			CLEANUP(ResetRamZoneFlags());
sl@0
  8416
			TEST_FAIL;
sl@0
  8417
			}
sl@0
  8418
		else
sl@0
  8419
			{
sl@0
  8420
			test.Printf(_L("Passed...\n"));	
sl@0
  8421
			}
sl@0
  8422
		}	
sl@0
  8423
	TestEnd();
sl@0
  8424
sl@0
  8425
	TUint mostPrefArrayIndex = 0;
sl@0
  8426
	TUint mostPrefIndex = 0;
sl@0
  8427
	TUint leastPrefIndex = 0;
sl@0
  8428
	const TUint KFreeMostPref = 10;
sl@0
  8429
	TUint flag = 0;
sl@0
  8430
	TUint prefIndex;
sl@0
  8431
	TUint totalFree;
sl@0
  8432
sl@0
  8433
sl@0
  8434
	for (TUint testStep = 0; testStep < 2; testStep++)
sl@0
  8435
		{
sl@0
  8436
		switch(testStep)
sl@0
  8437
			{
sl@0
  8438
			case 0:
sl@0
  8439
				test.Next(_L("Test22: Ensure that the General Defrag looks at the flags 1"));
sl@0
  8440
				break;
sl@0
  8441
sl@0
  8442
			case 1:
sl@0
  8443
				test.Next(_L("Test23: Ensure that the General Defrag looks at the flags 2"));
sl@0
  8444
				break;
sl@0
  8445
			}
sl@0
  8446
			
sl@0
  8447
		TestStart();
sl@0
  8448
		gChunkArray1 = new RChunk;	
sl@0
  8449
		gChunkArraySize1 = 1;
sl@0
  8450
		TUint minCacheSize = 0;
sl@0
  8451
		TUint maxCacheSize = 0;
sl@0
  8452
		TUint currentCacheSize = 0;
sl@0
  8453
		TUint freeNeededInMostPref = 0;
sl@0
  8454
		if (!gPagedRom)
sl@0
  8455
			{
sl@0
  8456
			test.Printf(_L("Not a paged ROM - Skipping...\n"));
sl@0
  8457
			goto skipTest22;
sl@0
  8458
			}
sl@0
  8459
sl@0
  8460
		// Find the most pref zone with free pages
sl@0
  8461
		r = FindMostPrefWithFree(mostPrefIndex, &mostPrefArrayIndex);
sl@0
  8462
		if (r != KErrNone)
sl@0
  8463
			{
sl@0
  8464
			test.Printf(_L("Cannot find zone with free pages - Skipping...\n"));
sl@0
  8465
			goto skipTest22;
sl@0
  8466
			}
sl@0
  8467
sl@0
  8468
		// Ensure that the least pref zone is empty
sl@0
  8469
		leastPrefIndex = gPrefArray[gZoneCount - 1];
sl@0
  8470
		if (gZoneUtilArray[leastPrefIndex].iFreePages != gZoneUtilArray[leastPrefIndex].iPhysPages)
sl@0
  8471
			{
sl@0
  8472
			test.Printf(_L("Least pref zone is not empty - Skipping...\n"));
sl@0
  8473
			goto skipTest22;
sl@0
  8474
			}
sl@0
  8475
sl@0
  8476
		// Allocate 1 movable page to the least preferable zone
sl@0
  8477
		r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, leastPrefIndex, 1);
sl@0
  8478
		if (r != KErrNone)
sl@0
  8479
			{			
sl@0
  8480
			test.Printf(_L("Failed to allocate movable r = %d - Skipping...\n"), r);
sl@0
  8481
			goto skipTest22;
sl@0
  8482
			}
sl@0
  8483
		
sl@0
  8484
		// Allocate 1 discardable page to the least preferable zone
sl@0
  8485
		if (gZoneUtilArray[leastPrefIndex].iFreePages != 0)
sl@0
  8486
			{
sl@0
  8487
			TInt disPages;
sl@0
  8488
			r = ZoneAllocDiscard(leastPrefIndex, 1, disPages);
sl@0
  8489
			if (r != KErrNone)
sl@0
  8490
				{			
sl@0
  8491
				test.Printf(_L("Failed to allocate discardable pages r = %d - Skipping...\n"), r);
sl@0
  8492
				goto skipTest22;
sl@0
  8493
				}
sl@0
  8494
sl@0
  8495
			// up the minimum cache size so that the pages have to be moved - not discarded
sl@0
  8496
			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
sl@0
  8497
			r = DPTest::SetCacheSize(currentCacheSize, currentCacheSize);
sl@0
  8498
			if (r != KErrNone)
sl@0
  8499
				{			
sl@0
  8500
				test.Printf(_L("Failed to set cache size r = %d - Skipping...\n"), r);
sl@0
  8501
				goto skipTest22;
sl@0
  8502
				}
sl@0
  8503
			}
sl@0
  8504
		else
sl@0
  8505
			{
sl@0
  8506
			test.Printf(_L("Least pref zone has no free pages - Skipping...\n"));
sl@0
  8507
			goto skipTest22;
sl@0
  8508
			}
sl@0
  8509
sl@0
  8510
		// Check that the least pref zone has movable and discardable pages in it
sl@0
  8511
		GetAllPageInfo();
sl@0
  8512
		if (gZoneUtilArray[leastPrefIndex].iAllocMovable == 0 ||
sl@0
  8513
			gZoneUtilArray[leastPrefIndex].iAllocDiscardable == 0)
sl@0
  8514
			{
sl@0
  8515
			test.Printf(_L("No movable or discardable in least pref zone\n"));
sl@0
  8516
			PrintPageInfo();
sl@0
  8517
			goto skipTest22;
sl@0
  8518
			}
sl@0
  8519
sl@0
  8520
		ResetRamZoneFlags();
sl@0
  8521
		// if most pref zone has too many free pages fill up with fixed
sl@0
  8522
		if (gZoneUtilArray[mostPrefIndex].iFreePages > KFreeMostPref)
sl@0
  8523
			{
sl@0
  8524
			TUint allocPages = gZoneUtilArray[mostPrefIndex].iFreePages - KFreeMostPref;
sl@0
  8525
			r = Ldd.ZoneAllocToMany(mostPrefIndex, allocPages);
sl@0
  8526
			if (r != KErrNone)
sl@0
  8527
				{	
sl@0
  8528
				test.Printf(_L("Failed allocate 0x%x fixed to index %d r = %d - Skipping...\n"), 
sl@0
  8529
								allocPages, mostPrefIndex,r);
sl@0
  8530
				PrintPageInfo();
sl@0
  8531
				goto skipTest22;
sl@0
  8532
				}
sl@0
  8533
			}
sl@0
  8534
		
sl@0
  8535
		
sl@0
  8536
		// if the no. of discardable pages is less than free in most pref increase the min cache size
sl@0
  8537
		GetAllPageInfo();
sl@0
  8538
		if (gZoneUtilArray[leastPrefIndex].iAllocDiscardable < gZoneUtilArray[mostPrefIndex].iFreePages)
sl@0
  8539
			{
sl@0
  8540
			TUint discDiff = gZoneUtilArray[mostPrefIndex].iFreePages - gZoneUtilArray[leastPrefIndex].iAllocDiscardable;
sl@0
  8541
			test.Printf(_L("discDiff = 0x%x\n"), discDiff);
sl@0
  8542
			TInt disPages;
sl@0
  8543
			if (ZoneAllocDiscard(leastPrefIndex, discDiff, disPages) != KErrNone)
sl@0
  8544
				{			
sl@0
  8545
				test.Printf(_L("Failed allocate discardable to zone index %d- Skipping...\n"), leastPrefIndex);
sl@0
  8546
				goto skipTest22;
sl@0
  8547
				}
sl@0
  8548
			// up the minimum cache size by the difference as we don't want these pages to be discarded
sl@0
  8549
			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
sl@0
  8550
			r = DPTest::SetCacheSize(currentCacheSize, currentCacheSize);
sl@0
  8551
			test.Printf(_L("r = %d\n"), r);
sl@0
  8552
			if (r != KErrNone)
sl@0
  8553
				{
sl@0
  8554
				test.Printf(_L("Failed to set cache size r = %d - Skipping...\n"), r);
sl@0
  8555
				goto skipTest22;
sl@0
  8556
				}
sl@0
  8557
			}
sl@0
  8558
		ResetRamZoneFlags();
sl@0
  8559
sl@0
  8560
		// if the rest of the zones are either full or empty put a fixed page into the next most pref empty zone
sl@0
  8561
		GetPrefOrder();
sl@0
  8562
		prefIndex = 0;
sl@0
  8563
		totalFree = 0;
sl@0
  8564
		for (; prefIndex < gZoneCount && totalFree < gZoneUtilArray[leastPrefIndex].iAllocDiscardable; prefIndex++)
sl@0
  8565
			{// Look for zone that has enough free pages to fit all the discardable
sl@0
  8566
			TUint zoneIndex = gPrefArray[prefIndex];
sl@0
  8567
			if (zoneIndex != mostPrefIndex && zoneIndex != leastPrefIndex)
sl@0
  8568
				{
sl@0
  8569
				if (gZoneUtilArray[zoneIndex].iFreePages != gZoneUtilArray[zoneIndex].iPhysPages)
sl@0
  8570
					{
sl@0
  8571
					totalFree += gZoneUtilArray[zoneIndex].iFreePages;
sl@0
  8572
					}
sl@0
  8573
				else
sl@0
  8574
					{
sl@0
  8575
					r = Ldd.ZoneAllocToMany(zoneIndex, 1);
sl@0
  8576
					if (r != KErrNone)
sl@0
  8577
						{
sl@0
  8578
						test.Printf(_L("Couldn't alloc fixed to zone index %d - r = %d\n"), zoneIndex, r);
sl@0
  8579
						goto skipTest22;
sl@0
  8580
						}
sl@0
  8581
					GetAllPageInfo();
sl@0
  8582
					totalFree += gZoneUtilArray[zoneIndex].iFreePages;
sl@0
  8583
					}
sl@0
  8584
				}
sl@0
  8585
			}
sl@0
  8586
sl@0
  8587
		if (prefIndex >= gZoneCount)
sl@0
  8588
			{
sl@0
  8589
			test.Printf(_L("Couldn't find zone\n"));
sl@0
  8590
			goto skipTest22;
sl@0
  8591
			}
sl@0
  8592
sl@0
  8593
sl@0
  8594
		
sl@0
  8595
		// If the most preferable zone does not have enough free pages, skip
sl@0
  8596
		freeNeededInMostPref = (testStep == 0) ? gZoneUtilArray[leastPrefIndex].iAllocMovable : gZoneUtilArray[leastPrefIndex].iAllocDiscardable;
sl@0
  8597
		if (gZoneUtilArray[mostPrefIndex].iFreePages < freeNeededInMostPref)
sl@0
  8598
			{
sl@0
  8599
			test.Printf(_L("Free needed in mostPref(%d) = %d, Free available in mostPref = %d - skipping...\n"), 
sl@0
  8600
							mostPrefIndex,freeNeededInMostPref,gZoneUtilArray[mostPrefIndex].iFreePages);
sl@0
  8601
			goto skipTest22;
sl@0
  8602
			}
sl@0
  8603
sl@0
  8604
		GetAllPageInfo();
sl@0
  8605
		// Set up the RAM zone flags for the test
sl@0
  8606
		flag = (testStep == 0)? NO_MOVE_FLAG: NO_DISCARD_FLAG;
sl@0
  8607
		// Set all zones except most pref to KRamZoneFlagNoMovable	or KRamZoneFlagNoDiscard
sl@0
  8608
		for (TUint index = 0; index < gZoneCount; index++)
sl@0
  8609
			{
sl@0
  8610
			TUint zoneID = gZoneConfigArray[index].iZoneId;
sl@0
  8611
			if (index != mostPrefIndex)
sl@0
  8612
				{
sl@0
  8613
				r = Ldd.SetZoneFlag(zoneID, gZoneConfigArray[index].iFlags, flag);
sl@0
  8614
				if (r != KErrNone)
sl@0
  8615
					{			
sl@0
  8616
					test.Printf(_L("Failed to set flag r = %d - Skipping...\n"), r);
sl@0
  8617
					goto skipTest22;
sl@0
  8618
					}
sl@0
  8619
				}
sl@0
  8620
			}
sl@0
  8621
		
sl@0
  8622
		GetOriginalPageCount();
sl@0
  8623
		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  8624
		
sl@0
  8625
		GetAllPageInfo();
sl@0
  8626
		switch(testStep)
sl@0
  8627
			{
sl@0
  8628
			case 0:
sl@0
  8629
				if (r != KErrNone ||
sl@0
  8630
					gOriginalPageCountArray[mostPrefIndex].iAllocMovable >= gZoneUtilArray[mostPrefIndex].iAllocMovable ||
sl@0
  8631
					gZoneUtilArray[leastPrefIndex].iFreePages != gZoneUtilArray[leastPrefIndex].iPhysPages)
sl@0
  8632
					{
sl@0
  8633
					test.Printf(_L("FAIL:r=%d MostPref(%d): origMov 0x%x curMov 0x%x LeastPref(%d): origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), 
sl@0
  8634
									r, mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocMovable, gZoneUtilArray[mostPrefIndex].iAllocMovable, 
sl@0
  8635
									leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocMovable, gZoneUtilArray[leastPrefIndex].iAllocMovable,
sl@0
  8636
									gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable, gZoneUtilArray[leastPrefIndex].iAllocDiscardable);
sl@0
  8637
					CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  8638
					TEST_FAIL;
sl@0
  8639
					}
sl@0
  8640
				else
sl@0
  8641
					{
sl@0
  8642
					test.Printf(_L("Passed...\n"));
sl@0
  8643
					}
sl@0
  8644
				break;
sl@0
  8645
sl@0
  8646
			case 1:
sl@0
  8647
				if (r != KErrNone ||
sl@0
  8648
					gOriginalPageCountArray[mostPrefIndex].iAllocDiscardable >= gZoneUtilArray[mostPrefIndex].iAllocDiscardable ||
sl@0
  8649
					gZoneUtilArray[leastPrefIndex].iFreePages != gZoneUtilArray[leastPrefIndex].iPhysPages)
sl@0
  8650
					{
sl@0
  8651
					test.Printf(_L("FAIL:r=%d MostPref(%d): origMov 0x%x curMov 0x%x LeastPref(%d): origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), 
sl@0
  8652
									r, mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocMovable, gZoneUtilArray[mostPrefIndex].iAllocMovable, 
sl@0
  8653
									leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocMovable, gZoneUtilArray[leastPrefIndex].iAllocMovable,
sl@0
  8654
									gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable, gZoneUtilArray[leastPrefIndex].iAllocDiscardable);
sl@0
  8655
					CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  8656
					TEST_FAIL;
sl@0
  8657
					}
sl@0
  8658
				else
sl@0
  8659
					{
sl@0
  8660
					test.Printf(_L("Passed...\n"));
sl@0
  8661
					}
sl@0
  8662
				break;
sl@0
  8663
			}
sl@0
  8664
sl@0
  8665
	skipTest22:
sl@0
  8666
		TestEnd();
sl@0
  8667
		}
sl@0
  8668
sl@0
  8669
	test.Next(_L("Test24: Ensure that the General Defrag doesnt move or discard pages if KRamZoneFlagNoMovable set on all zones "));	
sl@0
  8670
	TestStart();
sl@0
  8671
	gChunkArray1 = new RChunk;
sl@0
  8672
	gChunkArraySize1 = 1;
sl@0
  8673
	// Find the most pref zone with free pages
sl@0
  8674
	GetPrefOrder();
sl@0
  8675
	TInt disPages = 0;	
sl@0
  8676
	if (FindMostPrefWithFree(mostPrefIndex, &mostPrefArrayIndex) != KErrNone)
sl@0
  8677
		{
sl@0
  8678
		test.Printf(_L("Cannot find zone with free pages - Skipping...\n"));
sl@0
  8679
		goto skipTest24;
sl@0
  8680
		}
sl@0
  8681
sl@0
  8682
	// Ensure that the least pref zone has free pages in it
sl@0
  8683
	leastPrefIndex = gPrefArray[gZoneCount-1];
sl@0
  8684
	if (gZoneUtilArray[leastPrefIndex].iFreePages == 0)
sl@0
  8685
		{
sl@0
  8686
		test.Printf(_L("Least pref zone has no free pages - Skipping...\n"));
sl@0
  8687
		goto skipTest24;
sl@0
  8688
		}
sl@0
  8689
sl@0
  8690
	// Allocate 1 movable page to the least preferable zone
sl@0
  8691
	r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, leastPrefIndex, 1);
sl@0
  8692
	if (r != KErrNone)
sl@0
  8693
		{			
sl@0
  8694
		test.Printf(_L("Failed to allocate movable page r = %d - Skipping...\n"), r);
sl@0
  8695
		goto skipTest24;
sl@0
  8696
		}
sl@0
  8697
	
sl@0
  8698
	if (gPagedRom)
sl@0
  8699
		{
sl@0
  8700
		
sl@0
  8701
		TUint minCacheSize = 0;
sl@0
  8702
		TUint maxCacheSize = 0;
sl@0
  8703
		TUint currentCacheSize = 0;
sl@0
  8704
		
sl@0
  8705
		// Allocate 1 discardable page to the least preferable zone
sl@0
  8706
		if (gZoneUtilArray[leastPrefIndex].iFreePages != 0)
sl@0
  8707
			{
sl@0
  8708
			r = ZoneAllocDiscard(leastPrefIndex, 1, disPages);
sl@0
  8709
			if (r != KErrNone)
sl@0
  8710
				{
sl@0
  8711
				test.Printf(_L("Discardable pages not allocated r= %d\n"), r);
sl@0
  8712
				}
sl@0
  8713
			
sl@0
  8714
			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
sl@0
  8715
			r = DPTest::SetCacheSize(currentCacheSize, currentCacheSize);
sl@0
  8716
			if (r != KErrNone)
sl@0
  8717
				{			
sl@0
  8718
				test.Printf(_L("Failed to set cache size r = %d - Skipping...\n"), r);
sl@0
  8719
				goto skipTest24;
sl@0
  8720
				}
sl@0
  8721
			}	
sl@0
  8722
		}
sl@0
  8723
sl@0
  8724
	
sl@0
  8725
	// Check that the least pref zone has movable and discardable (if allocated) pages in it
sl@0
  8726
	GetAllPageInfo();
sl@0
  8727
	if (gZoneUtilArray[leastPrefIndex].iAllocMovable == 0 ||
sl@0
  8728
		gZoneUtilArray[leastPrefIndex].iAllocDiscardable < (TUint)disPages ||
sl@0
  8729
		gZoneUtilArray[leastPrefIndex].iAllocMovable > gTotalPageCount.iFreePages - gZoneUtilArray[leastPrefIndex].iFreePages)
sl@0
  8730
		{
sl@0
  8731
		test.Printf(_L("No movable in least pref zone or no space for moveable pages to be moved to\n"));
sl@0
  8732
		PrintPageInfo();
sl@0
  8733
		goto skipTest24;
sl@0
  8734
		}
sl@0
  8735
sl@0
  8736
	ResetRamZoneFlags();
sl@0
  8737
	GetAllPageInfo();
sl@0
  8738
	// Now set all zones to KRamZoneFlagNoMovable
sl@0
  8739
	for (TUint index = 0; index < gZoneCount; index++)
sl@0
  8740
		{
sl@0
  8741
		TUint zoneID = gZoneConfigArray[index].iZoneId;
sl@0
  8742
		r = Ldd.SetZoneFlag(zoneID, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
sl@0
  8743
		if (r != KErrNone)
sl@0
  8744
			{			
sl@0
  8745
			test.Printf(_L("Failed to set cache size r = %d - Skipping...\n"), r);
sl@0
  8746
			goto skipTest24;
sl@0
  8747
			}
sl@0
  8748
		}
sl@0
  8749
sl@0
  8750
	GetOriginalPageCount();
sl@0
  8751
	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  8752
	GetAllPageInfo();
sl@0
  8753
	if (r != KErrNone ||
sl@0
  8754
		gOriginalPageCountArray[leastPrefIndex].iAllocMovable != gZoneUtilArray[leastPrefIndex].iAllocMovable ||
sl@0
  8755
		gZoneUtilArray[leastPrefIndex].iAllocDiscardable < gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable)
sl@0
  8756
		{
sl@0
  8757
		test.Printf(_L("FAIL:r=%d LeastPref(%d): origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), 
sl@0
  8758
						r,leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocMovable, gZoneUtilArray[leastPrefIndex].iAllocMovable,
sl@0
  8759
						gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable, gZoneUtilArray[leastPrefIndex].iAllocDiscardable);
sl@0
  8760
		CLEANUP(ResetRamZoneFlags());
sl@0
  8761
		TEST_FAIL;
sl@0
  8762
		}
sl@0
  8763
	else
sl@0
  8764
		{
sl@0
  8765
		test.Printf(_L("Passed...\n"));
sl@0
  8766
		}
sl@0
  8767
skipTest24:
sl@0
  8768
	TestEnd();
sl@0
  8769
sl@0
  8770
	test.End();
sl@0
  8771
	return KErrNone;
sl@0
  8772
	}
sl@0
  8773
sl@0
  8774
sl@0
  8775
//
sl@0
  8776
// Template functions encapsulating ControlIo magic
sl@0
  8777
//
sl@0
  8778
template <class C>
sl@0
  8779
TInt controlIo(RFs &fs, TInt drv, TInt fkn, C &c)
sl@0
  8780
	{
sl@0
  8781
    TPtr8 ptrC((TUint8 *)&c, sizeof(C), sizeof(C));
sl@0
  8782
sl@0
  8783
    TInt r = fs.ControlIo(drv, fkn, ptrC);
sl@0
  8784
sl@0
  8785
    return r;
sl@0
  8786
	}
sl@0
  8787
sl@0
  8788
//
sl@0
  8789
// FormatMMC
sl@0
  8790
//
sl@0
  8791
// Formats the MMC card
sl@0
  8792
//
sl@0
  8793
void FormatMMC()
sl@0
  8794
	{	
sl@0
  8795
	test.Printf(_L("Formatting MMC...\n"));
sl@0
  8796
sl@0
  8797
	RFs theFs;
sl@0
  8798
	TBuf<4> driveBuf = _L("D:\\");
sl@0
  8799
	RFormat format;
sl@0
  8800
	TInt count;
sl@0
  8801
	TChar driveLet;
sl@0
  8802
sl@0
  8803
	TInt r = theFs.Connect();
sl@0
  8804
	test_KErrNone(r);
sl@0
  8805
	
sl@0
  8806
	r = theFs.DriveToChar(gDrive, driveLet);
sl@0
  8807
	test_KErrNone(r);
sl@0
  8808
sl@0
  8809
	driveBuf[0] = driveLet;
sl@0
  8810
	test.Printf(_L("Formatting Drive: %C\n"),(TInt)driveLet);
sl@0
  8811
	
sl@0
  8812
	r = format.Open(theFs,driveBuf,EFullFormat,count);
sl@0
  8813
	test_KErrNone(r);
sl@0
  8814
	
sl@0
  8815
	while(count)
sl@0
  8816
		{
sl@0
  8817
		TInt r = format.Next(count);
sl@0
  8818
		test_KErrNone(r);
sl@0
  8819
		}
sl@0
  8820
	format.Close();
sl@0
  8821
	theFs.Close();
sl@0
  8822
	}
sl@0
  8823
sl@0
  8824
sl@0
  8825
//
sl@0
  8826
// FileNameGen
sl@0
  8827
//
sl@0
  8828
// Generates file names to create the files
sl@0
  8829
//
sl@0
  8830
void FileNameGen(TDes16& aBuffer, TInt aLong, TInt aPos) 
sl@0
  8831
	{
sl@0
  8832
	TInt padding;
sl@0
  8833
	TInt i = 0;
sl@0
  8834
	TBuf16<10> tempbuf;
sl@0
  8835
sl@0
  8836
	_LIT(KNumber,"%d");
sl@0
  8837
	tempbuf.Format(KNumber,aPos);
sl@0
  8838
	
sl@0
  8839
	padding = aLong-tempbuf.Size()/2;
sl@0
  8840
	aBuffer = _L("");
sl@0
  8841
	
sl@0
  8842
	while(i < padding)
sl@0
  8843
		{
sl@0
  8844
		aBuffer.Append('F');
sl@0
  8845
		i++;
sl@0
  8846
		}
sl@0
  8847
	aBuffer.Append(tempbuf);
sl@0
  8848
sl@0
  8849
	_LIT(KExtension1, ".TXT");
sl@0
  8850
	aBuffer.Append(KExtension1);
sl@0
  8851
	}
sl@0
  8852
sl@0
  8853
sl@0
  8854
//
sl@0
  8855
// CreateFiles
sl@0
  8856
//
sl@0
  8857
// Creates the files to fill part of the read cache
sl@0
  8858
//
sl@0
  8859
void CreateFiles(TInt aFiles, TInt aFileSize)
sl@0
  8860
	{
sl@0
  8861
	TInt i = 0, r = 0;
sl@0
  8862
	RFile file;
sl@0
  8863
	TBuf16<50> directory;
sl@0
  8864
	
sl@0
  8865
	TBuf16<50> path;
sl@0
  8866
	TBuf16<50> buffer(50); 	
sl@0
  8867
	
sl@0
  8868
	directory = gSessionPath;
sl@0
  8869
	
sl@0
  8870
	test.Printf(_L("Creating %d files for filling the cache (size %d)\n"), aFiles, aFileSize);
sl@0
  8871
sl@0
  8872
	// create a big buffer to speed things up
sl@0
  8873
	HBufC8* bigBuf = NULL;
sl@0
  8874
	const TInt KBigBufferSize = 32 * 1024;
sl@0
  8875
	TRAPD(res,bigBuf = HBufC8::NewL(KBigBufferSize));
sl@0
  8876
	test(res == KErrNone && bigBuf != NULL);
sl@0
  8877
		
sl@0
  8878
	TPtr8 bigBufWritePtr(NULL, 0);	
sl@0
  8879
	bigBufWritePtr.Set(bigBuf->Des());
sl@0
  8880
sl@0
  8881
	// Fill the buffer
sl@0
  8882
	TChar aC = 'A';
sl@0
  8883
	for(i = 0; i < KBigBufferSize; i++)
sl@0
  8884
		{
sl@0
  8885
		bigBufWritePtr.Append((i%32) + aC);
sl@0
  8886
		}
sl@0
  8887
	
sl@0
  8888
sl@0
  8889
	i = 0;		
sl@0
  8890
	while(i < aFiles) 
sl@0
  8891
		{
sl@0
  8892
		if (i % 10 == 0)
sl@0
  8893
			test.Printf(_L("Creating file %d of %d...\r"), i, aFiles);
sl@0
  8894
		FileNameGen(buffer, 8, i+3) ;
sl@0
  8895
		path = directory;
sl@0
  8896
		path.Append(buffer);
sl@0
  8897
sl@0
  8898
		// delete file first to ensure it's contents are not in the cache (file may be on the closed file queue)
sl@0
  8899
		r = gTheFs.Delete(path);
sl@0
  8900
		test(r == KErrNone || r == KErrNotFound);
sl@0
  8901
sl@0
  8902
		r = file.Create(gTheFs,path,EFileShareAny|EFileWrite|EFileReadDirectIO|EFileWriteDirectIO);
sl@0
  8903
		if(r == KErrAlreadyExists) 
sl@0
  8904
			r = file.Open(gTheFs,path,EFileShareAny|EFileWrite|EFileReadDirectIO|EFileWriteDirectIO);
sl@0
  8905
		TInt j = 0;
sl@0
  8906
		i++;
sl@0
  8907
		while(j < aFileSize)
sl@0
  8908
			{
sl@0
  8909
			bigBufWritePtr.SetLength(Min(KBigBufferSize, aFileSize - j));
sl@0
  8910
			r = file.Write(bigBufWritePtr);
sl@0
  8911
			// Running out of disk space is expected for the last file.
sl@0
  8912
			// Premature "disk full" conditions need to abort.
sl@0
  8913
			if (r == KErrDiskFull)
sl@0
  8914
				{
sl@0
  8915
				test(i == aFiles);
sl@0
  8916
				break;
sl@0
  8917
				}
sl@0
  8918
			test_KErrNone(r);
sl@0
  8919
			j += bigBufWritePtr.Length();
sl@0
  8920
			}					
sl@0
  8921
sl@0
  8922
		file.Close();
sl@0
  8923
		}
sl@0
  8924
	test.Printf(_L("\nFiles created\n"));
sl@0
  8925
	delete bigBuf;
sl@0
  8926
	}
sl@0
  8927
sl@0
  8928
sl@0
  8929
//
sl@0
  8930
// FillCache
sl@0
  8931
//
sl@0
  8932
// Allocate discardable pages using file system caching
sl@0
  8933
//
sl@0
  8934
TInt FillCache(TInt aFiles, TInt aFileSize)
sl@0
  8935
	{
sl@0
  8936
	// Fail if files already open
sl@0
  8937
	test(!gFileCacheRun);
sl@0
  8938
sl@0
  8939
sl@0
  8940
	TInt i = 0, r = 0;
sl@0
  8941
	TBuf16<50> directory;
sl@0
  8942
	
sl@0
  8943
	TBuf16<50> path;
sl@0
  8944
	TBuf16<50> buffer(50); 	
sl@0
  8945
	HBufC8* buf = NULL;
sl@0
  8946
	TPtr8 bufPtr(NULL, 0);	
sl@0
  8947
	
sl@0
  8948
	TRAPD(res,buf = HBufC8::NewL(2));
sl@0
  8949
	test(res == KErrNone && buf != NULL);
sl@0
  8950
	bufPtr.Set(buf->Des());
sl@0
  8951
	
sl@0
  8952
	TESTDEBUG(test.Printf(_L("Filling the cache\n")));
sl@0
  8953
sl@0
  8954
	directory = gSessionPath;
sl@0
  8955
	
sl@0
  8956
	i = 0;		
sl@0
  8957
sl@0
  8958
	GetAllPageInfo();
sl@0
  8959
	TESTDEBUG(test.Printf(_L("total disc pages = %d\n"), gTotalPageCount.iDiscardablePages)); 
sl@0
  8960
	
sl@0
  8961
sl@0
  8962
	while(i < aFiles) 
sl@0
  8963
		{
sl@0
  8964
		FileNameGen(buffer, 8, i+3) ;
sl@0
  8965
		path = directory;
sl@0
  8966
		path.Append(buffer);
sl@0
  8967
		r = gFile[i].Open(gTheFs,path,EFileShareAny|EFileRead|EFileReadBuffered|EFileReadAheadOff);
sl@0
  8968
		test_KErrNone(r);
sl@0
  8969
		
sl@0
  8970
		TInt j = 0;
sl@0
  8971
		while(j < aFileSize)
sl@0
  8972
			{
sl@0
  8973
			r = gFile[i].Read(j,bufPtr);
sl@0
  8974
			test_KErrNone(r);
sl@0
  8975
			j += 4 * 1024;
sl@0
  8976
			}					
sl@0
  8977
		i++;
sl@0
  8978
		}
sl@0
  8979
	gFileCacheRun = ETrue;
sl@0
  8980
	GetAllPageInfo();
sl@0
  8981
	TESTDEBUG(test.Printf(_L("after - total disc pages = %d\n"), gTotalPageCount.iDiscardablePages)); 
sl@0
  8982
	delete buf;
sl@0
  8983
	TESTDEBUG(test.Printf(_L("Cache filled\n")));
sl@0
  8984
	
sl@0
  8985
#if defined(_DEBUG) || defined(_DEBUG_RELEASE)
sl@0
  8986
	// get number of items on Page Cache
sl@0
  8987
	TFileCacheStats startPageCacheStats;
sl@0
  8988
sl@0
  8989
	r = controlIo(gTheFs,gDrive, KControlIoFileCacheStats, startPageCacheStats);
sl@0
  8990
	test.Printf(_L("control stats r= %d\n"), r);
sl@0
  8991
	test(r==KErrNone || r == KErrNotSupported);
sl@0
  8992
	
sl@0
  8993
	TESTDEBUG(test.Printf(_L("Allocated segment count=%d\n"),startPageCacheStats.iAllocatedSegmentCount));
sl@0
  8994
#endif
sl@0
  8995
	// if we do not have any discardable pages then something went
sl@0
  8996
	// wrong with file caching
sl@0
  8997
	if (gTotalPageCount.iDiscardablePages == 0)
sl@0
  8998
		return KErrNotSupported;
sl@0
  8999
		
sl@0
  9000
	return KErrNone;
sl@0
  9001
	}
sl@0
  9002
	
sl@0
  9003
sl@0
  9004
sl@0
  9005
//
sl@0
  9006
// SetUpMMC
sl@0
  9007
//
sl@0
  9008
// Sets up the MMC to be used by the test by creating files on the MMC
sl@0
  9009
//
sl@0
  9010
void SetUpMMC()
sl@0
  9011
	{
sl@0
  9012
	FormatMMC();
sl@0
  9013
sl@0
  9014
	TInt r = 0;
sl@0
  9015
sl@0
  9016
	TChar driveToTest;	
sl@0
  9017
	
sl@0
  9018
	TVolumeInfo volInfo;
sl@0
  9019
	
sl@0
  9020
	r = gTheFs.DriveToChar(gDrive, driveToTest);
sl@0
  9021
	test_KErrNone(r);
sl@0
  9022
					
sl@0
  9023
	r = gTheFs.CharToDrive(driveToTest,gDrive);
sl@0
  9024
	test_KErrNone(r);
sl@0
  9025
sl@0
  9026
	gSessionPath = _L("?:\\F32-TST\\");
sl@0
  9027
	gSessionPath[0] = (TUint16) driveToTest;
sl@0
  9028
	test.Printf(_L("Drive Letter=%C\n"),(TInt)driveToTest);
sl@0
  9029
sl@0
  9030
	TDriveInfo info;
sl@0
  9031
	r = gTheFs.Drive(info,gDrive);
sl@0
  9032
	test_KErrNone(r);
sl@0
  9033
	r = gTheFs.SetSessionPath(gSessionPath);
sl@0
  9034
	test_KErrNone(r);
sl@0
  9035
	r = gTheFs.MkDirAll(gSessionPath);
sl@0
  9036
	if (r != KErrNone && r != KErrAlreadyExists)
sl@0
  9037
		{
sl@0
  9038
		test_KErrNone(r);
sl@0
  9039
		}
sl@0
  9040
	
sl@0
  9041
	r = gTheFs.Volume(volInfo, gDrive);
sl@0
  9042
	test_KErrNone(r);
sl@0
  9043
	TInt64 gMediaSize = volInfo.iSize;
sl@0
  9044
sl@0
  9045
	// This calculation is approximate because the client cannot know
sl@0
  9046
	// internal allocation mechanisms of the filesystem, i.e. how much
sl@0
  9047
	// metadata is associated with a file of name X / size Y, whether
sl@0
  9048
	// space used by such metadata is reflected in TDriveInfo::iSize and
sl@0
  9049
	// what block/clustersize the filesystem will round filesizes to.
sl@0
  9050
	// The last file that fills up the drive may therefore be partial
sl@0
  9051
	// (smaller than this calculation predicts).
sl@0
  9052
	TInt maxPossibleFiles = gFilesNeededToFillCache;
sl@0
  9053
	test.Printf(_L("Original files needed = %d\n"), maxPossibleFiles);
sl@0
  9054
	if(gMediaSize < (KDefaultCacheSize * maxPossibleFiles))
sl@0
  9055
		{
sl@0
  9056
		maxPossibleFiles = (gMediaSize - 10) / KDefaultCacheSize;
sl@0
  9057
		test.Printf(_L("Disk size is smaller - files needed = %d\n"), maxPossibleFiles);
sl@0
  9058
		}
sl@0
  9059
	gFilesNeededToFillCache = maxPossibleFiles;
sl@0
  9060
	CreateFiles(gFilesNeededToFillCache, KDefaultCacheSize);
sl@0
  9061
	}
sl@0
  9062
sl@0
  9063
sl@0
  9064
//
sl@0
  9065
// TestFileCaching
sl@0
  9066
//
sl@0
  9067
//---------------------------------------------------------------------------------------------------------------------
sl@0
  9068
//! @SYMTestCaseID				KBASE-t_ramdefrag-0599
sl@0
  9069
//! @SYMTestType				CIT
sl@0
  9070
//! @SYMTestCaseDesc			Verifying that when File System Caching allocates dicardable pages, 
sl@0
  9071
//! 							Defrag and allocation of fixed pages happens correctly. 
sl@0
  9072
//! @SYMPREQ					PREQ308
sl@0
  9073
//! @SYMTestPriority			High
sl@0
  9074
//! @SYMTestActions				
sl@0
  9075
//! 	1.	Fill the file system cache to allocate discardable pages
sl@0
  9076
//! 		following this call EmptyRamZone() in a zone with discardable pages. 
sl@0
  9077
//! 	2.	Fill the file system cache to allocate discardable pages
sl@0
  9078
//! 		folling this allocate discontiguous fixed pages to a zone
sl@0
  9079
//! 	3.	Fill the file system cache to allocate discardable pages
sl@0
  9080
//! 	.	following this allocate discontuguous fixed pages
sl@0
  9081
//! 	4.	Fill the file system cache to allocate discardable pages
sl@0
  9082
//! 		following this allocate less than 16 contiguous fixed pages
sl@0
  9083
//! 	5.	Fill the file system cache to allocate discardable pages
sl@0
  9084
//! 		following this allocate more than 16 contiguous fixed pages
sl@0
  9085
//!
sl@0
  9086
//! @SYMTestExpectedResults
sl@0
  9087
//! 	1.	Discardable pages are removed
sl@0
  9088
//! 	2.	KErrNone
sl@0
  9089
//! 	3.	KErrNone
sl@0
  9090
//! 	4.	KErrNone and numDiscardablePages != 0
sl@0
  9091
//! 	5.	KErrNone and numDiscardablePages = 0
sl@0
  9092
//---------------------------------------------------------------------------------------------------------------------
sl@0
  9093
TInt TestFileCaching()
sl@0
  9094
	{
sl@0
  9095
	const TUint KDisPagesReq = 1;
sl@0
  9096
	TInt r = KErrNone;
sl@0
  9097
	TInt allocSize = 0;
sl@0
  9098
	TUint zoneID = 0;
sl@0
  9099
	TUint index = 0;
sl@0
  9100
	TUint minDiscardPages = 0;
sl@0
  9101
	if (gDrive == KNoRemovableDrive)
sl@0
  9102
		{
sl@0
  9103
		test.Start(_L("Cannot find drive to write files to - Skipping FS Caching Tests\n"));
sl@0
  9104
		test.End();
sl@0
  9105
		return 0;
sl@0
  9106
		}
sl@0
  9107
	
sl@0
  9108
	r = gTheFs.Connect();
sl@0
  9109
	test_KErrNone(r);
sl@0
  9110
sl@0
  9111
	SetUpMMC();
sl@0
  9112
	
sl@0
  9113
	RRamDefragFuncTestLdd Ldd2;
sl@0
  9114
	r = Ldd2.Open();
sl@0
  9115
	test_KErrNone(r);
sl@0
  9116
	
sl@0
  9117
	test.Start(_L("Test1: Test EmptyRamZone() clears file server cache pages"));	
sl@0
  9118
	TestStart();	
sl@0
  9119
	
sl@0
  9120
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  9121
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  9122
sl@0
  9123
	r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
sl@0
  9124
	if (r != KErrNone)
sl@0
  9125
		{
sl@0
  9126
		test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
sl@0
  9127
		goto skipFileCacheTests;
sl@0
  9128
		}
sl@0
  9129
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  9130
	
sl@0
  9131
	GetAllPageInfo();
sl@0
  9132
	while (index < gZoneCount && gZoneUtilArray[index].iAllocDiscardable < KDisPagesReq)
sl@0
  9133
		{
sl@0
  9134
		++ index;
sl@0
  9135
		}
sl@0
  9136
	
sl@0
  9137
	if (index == gZoneCount)  
sl@0
  9138
		{
sl@0
  9139
		test.Printf(_L("Cannot find zone to perform test on - Skipping test step...\n"));
sl@0
  9140
		}
sl@0
  9141
	else
sl@0
  9142
		{
sl@0
  9143
		zoneID = gZoneConfigArray[index].iZoneId;
sl@0
  9144
sl@0
  9145
		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, zoneID);
sl@0
  9146
		
sl@0
  9147
		GetAllPageInfo();
sl@0
  9148
		if (gZoneUtilArray[index].iAllocDiscardable != 0)
sl@0
  9149
			{
sl@0
  9150
			test.Printf(_L("Fail: Zone ID 0x%x has 0x%x discardable pages\n"), zoneID, gZoneUtilArray[index].iAllocDiscardable);
sl@0
  9151
			TEST_FAIL;
sl@0
  9152
			}
sl@0
  9153
		else
sl@0
  9154
			{
sl@0
  9155
			test.Printf(_L("Passed...\n"));
sl@0
  9156
			}
sl@0
  9157
		}
sl@0
  9158
	TestEnd();
sl@0
  9159
sl@0
  9160
	test.Next(_L("Test2: Filling the FS Cache and allocating fixed pages to a zone"));	
sl@0
  9161
	TestStart();
sl@0
  9162
	
sl@0
  9163
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  9164
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  9165
sl@0
  9166
	r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
sl@0
  9167
	if (r != KErrNone)
sl@0
  9168
		{
sl@0
  9169
		test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
sl@0
  9170
		goto skipFileCacheTests;
sl@0
  9171
		}
sl@0
  9172
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  9173
sl@0
  9174
	GetAllPageInfo();	
sl@0
  9175
	index = 0;
sl@0
  9176
	while (index < gZoneCount && gZoneUtilArray[index].iAllocDiscardable < KDisPagesReq)
sl@0
  9177
		{
sl@0
  9178
		++ index;
sl@0
  9179
		} 
sl@0
  9180
sl@0
  9181
	if (index == gZoneCount)  
sl@0
  9182
		{
sl@0
  9183
		test.Printf(_L("Cannot find zone to perform test on - Skipping test step...\n"));
sl@0
  9184
		}
sl@0
  9185
	else
sl@0
  9186
		{
sl@0
  9187
		zoneID = gZoneConfigArray[index].iZoneId;
sl@0
  9188
sl@0
  9189
		// Just need to attempt to allocate one more page than there is free in the zone
sl@0
  9190
		allocSize = gZoneUtilArray[index].iFreePages + 1;
sl@0
  9191
		
sl@0
  9192
		test.Printf(_L("Allocating 0x%x fixed pages to zone ID 0x%x.....\n"), allocSize, zoneID);
sl@0
  9193
		r = Ldd.ZoneAllocDiscontiguous(zoneID, allocSize);
sl@0
  9194
		test.Printf(_L("r = %d\n"), r);
sl@0
  9195
sl@0
  9196
		GetAllPageInfo();
sl@0
  9197
		if (r != KErrNone)
sl@0
  9198
			{
sl@0
  9199
			test.Printf(_L("Fail: Fixed pages have not been allocated, r = %d, expected = %d\n"), r, KErrNone);
sl@0
  9200
			TEST_FAIL;
sl@0
  9201
			}
sl@0
  9202
		else
sl@0
  9203
			{
sl@0
  9204
			test.Printf(_L("Passed...\n"));
sl@0
  9205
			}
sl@0
  9206
		TESTDEBUG(test.Printf(_L("Freeing all Fixed Pages.....\n")));
sl@0
  9207
		Ldd.FreeAllFixedPages();
sl@0
  9208
		}	
sl@0
  9209
	TestEnd();
sl@0
  9210
sl@0
  9211
sl@0
  9212
	test.Next(_L("Test3: Filling the FS Cache and allocating fixed pages"));	
sl@0
  9213
	TestStart();
sl@0
  9214
	r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
sl@0
  9215
	if (r != KErrNone)
sl@0
  9216
		{
sl@0
  9217
		test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
sl@0
  9218
		goto skipFileCacheTests;
sl@0
  9219
		}
sl@0
  9220
sl@0
  9221
	GetAllPageInfo();	
sl@0
  9222
	index = 0;
sl@0
  9223
	while (index < gZoneCount && gZoneUtilArray[index].iAllocDiscardable < KDisPagesReq)
sl@0
  9224
		{
sl@0
  9225
		++ index;
sl@0
  9226
		}
sl@0
  9227
		
sl@0
  9228
	if (index == gZoneCount)  
sl@0
  9229
		{
sl@0
  9230
		test.Printf(_L("Cannot find zone to perform test on - Skipping test step...\n"));
sl@0
  9231
		}
sl@0
  9232
	else
sl@0
  9233
		{ 
sl@0
  9234
		zoneID = gZoneConfigArray[index].iZoneId;
sl@0
  9235
sl@0
  9236
		allocSize = 14; 
sl@0
  9237
sl@0
  9238
		TESTDEBUG(test.Printf(_L("Filling the remaining free pages with fixed pages.....\n")));
sl@0
  9239
		Ldd.AllocateFixed(gTotalPageCount.iFreePages);
sl@0
  9240
		
sl@0
  9241
		test.Printf(_L("Allocating 0x%x fixed pages to zone ID 0x%x.....\n"), allocSize, zoneID);
sl@0
  9242
		r = Ldd2.AllocateFixed(allocSize);
sl@0
  9243
		
sl@0
  9244
		TESTDEBUG(test.Printf(_L("r = %d\n"), r));
sl@0
  9245
		
sl@0
  9246
		GetAllPageInfo();
sl@0
  9247
		if (r != KErrNone)
sl@0
  9248
			{
sl@0
  9249
			test.Printf(_L("Fail: Fixed pages have not been allocated, r = %d, expected = %d\n"), r, KErrNone);
sl@0
  9250
			CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  9251
			CLEANUP(Ldd2.FreeAllFixedPages());
sl@0
  9252
			TEST_FAIL;
sl@0
  9253
			}
sl@0
  9254
		else
sl@0
  9255
			{
sl@0
  9256
			test.Printf(_L("Passed...\n"));
sl@0
  9257
			}
sl@0
  9258
		TESTDEBUG(test.Printf(_L("Freeing all Fixed Pages.....\n")));
sl@0
  9259
		Ldd2.FreeAllFixedPages();
sl@0
  9260
		}
sl@0
  9261
	TestEnd();
sl@0
  9262
sl@0
  9263
	test.Next(_L("Test4: Filling the FS Cache and allocating less than 16 contiguous fixed pages"));	
sl@0
  9264
	TestStart();
sl@0
  9265
	
sl@0
  9266
	r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);	
sl@0
  9267
	if (r != KErrNone)
sl@0
  9268
		{
sl@0
  9269
		test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
sl@0
  9270
		goto skipFileCacheTests;
sl@0
  9271
		}
sl@0
  9272
	allocSize = 14 << gPageShift; 
sl@0
  9273
	
sl@0
  9274
	TESTDEBUG(test.Printf(_L("Filling the remaining free pages with fixed pages.....\n")));
sl@0
  9275
	GetAllPageInfo();
sl@0
  9276
	// Allocate the fixed array before getting any page counts
sl@0
  9277
	for (TUint index = 0; index < gZoneCount; index++)
sl@0
  9278
		{
sl@0
  9279
		GetAllPageInfo();
sl@0
  9280
		if (gZoneUtilArray[index].iFreePages)
sl@0
  9281
			{
sl@0
  9282
			r = Ldd.ZoneAllocToManyArray(index, gZoneUtilArray[index].iFreePages);
sl@0
  9283
			if (r != KErrNone)
sl@0
  9284
				{
sl@0
  9285
				test.Printf(_L("Failed to allocate fixed array Zone %d r = %d - Skipping...\n"), index,  r);
sl@0
  9286
				goto SkipTest4;
sl@0
  9287
				}
sl@0
  9288
			}
sl@0
  9289
		}
sl@0
  9290
sl@0
  9291
	// Now fill all zones with fixed pages, 1 zone at a time
sl@0
  9292
	// to avoid the discardable pages being disturbed
sl@0
  9293
	for (TUint index = 0; index < gZoneCount; index++)
sl@0
  9294
		{
sl@0
  9295
		GetAllPageInfo();
sl@0
  9296
		if (gZoneUtilArray[index].iFreePages)
sl@0
  9297
			{
sl@0
  9298
			r = Ldd.ZoneAllocToMany2(index, gZoneUtilArray[index].iFreePages);
sl@0
  9299
			if (r != KErrNone)
sl@0
  9300
				{
sl@0
  9301
				test.Printf(_L("Failed to allocate %d fixed to Zone %d r = %d - Skipping...\n"), 
sl@0
  9302
													gZoneUtilArray[index].iFreePages, index,  r);
sl@0
  9303
				goto SkipTest4;
sl@0
  9304
				}
sl@0
  9305
			}
sl@0
  9306
		}
sl@0
  9307
	
sl@0
  9308
	GetAllPageInfo();
sl@0
  9309
	test.Printf(_L("number of free pages = 0x%x\n"), gTotalPageCount.iFreePages);
sl@0
  9310
	if (gTotalPageCount.iFreePages || 
sl@0
  9311
		gTotalPageCount.iDiscardablePages <= (TUint)(allocSize >> gPageShift))
sl@0
  9312
		{
sl@0
  9313
		test.Printf(_L("Setup failed - Skipping...\n"));
sl@0
  9314
		goto SkipTest4;
sl@0
  9315
		}
sl@0
  9316
sl@0
  9317
	test.Printf(_L("Allocating 0x%x fixed pages.....\n"), allocSize >> gPageShift);
sl@0
  9318
	r = Ldd2.AllocContiguous(allocSize);
sl@0
  9319
	TESTDEBUG(test.Printf(_L("r = %d\n"), r));
sl@0
  9320
	
sl@0
  9321
	GetAllPageInfo();
sl@0
  9322
	if (r != KErrNone || !gTotalPageCount.iDiscardablePages)
sl@0
  9323
		{
sl@0
  9324
		test.Printf(_L("Fail: Fixed pages have not been allocated, r = %d, expected = %d\n"), r, KErrNone);
sl@0
  9325
		CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  9326
		CLEANUP(Ldd2.FreeAllFixedPages());
sl@0
  9327
		TEST_FAIL;
sl@0
  9328
		}
sl@0
  9329
	else
sl@0
  9330
		{
sl@0
  9331
		test.Printf(_L("Passed...\n"));
sl@0
  9332
		}
sl@0
  9333
sl@0
  9334
SkipTest4:
sl@0
  9335
	TESTDEBUG(test.Printf(_L("Freeing all Fixed Pages.....\n")));
sl@0
  9336
	Ldd2.FreeAllFixedPages();
sl@0
  9337
	TestEnd();
sl@0
  9338
sl@0
  9339
	test.Next(_L("Test5: Filling the FS Cache and allocating more than 16 contiguous fixed pages"));	
sl@0
  9340
	TestStart();
sl@0
  9341
	// TestEnd() will have reduced any cache pages to minimum so just get current 
sl@0
  9342
	// count of discardable pages.
sl@0
  9343
	GetAllPageInfo();
sl@0
  9344
	minDiscardPages = gTotalPageCount.iDiscardablePages;
sl@0
  9345
	
sl@0
  9346
	r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
sl@0
  9347
	if (r != KErrNone)
sl@0
  9348
		{
sl@0
  9349
		test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
sl@0
  9350
		goto skipFileCacheTests;
sl@0
  9351
		}
sl@0
  9352
	
sl@0
  9353
	allocSize = 18 << gPageShift; 
sl@0
  9354
sl@0
  9355
	TESTDEBUG(test.Printf(_L("Filling the remaining free pages with fixed pages.....\n")));
sl@0
  9356
	GetAllPageInfo();
sl@0
  9357
	
sl@0
  9358
	// Allocate the fixed array before getting any page counts
sl@0
  9359
	for (TUint index = 0; index < gZoneCount; index++)
sl@0
  9360
		{
sl@0
  9361
		GetAllPageInfo();
sl@0
  9362
		if (gZoneUtilArray[index].iFreePages)
sl@0
  9363
			{
sl@0
  9364
			r = Ldd.ZoneAllocToManyArray(index, gZoneUtilArray[index].iFreePages);
sl@0
  9365
			if (r != KErrNone)
sl@0
  9366
				{
sl@0
  9367
				test.Printf(_L("Failed to allocate fixed array Zone %d r = %d - Skipping...\n"), index,  r);
sl@0
  9368
				goto SkipTest5;
sl@0
  9369
				}
sl@0
  9370
			}
sl@0
  9371
		}
sl@0
  9372
sl@0
  9373
	// Now fill all zones with fixed pages, 1 zone at a time
sl@0
  9374
	// to avoid the discardable pages being disturbed
sl@0
  9375
	for (TUint index = 0; index < gZoneCount; index++)
sl@0
  9376
		{
sl@0
  9377
		GetAllPageInfo();
sl@0
  9378
		if (gZoneUtilArray[index].iFreePages)
sl@0
  9379
			{
sl@0
  9380
			r = Ldd.ZoneAllocToMany2(index, gZoneUtilArray[index].iFreePages);
sl@0
  9381
			if (r != KErrNone)
sl@0
  9382
				{
sl@0
  9383
				test.Printf(_L("Failed to allocate %d fixed to Zone %d r = %d - Skipping...\n"), 
sl@0
  9384
													gZoneUtilArray[index].iFreePages, index,  r);
sl@0
  9385
				goto SkipTest5;
sl@0
  9386
				}
sl@0
  9387
			}
sl@0
  9388
		}
sl@0
  9389
	
sl@0
  9390
	GetAllPageInfo();
sl@0
  9391
	test.Printf(_L("number of free pages = 0x%x\n"), gTotalPageCount.iFreePages);
sl@0
  9392
	if (gTotalPageCount.iFreePages)
sl@0
  9393
		{
sl@0
  9394
		test.Printf(_L("Setup failed - Skipping...\n"));
sl@0
  9395
		goto SkipTest5;
sl@0
  9396
		}
sl@0
  9397
	
sl@0
  9398
	test.Printf(_L("Allocating 0x%x fixed pages.....\n"), allocSize >> gPageShift);
sl@0
  9399
	r = Ldd2.AllocContiguous(allocSize);
sl@0
  9400
	TESTDEBUG(test.Printf(_L("r = %d\n"), r));
sl@0
  9401
sl@0
  9402
	GetAllPageInfo();
sl@0
  9403
	if (r != KErrNone || gTotalPageCount.iDiscardablePages != minDiscardPages)
sl@0
  9404
		{
sl@0
  9405
		test.Printf(_L("Fail: r = %d, expected = %d - Discardable Pages = 0x%x, expected = %d\n"), 
sl@0
  9406
								r, KErrNone, gTotalPageCount.iDiscardablePages, minDiscardPages);
sl@0
  9407
		CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  9408
		CLEANUP(Ldd2.FreeAllFixedPages());
sl@0
  9409
		TEST_FAIL;
sl@0
  9410
		}
sl@0
  9411
	else
sl@0
  9412
		{
sl@0
  9413
		test.Printf(_L("Passed...\n"));
sl@0
  9414
		}
sl@0
  9415
SkipTest5:
sl@0
  9416
sl@0
  9417
skipFileCacheTests:
sl@0
  9418
	TestEnd();
sl@0
  9419
	TESTDEBUG(test.Printf(_L("Freeing all Fixed Pages.....\n")));
sl@0
  9420
	Ldd2.FreeAllFixedPages();
sl@0
  9421
	Ldd2.Close();
sl@0
  9422
	gTheFs.Close();
sl@0
  9423
	FormatMMC();
sl@0
  9424
	test.End();
sl@0
  9425
	return KErrNone;
sl@0
  9426
sl@0
  9427
	}
sl@0
  9428
sl@0
  9429
sl@0
  9430
//
sl@0
  9431
// TestOneZoneConfig
sl@0
  9432
//
sl@0
  9433
//---------------------------------------------------------------------------------------------------------------------
sl@0
  9434
//! @SYMTestCaseID				KBASE-t_ramdefrag-0600
sl@0
  9435
//! @SYMTestType				CIT
sl@0
  9436
//! @SYMTestCaseDesc			Verifying that when only 1 zone is cofigured in the variant, that
sl@0
  9437
//!								the defrag and allocation of fixed pages happend correctly
sl@0
  9438
//! @SYMPREQ					PREQ308
sl@0
  9439
//! @SYMTestPriority			High
sl@0
  9440
//! @SYMTestActions				
sl@0
  9441
//! 	1.	Fragment the memory and perform a DefragRam
sl@0
  9442
//! 	2.	Fragment the memory and perform an EmptyZone
sl@0
  9443
//! 	3.	Fragment the memory and perform a ClaimZone
sl@0
  9444
//! 	4.	Call ZoneAllocPhysicalRam to allocate discontiguous fixed pages
sl@0
  9445
//! 	5.	Call ZoneAllocPhysicalRam to allocate contiguous fixed pages
sl@0
  9446
//!
sl@0
  9447
//! @SYMTestExpectedResults
sl@0
  9448
//! 	1.	Number of pages does not differ from the original
sl@0
  9449
//! 	2.	KErrNoMemroy and discardable pages are discarded of
sl@0
  9450
//! 	3.	KErrNoMemory
sl@0
  9451
//! 	4.	KErrNone
sl@0
  9452
//! 	5.	KErrNone
sl@0
  9453
//---------------------------------------------------------------------------------------------------------------------
sl@0
  9454
TInt TestOneZoneConfig()
sl@0
  9455
	{
sl@0
  9456
	TInt r = gTheFs.Connect();
sl@0
  9457
	test_KErrNone(r);
sl@0
  9458
	
sl@0
  9459
	if (gDrive != KNoRemovableDrive)
sl@0
  9460
		{	
sl@0
  9461
		SetUpMMC();
sl@0
  9462
		}
sl@0
  9463
sl@0
  9464
	TUint index = 0;
sl@0
  9465
	GetAllPageInfo();
sl@0
  9466
	TUint zoneID = gZoneConfigArray[index].iZoneId;
sl@0
  9467
	test.Printf(_L("Zone ID = 0x%x\n"), zoneID);
sl@0
  9468
	TUint minCacheSize = 0;
sl@0
  9469
	TUint maxCacheSize = 0;
sl@0
  9470
	TUint currentCacheSize = 0;
sl@0
  9471
	
sl@0
  9472
sl@0
  9473
	TUint origFree = 0;
sl@0
  9474
	TUint origUnknown = 0;
sl@0
  9475
	TUint origFixed = 0;
sl@0
  9476
	TUint origMovable = 0;
sl@0
  9477
	TUint origDiscard = 0;
sl@0
  9478
	TUint origOther = 0;
sl@0
  9479
sl@0
  9480
	if (gPagedRom)
sl@0
  9481
		{
sl@0
  9482
		DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
sl@0
  9483
		TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
sl@0
  9484
									minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
sl@0
  9485
sl@0
  9486
		TUint setMin = 60 << gPageShift;
sl@0
  9487
		TInt setMax = -1;
sl@0
  9488
		TInt r = DPTest::SetCacheSize(setMin, setMax);
sl@0
  9489
		if (r != KErrNone)
sl@0
  9490
			{
sl@0
  9491
			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
sl@0
  9492
			CLEANUP(ResetDPCache());
sl@0
  9493
			TEST_FAIL;
sl@0
  9494
			}
sl@0
  9495
		DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
sl@0
  9496
		TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
sl@0
  9497
									minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
sl@0
  9498
		}
sl@0
  9499
sl@0
  9500
	test.Start(_L("Test1: Fragmenting the memory and performing a general defrag"));	
sl@0
  9501
	TestStart();	
sl@0
  9502
	
sl@0
  9503
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  9504
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  9505
	
sl@0
  9506
	if (gDrive != KNoRemovableDrive)
sl@0
  9507
		{
sl@0
  9508
		FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
sl@0
  9509
		}
sl@0
  9510
	else
sl@0
  9511
		{
sl@0
  9512
		test.Printf(_L("Cannot find drive to write files to - Not allocating discardable pages through FS Caching\n"));
sl@0
  9513
		}
sl@0
  9514
sl@0
  9515
	GetAllPageInfo();
sl@0
  9516
	origFree = gTotalPageCount.iFreePages;
sl@0
  9517
	origUnknown = gTotalPageCount.iUnknownPages;
sl@0
  9518
	origFixed = gTotalPageCount.iFixedPages;
sl@0
  9519
	origMovable = gTotalPageCount.iMovablePages;
sl@0
  9520
	origDiscard = gTotalPageCount.iDiscardablePages;
sl@0
  9521
	origOther = gTotalPageCount.iOtherPages;
sl@0
  9522
	
sl@0
  9523
	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  9524
	
sl@0
  9525
	GetAllPageInfo();
sl@0
  9526
sl@0
  9527
	if ((origUnknown != gTotalPageCount.iUnknownPages) ||
sl@0
  9528
		(origFixed != gTotalPageCount.iFixedPages) ||
sl@0
  9529
		(origMovable != gTotalPageCount.iMovablePages) ||
sl@0
  9530
		(origDiscard != gTotalPageCount.iDiscardablePages) ||
sl@0
  9531
		(origOther != gTotalPageCount.iOtherPages))
sl@0
  9532
		{
sl@0
  9533
		test.Printf(_L("Fail: Pages after defrag are not equal to those before"));
sl@0
  9534
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  9535
		TEST_FAIL;
sl@0
  9536
		}
sl@0
  9537
	else
sl@0
  9538
		{
sl@0
  9539
		test.Printf(_L("Passed..."));
sl@0
  9540
		}
sl@0
  9541
	// This will free any allocated memory
sl@0
  9542
	TestEnd();
sl@0
  9543
sl@0
  9544
sl@0
  9545
	test.Next(_L("Test2: Fragmenting the memory and performing an EmptyZone"));	
sl@0
  9546
	TestStart();	
sl@0
  9547
	
sl@0
  9548
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  9549
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  9550
	
sl@0
  9551
	if (gDrive != KNoRemovableDrive)
sl@0
  9552
		{
sl@0
  9553
		FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
sl@0
  9554
		}
sl@0
  9555
	else
sl@0
  9556
		{
sl@0
  9557
		test.Printf(_L("Cannot find drive to write files to - Not allocating discardable pages through FS Caching\n"));
sl@0
  9558
		}	
sl@0
  9559
	r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, zoneID);
sl@0
  9560
sl@0
  9561
	if (r != KErrNoMemory || CheckZoneIsOff(index))
sl@0
  9562
		{
sl@0
  9563
		test.Printf(_L("Fail: r = %d, expected = -4"), r);
sl@0
  9564
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  9565
		TEST_FAIL;
sl@0
  9566
		}
sl@0
  9567
	else
sl@0
  9568
		{
sl@0
  9569
		test.Printf(_L("Passed..."));
sl@0
  9570
		}
sl@0
  9571
	// This will free any allocated memory
sl@0
  9572
	TestEnd();
sl@0
  9573
sl@0
  9574
	test.Next(_L("Test3: Fragmenting the memory and performing a ClaimZone"));	
sl@0
  9575
	TestStart();	
sl@0
  9576
	
sl@0
  9577
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  9578
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  9579
sl@0
  9580
	if (gDrive != KNoRemovableDrive)
sl@0
  9581
		{
sl@0
  9582
		FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
sl@0
  9583
		}
sl@0
  9584
	else
sl@0
  9585
		{
sl@0
  9586
		test.Printf(_L("Cannot find drive to write files to - Not allocating discardable pages through FS Caching\n"));
sl@0
  9587
		}
sl@0
  9588
	
sl@0
  9589
	GetAllPageInfo();
sl@0
  9590
	origMovable = gTotalPageCount.iMovablePages;
sl@0
  9591
		
sl@0
  9592
	r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneID);
sl@0
  9593
sl@0
  9594
	GetAllPageInfo();
sl@0
  9595
	if (r != KErrNoMemory || origMovable != gTotalPageCount.iMovablePages)
sl@0
  9596
		{
sl@0
  9597
		test.Printf(_L("Fail: r = %d, expected = -4"), r);
sl@0
  9598
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  9599
		TEST_FAIL;
sl@0
  9600
		}
sl@0
  9601
	else
sl@0
  9602
		{
sl@0
  9603
		test.Printf(_L("Passed..."));
sl@0
  9604
		}
sl@0
  9605
	// This will free any allocated memory.
sl@0
  9606
	TestEnd();
sl@0
  9607
sl@0
  9608
	test.Next(_L("Test4: Calling ZoneAllocPhysicalRam to allocate discontiguous fixed pages"));	
sl@0
  9609
	TestStart();	
sl@0
  9610
	
sl@0
  9611
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  9612
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  9613
	
sl@0
  9614
	GetAllPageInfo();	
sl@0
  9615
	origFree = gTotalPageCount.iFreePages;
sl@0
  9616
	origUnknown = gTotalPageCount.iUnknownPages;
sl@0
  9617
	origFixed = gTotalPageCount.iFixedPages;
sl@0
  9618
	origMovable = gTotalPageCount.iMovablePages;
sl@0
  9619
	origDiscard = gTotalPageCount.iDiscardablePages;
sl@0
  9620
	origOther = gTotalPageCount.iOtherPages;
sl@0
  9621
	
sl@0
  9622
	r = Ldd.ZoneAllocDiscontiguous(zoneID, (TInt)(origFree / 2));
sl@0
  9623
sl@0
  9624
	GetAllPageInfo();
sl@0
  9625
	if (gTotalPageCount.iFixedPages < (origFixed + (origFree / 2)))
sl@0
  9626
		{
sl@0
  9627
		test.Printf(_L("Fail: fixed pages = 0x%x, expected >= 0x%x\n"), 
sl@0
  9628
						gTotalPageCount.iFixedPages, (origFixed + (origFree / 2)));
sl@0
  9629
		CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  9630
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  9631
		TEST_FAIL;
sl@0
  9632
		}
sl@0
  9633
	else
sl@0
  9634
		{
sl@0
  9635
		test.Printf(_L("Passed..."));
sl@0
  9636
		}
sl@0
  9637
	Ldd.FreeAllFixedPages();
sl@0
  9638
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  9639
	TestEnd();
sl@0
  9640
sl@0
  9641
sl@0
  9642
	test.Next(_L("Test5: Calling ZoneAllocPhysicalRam to allocate contiguous fixed pages"));	
sl@0
  9643
	TestStart();	
sl@0
  9644
	
sl@0
  9645
	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
sl@0
  9646
	FreeMovable(gChunkArray1, gChunkArraySize1);
sl@0
  9647
sl@0
  9648
	GetAllPageInfo();
sl@0
  9649
	origFree = gTotalPageCount.iFreePages;
sl@0
  9650
	origUnknown = gTotalPageCount.iUnknownPages;
sl@0
  9651
	origFixed = gTotalPageCount.iFixedPages;
sl@0
  9652
	origMovable = gTotalPageCount.iMovablePages;
sl@0
  9653
	origDiscard = gTotalPageCount.iDiscardablePages;
sl@0
  9654
	origOther = gTotalPageCount.iOtherPages;
sl@0
  9655
	
sl@0
  9656
	TInt allocSize = 50 << gPageShift; 
sl@0
  9657
	
sl@0
  9658
	r = Ldd.ZoneAllocContiguous(zoneID, allocSize);
sl@0
  9659
sl@0
  9660
	GetAllPageInfo();
sl@0
  9661
	if (gTotalPageCount.iFixedPages < (origFixed + (allocSize >> gPageShift)))
sl@0
  9662
		{
sl@0
  9663
		test.Printf(_L("Fail: fixed pages = 0x%x, expected >= 0x%x\n"), 
sl@0
  9664
							gTotalPageCount.iFixedPages, (origFixed + (allocSize >> gPageShift)));
sl@0
  9665
		CLEANUP(Ldd.FreeAllFixedPages());
sl@0
  9666
		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
sl@0
  9667
		TEST_FAIL;
sl@0
  9668
		}
sl@0
  9669
	else
sl@0
  9670
		{
sl@0
  9671
		test.Printf(_L("Passed..."));
sl@0
  9672
		}
sl@0
  9673
	Ldd.FreeAllFixedPages();
sl@0
  9674
	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
sl@0
  9675
	TestEnd();
sl@0
  9676
sl@0
  9677
	if (gPagedRom)
sl@0
  9678
		{
sl@0
  9679
		test_KErrNone(DPTest::FlushCache());
sl@0
  9680
		ResetDPCache();
sl@0
  9681
		}
sl@0
  9682
sl@0
  9683
	gTheFs.Close();
sl@0
  9684
	if (gDrive != KNoRemovableDrive)
sl@0
  9685
		{
sl@0
  9686
		FormatMMC();
sl@0
  9687
		}
sl@0
  9688
	
sl@0
  9689
	test.End();
sl@0
  9690
	return KErrNone;
sl@0
  9691
	}
sl@0
  9692
sl@0
  9693
sl@0
  9694
//
sl@0
  9695
// RunDefragTests
sl@0
  9696
//
sl@0
  9697
// List of defrag tests to be run 
sl@0
  9698
//
sl@0
  9699
void RunDefragTests()
sl@0
  9700
	{
sl@0
  9701
	test.Start(_L("Testing the moving of pages in a defrag"));
sl@0
  9702
	TestMovPgsDefrag();
sl@0
  9703
sl@0
  9704
	test.Next(_L("Verifying the implementation of the function TRamDefragRequest::DefragRam() arg aMaxPages"));
sl@0
  9705
	TestDefragRamMaxPages();
sl@0
  9706
sl@0
  9707
	test.Next(_L("Verifying the implementation of the function TRamDefragRequest::EmptyRamZone()"));
sl@0
  9708
	TestEmptyRamZone();
sl@0
  9709
sl@0
  9710
	test.Next(_L("Verifying the implementation of the function Epoc::GetRamZonePageCount()"));
sl@0
  9711
	TestGetRamZonePageCount();
sl@0
  9712
sl@0
  9713
	test.Next(_L("Verifying the implementation of the function TRamDefragRequest::ClaimRamZone()"));
sl@0
  9714
	TestClaimRamZone();
sl@0
  9715
sl@0
  9716
	test.Next(_L("Verifying the implementation of the function TRamDefragRequest::Cancel()"));
sl@0
  9717
	TestCancelDefrag();
sl@0
  9718
sl@0
  9719
	test.Next(_L("Verifying that pages are moved correctly"));
sl@0
  9720
	TestMovingPages();
sl@0
  9721
sl@0
  9722
	test.Next(_L("Verifying Semaphore Methods of the Defrag"));
sl@0
  9723
	TestDefragSemMethod();
sl@0
  9724
sl@0
  9725
	test.Next(_L("Verifying Dfc Methods of the Defrag"));
sl@0
  9726
	TestDefragDfcMethod();
sl@0
  9727
sl@0
  9728
	test.Next(_L("Testing priorities"));
sl@0
  9729
	TestPriorities();
sl@0
  9730
sl@0
  9731
	test.Next(_L("Testing File System Caching"));
sl@0
  9732
	if (!gPagedRom)
sl@0
  9733
		{
sl@0
  9734
		TestFileCaching();
sl@0
  9735
		}
sl@0
  9736
	else
sl@0
  9737
		{
sl@0
  9738
		test.Printf(_L("Skipping... \n"));
sl@0
  9739
		}
sl@0
  9740
	
sl@0
  9741
	test.Next(_L("Testing general RAM defrag implementation"));
sl@0
  9742
	TestGenDefrag();
sl@0
  9743
sl@0
  9744
	test.End();
sl@0
  9745
	}
sl@0
  9746
sl@0
  9747
sl@0
  9748
//
sl@0
  9749
// RunAllocTests
sl@0
  9750
//
sl@0
  9751
// List of allocating tests to be run 
sl@0
  9752
// These tests only need to be executed once
sl@0
  9753
//
sl@0
  9754
void RunAllocTests()
sl@0
  9755
	{
sl@0
  9756
	test.Start(_L("Verifying the allocating strategies"));
sl@0
  9757
	TestAllocStrategies();
sl@0
  9758
sl@0
  9759
	test.Next(_L("Verifying the contiguous overload of Epoc::ZoneAllocPhysicalRam()"));
sl@0
  9760
	TestZoneAllocContiguous();
sl@0
  9761
sl@0
  9762
	test.Next(_L("Verifying the discontiguous overload of Epoc::ZoneAllocPhysicalRam()"));
sl@0
  9763
	TestZoneAllocDiscontiguous();
sl@0
  9764
	
sl@0
  9765
	test.Next(_L("Test Free Zone"));
sl@0
  9766
	TestFreeZone();
sl@0
  9767
sl@0
  9768
	test.Next(_L("Testing zone flags"));
sl@0
  9769
	TestFlags();
sl@0
  9770
sl@0
  9771
	test.End();
sl@0
  9772
	}
sl@0
  9773
sl@0
  9774
sl@0
  9775
//
sl@0
  9776
// E32Main
sl@0
  9777
//
sl@0
  9778
// Main entry point.
sl@0
  9779
//
sl@0
  9780
TInt E32Main()
sl@0
  9781
	{
sl@0
  9782
	test.Title();
sl@0
  9783
	DeviceDriver(TEST_DRIVER_OPEN);
sl@0
  9784
	gTotalRamLost = 0;
sl@0
  9785
	
sl@0
  9786
	TInt r = TestSetup();
sl@0
  9787
	if (r != KErrNone)
sl@0
  9788
		{
sl@0
  9789
		test.Printf(_L("Test Setup failed, r = %d\n"), r);
sl@0
  9790
		TestCleanup();
sl@0
  9791
		return r;
sl@0
  9792
		}
sl@0
  9793
sl@0
  9794
	if (gZoneCount == 1)
sl@0
  9795
		{
sl@0
  9796
		GetAllPageInfo();
sl@0
  9797
sl@0
  9798
		test.Start(_L("Zone Count 1..."));
sl@0
  9799
		TestOneZoneConfig();
sl@0
  9800
		}
sl@0
  9801
	else
sl@0
  9802
		{
sl@0
  9803
		test.Start(_L("Running Alloc tests"));
sl@0
  9804
		RunAllocTests();
sl@0
  9805
sl@0
  9806
		Ldd.ResetDriver();
sl@0
  9807
		Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  9808
		
sl@0
  9809
sl@0
  9810
		test.Next(_L("Running Defrag tests"));
sl@0
  9811
		RunDefragTests();
sl@0
  9812
sl@0
  9813
		Ldd.ResetDriver();
sl@0
  9814
		Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
sl@0
  9815
		}
sl@0
  9816
	
sl@0
  9817
sl@0
  9818
	test.Printf(_L("The total number of test steps failed = %d\n"), gTestStepFailed);
sl@0
  9819
	test_Equal(KErrNone, gTestStepFailed);
sl@0
  9820
	
sl@0
  9821
	TestCleanup();
sl@0
  9822
	
sl@0
  9823
	DeviceDriver(TEST_DRIVER_CLOSE);
sl@0
  9824
	test.End();
sl@0
  9825
	test.Close();
sl@0
  9826
	
sl@0
  9827
	return 0;
sl@0
  9828
	}