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_KTIME_ADD
102 # define ktime_add(lhs, rhs) ({ (ktime_t){ .tv64 = (lhs).tv64 + (rhs).tv64 }; })
103 #endif /* !HAVE_KTIME_ADD */
105 #ifndef HAVE_KTIME_AFTER
106 static inline bool ktime_after(const ktime_t cmp1, const ktime_t cmp2)
108 return cmp1.tv64 > cmp2.tv64;
110 #endif /* !HAVE_KTIME_AFTER */
112 #ifndef HAVE_KTIME_BEFORE
113 static inline bool ktime_before(const ktime_t cmp1, const ktime_t cmp2)
115 return cmp1.tv64 < cmp2.tv64;
117 #endif /* !HAVE_KTIME_BEFORE */
119 #ifndef HAVE_KTIME_COMPARE
120 static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
122 if (cmp1.tv64 < cmp2.tv64)
124 if (cmp1.tv64 > cmp2.tv64)
128 #endif /* !HAVE_KTIME_COMPARE */
130 #ifndef HAVE_KTIME_GET_TS64
131 void ktime_get_ts64(struct timespec64 *ts);
132 #endif /* HAVE_KTIME_GET_TS */
134 #ifndef HAVE_KTIME_GET_REAL_TS64
135 void ktime_get_real_ts64(struct timespec64 *ts);
136 #endif /* HAVE_KTIME_GET_REAL_TS */
138 #ifndef HAVE_KTIME_GET_REAL_SECONDS
139 time64_t ktime_get_real_seconds(void);
140 #endif /* HAVE_KTIME_GET_REAL_SECONDS */
142 #ifndef HAVE_KTIME_GET_SECONDS
143 time64_t ktime_get_seconds(void);
144 #endif /* HAVE_KTIME_GET_SECONDS */
146 #ifdef NEED_KTIME_GET_NS
147 static inline u64 ktime_get_ns(void)
149 return ktime_to_ns(ktime_get());
151 #endif /* NEED_KTIME_GET_NS */
153 #ifdef NEED_KTIME_GET_REAL_NS
154 static inline u64 ktime_get_real_ns(void)
156 return ktime_to_ns(ktime_get_real());
158 #endif /* NEED_KTIME_GET_REAL_NS */
160 #ifndef HAVE_KTIME_MS_DELTA
161 static inline s64 ktime_ms_delta(const ktime_t later, const ktime_t earlier)
163 return ktime_to_ms(ktime_sub(later, earlier));
165 #endif /* HAVE_KTIME_MS_DELTA */
167 #ifndef HAVE_KTIME_TO_TIMESPEC64
168 static inline struct timespec64 ktime_to_timespec64(ktime_t kt)
170 struct timespec ts = ns_to_timespec((kt).tv64);
172 return timespec_to_timespec64(ts);
174 #endif /* HAVE_KTIME_TO_TIMESPEC64 */
176 #ifndef HAVE_TIMESPEC64_SUB
177 static inline struct timespec64
178 timespec64_sub(struct timespec64 later, struct timespec64 earlier)
180 struct timespec diff;
182 diff = timespec_sub(timespec64_to_timespec(later),
183 timespec64_to_timespec(earlier));
184 return timespec_to_timespec64(diff);
188 #ifndef HAVE_TIMESPEC64_TO_KTIME
189 static inline ktime_t timespec64_to_ktime(struct timespec64 ts)
191 return ktime_set(ts.tv_sec, ts.tv_nsec);
195 static inline unsigned long cfs_time_seconds(time64_t seconds)
197 return nsecs_to_jiffies(seconds * NSEC_PER_SEC);
200 #ifdef HAVE_TIMER_SETUP
201 #define cfs_timer_cb_arg_t struct timer_list *
202 #define cfs_from_timer(var, callback_timer, timer_fieldname) \
203 from_timer(var, callback_timer, timer_fieldname)
204 #define cfs_timer_setup(timer, callback, data, flags) \
205 timer_setup((timer), (callback), (flags))
206 #define CFS_DEFINE_TIMER(_name, _function, _expires, _data) \
207 DEFINE_TIMER((_name), (_function))
208 #define cfs_timer_cb_arg(var, timer_fieldname) (&(var)->timer_fieldname)
210 #define cfs_timer_cb_arg_t unsigned long
211 #define cfs_from_timer(var, data, timer_fieldname) (typeof(var))(data)
212 #define cfs_timer_setup(timer, callback, data, flags) \
213 setup_timer((timer), (callback), (data))
214 #define CFS_DEFINE_TIMER(_name, _function, _expires, _data) \
215 DEFINE_TIMER((_name), (_function), (_expires), (_data))
216 #define cfs_timer_cb_arg(var, timer_fieldname) (cfs_timer_cb_arg_t)(var)
219 #endif /* __LIBCFS_LINUX_LINUX_TIME_H__ */