1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
14  */
15 
16 
17 #include <sys/types.h>
18 #include <sys/time.h>
19 #include <sys/thread.h>
20 #include <sys/proc.h>
21 
22 #include <sys/poll.h>
23 
24 #include <time.h>
25 
26 int hz = 1000;
27 int tick_per_msec = 0;
28 int msec_per_tick = 1;
29 int usec_per_tick = 1000;
30 int nsec_per_tick = 1000000;
31 time_t boot_time = 0;
32 
33 #pragma init(_boot_time_init)
34 static int
_boot_time_init(void)35 _boot_time_init(void)
36 {
37 	boot_time = time(NULL);
38 	return (0);
39 }
40 
41 clock_t
ddi_get_lbolt(void)42 ddi_get_lbolt(void)
43 {
44 	hrtime_t hrt;
45 
46 	hrt = gethrtime();
47 	return (hrt / nsec_per_tick);
48 }
49 
50 int64_t
ddi_get_lbolt64(void)51 ddi_get_lbolt64(void)
52 {
53 	hrtime_t hrt;
54 
55 	hrt = gethrtime();
56 	return (hrt / nsec_per_tick);
57 }
58 
59 hrtime_t
gethrtime_unscaled(void)60 gethrtime_unscaled(void)
61 {
62 	return (gethrtime());
63 }
64 
65 void
gethrestime(timespec_t * ts)66 gethrestime(timespec_t *ts)
67 {
68 	struct timeval tv;
69 
70 	(void) gettimeofday(&tv, NULL);
71 	ts->tv_sec = tv.tv_sec;
72 	ts->tv_nsec = tv.tv_usec * 1000;
73 }
74 
75 time_t
gethrestime_sec(void)76 gethrestime_sec(void)
77 {
78 	return (time(NULL));
79 }
80 
81 /* ARGSUSED */
82 void
scalehrtime(hrtime_t * t)83 scalehrtime(hrtime_t *t)
84 {
85 }
86 
87 /*
88  * These functions are blatently stolen from the kernel.
89  * See the dissertation in the comments preceding the
90  * hrt2ts() and ts2hrt() functions in:
91  *	uts/common/os/timers.c
92  */
93 void
hrt2ts(hrtime_t hrt,timespec_t * tsp)94 hrt2ts(hrtime_t hrt, timespec_t *tsp)
95 {
96 	uint32_t sec, nsec, tmp;
97 
98 	tmp = (uint32_t)(hrt >> 30);
99 	sec = tmp - (tmp >> 2);
100 	sec = tmp - (sec >> 5);
101 	sec = tmp + (sec >> 1);
102 	sec = tmp - (sec >> 6) + 7;
103 	sec = tmp - (sec >> 3);
104 	sec = tmp + (sec >> 1);
105 	sec = tmp + (sec >> 3);
106 	sec = tmp + (sec >> 4);
107 	tmp = (sec << 7) - sec - sec - sec;
108 	tmp = (tmp << 7) - tmp - tmp - tmp;
109 	tmp = (tmp << 7) - tmp - tmp - tmp;
110 	nsec = (uint32_t)hrt - (tmp << 9);
111 	while (nsec >= NANOSEC) {
112 		nsec -= NANOSEC;
113 		sec++;
114 	}
115 	tsp->tv_sec = (time_t)sec;
116 	tsp->tv_nsec = nsec;
117 }
118 
119 hrtime_t
ts2hrt(const timestruc_t * tsp)120 ts2hrt(const timestruc_t *tsp)
121 {
122 	hrtime_t hrt;
123 
124 	hrt = tsp->tv_sec;
125 	hrt = (hrt << 7) - hrt - hrt - hrt;
126 	hrt = (hrt << 7) - hrt - hrt - hrt;
127 	hrt = (hrt << 7) - hrt - hrt - hrt;
128 	hrt = (hrt << 9) + tsp->tv_nsec;
129 	return (hrt);
130 }
131