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