sl@0
|
1 |
// Copyright (c) 1996-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 |
// Very simple test of CPU overhead
|
sl@0
|
15 |
//
|
sl@0
|
16 |
//
|
sl@0
|
17 |
|
sl@0
|
18 |
/**
|
sl@0
|
19 |
@file bf_cpu.cpp
|
sl@0
|
20 |
*/
|
sl@0
|
21 |
|
sl@0
|
22 |
#include <e32std.h>
|
sl@0
|
23 |
#include <e32std_private.h>
|
sl@0
|
24 |
#include <e32base.h>
|
sl@0
|
25 |
#include <e32base_private.h>
|
sl@0
|
26 |
#include <e32test.h>
|
sl@0
|
27 |
#include <e32svr.h>
|
sl@0
|
28 |
#include "user_config.h"
|
sl@0
|
29 |
|
sl@0
|
30 |
|
sl@0
|
31 |
#define TEST_WRITE_OVERHEAD
|
sl@0
|
32 |
#define NON_WRITING_LOOPS
|
sl@0
|
33 |
|
sl@0
|
34 |
const TInt KAverageOverInSeconds=10; ///< Number of seconds to run tests for
|
sl@0
|
35 |
|
sl@0
|
36 |
TInt64 Count; ///< Global variable used to count number of operations completed
|
sl@0
|
37 |
RSemaphore CountSem; ///< control access to Count;
|
sl@0
|
38 |
|
sl@0
|
39 |
|
sl@0
|
40 |
RTest test(_L("BF_CPU"));
|
sl@0
|
41 |
|
sl@0
|
42 |
|
sl@0
|
43 |
|
sl@0
|
44 |
TBusLocalDrive drive;
|
sl@0
|
45 |
TLocalDriveCapsV2Buf driveInfo;
|
sl@0
|
46 |
|
sl@0
|
47 |
LOCAL_D TBool StopTest; ///< set to ETrue to stop the test
|
sl@0
|
48 |
|
sl@0
|
49 |
|
sl@0
|
50 |
#ifdef TEST_WRITE_OVERHEAD
|
sl@0
|
51 |
LOCAL_D TBool StopZeroTest;
|
sl@0
|
52 |
|
sl@0
|
53 |
LOCAL_C TInt WriteZeroThread(TAny*)
|
sl@0
|
54 |
/**
|
sl@0
|
55 |
* Performs writes of zero length continuously
|
sl@0
|
56 |
*/
|
sl@0
|
57 |
{
|
sl@0
|
58 |
#if 0
|
sl@0
|
59 |
_LIT( KPanicCat, "ZERWRTH" );
|
sl@0
|
60 |
#endif
|
sl@0
|
61 |
|
sl@0
|
62 |
TBuf8<513> buf;
|
sl@0
|
63 |
buf.SetLength(513);
|
sl@0
|
64 |
|
sl@0
|
65 |
while( !StopZeroTest )
|
sl@0
|
66 |
{
|
sl@0
|
67 |
// Return values are bogus when doing overhead testing
|
sl@0
|
68 |
drive.Write( 513, buf );
|
sl@0
|
69 |
}
|
sl@0
|
70 |
return KErrNone;
|
sl@0
|
71 |
}
|
sl@0
|
72 |
#endif
|
sl@0
|
73 |
|
sl@0
|
74 |
|
sl@0
|
75 |
LOCAL_C TInt WriteThread(TAny*)
|
sl@0
|
76 |
/**
|
sl@0
|
77 |
* Performs writes continuously
|
sl@0
|
78 |
*/
|
sl@0
|
79 |
{
|
sl@0
|
80 |
_LIT( KPanicCat, "WRTHRD" );
|
sl@0
|
81 |
|
sl@0
|
82 |
TBuf8<512> buf;
|
sl@0
|
83 |
buf.SetLength(512);
|
sl@0
|
84 |
buf.Fill(0xFF); // all 0xFF so we can repeatedly overwrite
|
sl@0
|
85 |
|
sl@0
|
86 |
while( !StopTest )
|
sl@0
|
87 |
{
|
sl@0
|
88 |
TInt r = drive.Write( 0, buf );
|
sl@0
|
89 |
if( KErrNone != r )
|
sl@0
|
90 |
{
|
sl@0
|
91 |
User::Panic( KPanicCat, r );
|
sl@0
|
92 |
}
|
sl@0
|
93 |
}
|
sl@0
|
94 |
return KErrNone;
|
sl@0
|
95 |
}
|
sl@0
|
96 |
|
sl@0
|
97 |
|
sl@0
|
98 |
|
sl@0
|
99 |
LOCAL_C TInt CpuThread(TAny*)
|
sl@0
|
100 |
/**
|
sl@0
|
101 |
* Just increments the counter
|
sl@0
|
102 |
*/
|
sl@0
|
103 |
{
|
sl@0
|
104 |
while( !StopTest )
|
sl@0
|
105 |
{
|
sl@0
|
106 |
CountSem.Wait();
|
sl@0
|
107 |
#ifdef NON_WRITING_LOOPS
|
sl@0
|
108 |
for( volatile TInt i = 5000; i > 0; i-- );
|
sl@0
|
109 |
#endif
|
sl@0
|
110 |
++Count;
|
sl@0
|
111 |
CountSem.Signal();
|
sl@0
|
112 |
}
|
sl@0
|
113 |
return KErrNone;
|
sl@0
|
114 |
}
|
sl@0
|
115 |
|
sl@0
|
116 |
|
sl@0
|
117 |
void runTest()
|
sl@0
|
118 |
{
|
sl@0
|
119 |
RThread writeThread;
|
sl@0
|
120 |
TInt r=writeThread.Create(_L("WRITER"),WriteThread,KDefaultStackSize,&User::Heap(),NULL);
|
sl@0
|
121 |
test(r==KErrNone);
|
sl@0
|
122 |
|
sl@0
|
123 |
RThread cpuThread;
|
sl@0
|
124 |
r=cpuThread.Create(_L("CPU-ER"),CpuThread,KDefaultStackSize,&User::Heap(),NULL);
|
sl@0
|
125 |
test(r==KErrNone);
|
sl@0
|
126 |
|
sl@0
|
127 |
#ifdef TEST_WRITE_OVERHEAD
|
sl@0
|
128 |
RThread writeZeroThread;
|
sl@0
|
129 |
r=writeZeroThread.Create(_L("WRITERZERO"),WriteZeroThread,KDefaultStackSize,&User::Heap(),NULL);
|
sl@0
|
130 |
test(r==KErrNone);
|
sl@0
|
131 |
#endif
|
sl@0
|
132 |
|
sl@0
|
133 |
r = CountSem.CreateLocal(1);
|
sl@0
|
134 |
test(r==KErrNone);
|
sl@0
|
135 |
|
sl@0
|
136 |
|
sl@0
|
137 |
StopTest = EFalse; // allow the test to run
|
sl@0
|
138 |
|
sl@0
|
139 |
TRequestStatus deadStatWrite;
|
sl@0
|
140 |
TRequestStatus deadStatCpu;
|
sl@0
|
141 |
writeThread.Logon( deadStatWrite );
|
sl@0
|
142 |
cpuThread.Logon( deadStatCpu );
|
sl@0
|
143 |
|
sl@0
|
144 |
// make writer thread have priority over CPU usage thread
|
sl@0
|
145 |
writeThread.SetPriority( EPriorityMore );
|
sl@0
|
146 |
|
sl@0
|
147 |
// make this thread highest priority
|
sl@0
|
148 |
RThread().SetPriority( EPriorityMuchMore );
|
sl@0
|
149 |
|
sl@0
|
150 |
|
sl@0
|
151 |
cpuThread.Resume();
|
sl@0
|
152 |
|
sl@0
|
153 |
#ifdef TEST_WRITE_OVERHEAD
|
sl@0
|
154 |
TRequestStatus deadStatWriteZero;
|
sl@0
|
155 |
writeZeroThread.Logon( deadStatWriteZero );
|
sl@0
|
156 |
// make writer thread have priority over CPU usage thread
|
sl@0
|
157 |
writeZeroThread.SetPriority( EPriorityMore );
|
sl@0
|
158 |
StopZeroTest = EFalse;
|
sl@0
|
159 |
writeZeroThread.Resume();
|
sl@0
|
160 |
#endif
|
sl@0
|
161 |
|
sl@0
|
162 |
// wait for thread to initialise
|
sl@0
|
163 |
User::After(1000000);
|
sl@0
|
164 |
|
sl@0
|
165 |
CountSem.Wait();
|
sl@0
|
166 |
Count=0;
|
sl@0
|
167 |
CountSem.Signal();
|
sl@0
|
168 |
|
sl@0
|
169 |
User::After(KAverageOverInSeconds*1000000);
|
sl@0
|
170 |
|
sl@0
|
171 |
CountSem.Wait();
|
sl@0
|
172 |
TInt64 noWriteCount( Count ); // number of counts when not writing
|
sl@0
|
173 |
CountSem.Signal();
|
sl@0
|
174 |
|
sl@0
|
175 |
|
sl@0
|
176 |
#ifdef TEST_WRITE_OVERHEAD
|
sl@0
|
177 |
// kill the zero writer
|
sl@0
|
178 |
StopZeroTest = ETrue;
|
sl@0
|
179 |
User::WaitForRequest( deadStatWriteZero );
|
sl@0
|
180 |
CLOSE_AND_WAIT(writeZeroThread);
|
sl@0
|
181 |
#endif
|
sl@0
|
182 |
|
sl@0
|
183 |
test.Printf( _L("Loops without writing = %ld"), noWriteCount );
|
sl@0
|
184 |
|
sl@0
|
185 |
// start write thread
|
sl@0
|
186 |
writeThread.Resume();
|
sl@0
|
187 |
User::After(1000000);
|
sl@0
|
188 |
|
sl@0
|
189 |
CountSem.Wait();
|
sl@0
|
190 |
Count=0;
|
sl@0
|
191 |
CountSem.Signal();
|
sl@0
|
192 |
|
sl@0
|
193 |
User::After(KAverageOverInSeconds*1000000);
|
sl@0
|
194 |
|
sl@0
|
195 |
CountSem.Wait();
|
sl@0
|
196 |
TInt64 withWriteCount( Count ); // number of counts when writing
|
sl@0
|
197 |
CountSem.Signal();
|
sl@0
|
198 |
|
sl@0
|
199 |
test.Printf( _L("Loops while writing = %ld"), withWriteCount );
|
sl@0
|
200 |
|
sl@0
|
201 |
// tell test to stop and wait for thread to exit.
|
sl@0
|
202 |
cpuThread.Kill(KErrNone);
|
sl@0
|
203 |
StopTest = ETrue;
|
sl@0
|
204 |
User::WaitForRequest( deadStatWrite );
|
sl@0
|
205 |
|
sl@0
|
206 |
CLOSE_AND_WAIT(writeThread);
|
sl@0
|
207 |
CLOSE_AND_WAIT(cpuThread);
|
sl@0
|
208 |
|
sl@0
|
209 |
|
sl@0
|
210 |
|
sl@0
|
211 |
TInt64 calc( withWriteCount );
|
sl@0
|
212 |
calc = calc * 100;
|
sl@0
|
213 |
calc = calc / noWriteCount;
|
sl@0
|
214 |
|
sl@0
|
215 |
test.Printf( _L("%% CPU used = %d"), 100 - I64LOW(calc) );
|
sl@0
|
216 |
}
|
sl@0
|
217 |
|
sl@0
|
218 |
|
sl@0
|
219 |
|
sl@0
|
220 |
LOCAL_C TInt EraseSegment( TInt aSegmentNumber )
|
sl@0
|
221 |
/**
|
sl@0
|
222 |
* Erases a segment on Flash
|
sl@0
|
223 |
*
|
sl@0
|
224 |
* @param aSegmentNumber index of segment to erase
|
sl@0
|
225 |
* @return KErrNone or error code
|
sl@0
|
226 |
*/
|
sl@0
|
227 |
{
|
sl@0
|
228 |
TInt offset = aSegmentNumber * driveInfo().iEraseBlockSize;
|
sl@0
|
229 |
|
sl@0
|
230 |
TInt r = drive.Format( offset, driveInfo().iEraseBlockSize );
|
sl@0
|
231 |
test.Printf( _L("erase returns %d"), r );
|
sl@0
|
232 |
return r;
|
sl@0
|
233 |
}
|
sl@0
|
234 |
|
sl@0
|
235 |
|
sl@0
|
236 |
|
sl@0
|
237 |
|
sl@0
|
238 |
void Initialize()
|
sl@0
|
239 |
/**
|
sl@0
|
240 |
* Open channel to media driver
|
sl@0
|
241 |
*/
|
sl@0
|
242 |
{
|
sl@0
|
243 |
//
|
sl@0
|
244 |
// Load the media driver
|
sl@0
|
245 |
//
|
sl@0
|
246 |
#ifndef SKIP_PDD_LOAD
|
sl@0
|
247 |
test.Printf( _L("Loading %S\n"), &KLfsDriverName );
|
sl@0
|
248 |
TInt r = User::LoadPhysicalDevice( KLfsDriverName );
|
sl@0
|
249 |
test( KErrNone == r || KErrAlreadyExists == r );
|
sl@0
|
250 |
#endif
|
sl@0
|
251 |
|
sl@0
|
252 |
#ifdef UNMOUNT_DRIVE
|
sl@0
|
253 |
RFs fs;
|
sl@0
|
254 |
test( KErrNone == fs.Connect() );
|
sl@0
|
255 |
#if 0
|
sl@0
|
256 |
// XXX not EKA2
|
sl@0
|
257 |
test( KErrNone == fs.SetDefaultPath( _L("Z:\\") ) );
|
sl@0
|
258 |
#endif
|
sl@0
|
259 |
TFullName name;
|
sl@0
|
260 |
fs.FileSystemName( name, KLffsLogicalDriveNumber );
|
sl@0
|
261 |
if( name.Length() > 0 )
|
sl@0
|
262 |
{
|
sl@0
|
263 |
test.Printf( _L("Unmounting drive") );
|
sl@0
|
264 |
test( KErrNone == fs.DismountFileSystem( _L("Lffs"), KLffsLogicalDriveNumber) );
|
sl@0
|
265 |
User::After( 2000000 );
|
sl@0
|
266 |
test.Printf( _L("Drive unmounted") );
|
sl@0
|
267 |
}
|
sl@0
|
268 |
fs.Close();
|
sl@0
|
269 |
#endif
|
sl@0
|
270 |
|
sl@0
|
271 |
//
|
sl@0
|
272 |
// Open a TBusLogicalDevice to it
|
sl@0
|
273 |
//
|
sl@0
|
274 |
test.Printf( _L("Opening media channel\n") );
|
sl@0
|
275 |
TBool changedFlag = EFalse;
|
sl@0
|
276 |
test( KErrNone == drive.Connect( KDriveNumber, changedFlag ) );
|
sl@0
|
277 |
|
sl@0
|
278 |
//
|
sl@0
|
279 |
// Get size of Flash drive, block size, block count
|
sl@0
|
280 |
//
|
sl@0
|
281 |
drive.Caps(driveInfo);
|
sl@0
|
282 |
}
|
sl@0
|
283 |
|
sl@0
|
284 |
|
sl@0
|
285 |
|
sl@0
|
286 |
TInt E32Main()
|
sl@0
|
287 |
{
|
sl@0
|
288 |
|
sl@0
|
289 |
test.Title();
|
sl@0
|
290 |
test.Start(_L("Testing CPU overhead"));
|
sl@0
|
291 |
|
sl@0
|
292 |
Initialize();
|
sl@0
|
293 |
|
sl@0
|
294 |
test.Printf( _L("Erasing first segment") );
|
sl@0
|
295 |
TInt r = EraseSegment( 0 );
|
sl@0
|
296 |
test( KErrNone == r );
|
sl@0
|
297 |
test.Printf( _L("Segment erased") );
|
sl@0
|
298 |
|
sl@0
|
299 |
runTest();
|
sl@0
|
300 |
|
sl@0
|
301 |
drive.Disconnect();
|
sl@0
|
302 |
test.End();
|
sl@0
|
303 |
return(KErrNone);
|
sl@0
|
304 |
}
|
sl@0
|
305 |
|