Whamcloud - gitweb
f5645b1a8b08740cda7c1b7bcc23e32f89a7dd54
[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 [sun.com URL with a
20  * copy of GPLv2].
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 #define ONE_BILLION ((u_int64_t)1000000000)
88 #define ONE_MILLION ((u_int64_t)   1000000)
89
90 #define HZ (100)
91
92 struct timeval {
93         time_t          tv_sec;         /* seconds */
94         suseconds_t     tv_usec;        /* microseconds */
95 };
96
97 struct timespec {
98     ulong_ptr tv_sec;
99     ulong_ptr tv_nsec;
100 };
101
102 #ifdef __KERNEL__
103
104 #include <libcfs/winnt/portals_compat25.h>
105
106 /*
107  * Generic kernel stuff
108  */
109
110 typedef struct timeval cfs_fs_time_t;
111
112 typedef u_int64_t cfs_time_t;
113 typedef int64_t cfs_duration_t;
114
115 static inline void do_gettimeofday(struct timeval *tv)
116 {
117     LARGE_INTEGER Time;
118
119     KeQuerySystemTime(&Time);
120
121     tv->tv_sec  = (long_ptr) (Time.QuadPart / 10000000);
122     tv->tv_usec = (long_ptr) (Time.QuadPart % 10000000) / 10;
123 }
124
125 static inline cfs_time_t JIFFIES()
126 {
127     LARGE_INTEGER Tick;
128     LARGE_INTEGER Elapse;
129
130     KeQueryTickCount(&Tick);
131
132     Elapse.QuadPart  = Tick.QuadPart * KeQueryTimeIncrement();
133     Elapse.QuadPart /= (10000000 / HZ);
134
135     return Elapse.QuadPart;
136 }
137
138 static inline cfs_time_t cfs_time_current(void)
139 {
140     return JIFFIES();
141 }
142
143 static inline cfs_time_t cfs_time_current_sec(void)
144 {
145     return (JIFFIES() / HZ);
146 }
147
148 static inline cfs_time_t cfs_time_add(cfs_time_t t, cfs_duration_t d)
149 {
150     return (t + d);
151 }
152
153 static inline cfs_duration_t cfs_time_sub(cfs_time_t t1, cfs_time_t t2)
154 {
155     return (t1 - t2);
156 }
157
158 static inline int cfs_time_before(cfs_time_t t1, cfs_time_t t2)
159 {
160     return ((int64_t)t1 - (int64_t)t2) < 0; 
161 }
162
163 static inline int cfs_time_beforeq(cfs_time_t t1, cfs_time_t t2)
164 {
165     return ((int64_t)t1 - (int64_t)t2) <= 0;
166 }
167
168 static inline void cfs_fs_time_current(cfs_fs_time_t *t)
169 {
170     ULONG         Linux;
171     LARGE_INTEGER Sys;
172
173     KeQuerySystemTime(&Sys);
174
175     RtlTimeToSecondsSince1970(&Sys, &Linux);
176
177     t->tv_sec  = Linux;
178     t->tv_usec = (Sys.LowPart % 10000000) / 10;
179 }
180
181 static inline cfs_time_t cfs_fs_time_sec(cfs_fs_time_t *t)
182 {
183     return t->tv_sec;
184 }
185
186 static inline u_int64_t __cfs_fs_time_flat(cfs_fs_time_t *t)
187 {
188     return ((u_int64_t)t->tv_sec) * ONE_MILLION + t->tv_usec;
189 }
190
191 static inline int cfs_fs_time_before(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 int cfs_fs_time_beforeq(cfs_fs_time_t *t1, cfs_fs_time_t *t2)
197 {
198     return (__cfs_fs_time_flat(t1) <= __cfs_fs_time_flat(t2));
199 }
200
201 static inline cfs_duration_t cfs_time_seconds(int seconds)
202 {
203     return (cfs_duration_t)seconds * HZ;
204 }
205
206 static inline cfs_time_t cfs_duration_sec(cfs_duration_t d)
207 {
208         return d / HZ;
209 }
210
211 static inline void cfs_duration_usec(cfs_duration_t d, struct timeval *s)
212 {
213         s->tv_sec = (suseconds_t) (d / HZ);
214         s->tv_usec = (time_t)((d - (cfs_duration_t)s->tv_sec * HZ) *
215                               ONE_MILLION / HZ);
216 }
217
218 static inline void cfs_duration_nsec(cfs_duration_t d, struct timespec *s)
219 {
220         s->tv_sec = (suseconds_t) (d / HZ);
221         s->tv_nsec = (time_t)((d - (cfs_duration_t)s->tv_sec * HZ) *
222                               ONE_BILLION / HZ);
223 }
224
225 static inline void cfs_fs_time_usec(cfs_fs_time_t *t, struct timeval *v)
226 {
227         *v = *t;
228 }
229
230 static inline void cfs_fs_time_nsec(cfs_fs_time_t *t, struct timespec *s)
231 {
232         s->tv_sec  = t->tv_sec;
233         s->tv_nsec = t->tv_usec * 1000;
234 }
235
236 #define cfs_time_current_64 cfs_time_current
237 #define cfs_time_add_64     cfs_time_add
238 #define cfs_time_shift_64   cfs_time_shift
239 #define cfs_time_before_64  cfs_time_before
240 #define cfs_time_beforeq_64 cfs_time_beforeq
241
242 /*
243  * One jiffy
244  */
245 #define CFS_TICK                (1)
246
247 #define LTIME_S(t)                      (t)
248
249 #define CFS_TIME_T              "%I64u"
250 #define CFS_DURATION_T          "%I64d"
251
252 #else   /* !__KERNEL__ */
253
254 /*
255  * Liblustre. time(2) based implementation.
256  */
257 #include <libcfs/user-time.h>
258
259
260 //
261 // Time routines ...
262 //
263
264 NTSYSAPI
265 CCHAR
266 NTAPI
267 NtQuerySystemTime(
268     OUT PLARGE_INTEGER  CurrentTime
269     );
270
271
272 NTSYSAPI
273 BOOLEAN
274 NTAPI
275 RtlTimeToSecondsSince1970(
276     IN PLARGE_INTEGER  Time,
277     OUT PULONG  ElapsedSeconds
278     );
279
280
281 NTSYSAPI
282 VOID
283 NTAPI
284 RtlSecondsSince1970ToTime(
285     IN ULONG  ElapsedSeconds,
286     OUT PLARGE_INTEGER  Time
287     );
288
289 NTSYSAPI
290 VOID
291 NTAPI
292 Sleep(
293   DWORD dwMilliseconds   // sleep time in milliseconds
294 );
295
296
297 static inline void sleep(int time)
298 {
299     DWORD Time = 1000 * time;
300     Sleep(Time);
301 }
302
303
304 static inline void do_gettimeofday(struct timeval *tv)
305 {
306     LARGE_INTEGER Time;
307
308     NtQuerySystemTime(&Time);
309
310     tv->tv_sec  = (long_ptr) (Time.QuadPart / 10000000);
311     tv->tv_usec = (long_ptr) (Time.QuadPart % 10000000) / 10;
312 }
313
314 static inline int gettimeofday(struct timeval *tv, void * tz)
315 {
316     do_gettimeofday(tv);
317     return 0;
318 }
319
320 #endif /* __KERNEL__ */
321
322 /* __LIBCFS_LINUX_LINUX_TIME_H__ */
323 #endif
324 /*
325  * Local variables:
326  * c-indentation-style: "K&R"
327  * c-basic-offset: 8
328  * tab-width: 8
329  * fill-column: 80
330  * scroll-step: 1
331  * End:
332  */