Whamcloud - gitweb
5cbaecb5f2cf47ea3653e400d35b61b9d684d967
[fs/lustre-release.git] / libcfs / include / libcfs / user-time.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/include/libcfs/user-time.h
37  *
38  * Implementation of portable time API for user-level.
39  *
40  * Author: Nikita Danilov <nikita@clusterfs.com>
41  */
42
43 #ifndef __LIBCFS_USER_TIME_H__
44 #define __LIBCFS_USER_TIME_H__
45
46 #ifndef __LIBCFS_LIBCFS_H__
47 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
48 #endif
49
50 /* Portable time API */
51
52 /*
53  * Platform provides three opaque data-types:
54  *
55  *  cfs_time_t        represents point in time. This is internal kernel
56  *                    time rather than "wall clock". This time bears no
57  *                    relation to gettimeofday().
58  *
59  *  cfs_duration_t    represents time interval with resolution of internal
60  *                    platform clock
61  *
62  *  cfs_fs_time_t     represents instance in world-visible time. This is
63  *                    used in file-system time-stamps
64  *
65  *  cfs_time_t     cfs_time_current(void);
66  *  cfs_time_t     cfs_time_add    (cfs_time_t, cfs_duration_t);
67  *  cfs_duration_t cfs_time_sub    (cfs_time_t, cfs_time_t);
68  *  int            cfs_time_before (cfs_time_t, cfs_time_t);
69  *  int            cfs_time_beforeq(cfs_time_t, cfs_time_t);
70  *
71  *  cfs_duration_t cfs_duration_build(int64_t);
72  *
73  *  time_t         cfs_duration_sec (cfs_duration_t);
74  *  void           cfs_duration_usec(cfs_duration_t, struct timeval *);
75  *  void           cfs_duration_nsec(cfs_duration_t, struct timespec *);
76  *
77  *  void           cfs_fs_time_current(cfs_fs_time_t *);
78  *  time_t         cfs_fs_time_sec    (cfs_fs_time_t *);
79  *  void           cfs_fs_time_usec   (cfs_fs_time_t *, struct timeval *);
80  *  void           cfs_fs_time_nsec   (cfs_fs_time_t *, struct timespec *);
81  *  int            cfs_fs_time_before (cfs_fs_time_t *, cfs_fs_time_t *);
82  *  int            cfs_fs_time_beforeq(cfs_fs_time_t *, cfs_fs_time_t *);
83  *
84  *  CFS_TIME_FORMAT
85  *  CFS_DURATION_FORMAT
86  *
87  */
88
89 #ifndef __KERNEL__
90
91 #define ONE_BILLION ((u_int64_t)1000000000)
92 #define ONE_MILLION 1000000
93
94 /*
95  * Liblustre. time(2) based implementation.
96  */
97
98 typedef time_t cfs_fs_time_t;
99 typedef time_t cfs_time_t;
100 typedef long cfs_duration_t;
101
102 /* looks like linux */
103 #define time_after(a, b) ((long)(b) - (long)(a) < 0)
104 #define time_before(a, b) time_after(b,a)
105 #define time_after_eq(a,b)      ((long)(a) - (long)(b) >= 0)
106 #define time_before_eq(a,b) time_after_eq(b,a)
107
108 static inline cfs_time_t cfs_time_current(void)
109 {
110         return time(NULL);
111 }
112
113 static inline cfs_duration_t cfs_time_seconds(int seconds)
114 {
115         return seconds;
116 }
117
118 static inline time_t cfs_time_current_sec(void)
119 {
120         return cfs_time_seconds(cfs_time_current());
121 }
122
123 static inline cfs_duration_t cfs_duration_build(int64_t nano)
124 {
125         return (cfs_duration_t) (nano / ONE_BILLION);
126 }
127
128 static inline time_t cfs_duration_sec(cfs_duration_t d)
129 {
130         return d;
131 }
132
133 static inline void cfs_duration_usec(cfs_duration_t d, struct timeval *s)
134 {
135         s->tv_sec = d;
136         s->tv_usec = 0;
137 }
138
139 static inline void cfs_duration_nsec(cfs_duration_t d, struct timespec *s)
140 {
141         s->tv_sec = d;
142         s->tv_nsec = 0;
143 }
144
145 static inline void cfs_fs_time_current(cfs_fs_time_t *t)
146 {
147         time(t);
148 }
149
150 static inline time_t cfs_fs_time_sec(cfs_fs_time_t *t)
151 {
152         return *t;
153 }
154
155 static inline void cfs_fs_time_usec(cfs_fs_time_t *t, struct timeval *v)
156 {
157         v->tv_sec = *t;
158         v->tv_usec = 0;
159 }
160
161 static inline void cfs_fs_time_nsec(cfs_fs_time_t *t, struct timespec *s)
162 {
163         s->tv_sec = *t;
164         s->tv_nsec = 0;
165 }
166
167 static inline int cfs_fs_time_before(cfs_fs_time_t *t1, cfs_fs_time_t *t2)
168 {
169         return *t1 < *t2;
170 }
171
172 static inline int cfs_fs_time_beforeq(cfs_fs_time_t *t1, cfs_fs_time_t *t2)
173 {
174         return *t1 <= *t2;
175 }
176
177 #define CFS_TICK                (1)
178
179 #define cfs_time_current_64 cfs_time_current
180 #define cfs_time_add_64     cfs_time_add
181 #define cfs_time_shift_64   cfs_time_shift
182 #define cfs_time_before_64  cfs_time_before
183 #define cfs_time_beforeq_64 cfs_time_beforeq
184
185 /* XXX needs to move to arch specific header or configured */
186 #ifndef CFS_TIME_T
187 #define CFS_TIME_T              "%lu"
188 #endif
189
190 #define CFS_DURATION_T          "%ld"
191
192 /* !__KERNEL__ */
193 #endif
194
195 /* __LIBCFS_USER_TIME_H__ */
196 #endif
197 /*
198  * Local variables:
199  * c-indentation-style: "K&R"
200  * c-basic-offset: 8
201  * tab-width: 8
202  * fill-column: 80
203  * scroll-step: 1
204  * End:
205  */