1 | /* $Id: tstTime-3.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Simple RTTime test.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #ifdef RT_OS_WINDOWS
|
---|
32 | # include <iprt/win/windows.h>
|
---|
33 |
|
---|
34 | #else /* posix */
|
---|
35 | # include <sys/time.h>
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | #include <iprt/time.h>
|
---|
39 | #include <iprt/stream.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include <iprt/initterm.h>
|
---|
42 | #include <iprt/thread.h>
|
---|
43 | #include <iprt/err.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | DECLINLINE(uint64_t) OSNanoTS(void)
|
---|
47 | {
|
---|
48 | #ifdef RT_OS_WINDOWS
|
---|
49 | uint64_t u64; /* manual say larger integer, should be safe to assume it's the same. */
|
---|
50 | GetSystemTimeAsFileTime((LPFILETIME)&u64);
|
---|
51 | return u64 * 100;
|
---|
52 |
|
---|
53 | #else /* posix */
|
---|
54 |
|
---|
55 | struct timeval tv;
|
---|
56 | gettimeofday(&tv, NULL);
|
---|
57 | return (uint64_t)tv.tv_sec * (uint64_t)(1000 * 1000 * 1000)
|
---|
58 | + (uint64_t)(tv.tv_usec * 1000);
|
---|
59 | #endif
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 | int main(int argc, char **argv)
|
---|
65 | {
|
---|
66 | RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB);
|
---|
67 |
|
---|
68 | if (argc <= 1)
|
---|
69 | {
|
---|
70 | RTPrintf("tstTime-3: usage: tstTime-3 <seconds> [seconds2 [..]]\n");
|
---|
71 | return 1;
|
---|
72 | }
|
---|
73 |
|
---|
74 | RTPrintf("tstTime-3: Testing difference between RTTimeNanoTS() and OS time...\n");
|
---|
75 |
|
---|
76 | for (int i = 1; i < argc; i++)
|
---|
77 | {
|
---|
78 | uint64_t cSeconds = 0;
|
---|
79 | int rc = RTStrToUInt64Ex(argv[i], NULL, 0, &cSeconds);
|
---|
80 | if (RT_FAILURE(rc))
|
---|
81 | {
|
---|
82 | RTPrintf("tstTime-3: Invalid argument %d: %s\n", i, argv[i]);
|
---|
83 | return 1;
|
---|
84 | }
|
---|
85 | RTPrintf("tstTime-3: %d - %RU64 seconds period...\n", i, cSeconds);
|
---|
86 |
|
---|
87 | RTTimeNanoTS(); OSNanoTS(); RTThreadSleep(1);
|
---|
88 | uint64_t u64RTStartTS = RTTimeNanoTS();
|
---|
89 | uint64_t u64OSStartTS = OSNanoTS();
|
---|
90 |
|
---|
91 | RTThreadSleep(cSeconds * 1000);
|
---|
92 |
|
---|
93 | uint64_t u64RTElapsedTS = RTTimeNanoTS();
|
---|
94 | uint64_t u64OSElapsedTS = OSNanoTS();
|
---|
95 | u64RTElapsedTS -= u64RTStartTS;
|
---|
96 | u64OSElapsedTS -= u64OSStartTS;
|
---|
97 |
|
---|
98 | RTPrintf("tstTime-3: %d - RT: %16RU64 ns\n", i, u64RTElapsedTS);
|
---|
99 | RTPrintf("tstTime-3: %d - OS: %16RU64 ns\n", i, u64OSElapsedTS);
|
---|
100 | RTPrintf("tstTime-3: %d - diff: %16RI64 ns\n", i, u64RTElapsedTS - u64OSElapsedTS);
|
---|
101 | }
|
---|
102 |
|
---|
103 | return 0;
|
---|
104 | }
|
---|