tstRTR0ThreadPreemption.cpp revision d84e3f7154abd56986d6ce98d9156769c7ae6587
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * IPRT R0 Testcase - Thread Preemption.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * Copyright (C) 2009-2015 Oracle Corporation
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * available from http://www.virtualbox.org. This file is free software;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * you can redistribute it and/or modify it under the terms of the GNU
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * General Public License (GPL) as published by the Free Software
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * The contents of this file may alternatively be used under the terms
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * of the Common Development and Distribution License Version 1.0
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * VirtualBox OSE distribution, in which case the provisions of the
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * CDDL are applicable instead of those of the GPL.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * You may elect to license modified versions of this file under the
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * terms and conditions of either the GPL or the CDDL or both.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync/*******************************************************************************
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync* Header Files *
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync*******************************************************************************/
bdec40b10acfe9f798021b508a0a7b8c786fc193vboxsync * Thread-context hook data.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync /* For RTTHREADCTXEVENT_PREEMPTING. */
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync /* For RTTHREADCTXEVENT_RESUMED. */
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync volatile bool fResumedInvoked;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * Thread-context hook function.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * @param enmEvent The thread-context event.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * @param pvUser Pointer to the user argument.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsyncstatic DECLCALLBACK(void) tstRTR0ThreadCtxHook(RTTHREADCTXEVENT enmEvent, void *pvUser)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync PTSTRTR0THREADCTXDATA pData = (PTSTRTR0THREADCTXDATA)pvUser;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pData->achResult, sizeof(pData->achResult), "!tstRTR0ThreadCtxHook: Invalid magic.");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync ASMAtomicWriteBool(&pData->fPreemptingInvoked, true);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync /* We've already been called once, we now might very well be on another CPU. Nothing to do here. */
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pData->achResult, sizeof(pData->achResult),
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync "!tstRTR0ThreadCtxHook[RTTHREADCTXEVENT_PREEMPTING]: Called with preemption enabled");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTNATIVETHREAD hCurrentThread = RTThreadNativeSelf();
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pData->achResult, sizeof(pData->achResult),
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync "!tstRTR0ThreadCtxHook[RTTHREADCTXEVENT_PREEMPTING]: Thread switched! Source=%RTnthrd Current=%RTnthrd.",
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pData->achResult, sizeof(pData->achResult),
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync "!tstRTR0ThreadCtxHook[RTTHREADCTXEVENT_PREEMPTING]: migrated uSourceCpuId=%RU32 uCurrentCpuId=%RU32",
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync /* We've already been called once successfully, nothing more to do. */
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pData->achResult, sizeof(pData->achResult),
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync "!tstRTR0ThreadCtxHook[RTTHREADCTXEVENT_RESUMED]: Called before preempting callback was invoked.");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTNATIVETHREAD hCurrentThread = RTThreadNativeSelf();
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pData->achResult, sizeof(pData->achResult),
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync "!tstRTR0ThreadCtxHook[RTTHREADCTXEVENT_RESUMED]: Thread switched! Source=%RTnthrd Current=%RTnthrd.",
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync AssertMsgFailed(("Invalid event %#x\n", enmEvent));
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * Service request callback function.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * @returns VBox status code.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * @param pSession The caller's session.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * @param u64Arg 64-bit integer argument.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * @param pReqHdr The request header. Input / Output. Optional.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsyncDECLEXPORT(int) TSTRTR0ThreadPreemptionSrvReqHandler(PSUPDRVSESSION pSession, uint32_t uOperation,
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * The big switch.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!42failure42%1024s", "");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!Interrupts disabled");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns false by default");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTTHREADPREEMPTSTATE State = RTTHREADPREEMPTSTATE_INITIALIZER;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after RTThreadPreemptDisable");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!Interrupts disabled");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTTHREADPREEMPTSTATE State = RTTHREADPREEMPTSTATE_INITIALIZER;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync uint64_t const cNsMax = UINT64_C(8)*1000U*1000U*1000U;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync uint64_t const cNsMax = UINT64_C(2)*1000U*1000U*1000U;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync cNanosSysElapsed = RTTimeSystemNanoTS() - u64StartSysTS;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!Preempt not pending after %'llu loops / %'llu ns / %'llu ns (sys)",
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "RTThreadPreemptIsPending returned true after %'llu loops / %'llu ns / %'llu ns (sys)",
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!Interrupts disabled");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after RTThreadPreemptDisable");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync bool const fDefault = RTThreadPreemptIsEnabled(NIL_RTTHREAD);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTTHREADPREEMPTSTATE State1 = RTTHREADPREEMPTSTATE_INITIALIZER;
bdec40b10acfe9f798021b508a0a7b8c786fc193vboxsync RTTHREADPREEMPTSTATE State2 = RTTHREADPREEMPTSTATE_INITIALIZER;
bdec40b10acfe9f798021b508a0a7b8c786fc193vboxsync RTTHREADPREEMPTSTATE State3 = RTTHREADPREEMPTSTATE_INITIALIZER;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 3rd RTThreadPreemptDisable");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) && !*pszErr)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 1st RTThreadPreemptRestore");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 2nd RTThreadPreemptDisable");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) && !*pszErr)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 2nd RTThreadPreemptRestore");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 1st RTThreadPreemptDisable");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) != fDefault && !*pszErr)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns false after 3rd RTThreadPreemptRestore");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadCtxHooksCreate must be called with preemption enabled");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync bool fRegistered = RTThreadCtxHooksAreRegistered(NIL_RTTHREADCTX);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadCtxHooksAreRegistered returns true before creating any hooks");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "RTThreadCtxHooksCreate returns VERR_NOT_SUPPORTED");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadCtxHooksCreate returns %Rrc", rc);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync fRegistered = RTThreadCtxHooksAreRegistered(hThreadCtx);
RTStrPrintf(pszErr, cchErr, "!RTThreadCtxHooksAreRegistered returns true before registering any hooks");
if (!fRegistered)
RTStrPrintf(pszErr, cchErr, "!RTThreadCtxHooksAreRegistered return false when hooks are supposedly registered");
"!tstRTR0ThreadCtxHooks[RTTHREADCTXEVENT_PREEMPTING] not invoked before migrating from CPU %RU32 to %RU32",
RTStrPrintf(pszErr, cchErr, "!tstRTR0ThreadCtxHooks[RTTHREADCTXEVENT_PREEMPTING] not invoked after ca. %u ms",
cMsSlept);
cMsSlept = 0;
RTStrPrintf(pszErr, cchErr, "!tstRTR0ThreadCtxHooks[RTTHREADCTXEVENT_RESUMED] not invoked after ca. %u ms",
cMsSlept);
if (fRegistered)
RTStrPrintf(pszErr, cchErr, "!RTThreadCtxHooksAreRegistered return true when hooks are deregistered");
return VINF_SUCCESS;