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_ctxsw.cpp
|
sl@0
|
15 |
// Overview:
|
sl@0
|
16 |
// Test and benchmark inter-thread context switch.
|
sl@0
|
17 |
// API Information:
|
sl@0
|
18 |
// RThread, RSemaphore, TRequestStatus.
|
sl@0
|
19 |
// Details:
|
sl@0
|
20 |
// - Creates a thread with a handle to a Semaphore, check for successful
|
sl@0
|
21 |
// creation and assign priority EPriorityLess.
|
sl@0
|
22 |
// - Creates another thread with the handle to the thread already created,
|
sl@0
|
23 |
// check for successful creation and assign priority EPriorityMuchLess.
|
sl@0
|
24 |
// - Makes the two threads eligible for execution.
|
sl@0
|
25 |
// - First thread counts the number of semaphore requests.
|
sl@0
|
26 |
// - Second thread counts the number of times the thread is signalled on
|
sl@0
|
27 |
// completion of request.
|
sl@0
|
28 |
// - Terminate the threads, print the count per second.
|
sl@0
|
29 |
// Platforms/Drives/Compatibility:
|
sl@0
|
30 |
// All.
|
sl@0
|
31 |
// Assumptions/Requirement/Pre-requisites:
|
sl@0
|
32 |
// Failures and causes:
|
sl@0
|
33 |
// Base Port information:
|
sl@0
|
34 |
//
|
sl@0
|
35 |
//
|
sl@0
|
36 |
|
sl@0
|
37 |
#define __E32TEST_EXTENSION__
|
sl@0
|
38 |
#include <e32test.h>
|
sl@0
|
39 |
|
sl@0
|
40 |
RTest test(_L("T_CTXSW"));
|
sl@0
|
41 |
|
sl@0
|
42 |
volatile TInt Count1;
|
sl@0
|
43 |
volatile TInt Count2;
|
sl@0
|
44 |
|
sl@0
|
45 |
TInt Thread1(TAny* aSemaphore)
|
sl@0
|
46 |
{
|
sl@0
|
47 |
RSemaphore s;
|
sl@0
|
48 |
s.SetHandle((TInt)aSemaphore);
|
sl@0
|
49 |
User::WaitForAnyRequest();
|
sl@0
|
50 |
s.Signal();
|
sl@0
|
51 |
FOREVER
|
sl@0
|
52 |
{
|
sl@0
|
53 |
User::WaitForAnyRequest();
|
sl@0
|
54 |
Count1++;
|
sl@0
|
55 |
}
|
sl@0
|
56 |
}
|
sl@0
|
57 |
|
sl@0
|
58 |
TInt Thread2(TAny* aThread)
|
sl@0
|
59 |
{
|
sl@0
|
60 |
TRequestStatus s;
|
sl@0
|
61 |
RThread t;
|
sl@0
|
62 |
t.SetHandle((TInt)aThread);
|
sl@0
|
63 |
FOREVER
|
sl@0
|
64 |
{
|
sl@0
|
65 |
Count2++;
|
sl@0
|
66 |
TRequestStatus* ps=&s;
|
sl@0
|
67 |
t.RequestComplete(ps,0);
|
sl@0
|
68 |
}
|
sl@0
|
69 |
}
|
sl@0
|
70 |
|
sl@0
|
71 |
const TInt KHeapSize=4096;
|
sl@0
|
72 |
void Test1()
|
sl@0
|
73 |
{
|
sl@0
|
74 |
test.Start(_L("Test 1"));
|
sl@0
|
75 |
|
sl@0
|
76 |
RSemaphore sem;
|
sl@0
|
77 |
TInt r = sem.CreateLocal(0);
|
sl@0
|
78 |
test_KErrNone(r);
|
sl@0
|
79 |
RThread t1,t2;
|
sl@0
|
80 |
RThread* pt1, *pt2;
|
sl@0
|
81 |
|
sl@0
|
82 |
pt1 = NULL;
|
sl@0
|
83 |
pt2 = NULL;
|
sl@0
|
84 |
|
sl@0
|
85 |
Count1=Count2=0;
|
sl@0
|
86 |
r=t1.Create(_L("Thread1"),Thread1,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)sem.Handle());
|
sl@0
|
87 |
if (!pt1)
|
sl@0
|
88 |
pt1=&t1;
|
sl@0
|
89 |
else
|
sl@0
|
90 |
test(pt1==&t1);
|
sl@0
|
91 |
|
sl@0
|
92 |
test_KErrNone(r);
|
sl@0
|
93 |
t1.SetPriority(EPriorityLess);
|
sl@0
|
94 |
r=t2.Create(_L("Thread2"),Thread2,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)t1.Handle());
|
sl@0
|
95 |
if (!pt2)
|
sl@0
|
96 |
pt2=&t2;
|
sl@0
|
97 |
else
|
sl@0
|
98 |
test(pt2==&t2);
|
sl@0
|
99 |
test_KErrNone(r);
|
sl@0
|
100 |
t2.SetPriority(EPriorityMuchLess);
|
sl@0
|
101 |
|
sl@0
|
102 |
TRequestStatus s1,s2;
|
sl@0
|
103 |
t1.Logon(s1);
|
sl@0
|
104 |
t2.Logon(s2);
|
sl@0
|
105 |
t1.Resume();
|
sl@0
|
106 |
t2.Resume();
|
sl@0
|
107 |
sem.Wait();
|
sl@0
|
108 |
Count1=Count2=0;
|
sl@0
|
109 |
User::After(16000000);
|
sl@0
|
110 |
t2.Kill(0);
|
sl@0
|
111 |
User::WaitForRequest(s2);
|
sl@0
|
112 |
t1.Kill(0);
|
sl@0
|
113 |
User::WaitForRequest(s1);
|
sl@0
|
114 |
|
sl@0
|
115 |
t2.Close();
|
sl@0
|
116 |
t1.Close();
|
sl@0
|
117 |
test.Printf(_L("Ran %d times in one second.\n"),(Count1+Count2)/16); // get the value
|
sl@0
|
118 |
sem.Close();
|
sl@0
|
119 |
|
sl@0
|
120 |
test.End();
|
sl@0
|
121 |
}
|
sl@0
|
122 |
|
sl@0
|
123 |
struct SThread3Info
|
sl@0
|
124 |
{
|
sl@0
|
125 |
TRequestStatus iStatus;
|
sl@0
|
126 |
TRequestStatus iLogonStatus;
|
sl@0
|
127 |
RThread iThread;
|
sl@0
|
128 |
volatile TUint32 iCount;
|
sl@0
|
129 |
SThread3Info* iOther;
|
sl@0
|
130 |
TUint32 iPadding[9];
|
sl@0
|
131 |
};
|
sl@0
|
132 |
|
sl@0
|
133 |
void Thread3(TAny* aInfo)
|
sl@0
|
134 |
{
|
sl@0
|
135 |
SThread3Info& me = *(SThread3Info*)aInfo;
|
sl@0
|
136 |
SThread3Info& you = *me.iOther;
|
sl@0
|
137 |
FOREVER
|
sl@0
|
138 |
{
|
sl@0
|
139 |
TRequestStatus* ps = &you.iStatus;
|
sl@0
|
140 |
you.iThread.RequestComplete(ps, 0);
|
sl@0
|
141 |
User::WaitForAnyRequest();
|
sl@0
|
142 |
++me.iCount;
|
sl@0
|
143 |
Count2++;
|
sl@0
|
144 |
}
|
sl@0
|
145 |
}
|
sl@0
|
146 |
|
sl@0
|
147 |
void Test2()
|
sl@0
|
148 |
{
|
sl@0
|
149 |
test.Start(_L("Test 2"));
|
sl@0
|
150 |
|
sl@0
|
151 |
SThread3Info info[2];
|
sl@0
|
152 |
TInt r;
|
sl@0
|
153 |
TInt i;
|
sl@0
|
154 |
for (i=0; i<2; ++i)
|
sl@0
|
155 |
{
|
sl@0
|
156 |
info[i].iCount = 0;
|
sl@0
|
157 |
info[i].iOther = &info[1-i];
|
sl@0
|
158 |
r = info[i].iThread.Create(KNullDesC, (TThreadFunction)&Thread3, 0x1000, 0, &info[i]);
|
sl@0
|
159 |
test_KErrNone(r);
|
sl@0
|
160 |
info[i].iThread.Logon(info[i].iLogonStatus);
|
sl@0
|
161 |
test_Equal(KRequestPending, info[i].iLogonStatus.Int());
|
sl@0
|
162 |
}
|
sl@0
|
163 |
info[0].iThread.SetPriority(EPriorityMuchLess);
|
sl@0
|
164 |
info[1].iThread.SetPriority(EPriorityLess);
|
sl@0
|
165 |
info[0].iThread.Resume();
|
sl@0
|
166 |
info[1].iThread.Resume();
|
sl@0
|
167 |
User::AfterHighRes(100000);
|
sl@0
|
168 |
TUint32 ic0 = info[0].iCount;
|
sl@0
|
169 |
TUint32 ic1 = info[1].iCount;
|
sl@0
|
170 |
User::AfterHighRes(10000000);
|
sl@0
|
171 |
info[0].iThread.Kill(0);
|
sl@0
|
172 |
info[1].iThread.Kill(0);
|
sl@0
|
173 |
User::WaitForRequest(info[0].iLogonStatus);
|
sl@0
|
174 |
User::WaitForRequest(info[1].iLogonStatus);
|
sl@0
|
175 |
test_Equal(EExitKill, info[0].iThread.ExitType());
|
sl@0
|
176 |
test_Equal(0, info[0].iThread.ExitReason());
|
sl@0
|
177 |
test_Equal(EExitKill, info[1].iThread.ExitType());
|
sl@0
|
178 |
test_Equal(0, info[1].iThread.ExitReason());
|
sl@0
|
179 |
|
sl@0
|
180 |
TUint32 fc0 = info[0].iCount;
|
sl@0
|
181 |
TUint32 fc1 = info[1].iCount;
|
sl@0
|
182 |
test.Printf(_L("T0: Initial %10u Final %10u Diff %10u PerSec %10u\n"), ic0, fc0, (fc0-ic0), (fc0-ic0)/10);
|
sl@0
|
183 |
test.Printf(_L("T1: Initial %10u Final %10u Diff %10u PerSec %10u\n"), ic1, fc1, (fc1-ic1), (fc1-ic1)/10);
|
sl@0
|
184 |
|
sl@0
|
185 |
CLOSE_AND_WAIT(info[0].iThread);
|
sl@0
|
186 |
CLOSE_AND_WAIT(info[1].iThread);
|
sl@0
|
187 |
|
sl@0
|
188 |
test.End();
|
sl@0
|
189 |
}
|
sl@0
|
190 |
|
sl@0
|
191 |
TInt E32Main()
|
sl@0
|
192 |
{
|
sl@0
|
193 |
|
sl@0
|
194 |
test.Title();
|
sl@0
|
195 |
test.Start(_L("Timing inter-thread context switches..."));
|
sl@0
|
196 |
|
sl@0
|
197 |
Test1();
|
sl@0
|
198 |
Test2();
|
sl@0
|
199 |
|
sl@0
|
200 |
test.End();
|
sl@0
|
201 |
return 0;
|
sl@0
|
202 |
}
|