1 | /* $Id: tstTime-3.cpp 48935 2013-10-07 21:19:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Simple RTTime test.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2011 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 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #ifdef RT_OS_WINDOWS
|
---|
31 | # include <Windows.h>
|
---|
32 |
|
---|
33 | #else /* posix */
|
---|
34 | # include <sys/time.h>
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | #include <iprt/time.h>
|
---|
38 | #include <iprt/stream.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include <iprt/initterm.h>
|
---|
41 | #include <iprt/thread.h>
|
---|
42 | #include <iprt/err.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | DECLINLINE(uint64_t) OSNanoTS(void)
|
---|
46 | {
|
---|
47 | #ifdef RT_OS_WINDOWS
|
---|
48 | uint64_t u64; /* manual say larger integer, should be safe to assume it's the same. */
|
---|
49 | GetSystemTimeAsFileTime((LPFILETIME)&u64);
|
---|
50 | return u64 * 100;
|
---|
51 |
|
---|
52 | #else /* posix */
|
---|
53 |
|
---|
54 | struct timeval tv;
|
---|
55 | gettimeofday(&tv, NULL);
|
---|
56 | return (uint64_t)tv.tv_sec * (uint64_t)(1000 * 1000 * 1000)
|
---|
57 | + (uint64_t)(tv.tv_usec * 1000);
|
---|
58 | #endif
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 | int main(int argc, char **argv)
|
---|
64 | {
|
---|
65 | RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB);
|
---|
66 |
|
---|
67 | if (argc <= 1)
|
---|
68 | {
|
---|
69 | RTPrintf("tstTime-3: usage: tstTime-3 <seconds> [seconds2 [..]]\n");
|
---|
70 | return 1;
|
---|
71 | }
|
---|
72 |
|
---|
73 | RTPrintf("tstTime-3: Testing difference between RTTimeNanoTS() and OS time...\n");
|
---|
74 |
|
---|
75 | for (int i = 1; i < argc; i++)
|
---|
76 | {
|
---|
77 | uint64_t cSeconds = 0;
|
---|
78 | int rc = RTStrToUInt64Ex(argv[i], NULL, 0, &cSeconds);
|
---|
79 | if (RT_FAILURE(rc))
|
---|
80 | {
|
---|
81 | RTPrintf("tstTime-3: Invalid argument %d: %s\n", i, argv[i]);
|
---|
82 | return 1;
|
---|
83 | }
|
---|
84 | RTPrintf("tstTime-3: %d - %RU64 seconds period...\n", i, cSeconds);
|
---|
85 |
|
---|
86 | RTTimeNanoTS(); OSNanoTS(); RTThreadSleep(1);
|
---|
87 | uint64_t u64RTStartTS = RTTimeNanoTS();
|
---|
88 | uint64_t u64OSStartTS = OSNanoTS();
|
---|
89 |
|
---|
90 | RTThreadSleep(cSeconds * 1000);
|
---|
91 |
|
---|
92 | uint64_t u64RTElapsedTS = RTTimeNanoTS();
|
---|
93 | uint64_t u64OSElapsedTS = OSNanoTS();
|
---|
94 | u64RTElapsedTS -= u64RTStartTS;
|
---|
95 | u64OSElapsedTS -= u64OSStartTS;
|
---|
96 |
|
---|
97 | RTPrintf("tstTime-3: %d - RT: %16RU64 ns\n", i, u64RTElapsedTS);
|
---|
98 | RTPrintf("tstTime-3: %d - OS: %16RU64 ns\n", i, u64OSElapsedTS);
|
---|
99 | RTPrintf("tstTime-3: %d - diff: %16RI64 ns\n", i, u64RTElapsedTS - u64OSElapsedTS);
|
---|
100 | }
|
---|
101 |
|
---|
102 | return 0;
|
---|
103 | }
|
---|