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