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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
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.
22 #include <e32std_private.h>
26 #include "user_config.h"
28 RTest test( _L("TF_SUSPENDSOAKW") );
47 inline TBool CheckDone() const
49 return (EIdle == iRequestedFunction);
52 inline void WaitForDone()
55 iWaitingSignal.Signal(); // resignal, ready for next Start()
58 void EraseBlock( TUint32 aOffset, TUint aLength );
61 void Panic( TInt aPanicNum );
62 void Start( TFunction aFunction );
64 static TInt EraserThread( TAny* aParam );
72 // Shared between main & eraser thread
74 TFunction iRequestedFunction;
76 RSemaphore iWaitingSignal;
80 // These are local to the eraser thread
84 TBusLocalDrive iDrive;
91 iThread.Terminate( KErrNone );
94 iWaitingSignal.Close();
97 void CEraser::Panic( TInt aPanicNum )
99 _LIT( KPanicCat, "ERASE-T" );
100 User::Panic( KPanicCat, aPanicNum );
101 RProcess().Panic( KPanicCat, aPanicNum );
105 void CEraser::CreateL()
107 // Create new thread and wait for it to become ready
110 iGoSignal.CreateLocal( 0 ); // initially blocked
111 iWaitingSignal.CreateLocal( 0 ); // initially blocked
113 User::LeaveIfError( iThread.Create( _L("ERASER"), EraserThread, 2048, 2048, 65536, this ) );
114 test.Printf( _L("Eraser thread created\n") );
118 test.Printf( _L("Waiting for thread to become ready\n") );
120 iWaitingSignal.Signal();
123 void CEraser::Start( TFunction aFunction )
125 // Start the suspender thread executing function aFunction
130 iRequestedFunction = aFunction;
142 void CEraser::WaitForReady()
144 iWaitingSignal.Wait();
147 void CEraser::EraseBlock( TUint32 aOffset, TUint aLength )
151 Start( EEraseBlock );
155 TInt CEraser::EraserThread( TAny* aParam )
157 // The thread which executes suspend functions
160 RDebug::Print( _L("Eraser thread starts") );
162 CEraser& self = *reinterpret_cast<CEraser*>(aParam);
165 // Open our own TBusLogicalDevice channel
168 if( KErrNone != self.iDrive.Connect( KDriveNumber, changedFlag ) )
173 RDebug::Print( _L("Eraser thread connected to drive") );
178 // Signal that we are ready for a request
180 _LIT( KWaitMsg, "Eraser thread waiting..." );
181 RDebug::Print( KWaitMsg );
182 self.iWaitingSignal.Signal();
185 // Wait for a request
187 self.iGoSignal.Wait();
188 _LIT( KGoMsg, "Eraser thread go (%d)" );
189 RDebug::Print( KGoMsg, self.iRequestedFunction );
191 switch( self.iRequestedFunction )
202 self.iRequestedFunction = EIdle;
205 self.iDrive.Disconnect();
209 void CEraser::DoEraseBlock()
214 _LIT( KEraseStartMsg, "Eraser starting erase..." );
215 RDebug::Print( KEraseStartMsg );
217 TInt r = iDrive.Format( TInt64(iOffset), iLength );
221 RDebug::Print( _L("Eraser: FAIL: erase request returns %d"), r );
228 class CSuspendTest : public CBase
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;
246 void DoRandomWriteSoak();
249 TBusLocalDrive iDrive;
260 TBuf8<512> iReadBuffer;
264 CSuspendTest::~CSuspendTest()
276 void CSuspendTest::CreateL()
279 // Create the eraser thread
281 iEraser = new(ELeave) CEraser;
285 // Load the device drivers
288 #ifndef SKIP_PDD_LOAD
289 test.Printf( _L("Loading %S\n"), &KLfsDriverName );
290 r = User::LoadPhysicalDevice( KLfsDriverName );
291 test( KErrNone == r || KErrAlreadyExists == r );
296 test( KErrNone == fs.Connect() );
297 test( KErrNone == fs.SetSessionPath( _L("Z:\\") ) );
299 fs.FileSystemName( name, KLffsLogicalDriveNumber );
300 if( name.Length() > 0 )
302 test.Printf( _L("Unmounting drive") );
303 test( KErrNone == fs.DismountFileSystem( _L("Lffs"), KLffsLogicalDriveNumber) );
304 User::After( 2000000 );
305 test.Printf( _L("Drive unmounted") );
311 // Open a TBusLogicalDevice to it
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;
320 // Get size of Flash drive, block size, block count
322 TLocalDriveCapsV2Buf info;
324 iFlashSize = I64LOW(info().iSize);
325 iBlockSize = info().iEraseBlockSize;
326 iBlockCount = iFlashSize / iBlockSize;
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 );
332 test.Printf( _L("CreateL complete\n") );
336 void CSuspendTest::DoTest()
338 // Main test dispatcher
345 TInt CSuspendTest::EraseOneBlock( TInt aBlockNumber )
347 // Erases block aBlockNumber on Flash
350 TInt blockBaseOffset = aBlockNumber * iBlockSize;
352 test.Printf( _L("Erasing block %d (offs=0x%x)\n"), aBlockNumber, blockBaseOffset );
354 TInt r = iDrive.Format( blockBaseOffset, iBlockSize );
356 test.Printf( _L("... block erased, rv=%d\n"), r );
361 TBool CSuspendTest::ValidateBlock( TInt aBlockNumber, TUint32 aFillWord )
363 // Checks that every word in block aBlockNumber has the value aFillWord
366 TUint offset = aBlockNumber * iBlockSize;
367 test.Printf( _L("Validating block %d (offs=0x%x)\n"), aBlockNumber, offset );
369 TBool failed = EFalse;
370 const TInt readBufLen = iReadBuffer.MaxLength();
372 for( TInt len = iBlockSize; len > 0 && !failed ;)
374 TInt r = iDrive.Read( offset, readBufLen, iReadBuffer );
377 test.Printf( _L("... FAIL: read failed (%d) at offset 0x%x\n"), r, offset );
378 test( KErrNone == r );
380 test( iReadBuffer.Length() == readBufLen );
382 TUint32* p = (TUint32*)iReadBuffer.Ptr();
383 for( TInt i = 0; i < readBufLen; i += 4 )
385 if( aFillWord != *p )
388 test.Printf( _L("... FAILED: word @ offs=0x%x, read=0x%x, expected=0x%x\n"),
389 offset+i, p[0], aFillWord );
394 offset += readBufLen;
402 TInt CSuspendTest::ZeroFillBlock( TInt aBlockNumber )
404 // Zero-fills and entire block
405 // The requires that writing works
408 test.Printf( _L("Zero-filling block %d\n"), aBlockNumber );
411 // Create a buffer full of zeros
413 const TInt KZeroBufSize = 512;
415 TBuf8<KZeroBufSize> buf;
416 buf.FillZ( buf.MaxLength() );
419 // Write the data out to the Flash
421 TInt writeCount = iBlockSize / KZeroBufSize;
423 TUint blockBaseOffset = aBlockNumber * iBlockSize;
424 TInt pos = blockBaseOffset;
425 for( ; (writeCount > 0) && (KErrNone == r); writeCount-- )
427 r = iDrive.Write( pos, buf );
430 test.Printf( _L("... FAIL: write failed (%d) at offset 0x%x\n"), pos );
439 TInt CSuspendTest::ZeroAllBlocks()
441 // Writes zeros to all blocks
444 test.Printf( _L("Zeroing all blocks\n") );
447 for( TInt i = 0; (i < iBlockCount) && (KErrNone == r); i++ )
449 r = ZeroFillBlock( i );
455 TBool CSuspendTest::ValidateAllBlocks( TUint32 aFillWord )
457 // Checks that all blocks contain aFillWord
460 test.Printf( _L("Validating all blocks\n") );
462 TBool failed = EFalse;
463 for( TInt i = 0; (i < iBlockCount) && (!failed); i++ )
465 failed = !ValidateBlock( i, aFillWord );
472 void CSuspendTest::TimeSinceStart() const
474 TTimeIntervalSeconds timeTaken;
477 TInt r = time.SecondsFrom(iStartTime, timeTaken);
479 TInt totalTime = timeTaken.Int();
481 TInt seconds = totalTime % 60;
482 TInt minutes = (totalTime / 60) % 60;
483 TInt hours = totalTime / 3600;
485 test.Printf( _L("Time since test started = %d:%d:%d\n"), hours, minutes, seconds );
490 void CSuspendTest::DoRandomWriteSoak()
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
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
504 test.Next( _L("Erase suspend soak test using random writes") );
506 TRandomGenerator random;
507 random.SetSeed( 0x13E00103 );
509 test.Printf( _L("Preparing buffer") );
511 writeBuf.SetLength( writeBuf.MaxLength() );
512 for( TInt i = writeBuf.Length(); i > 0;)
515 writeBuf[i] = static_cast<TUint8>( random.Next() );
518 test.Printf( _L("Starting test") );
519 random.SetSeed( MAKE_TINT64( 0xA05BE111,0x00101111 ) );
520 iStartTime.HomeTime();
523 // We repeat the test for each block, erasing block n and reading from
524 // block (n+1) modulo iBlockCount
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.
531 TBool firstErase = ETrue;
536 for( TInt eraseBlock = 0; eraseBlock < iBlockCount; eraseBlock++ )
538 TUint32 writeBlock = eraseBlock - 1;
539 if( 0 == eraseBlock )
541 writeBlock = iBlockCount - 1;
544 TUint32 erasePos = eraseBlock * iBlockSize;
545 TInt writePos = writeBlock * iBlockSize;
547 test.Printf( _L("Erasing block %d, writing to block %d"),
548 eraseBlock, writeBlock );
551 // Zero the block we are about to erase
553 test( KErrNone == ZeroFillBlock( eraseBlock ) );
554 test( ValidateBlock( eraseBlock, 0 ) );
559 _LIT( KEraseNotify, "Main thread starting erase\n" );
560 test.Printf( KEraseNotify );
561 iEraser->EraseBlock( erasePos, iBlockSize );
564 // Now we loop, waiting for random intervals, issuing
565 // writes, until the erase completes
568 TBool didWrite = EFalse;
569 while( !iEraser->CheckDone() )
572 // Get a pseudo-random interval between 0 and 524.288 milliseconds
574 TInt delayInMicroseconds = random.Next() % 0x80000;
575 User::After( delayInMicroseconds );
579 test( KErrNone == iDrive.Write( writePos, writeBuf ) );
580 _LIT( KWriteNotify, "Done write" );
581 test.Printf( KWriteNotify );
587 // Now check that the block was erased
589 test( ValidateBlock( eraseBlock, 0xFFFFFFFF ) );
593 // Also check that the data written to the Flash is correct.
598 test( KErrNone == iDrive.Read( writePos, writeBuf.Length(), readBuf ) );
599 test( readBuf == writeBuf );
600 test.Printf( _L("Write data is ok") );
612 test.Start(_L("Testing media erase+suspend operations"));
614 CSuspendTest suspendTest;
615 TRAPD( ret, suspendTest.CreateL() );
616 if( KErrNone == ret )
618 suspendTest.DoTest();