1 | /* $Id: time-win32.cpp 1 1970-01-01 00:00:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * InnoTek Portable Runtime - Time, win32.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP RTLOGGROUP_TIME
|
---|
27 | //#define USE_TICK_COUNT
|
---|
28 | //#define USE_PERFORMANCE_COUNTER
|
---|
29 | #define USE_FILE_TIME
|
---|
30 | //#define USE_INTERRUPT_TIME
|
---|
31 | #ifndef USE_INTERRUPT_TIME
|
---|
32 | # include <Windows.h>
|
---|
33 | #else
|
---|
34 | # define _X86_
|
---|
35 | extern "C" {
|
---|
36 | # include <ntddk.h>
|
---|
37 | }
|
---|
38 | # undef PAGE_SIZE
|
---|
39 | # undef PAGE_SHIFT
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #include <iprt/time.h>
|
---|
43 | #include <iprt/asm.h>
|
---|
44 | #include "internal/time.h"
|
---|
45 |
|
---|
46 |
|
---|
47 | DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
|
---|
48 | {
|
---|
49 | #if defined USE_TICK_COUNT
|
---|
50 | return (uint64_t)GetTickCount() * (uint64_t)1000000;
|
---|
51 |
|
---|
52 | #elif defined USE_PERFORMANCE_COUNTER
|
---|
53 | static LARGE_INTEGER llFreq;
|
---|
54 | static unsigned uMult;
|
---|
55 | if (!llFreq.QuadPart)
|
---|
56 | {
|
---|
57 | if (!QueryPerformanceFrequency(&llFreq))
|
---|
58 | return (uint64_t)GetTickCount() * (uint64_t)1000000;
|
---|
59 | llFreq.QuadPart /= 1000;
|
---|
60 | uMult = 1000000; /* no math genious, but this seemed to help avoiding floating point. */
|
---|
61 | }
|
---|
62 |
|
---|
63 | LARGE_INTEGER ll;
|
---|
64 | if (QueryPerformanceCounter(&ll))
|
---|
65 | return (ll.QuadPart * uMult) / llFreq.QuadPart;
|
---|
66 | else
|
---|
67 | return (uint64_t)GetTickCount() * (uint64_t)1000000;
|
---|
68 |
|
---|
69 | #elif defined USE_FILE_TIME
|
---|
70 | uint64_t u64; /* manual say larger integer, should be safe to assume it's the same. */
|
---|
71 | GetSystemTimeAsFileTime((LPFILETIME)&u64);
|
---|
72 | return u64 * 100;
|
---|
73 |
|
---|
74 | #elif defined USE_INTERRUPT_TIME
|
---|
75 |
|
---|
76 | /* HACK! HACK! HACK! HACK! HACK! HACK! */
|
---|
77 | /* HACK! HACK! HACK! HACK! HACK! HACK! */
|
---|
78 | /* HACK! HACK! HACK! HACK! HACK! HACK! */
|
---|
79 | # error "don't use this in production"
|
---|
80 |
|
---|
81 | static const KUSER_SHARED_DATA *s_pUserSharedData = NULL;
|
---|
82 | if (!s_pUserSharedData)
|
---|
83 | {
|
---|
84 | /** @todo clever detection algorithm.
|
---|
85 | * The com debugger class exports this too, windbg knows it too... */
|
---|
86 | s_pUserSharedData = (const KUSER_SHARED_DATA *)0x7ffe0000;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /* use interrupt time */
|
---|
90 | LARGE_INTEGER Time;
|
---|
91 | do
|
---|
92 | {
|
---|
93 | Time.HighPart = s_pUserSharedData->InterruptTime.High1Time;
|
---|
94 | Time.LowPart = s_pUserSharedData->InterruptTime.LowPart;
|
---|
95 | } while (s_pUserSharedData->InterruptTime.High2Time != Time.HighPart);
|
---|
96 |
|
---|
97 | return (uint64_t)Time.QuadPart * 100;
|
---|
98 |
|
---|
99 | #else
|
---|
100 | # error "Must select a method bright guy!"
|
---|
101 | #endif
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Gets the current nanosecond timestamp.
|
---|
107 | *
|
---|
108 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
109 | * resolution or performance optimizations.
|
---|
110 | *
|
---|
111 | * @returns nanosecond timestamp.
|
---|
112 | */
|
---|
113 | RTDECL(uint64_t) RTTimeSystemNanoTS(void)
|
---|
114 | {
|
---|
115 | return rtTimeGetSystemNanoTS();
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Gets the current millisecond timestamp.
|
---|
121 | *
|
---|
122 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
123 | * resolution or performance optimizations.
|
---|
124 | *
|
---|
125 | * @returns millisecond timestamp.
|
---|
126 | */
|
---|
127 | RTDECL(uint64_t) RTTimeSystemMilliTS(void)
|
---|
128 | {
|
---|
129 | return rtTimeGetSystemNanoTS();
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Gets the current system time.
|
---|
135 | *
|
---|
136 | * @returns pTime.
|
---|
137 | * @param pTime Where to store the time.
|
---|
138 | */
|
---|
139 | RTR3DECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime)
|
---|
140 | {
|
---|
141 | uint64_t u64; /* manual say larger integer, should be safe to assume it's the same. */
|
---|
142 | GetSystemTimeAsFileTime((LPFILETIME)&u64);
|
---|
143 | return RTTimeSpecSetNtTime(pTime, u64);
|
---|
144 | }
|
---|
145 |
|
---|