tstTimer.cpp revision c58f1213e628a545081c70e26c6b67a841cff880
/* $Id$ */
/** @file
* IPRT Testcase - Timers.
*/
/*
* Copyright (C) 2006-2011 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/initterm.h>
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static volatile unsigned gcTicks;
{
gcTicks++;
if (gu64Prev)
{
int i = (int)( RT_ELEMENTS(cFrequency)
if (i >= 0 && i < (int)RT_ELEMENTS(cFrequency))
cFrequency[i]++;
}
}
int main()
{
/*
* Init runtime
*/
unsigned cErrors = 0;
int rc = RTR3InitExeNoArguments(0);
if (RT_FAILURE(rc))
return RTMsgInitFailure(rc);
/*
* Check that the clock is reliable.
*/
RTPrintf("tstTimer: TESTING - RTTimeNanoTS() for 2sec\n");
uint64_t cIterations = 0;
do
{
{
RTPrintf("tstTimer: FAILURE - RTTimeNanoTS() is unreliable. uTS=%RU64 uTSLast=%RU64\n", uTS, uTSLast);
cErrors++;
}
{
RTPrintf("tstTimer: FAILURE - RTTimeNanoTS() is unreliable. cIterations=%RU64 uTS=%RU64 uTSBegin=%RU64\n", cIterations, uTS, uTSBegin);
return 1;
}
{
RTPrintf("tstTimer: FAILURE - uTSMillies=%RI64 uTSBegin=%RU64 uTSLast=%RU64 uTSDiff=%RU64\n",
cErrors++;
}
if (!cErrors)
RTPrintf("tstTimer: OK - RTTimeNanoTS()\n");
/*
* Tests.
*/
static struct
{
unsigned uMicroInterval;
unsigned uMilliesWait;
unsigned cLower;
unsigned cUpper;
} aTests[] =
{
{ 32000, 2000, 0, 0 },
{ 20000, 2000, 0, 0 },
{ 10000, 2000, 0, 0 },
{ 8000, 2000, 0, 0 },
{ 2000, 2000, 0, 0 },
{ 1000, 2000, 0, 0 },
{ 500, 5000, 0, 0 },
{ 200, 5000, 0, 0 },
{ 100, 5000, 0, 0 }
};
unsigned i = 0;
for (i = 0; i < RT_ELEMENTS(aTests); i++)
{
aTests[i].cLower = (aTests[i].uMilliesWait*1000 - aTests[i].uMilliesWait*100) / aTests[i].uMicroInterval;
aTests[i].cUpper = (aTests[i].uMilliesWait*1000 + aTests[i].uMilliesWait*100) / aTests[i].uMicroInterval;
RTPrintf("\n"
"tstTimer: TESTING - %d us interval, %d ms wait, expects %d-%d ticks.\n",
/*
* Start timer which ticks every 10ms.
*/
gcTicks = 0;
gu64Max = 0;
gu64Prev = 0;
#ifdef RT_OS_WINDOWS
continue;
#else
#endif
if (RT_FAILURE(rc))
{
cErrors++;
continue;
}
/*
* Start the timer and active waiting for the requested test period.
*/
uTSBegin = RTTimeNanoTS();
#ifndef RT_OS_WINDOWS
if (RT_FAILURE(rc))
{
cErrors++;
}
#endif
/* nothing */;
/* destroy the timer */
if (RT_FAILURE(rc))
{
cErrors++;
}
{
RTPrintf("tstTimer: FAILURE - RTTimerDestroy() didn't really stop the timer! gcTicks=%d cTicks=%d\n", gcTicks, cTicks);
cErrors++;
continue;
}
/*
* Check the number of ticks.
*/
{
RTPrintf("tstTimer: FAILURE - Too few ticks gcTicks=%d (expected %d-%d)", gcTicks, aTests[i].cUpper, aTests[i].cLower);
cErrors++;
}
{
RTPrintf("tstTimer: FAILURE - Too many ticks gcTicks=%d (expected %d-%d)", gcTicks, aTests[i].cUpper, aTests[i].cLower);
cErrors++;
}
else
for (int j = 0; j < (int)RT_ELEMENTS(cFrequency); j++)
{
RTPrintf("%+4d%c %6u %3llu.%02llu%% ",
for (unsigned k = 0; k < len; k++)
RTPrintf("*");
RTPrintf("\n");
}
}
/*
* Summary.
*/
if (!cErrors)
RTPrintf("tstTimer: SUCCESS\n");
else
return !!cErrors;
}