1 .\" $NetBSD: timecounter.9,v 1.6 2008/11/28 11:16:26 tsutsui Exp $
2 .\" $OpenBSD: tc_init.9,v 1.4 2007/05/31 19:20:01 jmc Exp $
4 .\" Copyright (c) 2004 Alexander Yurchenko <grange@openbsd.org>
6 .\" Permission to use, copy, modify, and distribute this software for any
7 .\" purpose with or without fee is hereby granted, provided that the above
8 .\" copyright notice and this permission notice appear in all copies.
10 .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 .\" Copyright (c) 2008 Izumi Tsutsui. All rights reserved.
20 .\" Redistribution and use in source and binary forms, with or without
21 .\" modification, are permitted provided that the following conditions
23 .\" 1. Redistributions of source code must retain the above copyright
24 .\" notice, this list of conditions and the following disclaimer.
25 .\" 2. Redistributions in binary form must reproduce the above copyright
26 .\" notice, this list of conditions and the following disclaimer in the
27 .\" documentation and/or other materials provided with the distribution.
29 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 .Nd machine-independent binary timescale
50 .Fn tc_init "struct timecounter *tc"
52 The timecounter interface is a machine-independent implementation
53 of a binary timescale using whatever hardware support is at hand
56 A timecounter is a binary counter which has two properties:
57 .Bl -bullet -offset indent
59 it runs at a fixed, known frequency
61 it has sufficient bits to not roll over in less than approximately
64 seconds) (the value 2 here is really 1 + delta, for some
65 indeterminate value of delta)
68 The interface between the hardware which implements a timecounter and the
69 machine-independent code which uses this to keep track of time is a
72 .Bd -literal -offset indent
74 timecounter_get_t *tc_get_timecount;
75 timecounter_pps_t *tc_poll_pps;
76 u_int tc_counter_mask;
77 u_int64_t tc_frequency;
81 struct timecounter *tc_next;
87 structure are described below.
88 .Bl -tag -width indent
89 .It Fn "u_int (*tc_get_timecount)" "struct timecounter *"
90 This function reads the counter.
91 It is not required to mask any unimplemented bits out, as long as they
93 .It Fn "void (*tc_poll_pps)" "struct timecounter *"
94 This function is optional and can be set to
96 It will be called whenever the timecounter is rewound, and is intended
97 to check for PPS events.
98 Normal hardware does not need it but timecounters which latch PPS in
100 .It Va tc_counter_mask
101 This mask should mask off any unimplemented bits.
103 Frequency of the counter in Hz.
105 Name of the timecounter.
106 Can be any nul-terminated string.
108 Used to determine if this timecounter is better than another timecounter \-
111 .Dq only use at explicit request .
113 Pointer to the timecounter's private parts.
118 To register a new timecounter,
119 the hardware device driver should fill a
121 structure with appropriate values and call the
123 function, giving a pointer to the structure as a
127 The timestamp format used in the machine independent timecounter
131 .Bd -literal -offset indent
140 field records the number of seconds as well as the
142 field in the traditional
151 field records fractional seconds represented in a fully 64 bit integer,
152 i.e. it goes all the way from
155 .Li 0xFFFFFFFFFFFFFFFF
157 The effective resolution of the
159 value depends on a frequency of the machine dependent timecounter source.
163 format is a binary number, not a pseudo-decimal number,
164 so it can be used as a simple binary counter
165 without expensive 64 bit arithmetics.
167 The timecounter framework is implemented in the file
168 .Pa sys/kern/kern_tc.c .
172 structure and related functions are defined in the file
175 .Xr clock_settime 2 ,
183 .Xr getmicrouptime 9 ,
185 .Xr getnanouptime 9 ,
193 .%A "Poul-Henning Kamp"
194 .%T "Timecounters: Efficient and precise timekeeping in SMP kernels"
195 .%J "Proceedings of EuroBSDCon 2002, Amsterdam"
196 .%U http://phk.freebsd.dk/pubs/timecounter.pdf
199 The timecounter interface first appeared in
203 by Frank Kardel and Simon Burge.