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
20 #include <e32std_private.h>
24 #include "user_config.h"
26 RTest test( _L("TF_SUSPENDSOAK") );
45 inline TBool CheckDone() const
47 return (EIdle == iRequestedFunction);
50 inline void WaitForDone()
53 iWaitingSignal.Signal(); // resignal, ready for next Start()
56 void EraseBlock( TUint32 aOffset, TUint aLength );
59 void Panic( TInt aPanicNum );
60 void Start( TFunction aFunction );
62 static TInt EraserThread( TAny* aParam );
70 // Shared between main & eraser thread
72 TFunction iRequestedFunction;
74 RSemaphore iWaitingSignal;
78 // These are local to the eraser thread
82 TBusLocalDrive iDrive;
89 iThread.Terminate( KErrNone );
92 iWaitingSignal.Close();
95 void CEraser::Panic( TInt aPanicNum )
97 _LIT( KPanicCat, "ERASE-T" );
98 User::Panic( KPanicCat, aPanicNum );
99 RProcess().Panic( KPanicCat, aPanicNum );
103 void CEraser::CreateL()
105 // Create new thread and wait for it to become ready
108 iGoSignal.CreateLocal( 0 ); // initially blocked
109 iWaitingSignal.CreateLocal( 0 ); // initially blocked
111 User::LeaveIfError( iThread.Create( _L("ERASER"), EraserThread, 2048, 2048, 65536, this ) );
112 test.Printf( _L("Eraser thread created\n") );
116 test.Printf( _L("Waiting for thread to become ready\n") );
118 iWaitingSignal.Signal();
121 void CEraser::Start( TFunction aFunction )
123 // Start the suspender thread executing function aFunction
128 iRequestedFunction = aFunction;
140 void CEraser::WaitForReady()
142 iWaitingSignal.Wait();
145 void CEraser::EraseBlock( TUint32 aOffset, TUint aLength )
147 // Execute a single read immediately to cause a suspend
152 Start( EEraseBlock );
156 TInt CEraser::EraserThread( TAny* aParam )
158 // The thread which executes suspend functions
161 RDebug::Print( _L("Eraser thread starts") );
163 CEraser& self = *reinterpret_cast<CEraser*>(aParam);
166 // Open our own TBusLogicalDevice channel
169 if( KErrNone != self.iDrive.Connect( KDriveNumber, changedFlag ) )
174 RDebug::Print( _L("Eraser thread connected to drive") );
179 // Signal that we are ready for a request
181 _LIT( KWaitMsg, "Eraser thread waiting..." );
182 RDebug::Print( KWaitMsg );
183 self.iWaitingSignal.Signal();
186 // Wait for a request
188 self.iGoSignal.Wait();
189 _LIT( KGoMsg, "Eraser thread go (%d)" );
190 RDebug::Print( KGoMsg, self.iRequestedFunction );
192 switch( self.iRequestedFunction )
203 self.iRequestedFunction = EIdle;
206 self.iDrive.Disconnect();
210 void CEraser::DoEraseBlock()
215 _LIT( KEraseStartMsg, "Eraser starting erase..." );
216 RDebug::Print( KEraseStartMsg );
218 TInt r = iDrive.Format( TInt64(iOffset), iLength );
222 RDebug::Print( _L("Eraser: FAIL: erase request returns %d"), r );
229 class CSuspendTest : public CBase
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;
247 void DoRandomReadSoak();
250 TBusLocalDrive iDrive;
261 TBuf8<512> iReadBuffer;
266 CSuspendTest::~CSuspendTest()
278 void CSuspendTest::CreateL()
281 // Create the eraser thread
283 iEraser = new(ELeave) CEraser;
287 // Load the device drivers
290 #ifndef SKIP_PDD_LOAD
291 test.Printf( _L("Loading %S\n"), &KLfsDriverName );
292 r = User::LoadPhysicalDevice( KLfsDriverName );
293 test( KErrNone == r || KErrAlreadyExists == r );
298 test( KErrNone == fs.Connect() );
299 test( KErrNone == fs.SetSessionPath( _L("Z:\\") ) );
301 fs.FileSystemName( name, KLffsLogicalDriveNumber );
302 if( name.Length() > 0 )
304 test.Printf( _L("Unmounting drive") );
305 test( KErrNone == fs.DismountFileSystem( _L("Lffs"), KLffsLogicalDriveNumber) );
306 User::After( 2000000 );
307 test.Printf( _L("Drive unmounted") );
313 // Open a TBusLogicalDevice to it
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;
322 // Get size of Flash drive, block size, block count
324 TLocalDriveCapsV2Buf info;
326 iFlashSize = I64LOW(info().iSize);
327 iBlockSize = info().iEraseBlockSize;
328 iBlockCount = iFlashSize / iBlockSize;
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 );
334 test.Printf( _L("CreateL complete\n") );
338 void CSuspendTest::DoTest()
340 // Main test dispatcher
347 TInt CSuspendTest::EraseOneBlock( TInt aBlockNumber )
349 // Erases block aBlockNumber on Flash
352 TInt blockBaseOffset = aBlockNumber * iBlockSize;
354 test.Printf( _L("Erasing block %d (offs=0x%x)\n"), aBlockNumber, blockBaseOffset );
356 TInt r = iDrive.Format( blockBaseOffset, iBlockSize );
358 test.Printf( _L("... block erased, rv=%d\n"), r );
363 TBool CSuspendTest::ValidateBlock( TInt aBlockNumber, TUint32 aFillWord )
365 // Checks that every word in block aBlockNumber has the value aFillWord
368 TUint offset = aBlockNumber * iBlockSize;
369 test.Printf( _L("Validating block %d (offs=0x%x)\n"), aBlockNumber, offset );
371 TBool failed = EFalse;
372 const TInt readBufLen = iReadBuffer.MaxLength();
374 for( TInt len = iBlockSize; len > 0 && !failed ;)
376 TInt r = iDrive.Read( offset, readBufLen, iReadBuffer );
379 test.Printf( _L("... FAIL: read failed (%d) at offset 0x%x\n"), r, offset );
380 test( KErrNone == r );
382 test( iReadBuffer.Length() == readBufLen );
384 TUint32* p = (TUint32*)iReadBuffer.Ptr();
385 for( TInt i = 0; i < readBufLen; i += 4 )
387 if( aFillWord != *p )
390 test.Printf( _L("... FAILED: word @ offs=0x%x, read=0x%x, expected=0x%x\n"),
391 offset+i, p[0], aFillWord );
396 offset += readBufLen;
404 TInt CSuspendTest::ZeroFillBlock( TInt aBlockNumber )
406 // Zero-fills and entire block
407 // The requires that writing works
410 test.Printf( _L("Zero-filling block %d\n"), aBlockNumber );
413 // Create a buffer full of zeros
415 const TInt KZeroBufSize = 512;
417 TBuf8<KZeroBufSize> buf;
418 buf.FillZ( buf.MaxLength() );
421 // Write the data out to the Flash
423 TInt writeCount = iBlockSize / KZeroBufSize;
425 TUint blockBaseOffset = aBlockNumber * iBlockSize;
426 TInt pos = blockBaseOffset;
427 for( ; (writeCount > 0) && (KErrNone == r); writeCount-- )
429 r = iDrive.Write( pos, buf );
432 test.Printf( _L("... FAIL: write failed (%d) at offset 0x%x\n"), pos );
441 TInt CSuspendTest::ZeroAllBlocks()
443 // Writes zeros to all blocks
446 test.Printf( _L("Zeroing all blocks\n") );
449 for( TInt i = 0; (i < iBlockCount) && (KErrNone == r); i++ )
451 r = ZeroFillBlock( i );
457 TBool CSuspendTest::ValidateAllBlocks( TUint32 aFillWord )
459 // Checks that all blocks contain aFillWord
462 test.Printf( _L("Validating all blocks\n") );
464 TBool failed = EFalse;
465 for( TInt i = 0; (i < iBlockCount) && (!failed); i++ )
467 failed = !ValidateBlock( i, aFillWord );
474 void CSuspendTest::TimeSinceStart() const
476 TTimeIntervalSeconds timeTaken;
479 TInt r = time.SecondsFrom(iStartTime, timeTaken);
481 TInt totalTime = timeTaken.Int();
483 TInt seconds = totalTime % 60;
484 TInt minutes = (totalTime / 60) % 60;
485 TInt hours = totalTime / 3600;
487 test.Printf( _L("Time since test started = %d:%d:%d\n"), hours, minutes, seconds );
492 void CSuspendTest::DoRandomReadSoak()
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
502 TRandomGenerator random;
503 random.SetSeed( MAKE_TINT64(0xA05BE111,0x00101111) );
505 test.Next( _L("Random read soak test") );
507 iStartTime.HomeTime();
510 // We repeat the test for each block, erasing block n and reading from
511 // block (n+1) modulo iBlockCount
517 for( TInt eraseBlock = 0; eraseBlock < iBlockCount; eraseBlock++ )
519 TUint32 readBlock = (eraseBlock + 1) % iBlockCount;
520 TUint32 erasePos = eraseBlock * iBlockSize;
521 TInt readPos = readBlock * iBlockSize;
524 // Zero the block we are about to erase
526 test( KErrNone == ZeroFillBlock( eraseBlock ) );
527 test( ValidateBlock( eraseBlock, 0 ) );
534 _LIT( KEraseNotify, "Main thread starting erase\n" );
535 test.Printf( KEraseNotify );
536 iEraser->EraseBlock( erasePos, iBlockSize );
539 // Now we loop, waiting for random intervals, issuing
540 // reads, until the erase completes
543 while( !iEraser->CheckDone() )
546 // Get a pseudo-random interval between 0 and 524.288 milliseconds
548 TInt delayInMicroseconds = random.Next() % 0x80000;
549 User::After( delayInMicroseconds );
551 test( KErrNone == iDrive.Read( readPos, buf.MaxLength(), buf ) );
552 _LIT( KReadNotify, "Done Read" );
553 test.Printf( KReadNotify );
557 // Now check that the block was erased
559 test( ValidateBlock( eraseBlock, 0xFFFFFFFF ) );
572 test.Start(_L("Testing media erase+suspend operations"));
574 CSuspendTest suspendTest;
575 TRAPD( ret, suspendTest.CreateL() );
576 if( KErrNone == ret )
578 suspendTest.DoTest();