os/kernelhwsrv/kerneltest/e32test/bench/t_asmbm.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/bench/t_asmbm.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,231 @@
     1.4 +// Copyright (c) 2005-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 +// e32test\bench\t_asmbm.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include "t_asmbm.h"
    1.22 +#include <hal.h>
    1.23 +#include <e32test.h>
    1.24 +
    1.25 +extern RTest test;
    1.26 +
    1.27 +const TReal KDefaultRunLength = 1.0;
    1.28 +const TInt KInitIterations = 3000;
    1.29 +
    1.30 +// Length of time to run benchmark for in seconds
    1.31 +TReal RunLength = KDefaultRunLength;
    1.32 +
    1.33 +TInt FastCounterFrequency;
    1.34 +TBool FastCounterCountsUp;
    1.35 +
    1.36 +/**
    1.37 + * Calculate the time in seconds corresponding to a fast counter delta value.
    1.38 + */
    1.39 +TReal TimeDelta(TInt aDelta)
    1.40 +	{
    1.41 +	if (!FastCounterCountsUp)
    1.42 +		aDelta = -aDelta;
    1.43 +	return ((TReal) aDelta + 1) / FastCounterFrequency;
    1.44 +	}
    1.45 +
    1.46 +/**
    1.47 + * Run a benchmark for the specifiec number of iterations and return the total
    1.48 + * time taken in seconds.
    1.49 + */
    1.50 +TReal TimeBenchmarkL(MBenchmarkList& aBenchmarks, TInt aIndex, const TBmParams& aParams)
    1.51 +	{
    1.52 +	TInt delta = 0;
    1.53 +	User::LeaveIfError(aBenchmarks.Run(aIndex, aParams, delta));
    1.54 +	User::After(20 * 1000); // hack: wait for kernel thread to exit
    1.55 +	return TimeDelta(delta);
    1.56 +	}
    1.57 +
    1.58 +void RunGeneralBenchmarkL(MBenchmarkList& aBenchmarks, TInt aIndex, const TBmInfo& aInfo)
    1.59 +    {
    1.60 +	// Run the benchmark with a small number of iterations and from this result
    1.61 +	// work out how many iterations we need to run it for RunLength seconds.
    1.62 +	// Loop till we get it right.
    1.63 +
    1.64 +	TBmParams params;
    1.65 +	params.iSourceAlign = 0;
    1.66 +	params.iDestAlign = 0;
    1.67 +	
    1.68 +	TInt iterations = KInitIterations;
    1.69 +	TReal time;
    1.70 +	for (;;)
    1.71 +		{
    1.72 +		params.iIts = iterations / 10;
    1.73 +		time = TimeBenchmarkL(aBenchmarks, aIndex, params);
    1.74 +		if (time >= RunLength)
    1.75 +			break;
    1.76 +		iterations = (TInt) ((RunLength * 1.2) / (time/(TReal)iterations));
    1.77 +		}
    1.78 +	
    1.79 +	TBuf<64> nameBuf;
    1.80 +	nameBuf.Copy(aInfo.iName);
    1.81 +    test.Printf(_L("%i\t%e\t%S\n"), aIndex, time/iterations * 1000000.0, &nameBuf);
    1.82 +    }
    1.83 +
    1.84 +void RunMemoryBenchmarkL(MBenchmarkList& aBenchmarks, TInt aIndex, const TBmInfo& aInfo)
    1.85 +    {
    1.86 +	// Run the benchmark with a small number of iterations and from this result
    1.87 +	// work out how many iterations we need to run it for RunLength seconds.
    1.88 +	// Loop till we get it right.
    1.89 +	
    1.90 +	TBmParams params;
    1.91 +	params.iSourceAlign = 0;
    1.92 +	params.iDestAlign = 0;
    1.93 +
    1.94 +	TInt iterations = KInitIterations;
    1.95 +	TReal time;
    1.96 +	for (;;)
    1.97 +		{
    1.98 +		params.iIts = iterations / 10;
    1.99 +		time = TimeBenchmarkL(aBenchmarks, aIndex, params);
   1.100 +		if (time >= RunLength)
   1.101 +			break;
   1.102 +		iterations = (TInt) ((RunLength * 1.2) / (time/(TReal)iterations));
   1.103 +		}
   1.104 +
   1.105 +	TBuf<64> nameBuf;
   1.106 +	nameBuf.Copy(aInfo.iName);
   1.107 +	test.Printf(_L("%i\t%S\talignment step == %d\n"), aIndex, &nameBuf, aInfo.iAlignStep);
   1.108 +
   1.109 +	for (TInt sourceAlign = 0 ; sourceAlign < 32 ; sourceAlign += aInfo.iAlignStep)
   1.110 +		{
   1.111 +		for (TInt destAlign = 0 ; destAlign < 32 ; destAlign += aInfo.iAlignStep)
   1.112 +			{
   1.113 +			params.iSourceAlign = sourceAlign;
   1.114 +			params.iDestAlign = destAlign;
   1.115 +			time = TimeBenchmarkL(aBenchmarks, aIndex, params);
   1.116 +			test.Printf(_L("%e\t"), time/iterations * 1000000.0);
   1.117 +			}
   1.118 +		test.Printf(_L("\n"));
   1.119 +		}
   1.120 +    }
   1.121 +
   1.122 +void RunBenchmarkL(MBenchmarkList& aBenchmarks, TInt aIndex, TUint aCategories)
   1.123 +	{
   1.124 +	TBmInfo info;
   1.125 +	User::LeaveIfError(aBenchmarks.Info(aIndex, info));
   1.126 +
   1.127 +	if (!(info.iCategories & aCategories))
   1.128 +		return;
   1.129 +
   1.130 +	if (info.iCategories & aCategories & KCategoryMemory)
   1.131 +		RunMemoryBenchmarkL(aBenchmarks, aIndex, info);
   1.132 +	else
   1.133 +		RunGeneralBenchmarkL(aBenchmarks, aIndex, info);
   1.134 +	}
   1.135 +
   1.136 +void BadUsage()
   1.137 +	{
   1.138 +	test.Printf(_L("usage: [ OPTIONS ] [ INDEX... ]\n"));
   1.139 +	test.Printf(_L("Options are:\n"));
   1.140 +	test.Printf(_L("  -r TIME  Set the length of time in seconds to run each benchmark for\n"));
   1.141 +	test.Printf(_L("  -m       Run memory alignment benchmarks only\n"));
   1.142 +	test.Printf(_L("  -x       Run extra benchmarks as well as normal ones\n"));
   1.143 +	}
   1.144 +
   1.145 +void RunBenchmarkTestsL(MBenchmarkList& aBenchmarks)
   1.146 +	{
   1.147 +	InitDataL();
   1.148 +	
   1.149 +	User::LeaveIfError(HAL::Get(HALData::EFastCounterFrequency, FastCounterFrequency));
   1.150 +	User::LeaveIfError(HAL::Get(HALData::EFastCounterCountsUp, FastCounterCountsUp));
   1.151 +
   1.152 +	TInt count = aBenchmarks.Count();
   1.153 +	TBool ok = ETrue;
   1.154 +	TUint categories = KCategoryGeneral;
   1.155 +
   1.156 +	RArray<TInt> testsToRun;
   1.157 +	CleanupClosePushL(testsToRun);
   1.158 +
   1.159 +	HBufC* buf = HBufC::NewLC(User::CommandLineLength());
   1.160 +	TPtr ptr = buf->Des();
   1.161 +	User::CommandLine(ptr);
   1.162 +
   1.163 +	if (ptr != KNullDesC)
   1.164 +		{
   1.165 +		TLex lex(ptr);
   1.166 +		TPtrC16 token;
   1.167 +
   1.168 +		while (ok && (token.Set(lex.NextToken()), token != KNullDesC))
   1.169 +			{
   1.170 +			if (token == _L("-r"))
   1.171 +				{
   1.172 +				token.Set(lex.NextToken());
   1.173 +				if (token == KNullDesC ||
   1.174 +					TLex(token).Val(RunLength) != KErrNone ||
   1.175 +					RunLength < 0.0)
   1.176 +					{
   1.177 +					BadUsage();
   1.178 +					ok = EFalse;
   1.179 +					}
   1.180 +				}
   1.181 +			else if (token == _L("-m"))
   1.182 +				{
   1.183 +				categories = KCategoryMemory;
   1.184 +				}
   1.185 +			else if (token == _L("-x"))
   1.186 +				{
   1.187 +				categories = KCategoryGeneral | KCategoryExtra;
   1.188 +				}
   1.189 +			else
   1.190 +				{
   1.191 +				TInt index;
   1.192 +				if (TLex(token).Val(index) != KErrNone)
   1.193 +					{
   1.194 +					BadUsage();
   1.195 +					ok = EFalse;
   1.196 +					}
   1.197 +				else if (index < 0 || index >= count)
   1.198 +					{
   1.199 +					test.Printf(_L("Index out of range: %d\n"), index);
   1.200 +					ok = EFalse;
   1.201 +					}
   1.202 +				else
   1.203 +					{
   1.204 +					testsToRun.AppendL(index);
   1.205 +					}
   1.206 +				}
   1.207 +			}
   1.208 +		}
   1.209 +
   1.210 +	CleanupStack::PopAndDestroy(buf);
   1.211 +
   1.212 +	if (ok)
   1.213 +		{
   1.214 +		test.Printf(_L("Note that these benchmarks are intended to guide optimisation, and not to\n"));
   1.215 +		test.Printf(_L("provide a meaningful indication of the speed of specific functions\n"));
   1.216 +		test.Printf(_L("\n"));
   1.217 +		if (testsToRun.Count() == 0)
   1.218 +			{
   1.219 +			for (TInt i = 0 ; i < count ; ++i)
   1.220 +				{				
   1.221 +				RunBenchmarkL(aBenchmarks, i, categories);
   1.222 +				}
   1.223 +			}
   1.224 +		else
   1.225 +			{
   1.226 +			for (TInt i = 0 ; i < testsToRun.Count() ; ++i)
   1.227 +				{
   1.228 +				RunBenchmarkL(aBenchmarks, testsToRun[i], categories);
   1.229 +				}
   1.230 +			}
   1.231 +		}
   1.232 +	
   1.233 +	CleanupStack::PopAndDestroy(&testsToRun);
   1.234 +	}