2 * Copyright (c) 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/usr.sbin/dntpd/client.c,v 1.6 2005/04/25 02:28:47 dillon Exp $
45 client_main(struct server_info **info_ary, int count)
47 struct server_info *best_off;
48 struct server_info *best_freq;
58 * Subtract the interval from poll_sleep and poll the client
61 for (i = 0; i < count; ++i)
62 client_poll(info_ary[i], min_sleep_opt);
65 * Find the best client (or synthesize one). A different client
66 * can be chosen for frequency and offset. Note in particular
67 * that offset counters and averaging code gets reset when an
68 * offset correction is made (otherwise the averaging history will
69 * cause later corrections to overshoot).
71 * The regression used to calculate the frequency is a much
72 * longer-term entity and is NOT reset, so it is still possible
73 * for the offset correction code to make minor adjustments to
74 * the frequency if it so desires.
76 * client_check may replace the server_info pointer with a new
81 for (i = 0; i < count; ++i)
82 client_check(&info_ary[i], &best_off, &best_freq);
87 * XXX it might not be a good idea to issue an offset correction if
88 * a prior offset correction is still in progress as this will skew
89 * the offset calculation. XXX either figure out how to correct the
90 * skew or do not issue a correction.
93 offset = best_off->lin_sumoffset / best_off->lin_countoffset;
94 lin_resetalloffsets(info_ary, count);
95 freq = sysntp_correct_offset(offset);
101 * Frequency correction (throw away minor freq adjusts from the
102 * offset code if we can't do a frequency correction here). Do
103 * not reissue if it hasn't changed from the last issued correction.
106 freq += best_freq->lin_cache_freq;
107 if (last_freq != freq) {
108 sysntp_correct_freq(freq);
114 * This function is responsible for managing the polling mode and
115 * figures out how long we should sleep.
117 for (i = 0; i < count; ++i)
118 client_manage_polling_mode(info_ary[i]);
121 * Polling loop sleep.
123 usleep(min_sleep_opt * 1000000 + random() % 500000);
128 client_poll(server_info_t info, int poll_interval)
136 * By default we always poll. If the polling interval comes under
137 * active management the poll_sleep will be non-zero.
139 if (info->poll_sleep > poll_interval) {
140 info->poll_sleep -= poll_interval;
143 info->poll_sleep = 0;
146 fprintf(stderr, "%s: poll, ", info->target);
149 if (udp_ntptimereq(info->fd, &rtv, <v, &lbtv) < 0) {
152 fprintf(stderr, "no response (%d failures in a row)\n",
155 if (info->poll_failed == POLL_FAIL_RESET) {
156 if (debug_opt && info->lin_count != 0) {
157 fprintf(stderr, "%s: resetting regression due to failures\n",
166 * Successful query. Update polling info for the polling mode manager.
169 info->poll_failed = 0;
172 * Figure out the offset (the difference between the reported
173 * time and our current time) for linear regression purposes.
175 offset = tv_delta_double(&rtv, <v);
188 strftime(buf, sizeof(buf), "%d-%b-%Y %H:%M:%S", tp);
189 fprintf(stderr, "%s.%03ld ",
190 buf, rtv.tv_usec / 1000);
192 lin_regress(info, <v, &lbtv, offset);
193 info = info->altinfo;
194 if (info && debug_opt) {
195 fprintf(stderr, "%*.*s: poll, ",
196 (int)strlen(info->target),
197 (int)strlen(info->target),
205 * Find the best client (or synthesize a fake info structure to return).
206 * We can find separate best clients for offset and frequency.
209 client_check(struct server_info **checkp,
210 struct server_info **best_off,
211 struct server_info **best_freq)
213 struct server_info *check = *checkp;
214 struct server_info *info;
217 * Start an alternate linear regression once our current one
218 * has passed a certain point.
220 if (check->lin_count >= LIN_RESTART / 2 && check->altinfo == NULL) {
221 info = malloc(sizeof(*info));
222 assert(info != NULL);
223 /* note: check->altinfo is NULL as of the bcopy */
224 bcopy(check, info, sizeof(*info));
225 check->altinfo = info;
230 * Replace our current linear regression with the alternate once
231 * the current one has hit its limit (beyond a certain point the
232 * linear regression starts to work against us, preventing us from
233 * reacting to changing conditions).
235 * Report any significant change in the offset or ppm.
237 if (check->lin_count >= LIN_RESTART) {
238 if ((info = check->altinfo) && info->lin_count >= LIN_RESTART / 2) {
242 freq_diff = info->lin_cache_freq - check->lin_cache_freq;
243 printf("%s: Switching to alternate, Frequence difference is %6.3f ppm\n",
244 info->target, freq_diff * 1.0E+6);
253 * BEST CLIENT FOR FREQUENCY CORRECTION:
255 * 8 samples and a correllation > 0.99, or
256 * 16 samples and a correllation > 0.96
259 if ((check->lin_count >= 8 && fabs(check->lin_cache_corr) >= 0.99) ||
260 (check->lin_count >= 16 && fabs(check->lin_cache_corr) >= 0.96)
263 fabs(check->lin_cache_corr) > fabs(info->lin_cache_corr)
272 * BEST CLIENT FOR OFFSET CORRECTION:
274 * Use the standard-deviation and require at least 4 samples. An
275 * offset correction is valid if the standard deviation is less then
276 * the average offset divided by 4.
279 if (check->lin_countoffset >= 4 &&
280 check->lin_cache_stddev < fabs(check->lin_sumoffset / check->lin_countoffset / 4)) {
282 fabs(check->lin_cache_stddev) < fabs(info->lin_cache_stddev)
291 * Actively manage the polling interval. Note that the poll_* fields are
292 * always transfered to the alternate regression when the check code replaces
293 * the current regression with a new one.
295 * This routine is called from the main loop for each base info structure.
296 * The polling mode applies to all alternates so we do not have to iterate
300 client_manage_polling_mode(struct server_info *info)
303 * If too many query failures occured go into a failure-recovery state.
304 * If we were in startup when we failed, go into the second failure
305 * state so a recovery returns us back to startup mode.
307 if (info->poll_failed >= POLL_FAIL_RESET &&
308 info->poll_mode != POLL_FAILED_1 &&
309 info->poll_mode != POLL_FAILED_2
312 fprintf(stderr, "%s: polling mode moving to a FAILED state.\n",
314 if (info->poll_mode != POLL_STARTUP)
315 info->poll_mode = POLL_FAILED_1;
317 info->poll_mode = POLL_FAILED_2;
318 info->poll_count = 0;
322 * Standard polling mode progression
324 switch(info->poll_mode) {
326 info->poll_mode = POLL_STARTUP;
327 info->poll_count = 0;
329 fprintf(stderr, "%s: polling mode INIT->STARTUP.\n", info->target);
332 if (info->poll_count < POLL_STARTUP_MAX) {
333 if (info->poll_sleep == 0)
334 info->poll_sleep = min_sleep_opt;
337 info->poll_mode = POLL_ACQUIRE;
338 info->poll_count = 0;
340 fprintf(stderr, "%s: polling mode STARTUP->ACQUIRE.\n",
346 * Acquisition mode using the nominal timeout. We do not shift
347 * to maintainance mode unless the correllation is at least 0.90
349 if (info->poll_count < POLL_ACQUIRE_MAX ||
350 info->lin_count < 8 ||
351 fabs(info->lin_cache_corr) < 0.85
354 info->poll_count >= POLL_ACQUIRE_MAX &&
355 info->lin_count == LIN_RESTART - 2
358 "%s: WARNING: Unable to shift this source to maintainance\n"
359 "mode. Target correllation is aweful.\n", info->target);
361 if (info->poll_sleep == 0)
362 info->poll_sleep = nom_sleep_opt;
365 info->poll_mode = POLL_MAINTAIN;
366 info->poll_count = 0;
368 fprintf(stderr, "%s: polling mode ACQUIRE->MAINTAIN.\n",
373 if (info->lin_count >= LIN_RESTART / 2 &&
374 fabs(info->lin_cache_corr) < 0.70
378 "%s: polling mode MAINTAIN->ACQUIRE. Unable to maintain\n"
379 "the maintainance mode because the correllation went"
380 " bad!\n", info->target);
382 info->poll_mode = POLL_ACQUIRE;
383 info->poll_count = 0;
386 if (info->poll_sleep == 0)
387 info->poll_sleep = max_sleep_opt;
392 * We have failed recently. If we recover return to the acquisition
395 * poll_count does not increment while we are failed. poll_failed
396 * does increment (but gets zero'd once we recover).
398 if (info->poll_count != 0) {
399 fprintf(stderr, "%s: polling mode FAILED1->ACQUIRE.\n",
401 info->poll_mode = POLL_ACQUIRE;
402 /* do not reset poll_count */
405 if (info->poll_failed >= POLL_RECOVERY_RESTART)
406 info->poll_mode = POLL_FAILED_2;
407 if (info->poll_sleep == 0)
408 info->poll_sleep = nom_sleep_opt;
412 * We have been failed for a very long time, or we failed while
413 * in startup. If we recover we have to go back into startup.
415 if (info->poll_count != 0) {
416 fprintf(stderr, "%s: polling mode FAILED2->STARTUP.\n",
418 info->poll_mode = POLL_STARTUP;
421 if (info->poll_sleep == 0)
422 info->poll_sleep = nom_sleep_opt;
430 * ltv local time as of when the offset error was calculated between
431 * local time and remote time.
433 * lbtv base time as of when local time was obtained. Used to
434 * calculate the cumulative corrections made to the system's
435 * real time clock so we can de-correct the offset for the
438 * X is the time axis, in seconds.
439 * Y is the uncorrected offset, in seconds.
442 lin_regress(server_info_t info, struct timeval *ltv, struct timeval *lbtv,
446 double uncorrected_offset;
449 * De-correcting the offset:
451 * The passed offset is (our_real_time - remote_real_time). To remove
452 * corrections from our_real_time we take the difference in the basetime
453 * (new_base_time - old_base_time) and subtract that from the offset.
454 * That is, if the basetime goesup, the uncorrected offset goes down.
456 if (info->lin_count == 0) {
458 info->lin_btv = *lbtv;
460 uncorrected_offset = offset;
462 time_axis = tv_delta_double(&info->lin_tv, ltv);
463 uncorrected_offset = offset - tv_delta_double(&info->lin_btv, lbtv);
467 * We have to use the uncorrected offset for frequency calculations.
470 info->lin_sumx += time_axis;
471 info->lin_sumx2 += time_axis * time_axis;
472 info->lin_sumy += uncorrected_offset;
473 info->lin_sumy2 += uncorrected_offset * uncorrected_offset;
474 info->lin_sumxy += time_axis * uncorrected_offset;
477 * We have to use the corrected offset for offset calculations.
479 ++info->lin_countoffset;
480 info->lin_sumoffset += offset;
481 info->lin_sumoffset2 += offset * offset;
484 * Calculate various derived values. This gets us slope, y-intercept,
485 * and correllation from the linear regression.
487 if (info->lin_count > 1) {
488 info->lin_cache_slope =
489 (info->lin_count * info->lin_sumxy - info->lin_sumx * info->lin_sumy) /
490 (info->lin_count * info->lin_sumx2 - info->lin_sumx * info->lin_sumx);
492 info->lin_cache_yint =
493 (info->lin_sumy - info->lin_cache_slope * info->lin_sumx) /
496 info->lin_cache_corr =
497 (info->lin_count * info->lin_sumxy - info->lin_sumx * info->lin_sumy) /
498 sqrt((info->lin_count * info->lin_sumx2 -
499 info->lin_sumx * info->lin_sumx) *
500 (info->lin_count * info->lin_sumy2 -
501 info->lin_sumy * info->lin_sumy)
506 * Calculate more derived values. This gets us the standard-deviation
507 * of offsets. The standard deviation approximately means that 68%
508 * of the samples fall within the calculated stddev of the mean.
510 if (info->lin_countoffset > 1) {
511 info->lin_cache_stddev =
512 sqrt((info->lin_sumoffset2 -
513 ((info->lin_sumoffset * info->lin_sumoffset /
514 info->lin_countoffset))) /
515 (info->lin_countoffset - 1.0));
519 * Save the most recent offset, we might use it in the future.
520 * Save the frequency correction (we might scale the slope later so
521 * we have a separate field for the actual frequency correction in
522 * seconds per second).
524 info->lin_cache_offset = offset;
525 info->lin_cache_freq = info->lin_cache_slope;
528 fprintf(stderr, "iter=%2d time=%7.3f off=%.6f uoff=%.6f",
529 (int)info->lin_count,
530 time_axis, offset, uncorrected_offset);
531 if (info->lin_count > 1) {
532 fprintf(stderr, " slope %7.6f"
533 " yint %3.2f corr %7.6f freq_ppm %4.2f",
534 info->lin_cache_slope,
535 info->lin_cache_yint,
536 info->lin_cache_corr,
537 info->lin_cache_freq * 1000000.0);
539 if (info->lin_countoffset > 1) {
540 fprintf(stderr, " stddev %7.6f", info->lin_cache_stddev);
542 fprintf(stderr, "\n");
547 * Reset the linear regression data. The info structure will not again be
548 * a candidate for frequency or offset correction until sufficient data
549 * has been accumulated to make a decision.
552 lin_reset(server_info_t info)
563 info->lin_countoffset = 0;
564 info->lin_sumoffset = 0;
565 info->lin_sumoffset2 = 0;
567 info->lin_cache_slope = 0;
568 info->lin_cache_yint = 0;
569 info->lin_cache_corr = 0;
570 info->lin_cache_offset = 0;
571 info->lin_cache_freq = 0;
574 * Destroy any additional alternative regressions.
576 while ((scan = info->altinfo) != NULL) {
577 info->altinfo = scan->altinfo;
583 * Sometimes we want to clean out the offset calculations without
584 * destroying the linear regression used to figure out the frequency
585 * correction. This usually occurs whenever we issue an offset
586 * adjustment to the system, which invalidates any offset data accumulated
590 lin_resetalloffsets(struct server_info **info_ary, int count)
595 for (i = 0; i < count; ++i) {
596 for (info = info_ary[i]; info; info = info->altinfo)
597 lin_resetoffsets(info);
602 lin_resetoffsets(server_info_t info)
604 info->lin_countoffset = 0;
605 info->lin_sumoffset = 0;
606 info->lin_sumoffset2 = 0;