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 */
45 * Platform provides three opaque data-types:
47 * cfs_time_t represents point in time. This is internal kernel
48 * time rather than "wall clock". This time bears no
49 * relation to gettimeofday().
51 * cfs_duration_t represents time interval with resolution of internal
54 * cfs_time_t cfs_time_current(void);
55 * cfs_time_t cfs_time_add (cfs_time_t, cfs_duration_t);
56 * cfs_duration_t cfs_time_sub (cfs_time_t, cfs_time_t);
58 * time_t cfs_duration_sec (cfs_duration_t);
61 #include <linux/hrtimer.h>
62 #include <linux/module.h>
63 #include <linux/kernel.h>
64 #include <linux/version.h>
65 #include <linux/jiffies.h>
66 #include <linux/hrtimer.h>
67 #include <linux/types.h>
68 #include <linux/time.h>
69 #include <asm/div64.h>
72 * Generic kernel stuff
75 typedef unsigned long cfs_time_t; /* jiffies */
76 typedef long cfs_duration_t;
78 #ifndef HAVE_TIMESPEC64
80 typedef __s64 time64_t;
82 #if __BITS_PER_LONG == 64
84 # define timespec64 timespec
86 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
91 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts)
98 time64_t tv_sec; /* seconds */
99 long tv_nsec; /* nanoseconds */
102 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
104 struct timespec64 ret;
106 ret.tv_sec = ts.tv_sec;
107 ret.tv_nsec = ts.tv_nsec;
111 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
115 ret.tv_sec = (time_t)ts64.tv_sec;
116 ret.tv_nsec = ts64.tv_nsec;
119 #endif /* __BITS_PER_LONG != 64 */
121 #endif /* HAVE_TIMESPEC64 */
123 #ifndef HAVE_KTIME_ADD
124 # define ktime_add(lhs, rhs) ({ (ktime_t){ .tv64 = (lhs).tv64 + (rhs).tv64 }; })
125 #endif /* !HAVE_KTIME_ADD */
127 #ifndef HAVE_KTIME_AFTER
128 static inline bool ktime_after(const ktime_t cmp1, const ktime_t cmp2)
130 return cmp1.tv64 > cmp2.tv64;
132 #endif /* !HAVE_KTIME_AFTER */
134 #ifndef HAVE_KTIME_BEFORE
135 static inline bool ktime_before(const ktime_t cmp1, const ktime_t cmp2)
137 return cmp1.tv64 < cmp2.tv64;
139 #endif /* !HAVE_KTIME_BEFORE */
141 #ifndef HAVE_KTIME_COMPARE
142 static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
144 if (cmp1.tv64 < cmp2.tv64)
146 if (cmp1.tv64 > cmp2.tv64)
150 #endif /* !HAVE_KTIME_COMPARE */
152 #ifndef HAVE_KTIME_GET_TS64
153 void ktime_get_ts64(struct timespec64 *ts);
154 #endif /* HAVE_KTIME_GET_TS */
156 #ifndef HAVE_KTIME_GET_REAL_TS64
157 void ktime_get_real_ts64(struct timespec64 *ts);
158 #endif /* HAVE_KTIME_GET_REAL_TS */
160 #ifndef HAVE_KTIME_GET_REAL_SECONDS
161 time64_t ktime_get_real_seconds(void);
162 #endif /* HAVE_KTIME_GET_REAL_SECONDS */
164 #ifndef HAVE_KTIME_GET_SECONDS
165 time64_t ktime_get_seconds(void);
166 #endif /* HAVE_KTIME_GET_SECONDS */
168 #ifdef NEED_KTIME_GET_NS
169 static inline u64 ktime_get_ns(void)
171 return ktime_to_ns(ktime_get());
173 #endif /* NEED_KTIME_GET_NS */
175 #ifdef NEED_KTIME_GET_REAL_NS
176 static inline u64 ktime_get_real_ns(void)
178 return ktime_to_ns(ktime_get_real());
180 #endif /* NEED_KTIME_GET_REAL_NS */
182 #ifndef HAVE_KTIME_MS_DELTA
183 static inline s64 ktime_ms_delta(const ktime_t later, const ktime_t earlier)
185 return ktime_to_ms(ktime_sub(later, earlier));
187 #endif /* HAVE_KTIME_MS_DELTA */
189 #ifndef HAVE_KTIME_TO_TIMESPEC64
190 static inline struct timespec64 ktime_to_timespec64(ktime_t kt)
192 struct timespec ts = ns_to_timespec((kt).tv64);
194 return timespec_to_timespec64(ts);
196 #endif /* HAVE_KTIME_TO_TIMESPEC64 */
198 #ifndef HAVE_TIMESPEC64_SUB
199 static inline struct timespec64
200 timespec64_sub(struct timespec64 later, struct timespec64 earlier)
202 struct timespec diff;
204 diff = timespec_sub(timespec64_to_timespec(later),
205 timespec64_to_timespec(earlier));
206 return timespec_to_timespec64(diff);
210 #ifndef HAVE_TIMESPEC64_TO_KTIME
211 static inline ktime_t timespec64_to_ktime(struct timespec64 ts)
213 return ktime_set(ts.tv_sec, ts.tv_nsec);
217 static inline int cfs_time_before(cfs_time_t t1, cfs_time_t t2)
219 return time_before(t1, t2);
222 static inline int cfs_time_beforeq(cfs_time_t t1, cfs_time_t t2)
224 return time_before_eq(t1, t2);
227 static inline cfs_time_t cfs_time_current(void)
232 static inline time_t cfs_time_current_sec(void)
234 return get_seconds();
237 static inline cfs_duration_t cfs_time_seconds(int seconds)
239 return ((cfs_duration_t)seconds) * msecs_to_jiffies(MSEC_PER_SEC);
242 static inline time_t cfs_duration_sec(cfs_duration_t d)
244 return d / msecs_to_jiffies(MSEC_PER_SEC);
247 #endif /* __LIBCFS_LINUX_LINUX_TIME_H__ */