1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
5 * Author: Nikita Danilov <nikita@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or modify it under the
10 * terms of version 2 of the GNU General Public License as published by the
11 * Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful, but WITHOUT ANY
14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License along
19 * with Lustre; if not, write to the Free Software Foundation, Inc., 675 Mass
20 * Ave, Cambridge, MA 02139, USA.
22 * Implementation of portable time API for XNU kernel
26 #ifndef __LIBCFS_DARWIN_DARWIN_TIME_H__
27 #define __LIBCFS_DARWIN_DARWIN_TIME_H__
29 #ifndef __LIBCFS_LIBCFS_H__
30 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
33 /* Portable time API */
36 * Platform provides three opaque data-types:
38 * cfs_time_t represents point in time. This is internal kernel
39 * time rather than "wall clock". This time bears no
40 * relation to gettimeofday().
42 * cfs_duration_t represents time interval with resolution of internal
45 * cfs_fs_time_t represents instance in world-visible time. This is
46 * used in file-system time-stamps
48 * cfs_time_t cfs_time_current(void);
49 * cfs_time_t cfs_time_add (cfs_time_t, cfs_duration_t);
50 * cfs_duration_t cfs_time_sub (cfs_time_t, cfs_time_t);
51 * int cfs_time_before (cfs_time_t, cfs_time_t);
52 * int cfs_time_beforeq(cfs_time_t, cfs_time_t);
54 * cfs_duration_t cfs_duration_build(int64_t);
56 * time_t cfs_duration_sec (cfs_duration_t);
57 * void cfs_duration_usec(cfs_duration_t, struct timeval *);
58 * void cfs_duration_nsec(cfs_duration_t, struct timespec *);
60 * void cfs_fs_time_current(cfs_fs_time_t *);
61 * time_t cfs_fs_time_sec (cfs_fs_time_t *);
62 * void cfs_fs_time_usec (cfs_fs_time_t *, struct timeval *);
63 * void cfs_fs_time_nsec (cfs_fs_time_t *, struct timespec *);
64 * int cfs_fs_time_before (cfs_fs_time_t *, cfs_fs_time_t *);
65 * int cfs_fs_time_beforeq(cfs_fs_time_t *, cfs_fs_time_t *);
67 * cfs_duration_t cfs_time_minimal_timeout(void)
74 #define ONE_BILLION ((u_int64_t)1000000000)
75 #define ONE_MILLION ((u_int64_t) 1000000)
78 #include <sys/types.h>
79 #include <sys/systm.h>
81 #ifndef __APPLE_API_PRIVATE
82 #define __APPLE_API_PRIVATE
84 #undef __APPLE_API_PRIVATE
89 #include <sys/kernel.h>
91 #include <mach/thread_act.h>
92 #include <mach/mach_types.h>
93 #include <mach/mach_traps.h>
94 #include <mach/thread_switch.h>
95 #include <mach/time_value.h>
96 #include <kern/sched_prim.h>
98 #include <vm/vm_kern.h>
99 #include <mach/machine/vm_param.h>
100 #include <kern/clock.h>
101 #include <kern/thread_call.h>
102 #include <sys/param.h>
105 #include <libcfs/darwin/darwin-types.h>
106 #include <libcfs/darwin/darwin-utils.h>
107 #include <libcfs/darwin/darwin-lock.h>
109 typedef u_int64_t cfs_time_t; /* nanoseconds */
110 typedef int64_t cfs_duration_t;
112 #define CFS_TIME_T "%llu"
113 #define CFS_DURATION_T "%lld"
115 typedef struct timeval cfs_fs_time_t;
117 static inline cfs_time_t cfs_time_current(void)
119 struct timespec instant;
122 return ((u_int64_t)instant.tv_sec) * ONE_BILLION + instant.tv_nsec;
125 static inline time_t cfs_time_current_sec(void)
127 struct timespec instant;
130 return instant.tv_sec;
133 static inline cfs_time_t cfs_time_add(cfs_time_t t, cfs_duration_t d)
138 static inline cfs_duration_t cfs_time_sub(cfs_time_t t1, cfs_time_t t2)
143 static inline int cfs_time_before(cfs_time_t t1, cfs_time_t t2)
145 return (int64_t)t1 - (int64_t)t2 < 0;
148 static inline int cfs_time_beforeq(cfs_time_t t1, cfs_time_t t2)
150 return (int64_t)t1 - (int64_t)t2 <= 0;
153 static inline void cfs_fs_time_current(cfs_fs_time_t *t)
158 static inline time_t cfs_fs_time_sec(cfs_fs_time_t *t)
163 static inline cfs_duration_t cfs_duration_build(int64_t nano)
169 static inline void cfs_fs_time_usec(cfs_fs_time_t *t, struct timeval *v)
174 static inline void cfs_fs_time_nsec(cfs_fs_time_t *t, struct timespec *s)
176 s->tv_sec = t->tv_sec;
177 s->tv_nsec = t->tv_usec * 1000;
180 static inline cfs_duration_t cfs_time_seconds(int seconds)
182 return cfs_duration_build(ONE_BILLION * (int64_t)seconds);
185 static inline cfs_time_t cfs_time_shift(int seconds)
187 return cfs_time_add(cfs_time_current(), cfs_time_seconds(seconds));
191 * internal helper function used by cfs_fs_time_before*()
193 static inline int64_t __cfs_fs_time_flat(cfs_fs_time_t *t)
195 return ((int64_t)t->tv_sec) * ONE_BILLION + t->tv_usec;
198 static inline int cfs_fs_time_before(cfs_fs_time_t *t1, cfs_fs_time_t *t2)
200 return __cfs_fs_time_flat(t1) - __cfs_fs_time_flat(t2) < 0;
203 static inline int cfs_fs_time_beforeq(cfs_fs_time_t *t1, cfs_fs_time_t *t2)
205 return __cfs_fs_time_flat(t1) - __cfs_fs_time_flat(t2) <= 0;
208 static inline time_t cfs_duration_sec(cfs_duration_t d)
210 return d / ONE_BILLION;
213 static inline void cfs_duration_usec(cfs_duration_t d, struct timeval *s)
215 s->tv_sec = d / ONE_BILLION;
216 s->tv_usec = (d - s->tv_sec * ONE_BILLION) / 1000;
219 static inline void cfs_duration_nsec(cfs_duration_t d, struct timespec *s)
221 s->tv_sec = d / ONE_BILLION;
222 s->tv_nsec = d - ((int64_t)s->tv_sec) * ONE_BILLION;
225 static inline cfs_duration_t cfs_time_minimal_timeout(void)
227 return ONE_BILLION / (u_int64_t)hz;
230 /* inline function cfs_time_minimal_timeout() can not be used to
231 * initiallize static variable */
232 #define CFS_MIN_DELAY (ONE_BILLION / (u_int64_t)100)
234 #define LTIME_S(t) (t)
242 #include <libcfs/user-time.h>
247 /* __LIBCFS_DARWIN_DARWIN_TIME_H__ */
251 * c-indentation-style: "K&R"