1/* $NetBSD: time.h,v 1.12 2018/06/03 05:55:08 chs Exp $ */
2
3/*-
4 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/cddl/compat/opensolaris/sys/time.h 296530 2016-03-08 18:28:24Z mav $
29 */
30
31#ifndef _OPENSOLARIS_SYS_TIME_H_
32#define _OPENSOLARIS_SYS_TIME_H_
33
34#include_next <sys/time.h>
35
36#define SEC 1
37#define MILLISEC 1000
38#define MICROSEC 1000000
39#define NANOSEC 1000000000
40#define TIME_MAX LLONG_MAX
41
42#define MSEC2NSEC(m) ((hrtime_t)(m) * (NANOSEC / MILLISEC))
43#define NSEC2MSEC(n) ((n) / (NANOSEC / MILLISEC))
44
45#define NSEC2SEC(n) ((n) / (NANOSEC / SEC))
46#define SEC2NSEC(m) ((hrtime_t)(m) * (NANOSEC / SEC))
47
48#ifndef __defined_hr_t
49#define __defined_hr_t
50typedef longlong_t hrtime_t;
51#endif
52
53#if defined(__i386__) || defined(__powerpc__)
54#define TIMESPEC_OVERFLOW(ts) \
55 ((ts)->tv_sec < INT32_MIN || (ts)->tv_sec > INT32_MAX)
56#else
57#define TIMESPEC_OVERFLOW(ts) \
58 ((ts)->tv_sec < INT64_MIN || (ts)->tv_sec > INT64_MAX)
59#endif
60
61#define SEC_TO_TICK(sec) ((sec) * hz)
62#define NSEC_TO_TICK(nsec) ((nsec) / (NANOSEC / hz))
63
64#ifdef _KERNEL
65
66static __inline hrtime_t
67gethrtime(void) {
68
69 struct timespec ts;
70 hrtime_t nsec;
71
72 getnanouptime(&ts);
73 nsec = (hrtime_t)ts.tv_sec * NANOSEC + ts.tv_nsec;
74 return (nsec);
75}
76
77#define gethrestime_sec() (time_second)
78#define gethrestime(ts) getnanotime(ts)
79#define gethrtime_waitfree() gethrtime()
80
81static inline int64_t
82ddi_get_lbolt64(void)
83{
84 struct timespec ts;
85 const int hz = 100;
86
87 getnanouptime(&ts);
88 return (int64_t)(SEC_TO_TICK(ts.tv_sec) + NSEC_TO_TICK(ts.tv_nsec));
89}
90
91#define ddi_get_lbolt() (clock_t)ddi_get_lbolt64()
92
93#else
94
95#ifdef __NetBSD__
96int clock_gettime(clockid_t, struct timespec *)
97 __RENAME(__clock_gettime50);
98#endif
99#ifdef __linux__
100#include <time.h>
101#endif
102
103static __inline hrtime_t gethrtime(void) {
104 struct timespec ts;
105 clock_gettime(CLOCK_REALTIME,&ts);
106 return (((u_int64_t) ts.tv_sec) * NANOSEC + ts.tv_nsec);
107}
108
109#define ddi_get_lbolt() (gethrtime() >> 23)
110#define ddi_get_lbolt64() (gethrtime() >> 23)
111
112#endif /* _KERNEL */
113
114#endif /* !_OPENSOLARIS_SYS_TIME_H_ */
115