VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstTime-2.cpp@ 101472

Last change on this file since 101472 was 98103, checked in by vboxsync, 22 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.3 KB
Line 
1/* $Id: tstTime-2.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Simple RTTime test.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/time.h>
42#include <iprt/stream.h>
43#include <iprt/initterm.h>
44#include <iprt/thread.h>
45#include <VBox/sup.h>
46
47
48/* HACK ALERT! */
49#if defined(RT_OS_DARWIN) && defined(RT_ARCH_ARM64)
50# undef RTTimeNanoTSWorkerName
51# define RTTimeNanoTSWorkerName() "system"
52#endif
53
54
55int main()
56{
57 unsigned cErrors = 0;
58 int i;
59 RTR3InitExeNoArguments(RTR3INIT_FLAGS_SUPLIB);
60 RTPrintf("tstTime-2: TESTING...\n");
61
62 /*
63 * RTNanoTimeTS() shall never return something which
64 * is less or equal to the return of the previous call.
65 */
66
67 RTTimeSystemNanoTS(); RTTimeNanoTS(); RTThreadYield();
68 uint64_t u64RTStartTS = RTTimeNanoTS();
69 uint64_t u64OSStartTS = RTTimeSystemNanoTS();
70#define NUMBER_OF_CALLS (100*_1M)
71
72 for (i = 0; i < NUMBER_OF_CALLS; i++)
73 RTTimeNanoTS();
74
75 uint64_t u64RTElapsedTS = RTTimeNanoTS();
76 uint64_t u64OSElapsedTS = RTTimeSystemNanoTS();
77 u64RTElapsedTS -= u64RTStartTS;
78 u64OSElapsedTS -= u64OSStartTS;
79 int64_t i64Diff = u64OSElapsedTS >= u64RTElapsedTS ? u64OSElapsedTS - u64RTElapsedTS : u64RTElapsedTS - u64OSElapsedTS;
80 if (i64Diff > (int64_t)(u64OSElapsedTS / 1000))
81 {
82 RTPrintf("tstTime-2: error: total time differs too much! u64OSElapsedTS=%#llx u64RTElapsedTS=%#llx delta=%lld\n",
83 u64OSElapsedTS, u64RTElapsedTS, u64OSElapsedTS - u64RTElapsedTS);
84 cErrors++;
85 }
86 else
87 RTPrintf("tstTime-2: total time difference: u64OSElapsedTS=%#llx u64RTElapsedTS=%#llx delta=%lld\n",
88 u64OSElapsedTS, u64RTElapsedTS, u64OSElapsedTS - u64RTElapsedTS);
89
90 RTPrintf("tstTime-2: %'u calls to RTTimeNanoTS in %'lluns -> %'llu calls/sec (%s)\n",
91 NUMBER_OF_CALLS, u64RTElapsedTS, (uint64_t)(NUMBER_OF_CALLS / ((double)u64RTElapsedTS / RT_NS_1SEC)), RTTimeNanoTSWorkerName());
92#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86) /** @todo This isn't really x86 or AMD64 specific... */
93 RTPrintf("tstTime-2: RTTimeDbgSteps -> %u (%d ppt)\n", RTTimeDbgSteps(), ((uint64_t)RTTimeDbgSteps() * 1000) / NUMBER_OF_CALLS);
94 RTPrintf("tstTime-2: RTTimeDbgExpired -> %u (%d ppt)\n", RTTimeDbgExpired(), ((uint64_t)RTTimeDbgExpired() * 1000) / NUMBER_OF_CALLS);
95 RTPrintf("tstTime-2: RTTimeDbgBad -> %u (%d ppt)\n", RTTimeDbgBad(), ((uint64_t)RTTimeDbgBad() * 1000) / NUMBER_OF_CALLS);
96 RTPrintf("tstTime-2: RTTimeDbgRaces -> %u (%d ppt)\n", RTTimeDbgRaces(), ((uint64_t)RTTimeDbgRaces() * 1000) / NUMBER_OF_CALLS);
97#endif
98
99 if (!cErrors)
100 RTPrintf("tstTime-2: SUCCESS\n");
101 else
102 RTPrintf("tstTime-2: FAILURE - %d errors\n", cErrors);
103 return !!cErrors;
104}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette