summaryrefslogtreecommitdiff
path: root/lib/libc/sys/microtime.c
blob: e85a86a78de8ab5dd7fe65818791a93630fac941 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*	$OpenBSD: microtime.c,v 1.1 2020/07/06 13:33:06 pirofti Exp $ */
/*
 * Copyright (c) 2000 Poul-Henning Kamp <phk@FreeBSD.org>
 * Copyright (c) 2020 Paul Irofti <paul@irofti.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/atomic.h>
#include <sys/timetc.h>

#include <time.h>

/*
 * Return the difference between the timehands' counter value now and what
 * was when we copied it to the timehands' offset_count.
 */
static inline int
tc_delta(struct timekeep *tk, u_int *delta)
{
	u_int tc;

	if (_tc_get_timecount(tk, &tc))
		return -1;
	*delta = (tc - tk->tk_offset_count) & tk->tk_counter_mask;
	return 0;
}

static int
binuptime(struct bintime *bt, struct timekeep *tk)
{
	u_int gen, delta;

	do {
		gen = tk->tk_generation;
		membar_consumer();
		*bt = tk->tk_offset;
		if (tc_delta(tk, &delta))
			return -1;
		bintimeaddfrac(bt, tk->tk_scale * delta, bt);
		membar_consumer();
	} while (gen == 0 || gen != tk->tk_generation);

	return 0;
}

static int
binruntime(struct bintime *bt, struct timekeep *tk)
{
	u_int gen, delta;

	do {
		gen = tk->tk_generation;
		membar_consumer();
		if (tc_delta(tk, &delta))
			return -1;
		bintimeaddfrac(&tk->tk_offset, tk->tk_scale * delta, bt);
		bintimesub(bt, &tk->tk_naptime, bt);
		membar_consumer();
	} while (gen == 0 || gen != tk->tk_generation);

	return 0;
}

static int
bintime(struct bintime *bt, struct timekeep *tk)
{
	u_int gen, delta;

	do {
		gen = tk->tk_generation;
		membar_consumer();
		*bt = tk->tk_offset;
		if (tc_delta(tk, &delta))
			return -1;
		bintimeaddfrac(bt, tk->tk_scale * delta, bt);
		bintimeadd(bt, &tk->tk_boottime, bt);
		membar_consumer();
	} while (gen == 0 || gen != tk->tk_generation);

	return 0;
}

int
_microtime(struct timeval *tvp, struct timekeep *tk)
{
	struct bintime bt;

	if (bintime(&bt, tk))
		return -1;
	BINTIME_TO_TIMEVAL(&bt, tvp);
	return 0;
}

int
_nanotime(struct timespec *tsp, struct timekeep *tk)
{
	struct bintime bt;

	if (bintime(&bt, tk))
		return -1;
	BINTIME_TO_TIMESPEC(&bt, tsp);
	return 0;
}

int
_nanoruntime(struct timespec *ts, struct timekeep *tk)
{
	struct bintime bt;

	if (binruntime(&bt, tk))
		return -1;
	BINTIME_TO_TIMESPEC(&bt, ts);
	return 0;
}


int
_nanouptime(struct timespec *tsp, struct timekeep *tk)
{
	struct bintime bt;

	if (binuptime(&bt, tk))
		return -1;
	BINTIME_TO_TIMESPEC(&bt, tsp);
	return 0;
}