VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/linux/time-r0drv-linux.c@ 13476

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

comment.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1/* $Id: time-r0drv-linux.c 9369 2008-06-03 22:30:19Z vboxsync $ */
2/** @file
3 * IPRT - Time, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2008 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#include "the-linux-kernel.h"
37#include <iprt/time.h>
38#include <iprt/asm.h>
39
40
41
42DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
43{
44#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 16) /* This must match timer-r0drv-linux.c! */
45 /*
46 * Use ktime_get_ts, this is also what clock_gettime(CLOCK_MONOTONIC,) is using.
47 */
48 uint64_t u64;
49 struct timespec Ts;
50 ktime_get_ts(&Ts);
51 u64 = Ts.tv_sec * UINT64_C(1000000000) + Ts.tv_nsec;
52 return u64;
53
54#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 60)
55 /*
56 * Seems there is no way of getting to the exact source of
57 * sys_clock_gettime(CLOCK_MONOTONIC, &ts) here, I think. But
58 * 64-bit jiffies adjusted for the initial value should be pretty
59 * much the same I hope.
60 */
61 uint64_t u64 = get_jiffies_64();
62# ifdef INITIAL_JIFFIES
63 u64 += INITIAL_JIFFIES;
64# endif
65 u64 *= TICK_NSEC;
66 return u64;
67
68#else /* < 2.5.60 */
69# if BITS_PER_LONG >= 64
70 /*
71 * This is the same as above, except that there is no get_jiffies_64()
72 * here and we rely on long, and therefor jiffies, being 64-bit instead.
73 */
74 uint64_t u64 = jiffies;
75# ifdef INITIAL_JIFFIES
76 u64 += INITIAL_JIFFIES;
77# endif
78 u64 *= TICK_NSEC;
79 return u64;
80
81# else /* 32 bit jiffies */
82 /*
83 * We'll have to try track jiffy rollovers here or we'll be
84 * in trouble every time it flips.
85 *
86 * The high dword of the s_u64Last is the rollover count, the
87 * low dword is the previous jiffies. Updating is done by
88 * atomic compare & exchange of course.
89 */
90 static uint64_t volatile s_u64Last = 0;
91 uint64_t u64;
92
93 for (;;)
94 {
95 uint64_t u64NewLast;
96 int32_t iDelta;
97 uint32_t cRollovers;
98 uint32_t u32LastJiffies;
99
100 /* sample the values */
101 unsigned long ulNow = jiffies;
102 uint64_t u64Last = s_u64Last;
103 if (ulNow != jiffies)
104 continue; /* try again */
105# ifdef INITIAL_JIFFIES
106 ulNow += INITIAL_JIFFIES;
107# endif
108
109 u32LastJiffies = (uint32_t)u64Last;
110 cRollovers = u64Last >> 32;
111
112 /*
113 * Check for rollover and update the static last value.
114 *
115 * We have to make sure we update it successfully to rule out
116 * an underrun because of racing someone.
117 */
118 iDelta = ulNow - u32LastJiffies;
119 if (iDelta < 0)
120 {
121 cRollovers++;
122 u64NewLast = RT_MAKE_U64(ulNow, cRollovers);
123 if (!ASMAtomicCmpXchgU64(&s_u64Last, u64NewLast, u64Last))
124 continue; /* race, try again */
125 }
126 else
127 {
128 u64NewLast = RT_MAKE_U64(ulNow, cRollovers);
129 ASMAtomicCmpXchgU64(&s_u64Last, u64NewLast, u64Last);
130 }
131
132 /* calcuate the return value */
133 u64 = ulNow;
134 u64 *= TICK_NSEC;
135 u64 += cRollovers * (_4G * TICK_NSEC);
136 break;
137 }
138
139 return u64;
140# endif /* 32 bit jiffies */
141#endif /* < 2.5.60 */
142}
143
144
145RTDECL(uint64_t) RTTimeNanoTS(void)
146{
147 return rtTimeGetSystemNanoTS();
148}
149
150
151RTDECL(uint64_t) RTTimeMilliTS(void)
152{
153 return rtTimeGetSystemNanoTS() / 1000000;
154}
155
156
157RTDECL(uint64_t) RTTimeSystemNanoTS(void)
158{
159 return rtTimeGetSystemNanoTS();
160}
161
162
163RTDECL(uint64_t) RTTimeSystemMilliTS(void)
164{
165 return rtTimeGetSystemNanoTS() / 1000000;
166}
167
168
169RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime)
170{
171#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 16)
172 struct timespec Ts;
173 ktime_get_real_ts(&Ts);
174 return RTTimeSpecSetTimespec(pTime, &Ts);
175
176#else /* < 2.6.16 */
177 struct timeval Tv;
178 do_gettimeofday(&Tv);
179 return RTTimeSpecSetTimeval(pTime, &Tv);
180#endif
181}
182
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