RTEMS 6.1-rc6
Loading...
Searching...
No Matches
timeffc.h
Go to the documentation of this file.
1
10/*-
11 * SPDX-License-Identifier: BSD-2-Clause
12 *
13 * Copyright (c) 2011 The University of Melbourne
14 * All rights reserved.
15 *
16 * This software was developed by Julien Ridoux at the University of Melbourne
17 * under sponsorship from the FreeBSD Foundation.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41#ifndef _SYS_TIMEFF_H_
42#define _SYS_TIMEFF_H_
43
44#include <sys/_ffcounter.h>
45
46/*
47 * Feed-forward clock estimate
48 * Holds time mark as a ffcounter and conversion to bintime based on current
49 * timecounter period and offset estimate passed by the synchronization daemon.
50 * Provides time of last daemon update, clock status and bound on error.
51 */
53 struct bintime update_time; /* Time of last estimates update. */
54 ffcounter update_ffcount; /* Counter value at last update. */
55 ffcounter leapsec_next; /* Counter value of next leap second. */
56 uint64_t period; /* Estimate of counter period. */
57 uint32_t errb_abs; /* Bound on absolute clock error [ns]. */
58 uint32_t errb_rate; /* Bound on counter rate error [ps/s]. */
59 uint32_t status; /* Clock status. */
60 int16_t leapsec_total; /* All leap seconds seen so far. */
61 int8_t leapsec; /* Next leap second (in {-1,0,1}). */
62};
63
64#if __BSD_VISIBLE
65#ifdef _KERNEL
66
67#ifndef __rtems__
68/* Define the kern.sysclock sysctl tree. */
69SYSCTL_DECL(_kern_sysclock);
70
71/* Define the kern.sysclock.ffclock sysctl tree. */
72SYSCTL_DECL(_kern_sysclock_ffclock);
73#endif /* __rtems__ */
74
75/*
76 * Index into the sysclocks array for obtaining the ASCII name of a particular
77 * sysclock.
78 */
79#define SYSCLOCK_FBCK 0
80#define SYSCLOCK_FFWD 1
81extern int sysclock_active;
82
83/*
84 * Parameters of counter characterisation required by feed-forward algorithms.
85 */
86#define FFCLOCK_SKM_SCALE 1024
87
88/*
89 * Feed-forward clock status
90 */
91#define FFCLOCK_STA_UNSYNC 1
92#define FFCLOCK_STA_WARMUP 2
93
94/*
95 * Flags for use by sysclock_snap2bintime() and various ffclock_ functions to
96 * control how the timecounter hardware is read and how the hardware snapshot is
97 * converted into absolute time.
98 * {FB|FF}CLOCK_FAST: Do not read the hardware counter, instead using the
99 * value at last tick. The time returned has a resolution
100 * of the kernel tick timer (1/hz [s]).
101 * FFCLOCK_LERP: Linear interpolation of ffclock time to guarantee
102 * monotonic time.
103 * FFCLOCK_LEAPSEC: Include leap seconds.
104 * {FB|FF}CLOCK_UPTIME: Time stamp should be relative to system boot, not epoch.
105 */
106#define FFCLOCK_FAST 0x00000001
107#define FFCLOCK_LERP 0x00000002
108#define FFCLOCK_LEAPSEC 0x00000004
109#define FFCLOCK_UPTIME 0x00000008
110#define FFCLOCK_MASK 0x0000ffff
111
112#define FBCLOCK_FAST 0x00010000 /* Currently unused. */
113#define FBCLOCK_UPTIME 0x00020000
114#define FBCLOCK_MASK 0xffff0000
115
116/*
117 * Feedback clock specific info structure. The feedback clock's estimation of
118 * clock error is an absolute figure determined by the NTP algorithm. The status
119 * is determined by the userland daemon.
120 */
121struct fbclock_info {
122 struct bintime error;
123 struct bintime tick_time;
124 uint64_t th_scale;
125 int status;
126};
127
128/*
129 * Feed-forward clock specific info structure. The feed-forward clock's
130 * estimation of clock error is an upper bound, which although potentially
131 * looser than the feedback clock equivalent, is much more reliable. The status
132 * is determined by the userland daemon.
133 */
134struct ffclock_info {
135 struct bintime error;
136 struct bintime tick_time;
137 struct bintime tick_time_lerp;
138 uint64_t period;
139 uint64_t period_lerp;
140 int leapsec_adjustment;
141 int status;
142};
143
144/*
145 * Snapshot of system clocks and related information. Holds time read from each
146 * clock based on a single read of the active hardware timecounter, as well as
147 * respective clock information such as error estimates and the ffcounter value
148 * at the time of the read.
149 */
150struct sysclock_snap {
151 struct fbclock_info fb_info;
152 struct ffclock_info ff_info;
153 ffcounter ffcount;
154 unsigned int delta;
155 int sysclock_active;
156};
157
158/* Take a snapshot of the system clocks and related information. */
159void sysclock_getsnapshot(struct sysclock_snap *clock_snap, int fast);
160
161/* Convert a timestamp from the selected system clock into bintime. */
162int sysclock_snap2bintime(struct sysclock_snap *cs, struct bintime *bt,
163 int whichclock, uint32_t flags);
164
165/* Resets feed-forward clock from RTC */
166void ffclock_reset_clock(struct timespec *ts);
167
168/*
169 * Return the current value of the feed-forward clock counter. Essential to
170 * measure time interval in counter units. If a fast timecounter is used by the
171 * system, may also allow fast but accurate timestamping.
172 */
173void ffclock_read_counter(ffcounter *ffcount);
174
175/*
176 * Retrieve feed-forward counter value and time of last kernel tick. This
177 * accepts the FFCLOCK_LERP flag.
178 */
179void ffclock_last_tick(ffcounter *ffcount, struct bintime *bt, uint32_t flags);
180
181/*
182 * Low level routines to convert a counter timestamp into absolute time and a
183 * counter timestamp interval into an interval in seconds. The absolute time
184 * conversion accepts the FFCLOCK_LERP flag.
185 */
186void ffclock_convert_abs(ffcounter ffcount, struct bintime *bt, uint32_t flags);
187void ffclock_convert_diff(ffcounter ffdelta, struct bintime *bt);
188
189/*
190 * Feed-forward clock routines.
191 *
192 * These functions rely on the timecounters and ffclock_estimates stored in
193 * fftimehands. Note that the error_bound parameter is not the error of the
194 * clock but an upper bound on the error of the absolute time or time interval
195 * returned.
196 *
197 * ffclock_abstime(): retrieves current time as counter value and convert this
198 * timestamp in seconds. The value (in seconds) of the converted timestamp
199 * depends on the flags passed: for a given counter value, different
200 * conversions are possible. Different clock models can be selected by
201 * combining flags (for example (FFCLOCK_LERP|FFCLOCK_UPTIME) produces
202 * linearly interpolated uptime).
203 * ffclock_difftime(): computes a time interval in seconds based on an interval
204 * measured in ffcounter units. This should be the preferred way to measure
205 * small time intervals very accurately.
206 */
207void ffclock_abstime(ffcounter *ffcount, struct bintime *bt,
208 struct bintime *error_bound, uint32_t flags);
209void ffclock_difftime(ffcounter ffdelta, struct bintime *bt,
210 struct bintime *error_bound);
211
212/*
213 * Wrapper routines to return current absolute time using the feed-forward
214 * clock. These functions are named after those defined in <sys/time.h>, which
215 * contains a description of the original ones.
216 */
217void ffclock_bintime(struct bintime *bt);
218void ffclock_nanotime(struct timespec *tsp);
219void ffclock_microtime(struct timeval *tvp);
220
221void ffclock_getbintime(struct bintime *bt);
222void ffclock_getnanotime(struct timespec *tsp);
223void ffclock_getmicrotime(struct timeval *tvp);
224
225void ffclock_binuptime(struct bintime *bt);
226void ffclock_nanouptime(struct timespec *tsp);
227void ffclock_microuptime(struct timeval *tvp);
228
229void ffclock_getbinuptime(struct bintime *bt);
230void ffclock_getnanouptime(struct timespec *tsp);
231void ffclock_getmicrouptime(struct timeval *tvp);
232
233/*
234 * Wrapper routines to convert a time interval specified in ffcounter units into
235 * seconds using the current feed-forward clock estimates.
236 */
237void ffclock_bindifftime(ffcounter ffdelta, struct bintime *bt);
238void ffclock_nanodifftime(ffcounter ffdelta, struct timespec *tsp);
239void ffclock_microdifftime(ffcounter ffdelta, struct timeval *tvp);
240
241/*
242 * When FFCLOCK is enabled in the kernel, [get]{bin,nano,micro}[up]time() become
243 * wrappers around equivalent feedback or feed-forward functions. Provide access
244 * outside of kern_tc.c to the feedback clock equivalent functions for
245 * specialised use i.e. these are not for general consumption.
246 */
247void fbclock_bintime(struct bintime *bt);
248void fbclock_nanotime(struct timespec *tsp);
249void fbclock_microtime(struct timeval *tvp);
250
251void fbclock_getbintime(struct bintime *bt);
252void fbclock_getnanotime(struct timespec *tsp);
253void fbclock_getmicrotime(struct timeval *tvp);
254
255void fbclock_binuptime(struct bintime *bt);
256void fbclock_nanouptime(struct timespec *tsp);
257void fbclock_microuptime(struct timeval *tvp);
258
259void fbclock_getbinuptime(struct bintime *bt);
260void fbclock_getnanouptime(struct timespec *tsp);
261void fbclock_getmicrouptime(struct timeval *tvp);
262
263/*
264 * Public system clock wrapper API which allows consumers to select which clock
265 * to obtain time from, independent of the current default system clock. These
266 * wrappers should be used instead of directly calling the underlying fbclock_
267 * or ffclock_ functions.
268 */
269static inline void
270bintime_fromclock(struct bintime *bt, int whichclock)
271{
272
273 if (whichclock == SYSCLOCK_FFWD)
274 ffclock_bintime(bt);
275 else
276 fbclock_bintime(bt);
277}
278
279static inline void
280nanotime_fromclock(struct timespec *tsp, int whichclock)
281{
282
283 if (whichclock == SYSCLOCK_FFWD)
284 ffclock_nanotime(tsp);
285 else
286 fbclock_nanotime(tsp);
287}
288
289static inline void
290microtime_fromclock(struct timeval *tvp, int whichclock)
291{
292
293 if (whichclock == SYSCLOCK_FFWD)
294 ffclock_microtime(tvp);
295 else
296 fbclock_microtime(tvp);
297}
298
299static inline void
300getbintime_fromclock(struct bintime *bt, int whichclock)
301{
302
303 if (whichclock == SYSCLOCK_FFWD)
304 ffclock_getbintime(bt);
305 else
306 fbclock_getbintime(bt);
307}
308
309static inline void
310getnanotime_fromclock(struct timespec *tsp, int whichclock)
311{
312
313 if (whichclock == SYSCLOCK_FFWD)
314 ffclock_getnanotime(tsp);
315 else
316 fbclock_getnanotime(tsp);
317}
318
319static inline void
320getmicrotime_fromclock(struct timeval *tvp, int whichclock)
321{
322
323 if (whichclock == SYSCLOCK_FFWD)
324 ffclock_getmicrotime(tvp);
325 else
326 fbclock_getmicrotime(tvp);
327}
328
329static inline void
330binuptime_fromclock(struct bintime *bt, int whichclock)
331{
332
333 if (whichclock == SYSCLOCK_FFWD)
334 ffclock_binuptime(bt);
335 else
336 fbclock_binuptime(bt);
337}
338
339static inline void
340nanouptime_fromclock(struct timespec *tsp, int whichclock)
341{
342
343 if (whichclock == SYSCLOCK_FFWD)
344 ffclock_nanouptime(tsp);
345 else
346 fbclock_nanouptime(tsp);
347}
348
349static inline void
350microuptime_fromclock(struct timeval *tvp, int whichclock)
351{
352
353 if (whichclock == SYSCLOCK_FFWD)
354 ffclock_microuptime(tvp);
355 else
356 fbclock_microuptime(tvp);
357}
358
359static inline void
360getbinuptime_fromclock(struct bintime *bt, int whichclock)
361{
362
363 if (whichclock == SYSCLOCK_FFWD)
364 ffclock_getbinuptime(bt);
365 else
366 fbclock_getbinuptime(bt);
367}
368
369static inline void
370getnanouptime_fromclock(struct timespec *tsp, int whichclock)
371{
372
373 if (whichclock == SYSCLOCK_FFWD)
374 ffclock_getnanouptime(tsp);
375 else
376 fbclock_getnanouptime(tsp);
377}
378
379static inline void
380getmicrouptime_fromclock(struct timeval *tvp, int whichclock)
381{
382
383 if (whichclock == SYSCLOCK_FFWD)
384 ffclock_getmicrouptime(tvp);
385 else
386 fbclock_getmicrouptime(tvp);
387}
388
389#else /* !_KERNEL */
390
391/* Feed-Forward Clock system calls. */
392__BEGIN_DECLS
393int ffclock_getcounter(ffcounter *ffcount);
394int ffclock_getestimate(struct ffclock_estimate *cest);
395int ffclock_setestimate(struct ffclock_estimate *cest);
396__END_DECLS
397
398#endif /* _KERNEL */
399#endif /* __BSD_VISIBLE */
400#endif /* _SYS_TIMEFF_H_ */
This header file provides interfaces of the feed-forward clock counter.
Definition: timeffc.h:52