VirtualBox

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

Last change on this file since 105589 was 104105, checked in by vboxsync, 8 months ago

Runtime/darwin/time-darwin.cpp: Avoid using double-precision floating point while computing nano timestamp.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.3 KB
Line 
1/* $Id: time-darwin.cpp 104105 2024-03-28 13:33:58Z 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 <iprt/asm-math.h>
51#include "internal/time.h"
52
53
54/*********************************************************************************************************************************
55* Global Variables *
56*********************************************************************************************************************************/
57static struct mach_timebase_info g_Info = { 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 g_Info = Info;
69 else
70 {
71 g_fFailedToGetTimeBaseInfo = true;
72 Assert(g_Info.denom == 0 && g_Info.numer == 0);
73 }
74}
75
76
77/**
78 * Internal worker.
79 * @returns Nanosecond timestamp.
80 */
81DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
82{
83 /* Lazy init. */
84 if (RT_UNLIKELY(g_Info.denom == 0 && !g_fFailedToGetTimeBaseInfo))
85 rtTimeDarwinLazyInit();
86
87 /* special case: absolute time is in nanoseconds. */
88 if (g_Info.denom == 1 && g_Info.numer == 1)
89 return mach_absolute_time();
90
91 /* general case: multiply by factor to get nanoseconds. */
92 if (g_Info.denom > 0 && g_Info.numer > 0)
93 {
94 /*
95 * We could use clock_gettime_nsec_np(CLOCK_UPTIME_RAW) but internally
96 * it might be calling mach_timebase_info() for each call. This is most
97 * likely faster since we avoid calling mach_timebase_info() here.
98 */
99 uint64_t const cTicks = mach_absolute_time();
100 return ASMMultU64ByU32DivByU32(cTicks, g_Info.numer, g_Info.denom);
101 }
102
103 /* worst case: fallback to gettimeofday(). */
104 struct timeval tv;
105 gettimeofday(&tv, NULL);
106 return (uint64_t)tv.tv_sec * RT_NS_1SEC_64
107 + (uint64_t)(tv.tv_usec * RT_NS_1US);
108}
109
110
111RTDECL(uint64_t) RTTimeSystemNanoTS(void)
112{
113 return rtTimeGetSystemNanoTS();
114}
115
116
117RTDECL(uint64_t) RTTimeSystemMilliTS(void)
118{
119 return rtTimeGetSystemNanoTS() / RT_NS_1MS;
120}
121
122
123RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime)
124{
125 /** @todo find nanosecond API for getting time of day. */
126 struct timeval tv;
127 gettimeofday(&tv, NULL);
128 return RTTimeSpecSetTimeval(pTime, &tv);
129}
130
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette