1 | /* $Id: time-linux.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Time, POSIX.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 | int rc;
|
---|
85 | #ifdef CLOCK_MONOTONIC
|
---|
86 | /*
|
---|
87 | * Real-Time API.
|
---|
88 | */
|
---|
89 | rc = clock_gettime(CLOCK_MONOTONIC, ts);
|
---|
90 | if (!rc)
|
---|
91 | {
|
---|
92 | iWorking = 0;
|
---|
93 | return 0;
|
---|
94 | }
|
---|
95 |
|
---|
96 | rc = sys_clock_gettime(CLOCK_MONOTONIC, ts);
|
---|
97 | if (!rc)
|
---|
98 | {
|
---|
99 | iWorking = 1;
|
---|
100 | return 0;
|
---|
101 | }
|
---|
102 | #endif /* CLOCK_MONOTONIC */
|
---|
103 |
|
---|
104 | /* give up */
|
---|
105 | iWorking = -2;
|
---|
106 | break;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | return -1;
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
|
---|
114 | {
|
---|
115 | /* check monotonic clock first. */
|
---|
116 | static bool fMonoClock = true;
|
---|
117 | if (fMonoClock)
|
---|
118 | {
|
---|
119 | struct timespec ts;
|
---|
120 | if (!mono_clock(&ts))
|
---|
121 | return (uint64_t)ts.tv_sec * (uint64_t)(1000 * 1000 * 1000)
|
---|
122 | + ts.tv_nsec;
|
---|
123 | fMonoClock = false;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /* fallback to gettimeofday(). */
|
---|
127 | struct timeval tv;
|
---|
128 | gettimeofday(&tv, NULL);
|
---|
129 | return (uint64_t)tv.tv_sec * (uint64_t)(1000 * 1000 * 1000)
|
---|
130 | + (uint64_t)(tv.tv_usec * 1000);
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Gets the current nanosecond timestamp.
|
---|
136 | *
|
---|
137 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
138 | * resolution or performance optimizations.
|
---|
139 | *
|
---|
140 | * @returns nanosecond timestamp.
|
---|
141 | */
|
---|
142 | RTDECL(uint64_t) RTTimeSystemNanoTS(void)
|
---|
143 | {
|
---|
144 | return rtTimeGetSystemNanoTS();
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Gets the current millisecond timestamp.
|
---|
150 | *
|
---|
151 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
152 | * resolution or performance optimizations.
|
---|
153 | *
|
---|
154 | * @returns millisecond timestamp.
|
---|
155 | */
|
---|
156 | RTDECL(uint64_t) RTTimeSystemMilliTS(void)
|
---|
157 | {
|
---|
158 | return rtTimeGetSystemNanoTS() / 1000000;
|
---|
159 | }
|
---|
160 |
|
---|