[4750] | 1 | /* $Id: time-darwin.cpp 104105 2024-03-28 13:33:58Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - Time, Darwin.
|
---|
[4750] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[4071] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 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 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[5999] | 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.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[4750] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[4750] | 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>
|
---|
[15870] | 49 | #include <iprt/assert.h>
|
---|
[104105] | 50 | #include <iprt/asm-math.h>
|
---|
[4750] | 51 | #include "internal/time.h"
|
---|
| 52 |
|
---|
| 53 |
|
---|
[57358] | 54 | /*********************************************************************************************************************************
|
---|
| 55 | * Global Variables *
|
---|
| 56 | *********************************************************************************************************************************/
|
---|
[15870] | 57 | static struct mach_timebase_info g_Info = { 0, 0 };
|
---|
| 58 | static bool g_fFailedToGetTimeBaseInfo = false;
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * Perform lazy init (pray we're not racing anyone in a bad way).
|
---|
| 63 | */
|
---|
| 64 | static void rtTimeDarwinLazyInit(void)
|
---|
[4750] | 65 | {
|
---|
[15870] | 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;
|
---|
[104105] | 72 | Assert(g_Info.denom == 0 && g_Info.numer == 0);
|
---|
[15870] | 73 | }
|
---|
| 74 | }
|
---|
[4750] | 75 |
|
---|
[15870] | 76 |
|
---|
| 77 | /**
|
---|
| 78 | * Internal worker.
|
---|
| 79 | * @returns Nanosecond timestamp.
|
---|
| 80 | */
|
---|
| 81 | DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
|
---|
| 82 | {
|
---|
| 83 | /* Lazy init. */
|
---|
| 84 | if (RT_UNLIKELY(g_Info.denom == 0 && !g_fFailedToGetTimeBaseInfo))
|
---|
| 85 | rtTimeDarwinLazyInit();
|
---|
| 86 |
|
---|
[104105] | 87 | /* special case: absolute time is in nanoseconds. */
|
---|
[15870] | 88 | if (g_Info.denom == 1 && g_Info.numer == 1)
|
---|
[4750] | 89 | return mach_absolute_time();
|
---|
| 90 |
|
---|
| 91 | /* general case: multiply by factor to get nanoseconds. */
|
---|
[104105] | 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 | }
|
---|
[4750] | 102 |
|
---|
| 103 | /* worst case: fallback to gettimeofday(). */
|
---|
| 104 | struct timeval tv;
|
---|
| 105 | gettimeofday(&tv, NULL);
|
---|
[52822] | 106 | return (uint64_t)tv.tv_sec * RT_NS_1SEC_64
|
---|
| 107 | + (uint64_t)(tv.tv_usec * RT_NS_1US);
|
---|
[4750] | 108 | }
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | RTDECL(uint64_t) RTTimeSystemNanoTS(void)
|
---|
| 112 | {
|
---|
| 113 | return rtTimeGetSystemNanoTS();
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 | RTDECL(uint64_t) RTTimeSystemMilliTS(void)
|
---|
| 118 | {
|
---|
[52822] | 119 | return rtTimeGetSystemNanoTS() / RT_NS_1MS;
|
---|
[4750] | 120 | }
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | RTDECL(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 |
|
---|