1 /*
2  *  Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3  *  Copyright (C) 2007 The Regents of the University of California.
4  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5  *  Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6  *  UCRL-CODE-235197
7  *
8  *  This file is part of the SPL, Solaris Porting Layer.
9  *
10  *  The SPL is free software; you can redistribute it and/or modify it
11  *  under the terms of the GNU General Public License as published by the
12  *  Free Software Foundation; either version 2 of the License, or (at your
13  *  option) any later version.
14  *
15  *  The SPL is distributed in the hope that it will be useful, but WITHOUT
16  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18  *  for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef _SPL_TIME_H
25 #define	_SPL_TIME_H
26 
27 #include <linux/module.h>
28 #include <linux/time.h>
29 #include <sys/types.h>
30 #include <sys/timer.h>
31 
32 #if defined(CONFIG_64BIT)
33 #define	TIME_MAX			INT64_MAX
34 #define	TIME_MIN			INT64_MIN
35 #else
36 #define	TIME_MAX			INT32_MAX
37 #define	TIME_MIN			INT32_MIN
38 #endif
39 
40 #define	SEC				1
41 #define	MILLISEC			1000
42 #define	MICROSEC			1000000
43 #define	NANOSEC				1000000000
44 
45 #define	MSEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / MILLISEC))
46 #define	NSEC2MSEC(n)	((n) / (NANOSEC / MILLISEC))
47 
48 #define	USEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / MICROSEC))
49 #define	NSEC2USEC(n)	((n) / (NANOSEC / MICROSEC))
50 
51 #define	NSEC2SEC(n)	((n) / (NANOSEC / SEC))
52 #define	SEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / SEC))
53 
54 static const int hz = HZ;
55 
56 typedef longlong_t		hrtime_t;
57 typedef struct timespec		timespec_t;
58 
59 #define	TIMESPEC_OVERFLOW(ts)		\
60 	((ts)->tv_sec < TIME_MIN || (ts)->tv_sec > TIME_MAX)
61 
62 #if defined(HAVE_INODE_TIMESPEC64_TIMES)
63 typedef struct timespec64	inode_timespec_t;
64 #else
65 typedef struct timespec		inode_timespec_t;
66 #endif
67 
68 /* Include for Lustre compatibility */
69 #define	timestruc_t	inode_timespec_t
70 
71 static inline void
72 gethrestime(inode_timespec_t *ts)
73 {
74 #if defined(HAVE_INODE_TIMESPEC64_TIMES)
75 
76 #if defined(HAVE_KTIME_GET_COARSE_REAL_TS64)
77 	ktime_get_coarse_real_ts64(ts);
78 #else
79 	*ts = current_kernel_time64();
80 #endif /* HAVE_KTIME_GET_COARSE_REAL_TS64 */
81 
82 #else
83 	*ts = current_kernel_time();
84 #endif
85 }
86 
87 static inline uint64_t
88 gethrestime_sec(void)
89 {
90 #if defined(HAVE_INODE_TIMESPEC64_TIMES)
91 #if defined(HAVE_KTIME_GET_COARSE_REAL_TS64)
92 	inode_timespec_t ts;
93 	ktime_get_coarse_real_ts64(&ts);
94 #else
95 	inode_timespec_t ts = current_kernel_time64();
96 #endif  /* HAVE_KTIME_GET_COARSE_REAL_TS64 */
97 
98 #else
99 	inode_timespec_t ts = current_kernel_time();
100 #endif
101 	return (ts.tv_sec);
102 }
103 
104 static inline hrtime_t
105 gethrtime(void)
106 {
107 #if defined(HAVE_KTIME_GET_RAW_TS64)
108 	struct timespec64 ts;
109 	ktime_get_raw_ts64(&ts);
110 #else
111 	struct timespec ts;
112 	getrawmonotonic(&ts);
113 #endif
114 	return (((hrtime_t)ts.tv_sec * NSEC_PER_SEC) + ts.tv_nsec);
115 }
116 
117 #endif  /* _SPL_TIME_H */
118