kerneltest/e32test/lffs/tf_suspendsoak.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2003-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 // Tests erasing of Flash while forcing suspend-resume cycles
       
    15 // This is a soak-test version that runs continously
       
    16 // 
       
    17 //
       
    18 
       
    19 #include <e32std.h>
       
    20 #include <e32std_private.h>
       
    21 #include <e32svr.h>
       
    22 #include <e32test.h>
       
    23 #include "randgen.h"
       
    24 #include "user_config.h"
       
    25 
       
    26 RTest test( _L("TF_SUSPENDSOAK") );
       
    27 
       
    28 
       
    29 
       
    30 
       
    31 class CEraser
       
    32 	{
       
    33 	public:
       
    34 		enum TFunction
       
    35 			{
       
    36 			EIdle,
       
    37 			EEraseBlock
       
    38 			};
       
    39 
       
    40 	public:
       
    41 		~CEraser();
       
    42 		void CreateL();
       
    43 		void Stop();
       
    44 		void WaitForReady();
       
    45 		inline TBool CheckDone() const
       
    46 			{
       
    47 			return (EIdle == iRequestedFunction);
       
    48 			}
       
    49 
       
    50 		inline void WaitForDone()
       
    51 			{
       
    52 			WaitForReady();
       
    53 			iWaitingSignal.Signal();	// resignal, ready for next Start()
       
    54 			};
       
    55 
       
    56 		void EraseBlock( TUint32 aOffset, TUint aLength );
       
    57 
       
    58 	private:
       
    59 		void Panic( TInt aPanicNum );
       
    60 		void Start( TFunction aFunction );
       
    61 
       
    62 		static TInt EraserThread( TAny* aParam );
       
    63 
       
    64 		void DoEraseBlock();
       
    65 
       
    66 	private:
       
    67 		RThread		iThread;
       
    68 
       
    69 		//
       
    70 		// Shared between main & eraser thread
       
    71 		//
       
    72 		TFunction	iRequestedFunction;
       
    73 		RSemaphore	iGoSignal;
       
    74 		RSemaphore	iWaitingSignal;
       
    75 		TBool		iStop;
       
    76 
       
    77 		//
       
    78 		// These are local to the eraser thread
       
    79 		//
       
    80 		TUint		iOffset;
       
    81 		TUint		iLength;
       
    82 		TBusLocalDrive	iDrive;
       
    83 	};
       
    84 
       
    85 
       
    86 
       
    87 CEraser::~CEraser()
       
    88 	{
       
    89 	iThread.Terminate( KErrNone );
       
    90 	iThread.Close();
       
    91 	iGoSignal.Close();
       
    92 	iWaitingSignal.Close();
       
    93 	}
       
    94 
       
    95 void CEraser::Panic( TInt aPanicNum )
       
    96 	{
       
    97 	_LIT( KPanicCat, "ERASE-T" );
       
    98 	User::Panic( KPanicCat, aPanicNum );
       
    99 	RProcess().Panic( KPanicCat, aPanicNum );
       
   100 	}
       
   101 
       
   102 
       
   103 void CEraser::CreateL()
       
   104 	//
       
   105 	// Create new thread and wait for it to become ready
       
   106 	//
       
   107 	{
       
   108 	iGoSignal.CreateLocal( 0 );	// initially blocked
       
   109 	iWaitingSignal.CreateLocal( 0 );	// initially blocked
       
   110 	iStop = EFalse;
       
   111 	User::LeaveIfError( iThread.Create( _L("ERASER"), EraserThread, 2048, 2048, 65536, this ) );
       
   112 	test.Printf( _L("Eraser thread created\n") );
       
   113 	
       
   114 	iThread.Resume();
       
   115 	
       
   116 	test.Printf( _L("Waiting for thread to become ready\n") );
       
   117 	WaitForReady();
       
   118 	iWaitingSignal.Signal();
       
   119 	}
       
   120 
       
   121 void CEraser::Start( TFunction aFunction )
       
   122 	//
       
   123 	// Start the suspender thread executing function aFunction
       
   124 	//
       
   125 	{
       
   126 	iStop = EFalse;
       
   127 	WaitForReady();
       
   128 	iRequestedFunction = aFunction;
       
   129 	iGoSignal.Signal();
       
   130 	}
       
   131 
       
   132 void CEraser::Stop()
       
   133 	//
       
   134 	// Stop the thread
       
   135 	//
       
   136 	{
       
   137 	iStop = ETrue;
       
   138 	}
       
   139 
       
   140 void CEraser::WaitForReady()
       
   141 	{
       
   142 	iWaitingSignal.Wait();
       
   143 	}
       
   144 
       
   145 void CEraser::EraseBlock( TUint32 aOffset, TUint aLength )
       
   146 	//
       
   147 	// Execute a single read immediately to cause a suspend
       
   148 	//
       
   149 	{
       
   150 	iOffset = aOffset;
       
   151 	iLength = aLength;
       
   152 	Start( EEraseBlock );
       
   153 	}
       
   154 
       
   155 
       
   156 TInt CEraser::EraserThread( TAny* aParam )
       
   157 	//
       
   158 	// The thread which executes suspend functions
       
   159 	//
       
   160 	{
       
   161 	RDebug::Print( _L("Eraser thread starts") );
       
   162 
       
   163 	CEraser& self = *reinterpret_cast<CEraser*>(aParam);
       
   164 
       
   165 	//
       
   166 	// Open our own TBusLogicalDevice channel
       
   167 	//
       
   168 	TBool changedFlag;
       
   169 	if( KErrNone != self.iDrive.Connect( KDriveNumber, changedFlag ) )
       
   170 		{
       
   171 		self.Panic( 1 );
       
   172 		}
       
   173 
       
   174 	RDebug::Print( _L("Eraser thread connected to drive") );
       
   175 	
       
   176 	while( !self.iStop )
       
   177 		{
       
   178 		//
       
   179 		// Signal that we are ready for a request
       
   180 		//
       
   181 		_LIT( KWaitMsg, "Eraser thread waiting..." );
       
   182 		RDebug::Print( KWaitMsg );
       
   183 		self.iWaitingSignal.Signal();
       
   184 
       
   185 		//
       
   186 		// Wait for a request
       
   187 		//
       
   188 		self.iGoSignal.Wait();
       
   189 		_LIT( KGoMsg, "Eraser thread go (%d)" );
       
   190 		RDebug::Print( KGoMsg, self.iRequestedFunction );
       
   191 
       
   192 		switch( self.iRequestedFunction )
       
   193 			{
       
   194 			case EEraseBlock:
       
   195 				self.DoEraseBlock();
       
   196 				break;
       
   197 
       
   198 			case EIdle:
       
   199 			default:
       
   200 				self.Panic( 0 );
       
   201 			}
       
   202 
       
   203 		self.iRequestedFunction = EIdle;
       
   204 		}
       
   205 
       
   206 	self.iDrive.Disconnect();
       
   207 	return KErrNone;
       
   208 	}
       
   209 
       
   210 void CEraser::DoEraseBlock()
       
   211 	//
       
   212 	// Issue an erase
       
   213 	//
       
   214 	{
       
   215 	_LIT( KEraseStartMsg, "Eraser starting erase..." );
       
   216 	RDebug::Print( KEraseStartMsg );
       
   217 	
       
   218 	TInt r = iDrive.Format( TInt64(iOffset), iLength );
       
   219 	
       
   220 	if( KErrNone != r )
       
   221 		{
       
   222 		RDebug::Print( _L("Eraser: FAIL: erase request returns %d"), r );
       
   223 		Panic( 2 );
       
   224 		}
       
   225 	}
       
   226 
       
   227 
       
   228 
       
   229 class CSuspendTest : public CBase
       
   230 	{
       
   231 	public:
       
   232 		~CSuspendTest();
       
   233 
       
   234 		void CreateL();
       
   235 
       
   236 		void DoTest();
       
   237 
       
   238 	private:
       
   239 
       
   240 		TInt EraseOneBlock( TInt aBlockNumber );
       
   241 		TInt ZeroFillBlock( TInt aBlockNumber );
       
   242 		TBool ValidateBlock( TInt aBlockNumber, TUint32 aFillWord );
       
   243 		TInt ZeroAllBlocks();
       
   244 		TBool ValidateAllBlocks( TUint32 aFillWord );
       
   245 		void TimeSinceStart() const;
       
   246 
       
   247 		void DoRandomReadSoak();
       
   248 
       
   249 	private:
       
   250 		TBusLocalDrive	iDrive;
       
   251 		TBool			iDriveOpened;
       
   252 
       
   253 		CEraser*		iEraser;
       
   254 
       
   255 		TInt			iFlashSize;
       
   256 		TInt			iBlockSize;
       
   257 		TInt			iBlockCount;
       
   258 
       
   259 		TTime			iStartTime;
       
   260 
       
   261 		TBuf8<512>		iReadBuffer;
       
   262 
       
   263 	};
       
   264 
       
   265 
       
   266 CSuspendTest::~CSuspendTest()
       
   267 	{
       
   268 	if( iDriveOpened )
       
   269 		{
       
   270 		iDrive.Disconnect();
       
   271 		}
       
   272 
       
   273 	delete iEraser;
       
   274 	}
       
   275 
       
   276 
       
   277 
       
   278 void CSuspendTest::CreateL()
       
   279 	{
       
   280 	//
       
   281 	// Create the eraser thread
       
   282 	//
       
   283 	iEraser = new(ELeave) CEraser;
       
   284 	iEraser->CreateL();
       
   285 
       
   286 	//
       
   287 	// Load the device drivers
       
   288 	//
       
   289 	TInt r;
       
   290 #ifndef SKIP_PDD_LOAD
       
   291 	test.Printf( _L("Loading %S\n"), &KLfsDriverName );
       
   292 	r = User::LoadPhysicalDevice( KLfsDriverName );
       
   293 	test( KErrNone == r || KErrAlreadyExists == r );
       
   294 #endif
       
   295 
       
   296 #ifdef UNMOUNT_DRIVE
       
   297 	RFs fs;
       
   298 	test( KErrNone == fs.Connect() );
       
   299 	test( KErrNone == fs.SetSessionPath( _L("Z:\\") ) );
       
   300 	TFullName name;
       
   301 	fs.FileSystemName( name, KLffsLogicalDriveNumber );
       
   302 	if( name.Length() > 0 )
       
   303 		{
       
   304 		test.Printf( _L("Unmounting drive") );
       
   305 		test( KErrNone == fs.DismountFileSystem( _L("Lffs"), KLffsLogicalDriveNumber) );
       
   306 		User::After( 2000000 );
       
   307 		test.Printf( _L("Drive unmounted") );
       
   308 		}
       
   309 	fs.Close();
       
   310 #endif
       
   311 
       
   312 	//
       
   313 	// Open a TBusLogicalDevice to it
       
   314 	//
       
   315 	test.Printf( _L("Opening media channel\n") );
       
   316 	TBool changedFlag = EFalse;
       
   317 	r = iDrive.Connect( KDriveNumber, changedFlag );
       
   318 	User::LeaveIfError( r );
       
   319 	iDriveOpened = ETrue;
       
   320 
       
   321 	//
       
   322 	// Get size of Flash drive, block size, block count
       
   323 	//
       
   324 	TLocalDriveCapsV2Buf info;
       
   325     iDrive.Caps(info);
       
   326 	iFlashSize = I64LOW(info().iSize);
       
   327 	iBlockSize = info().iEraseBlockSize;
       
   328 	iBlockCount = iFlashSize / iBlockSize;
       
   329 
       
   330 	test.Printf( _L("Flash size is 0x%x bytes\n"), iFlashSize );
       
   331 	test.Printf( _L("Block size is 0x%x bytes\n"), iBlockSize );
       
   332 	test.Printf( _L("Block count is %d\n"), iBlockCount );
       
   333 
       
   334 	test.Printf( _L("CreateL complete\n") );
       
   335 	}
       
   336 
       
   337 
       
   338 void CSuspendTest::DoTest()
       
   339 	//
       
   340 	// Main test dispatcher
       
   341 	//
       
   342 	{
       
   343 	DoRandomReadSoak();
       
   344 	}
       
   345 
       
   346 
       
   347 TInt CSuspendTest::EraseOneBlock( TInt aBlockNumber )
       
   348 	//
       
   349 	// Erases block aBlockNumber on Flash
       
   350 	//
       
   351 	{
       
   352 	TInt blockBaseOffset = aBlockNumber * iBlockSize;
       
   353 
       
   354 	test.Printf( _L("Erasing block %d (offs=0x%x)\n"), aBlockNumber, blockBaseOffset );
       
   355 	
       
   356 	TInt r = iDrive.Format( blockBaseOffset, iBlockSize );
       
   357 
       
   358 	test.Printf( _L("... block erased, rv=%d\n"), r );
       
   359 	return r;
       
   360 	}
       
   361 
       
   362 
       
   363 TBool CSuspendTest::ValidateBlock( TInt aBlockNumber, TUint32 aFillWord )
       
   364 	//
       
   365 	// Checks that every word in block aBlockNumber has the value aFillWord
       
   366 	//
       
   367 	{
       
   368 	TUint offset = aBlockNumber * iBlockSize;
       
   369 	test.Printf( _L("Validating block %d (offs=0x%x)\n"), aBlockNumber, offset );
       
   370 	
       
   371 	TBool failed = EFalse;
       
   372 	const TInt readBufLen = iReadBuffer.MaxLength();
       
   373 
       
   374 	for( TInt len = iBlockSize; len > 0 && !failed ;)
       
   375 		{
       
   376 		TInt r = iDrive.Read( offset, readBufLen, iReadBuffer );
       
   377 		if( r != KErrNone )
       
   378 			{
       
   379 			test.Printf( _L("... FAIL: read failed (%d) at offset 0x%x\n"), r, offset );
       
   380 			test( KErrNone == r );
       
   381 			}
       
   382 		test( iReadBuffer.Length() == readBufLen );
       
   383 
       
   384 		TUint32* p = (TUint32*)iReadBuffer.Ptr();
       
   385 		for( TInt i = 0; i < readBufLen; i += 4 )
       
   386 			{
       
   387 			if( aFillWord != *p )
       
   388 				{
       
   389 				failed = ETrue;
       
   390 				test.Printf( _L("... FAILED: word @ offs=0x%x, read=0x%x, expected=0x%x\n"), 
       
   391 								offset+i, p[0], aFillWord );
       
   392 				break;
       
   393 				}
       
   394 			++p;
       
   395 			}
       
   396 		offset += readBufLen;
       
   397 		len -= readBufLen;
       
   398 		}
       
   399 	
       
   400 	return !failed;
       
   401 	}
       
   402 
       
   403 
       
   404 TInt CSuspendTest::ZeroFillBlock( TInt aBlockNumber )
       
   405 	//
       
   406 	// Zero-fills and entire block
       
   407 	// The requires that writing works
       
   408 	//
       
   409 	{
       
   410 	test.Printf( _L("Zero-filling block %d\n"), aBlockNumber );
       
   411 
       
   412 	//
       
   413 	// Create a buffer full of zeros
       
   414 	//
       
   415 	const TInt KZeroBufSize = 512;
       
   416 
       
   417 	TBuf8<KZeroBufSize> buf;
       
   418 	buf.FillZ( buf.MaxLength() );
       
   419 
       
   420 	//
       
   421 	// Write the data out to the Flash
       
   422 	//
       
   423 	TInt writeCount = iBlockSize / KZeroBufSize;
       
   424 	TInt r = KErrNone;
       
   425 	TUint blockBaseOffset = aBlockNumber * iBlockSize;
       
   426 	TInt pos = blockBaseOffset;
       
   427 	for( ; (writeCount > 0) && (KErrNone == r); writeCount-- )
       
   428 		{
       
   429 		r = iDrive.Write( pos, buf );
       
   430 		if( r != KErrNone )
       
   431 			{
       
   432 			test.Printf( _L("... FAIL: write failed (%d) at offset 0x%x\n"), pos );
       
   433 			}
       
   434 		pos += KZeroBufSize;
       
   435 		}
       
   436 
       
   437 	return r;
       
   438 	}
       
   439 
       
   440 
       
   441 TInt CSuspendTest::ZeroAllBlocks()
       
   442 	//
       
   443 	// Writes zeros to all blocks
       
   444 	//
       
   445 	{
       
   446 	test.Printf( _L("Zeroing all blocks\n") );
       
   447 
       
   448 	TInt r = KErrNone;
       
   449 	for( TInt i = 0; (i < iBlockCount) && (KErrNone == r); i++ )
       
   450 		{
       
   451 		r = ZeroFillBlock( i );
       
   452 		}
       
   453 
       
   454 	return r;
       
   455 	}
       
   456 
       
   457 TBool CSuspendTest::ValidateAllBlocks( TUint32 aFillWord )
       
   458 	//
       
   459 	// Checks that all blocks contain aFillWord
       
   460 	//
       
   461 	{
       
   462 	test.Printf( _L("Validating all blocks\n") );
       
   463 
       
   464 	TBool failed = EFalse;
       
   465 	for( TInt i = 0; (i < iBlockCount) && (!failed); i++ )
       
   466 		{
       
   467 		failed = !ValidateBlock( i, aFillWord );
       
   468 		}
       
   469 
       
   470 	return !failed;
       
   471 	}
       
   472 
       
   473 
       
   474 void CSuspendTest::TimeSinceStart() const
       
   475 	{
       
   476 	TTimeIntervalSeconds timeTaken;
       
   477 	TTime time;
       
   478 	time.HomeTime();
       
   479 	TInt r = time.SecondsFrom(iStartTime, timeTaken);
       
   480 	test(r == KErrNone);
       
   481 	TInt totalTime = timeTaken.Int();
       
   482 
       
   483 	TInt seconds = totalTime % 60;
       
   484 	TInt minutes = (totalTime / 60) % 60;
       
   485 	TInt hours   = totalTime / 3600;
       
   486 
       
   487 	test.Printf( _L("Time since test started = %d:%d:%d\n"), hours, minutes, seconds );
       
   488 	}
       
   489 
       
   490 
       
   491 
       
   492 void CSuspendTest::DoRandomReadSoak()
       
   493 	//
       
   494 	// For each block issues an erase and then
       
   495 	// starts issuing read requests. The intervals
       
   496 	// between read requests are derived from the
       
   497 	// pseudo-random number generator.
       
   498 	// Each block is checked after is has been erased
       
   499 	//
       
   500 	{
       
   501 
       
   502 	TRandomGenerator random;
       
   503 	random.SetSeed( MAKE_TINT64(0xA05BE111,0x00101111) );
       
   504 
       
   505 	test.Next( _L("Random read soak test") );
       
   506 
       
   507 	iStartTime.HomeTime();
       
   508 
       
   509 	//
       
   510 	// We repeat the test for each block, erasing block n and reading from
       
   511 	// block (n+1) modulo iBlockCount
       
   512 	//
       
   513 	for(;;)
       
   514 		{
       
   515 		TimeSinceStart();
       
   516 
       
   517 		for( TInt eraseBlock = 0; eraseBlock < iBlockCount; eraseBlock++ )
       
   518 			{
       
   519 			TUint32 readBlock = (eraseBlock + 1) % iBlockCount;
       
   520 			TUint32 erasePos = eraseBlock * iBlockSize;
       
   521 			TInt readPos = readBlock * iBlockSize;
       
   522 
       
   523 			//
       
   524 			// Zero the block we are about to erase
       
   525 			//
       
   526 			test( KErrNone == ZeroFillBlock( eraseBlock ) );
       
   527 			test( ValidateBlock( eraseBlock, 0 ) );
       
   528 			
       
   529 			TBuf8<32> buf;
       
   530 
       
   531 			//
       
   532 			// Start the erase
       
   533 			//
       
   534 			_LIT( KEraseNotify, "Main thread starting erase\n" );
       
   535 			test.Printf( KEraseNotify );
       
   536 			iEraser->EraseBlock( erasePos, iBlockSize );
       
   537 
       
   538 			//
       
   539 			// Now we loop, waiting for random intervals, issuing
       
   540 			// reads, until the erase completes
       
   541 			//
       
   542 
       
   543 			while( !iEraser->CheckDone() )
       
   544 				{
       
   545 				//
       
   546 				// Get a pseudo-random interval between 0 and 524.288 milliseconds
       
   547 				//
       
   548 				TInt delayInMicroseconds = random.Next() % 0x80000;
       
   549 				User::After( delayInMicroseconds );
       
   550 
       
   551 				test( KErrNone == iDrive.Read( readPos, buf.MaxLength(), buf ) );
       
   552 				_LIT( KReadNotify, "Done Read" );
       
   553 				test.Printf( KReadNotify );
       
   554 				}
       
   555 
       
   556 			//
       
   557 			// Now check that the block was erased
       
   558 			//
       
   559 			test( ValidateBlock( eraseBlock, 0xFFFFFFFF ) );
       
   560 			
       
   561 			}
       
   562 		}
       
   563 
       
   564 	}
       
   565 
       
   566 
       
   567 
       
   568 
       
   569 void E32Main()
       
   570 	{
       
   571 	test.Title();
       
   572 	test.Start(_L("Testing media erase+suspend operations"));
       
   573 
       
   574 	CSuspendTest suspendTest;
       
   575 	TRAPD( ret, suspendTest.CreateL() );
       
   576 	if( KErrNone == ret )
       
   577 		{
       
   578 		suspendTest.DoTest();
       
   579 		}
       
   580 
       
   581 	test.End();
       
   582 	}