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