4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * libcfs/include/libcfs/linux/linux-time.h
34 * Implementation of portable time API for Linux (kernel and user-level).
36 * Author: Nikita Danilov <nikita@clusterfs.com>
39 #ifndef __LIBCFS_LINUX_LINUX_TIME_H__
40 #define __LIBCFS_LINUX_LINUX_TIME_H__
42 /* Portable time API */
43 #include <linux/hrtimer.h>
44 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/version.h>
47 #include <linux/jiffies.h>
48 #include <linux/hrtimer.h>
49 #include <linux/types.h>
50 #include <linux/time.h>
51 #include <asm/div64.h>
54 * Generic kernel stuff
56 #ifndef HAVE_TIMESPEC64
58 typedef __s64 time64_t;
60 #if __BITS_PER_LONG == 64
62 # define timespec64 timespec
64 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
69 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts)
76 time64_t tv_sec; /* seconds */
77 long tv_nsec; /* nanoseconds */
80 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
82 struct timespec64 ret;
84 ret.tv_sec = ts.tv_sec;
85 ret.tv_nsec = ts.tv_nsec;
89 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
93 ret.tv_sec = (time_t)ts64.tv_sec;
94 ret.tv_nsec = ts64.tv_nsec;
97 #endif /* __BITS_PER_LONG != 64 */
99 #endif /* HAVE_TIMESPEC64 */
101 #ifndef HAVE_NS_TO_TIMESPEC64
102 static inline struct timespec64 ns_to_timespec64(const s64 nsec)
104 struct timespec64 ts;
108 return (struct timespec64) {0, 0};
110 ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem);
111 if (unlikely(rem < 0)) {
121 #ifndef HAVE_KTIME_ADD
122 # define ktime_add(lhs, rhs) ({ (ktime_t){ .tv64 = (lhs).tv64 + (rhs).tv64 }; })
123 #endif /* !HAVE_KTIME_ADD */
125 #ifndef HAVE_KTIME_AFTER
126 static inline bool ktime_after(const ktime_t cmp1, const ktime_t cmp2)
128 return cmp1.tv64 > cmp2.tv64;
130 #endif /* !HAVE_KTIME_AFTER */
132 #ifndef HAVE_KTIME_BEFORE
133 static inline bool ktime_before(const ktime_t cmp1, const ktime_t cmp2)
135 return cmp1.tv64 < cmp2.tv64;
137 #endif /* !HAVE_KTIME_BEFORE */
139 #ifndef HAVE_KTIME_COMPARE
140 static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
142 if (cmp1.tv64 < cmp2.tv64)
144 if (cmp1.tv64 > cmp2.tv64)
148 #endif /* !HAVE_KTIME_COMPARE */
150 #ifndef HAVE_KTIME_GET_TS64
151 void ktime_get_ts64(struct timespec64 *ts);
152 #endif /* HAVE_KTIME_GET_TS */
154 #ifndef HAVE_KTIME_GET_REAL_TS64
155 void ktime_get_real_ts64(struct timespec64 *ts);
156 #endif /* HAVE_KTIME_GET_REAL_TS */
158 #ifndef HAVE_KTIME_GET_REAL_SECONDS
159 time64_t ktime_get_real_seconds(void);
160 #endif /* HAVE_KTIME_GET_REAL_SECONDS */
162 #ifndef HAVE_KTIME_GET_SECONDS
163 time64_t ktime_get_seconds(void);
164 #endif /* HAVE_KTIME_GET_SECONDS */
166 #ifdef NEED_KTIME_GET_NS
167 static inline u64 ktime_get_ns(void)
169 return ktime_to_ns(ktime_get());
171 #endif /* NEED_KTIME_GET_NS */
173 #ifdef NEED_KTIME_GET_REAL_NS
174 static inline u64 ktime_get_real_ns(void)
176 return ktime_to_ns(ktime_get_real());
178 #endif /* NEED_KTIME_GET_REAL_NS */
180 #ifndef HAVE_KTIME_MS_DELTA
181 static inline s64 ktime_ms_delta(const ktime_t later, const ktime_t earlier)
183 return ktime_to_ms(ktime_sub(later, earlier));
185 #endif /* HAVE_KTIME_MS_DELTA */
187 #ifndef HAVE_KTIME_TO_TIMESPEC64
188 static inline struct timespec64 ktime_to_timespec64(ktime_t kt)
190 struct timespec ts = ns_to_timespec((kt).tv64);
192 return timespec_to_timespec64(ts);
194 #endif /* HAVE_KTIME_TO_TIMESPEC64 */
196 #ifndef HAVE_TIMESPEC64_SUB
197 static inline struct timespec64
198 timespec64_sub(struct timespec64 later, struct timespec64 earlier)
200 struct timespec diff;
202 diff = timespec_sub(timespec64_to_timespec(later),
203 timespec64_to_timespec(earlier));
204 return timespec_to_timespec64(diff);
208 #ifndef HAVE_TIMESPEC64_TO_KTIME
209 static inline ktime_t timespec64_to_ktime(struct timespec64 ts)
211 return ktime_set(ts.tv_sec, ts.tv_nsec);
215 static inline unsigned long cfs_time_seconds(time64_t seconds)
217 return nsecs_to_jiffies(seconds * NSEC_PER_SEC);
220 #ifdef HAVE_NEW_DEFINE_TIMER
221 # ifndef TIMER_DATA_TYPE
222 # define TIMER_DATA_TYPE struct timer_list *
225 #define CFS_DEFINE_TIMER(_name, _function, _expires, _data) \
226 DEFINE_TIMER((_name), (_function))
228 # ifndef TIMER_DATA_TYPE
229 # define TIMER_DATA_TYPE unsigned long
232 #define CFS_DEFINE_TIMER(_name, _function, _expires, _data) \
233 DEFINE_TIMER((_name), (_function), (_expires), (_data))
236 #ifdef HAVE_TIMER_SETUP
237 #define cfs_timer_cb_arg_t struct timer_list *
238 #define cfs_from_timer(var, callback_timer, timer_fieldname) \
239 from_timer(var, callback_timer, timer_fieldname)
240 #define cfs_timer_setup(timer, callback, data, flags) \
241 timer_setup((timer), (callback), (flags))
242 #define cfs_timer_cb_arg(var, timer_fieldname) (&(var)->timer_fieldname)
244 #define cfs_timer_cb_arg_t unsigned long
245 #define cfs_from_timer(var, data, timer_fieldname) (typeof(var))(data)
246 #define cfs_timer_setup(timer, callback, data, flags) \
247 setup_timer((timer), (callback), (data))
248 #define cfs_timer_cb_arg(var, timer_fieldname) (cfs_timer_cb_arg_t)(var)
251 #endif /* __LIBCFS_LINUX_LINUX_TIME_H__ */