1 | /* $Id: tstTime-3.cpp 8245 2008-04-21 17:24:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Simple RTTime test.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #ifdef RT_OS_WINDOWS
|
---|
35 | # include <Windows.h>
|
---|
36 |
|
---|
37 | #elif defined RT_OS_L4
|
---|
38 |
|
---|
39 | #else /* posix */
|
---|
40 | # include <sys/time.h>
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #include <iprt/time.h>
|
---|
44 | #include <iprt/stream.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 | #include <iprt/runtime.h>
|
---|
47 | #include <iprt/thread.h>
|
---|
48 | #include <iprt/err.h>
|
---|
49 |
|
---|
50 |
|
---|
51 | DECLINLINE(uint64_t) OSNanoTS(void)
|
---|
52 | {
|
---|
53 | #ifdef RT_OS_WINDOWS
|
---|
54 | uint64_t u64; /* manual say larger integer, should be safe to assume it's the same. */
|
---|
55 | GetSystemTimeAsFileTime((LPFILETIME)&u64);
|
---|
56 | return u64 * 100;
|
---|
57 |
|
---|
58 | #elif defined RT_OS_L4
|
---|
59 | /** @todo fix a different timesource on l4. */
|
---|
60 | return RTTimeNanoTS();
|
---|
61 |
|
---|
62 | #else /* posix */
|
---|
63 |
|
---|
64 | struct timeval tv;
|
---|
65 | gettimeofday(&tv, NULL);
|
---|
66 | return (uint64_t)tv.tv_sec * (uint64_t)(1000 * 1000 * 1000)
|
---|
67 | + (uint64_t)(tv.tv_usec * 1000);
|
---|
68 | #endif
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 |
|
---|
73 | int main(int argc, char **argv)
|
---|
74 | {
|
---|
75 | RTR3Init();
|
---|
76 |
|
---|
77 | if (argc <= 1)
|
---|
78 | {
|
---|
79 | RTPrintf("tstTime-3: usage: tstTime-3 <seconds> [seconds2 [..]]\n");
|
---|
80 | return 1;
|
---|
81 | }
|
---|
82 |
|
---|
83 | RTPrintf("tstTime-3: Testing difference between RTTimeNanoTS() and OS time...\n");
|
---|
84 |
|
---|
85 | for (int i = 1; i < argc; i++)
|
---|
86 | {
|
---|
87 | uint64_t cSeconds = 0;
|
---|
88 | int rc = RTStrToUInt64Ex(argv[i], NULL, 0, &cSeconds);
|
---|
89 | if (RT_FAILURE(rc))
|
---|
90 | {
|
---|
91 | RTPrintf("tstTime-3: Invalid argument %d: %s\n", i, argv[i]);
|
---|
92 | return 1;
|
---|
93 | }
|
---|
94 | RTPrintf("tstTime-3: %d - %RU64 seconds period...\n", i, cSeconds);
|
---|
95 |
|
---|
96 | RTTimeNanoTS(); OSNanoTS(); RTThreadSleep(1);
|
---|
97 | uint64_t u64RTStartTS = RTTimeNanoTS();
|
---|
98 | uint64_t u64OSStartTS = OSNanoTS();
|
---|
99 |
|
---|
100 | RTThreadSleep(cSeconds * 1000);
|
---|
101 |
|
---|
102 | uint64_t u64RTElapsedTS = RTTimeNanoTS();
|
---|
103 | uint64_t u64OSElapsedTS = OSNanoTS();
|
---|
104 | u64RTElapsedTS -= u64RTStartTS;
|
---|
105 | u64OSElapsedTS -= u64OSStartTS;
|
---|
106 |
|
---|
107 | RTPrintf("tstTime-3: %d - RT: %16RU64 ns\n", i, u64RTElapsedTS);
|
---|
108 | RTPrintf("tstTime-3: %d - OS: %16RU64 ns\n", i, u64OSElapsedTS);
|
---|
109 | RTPrintf("tstTime-3: %d - diff: %16RI64 ns\n", i, u64RTElapsedTS - u64OSElapsedTS);
|
---|
110 | }
|
---|
111 |
|
---|
112 | return 0;
|
---|
113 | }
|
---|