tstTime-2.cpp revision 1e40f57c72c881067b0314f898e1004211bb7650
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth/* $Id$ */
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth/** @file
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth * innotek Portable Runtime Testcase - Simple RTTime test.
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth */
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth/*
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth * Copyright (C) 2006-2007 innotek GmbH
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth *
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth * This file is part of VirtualBox Open Source Edition (OSE), as
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth * available from http://www.virtualbox.org. This file is free software;
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth * you can redistribute it and/or modify it under the terms of the GNU
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth * General Public License as published by the Free Software Foundation,
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth * distribution. VirtualBox OSE is distributed in the hope that it will
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth * be useful, but WITHOUT ANY WARRANTY of any kind.
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth */
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth/*******************************************************************************
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth* Header Files *
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth*******************************************************************************/
9a70fc3be3b1e966bf78825cdb8d509963a6f0a1Mark J. Nelson#include <iprt/time.h>
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth#include <iprt/stream.h>
0c45178b5714d692c44817c2235320c3f2b030abwl#include <iprt/runtime.h>
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth#include <iprt/thread.h>
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth#include <VBox/sup.h>
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcthint main()
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth{
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth unsigned cErrors = 0;
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth int i;
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth RTR3Init();
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth RTPrintf("tstTime-2: TESTING...\n");
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth /*
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth * RTNanoTimeTS() shall never return something which
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth * is less or equal to the return of the previous call.
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth */
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth RTTimeSystemNanoTS(); RTTimeNanoTS(); RTThreadYield();
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth uint64_t u64RTStartTS = RTTimeNanoTS();
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth uint64_t u64OSStartTS = RTTimeSystemNanoTS();
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth#define NUMBER_OF_CALLS (100*_1M)
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth for (i = 0; i < NUMBER_OF_CALLS; i++)
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth RTTimeNanoTS();
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth uint64_t u64RTElapsedTS = RTTimeNanoTS();
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth uint64_t u64OSElapsedTS = RTTimeSystemNanoTS();
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth u64RTElapsedTS -= u64RTStartTS;
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth u64OSElapsedTS -= u64OSStartTS;
9a70fc3be3b1e966bf78825cdb8d509963a6f0a1Mark J. Nelson int64_t i64Diff = u64OSElapsedTS >= u64RTElapsedTS ? u64OSElapsedTS - u64RTElapsedTS : u64RTElapsedTS - u64OSElapsedTS;
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth if (i64Diff > (int64_t)(u64OSElapsedTS / 1000))
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth {
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth RTPrintf("tstTime-2: error: total time differs too much! u64OSElapsedTS=%#llx u64RTElapsedTS=%#llx delta=%lld\n",
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth u64OSElapsedTS, u64RTElapsedTS, u64OSElapsedTS - u64RTElapsedTS);
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth cErrors++;
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth }
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth else
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth RTPrintf("tstTime-2: total time difference: u64OSElapsedTS=%#llx u64RTElapsedTS=%#llx delta=%lld\n",
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth u64OSElapsedTS, u64RTElapsedTS, u64OSElapsedTS - u64RTElapsedTS);
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth RTPrintf("tstTime-2: %u calls to RTTimeNanoTS\n", NUMBER_OF_CALLS);
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth RTPrintf("tstTime-2: RTTimeDbgSteps -> %u (%d ppt)\n", RTTimeDbgSteps(), ((uint64_t)RTTimeDbgSteps() * 1000) / NUMBER_OF_CALLS);
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth RTPrintf("tstTime-2: RTTimeDbgExpired -> %u (%d ppt)\n", RTTimeDbgExpired(), ((uint64_t)RTTimeDbgExpired() * 1000) / NUMBER_OF_CALLS);
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth RTPrintf("tstTime-2: RTTimeDbgBad -> %u (%d ppt)\n", RTTimeDbgBad(), ((uint64_t)RTTimeDbgBad() * 1000) / NUMBER_OF_CALLS);
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth RTPrintf("tstTime-2: RTTimeDbgRaces -> %u (%d ppt)\n", RTTimeDbgRaces(), ((uint64_t)RTTimeDbgRaces() * 1000) / NUMBER_OF_CALLS);
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth if (!cErrors)
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth RTPrintf("tstTime-2: SUCCESS\n");
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth else
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth RTPrintf("tstTime-2: FAILURE - %d errors\n", cErrors);
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth return !!cErrors;
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth}
1e1ddd6cc98ab5af8293f7ebd132be62900730fdcth