kerneltest/e32test/demandpaging/t_thrash.cpp
changeset 0 a41df078684a
child 6 0173bcd7697c
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32test\demandpaging\t_thrash.cpp
       
    15 // todo: test combinations of rom / code / data paging
       
    16 //
       
    17 
       
    18 #define __E32TEST_EXTENSION__
       
    19 #include <e32test.h>
       
    20 #include <dptest.h>
       
    21 #include <e32hal.h>
       
    22 #include <u32hal.h>
       
    23 #include <u32exec.h>
       
    24 #include <e32svr.h>
       
    25 #include <e32panic.h>
       
    26 #include "u32std.h"
       
    27 #include <e32msgqueue.h>
       
    28 #include <e32atomics.h>
       
    29 #include <e32math.h>
       
    30 
       
    31 #include "t_dpcmn.h"
       
    32 #include "../mmu/mmudetect.h"
       
    33 #include "../mmu/d_memorytest.h"
       
    34 #include "../mmu/t_codepaging_dll.h"
       
    35 
       
    36 RTest test(_L("T_THRASH"));
       
    37 
       
    38 volatile TBool gRunThrashTest = EFalse;
       
    39 
       
    40 _LIT(KChunkName, "t_thrash chunk");
       
    41 
       
    42 class TRandom
       
    43 	{
       
    44 public:
       
    45 	TRandom();
       
    46 	TUint32 Next();
       
    47 
       
    48 private:
       
    49 	enum
       
    50 		{
       
    51 		KA = 1664525,
       
    52 		KB = 1013904223
       
    53 		};
       
    54 	TUint32 iV;
       
    55 	};
       
    56 
       
    57 TRandom::TRandom()
       
    58 	{
       
    59 	iV = (TUint32)this + RThread().Id() + User::FastCounter() + 23;
       
    60 	}
       
    61 
       
    62 TUint32 TRandom::Next()
       
    63 	{
       
    64 	iV = KA * iV + KB;
       
    65 	return iV;
       
    66 	}
       
    67 
       
    68 void CreatePagedChunk(TInt aSizeInPages)
       
    69 	{
       
    70 	test_Equal(0,gChunk.Handle());
       
    71 	
       
    72 	TChunkCreateInfo createInfo;
       
    73 	TInt size = aSizeInPages * gPageSize;
       
    74 	createInfo.SetNormal(size, size);
       
    75 	createInfo.SetPaging(TChunkCreateInfo::EPaged);
       
    76 	createInfo.SetOwner(EOwnerProcess);
       
    77 	createInfo.SetGlobal(KChunkName);
       
    78 	test_KErrNone(gChunk.Create(createInfo));
       
    79 	test(gChunk.IsPaged()); // this is only ever called if data paging is supported
       
    80 	}
       
    81 
       
    82 TUint32* PageBasePtr(TInt aPage)
       
    83 	{
       
    84 	return (TUint32*)(gChunk.Base() + (gPageSize * aPage));
       
    85 	}
       
    86 
       
    87 enum TWorkload
       
    88 	{
       
    89 	EWorkloadSequential,
       
    90 	EWorkloadRandom,
       
    91 	EWorkloadShuffle
       
    92 	};
       
    93 
       
    94 struct SThrashTestArgs
       
    95 	{
       
    96 	TInt iThreadGroup;
       
    97 	TInt iGroupSize;
       
    98 	TWorkload iWorkload;
       
    99 	TUint8* iBasePtr;
       
   100 	volatile TInt iPageCount;
       
   101 	volatile TInt64 iAccesses;
       
   102 	};
       
   103 
       
   104 TInt ThrashTestFunc(TAny* aArg)
       
   105 	{
       
   106 	SThrashTestArgs* args = (SThrashTestArgs*)aArg;
       
   107 
       
   108 	TRandom random;
       
   109 	TInt startPage = args->iThreadGroup * args->iGroupSize;
       
   110 	TInt* ptr = (TInt*)(args->iBasePtr + startPage * gPageSize);
       
   111 	switch (args->iWorkload)
       
   112 		{
       
   113 		case EWorkloadSequential:
       
   114 			while (gRunThrashTest)
       
   115 				{
       
   116 				TInt size = (args->iPageCount * gPageSize) / sizeof(TInt);
       
   117 				for (TInt i = 0 ; i < size && gRunThrashTest ; ++i)
       
   118 					{
       
   119 					ptr[i] = random.Next();
       
   120 					__e32_atomic_add_ord64(&args->iAccesses, 1);
       
   121 					}
       
   122 				}
       
   123 			break;
       
   124 				
       
   125 		case EWorkloadRandom:
       
   126 			{
       
   127 			TInt acc = 0;
       
   128 			while (gRunThrashTest)
       
   129 				{
       
   130 				TInt size = (args->iPageCount * gPageSize) / sizeof(TInt);
       
   131 				for (TInt i = 0 ; i < size && gRunThrashTest ; ++i)
       
   132 					{
       
   133 					TUint32 rand = random.Next();
       
   134 					TInt action = rand >> 31;
       
   135 					TInt r = rand % size;
       
   136 					if (action == 0)
       
   137 						acc += ptr[r];
       
   138 					else
       
   139 						ptr[r] = acc;
       
   140 					__e32_atomic_add_ord64(&args->iAccesses, 1);
       
   141 					}
       
   142 				}
       
   143 			}
       
   144 			break;
       
   145 			
       
   146 		case EWorkloadShuffle:
       
   147 			{
       
   148 			TInt i;
       
   149 			while (gRunThrashTest)
       
   150 				{
       
   151 				TInt size = (args->iPageCount * gPageSize) / sizeof(TInt);
       
   152 				for (i = 0 ; gRunThrashTest && i < (size - 1) ; ++i)
       
   153 					{
       
   154 					Mem::Swap(&ptr[i], &ptr[i + random.Next() % (size - i - 1) + 1], sizeof(TInt));
       
   155 					__e32_atomic_add_ord64(&args->iAccesses, 2);
       
   156 					}
       
   157 				}
       
   158 			}
       
   159 			break;
       
   160 
       
   161 		default:
       
   162 			test(EFalse);
       
   163 		}
       
   164 
       
   165 	return KErrNone;;
       
   166 	}
       
   167 
       
   168 struct SThrashThreadData
       
   169 	{
       
   170 	RThread iThread;
       
   171 	TRequestStatus iStatus;
       
   172 	SThrashTestArgs iArgs;
       
   173 	};
       
   174 
       
   175 void ThrashTest(TInt aThreads,			// number of threads to run
       
   176 				TBool aSharedData,		// whether all threads share the same data
       
   177 				TWorkload aWorkload,
       
   178 				TInt aBeginPages,		// number of pages to start with for last/all threads
       
   179 				TInt aEndPages,			// number of pages to end with for last/all threads
       
   180 				TInt aOtherPages)		// num of pages for other threads, or zero to use same value for all
       
   181 	{
       
   182 	RDebug::Printf("\nPages Accesses     ThL");
       
   183 
       
   184 	DPTest::FlushCache();
       
   185 	User::After(1000000);
       
   186 
       
   187 	TInt pagesNeeded;
       
   188 	TInt maxPages = Max(aBeginPages, aEndPages);
       
   189 	TInt groupSize = 0;
       
   190 	if (aSharedData)
       
   191 		pagesNeeded = Max(maxPages, aOtherPages);
       
   192 	else
       
   193 		{
       
   194 		if (aOtherPages)
       
   195 			{
       
   196 			groupSize = aOtherPages;
       
   197 			pagesNeeded = (aThreads - 1) * aOtherPages + maxPages;
       
   198 			}
       
   199 		else
       
   200 			{
       
   201 			groupSize = maxPages;
       
   202 			pagesNeeded = aThreads * maxPages;
       
   203 			}
       
   204 		}
       
   205 	CreatePagedChunk(pagesNeeded);
       
   206 	
       
   207 	SThrashThreadData* threads = new SThrashThreadData[aThreads];
       
   208 	test_NotNull(threads);
       
   209 	
       
   210 	gRunThrashTest = ETrue;
       
   211 	TInt pageCount = aBeginPages;
       
   212 	const TInt maxSteps = 30;
       
   213 	TInt step = aEndPages >= aBeginPages ? Max((aEndPages - aBeginPages) / maxSteps, 1) : Min((aEndPages - aBeginPages) / maxSteps, -1);
       
   214 	
       
   215 	TInt i;
       
   216 	for (i = 0 ; i < aThreads ; ++i)
       
   217 		{
       
   218 		SThrashThreadData& thread = threads[i];
       
   219 		thread.iArgs.iThreadGroup = aSharedData ? 0 : i;
       
   220 		thread.iArgs.iGroupSize = groupSize;
       
   221 		thread.iArgs.iWorkload = aWorkload;
       
   222 		thread.iArgs.iBasePtr = gChunk.Base();
       
   223 		if (aOtherPages)
       
   224 			thread.iArgs.iPageCount = (i == aThreads - 1) ? pageCount : aOtherPages;
       
   225 		else
       
   226 			thread.iArgs.iPageCount = pageCount;
       
   227 		test_KErrNone(thread.iThread.Create(KNullDesC, ThrashTestFunc, gPageSize, NULL, &thread.iArgs));
       
   228 		thread.iThread.Logon(thread.iStatus);
       
   229 		thread.iThread.SetPriority(EPriorityLess);
       
   230 		threads[i].iThread.Resume();
       
   231 		}
       
   232 
       
   233 	for (;;)
       
   234 		{
       
   235 		if (aOtherPages)
       
   236 			threads[aThreads - 1].iArgs.iPageCount = pageCount;
       
   237 		else
       
   238 			{
       
   239 			for (i = 0 ; i < aThreads ; ++i)
       
   240 				threads[i].iArgs.iPageCount = pageCount;
       
   241 			}
       
   242 		
       
   243 		for (i = 0 ; i < aThreads ; ++i)
       
   244 			__e32_atomic_store_ord64(&threads[i].iArgs.iAccesses, 0);
       
   245 		
       
   246 		User::After(2000000);		
       
   247 		TInt thrashLevel = UserSvr::HalFunction(EHalGroupVM, EVMHalGetThrashLevel, 0, 0);
       
   248 		test(thrashLevel >= 0 && thrashLevel <= 255);
       
   249 		
       
   250 		TInt64 totalAccesses = 0;
       
   251 		TInt totalPages = 0;
       
   252 		for (i = 0 ; i < aThreads ; ++i)
       
   253 			{
       
   254 			totalAccesses += __e32_atomic_load_acq64(&threads[i].iArgs.iAccesses);
       
   255 			if (aSharedData)
       
   256 				totalPages = Max(totalPages, threads[i].iArgs.iPageCount);
       
   257 			else
       
   258 				totalPages += threads[i].iArgs.iPageCount;
       
   259 			}
       
   260 
       
   261 		test.Printf(_L("%5d %12ld %3d"), totalPages, totalAccesses, thrashLevel);
       
   262 		for (i = 0 ; i < aThreads ; ++i)
       
   263 			{
       
   264 			test.Printf(_L(" %5d %12ld"),
       
   265 						threads[i].iArgs.iPageCount,
       
   266 						__e32_atomic_load_acq64(&threads[i].iArgs.iAccesses));
       
   267 			test_Equal(KRequestPending, threads[i].iStatus.Int());
       
   268 			}
       
   269 		test.Printf(_L("\n"));
       
   270 
       
   271 		if (aEndPages >= aBeginPages ? pageCount >= aEndPages : pageCount < aEndPages)
       
   272 			break;
       
   273 		pageCount += step;
       
   274 		}
       
   275 	
       
   276 	gRunThrashTest = EFalse;
       
   277 	
       
   278 	for (i = 0 ; i < aThreads ; ++i)
       
   279 		{
       
   280 		SThrashThreadData& thread = threads[i];
       
   281 		User::WaitForRequest(thread.iStatus);
       
   282 		test_Equal(EExitKill, thread.iThread.ExitType());
       
   283 		test_KErrNone(thread.iStatus.Int());
       
   284 		thread.iThread.Close();
       
   285 		}
       
   286 
       
   287 	gChunk.Close();	
       
   288 	RDebug::Printf("\n");
       
   289 	}
       
   290 
       
   291 void TestThrashing()
       
   292 	{
       
   293 	TInt minPages = (3 * gMaxCacheSize) / 4 - 4;
       
   294 	TInt maxPages = (5 * gMaxCacheSize) / 4;
       
   295 	TInt minPages2 = (3 * gMaxCacheSize) / 8 - 4;
       
   296 	TInt maxPages2 = (5 * gMaxCacheSize) / 8;
       
   297 	TInt minPages4 = (3 * gMaxCacheSize) / 16 - 4;
       
   298 	TInt maxPages4 = (5 * gMaxCacheSize) / 16;
       
   299 
       
   300 	// Single thread increasing in size
       
   301 	test.Next(_L("Thrash test: single thread, sequential workload"));
       
   302 	ThrashTest(1, ETrue, EWorkloadSequential, minPages, maxPages, 0);
       
   303 	
       
   304 	test.Next(_L("Thrash test: single thread, random workload"));
       
   305 	ThrashTest(1, ETrue, EWorkloadRandom, minPages, maxPages, 0);
       
   306 	
       
   307 	test.Next(_L("Thrash test: single thread, shuffle workload"));
       
   308 	ThrashTest(1, ETrue, EWorkloadShuffle, minPages, maxPages, 0);
       
   309 
       
   310 	// Multiple threads with shared data, one thread incresing in size
       
   311 	test.Next(_L("Thrash test: two threads with shared data, one thread increasing, random workload"));
       
   312 	ThrashTest(2, ETrue, EWorkloadRandom, minPages, maxPages, minPages);
       
   313 	
       
   314 	test.Next(_L("Thrash test: four threads with shared data, one thread increasing, random workload"));
       
   315 	ThrashTest(4, ETrue, EWorkloadRandom, minPages, maxPages, minPages);
       
   316 
       
   317 	// Multiple threads with shared data, all threads incresing in size
       
   318 	test.Next(_L("Thrash test: two threads with shared data, all threads increasing, random workload"));
       
   319 	ThrashTest(2, ETrue, EWorkloadRandom, minPages, maxPages, 0);
       
   320 	
       
   321 	test.Next(_L("Thrash test: four threads with shared data, all threads increasing, random workload"));
       
   322 	ThrashTest(4, ETrue, EWorkloadRandom, minPages, maxPages, 0);
       
   323 	
       
   324 	// Multiple threads with independent data, one thread incresing in size
       
   325 	test.Next(_L("Thrash test: two threads with independent data, one thread increasing, random workload"));
       
   326 	ThrashTest(2, EFalse, EWorkloadRandom, minPages2, maxPages2, gMaxCacheSize / 2);
       
   327 	
       
   328 	test.Next(_L("Thrash test: four threads with independent data, one thread increasing, random workload"));
       
   329 	ThrashTest(4, EFalse, EWorkloadRandom, minPages4, maxPages4, gMaxCacheSize / 4);
       
   330 	
       
   331 	// Multiple threads with independant data, all threads incresing in size
       
   332 	test.Next(_L("Thrash test: two threads with independent data, all threads increasing, random workload"));
       
   333 	ThrashTest(2, EFalse, EWorkloadRandom, minPages2, maxPages2, 0);
       
   334 
       
   335 	test.Next(_L("Thrash test: four threads with independent data, all threads increasing, random workload"));
       
   336 	ThrashTest(4, EFalse, EWorkloadRandom, minPages4, maxPages4, 0);
       
   337 
       
   338 	// Attempt to create thrash state where there is sufficient cache
       
   339 	test.Next(_L("Thrash test: two threads with independent data, one threads decreasing, random workload"));
       
   340 	TInt halfCacheSize = gMaxCacheSize / 2;
       
   341 	ThrashTest(2, EFalse, EWorkloadRandom, halfCacheSize + 10, halfCacheSize - 30, halfCacheSize);
       
   342 	}
       
   343 
       
   344 void TestThrashHal()
       
   345 	{			 
       
   346 	test.Next(_L("Test EVMHalSetThrashThresholds"));
       
   347 	test_Equal(KErrArgument, UserSvr::HalFunction(EHalGroupVM, EVMHalSetThrashThresholds, (TAny*)256, 0));
       
   348 	test_Equal(KErrArgument, UserSvr::HalFunction(EHalGroupVM, EVMHalSetThrashThresholds, (TAny*)0, (TAny*)1));
       
   349 	test_KErrNone(UserSvr::HalFunction(EHalGroupVM, EVMHalSetThrashThresholds, 0, 0));
       
   350 	test_KErrNone(UserSvr::HalFunction(EHalGroupVM, EVMHalSetThrashThresholds, (TAny*)255, 0));
       
   351 	test_KErrNone(UserSvr::HalFunction(EHalGroupVM, EVMHalSetThrashThresholds, (TAny*)200, (TAny*)150));
       
   352 
       
   353 	test.Next(_L("Test EVMHalGetThrashLevel"));
       
   354 	User::After(2000000);
       
   355 	TInt r = UserSvr::HalFunction(EHalGroupVM, EVMHalGetThrashLevel, 0, 0);
       
   356 	test(r >= 0 && r <= 255);
       
   357 	test.Printf(_L("Thrash level == %d\n"), r);
       
   358 	test(r <= 10);  // should indicate lightly loaded system
       
   359 
       
   360 	if (!gDataPagingSupported)
       
   361 		return;  // rest of this test relies on data paging
       
   362 
       
   363 	// set up thrashing notification
       
   364 	RChangeNotifier notifier;
       
   365 	test_KErrNone(notifier.Create());
       
   366 	TRequestStatus status;
       
   367 	test_KErrNone(notifier.Logon(status));
       
   368 	test_KErrNone(notifier.Logon(status));  // first logon completes immediately
       
   369 	test_Equal(KRequestPending, status.Int());
       
   370 	
       
   371 	// stress system and check thrash level and notification
       
   372 	ThrashTest(1, ETrue, EWorkloadRandom, gMaxCacheSize * 2, gMaxCacheSize * 2 + 5, 0);
       
   373 	r = UserSvr::HalFunction(EHalGroupVM, EVMHalGetThrashLevel, 0, 0);
       
   374 	test(r >= 0 && r <= 255);
       
   375 	test.Printf(_L("Thrash level == %d\n"), r);
       
   376 	test(r > 200);  // should indicate thrashing
       
   377 	test_Equal(EChangesThrashLevel, status.Int());
       
   378 	User::WaitForAnyRequest();
       
   379 
       
   380 	// wait for system to calm down and check notification again
       
   381 	test_KErrNone(notifier.Logon(status));
       
   382 	User::WaitForAnyRequest();
       
   383 	test_Equal(EChangesThreadDeath, status.Int());
       
   384 
       
   385 	test_KErrNone(notifier.Logon(status));
       
   386 	User::After(2000000);
       
   387 	r = UserSvr::HalFunction(EHalGroupVM, EVMHalGetThrashLevel, 0, 0);
       
   388 	test(r >= 0 && r <= 255);
       
   389 	test.Printf(_L("Thrash level == %d\n"), r);
       
   390 	test(r <= 10);  // should indicate lightly loaded system
       
   391 	test_Equal(EChangesThrashLevel, status.Int());	
       
   392 	User::WaitForAnyRequest();
       
   393 	}
       
   394 
       
   395 void TestThrashHalNotSupported()
       
   396 	{
       
   397 	test_Equal(KErrNotSupported, UserSvr::HalFunction(EHalGroupVM, EVMHalGetThrashLevel, 0, 0));
       
   398 	test_Equal(KErrNotSupported, UserSvr::HalFunction(EHalGroupVM, EVMHalSetThrashThresholds, 0, 0));
       
   399 	}
       
   400 
       
   401 TInt E32Main()
       
   402 	{
       
   403 	test.Title();
       
   404 	test.Start(_L("Test thrashing monitor"));
       
   405 
       
   406 	test_KErrNone(GetGlobalPolicies());
       
   407 
       
   408 	TBool flexibleMemoryModel = (MemModelAttributes() & EMemModelTypeMask) == EMemModelTypeFlexible;
       
   409 	if (flexibleMemoryModel)
       
   410 		TestThrashHal();
       
   411 	else
       
   412 		TestThrashHalNotSupported();
       
   413 	
       
   414 	if (gDataPagingSupported && User::CommandLineLength() > 0)
       
   415 		{		
       
   416 		test.Next(_L("Extended thrashing tests"));
       
   417 		TestThrashing();
       
   418 		}
       
   419 
       
   420 	test.End();
       
   421 	return 0;
       
   422 	}