VirtualBox

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

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

InnoTek -> innotek: all the headers and comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1/* $Id: time-linux.cpp 2981 2007-06-01 16:01:28Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Time, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP RTLOGGROUP_TIME
27#define RTTIME_INCL_TIMEVAL
28#include <sys/time.h>
29#include <time.h>
30#include <sys/syscall.h>
31#include <unistd.h>
32#ifndef __NR_clock_gettime
33# define __NR_timer_create 259
34# define __NR_clock_gettime (__NR_timer_create+6)
35#endif
36
37#include <iprt/time.h>
38#include "internal/time.h"
39
40
41DECLINLINE(int) sys_clock_gettime(clockid_t id, struct timespec *ts)
42{
43 int rc = syscall(__NR_clock_gettime, id, ts);
44 if (rc >= 0)
45 return rc;
46 return -1;
47}
48
49
50/**
51 * Wrapper around various monotone time sources.
52 */
53DECLINLINE(int) mono_clock(struct timespec *ts)
54{
55 static int iWorking = -1;
56 switch (iWorking)
57 {
58#ifdef CLOCK_MONOTONIC
59 /*
60 * Standard clock_gettime()
61 */
62 case 0:
63 return clock_gettime(CLOCK_MONOTONIC, ts);
64
65 /*
66 * Syscall clock_gettime().
67 */
68 case 1:
69 return sys_clock_gettime(CLOCK_MONOTONIC, ts);
70
71#endif /* CLOCK_MONOTONIC */
72
73
74 /*
75 * Figure out what's working.
76 */
77 case -1:
78 {
79 int rc;
80#ifdef CLOCK_MONOTONIC
81 /*
82 * Real-Time API.
83 */
84 rc = clock_gettime(CLOCK_MONOTONIC, ts);
85 if (!rc)
86 {
87 iWorking = 0;
88 return 0;
89 }
90
91 rc = sys_clock_gettime(CLOCK_MONOTONIC, ts);
92 if (!rc)
93 {
94 iWorking = 1;
95 return 0;
96 }
97#endif /* CLOCK_MONOTONIC */
98
99 /* give up */
100 iWorking = -2;
101 break;
102 }
103 }
104 return -1;
105}
106
107
108DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
109{
110 /* check monotonic clock first. */
111 static bool fMonoClock = true;
112 if (fMonoClock)
113 {
114 struct timespec ts;
115 if (!mono_clock(&ts))
116 return (uint64_t)ts.tv_sec * (uint64_t)(1000 * 1000 * 1000)
117 + ts.tv_nsec;
118 fMonoClock = false;
119 }
120
121 /* fallback to gettimeofday(). */
122 struct timeval tv;
123 gettimeofday(&tv, NULL);
124 return (uint64_t)tv.tv_sec * (uint64_t)(1000 * 1000 * 1000)
125 + (uint64_t)(tv.tv_usec * 1000);
126}
127
128
129/**
130 * Gets the current nanosecond timestamp.
131 *
132 * This differs from RTTimeNanoTS in that it will use system APIs and not do any
133 * resolution or performance optimizations.
134 *
135 * @returns nanosecond timestamp.
136 */
137RTDECL(uint64_t) RTTimeSystemNanoTS(void)
138{
139 return rtTimeGetSystemNanoTS();
140}
141
142
143/**
144 * Gets the current millisecond 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 millisecond timestamp.
150 */
151RTDECL(uint64_t) RTTimeSystemMilliTS(void)
152{
153 return rtTimeGetSystemNanoTS() / 1000000;
154}
155
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