Update contrib.
1 // Copyright (c) 1996-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 // Very simple test of CPU overhead
23 #include <e32std_private.h>
25 #include <e32base_private.h>
28 #include "user_config.h"
31 #define TEST_WRITE_OVERHEAD
32 #define NON_WRITING_LOOPS
34 const TInt KAverageOverInSeconds=10; ///< Number of seconds to run tests for
36 TInt64 Count; ///< Global variable used to count number of operations completed
37 RSemaphore CountSem; ///< control access to Count;
40 RTest test(_L("BF_CPU"));
45 TLocalDriveCapsV2Buf driveInfo;
47 LOCAL_D TBool StopTest; ///< set to ETrue to stop the test
50 #ifdef TEST_WRITE_OVERHEAD
51 LOCAL_D TBool StopZeroTest;
53 LOCAL_C TInt WriteZeroThread(TAny*)
55 * Performs writes of zero length continuously
59 _LIT( KPanicCat, "ZERWRTH" );
65 while( !StopZeroTest )
67 // Return values are bogus when doing overhead testing
68 drive.Write( 513, buf );
75 LOCAL_C TInt WriteThread(TAny*)
77 * Performs writes continuously
80 _LIT( KPanicCat, "WRTHRD" );
84 buf.Fill(0xFF); // all 0xFF so we can repeatedly overwrite
88 TInt r = drive.Write( 0, buf );
91 User::Panic( KPanicCat, r );
99 LOCAL_C TInt CpuThread(TAny*)
101 * Just increments the counter
107 #ifdef NON_WRITING_LOOPS
108 for( volatile TInt i = 5000; i > 0; i-- );
120 TInt r=writeThread.Create(_L("WRITER"),WriteThread,KDefaultStackSize,&User::Heap(),NULL);
124 r=cpuThread.Create(_L("CPU-ER"),CpuThread,KDefaultStackSize,&User::Heap(),NULL);
127 #ifdef TEST_WRITE_OVERHEAD
128 RThread writeZeroThread;
129 r=writeZeroThread.Create(_L("WRITERZERO"),WriteZeroThread,KDefaultStackSize,&User::Heap(),NULL);
133 r = CountSem.CreateLocal(1);
137 StopTest = EFalse; // allow the test to run
139 TRequestStatus deadStatWrite;
140 TRequestStatus deadStatCpu;
141 writeThread.Logon( deadStatWrite );
142 cpuThread.Logon( deadStatCpu );
144 // make writer thread have priority over CPU usage thread
145 writeThread.SetPriority( EPriorityMore );
147 // make this thread highest priority
148 RThread().SetPriority( EPriorityMuchMore );
153 #ifdef TEST_WRITE_OVERHEAD
154 TRequestStatus deadStatWriteZero;
155 writeZeroThread.Logon( deadStatWriteZero );
156 // make writer thread have priority over CPU usage thread
157 writeZeroThread.SetPriority( EPriorityMore );
158 StopZeroTest = EFalse;
159 writeZeroThread.Resume();
162 // wait for thread to initialise
163 User::After(1000000);
169 User::After(KAverageOverInSeconds*1000000);
172 TInt64 noWriteCount( Count ); // number of counts when not writing
176 #ifdef TEST_WRITE_OVERHEAD
177 // kill the zero writer
178 StopZeroTest = ETrue;
179 User::WaitForRequest( deadStatWriteZero );
180 CLOSE_AND_WAIT(writeZeroThread);
183 test.Printf( _L("Loops without writing = %ld"), noWriteCount );
185 // start write thread
186 writeThread.Resume();
187 User::After(1000000);
193 User::After(KAverageOverInSeconds*1000000);
196 TInt64 withWriteCount( Count ); // number of counts when writing
199 test.Printf( _L("Loops while writing = %ld"), withWriteCount );
201 // tell test to stop and wait for thread to exit.
202 cpuThread.Kill(KErrNone);
204 User::WaitForRequest( deadStatWrite );
206 CLOSE_AND_WAIT(writeThread);
207 CLOSE_AND_WAIT(cpuThread);
211 TInt64 calc( withWriteCount );
213 calc = calc / noWriteCount;
215 test.Printf( _L("%% CPU used = %d"), 100 - I64LOW(calc) );
220 LOCAL_C TInt EraseSegment( TInt aSegmentNumber )
222 * Erases a segment on Flash
224 * @param aSegmentNumber index of segment to erase
225 * @return KErrNone or error code
228 TInt offset = aSegmentNumber * driveInfo().iEraseBlockSize;
230 TInt r = drive.Format( offset, driveInfo().iEraseBlockSize );
231 test.Printf( _L("erase returns %d"), r );
240 * Open channel to media driver
244 // Load the media driver
246 #ifndef SKIP_PDD_LOAD
247 test.Printf( _L("Loading %S\n"), &KLfsDriverName );
248 TInt r = User::LoadPhysicalDevice( KLfsDriverName );
249 test( KErrNone == r || KErrAlreadyExists == r );
254 test( KErrNone == fs.Connect() );
257 test( KErrNone == fs.SetDefaultPath( _L("Z:\\") ) );
260 fs.FileSystemName( name, KLffsLogicalDriveNumber );
261 if( name.Length() > 0 )
263 test.Printf( _L("Unmounting drive") );
264 test( KErrNone == fs.DismountFileSystem( _L("Lffs"), KLffsLogicalDriveNumber) );
265 User::After( 2000000 );
266 test.Printf( _L("Drive unmounted") );
272 // Open a TBusLogicalDevice to it
274 test.Printf( _L("Opening media channel\n") );
275 TBool changedFlag = EFalse;
276 test( KErrNone == drive.Connect( KDriveNumber, changedFlag ) );
279 // Get size of Flash drive, block size, block count
281 drive.Caps(driveInfo);
290 test.Start(_L("Testing CPU overhead"));
294 test.Printf( _L("Erasing first segment") );
295 TInt r = EraseSegment( 0 );
296 test( KErrNone == r );
297 test.Printf( _L("Segment erased") );