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