Whamcloud - gitweb
* Landed portals:b_port_step as follows...
[fs/lustre-release.git] / lnet / 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  * 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 user-level.
23  *
24  */
25
26 #ifndef __LIBCFS_USER_TIME_H__
27 #define __LIBCFS_USER_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_duration_t cfs_time_minimal_timeout(void)
68  *
69  *  CFS_TIME_FORMAT
70  *  CFS_DURATION_FORMAT
71  *
72  */
73
74 #define ONE_BILLION ((u_int64_t)1000000000)
75 #define ONE_MILLION ((u_int64_t)   1000000)
76
77 #ifndef __KERNEL__
78
79 /*
80  * Liblustre. time(2) based implementation.
81  */
82
83 #include <sys/types.h>
84 #include <sys/time.h>
85 #include <time.h>
86
87 typedef time_t cfs_fs_time_t;
88 typedef time_t cfs_time_t;
89 typedef long cfs_duration_t;
90
91 static inline cfs_time_t cfs_time_current(void)
92 {
93         return time(NULL);
94 }
95
96 static inline cfs_duration_t cfs_time_seconds(int seconds)
97 {
98         return seconds;
99 }
100
101 static inline int cfs_time_before(cfs_time_t t1, cfs_time_t t2)
102 {
103         return t1 < t2;
104 }
105
106 static inline int cfs_time_beforeq(cfs_time_t t1, cfs_time_t t2)
107 {
108         return t1 <= t2;
109 }
110
111 static inline cfs_duration_t cfs_duration_build(int64_t nano)
112 {
113         return nano / ONE_BILLION;
114 }
115
116 static inline time_t cfs_duration_sec(cfs_duration_t d)
117 {
118         return d;
119 }
120
121 static inline void cfs_duration_usec(cfs_duration_t d, struct timeval *s)
122 {
123         s->tv_sec = d;
124         s->tv_usec = 0;
125 }
126
127 static inline void cfs_duration_nsec(cfs_duration_t d, struct timespec *s)
128 {
129         s->tv_sec = d;
130         s->tv_nsec = 0;
131 }
132
133 static inline void cfs_fs_time_current(cfs_fs_time_t *t)
134 {
135         time(t);
136 }
137
138 static inline time_t cfs_fs_time_sec(cfs_fs_time_t *t)
139 {
140         return *t;
141 }
142
143 static inline void cfs_fs_time_usec(cfs_fs_time_t *t, struct timeval *v)
144 {
145         v->tv_sec = *t;
146         v->tv_usec = 0;
147 }
148
149 static inline void cfs_fs_time_nsec(cfs_fs_time_t *t, struct timespec *s)
150 {
151         s->tv_sec = *t;
152         s->tv_nsec = 0;
153 }
154
155 static inline int cfs_fs_time_before(cfs_fs_time_t *t1, cfs_fs_time_t *t2)
156 {
157         return *t1 < *t2;
158 }
159
160 static inline int cfs_fs_time_beforeq(cfs_fs_time_t *t1, cfs_fs_time_t *t2)
161 {
162         return *t1 <= *t2;
163 }
164
165 static inline cfs_duration_t cfs_time_minimal_timeout(void)
166 {
167         return 1;
168 }
169
170 #define CFS_MIN_DELAY           (1)
171
172 static inline cfs_time_t cfs_time_add(cfs_time_t t, cfs_duration_t d)
173 {
174         return t + d;
175 }
176
177 static inline cfs_duration_t cfs_time_sub(cfs_time_t t1, cfs_time_t t2)
178 {
179         return t1 - t2;
180 }
181
182 #define CFS_TIME_T              "%lu"
183 #define CFS_DURATION_T          "%ld"
184
185 /* !__KERNEL__ */
186 #endif
187
188 /* __LIBCFS_USER_TIME_H__ */
189 #endif
190 /*
191  * Local variables:
192  * c-indentation-style: "K&R"
193  * c-basic-offset: 8
194  * tab-width: 8
195  * fill-column: 80
196  * scroll-step: 1
197  * End:
198  */