cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync/* $Id$ */
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync/** @file
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync * IPRT Testcase - RTThreadGetExecution.
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync */
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync/*
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync * Copyright (C) 2010 Oracle Corporation
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync *
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync * available from http://www.virtualbox.org. This file is free software;
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync * you can redistribute it and/or modify it under the terms of the GNU
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync * General Public License (GPL) as published by the Free Software
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync *
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync * The contents of this file may alternatively be used under the terms
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync * of the Common Development and Distribution License Version 1.0
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync * VirtualBox OSE distribution, in which case the provisions of the
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync * CDDL are applicable instead of those of the GPL.
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync *
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync * You may elect to license modified versions of this file under the
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync * terms and conditions of either the GPL or the CDDL or both.
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync */
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync/*******************************************************************************
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync* Header Files *
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync*******************************************************************************/
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync#include <iprt/thread.h>
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync#include <iprt/asm.h>
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync#include <iprt/test.h>
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync#include <iprt/string.h>
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync#include <iprt/stream.h>
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync#include <iprt/time.h>
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync/*******************************************************************************
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync* Global Variables *
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync*******************************************************************************/
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsyncstatic RTTEST g_hTest;
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsyncstatic volatile uint64_t g_kernel, g_user;
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsyncstatic DECLCALLBACK(int) testThread(RTTHREAD hSelf, void *pvUser)
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync{
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync uint64_t u64Now = RTTimeMilliTS();
50be00ffcd2f0cdb310af876a0ae4179c1b07ccbvboxsync uint64_t kernel, kernelStart, user, userStart;
50be00ffcd2f0cdb310af876a0ae4179c1b07ccbvboxsync RTThreadGetExecutionTimeMilli(&kernelStart, &userStart);
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync while (RTTimeMilliTS() < u64Now + 1000)
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync ;
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync RTThreadGetExecutionTimeMilli(&kernel, &user);
50be00ffcd2f0cdb310af876a0ae4179c1b07ccbvboxsync RTPrintf("kernel = %4lldms, user = %4lldms\n", kernel - kernelStart, user - userStart);
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync ASMAtomicAddU64(&g_kernel, kernel);
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync ASMAtomicAddU64(&g_user, user);
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync return VINF_SUCCESS;
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync}
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsyncstatic void test1(void)
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync{
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync RTTestSub(g_hTest, "Interrupt RTThreadSleep");
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync RTTHREAD hThread[16];
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync RTMSINTERVAL msWait = 1000;
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync for (unsigned i = 0; i < RT_ELEMENTS(hThread); i++)
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync {
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync RTTESTI_CHECK_RC_RETV(RTThreadCreate(&hThread[i], testThread, NULL, 0, RTTHREADTYPE_DEFAULT,
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync RTTHREADFLAGS_WAITABLE, "test"), VINF_SUCCESS);
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync }
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync RTThreadSleep(500);
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync RTPrintf("Waiting for %dms ...\n", msWait);
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync RTThreadSleep(msWait);
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync for (unsigned i = 0; i < RT_ELEMENTS(hThread); i++)
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync RTTESTI_CHECK_RC(RTThreadWait(hThread[i], RT_INDEFINITE_WAIT, NULL), VINF_SUCCESS);
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync RTPrintf("sum kernel = %lldms, sum user = %lldms\n", g_kernel, g_user);
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync}
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsyncint main()
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync{
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync RTEXITCODE rcExit = RTTestInitAndCreate("tstRTThreadExecutionTime", &g_hTest);
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync if (rcExit != RTEXITCODE_SUCCESS)
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync return rcExit;
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync test1();
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync return RTTestSummaryAndDestroy(g_hTest);
cfc61eb1cf1a2418102f57ccc29ae617d165e492vboxsync}