1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/include/libcfs/libcfs_time.h
42 #ifndef __LIBCFS_TIME_H__
43 #define __LIBCFS_TIME_H__
45 * generic time manipulation functions.
48 static inline cfs_time_t cfs_time_add(cfs_time_t t, cfs_duration_t d)
50 return (cfs_time_t)(t + d);
53 static inline cfs_duration_t cfs_time_sub(cfs_time_t t1, cfs_time_t t2)
55 return (cfs_time_t)(t1 - t2);
58 static inline int cfs_time_after(cfs_time_t t1, cfs_time_t t2)
60 return cfs_time_before(t2, t1);
63 static inline int cfs_time_aftereq(cfs_time_t t1, cfs_time_t t2)
65 return cfs_time_beforeq(t2, t1);
69 static inline cfs_time_t cfs_time_shift(int seconds)
71 return cfs_time_add(cfs_time_current(), cfs_time_seconds(seconds));
74 static inline long cfs_timeval_sub(struct timeval *large, struct timeval *small,
75 struct timeval *result)
78 (large->tv_sec - small->tv_sec) * ONE_MILLION +
79 (large->tv_usec - small->tv_usec));
81 result->tv_usec = r % ONE_MILLION;
82 result->tv_sec = r / ONE_MILLION;
87 static inline void cfs_slow_warning(cfs_time_t now, int seconds, char *msg)
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)));
95 #define CFS_RATELIMIT(seconds) \
98 * XXX nikita: non-portable initializer \
100 static time_t __next_message = 0; \
103 if (cfs_time_after(cfs_time_current(), __next_message)) \
106 __next_message = cfs_time_shift(seconds); \
113 * helper function similar to do_gettimeofday() of Linux kernel
115 static inline void cfs_fs_timeval(struct timeval *tv)
119 cfs_fs_time_current(&time);
120 cfs_fs_time_usec(&time, tv);
124 * return valid time-out based on user supplied one. Currently we only check
125 * that time-out is not shorted than allowed.
127 static inline cfs_duration_t cfs_timeout_cap(cfs_duration_t timeout)
129 if (timeout < CFS_TICK)