First public contribution.
1 // Copyright (c) 2005-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\bench\d_kernasmfnc.cpp
19 #include <e32cmn_private.h>
20 #include <kernel/kern_priv.h>
22 #include "d_kernasmbm.h"
24 TUint8* UserPtr; // pointer to user-side buffer
25 TUint8* KernelPtr; // pointer to kernel-side buffer
27 NFastMutex* FastMutex;
28 NFastSemaphore* FastSem;
36 TDfc Dfc(DfcFunc, NULL, Kern::DfcQue0(), 3);
38 TPriList<TPriListLink, 64> PriList;
40 _LIT(KObjectName, "This is a valid object name.");
42 TBitMapAllocator* Bma;
44 const TUint8* CharData = (const TUint8*) "Here's some char data to compare with memicmp";
46 // Allocate a 32-byte aligned block
47 TAny* AllocAligned(TUint aSize)
49 TUint mem = (TUint)Kern::Alloc(aSize + 32 + 4);
52 TUint* ptr = (TUint*) ALIGN_ADDR(mem + 4);
57 // Free a block allocated by AllocAligned
58 void FreeAligned(TAny* ptr)
60 Kern::Free((TAny*)((TUint*)ptr)[-1]);
63 TBitMapAllocator* AllocAlignedBMA(TInt aSize, TBool aState)
65 TInt nmapw=(aSize+31)>>5;
66 TInt memsz=sizeof(TBitMapAllocator)+(nmapw-1)*sizeof(TUint32);
67 TBitMapAllocator* pA=(TBitMapAllocator*)AllocAligned(memsz);
69 new(pA) TBitMapAllocator(aSize, aState);
73 TInt ThreadFunc(TAny* aPtr)
80 KernelPtr = (TUint8*)AllocAligned(KKernAsmBmBufferSize);
84 Bma = AllocAlignedBMA(256, ETrue);
88 FastSem = (NFastSemaphore*) AllocAligned(sizeof(NFastSemaphore));
91 new (FastSem) NFastSemaphore;
92 NKern::FSSetOwner(FastSem, NULL);
94 FastMutex = (NFastMutex*) AllocAligned(sizeof(FastMutex));
97 new (FastMutex) NFastMutex;
99 SThreadCreateInfo info;
100 info.iType=EThreadSupervisor;
101 info.iFunction=ThreadFunc;
103 info.iSupervisorStack=NULL;
104 info.iSupervisorStackSize=0; // zero means use default value
105 info.iInitialThreadPriority=NKern::CurrentThread()->iPriority;
106 info.iName.Set(_L("bmthread2"));
107 info.iTotalSize = sizeof(info);
109 NKern::ThreadEnterCS();
110 TInt r = Kern::ThreadCreate(info);
111 NKern::ThreadLeaveCS();
115 Thread = (DThread*)info.iHandle;
122 FreeAligned(KernelPtr); KernelPtr = NULL;
123 FreeAligned(Bma); Bma = NULL;
124 FreeAligned(FastSem); FastSem = NULL;
125 FreeAligned(FastMutex); FastMutex = NULL;
126 Kern::ThreadResume(*Thread); Thread = NULL; // thread will now exit
129 // 1. Functions that have C++ equivalents
135 DEFINE_BENCHMARK(Dfc_DoEnqueCancel,
137 Dfc.DoEnque(); Dfc.Cancel(),
140 DEFINE_BENCHMARK(Dfc_AddCancel,
142 Dfc.Add(); Dfc.Cancel(),
145 // Not exported: TDfc::DoEnqueFinal, TDfc::ThreadFunction
149 DEFINE_BENCHMARK(NKern_LockUnlockSystem,
151 NKern::LockSystem(); NKern::UnlockSystem(),
154 DEFINE_BENCHMARK(NFastMutex_WaitSignal,
156 FastMutex->Wait(); FastMutex->Signal(),
157 NKern::Unlock()); // no contention
159 DEFINE_BENCHMARK(NKern_FMWaitSignal,
161 NKern::FMWait(FastMutex); NKern::FMSignal(FastMutex),
164 // Benchmark that exercises contention for fast mutex
165 DEFINE_THREADED_BENCHMARK(NFastMutex_ThreadSwitch,
167 Kern::ThreadResume(*iThread2),
168 NKern::FSWait(FastSem); NKern::FMWait(FastMutex); NKern::FMSignal(FastMutex),
169 NKern::FMWait(FastMutex); NKern::FSSignal(FastSem); NKern::FMSignal(FastMutex),
172 // Benchmark thread switching with suspend/resume for comparison
173 DEFINE_THREADED_BENCHMARK(Kern_ThreadSwitch,
176 Kern::ThreadResume(*iThread2),
177 Kern::ThreadSuspend(*iThread2, 1),
178 Kern::ThreadResume(*iThread2));
180 // 1.1.3 Fast semaphore
182 DEFINE_BENCHMARK(NFastSem_Wait,
183 NKern::Lock(); FastSem->SignalN(aParams.iIts * 10),
187 DEFINE_BENCHMARK(NFastSem_Signal,
190 FastSem->Reset(); NKern::Unlock());
192 DEFINE_BENCHMARK(NFastSem_SignalN,
195 FastSem->Reset(); NKern::Unlock());
197 DEFINE_BENCHMARK(NFastSem_Reset,
202 // NFastSemaphore::WaitCancel not exported
204 DEFINE_THREADED_BENCHMARK(NFastSem_ThreadSwitch,
206 Kern::ThreadResume(*iThread2),
207 NKern::FSWait(FastSem),
208 NKern::FSSignal(FastSem),
211 DEFINE_BENCHMARK(NKern_WaitForAnyReq,
212 NKern::ThreadRequestSignal(NULL, aParams.iIts * 10),
213 NKern::WaitForAnyRequest(),
216 DEFINE_BENCHMARK(NKern_ThreadReqSignal,
217 NThread* t = &Thread->iNThread; NKern::Lock(),
218 NKern::ThreadRequestSignal(t),
219 t->iRequestSemaphore.Reset();NKern::Unlock());
221 DEFINE_BENCHMARK(NKern_ThreadReqSignal2,
222 NThread* t = &Thread->iNThread,
223 NKern::FMWait(FastMutex); NKern::ThreadRequestSignal(t, FastMutex),
224 NKern::Lock(); t->iRequestSemaphore.Reset(); NKern::Unlock(););
228 DEFINE_BENCHMARK(NTimer_OneShot,
230 timer.OneShot(100); timer.Cancel(),
233 DEFINE_BENCHMARK(NTimer_Again,
235 timer.Again(100); timer.Cancel(),
246 // 1.1.5 Priority list
248 DEFINE_BENCHMARK(TPriList_AddRemove,
249 TPriListLink link(15),
250 PriList.Add(&link); PriList.Remove(&link),
253 DEFINE_BENCHMARK(TPriList_AddRemove2,
254 TPriListLink link(15); TPriListLink link2(15); PriList.Add(&link2),
255 PriList.Add(&link); PriList.Remove(&link),
256 PriList.Remove(&link2));
258 DEFINE_BENCHMARK(TPriList_First,
259 TPriListLink link(15); PriList.Add(&link),
261 PriList.Remove(&link));
263 DEFINE_BENCHMARK(TPriList_HighestPri,
264 TPriListLink link(15); PriList.Add(&link),
266 PriList.Remove(&link));
268 DEFINE_BENCHMARK(TPriList_ChangePri,
269 TPriListLink link(15); PriList.Add(&link); TInt pri = 15,
270 pri ^= 32; PriList.ChangePriority(&link, pri),
271 PriList.Remove(&link));
275 DEFINE_BENCHMARK(Kern_ValidateFullName,
277 Kern::ValidateFullName(KObjectName),
282 DEFINE_BENCHMARK(TBitMapAlloc_Ctor,
284 new (Bma) TBitMapAllocator(256, ETrue),
287 DEFINE_BENCHMARK(TBitMapAlloc_AllocFree1,
289 Bma->Alloc(17, 119); Bma->Free(17, 119),
292 DEFINE_BENCHMARK(TBitMapAlloc_AllocFree2,
294 Bma->Free(Bma->Alloc()),
297 DEFINE_BENCHMARK(TBitMapAlloc_SelectFree,
299 Bma->SelectiveFree(0, 153),
302 DEFINE_BENCHMARK(TBitMapAlloc_NotFree,
304 Bma->NotFree(0, 119),
307 DEFINE_BENCHMARK(TBitMapAlloc_NotAlloc,
309 Bma->NotAllocated(0, 119),
312 DEFINE_BENCHMARK(TBitMapAlloc_AllocList,
314 Bma->AllocList(32, BitmapList); Bma->Free(0, 32),
317 DEFINE_BENCHMARK(TBitMapAlloc_AllocAligned,
318 Bma->Alloc(0, 35); TInt a = 0; TInt b=0,
319 Bma->AllocAligned(64, 3, 0, EFalse, a, b),
322 DEFINE_BENCHMARK(Kern_ValidateName,
324 Kern::ValidateName(KObjectName),
327 DEFINE_BENCHMARK(memicmp,
329 memicmp(CharData, CharData, 49),
332 // 2. Functions that have no C++ equivalent
336 DEFINE_BENCHMARK(NKern_LockedInc,
341 DEFINE_BENCHMARK(NKern_LockedDec,
346 DEFINE_BENCHMARK(NKern_LockedAdd,
348 NKern::LockedAdd(i, 0),
351 DEFINE_BENCHMARK(NKern_LockedSetClear,
353 NKern::LockedSetClear(i, 0, 0),
356 DEFINE_BENCHMARK(NKern_LockedSetClear8,
358 NKern::LockedSetClear8(i, 0, 0),
361 DEFINE_BENCHMARK(NKern_SafeInc,
366 DEFINE_BENCHMARK(NKern_SafeDec,
371 DEFINE_BENCHMARK(NKern_SafeSwap,
373 NKern::SafeSwap(0, i),
376 DEFINE_BENCHMARK(NKern_SafeSwap8,
378 NKern::SafeSwap8(0, i),
381 DEFINE_BENCHMARK(NKern_LockUnlock,
383 NKern::Lock(); NKern::Unlock(),
386 DEFINE_BENCHMARK(NKern_DisableInts1,
388 NKern::RestoreInterrupts(NKern::DisableInterrupts(1)),
391 DEFINE_BENCHMARK(NKern_DisableInts2,
393 NKern::RestoreInterrupts(NKern::DisableInterrupts(2)),
398 DEFINE_BENCHMARK(Kern_NanoWait,
400 Kern::NanoWait(1000),
403 DEFINE_BENCHMARK(Kern_KUSafeInc,
404 umemset(UserPtr, 1, sizeof(TInt)),
405 Kern::KUSafeInc(*(TInt*)UserPtr),
408 DEFINE_BENCHMARK(Kern_KUSafeDec,
409 umemset(UserPtr, KMaxTInt8, sizeof(TInt)),
410 Kern::KUSafeDec(*(TInt*)UserPtr),
413 DEFINE_BENCHMARK(DThread_ObjectFromHandle,
414 DThread* thread = &Kern::CurrentThread(),
415 thread->ObjectFromHandle(1),
418 DEFINE_BENCHMARK(Kern_ObjectFromHandle,
419 DThread* thread = &Kern::CurrentThread(),
420 Kern::ObjectFromHandle(thread, 1, -1),
423 DEFINE_BENCHMARK(Kern_KUSafeRead,
425 Kern::KUSafeRead(UserPtr, buf, 128),
428 DEFINE_BENCHMARK(Kern_SafeRead,
430 Kern::SafeRead(KernelPtr, buf, 128),
433 DEFINE_BENCHMARK(Kern_KUSafeWrite,
435 Kern::KUSafeWrite(UserPtr, buf, 128),
438 DEFINE_BENCHMARK(Kern_SafeWrite,
440 Kern::SafeWrite(KernelPtr, buf, 128),
443 DEFINE_BENCHMARK(Kern_SafeRead4,
445 Kern::SafeRead(KernelPtr, buf, 4),
448 DEFINE_BENCHMARK(Kern_SafeRead8,
450 Kern::SafeRead(KernelPtr, buf, 8),
455 DEFINE_BENCHMARK(umemput32_16,
457 umemput32(UserPtr, KernelPtr, 16),
460 DEFINE_BENCHMARK(umemput32_4,
462 umemput32(UserPtr, KernelPtr, 4),
465 DEFINE_MEMORY_BENCHMARK(umemput32_64K,
470 umemput32(dest, src, 65536),
473 DEFINE_BENCHMARK(umemput_32,
475 umemput(UserPtr, KernelPtr, 32),
478 DEFINE_BENCHMARK(umemput_40,
480 umemput(UserPtr, KernelPtr, 40),
483 DEFINE_MEMORY_BENCHMARK(umemput_64K,
488 umemput(dest, src, 65536),
491 DEFINE_BENCHMARK(umemget32_32,
493 umemget32(KernelPtr, UserPtr, 32),
496 DEFINE_MEMORY_BENCHMARK(umemget32_64K,
501 umemget32(dest, src, 65536),
504 DEFINE_BENCHMARK(umemget_7,
506 umemget(KernelPtr, UserPtr, 7),
509 DEFINE_MEMORY_BENCHMARK(umemget_64K,
514 umemget(dest, src, 65536),
517 DEFINE_BENCHMARK(umemset_64K,
519 umemset(UserPtr, 23, 65536),
523 // K::ObjectFromHandle(TInt /*aHandle*/)
524 // K::ObjectFromHandle(TInt /*aHandle*/, TInt /*aType*/)
525 // ExecHandler::LockedInc
526 // ExecHandler::LockedDec
527 // ExecHandler::SafeInc
528 // ExecHandler::SafeDec