kerneltest/e32test/mmu/t_shbuf.cpp
changeset 281 13fbfa31d2ba
parent 201 43365a9b78a3
equal deleted inserted replaced
266:0008ccd16016 281:13fbfa31d2ba
  2334 void TTestPoolModel::DisplayCounters()
  2334 void TTestPoolModel::DisplayCounters()
  2335 	{
  2335 	{
  2336 	test.Printf(_L("%-6u%-6u%-6u%-6u%-6u\n"), iAllocated, iFree, iAllocated + iFree, iGrowTrigger, iShrinkTrigger);
  2336 	test.Printf(_L("%-6u%-6u%-6u%-6u%-6u\n"), iAllocated, iFree, iAllocated + iFree, iGrowTrigger, iShrinkTrigger);
  2337 	}
  2337 	}
  2338 
  2338 
       
  2339 const TInt KMaxCountAlignmentRetries = 4;
       
  2340 #define CHECK_COUNT_ALIGNMENT(a,b) CheckCountAlignmentWithTimeout(a,b,(TUint)__LINE__)
       
  2341 #define RETRY_BUF_ALLOC(a,b,c,d,e) RetryBufAlloc(a,b,c,d,e,(TUint)__LINE__)
       
  2342 #define RETRY_BUF_FREE(a,b,c,d) RetryBufFree(a,b,c,d,(TUint)__LINE__)
       
  2343 
       
  2344 TInt CheckCountAlignmentWithTimeout(TTestPoolModel& aModel, RShPool& aPool, const TUint aLineNum)
       
  2345 	{
       
  2346 	// Allow a maximum time for the model and pool counts to align
       
  2347 	// Return the period remaining after they align (zero if alignement is not reached)
       
  2348 	TInt timeout = KTestFreeCountTimeOut / KTestWaitBeforeRetry;
       
  2349 	while (aModel.FreeCount() != aPool.FreeCount())
       
  2350 		{
       
  2351 		User::After(KTestWaitBeforeRetry);
       
  2352 		if(--timeout == 0)
       
  2353 			{
       
  2354 			test.Printf(_L("Timeout: Free==%u (expected %u, line %d)\n"), aPool.FreeCount(), aModel.FreeCount(), aLineNum);
       
  2355 			aModel.DisplayCounters();
       
  2356 			return timeout;
       
  2357 			}
       
  2358 		if ((timeout * KTestWaitBeforeRetry) % 1000000 == 0)
       
  2359 			{
       
  2360 			test.Printf(_L("Time out in %d seconds! (line %d)\n"), timeout * KTestWaitBeforeRetry / 1000000, aLineNum);
       
  2361 			}
       
  2362 		--timeout;
       
  2363 		};
       
  2364 	return timeout;
       
  2365 	}
       
  2366 
       
  2367 TInt RetryBufAlloc(RArray<RShBuf>& aBufarray, TTestPoolModel& aModel, RShPool& aPool, RShBuf& aBuf, TUint aBufferFlags, const TUint aLineNum)
       
  2368 	{
       
  2369 	// Free a buffer, then alloc
       
  2370 	TShPoolInfo info;
       
  2371 	aPool.GetInfo(info);
       
  2372 
       
  2373 	aBufarray[aBufarray.Count() - 1].Close();
       
  2374 	aBufarray.Remove(aBufarray.Count() - 1);
       
  2375 	aModel.Free();		
       
  2376 	//
       
  2377 	TInt r = aBuf.Alloc(aPool, aBufferFlags);
       
  2378 	if (r)
       
  2379 		{
       
  2380 		test.Printf(_L("Line %d, Re-Alloc fail, after %d of %d; Free==%u (expected %u)\n"),
       
  2381 			aLineNum, aBufarray.Count(), info.iMaxBufs, aPool.FreeCount(), aModel.FreeCount());
       
  2382 		}
       
  2383 	else
       
  2384 		{
       
  2385 		aModel.Alloc();
       
  2386 		if (!(aBufferFlags & EShPoolAllocNoMap))
       
  2387 			{
       
  2388 			TPtr8 ptr(aBuf.Ptr(), aBuf.Size(),aBuf.Size());
       
  2389 			ptr.Fill(aBufarray.Count() % 256);
       
  2390 			}
       
  2391 		aBufarray.Append(aBuf);
       
  2392 		}
       
  2393 
       
  2394 	return r;
       
  2395 	}
       
  2396 
       
  2397 TInt RetryBufFree(RArray<RShBuf>& aBufarray, TTestPoolModel& aModel, RShPool& aPool, TUint aBufferFlags, const TUint aLineNum)
       
  2398 	{
       
  2399 	// Allocate a buffer, then free it
       
  2400 	TShPoolInfo info;
       
  2401 	aPool.GetInfo(info);
       
  2402 	RShBuf buf;
       
  2403 	TInt r = buf.Alloc(aPool, aBufferFlags);
       
  2404 	if (r)
       
  2405 		{
       
  2406 		test.Printf(_L("Line %d, Re-Alloc fail, after %d of %d; Free==%u (expected %u)\n"),
       
  2407 			aLineNum, aBufarray.Count(), info.iMaxBufs, aPool.FreeCount(), aModel.FreeCount());
       
  2408 		}
       
  2409 	else
       
  2410 		{
       
  2411 		aModel.Alloc();
       
  2412 		if (!(aBufferFlags & EShPoolAllocNoMap))
       
  2413 			{
       
  2414 			TPtr8 ptr(buf.Ptr(), buf.Size(),buf.Size());
       
  2415 			ptr.Fill(aBufarray.Count() % 256);
       
  2416 			}
       
  2417 		aBufarray.Append(buf);
       
  2418 		//
       
  2419 		aBufarray[aBufarray.Count() - 1].Close();
       
  2420 		aBufarray.Remove(aBufarray.Count() - 1);
       
  2421 		aModel.Free();		
       
  2422 		}
       
  2423 	return r;
       
  2424 	}
       
  2425 
       
  2426 
       
  2427 
  2339 void PoolGrowingTestRoutine(const TShPoolCreateInfo& aInfo, TUint aBufferFlags = 0)
  2428 void PoolGrowingTestRoutine(const TShPoolCreateInfo& aInfo, TUint aBufferFlags = 0)
  2340 	{
  2429 	{
  2341 	TInt r;
  2430 	TInt r;
  2342 	TInt timeout;
       
  2343 	RShPool pool;
  2431 	RShPool pool;
  2344 	r = pool.Create(aInfo, KDefaultPoolHandleFlags);
  2432 	r = pool.Create(aInfo, KDefaultPoolHandleFlags);
  2345 	test_KErrNone(r);
  2433 	test_KErrNone(r);
  2346 
  2434 
  2347 	TShPoolInfo info;
  2435 	TShPoolInfo info;
  2357 	TTestPoolModel model(info);
  2445 	TTestPoolModel model(info);
  2358 	RArray<RShBuf> bufarray;
  2446 	RArray<RShBuf> bufarray;
  2359 	test_Equal(info.iInitialBufs, pool.FreeCount());
  2447 	test_Equal(info.iInitialBufs, pool.FreeCount());
  2360 
  2448 
  2361 	// Buffer allocation
  2449 	// Buffer allocation
       
  2450 	TInt retriesRemaining = KMaxCountAlignmentRetries;
  2362 	do
  2451 	do
  2363 		{
  2452 		{
  2364 		timeout = KTestFreeCountTimeOut / KTestWaitBeforeRetry;
       
  2365 		while (model.FreeCount() != pool.FreeCount())
       
  2366 			{
       
  2367 			User::After(KTestWaitBeforeRetry);
       
  2368 			test_Assert(--timeout,
       
  2369 				test.Printf(_L("Timeout: Free==%u (expected %u)\n"), pool.FreeCount(), model.FreeCount());
       
  2370 				model.DisplayCounters();
       
  2371 				);
       
  2372 			if ((timeout * KTestWaitBeforeRetry) % 1000000 == 0)
       
  2373 				{
       
  2374 				test.Printf(_L("Time out in %d seconds! (line %d)\n"), timeout * KTestWaitBeforeRetry / 1000000, __LINE__);
       
  2375 				}
       
  2376 			}
       
  2377 		RShBuf buf;
  2453 		RShBuf buf;
  2378 		r = buf.Alloc(pool, aBufferFlags);
  2454 		r = buf.Alloc(pool, aBufferFlags);
  2379 		if (r == KErrNoMemory)
  2455 		if (r == KErrNoMemory)
  2380 			{
  2456 			{
  2381 			// We expect to get a failure when all buffers are allocated
  2457 			// We expect to get a failure when all buffers are allocated
  2402 				{
  2478 				{
  2403 				TPtr8 ptr(buf.Ptr(), buf.Size(),buf.Size());
  2479 				TPtr8 ptr(buf.Ptr(), buf.Size(),buf.Size());
  2404 				ptr.Fill(bufarray.Count() % 256);
  2480 				ptr.Fill(bufarray.Count() % 256);
  2405 				}
  2481 				}
  2406 			bufarray.Append(buf);
  2482 			bufarray.Append(buf);
  2407 			}
  2483 
       
  2484 			while ((!CHECK_COUNT_ALIGNMENT(model,pool)) && retriesRemaining--)
       
  2485 				{
       
  2486 				// Count mismatch. Due to the operation of this test (single Alloc, then wait and check)
       
  2487 				// it is possible for a count mis-match to occur. This is not a problem in normal operation
       
  2488 				// as the kernel-side count will increase on the next Alloc call, triggering the pool growth.
       
  2489 				// For now, remove the just-added buffer then repeat the  Alloc
       
  2490 				// (but only do this for a maximum number of times, to preclude getting stuck in an infinite loop).
       
  2491 				test_Assert(retriesRemaining,
       
  2492 					test.Printf(_L("Timeout: Free==%u (expected %u), retries remaining: %d\n"), pool.FreeCount(), model.FreeCount(), retriesRemaining);
       
  2493 					model.DisplayCounters();
       
  2494 					);
       
  2495 				//
       
  2496 				r = RETRY_BUF_ALLOC(bufarray, model, pool, buf, aBufferFlags);
       
  2497 				};
       
  2498 			retriesRemaining = KMaxCountAlignmentRetries;
       
  2499 			}
       
  2500 
  2408 		}
  2501 		}
  2409 	while (r == KErrNone);
  2502 	while (r == KErrNone);
  2410 
  2503 
  2411 	test_Equal(KErrNoMemory, r);
  2504 	test_Equal(KErrNoMemory, r);
  2412 	test_Equal(info.iMaxBufs, bufarray.Count());
  2505 	test_Equal(info.iMaxBufs, bufarray.Count());
  2421 			TPtr8 ptr(bufarray[bufarray.Count() - 1].Ptr(), bufarray[bufarray.Count() - 1].Size(),bufarray[bufarray.Count() - 1].Size());
  2514 			TPtr8 ptr(bufarray[bufarray.Count() - 1].Ptr(), bufarray[bufarray.Count() - 1].Size(),bufarray[bufarray.Count() - 1].Size());
  2422 			ptr.Fill((bufarray.Count() + 1) % 256);
  2515 			ptr.Fill((bufarray.Count() + 1) % 256);
  2423 			}
  2516 			}
  2424 		bufarray[bufarray.Count() - 1].Close();
  2517 		bufarray[bufarray.Count() - 1].Close();
  2425 		bufarray.Remove(bufarray.Count() - 1);
  2518 		bufarray.Remove(bufarray.Count() - 1);
  2426 		model.Free();
  2519 		model.Free();		
  2427 		
  2520 		
  2428 		timeout = KTestFreeCountTimeOut / KTestWaitBeforeRetry;
  2521 		while ((!CHECK_COUNT_ALIGNMENT(model,pool)) && retriesRemaining--)
  2429 		while (model.FreeCount() != pool.FreeCount())
  2522 			{
  2430 			{
  2523 			// Count mismatch. Re-add the buffer then repeat the Free.
  2431 			User::After(KTestWaitBeforeRetry);
  2524 			// (but only do this for a maximum number of times, to preclude getting stuck in an infinite loop).
  2432 			test_Assert(--timeout,
  2525 			test_Assert(retriesRemaining,
  2433 				test.Printf(_L("Timeout: Free==%u (expected %u)\n"), pool.FreeCount(), model.FreeCount());
  2526 				test.Printf(_L("Timeout: Free==%u (expected %u), retries remaining: %d\n"), pool.FreeCount(), model.FreeCount(), retriesRemaining);
  2434 				model.DisplayCounters();
  2527 				model.DisplayCounters();
  2435 				);
  2528 				);
  2436 			if ((timeout * KTestWaitBeforeRetry) % 1000000 == 0)
  2529 			//
  2437 				{
  2530 			r = RETRY_BUF_FREE(bufarray, model, pool, aBufferFlags);
  2438 				test.Printf(_L("Time out in %d seconds! (line %d)\n"), timeout * KTestWaitBeforeRetry / 1000000, __LINE__);
  2531 			};
  2439 				}
  2532 		retriesRemaining = KMaxCountAlignmentRetries;		
  2440 			}
       
  2441 		}
  2533 		}
  2442 
  2534 
  2443 	// ... and re-allocate them
  2535 	// ... and re-allocate them
  2444 	do
  2536 	do
  2445 		{
  2537 		{
  2446 		timeout = KTestFreeCountTimeOut / KTestWaitBeforeRetry;
       
  2447 		while (model.FreeCount() != pool.FreeCount())
       
  2448 			{
       
  2449 			User::After(KTestWaitBeforeRetry);
       
  2450 			test_Assert(--timeout,
       
  2451 				test.Printf(_L("Timeout: Free==%u (expected %u)\n"), pool.FreeCount(), model.FreeCount());
       
  2452 				model.DisplayCounters();
       
  2453 				);
       
  2454 			if ((timeout * KTestWaitBeforeRetry) % 1000000 == 0)
       
  2455 				{
       
  2456 				test.Printf(_L("Time out in %d seconds! (line %d)\n"), timeout * KTestWaitBeforeRetry / 1000000, __LINE__);
       
  2457 				}
       
  2458 			}
       
  2459 		RShBuf buf;
  2538 		RShBuf buf;
  2460 		r = buf.Alloc(pool, aBufferFlags);
  2539 		r = buf.Alloc(pool, aBufferFlags);
  2461 		if (r == KErrNoMemory)
  2540 		if (r == KErrNoMemory)
  2462 			{
  2541 			{
  2463 			// We expect to get a failure when all buffers are allocated
  2542 			// We expect to get a failure when all buffers are allocated
  2484 				{
  2563 				{
  2485 				TPtr8 ptr(buf.Ptr(), buf.Size(),buf.Size());
  2564 				TPtr8 ptr(buf.Ptr(), buf.Size(),buf.Size());
  2486 				ptr.Fill(bufarray.Count() % 256);
  2565 				ptr.Fill(bufarray.Count() % 256);
  2487 				}
  2566 				}
  2488 			bufarray.Append(buf);
  2567 			bufarray.Append(buf);
       
  2568 
       
  2569 			while ((!CHECK_COUNT_ALIGNMENT(model,pool)) && retriesRemaining--)
       
  2570 				{
       
  2571 				// Count mismatch. Remove the just-added buffer then repeat the  Alloc
       
  2572 				// (but only do this for a maximum number of times, to preclude getting stuck in an infinite loop).
       
  2573 				test_Assert(retriesRemaining,
       
  2574 					test.Printf(_L("Timeout: Free==%u (expected %u), retries remaining: %d\n"), pool.FreeCount(), model.FreeCount(), retriesRemaining);
       
  2575 					model.DisplayCounters();
       
  2576 					);
       
  2577 				//
       
  2578 				r = RETRY_BUF_ALLOC(bufarray, model, pool, buf, aBufferFlags);
       
  2579 				};
       
  2580 			retriesRemaining = KMaxCountAlignmentRetries;
  2489 			}
  2581 			}
  2490 		}
  2582 		}
  2491 	while (r == KErrNone);
  2583 	while (r == KErrNone);
  2492 
  2584 
  2493 	test_Equal(KErrNoMemory, r);
  2585 	test_Equal(KErrNoMemory, r);
  2503 			TPtr8 ptr(bufarray[bufarray.Count() - 1].Ptr(), bufarray[bufarray.Count() - 1].Size(),bufarray[bufarray.Count() - 1].Size());
  2595 			TPtr8 ptr(bufarray[bufarray.Count() - 1].Ptr(), bufarray[bufarray.Count() - 1].Size(),bufarray[bufarray.Count() - 1].Size());
  2504 			ptr.Fill((bufarray.Count() + 1) % 256);
  2596 			ptr.Fill((bufarray.Count() + 1) % 256);
  2505 			}
  2597 			}
  2506 		bufarray[bufarray.Count() - 1].Close();
  2598 		bufarray[bufarray.Count() - 1].Close();
  2507 		bufarray.Remove(bufarray.Count() - 1);
  2599 		bufarray.Remove(bufarray.Count() - 1);
  2508 		model.Free();
  2600 		model.Free();	
  2509 		
  2601 		
  2510 		timeout = KTestFreeCountTimeOut / KTestWaitBeforeRetry;
  2602 		while ((!CHECK_COUNT_ALIGNMENT(model,pool)) && retriesRemaining--)
  2511 		while (model.FreeCount() != pool.FreeCount())
  2603 			{
  2512 			{
  2604 			// Count mismatch. Re-add the buffer then repeat the Free.
  2513 			User::After(KTestWaitBeforeRetry);
  2605 			// (but only do this for a maximum number of times, to preclude getting stuck in an infinite loop).
  2514 			test_Assert(--timeout,
  2606 			test_Assert(retriesRemaining,
  2515 				test.Printf(_L("Timeout: Free==%u (expected %u)\n"), pool.FreeCount(), model.FreeCount());
  2607 				test.Printf(_L("Timeout: Free==%u (expected %u), retries remaining: %d\n"), pool.FreeCount(), model.FreeCount(), retriesRemaining);
  2516 				model.DisplayCounters();
  2608 				model.DisplayCounters();
  2517 				);
  2609 				);
  2518 			if ((timeout * KTestWaitBeforeRetry) % 1000000 == 0)
  2610 			//
  2519 				{
  2611 			r = RETRY_BUF_FREE(bufarray, model, pool, aBufferFlags);
  2520 				test.Printf(_L("Time out in %d seconds! (line %d)\n"), timeout * KTestWaitBeforeRetry / 1000000, __LINE__);
  2612 			};
  2521 				}
  2613 		retriesRemaining = KMaxCountAlignmentRetries;		
  2522 			}
       
  2523 		}
  2614 		}
  2524 
  2615 
  2525 	// Pool should have shrunk back to its initial size
  2616 	// Pool should have shrunk back to its initial size
  2526 	test_Equal(info.iInitialBufs, pool.FreeCount());
  2617 	test_Equal(info.iInitialBufs, pool.FreeCount());
  2527 	bufarray.Close();
  2618 	bufarray.Close();