1 | /* $Id: time-linux.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Time, POSIX.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_TIME
|
---|
32 | #define RTTIME_INCL_TIMEVAL
|
---|
33 | #include <sys/time.h>
|
---|
34 | #include <time.h>
|
---|
35 | #include <sys/syscall.h>
|
---|
36 | #include <unistd.h>
|
---|
37 | #ifndef __NR_clock_gettime
|
---|
38 | # define __NR_timer_create 259
|
---|
39 | # define __NR_clock_gettime (__NR_timer_create+6)
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #include <iprt/time.h>
|
---|
43 | #include "internal/time.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | DECLINLINE(int) sys_clock_gettime(clockid_t id, struct timespec *ts)
|
---|
47 | {
|
---|
48 | int rc = syscall(__NR_clock_gettime, id, ts);
|
---|
49 | if (rc >= 0)
|
---|
50 | return rc;
|
---|
51 | return -1;
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Wrapper around various monotone time sources.
|
---|
57 | */
|
---|
58 | DECLINLINE(int) mono_clock(struct timespec *ts)
|
---|
59 | {
|
---|
60 | static int iWorking = -1;
|
---|
61 | switch (iWorking)
|
---|
62 | {
|
---|
63 | #ifdef CLOCK_MONOTONIC
|
---|
64 | /*
|
---|
65 | * Standard clock_gettime()
|
---|
66 | */
|
---|
67 | case 0:
|
---|
68 | return clock_gettime(CLOCK_MONOTONIC, ts);
|
---|
69 |
|
---|
70 | /*
|
---|
71 | * Syscall clock_gettime().
|
---|
72 | */
|
---|
73 | case 1:
|
---|
74 | return sys_clock_gettime(CLOCK_MONOTONIC, ts);
|
---|
75 |
|
---|
76 | #endif /* CLOCK_MONOTONIC */
|
---|
77 |
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * Figure out what's working.
|
---|
81 | */
|
---|
82 | case -1:
|
---|
83 | {
|
---|
84 | #ifdef CLOCK_MONOTONIC
|
---|
85 | /*
|
---|
86 | * Real-Time API.
|
---|
87 | */
|
---|
88 | int rc = clock_gettime(CLOCK_MONOTONIC, ts);
|
---|
89 | if (!rc)
|
---|
90 | {
|
---|
91 | iWorking = 0;
|
---|
92 | return 0;
|
---|
93 | }
|
---|
94 |
|
---|
95 | rc = sys_clock_gettime(CLOCK_MONOTONIC, ts);
|
---|
96 | if (!rc)
|
---|
97 | {
|
---|
98 | iWorking = 1;
|
---|
99 | return 0;
|
---|
100 | }
|
---|
101 | #endif /* CLOCK_MONOTONIC */
|
---|
102 |
|
---|
103 | /* give up */
|
---|
104 | iWorking = -2;
|
---|
105 | break;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | return -1;
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
|
---|
113 | {
|
---|
114 | /* check monotonic clock first. */
|
---|
115 | static bool fMonoClock = true;
|
---|
116 | if (fMonoClock)
|
---|
117 | {
|
---|
118 | struct timespec ts;
|
---|
119 | if (!mono_clock(&ts))
|
---|
120 | return (uint64_t)ts.tv_sec * RT_NS_1SEC_64
|
---|
121 | + ts.tv_nsec;
|
---|
122 | fMonoClock = false;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /* fallback to gettimeofday(). */
|
---|
126 | struct timeval tv;
|
---|
127 | gettimeofday(&tv, NULL);
|
---|
128 | return (uint64_t)tv.tv_sec * RT_NS_1SEC_64
|
---|
129 | + (uint64_t)(tv.tv_usec * RT_NS_1US);
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Gets the current nanosecond timestamp.
|
---|
135 | *
|
---|
136 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
137 | * resolution or performance optimizations.
|
---|
138 | *
|
---|
139 | * @returns nanosecond timestamp.
|
---|
140 | */
|
---|
141 | RTDECL(uint64_t) RTTimeSystemNanoTS(void)
|
---|
142 | {
|
---|
143 | return rtTimeGetSystemNanoTS();
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Gets the current millisecond timestamp.
|
---|
149 | *
|
---|
150 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
151 | * resolution or performance optimizations.
|
---|
152 | *
|
---|
153 | * @returns millisecond timestamp.
|
---|
154 | */
|
---|
155 | RTDECL(uint64_t) RTTimeSystemMilliTS(void)
|
---|
156 | {
|
---|
157 | return rtTimeGetSystemNanoTS() / RT_NS_1MS;
|
---|
158 | }
|
---|
159 |
|
---|