1 | /* $Id: time-posix.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Time, POSIX.
|
---|
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 <sys/time.h>
|
---|
44 | #include <time.h>
|
---|
45 |
|
---|
46 | #include <iprt/time.h>
|
---|
47 | #include "internal/time.h"
|
---|
48 |
|
---|
49 |
|
---|
50 | DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
|
---|
51 | {
|
---|
52 | #if defined(CLOCK_MONOTONIC) && !defined(RT_OS_L4) && !defined(RT_OS_OS2)
|
---|
53 | /* check monotonic clock first. */
|
---|
54 | static bool s_fMonoClock = true;
|
---|
55 | if (s_fMonoClock)
|
---|
56 | {
|
---|
57 | struct timespec ts;
|
---|
58 | if (!clock_gettime(CLOCK_MONOTONIC, &ts))
|
---|
59 | return (uint64_t)ts.tv_sec * RT_NS_1SEC_64
|
---|
60 | + ts.tv_nsec;
|
---|
61 | s_fMonoClock = false;
|
---|
62 | }
|
---|
63 | #endif
|
---|
64 |
|
---|
65 | /* fallback to gettimeofday(). */
|
---|
66 | struct timeval tv;
|
---|
67 | gettimeofday(&tv, NULL);
|
---|
68 | return (uint64_t)tv.tv_sec * RT_NS_1SEC_64
|
---|
69 | + (uint64_t)(tv.tv_usec * RT_NS_1US);
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Gets the current nanosecond timestamp.
|
---|
75 | *
|
---|
76 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
77 | * resolution or performance optimizations.
|
---|
78 | *
|
---|
79 | * @returns nanosecond timestamp.
|
---|
80 | */
|
---|
81 | RTDECL(uint64_t) RTTimeSystemNanoTS(void)
|
---|
82 | {
|
---|
83 | return rtTimeGetSystemNanoTS();
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Gets the current millisecond timestamp.
|
---|
89 | *
|
---|
90 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
91 | * resolution or performance optimizations.
|
---|
92 | *
|
---|
93 | * @returns millisecond timestamp.
|
---|
94 | */
|
---|
95 | RTDECL(uint64_t) RTTimeSystemMilliTS(void)
|
---|
96 | {
|
---|
97 | return rtTimeGetSystemNanoTS() / RT_NS_1MS;
|
---|
98 | }
|
---|
99 |
|
---|