os/kernelhwsrv/kerneltest/e32test/lffs/bf_cpu.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/lffs/bf_cpu.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,305 @@
     1.4 +// Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// Very simple test of CPU overhead
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +/**
    1.22 + @file bf_cpu.cpp
    1.23 +*/
    1.24 +
    1.25 +#include <e32std.h>
    1.26 +#include <e32std_private.h>
    1.27 +#include <e32base.h>
    1.28 +#include <e32base_private.h>
    1.29 +#include <e32test.h>
    1.30 +#include <e32svr.h>
    1.31 +#include "user_config.h"
    1.32 +
    1.33 +
    1.34 +#define TEST_WRITE_OVERHEAD
    1.35 +#define NON_WRITING_LOOPS
    1.36 +
    1.37 +const TInt KAverageOverInSeconds=10;	///< Number of seconds to run tests for
    1.38 +
    1.39 +TInt64 Count;				///< Global variable used to count number of operations completed
    1.40 +RSemaphore CountSem;		///< control access to Count;
    1.41 +
    1.42 +
    1.43 +RTest test(_L("BF_CPU"));
    1.44 +
    1.45 +
    1.46 +
    1.47 +TBusLocalDrive	drive;
    1.48 +TLocalDriveCapsV2Buf driveInfo;
    1.49 +
    1.50 +LOCAL_D TBool StopTest;		///< set to ETrue to stop the test
    1.51 +
    1.52 +
    1.53 +#ifdef TEST_WRITE_OVERHEAD
    1.54 +LOCAL_D TBool StopZeroTest;
    1.55 +
    1.56 +LOCAL_C TInt WriteZeroThread(TAny*)
    1.57 +	/**
    1.58 +	 * Performs writes of zero length continuously
    1.59 +	 */
    1.60 +	{
    1.61 +#if 0
    1.62 +	_LIT( KPanicCat, "ZERWRTH" );
    1.63 +#endif
    1.64 +	
    1.65 +	TBuf8<513> buf;
    1.66 +	buf.SetLength(513);
    1.67 +	
    1.68 +	while( !StopZeroTest )
    1.69 +		{
    1.70 +		// Return values are bogus when doing overhead testing
    1.71 +		drive.Write( 513, buf );
    1.72 +		}
    1.73 +	return KErrNone;
    1.74 +	}
    1.75 +#endif
    1.76 +
    1.77 +
    1.78 +LOCAL_C TInt WriteThread(TAny*)
    1.79 +	/**
    1.80 +	 * Performs writes continuously
    1.81 +	 */
    1.82 +	{
    1.83 +	_LIT( KPanicCat, "WRTHRD" );
    1.84 +	
    1.85 +	TBuf8<512> buf;
    1.86 +	buf.SetLength(512);
    1.87 +	buf.Fill(0xFF);		// all 0xFF so we can repeatedly overwrite
    1.88 +	
    1.89 +	while( !StopTest )
    1.90 +		{
    1.91 +		TInt r = drive.Write( 0, buf );
    1.92 +		if( KErrNone != r )
    1.93 +			{
    1.94 +			User::Panic( KPanicCat, r );
    1.95 +			}
    1.96 +		}
    1.97 +	return KErrNone;
    1.98 +	}
    1.99 +
   1.100 +
   1.101 +
   1.102 +LOCAL_C TInt CpuThread(TAny*)
   1.103 +	/**
   1.104 +	 * Just increments the counter
   1.105 +	 */
   1.106 +	{
   1.107 +	while( !StopTest )
   1.108 +		{
   1.109 +		CountSem.Wait();
   1.110 +#ifdef NON_WRITING_LOOPS
   1.111 +		for( volatile TInt i = 5000; i > 0; i-- );
   1.112 +#endif
   1.113 +		++Count;
   1.114 +		CountSem.Signal();
   1.115 +		}
   1.116 +	return KErrNone;
   1.117 +	}
   1.118 +
   1.119 +
   1.120 +void runTest()
   1.121 +	{
   1.122 +    RThread writeThread;
   1.123 +	TInt r=writeThread.Create(_L("WRITER"),WriteThread,KDefaultStackSize,&User::Heap(),NULL);
   1.124 +	test(r==KErrNone);
   1.125 +
   1.126 +	RThread cpuThread;
   1.127 +	r=cpuThread.Create(_L("CPU-ER"),CpuThread,KDefaultStackSize,&User::Heap(),NULL);
   1.128 +	test(r==KErrNone);
   1.129 +
   1.130 +#ifdef TEST_WRITE_OVERHEAD
   1.131 +    RThread writeZeroThread;
   1.132 +	r=writeZeroThread.Create(_L("WRITERZERO"),WriteZeroThread,KDefaultStackSize,&User::Heap(),NULL);
   1.133 +	test(r==KErrNone);
   1.134 +#endif
   1.135 +
   1.136 +	r = CountSem.CreateLocal(1);
   1.137 +	test(r==KErrNone);
   1.138 +
   1.139 +
   1.140 +	StopTest = EFalse;	// allow the test to run
   1.141 +
   1.142 +    TRequestStatus deadStatWrite;
   1.143 +    TRequestStatus deadStatCpu;
   1.144 +	writeThread.Logon( deadStatWrite );
   1.145 +	cpuThread.Logon( deadStatCpu );
   1.146 +	
   1.147 +	// make writer thread have priority over CPU usage thread
   1.148 +	writeThread.SetPriority( EPriorityMore );
   1.149 +
   1.150 +	// make this thread highest priority
   1.151 +	RThread().SetPriority( EPriorityMuchMore );
   1.152 +	
   1.153 +	
   1.154 +    cpuThread.Resume();
   1.155 +	
   1.156 +#ifdef TEST_WRITE_OVERHEAD
   1.157 +    TRequestStatus deadStatWriteZero;
   1.158 +	writeZeroThread.Logon( deadStatWriteZero );
   1.159 +	// make writer thread have priority over CPU usage thread
   1.160 +	writeZeroThread.SetPriority( EPriorityMore );
   1.161 +	StopZeroTest = EFalse;
   1.162 +	writeZeroThread.Resume();
   1.163 +#endif
   1.164 +	
   1.165 +	// wait for thread to initialise
   1.166 +	User::After(1000000);
   1.167 +
   1.168 +	CountSem.Wait();
   1.169 +	Count=0;
   1.170 +	CountSem.Signal();
   1.171 +
   1.172 +    User::After(KAverageOverInSeconds*1000000);
   1.173 +    
   1.174 +	CountSem.Wait();
   1.175 +	TInt64 noWriteCount( Count );	// number of counts when not writing
   1.176 +	CountSem.Signal();
   1.177 +
   1.178 +	
   1.179 +#ifdef TEST_WRITE_OVERHEAD
   1.180 +	// kill the zero writer
   1.181 +	StopZeroTest = ETrue;
   1.182 +	User::WaitForRequest( deadStatWriteZero );
   1.183 +	CLOSE_AND_WAIT(writeZeroThread);
   1.184 +#endif
   1.185 +    
   1.186 +	test.Printf( _L("Loops without writing = %ld"), noWriteCount );
   1.187 +	
   1.188 +	// start write thread
   1.189 +	writeThread.Resume();
   1.190 +	User::After(1000000);
   1.191 +    
   1.192 +	CountSem.Wait();
   1.193 +	Count=0;
   1.194 +	CountSem.Signal();
   1.195 +
   1.196 +    User::After(KAverageOverInSeconds*1000000);
   1.197 +    
   1.198 +	CountSem.Wait();
   1.199 +	TInt64 withWriteCount( Count );	// number of counts when writing
   1.200 +	CountSem.Signal();
   1.201 +
   1.202 +	test.Printf( _L("Loops while writing = %ld"), withWriteCount );
   1.203 +	
   1.204 +	// tell test to stop and wait for thread to exit.
   1.205 +	cpuThread.Kill(KErrNone);
   1.206 +	StopTest = ETrue;
   1.207 +	User::WaitForRequest( deadStatWrite );
   1.208 +	
   1.209 +	CLOSE_AND_WAIT(writeThread);
   1.210 +	CLOSE_AND_WAIT(cpuThread);
   1.211 +    
   1.212 +
   1.213 +
   1.214 +	TInt64 calc( withWriteCount );
   1.215 +	calc = calc * 100;
   1.216 +	calc = calc / noWriteCount;
   1.217 +		
   1.218 +	test.Printf( _L("%% CPU used = %d"), 100 - I64LOW(calc) );
   1.219 +    }
   1.220 +
   1.221 +
   1.222 +
   1.223 +LOCAL_C TInt EraseSegment( TInt aSegmentNumber )
   1.224 +	/**
   1.225 +	 * Erases a segment on Flash
   1.226 +	 *
   1.227 +	 * @param aSegmentNumber index of segment to erase
   1.228 +	 * @return KErrNone or error code
   1.229 +	 */
   1.230 +	{
   1.231 +	TInt offset = aSegmentNumber * driveInfo().iEraseBlockSize;
   1.232 +	
   1.233 +	TInt r = drive.Format( offset, driveInfo().iEraseBlockSize );
   1.234 +	test.Printf( _L("erase returns %d"), r );
   1.235 +	return  r;
   1.236 +	}
   1.237 +
   1.238 +
   1.239 +
   1.240 +
   1.241 +void Initialize()
   1.242 +	/**
   1.243 +	 * Open channel to media driver
   1.244 +	 */
   1.245 +	{
   1.246 +	//
   1.247 +	// Load the media driver
   1.248 +	//
   1.249 +#ifndef SKIP_PDD_LOAD
   1.250 +	test.Printf( _L("Loading %S\n"), &KLfsDriverName );
   1.251 +	TInt r = User::LoadPhysicalDevice( KLfsDriverName );
   1.252 +	test( KErrNone == r || KErrAlreadyExists == r );
   1.253 +#endif
   1.254 +
   1.255 +#ifdef UNMOUNT_DRIVE
   1.256 +	RFs fs;
   1.257 +	test( KErrNone == fs.Connect() );
   1.258 +#if 0
   1.259 +	// XXX not EKA2
   1.260 +	test( KErrNone == fs.SetDefaultPath( _L("Z:\\") ) );
   1.261 +#endif
   1.262 +	TFullName name;
   1.263 +	fs.FileSystemName( name, KLffsLogicalDriveNumber );
   1.264 +	if( name.Length() > 0 )
   1.265 +		{
   1.266 +		test.Printf( _L("Unmounting drive") );
   1.267 +		test( KErrNone == fs.DismountFileSystem( _L("Lffs"), KLffsLogicalDriveNumber) );
   1.268 +		User::After( 2000000 );
   1.269 +		test.Printf( _L("Drive unmounted") );
   1.270 +		}
   1.271 +	fs.Close();
   1.272 +#endif
   1.273 +
   1.274 +	//
   1.275 +	// Open a TBusLogicalDevice to it
   1.276 +	//
   1.277 +	test.Printf( _L("Opening media channel\n") );
   1.278 +	TBool changedFlag = EFalse;
   1.279 +	test( KErrNone == drive.Connect( KDriveNumber, changedFlag ) );
   1.280 +	
   1.281 +	//
   1.282 +	// Get size of Flash drive, block size, block count
   1.283 +	//
   1.284 +    drive.Caps(driveInfo);
   1.285 +	}
   1.286 +
   1.287 +
   1.288 +
   1.289 +TInt E32Main()
   1.290 +    {
   1.291 +
   1.292 +    test.Title();
   1.293 +    test.Start(_L("Testing CPU overhead"));
   1.294 +
   1.295 +	Initialize();
   1.296 +
   1.297 +	test.Printf( _L("Erasing first segment") );
   1.298 +	TInt r = EraseSegment( 0 );
   1.299 +	test( KErrNone == r );
   1.300 +	test.Printf( _L("Segment erased") );
   1.301 +
   1.302 +	runTest();
   1.303 +
   1.304 +	drive.Disconnect();
   1.305 +    test.End();
   1.306 +	return(KErrNone);
   1.307 +    }
   1.308 +