commands/chkdrift/chkdrift.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // chkdrift.cpp
       
     2 // 
       
     3 // Copyright (c) 2008 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include <hal.h>
       
    14 #include <fshell/ioutils.h>
       
    15 
       
    16 using namespace IoUtils;
       
    17 
       
    18 class CCmdChkdrift : public CCommandBase
       
    19 	{
       
    20 public:
       
    21 	static CCommandBase* NewLC();
       
    22 	~CCmdChkdrift();
       
    23 private:
       
    24 	CCmdChkdrift();
       
    25 private: // From CCommandBase.
       
    26 	virtual const TDesC& Name() const;
       
    27 	virtual void DoRunL();
       
    28 	};
       
    29 
       
    30 
       
    31 CCommandBase* CCmdChkdrift::NewLC()
       
    32 	{
       
    33 	CCmdChkdrift* self = new(ELeave) CCmdChkdrift();
       
    34 	CleanupStack::PushL(self);
       
    35 	self->BaseConstructL();
       
    36 	return self;
       
    37 	}
       
    38 
       
    39 CCmdChkdrift::~CCmdChkdrift()
       
    40 	{
       
    41 	}
       
    42 
       
    43 CCmdChkdrift::CCmdChkdrift()
       
    44 	{
       
    45 	}
       
    46 
       
    47 const TDesC& CCmdChkdrift::Name() const
       
    48 	{
       
    49 	_LIT(KName, "chkdrift");	
       
    50 
       
    51 	return KName;
       
    52 	}
       
    53 
       
    54 TInt NanoTickPeriod()
       
    55 	{
       
    56 	TInt nanoTickPeriod;
       
    57 	if (HAL::Get(HAL::ENanoTickPeriod, nanoTickPeriod) != KErrNone)
       
    58 		{
       
    59 		nanoTickPeriod = 1000;
       
    60 		}
       
    61 	return nanoTickPeriod;
       
    62 	}
       
    63 
       
    64 TInt FastCounterFrequency()
       
    65 	{
       
    66 	TInt fastCounterFrequency;
       
    67 	if (HAL::Get(HAL::EFastCounterFrequency, fastCounterFrequency) != KErrNone)
       
    68 		{
       
    69 		fastCounterFrequency = 1000;
       
    70 		}
       
    71 	return fastCounterFrequency;
       
    72 	}
       
    73 
       
    74 TBool FastCounterCountsUp()
       
    75 	{
       
    76 	TBool countsUp;
       
    77 	if (HAL::Get(HAL::EFastCounterCountsUp, countsUp) != KErrNone)
       
    78 		{
       
    79 		countsUp = EFalse;
       
    80 		}
       
    81 	return countsUp;
       
    82 	}
       
    83 
       
    84 void FormatTime(const TTimeIntervalMicroSeconds& aInterval, TDes& aBuf)
       
    85 	{
       
    86 	const TInt ms = 1000;
       
    87 	const TInt s = ms * ms;
       
    88 
       
    89 	TBuf<16> format(_L("%.2f "));
       
    90 
       
    91 	TReal interval = aInterval.Int64();
       
    92 	if (interval >= s)
       
    93 		{
       
    94 		interval /= s;
       
    95 		format.Append(_L("s"));
       
    96 		}
       
    97 	else if (interval >= ms)
       
    98 		{
       
    99 		interval /= ms;
       
   100 		format.Append(_L("ms"));
       
   101 		}
       
   102 	else
       
   103 		{
       
   104 		format.Append(_L("us"));
       
   105 		}
       
   106 
       
   107 	aBuf.Format(format, interval);
       
   108 	}
       
   109 
       
   110 void CCmdChkdrift::DoRunL()
       
   111 	{
       
   112 	TInt nanoTickPeriod = NanoTickPeriod();
       
   113 	Printf(_L("NKern tick period: %d\r\n"), nanoTickPeriod);
       
   114 	TInt fastCounterFrequency = FastCounterFrequency();
       
   115 	Printf(_L("Fast counter frequency: %d\r\n\r\n"), fastCounterFrequency);
       
   116 
       
   117 	Printf(_L("Press any key to start test\r\n"));
       
   118 	TBuf<1> key;
       
   119 	ReadL(key);
       
   120 	TUint fast1 = User::FastCounter();
       
   121 	TUint nano1 = User::NTickCount();
       
   122 
       
   123 	Printf(_L("Press any key to stop test\r\n"));
       
   124 	ReadL(key);
       
   125 	TUint fast2 = User::FastCounter();
       
   126 	TUint nano2 = User::NTickCount();
       
   127 
       
   128 	Printf(_L("before: nano: %u, fast: %u\r\n"), nano1, fast1);
       
   129 	Printf(_L("after: nano: %u, fast: %u\r\n"), nano2, fast2);
       
   130 	TUint64 diffNano = nano2 - nano1;
       
   131 	diffNano *= NanoTickPeriod();
       
   132 	TUint64 diffFast;
       
   133 	
       
   134 	if (FastCounterCountsUp())
       
   135 		{
       
   136 		diffFast = fast2 - fast1;
       
   137 		}
       
   138 	else
       
   139 		{
       
   140 		diffFast = fast1 - fast2;
       
   141 		}
       
   142 
       
   143 	TUint64 fastCounterPeriod = 1000000 / FastCounterFrequency();
       
   144 	diffFast *= fastCounterPeriod;
       
   145 	TBuf<32> timeNano;
       
   146 	FormatTime(diffNano, timeNano);
       
   147 	TBuf<32> timeFast;
       
   148 	FormatTime(diffFast, timeFast);
       
   149 	Printf(_L("difference:\r\n\tnano: %u ticks (%Lu us, %S)\r\n\tfast: %u ticks (%Lu us, %S)\r\n"), nano2 - nano1, diffNano, &timeNano, fast2 - fast1, diffFast, &timeFast);
       
   150 	}
       
   151 
       
   152 
       
   153 EXE_BOILER_PLATE(CCmdChkdrift)
       
   154