os/kernelhwsrv/kerneltest/e32test/active/t_timerduration.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/active/t_timerduration.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,177 @@
     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 +// Overview:
    1.18 +// The test measures the duration of the user-side timer services.
    1.19 +// API Information:
    1.20 +// User::After(...)
    1.21 +// User::At(...)
    1.22 +// User::AfterHighRes(...)
    1.23 +// Details:
    1.24 +// - Calls time services a number of times with the same input arguments.
    1.25 +// - Records and prints the minimum and maximum duration of each test case.
    1.26 +// - Tests the duration of User::After and User::AfterHighRes on target.
    1.27 +// Platforms/Drives/Compatibility:
    1.28 +// Emulator and Hardware (Automatic). 
    1.29 +// Assumptions/Requirement/Pre-requisites:
    1.30 +// Failures and causes:
    1.31 +// The test can fail only on target.
    1.32 +// - The duration of Timer::After(aTime) is not within the limits (from <aTime> to <aTime + 1000000/64+2*NanoKarnelTickPeriod>)
    1.33 +// - The duration of Timer::AfterHighRes(aTime) is not within the limits (from <aTime> to <aTime+2*NanoKarnelTickPeriod>)
    1.34 +// Base Port information:
    1.35 +// 
    1.36 +//
    1.37 +
    1.38 +#include <e32std.h>
    1.39 +#include <e32std_private.h>
    1.40 +#include <e32test.h>
    1.41 +#include <e32math.h>
    1.42 +#include <e32svr.h>
    1.43 +#include <hal.h>
    1.44 +
    1.45 +LOCAL_D RTest test(_L("T_TIMERDURATION"));
    1.46 +
    1.47 +// Max number of different time values measured
    1.48 +const TInt KMaxTimeValues = 12;
    1.49 +
    1.50 +// number of times measurement taken to average
    1.51 +const TInt KMaxTimeMeasurements = 20;
    1.52 +TInt MaxTimeMeasurements;
    1.53 +TInt TimeRawMS[KMaxTimeMeasurements];//Holds the ROW time in Kernel ticks
    1.54 +
    1.55 +TInt* TimeValue;
    1.56 +TInt TimeMin[KMaxTimeValues];
    1.57 +TInt TimeMax[KMaxTimeValues];
    1.58 +	
    1.59 +void calcStats(TInt i)
    1.60 +	{
    1.61 +	TimeMin[i]=TimeRawMS[0];
    1.62 +	TimeMax[i]=TimeRawMS[0];
    1.63 +	for (TInt j=1; j<MaxTimeMeasurements; ++j)
    1.64 +			{
    1.65 +			if (TimeMin[i]>TimeRawMS[j]) TimeMin[i]=TimeRawMS[j];
    1.66 +			if (TimeMax[i]<TimeRawMS[j]) TimeMax[i]=TimeRawMS[j];
    1.67 +			}
    1.68 +	}
    1.69 +
    1.70 +void printStats()
    1.71 +	{
    1.72 +	test.Printf(_L("Value\tMin\tMax"));
    1.73 +	for (TInt i=0;i<KMaxTimeValues;++i)
    1.74 +		{
    1.75 +		if (TimeValue[i]<0) break;
    1.76 +		test.Printf(_L("%d\t%d\t%d"),TimeValue[i],TimeMin[i],TimeMax[i]);
    1.77 +		}
    1.78 +	}
    1.79 +
    1.80 +#define __BEFORE_WAIT__ \
    1.81 +	test.Printf(_L("Measuring value(%d measurements at each value):"), MaxTimeMeasurements);\
    1.82 +	for (i=0;i<KMaxTimeValues;++i)\
    1.83 +		{\
    1.84 +		if (TimeValue[i]<0) break;\
    1.85 +		test.Printf(_L("%d microseconds ..."),TimeValue[i]);\
    1.86 +		value = TimeValue[i];\
    1.87 +		for (j=0; j<MaxTimeMeasurements; ++j)\
    1.88 +			{\
    1.89 +			User::AfterHighRes((Math::Random()&0xf)*1000);\
    1.90 +	
    1.91 +#define __MEASURE1__ tick1 = User::NTickCount();
    1.92 +
    1.93 +#define __MEASURE2__ tick2 = User::NTickCount();
    1.94 +
    1.95 +#define __AFTER_WAIT__ \
    1.96 +			TimeRawMS[j]=(tick2-tick1)*tickPeriod;\
    1.97 +			}\
    1.98 +		calcStats(i);\
    1.99 +		}\
   1.100 +	printStats();\
   1.101 +
   1.102 +GLDEF_C TInt E32Main()
   1.103 +    {
   1.104 +    TInt i,j;
   1.105 +	test.Title();
   1.106 +	test.Start(_L("Timer resolution test"));
   1.107 +	test.SetLogged(ETrue);
   1.108 +	RThread This;
   1.109 +	This.SetPriority(EPriorityRealTime);
   1.110 +	TUint tick1,tick2;
   1.111 +	TInt value, tickPeriod;
   1.112 +	HAL::Get(HAL::ENanoTickPeriod, tickPeriod);
   1.113 +	test.Printf(_L("tickPeriod=%d"),tickPeriod);
   1.114 +///////////////////////////////////////////
   1.115 +	test.Next(_L("Calibrate"));
   1.116 +	MaxTimeMeasurements = KMaxTimeMeasurements;
   1.117 +	TInt TimeValues1[KMaxTimeValues]={0,-1};
   1.118 +	TimeValue = &TimeValues1[0];
   1.119 +	__BEFORE_WAIT__
   1.120 +	__MEASURE1__
   1.121 +	__MEASURE2__
   1.122 +	__AFTER_WAIT__
   1.123 +///////////////////////////////////////////
   1.124 +	test.Next(_L("User::After"));
   1.125 +	TInt TimeValues2[KMaxTimeValues]={10000, 40000,80000,160000,320000,-1};
   1.126 +	TimeValue = &TimeValues2[0];
   1.127 +	__BEFORE_WAIT__
   1.128 +	__MEASURE1__
   1.129 +			User::After(value);
   1.130 +	__MEASURE2__
   1.131 +	__AFTER_WAIT__
   1.132 +#if defined(__EPOC32__)
   1.133 +	//Check that User::After calls completed within boundaries
   1.134 +    TInt k;
   1.135 +	for (k = 0; k<KMaxTimeValues; k++)
   1.136 +		{
   1.137 +		if (TimeValue[k] == -1) break;
   1.138 +		test(TimeValue[k] <= TimeMin[k]);
   1.139 +		TInt aTimerResolution = 1000000/64;
   1.140 +		test((TimeValue[k] + aTimerResolution +2*tickPeriod) >= TimeMax[k]);
   1.141 +		}
   1.142 +#endif
   1.143 +///////////////////////////////////////////
   1.144 +	test.Next(_L("User::At"));
   1.145 +	MaxTimeMeasurements = KMaxTimeMeasurements/2;
   1.146 +	TInt TimeValues3[KMaxTimeValues]={950000,1000000,-1};
   1.147 +	TimeValue = &TimeValues3[0];
   1.148 +	TTime time;
   1.149 +	time.Set(_L("20050101:000001.000000"));
   1.150 +	User::SetHomeTime(time);
   1.151 +	__BEFORE_WAIT__
   1.152 +			time.HomeTime();
   1.153 +			time += (TTimeIntervalMicroSeconds32)value;
   1.154 +	__MEASURE1__
   1.155 +			User::At(time);
   1.156 +	__MEASURE2__
   1.157 +	__AFTER_WAIT__
   1.158 +///////////////////////////////////////////
   1.159 +	test.Next(_L("User::AfterHighRes"));
   1.160 +	MaxTimeMeasurements = KMaxTimeMeasurements;
   1.161 +	TInt TimeValues4[KMaxTimeValues]={1000,2000,4000,8000,16000,32000,64000,128000,-1};
   1.162 +	TimeValue = &TimeValues4[0];
   1.163 +	__BEFORE_WAIT__
   1.164 +	__MEASURE1__
   1.165 +			User::AfterHighRes(value);
   1.166 +	__MEASURE2__
   1.167 +	__AFTER_WAIT__
   1.168 +#if defined(__EPOC32__)
   1.169 +	//Check that User::AfterHighRes calls completed within boundaries
   1.170 +	for (k = 0; k<KMaxTimeValues; k++)
   1.171 +		{
   1.172 +		if (TimeValue[k] == -1) break;
   1.173 +		test(TimeValue[k] <= TimeMin[k]);
   1.174 +		test((TimeValue[k] + 2*tickPeriod) >= TimeMax[k]);
   1.175 +		}
   1.176 +#endif
   1.177 +///////////////////////////////////////////
   1.178 +	test.End();
   1.179 +	return(KErrNone);
   1.180 +	}