Whamcloud - gitweb
LU-3963 Revert bitops changes
[fs/lustre-release.git] / libcfs / include / libcfs / winnt / winnt-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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * libcfs/include/libcfs/winnt/winnt-time.h
35  *
36  * Implementation of portable time API for Winnt (kernel and user-level).
37  */
38
39 #ifndef __LIBCFS_WINNT_LINUX_TIME_H__
40 #define __LIBCFS_WINNT_LINUX_TIME_H__
41
42 #ifndef __LIBCFS_LIBCFS_H__
43 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
44 #endif
45
46 /* Portable time API */
47
48 /*
49  * Platform provides three opaque data-types:
50  *
51  *  cfs_time_t        represents point in time. This is internal kernel
52  *                    time rather than "wall clock". This time bears no
53  *                    relation to gettimeofday().
54  *
55  *  cfs_duration_t    represents time interval with resolution of internal
56  *                    platform clock
57  *
58  *  cfs_fs_time_t     represents instance in world-visible time. This is
59  *                    used in file-system time-stamps
60  *
61  *  cfs_time_t     cfs_time_current(void);
62  *  cfs_time_t     cfs_time_add    (cfs_time_t, cfs_duration_t);
63  *  cfs_duration_t cfs_time_sub    (cfs_time_t, cfs_time_t);
64  *  int            cfs_time_before (cfs_time_t, cfs_time_t);
65  *  int            cfs_time_beforeq(cfs_time_t, cfs_time_t);
66  *
67  *  cfs_duration_t cfs_duration_build(int64_t);
68  *
69  *  time_t         cfs_duration_sec (cfs_duration_t);
70  *  void           cfs_duration_usec(cfs_duration_t, struct timeval *);
71  *  void           cfs_duration_nsec(cfs_duration_t, struct timespec *);
72  *
73  *  void           cfs_fs_time_current(cfs_fs_time_t *);
74  *  time_t         cfs_fs_time_sec    (cfs_fs_time_t *);
75  *  void           cfs_fs_time_usec   (cfs_fs_time_t *, struct timeval *);
76  *  void           cfs_fs_time_nsec   (cfs_fs_time_t *, struct timespec *);
77  *  int            cfs_fs_time_before (cfs_fs_time_t *, cfs_fs_time_t *);
78  *  int            cfs_fs_time_beforeq(cfs_fs_time_t *, cfs_fs_time_t *);
79  *
80  *  CFS_TIME_FORMAT
81  *  CFS_DURATION_FORMAT
82  *
83  */
84
85 struct timeval {
86     time_t      tv_sec;   /* seconds */
87     suseconds_t tv_usec;  /* microseconds */
88 };
89
90 typedef time_t cfs_time_t;
91 typedef time_t cfs_duration_t;
92
93 #ifdef __KERNEL__
94
95 #include <libcfs/winnt/portals_compat25.h>
96
97 #define HZ (100)
98
99 struct timespec {
100     __u32   tv_sec;
101     __u32   tv_nsec;
102 };
103 typedef struct timeval cfs_fs_time_t;
104
105
106 #define ONE_BILLION ((u_int64_t)1000000000)
107 #define ONE_MILLION ((u_int64_t)   1000000)
108
109 /*
110  * Generic kernel stuff
111  */
112
113 #define jiffies     (ULONG_PTR)JIFFIES()
114 #define cfs_jiffies (ULONG_PTR)JIFFIES()
115
116 static inline void do_gettimeofday(struct timeval *tv)
117 {
118     LARGE_INTEGER Time;
119
120     KeQuerySystemTime(&Time);
121
122     tv->tv_sec  = (time_t) (Time.QuadPart / 10000000);
123     tv->tv_usec = (suseconds_t) (Time.QuadPart % 10000000) / 10;
124 }
125
126 static inline LONGLONG JIFFIES()
127 {
128     LARGE_INTEGER Tick;
129     LARGE_INTEGER Elapse;
130
131     KeQueryTickCount(&Tick);
132
133     Elapse.QuadPart  = Tick.QuadPart * KeQueryTimeIncrement();
134     Elapse.QuadPart /= (10000000 / HZ);
135
136     return Elapse.QuadPart;
137 }
138
139 static inline cfs_time_t cfs_time_current(void)
140 {
141     return (cfs_time_t)JIFFIES();
142 }
143
144 static inline time_t cfs_time_current_sec(void)
145 {
146     return (time_t)(JIFFIES() / HZ);
147 }
148
149 #define cfs_time_before(t1, t2) (((signed)(t1) - (signed)(t2)) < 0)
150 #define cfs_time_beforeq(t1, t2) (((signed)(t1) - (signed)(t2)) <= 0)
151
152 static inline void cfs_fs_time_current(cfs_fs_time_t *t)
153 {
154     ULONG         Linux;
155     LARGE_INTEGER Sys;
156
157     KeQuerySystemTime(&Sys);
158
159     RtlTimeToSecondsSince1970(&Sys, &Linux);
160
161     t->tv_sec  = Linux;
162     t->tv_usec = (Sys.LowPart % 10000000) / 10;
163 }
164
165 static inline unsigned long get_seconds(void)
166 {
167     cfs_fs_time_t t;
168     cfs_fs_time_current(&t);
169     return (unsigned long) t.tv_sec;
170 }
171
172 static inline cfs_time_t cfs_fs_time_sec(cfs_fs_time_t *t)
173 {
174     return (cfs_time_t)t->tv_sec;
175 }
176
177 static inline unsigned long __cfs_fs_time_flat(cfs_fs_time_t *t)
178 {
179     return (unsigned long)(t->tv_sec) * ONE_MILLION + t->tv_usec;
180 }
181
182 static inline int cfs_fs_time_before(cfs_fs_time_t *t1, cfs_fs_time_t *t2)
183 {
184     return (__cfs_fs_time_flat(t1) < __cfs_fs_time_flat(t2));
185 }
186
187 static inline int cfs_fs_time_beforeq(cfs_fs_time_t *t1, cfs_fs_time_t *t2)
188 {
189     return (__cfs_fs_time_flat(t1) <= __cfs_fs_time_flat(t2));
190 }
191
192 static inline cfs_duration_t cfs_time_seconds(cfs_duration_t seconds)
193 {
194     return  (cfs_duration_t)(seconds * HZ);
195 }
196
197 static inline time_t cfs_duration_sec(cfs_duration_t d)
198 {
199     return (time_t)(d / HZ);
200 }
201
202 static inline void cfs_duration_usec(cfs_duration_t d, struct timeval *s)
203 {
204     s->tv_sec = (__u32)(d / HZ);
205     s->tv_usec = (__u32)((d - (cfs_duration_t)s->tv_sec * HZ) *
206                               ONE_MILLION / HZ);
207 }
208
209 static inline void cfs_duration_nsec(cfs_duration_t d, struct timespec *s)
210 {
211     s->tv_sec = (__u32) (d / HZ);
212     s->tv_nsec = (__u32)((d - (cfs_duration_t)s->tv_sec * HZ) *
213                            ONE_BILLION / HZ);
214 }
215
216 static inline void cfs_fs_time_usec(cfs_fs_time_t *t, struct timeval *v)
217 {
218     *v = *t;
219 }
220
221 static inline void cfs_fs_time_nsec(cfs_fs_time_t *t, struct timespec *s)
222 {
223     s->tv_sec  = (__u32) t->tv_sec;
224     s->tv_nsec = (__u32) t->tv_usec * 1000;
225 }
226
227
228 #define cfs_time_current_64 JIFFIES
229
230 static inline __u64 cfs_time_add_64(__u64 t, __u64 d)
231 {
232     return t + d;
233 }
234
235 static inline __u64 cfs_time_shift_64(cfs_duration_t seconds)
236 {
237     return cfs_time_add_64(cfs_time_current_64(),
238                            cfs_time_seconds(seconds));
239 }
240
241 static inline int cfs_time_before_64(__u64 t1, __u64 t2)
242 {
243     return (__s64)t2 - (__s64)t1 > 0;
244 }
245
246 static inline int cfs_time_beforeq_64(__u64 t1, __u64 t2)
247 {
248     return (__s64)t2 - (__s64)t1 >= 0;
249 }
250
251 /*
252  * One jiffy
253  */
254 #define CFS_TICK                (1)
255 #define LTIME_S(t)                      *((__u64 *)&(t))
256
257 #define CFS_TIME_T              "%u"
258 #define CFS_DURATION_T          "%d"
259
260 #else   /* !__KERNEL__ */
261
262 #include <time.h>
263 #ifdef HAVE_LIBPTHREAD
264 #include <pthread.h>
265 #else
266 struct timespec {
267     unsigned long tv_sec;
268     unsigned long tv_nsec;
269 };
270 #endif /* HAVE_LIBPTHREAD */
271
272 #include "../user-time.h"
273
274 /* liblustre. time(2) based implementation. */
275 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
276 void sleep(int time);
277 void do_gettimeofday(struct timeval *tv);
278 int gettimeofday(struct timeval *tv, void * tz);
279
280 #endif /* !__KERNEL__ */
281
282 /* __LIBCFS_LINUX_LINUX_TIME_H__ */
283 #endif
284 /*
285  * Local variables:
286  * c-indentation-style: "K&R"
287  * c-basic-offset: 8
288  * tab-width: 8
289  * fill-column: 80
290  * scroll-step: 1
291  * End:
292  */