Whamcloud - gitweb
35862a63d60f75855c08ac4392d61c8cb2800e81
[fs/lustre-release.git] / lnet / include / libcfs / darwin / darwin-time.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2004 Cluster File Systems, Inc.
5  * Author: Nikita Danilov <nikita@clusterfs.com>
6  *
7  * This file is part of Lustre, http://www.lustre.org.
8  *
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.
12  *
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
16  * details.
17  *
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.
21  *
22  * Implementation of portable time API for XNU kernel
23  *
24  */
25
26 #ifndef __LIBCFS_DARWIN_DARWIN_TIME_H__
27 #define __LIBCFS_DARWIN_DARWIN_TIME_H__
28
29 #ifndef __LIBCFS_LIBCFS_H__
30 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
31 #endif
32
33 /* Portable time API */
34
35 /*
36  * Platform provides three opaque data-types:
37  *
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().
41  *
42  *  cfs_duration_t    represents time interval with resolution of internal
43  *                    platform clock
44  *
45  *  cfs_fs_time_t     represents instance in world-visible time. This is
46  *                    used in file-system time-stamps
47  *
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);
53  *
54  *  cfs_duration_t cfs_duration_build(int64_t);
55  *
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 *);
59  *
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 *);
66  *
67  *  CFS_TIME_FORMAT
68  *  CFS_DURATION_FORMAT
69  *
70  */
71
72 #define ONE_BILLION ((u_int64_t)1000000000)
73 #define ONE_MILLION 1000000
74
75 #ifdef __KERNEL__
76 #include <sys/types.h>
77 #include <sys/systm.h>
78
79 #include <sys/kernel.h>
80
81 #include <mach/mach_types.h>
82 #include <mach/time_value.h>
83 #include <kern/clock.h>
84 #include <sys/param.h>
85
86 #include <libcfs/darwin/darwin-types.h>
87 #include <libcfs/darwin/darwin-utils.h>
88 #include <libcfs/darwin/darwin-lock.h>
89
90 /*
91  * There are three way to measure time in OS X:
92  * 1. nanoseconds
93  * 2. absolute time (abstime unit equal to the length of one bus cycle),
94  *    schedule of thread/timer are counted by absolute time, but abstime
95  *    in different mac can be different also, so we wouldn't use it.
96  * 3. clock interval (1sec = 100hz). But clock interval only taken by KPI
97  *    like tsleep().
98  *
99  * We use nanoseconds (uptime, not calendar time)
100  *
101  * clock_get_uptime()   :get absolute time since bootup.
102  * nanouptime()         :get nanoseconds since bootup
103  * microuptime()        :get microseonds since bootup
104  * nanotime()           :get nanoseconds since epoch
105  * microtime()          :get microseconds since epoch
106  */
107 typedef u_int64_t cfs_time_t; /* nanoseconds */
108 typedef int64_t cfs_duration_t;
109
110 #define CFS_TIME_T              "%llu"
111 #define CFS_DURATION_T          "%lld"
112
113 typedef struct timeval cfs_fs_time_t;
114
115 static inline cfs_time_t cfs_time_current(void)
116 {
117         struct timespec instant;
118
119         nanouptime(&instant);
120         return ((u_int64_t)instant.tv_sec) * NSEC_PER_SEC + instant.tv_nsec;
121 }
122
123 static inline time_t cfs_time_current_sec(void)
124 {
125         struct timespec instant;
126
127         nanouptime(&instant);
128         return instant.tv_sec;
129 }
130
131 static inline cfs_time_t cfs_time_add(cfs_time_t t, cfs_duration_t d)
132 {
133         return t + d;
134 }
135
136 static inline cfs_duration_t cfs_time_sub(cfs_time_t t1, cfs_time_t t2)
137 {
138         return t1 - t2;
139 }
140
141 static inline int cfs_time_before(cfs_time_t t1, cfs_time_t t2)
142 {
143         return (int64_t)t1 - (int64_t)t2 < 0;
144 }
145
146 static inline int cfs_time_beforeq(cfs_time_t t1, cfs_time_t t2)
147 {
148         return (int64_t)t1 - (int64_t)t2 <= 0;
149 }
150
151 static inline void cfs_fs_time_current(cfs_fs_time_t *t)
152 {
153         microtime((struct timeval *)t);
154 }
155
156 static inline time_t cfs_fs_time_sec(cfs_fs_time_t *t)
157 {
158         return t->tv_sec;
159 }
160
161 static inline void cfs_fs_time_usec(cfs_fs_time_t *t, struct timeval *v)
162 {
163         *v = *t;
164 }
165
166 static inline void cfs_fs_time_nsec(cfs_fs_time_t *t, struct timespec *s)
167 {
168         s->tv_sec  = t->tv_sec;
169         s->tv_nsec = t->tv_usec * NSEC_PER_USEC;
170 }
171
172 static inline cfs_duration_t cfs_time_seconds(int seconds)
173 {
174         return (NSEC_PER_SEC * (int64_t)seconds);
175 }
176
177 /*
178  * internal helper function used by cfs_fs_time_before*()
179  */
180 static inline int64_t __cfs_fs_time_flat(cfs_fs_time_t *t)
181 {
182         return ((int64_t)t->tv_sec)*NSEC_PER_SEC + t->tv_usec*NSEC_PER_USEC;
183 }
184
185 static inline int cfs_fs_time_before(cfs_fs_time_t *t1, cfs_fs_time_t *t2)
186 {
187         return __cfs_fs_time_flat(t1) - __cfs_fs_time_flat(t2) < 0;
188 }
189
190 static inline int cfs_fs_time_beforeq(cfs_fs_time_t *t1, cfs_fs_time_t *t2)
191 {
192         return __cfs_fs_time_flat(t1) - __cfs_fs_time_flat(t2) <= 0;
193 }
194
195 static inline time_t cfs_duration_sec(cfs_duration_t d)
196 {
197         return d / NSEC_PER_SEC;
198 }
199
200 static inline void cfs_duration_usec(cfs_duration_t d, struct timeval *s)
201 {
202         s->tv_sec = d / NSEC_PER_SEC;
203         s->tv_usec = (d - ((int64_t)s->tv_sec) * NSEC_PER_SEC) / NSEC_PER_USEC;
204 }
205
206 static inline void cfs_duration_nsec(cfs_duration_t d, struct timespec *s)
207 {
208         s->tv_sec = d / NSEC_PER_SEC;
209         s->tv_nsec = d - ((int64_t)s->tv_sec) * NSEC_PER_SEC;
210 }
211
212 #define cfs_time_current_64 cfs_time_current
213 #define cfs_time_add_64     cfs_time_add
214 #define cfs_time_shift_64   cfs_time_shift
215 #define cfs_time_before_64  cfs_time_before
216 #define cfs_time_beforeq_64 cfs_time_beforeq
217
218 /* 
219  * One jiffy (in nanoseconds)
220  *
221  * osfmk/kern/sched_prim.c
222  * #define DEFAULT_PREEMPTION_RATE      100
223  */
224 #define CFS_TICK                (NSEC_PER_SEC / (u_int64_t)100)
225
226 #define LTIME_S(t)              (t)
227
228 /* __KERNEL__ */
229 #else
230
231 /*
232  * User level
233  */
234 #include <libcfs/user-time.h>
235
236 /* __KERNEL__ */
237 #endif
238
239 /* __LIBCFS_DARWIN_DARWIN_TIME_H__ */
240 #endif
241 /*
242  * Local variables:
243  * c-indentation-style: "K&R"
244  * c-basic-offset: 8
245  * tab-width: 8
246  * fill-column: 80
247  * scroll-step: 1
248  * End:
249  */