blob: 81bb9c7a4eb3fec22b97ea1c2c1cd17e5baffd23 [file] [log] [blame]
Thomas Gleixner97fc79f2006-01-09 20:52:31 -08001/*
2 * include/linux/ktime.h
3 *
4 * ktime_t - nanosecond-resolution time format.
5 *
6 * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
7 * Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
8 *
9 * data type definitions, declarations, prototypes and macros.
10 *
11 * Started by: Thomas Gleixner and Ingo Molnar
12 *
Thomas Gleixner66188fa2006-02-01 03:05:13 -080013 * Credits:
14 *
15 * Roman Zippel provided the ideas and primary code snippets of
16 * the ktime_t union and further simplifications of the original
17 * code.
18 *
Thomas Gleixner97fc79f2006-01-09 20:52:31 -080019 * For licencing details see kernel-base/COPYING
20 */
21#ifndef _LINUX_KTIME_H
22#define _LINUX_KTIME_H
23
24#include <linux/time.h>
25#include <linux/jiffies.h>
26
27/*
28 * ktime_t:
29 *
30 * On 64-bit CPUs a single 64-bit variable is used to store the hrtimers
31 * internal representation of time values in scalar nanoseconds. The
32 * design plays out best on 64-bit CPUs, where most conversions are
33 * NOPs and most arithmetic ktime_t operations are plain arithmetic
34 * operations.
35 *
36 * On 32-bit CPUs an optimized representation of the timespec structure
37 * is used to avoid expensive conversions from and to timespecs. The
38 * endian-aware order of the tv struct members is choosen to allow
39 * mathematical operations on the tv64 member of the union too, which
40 * for certain operations produces better code.
41 *
42 * For architectures with efficient support for 64/32-bit conversions the
43 * plain scalar nanosecond based representation can be selected by the
44 * config switch CONFIG_KTIME_SCALAR.
45 */
46typedef union {
47 s64 tv64;
48#if BITS_PER_LONG != 64 && !defined(CONFIG_KTIME_SCALAR)
49 struct {
50# ifdef __BIG_ENDIAN
51 s32 sec, nsec;
52# else
53 s32 nsec, sec;
54# endif
55 } tv;
56#endif
57} ktime_t;
58
Thomas Gleixner96dd7422006-09-06 00:03:42 -070059#define KTIME_MAX ((s64)~((u64)1 << 63))
Thomas Gleixner53790582007-03-16 14:15:57 -080060#if (BITS_PER_LONG == 64)
61# define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
62#else
63# define KTIME_SEC_MAX LONG_MAX
64#endif
Thomas Gleixner97fc79f2006-01-09 20:52:31 -080065
66/*
67 * ktime_t definitions when using the 64-bit scalar representation:
68 */
69
70#if (BITS_PER_LONG == 64) || defined(CONFIG_KTIME_SCALAR)
71
Thomas Gleixner97fc79f2006-01-09 20:52:31 -080072/**
73 * ktime_set - Set a ktime_t variable from a seconds/nanoseconds value
Thomas Gleixner97fc79f2006-01-09 20:52:31 -080074 * @secs: seconds to set
75 * @nsecs: nanoseconds to set
76 *
77 * Return the ktime_t representation of the value
78 */
79static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)
80{
Thomas Gleixner96dd7422006-09-06 00:03:42 -070081#if (BITS_PER_LONG == 64)
82 if (unlikely(secs >= KTIME_SEC_MAX))
83 return (ktime_t){ .tv64 = KTIME_MAX };
84#endif
Thomas Gleixner97fc79f2006-01-09 20:52:31 -080085 return (ktime_t) { .tv64 = (s64)secs * NSEC_PER_SEC + (s64)nsecs };
86}
87
88/* Subtract two ktime_t variables. rem = lhs -rhs: */
89#define ktime_sub(lhs, rhs) \
90 ({ (ktime_t){ .tv64 = (lhs).tv64 - (rhs).tv64 }; })
91
92/* Add two ktime_t variables. res = lhs + rhs: */
93#define ktime_add(lhs, rhs) \
94 ({ (ktime_t){ .tv64 = (lhs).tv64 + (rhs).tv64 }; })
95
96/*
97 * Add a ktime_t variable and a scalar nanosecond value.
98 * res = kt + nsval:
99 */
100#define ktime_add_ns(kt, nsval) \
101 ({ (ktime_t){ .tv64 = (kt).tv64 + (nsval) }; })
102
103/* convert a timespec to ktime_t format: */
Roman Zippelb2ee9db2006-02-15 15:17:40 -0800104static inline ktime_t timespec_to_ktime(struct timespec ts)
105{
106 return ktime_set(ts.tv_sec, ts.tv_nsec);
107}
Thomas Gleixner97fc79f2006-01-09 20:52:31 -0800108
109/* convert a timeval to ktime_t format: */
Roman Zippelb2ee9db2006-02-15 15:17:40 -0800110static inline ktime_t timeval_to_ktime(struct timeval tv)
111{
112 return ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC);
113}
Thomas Gleixner97fc79f2006-01-09 20:52:31 -0800114
115/* Map the ktime_t to timespec conversion to ns_to_timespec function */
116#define ktime_to_timespec(kt) ns_to_timespec((kt).tv64)
117
118/* Map the ktime_t to timeval conversion to ns_to_timeval function */
119#define ktime_to_timeval(kt) ns_to_timeval((kt).tv64)
120
Thomas Gleixner97fc79f2006-01-09 20:52:31 -0800121/* Convert ktime_t to nanoseconds - NOP in the scalar storage format: */
122#define ktime_to_ns(kt) ((kt).tv64)
123
124#else
125
126/*
127 * Helper macros/inlines to get the ktime_t math right in the timespec
128 * representation. The macros are sometimes ugly - their actual use is
129 * pretty okay-ish, given the circumstances. We do all this for
130 * performance reasons. The pure scalar nsec_t based code was nice and
131 * simple, but created too many 64-bit / 32-bit conversions and divisions.
132 *
133 * Be especially aware that negative values are represented in a way
134 * that the tv.sec field is negative and the tv.nsec field is greater
135 * or equal to zero but less than nanoseconds per second. This is the
136 * same representation which is used by timespecs.
137 *
138 * tv.sec < 0 and 0 >= tv.nsec < NSEC_PER_SEC
139 */
140
Thomas Gleixner97fc79f2006-01-09 20:52:31 -0800141/* Set a ktime_t variable to a value in sec/nsec representation: */
142static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)
143{
144 return (ktime_t) { .tv = { .sec = secs, .nsec = nsecs } };
145}
146
147/**
148 * ktime_sub - subtract two ktime_t variables
Thomas Gleixner97fc79f2006-01-09 20:52:31 -0800149 * @lhs: minuend
150 * @rhs: subtrahend
151 *
152 * Returns the remainder of the substraction
153 */
154static inline ktime_t ktime_sub(const ktime_t lhs, const ktime_t rhs)
155{
156 ktime_t res;
157
158 res.tv64 = lhs.tv64 - rhs.tv64;
159 if (res.tv.nsec < 0)
160 res.tv.nsec += NSEC_PER_SEC;
161
162 return res;
163}
164
165/**
166 * ktime_add - add two ktime_t variables
Thomas Gleixner97fc79f2006-01-09 20:52:31 -0800167 * @add1: addend1
168 * @add2: addend2
169 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800170 * Returns the sum of @add1 and @add2.
Thomas Gleixner97fc79f2006-01-09 20:52:31 -0800171 */
172static inline ktime_t ktime_add(const ktime_t add1, const ktime_t add2)
173{
174 ktime_t res;
175
176 res.tv64 = add1.tv64 + add2.tv64;
177 /*
178 * performance trick: the (u32) -NSEC gives 0x00000000Fxxxxxxx
179 * so we subtract NSEC_PER_SEC and add 1 to the upper 32 bit.
180 *
181 * it's equivalent to:
182 * tv.nsec -= NSEC_PER_SEC
183 * tv.sec ++;
184 */
185 if (res.tv.nsec >= NSEC_PER_SEC)
186 res.tv64 += (u32)-NSEC_PER_SEC;
187
188 return res;
189}
190
191/**
192 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixner97fc79f2006-01-09 20:52:31 -0800193 * @kt: addend
194 * @nsec: the scalar nsec value to add
195 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800196 * Returns the sum of @kt and @nsec in ktime_t format
Thomas Gleixner97fc79f2006-01-09 20:52:31 -0800197 */
198extern ktime_t ktime_add_ns(const ktime_t kt, u64 nsec);
199
200/**
201 * timespec_to_ktime - convert a timespec to ktime_t format
Thomas Gleixner97fc79f2006-01-09 20:52:31 -0800202 * @ts: the timespec variable to convert
203 *
204 * Returns a ktime_t variable with the converted timespec value
205 */
206static inline ktime_t timespec_to_ktime(const struct timespec ts)
207{
208 return (ktime_t) { .tv = { .sec = (s32)ts.tv_sec,
209 .nsec = (s32)ts.tv_nsec } };
210}
211
212/**
213 * timeval_to_ktime - convert a timeval to ktime_t format
Thomas Gleixner97fc79f2006-01-09 20:52:31 -0800214 * @tv: the timeval variable to convert
215 *
216 * Returns a ktime_t variable with the converted timeval value
217 */
218static inline ktime_t timeval_to_ktime(const struct timeval tv)
219{
220 return (ktime_t) { .tv = { .sec = (s32)tv.tv_sec,
221 .nsec = (s32)tv.tv_usec * 1000 } };
222}
223
224/**
225 * ktime_to_timespec - convert a ktime_t variable to timespec format
Thomas Gleixner97fc79f2006-01-09 20:52:31 -0800226 * @kt: the ktime_t variable to convert
227 *
228 * Returns the timespec representation of the ktime value
229 */
230static inline struct timespec ktime_to_timespec(const ktime_t kt)
231{
232 return (struct timespec) { .tv_sec = (time_t) kt.tv.sec,
233 .tv_nsec = (long) kt.tv.nsec };
234}
235
236/**
237 * ktime_to_timeval - convert a ktime_t variable to timeval format
Thomas Gleixner97fc79f2006-01-09 20:52:31 -0800238 * @kt: the ktime_t variable to convert
239 *
240 * Returns the timeval representation of the ktime value
241 */
242static inline struct timeval ktime_to_timeval(const ktime_t kt)
243{
244 return (struct timeval) {
245 .tv_sec = (time_t) kt.tv.sec,
246 .tv_usec = (suseconds_t) (kt.tv.nsec / NSEC_PER_USEC) };
247}
248
249/**
Thomas Gleixner97fc79f2006-01-09 20:52:31 -0800250 * ktime_to_ns - convert a ktime_t variable to scalar nanoseconds
251 * @kt: the ktime_t variable to convert
252 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800253 * Returns the scalar nanoseconds representation of @kt
Thomas Gleixner97fc79f2006-01-09 20:52:31 -0800254 */
Thomas Gleixnercfd18932006-12-06 20:37:38 -0800255static inline s64 ktime_to_ns(const ktime_t kt)
Thomas Gleixner97fc79f2006-01-09 20:52:31 -0800256{
Thomas Gleixnercfd18932006-12-06 20:37:38 -0800257 return (s64) kt.tv.sec * NSEC_PER_SEC + kt.tv.nsec;
Thomas Gleixner97fc79f2006-01-09 20:52:31 -0800258}
259
260#endif
261
YOSHIFUJI Hideaki84299b32007-04-24 16:21:38 -0700262static inline s64 ktime_to_us(const ktime_t kt)
263{
264 struct timeval tv = ktime_to_timeval(kt);
265 return (s64) tv.tv_sec * USEC_PER_SEC + tv.tv_usec;
266}
267
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800268/*
269 * The resolution of the clocks. The resolution value is returned in
270 * the clock_getres() system call to give application programmers an
271 * idea of the (in)accuracy of timers. Timer values are rounded up to
272 * this resolution values.
273 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800274#define KTIME_LOW_RES (ktime_t){ .tv64 = TICK_NSEC }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800275
276/* Get the monotonic time in timespec format: */
277extern void ktime_get_ts(struct timespec *ts);
278
279/* Get the real (wall-) time in timespec format: */
280#define ktime_get_real_ts(ts) getnstimeofday(ts)
281
Thomas Gleixner97fc79f2006-01-09 20:52:31 -0800282#endif