os/kernelhwsrv/kerneltest/e32test/bench/t_ctxsw3.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1997-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
// e32test\bench\t_ctxsw3.cpp
sl@0
    15
// Overview:
sl@0
    16
// Benchmark the inter-thread context switching. 
sl@0
    17
// API Information:
sl@0
    18
// RThread, RRSemaphore.
sl@0
    19
// Details:
sl@0
    20
// - Create a semaphore.
sl@0
    21
// - Create a thread (thread1) with a handle to the Semaphore and 
sl@0
    22
// priority EPriorityLess. Verify the successful creation.
sl@0
    23
// - Create another thread (thread2) with a handle to the Semaphore 
sl@0
    24
// and priority EPriorityMuchLess. Verify the successful creation.
sl@0
    25
// - thread1 counts the number of times the semaphore count is decremented.
sl@0
    26
// - thread2 counts the number of times the semaphore count is incremented.
sl@0
    27
// - Display the number of times the semaphore is signalled per second.
sl@0
    28
// Platforms/Drives/Compatibility:
sl@0
    29
// All.
sl@0
    30
// Assumptions/Requirement/Pre-requisites:
sl@0
    31
// Failures and causes:
sl@0
    32
// Base Port information:
sl@0
    33
// 
sl@0
    34
//
sl@0
    35
sl@0
    36
#include <e32test.h>
sl@0
    37
sl@0
    38
RTest test(_L("T_CTXSW3"));
sl@0
    39
sl@0
    40
LOCAL_D TInt Count;
sl@0
    41
sl@0
    42
LOCAL_D RSemaphore StartSem;
sl@0
    43
sl@0
    44
LOCAL_C TInt Thread1(TAny* aSemaphore)
sl@0
    45
	{
sl@0
    46
	RSemaphore s;
sl@0
    47
	s.SetHandle((TInt)aSemaphore);
sl@0
    48
	StartSem.Wait();
sl@0
    49
	FOREVER
sl@0
    50
		{
sl@0
    51
		s.Wait();
sl@0
    52
		Count++;
sl@0
    53
		}
sl@0
    54
	}
sl@0
    55
sl@0
    56
LOCAL_C TInt Thread2(TAny* aSemaphore)
sl@0
    57
	{
sl@0
    58
	RSemaphore s;
sl@0
    59
	s.SetHandle((TInt)aSemaphore);
sl@0
    60
	StartSem.Wait();
sl@0
    61
	FOREVER
sl@0
    62
		{
sl@0
    63
		s.Signal();
sl@0
    64
		Count++;
sl@0
    65
		}
sl@0
    66
	}
sl@0
    67
sl@0
    68
const TInt KHeapSize=4096;
sl@0
    69
sl@0
    70
GLDEF_C TInt E32Main()
sl@0
    71
	{
sl@0
    72
	test.Title();
sl@0
    73
	test.Start(_L("Timing inter-thread context switches..."));
sl@0
    74
sl@0
    75
	TInt r=StartSem.CreateLocal(0);
sl@0
    76
	test(r==KErrNone);
sl@0
    77
sl@0
    78
	RSemaphore sem;
sl@0
    79
	r=sem.CreateLocal(0);
sl@0
    80
	test(r==KErrNone);
sl@0
    81
	RThread t1,t2;
sl@0
    82
	r=t1.Create(_L("Thread1"),Thread1,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)sem.Handle());
sl@0
    83
	test(r==KErrNone);
sl@0
    84
	t1.SetPriority(EPriorityLess);
sl@0
    85
	r=t2.Create(_L("Thread2"),Thread2,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)sem.Handle());
sl@0
    86
	test(r==KErrNone);
sl@0
    87
	t2.SetPriority(EPriorityMuchLess);
sl@0
    88
sl@0
    89
	TRequestStatus s1,s2;
sl@0
    90
	t1.Logon(s1);
sl@0
    91
	t2.Logon(s2);
sl@0
    92
	t1.Resume();
sl@0
    93
	t2.Resume();
sl@0
    94
sl@0
    95
	Count=0;
sl@0
    96
	User::After(20000);
sl@0
    97
	StartSem.Signal(2);
sl@0
    98
	User::After(2000000);
sl@0
    99
	TInt count=Count;
sl@0
   100
sl@0
   101
	t2.Kill(0);
sl@0
   102
	User::WaitForRequest(s2);
sl@0
   103
	t1.Kill(0);
sl@0
   104
	User::WaitForRequest(s1);
sl@0
   105
	t2.Close();
sl@0
   106
	t1.Close();
sl@0
   107
	sem.Close();
sl@0
   108
sl@0
   109
	test.Printf(_L("%d per second\n"),count/2);
sl@0
   110
sl@0
   111
	test.End();
sl@0
   112
	return 0;
sl@0
   113
	}