Whamcloud - gitweb
LU-9019 libcfs: remove the remaining cfs_time 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 #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>
52
53 /*
54  * Generic kernel stuff
55  */
56 #ifndef HAVE_TIMESPEC64
57
58 typedef __s64 time64_t;
59
60 #if __BITS_PER_LONG == 64
61
62 # define timespec64 timespec
63
64 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
65 {
66         return ts;
67 }
68
69 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts)
70 {
71         return ts;
72 }
73
74 #else
75 struct timespec64 {
76         time64_t        tv_sec;         /* seconds */
77         long            tv_nsec;        /* nanoseconds */
78 };
79
80 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
81 {
82         struct timespec64 ret;
83
84         ret.tv_sec = ts.tv_sec;
85         ret.tv_nsec = ts.tv_nsec;
86         return ret;
87 }
88
89 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
90 {
91         struct timespec ret;
92
93         ret.tv_sec = (time_t)ts64.tv_sec;
94         ret.tv_nsec = ts64.tv_nsec;
95         return ret;
96 }
97 #endif /* __BITS_PER_LONG != 64 */
98
99 #endif /* HAVE_TIMESPEC64 */
100
101 #ifndef HAVE_KTIME_ADD
102 # define ktime_add(lhs, rhs) ({ (ktime_t){ .tv64 = (lhs).tv64 + (rhs).tv64 }; })
103 #endif /* !HAVE_KTIME_ADD */
104
105 #ifndef HAVE_KTIME_AFTER
106 static inline bool ktime_after(const ktime_t cmp1, const ktime_t cmp2)
107 {
108         return cmp1.tv64 > cmp2.tv64;
109 }
110 #endif /* !HAVE_KTIME_AFTER */
111
112 #ifndef HAVE_KTIME_BEFORE
113 static inline bool ktime_before(const ktime_t cmp1, const ktime_t cmp2)
114 {
115         return cmp1.tv64 < cmp2.tv64;
116 }
117 #endif /* !HAVE_KTIME_BEFORE */
118
119 #ifndef HAVE_KTIME_COMPARE
120 static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
121 {
122         if (cmp1.tv64 < cmp2.tv64)
123                 return -1;
124         if (cmp1.tv64 > cmp2.tv64)
125                 return 1;
126         return 0;
127 }
128 #endif /* !HAVE_KTIME_COMPARE */
129
130 #ifndef HAVE_KTIME_GET_TS64
131 void ktime_get_ts64(struct timespec64 *ts);
132 #endif /* HAVE_KTIME_GET_TS */
133
134 #ifndef HAVE_KTIME_GET_REAL_TS64
135 void ktime_get_real_ts64(struct timespec64 *ts);
136 #endif /* HAVE_KTIME_GET_REAL_TS */
137
138 #ifndef HAVE_KTIME_GET_REAL_SECONDS
139 time64_t ktime_get_real_seconds(void);
140 #endif /* HAVE_KTIME_GET_REAL_SECONDS */
141
142 #ifndef HAVE_KTIME_GET_SECONDS
143 time64_t ktime_get_seconds(void);
144 #endif /* HAVE_KTIME_GET_SECONDS */
145
146 #ifdef NEED_KTIME_GET_NS
147 static inline u64 ktime_get_ns(void)
148 {
149         return ktime_to_ns(ktime_get());
150 }
151 #endif /* NEED_KTIME_GET_NS */
152
153 #ifdef NEED_KTIME_GET_REAL_NS
154 static inline u64 ktime_get_real_ns(void)
155 {
156         return ktime_to_ns(ktime_get_real());
157 }
158 #endif /* NEED_KTIME_GET_REAL_NS */
159
160 #ifndef HAVE_KTIME_MS_DELTA
161 static inline s64 ktime_ms_delta(const ktime_t later, const ktime_t earlier)
162 {
163         return ktime_to_ms(ktime_sub(later, earlier));
164 }
165 #endif /* HAVE_KTIME_MS_DELTA */
166
167 #ifndef HAVE_KTIME_TO_TIMESPEC64
168 static inline struct timespec64 ktime_to_timespec64(ktime_t kt)
169 {
170         struct timespec ts = ns_to_timespec((kt).tv64);
171
172         return timespec_to_timespec64(ts);
173 }
174 #endif /* HAVE_KTIME_TO_TIMESPEC64 */
175
176 #ifndef HAVE_TIMESPEC64_SUB
177 static inline struct timespec64
178 timespec64_sub(struct timespec64 later, struct timespec64 earlier)
179 {
180         struct timespec diff;
181
182         diff = timespec_sub(timespec64_to_timespec(later),
183                             timespec64_to_timespec(earlier));
184         return timespec_to_timespec64(diff);
185 }
186 #endif
187
188 #ifndef HAVE_TIMESPEC64_TO_KTIME
189 static inline ktime_t timespec64_to_ktime(struct timespec64 ts)
190 {
191         return ktime_set(ts.tv_sec, ts.tv_nsec);
192 }
193 #endif
194
195 static inline unsigned long cfs_time_seconds(time64_t seconds)
196 {
197         return nsecs_to_jiffies(seconds * NSEC_PER_SEC);
198 }
199
200 #endif /* __LIBCFS_LINUX_LINUX_TIME_H__ */