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/
31 * libcfs/include/libcfs/linux/linux-time.h
33 * Implementation of portable time API for Linux (kernel and user-level).
35 * Author: Nikita Danilov <nikita@clusterfs.com>
38 #ifndef __LIBCFS_LINUX_LINUX_TIME_H__
39 #define __LIBCFS_LINUX_LINUX_TIME_H__
41 /* Portable time API */
42 #include <linux/hrtimer.h>
43 #include <linux/module.h>
44 #include <linux/kernel.h>
45 #include <linux/version.h>
46 #include <linux/jiffies.h>
47 #include <linux/hrtimer.h>
48 #include <linux/types.h>
49 #include <linux/time.h>
50 #include <asm/div64.h>
53 * Generic kernel stuff
55 #ifndef HAVE_TIMESPEC64
57 typedef __s64 time64_t;
59 #if __BITS_PER_LONG == 64
61 # define timespec64 timespec
63 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
68 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts)
75 time64_t tv_sec; /* seconds */
76 long tv_nsec; /* nanoseconds */
79 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
81 struct timespec64 ret;
83 ret.tv_sec = ts.tv_sec;
84 ret.tv_nsec = ts.tv_nsec;
88 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
92 ret.tv_sec = (time_t)ts64.tv_sec;
93 ret.tv_nsec = ts64.tv_nsec;
96 #endif /* __BITS_PER_LONG != 64 */
98 #endif /* HAVE_TIMESPEC64 */
100 #ifndef HAVE_NS_TO_TIMESPEC64
101 static inline struct timespec64 ns_to_timespec64(const s64 nsec)
103 struct timespec64 ts;
107 return (struct timespec64) {0, 0};
109 ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem);
110 if (unlikely(rem < 0)) {
120 #ifndef HAVE_KTIME_ADD
121 # define ktime_add(lhs, rhs) ({ (ktime_t){ .tv64 = (lhs).tv64 + (rhs).tv64 }; })
122 #endif /* !HAVE_KTIME_ADD */
124 #ifndef HAVE_KTIME_AFTER
125 static inline bool ktime_after(const ktime_t cmp1, const ktime_t cmp2)
127 return cmp1.tv64 > cmp2.tv64;
129 #endif /* !HAVE_KTIME_AFTER */
131 #ifndef HAVE_KTIME_BEFORE
132 static inline bool ktime_before(const ktime_t cmp1, const ktime_t cmp2)
134 return cmp1.tv64 < cmp2.tv64;
136 #endif /* !HAVE_KTIME_BEFORE */
138 #ifndef HAVE_KTIME_COMPARE
139 static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
141 if (cmp1.tv64 < cmp2.tv64)
143 if (cmp1.tv64 > cmp2.tv64)
147 #endif /* !HAVE_KTIME_COMPARE */
149 #ifndef HAVE_KTIME_GET_TS64
150 void ktime_get_ts64(struct timespec64 *ts);
151 #endif /* HAVE_KTIME_GET_TS */
153 #ifndef HAVE_KTIME_GET_REAL_TS64
154 void ktime_get_real_ts64(struct timespec64 *ts);
155 #endif /* HAVE_KTIME_GET_REAL_TS */
157 #ifndef HAVE_KTIME_GET_REAL_SECONDS
158 time64_t ktime_get_real_seconds(void);
159 #endif /* HAVE_KTIME_GET_REAL_SECONDS */
161 #ifndef HAVE_KTIME_GET_SECONDS
162 time64_t ktime_get_seconds(void);
163 #endif /* HAVE_KTIME_GET_SECONDS */
165 #ifdef NEED_KTIME_GET_NS
166 static inline u64 ktime_get_ns(void)
168 return ktime_to_ns(ktime_get());
170 #endif /* NEED_KTIME_GET_NS */
172 #ifdef NEED_KTIME_GET_REAL_NS
173 static inline u64 ktime_get_real_ns(void)
175 return ktime_to_ns(ktime_get_real());
177 #endif /* NEED_KTIME_GET_REAL_NS */
179 #ifndef HAVE_KTIME_MS_DELTA
180 static inline s64 ktime_ms_delta(const ktime_t later, const ktime_t earlier)
182 return ktime_to_ms(ktime_sub(later, earlier));
184 #endif /* HAVE_KTIME_MS_DELTA */
186 #ifndef HAVE_KTIME_TO_TIMESPEC64
187 static inline struct timespec64 ktime_to_timespec64(ktime_t kt)
189 struct timespec ts = ns_to_timespec((kt).tv64);
191 return timespec_to_timespec64(ts);
193 #endif /* HAVE_KTIME_TO_TIMESPEC64 */
195 #ifndef HAVE_TIMESPEC64_SUB
196 static inline struct timespec64
197 timespec64_sub(struct timespec64 later, struct timespec64 earlier)
199 struct timespec diff;
201 diff = timespec_sub(timespec64_to_timespec(later),
202 timespec64_to_timespec(earlier));
203 return timespec_to_timespec64(diff);
207 #ifndef HAVE_TIMESPEC64_TO_KTIME
208 static inline ktime_t timespec64_to_ktime(struct timespec64 ts)
210 return ktime_set(ts.tv_sec, ts.tv_nsec);
214 static inline unsigned long cfs_time_seconds(time64_t seconds)
216 return nsecs_to_jiffies64(seconds * NSEC_PER_SEC);
219 #ifdef HAVE_NEW_DEFINE_TIMER
220 # ifndef TIMER_DATA_TYPE
221 # define TIMER_DATA_TYPE struct timer_list *
224 #define CFS_DEFINE_TIMER(_name, _function, _expires, _data) \
225 DEFINE_TIMER((_name), (_function))
227 # ifndef TIMER_DATA_TYPE
228 # define TIMER_DATA_TYPE unsigned long
231 #define CFS_DEFINE_TIMER(_name, _function, _expires, _data) \
232 DEFINE_TIMER((_name), (_function), (_expires), (_data))
235 #ifdef HAVE_TIMER_SETUP
236 #define cfs_timer_cb_arg_t struct timer_list *
237 #define cfs_from_timer(var, callback_timer, timer_fieldname) \
238 from_timer(var, callback_timer, timer_fieldname)
239 #define cfs_timer_setup(timer, callback, data, flags) \
240 timer_setup((timer), (callback), (flags))
241 #define cfs_timer_cb_arg(var, timer_fieldname) (&(var)->timer_fieldname)
243 #define cfs_timer_cb_arg_t unsigned long
244 #define cfs_from_timer(var, data, timer_fieldname) (typeof(var))(data)
245 #define cfs_timer_setup(timer, callback, data, flags) \
246 setup_timer((timer), (callback), (data))
247 #define cfs_timer_cb_arg(var, timer_fieldname) (cfs_timer_cb_arg_t)(var)
250 #endif /* __LIBCFS_LINUX_LINUX_TIME_H__ */