4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * libcfs/include/libcfs/winnt/winnt-time.h
36 * Implementation of portable time API for Winnt (kernel and user-level).
39 #ifndef __LIBCFS_WINNT_LINUX_TIME_H__
40 #define __LIBCFS_WINNT_LINUX_TIME_H__
42 #ifndef __LIBCFS_LIBCFS_H__
43 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
46 /* Portable time API */
49 * Platform provides three opaque data-types:
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().
55 * cfs_duration_t represents time interval with resolution of internal
58 * cfs_fs_time_t represents instance in world-visible time. This is
59 * used in file-system time-stamps
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);
67 * cfs_duration_t cfs_duration_build(int64_t);
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 *);
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 *);
86 time_t tv_sec; /* seconds */
87 suseconds_t tv_usec; /* microseconds */
90 typedef time_t cfs_time_t;
91 typedef time_t cfs_duration_t;
95 #include <libcfs/winnt/portals_compat25.h>
103 typedef struct timeval cfs_fs_time_t;
106 #define ONE_BILLION ((u_int64_t)1000000000)
107 #define ONE_MILLION ((u_int64_t) 1000000)
110 * Generic kernel stuff
113 #define jiffies (ULONG_PTR)JIFFIES()
114 #define cfs_jiffies (ULONG_PTR)JIFFIES()
116 static inline void do_gettimeofday(struct timeval *tv)
120 KeQuerySystemTime(&Time);
122 tv->tv_sec = (time_t) (Time.QuadPart / 10000000);
123 tv->tv_usec = (suseconds_t) (Time.QuadPart % 10000000) / 10;
126 static inline LONGLONG JIFFIES()
129 LARGE_INTEGER Elapse;
131 KeQueryTickCount(&Tick);
133 Elapse.QuadPart = Tick.QuadPart * KeQueryTimeIncrement();
134 Elapse.QuadPart /= (10000000 / HZ);
136 return Elapse.QuadPart;
139 static inline cfs_time_t cfs_time_current(void)
141 return (cfs_time_t)JIFFIES();
144 static inline time_t cfs_time_current_sec(void)
146 return (time_t)(JIFFIES() / HZ);
149 #define cfs_time_before(t1, t2) (((signed)(t1) - (signed)(t2)) < 0)
150 #define cfs_time_beforeq(t1, t2) (((signed)(t1) - (signed)(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 unsigned long get_seconds(void)
168 cfs_fs_time_current(&t);
169 return (unsigned long) t.tv_sec;
172 static inline cfs_time_t cfs_fs_time_sec(cfs_fs_time_t *t)
174 return (cfs_time_t)t->tv_sec;
177 static inline unsigned long __cfs_fs_time_flat(cfs_fs_time_t *t)
179 return (unsigned long)(t->tv_sec) * ONE_MILLION + t->tv_usec;
182 static inline int cfs_fs_time_before(cfs_fs_time_t *t1, cfs_fs_time_t *t2)
184 return (__cfs_fs_time_flat(t1) < __cfs_fs_time_flat(t2));
187 static inline int cfs_fs_time_beforeq(cfs_fs_time_t *t1, cfs_fs_time_t *t2)
189 return (__cfs_fs_time_flat(t1) <= __cfs_fs_time_flat(t2));
192 static inline cfs_duration_t cfs_time_seconds(cfs_duration_t seconds)
194 return (cfs_duration_t)(seconds * HZ);
197 static inline time_t cfs_duration_sec(cfs_duration_t d)
199 return (time_t)(d / HZ);
202 static inline void cfs_duration_usec(cfs_duration_t d, struct timeval *s)
204 s->tv_sec = (__u32)(d / HZ);
205 s->tv_usec = (__u32)((d - (cfs_duration_t)s->tv_sec * HZ) *
209 static inline void cfs_duration_nsec(cfs_duration_t d, struct timespec *s)
211 s->tv_sec = (__u32) (d / HZ);
212 s->tv_nsec = (__u32)((d - (cfs_duration_t)s->tv_sec * HZ) *
216 static inline void cfs_fs_time_usec(cfs_fs_time_t *t, struct timeval *v)
221 static inline void cfs_fs_time_nsec(cfs_fs_time_t *t, struct timespec *s)
223 s->tv_sec = (__u32) t->tv_sec;
224 s->tv_nsec = (__u32) t->tv_usec * 1000;
228 #define cfs_time_current_64 JIFFIES
230 static inline __u64 cfs_time_add_64(__u64 t, __u64 d)
235 static inline __u64 cfs_time_shift_64(cfs_duration_t seconds)
237 return cfs_time_add_64(cfs_time_current_64(),
238 cfs_time_seconds(seconds));
241 static inline int cfs_time_before_64(__u64 t1, __u64 t2)
243 return (__s64)t2 - (__s64)t1 > 0;
246 static inline int cfs_time_beforeq_64(__u64 t1, __u64 t2)
248 return (__s64)t2 - (__s64)t1 >= 0;
255 #define LTIME_S(t) *((__u64 *)&(t))
257 #define CFS_TIME_T "%u"
258 #define CFS_DURATION_T "%d"
260 #else /* !__KERNEL__ */
263 #ifdef HAVE_LIBPTHREAD
267 unsigned long tv_sec;
268 unsigned long tv_nsec;
270 #endif /* HAVE_LIBPTHREAD */
272 #include "../user-time.h"
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);
280 #endif /* !__KERNEL__ */
282 /* __LIBCFS_LINUX_LINUX_TIME_H__ */
286 * c-indentation-style: "K&R"