Whamcloud - gitweb
LU-9019 libcfs: remove cfs_time_XXX_64 wrappers
[fs/lustre-release.git] / libcfs / include / libcfs / linux / linux-time.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * libcfs/include/libcfs/linux/linux-time.h
33  *
34  * Implementation of portable time API for Linux (kernel and user-level).
35  *
36  * Author: Nikita Danilov <nikita@clusterfs.com>
37  */
38
39 #ifndef __LIBCFS_LINUX_LINUX_TIME_H__
40 #define __LIBCFS_LINUX_LINUX_TIME_H__
41
42 /* Portable time API */
43
44 /*
45  * Platform provides three opaque data-types:
46  *
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().
50  *
51  *  cfs_duration_t    represents time interval with resolution of internal
52  *                    platform clock
53  *
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);
57  *
58  *  time_t         cfs_duration_sec (cfs_duration_t);
59  */
60
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>
70
71 /*
72  * Generic kernel stuff
73  */
74
75 typedef unsigned long cfs_time_t;      /* jiffies */
76 typedef long cfs_duration_t;
77
78 #ifndef HAVE_TIMESPEC64
79
80 typedef __s64 time64_t;
81
82 #if __BITS_PER_LONG == 64
83
84 # define timespec64 timespec
85
86 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
87 {
88         return ts;
89 }
90
91 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts)
92 {
93         return ts;
94 }
95
96 #else
97 struct timespec64 {
98         time64_t        tv_sec;         /* seconds */
99         long            tv_nsec;        /* nanoseconds */
100 };
101
102 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
103 {
104         struct timespec64 ret;
105
106         ret.tv_sec = ts.tv_sec;
107         ret.tv_nsec = ts.tv_nsec;
108         return ret;
109 }
110
111 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
112 {
113         struct timespec ret;
114
115         ret.tv_sec = (time_t)ts64.tv_sec;
116         ret.tv_nsec = ts64.tv_nsec;
117         return ret;
118 }
119 #endif /* __BITS_PER_LONG != 64 */
120
121 #endif /* HAVE_TIMESPEC64 */
122
123 #ifndef HAVE_KTIME_ADD
124 # define ktime_add(lhs, rhs) ({ (ktime_t){ .tv64 = (lhs).tv64 + (rhs).tv64 }; })
125 #endif /* !HAVE_KTIME_ADD */
126
127 #ifndef HAVE_KTIME_AFTER
128 static inline bool ktime_after(const ktime_t cmp1, const ktime_t cmp2)
129 {
130         return cmp1.tv64 > cmp2.tv64;
131 }
132 #endif /* !HAVE_KTIME_AFTER */
133
134 #ifndef HAVE_KTIME_BEFORE
135 static inline bool ktime_before(const ktime_t cmp1, const ktime_t cmp2)
136 {
137         return cmp1.tv64 < cmp2.tv64;
138 }
139 #endif /* !HAVE_KTIME_BEFORE */
140
141 #ifndef HAVE_KTIME_COMPARE
142 static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
143 {
144         if (cmp1.tv64 < cmp2.tv64)
145                 return -1;
146         if (cmp1.tv64 > cmp2.tv64)
147                 return 1;
148         return 0;
149 }
150 #endif /* !HAVE_KTIME_COMPARE */
151
152 #ifndef HAVE_KTIME_GET_TS64
153 void ktime_get_ts64(struct timespec64 *ts);
154 #endif /* HAVE_KTIME_GET_TS */
155
156 #ifndef HAVE_KTIME_GET_REAL_TS64
157 void ktime_get_real_ts64(struct timespec64 *ts);
158 #endif /* HAVE_KTIME_GET_REAL_TS */
159
160 #ifndef HAVE_KTIME_GET_REAL_SECONDS
161 time64_t ktime_get_real_seconds(void);
162 #endif /* HAVE_KTIME_GET_REAL_SECONDS */
163
164 #ifndef HAVE_KTIME_GET_SECONDS
165 time64_t ktime_get_seconds(void);
166 #endif /* HAVE_KTIME_GET_SECONDS */
167
168 #ifdef NEED_KTIME_GET_NS
169 static inline u64 ktime_get_ns(void)
170 {
171         return ktime_to_ns(ktime_get());
172 }
173 #endif /* NEED_KTIME_GET_NS */
174
175 #ifdef NEED_KTIME_GET_REAL_NS
176 static inline u64 ktime_get_real_ns(void)
177 {
178         return ktime_to_ns(ktime_get_real());
179 }
180 #endif /* NEED_KTIME_GET_REAL_NS */
181
182 #ifndef HAVE_KTIME_MS_DELTA
183 static inline s64 ktime_ms_delta(const ktime_t later, const ktime_t earlier)
184 {
185         return ktime_to_ms(ktime_sub(later, earlier));
186 }
187 #endif /* HAVE_KTIME_MS_DELTA */
188
189 #ifndef HAVE_KTIME_TO_TIMESPEC64
190 static inline struct timespec64 ktime_to_timespec64(ktime_t kt)
191 {
192         struct timespec ts = ns_to_timespec((kt).tv64);
193
194         return timespec_to_timespec64(ts);
195 }
196 #endif /* HAVE_KTIME_TO_TIMESPEC64 */
197
198 #ifndef HAVE_TIMESPEC64_SUB
199 static inline struct timespec64
200 timespec64_sub(struct timespec64 later, struct timespec64 earlier)
201 {
202         struct timespec diff;
203
204         diff = timespec_sub(timespec64_to_timespec(later),
205                             timespec64_to_timespec(earlier));
206         return timespec_to_timespec64(diff);
207 }
208 #endif
209
210 #ifndef HAVE_TIMESPEC64_TO_KTIME
211 static inline ktime_t timespec64_to_ktime(struct timespec64 ts)
212 {
213         return ktime_set(ts.tv_sec, ts.tv_nsec);
214 }
215 #endif
216
217 static inline int cfs_time_before(cfs_time_t t1, cfs_time_t t2)
218 {
219         return time_before(t1, t2);
220 }
221
222 static inline int cfs_time_beforeq(cfs_time_t t1, cfs_time_t t2)
223 {
224         return time_before_eq(t1, t2);
225 }
226
227 static inline cfs_time_t cfs_time_current(void)
228 {
229         return jiffies;
230 }
231
232 static inline time_t cfs_time_current_sec(void)
233 {
234         return get_seconds();
235 }
236
237 static inline cfs_duration_t cfs_time_seconds(int seconds)
238 {
239         return ((cfs_duration_t)seconds) * msecs_to_jiffies(MSEC_PER_SEC);
240 }
241
242 static inline time_t cfs_duration_sec(cfs_duration_t d)
243 {
244         return d / msecs_to_jiffies(MSEC_PER_SEC);
245 }
246
247 #endif /* __LIBCFS_LINUX_LINUX_TIME_H__ */