1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/bench/d_kernasmfnc.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,528 @@
1.4 +// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32test\bench\d_kernasmfnc.cpp
1.18 +//
1.19 +//
1.20 +
1.21 +#include <e32cmn.h>
1.22 +#include <e32cmn_private.h>
1.23 +#include <kernel/kern_priv.h>
1.24 +#include "d_kernbm.h"
1.25 +#include "d_kernasmbm.h"
1.26 +
1.27 +TUint8* UserPtr; // pointer to user-side buffer
1.28 +TUint8* KernelPtr; // pointer to kernel-side buffer
1.29 +
1.30 +NFastMutex* FastMutex;
1.31 +NFastSemaphore* FastSem;
1.32 +
1.33 +DThread* Thread;
1.34 +
1.35 +void DfcFunc(TAny*)
1.36 + {
1.37 + }
1.38 +
1.39 +TDfc Dfc(DfcFunc, NULL, Kern::DfcQue0(), 3);
1.40 +
1.41 +TPriList<TPriListLink, 64> PriList;
1.42 +
1.43 +_LIT(KObjectName, "This is a valid object name.");
1.44 +
1.45 +TBitMapAllocator* Bma;
1.46 +TInt BitmapList[32];
1.47 +const TUint8* CharData = (const TUint8*) "Here's some char data to compare with memicmp";
1.48 +
1.49 +// Allocate a 32-byte aligned block
1.50 +TAny* AllocAligned(TUint aSize)
1.51 + {
1.52 + TUint mem = (TUint)Kern::Alloc(aSize + 32 + 4);
1.53 + if (!mem)
1.54 + return NULL;
1.55 + TUint* ptr = (TUint*) ALIGN_ADDR(mem + 4);
1.56 + ptr[-1] = mem;
1.57 + return ptr;
1.58 + }
1.59 +
1.60 +// Free a block allocated by AllocAligned
1.61 +void FreeAligned(TAny* ptr)
1.62 + {
1.63 + Kern::Free((TAny*)((TUint*)ptr)[-1]);
1.64 + }
1.65 +
1.66 +TBitMapAllocator* AllocAlignedBMA(TInt aSize, TBool aState)
1.67 + {
1.68 + TInt nmapw=(aSize+31)>>5;
1.69 + TInt memsz=sizeof(TBitMapAllocator)+(nmapw-1)*sizeof(TUint32);
1.70 + TBitMapAllocator* pA=(TBitMapAllocator*)AllocAligned(memsz);
1.71 + if (pA)
1.72 + new(pA) TBitMapAllocator(aSize, aState);
1.73 + return pA;
1.74 + }
1.75 +
1.76 +TInt ThreadFunc(TAny* aPtr)
1.77 + {
1.78 + return KErrNone;
1.79 + }
1.80 +
1.81 +TInt InitData()
1.82 + {
1.83 + KernelPtr = (TUint8*)AllocAligned(KKernAsmBmBufferSize);
1.84 + if (!KernelPtr)
1.85 + return KErrNoMemory;
1.86 +
1.87 + Bma = AllocAlignedBMA(256, ETrue);
1.88 + if (!Bma)
1.89 + return KErrNoMemory;
1.90 +
1.91 + FastSem = (NFastSemaphore*) AllocAligned(sizeof(NFastSemaphore));
1.92 + if (!FastSem)
1.93 + return KErrNoMemory;
1.94 + new (FastSem) NFastSemaphore;
1.95 + NKern::FSSetOwner(FastSem, NULL);
1.96 +
1.97 + FastMutex = (NFastMutex*) AllocAligned(sizeof(FastMutex));
1.98 + if (!FastMutex)
1.99 + return KErrNoMemory;
1.100 + new (FastMutex) NFastMutex;
1.101 +
1.102 + SThreadCreateInfo info;
1.103 + info.iType=EThreadSupervisor;
1.104 + info.iFunction=ThreadFunc;
1.105 + info.iPtr=NULL;
1.106 + info.iSupervisorStack=NULL;
1.107 + info.iSupervisorStackSize=0; // zero means use default value
1.108 + info.iInitialThreadPriority=NKern::CurrentThread()->iPriority;
1.109 + info.iName.Set(_L("bmthread2"));
1.110 + info.iTotalSize = sizeof(info);
1.111 +
1.112 + NKern::ThreadEnterCS();
1.113 + TInt r = Kern::ThreadCreate(info);
1.114 + NKern::ThreadLeaveCS();
1.115 + if (r != KErrNone)
1.116 + return r;
1.117 +
1.118 + Thread = (DThread*)info.iHandle;
1.119 +
1.120 + return KErrNone;
1.121 + }
1.122 +
1.123 +void CloseData()
1.124 + {
1.125 + FreeAligned(KernelPtr); KernelPtr = NULL;
1.126 + FreeAligned(Bma); Bma = NULL;
1.127 + FreeAligned(FastSem); FastSem = NULL;
1.128 + FreeAligned(FastMutex); FastMutex = NULL;
1.129 + Kern::ThreadResume(*Thread); Thread = NULL; // thread will now exit
1.130 + }
1.131 +
1.132 +// 1. Functions that have C++ equivalents
1.133 +
1.134 +// 1.1 NKern
1.135 +
1.136 +// 1.1.1 DFCs
1.137 +
1.138 +DEFINE_BENCHMARK(Dfc_DoEnqueCancel,
1.139 + NKern::Lock(),
1.140 + Dfc.DoEnque(); Dfc.Cancel(),
1.141 + NKern::Unlock());
1.142 +
1.143 +DEFINE_BENCHMARK(Dfc_AddCancel,
1.144 + NKern::Lock(),
1.145 + Dfc.Add(); Dfc.Cancel(),
1.146 + NKern::Unlock());
1.147 +
1.148 +// Not exported: TDfc::DoEnqueFinal, TDfc::ThreadFunction
1.149 +
1.150 +// 1.1.2 Fast mutex
1.151 +
1.152 +DEFINE_BENCHMARK(NKern_LockUnlockSystem,
1.153 + ,
1.154 + NKern::LockSystem(); NKern::UnlockSystem(),
1.155 + );
1.156 +
1.157 +DEFINE_BENCHMARK(NFastMutex_WaitSignal,
1.158 + NKern::Lock(),
1.159 + FastMutex->Wait(); FastMutex->Signal(),
1.160 + NKern::Unlock()); // no contention
1.161 +
1.162 +DEFINE_BENCHMARK(NKern_FMWaitSignal,
1.163 + ,
1.164 + NKern::FMWait(FastMutex); NKern::FMSignal(FastMutex),
1.165 + ); // no contention
1.166 +
1.167 +// Benchmark that exercises contention for fast mutex
1.168 +DEFINE_THREADED_BENCHMARK(NFastMutex_ThreadSwitch,
1.169 + -1,
1.170 + Kern::ThreadResume(*iThread2),
1.171 + NKern::FSWait(FastSem); NKern::FMWait(FastMutex); NKern::FMSignal(FastMutex),
1.172 + NKern::FMWait(FastMutex); NKern::FSSignal(FastSem); NKern::FMSignal(FastMutex),
1.173 + );
1.174 +
1.175 +// Benchmark thread switching with suspend/resume for comparison
1.176 +DEFINE_THREADED_BENCHMARK(Kern_ThreadSwitch,
1.177 + 1,
1.178 + ,
1.179 + Kern::ThreadResume(*iThread2),
1.180 + Kern::ThreadSuspend(*iThread2, 1),
1.181 + Kern::ThreadResume(*iThread2));
1.182 +
1.183 +// 1.1.3 Fast semaphore
1.184 +
1.185 +DEFINE_BENCHMARK(NFastSem_Wait,
1.186 + NKern::Lock(); FastSem->SignalN(aParams.iIts * 10),
1.187 + FastSem->Wait(),
1.188 + NKern::Unlock());
1.189 +
1.190 +DEFINE_BENCHMARK(NFastSem_Signal,
1.191 + NKern::Lock(),
1.192 + FastSem->Signal(),
1.193 + FastSem->Reset(); NKern::Unlock());
1.194 +
1.195 +DEFINE_BENCHMARK(NFastSem_SignalN,
1.196 + NKern::Lock(),
1.197 + FastSem->SignalN(1),
1.198 + FastSem->Reset(); NKern::Unlock());
1.199 +
1.200 +DEFINE_BENCHMARK(NFastSem_Reset,
1.201 + NKern::Lock(),
1.202 + FastSem->Reset(),
1.203 + NKern::Unlock());
1.204 +
1.205 +// NFastSemaphore::WaitCancel not exported
1.206 +
1.207 +DEFINE_THREADED_BENCHMARK(NFastSem_ThreadSwitch,
1.208 + -1,
1.209 + Kern::ThreadResume(*iThread2),
1.210 + NKern::FSWait(FastSem),
1.211 + NKern::FSSignal(FastSem),
1.212 + );
1.213 +
1.214 +DEFINE_BENCHMARK(NKern_WaitForAnyReq,
1.215 + NKern::ThreadRequestSignal(NULL, aParams.iIts * 10),
1.216 + NKern::WaitForAnyRequest(),
1.217 + );
1.218 +
1.219 +DEFINE_BENCHMARK(NKern_ThreadReqSignal,
1.220 + NThread* t = &Thread->iNThread; NKern::Lock(),
1.221 + NKern::ThreadRequestSignal(t),
1.222 + t->iRequestSemaphore.Reset();NKern::Unlock());
1.223 +
1.224 +DEFINE_BENCHMARK(NKern_ThreadReqSignal2,
1.225 + NThread* t = &Thread->iNThread,
1.226 + NKern::FMWait(FastMutex); NKern::ThreadRequestSignal(t, FastMutex),
1.227 + NKern::Lock(); t->iRequestSemaphore.Reset(); NKern::Unlock(););
1.228 +
1.229 +// 1.1.4 Timers
1.230 +
1.231 +DEFINE_BENCHMARK(NTimer_OneShot,
1.232 + NTimer timer,
1.233 + timer.OneShot(100); timer.Cancel(),
1.234 + );
1.235 +
1.236 +DEFINE_BENCHMARK(NTimer_Again,
1.237 + NTimer timer,
1.238 + timer.Again(100); timer.Cancel(),
1.239 + );
1.240 +
1.241 +// Not exported:
1.242 +// NTimerQ::Add
1.243 +// NTimerQ::AddFinal
1.244 +// NTimerQ::DfcFn
1.245 +// NTimerQ::Dfc
1.246 +// Not tested:
1.247 +// NTimerQ::IdleTime
1.248 +
1.249 +// 1.1.5 Priority list
1.250 +
1.251 +DEFINE_BENCHMARK(TPriList_AddRemove,
1.252 + TPriListLink link(15),
1.253 + PriList.Add(&link); PriList.Remove(&link),
1.254 + );
1.255 +
1.256 +DEFINE_BENCHMARK(TPriList_AddRemove2,
1.257 + TPriListLink link(15); TPriListLink link2(15); PriList.Add(&link2),
1.258 + PriList.Add(&link); PriList.Remove(&link),
1.259 + PriList.Remove(&link2));
1.260 +
1.261 +DEFINE_BENCHMARK(TPriList_First,
1.262 + TPriListLink link(15); PriList.Add(&link),
1.263 + PriList.First(),
1.264 + PriList.Remove(&link));
1.265 +
1.266 +DEFINE_BENCHMARK(TPriList_HighestPri,
1.267 + TPriListLink link(15); PriList.Add(&link),
1.268 + PriList.First(),
1.269 + PriList.Remove(&link));
1.270 +
1.271 +DEFINE_BENCHMARK(TPriList_ChangePri,
1.272 + TPriListLink link(15); PriList.Add(&link); TInt pri = 15,
1.273 + pri ^= 32; PriList.ChangePriority(&link, pri),
1.274 + PriList.Remove(&link));
1.275 +
1.276 +// 1.2 Kern
1.277 +
1.278 +DEFINE_BENCHMARK(Kern_ValidateFullName,
1.279 + ,
1.280 + Kern::ValidateFullName(KObjectName),
1.281 + );
1.282 +
1.283 +// 1.3. klib
1.284 +
1.285 +DEFINE_BENCHMARK(TBitMapAlloc_Ctor,
1.286 + ,
1.287 + new (Bma) TBitMapAllocator(256, ETrue),
1.288 + );
1.289 +
1.290 +DEFINE_BENCHMARK(TBitMapAlloc_AllocFree1,
1.291 + ,
1.292 + Bma->Alloc(17, 119); Bma->Free(17, 119),
1.293 + );
1.294 +
1.295 +DEFINE_BENCHMARK(TBitMapAlloc_AllocFree2,
1.296 + Bma->Alloc(0, 119),
1.297 + Bma->Free(Bma->Alloc()),
1.298 + Bma->Free(0, 119));
1.299 +
1.300 +DEFINE_BENCHMARK(TBitMapAlloc_SelectFree,
1.301 + ,
1.302 + Bma->SelectiveFree(0, 153),
1.303 + );
1.304 +
1.305 +DEFINE_BENCHMARK(TBitMapAlloc_NotFree,
1.306 + ,
1.307 + Bma->NotFree(0, 119),
1.308 + );
1.309 +
1.310 +DEFINE_BENCHMARK(TBitMapAlloc_NotAlloc,
1.311 + ,
1.312 + Bma->NotAllocated(0, 119),
1.313 + );
1.314 +
1.315 +DEFINE_BENCHMARK(TBitMapAlloc_AllocList,
1.316 + ,
1.317 + Bma->AllocList(32, BitmapList); Bma->Free(0, 32),
1.318 + );
1.319 +
1.320 +DEFINE_BENCHMARK(TBitMapAlloc_AllocAligned,
1.321 + Bma->Alloc(0, 35); TInt a = 0; TInt b=0,
1.322 + Bma->AllocAligned(64, 3, 0, EFalse, a, b),
1.323 + Bma->Free(0, 35));
1.324 +
1.325 +DEFINE_BENCHMARK(Kern_ValidateName,
1.326 + ,
1.327 + Kern::ValidateName(KObjectName),
1.328 + );
1.329 +
1.330 +DEFINE_BENCHMARK(memicmp,
1.331 + ,
1.332 + memicmp(CharData, CharData, 49),
1.333 + );
1.334 +
1.335 +// 2. Functions that have no C++ equivalent
1.336 +
1.337 +// 2.1. NKern
1.338 +
1.339 +DEFINE_BENCHMARK(NKern_LockedInc,
1.340 + TInt i,
1.341 + NKern::LockedInc(i),
1.342 + );
1.343 +
1.344 +DEFINE_BENCHMARK(NKern_LockedDec,
1.345 + TInt i,
1.346 + NKern::LockedDec(i),
1.347 + );
1.348 +
1.349 +DEFINE_BENCHMARK(NKern_LockedAdd,
1.350 + TInt i,
1.351 + NKern::LockedAdd(i, 0),
1.352 + );
1.353 +
1.354 +DEFINE_BENCHMARK(NKern_LockedSetClear,
1.355 + TUint32 i,
1.356 + NKern::LockedSetClear(i, 0, 0),
1.357 + );
1.358 +
1.359 +DEFINE_BENCHMARK(NKern_LockedSetClear8,
1.360 + TUint8 i,
1.361 + NKern::LockedSetClear8(i, 0, 0),
1.362 + );
1.363 +
1.364 +DEFINE_BENCHMARK(NKern_SafeInc,
1.365 + TInt i = 0,
1.366 + NKern::SafeInc(i),
1.367 + );
1.368 +
1.369 +DEFINE_BENCHMARK(NKern_SafeDec,
1.370 + TInt i = KMaxTInt,
1.371 + NKern::SafeDec(i),
1.372 + );
1.373 +
1.374 +DEFINE_BENCHMARK(NKern_SafeSwap,
1.375 + TAny* i,
1.376 + NKern::SafeSwap(0, i),
1.377 + );
1.378 +
1.379 +DEFINE_BENCHMARK(NKern_SafeSwap8,
1.380 + TUint8 i,
1.381 + NKern::SafeSwap8(0, i),
1.382 + );
1.383 +
1.384 +DEFINE_BENCHMARK(NKern_LockUnlock,
1.385 + ,
1.386 + NKern::Lock(); NKern::Unlock(),
1.387 + );
1.388 +
1.389 +DEFINE_BENCHMARK(NKern_DisableInts1,
1.390 + ,
1.391 + NKern::RestoreInterrupts(NKern::DisableInterrupts(1)),
1.392 + );
1.393 +
1.394 +DEFINE_BENCHMARK(NKern_DisableInts2,
1.395 + ,
1.396 + NKern::RestoreInterrupts(NKern::DisableInterrupts(2)),
1.397 + );
1.398 +
1.399 +// 2.2 Kern
1.400 +
1.401 +DEFINE_BENCHMARK(Kern_NanoWait,
1.402 + ,
1.403 + Kern::NanoWait(1000),
1.404 + ); // expect 1uS!
1.405 +
1.406 +DEFINE_BENCHMARK(Kern_KUSafeInc,
1.407 + umemset(UserPtr, 1, sizeof(TInt)),
1.408 + Kern::KUSafeInc(*(TInt*)UserPtr),
1.409 + );
1.410 +
1.411 +DEFINE_BENCHMARK(Kern_KUSafeDec,
1.412 + umemset(UserPtr, KMaxTInt8, sizeof(TInt)),
1.413 + Kern::KUSafeDec(*(TInt*)UserPtr),
1.414 + );
1.415 +
1.416 +DEFINE_BENCHMARK(DThread_ObjectFromHandle,
1.417 + DThread* thread = &Kern::CurrentThread(),
1.418 + thread->ObjectFromHandle(1),
1.419 + );
1.420 +
1.421 +DEFINE_BENCHMARK(Kern_ObjectFromHandle,
1.422 + DThread* thread = &Kern::CurrentThread(),
1.423 + Kern::ObjectFromHandle(thread, 1, -1),
1.424 + );
1.425 +
1.426 +DEFINE_BENCHMARK(Kern_KUSafeRead,
1.427 + TUint8 buf[128],
1.428 + Kern::KUSafeRead(UserPtr, buf, 128),
1.429 + );
1.430 +
1.431 +DEFINE_BENCHMARK(Kern_SafeRead,
1.432 + TUint8 buf[128],
1.433 + Kern::SafeRead(KernelPtr, buf, 128),
1.434 + );
1.435 +
1.436 +DEFINE_BENCHMARK(Kern_KUSafeWrite,
1.437 + TUint8 buf[128],
1.438 + Kern::KUSafeWrite(UserPtr, buf, 128),
1.439 + );
1.440 +
1.441 +DEFINE_BENCHMARK(Kern_SafeWrite,
1.442 + TUint8 buf[128],
1.443 + Kern::SafeWrite(KernelPtr, buf, 128),
1.444 + );
1.445 +
1.446 +DEFINE_BENCHMARK(Kern_SafeRead4,
1.447 + TUint8 buf[128],
1.448 + Kern::SafeRead(KernelPtr, buf, 4),
1.449 + );
1.450 +
1.451 +DEFINE_BENCHMARK(Kern_SafeRead8,
1.452 + TUint8 buf[128],
1.453 + Kern::SafeRead(KernelPtr, buf, 8),
1.454 + );
1.455 +
1.456 +// 2.3 klib
1.457 +
1.458 +DEFINE_BENCHMARK(umemput32_16,
1.459 + ,
1.460 + umemput32(UserPtr, KernelPtr, 16),
1.461 + );
1.462 +
1.463 +DEFINE_BENCHMARK(umemput32_4,
1.464 + ,
1.465 + umemput32(UserPtr, KernelPtr, 4),
1.466 + );
1.467 +
1.468 +DEFINE_MEMORY_BENCHMARK(umemput32_64K,
1.469 + 4,
1.470 + KernelPtr,
1.471 + UserPtr,
1.472 + ,
1.473 + umemput32(dest, src, 65536),
1.474 + );
1.475 +
1.476 +DEFINE_BENCHMARK(umemput_32,
1.477 + ,
1.478 + umemput(UserPtr, KernelPtr, 32),
1.479 + );
1.480 +
1.481 +DEFINE_BENCHMARK(umemput_40,
1.482 + ,
1.483 + umemput(UserPtr, KernelPtr, 40),
1.484 + );
1.485 +
1.486 +DEFINE_MEMORY_BENCHMARK(umemput_64K,
1.487 + 1,
1.488 + KernelPtr,
1.489 + UserPtr,
1.490 + ,
1.491 + umemput(dest, src, 65536),
1.492 + );
1.493 +
1.494 +DEFINE_BENCHMARK(umemget32_32,
1.495 + ,
1.496 + umemget32(KernelPtr, UserPtr, 32),
1.497 + );
1.498 +
1.499 +DEFINE_MEMORY_BENCHMARK(umemget32_64K,
1.500 + 4,
1.501 + UserPtr,
1.502 + KernelPtr,
1.503 + ,
1.504 + umemget32(dest, src, 65536),
1.505 + );
1.506 +
1.507 +DEFINE_BENCHMARK(umemget_7,
1.508 + ,
1.509 + umemget(KernelPtr, UserPtr, 7),
1.510 + );
1.511 +
1.512 +DEFINE_MEMORY_BENCHMARK(umemget_64K,
1.513 + 1,
1.514 + UserPtr,
1.515 + KernelPtr,
1.516 + ,
1.517 + umemget(dest, src, 65536),
1.518 + );
1.519 +
1.520 +DEFINE_BENCHMARK(umemset_64K,
1.521 + ,
1.522 + umemset(UserPtr, 23, 65536),
1.523 + );
1.524 +
1.525 +// Not exported:
1.526 +// K::ObjectFromHandle(TInt /*aHandle*/)
1.527 +// K::ObjectFromHandle(TInt /*aHandle*/, TInt /*aType*/)
1.528 +// ExecHandler::LockedInc
1.529 +// ExecHandler::LockedDec
1.530 +// ExecHandler::SafeInc
1.531 +// ExecHandler::SafeDec