1 | /* $Id: time-darwin.cpp 1 1970-01-01 00:00:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * InnoTek Portable Runtime - Time, Darwin.
|
---|
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 RTTIME_INCL_TIMEVAL
|
---|
28 | #include <mach/mach_time.h>
|
---|
29 | #include <mach/kern_return.h>
|
---|
30 | #include <sys/time.h>
|
---|
31 | #include <time.h>
|
---|
32 |
|
---|
33 | #include <iprt/time.h>
|
---|
34 | #include "internal/time.h"
|
---|
35 |
|
---|
36 |
|
---|
37 | DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
|
---|
38 | {
|
---|
39 | static struct mach_timebase_info s_Info = { 0, 0 };
|
---|
40 | static double s_rdFactor = 0.0;
|
---|
41 |
|
---|
42 | /* get the factors the first time (pray we're not racing anyone) */
|
---|
43 | if (s_Info.denom == 0)
|
---|
44 | {
|
---|
45 | static bool s_fFailedToGetTimeBaseInfo = false;
|
---|
46 | if (!s_fFailedToGetTimeBaseInfo)
|
---|
47 | {
|
---|
48 | struct mach_timebase_info Info;
|
---|
49 | if (mach_timebase_info(&Info) != KERN_SUCCESS)
|
---|
50 | {
|
---|
51 | s_rdFactor = (double)Info.numer / (double)Info.denom;
|
---|
52 | s_Info = Info;
|
---|
53 | }
|
---|
54 | else
|
---|
55 | {
|
---|
56 | s_Info.denom = s_Info.numer = 0;
|
---|
57 | s_fFailedToGetTimeBaseInfo = true;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | /* special case: absolute time is in nanoseconds */
|
---|
63 | if (s_Info.denom == 1 && s_Info.numer == 1)
|
---|
64 | return mach_absolute_time();
|
---|
65 |
|
---|
66 | /* general case: multiply by factor to get nanoseconds. */
|
---|
67 | if (s_rdFactor != 0.0)
|
---|
68 | return mach_absolute_time() * s_rdFactor;
|
---|
69 |
|
---|
70 | /* worst case: fallback to gettimeofday(). */
|
---|
71 | struct timeval tv;
|
---|
72 | gettimeofday(&tv, NULL);
|
---|
73 | return (uint64_t)tv.tv_sec * (uint64_t)(1000 * 1000 * 1000)
|
---|
74 | + (uint64_t)(tv.tv_usec * 1000);
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Gets the current nanosecond timestamp.
|
---|
80 | *
|
---|
81 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
82 | * resolution or performance optimizations.
|
---|
83 | *
|
---|
84 | * @returns nanosecond timestamp.
|
---|
85 | */
|
---|
86 | RTDECL(uint64_t) RTTimeSystemNanoTS(void)
|
---|
87 | {
|
---|
88 | return rtTimeGetSystemNanoTS();
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Gets the current millisecond timestamp.
|
---|
94 | *
|
---|
95 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
96 | * resolution or performance optimizations.
|
---|
97 | *
|
---|
98 | * @returns millisecond timestamp.
|
---|
99 | */
|
---|
100 | RTDECL(uint64_t) RTTimeSystemMilliTS(void)
|
---|
101 | {
|
---|
102 | return rtTimeGetSystemNanoTS();
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Gets the current system time.
|
---|
108 | *
|
---|
109 | * @returns pTime.
|
---|
110 | * @param pTime Where to store the time.
|
---|
111 | */
|
---|
112 | RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime)
|
---|
113 | {
|
---|
114 | /** @todo find nanosecond API for getting time of day. */
|
---|
115 | struct timeval tv;
|
---|
116 | gettimeofday(&tv, NULL);
|
---|
117 | return RTTimeSpecSetTimeval(pTime, &tv);
|
---|
118 | }
|
---|
119 |
|
---|