VirtualBox

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

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

Biggest check-in ever. New source code headers for all (C) innotek files.

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