aboutsummaryrefslogtreecommitdiff
path: root/networking/ntpd.c
diff options
context:
space:
mode:
Diffstat (limited to 'networking/ntpd.c')
-rw-r--r--networking/ntpd.c100
1 files changed, 82 insertions, 18 deletions
diff --git a/networking/ntpd.c b/networking/ntpd.c
index 17e5c7da6..8205ab271 100644
--- a/networking/ntpd.c
+++ b/networking/ntpd.c
@@ -165,8 +165,12 @@
165 * Using exact power of 2 (1/8) results in smaller code 165 * Using exact power of 2 (1/8) results in smaller code
166 */ 166 */
167#define SLEW_THRESHOLD 0.125 167#define SLEW_THRESHOLD 0.125
168//^^^^^^^^^^^^^^^^^^^^^^^^^^ TODO: man adjtimex about tmx.offset:
169// "Since Linux 2.6.26, the supplied value is clamped to the range (-0.5s, +0.5s)"
170// - can use this larger value instead?
171
168/* Stepout threshold (sec). std ntpd uses 900 (11 mins (!)) */ 172/* Stepout threshold (sec). std ntpd uses 900 (11 mins (!)) */
169#define WATCH_THRESHOLD 128 173//UNUSED: #define WATCH_THRESHOLD 128
170/* NB: set WATCH_THRESHOLD to ~60 when debugging to save time) */ 174/* NB: set WATCH_THRESHOLD to ~60 when debugging to save time) */
171//UNUSED: #define PANIC_THRESHOLD 1000 /* panic threshold (sec) */ 175//UNUSED: #define PANIC_THRESHOLD 1000 /* panic threshold (sec) */
172 176
@@ -419,6 +423,7 @@ struct globals {
419 uint8_t discipline_state; // doc calls it c.state 423 uint8_t discipline_state; // doc calls it c.state
420 uint8_t poll_exp; // s.poll 424 uint8_t poll_exp; // s.poll
421 int polladj_count; // c.count 425 int polladj_count; // c.count
426 int FREQHOLD_cnt;
422 long kernel_freq_drift; 427 long kernel_freq_drift;
423 peer_t *last_update_peer; 428 peer_t *last_update_peer;
424 double last_update_offset; // c.last 429 double last_update_offset; // c.last
@@ -1034,6 +1039,7 @@ step_time(double offset)
1034 tval = tvn.tv_sec; 1039 tval = tvn.tv_sec;
1035 strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval); 1040 strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval);
1036 bb_error_msg("setting time to %s.%06u (offset %+fs)", buf, (unsigned)tvn.tv_usec, offset); 1041 bb_error_msg("setting time to %s.%06u (offset %+fs)", buf, (unsigned)tvn.tv_usec, offset);
1042 //maybe? G.FREQHOLD_cnt = 0;
1037 1043
1038 /* Correct various fields which contain time-relative values: */ 1044 /* Correct various fields which contain time-relative values: */
1039 1045
@@ -1709,39 +1715,96 @@ update_local_clock(peer_t *p)
1709 tmx.freq = G.discipline_freq_drift * 65536e6; 1715 tmx.freq = G.discipline_freq_drift * 65536e6;
1710#endif 1716#endif
1711 tmx.modes = ADJ_OFFSET | ADJ_STATUS | ADJ_TIMECONST;// | ADJ_MAXERROR | ADJ_ESTERROR; 1717 tmx.modes = ADJ_OFFSET | ADJ_STATUS | ADJ_TIMECONST;// | ADJ_MAXERROR | ADJ_ESTERROR;
1712 tmx.constant = (int)G.poll_exp - 4; 1718
1713 /* EXPERIMENTAL.
1714 * The below if statement should be unnecessary, but...
1715 * It looks like Linux kernel's PLL is far too gentle in changing
1716 * tmx.freq in response to clock offset. Offset keeps growing
1717 * and eventually we fall back to smaller poll intervals.
1718 * We can make correction more aggressive (about x2) by supplying
1719 * PLL time constant which is one less than the real one.
1720 * To be on a safe side, let's do it only if offset is significantly
1721 * larger than jitter.
1722 */
1723 if (G.offset_to_jitter_ratio >= TIMECONST_HACK_GATE)
1724 tmx.constant--;
1725 tmx.offset = (long)(offset * 1000000); /* usec */ 1719 tmx.offset = (long)(offset * 1000000); /* usec */
1726 if (SLEW_THRESHOLD < STEP_THRESHOLD) { 1720 if (SLEW_THRESHOLD < STEP_THRESHOLD) {
1727 if (tmx.offset > (long)(SLEW_THRESHOLD * 1000000)) { 1721 if (tmx.offset > (long)(SLEW_THRESHOLD * 1000000)) {
1728 tmx.offset = (long)(SLEW_THRESHOLD * 1000000); 1722 tmx.offset = (long)(SLEW_THRESHOLD * 1000000);
1729 tmx.constant--;
1730 } 1723 }
1731 if (tmx.offset < -(long)(SLEW_THRESHOLD * 1000000)) { 1724 if (tmx.offset < -(long)(SLEW_THRESHOLD * 1000000)) {
1732 tmx.offset = -(long)(SLEW_THRESHOLD * 1000000); 1725 tmx.offset = -(long)(SLEW_THRESHOLD * 1000000);
1733 tmx.constant--;
1734 } 1726 }
1735 } 1727 }
1736 if (tmx.constant < 0)
1737 tmx.constant = 0;
1738 1728
1739 tmx.status = STA_PLL; 1729 tmx.status = STA_PLL;
1730 if (G.FREQHOLD_cnt != 0) {
1731 /* man adjtimex on STA_FREQHOLD:
1732 * "Normally adjustments made via ADJ_OFFSET result in dampened
1733 * frequency adjustments also being made.
1734 * This flag prevents the small frequency adjustment from being
1735 * made when correcting for an ADJ_OFFSET value."
1736 *
1737 * Use this flag for a few first adjustments at the beginning
1738 * of ntpd execution, otherwise even relatively small initial
1739 * offset tend to cause largish changes to in-kernel tmx.freq.
1740 * If ntpd was restarted due to e.g. switch to another network,
1741 * this destroys already well-established tmx.freq value.
1742 */
1743 if (G.FREQHOLD_cnt < 0) {
1744 /* Initialize it */
1745// Example: a laptop whose clock runs slower when hibernated,
1746// after wake up it still has good tmx.freq, but accumulated ~0.5 sec offset:
1747// Run with code where initial G.FREQHOLD_cnt was always 8:
1748//15:17:52.947 no valid datapoints, no peer selected
1749//15:17:56.515 update from:<IP> offset:+0.485133 delay:0.157762 jitter:0.209310 clock drift:-1.393ppm tc:4
1750//15:17:57.719 update from:<IP> offset:+0.483825 delay:0.158070 jitter:0.181159 clock drift:-1.393ppm tc:4
1751//15:17:59.925 update from:<IP> offset:+0.479504 delay:0.158147 jitter:0.156657 clock drift:-1.393ppm tc:4
1752//15:18:33.322 update from:<IP> offset:+0.428119 delay:0.158317 jitter:0.138071 clock drift:-1.393ppm tc:4
1753//15:19:06.718 update from:<IP> offset:+0.376932 delay:0.158276 jitter:0.122075 clock drift:-1.393ppm tc:4
1754//15:19:39.114 update from:<IP> offset:+0.327022 delay:0.158384 jitter:0.108538 clock drift:-1.393ppm tc:4
1755//15:20:12.715 update from:<IP> offset:+0.275596 delay:0.158297 jitter:0.097292 clock drift:-1.393ppm tc:4
1756//15:20:45.111 update from:<IP> offset:+0.225715 delay:0.158271 jitter:0.087841 clock drift:-1.393ppm tc:4
1757// If allwed to continue, it would start increasing tmx.freq now.
1758// Instead, it was ^Ced, and started anew:
1759//15:21:15.043 no valid datapoints, no peer selected
1760//15:21:17.408 update from:<IP> offset:+0.175910 delay:0.158314 jitter:0.076683 clock drift:-1.393ppm tc:4
1761//15:21:19.774 update from:<IP> offset:+0.171784 delay:0.158401 jitter:0.066436 clock drift:-1.393ppm tc:4
1762//15:21:22.140 update from:<IP> offset:+0.171660 delay:0.158592 jitter:0.057536 clock drift:-1.393ppm tc:4
1763//15:21:22.140 update from:<IP> offset:+0.167126 delay:0.158507 jitter:0.049792 clock drift:-1.393ppm tc:4
1764//15:21:55.696 update from:<IP> offset:+0.115223 delay:0.158277 jitter:0.050240 clock drift:-1.393ppm tc:4
1765//15:22:29.093 update from:<IP> offset:+0.068051 delay:0.158243 jitter:0.049405 clock drift:-1.393ppm tc:5
1766//15:23:02.490 update from:<IP> offset:+0.051632 delay:0.158215 jitter:0.043545 clock drift:-1.393ppm tc:5
1767//15:23:34.726 update from:<IP> offset:+0.039984 delay:0.158157 jitter:0.038106 clock drift:-1.393ppm tc:5
1768// STA_FREQHOLD no longer set, started increasing tmx.freq now:
1769//15:24:06.961 update from:<IP> offset:+0.030968 delay:0.158190 jitter:0.033306 clock drift:+2.387ppm tc:5
1770//15:24:40.357 update from:<IP> offset:+0.023648 delay:0.158211 jitter:0.029072 clock drift:+5.454ppm tc:5
1771//15:25:13.774 update from:<IP> offset:+0.018068 delay:0.157660 jitter:0.025288 clock drift:+7.728ppm tc:5
1772//15:26:19.173 update from:<IP> offset:+0.010057 delay:0.157969 jitter:0.022255 clock drift:+8.361ppm tc:6
1773//15:27:26.602 update from:<IP> offset:+0.006737 delay:0.158103 jitter:0.019316 clock drift:+8.792ppm tc:6
1774//15:28:33.030 update from:<IP> offset:+0.004513 delay:0.158294 jitter:0.016765 clock drift:+9.080ppm tc:6
1775//15:29:40.617 update from:<IP> offset:+0.002787 delay:0.157745 jitter:0.014543 clock drift:+9.258ppm tc:6
1776//15:30:47.045 update from:<IP> offset:+0.001324 delay:0.157709 jitter:0.012594 clock drift:+9.342ppm tc:6
1777//15:31:53.473 update from:<IP> offset:+0.000007 delay:0.158142 jitter:0.010922 clock drift:+9.343ppm tc:6
1778//15:32:58.902 update from:<IP> offset:-0.000728 delay:0.158222 jitter:0.009454 clock drift:+9.298ppm tc:6
1779 /*
1780 * This expression would choose 15 in the above example.
1781 */
1782 G.FREQHOLD_cnt = 8 + ((unsigned)(abs(tmx.offset)) >> 16);
1783 }
1784 G.FREQHOLD_cnt--;
1785 tmx.status |= STA_FREQHOLD;
1786 }
1740 if (G.ntp_status & LI_PLUSSEC) 1787 if (G.ntp_status & LI_PLUSSEC)
1741 tmx.status |= STA_INS; 1788 tmx.status |= STA_INS;
1742 if (G.ntp_status & LI_MINUSSEC) 1789 if (G.ntp_status & LI_MINUSSEC)
1743 tmx.status |= STA_DEL; 1790 tmx.status |= STA_DEL;
1744 1791
1792 tmx.constant = (int)G.poll_exp - 4;
1793 /* EXPERIMENTAL.
1794 * The below if statement should be unnecessary, but...
1795 * It looks like Linux kernel's PLL is far too gentle in changing
1796 * tmx.freq in response to clock offset. Offset keeps growing
1797 * and eventually we fall back to smaller poll intervals.
1798 * We can make correction more aggressive (about x2) by supplying
1799 * PLL time constant which is one less than the real one.
1800 * To be on a safe side, let's do it only if offset is significantly
1801 * larger than jitter.
1802 */
1803 if (G.offset_to_jitter_ratio >= TIMECONST_HACK_GATE)
1804 tmx.constant--;
1805 if (tmx.constant < 0)
1806 tmx.constant = 0;
1807
1745 //tmx.esterror = (uint32_t)(clock_jitter * 1e6); 1808 //tmx.esterror = (uint32_t)(clock_jitter * 1e6);
1746 //tmx.maxerror = (uint32_t)((sys_rootdelay / 2 + sys_rootdisp) * 1e6); 1809 //tmx.maxerror = (uint32_t)((sys_rootdelay / 2 + sys_rootdisp) * 1e6);
1747 rc = adjtimex(&tmx); 1810 rc = adjtimex(&tmx);
@@ -2227,6 +2290,7 @@ static NOINLINE void ntp_init(char **argv)
2227 if (BURSTPOLL != 0) 2290 if (BURSTPOLL != 0)
2228 G.poll_exp = BURSTPOLL; /* speeds up initial sync */ 2291 G.poll_exp = BURSTPOLL; /* speeds up initial sync */
2229 G.last_script_run = G.reftime = G.last_update_recv_time = gettime1900d(); /* sets G.cur_time too */ 2292 G.last_script_run = G.reftime = G.last_update_recv_time = gettime1900d(); /* sets G.cur_time too */
2293 G.FREQHOLD_cnt = -1;
2230 2294
2231 /* Parse options */ 2295 /* Parse options */
2232 peers = NULL; 2296 peers = NULL;