VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/linux/time-linux.cpp@ 103795

Last change on this file since 103795 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.4 KB
Line 
1/* $Id: time-linux.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#include <sys/syscall.h>
46#include <unistd.h>
47#ifndef __NR_clock_gettime
48# define __NR_timer_create 259
49# define __NR_clock_gettime (__NR_timer_create+6)
50#endif
51
52#include <iprt/time.h>
53#include "internal/time.h"
54
55
56DECLINLINE(int) sys_clock_gettime(clockid_t id, struct timespec *ts)
57{
58 int rc = syscall(__NR_clock_gettime, id, ts);
59 if (rc >= 0)
60 return rc;
61 return -1;
62}
63
64
65/**
66 * Wrapper around various monotone time sources.
67 */
68DECLINLINE(int) mono_clock(struct timespec *ts)
69{
70 static int iWorking = -1;
71 switch (iWorking)
72 {
73#ifdef CLOCK_MONOTONIC
74 /*
75 * Standard clock_gettime()
76 */
77 case 0:
78 return clock_gettime(CLOCK_MONOTONIC, ts);
79
80 /*
81 * Syscall clock_gettime().
82 */
83 case 1:
84 return sys_clock_gettime(CLOCK_MONOTONIC, ts);
85
86#endif /* CLOCK_MONOTONIC */
87
88
89 /*
90 * Figure out what's working.
91 */
92 case -1:
93 {
94#ifdef CLOCK_MONOTONIC
95 /*
96 * Real-Time API.
97 */
98 int rc = clock_gettime(CLOCK_MONOTONIC, ts);
99 if (!rc)
100 {
101 iWorking = 0;
102 return 0;
103 }
104
105 rc = sys_clock_gettime(CLOCK_MONOTONIC, ts);
106 if (!rc)
107 {
108 iWorking = 1;
109 return 0;
110 }
111#endif /* CLOCK_MONOTONIC */
112
113 /* give up */
114 iWorking = -2;
115 break;
116 }
117 }
118 return -1;
119}
120
121
122DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
123{
124 /* check monotonic clock first. */
125 static bool fMonoClock = true;
126 if (fMonoClock)
127 {
128 struct timespec ts;
129 if (!mono_clock(&ts))
130 return (uint64_t)ts.tv_sec * RT_NS_1SEC_64
131 + ts.tv_nsec;
132 fMonoClock = false;
133 }
134
135 /* fallback to gettimeofday(). */
136 struct timeval tv;
137 gettimeofday(&tv, NULL);
138 return (uint64_t)tv.tv_sec * RT_NS_1SEC_64
139 + (uint64_t)(tv.tv_usec * RT_NS_1US);
140}
141
142
143/**
144 * Gets the current nanosecond timestamp.
145 *
146 * This differs from RTTimeNanoTS in that it will use system APIs and not do any
147 * resolution or performance optimizations.
148 *
149 * @returns nanosecond timestamp.
150 */
151RTDECL(uint64_t) RTTimeSystemNanoTS(void)
152{
153 return rtTimeGetSystemNanoTS();
154}
155
156
157/**
158 * Gets the current millisecond timestamp.
159 *
160 * This differs from RTTimeNanoTS in that it will use system APIs and not do any
161 * resolution or performance optimizations.
162 *
163 * @returns millisecond timestamp.
164 */
165RTDECL(uint64_t) RTTimeSystemMilliTS(void)
166{
167 return rtTimeGetSystemNanoTS() / RT_NS_1MS;
168}
169
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