Whamcloud - gitweb
use special macro for print time_t, cleanup in includes.
[fs/lustre-release.git] / lnet / include / libcfs / linux / linux-time.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2004 Cluster File Systems, Inc.
5  * Author: Nikita Danilov <nikita@clusterfs.com>
6  *
7  * This file is part of Lustre, http://www.lustre.org.
8  *
9  * Lustre is free software; you can redistribute it and/or modify it under the
10  * terms of version 2 of the GNU General Public License as published by the
11  * Free Software Foundation.
12  *
13  * Lustre is distributed in the hope that it will be useful, but WITHOUT ANY
14  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with Lustre; if not, write to the Free Software Foundation, Inc., 675 Mass
20  * Ave, Cambridge, MA 02139, USA.
21  *
22  * Implementation of portable time API for Linux (kernel and user-level).
23  *
24  */
25
26 #ifndef __LIBCFS_LINUX_LINUX_TIME_H__
27 #define __LIBCFS_LINUX_LINUX_TIME_H__
28
29 #ifndef __LIBCFS_LIBCFS_H__
30 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
31 #endif
32
33 /* Portable time API */
34
35 /*
36  * Platform provides three opaque data-types:
37  *
38  *  cfs_time_t        represents point in time. This is internal kernel
39  *                    time rather than "wall clock". This time bears no
40  *                    relation to gettimeofday().
41  *
42  *  cfs_duration_t    represents time interval with resolution of internal
43  *                    platform clock
44  *
45  *  cfs_fs_time_t     represents instance in world-visible time. This is
46  *                    used in file-system time-stamps
47  *
48  *  cfs_time_t     cfs_time_current(void);
49  *  cfs_time_t     cfs_time_add    (cfs_time_t, cfs_duration_t);
50  *  cfs_duration_t cfs_time_sub    (cfs_time_t, cfs_time_t);
51  *  int            cfs_time_before (cfs_time_t, cfs_time_t);
52  *  int            cfs_time_beforeq(cfs_time_t, cfs_time_t);
53  *
54  *  cfs_duration_t cfs_duration_build(int64_t);
55  *
56  *  time_t         cfs_duration_sec (cfs_duration_t);
57  *  void           cfs_duration_usec(cfs_duration_t, struct timeval *);
58  *  void           cfs_duration_nsec(cfs_duration_t, struct timespec *);
59  *
60  *  void           cfs_fs_time_current(cfs_fs_time_t *);
61  *  time_t         cfs_fs_time_sec    (cfs_fs_time_t *);
62  *  void           cfs_fs_time_usec   (cfs_fs_time_t *, struct timeval *);
63  *  void           cfs_fs_time_nsec   (cfs_fs_time_t *, struct timespec *);
64  *  int            cfs_fs_time_before (cfs_fs_time_t *, cfs_fs_time_t *);
65  *  int            cfs_fs_time_beforeq(cfs_fs_time_t *, cfs_fs_time_t *);
66  *
67  *  CFS_TIME_FORMAT
68  *  CFS_DURATION_FORMAT
69  *
70  */
71
72 #define ONE_BILLION ((u_int64_t)1000000000)
73 #define ONE_MILLION 1000000
74
75 #ifdef __KERNEL__
76 #ifndef AUTOCONF_INCLUDED
77 #include <linux/config.h>
78 #endif
79 #include <linux/module.h>
80 #include <linux/kernel.h>
81 #include <linux/version.h>
82 #include <linux/time.h>
83 #include <asm/div64.h>
84
85 #include <libcfs/linux/portals_compat25.h>
86
87 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
88
89 /*
90  * old kernels---CURRENT_TIME is struct timeval
91  */
92 typedef struct timeval cfs_fs_time_t;
93
94 static inline void cfs_fs_time_usec(cfs_fs_time_t *t, struct timeval *v)
95 {
96         *v = *t;
97 }
98
99 static inline void cfs_fs_time_nsec(cfs_fs_time_t *t, struct timespec *s)
100 {
101         s->tv_sec  = t->tv_sec;
102         s->tv_nsec = t->tv_usec * 1000;
103 }
104
105 /*
106  * internal helper function used by cfs_fs_time_before*()
107  */
108 static inline unsigned long long __cfs_fs_time_flat(cfs_fs_time_t *t)
109 {
110         return (unsigned long long)t->tv_sec * ONE_MILLION + t->tv_usec;
111 }
112
113 #define CURRENT_KERN_TIME        xtime
114
115 #else
116 /* (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)) */
117
118 /*
119  * post 2.5 kernels.
120  */
121
122 #include <linux/jiffies.h>
123
124 typedef struct timespec cfs_fs_time_t;
125
126 static inline void cfs_fs_time_usec(cfs_fs_time_t *t, struct timeval *v)
127 {
128         v->tv_sec  = t->tv_sec;
129         v->tv_usec = t->tv_nsec / 1000;
130 }
131
132 static inline void cfs_fs_time_nsec(cfs_fs_time_t *t, struct timespec *s)
133 {
134         *s = *t;
135 }
136
137 /*
138  * internal helper function used by cfs_fs_time_before*()
139  */
140 static inline unsigned long long __cfs_fs_time_flat(cfs_fs_time_t *t)
141 {
142         return (unsigned long long)t->tv_sec * ONE_BILLION + t->tv_nsec;
143 }
144
145 #define CURRENT_KERN_TIME        CURRENT_TIME
146
147 /* (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)) */
148 #endif
149
150 /*
151  * Generic kernel stuff
152  */
153
154 typedef unsigned long cfs_time_t;      /* jiffies */
155 typedef long cfs_duration_t;
156
157
158 static inline cfs_time_t cfs_time_current(void)
159 {
160         return jiffies;
161 }
162
163 static inline time_t cfs_time_current_sec(void)
164 {
165         return CURRENT_SECONDS;
166 }
167
168 static inline cfs_time_t cfs_time_add(cfs_time_t t, cfs_duration_t d)
169 {
170         return t + d;
171 }
172
173 static inline cfs_duration_t cfs_time_sub(cfs_time_t t1, cfs_time_t t2)
174 {
175         return t1 - t2;
176 }
177
178 static inline int cfs_time_before(cfs_time_t t1, cfs_time_t t2)
179 {
180         return time_before(t1, t2);
181 }
182
183 static inline int cfs_time_beforeq(cfs_time_t t1, cfs_time_t t2)
184 {
185         return time_before_eq(t1, t2);
186 }
187
188 static inline void cfs_fs_time_current(cfs_fs_time_t *t)
189 {
190         *t = CURRENT_KERN_TIME;
191 }
192
193 static inline time_t cfs_fs_time_sec(cfs_fs_time_t *t)
194 {
195         return t->tv_sec;
196 }
197
198 static inline int cfs_fs_time_before(cfs_fs_time_t *t1, cfs_fs_time_t *t2)
199 {
200         return __cfs_fs_time_flat(t1) <  __cfs_fs_time_flat(t2);
201 }
202
203 static inline int cfs_fs_time_beforeq(cfs_fs_time_t *t1, cfs_fs_time_t *t2)
204 {
205         return __cfs_fs_time_flat(t1) <= __cfs_fs_time_flat(t2);
206 }
207
208 #if 0
209 static inline cfs_duration_t cfs_duration_build(int64_t nano)
210 {
211 #if (BITS_PER_LONG == 32)
212         /* We cannot use do_div(t, ONE_BILLION), do_div can only process
213          * 64 bits n and 32 bits base */
214         int64_t  t = nano * HZ;
215         do_div(t, 1000);
216         do_div(t, 1000000);
217         return (cfs_duration_t)t;
218 #else
219         return (nano * HZ / ONE_BILLION);
220 #endif
221 }
222 #endif
223
224 static inline cfs_duration_t cfs_time_seconds(int seconds)
225 {
226         return ((cfs_duration_t)seconds) * HZ;
227 }
228
229 static inline time_t cfs_duration_sec(cfs_duration_t d)
230 {
231         return d / HZ;
232 }
233
234 static inline void cfs_duration_usec(cfs_duration_t d, struct timeval *s)
235 {
236 #if (BITS_PER_LONG == 32) && (HZ > 4096)
237         __u64 t;
238
239         s->tv_sec = d / HZ;
240         t = (d - (cfs_duration_t)s->tv_sec * HZ) * ONE_MILLION;
241         do_div(t, HZ);
242         s->tv_usec = t;
243 #else
244         s->tv_sec = d / HZ;
245         s->tv_usec = ((d - (cfs_duration_t)s->tv_sec * HZ) * ONE_MILLION) / HZ;
246 #endif
247 }
248
249 static inline void cfs_duration_nsec(cfs_duration_t d, struct timespec *s)
250 {
251 #if (BITS_PER_LONG == 32)
252         __u64 t;
253
254         s->tv_sec = d / HZ;
255         t = (d - s->tv_sec * HZ) * ONE_BILLION;
256         do_div(t, HZ);
257         s->tv_nsec = t;
258 #else
259         s->tv_sec = d / HZ;
260         s->tv_nsec = ((d - s->tv_sec * HZ) * ONE_BILLION) / HZ;
261 #endif
262 }
263
264 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
265
266 #define cfs_time_current_64 get_jiffies_64
267
268 static inline __u64 cfs_time_add_64(__u64 t, __u64 d)
269 {
270         return t + d;
271 }
272
273 static inline __u64 cfs_time_shift_64(int seconds)
274 {
275         return cfs_time_add_64(cfs_time_current_64(),
276                                cfs_time_seconds(seconds));
277 }
278
279 static inline int cfs_time_before_64(__u64 t1, __u64 t2)
280 {
281         return (__s64)t2 - (__s64)t1 > 0;
282 }
283
284 static inline int cfs_time_beforeq_64(__u64 t1, __u64 t2)
285 {
286         return (__s64)t2 - (__s64)t1 >= 0;
287 }
288
289 #else
290 #define cfs_time_current_64 cfs_time_current
291 #define cfs_time_add_64     cfs_time_add
292 #define cfs_time_shift_64   cfs_time_shift
293 #define cfs_time_before_64  cfs_time_before
294 #define cfs_time_beforeq_64 cfs_time_beforeq
295 #endif
296
297 /*
298  * One jiffy
299  */
300 #define CFS_TICK                (1)
301
302 #define CFS_TIME_T              "%lu"
303 #define CFS_DURATION_T          "%ld"
304
305 #else   /* !__KERNEL__ */
306
307 /*
308  * Liblustre. time(2) based implementation.
309  */
310
311 #define CFS_TIME_T              "%lu"
312
313 #include <libcfs/user-time.h>
314
315 #endif /* __KERNEL__ */
316
317 /* __LIBCFS_LINUX_LINUX_TIME_H__ */
318 #endif
319 /*
320  * Local variables:
321  * c-indentation-style: "K&R"
322  * c-basic-offset: 8
323  * tab-width: 8
324  * fill-column: 80
325  * scroll-step: 1
326  * End:
327  */