Whamcloud - gitweb
96a1d75b97668c799c1bdf448cd62e19873e243c
[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_after(cfs_time_t t1, cfs_time_t t2)
59 {
60         return cfs_time_before(t2, t1);
61 }
62
63 static inline int cfs_time_aftereq(cfs_time_t t1, cfs_time_t t2)
64 {
65         return cfs_time_beforeq(t2, t1);
66 }
67
68
69 static inline cfs_time_t cfs_time_shift(int seconds)
70 {
71         return cfs_time_add(cfs_time_current(), cfs_time_seconds(seconds));
72 }
73
74 static inline long cfs_timeval_sub(struct timeval *large, struct timeval *small,
75                                    struct timeval *result)
76 {
77         long r = (long) (
78                 (large->tv_sec - small->tv_sec) * ONE_MILLION +
79                 (large->tv_usec - small->tv_usec));
80         if (result != NULL) {
81                 result->tv_usec = r % ONE_MILLION;
82                 result->tv_sec = r / ONE_MILLION;
83         }
84         return r;
85 }
86
87 static inline void cfs_slow_warning(cfs_time_t now, int seconds, char *msg)
88 {
89         if (cfs_time_after(cfs_time_current(),
90                            cfs_time_add(now, cfs_time_seconds(15))))
91                 CERROR("slow %s "CFS_TIME_T" sec\n", msg,
92                        cfs_duration_sec(cfs_time_sub(cfs_time_current(),now)));
93 }
94
95 #define CFS_RATELIMIT(seconds)                                  \
96 ({                                                              \
97         /*                                                      \
98          * XXX nikita: non-portable initializer                 \
99          */                                                     \
100         static time_t __next_message = 0;                       \
101         int result;                                             \
102                                                                 \
103         if (cfs_time_after(cfs_time_current(), __next_message)) \
104                 result = 1;                                     \
105         else {                                                  \
106                 __next_message = cfs_time_shift(seconds);       \
107                 result = 0;                                     \
108         }                                                       \
109         result;                                                 \
110 })
111
112 /*
113  * helper function similar to do_gettimeofday() of Linux kernel
114  */
115 static inline void cfs_fs_timeval(struct timeval *tv)
116 {
117         cfs_fs_time_t time;
118
119         cfs_fs_time_current(&time);
120         cfs_fs_time_usec(&time, tv);
121 }
122
123 /*
124  * return valid time-out based on user supplied one. Currently we only check
125  * that time-out is not shorted than allowed.
126  */
127 static inline cfs_duration_t cfs_timeout_cap(cfs_duration_t timeout)
128 {
129         if (timeout < CFS_TICK)
130                 timeout = CFS_TICK;
131         return timeout;
132 }
133
134 #endif