tstTime-3.cpp revision 230bd8589bba39933ac5ec21482d6186d675e604
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/* $Id$ */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/** @file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * IPRT Testcase - Simple RTTime test.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (C) 2006-2007 Oracle Corporation
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This file is part of VirtualBox Open Source Edition (OSE), as
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * available from http://www.virtualbox.org. This file is free software;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * you can redistribute it and/or modify it under the terms of the GNU
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * General Public License (GPL) as published by the Free Software
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Foundation, in version 2 as it comes in the "COPYING" file of the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file may alternatively be used under the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License Version 1.0
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * VirtualBox OSE distribution, in which case the provisions of the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * CDDL are applicable instead of those of the GPL.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You may elect to license modified versions of this file under the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * terms and conditions of either the GPL or the CDDL or both.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/*******************************************************************************
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster* Header Files *
5f4d6070f856d0de82c1d16459b89233e2e81ff2Kohei Tamura*******************************************************************************/
5f4d6070f856d0de82c1d16459b89233e2e81ff2Kohei Tamura#ifdef RT_OS_WINDOWS
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster# include <Windows.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#elif defined RT_OS_L4
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#else /* posix */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster# include <sys/time.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#endif
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <iprt/time.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <iprt/stream.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <iprt/string.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <iprt/initterm.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <iprt/thread.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <iprt/err.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterDECLINLINE(uint64_t) OSNanoTS(void)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#ifdef RT_OS_WINDOWS
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster uint64_t u64; /* manual say larger integer, should be safe to assume it's the same. */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster GetSystemTimeAsFileTime((LPFILETIME)&u64);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return u64 * 100;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#elif defined RT_OS_L4
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /** @todo fix a different timesource on l4. */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return RTTimeNanoTS();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#else /* posix */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster struct timeval tv;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster gettimeofday(&tv, NULL);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (uint64_t)tv.tv_sec * (uint64_t)(1000 * 1000 * 1000)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + (uint64_t)(tv.tv_usec * 1000);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#endif
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterint main(int argc, char **argv)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (argc <= 1)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster RTPrintf("tstTime-3: usage: tstTime-3 <seconds> [seconds2 [..]]\n");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return 1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster RTPrintf("tstTime-3: Testing difference between RTTimeNanoTS() and OS time...\n");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int i = 1; i < argc; i++)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster uint64_t cSeconds = 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int rc = RTStrToUInt64Ex(argv[i], NULL, 0, &cSeconds);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (RT_FAILURE(rc))
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster RTPrintf("tstTime-3: Invalid argument %d: %s\n", i, argv[i]);
5f4d6070f856d0de82c1d16459b89233e2e81ff2Kohei Tamura return 1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster RTPrintf("tstTime-3: %d - %RU64 seconds period...\n", i, cSeconds);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster RTTimeNanoTS(); OSNanoTS(); RTThreadSleep(1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster uint64_t u64RTStartTS = RTTimeNanoTS();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster uint64_t u64OSStartTS = OSNanoTS();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
5f4d6070f856d0de82c1d16459b89233e2e81ff2Kohei Tamura RTThreadSleep(cSeconds * 1000);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster uint64_t u64RTElapsedTS = RTTimeNanoTS();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster uint64_t u64OSElapsedTS = OSNanoTS();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster u64RTElapsedTS -= u64RTStartTS;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster u64OSElapsedTS -= u64OSStartTS;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster RTPrintf("tstTime-3: %d - RT: %16RU64 ns\n", i, u64RTElapsedTS);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster RTPrintf("tstTime-3: %d - OS: %16RU64 ns\n", i, u64OSElapsedTS);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster RTPrintf("tstTime-3: %d - diff: %16RI64 ns\n", i, u64RTElapsedTS - u64OSElapsedTS);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster