1 | /* $Id: time-r0drv-darwin.cpp 8155 2008-04-18 15:16:47Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Time, Ring-0 Driver, Darwin.
|
---|
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 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #define LOG_GROUP RTLOGGROUP_TIME
|
---|
36 | #include "the-darwin-kernel.h"
|
---|
37 | #include <iprt/time.h>
|
---|
38 | #include <iprt/asm.h>
|
---|
39 |
|
---|
40 |
|
---|
41 | DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
|
---|
42 | {
|
---|
43 | static int8_t s_fSimple = -1;
|
---|
44 |
|
---|
45 | /* first call: check if life is simple or not. */
|
---|
46 | if (s_fSimple < 0)
|
---|
47 | {
|
---|
48 | struct mach_timebase_info Info;
|
---|
49 | clock_timebase_info(&Info);
|
---|
50 | ASMAtomicXchgS8((int8_t * volatile)&s_fSimple, Info.denom == 1 && Info.numer == 1);
|
---|
51 | }
|
---|
52 |
|
---|
53 | /* special case: absolute time is in nanoseconds */
|
---|
54 | if (s_fSimple)
|
---|
55 | return mach_absolute_time();
|
---|
56 |
|
---|
57 | /* general case: let mach do the mult/div for us. */
|
---|
58 | uint64_t u64;
|
---|
59 | absolutetime_to_nanoseconds(mach_absolute_time(), &u64);
|
---|
60 | return u64;
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Gets the current nanosecond timestamp.
|
---|
66 | *
|
---|
67 | * @returns nanosecond timestamp.
|
---|
68 | */
|
---|
69 | RTDECL(uint64_t) RTTimeNanoTS(void)
|
---|
70 | {
|
---|
71 | return rtTimeGetSystemNanoTS();
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Gets the current millisecond timestamp.
|
---|
77 | *
|
---|
78 | * @returns millisecond timestamp.
|
---|
79 | */
|
---|
80 | RTDECL(uint64_t) RTTimeMilliTS(void)
|
---|
81 | {
|
---|
82 | return rtTimeGetSystemNanoTS() / 1000000;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Gets the current nanosecond timestamp.
|
---|
88 | *
|
---|
89 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
90 | * resolution or performance optimizations.
|
---|
91 | *
|
---|
92 | * @returns nanosecond timestamp.
|
---|
93 | */
|
---|
94 | RTDECL(uint64_t) RTTimeSystemNanoTS(void)
|
---|
95 | {
|
---|
96 | return rtTimeGetSystemNanoTS();
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Gets the current millisecond timestamp.
|
---|
102 | *
|
---|
103 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
104 | * resolution or performance optimizations.
|
---|
105 | *
|
---|
106 | * @returns millisecond timestamp.
|
---|
107 | */
|
---|
108 | RTDECL(uint64_t) RTTimeSystemMilliTS(void)
|
---|
109 | {
|
---|
110 | return rtTimeGetSystemNanoTS() / 1000000;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Gets the current system time.
|
---|
116 | *
|
---|
117 | * @returns pTime.
|
---|
118 | * @param pTime Where to store the time.
|
---|
119 | */
|
---|
120 | RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime)
|
---|
121 | {
|
---|
122 | uint32_t u32Secs;
|
---|
123 | uint32_t u32Nanosecs;
|
---|
124 | clock_get_calendar_nanotime(&u32Secs, &u32Nanosecs);
|
---|
125 | return RTTimeSpecSetNano(pTime, (uint64_t)u32Secs * 1000000000 + u32Nanosecs);
|
---|
126 | }
|
---|
127 |
|
---|