Whamcloud - gitweb
946c2be7d52006004432e7ecaa2b9c1e66ad8de3
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_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
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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/libcfs_time.h
37  *
38  * Time functions.
39  *
40  */
41
42 #ifndef __LIBCFS_TIME_H__
43 #define __LIBCFS_TIME_H__
44 /*
45  * generic time manipulation functions.
46  */
47
48 static inline cfs_time_t cfs_time_add(cfs_time_t t, cfs_duration_t d)
49 {
50         return (cfs_time_t)(t + d);
51 }
52
53 static inline cfs_duration_t cfs_time_sub(cfs_time_t t1, cfs_time_t t2)
54 {
55         return (cfs_time_t)(t1 - t2);
56 }
57
58 static inline int cfs_time_before(cfs_time_t t1, cfs_time_t t2)
59 {
60         return time_before(t1, t2);
61 }
62
63 static inline int cfs_time_beforeq(cfs_time_t t1, cfs_time_t t2)
64 {
65         return time_before_eq(t1, t2);
66 }
67
68 static inline int cfs_time_after(cfs_time_t t1, cfs_time_t t2)
69 {
70         return cfs_time_before(t2, t1);
71 }
72
73 static inline int cfs_time_aftereq(cfs_time_t t1, cfs_time_t t2)
74 {
75         return cfs_time_beforeq(t2, t1);
76 }
77
78
79 static inline cfs_time_t cfs_time_shift(int seconds)
80 {
81         return cfs_time_add(cfs_time_current(), cfs_time_seconds(seconds));
82 }
83
84 static inline long cfs_timeval_sub(struct timeval *large, struct timeval *small,
85                                    struct timeval *result)
86 {
87         long r = (long) (
88                 (large->tv_sec - small->tv_sec) * ONE_MILLION +
89                 (large->tv_usec - small->tv_usec));
90         if (result != NULL) {
91                 result->tv_usec = r % ONE_MILLION;
92                 result->tv_sec = r / ONE_MILLION;
93         }
94         return r;
95 }
96
97 static inline void cfs_slow_warning(cfs_time_t now, int seconds, char *msg)
98 {
99         if (cfs_time_after(cfs_time_current(),
100                            cfs_time_add(now, cfs_time_seconds(15))))
101                 CERROR("slow %s "CFS_TIME_T" sec\n", msg,
102                        cfs_duration_sec(cfs_time_sub(cfs_time_current(),now)));
103 }
104
105 #define CFS_RATELIMIT(seconds)                                  \
106 ({                                                              \
107         /*                                                      \
108          * XXX nikita: non-portable initializer                 \
109          */                                                     \
110         static time_t __next_message = 0;                       \
111         int result;                                             \
112                                                                 \
113         if (cfs_time_after(cfs_time_current(), __next_message)) \
114                 result = 1;                                     \
115         else {                                                  \
116                 __next_message = cfs_time_shift(seconds);       \
117                 result = 0;                                     \
118         }                                                       \
119         result;                                                 \
120 })
121
122 /*
123  * helper function similar to do_gettimeofday() of Linux kernel
124  */
125 static inline void cfs_fs_timeval(struct timeval *tv)
126 {
127         cfs_fs_time_t time;
128
129         cfs_fs_time_current(&time);
130         cfs_fs_time_usec(&time, tv);
131 }
132
133 /*
134  * return valid time-out based on user supplied one. Currently we only check
135  * that time-out is not shorted than allowed.
136  */
137 static inline cfs_duration_t cfs_timeout_cap(cfs_duration_t timeout)
138 {
139         if (timeout < CFS_TICK)
140                 timeout = CFS_TICK;
141         return timeout;
142 }
143
144 #endif