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