kerneltest/e32test/benchmark/thread.cpp
changeset 0 a41df078684a
child 39 2bb754abd467
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2002-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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <e32test.h>
       
    17 #include <e32svr.h>
       
    18 
       
    19 #include "bm_suite.h"
       
    20 
       
    21 class Thread : public BMProgram
       
    22 	{
       
    23 public :
       
    24 	Thread() : BMProgram(_L("Threads"))
       
    25 		{}
       
    26 	virtual TBMResult* Run(TBMUInt64 aIter, TInt* aCount);
       
    27 
       
    28 	typedef void (*MeasurementFunc)(TBMResult*, TBMUInt64 aIter);
       
    29 	struct Measurement 
       
    30 		{
       
    31 		MeasurementFunc iFunc;
       
    32 		TPtrC			iName;
       
    33 
       
    34 		Measurement(MeasurementFunc aFunc, const TDesC& aName) : 
       
    35 					iFunc(aFunc), iName(aName) {}
       
    36 		};
       
    37 
       
    38 	static TBMResult iResults[];
       
    39 	static Measurement iMeasurements[];
       
    40 
       
    41 	static TBMTicks	iChildTime;
       
    42 
       
    43 	static void Creation(TBMResult*, TBMUInt64 aIter);
       
    44 	static TInt CreationChild(TAny*);
       
    45 	static void CreationSuicide(TBMResult*, TBMUInt64 aIter);
       
    46 	static TInt CreationSuicideChild(TAny*);
       
    47 	static void Suicide(TBMResult*, TBMUInt64 aIter);
       
    48 	static TInt SuicideChild(TAny*);
       
    49 	static void Killing(TBMResult*, TBMUInt64 aIter);
       
    50 	static TInt KillingChild(TAny*);
       
    51 	static void SetTls(TBMResult*, TBMUInt64 aIter);
       
    52 	static void GetTls(TBMResult*, TBMUInt64 aIter);
       
    53 
       
    54 	void EnableCleanup()
       
    55 		{
       
    56 		TInt prio = BMProgram::SetAbsPriority(RThread(), iOrigAbsPriority);
       
    57 		BMProgram::SetAbsPriority(RThread(), prio);
       
    58 		}
       
    59 	};
       
    60 
       
    61 Thread::Measurement Thread::iMeasurements[] =
       
    62 	{
       
    63 	Measurement(&Thread::Creation, _L("Thread Creation Latency")),
       
    64 	Measurement(&Thread::CreationSuicide, _L("Thread Creation Suicide")),
       
    65 	Measurement(&Thread::Suicide, _L("Thread Suicide")),
       
    66 	Measurement(&Thread::Killing, _L("Thread Killing")),
       
    67 	Measurement(&Thread::SetTls, _L("Setting per-thread data")),
       
    68 	Measurement(&Thread::GetTls, _L("Getting per-thread data"))
       
    69 	};
       
    70 TBMResult Thread::iResults[sizeof(Thread::iMeasurements)/sizeof(Thread::iMeasurements[0])];
       
    71 
       
    72 TBMTicks Thread::iChildTime;
       
    73 
       
    74 static Thread prog;
       
    75 
       
    76 void Thread::Creation(TBMResult* aResult, TBMUInt64 aIter)
       
    77 	{
       
    78 	for (TBMUInt64 i = 0; i < aIter; ++i)
       
    79 		{
       
    80 		RThread child;
       
    81 		TRequestStatus st;
       
    82 		TBMTicks t1;
       
    83 		::bmTimer.Stamp(&t1);
       
    84 		TInt r = child.Create(KNullDesC, Thread::CreationChild, 0x2000, NULL, NULL);
       
    85 		BM_ERROR(r, r == KErrNone);
       
    86 		child.Logon(st);
       
    87 		BMProgram::SetAbsPriority(RThread(), KBMPriorityHigh);
       
    88 		child.Resume();
       
    89 		User::WaitForRequest(st);
       
    90 		BM_ERROR(st.Int(), st == KErrNone);
       
    91 		aResult->Cumulate(TBMTicksDelta(t1, iChildTime));
       
    92 		CLOSE_AND_WAIT(child);
       
    93 		prog.EnableCleanup();
       
    94 		}
       
    95 	}
       
    96 
       
    97 TInt Thread::CreationChild(TAny*)
       
    98 	{
       
    99 	::bmTimer.Stamp(&iChildTime);
       
   100 	return KErrNone;
       
   101 	}
       
   102 
       
   103 void Thread::CreationSuicide(TBMResult* aResult, TBMUInt64 aIter)
       
   104 	{
       
   105 	for (TBMUInt64 i = 0; i < aIter; ++i)
       
   106 		{
       
   107 		RThread child;
       
   108 		TRequestStatus st;
       
   109 		TBMTicks t1;
       
   110 		::bmTimer.Stamp(&t1);
       
   111 		TInt r = child.Create(KNullDesC, Thread::CreationSuicideChild, 0x2000, NULL, NULL);
       
   112 		BM_ERROR(r, r == KErrNone);
       
   113 		child.Logon(st);
       
   114 		BMProgram::SetAbsPriority(RThread(), KBMPriorityLow);
       
   115 		child.Resume();
       
   116 		User::WaitForRequest(st);
       
   117 		BM_ERROR(st.Int(), st == KErrNone);
       
   118 		TBMTicks t2;
       
   119 		::bmTimer.Stamp(&t2);
       
   120 		aResult->Cumulate(TBMTicksDelta(t1, t2));
       
   121 		CLOSE_AND_WAIT(child);
       
   122 		prog.EnableCleanup();
       
   123 		}
       
   124 	}
       
   125 
       
   126 TInt Thread::CreationSuicideChild(TAny*)
       
   127 	{
       
   128 	return KErrNone;
       
   129 	}
       
   130 						
       
   131 void Thread::Suicide(TBMResult* aResult, TBMUInt64 aIter)
       
   132 	{
       
   133 	for (TBMUInt64 i = 0; i < aIter; ++i)
       
   134 		{
       
   135 		RThread child;
       
   136 		TRequestStatus st;
       
   137 		TInt r = child.Create(KNullDesC, Thread::SuicideChild, 0x2000, NULL, NULL);
       
   138 		BM_ERROR(r, r == KErrNone);
       
   139 		child.Logon(st);
       
   140 		BMProgram::SetAbsPriority(RThread(), KBMPriorityLow);
       
   141 		child.Resume();
       
   142 		User::WaitForRequest(st);
       
   143 		BM_ERROR(st.Int(), st == KErrNone);
       
   144 		TBMTicks t2;
       
   145 		::bmTimer.Stamp(&t2);
       
   146 		aResult->Cumulate(TBMTicksDelta(iChildTime, t2));
       
   147 		CLOSE_AND_WAIT(child);
       
   148 		prog.EnableCleanup();
       
   149 		}
       
   150 	}
       
   151 
       
   152 TInt Thread::SuicideChild(TAny*)
       
   153 	{
       
   154 	::bmTimer.Stamp(&iChildTime);
       
   155 	return KErrNone;
       
   156 	}
       
   157 
       
   158 void Thread::Killing(TBMResult* aResult, TBMUInt64 aIter)
       
   159 	{
       
   160 	for (TBMUInt64 i = 0; i < aIter; ++i)
       
   161 		{
       
   162 		RThread child;
       
   163 		TRequestStatus st;
       
   164 		TInt r = child.Create(KNullDesC, Thread::KillingChild, 0x2000, NULL, NULL);
       
   165 		BM_ERROR(r, r == KErrNone);
       
   166 		child.Logon(st);
       
   167 		BMProgram::SetAbsPriority(RThread(), KBMPriorityHigh);
       
   168 		child.Resume();
       
   169 		TBMTicks t1;
       
   170 		::bmTimer.Stamp(&t1);
       
   171 		child.Kill(KErrCancel);
       
   172 		User::WaitForRequest(st);
       
   173 		BM_ERROR(st.Int(), st == KErrCancel);
       
   174 		TBMTicks t2;
       
   175 		::bmTimer.Stamp(&t2);
       
   176 		aResult->Cumulate(TBMTicksDelta(t1, t2));
       
   177 		CLOSE_AND_WAIT(child);
       
   178 		prog.EnableCleanup();
       
   179 		}
       
   180 	}
       
   181 
       
   182 TInt Thread::KillingChild(TAny*)
       
   183 	{
       
   184 	User::WaitForAnyRequest();
       
   185 	return KErrNone;
       
   186 	}
       
   187 
       
   188 #define TLS_KEY ((TInt32) &Thread::SetTls)
       
   189 
       
   190 void Thread::SetTls(TBMResult* aResult, TBMUInt64 aIter)
       
   191 	{
       
   192 	aIter <<= 4;
       
   193 
       
   194 	TBMTimeInterval ti;
       
   195 	ti.Begin();
       
   196 	for (TBMUInt64 i = 0; i < aIter; ++i)
       
   197 		{
       
   198 		TInt r = UserSvr::DllSetTls(TLS_KEY, 0);
       
   199 		BM_ERROR(r, r == KErrNone);
       
   200 		}
       
   201 	TBMTicks t = ti.End();
       
   202 	aResult->Cumulate(t, aIter);
       
   203 
       
   204 	UserSvr::DllFreeTls(TLS_KEY);
       
   205 	}
       
   206 
       
   207 void Thread::GetTls(TBMResult* aResult, TBMUInt64 aIter)
       
   208 	{
       
   209 	aIter <<= 4;
       
   210 
       
   211 	TInt r = UserSvr::DllSetTls(TLS_KEY, 0);
       
   212 	BM_ERROR(r, r == KErrNone);
       
   213 
       
   214 	TBMTimeInterval ti;
       
   215 	ti.Begin();
       
   216 	for (TBMUInt64 i = 0; i < aIter; ++i)
       
   217 		{
       
   218 		UserSvr::DllTls(TLS_KEY);
       
   219 		}
       
   220 	TBMTicks t = ti.End();
       
   221 	aResult->Cumulate(t, aIter);
       
   222 	
       
   223 	UserSvr::DllFreeTls(TLS_KEY);
       
   224 	}
       
   225 
       
   226 TBMResult* Thread::Run(TBMUInt64 aIter, TInt* aCount)
       
   227 	{
       
   228 	TInt count = sizeof(iResults)/sizeof(iResults[0]);
       
   229 
       
   230 	for (TInt i = 0; i < count; ++i)
       
   231 		{
       
   232 		iResults[i].Reset(iMeasurements[i].iName);
       
   233 		iMeasurements[i].iFunc(&iResults[i], aIter);
       
   234 		iResults[i].Update();
       
   235 		}
       
   236 	
       
   237 	*aCount = count;
       
   238 	return iResults;
       
   239 	}
       
   240 
       
   241 void AddThread()
       
   242 	{
       
   243 	BMProgram* next = bmSuite;
       
   244 	bmSuite=(BMProgram*)&prog;
       
   245 	bmSuite->Next()=next;
       
   246 	}