os/kernelhwsrv/kerneltest/e32test/system/t_dobject.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2004-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\system\t_dobject.cpp
    15 // Overview:
    16 // Test RObjectIx strategy of memory reallocation and 
    17 // free list maintenance.
    18 // Test DObjectCon findhandle methods
    19 // API Information:
    20 // DObject, RObjectIx
    21 // Details:
    22 // - Add a number of DObjects to RObjectIx, then remove them 
    23 // in the same order. Verify results are as expected. Time
    24 // how long the process takes to complete.
    25 // - Add a number of DObjects to RObjectIx, then remove them
    26 // in the reverse order. Verify results are as expected. Time
    27 // how long the process takes to complete.
    28 // - Add and remove a random number of DObjects to/from RObjectIx.
    29 // Time how long the process takes to complete.
    30 // Platforms/Drives/Compatibility:
    31 // All.
    32 // Assumptions/Requirement/Pre-requisites:
    33 // Failures and causes:
    34 // Base Port information:
    35 // 
    36 //
    37 
    38 #define __E32TEST_EXTENSION__
    39 #include <e32test.h>
    40 #include <e32math.h>
    41 #include "d_dobject.h"
    42 
    43 LOCAL_D RTest test(_L("T_DOBJECT"));
    44 
    45 TInt TestRObjectIxAccess(TAny* aRObjectIxPtr)
    46 	{
    47 	const TInt  KConcurrentDObjectTestRepeats = 1;
    48 	
    49 	RTestDObject  ldd;
    50 	TInt  ret;
    51 	
    52 	ret = ldd.Open();
    53 	if (ret == KErrNone)
    54 		{
    55 		for (TInt  repeat = 0;  repeat < KConcurrentDObjectTestRepeats;  repeat++)
    56 			{
    57 			ret = ldd.RObjectIxThreadTestExerciseIx(aRObjectIxPtr);
    58 			if (ret != KErrNone)
    59 				{
    60 				break;
    61 				}
    62 			}
    63 		ldd.Close();
    64 		}
    65 	
    66 	return ret;
    67 	} // TestRObjectIxAccess
    68 
    69 void TestConcurrentRObjectIxAccess(RTestDObject& aLdd)
    70 	{
    71 	const TInt  KConcurrentDObjectThreads = 4;
    72 	_LIT(KConcurrentDObjectThreadName, "T_DObject_Thread");
    73 	
    74 	TInt ret;
    75 	
    76 	//
    77 	// Create a RObjectIx in the driver (pointer not valid user side!)...
    78 	//
    79 	void*  objectIxPtr = NULL;
    80 	
    81 	ret = aLdd.RObjectIxThreadTestCreateIx(objectIxPtr);
    82 	test_KErrNone(ret);
    83 	
    84 	//
    85 	// Run KConcurrentDObjectThreads number of threads which random add/remove
    86 	// DObjects to the same RObjectIx...
    87 	//
    88 	RThread  threadHandle[KConcurrentDObjectThreads];
    89 	TRequestStatus  status[KConcurrentDObjectThreads];
    90 	TBuf<32>  threadName;
    91 	TInt  thread;
    92 
    93 	for (thread = 0;  thread < KConcurrentDObjectThreads;  thread++)
    94 		{
    95 		threadName.Copy(KConcurrentDObjectThreadName);
    96 		threadName.AppendNum(thread);
    97 		
    98 		ret = threadHandle[thread].Create(threadName, TestRObjectIxAccess, KDefaultStackSize, NULL, objectIxPtr);
    99 		test_KErrNone(ret);
   100 
   101 		threadHandle[thread].Logon(status[thread]);
   102 		}
   103 
   104 	//
   105 	// The test thread must be higher priority to ensure all the threads start.
   106 	// All the threads are then resumed and allowed to run to completion...
   107 	//
   108 	RThread().SetPriority(EPriorityMore);
   109 
   110 	for (thread = 0;  thread < KConcurrentDObjectThreads;  thread++)
   111 		{
   112 		threadHandle[thread].Resume();
   113 		}
   114 
   115 	for (thread = 0;  thread < KConcurrentDObjectThreads;  thread++)
   116 		{
   117 		User::WaitForRequest(status[thread]);
   118 		test_KErrNone(status[thread].Int());
   119 		CLOSE_AND_WAIT(threadHandle[thread]);
   120 		}
   121 	
   122 	RThread().SetPriority(EPriorityNormal);
   123 
   124 	//
   125 	// Free the RObjectIx in the driver...
   126 	//
   127 	ret = aLdd.RObjectIxThreadTestFreeIx(objectIxPtr);
   128 	test_KErrNone(ret);
   129 	} // TestConcurrentRObjectIxAccess
   130 
   131 
   132 void ListAllMutexes()
   133 	{
   134 	test.Printf(_L("Mutexes:\n"));
   135 	TFullName name;
   136 	TFindMutex find;
   137 	while (find.Next(name) == KErrNone)
   138 		{
   139 		test.Printf(_L("  %S (find handle == %08x)\n"), &name, find.Handle());
   140 		}
   141 	}
   142 
   143 const TInt KObjectCount = 20;
   144 _LIT(KDoubleMatch, "*double*");
   145 _LIT(KTrippleMatch, "*tripple*");
   146 
   147 RMutex Mutexes[KObjectCount];
   148 TBuf<32> ObjectName;
   149 
   150 const TDesC& MutexName(TInt i)
   151 	{
   152 	ObjectName.Zero();
   153 	ObjectName.AppendFormat(_L("Mutex_%02d"), i);
   154 	if (i % 2 == 0)
   155 		ObjectName.Append(_L("_double"));
   156 	if (i % 3 == 0)
   157 		ObjectName.Append(_L("_tripple"));
   158 	return ObjectName;
   159 	}
   160 
   161 void CreateMutexes()
   162 	{
   163 	for (TInt i = 0 ; i < KObjectCount ; ++i)
   164 		{
   165 		test(Mutexes[i].CreateGlobal(MutexName(i)) == KErrNone);
   166 		}
   167 	}
   168 
   169 void DeleteMutexes()
   170 	{
   171 	for (TInt i = 0 ; i < KObjectCount ; ++i)
   172 		{
   173 		Mutexes[i].Close();
   174 		}
   175 	}
   176 
   177 void TestMutexesCreated()
   178 	{
   179 	test.Next(_L("Test mutexes have been created"));
   180 	
   181 	TFullName name;
   182 
   183 	for (TInt i = 0 ; i < KObjectCount ; ++i)
   184 		{
   185 		TFindMutex find(MutexName(i));
   186 		test(find.Next(name) == KErrNone);
   187 		test.Printf(_L("  %02d: found handle %08x\n"), i, find.Handle());
   188 		}
   189 	}
   190 
   191 void TestMutexesDeleted()
   192 	{
   193 	test.Next(_L("Test mutexes deleted"));
   194 
   195 	TFullName name;
   196 
   197 	for (TInt i = 0 ; i < KObjectCount ; ++i)
   198 		{
   199 		TFindMutex find(MutexName(i));
   200 		test(find.Next(name) == KErrNotFound);
   201 		}
   202 	}
   203 
   204 
   205 void TestFindSpecificMutex()
   206 	{
   207 	test.Next(_L("Test finding specific mutexes"));
   208 	
   209 	for (TInt i = 0 ; i < KObjectCount ; ++i)
   210 		{
   211 		TFullName name;
   212 		TFindMutex find(MutexName(i));
   213 		test(find.Next(name) == KErrNone);
   214 		test.Printf(_L("  %02d: found handle %08x\n"), i, find.Handle());
   215 		test(name == MutexName(i));
   216 		RMutex mutex;
   217 		test(mutex.Open(find) == KErrNone);
   218 		test(mutex.Name() == MutexName(i));
   219 		mutex.Close();
   220 		test(find.Next(name) == KErrNotFound);
   221 		}	
   222 	}
   223 
   224 void TestFindMutexGroups()
   225 	{
   226 	test.Next(_L("Test finding groups of mutexes using wildcard name matching"));
   227 
   228 	TFullName name;
   229 	TInt i;
   230 	
   231 	TFindMutex find2(KDoubleMatch);
   232 	for (i = 0 ; i < KObjectCount ; i += 2)
   233 		{
   234 		test(find2.Next(name) == KErrNone);
   235 		test.Printf(_L("  %02d: found handle %08x\n"), i, find2.Handle());
   236 		test(name == MutexName(i));
   237 		}
   238 	test(find2.Next(name) == KErrNotFound);
   239 
   240 	TFindMutex find3(KTrippleMatch);
   241 	for (i = 0 ; i < KObjectCount ; i += 3)
   242 		{
   243 		test(find3.Next(name) == KErrNone);
   244 		test.Printf(_L("  %02d: found handle %08x\n"), i, find3.Handle());
   245 		test(name == MutexName(i));
   246 		}
   247 	test(find3.Next(name) == KErrNotFound);
   248 	}
   249 
   250 void TestMatchChange()
   251 	{
   252 	test.Next(_L("Test changing match half way through find"));
   253 
   254 
   255 	TFullName name;
   256 	TInt i;
   257 	
   258 	TFindMutex find(KDoubleMatch);
   259 	for (i = 0 ; i < KObjectCount/2 ; i += 2)
   260 		{
   261 		test(find.Next(name) == KErrNone);
   262 		test.Printf(_L("  %02d: found handle %08x\n"), i, find.Handle());
   263 		test(name == MutexName(i));
   264 		}
   265 
   266 	find.Find(KTrippleMatch);
   267 	for (i = 0 ; i < KObjectCount ; i += 3)
   268 		{
   269 		test(find.Next(name) == KErrNone);
   270 		test.Printf(_L("  %02d: found handle %08x\n"), i, find.Handle());
   271 		test(name == MutexName(i));
   272 		}
   273 	test(find.Next(name) == KErrNotFound);
   274 	}
   275 
   276 void TestFindAndDeleteMutex1()
   277 	{
   278 	test.Next(_L("Test finding mutexes when the last found object has been deleted"));
   279 
   280 	// Find and delete even mutexes
   281 	TFullName name;
   282 	TInt i;
   283 	for (i = 0 ; i < KObjectCount ; i += 2)
   284 		{
   285 		TFindMutex find2(MutexName(i));
   286 		test(find2.Next(name) == KErrNone);
   287 		test.Printf(_L("  %02d: found handle %08x\n"), i, find2.Handle());
   288 		Mutexes[i].Close();
   289 		RMutex mutex;
   290 		test(mutex.Open(find2) == KErrNotFound);
   291 		}
   292 
   293 	// Check odd mutexes remaining
   294 	for (i = 1 ; i < KObjectCount ; i += 2)
   295 		{
   296 		TFindMutex find(MutexName(i));
   297 		test(find.Next(name) == KErrNone);
   298 		}
   299 	}
   300 
   301 void TestFindAndDeleteMutex2()
   302 	{
   303 	test.Next(_L("Test finding mutexes when the last found object has moved in the container"));
   304 
   305 	// Find even mutexes and delete odd
   306 	TFullName name;
   307 	TInt i;
   308 	for (i = 0 ; i < KObjectCount ; i += 2)
   309 		{
   310 		TFindMutex find2(MutexName(i));
   311 		test(find2.Next(name) == KErrNone);
   312 		test.Printf(_L("  %02d: found handle %08x\n"), i, find2.Handle());
   313 		Mutexes[(i+KObjectCount-1)%KObjectCount].Close();	// -1%n = -1 or n-1, unspecified
   314 		RMutex mutex;
   315 		test(mutex.Open(find2) == KErrNone);
   316 		test(mutex.Name() == MutexName(i));
   317 		mutex.Close();
   318 		}
   319 
   320 	// Check even mutexes remaining
   321 	for (i = 0 ; i < KObjectCount ; i += 2)
   322 		{
   323 		TFindMutex find(MutexName(i));
   324 		test(find.Next(name) == KErrNone);
   325 		}
   326 	
   327 	}
   328 
   329 void TestFindWithCreation()
   330 	{
   331 	test.Next(_L("Test finding mutexes interleaved with creation"));
   332 
   333 	TFullName name;
   334 	
   335 	for (TInt i = 0 ; i < KObjectCount ; ++i)
   336 		{
   337 		test(Mutexes[i].CreateGlobal(MutexName(i)) == KErrNone);
   338 		TFindMutex find(MutexName(i));
   339 		test(find.Next(name) == KErrNone);
   340 		test.Printf(_L("  %02d: found handle %08x\n"), i, find.Handle());
   341 		RMutex mutex;
   342 		test(mutex.Open(find) == KErrNone);
   343 		test(mutex.Name() == MutexName(i));
   344 		mutex.Close();
   345 		}
   346 	}
   347 
   348 void TestFindWithCreation2()
   349 	{
   350 	test.Next(_L("Test finding mutexes interleaved with creation and deletion"));
   351 
   352 	TFullName name;
   353 
   354 	for (TInt i = 0 ; i < KObjectCount ; ++i)
   355 		{
   356 		RMutex mutex;
   357 		test(mutex.CreateGlobal(MutexName(0)) == KErrNone);
   358 		TFindMutex find(MutexName(0));
   359 		test(find.Next(name) == KErrNone);
   360 		test(name == MutexName(0));
   361 		test.Printf(_L("  %02d: found handle %08x\n"), i, find.Handle());
   362 		mutex.Close();
   363 
   364 		TFindMutex find2(MutexName(0));
   365 		test(find2.Next(name) == KErrNotFound);
   366 		}
   367 	}
   368 
   369 void TestFindHandleOutOfRange()
   370 	{
   371 	test.Next(_L("Test finding mutexes when find handle index is off the end of container's array"));
   372 
   373 	TFullName name;
   374 
   375 	for (TInt i = 0 ; i < KObjectCount ; ++i)
   376 		{
   377 		TFindMutex find(MutexName(i));
   378 		test(find.Next(name) == KErrNone);
   379 		test.Printf(_L("  %02d: found handle %08x\n"), i, find.Handle());
   380 		RMutex mutex;
   381 		test(mutex.Open(find) == KErrNone);
   382 		test(mutex.Name() == MutexName(i));
   383 		mutex.Close();
   384 
   385 		// towards the end, suddenly delete half the mutexes
   386 		if (i == (3 * KObjectCount) / 4)
   387 			{
   388 			for (TInt j = 0 ; j < KObjectCount / 2 ; ++j)
   389 				Mutexes[j].Close();
   390 			}
   391 		}
   392 	}
   393 
   394 void TestFindHandles()
   395 	{
   396 	test.Start(_L("Test FindHandle APIs using mutex classes"));
   397 
   398 	CreateMutexes();
   399 	ListAllMutexes();
   400 	TestMutexesCreated();
   401 	TestFindSpecificMutex();
   402 	TestFindMutexGroups();
   403 	TestMatchChange();
   404 	DeleteMutexes();
   405 	TestMutexesDeleted();
   406 
   407 	CreateMutexes();
   408 	TestFindAndDeleteMutex1();
   409 	DeleteMutexes();
   410 
   411 	CreateMutexes();
   412 	TestFindHandleOutOfRange();
   413 	DeleteMutexes();
   414 	
   415 	CreateMutexes();
   416 	TestFindAndDeleteMutex2();
   417 	DeleteMutexes();
   418 
   419 	TestFindWithCreation();
   420 	DeleteMutexes();
   421 
   422 	TestFindWithCreation2();
   423 	TestMutexesDeleted();
   424 	
   425 	test.End();
   426 	}
   427 
   428 GLDEF_C TInt E32Main()
   429     {
   430 	SParam param;
   431 	TInt duration, r;
   432 	RTestDObject ldd;
   433 
   434 	test.Title();
   435 
   436 	test.Start(_L("Loading test driver..."));
   437 
   438 	r=User::LoadLogicalDevice(KDObjectTestLddName);
   439 	test(r==KErrNone || r==KErrAlreadyExists);
   440 	r=ldd.Open();
   441 	test(r==KErrNone);
   442 
   443 	test.Next(_L("RObjectIxTest1 test ..."));
   444 	r=ldd.RObjectIxTest1(duration);
   445 	test(KErrNone==r);
   446 	test.Printf(_L("... completed in %d kernel ticks\n") , duration);
   447 
   448 	test.Next(_L("RObjectIxTest2 test ..."));
   449 	r=ldd.RObjectIxTest2(duration);
   450 	test(KErrNone==r);
   451 	test.Printf(_L("... completed in %d kernel ticks\n") , duration);
   452 
   453 	test.Next(_L("RObjectIxTest3 test (performance) ..."));
   454 	param.iSeed[0] = 0;
   455 	param.iSeed[1] = 1;
   456 	param.iPerformanceTest = ETrue;
   457 	r=ldd.RObjectIxTest3(param);
   458 	test(KErrNone==r);
   459 	test.Printf(_L("... completed in %d kernel ticks\n") , param.duration);
   460 
   461 	test.Next(_L("RObjectIxTest3 test (random)..."));
   462 	param.iSeed[0]=User::TickCount();
   463 	param.iSeed[1]=User::TickCount();
   464 	param.iPerformanceTest = EFalse;
   465 	test.Printf(_L("... seeds=%xh and %xh ..."),param.iSeed[0],param.iSeed[1]);
   466 	r=ldd.RObjectIxTest3(param);
   467 	test(KErrNone==r);
   468 	test.Printf(_L("... completed in %d kernel ticks\n") , param.duration);
   469  
   470 	test.Next(_L("RObjectIxTest4 test (reserved slots)..."));
   471 	test_KErrNone(ldd.RObjectIxTest4(duration));
   472 	test.Printf(_L("... completed in %d kernel ticks\n") , duration);
   473 
   474 	test.Next(_L("Test Concurrent access to RObjectIx"));
   475 	TestConcurrentRObjectIxAccess(ldd);
   476 
   477 	test.Next(_L("Test Invalid handle look up"));
   478  	test_KErrNone(ldd.InvalidHandleLookupTest());
   479 
   480  	test.Next(_L("Test Kern::ValidateName and Kern::ValidateFullName"));
   481  	test_KErrNone(ldd.DObjectNameTest());
   482  
   483 	test.Next(_L("Closing test driver"));
   484 	ldd.Close();
   485 
   486 	test.Next(_L("FindHandles test"));
   487 	TestFindHandles();
   488 	
   489 	test.End();
   490 
   491 	return(0);
   492 	}