VirtualBox

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

Last change on this file since 19167 was 8245, checked in by vboxsync, 17 years ago

rebranding: IPRT files again.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1/* $Id: time-linux.cpp 8245 2008-04-21 17:24:28Z vboxsync $ */
2/** @file
3 * IPRT - Time, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#define LOG_GROUP RTLOGGROUP_TIME
36#define RTTIME_INCL_TIMEVAL
37#include <sys/time.h>
38#include <time.h>
39#include <sys/syscall.h>
40#include <unistd.h>
41#ifndef __NR_clock_gettime
42# define __NR_timer_create 259
43# define __NR_clock_gettime (__NR_timer_create+6)
44#endif
45
46#include <iprt/time.h>
47#include "internal/time.h"
48
49
50DECLINLINE(int) sys_clock_gettime(clockid_t id, struct timespec *ts)
51{
52 int rc = syscall(__NR_clock_gettime, id, ts);
53 if (rc >= 0)
54 return rc;
55 return -1;
56}
57
58
59/**
60 * Wrapper around various monotone time sources.
61 */
62DECLINLINE(int) mono_clock(struct timespec *ts)
63{
64 static int iWorking = -1;
65 switch (iWorking)
66 {
67#ifdef CLOCK_MONOTONIC
68 /*
69 * Standard clock_gettime()
70 */
71 case 0:
72 return clock_gettime(CLOCK_MONOTONIC, ts);
73
74 /*
75 * Syscall clock_gettime().
76 */
77 case 1:
78 return sys_clock_gettime(CLOCK_MONOTONIC, ts);
79
80#endif /* CLOCK_MONOTONIC */
81
82
83 /*
84 * Figure out what's working.
85 */
86 case -1:
87 {
88 int rc;
89#ifdef CLOCK_MONOTONIC
90 /*
91 * Real-Time API.
92 */
93 rc = clock_gettime(CLOCK_MONOTONIC, ts);
94 if (!rc)
95 {
96 iWorking = 0;
97 return 0;
98 }
99
100 rc = sys_clock_gettime(CLOCK_MONOTONIC, ts);
101 if (!rc)
102 {
103 iWorking = 1;
104 return 0;
105 }
106#endif /* CLOCK_MONOTONIC */
107
108 /* give up */
109 iWorking = -2;
110 break;
111 }
112 }
113 return -1;
114}
115
116
117DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
118{
119 /* check monotonic clock first. */
120 static bool fMonoClock = true;
121 if (fMonoClock)
122 {
123 struct timespec ts;
124 if (!mono_clock(&ts))
125 return (uint64_t)ts.tv_sec * (uint64_t)(1000 * 1000 * 1000)
126 + ts.tv_nsec;
127 fMonoClock = false;
128 }
129
130 /* fallback to gettimeofday(). */
131 struct timeval tv;
132 gettimeofday(&tv, NULL);
133 return (uint64_t)tv.tv_sec * (uint64_t)(1000 * 1000 * 1000)
134 + (uint64_t)(tv.tv_usec * 1000);
135}
136
137
138/**
139 * Gets the current nanosecond timestamp.
140 *
141 * This differs from RTTimeNanoTS in that it will use system APIs and not do any
142 * resolution or performance optimizations.
143 *
144 * @returns nanosecond timestamp.
145 */
146RTDECL(uint64_t) RTTimeSystemNanoTS(void)
147{
148 return rtTimeGetSystemNanoTS();
149}
150
151
152/**
153 * Gets the current millisecond timestamp.
154 *
155 * This differs from RTTimeNanoTS in that it will use system APIs and not do any
156 * resolution or performance optimizations.
157 *
158 * @returns millisecond timestamp.
159 */
160RTDECL(uint64_t) RTTimeSystemMilliTS(void)
161{
162 return rtTimeGetSystemNanoTS() / 1000000;
163}
164
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