VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/darwin/time-darwin.cpp@ 99127

Last change on this file since 99127 was 99127, checked in by vboxsync, 14 months ago

Revert r156473 as clock_gettime_nsec_np(CLOCK_UPTIME_RAW) is apparently doing the same as we do just not as efficient (calls mach_timebase_info() every time)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.0 KB
Line 
1/* $Id: time-darwin.cpp 99127 2023-03-23 08:23:39Z vboxsync $ */
2/** @file
3 * IPRT - Time, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_TIME
42#define RTTIME_INCL_TIMEVAL
43#include <mach/mach_time.h>
44#include <mach/kern_return.h>
45#include <sys/time.h>
46#include <time.h>
47
48#include <iprt/time.h>
49#include <iprt/assert.h>
50#include "internal/time.h"
51
52
53/*********************************************************************************************************************************
54* Global Variables *
55*********************************************************************************************************************************/
56static struct mach_timebase_info g_Info = { 0, 0 };
57static double g_rdFactor = 0.0;
58static bool g_fFailedToGetTimeBaseInfo = false;
59
60
61/**
62 * Perform lazy init (pray we're not racing anyone in a bad way).
63 */
64static void rtTimeDarwinLazyInit(void)
65{
66 struct mach_timebase_info Info;
67 if (mach_timebase_info(&Info) == KERN_SUCCESS)
68 {
69 g_rdFactor = (double)Info.numer / (double)Info.denom;
70 g_Info = Info;
71 }
72 else
73 {
74 g_fFailedToGetTimeBaseInfo = true;
75 Assert(g_Info.denom == 0 && g_Info.numer == 0 && g_rdFactor == 0.0);
76 }
77}
78
79
80/**
81 * Internal worker.
82 * @returns Nanosecond timestamp.
83 */
84DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
85{
86 /* Lazy init. */
87 if (RT_UNLIKELY(g_Info.denom == 0 && !g_fFailedToGetTimeBaseInfo))
88 rtTimeDarwinLazyInit();
89
90 /* special case: absolute time is in nanoseconds */
91 if (g_Info.denom == 1 && g_Info.numer == 1)
92 return mach_absolute_time();
93
94 /* general case: multiply by factor to get nanoseconds. */
95 if (g_rdFactor != 0.0)
96 return mach_absolute_time() * g_rdFactor;
97
98 /* worst case: fallback to gettimeofday(). */
99 struct timeval tv;
100 gettimeofday(&tv, NULL);
101 return (uint64_t)tv.tv_sec * RT_NS_1SEC_64
102 + (uint64_t)(tv.tv_usec * RT_NS_1US);
103}
104
105
106RTDECL(uint64_t) RTTimeSystemNanoTS(void)
107{
108 return rtTimeGetSystemNanoTS();
109}
110
111
112RTDECL(uint64_t) RTTimeSystemMilliTS(void)
113{
114 return rtTimeGetSystemNanoTS() / RT_NS_1MS;
115}
116
117
118RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime)
119{
120 /** @todo find nanosecond API for getting time of day. */
121 struct timeval tv;
122 gettimeofday(&tv, NULL);
123 return RTTimeSpecSetTimeval(pTime, &tv);
124}
125
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use