VirtualBox

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

Last change on this file since 48935 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

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