cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync/* $Id$ */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync/** @file
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * Query the time and check that it always goes forward, POSIX only.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync/*
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * Copyright (C) 2011-2014 Oracle Corporation
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync *
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * available from http://www.virtualbox.org. This file is free software;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * you can redistribute it and/or modify it under the terms of the GNU
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * General Public License (GPL) as published by the Free Software
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync *
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * The contents of this file may alternatively be used under the terms
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * of the Common Development and Distribution License Version 1.0
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * VirtualBox OSE distribution, in which case the provisions of the
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * CDDL are applicable instead of those of the GPL.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync *
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * You may elect to license modified versions of this file under the
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * terms and conditions of either the GPL or the CDDL or both.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync/*******************************************************************************
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync* Header Files *
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync*******************************************************************************/
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#include <stdio.h>
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#include <time.h>
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#include <sys/time.h>
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncint main()
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync{
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync unsigned cErrors = 0;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#ifdef USE_CLOCK_MONOTONIC
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync struct timespec aTs[2];
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync struct timespec *pCur = &aTs[0];
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync struct timespec *pPrev = &aTs[1];
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync struct timespec *pTmp;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#else
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync struct timeval aTv[2];
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync struct timeval *pCur = &aTv[0];
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync struct timeval *pPrev = &aTv[1];
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync struct timeval *pTmp;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#endif
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#ifdef USE_CLOCK_MONOTONIC
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync clock_gettime(CLOCK_MONOTONIC, pPrev);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#else
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync gettimeofday(pPrev, NULL);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#endif
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync for (;;)
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync {
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#ifdef USE_CLOCK_MONOTONIC
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync clock_gettime(CLOCK_MONOTONIC, pCur);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#else
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync gettimeofday(pCur, NULL);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#endif
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if ( pCur->tv_sec == pPrev->tv_sec
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#ifdef USE_CLOCK_MONOTONIC
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync && pCur->tv_nsec < pPrev->tv_nsec
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#else
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync && pCur->tv_usec < pPrev->tv_usec
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#endif
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync )
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync {
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#ifdef USE_CLOCK_MONOTONIC
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync printf("tv_nsec in the past: %ld.%09d < %ld.%09d - %u nsec\n",
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync (long)pCur->tv_sec, (unsigned)pCur->tv_nsec,
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync (long)pPrev->tv_sec, (unsigned)pPrev->tv_nsec,
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync (unsigned)pPrev->tv_nsec - (unsigned)pCur->tv_nsec);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#else
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync printf("tv_usec in the past: %ld.%06d < %ld.%06d - %u usec\n",
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync (long)pCur->tv_sec, (unsigned)pCur->tv_usec,
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync (long)pPrev->tv_sec, (unsigned)pPrev->tv_usec,
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync (unsigned)pPrev->tv_usec - (unsigned)pCur->tv_usec);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#endif
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync cErrors++;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if (cErrors > 1000)
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync break;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync }
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync else if (pCur->tv_sec < pPrev->tv_sec)
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync {
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#ifdef USE_CLOCK_MONOTONIC
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync printf("tv_sec in the past: %ld.%09d < %ld.%09d\n",
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync (long)pCur->tv_sec, (unsigned)pCur->tv_nsec,
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync (long)pPrev->tv_sec, (unsigned)pPrev->tv_nsec);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#else
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync printf("tv_sec in the past: %ld.%06d < %ld.%06d\n",
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync (long)pCur->tv_sec, (unsigned)pCur->tv_usec,
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync (long)pPrev->tv_sec, (unsigned)pPrev->tv_usec);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#endif
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync cErrors++;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if (cErrors > 1000)
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync break;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync }
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync else
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync {
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /* swap */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync pTmp = pPrev;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync pPrev = pCur;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync pCur = pTmp;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync }
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync }
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync return 1;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync}