Update contrib.
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\defrag\t_ramdefrag.cpp
15 // RAM Defragmentation Functional Tests
19 //#define RUN_ALL_TESTS // Uncomment to ensure that all tests are run regardless of test failures
20 //#define DEBUG_VER // Uncomment for information output from tests
24 #define __E32TEST_EXTENSION__
26 RTest test(_L("T_RAMDEFRAG"));
32 #include <e32msgqueue.h>
40 #include "t_ramdefrag.h"
42 #define READ(a) ReadByte((volatile TUint8*)(a))
46 #define TEST_FAIL {gTestStepFailed++;}
49 #define TEST_FAIL {TestCleanup(); test(EFalse);}
50 #define CLEANUP(a) {if (!gFailPrintPageInfo) \
53 gFailPrintPageInfo = ETrue; \
60 #define TEST_DRIVER_OPEN 1
61 #define TEST_DRIVER_CLOSE 0
63 #define BEST_MOVABLE 1
64 #define BEST_DISCARDABLE 2
67 #define Z_ALLOC_CONTIG 1
68 #define Z_ALLOC_DISC 2
70 LOCAL_D TUint gTestStarted = EFalse; // Used to ensure matching TestStart() and TestEnd().
71 LOCAL_D TBool gPagedRom = ETrue; // Stores whether or not is a paged ROM
72 LOCAL_D TInt gTestStepFailed = 0; // Stores the number of test steps failed
73 LOCAL_D TBool gFailPrintPageInfo = EFalse; // Set to ETrue once CLEANUP has been invoked once.
74 LOCAL_D TBool gFileCacheRun = EFalse; // Set to ETrue whe FSCaching tests have been run
76 LOCAL_D TInt gRamSize; // The total RAM size in bytes
77 LOCAL_D TInt gFreeRam; // The amount of free RAM available in bytes
78 LOCAL_D TInt gPageSize; // The number of bytes per page
79 LOCAL_D TUint gPageShift;
81 LOCAL_D TInt gRamUsed; // The amount of RAM used in bytes
83 LOCAL_D TInt gInitialRam; // The initial free RAM before a test starts
84 LOCAL_D TInt gEndRam; // The end free RAM when a test finishes
85 LOCAL_D TUint gOriginalMinCacheSize; // The original DP minSize
86 LOCAL_D TUint gOriginalMaxCacheSize; // The original DP maxSize
87 LOCAL_D TInt gTotalRamLost; // The total amount of RAM lost during the test
89 LOCAL_D TUint gZoneCount = 0; // Number of zones
90 LOCAL_D const TUint KInvalidZoneID = 0xffffffff; // Invalid value for a zone ID
91 LOCAL_D STestPageCount gTotalPageCount;
94 LOCAL_D struct SRamZoneConfig* gZoneConfigArray; // Contains the configurations of all the zones
95 LOCAL_D struct SRamZoneUtilisation* gZoneUtilArray; // Contains the utilisations of all the zones
96 LOCAL_D struct SRamZoneUtilisation* gOriginalPageCountArray; // Contains the original utilisations of the zones
97 LOCAL_D TInt* gPrefArray; // Contains the preference order of the zones
98 LOCAL_D TUint8* gOrigFlagArray; // Contains the orignal values for the zone flags
100 LOCAL_D TInt gDefragMaxPages = 0;
102 const TInt KFillAllMovable = -1;
104 LOCAL_D RChunk* gChunkArray1 = NULL; // Stores reference to all the chunks that have been created
105 LOCAL_D RChunk* gChunkArray2 = NULL; // Stores reference to all the chunks that have been created
106 LOCAL_D TUint gChunkArraySize1 = 0; // The size of the array gChunkArray
107 LOCAL_D TUint gChunkArraySize2 = 0; // The size of the array gChunkArray
108 const TUint KChunkDefaultSize = 0x300000;
109 const TUint KMaxChunks = 14;
110 LOCAL_D const TUint KNumAllocChunks = 10; // The number of chunks to be allocd for some tests.
112 LOCAL_D RRamDefragFuncTestLdd Ldd; // Main Ldd used to call into device driver
114 LOCAL_D TBuf<20> gTestThreadName =_L("TestThread");
115 LOCAL_D RThread gTestThread;
116 LOCAL_D TRequestStatus status;
118 LOCAL_D TInt gDrive; // The removable media drive
120 LOCAL_D TFileName gSessionPath;
122 const TInt KNoRemovableDrive = -1; // gDrive is set to this when no suitable drive can be found.
123 const TInt KDefaultCacheSize = (128 + 12) * 1024; // The default file system cache size
124 const TUint KNumFilesOrig = (32 * 1024 * 1024) / KDefaultCacheSize; // The number of files that are needed to fill the file system cache
125 LOCAL_D TInt gFilesNeededToFillCache = KNumFilesOrig ; // Not constant as can change depending on the size of the disk
126 LOCAL_D RFile gFile[KNumFilesOrig];
128 LOCAL_D TInt* gCandList1; // Array of zones that have the same preference and the same
129 LOCAL_D TInt* gCandList2; // amount of free pages
130 const TInt KInvalidCandIndex = -1;
135 // Gets the removable drive number
140 TInt r = theFs.Connect();
143 TInt driveLet = KNoRemovableDrive;
146 for (; i <= EDriveZ; i++)
149 r = theFs.Volume(volInfo, i);
151 {// This drive no. exists so determine if it is removable and
152 // formattable media.
153 if ((volInfo.iDrive.iDriveAtt & KDriveAttRemovable) &&
154 (volInfo.iDrive.iMediaAtt & KMediaAttFormattable))
169 // Opens or closes the device driver used
171 TInt DeviceDriver(TInt aFunctionNum)
174 switch (aFunctionNum)
176 case TEST_DRIVER_OPEN:
178 r = User::LoadLogicalDevice(KRamDefragFuncTestLddName);
179 test(r==KErrNone || r==KErrAlreadyExists);
185 case TEST_DRIVER_CLOSE:
188 r = User::FreeLogicalDevice(KRamDefragFuncTestLddName);
202 // GetOriginalPageCount
204 // Obtains the orginal types of pages in each of the zones
206 void GetOriginalPageCount()
210 TESTDEBUG(test.Printf(_L("ram defrag : Get info about the zones\n")));
211 for (index = 0; index < gZoneCount; index ++)
213 ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneUtilisation,(TAny*)index, (TAny*)&gOriginalPageCountArray[index]);
214 test(ret == KErrNone);
222 // Prints various page information to the screen
226 test.Printf(_L("\nZONE CONFIGURATIONS:\n"));
227 for (TUint index = 0; index < gZoneCount; index ++)
229 TInt ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneConfig,(TAny*)index, (TAny*)&gZoneConfigArray[index]);
230 test(ret == KErrNone);
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"),
232 gZoneConfigArray[index].iZoneId,gZoneConfigArray[index].iZoneIndex,
233 gZoneConfigArray[index].iPhysBase,gZoneConfigArray[index].iPhysEnd,
234 gZoneConfigArray[index].iPhysPages,gZoneConfigArray[index].iPref,gZoneConfigArray[index].iFlags);
236 test.Printf(_L("\nZONE UTILISATIONS:\n"));
237 for (TUint index = 0; index < gZoneCount; index ++)
239 TInt ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneUtilisation,(TAny*)index, (TAny*)&gZoneUtilArray[index]);
240 test(ret == KErrNone);
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"),
242 gZoneUtilArray[index].iZoneId,gZoneUtilArray[index].iZoneIndex,gZoneConfigArray[index].iPref,
243 gZoneUtilArray[index].iPhysPages,gZoneUtilArray[index].iFreePages,
244 gZoneUtilArray[index].iAllocUnknown,gZoneUtilArray[index].iAllocFixed,gZoneUtilArray[index].iAllocMovable,
245 gZoneUtilArray[index].iAllocDiscardable,gZoneUtilArray[index].iAllocOther);
253 // Get various different page information for all zones
254 // Also updates the total page count
256 void GetAllPageInfo()
259 gTotalPageCount.iFreePages = 0;
260 gTotalPageCount.iUnknownPages = 0;
261 gTotalPageCount.iFixedPages = 0;
262 gTotalPageCount.iMovablePages = 0;
263 gTotalPageCount.iDiscardablePages = 0;
264 gTotalPageCount.iOtherPages = 0;
266 // now get the config of each of the zones.
268 TESTDEBUG(test.Printf(_L("ram defrag : Get info about the zones\n")));
269 for (index = 0; index < gZoneCount; index ++)
271 ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneConfig,(TAny*)index, (TAny*)&gZoneConfigArray[index]);
272 test(ret == KErrNone);
274 ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneUtilisation,(TAny*)index, (TAny*)&gZoneUtilArray[index]);
275 test(ret == KErrNone);
277 gTotalPageCount.iFreePages += gZoneUtilArray[index].iFreePages;
278 gTotalPageCount.iUnknownPages += gZoneUtilArray[index].iAllocUnknown;
279 gTotalPageCount.iFixedPages += gZoneUtilArray[index].iAllocFixed;
280 gTotalPageCount.iMovablePages += gZoneUtilArray[index].iAllocMovable;
281 gTotalPageCount.iDiscardablePages += gZoneUtilArray[index].iAllocDiscardable;
282 gTotalPageCount.iOtherPages += gZoneUtilArray[index].iAllocOther;
284 TESTDEBUG(test.Printf(_L("free=0x%x unknown=0x%x fixed=0x%x move=0x%x discard=0x%x other=0x%x\n"),
285 gTotalPageCount.iFreePages, gTotalPageCount.iUnknownPages, gTotalPageCount.iFixedPages,
286 gTotalPageCount.iMovablePages, gTotalPageCount.iDiscardablePages,gTotalPageCount.iOtherPages));
288 TESTDEBUG(PrintPageInfo());
291 void RestoreRamZoneFlags()
293 GetAllPageInfo(); // Update the current set of RAM zone flag data.
294 for (TUint index=0; index < gZoneCount; index++)
296 TUint zoneDefragID = gZoneConfigArray[index].iZoneId;
297 Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ORIG_FLAG, gOrigFlagArray[index]);
301 void ResetRamZoneFlags()
303 GetAllPageInfo(); // Update the current set of RAM zone flag data.
304 for (TUint index=0; index < gZoneCount; index++)
306 TUint zoneDefragID = gZoneConfigArray[index].iZoneId;
307 Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, RESET_FLAG);
311 void RemoveChunkAlloc(RChunk*& aChunkArray, TUint& aChunkArraySize);
316 // If the File System Caching tests have been run,
317 // ensure that they are cleaned up
321 // First close all the open handles to the RFile objects open
322 for (i = 0; i < KNumFilesOrig; i++)
327 // Now call EmptyRamZone on every zone to ensure that
328 // discardable pages are cleaned up
331 for (i = 0; i < gZoneCount; i++)
333 TUint zoneID = gZoneConfigArray[i].iZoneId;
334 Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, zoneID);
336 gFileCacheRun = EFalse;
343 // Cleans up all the allocations made at the beginning of the test
350 // Revert the cleared flags to their original values before the tests were carried out
351 RestoreRamZoneFlags();
353 // Reset the DP cache and remove any allocated chunks and fixed pages.
354 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
355 RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
357 Ldd.FreeAllFixedPages();
358 Ldd.FreeFromAllZones();
361 User::Free(gPrefArray);
364 User::Free(gOrigFlagArray);
365 gOrigFlagArray = NULL;
367 User::Free(gCandList1);
370 User::Free(gCandList2);
373 User::Free(gOriginalPageCountArray);
374 gOriginalPageCountArray = NULL;
376 // Output the last possible state of memory
377 if (!gFailPrintPageInfo)
380 User::Free(gZoneConfigArray);
381 gZoneConfigArray = NULL;
383 User::Free(gZoneUtilArray);
384 gZoneUtilArray = NULL;
391 // Get the necessary information needed to carry out the tests
398 // first get the number of zones
399 TInt ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneCount,&gZoneCount,0);
402 test.Printf(_L("Cannot obtain the number of zones\n"));
405 test.Printf(_L("RAM Zones (count=%u)\n"),gZoneCount);
407 // Obtain the size of the RAM and the size of a page
408 ret = HAL::Get(HAL::EMemoryRAM, gRamSize);
411 test.Printf(_L("Cannot obtain the size of RAM\n"));
415 // Retrieve the page size and use it to detemine the page shift (assumes 32-bit system).
416 ret = HAL::Get(HAL::EMemoryPageSize, gPageSize);
419 test.Printf(_L("Cannot obtain the page size\n"));
423 TUint32 pageMask = gPageSize;
431 test.Printf(_L("ERROR - page size not a power of 2"));
432 return KErrNotSupported;
440 gZoneConfigArray = (SRamZoneConfig *)User::AllocZ(sizeof(SRamZoneConfig) * gZoneCount);
441 if (gZoneConfigArray == NULL)
443 gZoneUtilArray = (SRamZoneUtilisation *)User::AllocZ(sizeof(SRamZoneUtilisation) * gZoneCount);
444 if (gZoneUtilArray == NULL)
446 gPrefArray = (TInt *)User::AllocZ(sizeof(TInt) * gZoneCount);
447 if (gPrefArray == NULL)
449 gOrigFlagArray = (TUint8 *)User::AllocZ(sizeof(TUint8) * gZoneCount);
450 if (gOrigFlagArray == NULL)
452 gOriginalPageCountArray = (SRamZoneUtilisation *)User::AllocZ(sizeof(SRamZoneUtilisation) * gZoneCount);
453 if (gOriginalPageCountArray == NULL)
456 gCandList1 = (TInt *)User::AllocZ(sizeof(TInt) * gZoneCount);
457 if (gCandList1 == NULL)
459 gCandList2 = (TInt *)User::AllocZ(sizeof(TInt) * gZoneCount);
460 if (gCandList2 == NULL)
466 // Store the original flags
467 for (i=0; i < gZoneCount; i++)
468 gOrigFlagArray[i] = gZoneConfigArray[i].iFlags;
470 // Now clear the flags for carrying out tests
475 // Check whether the ROM is paged or not
476 TRomHeader* romHeader = (TRomHeader*)UserSvr::RomHeaderAddress();
477 if(!romHeader->iPageableRomStart)
479 test.Printf(_L("Not a Paged ROM - Skipping all discardable page tests.\n"));
483 {// Save the current state of the DP cache so it can be restored when required and
484 // after the test has finished.
485 TUint currentCacheSize;
486 DPTest::CacheSize(gOriginalMinCacheSize, gOriginalMaxCacheSize, currentCacheSize);
487 TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"),
488 gOriginalMinCacheSize >> gPageShift, gOriginalMaxCacheSize >> gPageShift,
489 currentCacheSize >> gPageShift));
499 // Updating the various RAM information
503 HAL::Get(HAL::EMemoryRAMFree, gFreeRam);
504 TESTDEBUG(gRamUsed = gRamSize - gFreeRam);
509 // CheckRamDifference
511 // Checks the difference between the initial free RAM and the end free RAM
513 void CheckRamDifference()
515 if (gInitialRam == gEndRam)
517 TESTDEBUG(test.Printf(_L("No RAM was lost during this test\n")));
521 TInt diff = gInitialRam - gEndRam;
524 gTotalRamLost = gTotalRamLost + diff;
528 TInt VerifyMovDisAlloc();
532 // Updates the RAM information at the beginning of a test step
537 gTestStarted = ETrue;
541 Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
542 if (VerifyMovDisAlloc() != KErrNone)
549 gInitialRam = gFreeRam;
550 TESTDEBUG(test.Printf(_L("Initial Free RAM = 0x%x, Initial RAM Used = 0x%x\n"), gFreeRam, gRamUsed));
554 void RemoveChunkAlloc(RChunk*& aChunkArray, TUint& aChunkArraySize);
558 // Updates RAM information at end of test step and checks the RAM delta
563 gTestStarted = EFalse;
567 // Clean up anything that may need to be cleaned.
569 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
570 RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
572 Ldd.FreeAllFixedPages();
577 TESTDEBUG(test.Printf(_L("End RAM Free = 0x%x, End RAM Used = 0x%x\n"), gEndRam, gRamUsed));
578 CheckRamDifference();
579 test.Printf(_L(" \n"));
583 // CheckZonesSwitchedOff
585 // Checks that zones have been switched off
587 TBool CheckZonesSwitchedOff()
590 for (TUint i = 0; i < gZoneCount; i++)
592 if (gOriginalPageCountArray[i].iFreePages != gOriginalPageCountArray[i].iPhysPages &&
593 gZoneUtilArray[i].iFreePages == gZoneUtilArray[i].iPhysPages)
605 // Checks if a particular zone is empty
607 TBool CheckZoneIsOff(TUint aZoneIndex)
610 if (gZoneUtilArray[aZoneIndex].iFreePages == gZoneUtilArray[aZoneIndex].iPhysPages)
612 TESTDEBUG(test.Printf(_L("Zone index %d is Empty\n"), aZoneIndex));
617 TESTDEBUG(test.Printf(_L("Zone index %d is Not empty\n"), aZoneIndex));
626 // Go through each zone ordering them in preference order
631 TESTDEBUG(PrintPageInfo());
633 for (TUint i=0; i < (TUint)gZoneCount; i++)
634 gPrefArray[i] = KErrNotFound;
636 for (TUint curIndex = 0; curIndex < gZoneCount; curIndex++)
638 TBool currentEmpty = gZoneUtilArray[curIndex].iPhysPages == gZoneUtilArray[curIndex].iFreePages;
639 TUint currentPref = gZoneConfigArray[curIndex].iPref;
640 TUint currentImmovPages = gZoneUtilArray[curIndex].iAllocFixed +
641 gZoneUtilArray[curIndex].iAllocUnknown;
642 TUint morePrefCnt = 0;
643 for (TUint index = 0; index < gZoneCount; index++)
644 {// A RAM zone with the same iPref is more preferable if it has
645 // more immovable pages.
646 if (gZoneConfigArray[index].iPref < currentPref ||
647 (gZoneConfigArray[index].iPref == currentPref &&
648 (currentImmovPages < gZoneUtilArray[index].iAllocFixed + gZoneUtilArray[index].iAllocUnknown ||
650 gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages))))
656 while (gPrefArray[morePrefCnt] != KErrNotFound)
657 {// Zone(s) of this preference and size already exist so
658 // place this one after it/them
661 gPrefArray[morePrefCnt] = curIndex;
668 // Return ETrue if the RAM zones are of the same preference.
670 // NOTE - This requires GetAllPageInfo() to have already been called.
672 TBool ZonesSamePref(TUint aZoneIndex1, TUint aZoneIndex2)
674 TBool zoneEmpty1 = gZoneUtilArray[aZoneIndex1].iFreePages == gZoneUtilArray[aZoneIndex1].iPhysPages;
675 TBool zoneEmpty2 = gZoneUtilArray[aZoneIndex2].iFreePages == gZoneUtilArray[aZoneIndex2].iPhysPages;
676 if (gZoneConfigArray[aZoneIndex1].iPref == gZoneConfigArray[aZoneIndex2].iPref &&
677 (gZoneUtilArray[aZoneIndex1].iAllocFixed + gZoneUtilArray[aZoneIndex1].iAllocUnknown ==
678 gZoneUtilArray[aZoneIndex2].iAllocFixed + gZoneUtilArray[aZoneIndex2].iAllocUnknown &&
679 (zoneEmpty1 == zoneEmpty2)))
690 // Checks all zones and returns the most preferable RAM zone which
691 // is completely emtpy
693 // @param aZoneIndex On return this will contain the index into gZoneUtilArray of the most preferable empty RAM zone.
694 // @param aPrefIndex On return this will contain the index into gPrefArray of the most preferable empty RAM zone.
696 // @return KErrNotFound if a zone cannot be found, else KErrNone
698 TInt FindMostPrefEmpty(TUint& aZoneIndex, TUint* aPrefIndex = NULL)
700 // Get the most pref zone which is completely free to use as a test zone
703 for (; prefIndex < gZoneCount; prefIndex++)
705 TUint zoneIndex = gPrefArray[prefIndex];
706 if (gZoneUtilArray[zoneIndex].iFreePages == gZoneUtilArray[zoneIndex].iPhysPages)
708 aZoneIndex = zoneIndex;
711 *aPrefIndex = prefIndex;
720 // FindLeastPrefEmpty
722 // Checks all zones and returns the least preferable RAM zone which
723 // is completely emtpy
725 // @param aZoneIndex On return this will contain the index into gZoneUtilArray of the least preferable empty RAM zone.
726 // @param aPrefIndex On return this will contain the index into gPrefArray of the least preferable empty RAM zone.
728 // @return KErrNotFound if a zone cannot be found, else KErrNone
730 TInt FindLeastPrefEmpty(TUint& aZoneIndex, TUint* aPrefIndex = NULL)
732 // Get the most pref zone which is completely free to use as a test zone
734 TInt prefIndex = gZoneCount - 1;
735 for (; prefIndex >= 0; prefIndex--)
737 TUint zoneIndex = gPrefArray[prefIndex];
738 if (gZoneUtilArray[zoneIndex].iFreePages == gZoneUtilArray[zoneIndex].iPhysPages)
740 aZoneIndex = zoneIndex;
743 *aPrefIndex = (TUint)prefIndex;
752 // FindMostPrefWithFree
754 // Checks all zones and returns the most preferable RAM zone which
755 // has at least 1 free page
757 // @param aZoneIndex On return this will contain the index into gZoneUtilArray of the most preferable RAM zone with free pages.
758 // @param aPrefIndex On return this will contain the index into gPrefArray of the most preferable RAM zone with free pages.
760 // @return KErrNotFound if a zone cannot be found, else KErrNone
762 TInt FindMostPrefWithFree(TUint& aZoneIndex, TUint* aPrefIndex = NULL)
764 // Get the most pref zone which has free pages
767 for (; prefIndex < gZoneCount; prefIndex++)
769 TUint zoneIndex = gPrefArray[prefIndex];
770 if (gZoneUtilArray[zoneIndex].iFreePages)
772 aZoneIndex = zoneIndex;
775 *aPrefIndex = prefIndex;
785 // Check whether a call to TRamDefragRequest::DefragRam() would be able to
788 TBool CanGenSucceed()
791 TBool genSucceed = EFalse;
792 // Work out if general has anything to do
793 for(TInt prefIndex = (TInt)gZoneCount-1; prefIndex >= 0; prefIndex--)
795 TUint zoneIndex = gPrefArray[prefIndex];
796 TESTDEBUG(test.Printf(_L("prefIndex = %d zoneIndex = 0x%x\n"), prefIndex, zoneIndex));
798 TUint samePrefCount = 1;
799 TUint samePrefSucceed = 0;
800 // The number of zones of this preference that will be skipped by the general defrag
801 TUint samePrefEmptyImmovable = 0;
803 // Determine how many zones have the same preference as this one
804 TInt prevPrefIndex = (prefIndex != 0)? (prefIndex - 1) : -1;
805 for (; prevPrefIndex >= 0; prevPrefIndex--)
807 TUint prevIndex = gPrefArray[prevPrefIndex];
808 if (ZonesSamePref(zoneIndex, prevIndex))
812 else // no more zones with this preference
815 TESTDEBUG(test.Printf(_L("samePrefCount = %d\n"), samePrefCount));
817 for (TInt l = prefIndex - (samePrefCount-1); l <= prefIndex; l++)
819 TUint curPrefIndex = gPrefArray[l];
820 TESTDEBUG(test.Printf(_L("curPrefIndex = %d\n"), curPrefIndex));
821 if (gZoneUtilArray[curPrefIndex].iFreePages != gZoneConfigArray[curPrefIndex].iPhysPages)
823 TBool clearMovable = EFalse;
824 TBool clearDiscardable = EFalse;
826 if (gZoneUtilArray[curPrefIndex].iAllocUnknown || gZoneUtilArray[curPrefIndex].iAllocFixed)
828 TESTDEBUG(test.Printf(_L("unknown or fixed\n")));
829 samePrefEmptyImmovable++;
832 if (gZoneUtilArray[curPrefIndex].iAllocMovable)
833 {// determine if movable can potentially be cleared from this zone
834 TUint freeInLower = 0;
835 for (TInt j=0; j <= prefIndex; j++)
837 TUint idx = gPrefArray[j];
838 if (idx == curPrefIndex)
840 freeInLower += gZoneUtilArray[idx].iFreePages;
842 if (gZoneUtilArray[curPrefIndex].iAllocMovable <= freeInLower)
844 clearMovable = ETrue;
845 TESTDEBUG(test.Printf(_L("Can clear movable, curPrefIndex = %d\n"), curPrefIndex));
850 TESTDEBUG(test.Printf(_L("Can clear movable, curPrefIndex = %d\n"), curPrefIndex));
851 clearMovable = ETrue;
853 if (gZoneUtilArray[curPrefIndex].iAllocDiscardable)
857 TUint minCacheSize = 0;
858 TUint maxCacheSize = 0;
859 TUint currentCacheSize = 0;
861 DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
862 TUint spareCache = currentCacheSize - minCacheSize;
863 if (spareCache >= gZoneUtilArray[curPrefIndex].iAllocDiscardable )
865 clearDiscardable = ETrue;
866 TESTDEBUG(test.Printf(_L("Paged: Can clear discardable\n")));
869 {// determine space for discardable in more preferable zones
870 TUint freeInLower = 0;
871 for (TInt j=0; j <= prefIndex; j++)
873 TUint idx = gPrefArray[j];
874 if (idx == curPrefIndex)
876 freeInLower += gZoneUtilArray[idx].iFreePages;
878 if (gZoneUtilArray[curPrefIndex].iAllocDiscardable - spareCache <= freeInLower)
880 clearDiscardable = ETrue;
881 TESTDEBUG(test.Printf(_L("Paged: Can clear discardable curPrefIndex = %d\n"), curPrefIndex));
886 {//Should always be OK to discard as no min cache size on non-paged ROMS
887 clearDiscardable = ETrue;
888 test.Printf(_L("Can clear discardable curPrefIndex = %d\n"), curPrefIndex);
893 clearDiscardable = ETrue;
896 if (clearDiscardable && clearMovable)
899 TESTDEBUG(test.Printf(_L("General should succeed ID=%x\n"), gZoneConfigArray[curPrefIndex].iZoneId));
903 {//zone already empty
904 samePrefEmptyImmovable++;
907 if (samePrefSucceed == 0 && samePrefEmptyImmovable == 0)
908 {// no zones can be defragged and none are already empty/have immovable.
911 if (samePrefEmptyImmovable != samePrefCount)
912 {// Have reached some zones with allocated pages in them.
913 if (samePrefSucceed + samePrefEmptyImmovable == samePrefCount)
914 {// general should definitely succeed as each of the zones of this preference
915 // can be emptied or are already empty/have immovable pages allocated.
916 TESTDEBUG(test.Printf(_L("General should succeed \n")));
921 prefIndex -= samePrefCount - 1;
930 // Read a particular byte
932 TUint8 ReadByte(volatile TUint8* aPtr)
941 // Allocate Discardable pages in the form of demand paged pages
943 // @param aNumDiscardableBytes On return this will contain the number of discardable bytes above the min cache size.
944 // @param aMaxBytes The new limit for maximum number of bytes in the DP cache, set to KMaxTUInt64 to fill RAM.
945 // @param aMinOffsetBytes When not set to KMaxTUint64, this sets the min cache size to be the max cache size - aMinOffsetBytes.
947 TInt AllocDiscardable(TInt& aNumDiscardableBytes, TUint64 aMaxBytes = KMaxTUint64, TUint64 aMinOffsetBytes = KMaxTUint64)
949 TUint minCacheSize = 0;
950 TUint maxCacheSize = 0;
951 TUint currentCacheSize = 0;
952 DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
953 TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"),
954 minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
956 TESTDEBUG(test.Printf(_L("SetCacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x\n"), minCacheSize >> gPageShift, aMaxBytes >> gPageShift));
958 if (aMaxBytes == KMaxTUint64)
959 {// Need to fill all of free memory with discardable pages
961 maxCacheSize = minCacheSize + gFreeRam;
962 if (aMinOffsetBytes != KMaxTUint64)
963 {// Set the min cache size relative to the max cache size.
964 minCacheSize = maxCacheSize - aMinOffsetBytes;
966 TESTDEBUG(test.Printf(_L("free 0x%x max 0x%x min 0x%x\n"), gFreeRam, maxCacheSize, minCacheSize));
970 maxCacheSize = aMaxBytes;
973 TInt r = DPTest::SetCacheSize(maxCacheSize, maxCacheSize);
976 r = DPTest::SetCacheSize(minCacheSize, maxCacheSize);
979 DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
980 TESTDEBUG(test.Printf(_L("After CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"),
981 minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
983 aNumDiscardableBytes = currentCacheSize - minCacheSize;
984 TESTDEBUG(test.Printf(_L("Number of discardable bytes 0x%x\n"), aNumDiscardableBytes));
986 if (aMaxBytes == KMaxTUint64)
989 if (gFreeRam != aNumDiscardableBytes)
990 {// The only free RAM should be that of the DP cache.
991 test.Printf(_L("gFreeRam 0x%x aNumDiscardableBytes 0x%x\n"), gFreeRam, aNumDiscardableBytes);
1002 // Flush the cache and set the boundaries back to their original values
1008 TUint minCacheSize = 0;
1009 TUint maxCacheSize = 0;
1010 TUint currentCacheSize = 0;
1012 TESTDEBUG(test.Printf(_L("FlushCache\n")));
1013 TInt r = DPTest::FlushCache();
1015 DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
1016 TESTDEBUG(test.Printf(_L("After CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"),
1017 minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
1020 r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
1021 r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMaxCacheSize);
1022 TESTDEBUG(test.Printf(_L("SetCacheSize returns r = %d\n"), r));
1023 DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
1024 TESTDEBUG(test.Printf(_L("After CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"),
1025 minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
1032 // Write to a number of chunks
1034 TInt WriteToChunk(RChunk* aChunkArray, TUint aChunkArraySize, TInt aChunk, TUint8 aStart=0)
1036 for (TUint i=0; i<10; i++) // Write to all chunks 10 times
1038 for (TUint j=0; j < aChunkArraySize; j++) //Write to all open chunks except aChunk.
1040 if (aChunkArray[j].Handle() != NULL)
1042 if ((TInt)j == aChunk) // Don't write to specified chunk
1046 TUint8* base = aChunkArray[j].Base();
1047 for (TUint8 k = aStart; k < aStart + 10; k++)
1049 *base++ = k; // write 0 - 9 to the chunk
1061 // Read chunks - If a chunk is specified, that particular chunk is not read
1063 TInt ReadChunk(RChunk* aChunkArray, TUint aChunkArraySize, TInt aChunk=-1)
1065 for (TUint j=0; j < aChunkArraySize; j++) //Read all open chunks
1067 if (aChunkArray[j].Handle() != NULL)
1069 if ((TInt)j == aChunk) // Don't read specified chunk
1073 TUint8* base = aChunkArray[j].Base();
1074 while ((aChunkArray[j].Base() + aChunkArray[j].Size()) != base)
1087 // Remove ALL chunks allocated
1089 // @param aChunkArray The array that stores a reference to the chunks created.
1090 // @param aChunkArraySize The size of aChunkArray.
1092 void RemoveChunkAlloc(RChunk*& aChunkArray, TUint& aChunkArraySize)
1094 TInt closedChunks = 0;
1096 if (aChunkArray == NULL)
1097 {// The chunk array has already been deleted.
1101 for (TUint i = 0; i < aChunkArraySize; i++)
1103 if (aChunkArray[i].Handle() != NULL)
1105 aChunkArray[i].Close();
1109 delete[] aChunkArray;
1111 aChunkArraySize = 0;
1113 test.Printf(_L("Free RAM after closing %d chunks = 0x%x\n"),closedChunks,gFreeRam);
1117 TBool SpaceAvailForPageTables(TUint aZoneIndex, TInt aNumPages)
1119 // Every 1MB allocated needs a new page table
1120 const TUint KDataBytesPerPageTable = 1024 * 1024;
1122 // 1 Page can fit 4 page tables
1123 const TUint KPageTablesPerPage = 4;
1127 if (aNumPages == KFillAllMovable)
1129 aNumPages = gTotalPageCount.iFreePages;
1132 TUint allocBytes = aNumPages << gPageShift;
1134 // Add 1 as you always require at least 1 page table
1135 TUint pageTablesRequired = (allocBytes / KDataBytesPerPageTable) + 1;
1136 // Add 1 as the first 1-3 page tables may require a new page.
1137 TUint pageTablePagesRequired = (pageTablesRequired / KPageTablesPerPage) + 1;
1139 // Determine the number of free pages in the other zones
1140 TUint freeInOther = 0;
1141 for (TUint index = 0; index < gZoneCount; index++)
1143 if (index != aZoneIndex)
1145 freeInOther += gZoneUtilArray[index].iFreePages;
1149 // Need an overhead for the heap to grow (5 pages)
1150 const TUint KOverhead = 5;
1151 if (freeInOther < pageTablePagesRequired + KOverhead)
1152 {// Not enough space in other zones to fit all page tables
1153 test.Printf(_L("No space in other zones for page table pages\n"));
1156 // There is space available in the other zones to fit all the page tables
1162 // Allocate movable memory in the form of chunks
1164 // @param aChunkArray The array to store a reference to the chunks created.
1165 // @param aChunkArraySize The size of aChunkArray.
1166 // @param aNumChunks The number of chunks to create.
1167 // @param aNumPages The size of each chunk.
1169 TInt AllocMovable(RChunk*& aChunkArray, TUint& aChunkArraySize, TInt aNumChunks, TUint aChunkSize=KChunkDefaultSize, TBool aForceFill = ETrue)
1173 TUint chunksAllocd = 0;
1174 TBool fillAll = EFalse;
1176 TInt numChunks = aNumChunks;
1178 // Allocate chunks to take up all of memory with the maximum number of chunks
1179 if (aNumChunks == KFillAllMovable)
1182 if (aChunkSize == KChunkDefaultSize)
1184 numChunks = KMaxChunks;
1185 aChunkSize = gFreeRam / numChunks;
1189 numChunks = gFreeRam / aChunkSize;
1193 test.Printf(_L("gFreeRam = 0x%x, aChunkSize = 0x%x, numChunks = %d\n"), gFreeRam, aChunkSize, numChunks);
1195 // Allocate as many chunks as is specified, either with the default chunk size or a specified chunk size
1196 if (aChunkArray == NULL)
1198 aChunkArraySize = numChunks;
1199 aChunkArray = new RChunk[aChunkArraySize];
1200 if (aChunkArray == NULL)
1201 return KErrNoMemory;
1204 // Create chunks for each RChunk with a NULL handle.
1205 for (i = 0; i < aChunkArraySize; i++)
1207 if (aChunkArray[i].Handle() == NULL)
1209 // Keep going even if a chunk creation fails as the flag tests rely
1211 r = aChunkArray[i].CreateLocal(aChunkSize, aChunkSize);
1212 if (r != KErrNone && fillAll && aForceFill)
1214 while (aChunkArray[i].CreateLocal(aChunkSize, aChunkSize) != KErrNone)
1216 aChunkSize -= gPageSize;
1224 User::After(10); // Wait so that the next chunk gets allocated in the next time slice
1226 test.Printf(_L("Number of chunks allocd = %d\n"),chunksAllocd);
1233 // Allocate the specified number of movable pages to a specific zone
1234 // If the number of pages is not specified, then fill the specified zone with
1237 // @param aChunkArray The array to store a reference to the chunks created.
1238 // @param aChunkArraySize The size of aChunkArray.
1239 // @param aZoneIndex The zone index to allocate movable pages to.
1240 // @param aNumPages The number of movable pages to allocate.
1242 TInt ZoneAllocMovable(RChunk*& aChunkArray, TUint& aChunkArraySize, TUint aZoneIndex, TUint aNumPages = KMaxTUint)
1244 ResetRamZoneFlags();
1246 TUint allocBytes = 0;
1248 if (aNumPages == KMaxTUint)
1250 aNumPages = gZoneUtilArray[aZoneIndex].iFreePages;
1253 allocBytes = aNumPages << gPageShift;
1255 if (!SpaceAvailForPageTables(aZoneIndex, aNumPages))
1260 // Block all other zones from allocation
1261 for(TUint index = 0; index < gZoneCount; index++)
1263 if (index == aZoneIndex)
1265 r = Ldd.SetZoneFlag(gZoneConfigArray[index].iZoneId, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
1269 r = Ldd.SetZoneFlag(gZoneConfigArray[index].iZoneId, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
1273 test.Printf(_L("Failed to set RAM zone flag on zone %d r = %d\n"), index, r);
1278 // Allocate the movable pages
1279 r = AllocMovable(aChunkArray, aChunkArraySize, 1, allocBytes);
1281 ResetRamZoneFlags();
1288 // Allocate the specified number of discardable pages to a specific zone
1290 // @param aZoneIndex The zone index to allocate discardable pages to.
1291 // @param aNumPages The number of discardable pages to allocate.
1292 // @param aDisPages On return this will contain the number of discardable pages allocated
1294 TInt ZoneAllocDiscard(TUint aZoneIndex, TUint aNumPages, TInt& aDisPages)
1297 ResetRamZoneFlags();
1299 for (TUint index = 0; index < gZoneCount; index++)
1301 TUint zoneID = gZoneConfigArray[index].iZoneId;
1302 if (index == aZoneIndex)
1304 r = Ldd.SetZoneFlag(zoneID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
1308 r = Ldd.SetZoneFlag(zoneID, gZoneConfigArray[index].iFlags, NO_DISCARD_FLAG);
1313 test.Printf(_L("Failed to set flag r = %d\n"), r);
1318 TUint disBytes = gTotalPageCount.iDiscardablePages + aNumPages << gPageShift;
1319 r = AllocDiscardable(aDisPages, disBytes);
1320 aDisPages = aDisPages >> gPageShift;
1323 test.Printf(_L("Discardable pages not allocated r = %d\n"), r);
1325 ResetRamZoneFlags();
1332 // Free movable pages by closing chunks.
1333 // The function will close every other chunk so that the movable pages are scattered in every zone
1335 // @param aChunkArray The array that stores reference to the chunks created.
1336 // @param aChunkArraySize The size of aChunkArray.
1338 TInt FreeMovable(RChunk* aChunkArray, TUint aChunkArraySize)
1341 TInt closedChunks = 0;
1343 for (i=0; i < aChunkArraySize; i+=2) // Close every other chunk
1345 if (aChunkArray[i].Handle() != NULL)
1347 aChunkArray[i].Close();
1352 test.Printf(_L("Free RAM after closing %d chunks = 0x%x\n"),closedChunks,gFreeRam);
1359 // Obtains the most preferable zone for allocating a specific type of page
1361 // @param aPageType The page type that we are interested in
1362 // @param aBestPrefIndex The index into the preference array for the zone.
1364 // @return KErrNotFound if a zone cannot be found, else the zone index into gZoneUtilArray
1366 TInt GetBestZone(TInt aPageType, TUint* aBestPrefIndex = NULL)
1374 case BEST_DISCARDABLE:
1376 TInt bestIndex = KErrNotFound;
1377 TUint bestPrefIndex = 0;
1379 TInt startIndex = KErrNotFound;
1380 TBool zoneFound = EFalse;
1381 // Find the least preferable zone that has movable or discardable pages
1382 for (TInt prefIndex = gZoneCount - 1; prefIndex >= 0; prefIndex--)
1384 curIndex = gPrefArray[prefIndex];
1385 if (gZoneUtilArray[curIndex].iAllocMovable || gZoneUtilArray[curIndex].iAllocDiscardable)
1387 startIndex = prefIndex;
1391 if (startIndex == KErrNotFound)
1394 // Work up the preference list to look for the best zone
1395 for (TInt prefIndex = startIndex; prefIndex >= 0; prefIndex--)
1397 curIndex = gPrefArray[prefIndex];
1398 if (gZoneUtilArray[curIndex].iFreePages)
1400 bestIndex = curIndex;
1401 bestPrefIndex = prefIndex;
1407 // If the zone still isn't found, look down the preference list
1410 for (TUint prefIndex = startIndex; prefIndex < gZoneCount; prefIndex++)
1412 curIndex = gPrefArray[prefIndex];
1413 if (gZoneUtilArray[curIndex].iFreePages)
1415 bestIndex = curIndex;
1416 bestPrefIndex = prefIndex;
1423 test.Printf(_L("leastPref = %d\n"), bestIndex);
1425 *aBestPrefIndex = bestPrefIndex;
1430 for (TUint prefIndex = 0; prefIndex < gZoneCount; prefIndex++)
1432 TUint mostPref = gPrefArray[prefIndex];
1433 if (gZoneUtilArray[mostPref].iFreePages != 0 ||
1434 gZoneUtilArray[mostPref].iAllocMovable != 0 ||
1435 gZoneUtilArray[mostPref].iAllocDiscardable != 0)
1437 test.Printf(_L("mostPref = %d\n"), mostPref);
1439 *aBestPrefIndex = prefIndex;
1446 test.Printf(_L("Cannot find zone\n"));
1447 return KErrNotFound;
1450 void GetCandList1(TInt aIndex);
1452 // VerifyMovDisAlloc
1454 // Checks that all movable and discardable pages are in the correct RAM zones.
1455 // Should only be invoked after a general defragmentation or a general
1456 // defragmentation followed by an allocation.
1458 // NOTE - This shouldn't be used to verify RAM if a RAM specific allocation or
1459 // zone claim operation has been performed.
1461 // @return KErrNone if RAM layout is good, KErrGeneral if not, KErrNotFound if no
1462 // movable or discardable pages are allocated.
1464 TInt VerifyMovDisAlloc()
1467 TInt leastInUse = KErrNotFound;
1468 TUint requiredMovDis = 0;
1469 TUint totalMorePrefInUse = 0;
1470 TBool verifySpread = ETrue;
1471 TBool prevZoneNotFull = EFalse;
1473 // Determine which is the least prefable RAM zone in use and how many pages
1474 // are allocated of each type.
1475 for (TInt prefIndex = gZoneCount - 1; prefIndex >= 0; prefIndex--)
1477 TUint index = gPrefArray[prefIndex];
1478 TUint allocMov = gZoneUtilArray[index].iAllocMovable;
1479 TUint allocDis = gZoneUtilArray[index].iAllocDiscardable;
1480 TUint allocFixed = gZoneUtilArray[index].iAllocFixed;
1481 TESTDEBUG(test.Printf(_L("pref ID 0x%x\n"), gZoneConfigArray[index].iZoneId));
1482 if (allocMov || allocDis || allocFixed)
1484 TBool zoneNotFull = EFalse;
1485 GetCandList1(index);
1486 TUint candIndex = 0;
1487 for (; candIndex < gZoneCount; candIndex++)
1489 TInt zoneIndexCand = gCandList1[candIndex];
1490 if (zoneIndexCand == KInvalidCandIndex)
1494 allocMov += gZoneUtilArray[zoneIndexCand].iAllocMovable;
1495 allocDis += gZoneUtilArray[zoneIndexCand].iAllocDiscardable;
1496 if (gZoneUtilArray[zoneIndexCand].iFreePages)
1498 zoneNotFull = ETrue;
1501 prefIndex -= candIndex - 1;
1502 if (leastInUse == KErrNotFound)
1503 {// Have found least preferable RAM zone that is in use.
1506 {// The least preferable RAM zone has fixed pages in it so
1507 // RAM may be more spread out than is necessary.
1508 verifySpread = EFalse;
1513 if ((allocMov || allocDis) && prevZoneNotFull)
1514 {// The previous least preferable RAM zones were not full so shouldn't
1515 // be any movable or discardable pages in the RAM zones of this preference.
1516 test.Printf(_L("Movable or discardable pages in more preferable RAM zones unnecessarily\n"));
1519 prevZoneNotFull = zoneNotFull;
1521 // Not the least preferable RAM zone so add to total allocatable.
1522 totalMorePrefInUse += allocMov + allocDis + gZoneUtilArray[index].iFreePages;
1524 requiredMovDis += allocMov + allocDis;
1528 if (leastInUse == KErrNotFound)
1529 {// No movable or discardable pages are allocated.
1530 test.Printf(_L("No in use RAM zones found????\n"));
1531 return KErrNotFound;
1534 if (totalMorePrefInUse > requiredMovDis)
1535 {// There enough allocatable pages in the RAM zones below the currently
1536 // least preferable RAM in use.
1537 test.Printf(_L("Memory is spread out totalMorePref 0x%x required 0x%x\n"), totalMorePrefInUse, requiredMovDis);
1548 // Populates a list of all zones that have the same preference and the same amount
1549 // of immovable pages
1551 void GetCandList1(TInt aIndex)
1553 for (TUint i=0; i<gZoneCount; i++)
1555 gCandList1[i] = KInvalidCandIndex;
1558 TInt candListIndex = 0;
1560 for (TUint i=0; i<gZoneCount; i++)
1562 if (ZonesSamePref(aIndex, i))
1564 gCandList1[candListIndex] = i;
1573 // Populates a list of all zones that have the same preference and the same amount
1574 // of immovable pages
1576 void GetCandList2(TInt aIndex)
1578 for (TUint i=0; i<gZoneCount; i++)
1580 gCandList2[i] = KInvalidCandIndex;
1583 TInt candListIndex = 0;
1585 for (TUint i=0; i<gZoneCount; i++)
1587 if (ZonesSamePref(aIndex, i))
1589 gCandList2[candListIndex] = i;
1596 // GetCandListFixed1
1598 // Populates a list of all zones that have the same preference and the same
1599 // amount of immovable pages, it will ignore RAM zones that are full of
1600 // immovable pages as these are the only RAM zones that fixed pages can't be
1603 void GetCandListFixed1(TInt aIndex)
1605 for (TUint i=0; i<gZoneCount; i++)
1607 gCandList1[i] = KInvalidCandIndex;
1610 TInt candListIndex = 0;
1612 for (TUint i=0; i<gZoneCount; i++)
1614 if (ZonesSamePref(aIndex, i) &&
1615 gZoneUtilArray[i].iAllocFixed + gZoneUtilArray[i].iAllocUnknown !=
1616 gZoneConfigArray[i].iPhysPages)
1618 gCandList1[candListIndex] = i;
1625 // MultiGenDefragThreadFunc
1627 // Called when a general defrag is called to run at the same time as another operation
1629 TInt MultiGenDefragThreadFunc(TAny* /*aPtr*/)
1631 RRamDefragFuncTestLdd Ldd2;
1632 TInt r = Ldd2.Open();
1635 RDebug::Printf("Unable to open Ldd2 in MultiGenDefragThreadFunc, r = %d\n", r);
1639 RThread thisThread = RThread();
1640 thisThread.SetPriority(EPriorityLess);
1641 r = Ldd2.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
1647 TBool gTestLoop = EFalse;
1649 // MultiLoopGenDefragThreadFunc
1651 // Called when a general defrag is called to run at the same time as another operation
1653 TInt MultiLoopGenDefragThreadFunc(TAny* /*aPtr*/)
1655 RRamDefragFuncTestLdd Ldd2;
1656 TInt r = Ldd2.Open();
1659 RDebug::Printf("Unable to open Ldd2 in MultiLoopGenDefragThreadFunc, r = %d\n", r);
1663 RThread thisThread = RThread();
1664 thisThread.SetPriority(EPriorityLess);
1667 r = Ldd2.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
1678 // MultiEmptyZoneThreadFunc
1680 // Called when a zone defrag is called to run at the same time as another operation
1682 TInt MultiEmptyZoneThreadFunc(TAny* zoneID)
1684 RRamDefragFuncTestLdd Ldd2;
1685 TInt r = Ldd2.Open();
1688 RDebug::Printf("Unable to open Ldd2 in MultiEmptyZoneThreadFunc, r = %d\n", r);
1692 RThread thisThread = RThread();
1693 thisThread.SetPriority(EPriorityLess);
1694 r = Ldd2.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, (TUint)zoneID);
1702 // MultiClaimZoneThreadFunc
1704 // Called when a ClaimRamZone is called to run at the same time as another operation
1706 TInt MultiClaimZoneThreadFunc(TAny* zoneID)
1708 RRamDefragFuncTestLdd Ldd2;
1709 TInt r = Ldd2.Open();
1712 RDebug::Printf("Unable to open Ldd2 in MultiClaimZoneThreadFunc, r = %d\n", r);
1716 RThread thisThread = RThread();
1717 thisThread.SetPriority(EPriorityLess);
1718 r = Ldd2.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, (TUint)zoneID);
1726 // TestAllocStrategies
1728 // Verifying that pages are allocated correctly. All tests rely on a general defragmentation occuring
1729 // in TestStart() so that memory is in a defragmented state before the allocations.
1731 //---------------------------------------------------------------------------------------------------------------------
1732 //! @SYMTestCaseID KBASE-t_ramdefrag-0525
1733 //! @SYMTestCaseDesc Verifying that pages are allocated correctly
1734 //! @SYMTestType CIT
1735 //! @SYMPREQ PREQ308
1736 //! @SYMTestPriority High
1738 //! 1. Allocate one chunk to memory.
1739 //! Check which zone the chunk has been allocated to by checking the
1740 //! number of movable pages before and after the allocation has taken place.
1741 //! 2. Allocate one chunk to memory that is large enough to cause an empty RAM
1742 //! zone to be used.
1743 //! 3. Allocate a certain number of fixed pages to memory.
1744 //! Check which zone the chunk has been allocated to by checking the
1745 //! number of fixed pages before and after the allocation has taken place.
1746 //! 4. Allocate fixed pages when it is known that discardable pages will need
1747 //! to be discarded for the allocation to succeed.
1748 //! 5. Allocate fixed pages when it is known that movable pages will need
1749 //! to be moved for the allocation to succeed.
1750 //! 6. Allocate fixed pages when it is known that movable pages will need
1751 //! to be moved for the allocation to succeed, Determine the "best" zone for fixed pages and allocate 1 more
1753 //! 7. Allocate discardable pages by loading pages that are demand paged.
1754 //! Check which zone the memory has been allocated to by checking the number of
1755 //! discardable pages before and after the allocation has taken place.
1757 //! @SYMTestExpectedResults
1758 //! 1. The memory has been allocated to the most preferred zone with the least amount of
1759 //! space accounting for the zone threshold for movable pages.
1760 //! 2. The new RAM zone is used and the movable page are allocated into it first then the other
1761 //! more preferable RAM zones.
1762 //! 3. The fixed pages are allocated to the most preferred zone
1763 //! 4. The fixed pages are allocatted to the most preferred zone with free, movable or discardable pages in it.
1764 //! 5. The fixed pages are allocatted to the most preferred zone with free, movable or discardable pages in it.
1765 //! 6. Extra page is placed in the next preferable to the "best"zone
1766 //! 7. Memory is allocated to the most preferred zone with the least amount of
1767 //! space accounting for the zone threshold for discardable pages.
1768 //---------------------------------------------------------------------------------------------------------------------
1769 TInt TestAllocStrategies()
1772 test.Start(_L("Test1: Check which zone a movable page has been allocated to "));
1773 TestStart(); //This will perform a general defrag which should tidy up RAM for us
1774 gChunkArray1 = new RChunk;
1775 gChunkArraySize1 = 1;
1776 TBool zoneFound = EFalse;
1778 // Determine if suitable RAM zone exists for testing
1779 TInt best = GetBestZone(BEST_MOVABLE);
1780 if (best == KErrNotFound)
1782 test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
1786 test.Printf(_L("best = %d\n"), best);
1787 // Populate the candidate list
1789 // Allocate 1 movable page
1790 GetOriginalPageCount();
1791 if (AllocMovable(gChunkArray1, gChunkArraySize1, 1, gPageSize) != KErrNone)
1793 test.Printf(_L("Not enough free RAM for test - Skipping...\n"));
1798 TInt r = VerifyMovDisAlloc();
1799 // Need to check all candidates to see if page has gone into any one of them
1800 for (TUint i=0; i < gZoneCount; i++)
1802 if (gCandList2[i] == KInvalidCandIndex)
1806 TUint zoneIndex = gCandList2[i];
1807 if (gOriginalPageCountArray[zoneIndex].iAllocMovable < gZoneUtilArray[zoneIndex].iAllocMovable)
1814 if (r == KErrNone && zoneFound)
1816 test.Printf(_L("Pass: Movable allocated to the zone expected\n"));
1820 test.Printf(_L("Fail: Movable allocated to a zone that was not expected, r %d\n"), r);
1821 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
1828 test.Next(_L("Test2: Check that movable pages allocated from least preferable RAM zone to be used\n"));
1830 gChunkArray1 = new RChunk;
1831 gChunkArraySize1 = 1;
1832 // Determine if suitable RAM zone exists for testing.
1833 TUint bestPrefIndex;
1834 best = GetBestZone(BEST_MOVABLE, &bestPrefIndex);
1835 TUint morePrefIndex = bestPrefIndex - 1;
1836 TUint nextLeastPrefIndex = bestPrefIndex + 1;
1839 // Check number of free pages in the more preferable zones
1840 TUint freeInMorePref = 0;
1841 for (TUint i = 0; i < bestPrefIndex; i++)
1843 freeInMorePref += gZoneUtilArray[gPrefArray[i]].iFreePages;
1846 test.Printf(_L("freeInMorePref = 0x%x\n"), freeInMorePref);
1847 const TUint KHeapOverhead = 5; // Room for kernel heap allocations so they don't affect the page count
1848 if (best == KErrNotFound || nextLeastPrefIndex >= gZoneCount ||
1849 (gZoneUtilArray[gPrefArray[nextLeastPrefIndex]].iFreePages < KHeapOverhead &&
1850 freeInMorePref < KHeapOverhead)||
1851 gZoneConfigArray[gPrefArray[nextLeastPrefIndex]].iPref == gZoneConfigArray[best].iPref ||
1852 gZoneConfigArray[gPrefArray[morePrefIndex]].iPref == gZoneConfigArray[best].iPref)
1853 {// No less preferable RAM zone or there are more or less preferable of
1854 // same preference so re-ordering potential makes verification too complex.
1855 test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
1859 // Ensure the zone is almost full as chunks will get allocated in blocks
1860 // by almost filling the best zone with fixed pages, leaving a space (e.g. 20 pages)
1861 const TUint KSpaceNeeded = 20;
1862 if (gZoneUtilArray[best].iFreePages > KSpaceNeeded)
1864 TUint zoneID = gZoneUtilArray[best].iZoneId;
1865 TUint diffAlloc = gZoneUtilArray[best].iFreePages - KSpaceNeeded;
1866 TInt r = Ldd.ZoneAllocDiscontiguous(zoneID, diffAlloc);
1869 CLEANUP(Ldd.FreeAllFixedPages());
1877 // Update the number of free pages in the more preferable zones
1879 for (TUint i = 0; i < bestPrefIndex; i++)
1881 freeInMorePref += gZoneUtilArray[gPrefArray[i]].iFreePages;
1883 TUint origFixed = gTotalPageCount.iFixedPages;
1885 GetOriginalPageCount();
1887 // Allocate enough movable pages that the next least preferable RAM zone
1888 // will need to be used.
1889 TUint movPages = gZoneUtilArray[best].iFreePages + 1;
1890 TUint movBytes = movPages << gPageShift;
1891 TInt r = AllocMovable(gChunkArray1, gChunkArraySize1, 1, movBytes);
1894 CLEANUP(Ldd.FreeAllFixedPages());
1895 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
1900 TUint curFixed = gTotalPageCount.iFixedPages;
1901 TInt difFixed = curFixed - origFixed;
1903 // If there is space in the more preferable zones then they should be allocated starting from
1904 // the "best" zone to the more preferable zones, else from the next least preferable zone to the more preferable zones
1906 if ((TInt)freeInMorePref > difFixed)
1907 {//No new zones should be turned on, allocation should have gone into bestPrefIndex then down towards most preferred zones
1908 TUint nextIndex = gPrefArray[nextLeastPrefIndex];
1909 if (gOriginalPageCountArray[best].iAllocMovable >= gZoneUtilArray[best].iAllocMovable ||
1910 gOriginalPageCountArray[nextIndex].iAllocMovable < gZoneUtilArray[nextIndex].iAllocMovable)
1912 test.Printf(_L("Movable page allocated incorrectly origFree 0x%x curFree 0x%x nxtPrefFree 0x%x\n"),
1913 gOriginalPageCountArray[best].iFreePages, gZoneUtilArray[best].iFreePages,
1914 gZoneUtilArray[nextLeastPrefIndex].iFreePages);
1915 CLEANUP(Ldd.FreeAllFixedPages());
1916 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
1919 prefIndex = morePrefIndex;
1922 {// If there are enough free pages in "nextIndex" to fit all movable pages allocated then the movable
1923 // page count in "best" should stay the same, else they should be allocated between "nextIndex" and "best".
1924 TUint nextIndex = gPrefArray[nextLeastPrefIndex];
1925 test.Printf(_L("nextIndex = %d\n"), nextIndex);
1926 if (gOriginalPageCountArray[nextIndex].iAllocMovable >= gZoneUtilArray[nextIndex].iAllocMovable ||
1927 (gOriginalPageCountArray[nextIndex].iFreePages >= movPages &&
1928 gOriginalPageCountArray[best].iAllocMovable != gZoneUtilArray[best].iAllocMovable) ||
1929 (gOriginalPageCountArray[nextIndex].iFreePages < movPages &&
1930 gOriginalPageCountArray[best].iAllocMovable == gZoneUtilArray[best].iAllocMovable))
1932 test.Printf(_L("Movable page allocated incorrectly origFree 0x%x curFree 0x%x nxtPrefFree 0x%x\n"),
1933 gOriginalPageCountArray[nextIndex].iFreePages, gZoneUtilArray[nextIndex].iFreePages,
1934 gZoneUtilArray[nextIndex].iFreePages);
1935 CLEANUP(Ldd.FreeAllFixedPages());
1936 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
1939 prefIndex = bestPrefIndex;
1942 // Check that movable pages have only been allocated into the more
1943 // preferable RAM zones if the less preferable ram zones in use are full.
1948 TUint indexCurrent = gPrefArray[prefIndex];
1949 TUint indexLessPref = gPrefArray[prefIndex+1];
1951 if (gOriginalPageCountArray[indexCurrent].iAllocMovable < gZoneUtilArray[indexCurrent].iAllocMovable &&
1952 gZoneUtilArray[indexLessPref].iFreePages)
1953 {// Current in use zone or less preferable than current has free pages so fail
1954 test.Printf(_L("Movable page allocated incorrectly origFree 0x%x curFree 0x%x nxtPrefFree 0x%x\n"),
1955 gOriginalPageCountArray[best].iFreePages, gZoneUtilArray[best].iFreePages,
1956 gZoneUtilArray[nextLeastPrefIndex].iFreePages);
1957 CLEANUP(Ldd.FreeAllFixedPages());
1958 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
1963 test.Printf(_L("Pass: Pages allocated to the zone expected\n"));
1967 test.Next(_L("Test3: Check which zone a fixed page has been allocated to "));
1968 for (TUint testStep = 0; testStep < 2; testStep++)
1973 test.Printf(_L("Testing discontiguous allocations\n"));
1977 test.Printf(_L("Testing contiguous allocations\n"));
1982 GetOriginalPageCount();
1984 best = GetBestZone(BEST_FIXED);
1985 TESTDEBUG(test.Printf(_L("best = %d\n"), best));
1986 if (best == KErrNotFound)
1988 test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
1993 TInt allocFixedPages = 1; // Try and allocate just 1 fixed page
1997 Ldd.AllocateFixed(allocFixedPages);
2001 TUint allocFixedBytes = allocFixedPages << gPageShift;
2002 Ldd.AllocContiguous(allocFixedBytes);
2008 for (TUint i=0; i<gZoneCount; i++)
2010 if (gCandList1[i] == KInvalidCandIndex)
2014 TUint zoneIndex = gCandList1[i];
2015 if (gOriginalPageCountArray[zoneIndex].iAllocFixed + allocFixedPages <= gZoneUtilArray[zoneIndex].iAllocFixed)
2024 test.Printf(_L("Pass: Chunk has been allocated to the zone expected\n"));
2028 test.Printf(_L("Fail: Fixed been allocated to a zone that was not expected\n"));
2029 CLEANUP(Ldd.FreeAllFixedPages());
2033 Ldd.FreeAllFixedPages();
2038 test.Next(_L("Test4: Check fixed page allocations will discard pages"));
2039 for (TUint testStep = 0; testStep < 2; testStep++)
2044 test.Printf(_L("Testing discontiguous allocations\n"));
2048 test.Printf(_L("Testing contiguous allocations\n"));
2057 test.Printf(_L("Not a paged ROM - Skipping test step\n"));
2061 best = GetBestZone(BEST_FIXED);
2062 TESTDEBUG(test.Printf(_L("best = %d\n"), best));
2063 if (best == KErrNotFound)
2065 test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
2068 // Ensure discardable pages in the preferred RAM zone.
2069 r = AllocDiscardable(discardBytes);
2072 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
2075 if (discardBytes < gPageSize)
2076 {// Can't discard any pages so test can't run.
2077 test.Printf(_L("Memory too full to perform test - Skipping...\n"));
2081 // Make sure all RAM zones that the fixed page allocation could potentially
2082 // go to have discardable pages allocated in it.
2083 GetOriginalPageCount();
2084 GetCandListFixed1(best);
2085 for (TUint i = 0; i < gZoneCount; i++)
2087 if (gCandList1[i] == KInvalidCandIndex)
2091 TUint zoneIndex = gCandList1[i];
2092 if (gOriginalPageCountArray[zoneIndex].iAllocDiscardable == 0)
2094 test.Printf(_L("No dicardable pages in one of the candidates RAM zones - Skipping...\n"));
2097 if (gOriginalPageCountArray[zoneIndex].iFreePages != 0)
2099 test.Printf(_L("Some free pages in candidate RAM zone ID%x\n"),
2100 gZoneConfigArray[zoneIndex].iZoneId);
2101 CLEANUP(ResetDPCache());
2106 // Allocate 1 fixed page and ensure that it discards a page.
2110 r = Ldd.AllocateFixed(1);
2114 r = Ldd.AllocContiguous(gPageSize);
2118 // Find RAM zone the fixed page was allocated into.
2121 for (TUint i = 0; i < gZoneCount; i++)
2123 if (gCandList1[i] == KInvalidCandIndex)
2127 TUint zoneIndex = gCandList1[i];
2128 if (gOriginalPageCountArray[zoneIndex].iAllocFixed < gZoneUtilArray[zoneIndex].iAllocFixed)
2131 if (gOriginalPageCountArray[zoneIndex].iAllocDiscardable <= gZoneUtilArray[zoneIndex].iAllocDiscardable &&
2132 gOriginalPageCountArray[zoneIndex].iAllocMovable <= gZoneUtilArray[zoneIndex].iAllocMovable)
2134 test.Printf(_L("Fixed pages allocated but no pages discarded in RAM zone ID 0x%x\n"),
2135 gZoneConfigArray[zoneIndex].iZoneId);
2136 CLEANUP(ResetDPCache());
2142 if (!zoneFound || r != KErrNone)
2144 test.Printf(_L("No fixed pages were allocated r = %d\n"), r);
2145 CLEANUP(ResetDPCache());
2150 test.Printf(_L("Pass: Pages been allocated to the zone expected\n"));
2153 // This will free any DP cache pages and fixed pages allocated.
2158 for (TUint testIndex = 0; testIndex < 2; testIndex++)
2163 test.Next(_L("Test5: Check fixed page allocations (discontiguous) will move pages"));
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"));
2172 gChunkArray1 = new RChunk;
2173 gChunkArraySize1 = 1;
2175 TUint freeInOtherZones = 0;
2176 TUint allocatablePages;
2177 TUint allocImmovPages;
2178 TUint bestPrefIndex;
2179 TUint nextBestIndex = 0;
2180 const TUint KMovAllocOverhead = 5; // need pages for page tables and kernel heap expansion.
2182 best = GetBestZone(BEST_FIXED, &bestPrefIndex);
2183 TESTDEBUG(test.Printf(_L("best = %d\n"), best));
2184 GetCandListFixed1(best);
2185 TUint candidates = 0;
2186 for (TUint i = 0; i < gZoneCount; i++)
2188 if (gCandList1[i] == KInvalidCandIndex)
2194 if (best == KErrNotFound ||
2195 (gZoneUtilArray[best].iAllocMovable == 0 && gZoneUtilArray[best].iFreePages < KMovAllocOverhead) ||
2198 test.Printf(_L("Cannot find zone or too many equal pref zones to perform test - Skipping...\n"));
2203 {// need to work out what the next best zone would be
2205 if (bestPrefIndex + 1 >= gZoneCount)
2207 test.Printf(_L("Cannot find next best zone - Skipping...\n"));
2210 nextBestIndex = gPrefArray[bestPrefIndex + 1];
2211 test.Printf(_L("nextBestIndex= %d\n"), nextBestIndex);
2212 GetCandListFixed1(nextBestIndex);
2214 for (TUint i = 0; i < gZoneCount; i++)
2216 if (gCandList1[i] == KInvalidCandIndex)
2222 if (gZoneUtilArray[nextBestIndex].iPhysPages == gZoneUtilArray[nextBestIndex].iAllocFixed +
2223 gZoneUtilArray[nextBestIndex].iAllocUnknown ||
2226 test.Printf(_L("Cannot find zone or too many equal pref zones to perform test - Skipping...\n"));
2233 for (TUint i = 0; i < gZoneCount; i++)
2235 if (i != (TUint)best)
2237 freeInOtherZones += gZoneUtilArray[i].iFreePages;
2240 allocatablePages = gZoneUtilArray[best].iFreePages +
2241 gZoneUtilArray[best].iAllocMovable +
2242 gZoneUtilArray[best].iAllocDiscardable;
2244 if (allocatablePages > freeInOtherZones)
2246 test.Printf(_L("Not enough free RAM for test - Skipping...\n"));
2250 // Allocate the fixed array before getting any page counts
2251 r = Ldd.AllocFixedArray(allocatablePages + 1);
2254 test.Printf(_L("Failed to allocate fixed array r = %d - Skipping...\n"), r);
2258 // Fill the RAM zone with movable pages if none already in it.
2260 if (gZoneUtilArray[best].iAllocMovable == 0)
2262 if (!gZoneUtilArray[best].iFreePages)
2264 test.Printf(_L("RAM zone ID %x too full for test - Skipping...\n"), gZoneConfigArray[best].iZoneId);
2267 // Fill the zone with movable pages
2268 r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, best);
2271 test.Printf(_L("Failed to fill zone index %d with movable r = %d\n"), best, r);
2272 CLEANUP(ResetRamZoneFlags());
2276 if (GetBestZone(BEST_FIXED) != best)
2278 test.Printf(_L("Unable to complete test; RAM zone reordering - Skipping...\n"));
2282 // Allocate fixed pages after reseting the allocation flags.
2284 ResetRamZoneFlags();
2285 allocatablePages = gZoneUtilArray[best].iFreePages +
2286 gZoneUtilArray[best].iAllocMovable +
2287 gZoneUtilArray[best].iAllocDiscardable;
2291 r = Ldd.AllocateFixed2(allocatablePages);
2293 allocImmovPages = gZoneUtilArray[best].iAllocFixed + gZoneUtilArray[best].iAllocUnknown;
2294 if (r != KErrNone || gZoneConfigArray[best].iPhysPages != allocImmovPages)
2296 test.Printf(_L("RAM zone ID 0x%x not full of immovable pages\n"), gZoneConfigArray[best].iZoneId);
2297 CLEANUP(ResetRamZoneFlags());
2302 test.Printf(_L("Pass: Pages allocated to the zone expected\n"));
2307 GetOriginalPageCount();
2308 r = Ldd.AllocateFixed2(allocatablePages + 1);
2310 allocImmovPages = gZoneUtilArray[best].iAllocFixed + gZoneUtilArray[best].iAllocUnknown;
2311 if (r != KErrNone || gZoneUtilArray[best].iPhysPages != allocImmovPages ||
2312 gZoneUtilArray[nextBestIndex].iAllocFixed <= gOriginalPageCountArray[nextBestIndex].iAllocFixed)
2314 test.Printf(_L("RAM zone ID 0x%x not full of immovable pages or next best ID 0x%x no fix pages\n"),
2315 gZoneConfigArray[best].iZoneId, gZoneConfigArray[nextBestIndex].iZoneId);
2316 test.Printf(_L("nextBest %d origFix 0x%x curFix 0x%x\n"),
2317 nextBestIndex, gOriginalPageCountArray[nextBestIndex].iAllocFixed,
2318 gZoneUtilArray[nextBestIndex].iAllocFixed);
2319 CLEANUP(ResetRamZoneFlags());
2322 // Go through every other zone and check that fixed pages haven't increased
2323 for (TUint i = 0; i < gZoneCount; i++)
2325 if (i != (TUint)best && i != (TUint)nextBestIndex &&
2326 gZoneUtilArray[i].iAllocFixed > gOriginalPageCountArray[i].iAllocFixed)
2328 test.Printf(_L("FAIL: Fix page count increased zoneIndex %d orig 0x%x current 0x%x\n"),
2329 i, gOriginalPageCountArray[i].iAllocFixed, gZoneUtilArray[i].iAllocFixed);
2330 CLEANUP(ResetRamZoneFlags());
2335 test.Printf(_L("Pass: Pages allocated to the zone expected\n"));
2339 // This will perform any required clean up.
2340 ResetRamZoneFlags();
2344 test.Next(_L("Test7: Check which zone a discardable page has been allocated to"));
2350 // Try to allocate just one more discardable page
2351 TUint allocBytes = (gTotalPageCount.iDiscardablePages + 1) << gPageShift;
2352 TInt discardablePages;
2354 best = GetBestZone(BEST_DISCARDABLE);
2355 if (best == KErrNotFound)
2357 test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
2361 // Populate the candidate list
2364 GetOriginalPageCount();
2365 TInt r = AllocDiscardable(discardablePages, allocBytes);
2368 test.Printf(_L("Fail: r %d\n"), r);
2369 CLEANUP(ResetDPCache());
2373 r = VerifyMovDisAlloc();
2374 // Need to check all candidates to see if page has gone into any one of them
2375 for (TUint i=0; i < gZoneCount; i++)
2377 if (gCandList2[i] == KInvalidCandIndex)
2381 TUint zoneIndex = gCandList2[i];
2382 if (gOriginalPageCountArray[zoneIndex].iAllocDiscardable < gZoneUtilArray[zoneIndex].iAllocDiscardable)
2389 if (r == KErrNone && zoneFound)
2391 test.Printf(_L("Pass: Discardable allocated to the zone expected\n"));
2395 test.Printf(_L("Fail: Discardable been allocated to a zone that was not expected r %d\n"), r);
2396 CLEANUP(ResetDPCache());
2403 test.Printf(_L("Not a paged ROM - Skipping test step\n"));
2407 test.Next(_L("Test8: Check fixed page allocations (contiguous) will move pages"));
2409 gChunkArray1 = new RChunk;
2410 gChunkArraySize1 = 1;
2413 TUint testZoneIndex = 0;
2415 // Get the most pref zone which is completely free to use as a test zone
2416 r = FindMostPrefEmpty(testZoneIndex);
2419 test.Printf(_L("Cannot find empty zone - Skipping...\n"));
2424 // fill the test zone with movable pages
2425 r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, testZoneIndex);
2427 if (r != KErrNone ||
2428 gZoneUtilArray[testZoneIndex].iAllocMovable != gZoneUtilArray[testZoneIndex].iPhysPages)
2430 test.Printf(_L("Failed to allocate movable r = %d - Skipping...\n"), r);
2434 if (gTotalPageCount.iFreePages < 1)
2436 test.Printf(_L("Insufficient memory - totalFreePages = 0x%x - Skipping...\n"), gTotalPageCount.iFreePages);
2440 for (TUint zoneIndex = 0; zoneIndex < gZoneCount; zoneIndex++)
2442 TUint zoneId = gZoneConfigArray[zoneIndex].iZoneId;
2443 if (zoneIndex != testZoneIndex)
2445 r = Ldd.SetZoneFlag(zoneId, gZoneConfigArray[zoneIndex].iFlags, NO_FIXED_FLAG);
2448 test.Printf(_L("Failed to set flag on zone index %d r = %d- Skipping...\n"), zoneIndex, r);
2454 //attempt to alloc 1 contiguous fixed page
2455 r = Ldd.AllocContiguous(gPageSize);
2457 if (r != KErrNone ||
2458 !gZoneUtilArray[testZoneIndex].iAllocFixed)
2460 test.Printf(_L("FAIL: no fixed pages in testZoneIndex(%d) r = %d\n"), testZoneIndex, r);
2461 CLEANUP(Ldd.FreeAllFixedPages());
2466 test.Printf(_L("Passed...\n"));
2480 //---------------------------------------------------------------------------------------------------------------------
2481 //! @SYMTestCaseID KBASE-t_ramdefrag-0526
2482 //! @SYMTestType CIT
2483 //! @SYMTestCaseDesc Verifying that pages are moved correctly
2484 //! @SYMPREQ PREQ308
2485 //! @SYMTestPriority High
2487 //! 1. Fragment the memory. Write to all chunks apart from chunk 9.
2488 //! Following this start a RAM defrag and whilst defrag is running write to chunk 9.
2489 //! 2. Fragment the memory. Start a RAM defrag and whilst defrag is running
2490 //! continuously write different values to the chunks.
2491 //! 3. Fragment the memory. Following this start a RAM defrag and whilst this
2492 //! is happening, continuously read from all chunks.
2493 //! 4. Allocate some movable pages. Call a device driver that will allocate fixed
2494 //! pages and write values to these fixed pages.
2495 //! Close every other chunk so that only 5 chunks are now still open.
2496 //! Following this perform a RAM defrag.
2497 //! Read from the fixed pages that were originally written to, ensuring
2498 //! that they have not changed.
2499 //! 5. Without starting any processes, allocate discardable pages by loading
2500 //! pages that are demand paged.
2501 //! Read each of the constants from beginning to end.
2502 //! Following this perform a RAM defrag.
2504 //! @SYMTestExpectedResults
2505 //! 1. Zones are emptied
2506 //! 2. Zones are emptied
2507 //! 3. Zones are emptied
2508 //! 4. The values written to the fixed pages have not changed.
2509 //! 5. Zones are emptied
2510 //---------------------------------------------------------------------------------------------------------------------
2511 TInt TestMovingPages()
2513 const TInt KAllChunks = -1; // Specifies that all chunks should be written to
2515 test.Start(_L("Test1: Whilst moving page, change the usage "));
2518 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks, KChunkDefaultSize);
2519 FreeMovable(gChunkArray1, gChunkArraySize1);
2521 // Find a chunk that exists
2522 TInt chunkIndex = gChunkArraySize1 - 1;
2523 for (; chunkIndex >= 0 && gChunkArray1[chunkIndex].Handle() == NULL; chunkIndex--);
2526 test.Printf(_L("No chunks were allocated\n"));
2527 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
2531 WriteToChunk(gChunkArray1, gChunkArraySize1, chunkIndex);
2533 TInt r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
2536 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
2537 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
2540 gTestThread.Logon(status);
2541 gTestThread.Resume();
2543 TUint8* base = gChunkArray1[chunkIndex].Base();
2544 while (status.Int() == KRequestPending)
2547 for (TInt8 k = 0; k < 10; k ++)
2549 if (base == gChunkArray1[chunkIndex].Base() + gChunkArray1[chunkIndex].Size())
2551 base = gChunkArray1[chunkIndex].Base();
2553 *base++ = k; // write 0 - 9 to the chunk
2557 User::WaitForRequest(status);
2559 TESTDEBUG(test.Printf(_L("defrag running on another thread returns %d\n"), r));
2561 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
2565 test.Printf(_L("Correct return value\n"));
2566 test.Printf(_L("Passed...\n"));
2570 test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
2574 gTestThread.Close();
2578 test.Next(_L("Test2: Whilst moving page, change the contents of the page"));
2581 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks, KChunkDefaultSize);
2582 FreeMovable(gChunkArray1, gChunkArraySize1);
2583 WriteToChunk(gChunkArray1, gChunkArraySize1, KAllChunks);
2585 r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
2588 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
2589 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
2592 gTestThread.Logon(status);
2593 gTestThread.Resume();
2595 TUint8 startValue = 0;
2596 while (status.Int() == KRequestPending)
2599 WriteToChunk(gChunkArray1, gChunkArraySize1, KAllChunks, startValue);
2600 if (++startValue > 245)
2606 User::WaitForRequest(status);
2608 TESTDEBUG(test.Printf(_L("defrag running on another thread returns %d\n"), r));
2610 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
2614 test.Printf(_L("Correct return value\n"));
2615 test.Printf(_L("Passed...\n"));
2619 test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
2623 gTestThread.Close();
2627 test.Next(_L("Test3: Whilst moving page, read pages"));
2630 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks, KChunkDefaultSize);
2631 FreeMovable(gChunkArray1, gChunkArraySize1);
2632 WriteToChunk(gChunkArray1, gChunkArraySize1, KAllChunks);
2634 r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
2637 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
2638 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
2641 gTestThread.Logon(status);
2642 gTestThread.Resume();
2644 while (status.Int() == KRequestPending)
2646 User::After(100000000);
2647 ReadChunk(gChunkArray1, gChunkArraySize1);
2650 User::WaitForRequest(status);
2652 TESTDEBUG(test.Printf(_L("defrag running on another thread returns %d\n"), r));
2654 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
2658 test.Printf(_L("Correct return value\n"));
2659 test.Printf(_L("Passed...\n"));
2663 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
2667 gTestThread.Close();
2670 test.Next(_L("Test4: Allocate fixed pages and then perform a defrag"));
2673 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
2674 FreeMovable(gChunkArray1, gChunkArraySize1);
2676 Ldd.AllocateFixedWrite(FILL_ALL_FIXED);
2678 GetOriginalPageCount();
2679 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
2680 test.Printf(_L("defrag returns %d\n"), r);
2682 if (CheckZonesSwitchedOff() != EFalse)
2684 test.Printf(_L("Fail: Zones were switched off when they shouldn't have been\n"));
2685 CLEANUP(r = Ldd.FreeAllFixedPagesRead());
2686 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
2691 test.Printf(_L("Passed...\n"));
2693 r = Ldd.FreeAllFixedPagesRead();
2694 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
2697 test.Next(_L("Test5: Allocate discardable pages and then perform a defrag"));
2701 TInt discardablePages;
2704 r = AllocDiscardable(discardablePages);
2707 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
2710 TESTDEBUG(test.Printf(_L("Number of discardable pages = 0x%x\n"), discardablePages >> gPageShift));
2712 GetOriginalPageCount();
2713 TBool genSucceed = CanGenSucceed();
2714 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
2716 TESTDEBUG(test.Printf(_L("defrag returns %d\n"), r));
2718 if (genSucceed && CheckZonesSwitchedOff() == EFalse)
2720 test.Printf(_L("Fail: Zones were not switched off when they should have been\n"));
2721 CLEANUP(ResetDPCache());
2726 test.Printf(_L("Passed...\n"));
2733 test.Printf(_L("Not a paged ROM - Skipping test step\n"));
2745 //---------------------------------------------------------------------------------------------------------------------
2746 //! @SYMTestCaseID KBASE-t_ramdefrag-0527
2747 //! @SYMTestType CIT
2748 //! @SYMTestCaseDesc Verifying the moving of pages in the defrag implmentation
2749 //! @SYMPREQ PREQ308
2750 //! @SYMTestPriority High
2752 //! 1. Fragment the memory. Following this perform a RAM defrag.
2753 //! Now that all movable memory has now been moved and the relevant source
2754 //! zones have been switched off, again attempt to perform a RAM defrag.
2755 //! 2. Fragment the memory. Following this perform a RAM defrag and whilst
2756 //! this is happening, continuously create more chunks.
2757 //! 3. Set up memory so that there are a mix of movable and free pages in a low preference zone, with there
2758 //! being movable pages in the higher preference zones. Following this call a general defrag
2759 //! 4. Set up memory so that there are a mix of movable and free pages and a couple of free pages
2760 //! in a low preference zone, with there being enough free pages in the higher preference zones for the
2761 //! pages to be moved to. Following this call a general defrag
2762 //! 5. Set up memory so that there is a fixed page and a movable page in an empty zone (testZone). Also ensure that
2763 //! there are movable and free pages in more preferable zones than the test zone. Following this call a general
2765 //! 6. Set up memory so that the mostPrefZone contains movable and free pages. Allocate 1 movable and 1 fixed
2766 //! page into an empty zone (testZone). Following this call a general defrag.
2768 //! @SYMTestExpectedResults
2769 //! 1. Second defrag does not empty any zones
2770 //! 2. No zones have been emptied
2771 //! 3. Zones are emptied if the general defrag can succeed
2772 //! 4. Defrag fills up the less preferable zones first
2773 //! 5. movable pages are moved from the testZone
2774 //! 6. testZone is not emptied
2775 //---------------------------------------------------------------------------------------------------------------------
2776 TInt TestMovPgsDefrag()
2780 test.Start(_L("Test1: Performing a defrag twice"));
2783 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
2784 FreeMovable(gChunkArray1, gChunkArraySize1);
2786 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
2787 TESTDEBUG(test.Printf(_L("After calling general defrag r = %d\n"), r));
2789 r = VerifyMovDisAlloc();
2790 // The first defrag should empty zones if the general can succeed
2793 test.Printf(_L("Fail: r = %d, memory is not laid out as expected\n"), r);
2794 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
2798 GetOriginalPageCount();
2799 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
2801 TESTDEBUG(test.Printf(_L("After calling general defrag again r = %d\n"), r));
2803 // The second call to general defrag should have nothing further to do
2804 if (r != KErrNone || CheckZonesSwitchedOff() != EFalse)
2806 test.Printf(_L("Fail: r = %d, expected = %d, or zones have been switched off \n"),
2808 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
2813 test.Printf(_L("Passed...\n"));
2816 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
2821 test.Next(_L("Test2: Ensure new memory allocations can occur successfully during a general defrag"));
2824 // Create a thread to call Defrag continuously and resume it
2825 r = gTestThread.Create(gTestThreadName,MultiLoopGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
2828 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
2831 gTestThread.Logon(status);
2833 gTestThread.Resume();
2835 // Whilst the defrag loop is taking place, continuously allocate and free memory
2836 for (TInt i = 0; i < 100; i++)
2838 r = AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
2841 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
2844 User::WaitForRequest(status);
2845 TESTDEBUG(test.Printf(_L("defrag running on another thread returns %d\n"), r));
2847 if (r != KErrNone || status.Int() != KErrNone)
2849 test.Printf(_L("Fail: r = %d, status.Int() = %d\n"), r, status.Int());
2850 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
2855 test.Printf(_L("Passed...\n"));
2858 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
2859 RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
2860 gTestThread.Close();
2864 test.Next(_L("Test3: Check whether RAM defrag switches off any zones"));
2867 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
2868 FreeMovable(gChunkArray1, gChunkArraySize1);
2870 GetOriginalPageCount();
2871 TBool genSucceed = CanGenSucceed();
2872 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
2874 TESTDEBUG(test.Printf(_L("After calling zone defrag r = %d\n"), r));
2876 if (r != KErrNone || (genSucceed && CheckZonesSwitchedOff() == EFalse))
2878 test.Printf(_L("Fail: No zones were switched off or r = %d, expected = %d\n"), r, KErrNone);
2879 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
2884 test.Printf(_L("Passed...\n"));
2887 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
2891 test.Next(_L("Test4: Verify that a general defrag moves pages to the least preferable RAM zone in use"));
2893 gChunkArray1 = new RChunk;
2894 gChunkArraySize1 = 1;
2895 // Get the preference order
2898 TUint mostPrefIndex = 0;
2899 TUint mostPrefArrayIndex = 0;
2901 TUint lastPrefIndex = 0;
2902 TUint lastPrefArrayIndex = 0;
2905 TUint totFreeInTestZones = 0;
2906 TBool zoneNotEmptyOrFull = EFalse;
2909 // Find the first most pref zone that has free pages in it
2910 r = FindMostPrefWithFree(mostPrefIndex, &mostPrefArrayIndex);
2913 test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
2917 // Find the next most preferable free zone
2918 lastPrefArrayIndex = mostPrefArrayIndex + 1;
2920 while (lastPrefArrayIndex < gZoneCount &&
2921 gZoneUtilArray[gPrefArray[lastPrefArrayIndex]].iFreePages != gZoneUtilArray[gPrefArray[lastPrefArrayIndex]].iPhysPages)
2923 lastPrefArrayIndex++;
2926 if (lastPrefArrayIndex >= gZoneCount)
2928 test.Printf(_L("Skipping...\n"));
2932 // Block all other zones
2933 for (TUint prefIndex = lastPrefArrayIndex + 1; prefIndex < gZoneCount; prefIndex++)
2935 TUint zoneIndex = gPrefArray[prefIndex];
2936 Ldd.SetZoneFlag(gZoneConfigArray[zoneIndex].iZoneId, gZoneConfigArray[zoneIndex].iFlags, NO_ALLOC_FLAG);
2940 // Zone alloc to fill half of the last zone under test
2942 lastPrefIndex = gPrefArray[lastPrefArrayIndex];
2943 r = Ldd.ZoneAllocDiscontiguous(gZoneConfigArray[lastPrefIndex].iZoneId, gZoneUtilArray[lastPrefIndex].iFreePages >> 1);
2946 test.Printf(_L("Failed to allocate fixed pages r = %d - Skipping...\n"), r);
2951 // Go through the zones under test and determine the number of free pages
2953 for(TUint prefIndex = mostPrefArrayIndex; prefIndex <= lastPrefArrayIndex; prefIndex++)
2955 TUint zoneIndex = gPrefArray[prefIndex];
2956 totFreeInTestZones += gZoneUtilArray[zoneIndex].iFreePages;
2959 // Allocate movable pages to almost fill zones under test
2960 movPages = totFreeInTestZones;
2961 movBytes = movPages << gPageShift;
2962 while (movBytes && AllocMovable(gChunkArray1, gChunkArraySize1, 1, movBytes) != KErrNone)
2964 movBytes -= gPageSize;
2969 test.Printf(_L("Failed to allocate 0x%x movable pages - Skipping...\n"), movPages);
2974 // Free the allocated fixed pages
2975 Ldd.FreeAllFixedPages();
2977 // Reset all the flags
2978 ResetRamZoneFlags();
2980 // Check that the most preferable zone still has movable pages in it
2981 // and also check that the last zone under test has free pages in it
2983 if (gTotalPageCount.iMovablePages < movPages ||
2984 gZoneUtilArray[mostPrefIndex].iAllocMovable == 0 ||
2985 gZoneUtilArray[lastPrefIndex].iFreePages == 0)
2987 test.Printf(_L("Setup failed - Skipping...\n"));
2991 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
2992 // Check that defrag allocated the movable from the less preferable to the more preferable
2994 totMov = gTotalPageCount.iMovablePages;
2995 for(TInt prefIndex = gZoneCount - 1; prefIndex >= 0 ; prefIndex--)
2997 TUint zoneIndex = gPrefArray[prefIndex];
2998 GetCandList1(zoneIndex);
2999 TUint candIndex = 0;
3000 for (; candIndex < gZoneCount; candIndex++)
3001 {// Check all the zones of this preference
3002 TInt zoneIndexCand = gCandList1[candIndex];
3003 if (zoneIndexCand == KInvalidCandIndex)
3007 totMov -= gZoneUtilArray[zoneIndexCand].iAllocMovable;
3008 if (gZoneUtilArray[zoneIndexCand].iFreePages != gZoneUtilArray[zoneIndexCand].iPhysPages &&
3009 gZoneUtilArray[zoneIndexCand].iFreePages != 0)
3011 zoneNotEmptyOrFull = ETrue;
3014 prefIndex -= candIndex - 1 ;
3016 if (zoneNotEmptyOrFull && totMov != 0)
3018 test.Printf(_L("FAIL: index = %d free = 0x%x totMov = 0x%x\n"),
3019 zoneIndex, gZoneUtilArray[zoneIndex].iFreePages, totMov);
3020 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
3021 CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
3030 test.Next(_L("Test5: Verify that a general defrag tidies up if RAM zone to be emptied contains fixed pages"));
3032 gChunkArray1 = new RChunk;
3033 gChunkArraySize1 = 1;
3034 gChunkArray2 = new RChunk;
3035 gChunkArraySize2 = 1;
3037 // find 2 empty zones - put 1 movable in 1 of them and 1 movable and 1 fixed in the other
3038 TUint testZoneIndex1;
3039 TUint testZoneIndex2;
3041 TUint numFreeInUse = 0;
3043 r = FindMostPrefEmpty(testZoneIndex1);
3046 test.Printf(_L("Skipping...\n"));
3049 // Allocate 1 movable page to the test zone
3050 r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, testZoneIndex1, 1);
3053 test.Printf(_L("Failed to allocate 1 movable page to zone index %d, r = %d - Skipping...\n"), testZoneIndex1, r);
3057 r = FindMostPrefEmpty(testZoneIndex2);
3060 test.Printf(_L("Skipping...\n"));
3063 // Allocate 1 movable page to the test zone
3064 r = ZoneAllocMovable(gChunkArray2, gChunkArraySize2, testZoneIndex2, 1);
3067 test.Printf(_L("Failed to allocate 1 movable page to zone index %d, r = %d - Skipping...\n"), testZoneIndex2, r);
3071 // Zone alloc to put 1 fixed page last zone under test
3072 testZoneID2 = gZoneConfigArray[testZoneIndex2].iZoneId;
3073 r = Ldd.ZoneAllocContiguous(testZoneID2, gPageSize);
3076 test.Printf(_L("Failed to allocate 1 fixed page to zone %d index, r = %d - Skipping...\n"), testZoneIndex2, r);
3080 // Allocate 2 fixed (as there is 1 in our test zone) to prevent reordering
3082 for (TUint index = 0; index < gZoneCount; index++)
3084 TUint totImmovable = gZoneUtilArray[index].iAllocFixed + gZoneUtilArray[index].iAllocUnknown ;
3085 if (index != testZoneIndex1 &&
3086 index != testZoneIndex2 &&
3087 gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
3090 r = Ldd.ZoneAllocToMany(index, 2 - totImmovable);
3093 test.Printf(_L("Failed to alloc 0x%x fixed to zone index %d r = %d - Skipping...\n"),
3100 // Check that the number of free pages in the other in-use zones >= movable pages in test zone
3102 for (TUint index = 0; index < gZoneCount; index++)
3104 if (index != testZoneIndex1 &&
3105 index != testZoneIndex2 &&
3106 gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages)
3108 numFreeInUse += gZoneUtilArray[index].iFreePages;
3111 if (gZoneUtilArray[testZoneIndex1].iAllocMovable + gZoneUtilArray[testZoneIndex1].iAllocDiscardable +
3112 gZoneUtilArray[testZoneIndex2].iAllocMovable + gZoneUtilArray[testZoneIndex2].iAllocDiscardable > numFreeInUse ||
3113 gZoneUtilArray[testZoneIndex2].iAllocFixed == 0)
3115 test.Printf(_L("Setup failed - Skipping...\n"));
3119 // Added extra tracing to debug random failures
3122 // Call a general defrag
3123 GetOriginalPageCount();
3124 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
3126 // Check that the defrag tidies up
3128 if(gZoneUtilArray[testZoneIndex1].iAllocMovable ||
3129 gZoneUtilArray[testZoneIndex2].iAllocMovable)
3131 test.Printf(_L("FAIL: testZoneIndex1(%d): mov orig 0x%x cur 0x%x, testZoneIndex2(%d) mov orig 0x%x cur 0x%x \n"),
3132 testZoneIndex1, gOriginalPageCountArray[testZoneIndex1].iAllocMovable, gZoneUtilArray[testZoneIndex1].iAllocMovable,
3133 testZoneIndex2, gOriginalPageCountArray[testZoneIndex2].iAllocMovable, gZoneUtilArray[testZoneIndex2].iAllocMovable);
3134 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
3135 CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
3136 CLEANUP(Ldd.FreeAllFixedPages());
3144 test.Next(_L("Test6: Verify that a general defrag will not move pages if RAM zone to be emptied contains fixed pages"));
3146 gChunkArray1 = new RChunk;
3147 gChunkArraySize1 = 1;
3148 // Find the first most pref zone that has free pages in it
3149 r = FindMostPrefWithFree(mostPrefIndex, &mostPrefArrayIndex);
3152 test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
3156 // Find the next most preferable free zone
3157 lastPrefArrayIndex = mostPrefArrayIndex + 1;
3158 while (lastPrefArrayIndex < gZoneCount &&
3159 gZoneUtilArray[gPrefArray[lastPrefArrayIndex]].iFreePages != gZoneUtilArray[gPrefArray[lastPrefArrayIndex]].iPhysPages)
3161 lastPrefArrayIndex++;
3163 if (lastPrefArrayIndex >= gZoneCount)
3165 test.Printf(_L("Skipping...\n"));
3170 // Zone alloc to put 1 fixed page last zone under test
3171 lastPrefIndex = gPrefArray[lastPrefArrayIndex];
3172 r = Ldd.ZoneAllocDiscontiguous(gZoneConfigArray[lastPrefIndex].iZoneId, 1);
3175 test.Printf(_L("Failed to allocate 1 fixed page to zone index %d r = %d - Skipping...\n"), lastPrefIndex, r);
3180 if (!gZoneUtilArray[mostPrefIndex].iFreePages)
3182 test.Printf(_L("Not enough space in zone under test mostPrefIndex = %d - Skipping...\n"), mostPrefIndex);
3186 // Block all zones apart from the last zone under test from allocation
3187 for (TUint prefIndex = 0; prefIndex < gZoneCount; prefIndex++)
3189 TUint zoneIndex = gPrefArray[prefIndex];
3190 if (zoneIndex != lastPrefIndex)
3192 Ldd.SetZoneFlag(gZoneConfigArray[zoneIndex].iZoneId, gZoneConfigArray[zoneIndex].iFlags, NO_ALLOC_FLAG);
3196 TESTDEBUG(test.Printf(_L("mostPrefIndex = %d lastPrefIndex = %d\n"), mostPrefIndex, lastPrefIndex));
3198 // Allocate movable pages to the lastPrefZone that will fit into the most pref zone
3199 movPages = gZoneUtilArray[mostPrefIndex].iFreePages;
3200 movBytes = movPages << gPageShift;
3201 while (movBytes && AllocMovable(gChunkArray1, gChunkArraySize1, 1, movBytes) != KErrNone)
3203 movBytes -= gPageSize;
3207 test.Printf(_L("Failed to allocate 0x%x movable pages r = %d - Skipping...\n"), movPages, r);
3211 // Reset all the flags
3212 ResetRamZoneFlags();
3215 // Check that the number of movable pages in the least pref will fit
3216 // into the most preferable zone
3218 if (gZoneUtilArray[lastPrefIndex].iAllocMovable > gZoneUtilArray[mostPrefIndex].iFreePages)
3220 test.Printf(_L("Setup failed - Skipping...\n"));
3225 // Added extra tracing to debug random failures
3228 // Call a general defrag
3229 GetOriginalPageCount();
3230 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
3232 // Check that the defrag didn't move any pages from the least pref zone
3234 if(gOriginalPageCountArray[lastPrefIndex].iAllocMovable > gZoneUtilArray[lastPrefIndex].iAllocMovable)
3236 test.Printf(_L("FAIL: mostPref(index %d): origMov = 0x%x curMov = 0x%x, lastPref(index %d): origMov = 0x%x curMov = 0x%x \n"),
3237 mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocMovable, gZoneUtilArray[mostPrefIndex].iAllocMovable,
3238 lastPrefIndex, gOriginalPageCountArray[lastPrefIndex].iAllocMovable, gZoneUtilArray[lastPrefIndex].iAllocMovable);
3239 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
3240 CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
3241 CLEANUP(Ldd.FreeAllFixedPages());
3253 // GenDefragTest123Setup
3255 // Used to set up the memory ready for testing in TestGenDefrag, Test Steps 1, 2, 3 and 4
3257 // @param aMovAlloc The number of movable pages to allocate.
3258 // @param aFixAlloc The number of fixed pages to allocate.
3259 // @param aDisAlloc The number of discardable pages to allocate.
3260 // @param aMovIndex On return contains the test zone index.
3261 // @param aNumFreeInUse On return contains the number of free pages in the inuse zones.
3262 // @param aTestNo The test number that is using this setup.
3264 TInt GenDefragTest123Setup(TUint aMovAlloc, TUint aFixAlloc, TUint aDisAlloc, TUint& aMovIndex, TUint& aNumFreeInUse, TUint aTestNo)
3268 const TUint KFillUpTo = 5; // Rough estimate of how full the in-use zones should be
3269 const TUint KSpaceNeeded = 2;
3270 TUint movPrefIndex = 0;
3272 {// Test 4 we need to look for the LEAST preferable RAM zone which is empty
3273 TUint mostPrefIndex;
3274 r = FindLeastPrefEmpty(aMovIndex, &movPrefIndex);
3275 TInt r2 = FindMostPrefEmpty(mostPrefIndex);
3276 if (r != KErrNone || r2 != KErrNone || !gPagedRom ||
3277 gZoneConfigArray[aMovIndex].iPref == gZoneConfigArray[mostPrefIndex].iPref)
3279 test.Printf(_L("r %d r2 %d or not a paged ROM or Equal zone preferences- Skipping test step...\n"),
3285 {// All other tests look the MOST preferable RAM zone which is empty
3286 r = FindMostPrefEmpty(aMovIndex, &movPrefIndex);
3287 if (r != KErrNone || !gPagedRom)
3289 test.Printf(_L("No suitable RAM zone found or not a paged ROM - Skipping test step...\n"));
3294 // Go through all the in-use zones and check how many free pages there are
3295 // Allocate fixed so there is only KFillUpTo pages in each in-use zone to
3296 // ensure that the total number of free pages in the in-use zones can fit into
3297 // the zone under test
3299 for (TUint index = 0; index < gZoneCount; index++)
3301 if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
3302 gZoneUtilArray[index].iFreePages > KFillUpTo)
3304 r = Ldd.ZoneAllocToMany(index, gZoneUtilArray[index].iFreePages - KFillUpTo);
3307 test.Printf(_L("Failed to alloc 0x%x fixed to zone index %d r = %d - Ending setup...\n"),
3308 gZoneUtilArray[index].iFreePages - KFillUpTo, index, r);
3315 // If any of the in-use zones doesn't contain any fixed and unknown pages allocate one fixed
3316 for (TUint index = 0; index < gZoneCount; index++)
3318 if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
3319 !gZoneUtilArray[index].iAllocFixed && !gZoneUtilArray[index].iAllocUnknown)
3321 r = Ldd.ZoneAllocToMany(index, 1);
3324 test.Printf(_L("Failed to alloc 0x%x fixed to zone index %d r = %d - Ending setup...\n"),
3332 // Check the total number of free pages in the in-use zones
3333 for (TUint index = 0; index < gZoneCount; index++)
3335 if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages)
3337 aNumFreeInUse += gZoneUtilArray[index].iFreePages;
3340 TESTDEBUG(test.Printf(_L("aNumFreeInUse = 0x%x\n"), aNumFreeInUse));
3342 test.Printf(_L("aMovIndex = %d\n"), aMovIndex);
3343 if (aNumFreeInUse < KSpaceNeeded)
3345 test.Printf(_L("RAM zone to be tested is too full - Skipping test step...\n"));
3350 // Ensure that there are movable pages in the zone under test
3351 // ensuring that there is also room for discardable pages
3353 if (aMovAlloc != 0 && gZoneUtilArray[aMovIndex].iAllocMovable == 0)
3355 // Allocate just aMovAlloc movable pages
3356 TUint movPagesAlloc = aMovAlloc;
3357 if (aTestNo == 2 || aTestNo == 4)
3358 {// Tests 2 and 4 require that movable pages can't be moved from zone
3359 movPagesAlloc = aNumFreeInUse + 1;
3360 if (movPagesAlloc > gZoneUtilArray[aMovIndex].iFreePages)
3362 test.Printf(_L("Insufficiant space in zone %d to allocate 0x%x pages - Ending setup...\n"),
3363 aMovIndex, movPagesAlloc);
3368 r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, aMovIndex, movPagesAlloc);
3369 if (gZoneUtilArray[aMovIndex].iAllocMovable != movPagesAlloc ||
3370 gZoneUtilArray[aMovIndex].iFreePages == 0 ||
3372 {// Movable page didn't go into RAM, or RAM zone is full.
3373 test.Printf(_L("Can't complete test with current RAM layout. - Skipping...\n"));
3374 test.Printf(_L("zone mov 0x%x free 0x%x r=%d\n"),
3375 gZoneUtilArray[aMovIndex].iAllocMovable,
3376 gZoneUtilArray[aMovIndex].iFreePages, r);
3381 // Block all other zones from allocation
3382 for(TUint i = 0; i < gZoneCount; i++)
3386 r = Ldd.SetZoneFlag(gZoneConfigArray[i].iZoneId, gZoneConfigArray[i].iFlags, NO_ALLOC_FLAG);
3389 test.Printf(_L("Failed to set flag on zone index %d r = %d- Ending setup...\n"), i, r);
3395 // Now ensure that there are fixed pages in the zone under test
3396 // ensuring that there is also room for discardable pages
3398 if (aFixAlloc != 0 && gZoneUtilArray[aMovIndex].iAllocFixed == 0)
3399 {//Allocate just aFixAlloc fixed pages
3400 r = Ldd.ZoneAllocToMany(aMovIndex, aFixAlloc);
3402 if (gZoneUtilArray[aMovIndex].iAllocMovable == 0 ||
3403 gZoneUtilArray[aMovIndex].iFreePages == 0 ||
3404 gZoneUtilArray[aMovIndex].iAllocFixed == 0 ||
3406 {// Fixed page didn't go into RAM or RAM zone is full.
3407 test.Printf(_L("Can't complete test with current RAM layout. - Skipping...\n"));
3408 test.Printf(_L("zone mov 0x%x fixed 0x%x free 0x%x r=%d\n"),
3409 gZoneUtilArray[aMovIndex].iAllocMovable,
3410 gZoneUtilArray[aMovIndex].iAllocFixed,
3411 gZoneUtilArray[aMovIndex].iFreePages, r);
3418 // Allocate aDisAlloc number of discardable pages
3419 if (aDisAlloc != 0 && gZoneUtilArray[aMovIndex].iAllocDiscardable == 0)
3420 {//Allocate just aDisAlloc discardable pages
3422 r = ZoneAllocDiscard(aMovIndex, aDisAlloc, discard);
3424 if (r != KErrNone || gZoneUtilArray[aMovIndex].iAllocDiscardable == 0)
3425 {// Discardable page didn't go into RAM or RAM zone is full.
3426 test.Printf(_L("Can't complete test with current RAM layout. - Skipping...\n"));
3427 test.Printf(_L("zone mov 0x%x discardable 0x%x free 0x%x r=%d\n"),
3428 gZoneUtilArray[aMovIndex].iAllocDiscardable,
3429 gZoneUtilArray[aMovIndex].iAllocFixed,
3430 gZoneUtilArray[aMovIndex].iFreePages, r);
3435 // Update the total number of free pages in the in-use zones
3437 for (TUint index = 0; index < gZoneCount; index++)
3439 if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
3442 aNumFreeInUse += gZoneUtilArray[index].iFreePages;
3445 TESTDEBUG(test.Printf(_L("aNumFreeInUse = 0x%x\n"), aNumFreeInUse));
3447 // now reset all the flags
3448 ResetRamZoneFlags();
3455 //---------------------------------------------------------------------------------------------------------------------
3456 //! @SYMTestCaseID KBASE-t_ramdefrag-0528
3457 //! @SYMTestType CIT
3458 //! @SYMTestCaseDesc Verifying general scenarios when RAM defrag would take place
3459 //! @SYMPREQ PREQ308
3460 //! @SYMTestPriority High
3462 //! 1. Allocate 1 movable, 1 fixed and 1 discardable pages into the most preferable empty RAM zone
3463 //! ensuring that there is enough free space in the more preferable RAM zones for the
3464 //! movable pages in the RAM zone under test. Following this call a general defrag.
3465 //! 2. Allocate 1 movable and 1 discardable page into into the most preferable empty RAM zone
3466 //! ensuring that there is not enough free space in the more preferable RAM zones for the
3467 //! movable pages in the RAM zone under test. Following this call a general defrag.
3468 //! 3. Allocate 1 movable and 1 discardable and 1 fixed page into the most preferable empty RAM zone(a).
3469 //! Following this, place 1 movable page in an empty zone(b) ensuring that there is enough free space in
3470 //! the more preferable RAM zones for the movable pages in both zones (a) and (b).
3471 //! Following this call a general defrag.
3472 //! 4. Allocate 1 movable and 1 discardable page into the least preferable empty RAM zone
3473 //! ensuring that there is not enough free space in the more preferable RAM zones for the
3474 //! movable pages in the RAM zone under test. Following this call a general defrag.
3475 //! 5. Fragment the memory.
3476 //! Following this allocate discardable pages by loading pages that are demand paged.
3477 //! Read each of the constants from beginning to end.
3478 //! Following this perform a RAM defrag.
3479 //! 6. Call a device driver that will continuously allocate fixed pages to the
3480 //! memory until it reports out of memory. Following this perform a RAM defrag.
3481 //! 7. Defrag memory filled with discardable pages when the min cache size is reached
3482 //! 8. Fragment the memory. Following this, continuously call a general defrag, each time
3483 //! reducing the size of the chunks that are allocated
3485 //! @SYMTestExpectedResults
3486 //! 1. The RAM zone under test is not emptied and all the discardable pages contained in it
3487 //! have not been discarded.
3488 //! 2. The RAM zone under test is not emptied and all the discardable pages contained in it
3489 //! have not been discarded.
3490 //! 3. RAM zones (a) and (b) have been emptied and all the discardable pages contained in them
3491 //! have not been discarded.
3492 //! 4. The RAM zone under test is emptied
3493 //! 5. Pages are discarded
3494 //! 6. No zones are emptied
3495 //! 7. The least preferable zone is skipped but the other zones are defragmented as required.
3496 //! 8. Zones are emptied if the general defrag can succeed
3497 //---------------------------------------------------------------------------------------------------------------------
3498 TInt TestGenDefrag()
3502 test.Start(_L("Test1: Test General Defrag doesn't discard pages when fixed page in zone to be emptied"));
3504 gChunkArray1 = new RChunk;
3505 gChunkArraySize1 = 1;
3506 TUint numFreeInUse = 0;
3509 // We just need 1 movable, 1 discardable and 1 fixed in the zone to ensure it cannot be emptied
3510 // as the fixed should block the defrag
3511 TInt movAllocPages = 1;
3512 TInt fixAllocPages = 1;
3513 TInt disAllocPages = 1;
3516 if (GenDefragTest123Setup(movAllocPages, fixAllocPages, disAllocPages, zoneIndex, numFreeInUse, testNo) != KErrNone)
3518 test.Printf(_L("Setup failed - Skipping..\n"));
3522 // Call a general defrag and check that no discardable pages were freed.
3523 GetOriginalPageCount();
3524 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
3526 if (r != KErrNone ||
3527 gZoneUtilArray[zoneIndex].iAllocMovable < gOriginalPageCountArray[zoneIndex].iAllocMovable ||
3528 gOriginalPageCountArray[zoneIndex].iAllocDiscardable > gZoneUtilArray[zoneIndex].iAllocDiscardable)
3530 test.Printf(_L("Fail: r = %d zoneIndex %d origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), r, zoneIndex,
3531 gOriginalPageCountArray[zoneIndex].iAllocMovable, gZoneUtilArray[zoneIndex].iAllocMovable,
3532 gOriginalPageCountArray[zoneIndex].iAllocDiscardable, gZoneUtilArray[zoneIndex].iAllocDiscardable);
3533 CLEANUP(Ldd.FreeAllFixedPages());
3538 test.Printf(_L("Passed...\n"));
3542 // TestEnd() will perform any required clean up.
3545 test.Next(_L("Test2: Test General Defrag doesn't discard pages when zone can't be emptied"));
3547 gChunkArray1 = new RChunk;
3548 gChunkArraySize1 = 1;
3550 // No fixed, but there should be no space for the movable to go to
3551 // GenDefragTest123Setup(), will ensure that this is the case
3552 // by allocating 1 more movable page than there is free in the in-use zones
3557 if (GenDefragTest123Setup(movAllocPages, fixAllocPages, disAllocPages, zoneIndex, numFreeInUse, testNo) != KErrNone)
3559 test.Printf(_L("Setup failed - Skipping..\n"));
3563 // Call a general defrag and check that no discardable pages were freed.
3564 GetOriginalPageCount();
3565 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
3568 if (r != KErrNone ||
3569 gZoneUtilArray[zoneIndex].iAllocMovable < gOriginalPageCountArray[zoneIndex].iAllocMovable ||
3570 gOriginalPageCountArray[zoneIndex].iAllocDiscardable > gZoneUtilArray[zoneIndex].iAllocDiscardable)
3572 test.Printf(_L("Fail: r = %d zoneIndex %d origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), r, zoneIndex,
3573 gOriginalPageCountArray[zoneIndex].iAllocMovable, gZoneUtilArray[zoneIndex].iAllocMovable,
3574 gOriginalPageCountArray[zoneIndex].iAllocDiscardable, gZoneUtilArray[zoneIndex].iAllocDiscardable);
3575 CLEANUP(Ldd.FreeAllFixedPages());
3580 test.Printf(_L("Passed...\n"));
3584 // TestEnd() will perform any required clean up.
3587 test.Next(_L("Test3: Test General Defrag tidies when fixed page in zone to be emptied but other can be emptied"));
3589 gChunkArray1 = new RChunk;
3590 gChunkArraySize1 = 1;
3591 gChunkArray2 = new RChunk;
3592 gChunkArraySize2 = 1;
3596 // First use GenDefragTest123Setup() to ensure we place 1 fixed, 1 movable and 1 discardable
3597 // in the least preferable zone
3601 TBool zoneFound = EFalse;
3602 TUint emptyZoneIndex = 0;
3604 if (GenDefragTest123Setup(movAllocPages, fixAllocPages, disAllocPages, zoneIndex, numFreeInUse, testNo) != KErrNone)
3606 test.Printf(_L("Setup failed - Skipping..\n"));
3610 // Find an empty zone and place a movable page in it
3612 for (TUint i = 0; i < gZoneCount; i++)
3614 if (gZoneUtilArray[i].iFreePages == gZoneUtilArray[i].iPhysPages)
3623 test.Printf(_L("Can't find empty zone - Skipping..\n"));
3627 // Allocate 1 movable page into the empty zone
3628 r = ZoneAllocMovable(gChunkArray2, gChunkArraySize2, emptyZoneIndex, 1);
3630 if (r != KErrNone ||
3631 gZoneUtilArray[emptyZoneIndex].iAllocMovable == 0 ||
3632 gZoneUtilArray[emptyZoneIndex].iAllocFixed != 0)
3634 test.Printf(_L("Movable pages not allocated or fixed pages allocated. - Skipping...\n"));
3635 test.Printf(_L("zone mov 0x%x free 0x%x r =% d\n"),
3636 gZoneUtilArray[emptyZoneIndex].iAllocMovable,
3637 gZoneUtilArray[emptyZoneIndex].iFreePages, r);
3641 // Check that the amount we are allocating can actually be moved into the in-use zones
3642 totalToMove = gZoneUtilArray[zoneIndex].iAllocMovable + gZoneUtilArray[zoneIndex].iAllocDiscardable +
3643 gZoneUtilArray[emptyZoneIndex].iAllocMovable + gZoneUtilArray[emptyZoneIndex].iAllocDiscardable;
3645 // Check the total number of free pages in the in-use zones
3647 for (TUint index = 0; index < gZoneCount; index++)
3649 if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
3650 index != zoneIndex && index != emptyZoneIndex)
3652 numFreeInUse += gZoneUtilArray[index].iFreePages;
3655 if(numFreeInUse < totalToMove)
3657 test.Printf(_L("No space to move pages numFreeInUse = 0x%x totalToMove = 0x%x - Skipping..\n"),
3658 numFreeInUse, totalToMove);
3662 // Call a general defrag and check that zone is emptied.
3663 GetOriginalPageCount();
3664 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
3667 if (r != KErrNone ||
3668 gZoneUtilArray[zoneIndex].iAllocMovable ||
3669 gZoneUtilArray[zoneIndex].iAllocDiscardable ||
3670 gZoneUtilArray[emptyZoneIndex].iAllocMovable)
3672 test.Printf(_L("Fail: r = %d zoneIndex %d origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), r, zoneIndex,
3673 gOriginalPageCountArray[zoneIndex].iAllocMovable, gZoneUtilArray[zoneIndex].iAllocMovable,
3674 gOriginalPageCountArray[zoneIndex].iAllocDiscardable, gZoneUtilArray[zoneIndex].iAllocDiscardable);
3675 CLEANUP(Ldd.FreeAllFixedPages());
3680 test.Printf(_L("Passed...\n"));
3684 // TestEnd() will perform any required clean up.
3687 test.Next(_L("Test4: Test General Defrag moves pages into the next most pref if they don't fit into the most pref"));
3689 gChunkArray1 = new RChunk;
3690 gChunkArraySize1 = 1;
3693 // No fixed, but there should be no space for the movable to go to
3694 // GenDefragTest123Setup(), will ensure that this is the case
3695 // by allocating 1 more movable page than there is free in the in-use zones
3700 if (GenDefragTest123Setup(movAllocPages, fixAllocPages, disAllocPages, zoneIndex, numFreeInUse, testNo) != KErrNone)
3702 test.Printf(_L("Setup failed - Skipping..\n"));
3706 // Call a general defrag and check that the test zone is emptied.
3707 GetOriginalPageCount();
3708 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
3711 if (r != KErrNone ||
3712 gZoneUtilArray[zoneIndex].iFreePages != gZoneUtilArray[zoneIndex].iPhysPages)
3714 test.Printf(_L("Fail: r = %d zoneIndex %d origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), r, zoneIndex,
3715 gOriginalPageCountArray[zoneIndex].iAllocMovable, gZoneUtilArray[zoneIndex].iAllocMovable,
3716 gOriginalPageCountArray[zoneIndex].iAllocDiscardable, gZoneUtilArray[zoneIndex].iAllocDiscardable);
3717 CLEANUP(Ldd.FreeAllFixedPages());
3722 test.Printf(_L("Passed...\n"));
3726 // TestEnd() will perform any required clean up.
3729 test.Next(_L("Test5: Defrag memory filled with discardable pages\n"));
3733 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
3734 FreeMovable(gChunkArray1, gChunkArraySize1);
3737 TInt discardablePages;
3740 r = AllocDiscardable(discardablePages);
3743 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
3744 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
3747 TESTDEBUG(test.Printf(_L("Number of discardable pages = 0x%x\n"), discardablePages >> gPageShift));
3749 GetOriginalPageCount();
3750 TBool genSucceed = CanGenSucceed();
3751 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
3752 TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
3756 test.Printf(_L("Fail: r = %d, expected = %d\n"),r,KErrNone);
3757 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
3760 else if (genSucceed && CheckZonesSwitchedOff() == EFalse)
3762 test.Printf(_L("No Zones Switched off and should have been\n"));
3763 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
3768 test.Printf(_L("Passed...\n"));
3771 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
3776 test.Printf(_L("Not a paged ROM - Skipping test step\n"));
3781 test.Next(_L("Test6: Defrag memory filled with fixed pages"));
3784 TESTDEBUG(test.Printf(_L("Filling memory with fixed pages, r = %d\n")));
3785 r = Ldd.AllocateFixed(FILL_ALL_FIXED);
3787 GetOriginalPageCount();
3788 TBool genSucceed = CanGenSucceed();
3789 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
3790 TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
3792 // The zones should be full of fixed pages so the general should do nothing.
3793 if (r != KErrNone ||
3795 CheckZonesSwitchedOff())
3797 test.Printf(_L("Fail: r = %d, expected = %d, or zone have been emptied\n"), r, KErrNone);
3798 CLEANUP(Ldd.FreeAllFixedPages());
3803 test.Printf(_L("Passed...\n"));
3805 Ldd.FreeAllFixedPages();
3808 test.Next(_L("Test7: Defrag memory filled with discardable pages when the min cache size is reached\n"));
3812 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
3813 FreeMovable(gChunkArray1, gChunkArraySize1);
3816 TInt discardablePages;
3819 r = AllocDiscardable(discardablePages);
3822 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
3823 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
3826 TESTDEBUG(test.Printf(_L("Number of discardable pages = 0x%x\n"), discardablePages >> gPageShift));
3828 TUint minCacheSize = 0;
3829 TUint maxCacheSize = 0;
3830 TUint currentCacheSize = 0;
3832 DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
3834 TUint setMin = maxCacheSize;
3835 TUint64 setMax = maxCacheSize;
3836 TInt r = DPTest::SetCacheSize(setMin, setMax);
3839 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
3840 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
3841 CLEANUP(ResetDPCache());
3844 TESTDEBUG(test.Printf(_L("After SetCacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x\n"),
3845 setMin >> gPageShift, setMax >> gPageShift));
3847 GetOriginalPageCount();
3848 genSucceed = CanGenSucceed();
3849 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
3850 TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
3854 test.Printf(_L("Fail: r = %d, expected = %d\n"),r,KErrNone);
3855 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
3856 CLEANUP(ResetDPCache());
3859 else if (genSucceed && CheckZonesSwitchedOff() == EFalse)
3861 test.Printf(_L("No Zones Switched off and should have been\n"));
3862 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
3863 CLEANUP(ResetDPCache());
3868 test.Printf(_L("Passed...\n"));
3871 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
3878 test.Printf(_L("Not a paged ROM - Skipping test step\n"));
3882 test.Next(_L("Test8: Defrag fragmented memory for various smaller chunk sizes"));
3883 TInt chunkSize = 0x80000;
3884 while (chunkSize >= 0x4000)
3886 test.Printf(_L("chunkSize = %dKB\n"), chunkSize/1024);
3888 r = AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable, chunkSize, EFalse);
3889 r = FreeMovable(gChunkArray1, gChunkArraySize1);
3891 GetOriginalPageCount();
3892 TBool genSucceed = CanGenSucceed();
3893 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
3894 TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
3898 test.Printf(_L("Fail: r = %d, expected = %d\n"),r,KErrNone);
3899 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
3902 else if (genSucceed && CheckZonesSwitchedOff() == EFalse)
3904 test.Printf(_L("No Zones Switched off and should have been\n"));
3905 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
3910 test.Printf(_L("Passed...\n"));
3913 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
3915 chunkSize = chunkSize>>1;
3918 test.Next(_L("Test9: Defrag fragmented memory "));
3921 r = AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
3922 r = FreeMovable(gChunkArray1, gChunkArraySize1);
3924 GetOriginalPageCount();
3925 genSucceed = CanGenSucceed();
3926 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
3927 TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
3931 test.Printf(_L("Fail: r = %d, expected = %d\n"),r,KErrNone);
3932 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
3935 else if (genSucceed && CheckZonesSwitchedOff() == EFalse)
3937 test.Printf(_L("No Zones Switched off and should have been\n"));
3938 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
3943 test.Printf(_L("Passed...\n"));
3946 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
3955 // TestGetRamZonePageCount
3957 //---------------------------------------------------------------------------------------------------------------------
3958 //! @SYMTestCaseID KBASE-t_ramdefrag-0529
3959 //! @SYMTestType CIT
3960 //! @SYMTestCaseDesc Verifying the implementation of the function GetRamZonePageCount()
3961 //! @SYMPREQ PREQ308
3962 //! @SYMTestPriority High
3964 //! 1. Fragment the memory. Following this, call GetRamZonePageCount() on every zone and verify
3965 //! the values with the test HAL functions.
3966 //! 2. Fragment the memory. Following this, call function with an valid aID
3968 //! @SYMTestExpectedResults
3971 //---------------------------------------------------------------------------------------------------------------------
3972 TInt TestGetRamZonePageCount()
3974 test.Start(_L("Test1: Call GetRamZonePageCount() on every zone one after the other"));
3977 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
3978 FreeMovable(gChunkArray1, gChunkArraySize1);
3980 for (TUint index = 0; index < gZoneCount; index++)
3983 TUint zoneID = gZoneConfigArray[index].iZoneId;
3985 STestUserSidePageCount pageData;
3986 TInt r = Ldd.PageCount(zoneID, &pageData);
3987 TESTDEBUG(test.Printf(_L("Page count function r = %d\n"), r));
3990 test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
3991 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
3995 if (pageData.iFreePages != gZoneUtilArray[index].iFreePages ||
3996 pageData.iFixedPages != gZoneUtilArray[index].iAllocFixed ||
3997 pageData.iMovablePages != gZoneUtilArray[index].iAllocMovable ||
3998 pageData.iDiscardablePages != gZoneUtilArray[index].iAllocDiscardable)
4000 test.Printf(_L("RAM zone page count does not match test HAL page count, Zone %d\n"), zoneID);
4001 test.Printf(_L("PgCnt: free = 0x%x, fixed = 0x%x, movable= 0x%x, discard = 0x%x\n"),
4002 pageData.iFreePages, pageData.iFixedPages, pageData.iMovablePages, pageData.iDiscardablePages);
4003 test.Printf(_L("HalFunc: free = 0x%x, fixed = 0x%x, movable= 0x%x, discard = 0x%x\n"),
4004 gZoneUtilArray[index].iFreePages, gZoneUtilArray[index].iAllocFixed,
4005 gZoneUtilArray[index].iAllocMovable, gZoneUtilArray[index].iAllocDiscardable);
4006 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
4011 test.Printf(_L("Zone %d Passed...\n"), zoneID);
4013 TESTDEBUG(test.Printf(_L("iFreePages = 0x%x, iFixedPages = 0x%x, iMovablePages = 0x%x, iDiscardablePages = 0x%x\n"),
4014 pageData.iFreePages, pageData.iFixedPages, pageData.iMovablePages, pageData.iDiscardablePages));
4016 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
4019 test.Next(_L("Test2: Call GetRamZonePageCount() with an invalid aID "));
4022 TUint zoneID = KInvalidZoneID;
4023 STestUserSidePageCount pageData;
4024 TInt r = Ldd.PageCount(zoneID, &pageData);
4025 TESTDEBUG(test.Printf(_L("Page count function r = %d\n"), r));
4026 if (r != KErrArgument)
4028 test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
4033 test.Printf(_L("Passed...\n"));
4041 const TUint KIndex2Alloc = 10;
4042 const TUint KTestMaxPages = 6;
4045 // DefragMaxPagesSetup
4047 // Arranges the memory layout in preparation for TestDefragRamMaxPages()
4049 // WARNING THIS WILL BE UNRELIABLE IF aMaxPages > KIndex2Alloc
4051 TInt DefragMaxPagesSetup(TInt aPageType, TUint aMaxPages, TUint& aIndex, TUint& aIndex2)
4053 TInt r = KErrNoMemory;
4055 // Get the preference order and determine the 2 most preferable zones
4056 // that are currently not in use.
4058 TUint freeInUsePages = 0;
4059 TBool zonesFound = EFalse;
4060 for (TUint i = 1; i < gZoneCount; i++)
4062 aIndex = gPrefArray[i];
4063 aIndex2 = gPrefArray[i-1];
4064 TUint indexFree = gZoneUtilArray[aIndex].iFreePages;
4065 TUint index2Free = gZoneUtilArray[aIndex2].iFreePages;
4066 if (indexFree == gZoneUtilArray[aIndex].iPhysPages &&
4067 index2Free == gZoneUtilArray[aIndex2].iPhysPages &&
4068 indexFree >= aMaxPages && index2Free >= KIndex2Alloc &&
4069 freeInUsePages >= KIndex2Alloc + aMaxPages)
4074 freeInUsePages += index2Free;
4077 // Could suitable RAM zones be found.
4080 test.Printf(_L("Insufficient memory - Skipping test...\n"));
4081 return KErrNoMemory;
4084 if (aPageType == BEST_MOVABLE)
4086 // Allocate KIndex2Alloc movable pages to aIndex2
4087 r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, aIndex2, KIndex2Alloc);
4090 test.Printf(_L("Insufficient memory - Skipping test...\n"));
4097 // Now allow movable pages to be allocated into the least preferable RAM zone under test only.
4099 if (aMaxPages >= gZoneUtilArray[aIndex].iFreePages)
4101 test.Printf(_L("Insufficient memory available - Skipping test...\n"));
4105 // Allocate aMaxPages movable pages to aIndex
4106 r = ZoneAllocMovable(gChunkArray2, gChunkArraySize2, aIndex, aMaxPages);
4109 test.Printf(_L("Insufficient memory - Skipping test...\n"));
4114 // Determine how many free pages there are in the RAM zones more preferable
4115 // than the RAM zones under test.
4118 for (TUint i = 0; i < gZoneCount; i++)
4120 TUint tmpIndex = gPrefArray[i];
4121 if (tmpIndex == aIndex2)
4123 freeInUsePages += gZoneUtilArray[tmpIndex].iFreePages;
4125 // Verify that the RAM layout is still suitable for the test.
4126 if (gZoneUtilArray[aIndex].iAllocMovable != aMaxPages ||
4127 gZoneUtilArray[aIndex2].iAllocMovable != KIndex2Alloc ||
4128 gZoneUtilArray[aIndex].iAllocDiscardable || gZoneUtilArray[aIndex].iAllocFixed ||
4129 gZoneUtilArray[aIndex2].iAllocDiscardable || gZoneUtilArray[aIndex2].iAllocFixed ||
4130 freeInUsePages < KIndex2Alloc + aMaxPages)
4132 test.Printf(_L("Insufficient memory - Skipping test...\n"));
4137 // now reset all the flags
4138 ResetRamZoneFlags();
4140 // Perform a general defrag
4141 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, KTestMaxPages);
4145 if (aPageType == BEST_DISCARDABLE)
4148 TUint minCacheSize = 0;
4149 TUint maxCacheSize = 0;
4150 TUint currentCacheSize = 0;
4151 DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
4152 test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"),
4153 minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift);
4157 // Allocate a number of discardable pages to the 2nd least preferable zone under test
4159 r = ZoneAllocDiscard(aIndex2, KIndex2Alloc, disPages);
4162 test.Printf(_L("ZoneAllocDiscard() r = %d KIndex2Alloc = 0x%x disPages = 0x%x aIndex2 = %d - Skipping test...\n"),
4163 r, KIndex2Alloc, disPages, aIndex2);
4169 TUint discFillBytes = KIndex2Alloc << gPageShift;
4170 r = DPTest::SetCacheSize(currentCacheSize + discFillBytes, currentCacheSize + discFillBytes);
4173 test.Printf(_L("SetCacheSize r = 0x%x currentCacheSize + discFillBytes = 0x%x - Skipping test...\n"),
4174 r, currentCacheSize + discFillBytes);
4182 // Allocate a discardable pages equal to aMaxPages to the least preferable zone under test
4184 if(aMaxPages >= gTotalPageCount.iFreePages)
4186 test.Printf(_L("Insufficient memory available - Skipping test...\n"));
4190 TUint allocPages = aMaxPages + KIndex2Alloc;
4191 r = ZoneAllocDiscard(aIndex, aMaxPages, disPages);
4194 test.Printf(_L("ZoneAllocDiscard() r = %d aMaxPages = 0x%x disPages = 0x%x aIndex = %d - Skipping test...\n"),
4195 r, aMaxPages, disPages, aIndex);
4201 discFillBytes = allocPages << gPageShift;
4202 r = DPTest::SetCacheSize(currentCacheSize + discFillBytes, currentCacheSize + discFillBytes);
4205 test.Printf(_L("SetCacheSize r = %d currentCacheSize + discFillBytes = 0x%x - Skipping test...\n"),
4206 r, currentCacheSize + discFillBytes);
4214 // Determine how many free pages there are in the RAM zones more preferable
4215 // than the RAM zones under test.
4218 for (TUint i = 0; i < gZoneCount; i++)
4220 TUint tmpIndex = gPrefArray[i];
4221 if (tmpIndex == aIndex2)
4223 freeInUsePages += gZoneUtilArray[tmpIndex].iFreePages;
4225 // Verify that the RAM layout is still suitable for the test.
4226 if (r != KErrNone || gZoneUtilArray[aIndex].iAllocDiscardable != aMaxPages ||
4227 gZoneUtilArray[aIndex2].iAllocDiscardable != KIndex2Alloc ||
4228 gZoneUtilArray[aIndex].iAllocMovable || gZoneUtilArray[aIndex].iAllocFixed ||
4229 gZoneUtilArray[aIndex2].iAllocMovable || gZoneUtilArray[aIndex2].iAllocFixed ||
4230 freeInUsePages < KIndex2Alloc + aMaxPages)
4232 test.Printf(_L("Insufficient memory - Skipping test...\n"));
4237 // now reset all the flags
4238 ResetRamZoneFlags();
4240 // Perform a general defrag with maxPages = KTestMaxPages
4241 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, KTestMaxPages);
4246 // Reset all the flags
4247 ResetRamZoneFlags();
4253 // TestDefragRamMaxPages
4255 //---------------------------------------------------------------------------------------------------------------------
4256 //! @SYMTestCaseID KBASE-t_ramdefrag-0530
4257 //! @SYMTestType CIT
4258 //! @SYMTestCaseDesc Verifying the implementation of the function TRamDefragRequest::DefragRam()
4259 //! @SYMPREQ PREQ308
4260 //! @SYMTestPriority High
4262 //! 1. Call function with invalid aMaxPages
4263 //! 2. Call DefragRam when aMaxPages < number of movable pages in least preferable zone
4264 //! 3. Call DefragRam when aMaxPages > number of movable pages in least preferable zone
4265 //! 4. Call DefragRam when aMaxPages = number of movable pages in least preferable zone
4266 //! 5. Call DefragRam when aMaxPages < number of discardable pages in least preferable zone
4267 //! 6. Call DefragRam when aMaxPages > number of discardable pages in least preferable zone
4268 //! 7. Call DefragRam when aMaxPages = number of discardable pages in least preferable zone
4270 //! @SYMTestExpectedResults
4272 //! 2. Least preferable zone and 2nd least preferable zone have not been emptied
4273 //! 3. Least preferable zone has been emptied and 2nd least preferable zone has not been emptied
4274 //! 4. Least preferable zone has been emptied and 2nd least preferable zone not not been emptied
4275 //! 5. Least preferable zone and 2nd least preferable zone have not been emptied
4276 //! 6. Least preferable zone has been emptied and 2nd least preferable zone has not been emptied
4277 //! 7. Least preferable zone has been emptied and 2nd least preferable zone not not been emptied
4278 //---------------------------------------------------------------------------------------------------------------------
4279 TInt TestDefragRamMaxPages()
4281 test.Start(_L("Test1: Call DefragRam with invalid aMaxPages "));
4284 gDefragMaxPages = -1;
4286 TInt r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
4287 TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
4288 if (r != KErrArgument)
4290 test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
4295 test.Printf(_L("Passed...\n"));
4299 test.Next(_L("Test2: Call DefragRam aMaxPages < number of movable pages in least preferable zone "));
4301 gChunkArray1 = new RChunk;
4302 gChunkArraySize1 = 1;
4303 gChunkArray2 = new RChunk;
4304 gChunkArraySize2 = 1;
4307 r = DefragMaxPagesSetup(BEST_MOVABLE, KTestMaxPages + 1, index, index2);
4308 if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
4309 {// Cannot perform test with fixed pages in least preferable zones
4310 test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
4317 if (gZoneUtilArray[index].iAllocMovable < KTestMaxPages + 1 ||
4318 gZoneUtilArray[index2].iAllocMovable < KIndex2Alloc)
4320 test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"),
4321 index, KTestMaxPages + 1, index2, KIndex2Alloc);
4322 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
4323 CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
4324 CLEANUP(Ldd.FreeFromAllZones());
4328 // Double check that if DefragRam is called with maxPages = 0, these zones are emptied
4329 // if there is free space elsewhere
4330 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
4332 if (gTotalPageCount.iFreePages > KIndex2Alloc + KTestMaxPages + 1 &&
4333 (gZoneUtilArray[index].iAllocMovable != 0 || gZoneUtilArray[index2].iAllocMovable != 0))
4335 test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"),
4336 index, 0, index2, 0);
4337 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
4338 CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
4339 CLEANUP(Ldd.FreeFromAllZones());
4344 test.Printf(_L("Passed...\n"));
4349 test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
4351 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
4352 RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
4354 Ldd.FreeFromAllZones();
4358 test.Next(_L("Test3: Call DefragRam aMaxPages > number of movable pages in least preferable zone "));
4360 gChunkArray1 = new RChunk;
4361 gChunkArraySize1 = 1;
4362 gChunkArray2 = new RChunk;
4363 gChunkArraySize2 = 1;
4364 r = DefragMaxPagesSetup(BEST_MOVABLE, KTestMaxPages - 1, index, index2);
4365 if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
4366 {// Cannot perform test with fixed pages in least preferable zones
4367 test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
4374 if (gZoneUtilArray[index].iAllocMovable != 0)
4376 test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"),
4377 index, 0, index2, KIndex2Alloc);
4378 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
4379 CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
4380 CLEANUP(Ldd.FreeFromAllZones());
4384 // Double check that if DefragRam is called with maxPages = 0, these zones are emptied
4385 // if there is free space elsewhere
4386 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
4389 if (gTotalPageCount.iFreePages > KIndex2Alloc + KTestMaxPages - 1 &&
4390 (gZoneUtilArray[index].iAllocMovable != 0 || gZoneUtilArray[index2].iAllocMovable != 0))
4392 test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"),
4393 index, 0, index2, 0);
4394 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
4395 CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
4396 CLEANUP(Ldd.FreeFromAllZones());
4401 test.Printf(_L("Passed...\n"));
4406 test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
4409 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
4410 RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
4412 Ldd.FreeFromAllZones();
4416 test.Next(_L("Test4: Call DefragRam aMaxPages = number of movable pages in least preferable zone "));
4418 gChunkArray1 = new RChunk;
4419 gChunkArraySize1 = 1;
4420 gChunkArray2 = new RChunk;
4421 gChunkArraySize2 = 1;
4422 r = DefragMaxPagesSetup(BEST_MOVABLE, KTestMaxPages, index, index2);
4423 if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
4424 {// Cannot perform test with fixed pages in least preferable zones
4425 test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
4432 if (gZoneUtilArray[index].iAllocMovable != 0)
4434 test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"),
4435 index, 0, index2, KIndex2Alloc);
4436 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
4437 CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
4438 CLEANUP(Ldd.FreeFromAllZones());
4442 // Double check that if DefragRam is called with maxPages = 0, these zones are emptied
4443 // if there is free space elsewhere
4444 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
4447 if (gTotalPageCount.iFreePages > KIndex2Alloc + KTestMaxPages &&
4448 (gZoneUtilArray[index].iAllocMovable != 0 || gZoneUtilArray[index2].iAllocMovable != 0))
4450 test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"),
4451 index, 0, index2, 0);
4452 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
4453 CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
4454 CLEANUP(Ldd.FreeFromAllZones());
4459 test.Printf(_L("Passed...\n"));
4464 test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
4466 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
4467 RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
4469 Ldd.FreeFromAllZones();
4472 test.Next(_L("Test5: Call DefragRam aMaxPages < number of discardable pages in least preferable zone "));
4476 r = DefragMaxPagesSetup(BEST_DISCARDABLE, KTestMaxPages + 1, index, index2);
4477 if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
4478 {// Cannot perform test with fixed pages in least preferable zones
4479 test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
4486 if (gZoneUtilArray[index].iAllocDiscardable < KTestMaxPages + 1 ||
4487 gZoneUtilArray[index2].iAllocDiscardable < KIndex2Alloc)
4489 test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"),
4490 index, KTestMaxPages + 1, index2, KIndex2Alloc);
4491 CLEANUP(ResetDPCache());
4492 CLEANUP(Ldd.FreeFromAllZones());
4496 // Double check that if DefragRam is called with maxPages = 0, these zones are emptied
4497 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
4500 if (gZoneUtilArray[index].iAllocDiscardable != 0 ||
4501 gZoneUtilArray[index2].iAllocDiscardable != 0)
4503 test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"),
4504 index, 0, index2, 0);
4505 CLEANUP(ResetDPCache());
4506 CLEANUP(Ldd.FreeFromAllZones());
4511 test.Printf(_L("Passed...\n"));
4516 test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
4520 Ldd.FreeFromAllZones();
4524 test.Printf(_L("Not a Paged Rom - Skipping...\n"));
4528 test.Next(_L("Test6: Call DefragRam aMaxPages > number of discardable pages in least preferable zone "));
4533 r = DefragMaxPagesSetup(BEST_DISCARDABLE, KTestMaxPages - 1, index, index2);
4535 if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
4536 {// Cannot perform test with fixed pages in least preferable zones
4537 test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
4544 if (gZoneUtilArray[index].iAllocDiscardable != 0||
4545 gZoneUtilArray[index2].iAllocDiscardable != KIndex2Alloc)
4547 test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"),
4548 index, 0, index2, KIndex2Alloc);
4549 CLEANUP(ResetDPCache());
4550 CLEANUP(Ldd.FreeFromAllZones());
4554 // Double check that if DefragRam is called with maxPages = 0, these zones are emptied
4555 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
4557 if (gZoneUtilArray[index].iAllocDiscardable != 0||
4558 gZoneUtilArray[index2].iAllocDiscardable != 0)
4560 test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"),
4561 index, 0, index2, 0);
4562 CLEANUP(ResetDPCache());
4563 CLEANUP(Ldd.FreeFromAllZones());
4568 test.Printf(_L("Passed...\n"));
4573 test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
4577 Ldd.FreeFromAllZones();
4581 test.Printf(_L("Not a Paged Rom - Skipping...\n"));
4586 test.Next(_L("Test4: Call DefragRam aMaxPages = number of discardable pages in least preferable zone "));
4591 r = DefragMaxPagesSetup(BEST_DISCARDABLE, KTestMaxPages, index, index2);
4593 if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
4594 {// Cannot perform test with fixed pages in least preferable zones
4595 test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
4602 if (gZoneUtilArray[index].iAllocDiscardable != 0||
4603 gZoneUtilArray[index2].iAllocDiscardable != KIndex2Alloc)
4605 test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"),
4606 index, 0, index2, KIndex2Alloc);
4607 CLEANUP(ResetDPCache());
4608 CLEANUP(Ldd.FreeFromAllZones());
4612 // Double check that if DefragRam is called with maxPages = 0, these zones are emptied
4613 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
4615 if (gZoneUtilArray[index].iAllocDiscardable != 0||
4616 gZoneUtilArray[index2].iAllocDiscardable != 0)
4618 test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"),
4619 index, 0, index2, 0);
4620 CLEANUP(ResetDPCache());
4621 CLEANUP(Ldd.FreeFromAllZones());
4626 test.Printf(_L("Passed...\n"));
4631 test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
4635 Ldd.FreeFromAllZones();
4639 test.Printf(_L("Not a Paged Rom - Skipping...\n"));
4651 //---------------------------------------------------------------------------------------------------------------------
4652 //! @SYMTestCaseID KBASE-t_ramdefrag-0531
4653 //! @SYMTestType CIT
4654 //! @SYMTestCaseDesc Verifying the implementation of the function TRamDefragRequest::EmptyRamZone()
4655 //! @SYMPREQ PREQ308
4656 //! @SYMTestPriority High
4658 //! 1. Fragment the memory. Call function with a valid aID,
4659 //! i.e. a zone which lies within the range of zones.
4660 //! 2. Fragment the memory. Call function with an invalid aID
4661 //! i.e. a zone which does not exist within the range of zones.
4662 //! 3. Fragment the memory. Following this, call a zone specific defrag on a particular zone.
4663 //! Call the zone specific defrag again on the same zone whilst the other defrag is still
4664 //! running on that zone.
4665 //! 4. Fragment the memory. Following this, call the TRamDefragRequest::DefragRam() to perform general defrag.
4666 //! Following this call zone specific defrag whilst the general defrag is still running.
4667 //! 5. Fragment the memory. Following this, call a zone specific defrag on a particular zone.
4668 //! Whilst the zone defrag is running, call a general defrag
4669 //! 6: Fragment the memory. Following this, call the function on specifc zone and
4670 //! at the same time allocate pages to the zone
4671 //! 7. Fragment the memory. Call EmptyRamZone() on every zone one after the other.
4672 //! 8. Fragment the memory. Allocate a couple of fixed pages to a zone that contains movable pages.
4673 //! Following this, Call EmptyRamZone() on that zone.
4675 //! @SYMTestExpectedResults
4678 //! 3. KErrNone and the zone has been emptied
4679 //! 4. Zone has not been emptied, as the general defrag has already placed pages in the zone
4680 //! 5. Zone has been emptied
4681 //! 6. KErrNone and pages have been allocated in the zone
4682 //! 7. One of the following scenarios should occur:
4683 //! a. If the cache has not reached its minimum size, then all the movable pages should
4684 //! be moved from the zone if there are enough free pages in the other zones.
4685 //! However, when demand paging is off, all movable pages should be moved from the zone, again if there
4686 //! are enough free pages in the other zones.
4687 //! b. If the current size of the cache minus the number of discardable pages in the zone being emptied is
4688 //! not less than the minimum cache size, then all the discardable pages should be discarded of.
4689 //! However, when demand paging is off, all discardable pages should be removed from
4691 //! 8. KErrNoMemory, however all movable and discardable pages have been moved from the zone
4692 //---------------------------------------------------------------------------------------------------------------------
4693 TInt TestEmptyRamZone()
4698 test.Start(_L("Test1: Call EmptyRamZone with valid aID "));
4701 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
4702 FreeMovable(gChunkArray1, gChunkArraySize1);
4705 TUint index = gZoneCount - 1;
4706 while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
4710 TUint defragZoneID = gZoneConfigArray[index].iZoneId;
4711 test.Printf(_L("Zone ID = %d\n"), defragZoneID);
4712 if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
4714 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
4718 r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
4720 TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
4721 if (freeInOthers && (r != KErrNone || !CheckZoneIsOff(index)))
4723 test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
4724 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
4729 test.Printf(_L("Passed...\n"));
4733 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
4737 test.Next(_L("Test2: Call EmptyRamZone with invalid aID "));
4739 gChunkArray1 = new RChunk;
4740 gChunkArraySize1 = 1;
4741 defragZoneID = KInvalidZoneID;
4743 r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
4745 if (r != KErrArgument)
4747 test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
4752 test.Printf(_L("Passed...\n"));
4758 test.Next(_L("Test3: Call EmptyRamZone twice at the same time "));
4761 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
4762 FreeMovable(gChunkArray1, gChunkArraySize1);
4765 index = gZoneCount - 1;
4766 while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
4770 defragZoneID = gZoneConfigArray[index].iZoneId;
4772 if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
4774 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
4778 r = gTestThread.Create(gTestThreadName,MultiEmptyZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny *)defragZoneID);
4781 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
4782 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
4786 gTestThread.Logon(status);
4787 gTestThread.Resume();
4791 r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
4793 User::WaitForRequest(status);
4797 TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
4799 (freeInOthers && (r != KErrNone || r2 != KErrNone || !CheckZoneIsOff(index))))
4801 test.Printf(_L("Fail: r = %d, r2 = %d, expected = %d, or zone %d has not been emptied\n"),
4802 r, r2, KErrNone, defragZoneID);
4803 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
4808 test.Printf(_L("Passed...\n"));
4811 gTestThread.Close();
4814 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
4818 test.Next(_L("Test4: Call general defrag and zone defrag at the same time "));
4821 TInt waitTime = 10000;
4823 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
4824 FreeMovable(gChunkArray1, gChunkArraySize1);
4827 index = gZoneCount - 1;
4828 while (index > 0 && gZoneUtilArray[index].iAllocFixed > 0)
4832 defragZoneID = gZoneConfigArray[index].iZoneId;
4834 if (index == 0 && gZoneUtilArray[index].iAllocFixed > 0)
4836 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
4840 r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
4843 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
4844 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
4847 gTestThread.Logon(status);
4849 gTestThread.Resume();
4850 User::After(waitTime);
4852 TESTDEBUG(test.Printf(_L("Zone defrag running on main thread\n")));
4854 r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
4856 User::WaitForRequest(status);
4860 TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
4861 if (r2 != KErrNone ||
4862 (freeInOthers && (r != KErrNone || !CheckZoneIsOff(index))))
4864 test.Printf(_L("Fail: r = %d, r2 = %d, expected = %d, or zone %d is on unexpectedly\n"),
4865 r, r2, KErrNone, defragZoneID);
4866 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
4871 test.Printf(_L("Passed...\n"));
4874 gTestThread.Close();
4877 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
4881 test.Next(_L("Test5: Call zone defrag and general defrag at the same time "));
4884 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
4885 FreeMovable(gChunkArray1, gChunkArraySize1);
4888 index = gZoneCount - 1;
4889 while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
4893 defragZoneID = gZoneConfigArray[index].iZoneId;
4895 if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
4897 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
4901 r = gTestThread.Create(gTestThreadName,MultiEmptyZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)defragZoneID);
4904 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
4905 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
4908 gTestThread.Logon(status);
4910 gTestThread.Resume();
4913 TESTDEBUG(test.Printf(_L("Zone defrag running on main thread\n")));
4915 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
4917 User::WaitForRequest(status);
4921 TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
4922 if (r != KErrNone ||
4923 (freeInOthers && (r2 != KErrNone || !CheckZoneIsOff(index))))
4925 test.Printf(_L("Fail: r = %d, r2 = %d, expected = %d, or zone 0x%x is on unexpectedly\n"),
4926 r, r2, KErrNone, defragZoneID);
4927 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
4932 test.Printf(_L("Passed...\n"));
4935 gTestThread.Close();
4938 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
4942 test.Next(_L("Test6: Call function TRamDefragRequest::EmptyRamZone on specifc zone and at the same time allocate pages to the zone"));
4945 TInt pagesAlloc = 1; // Try and allocate just one page whilst trying to empty the zone.
4947 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
4948 FreeMovable(gChunkArray1, gChunkArraySize1);
4951 index = gZoneCount - 1;
4952 while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
4956 defragZoneID = gZoneConfigArray[index].iZoneId;
4958 if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
4960 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
4965 for (waitTime = 1000; waitTime > 0; waitTime-=10)
4967 r = gTestThread.Create(gTestThreadName,MultiEmptyZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)defragZoneID);
4970 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
4971 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
4974 gTestThread.Logon(status);
4976 gTestThread.Resume();
4977 User::After(waitTime);
4979 r = Ldd.ZoneAllocDiscontiguous(defragZoneID, pagesAlloc);
4981 User::WaitForRequest(status);
4986 test.Printf(_L("Empty was unsuccsessful: r2 = %d, expected = %d\n"), r2, KErrNone);
4988 else if (r != KErrNone)
4990 test.Printf(_L("Fail: r = %d, expected = %d, r2 = %d, expected = %d, zone = 0x%x\n"),
4991 r, KErrNone, r2, KErrNone, defragZoneID);
4992 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
4993 CLEANUP(Ldd.FreeAllFixedPages());
5001 Ldd.FreeAllFixedPages();
5003 gTestThread.Close();
5007 test.Printf(_L("Passed...\n"));
5011 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
5015 test.Next(_L("Test7: Call function TRamDefragRequest::EmptyRamZone on every zone one after the other"));
5020 for (index = 0; index < gZoneCount; index ++)
5022 // Variables for DP ROM cache sizes
5023 TUint minCacheSize = 0;
5024 TUint maxCacheSize = 0;
5025 TUint currentCacheSize = 0;
5029 DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
5030 TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"),
5031 minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
5032 // Calculate the maximum size available for the DP cache.
5033 TUint minZoneFree = KMaxTUint;
5034 TUint maxDisFillBytes = 0;
5036 for (TUint i = 0; i < gZoneCount; i++)
5038 maxDisFillBytes += gZoneUtilArray[i].iFreePages;
5039 maxDisFillBytes += gZoneUtilArray[i].iAllocDiscardable;
5040 if (minZoneFree > gZoneUtilArray[i].iFreePages)
5041 minZoneFree = gZoneUtilArray[i].iFreePages;
5043 test.Printf(_L("Free pages 0x%x maxDisFillBytes 0x%x\n"), gTotalPageCount.iFreePages, maxDisFillBytes);
5044 maxDisFillBytes <<= gPageShift;
5046 r = DPTest::SetCacheSize(maxDisFillBytes, maxDisFillBytes);
5049 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
5050 CLEANUP(ResetDPCache());
5054 DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
5055 TESTDEBUG(test.Printf(_L("CacheSize2: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"),
5056 minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
5057 if (currentCacheSize != maxDisFillBytes)
5059 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
5060 CLEANUP(ResetDPCache());
5065 test.Printf(_L("Free pages after alloc discardable1 = 0x%x\n"), gTotalPageCount.iFreePages);
5067 r = DPTest::SetCacheSize(minZoneFree << gPageShift, maxDisFillBytes);
5070 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
5071 CLEANUP(ResetDPCache());
5075 // Check all zones have no free pages.
5077 for (TUint i = 0; i < gZoneCount; i++)
5079 if (gZoneUtilArray[i].iFreePages != 0)
5081 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
5082 CLEANUP(ResetDPCache());
5089 // Fragment the RAM with some movable pages
5090 AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
5091 FreeMovable(gChunkArray1, gChunkArraySize1);
5094 TUint zoneID = gZoneConfigArray[index].iZoneId;
5097 TUint origUnknownInZone = gZoneUtilArray[index].iAllocUnknown;
5098 TUint origMovInZone = gZoneUtilArray[index].iAllocMovable;
5099 TUint origFreeInZone = gZoneUtilArray[index].iFreePages;
5101 test.Printf(_L("Zone ID 0x%x - fixedPages = 0x%x, unknownPages = 0x%x, discardablePages = 0x%x movable = 0x%x free = 0x%x\n"),
5102 zoneID, gZoneUtilArray[index].iAllocFixed, origUnknownInZone, gZoneUtilArray[index].iAllocDiscardable,
5103 origMovInZone, origFreeInZone);
5106 r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, zoneID);
5109 TUint unknownPages = gZoneUtilArray[index].iAllocUnknown;
5110 TUint discPages = gZoneUtilArray[index].iAllocDiscardable;
5111 TUint movablePages = gZoneUtilArray[index].iAllocMovable;
5113 TUint freeInOtherZones = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
5114 if(movablePages && freeInOtherZones)
5116 test.Printf(_L("Fail: Zone ID %x all the movable pages haven't been moved\n"), zoneID);
5117 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5118 CLEANUP(ResetDPCache());
5122 if (origUnknownInZone != unknownPages)
5124 test.Printf(_L("Fail: Zone ID %x unknown pages before and after are not equal\n"), zoneID);
5125 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5126 CLEANUP(ResetDPCache());
5132 if (discPages && freeInOtherZones)
5134 test.Printf(_L("Fail: Zone ID %x all the discardable pages haven't been moved\n"), zoneID);
5135 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5136 CLEANUP(ResetDPCache());
5142 DPTest::CacheSize(minCacheSize, maxCacheSize, currentCacheSize);
5143 TESTDEBUG(test.Printf(_L("CacheSize3: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"),
5144 minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
5145 // The the discardable pages should have been discarded or moved unless
5146 // there is no room in the other zones and the cache size is already at its minimum
5147 if (discPages && (freeInOtherZones || currentCacheSize != minCacheSize))
5149 test.Printf(_L("Fail: Zone 0x%x all the discardable pages haven't been moved\n"), zoneID);
5150 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5151 CLEANUP(ResetDPCache());
5155 test.Printf(_L("Passed...\n"));
5157 // TestEnd() willl cleanup what's required
5160 test.Next(_L("Test8: Call function TRamDefragRequest::EmptyRamZone on a zone that contains fixed and movable pages"));
5163 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
5164 FreeMovable(gChunkArray1, gChunkArraySize1);
5167 index = gZoneCount - 1;
5168 while (index > 0 && (gZoneUtilArray[index].iAllocMovable < 10 || gZoneUtilArray[index].iFreePages < 3))
5172 defragZoneID = gZoneConfigArray[index].iZoneId;
5174 if (index == 0 && (gZoneUtilArray[index].iAllocMovable < 10 || gZoneUtilArray[index].iFreePages < 3))
5176 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
5180 test.Printf(_L("defragZoneID = 0x%x\n"), defragZoneID);
5181 test.Printf(_L("movable = 0x%x discardable = 0x%x\n"),
5182 gZoneUtilArray[index].iAllocMovable, gZoneUtilArray[index].iAllocDiscardable);
5185 Ldd.ZoneAllocDiscontiguous(defragZoneID, 2); // Allocated 2 fixed pages to ensure that zone cannot be emptied
5187 r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
5190 TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
5191 if (r != KErrNoMemory ||
5193 (gZoneUtilArray[index].iAllocMovable ||
5194 gZoneUtilArray[index].iAllocDiscardable)))
5196 test.Printf(_L("r = %d, expected = %d, or all movable/discardable pages have not been moved"),
5198 CLEANUP(Ldd.FreeAllFixedPages());
5199 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5204 test.Printf(_L("Passed..."));
5207 Ldd.FreeAllFixedPages();
5210 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
5221 //---------------------------------------------------------------------------------------------------------------------
5222 //! @SYMTestCaseID KBASE-t_ramdefrag-0532
5223 //! @SYMTestType CIT
5224 //! @SYMTestCaseDesc Verifying the implemntation of the function TRamDefragRequest::ClaimRamZone()
5225 //! @SYMPREQ PREQ308
5226 //! @SYMTestPriority High
5228 //! 1. Fragment the memory. Following this, call function with a valid aID
5229 //! 2. Fragment the memory. Following this, call function with an invalid aID
5230 //! 3. Fragment the memory. Following this, start a zone defrag and whilst this
5231 //! is running, call function on the same zone.
5232 //! 4. Fragment the memory. Following this, start a general defrag and whilst this
5233 //! is running, call function on the same zone.
5234 //! 5. Fragment the memory. Following this, call ClaimRamZone() on a specific zone, whilst at the
5235 //! same time calling ClaimRamZone() on another zone.
5236 //! 6: Fragment the memory. Call function TRamDefragRequest::ClaimRamZone on specifc zone and at the
5237 //! same time allocate pages to the zone
5238 //! 7. Fragment the memory. Following this allocate fixed pages to a zone and attempt to claim
5240 //! 8. Fragment the memory. Following this call ClaimRamZone() on every zone, one after the other
5242 //! @SYMTestExpectedResults
5245 //! 3. KErrNone and the zone has been claimed
5246 //! 4. KErrNone and the zone has been claimed
5247 //! 5. KErrNone and both zones have been claimed
5249 //! 7. KErrNoMemory, all movable and discardable pages have been moved
5250 //! 8. One of the following scenarios should occur:
5251 //! a. If the cache has not reached its minimum size and there are no unknown pages in
5252 //! the zone, then all the movable pages should be moved from the zone if there are enough free pages
5253 //! in the other zones.
5254 //! However, when demand paging is off, all movable pages should be moved from the zone, again if there
5255 //! are enough free pages in the other zones and if there are no unknown pages in the zone.
5256 //! b. If the current size of the cache minus the number of discardable pages in the zone being emptied is not
5257 //! less than the minimum cache size and there are no unknown pages in the zone,
5258 //! then all the discardable pages should be discarded of.
5259 //! However, when demand paging is off, all discardable pages should be removed from
5260 //! the zone if there are no unknown pages in the zone.
5261 //---------------------------------------------------------------------------------------------------------------------
5262 TInt TestClaimRamZone()
5267 TInt waitTime = 1000;
5269 test.Start(_L("Test1: Call function TRamDefragRequest::ClaimRamZone with a valid aID"));
5272 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
5273 FreeMovable(gChunkArray1, gChunkArraySize1);
5276 TUint index = gZoneCount - 1;
5277 while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
5281 TUint defragZoneID = gZoneConfigArray[index].iZoneId;
5283 if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
5285 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
5289 r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
5291 TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
5292 if (freeInOthers && r != KErrNone &&
5293 gZoneUtilArray[index].iAllocFixed != gZoneUtilArray[index].iPhysPages)
5295 test.Printf(_L("Fail: Zone 0x%x has not been claimed r %d expected %d\n"), defragZoneID, r, KErrNone);
5296 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5301 test.Printf(_L("Passed...\n"));
5304 addr = gZoneConfigArray[index].iPhysBase;
5307 r = Ldd.FreeAllFixedPages();
5311 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
5315 test.Next(_L("Test2: Call function TRamDefragRequest::ClaimRamZone with an invalid aID"));
5317 gChunkArray1 = new RChunk;
5318 gChunkArraySize1 = 1;
5319 defragZoneID = KInvalidZoneID;
5321 r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
5322 if (r != KErrArgument)
5324 test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
5329 test.Printf(_L("Passed...\n"));
5335 test.Next(_L("Test3: Call function TRamDefragRequest::ClaimRamZone when a EmptyRamZone is already running"));
5338 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
5339 FreeMovable(gChunkArray1, gChunkArraySize1);
5342 index = gZoneCount - 1;
5343 while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
5347 defragZoneID = gZoneConfigArray[index].iZoneId;
5349 if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
5351 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
5356 for (waitTime = 1000; waitTime > 0; waitTime-=10)
5358 r = gTestThread.Create(gTestThreadName,MultiEmptyZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)defragZoneID);
5361 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
5362 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5366 gTestThread.Logon(status);
5367 gTestThread.Resume();
5368 User::After(waitTime);
5370 r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
5372 User::WaitForRequest(status);
5376 TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
5377 if (freeInOthers && r != KErrNone && r2 != KErrNone)
5379 test.Printf(_L("Fail: r = %d, r2 = %d, expected = %d, or zone ID 0x%x has not been claimed\n"),
5380 r, r2, KErrNone, defragZoneID);
5383 CLEANUP(r = Ldd.FreeAllFixedPages());
5385 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5395 r = Ldd.FreeAllFixedPages();
5398 gTestThread.Close();
5402 test.Printf(_L("Passed...\n"));
5405 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
5409 test.Next(_L("Test4: Call function TRamDefragRequest::ClaimRamZone when a general defrag is already running"));
5412 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
5413 FreeMovable(gChunkArray1, gChunkArraySize1);
5416 index = gZoneCount - 1;
5417 while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
5422 if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
5424 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
5428 defragZoneID = gZoneConfigArray[index].iZoneId;
5430 r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
5433 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
5434 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5438 gTestThread.Logon(status);
5439 gTestThread.Resume();
5440 User::After(waitTime);
5442 r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
5444 User::WaitForRequest(status);
5449 test.Printf(_L("ClaimZone: r = %d, expected = %d\n"), r, KErrNone);
5454 test.Printf(_L("General: r2 = %d, expected = %d\n"), r, KErrNone);
5458 TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
5459 if (freeInOthers && gZoneUtilArray[index].iAllocFixed != gZoneUtilArray[index].iPhysPages)
5461 test.Printf(_L("Fail: Zone ID 0x%x has not been claimed, r = %d, r2 = %d, expected = %d\n"),
5462 defragZoneID, r, r2, KErrNone);
5463 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5468 test.Printf(_L("Passed...\n"));
5473 r = Ldd.FreeAllFixedPages();
5476 gTestThread.Close();
5479 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
5484 test.Next(_L("Test5: Call function TRamDefragRequest::ClaimRamZone on specifc zone at the same time as calling on another zone"));
5487 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
5488 FreeMovable(gChunkArray1, gChunkArraySize1);
5491 index = gZoneCount - 1;
5492 while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
5496 defragZoneID = gZoneConfigArray[index].iZoneId;
5497 test.Printf(_L("index = 0x%x "), index);
5499 TUint index2 = gZoneCount - 1;
5500 while ( index2 > 0 &&
5501 (index == index2 || gZoneUtilArray[index2].iAllocFixed != 0 || gZoneUtilArray[index2].iAllocUnknown != 0))
5505 TUint defragZoneID2 = gZoneConfigArray[index2].iZoneId;
5506 test.Printf(_L("index2 = %d\n"), index2);
5508 if ((index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) ||
5509 (index2 == 0 && (index == index2 || gZoneUtilArray[index2].iAllocFixed != 0 || gZoneUtilArray[index2].iAllocUnknown != 0)))
5511 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
5515 r = gTestThread.Create(gTestThreadName,MultiClaimZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)(defragZoneID2));
5518 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
5519 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5523 gTestThread.Logon(status);
5524 gTestThread.Resume();
5525 User::After(waitTime);
5527 r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
5528 TESTDEBUG(test.Printf(_L("r = %d\n"), r));
5530 User::WaitForRequest(status);
5534 TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
5535 TUint freeInOthers2 = gTotalPageCount.iFreePages - gZoneUtilArray[index2].iFreePages;
5536 if ((freeInOthers && r != KErrNone &&
5537 gZoneUtilArray[index].iAllocFixed != gZoneUtilArray[index].iPhysPages) ||
5538 (freeInOthers2 && r != KErrNone &&
5539 gZoneUtilArray[index2].iAllocFixed != gZoneUtilArray[index2].iPhysPages))
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"),
5542 defragZoneID, defragZoneID2, r, r2, KErrNone);
5543 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5548 test.Printf(_L("Passed...\n"));
5553 r = Ldd.FreeAllFixedPages();
5557 {// Have to free from specific address as RAM zone claimed by other channel.
5558 addr = gZoneConfigArray[index2].iPhysBase;
5559 r = Ldd.FreeFromAddr(gZoneUtilArray[index2].iAllocFixed, addr);
5562 gTestThread.Close();
5565 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
5569 test.Next(_L("Test6: Call function TRamDefragRequest::ClaimRamZone on specifc zone and at the same time allocate pages to the zone"));
5572 TInt pagesAlloc = 1; // Try and allocate just one page whilst attempting to claim the zone
5574 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
5575 FreeMovable(gChunkArray1, gChunkArraySize1);
5578 index = gZoneCount - 1;
5579 while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
5583 if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
5585 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
5590 defragZoneID = gZoneConfigArray[index].iZoneId;
5592 for (waitTime = 1000; waitTime > 0; waitTime-=10)
5594 r = gTestThread.Create(gTestThreadName,MultiClaimZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)defragZoneID);
5597 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
5598 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5602 gTestThread.Logon(status);
5603 gTestThread.Resume();
5604 test(status.Int() == KRequestPending);
5605 User::After(waitTime);
5607 r = Ldd.ZoneAllocDiscontiguous(defragZoneID, pagesAlloc);
5609 TESTDEBUG(test.Printf(_L("r = %d\n"), r));
5611 User::WaitForRequest(status);
5615 TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
5616 if (freeInOthers && r2 != KErrNone)
5618 test.Printf(_L("Claim was unsuccsessful: r2 = %d, expected = %d\n"), r2, KErrNone);
5620 else if (r2 == KErrNone && r != KErrNoMemory)
5622 test.Printf(_L("Fail: r = %d, expected = %d, r2 = %d, expected = %d, zone ID = 0x%x\n"),
5623 r, KErrNoMemory, r2, KErrNone, defragZoneID);
5624 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5625 CLEANUP(Ldd.FreeAllFixedPages());
5626 // Free from address as a different channel claimed the RAM zone.
5627 addr = gZoneConfigArray[index].iPhysBase;
5628 CLEANUP(Ldd.FreeFromAddr(gZoneUtilArray[index].iAllocFixed, addr));
5636 Ldd.FreeAllFixedPages();
5640 {// Free from address as a different channel claimed the RAM zone.
5641 addr = gZoneConfigArray[index].iPhysBase;
5642 r = Ldd.FreeFromAddr(gZoneUtilArray[index].iAllocFixed, addr);
5644 gTestThread.Close();
5648 test.Printf(_L("Passed...\n"));
5652 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
5657 test.Next(_L("Test7: Call function TRamDefragRequest::ClaimRamZone on a zone that contains fixed and movable pages"));
5660 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
5661 FreeMovable(gChunkArray1, gChunkArraySize1);
5664 index = gZoneCount - 1;
5665 while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0) &&
5666 (gZoneUtilArray[index].iAllocMovable < 10 || gZoneUtilArray[index].iFreePages < 3))
5671 if (index == 0 && ((gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0) &&
5672 (gZoneUtilArray[index].iAllocMovable < 10 || gZoneUtilArray[index].iFreePages < 3)))
5674 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
5678 defragZoneID = gZoneConfigArray[index].iZoneId;
5679 TESTDEBUG(test.Printf(_L("defragZoneID = 0x%x\n"), defragZoneID));
5680 TESTDEBUG(test.Printf(_L("movable = 0x%x discardable = 0x%x\n"),
5681 gZoneUtilArray[index].iAllocMovable, gZoneUtilArray[index].iAllocDiscardable));
5684 Ldd.ZoneAllocDiscontiguous(defragZoneID, 2); // Allocated 2 fixed pages to ensure that zone cannot be emptied
5686 r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
5688 TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
5689 if (r != KErrNoMemory ||
5691 (gZoneUtilArray[index].iAllocMovable ||
5692 gZoneUtilArray[index].iAllocDiscardable)))
5694 test.Printf(_L("r = %d, expected = %d, or all movable/discardable pages have not been moved"),
5696 CLEANUP(Ldd.FreeAllFixedPages());
5697 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5702 test.Printf(_L("Passed...\n"));
5705 Ldd.FreeAllFixedPages();
5708 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
5711 test.Next(_L("Test8: Call function TRamDefragRequest::ClaimRamZone on every zone one after the other"));
5716 for (index = 0; index < gZoneCount; index ++)
5718 // Variables for DP ROM cache sizes
5719 TUint minCacheSize = 0;
5720 TUint maxCacheSize = 0;
5721 TUint currentCacheSize = 0;
5725 DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
5726 TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"),
5727 minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
5728 // Calculate the maximum size of the DP cache
5729 TUint minZoneFree = KMaxTUint;
5730 TUint maxDisFillBytes = 0;
5732 for (TUint i = 0; i < gZoneCount; i++)
5734 maxDisFillBytes += gZoneUtilArray[i].iFreePages;
5735 maxDisFillBytes += gZoneUtilArray[i].iAllocDiscardable;
5736 if (minZoneFree > gZoneUtilArray[i].iFreePages)
5737 minZoneFree = gZoneUtilArray[i].iFreePages;
5739 test.Printf(_L("Free pages 0x%x maxDisFillBytes 0x%x\n"), gTotalPageCount.iFreePages, maxDisFillBytes);
5740 maxDisFillBytes <<= gPageShift;
5742 r = DPTest::SetCacheSize(maxDisFillBytes, maxDisFillBytes);
5745 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
5746 CLEANUP(ResetDPCache());
5750 DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
5751 TESTDEBUG(test.Printf(_L("CacheSize2: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"),
5752 minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
5753 if (currentCacheSize != maxDisFillBytes)
5755 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
5756 CLEANUP(ResetDPCache());
5761 test.Printf(_L("Free pages after alloc discardable1 = 0x%x\n"), gTotalPageCount.iFreePages);
5763 r = DPTest::SetCacheSize(minZoneFree << gPageShift, maxDisFillBytes);
5766 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
5767 CLEANUP(ResetDPCache());
5771 // Check all zones have no free pages.
5773 for (TUint i = 0; i < gZoneCount; i++)
5775 if (gZoneUtilArray[i].iFreePages != 0)
5777 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
5778 CLEANUP(ResetDPCache());
5785 // Fragment the RAM with some movable pages
5786 AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
5787 FreeMovable(gChunkArray1, gChunkArraySize1);
5790 TUint zoneID = gZoneConfigArray[index].iZoneId;
5793 TUint origUnknownInZone = gZoneUtilArray[index].iAllocUnknown;
5794 TUint origMovInZone = gZoneUtilArray[index].iAllocMovable;
5795 TUint origFreeInZone = gZoneUtilArray[index].iFreePages;
5797 test.Printf(_L("Zone ID 0x%x - fixedPages = 0x%x, unknownPages = 0x%x, discardablePages = 0x%x movable = 0x%x free = 0x%x\n"),
5798 zoneID, gZoneUtilArray[index].iAllocFixed, origUnknownInZone, gZoneUtilArray[index].iAllocDiscardable,
5799 origMovInZone, origFreeInZone);
5802 r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneID);
5805 TUint unknownPages = gZoneUtilArray[index].iAllocUnknown;
5806 TUint discPages = gZoneUtilArray[index].iAllocDiscardable;
5807 TUint movablePages = gZoneUtilArray[index].iAllocMovable;
5811 r = Ldd.FreeAllFixedPages();
5813 TUint freeInOtherZones = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
5814 if (!unknownPages && movablePages && freeInOtherZones)
5816 test.Printf(_L("Fail: Zone ID %x all the movable pages haven't been moved\n"), zoneID);
5817 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5818 CLEANUP(ResetDPCache());
5822 if (origUnknownInZone != unknownPages)
5824 test.Printf(_L("Fail: Zone ID %x unknown pages before and after are not equal\n"), zoneID);
5825 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5826 CLEANUP(ResetDPCache());
5832 if (!unknownPages && freeInOtherZones && discPages)
5834 test.Printf(_L("Fail: Zone ID %x all the discardable pages haven't been moved\n"), zoneID);
5835 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5836 CLEANUP(ResetDPCache());
5842 DPTest::CacheSize(minCacheSize, maxCacheSize, currentCacheSize);
5843 TESTDEBUG(test.Printf(_L("CacheSize3: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"),
5844 minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
5845 // The the discardable pages should have been discarded or moved unless
5846 // there is no room in the other zones and the cache size is already at its minimum
5847 if (!unknownPages && discPages && (freeInOtherZones || currentCacheSize != minCacheSize))
5849 test.Printf(_L("Fail: Zone ID 0x%x all the discardable pages haven't been moved\n"), zoneID);
5850 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5851 CLEANUP(ResetDPCache());
5855 test.Printf(_L("Passed...\n"));
5857 // TestEnd() will cleanup what's required.
5867 //-----------------------------------------------------------------------------------------------------------------------------------------
5868 //! @SYMTestCaseID KBASE-t_ramdefrag-0533
5869 //! @SYMTestType CIT
5870 //! @SYMTestCaseDesc Verifying the implementation of the function TRamDefragRequest::CancelDefrag()
5871 //! @SYMPREQ PREQ308
5872 //! @SYMTestPriority High
5874 //! 1. Fragment the memory. Following this, start a general defrag and cancel it
5875 //! 2. Fragment the memory. Following this, start a zone defrag and cancel it
5876 //! 3. Fragment the memory. Following this, start a claim zone and cancel it
5878 //! @SYMTestExpectedResults
5882 //-----------------------------------------------------------------------------------------------------------------------------------------
5883 TInt TestCancelDefrag()
5886 TUint defragZoneID = 0;
5888 test.Start(_L("Test1: Call general defrag and cancel it"));
5891 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
5892 FreeMovable(gChunkArray1, gChunkArraySize1);
5894 r = Ldd.CheckCancel(DEFRAG_TYPE_GEN);
5896 TESTDEBUG(test.Printf(_L("r = %d\n"), r));
5897 if (r != KErrCancel)
5899 test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrCancel);
5900 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5905 test.Printf(_L("Passed...\n"));
5907 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
5911 test.Next(_L("Test2: Call zone defrag and cancel it"));
5914 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
5915 FreeMovable(gChunkArray1, gChunkArraySize1);
5918 TUint index = gZoneCount - 1;
5920 (gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
5921 gZoneUtilArray[index].iAllocFixed !=0 ||
5922 gZoneUtilArray[index].iAllocUnknown !=0))
5927 (gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
5928 gZoneUtilArray[index].iAllocFixed !=0 ||
5929 gZoneUtilArray[index].iAllocUnknown !=0))
5931 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
5935 defragZoneID = gZoneConfigArray[index].iZoneId;
5936 TESTDEBUG(test.Printf(_L("defragZoneID = 0x%x\n"),defragZoneID));
5938 r = Ldd.CheckCancel(DEFRAG_TYPE_EMPTY, defragZoneID);
5940 TESTDEBUG(test.Printf(_L("r = %d\n"), r));
5941 if (r != KErrCancel)
5943 test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrCancel);
5944 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5949 test.Printf(_L("Passed...\n"));
5953 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
5957 test.Next(_L("Test3: Call Claim RAM Zone and cancel it"));
5960 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
5961 FreeMovable(gChunkArray1, gChunkArraySize1);
5964 index = gZoneCount - 1;
5966 (gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
5967 gZoneUtilArray[index].iAllocFixed !=0 ||
5968 gZoneUtilArray[index].iAllocUnknown !=0))
5972 defragZoneID = gZoneConfigArray[index].iZoneId;
5973 TESTDEBUG(test.Printf(_L("defragZoneID = 0x%x\n"),defragZoneID));
5976 (gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
5977 gZoneUtilArray[index].iAllocFixed !=0 ||
5978 gZoneUtilArray[index].iAllocUnknown !=0))
5980 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
5984 r = Ldd.CheckCancel(DEFRAG_TYPE_CLAIM, defragZoneID);
5986 TESTDEBUG(test.Printf(_L("r = %d\n"), r));
5987 if (r != KErrCancel)
5989 test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrCancel);
5990 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
5995 test.Printf(_L("Passed...\n"));
5999 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
6007 // TestZoneAllocNoAffect
6009 // Ensures that fixed page allocations do not affect the movable
6010 // or discardable page allocations
6012 TInt TestZoneAllocNoAffect(TInt aZoneAllocType, TInt aPageType)
6014 TInt retVal = KErrNone;
6016 TInt mostPrefIndex = 0;
6017 TUint leastPrefIndex = 0;
6018 TUint leastPrefZoneID = 0;
6021 gChunkArray1 = new RChunk;
6022 gChunkArraySize1 = 1;
6026 test.Printf(_L("Test Epoc::ZoneAllocPhysicalRam() doesn't affect the allocation of movable pages\n"));
6029 case BEST_DISCARDABLE:
6030 test.Printf(_L("Test Epoc::ZoneAllocPhysicalRam() doesn't affect the allocation of discardable pages\n"));
6033 test.Printf(_L("Not a paged ROM - Skipping...\n"));
6038 // Fist find the zone that movable page allocations should go into
6039 // Getting the best movable will be the same for discardable as well
6040 mostPrefIndex = GetBestZone(aPageType);
6041 if (mostPrefIndex == KErrNotFound)
6043 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
6047 // Get the least pref zone to zone alloc into and ensure it has free pages
6049 leastPrefIndex = gPrefArray[gZoneCount - 1];
6050 if (gZoneConfigArray[mostPrefIndex].iPref == gZoneConfigArray[leastPrefIndex].iPref)
6052 test.Printf(_L("Zones with same preference - Skipping...\n"));
6055 if (leastPrefIndex == (TUint)mostPrefIndex ||
6056 gZoneUtilArray[leastPrefIndex].iFreePages == 0)
6058 test.Printf(_L("leastPrefIndex = mostPrefIndex or leastPrefIndex(%d) / mostPrefIndex(%d) has 0 free - Skipping test step...\n"),
6059 leastPrefIndex, mostPrefIndex);
6064 // Zone alloc 1 fixed page into the least preferable zone
6065 leastPrefZoneID = gZoneConfigArray[leastPrefIndex].iZoneId;
6066 GetOriginalPageCount();
6067 switch(aZoneAllocType)
6071 r = Ldd.ZoneAllocDiscontiguous(leastPrefZoneID, 1);
6075 case Z_ALLOC_CONTIG:
6077 r = Ldd.ZoneAllocContiguous(leastPrefZoneID, gPageSize);
6082 if (r != KErrNone ||
6083 gZoneUtilArray[leastPrefIndex].iAllocFixed <= gOriginalPageCountArray[leastPrefIndex].iAllocFixed)
6085 test.Printf(_L("Failed to allocate 1 fixed page to zone index %d r = %d\n"), leastPrefIndex, r);
6088 test.Printf(_L("leastPrefIndex = %d mostPrefIndex = %d\n"), leastPrefIndex, mostPrefIndex);
6092 // now allocate 1 movable page
6093 r = AllocMovable(gChunkArray1, gChunkArraySize1, 1, gPageSize);
6096 test.Printf(_L("Failed to allocate 1 movable page r = %d\n"), r);
6101 case BEST_DISCARDABLE:
6102 // now allocate 1 discardable page
6104 TUint disBytes = (gTotalPageCount.iDiscardablePages + 1) << gPageShift;
6105 r = AllocDiscardable(disPages, disBytes);
6108 test.Printf(_L("Failed to allocate 1 discardable page r = %d\n"), r);
6118 if (gOriginalPageCountArray[mostPrefIndex].iAllocMovable >= gZoneUtilArray[mostPrefIndex].iAllocMovable ||
6119 gOriginalPageCountArray[leastPrefIndex].iAllocMovable < gZoneUtilArray[leastPrefIndex].iAllocMovable)
6121 test.Printf(_L("FAIL: mostPrefIndex(%d) origMov 0x%x curMov 0x%x leastPrefIndex(%d) origMov 0x%x curMov 0x%x\n"),
6122 mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocMovable, gZoneUtilArray[mostPrefIndex].iAllocMovable,
6123 leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocMovable, gZoneUtilArray[leastPrefIndex].iAllocMovable);
6124 CLEANUP(Ldd.FreeAllFixedPages());
6125 retVal = KErrGeneral;
6129 test.Printf(_L("Passed...\n"));
6134 case BEST_DISCARDABLE:
6135 if (gOriginalPageCountArray[mostPrefIndex].iAllocDiscardable >= gZoneUtilArray[mostPrefIndex].iAllocDiscardable ||
6136 gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable < gZoneUtilArray[leastPrefIndex].iAllocDiscardable)
6138 test.Printf(_L("FAIL: mostPrefIndex(%d) origDis 0x%x curDis 0x%x leastPrefIndex(%d) origDis 0x%x curDis 0x%x\n"),
6139 mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocDiscardable, gZoneUtilArray[mostPrefIndex].iAllocDiscardable,
6140 leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable, gZoneUtilArray[leastPrefIndex].iAllocDiscardable);
6141 CLEANUP(Ldd.FreeAllFixedPages());
6142 retVal = KErrGeneral;
6146 test.Printf(_L("Passed...\n"));
6151 // This will clean up any fixed pages allocated.
6158 // TestZoneAllocContiguous
6160 //---------------------------------------------------------------------------------------------------------------------
6161 //! @SYMTestCaseID KBASE-t_ramdefrag-0535
6162 //! @SYMTestType CIT
6163 //! @SYMTestCaseDesc Verifying the contiguous overload of Epoc::ZoneAllocPhysicalRam().
6164 //! @SYMPREQ PREQ308
6165 //! @SYMTestPriority High
6167 //! 1. Call function with a valid aZoneID.
6168 //! 2. Call function with an invalid aZoneID
6169 //! 3. Call function with aSize > zone size
6170 //! 4. Call multiple RAM zone overload of the function with a mix of valid and invalid IDs.
6171 //! 5. Call multiple RAM zone overload of the function with contiguous RAM zones and attempt
6172 //! to allocate over both RAM zones.
6173 //! 6. Call function on a RAM zone that has some non-DP pages allocated into it and ask
6174 //! for the whole RAM zone to be filled with fixed pages.
6175 //! 7. Get the most preferable zone for movable page allocations (mostPref). Allocate 1 contiguous fixed page
6176 //! in the least preferable zone. Following this allocate 1 movable page
6177 //! 8. Get the most preferable zone for discardable page allocations (mostPref). Allocate 1 contiguous fixed
6178 //! page in the least preferable zone. Following this allocate 1 discardable page
6180 //! @SYMTestExpectedResults
6187 //! 7. Movable pages are allocated into zone mostPref.
6188 //! 8. Discardable pages are allocated into zone mostPref
6189 //---------------------------------------------------------------------------------------------------------------------
6190 TInt TestZoneAllocContiguous()
6193 test.Start(_L("Test1: Call function Epoc::ZoneAllocPhysicalRam() with a valid aZoneID"));
6196 const TUint KAllocPages = 2;
6198 GetOriginalPageCount();
6200 TUint index = gZoneCount - 1;
6201 while (index > 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
6205 TUint zoneID = gZoneConfigArray[index].iZoneId;
6206 if (index == 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
6208 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
6212 TUint allocBytes = KAllocPages << gPageShift;
6213 r = Ldd.ZoneAllocContiguous(zoneID, allocBytes);
6215 if (r == KErrNone &&
6216 gZoneUtilArray[index].iAllocFixed - gOriginalPageCountArray[index].iAllocFixed == KAllocPages)
6218 test.Printf(_L("Pass: Correct number of fixed pages allocated to zone ID 0x%x\n"),zoneID);
6222 test.Printf(_L("Fail: r = %d, expected = %d, or number of pages allocated is not expected\n"),
6224 CLEANUP(Ldd.FreeAllFixedPages());
6228 Ldd.FreeAllFixedPages();
6233 test.Next(_L("Test2: Call function Epoc::ZoneAllocPhysicalRam() with an invalid aZoneID"));
6236 zoneID = KInvalidZoneID;
6238 TESTDEBUG(test.Printf(_L("zoneID = 0x%x\n"), zoneID));
6240 r = Ldd.ZoneAllocContiguous(zoneID, gPageSize);
6242 if (r == KErrArgument)
6244 test.Printf(_L("Pass: Correct return value\n"));
6248 test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
6249 CLEANUP(Ldd.FreeAllFixedPages());
6252 Ldd.FreeAllFixedPages();
6256 test.Next(_L("Test3: Call function Epoc::ZoneAllocPhysicalRam() with aSize > zone size"));
6259 index = gZoneCount - 1;
6260 zoneID = gZoneConfigArray[index].iZoneId;
6262 TUint allocBytes = (gZoneUtilArray[index].iPhysPages + 5) << gPageShift;
6264 r = Ldd.ZoneAllocContiguous(zoneID, allocBytes);
6266 if (r == KErrArgument)
6268 test.Printf(_L("Pass: Correct return value and number of pages allocated is correct\n"));
6272 test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
6273 CLEANUP(Ldd.FreeAllFixedPages());
6276 Ldd.FreeAllFixedPages();
6279 const TUint KMultiZoneIds = 10;
6280 TUint* zoneIdArray = new TUint[KMultiZoneIds];
6281 test_NotNull(zoneIdArray);
6283 test.Next(_L("Test4: Test Epoc::ZoneAllocPhysicaRam() always fails when at least one ID is invalid"));
6286 index = gZoneCount - 1;
6287 while (index > 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
6291 if (index == 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
6293 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
6297 TUint zoneIdSize = 2;
6298 zoneIdArray[0] = gZoneConfigArray[index].iZoneId;
6299 zoneIdArray[1] = KInvalidZoneID;
6300 TUint allocBytes = KAllocPages << gPageShift;
6301 r = Ldd.MultiZoneAllocContiguous(zoneIdArray, zoneIdSize, allocBytes);
6302 if (r != KErrArgument)
6304 CLEANUP(Ldd.FreeAllFixedPages());
6309 test.Printf(_L("Pass: Correct return value\n"));
6315 test.Next(_L("Test5: Test Epoc::ZoneAllocPhysicalRam() can span multiple RAM zones"));
6317 // Attempt to find to physically contiguous RAM zones where higher addressed
6318 // one is empty, relies on RAM zones are returned by HAL functions in
6319 // ascending physical address order.
6321 TBool zonesFound = EFalse;
6322 index = gZoneCount - 1;
6323 for (; index > 1; index--)
6325 if (gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages &&
6326 gZoneUtilArray[index-1].iFreePages == gZoneUtilArray[index-1].iPhysPages &&
6327 gZoneConfigArray[index].iPhysBase - 1 == gZoneConfigArray[index-1].iPhysEnd)
6336 test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
6340 // Allocate one page more than the first RAM zone to force the allocation
6341 // to spread over both RAM zones.
6342 TUint allocPages = gZoneUtilArray[index-1].iPhysPages + 1;
6343 TUint allocBytes = allocPages << gPageShift;
6345 // Attempt to find a zone that has less free pages than the allocation
6346 // size so we can test that the method continues past too full RAM zones.
6347 zonesFound = EFalse;
6348 TUint noAllocZone = 0;
6349 for (; noAllocZone < gZoneCount; noAllocZone++)
6351 if (allocPages > gZoneUtilArray[noAllocZone].iFreePages &&
6352 noAllocZone != index && noAllocZone != index-1)
6361 zoneIdArray[0] = gZoneConfigArray[index-1].iZoneId;
6362 zoneIdArray[1] = gZoneConfigArray[index].iZoneId;
6365 {// Have a zone that won't meet the allocation so use it
6366 TESTDEBUG(test.Printf(_L("noAllocZone ID %x\n"), gZoneConfigArray[noAllocZone].iZoneId));
6368 zoneIdArray[0] = gZoneConfigArray[noAllocZone].iZoneId;
6369 zoneIdArray[1] = gZoneConfigArray[index-1].iZoneId;
6370 zoneIdArray[2] = gZoneConfigArray[index].iZoneId;
6373 r = Ldd.MultiZoneAllocContiguous(zoneIdArray, zoneIds, allocBytes);
6376 if (r != KErrNone ||
6377 gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
6378 gZoneUtilArray[index-1].iFreePages == gZoneUtilArray[index-1].iPhysPages)
6379 {// The allocation failed.
6380 CLEANUP(Ldd.FreeAllFixedPages());
6385 test.Printf(_L("Passed...\n"));
6388 // TestEnd() will free the allocation of fixed pages.
6391 test.Next(_L("Test6: Test Epoc::ZoneAllocPhysicalRam() returns KErrNoMemory when appropriate"));
6393 // Attempt to find a RAM zone with some non-discarable pages allocated into it.
6394 // (At time of writing discardable pages wouldn't be discarded on demand by
6395 // this function but this may be changed in the future as discontiguous case does that).
6397 zonesFound = EFalse;
6398 index = gZoneCount - 1;
6399 for (; index > 0; index--)
6401 if (gZoneUtilArray[index].iFreePages &&
6402 (gZoneUtilArray[index].iAllocMovable || gZoneUtilArray[index].iAllocFixed))
6411 test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
6415 // Attempt to allocate the whole of the RAM zone.
6416 GetOriginalPageCount();
6417 TUint allocBytes = gZoneConfigArray[index].iPhysPages << gPageShift;
6418 r = Ldd.ZoneAllocContiguous(gZoneConfigArray[index].iZoneId, allocBytes);
6420 // The allocation should have failed and no pages should have
6423 if (r != KErrNoMemory ||
6424 gOriginalPageCountArray[index].iFreePages > gZoneUtilArray[index].iFreePages)
6426 CLEANUP(Ldd.FreeAllFixedPages());
6431 test.Printf(_L("Pass: Correct return value\n"));
6434 // TestEnd() will free the allocation of fixed pages.
6436 delete[] zoneIdArray;
6439 test.Next(_L("Test7: Test Epoc::ZoneAllocPhysicalRam() (Contiguous) doesn't affect the allocation of movable pages"));
6440 r = TestZoneAllocNoAffect(Z_ALLOC_CONTIG, BEST_MOVABLE);
6446 test.Next(_L("Test8: Test Epoc::ZoneAllocPhysicalRam() (Contiguous) doesn't affect the allocation of discardable pages"));
6447 r = TestZoneAllocNoAffect(Z_ALLOC_CONTIG, BEST_DISCARDABLE);
6459 // TestZoneAllocDiscontiguous
6461 //---------------------------------------------------------------------------------------------------------------------
6462 //! @SYMTestCaseID KBASE-t_ramdefrag-0536
6463 //! @SYMTestType CIT
6464 //! @SYMTestCaseDesc Verifying the discontiguous overload of Epoc::ZoneAllocPhysicalRam().
6465 //! @SYMPREQ PREQ308
6466 //! @SYMTestPriority High
6468 //! 1. Call function with a valid aZoneID.
6469 //! 2. Call function with an invalid aZoneID
6470 //! 3. Call function with aNumPages > zone size
6471 //! 4. Call multiple RAM zone overload of function with a mix of valid and invalid IDs
6472 //! 5. Call multiple RAM zone overload of function so that the allocation will have to
6473 //! span multiple RAM zones.
6474 //! 6. Call function with memory full with DP cache that has reached it's
6475 //! minimum cache size.
6476 //! 7. Call function with memory not quite full with DP cache that has reached it's
6477 //! minimum cache size and with the specified RAM zone full.
6478 //! 8. Call function to allocate a whole RAM zone on a RAM zone that has non-discardable
6479 //! pages already allocated into it.
6480 //! 9. Call function to allocate one less than the whole RAM zone on a RAM zone that has movable
6481 //! pages allocated.
6482 //! 10. Get the most preferable zone for movable page allocations (mostPref). Allocate 1 discontiguous fixed page
6483 //! in the least preferable zone. Following this allocate 1 movable page
6484 //! 11. Get the most preferable zone for discardable page allocations (mostPref). Allocate 1 discontiguous fixed
6485 //! page in the least preferable zone. Following this allocate 1 discardable page
6487 //! @SYMTestExpectedResults
6496 //! 9. KErrNone (i.e. the movable pages are shifted out of the way but only those that need to be moved).
6497 //! 10. Movable pages are allocated into zone mostPref.
6498 //! 11. Discardable pages are allocated into zone mostPref
6499 //---------------------------------------------------------------------------------------------------------------------
6500 TInt TestZoneAllocDiscontiguous()
6503 test.Start(_L("Test1: Call function Epoc::ZoneAllocPhysicalRam() with a valid aZoneID"));
6506 const TUint KAllocPages = 5;
6507 // Detemine how many extra pages the kernel heap may grow by
6508 // as these may need to be accounted for.
6509 TUint fixedOverhead = Ldd.GetAllocDiff(KAllocPages);
6511 GetOriginalPageCount();
6513 TUint index = gZoneCount - 1;
6514 while (index > 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
6519 if (gZoneUtilArray[index].iFreePages < KAllocPages || gTotalPageCount.iFreePages < KAllocPages + fixedOverhead)
6521 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
6525 // Allocate KAllocPages discontiguous fixed pages into RAM zone zoneID
6526 // and verfiy that the pages were allocated to the correct zone, allow for
6527 // extra fixed pages to be allocated as the kernel heap may grow.
6528 TUint zoneID = gZoneConfigArray[index].iZoneId;
6529 r = Ldd.ZoneAllocDiscontiguous(zoneID, KAllocPages);
6532 if ((r == KErrNone) &&
6533 (gZoneUtilArray[index].iAllocFixed >= gOriginalPageCountArray[index].iAllocFixed + KAllocPages))
6535 test.Printf(_L("Pass: Correct return value and number of pages allocated is correct\n"));
6539 test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
6540 CLEANUP(Ldd.FreeAllFixedPages());
6543 Ldd.FreeAllFixedPages();
6548 test.Next(_L("Test2: Call function Epoc::ZoneAllocPhysicalRam() with an invalid aZoneID"));
6551 TUint zoneID = KInvalidZoneID;
6553 r = Ldd.ZoneAllocDiscontiguous(zoneID, KAllocPages);
6555 if (r == KErrArgument)
6557 test.Printf(_L("Pass: Correct return value\n"));
6561 test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
6562 CLEANUP(Ldd.FreeAllFixedPages());
6565 Ldd.FreeAllFixedPages();
6568 test.Next(_L("Test3: Call function Epoc::ZoneAllocPhysicalRam() when aNumPages > zone size"));
6572 index = gZoneCount - 1;
6573 zoneID = gZoneConfigArray[index].iZoneId;
6575 TUint allocPages = gZoneUtilArray[index].iPhysPages + 1;
6576 r = Ldd.ZoneAllocDiscontiguous(zoneID, allocPages);
6579 if (r == KErrArgument)
6581 test.Printf(_L("Pass: Correct return value and number of pages allocated is correct\n"));
6585 test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
6586 CLEANUP(Ldd.FreeAllFixedPages());
6589 Ldd.FreeAllFixedPages();
6592 const TUint KMultiZoneIds = 10;
6593 TUint* zoneIdArray = new TUint[KMultiZoneIds];
6594 test_NotNull(zoneIdArray);
6596 test.Next(_L("Test4: Test Epoc::ZoneAllocPhysicaRam() always fails when at least one ID is invalid"));
6599 TBool zonesFound = EFalse;
6600 index = gZoneCount - 1;
6601 for (; index > 0; index--)
6603 if (gZoneUtilArray[index].iFreePages >= KAllocPages)
6611 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
6615 TUint zoneIdSize = 2;
6616 zoneIdArray[0] = gZoneConfigArray[index].iZoneId;
6617 zoneIdArray[1] = KInvalidZoneID;
6618 r = Ldd.MultiZoneAllocDiscontiguous(zoneIdArray, zoneIdSize, KAllocPages);
6619 if (r != KErrArgument)
6620 {// Make sure we cleanup.
6621 CLEANUP(Ldd.FreeAllFixedPages());
6626 test.Printf(_L("Passed...\n"));
6632 test.Next(_L("Test5: Test Epoc::ZoneAllocPhysicalRam() can span multiple RAM zones"));
6634 zonesFound = EFalse;
6635 TUint zonesCount = 0;
6636 const TUint KTest5Zones = 2;
6637 TUint zoneIndices[KTest5Zones];
6640 // Attempt to find KTest5Zones RAM zones with some free pages,
6641 // search in reverse preference order to reduce chances of kernel heap pages
6642 // being allocated into the RAM zones under test.
6644 index = gZoneCount - 1;
6645 for (; index > 0; index--)
6647 TUint prefIndex = gPrefArray[index];
6648 if (gZoneUtilArray[prefIndex].iFreePages != 0)
6650 allocPages += gZoneUtilArray[prefIndex].iFreePages;
6651 zoneIndices[zonesCount++] = prefIndex;
6652 if (zonesCount == KTest5Zones)
6662 test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
6666 // Attempt to find a zone that has less free pages than the allocation
6667 // size so we can test that the method continues past too full RAM zones.
6668 zonesFound = EFalse;
6669 TUint noAllocZone = 0;
6670 for (; noAllocZone < gZoneCount; noAllocZone++)
6672 if (allocPages > gZoneUtilArray[noAllocZone].iFreePages &&
6673 noAllocZone != zoneIndices[0] && noAllocZone != zoneIndices[1])
6681 {// Fill memory with DP pages to test the allocation will discard
6682 // pages when necessary.
6684 r = AllocDiscardable(discard);
6686 if (r != KErrNone ||
6687 gZoneUtilArray[zoneIndices[0]].iFreePages != 0 ||
6688 gZoneUtilArray[zoneIndices[1]].iFreePages != 0)
6690 test.Printf(_L("r %d\n"), r);
6691 CLEANUP(ResetDPCache());
6696 TUint zoneIds = KTest5Zones;
6699 zoneIdArray[0] = gZoneConfigArray[zoneIndices[0]].iZoneId;
6700 zoneIdArray[1] = gZoneConfigArray[zoneIndices[1]].iZoneId;
6703 {// Have a zone that won't meet the allocation so use it
6704 TESTDEBUG(test.Printf(_L("noAllocZone ID %x\n"), gZoneConfigArray[noAllocZone].iZoneId));
6706 zoneIdArray[0] = gZoneConfigArray[noAllocZone].iZoneId;
6707 zoneIdArray[1] = gZoneConfigArray[zoneIndices[0]].iZoneId;
6708 zoneIdArray[2] = gZoneConfigArray[zoneIndices[1]].iZoneId;
6711 // Adjust the allocation size for any kernel heap pages that may be
6712 // required as they may get allocated into the RAM zones under test.
6713 allocPages -= Ldd.GetAllocDiff(allocPages);
6715 GetOriginalPageCount();
6716 r = Ldd.MultiZoneAllocDiscontiguous(zoneIdArray, zoneIds, allocPages);
6719 if (r != KErrNone ||
6720 gZoneUtilArray[zoneIndices[0]].iAllocFixed <= gOriginalPageCountArray[zoneIndices[0]].iAllocFixed ||
6721 gZoneUtilArray[zoneIndices[1]].iAllocFixed <= gOriginalPageCountArray[zoneIndices[1]].iAllocFixed)
6722 {// The allocation failed.
6723 CLEANUP(Ldd.FreeAllFixedPages());
6728 test.Printf(_L("Passed...\n"));
6731 // TestEnd() will free the allocation of fixed pages.
6737 test.Next(_L("Test6: Test Epoc::ZoneAllocPhysicalRam() returns KErrNoMemory when DP minimum cache size is hit"));
6740 TUint zoneFreePages = 0;
6741 index = gZoneCount - 1;
6742 for (; index > 0; index--)
6744 TUint prefIndex = gPrefArray[index];
6745 zoneFreePages = gZoneUtilArray[prefIndex].iFreePages;
6746 if (zoneFreePages > 2)
6756 test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
6760 // Fill the RAM with DP cache pages and up the minimum cache size so
6761 // that the allocation will fail.
6763 TInt r = AllocDiscardable(discardBytes, KMaxTUint64, (zoneFreePages - 1) << gPageShift);
6766 // Ensure that the RAM zone under test is full.
6768 if (gZoneUtilArray[index].iFreePages != 0)
6770 CLEANUP(ResetDPCache());
6774 r = Ldd.ZoneAllocDiscontiguous(gZoneConfigArray[index].iZoneId, zoneFreePages);
6776 if (r != KErrNoMemory)
6778 CLEANUP(Ldd.FreeAllFixedPages());
6783 test.Printf(_L("Passed...\n"));
6788 test.Next(_L("Test7: Test Epoc::ZoneAllocPhysicalRam() replaces DP cache when DP minimum cache size is hit"));
6790 gChunkArray1 = new RChunk;
6791 gChunkArraySize1 = 1;
6793 zonesFound = EFalse;
6794 for (index = gZoneCount - 1; index > 0 && !zonesFound; index--)
6796 TUint prefIndex = gPrefArray[index];
6797 zoneFreePages = gZoneUtilArray[prefIndex].iFreePages;
6798 if (zoneFreePages > 1)
6800 // Check there is at least one free page in the other RAM zones.
6802 for (; i < gZoneCount; i++)
6804 if (i != prefIndex && gZoneUtilArray[i].iFreePages != 0)
6815 test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
6820 // Attempt to allocate a movable page to create a gap for the DP cache
6821 // page to be replaced with.
6822 GetOriginalPageCount();
6823 r = AllocMovable(gChunkArray1, gChunkArraySize1, 1, gPageSize);
6826 TESTDEBUG(test.Printf(_L("index %d prev free 0x%x cur free 0x%x\n"),
6827 index, gOriginalPageCountArray[index].iFreePages, gZoneUtilArray[index].iFreePages));
6829 if (r != KErrNone ||
6830 gOriginalPageCountArray[index].iFreePages != gZoneUtilArray[index].iFreePages)
6831 {// The gap was allocated into the RAM zone under test so can't continue as
6832 // the DP cache will attempt to be reallocated into the same RAM zone.
6833 test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
6837 // Get the ldd to create the array for the fixed page addresses here
6838 // so that any kernel heap allocations have already occurred before
6839 // memory is filled etc. Make allocation really large so it will always be enough.
6840 r = Ldd.AllocFixedArray(50);
6842 // Fill RAM with DP cache pages and free the gap.
6844 r = AllocDiscardable(discardBytes, KMaxTUint64, 0);
6847 CLEANUP(ResetDPCache());
6851 TInt prevFreeBytes = gFreeRam;
6852 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
6854 TInt freedPages = (gFreeRam - prevFreeBytes) >> gPageShift;
6856 {// Something went wrong as should have freed at least one page
6857 CLEANUP(ResetDPCache());
6860 TUint extraFreePages = freedPages - 1;
6862 // Ensure that the RAM zone under test is full.
6864 if (gZoneUtilArray[index].iFreePages != 0)
6866 CLEANUP(ResetDPCache());
6870 // Allocate from the RAM zone which should force a DP cache
6871 // page to be allocated.
6872 GetOriginalPageCount();
6873 TUint fixedAllocPages = 1 + extraFreePages;
6874 r = Ldd.ZoneAllocDiscontiguous2(gZoneConfigArray[index].iZoneId, fixedAllocPages);
6877 if (r != KErrNone ||
6878 gOriginalPageCountArray[index].iAllocFixed + fixedAllocPages != gZoneUtilArray[index].iAllocFixed)
6880 test.Printf(_L("r %d index %d alloc 0x%x prevFixed 0x%x curFixed 0x%x\n"), r, index, fixedAllocPages,
6881 gOriginalPageCountArray[index].iAllocFixed, gZoneUtilArray[index].iAllocFixed);
6882 CLEANUP(Ldd.FreeAllFixedPages());
6887 test.Printf(_L("Passed...\n"));
6891 // This should cleanup any fixed pages allocated.
6895 test.Next(_L("Test8: Test Epoc::ZoneAllocPhysicalRam() return KErrNoMemory when appropriate"));
6897 // Search for a RAM zone that has some immovable pages allocated into
6898 // it but isn't totally full.
6900 zonesFound = EFalse;
6901 for (index = 0; index < gZoneCount; index++)
6903 if (gZoneUtilArray[index].iFreePages &&
6904 (gZoneUtilArray[index].iAllocUnknown || gZoneUtilArray[index].iAllocFixed))
6912 test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
6916 // Attempt to allocate the whole RAM zone.
6917 r = Ldd.ZoneAllocDiscontiguous(gZoneConfigArray[index].iZoneId, gZoneUtilArray[index].iPhysPages);
6919 if (r != KErrNoMemory)
6921 test.Printf(_L("FAIL: r %d index %d\n"), r, index);
6922 CLEANUP(Ldd.FreeAllFixedPages());
6927 test.Printf(_L("Passed...\n"));
6932 test.Next(_L("Test9: Test Epoc::ZoneAllocPhysicalRam() moves the required number of movable pages"));
6934 TUint allocFixedPages;
6935 // Search for a RAM zone that has at least 2 movable pages allocated into it.
6936 // Need 2 so that we can move one and leave one.
6938 zonesFound = EFalse;
6939 for (index = 0; index < gZoneCount; index++)
6941 if (gZoneUtilArray[index].iAllocMovable > 1)
6943 // Only use this zone if the other RAM zones have enough free space for
6944 // the movable page in this zone to be moved to.
6945 TUint freeInOther = 0;
6946 for (TUint i = 0; i < gZoneCount && !zonesFound; i++)
6950 freeInOther += gZoneUtilArray[i].iFreePages;
6953 if (freeInOther >= gZoneUtilArray[index].iAllocMovable)
6963 test.Printf(_L("No suitable RAM zone could be found - Skipping...\n"));
6967 // Allocate up to one less than the RAM zone size. Do 2 stage fixed allocation
6968 // to avoid kernel heap allocations spoiling test setup.
6969 r = Ldd.AllocFixedArray(gZoneConfigArray[index].iPhysPages);
6972 test.Printf(_L("Not enough free RAM to perform test - Skipping...\n"));
6977 if (gZoneUtilArray[index].iAllocMovable < 2)
6979 test.Printf(_L("Expanding kernel heap for phys address array spoiled RAM zone - Skipping...\n"));
6982 allocFixedPages = gZoneUtilArray[index].iAllocMovable +
6983 gZoneUtilArray[index].iAllocDiscardable +
6984 gZoneUtilArray[index].iFreePages - 1;
6985 r = Ldd.ZoneAllocDiscontiguous2(gZoneConfigArray[index].iZoneId, allocFixedPages);
6987 if (r != KErrNone || !gZoneUtilArray[index].iAllocMovable)
6989 test.Printf(_L("Fixed not allocated or too many movable moved RAM zone ID%x\n"),
6990 gZoneConfigArray[index].iZoneId);
6991 CLEANUP(Ldd.FreeAllFixedPages());
6996 // This will clean up any fixed pages allocated.
6999 delete[] zoneIdArray;
7001 test.Next(_L("Test10: Test Epoc::ZoneAllocPhysicalRam() (Discontiguous) doesn't affect the allocation of movable pages"));
7002 r = TestZoneAllocNoAffect(Z_ALLOC_DISC, BEST_MOVABLE);
7008 test.Next(_L("Test11: Test Epoc::ZoneAllocPhysicalRam() (Discontiguous) doesn't affect the allocation of discardable pages"));
7009 r = TestZoneAllocNoAffect(Z_ALLOC_DISC, BEST_DISCARDABLE);
7023 //---------------------------------------------------------------------------------------------------------------------
7024 //! @SYMTestCaseID KBASE-t_ramdefrag-0537
7025 //! @SYMTestType CIT
7026 //! @SYMTestCaseDesc Verifying the function Epoc::FreePhysicalRam()
7027 //! @SYMPREQ PREQ308
7028 //! @SYMTestPriority High
7030 //! 1. Allocate fixed pages and call function to free all fixed pages allocated.
7032 //! @SYMTestExpectedResults
7034 //---------------------------------------------------------------------------------------------------------------------
7039 test.Start(_L("Test1: Free allocated pages"));
7045 TUint index = gZoneCount - 1;
7047 (gZoneUtilArray[index].iAllocFixed != 0 ||
7048 gZoneUtilArray[index].iAllocUnknown != 0 ||
7049 (TInt)gZoneUtilArray[index].iFreePages < pages))
7053 zoneID = gZoneConfigArray[index].iZoneId;
7056 (gZoneUtilArray[index].iAllocFixed != 0 ||
7057 gZoneUtilArray[index].iAllocUnknown != 0 ||
7058 (TInt)gZoneUtilArray[index].iFreePages < pages))
7060 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
7064 TESTDEBUG(test.Printf(_L("Allocating 0x%x pages to zone ID 0x%x\n"), pages, zoneID));
7065 r = Ldd.ZoneAllocDiscontiguous(zoneID, pages);
7068 TESTDEBUG(test.Printf(_L("Freeing 0x%x fixed pages\n"), pages));
7071 r = Ldd.FreeZone(pages);
7073 TESTDEBUG(test.Printf(_L("r = %d\n"), r));
7076 test.Printf(_L("Pass: Correct return value\n"));
7080 test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
7092 // TestDefragSemMethod
7094 //---------------------------------------------------------------------------------------------------------------------
7095 //! @SYMTestCaseID KBASE-t_ramdefrag-0538
7096 //! @SYMTestType CIT
7097 //! @SYMTestCaseDesc Verifying the semaphore versions of the various defrag methods
7098 //! @SYMPREQ PREQ308
7099 //! @SYMTestPriority High
7101 //! 1. Fragment the memory. Following this, call the semaphore variation of DefragRam.
7102 //! 2. Fragment the memory. Following this, call the semaphore variation of EmptyRamZone
7103 //! 3. Fragment the memory. Following this, call the semaphore variation of ClaimRamZone
7105 //! @SYMTestExpectedResults
7106 //! 1. 1 or more zones have been emptied
7107 //! 2. Zone specified has been emptied
7108 //! 3. Zone has been claimed
7109 //---------------------------------------------------------------------------------------------------------------------
7110 TInt TestDefragSemMethod()
7114 test.Start(_L("Test1: Call semaphore method of DefragRam"));
7117 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
7118 FreeMovable(gChunkArray1, gChunkArraySize1);
7120 GetOriginalPageCount();
7121 TBool genSucceed = CanGenSucceed();
7122 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SEM);
7125 if (r != KErrNone ||(genSucceed && CheckZonesSwitchedOff() == EFalse))
7127 test.Printf(_L("Fail: r = %d, or zones have not been swtiched off\n"), r);
7128 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
7133 test.Printf(_L("Passed...\n"));
7135 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
7139 test.Next(_L("Test2: Call semaphore method of EmptyRamZone"));
7142 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
7143 FreeMovable(gChunkArray1, gChunkArraySize1);
7146 TUint index = gZoneCount - 1;
7147 while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
7151 TUint defragZoneID = gZoneConfigArray[index].iZoneId;
7153 if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
7155 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
7159 r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SEM, defragZoneID);
7162 TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
7163 if (freeInOthers && (r != KErrNone || !CheckZoneIsOff(index)))
7165 test.Printf(_L("Fail: r = %d, or zones has not been swtiched off\n"), r);
7166 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
7171 test.Printf(_L("Passed...\n"));
7174 // This will free any allocated memory.
7177 test.Next(_L("Test3: Call semaphore method of ClaimRamZone"));
7180 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
7181 FreeMovable(gChunkArray1, gChunkArraySize1);
7184 index = gZoneCount - 2;
7185 while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
7189 defragZoneID = gZoneConfigArray[index].iZoneId;
7191 if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
7193 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
7197 r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SEM, defragZoneID);
7200 TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
7201 if (CheckZoneIsOff(index) ||
7202 (freeInOthers && ( r != KErrNone ||
7203 gZoneUtilArray[index].iAllocFixed != gZoneConfigArray[index].iPhysPages)))
7205 test.Printf(_L("Fail: r = %d, or zone ID 0x%x has not been claimed\n"), r, defragZoneID);
7206 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
7211 test.Printf(_L("Passed...\n"));
7214 r = Ldd.FreeAllFixedPages();
7217 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
7227 // TestDefragDfcMethod
7229 //---------------------------------------------------------------------------------------------------------------------
7230 //! @SYMTestCaseID KBASE-t_ramdefrag-0539
7231 //! @SYMTestType CIT
7232 //! @SYMTestCaseDesc Verifying the Dfc versions of the various defrag methods
7233 //! @SYMPREQ PREQ308
7234 //! @SYMTestPriority High
7236 //! 1. Fragment the memory. Following this, call the Dfc variation of DefragRam.
7237 //! 2. Fragment the memory. Following this, call the Dfc variation of EmptyRamZone
7238 //! 3. Fragment the memory. Following this, call the Dfc variation of ClaimRamZone
7240 //! @SYMTestExpectedResults
7241 //! 1. 1 or more zones have been emptied
7242 //! 2. Zone specified has been emptied
7243 //! 3. Zone has been claimed
7244 //---------------------------------------------------------------------------------------------------------------------
7245 TInt TestDefragDfcMethod()
7249 test.Start(_L("Test1: Call Dfc method of DefragRam"));
7254 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
7255 FreeMovable(gChunkArray1, gChunkArraySize1);
7257 GetOriginalPageCount();
7258 TBool genSucceed = CanGenSucceed();
7259 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_DFC, 0, 0, -1, &req);
7260 TESTDEBUG(test.Printf(_L("After queueing defrag r = %d\n"), r));
7261 User::WaitForRequest(req);
7264 if (r != KErrNone || (genSucceed && CheckZonesSwitchedOff() == EFalse))
7266 test.Printf(_L("Fail: r = %d, or zones have not been swtiched off\n"), r);
7267 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
7272 test.Printf(_L("Passed...\n"));
7274 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
7278 test.Next(_L("Test2: Call Dfc method of EmptyRamZone"));
7281 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
7282 FreeMovable(gChunkArray1, gChunkArraySize1);
7285 TUint index = gZoneCount - 1;
7287 (gZoneUtilArray[index].iAllocMovable < 10 ||
7288 gZoneUtilArray[index].iAllocFixed != 0 ||
7289 gZoneUtilArray[index].iAllocUnknown != 0))
7294 TUint defragZoneID = gZoneConfigArray[index].iZoneId;
7295 test.Printf(_L("zone ID = 0x%x\n"), defragZoneID);
7297 (gZoneUtilArray[index].iAllocMovable < 10 ||
7298 gZoneUtilArray[index].iAllocFixed != 0 ||
7299 gZoneUtilArray[index].iAllocUnknown != 0))
7301 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
7305 r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_DFC, defragZoneID, 0, -1, &req);
7307 User::WaitForRequest(req);
7311 TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
7312 if (freeInOthers && (r != KErrNone || CheckZoneIsOff(index) == EFalse))
7314 test.Printf(_L("Fail: r = %d, or zones have not been swtiched off\n"), r);
7315 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
7320 test.Printf(_L("Passed...\n"));
7323 // This will free any allocated memory
7327 test.Next(_L("Test3: Call Dfc method of ClaimRamZone"));
7330 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
7331 FreeMovable(gChunkArray1, gChunkArraySize1);
7334 index = gZoneCount - 1;
7335 while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
7339 defragZoneID = gZoneConfigArray[index].iZoneId;
7341 if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
7343 test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
7347 r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_DFC, defragZoneID, 0, -1, &req);
7348 User::WaitForRequest(req);
7352 TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
7353 if (CheckZoneIsOff(index) ||
7354 (freeInOthers && (r != KErrNone ||
7355 gZoneUtilArray[index].iAllocFixed != gZoneConfigArray[index].iPhysPages)))
7357 test.Printf(_L("Fail: r = %d, or zone ID 0x%x has not been claimed\n"), r, defragZoneID);
7358 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
7361 CLEANUP(Ldd.FreeFromAddr( gZoneUtilArray[index].iAllocFixed,
7362 gZoneConfigArray[index].iPhysBase));
7368 test.Printf(_L("Passed...\n"));
7373 Ldd.FreeFromAddr( gZoneUtilArray[index].iAllocFixed,
7374 gZoneConfigArray[index].iPhysBase);
7377 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
7388 //---------------------------------------------------------------------------------------------------------------------
7389 //! @SYMTestCaseID KBASE-t_ramdefrag-0540
7390 //! @SYMTestType CIT
7391 //! @SYMTestCaseDesc Verifying the priorities of the defrag methods
7392 //! @SYMPREQ PREQ308
7393 //! @SYMTestPriority High
7395 //! 1. Call DefragRam with a lower invalid priority value, e.g. -2
7396 //! 2. Call DefragRam with a lower invalid priority value, e.g. 100
7397 //! 3. Queue three asynchronous defrags using the Dfc method:
7398 //! a. First one with the lowest priority, this will start the straight away
7399 //! and will busy the defrag method, causing any other defrag requests to
7401 //! b. Queue a defrag with a relatively low priority
7402 //! c. Queue a defrag with a higher priority than the one queued in (b)
7403 //! Record the order in which the defrags are completed
7405 //! @SYMTestExpectedResults
7408 //! 3. (a) will complete first as it started straight away.
7409 //! (b) and (c) were both queued whilst (a) was running,
7410 //! however as (c) has a higher priority, it will complete first -
7411 //! therefore the order returned would be "a,c,b"
7412 //---------------------------------------------------------------------------------------------------------------------
7413 TInt TestPriorities()
7415 test.Start(_L("Test1: Call defrag with an invalid lower priority"));
7418 TInt r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, 0, -2);
7419 TESTDEBUG(test.Printf(_L("r = %d\n"), r));
7420 if (r != KErrArgument)
7422 test.Printf(_L("Fail: r = %d, not expected\n"), r);
7427 test.Printf(_L("Passed...\n"));
7432 test.Next(_L("Test2: Call defrag with an invalid higher priority"));
7435 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, 0, 100);
7436 TESTDEBUG(test.Printf(_L("r = %d\n"), r));
7437 if (r != KErrArgument)
7439 test.Printf(_L("Fail: r = %d, not expected\n"), r);
7444 test.Printf(_L("Passed...\n"));
7448 if (UserSvr::HalFunction(EHalGroupKernel, EKernelHalNumLogicalCpus, 0, 0) == 1)
7449 {// Only test priorities on single core system as this test is not smp safe.
7450 test.Next(_L("Test3: Call Dfc method of EmptyRamZone to test priorities"));
7455 TRequestStatus req2;
7456 TRequestStatus req3;
7457 TInt expectedOrder = 132; // Priorities set in Device driver
7459 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
7460 FreeMovable(gChunkArray1, gChunkArraySize1);
7462 TUint index = (gZoneCount + 1) / 2;
7463 TUint defragZoneID = gZoneConfigArray[index].iZoneId;
7465 r = Ldd.CheckPriorities(DEFRAG_TYPE_EMPTY, defragZoneID, &req, &req2, &req3);
7467 User::WaitForRequest(req);
7468 User::WaitForRequest(req2);
7469 User::WaitForRequest(req3);
7471 TInt order = Ldd.GetDefragOrder();
7472 if (order != expectedOrder)
7474 test.Printf(_L("Fail: order = %d. expected = %d\n"), order, expectedOrder);
7475 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
7480 test.Printf(_L("Passed...\n"));
7482 // This will free any allocated memory
7494 //---------------------------------------------------------------------------------------------------------------------
7495 //! @SYMTestCaseID KBASE-t_ramdefrag-0541
7496 //! @SYMTestType CIT
7497 //! @SYMTestCaseDesc Verifying that when certain flags are set,
7498 //! only certain types of pages can be allocated to the zone.
7499 //! @SYMPREQ PREQ308
7500 //! @SYMTestPriority High
7502 //! 1. Set the NoFixed flag in a zone and allocate movable pages
7503 //! 2. Set the NoFixed flag in a zone and allocate fixed pages
7504 //! 3. Set the NoFixed flag in a zone and allocate discardable pages
7505 //! 4. Set the NoFixed flag in a zone and allocate fixed pages to that zone using Epoc::ZoneAllocPhysicalRam()
7506 //! 5. Set the NoFixed flag in a zone and allocate fixed pages by calling TRamDefragRequest::ClaimRamZone()
7507 //! 6. Set the NoMovable flag in a zone and allocate movable pages
7508 //! 7. Set the NoMovable flag in a zone and allocate fixed pages
7509 //! 8. Set the NoMovable flag in a zone and allocate discardable pages
7510 //! 9. Set the NoDiscardable flag in a zone and allocate movable pages
7511 //! 10. Set the NoDiscardable flag in a zone and allocate fixed pages
7512 //! 11. Set the NoDiscardable flag in a zone and allocate discardable pages
7513 //! 12. Set the OnlyDiscardable flag in a zone and allocate movable pages
7514 //! 13. Set the OnlyDiscardable flag in a zone and allocate fixed pages
7515 //! 14. Set the OnlyDiscardable flag in a zone and allocate discardable pages
7516 //! 15. Set the OnlyDiscardable flag in a zone and allocate fixed pages to that zone using Epoc::ZoneAllocPhysicalRam()
7517 //! 16. Set the OnlyDiscardable flag in a zone and allocate fixed pages by calling TRamDefragRequest::ClaimRamZone()
7518 //! 17. Set the NoFurtherAlloc flag in a zone and allocate movable pages
7519 //! 18. Set the NoFurtherAlloc flag in a zone and allocate fixed pages
7520 //! 19. Set the NoFurtherAlloc flag in a zone and allocate discardable pages
7521 //! 20. Set the NoFurtherAlloc flag in a zone and allocate fixed pages to that zone using Epoc::ZoneAllocPhysicalRam()
7522 //! 21. Set the NoFurtherAlloc flag in a zone and allocate fixed pages by calling TRamDefragRequest::ClaimRamZone()
7523 //! 22. Set up memory so that the least preferable RAM zone has movable pages + discardable pages > free pages in the most
7524 //! preferable zone. Ensure that the discardable pages cannot be discarded and so must be moved. Now set the flag on
7525 //! all zones barring the most preferable zone to KRamZoneFlagNoMovable, ensuring that most pref has no flags set.
7526 //! Following this call a general defrag.
7527 //! 23. Set up memory so that the least preferable RAM zone has movable pages + discardable pages > free pages in the most
7528 //! preferable RAM zone. Ensure that the discardable pages cannot be discarded and so must be moved.
7529 //! Now set the flag on all zones barring the most preferable zone to KRamZoneFlagNoDiscard, ensuring that
7530 //! most preferable RAM zone has no flags set. Following this call a general defrag.
7531 //! 24. Set up memory so that the least preferable RAM zone has movable pages and discardable pages. Set all the zone
7532 //! flags to KRamZoneFlagNoMovable. Following this call a general defrag.
7534 //! @SYMTestExpectedResults
7535 //! 1. Movable pages are allocated and no fixed pages allocated
7536 //! 2. No fixed pages have been allocated
7537 //! 3. Discardable pages are allocated and no fixed pages allocated
7538 //! 4. KErrNone, flag is ignored with zone specific allocation
7539 //! 5. KErrNone, flag is ignored when claiming a zone
7540 //! 6. No movable pages have been allocated
7541 //! 7. Fixed pages allocated, no movable allocated
7542 //! 8. Discardable pages allocated, no movable pages allocated
7543 //! 9. Movable pages allocated, no discardable pages allocated
7544 //! 10. Fixed pages allocated, no discardable allocated
7545 //! 11. No discardable pages allocated
7546 //! 12. No movable pages allocated
7547 //! 13. No fixed pages allocated
7548 //! 14. Discardable pages allocated, no movable or fixed allocated
7549 //! 15. KErrNone, flag is ignored with zone specific allocation
7550 //! 16. KErrNone, flag is ignored when claiming a zone
7551 //! 17. No moving, fixed or discardable pages allocated
7552 //! 18. No moving, fixed or discardable pages allocated
7553 //! 19. No moving, fixed or discardable pages allocated
7554 //! 20. KErrNoMemory, flag is obeyed with zone specific allocation
7555 //! 21. KErrNone, flag is ignored when claiming a zone
7556 //! 22. Movable pages moved to the most preferable zone, discardable pages moved to next most preferable zone
7557 //! 23. Discardable pages moved to most preferable zone, movable pages moved to next next most preferable zone
7558 //! 24. No pages are moved from the least preferable zone zone
7559 //---------------------------------------------------------------------------------------------------------------------
7563 TUint zoneDefragID = 0;
7565 test.Start(_L("Test1: No Fixed Flag, Alloc Movable"));
7568 TInt index = GetBestZone(BEST_MOVABLE);
7570 if (index == KErrNotFound)
7572 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
7576 zoneDefragID = gZoneConfigArray[index].iZoneId;
7577 if (SpaceAvailForPageTables(index, KFillAllMovable))
7579 GetOriginalPageCount();
7580 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
7583 AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
7585 if (gZoneUtilArray[index].iAllocMovable == gOriginalPageCountArray[index].iAllocMovable ||
7586 gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
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);
7589 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
7594 test.Printf(_L("Passed...\n"));
7599 test.Printf(_L("Skipping...\n"));
7605 test.Next(_L("Test2: No Fixed Flag, Alloc Fixed"));
7610 r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
7614 // find a zone that has free pages in it to block it
7617 for (; i < gZoneCount; i++)
7619 if (gZoneUtilArray[i].iFreePages > 0)
7626 if (i == gZoneCount)
7628 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
7632 zoneDefragID = gZoneConfigArray[index].iZoneId;
7634 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
7637 GetOriginalPageCount();
7638 r = Ldd.AllocateFixed(FILL_ALL_FIXED);
7641 // Ensure that either zone does not contain extra fixed pages
7642 if (gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
7644 test.Printf(_L("Fail: Fixed pages have been allocated into the zone ID 0x%x r = %d\n"), zoneDefragID, r);
7645 CLEANUP(Ldd.FreeAllFixedPages());
7650 test.Printf(_L("Passed...\n"));
7656 test.Next(_L("Test3: No Fixed Flag, Alloc Discardable"));
7660 index = GetBestZone(BEST_DISCARDABLE);
7662 if (index == KErrNotFound)
7664 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
7668 zoneDefragID = gZoneConfigArray[index].iZoneId;
7669 GetOriginalPageCount();
7671 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
7675 TInt discardablePages;
7676 r = AllocDiscardable(discardablePages);
7679 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
7684 if (gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
7686 test.Printf(_L("Fail: Fixed pages have been allocated\n"));
7687 CLEANUP(ResetDPCache());
7692 test.Printf(_L("Passed...\n"));
7698 test.Printf(_L("Not a paged ROM - Skipping test step\n"));
7702 test.Next(_L("Test4: No Fixed Flag, Alloc Fixed using ZoneAllocPhyicalRam"));
7705 index = GetBestZone(BEST_FIXED);
7707 if (index == KErrNotFound)
7709 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
7713 zoneDefragID = gZoneConfigArray[index].iZoneId;
7715 GetOriginalPageCount();
7716 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
7719 // Just need to try and allocate one page
7720 r = Ldd.ZoneAllocDiscontiguous(zoneDefragID, 1);
7725 test.Printf(_L("Fail: Fixed pages have not been allocated into the zone\n"));
7726 CLEANUP(Ldd.FreeAllFixedPages());
7731 test.Printf(_L("Passed...\n"));
7736 test.Next(_L("Test5: No Fixed Flag, Alloc Fixed by attempting to claim zone"));
7741 while ((TUint)index < gZoneCount && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
7746 if ((TUint)index == gZoneCount)
7748 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
7752 zoneDefragID = gZoneConfigArray[index].iZoneId;
7753 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
7756 r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneDefragID);
7758 TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
7759 if (freeInOthers && r != KErrNone)
7761 test.Printf(_L("Fail: Claim zone ID 0x%x was unsuccessful, r = %d\n"), zoneDefragID, r);
7766 test.Printf(_L("Passed...\n"));
7773 //------------------------------------------------------------
7774 test.Next(_L("Test6: No Movable Flag, Alloc Movable"));
7777 index = GetBestZone(BEST_MOVABLE);
7779 if (index == KErrNotFound)
7781 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
7785 zoneDefragID = gZoneConfigArray[index].iZoneId;
7787 GetOriginalPageCount();
7788 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
7791 AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable, KChunkDefaultSize, EFalse);
7794 if (gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable)
7796 test.Printf(_L("Fail: Movable pages have been allocated in the zone\n"));
7797 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
7802 test.Printf(_L("Passed...\n"));
7808 test.Next(_L("Test7: No Movable Flag, Alloc Fixed"));
7813 r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
7817 index = GetBestZone(BEST_FIXED);
7819 if (index == KErrNotFound)
7821 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
7825 zoneDefragID = gZoneConfigArray[index].iZoneId;
7827 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
7830 GetOriginalPageCount();
7831 // Fill up all of RAM with fixed pages.
7832 r = Ldd.AllocateFixed(FILL_ALL_FIXED);
7833 test.Printf(_L("r = %d\n"), r);
7836 if (gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable ||
7837 gZoneUtilArray[index].iAllocFixed <= gOriginalPageCountArray[index].iAllocFixed)
7839 test.Printf(_L("Fail: orig mov 0x%x fix 0x%x current mov 0x%x fix 0x%x\n"), gOriginalPageCountArray[index].iAllocMovable,
7840 gOriginalPageCountArray[index].iAllocFixed, gZoneUtilArray[index].iAllocMovable, gZoneUtilArray[index].iAllocFixed);
7841 CLEANUP(Ldd.FreeAllFixedPages());
7846 test.Printf(_L("Passed...\n"));
7852 test.Next(_L("Test8: No Movable Flag, Alloc Discardable"));
7856 index = GetBestZone(BEST_DISCARDABLE);
7858 if (index == KErrNotFound)
7860 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
7864 zoneDefragID = gZoneConfigArray[index].iZoneId;
7866 GetOriginalPageCount();
7867 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
7870 TInt discardablePages;
7871 r = AllocDiscardable(discardablePages);
7874 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
7879 if (gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable)
7881 test.Printf(_L("Fail: Movable pages have been allocated into the zone \n"));
7882 CLEANUP(ResetDPCache());
7887 test.Printf(_L("Passed...\n"));
7893 test.Printf(_L("Not a paged ROM - Skipping test step\n"));
7897 //-----------------------------------------------------------------------------------------------
7899 test.Next(_L("Test9: No Discardable Flag, Alloc Movable"));
7902 index = GetBestZone(BEST_MOVABLE);
7904 if (index == KErrNotFound)
7906 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
7910 zoneDefragID = gZoneConfigArray[index].iZoneId;
7912 GetOriginalPageCount();
7913 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_DISCARD_FLAG);
7916 AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
7919 if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable ||
7920 (gZoneUtilArray[index].iAllocMovable <= gOriginalPageCountArray[index].iAllocMovable &&
7921 gZoneUtilArray[index].iAllocFixed == gOriginalPageCountArray[index].iAllocFixed))
7923 test.Printf(_L("Fail: Either discardable pages have been allocated or movable pages have not been allocated\n"));
7924 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
7929 test.Printf(_L("Passed...\n"));
7935 test.Next(_L("Test10: No Discardable Flag, Alloc Fixed"));
7940 r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
7944 index = GetBestZone(BEST_FIXED);
7946 if (index == KErrNotFound)
7948 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
7952 zoneDefragID = gZoneConfigArray[index].iZoneId;
7954 GetOriginalPageCount();
7955 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_DISCARD_FLAG);
7957 // Fill up all of RAM with fixed pages.
7958 r = Ldd.AllocateFixed(FILL_ALL_FIXED);
7961 if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable ||
7962 gZoneUtilArray[index].iAllocFixed <= gOriginalPageCountArray[index].iAllocFixed)
7964 test.Printf(_L("Fail: Discardable pages have been allocated or fixed pages have not been allocated\n"));
7965 CLEANUP(Ldd.FreeAllFixedPages());
7970 test.Printf(_L("Passed...\n"));
7976 test.Next(_L("Test11: No Discardable Flag, Alloc Discardable"));
7980 index = GetBestZone(BEST_DISCARDABLE);
7982 if (index == KErrNotFound)
7984 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
7988 zoneDefragID = gZoneConfigArray[index].iZoneId;
7989 GetOriginalPageCount();
7990 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_DISCARD_FLAG);
7994 TInt discardablePages;
7995 r = AllocDiscardable(discardablePages);
7996 if (r != KErrNoMemory)
7997 {// Allocation should fail as no dis flag is set
7998 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
8003 if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable)
8005 test.Printf(_L("Fail: Discardable pages have been allocated into the zone\n"));
8006 CLEANUP(ResetDPCache());
8011 test.Printf(_L("Passed...\n"));
8017 test.Printf(_L("Not a paged ROM - Skipping test step\n"));
8021 //-----------------------------------------------------------------------------------------------
8023 test.Next(_L("Test12: Only Discardable Flag, Alloc Movable"));
8026 index = GetBestZone(BEST_MOVABLE);
8028 if (index == KErrNotFound)
8030 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
8034 zoneDefragID = gZoneConfigArray[index].iZoneId;
8036 GetOriginalPageCount();
8037 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
8039 AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable, KChunkDefaultSize, EFalse);
8042 if (gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable)
8044 test.Printf(_L("Fail: Movable pages have been allocated\n"));
8045 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
8050 test.Printf(_L("Passed...\n"));
8056 test.Next(_L("Test13: Only Discardable Flag, Alloc Fixed"));
8061 r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
8065 index = GetBestZone(BEST_FIXED);
8067 if (index == KErrNotFound)
8069 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
8073 zoneDefragID = gZoneConfigArray[index].iZoneId;
8075 GetOriginalPageCount();
8076 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
8079 // Fill up all of RAM with fixed pages.
8080 r = Ldd.AllocateFixed(FILL_ALL_FIXED);
8083 if (gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
8085 test.Printf(_L("Fail: Fixed pages have been allocated\n"));
8086 CLEANUP(Ldd.FreeAllFixedPages());
8091 test.Printf(_L("Passed...\n"));
8097 test.Next(_L("Test14: Only Discardable Flag, Alloc Discardable"));
8101 index = GetBestZone(BEST_DISCARDABLE);
8103 if (index == KErrNotFound)
8105 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
8109 zoneDefragID = gZoneConfigArray[index].iZoneId;
8111 GetOriginalPageCount();
8112 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
8116 TInt discardablePages;
8117 r = AllocDiscardable(discardablePages);
8120 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
8125 if (gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable ||
8126 gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
8128 test.Printf(_L("Fail: Pages other than discardable have been allocated\n"));
8129 CLEANUP(ResetDPCache());
8134 test.Printf(_L("Passed...\n"));
8140 test.Printf(_L("Not a paged ROM - Skipping test step\n"));
8144 test.Next(_L("Test15: Only Discardable Flag, Alloc Fixed using ZoneAllocPhyicalRam"));
8147 index = GetBestZone(BEST_FIXED);
8149 if (index == KErrNotFound)
8151 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
8155 zoneDefragID = gZoneConfigArray[index].iZoneId;
8157 GetOriginalPageCount();
8158 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
8161 // Just need to try and allocate one page
8162 r = Ldd.ZoneAllocDiscontiguous(zoneDefragID, 1);
8167 test.Printf(_L("Fail: Fixed pages have not been allocated into zone ID 0x%x\n"), zoneDefragID);
8168 CLEANUP(Ldd.FreeAllFixedPages());
8173 test.Printf(_L("Passed...\n"));
8178 test.Next(_L("Test16: Only Discardable Flag, Alloc Fixed by attempting to claim zone"));
8183 while ((TUint)index < gZoneCount && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
8188 if ((TUint)index == gZoneCount)
8190 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
8194 zoneDefragID = gZoneConfigArray[index].iZoneId;
8195 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
8198 r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneDefragID);
8200 TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
8201 if (freeInOthers && r != KErrNone)
8203 test.Printf(_L("Fail: Claim zone ID 0x%x was unsuccessful, r = %d\n"), zoneDefragID, r);
8208 test.Printf(_L("Passed...\n"));
8212 //-----------------------------------------------------------------------------------------------
8214 test.Next(_L("Test17: No further alloc Flag, Alloc Movable"));
8217 index = GetBestZone(BEST_MOVABLE);
8220 if (index == KErrNotFound)
8222 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
8226 zoneDefragID = gZoneConfigArray[index].iZoneId;
8228 GetOriginalPageCount();
8229 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
8231 AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable, KChunkDefaultSize, EFalse);
8234 if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable ||
8235 gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable ||
8236 gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
8238 test.Printf(_L("Fail: Pages been allocated\n"));
8239 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
8244 test.Printf(_L("Passed...\n"));
8250 test.Next(_L("Test18: No further alloc Flag, Alloc Fixed"));
8255 r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
8259 // Find a zone with free pages and set the flag on this zone
8260 // as it will ensure that you cannot fill all of free RAM with fixed pages
8261 index = KErrNotFound;
8263 for (TUint i = 0; i < gZoneCount; i++)
8265 if (gZoneUtilArray[i].iFreePages != 0)
8271 if (index == KErrNotFound)
8273 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
8277 zoneDefragID = gZoneConfigArray[index].iZoneId;
8279 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
8282 GetOriginalPageCount();
8283 r = Ldd.AllocateFixed(FILL_ALL_FIXED);
8285 // Ensure memory wasn't filled as it should have hit the blocked zone.
8287 if (r != KErrNoMemory || gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
8289 test.Printf(_L("Fail: orig fix 0x%x current fix 0x%x\n"), gOriginalPageCountArray[index].iAllocFixed, gZoneUtilArray[index].iAllocFixed);
8290 CLEANUP(Ldd.FreeAllFixedPages());
8295 test.Printf(_L("Passed...\n"));
8301 test.Next(_L("Test19: No further alloc Flag, Alloc Discardable"));
8305 index = GetBestZone(BEST_DISCARDABLE);
8307 if (index == KErrNotFound)
8309 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
8313 zoneDefragID = gZoneConfigArray[index].iZoneId;
8315 GetOriginalPageCount();
8316 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
8319 TInt discardablePages;
8320 r = AllocDiscardable(discardablePages);
8321 if (r != KErrNoMemory)
8322 {// Allocation should fail as no alloc flag is set
8323 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
8328 if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable ||
8329 gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable ||
8330 gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
8332 test.Printf(_L("Fail: Pages have been allocated into the zone\n"));
8333 CLEANUP(ResetDPCache());
8338 test.Printf(_L("Passed...\n"));
8344 test.Printf(_L("Not a paged ROM - Skipping test step\n"));
8349 test.Next(_L("Test20: No Further Alloc Flag, Alloc Fixed using ZoneAllocPhyicalRam"));
8354 while ((TUint)index < gZoneCount && (gZoneUtilArray[index].iFreePages == 0 ||
8355 gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
8360 if ((TUint)index == gZoneCount)
8362 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
8366 zoneDefragID = gZoneConfigArray[index].iZoneId;
8368 GetOriginalPageCount();
8369 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
8372 // Just need to try and allocate one page
8373 r = Ldd.ZoneAllocDiscontiguous(zoneDefragID, 1);
8376 if (r != KErrNoMemory)
8378 test.Printf(_L("Fail: Fixed pages have been allocated into zone ID 0x%x\n"), zoneDefragID);
8379 CLEANUP(Ldd.FreeAllFixedPages());
8384 test.Printf(_L("Passed...\n"));
8389 test.Next(_L("Test21: No Further Alloc Flag, Alloc Fixed by attempting to claim zone"));
8394 while ((TUint)index < gZoneCount && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
8399 if ((TUint)index == gZoneCount)
8401 test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
8405 zoneDefragID = gZoneConfigArray[index].iZoneId;
8406 r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
8409 r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneDefragID);
8411 TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
8412 if (freeInOthers && r != KErrNone)
8414 test.Printf(_L("Fail: Claim zone ID 0x%x was unsuccessful, r = %d\n"), zoneDefragID, r);
8415 CLEANUP(ResetRamZoneFlags());
8420 test.Printf(_L("Passed...\n"));
8425 TUint mostPrefArrayIndex = 0;
8426 TUint mostPrefIndex = 0;
8427 TUint leastPrefIndex = 0;
8428 const TUint KFreeMostPref = 10;
8434 for (TUint testStep = 0; testStep < 2; testStep++)
8439 test.Next(_L("Test22: Ensure that the General Defrag looks at the flags 1"));
8443 test.Next(_L("Test23: Ensure that the General Defrag looks at the flags 2"));
8448 gChunkArray1 = new RChunk;
8449 gChunkArraySize1 = 1;
8450 TUint minCacheSize = 0;
8451 TUint maxCacheSize = 0;
8452 TUint currentCacheSize = 0;
8453 TUint freeNeededInMostPref = 0;
8456 test.Printf(_L("Not a paged ROM - Skipping...\n"));
8460 // Find the most pref zone with free pages
8461 r = FindMostPrefWithFree(mostPrefIndex, &mostPrefArrayIndex);
8464 test.Printf(_L("Cannot find zone with free pages - Skipping...\n"));
8468 // Ensure that the least pref zone is empty
8469 leastPrefIndex = gPrefArray[gZoneCount - 1];
8470 if (gZoneUtilArray[leastPrefIndex].iFreePages != gZoneUtilArray[leastPrefIndex].iPhysPages)
8472 test.Printf(_L("Least pref zone is not empty - Skipping...\n"));
8476 // Allocate 1 movable page to the least preferable zone
8477 r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, leastPrefIndex, 1);
8480 test.Printf(_L("Failed to allocate movable r = %d - Skipping...\n"), r);
8484 // Allocate 1 discardable page to the least preferable zone
8485 if (gZoneUtilArray[leastPrefIndex].iFreePages != 0)
8488 r = ZoneAllocDiscard(leastPrefIndex, 1, disPages);
8491 test.Printf(_L("Failed to allocate discardable pages r = %d - Skipping...\n"), r);
8495 // up the minimum cache size so that the pages have to be moved - not discarded
8496 DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
8497 r = DPTest::SetCacheSize(currentCacheSize, currentCacheSize);
8500 test.Printf(_L("Failed to set cache size r = %d - Skipping...\n"), r);
8506 test.Printf(_L("Least pref zone has no free pages - Skipping...\n"));
8510 // Check that the least pref zone has movable and discardable pages in it
8512 if (gZoneUtilArray[leastPrefIndex].iAllocMovable == 0 ||
8513 gZoneUtilArray[leastPrefIndex].iAllocDiscardable == 0)
8515 test.Printf(_L("No movable or discardable in least pref zone\n"));
8520 ResetRamZoneFlags();
8521 // if most pref zone has too many free pages fill up with fixed
8522 if (gZoneUtilArray[mostPrefIndex].iFreePages > KFreeMostPref)
8524 TUint allocPages = gZoneUtilArray[mostPrefIndex].iFreePages - KFreeMostPref;
8525 r = Ldd.ZoneAllocToMany(mostPrefIndex, allocPages);
8528 test.Printf(_L("Failed allocate 0x%x fixed to index %d r = %d - Skipping...\n"),
8529 allocPages, mostPrefIndex,r);
8536 // if the no. of discardable pages is less than free in most pref increase the min cache size
8538 if (gZoneUtilArray[leastPrefIndex].iAllocDiscardable < gZoneUtilArray[mostPrefIndex].iFreePages)
8540 TUint discDiff = gZoneUtilArray[mostPrefIndex].iFreePages - gZoneUtilArray[leastPrefIndex].iAllocDiscardable;
8541 test.Printf(_L("discDiff = 0x%x\n"), discDiff);
8543 if (ZoneAllocDiscard(leastPrefIndex, discDiff, disPages) != KErrNone)
8545 test.Printf(_L("Failed allocate discardable to zone index %d- Skipping...\n"), leastPrefIndex);
8548 // up the minimum cache size by the difference as we don't want these pages to be discarded
8549 DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
8550 r = DPTest::SetCacheSize(currentCacheSize, currentCacheSize);
8551 test.Printf(_L("r = %d\n"), r);
8554 test.Printf(_L("Failed to set cache size r = %d - Skipping...\n"), r);
8558 ResetRamZoneFlags();
8560 // if the rest of the zones are either full or empty put a fixed page into the next most pref empty zone
8564 for (; prefIndex < gZoneCount && totalFree < gZoneUtilArray[leastPrefIndex].iAllocDiscardable; prefIndex++)
8565 {// Look for zone that has enough free pages to fit all the discardable
8566 TUint zoneIndex = gPrefArray[prefIndex];
8567 if (zoneIndex != mostPrefIndex && zoneIndex != leastPrefIndex)
8569 if (gZoneUtilArray[zoneIndex].iFreePages != gZoneUtilArray[zoneIndex].iPhysPages)
8571 totalFree += gZoneUtilArray[zoneIndex].iFreePages;
8575 r = Ldd.ZoneAllocToMany(zoneIndex, 1);
8578 test.Printf(_L("Couldn't alloc fixed to zone index %d - r = %d\n"), zoneIndex, r);
8582 totalFree += gZoneUtilArray[zoneIndex].iFreePages;
8587 if (prefIndex >= gZoneCount)
8589 test.Printf(_L("Couldn't find zone\n"));
8595 // If the most preferable zone does not have enough free pages, skip
8596 freeNeededInMostPref = (testStep == 0) ? gZoneUtilArray[leastPrefIndex].iAllocMovable : gZoneUtilArray[leastPrefIndex].iAllocDiscardable;
8597 if (gZoneUtilArray[mostPrefIndex].iFreePages < freeNeededInMostPref)
8599 test.Printf(_L("Free needed in mostPref(%d) = %d, Free available in mostPref = %d - skipping...\n"),
8600 mostPrefIndex,freeNeededInMostPref,gZoneUtilArray[mostPrefIndex].iFreePages);
8605 // Set up the RAM zone flags for the test
8606 flag = (testStep == 0)? NO_MOVE_FLAG: NO_DISCARD_FLAG;
8607 // Set all zones except most pref to KRamZoneFlagNoMovable or KRamZoneFlagNoDiscard
8608 for (TUint index = 0; index < gZoneCount; index++)
8610 TUint zoneID = gZoneConfigArray[index].iZoneId;
8611 if (index != mostPrefIndex)
8613 r = Ldd.SetZoneFlag(zoneID, gZoneConfigArray[index].iFlags, flag);
8616 test.Printf(_L("Failed to set flag r = %d - Skipping...\n"), r);
8622 GetOriginalPageCount();
8623 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
8629 if (r != KErrNone ||
8630 gOriginalPageCountArray[mostPrefIndex].iAllocMovable >= gZoneUtilArray[mostPrefIndex].iAllocMovable ||
8631 gZoneUtilArray[leastPrefIndex].iFreePages != gZoneUtilArray[leastPrefIndex].iPhysPages)
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"),
8634 r, mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocMovable, gZoneUtilArray[mostPrefIndex].iAllocMovable,
8635 leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocMovable, gZoneUtilArray[leastPrefIndex].iAllocMovable,
8636 gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable, gZoneUtilArray[leastPrefIndex].iAllocDiscardable);
8637 CLEANUP(Ldd.FreeAllFixedPages());
8642 test.Printf(_L("Passed...\n"));
8647 if (r != KErrNone ||
8648 gOriginalPageCountArray[mostPrefIndex].iAllocDiscardable >= gZoneUtilArray[mostPrefIndex].iAllocDiscardable ||
8649 gZoneUtilArray[leastPrefIndex].iFreePages != gZoneUtilArray[leastPrefIndex].iPhysPages)
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"),
8652 r, mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocMovable, gZoneUtilArray[mostPrefIndex].iAllocMovable,
8653 leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocMovable, gZoneUtilArray[leastPrefIndex].iAllocMovable,
8654 gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable, gZoneUtilArray[leastPrefIndex].iAllocDiscardable);
8655 CLEANUP(Ldd.FreeAllFixedPages());
8660 test.Printf(_L("Passed...\n"));
8669 test.Next(_L("Test24: Ensure that the General Defrag doesnt move or discard pages if KRamZoneFlagNoMovable set on all zones "));
8671 gChunkArray1 = new RChunk;
8672 gChunkArraySize1 = 1;
8673 // Find the most pref zone with free pages
8676 if (FindMostPrefWithFree(mostPrefIndex, &mostPrefArrayIndex) != KErrNone)
8678 test.Printf(_L("Cannot find zone with free pages - Skipping...\n"));
8682 // Ensure that the least pref zone has free pages in it
8683 leastPrefIndex = gPrefArray[gZoneCount-1];
8684 if (gZoneUtilArray[leastPrefIndex].iFreePages == 0)
8686 test.Printf(_L("Least pref zone has no free pages - Skipping...\n"));
8690 // Allocate 1 movable page to the least preferable zone
8691 r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, leastPrefIndex, 1);
8694 test.Printf(_L("Failed to allocate movable page r = %d - Skipping...\n"), r);
8701 TUint minCacheSize = 0;
8702 TUint maxCacheSize = 0;
8703 TUint currentCacheSize = 0;
8705 // Allocate 1 discardable page to the least preferable zone
8706 if (gZoneUtilArray[leastPrefIndex].iFreePages != 0)
8708 r = ZoneAllocDiscard(leastPrefIndex, 1, disPages);
8711 test.Printf(_L("Discardable pages not allocated r= %d\n"), r);
8714 DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
8715 r = DPTest::SetCacheSize(currentCacheSize, currentCacheSize);
8718 test.Printf(_L("Failed to set cache size r = %d - Skipping...\n"), r);
8725 // Check that the least pref zone has movable and discardable (if allocated) pages in it
8727 if (gZoneUtilArray[leastPrefIndex].iAllocMovable == 0 ||
8728 gZoneUtilArray[leastPrefIndex].iAllocDiscardable < (TUint)disPages ||
8729 gZoneUtilArray[leastPrefIndex].iAllocMovable > gTotalPageCount.iFreePages - gZoneUtilArray[leastPrefIndex].iFreePages)
8731 test.Printf(_L("No movable in least pref zone or no space for moveable pages to be moved to\n"));
8736 ResetRamZoneFlags();
8738 // Now set all zones to KRamZoneFlagNoMovable
8739 for (TUint index = 0; index < gZoneCount; index++)
8741 TUint zoneID = gZoneConfigArray[index].iZoneId;
8742 r = Ldd.SetZoneFlag(zoneID, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
8745 test.Printf(_L("Failed to set cache size r = %d - Skipping...\n"), r);
8750 GetOriginalPageCount();
8751 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
8753 if (r != KErrNone ||
8754 gOriginalPageCountArray[leastPrefIndex].iAllocMovable != gZoneUtilArray[leastPrefIndex].iAllocMovable ||
8755 gZoneUtilArray[leastPrefIndex].iAllocDiscardable < gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable)
8757 test.Printf(_L("FAIL:r=%d LeastPref(%d): origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"),
8758 r,leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocMovable, gZoneUtilArray[leastPrefIndex].iAllocMovable,
8759 gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable, gZoneUtilArray[leastPrefIndex].iAllocDiscardable);
8760 CLEANUP(ResetRamZoneFlags());
8765 test.Printf(_L("Passed...\n"));
8776 // Template functions encapsulating ControlIo magic
8779 TInt controlIo(RFs &fs, TInt drv, TInt fkn, C &c)
8781 TPtr8 ptrC((TUint8 *)&c, sizeof(C), sizeof(C));
8783 TInt r = fs.ControlIo(drv, fkn, ptrC);
8791 // Formats the MMC card
8795 test.Printf(_L("Formatting MMC...\n"));
8798 TBuf<4> driveBuf = _L("D:\\");
8803 TInt r = theFs.Connect();
8806 r = theFs.DriveToChar(gDrive, driveLet);
8809 driveBuf[0] = driveLet;
8810 test.Printf(_L("Formatting Drive: %C\n"),(TInt)driveLet);
8812 r = format.Open(theFs,driveBuf,EFullFormat,count);
8817 TInt r = format.Next(count);
8828 // Generates file names to create the files
8830 void FileNameGen(TDes16& aBuffer, TInt aLong, TInt aPos)
8837 tempbuf.Format(KNumber,aPos);
8839 padding = aLong-tempbuf.Size()/2;
8844 aBuffer.Append('F');
8847 aBuffer.Append(tempbuf);
8849 _LIT(KExtension1, ".TXT");
8850 aBuffer.Append(KExtension1);
8857 // Creates the files to fill part of the read cache
8859 void CreateFiles(TInt aFiles, TInt aFileSize)
8863 TBuf16<50> directory;
8866 TBuf16<50> buffer(50);
8868 directory = gSessionPath;
8870 test.Printf(_L("Creating %d files for filling the cache (size %d)\n"), aFiles, aFileSize);
8872 // create a big buffer to speed things up
8873 HBufC8* bigBuf = NULL;
8874 const TInt KBigBufferSize = 32 * 1024;
8875 TRAPD(res,bigBuf = HBufC8::NewL(KBigBufferSize));
8876 test(res == KErrNone && bigBuf != NULL);
8878 TPtr8 bigBufWritePtr(NULL, 0);
8879 bigBufWritePtr.Set(bigBuf->Des());
8883 for(i = 0; i < KBigBufferSize; i++)
8885 bigBufWritePtr.Append((i%32) + aC);
8893 test.Printf(_L("Creating file %d of %d...\r"), i, aFiles);
8894 FileNameGen(buffer, 8, i+3) ;
8896 path.Append(buffer);
8898 // delete file first to ensure it's contents are not in the cache (file may be on the closed file queue)
8899 r = gTheFs.Delete(path);
8900 test(r == KErrNone || r == KErrNotFound);
8902 r = file.Create(gTheFs,path,EFileShareAny|EFileWrite|EFileReadDirectIO|EFileWriteDirectIO);
8903 if(r == KErrAlreadyExists)
8904 r = file.Open(gTheFs,path,EFileShareAny|EFileWrite|EFileReadDirectIO|EFileWriteDirectIO);
8907 while(j < aFileSize)
8909 bigBufWritePtr.SetLength(Min(KBigBufferSize, aFileSize - j));
8910 r = file.Write(bigBufWritePtr);
8911 // Running out of disk space is expected for the last file.
8912 // Premature "disk full" conditions need to abort.
8913 if (r == KErrDiskFull)
8919 j += bigBufWritePtr.Length();
8924 test.Printf(_L("\nFiles created\n"));
8932 // Allocate discardable pages using file system caching
8934 TInt FillCache(TInt aFiles, TInt aFileSize)
8936 // Fail if files already open
8937 test(!gFileCacheRun);
8941 TBuf16<50> directory;
8944 TBuf16<50> buffer(50);
8946 TPtr8 bufPtr(NULL, 0);
8948 TRAPD(res,buf = HBufC8::NewL(2));
8949 test(res == KErrNone && buf != NULL);
8950 bufPtr.Set(buf->Des());
8952 TESTDEBUG(test.Printf(_L("Filling the cache\n")));
8954 directory = gSessionPath;
8959 TESTDEBUG(test.Printf(_L("total disc pages = %d\n"), gTotalPageCount.iDiscardablePages));
8964 FileNameGen(buffer, 8, i+3) ;
8966 path.Append(buffer);
8967 r = gFile[i].Open(gTheFs,path,EFileShareAny|EFileRead|EFileReadBuffered|EFileReadAheadOff);
8971 while(j < aFileSize)
8973 r = gFile[i].Read(j,bufPtr);
8979 gFileCacheRun = ETrue;
8981 TESTDEBUG(test.Printf(_L("after - total disc pages = %d\n"), gTotalPageCount.iDiscardablePages));
8983 TESTDEBUG(test.Printf(_L("Cache filled\n")));
8985 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
8986 // get number of items on Page Cache
8987 TFileCacheStats startPageCacheStats;
8989 r = controlIo(gTheFs,gDrive, KControlIoFileCacheStats, startPageCacheStats);
8990 test.Printf(_L("control stats r= %d\n"), r);
8991 test(r==KErrNone || r == KErrNotSupported);
8993 TESTDEBUG(test.Printf(_L("Allocated segment count=%d\n"),startPageCacheStats.iAllocatedSegmentCount));
8995 // if we do not have any discardable pages then something went
8996 // wrong with file caching
8997 if (gTotalPageCount.iDiscardablePages == 0)
8998 return KErrNotSupported;
9008 // Sets up the MMC to be used by the test by creating files on the MMC
9018 TVolumeInfo volInfo;
9020 r = gTheFs.DriveToChar(gDrive, driveToTest);
9023 r = gTheFs.CharToDrive(driveToTest,gDrive);
9026 gSessionPath = _L("?:\\F32-TST\\");
9027 gSessionPath[0] = (TUint16) driveToTest;
9028 test.Printf(_L("Drive Letter=%C\n"),(TInt)driveToTest);
9031 r = gTheFs.Drive(info,gDrive);
9033 r = gTheFs.SetSessionPath(gSessionPath);
9035 r = gTheFs.MkDirAll(gSessionPath);
9036 if (r != KErrNone && r != KErrAlreadyExists)
9041 r = gTheFs.Volume(volInfo, gDrive);
9043 TInt64 gMediaSize = volInfo.iSize;
9045 // This calculation is approximate because the client cannot know
9046 // internal allocation mechanisms of the filesystem, i.e. how much
9047 // metadata is associated with a file of name X / size Y, whether
9048 // space used by such metadata is reflected in TDriveInfo::iSize and
9049 // what block/clustersize the filesystem will round filesizes to.
9050 // The last file that fills up the drive may therefore be partial
9051 // (smaller than this calculation predicts).
9052 TInt maxPossibleFiles = gFilesNeededToFillCache;
9053 test.Printf(_L("Original files needed = %d\n"), maxPossibleFiles);
9054 if(gMediaSize < (KDefaultCacheSize * maxPossibleFiles))
9056 maxPossibleFiles = (gMediaSize - 10) / KDefaultCacheSize;
9057 test.Printf(_L("Disk size is smaller - files needed = %d\n"), maxPossibleFiles);
9059 gFilesNeededToFillCache = maxPossibleFiles;
9060 CreateFiles(gFilesNeededToFillCache, KDefaultCacheSize);
9067 //---------------------------------------------------------------------------------------------------------------------
9068 //! @SYMTestCaseID KBASE-t_ramdefrag-0599
9069 //! @SYMTestType CIT
9070 //! @SYMTestCaseDesc Verifying that when File System Caching allocates dicardable pages,
9071 //! Defrag and allocation of fixed pages happens correctly.
9072 //! @SYMPREQ PREQ308
9073 //! @SYMTestPriority High
9075 //! 1. Fill the file system cache to allocate discardable pages
9076 //! following this call EmptyRamZone() in a zone with discardable pages.
9077 //! 2. Fill the file system cache to allocate discardable pages
9078 //! folling this allocate discontiguous fixed pages to a zone
9079 //! 3. Fill the file system cache to allocate discardable pages
9080 //! . following this allocate discontuguous fixed pages
9081 //! 4. Fill the file system cache to allocate discardable pages
9082 //! following this allocate less than 16 contiguous fixed pages
9083 //! 5. Fill the file system cache to allocate discardable pages
9084 //! following this allocate more than 16 contiguous fixed pages
9086 //! @SYMTestExpectedResults
9087 //! 1. Discardable pages are removed
9090 //! 4. KErrNone and numDiscardablePages != 0
9091 //! 5. KErrNone and numDiscardablePages = 0
9092 //---------------------------------------------------------------------------------------------------------------------
9093 TInt TestFileCaching()
9095 const TUint KDisPagesReq = 1;
9100 TUint minDiscardPages = 0;
9101 if (gDrive == KNoRemovableDrive)
9103 test.Start(_L("Cannot find drive to write files to - Skipping FS Caching Tests\n"));
9108 r = gTheFs.Connect();
9113 RRamDefragFuncTestLdd Ldd2;
9117 test.Start(_L("Test1: Test EmptyRamZone() clears file server cache pages"));
9120 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
9121 FreeMovable(gChunkArray1, gChunkArraySize1);
9123 r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
9126 test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
9127 goto skipFileCacheTests;
9129 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
9132 while (index < gZoneCount && gZoneUtilArray[index].iAllocDiscardable < KDisPagesReq)
9137 if (index == gZoneCount)
9139 test.Printf(_L("Cannot find zone to perform test on - Skipping test step...\n"));
9143 zoneID = gZoneConfigArray[index].iZoneId;
9145 r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, zoneID);
9148 if (gZoneUtilArray[index].iAllocDiscardable != 0)
9150 test.Printf(_L("Fail: Zone ID 0x%x has 0x%x discardable pages\n"), zoneID, gZoneUtilArray[index].iAllocDiscardable);
9155 test.Printf(_L("Passed...\n"));
9160 test.Next(_L("Test2: Filling the FS Cache and allocating fixed pages to a zone"));
9163 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
9164 FreeMovable(gChunkArray1, gChunkArraySize1);
9166 r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
9169 test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
9170 goto skipFileCacheTests;
9172 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
9176 while (index < gZoneCount && gZoneUtilArray[index].iAllocDiscardable < KDisPagesReq)
9181 if (index == gZoneCount)
9183 test.Printf(_L("Cannot find zone to perform test on - Skipping test step...\n"));
9187 zoneID = gZoneConfigArray[index].iZoneId;
9189 // Just need to attempt to allocate one more page than there is free in the zone
9190 allocSize = gZoneUtilArray[index].iFreePages + 1;
9192 test.Printf(_L("Allocating 0x%x fixed pages to zone ID 0x%x.....\n"), allocSize, zoneID);
9193 r = Ldd.ZoneAllocDiscontiguous(zoneID, allocSize);
9194 test.Printf(_L("r = %d\n"), r);
9199 test.Printf(_L("Fail: Fixed pages have not been allocated, r = %d, expected = %d\n"), r, KErrNone);
9204 test.Printf(_L("Passed...\n"));
9206 TESTDEBUG(test.Printf(_L("Freeing all Fixed Pages.....\n")));
9207 Ldd.FreeAllFixedPages();
9212 test.Next(_L("Test3: Filling the FS Cache and allocating fixed pages"));
9214 r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
9217 test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
9218 goto skipFileCacheTests;
9223 while (index < gZoneCount && gZoneUtilArray[index].iAllocDiscardable < KDisPagesReq)
9228 if (index == gZoneCount)
9230 test.Printf(_L("Cannot find zone to perform test on - Skipping test step...\n"));
9234 zoneID = gZoneConfigArray[index].iZoneId;
9238 TESTDEBUG(test.Printf(_L("Filling the remaining free pages with fixed pages.....\n")));
9239 Ldd.AllocateFixed(gTotalPageCount.iFreePages);
9241 test.Printf(_L("Allocating 0x%x fixed pages to zone ID 0x%x.....\n"), allocSize, zoneID);
9242 r = Ldd2.AllocateFixed(allocSize);
9244 TESTDEBUG(test.Printf(_L("r = %d\n"), r));
9249 test.Printf(_L("Fail: Fixed pages have not been allocated, r = %d, expected = %d\n"), r, KErrNone);
9250 CLEANUP(Ldd.FreeAllFixedPages());
9251 CLEANUP(Ldd2.FreeAllFixedPages());
9256 test.Printf(_L("Passed...\n"));
9258 TESTDEBUG(test.Printf(_L("Freeing all Fixed Pages.....\n")));
9259 Ldd2.FreeAllFixedPages();
9263 test.Next(_L("Test4: Filling the FS Cache and allocating less than 16 contiguous fixed pages"));
9266 r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
9269 test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
9270 goto skipFileCacheTests;
9272 allocSize = 14 << gPageShift;
9274 TESTDEBUG(test.Printf(_L("Filling the remaining free pages with fixed pages.....\n")));
9276 // Allocate the fixed array before getting any page counts
9277 for (TUint index = 0; index < gZoneCount; index++)
9280 if (gZoneUtilArray[index].iFreePages)
9282 r = Ldd.ZoneAllocToManyArray(index, gZoneUtilArray[index].iFreePages);
9285 test.Printf(_L("Failed to allocate fixed array Zone %d r = %d - Skipping...\n"), index, r);
9291 // Now fill all zones with fixed pages, 1 zone at a time
9292 // to avoid the discardable pages being disturbed
9293 for (TUint index = 0; index < gZoneCount; index++)
9296 if (gZoneUtilArray[index].iFreePages)
9298 r = Ldd.ZoneAllocToMany2(index, gZoneUtilArray[index].iFreePages);
9301 test.Printf(_L("Failed to allocate %d fixed to Zone %d r = %d - Skipping...\n"),
9302 gZoneUtilArray[index].iFreePages, index, r);
9309 test.Printf(_L("number of free pages = 0x%x\n"), gTotalPageCount.iFreePages);
9310 if (gTotalPageCount.iFreePages ||
9311 gTotalPageCount.iDiscardablePages <= (TUint)(allocSize >> gPageShift))
9313 test.Printf(_L("Setup failed - Skipping...\n"));
9317 test.Printf(_L("Allocating 0x%x fixed pages.....\n"), allocSize >> gPageShift);
9318 r = Ldd2.AllocContiguous(allocSize);
9319 TESTDEBUG(test.Printf(_L("r = %d\n"), r));
9322 if (r != KErrNone || !gTotalPageCount.iDiscardablePages)
9324 test.Printf(_L("Fail: Fixed pages have not been allocated, r = %d, expected = %d\n"), r, KErrNone);
9325 CLEANUP(Ldd.FreeAllFixedPages());
9326 CLEANUP(Ldd2.FreeAllFixedPages());
9331 test.Printf(_L("Passed...\n"));
9335 TESTDEBUG(test.Printf(_L("Freeing all Fixed Pages.....\n")));
9336 Ldd2.FreeAllFixedPages();
9339 test.Next(_L("Test5: Filling the FS Cache and allocating more than 16 contiguous fixed pages"));
9341 // TestEnd() will have reduced any cache pages to minimum so just get current
9342 // count of discardable pages.
9344 minDiscardPages = gTotalPageCount.iDiscardablePages;
9346 r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
9349 test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
9350 goto skipFileCacheTests;
9353 allocSize = 18 << gPageShift;
9355 TESTDEBUG(test.Printf(_L("Filling the remaining free pages with fixed pages.....\n")));
9358 // Allocate the fixed array before getting any page counts
9359 for (TUint index = 0; index < gZoneCount; index++)
9362 if (gZoneUtilArray[index].iFreePages)
9364 r = Ldd.ZoneAllocToManyArray(index, gZoneUtilArray[index].iFreePages);
9367 test.Printf(_L("Failed to allocate fixed array Zone %d r = %d - Skipping...\n"), index, r);
9373 // Now fill all zones with fixed pages, 1 zone at a time
9374 // to avoid the discardable pages being disturbed
9375 for (TUint index = 0; index < gZoneCount; index++)
9378 if (gZoneUtilArray[index].iFreePages)
9380 r = Ldd.ZoneAllocToMany2(index, gZoneUtilArray[index].iFreePages);
9383 test.Printf(_L("Failed to allocate %d fixed to Zone %d r = %d - Skipping...\n"),
9384 gZoneUtilArray[index].iFreePages, index, r);
9391 test.Printf(_L("number of free pages = 0x%x\n"), gTotalPageCount.iFreePages);
9392 if (gTotalPageCount.iFreePages)
9394 test.Printf(_L("Setup failed - Skipping...\n"));
9398 test.Printf(_L("Allocating 0x%x fixed pages.....\n"), allocSize >> gPageShift);
9399 r = Ldd2.AllocContiguous(allocSize);
9400 TESTDEBUG(test.Printf(_L("r = %d\n"), r));
9403 if (r != KErrNone || gTotalPageCount.iDiscardablePages != minDiscardPages)
9405 test.Printf(_L("Fail: r = %d, expected = %d - Discardable Pages = 0x%x, expected = %d\n"),
9406 r, KErrNone, gTotalPageCount.iDiscardablePages, minDiscardPages);
9407 CLEANUP(Ldd.FreeAllFixedPages());
9408 CLEANUP(Ldd2.FreeAllFixedPages());
9413 test.Printf(_L("Passed...\n"));
9419 TESTDEBUG(test.Printf(_L("Freeing all Fixed Pages.....\n")));
9420 Ldd2.FreeAllFixedPages();
9431 // TestOneZoneConfig
9433 //---------------------------------------------------------------------------------------------------------------------
9434 //! @SYMTestCaseID KBASE-t_ramdefrag-0600
9435 //! @SYMTestType CIT
9436 //! @SYMTestCaseDesc Verifying that when only 1 zone is cofigured in the variant, that
9437 //! the defrag and allocation of fixed pages happend correctly
9438 //! @SYMPREQ PREQ308
9439 //! @SYMTestPriority High
9441 //! 1. Fragment the memory and perform a DefragRam
9442 //! 2. Fragment the memory and perform an EmptyZone
9443 //! 3. Fragment the memory and perform a ClaimZone
9444 //! 4. Call ZoneAllocPhysicalRam to allocate discontiguous fixed pages
9445 //! 5. Call ZoneAllocPhysicalRam to allocate contiguous fixed pages
9447 //! @SYMTestExpectedResults
9448 //! 1. Number of pages does not differ from the original
9449 //! 2. KErrNoMemroy and discardable pages are discarded of
9453 //---------------------------------------------------------------------------------------------------------------------
9454 TInt TestOneZoneConfig()
9456 TInt r = gTheFs.Connect();
9459 if (gDrive != KNoRemovableDrive)
9466 TUint zoneID = gZoneConfigArray[index].iZoneId;
9467 test.Printf(_L("Zone ID = 0x%x\n"), zoneID);
9468 TUint minCacheSize = 0;
9469 TUint maxCacheSize = 0;
9470 TUint currentCacheSize = 0;
9474 TUint origUnknown = 0;
9475 TUint origFixed = 0;
9476 TUint origMovable = 0;
9477 TUint origDiscard = 0;
9478 TUint origOther = 0;
9482 DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
9483 TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"),
9484 minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
9486 TUint setMin = 60 << gPageShift;
9488 TInt r = DPTest::SetCacheSize(setMin, setMax);
9491 test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
9492 CLEANUP(ResetDPCache());
9495 DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
9496 TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"),
9497 minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
9500 test.Start(_L("Test1: Fragmenting the memory and performing a general defrag"));
9503 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
9504 FreeMovable(gChunkArray1, gChunkArraySize1);
9506 if (gDrive != KNoRemovableDrive)
9508 FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
9512 test.Printf(_L("Cannot find drive to write files to - Not allocating discardable pages through FS Caching\n"));
9516 origFree = gTotalPageCount.iFreePages;
9517 origUnknown = gTotalPageCount.iUnknownPages;
9518 origFixed = gTotalPageCount.iFixedPages;
9519 origMovable = gTotalPageCount.iMovablePages;
9520 origDiscard = gTotalPageCount.iDiscardablePages;
9521 origOther = gTotalPageCount.iOtherPages;
9523 r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
9527 if ((origUnknown != gTotalPageCount.iUnknownPages) ||
9528 (origFixed != gTotalPageCount.iFixedPages) ||
9529 (origMovable != gTotalPageCount.iMovablePages) ||
9530 (origDiscard != gTotalPageCount.iDiscardablePages) ||
9531 (origOther != gTotalPageCount.iOtherPages))
9533 test.Printf(_L("Fail: Pages after defrag are not equal to those before"));
9534 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
9539 test.Printf(_L("Passed..."));
9541 // This will free any allocated memory
9545 test.Next(_L("Test2: Fragmenting the memory and performing an EmptyZone"));
9548 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
9549 FreeMovable(gChunkArray1, gChunkArraySize1);
9551 if (gDrive != KNoRemovableDrive)
9553 FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
9557 test.Printf(_L("Cannot find drive to write files to - Not allocating discardable pages through FS Caching\n"));
9559 r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, zoneID);
9561 if (r != KErrNoMemory || CheckZoneIsOff(index))
9563 test.Printf(_L("Fail: r = %d, expected = -4"), r);
9564 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
9569 test.Printf(_L("Passed..."));
9571 // This will free any allocated memory
9574 test.Next(_L("Test3: Fragmenting the memory and performing a ClaimZone"));
9577 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
9578 FreeMovable(gChunkArray1, gChunkArraySize1);
9580 if (gDrive != KNoRemovableDrive)
9582 FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
9586 test.Printf(_L("Cannot find drive to write files to - Not allocating discardable pages through FS Caching\n"));
9590 origMovable = gTotalPageCount.iMovablePages;
9592 r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneID);
9595 if (r != KErrNoMemory || origMovable != gTotalPageCount.iMovablePages)
9597 test.Printf(_L("Fail: r = %d, expected = -4"), r);
9598 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
9603 test.Printf(_L("Passed..."));
9605 // This will free any allocated memory.
9608 test.Next(_L("Test4: Calling ZoneAllocPhysicalRam to allocate discontiguous fixed pages"));
9611 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
9612 FreeMovable(gChunkArray1, gChunkArraySize1);
9615 origFree = gTotalPageCount.iFreePages;
9616 origUnknown = gTotalPageCount.iUnknownPages;
9617 origFixed = gTotalPageCount.iFixedPages;
9618 origMovable = gTotalPageCount.iMovablePages;
9619 origDiscard = gTotalPageCount.iDiscardablePages;
9620 origOther = gTotalPageCount.iOtherPages;
9622 r = Ldd.ZoneAllocDiscontiguous(zoneID, (TInt)(origFree / 2));
9625 if (gTotalPageCount.iFixedPages < (origFixed + (origFree / 2)))
9627 test.Printf(_L("Fail: fixed pages = 0x%x, expected >= 0x%x\n"),
9628 gTotalPageCount.iFixedPages, (origFixed + (origFree / 2)));
9629 CLEANUP(Ldd.FreeAllFixedPages());
9630 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
9635 test.Printf(_L("Passed..."));
9637 Ldd.FreeAllFixedPages();
9638 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
9642 test.Next(_L("Test5: Calling ZoneAllocPhysicalRam to allocate contiguous fixed pages"));
9645 AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
9646 FreeMovable(gChunkArray1, gChunkArraySize1);
9649 origFree = gTotalPageCount.iFreePages;
9650 origUnknown = gTotalPageCount.iUnknownPages;
9651 origFixed = gTotalPageCount.iFixedPages;
9652 origMovable = gTotalPageCount.iMovablePages;
9653 origDiscard = gTotalPageCount.iDiscardablePages;
9654 origOther = gTotalPageCount.iOtherPages;
9656 TInt allocSize = 50 << gPageShift;
9658 r = Ldd.ZoneAllocContiguous(zoneID, allocSize);
9661 if (gTotalPageCount.iFixedPages < (origFixed + (allocSize >> gPageShift)))
9663 test.Printf(_L("Fail: fixed pages = 0x%x, expected >= 0x%x\n"),
9664 gTotalPageCount.iFixedPages, (origFixed + (allocSize >> gPageShift)));
9665 CLEANUP(Ldd.FreeAllFixedPages());
9666 CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
9671 test.Printf(_L("Passed..."));
9673 Ldd.FreeAllFixedPages();
9674 RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
9679 test_KErrNone(DPTest::FlushCache());
9684 if (gDrive != KNoRemovableDrive)
9697 // List of defrag tests to be run
9699 void RunDefragTests()
9701 test.Start(_L("Testing the moving of pages in a defrag"));
9704 test.Next(_L("Verifying the implementation of the function TRamDefragRequest::DefragRam() arg aMaxPages"));
9705 TestDefragRamMaxPages();
9707 test.Next(_L("Verifying the implementation of the function TRamDefragRequest::EmptyRamZone()"));
9710 test.Next(_L("Verifying the implementation of the function Epoc::GetRamZonePageCount()"));
9711 TestGetRamZonePageCount();
9713 test.Next(_L("Verifying the implementation of the function TRamDefragRequest::ClaimRamZone()"));
9716 test.Next(_L("Verifying the implementation of the function TRamDefragRequest::Cancel()"));
9719 test.Next(_L("Verifying that pages are moved correctly"));
9722 test.Next(_L("Verifying Semaphore Methods of the Defrag"));
9723 TestDefragSemMethod();
9725 test.Next(_L("Verifying Dfc Methods of the Defrag"));
9726 TestDefragDfcMethod();
9728 test.Next(_L("Testing priorities"));
9731 test.Next(_L("Testing File System Caching"));
9738 test.Printf(_L("Skipping... \n"));
9741 test.Next(_L("Testing general RAM defrag implementation"));
9751 // List of allocating tests to be run
9752 // These tests only need to be executed once
9754 void RunAllocTests()
9756 test.Start(_L("Verifying the allocating strategies"));
9757 TestAllocStrategies();
9759 test.Next(_L("Verifying the contiguous overload of Epoc::ZoneAllocPhysicalRam()"));
9760 TestZoneAllocContiguous();
9762 test.Next(_L("Verifying the discontiguous overload of Epoc::ZoneAllocPhysicalRam()"));
9763 TestZoneAllocDiscontiguous();
9765 test.Next(_L("Test Free Zone"));
9768 test.Next(_L("Testing zone flags"));
9778 // Main entry point.
9783 DeviceDriver(TEST_DRIVER_OPEN);
9786 TInt r = TestSetup();
9789 test.Printf(_L("Test Setup failed, r = %d\n"), r);
9794 if (gZoneCount == 1)
9798 test.Start(_L("Zone Count 1..."));
9799 TestOneZoneConfig();
9803 test.Start(_L("Running Alloc tests"));
9807 Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
9810 test.Next(_L("Running Defrag tests"));
9814 Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
9818 test.Printf(_L("The total number of test steps failed = %d\n"), gTestStepFailed);
9819 test_Equal(KErrNone, gTestStepFailed);
9823 DeviceDriver(TEST_DRIVER_CLOSE);