[Git][NTPsec/ntpsec][master] Structifed clock state machine global variables.

James Browning gitlab at mg.gitlab.com
Mon Aug 19 18:48:33 UTC 2019



James Browning pushed to branch master at NTPsec / ntpsec


Commits:
a5819eca by Ian Bruene at 2019-08-19T17:51:15Z
Structifed clock state machine global variables.

- - - - -


6 changed files:

- include/ntpd.h
- ntpd/ntp_control.c
- ntpd/ntp_loopfilter.c
- ntpd/ntp_proto.c
- ntpd/ntp_refclock.c
- ntpd/ntp_timer.c


Changes:

=====================================
include/ntpd.h
=====================================
@@ -298,13 +298,16 @@ extern int	peer_ntpdate;		/* count of ntpdate peers */
 /*
  * Clock state machine variables
  */
-extern uint8_t	sys_poll;		/* system poll interval (log2 s) */
-extern int	tc_counter;		/* poll-adjust counter */
-extern double	last_offset;		/* last clock offset (s) */
-extern uint8_t	allan_xpt;		/* Allan intercept (log2 s) */
-extern double	clock_jitter;		/* clock jitter (s) */
-extern double	sys_offset;		/* system offset (s) */
-extern double	sys_jitter;		/* system jitter (s) */
+struct clock_state_machine {
+  uint8_t sys_poll;     /* system poll interval time constant/poll (log2 s) */
+  int     tc_counter;   /* poll-adjust counter */
+  double  last_offset;  /* last clock offset (s) */
+  uint8_t allan_xpt;    /* Allan intercept (log2 s) */
+  double  clock_jitter; /* clock jitter (s) */
+  double  sys_offset;   /* system offset (s) */
+  double  sys_jitter;   /* system jitter (s) */
+};
+extern struct clock_state_machine clkstate;
 
 /* ntp_monitor.c */
 struct monitor_data {


=====================================
ntpd/ntp_control.c
=====================================
@@ -1411,7 +1411,7 @@ ctl_putsys(
 		break;
 
 	case CS_POLL:
-		ctl_putuint(sys_var[CS_POLL].text, sys_poll);
+		ctl_putuint(sys_var[CS_POLL].text, clkstate.sys_poll);
 		break;
 
 	case CS_PEERID:
@@ -1441,7 +1441,7 @@ ctl_putsys(
 		}
 
 	case CS_OFFSET:
-		ctl_putdbl6(sys_var[CS_OFFSET].text, last_offset * MS_PER_S);
+		ctl_putdbl6(sys_var[CS_OFFSET].text, clkstate.last_offset * MS_PER_S);
 		break;
 
 	case CS_DRIFT:
@@ -1450,12 +1450,12 @@ ctl_putsys(
 		break;
 
 	case CS_JITTER:
-		ctl_putdbl6(sys_var[CS_JITTER].text, sys_jitter * MS_PER_S);
+		ctl_putdbl6(sys_var[CS_JITTER].text, clkstate.sys_jitter * MS_PER_S);
 		break;
 
 	case CS_ERROR:
 		/* a.k.a clk_jitter (s).  output as ms */
-		ctl_putdbl6(sys_var[CS_ERROR].text, clock_jitter * MS_PER_S);
+		ctl_putdbl6(sys_var[CS_ERROR].text, clkstate.clock_jitter * MS_PER_S);
 		break;
 
 	case CS_CLOCK:


=====================================
ntpd/ntp_loopfilter.c
=====================================
@@ -106,7 +106,6 @@
 static double	clock_minstep = CLOCK_MINSTEP; /* stepout threshold */
 static double	clock_panic = CLOCK_PANIC; /* panic threshold */
 double	clock_phi = CLOCK_PHI;	/* dispersion rate (s/s) */
-uint8_t	allan_xpt = CLOCK_ALLAN; /* Allan intercept (log2 s) */
 struct ntp_loop_data loop_data = {
   .clock_max_back = CLOCK_MAX, /* step threshold */
   .clock_max_fwd =  CLOCK_MAX, /* step threshold */
@@ -118,7 +117,6 @@ struct ntp_loop_data loop_data = {
  */
 static double clock_offset;	/* offset (non-lockclock case only) */
 static uptime_t clock_epoch;	/* last update (non-lockclock case only) */
-double	clock_jitter;		/* offset jitter */
 static double init_drift_comp; /* initial frequency (PPM) */
 unsigned int	sys_tai;		/* TAI offset from UTC */
 /* following variables are non-lockclock case only */
@@ -162,9 +160,9 @@ static bool	ext_enable;	/* external clock enabled */
  * Clock state machine variables
  */
 static int	state = 0;	/* clock discipline state (non-lockclock only) */
-uint8_t	sys_poll;		/* time constant/poll (log2 s) */
-int	tc_counter;		/* jiggle counter */
-double	last_offset;		/* last offset (s) */
+struct clock_state_machine clkstate = {
+  .allan_xpt = CLOCK_ALLAN,
+};
 
 /*
  * Huff-n'-puff filter variables
@@ -218,8 +216,8 @@ init_loopfilter(void)
 	/*
 	 * Initialize state variables.
 	 */
-	sys_poll = ntp_minpoll;
-	clock_jitter = LOGTOD(sys_vars.sys_precision);
+	clkstate.sys_poll = ntp_minpoll;
+	clkstate.clock_jitter = LOGTOD(sys_vars.sys_precision);
 	freq_cnt = (int)clock_minstep;
 }
 
@@ -446,8 +444,9 @@ local_clock(
 	 * the open-loop response and then go home.
 	 */
 	if (loop_data.lockclock || !clock_ctl.ntp_enable) {
-		record_loop_stats(fp_offset, loop_data.drift_comp, clock_jitter,
-		    loop_data.clock_stability, sys_poll);
+		record_loop_stats(fp_offset, loop_data.drift_comp,
+            clkstate.clock_jitter, loop_data.clock_stability,
+            clkstate.sys_poll);
 		return (0);
 	}
 
@@ -499,8 +498,9 @@ local_clock(
 			    fp_offset);
 			printf("ntpd: time slew %+.6fs\n", fp_offset);
 		}
-		record_loop_stats(fp_offset, loop_data.drift_comp, clock_jitter,
-		    loop_data.clock_stability, sys_poll);
+		record_loop_stats(fp_offset, loop_data.drift_comp,
+            clkstate.clock_jitter, loop_data.clock_stability,
+            clkstate.sys_poll);
 		exit(0);
 	}
 
@@ -538,11 +538,11 @@ local_clock(
 	 * Note the kernel is disabled if step is disabled or greater
 	 * than 0.5 s or in ntpdate mode.
 	 */
-	osys_poll = sys_poll;
-	if (sys_poll < peer->cfg.minpoll)
-		sys_poll = peer->cfg.minpoll;
-	if (sys_poll > peer->cfg.maxpoll)
-		sys_poll = peer->cfg.maxpoll;
+	osys_poll = clkstate.sys_poll;
+	if (clkstate.sys_poll < peer->cfg.minpoll)
+		clkstate.sys_poll = peer->cfg.minpoll;
+	if (clkstate.sys_poll > peer->cfg.maxpoll)
+		clkstate.sys_poll = peer->cfg.maxpoll;
 	mu = current_time - clock_epoch;
 	clock_frequency = loop_data.drift_comp;
 	rval = 1;
@@ -620,8 +620,8 @@ local_clock(
 			report_event(EVNT_CLOCKRESET, NULL, tbuf);
 			step_systime(fp_offset, ntp_set_tod);
 			reinit_timer();
-			tc_counter = 0;
-			clock_jitter = LOGTOD(sys_vars.sys_precision);
+			clkstate.tc_counter = 0;
+			clkstate.clock_jitter = LOGTOD(sys_vars.sys_precision);
 			rval = 2;
 			if (state == EVNT_NSET) {
 				rstclock(EVNT_FREQ, 0);
@@ -636,9 +636,9 @@ local_clock(
 		 * the jitter as the exponentially weighted offset
 		 * differences.
 		 */
-		etemp = SQUARE(clock_jitter);
-		dtemp = SQUARE(fp_offset - last_offset);
-		clock_jitter = SQRT(etemp + (dtemp - etemp) / CLOCK_AVG);
+		etemp = SQUARE(clkstate.clock_jitter);
+		dtemp = SQUARE(fp_offset - clkstate.last_offset);
+		clkstate.clock_jitter = SQRT(etemp + (dtemp - etemp) / CLOCK_AVG);
 		switch (state) {
 
 		/*
@@ -682,9 +682,9 @@ local_clock(
 				 * becomes ineffective above the Allan intercept
 				 * where the FLL becomes effective.
 				 */
-				if (sys_poll >= allan_xpt)
+				if (clkstate.sys_poll >= clkstate.allan_xpt)
 					clock_frequency += (fp_offset -
-					    clock_offset) / (max(ULOGTOD(sys_poll),
+					    clock_offset) / (max(ULOGTOD(clkstate.sys_poll),
 					    mu) * CLOCK_FLL);
 
 				/*
@@ -693,8 +693,8 @@ local_clock(
 				 * intercept. This reduces the PLL gain when the
 				 * FLL becomes effective.
 				 */
-				etemp = min(ULOGTOD(allan_xpt), mu);
-				dtemp = 4 * CLOCK_PLL * ULOGTOD(sys_poll);
+				etemp = min(ULOGTOD(clkstate.allan_xpt), mu);
+				dtemp = 4 * CLOCK_PLL * ULOGTOD(clkstate.sys_poll);
 				clock_frequency += fp_offset * etemp / (dtemp *
 				    dtemp);
 			}
@@ -749,14 +749,14 @@ local_clock(
 				dtemp = .5;
 			ntv.offset = (long)(clock_offset * NS_PER_S + dtemp);
 #ifdef STA_NANO
-			ntv.constant = sys_poll;
+			ntv.constant = clkstate.sys_poll;
 #else /* STA_NANO */
-			ntv.constant = sys_poll - 4;
+			ntv.constant = clkstate.sys_poll - 4;
 #endif /* STA_NANO */
 			if (ntv.constant < 0)
 				ntv.constant = 0;
 
-			ntv.esterror = (long)(clock_jitter * US_PER_S);
+			ntv.esterror = (long)(clkstate.clock_jitter * US_PER_S);
 			ntv.maxerror = (long)((sys_vars.sys_rootdelay / 2 +
 			    sys_vars.sys_rootdisp) * US_PER_S);
 			ntv.status = STA_PLL;
@@ -803,7 +803,7 @@ local_clock(
 		 * If the kernel PPS is lit, monitor its performance.
 		 */
 		if (ntv.status & STA_PPSTIME) {
-			clock_jitter = ntv.jitter * S_PER_NS;
+			clkstate.clock_jitter = ntv.jitter * S_PER_NS;
 		}
 
 #if defined(STA_NANO) && defined(NTP_API) && NTP_API == 4
@@ -854,23 +854,23 @@ local_clock(
 	 * fiddle with the poll during the startup clamp period.
 	 */
 	if (freq_cnt > 0) {
-		tc_counter = 0;
-	} else if (fabs(clock_offset) < CLOCK_PGATE * clock_jitter) {
-		tc_counter += sys_poll;
-		if (tc_counter > CLOCK_LIMIT) {
-			tc_counter = CLOCK_LIMIT;
-			if (sys_poll < peer->cfg.maxpoll) {
-				tc_counter = 0;
-				sys_poll++;
+		clkstate.tc_counter = 0;
+	} else if (fabs(clock_offset) < CLOCK_PGATE * clkstate.clock_jitter) {
+		clkstate.tc_counter += clkstate.sys_poll;
+		if (clkstate.tc_counter > CLOCK_LIMIT) {
+			clkstate.tc_counter = CLOCK_LIMIT;
+			if (clkstate.sys_poll < peer->cfg.maxpoll) {
+				clkstate.tc_counter = 0;
+				clkstate.sys_poll++;
 			}
 		}
 	} else {
-		tc_counter -= sys_poll << 1;
-		if (tc_counter < -CLOCK_LIMIT) {
-			tc_counter = -CLOCK_LIMIT;
-			if (sys_poll > peer->cfg.minpoll) {
-				tc_counter = 0;
-				sys_poll--;
+		clkstate.tc_counter -= clkstate.sys_poll << 1;
+		if (clkstate.tc_counter < -CLOCK_LIMIT) {
+			clkstate.tc_counter = -CLOCK_LIMIT;
+			if (clkstate.sys_poll > peer->cfg.minpoll) {
+				clkstate.tc_counter = 0;
+				clkstate.sys_poll--;
 			}
 		}
 	}
@@ -878,17 +878,18 @@ local_clock(
 	/*
 	 * If the time constant has changed, update the poll variables.
 	 */
-	if (osys_poll != sys_poll)
-		poll_update(peer, sys_poll);
+	if (osys_poll != clkstate.sys_poll)
+		poll_update(peer, clkstate.sys_poll);
 
 	/*
 	 * Yibbidy, yibbbidy, yibbidy; that'h all folks.
 	 */
-	record_loop_stats(clock_offset, loop_data.drift_comp, clock_jitter,
-	    loop_data.clock_stability, sys_poll);
+	record_loop_stats(clock_offset, loop_data.drift_comp,
+        clkstate.clock_jitter, loop_data.clock_stability, clkstate.sys_poll);
 	DPRINT(1, ("local_clock: offset %.9f jit %.9f freq %.6f stab %.3f poll %d\n",
-		   clock_offset, clock_jitter, loop_data.drift_comp * US_PER_S,
-		   loop_data.clock_stability * US_PER_S, sys_poll));
+		   clock_offset, clkstate.clock_jitter,
+           loop_data.drift_comp * US_PER_S,
+           loop_data.clock_stability * US_PER_S, clkstate.sys_poll));
 	return (rval);
 }
 
@@ -932,7 +933,7 @@ adj_host_clock(
 	} else if (clock_ctl.pll_control && clock_ctl.kern_enable) {
 		offset_adj = 0.;
 	} else {
-		offset_adj = clock_offset / (CLOCK_PLL * ULOGTOD(sys_poll));
+		offset_adj = clock_offset / (CLOCK_PLL * ULOGTOD(clkstate.sys_poll));
 	}
 
 	/*
@@ -979,12 +980,12 @@ rstclock(
 	)
 {
         DPRINT(2, ("local_clock: mu %" PRIu32 " state %d poll %d count %d\n",
-                  current_time - clock_epoch, trans, sys_poll,
-                  tc_counter));
+                  current_time - clock_epoch, trans, clkstate.sys_poll,
+                  clkstate.tc_counter));
 	if (trans != state && trans != EVNT_FSET)
 		report_event(trans, NULL, NULL);
 	state = trans;
-	last_offset = clock_offset = offset;
+	clkstate.last_offset = clock_offset = offset;
 	clock_epoch = current_time;
 }
 
@@ -1059,7 +1060,7 @@ start_kern_loop(void)
 	ntv.status = STA_PLL;
 	ntv.maxerror = sys_maxdisp;
 	ntv.esterror = sys_maxdisp;
-	ntv.constant = sys_poll; /* why is it that here constant is unconditionally set to sys_poll, whereas elsewhere is is modified depending on nanosecond vs. microsecond kernel? */
+	ntv.constant = clkstate.sys_poll; /* why is it that here constant is unconditionally set to sys_poll, whereas elsewhere is is modified depending on nanosecond vs. microsecond kernel? */
 #ifdef SIGSYS
 	/*
 	 * Use sigsetjmp() to save state and then call ntp_adjtime(); if
@@ -1228,7 +1229,7 @@ loop_config(
 	 * Tinker command variables for Ulrich Windl. Very dangerous.
 	 */
 	case LOOP_ALLAN:	/* Allan intercept (log2) (allan) */
-		allan_xpt = (uint8_t)freq;
+		clkstate.allan_xpt = (uint8_t)freq;
 		break;
 
 	case LOOP_PHI:		/* dispersion threshold (dispersion) */


=====================================
ntpd/ntp_proto.c
=====================================
@@ -119,11 +119,9 @@ bool leap_sec_in_progress;
  * Nonspecified system state variables
  */
 l_fp	sys_authdelay;		/* authentication delay */
-double	sys_offset;	/* current local clock offset */
 double	sys_mindisp = MINDISPERSE; /* minimum distance (s) */
 static double	sys_maxdist = MAXDISTANCE; /* selection threshold */
 double	sys_maxdisp = MAXDISPERSE; /* maximum dispersion */
-double	sys_jitter;		/* system jitter */
 static unsigned long	sys_epoch;	/* last clock update time */
 static	double sys_clockhop;	/* clockhop threshold */
 static int leap_vote_ins;	/* leap consensus for insert */
@@ -828,7 +826,7 @@ transmit(
 			 * Unreach is also reset for survivors in
 			 * clock_select().
 			 */
-			hpoll = sys_poll;
+			hpoll = clkstate.sys_poll;
 			if (!(peer->cfg.flags & FLAG_PREEMPT))
 				peer->unreach = 0;
 			if ((peer->cfg.flags & FLAG_BURST) && peer->retry ==
@@ -911,11 +909,11 @@ clock_update(
 	 */
 	sys_vars.sys_peer = peer;
 	sys_epoch = peer->epoch;
-	if (sys_poll < peer->cfg.minpoll)
-		sys_poll = peer->cfg.minpoll;
-	if (sys_poll > peer->cfg.maxpoll)
-		sys_poll = peer->cfg.maxpoll;
-	poll_update(peer, sys_poll);
+	if (clkstate.sys_poll < peer->cfg.minpoll)
+		clkstate.sys_poll = peer->cfg.minpoll;
+	if (clkstate.sys_poll > peer->cfg.maxpoll)
+		clkstate.sys_poll = peer->cfg.maxpoll;
+	poll_update(peer, clkstate.sys_poll);
 	sys_vars.sys_stratum = min(peer->stratum + 1, STRATUM_UNSPEC);
 	if (peer->stratum == STRATUM_REFCLOCK ||
 	    peer->stratum == STRATUM_UNSPEC)
@@ -942,9 +940,9 @@ clock_update(
 	 */
 	dtemp	= peer->rootdisp
 		+ peer->disp
-		+ sys_jitter
+		+ clkstate.sys_jitter
 		+ loop_data.clock_phi * (current_time - peer->update)
-		+ fabs(sys_offset);
+		+ fabs(clkstate.sys_offset);
 
 	if (dtemp > sys_mindisp)
 		sys_vars.sys_rootdisp = dtemp;
@@ -960,7 +958,7 @@ clock_update(
 	 * Comes now the moment of truth. Crank the clock discipline and
 	 * see what comes out.
 	 */
-	switch (local_clock(peer, sys_offset)) {
+	switch (local_clock(peer, clkstate.sys_offset)) {
 
 	/*
 	 * Clock exceeds panic threshold. Life as we know it ends.
@@ -983,7 +981,8 @@ clock_update(
 				pause();
 		}
 #endif /* HAVE_LIBSCF_H */
-		msyslog(LOG_ERR, "CLOCK: Panic: offset too big: %.3f", sys_offset);
+		msyslog(LOG_ERR, "CLOCK: Panic: offset too big: %.3f",
+            clkstate.sys_offset);
 		exit (1);
 		/* not reached */
 
@@ -998,7 +997,7 @@ clock_update(
 		sys_vars.sys_rootdelay = 0;
 		sys_vars.sys_rootdisp = 0;
 		sys_vars.sys_reftime = 0;
-		sys_jitter = LOGTOD(sys_vars.sys_precision);
+		clkstate.sys_jitter = LOGTOD(sys_vars.sys_precision);
 		leapsec_reset_frame();
 		break;
 
@@ -1277,7 +1276,7 @@ clock_filter(
 			peer->filter_disp[j] = sys_maxdisp;
 			dst[i] = sys_maxdisp;
 		} else if (peer->update - peer->filter_epoch[j] >
-		    (unsigned long)ULOGTOD(allan_xpt)) {
+		    (unsigned long)ULOGTOD(clkstate.allan_xpt)) {
 			dst[i] = peer->filter_delay[j] +
 			    peer->filter_disp[j];
 		} else {
@@ -1869,12 +1868,12 @@ clock_select(void)
 			typesystem = sys_prefer;
 			sys_clockhop = 0;
 			typesystem->new_status = CTL_PST_SEL_SYSPEER;
-			sys_offset = typesystem->offset;
-			sys_jitter = typesystem->jitter;
+			clkstate.sys_offset = typesystem->offset;
+			clkstate.sys_jitter = typesystem->jitter;
 			sys_vars.sys_rootdist = root_distance(typesystem);
 		}
 		DPRINT(1, ("select: combine offset %.9f jitter %.9f\n",
-			   sys_offset, sys_jitter));
+			   clkstate.sys_offset, clkstate.sys_jitter));
 	}
 #ifdef REFCLOCK
 	/*
@@ -1884,7 +1883,7 @@ clock_select(void)
 	 * if there is a prefer peer or there are no survivors and none
 	 * are required.
 	 */
-	if (typepps != NULL && fabs(sys_offset) < 0.4 &&
+	if (typepps != NULL && fabs(clkstate.sys_offset) < 0.4 &&
 	    (!typepps->is_pps_driver ||
 	     sys_prefer != NULL ||
 	     (typesystem != NULL && typepps->cfg.flags & FLAG_PREFER) ||
@@ -1892,11 +1891,11 @@ clock_select(void)
 		typesystem = typepps;
 		sys_clockhop = 0;
 		typesystem->new_status = CTL_PST_SEL_PPS;
-		sys_offset = typesystem->offset;
-		sys_jitter = typesystem->jitter;
+		clkstate.sys_offset = typesystem->offset;
+		clkstate.sys_jitter = typesystem->jitter;
 		sys_vars.sys_rootdist = root_distance(typesystem);
 		DPRINT(1, ("select: pps offset %.9f jitter %.9f\n",
-			   sys_offset, sys_jitter));
+			   clkstate.sys_offset, clkstate.sys_jitter));
 	} else if ( typepps &&
                     ( CTL_PST_SEL_PPS == typepps->new_status )) {
                 /* uh, oh, it WAS a valid PPS, but no longer */
@@ -1957,8 +1956,8 @@ clock_combine(
 		w += x * DIFF(peers[i].peer->offset,
 		    peers[syspeer].peer->offset);
 	}
-	sys_offset = z / y;
-	sys_jitter = SQRT(w / y + SQUARE(peers[syspeer].seljit));
+	clkstate.sys_offset = z / y;
+	clkstate.sys_jitter = SQRT(w / y + SQUARE(peers[syspeer].seljit));
 	sys_vars.sys_rootdist = peers[syspeer].synch;
 }
 
@@ -2700,7 +2699,7 @@ init_proto(const bool verbose)
 	sys_vars.sys_rootdelay = 0;
 	sys_vars.sys_rootdisp = 0;
 	sys_vars.sys_reftime = 0;
-	sys_jitter = 0;
+	clkstate.sys_jitter = 0;
 	measure_precision(verbose);
 	get_systime(&dummy);
 	sys_survivors = 0;


=====================================
ntpd/ntp_refclock.c
=====================================
@@ -537,8 +537,8 @@ refclock_receive(
 		return;
 
 	clock_filter(peer, pp->offset, 0., pp->jitter);
-	if (cal_enable && fabs(last_offset) < sys_mindisp && sys_vars.sys_peer !=
-	    NULL) {
+	if (cal_enable && fabs(clkstate.last_offset) < sys_mindisp &&
+		sys_vars.sys_peer != NULL) {
 		if (sys_vars.sys_peer->is_pps_driver &&
 		    !peer->is_pps_driver)
 			pp->fudgetime1 -= pp->offset * FUDGEFAC;


=====================================
ntpd/ntp_timer.c
=====================================
@@ -240,7 +240,7 @@ timer(void)
 			sys_vars.sys_refid = htonl(LOOPBACKADR);
 		else
 			memcpy(&sys_vars.sys_refid, "LOOP", REFIDLEN);
-		sys_offset = 0;
+		clkstate.sys_offset = 0;
 		sys_vars.sys_rootdelay = 0;
 		sys_vars.sys_rootdisp = 0;
 	}



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/a5819eca04b62192dffdd8a60c06ba9ae95e9515

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/a5819eca04b62192dffdd8a60c06ba9ae95e9515
You're receiving this email because of your account on gitlab.com.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20190819/e221075d/attachment-0001.htm>


More information about the vc mailing list