1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=4:tabstop=4:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
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.
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
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.
21 * Implementation of portable time API for Winnt (kernel and user-level).
25 #ifndef __LIBCFS_WINNT_LINUX_TIME_H__
26 #define __LIBCFS_WINNT_LINUX_TIME_H__
28 #ifndef __LIBCFS_LIBCFS_H__
29 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
32 /* Portable time API */
35 * Platform provides three opaque data-types:
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().
41 * cfs_duration_t represents time interval with resolution of internal
44 * cfs_fs_time_t represents instance in world-visible time. This is
45 * used in file-system time-stamps
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);
53 * cfs_duration_t cfs_duration_build(int64_t);
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 *);
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 *);
71 #define ONE_BILLION ((u_int64_t)1000000000)
72 #define ONE_MILLION ((u_int64_t) 1000000)
77 time_t tv_sec; /* seconds */
78 suseconds_t tv_usec; /* microseconds */
88 #include <libcfs/winnt/portals_compat25.h>
91 * Generic kernel stuff
94 typedef struct timeval cfs_fs_time_t;
96 typedef u_int64_t cfs_time_t;
97 typedef int64_t cfs_duration_t;
99 static inline void do_gettimeofday(struct timeval *tv)
103 KeQuerySystemTime(&Time);
105 tv->tv_sec = (long_ptr) (Time.QuadPart / 10000000);
106 tv->tv_usec = (long_ptr) (Time.QuadPart % 10000000) / 10;
109 static inline cfs_time_t JIFFIES()
112 LARGE_INTEGER Elapse;
114 KeQueryTickCount(&Tick);
116 Elapse.QuadPart = Tick.QuadPart * KeQueryTimeIncrement();
117 Elapse.QuadPart /= (10000000 / HZ);
119 return Elapse.QuadPart;
122 static inline cfs_time_t cfs_time_current(void)
127 static inline cfs_time_t cfs_time_current_sec(void)
129 return (JIFFIES() / HZ);
132 static inline cfs_time_t cfs_time_add(cfs_time_t t, cfs_duration_t d)
137 static inline cfs_duration_t cfs_time_sub(cfs_time_t t1, cfs_time_t t2)
142 static inline int cfs_time_before(cfs_time_t t1, cfs_time_t t2)
144 return ((int64_t)t1 - (int64_t)t2) < 0;
147 static inline int cfs_time_beforeq(cfs_time_t t1, cfs_time_t t2)
149 return ((int64_t)t1 - (int64_t)t2) <= 0;
152 static inline void cfs_fs_time_current(cfs_fs_time_t *t)
157 KeQuerySystemTime(&Sys);
159 RtlTimeToSecondsSince1970(&Sys, &Linux);
162 t->tv_usec = (Sys.LowPart % 10000000) / 10;
165 static inline cfs_time_t cfs_fs_time_sec(cfs_fs_time_t *t)
170 static inline u_int64_t __cfs_fs_time_flat(cfs_fs_time_t *t)
172 return ((u_int64_t)t->tv_sec) * ONE_MILLION + t->tv_usec;
175 static inline int cfs_fs_time_before(cfs_fs_time_t *t1, cfs_fs_time_t *t2)
177 return (__cfs_fs_time_flat(t1) < __cfs_fs_time_flat(t2));
180 static inline int cfs_fs_time_beforeq(cfs_fs_time_t *t1, cfs_fs_time_t *t2)
182 return (__cfs_fs_time_flat(t1) <= __cfs_fs_time_flat(t2));
185 static inline cfs_duration_t cfs_time_seconds(int seconds)
187 return (cfs_duration_t)seconds * HZ;
190 static inline cfs_time_t cfs_duration_sec(cfs_duration_t d)
195 static inline void cfs_duration_usec(cfs_duration_t d, struct timeval *s)
197 s->tv_sec = (suseconds_t) (d / HZ);
198 s->tv_usec = (time_t)((d - (cfs_duration_t)s->tv_sec * HZ) *
202 static inline void cfs_duration_nsec(cfs_duration_t d, struct timespec *s)
204 s->tv_sec = (suseconds_t) (d / HZ);
205 s->tv_nsec = (time_t)((d - (cfs_duration_t)s->tv_sec * HZ) *
209 static inline void cfs_fs_time_usec(cfs_fs_time_t *t, struct timeval *v)
214 static inline void cfs_fs_time_nsec(cfs_fs_time_t *t, struct timespec *s)
216 s->tv_sec = t->tv_sec;
217 s->tv_nsec = t->tv_usec * 1000;
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
230 #define LTIME_S(t) (t)
232 #define CFS_TIME_T "%I64u"
233 #define CFS_DURATION_T "%I64d"
235 #else /* !__KERNEL__ */
238 * Liblustre. time(2) based implementation.
240 #include <libcfs/user-time.h>
251 OUT PLARGE_INTEGER CurrentTime
258 RtlTimeToSecondsSince1970(
259 IN PLARGE_INTEGER Time,
260 OUT PULONG ElapsedSeconds
267 RtlSecondsSince1970ToTime(
268 IN ULONG ElapsedSeconds,
269 OUT PLARGE_INTEGER Time
276 DWORD dwMilliseconds // sleep time in milliseconds
280 static inline void sleep(int time)
282 DWORD Time = 1000 * time;
287 static inline void do_gettimeofday(struct timeval *tv)
291 NtQuerySystemTime(&Time);
293 tv->tv_sec = (long_ptr) (Time.QuadPart / 10000000);
294 tv->tv_usec = (long_ptr) (Time.QuadPart % 10000000) / 10;
297 static inline int gettimeofday(struct timeval *tv, void * tz)
303 #endif /* __KERNEL__ */
305 /* __LIBCFS_LINUX_LINUX_TIME_H__ */
309 * c-indentation-style: "K&R"