<p>Address: 127.127.28.<i>u</i><br>
Reference ID: <tt>SHM</tt><br>
Driver ID: <tt>SHM</tt></p>
+
<h4>Description</h4>
<p>This driver receives its reference clock info from a shared memory-segment. The shared memory-segment is created with owner-only access for unit 0 and 1, and world access for unit 2 and 3</p>
+
<h4>Structure of shared memory-segment</h4>
<pre>struct shmTime {
int mode; /* 0 - if valid set
int valid;
int dummy[10];
};</pre>
+
<h4>Operation mode=0</h4>
- <p>When the poll-method of the driver is called, the valid-flag of the shared memory-segment is checked:</p>
- <p>If set, the values in the record (clockTimeStampSec, clockTimeStampUSec, receiveTimeStampSec, receiveTimeStampUSec, leap, precision) are passed to ntp, and the valid-flag is cleared.</p>
- <p>If not set, a timeout is reported to ntp, nothing else happend</p>
+ <p>Each second, the valid-flag of the shared memory-segment is checked:</p>
+ <p>If set, the values in the record (clockTimeStampSec, clockTimeStampUSec, receiveTimeStampSec, receiveTimeStampUSec, leap, precision) are passed to ntp, and the valid-flag is cleared and a counter is bumped.</p>
+ <p>If not set, a counter is bumped</p>
<h4>Operation mode=1</h4>
- <p>When the poll-method of the driver is called, the valid-flag of the shared memory-segment is checked:</p>
- <p>If set, the count-field of the record is remembered, and the values in the record (clockTimeStampSec, clockTimeStampUSec, receiveTimeStampSec, receiveTimeStampUSec, leap, precision) are read. Then, the remembered count is compared to the count now in the record. If both are equal, the values read from the record are passed to ntp. If they differ, another process has modified the record while it was read out (was not able to produce this case), and failure is reported to ntp. The valid flag is cleared.</p>
- <p>If not set, a timeout is reported to ntp, nothing else happend</p>
- <h4>Fudge Factors</h4>
+ <p>Each second, the valid-flag of the shared memory-segment is checked:</p>
+ <p>If set, the count-field of the record is remembered, and the values in the record (clockTimeStampSec, clockTimeStampUSec, receiveTimeStampSec, receiveTimeStampUSec, leap, precision) are read. Then, the remembered count is compared to the count now in the record. If both are equal, the values read from the record are passed to ntp. If they differ, another process has modified the record while it was read out (was not able to produce this case), and failure is reported to ntp. The valid flag is cleared and a counter is bumped.</p>
+ <p>If not set, a counter is bumped</p>
+
+
+<h4>gpsd</h4>
+
+<a href="http://gpsd.berlios.de/"><i>gpsd</i></a>
+knows how to talk to many GPS devices.
+It works with <i>ntpd</i> through the SHM driver.
+<P>
+The <i>gpsd</i> man page suggests setting minpoll and maxpoll to 4.
+That was an attempt to reduce jitter.
+The SHM driver was fixed (Sep 2007) to collect data each second rather than
+once per polling interval so that suggestion is no longer reasonable.
+<P>
+
+
+<h4>Clockstats</h4>
+If flag4 is set when the driver is polled, a clockstats record is written.
+The first 3 fields are the normal date, time, and IP address common to all clockstats records.
+<P>
+The 4th field is the number of second ticks since the last poll.
+The 5th field is the number of good data samples found. The last 64 will be used by ntpd.
+The 6th field is the number of sample that didn't have valid data ready.
+The 7th field is the number of bad samples.
+The 8th field is the number of times the the mode 1 info was update while nptd was trying to grab a sample.
+<P>
+
+Here is a sample showing the GPS reception fading out:
+<pre>
+54364 84927.157 127.127.28.0 66 65 1 0 0
+54364 84990.161 127.127.28.0 63 63 0 0 0
+54364 85053.160 127.127.28.0 63 63 0 0 0
+54364 85116.159 127.127.28.0 63 62 1 0 0
+54364 85180.158 127.127.28.0 64 63 1 0 0
+54364 85246.161 127.127.28.0 66 66 0 0 0
+54364 85312.157 127.127.28.0 66 50 16 0 0
+54364 85375.160 127.127.28.0 63 41 22 0 0
+54364 85439.155 127.127.28.0 64 64 0 0 0
+54364 85505.158 127.127.28.0 66 36 30 0 0
+54364 85569.157 127.127.28.0 64 0 64 0 0
+54364 85635.157 127.127.28.0 66 0 66 0 0
+54364 85700.160 127.127.28.0 65 0 65 0 0
+</pre>
+
+ <h4>Fudge Factors</h4>
<dl>
<dt><tt>time1 <i>time</i></tt>
<dd>Specifies the time offset calibration factor, in seconds and fraction, with default 0.0.
<dt><tt>flag3 0 | 1</tt>
<dd>Not used by this driver.
<dt><tt>flag4 0 | 1</tt>
- <dd>Not used by this driver.
+ <dd>If flag4 is set, clockstats records will be written when the driver is polled.
<h4>Additional Information</h4>
<p><a href="../refclock.html">Reference Clock Drivers</a></p>
</dl>
<script type="text/javascript" language="javascript" src="scripts/footer.txt"></script>
</body>
-</html>
\ No newline at end of file
+</html>
+
* This driver supports a reference clock attached thru shared memory
*/
+/* Temp hack to simplify testing of the old mode. */
+#define OLDWAY 0
+
/*
* SHM interface definitions
*/
/*
* Function prototypes
*/
-static int shm_start (int, struct peer *);
-static void shm_shutdown (int, struct peer *);
-static void shm_poll (int unit, struct peer *);
+static int shm_start (int unit, struct peer *peer);
+static void shm_shutdown (int unit, struct peer *peer);
+static void shm_poll (int unit, struct peer *peer);
+static void shm_timer (int unit, struct peer *peer);
+ int shm_peek (int unit, struct peer *peer);
+ void shm_clockstats (int unit, struct peer *peer);
/*
* Transfer vector
struct refclock refclock_shm = {
shm_start, /* start up driver */
shm_shutdown, /* shut down driver */
- shm_poll, /* transmit poll message */
- noentry, /* not used */
- noentry, /* initialize driver (not used) */
- noentry, /* not used */
- NOFLAGS /* not used */
+ shm_poll, /* transmit poll message */
+ noentry, /* not used: control */
+ noentry, /* not used: init */
+ noentry, /* not used: buginfo */
+ shm_timer, /* once per second */
};
+
struct shmTime {
int mode; /* 0 - if valid set
* use values,
int dummy[10];
};
+struct shmunit {
+ struct shmTime *shm; /* pointer to shared memory segment */
+
+ /* debugging/monitoring counters - reset when printed */
+ int ticks; /* number of attempts to read data*/
+ int good; /* number of valid samples */
+ int notready; /* number of peeks without data ready */
+ int bad; /* number of invalid samples */
+ int clash; /* number of access clashes while reading */
+};
+
+
struct shmTime *getShmTime(int);
struct shmTime *getShmTime (int unit) {
)
{
struct refclockproc *pp;
+ struct shmunit *up;
+
pp = peer->procptr;
pp->io.clock_recv = noentry;
pp->io.srcclock = (caddr_t)peer;
pp->io.datalen = 0;
pp->io.fd = -1;
- pp->unitptr = (caddr_t)getShmTime(unit);
+
+ up = (struct shmunit *) emalloc(sizeof(*up));
+ if (up == NULL)
+ return (FALSE);
+ memset((char *)up, 0, sizeof(*up));
+ pp->unitptr = (caddr_t)up;
+
+ up->shm = getShmTime(unit);
/*
* Initialize miscellaneous peer variables
*/
memcpy((char *)&pp->refid, REFID, 4);
- if (pp->unitptr!=0) {
- ((struct shmTime*)pp->unitptr)->precision=PRECISION;
- peer->precision = ((struct shmTime*)pp->unitptr)->precision;
- ((struct shmTime*)pp->unitptr)->valid=0;
- ((struct shmTime*)pp->unitptr)->nsamples=NSAMPLES;
+ if (up->shm != 0) {
+ up->shm->precision = PRECISION;
+ peer->precision = up->shm->precision;
+ up->shm->valid=0;
+ up->shm->nsamples=NSAMPLES;
pp->clockdesc = DESCRIPTION;
return (1);
}
struct peer *peer
)
{
- register struct shmTime *up;
struct refclockproc *pp;
+ struct shmunit *up;
pp = peer->procptr;
- up = (struct shmTime *)pp->unitptr;
+ up = (struct shmunit *)pp->unitptr;
#ifndef SYS_WINNT
/* HMS: shmdt()wants char* or const void * */
- (void) shmdt (up);
+ (void) shmdt ((char *)up->shm);
#else
- UnmapViewOfFile (up);
+ UnmapViewOfFile (up->shm);
#endif
}
+/*
+ * shm_timer - called every second
+ */
+static void
+shm_timer(int unit, struct peer *peer)
+{
+ if (OLDWAY)
+ return;
+
+ shm_peek(unit, peer);
+}
+
+
/*
* shm_poll - called by the transmit procedure
*/
struct peer *peer
)
{
- register struct shmTime *up;
struct refclockproc *pp;
+ int ok;
+
+ pp = peer->procptr;
+
+ if (OLDWAY) {
+ ok = shm_peek(unit, peer);
+ if (!ok) return;
+ }
+
+ /*
+ * Process median filter samples. If none received, declare a
+ * timeout and keep going.
+ */
+ if (pp->coderecv == pp->codeproc) {
+ refclock_report(peer, CEVNT_TIMEOUT);
+ shm_clockstats(unit, peer);
+ return;
+ }
+ pp->lastref = pp->lastrec;
+ refclock_receive(peer);
+ shm_clockstats(unit, peer);
+}
+
+/*
+ * shm_peek - try to grab a sample
+ */
+int shm_peek(
+ int unit,
+ struct peer *peer
+ )
+{
+ struct refclockproc *pp;
+ struct shmunit *up;
+ struct shmTime *shm;
/*
* This is the main routine. It snatches the time from the shm
* board and tacks on a local timestamp.
*/
pp = peer->procptr;
- up = (struct shmTime*)pp->unitptr;
- if (up==0) { /* try to map again - this may succeed if meanwhile some-
- body has ipcrm'ed the old (unaccessible) shared mem
- segment */
- pp->unitptr = (caddr_t)getShmTime(unit);
- up = (struct shmTime*)pp->unitptr;
+ up = (struct shmunit*)pp->unitptr;
+ up->ticks++;
+ if (up->shm == 0) {
+ /* try to map again - this may succeed if meanwhile some-
+ body has ipcrm'ed the old (unaccessible) shared mem segment */
+ up->shm = getShmTime(unit);
}
- if (up==0) {
+ shm = up->shm;
+ if (shm == 0) {
refclock_report(peer, CEVNT_FAULT);
- return;
+ return(0);
}
- if (up->valid) {
+ if (shm->valid) {
struct timeval tvr;
struct timeval tvt;
struct tm *t;
tvr.tv_usec = 0;
tvt.tv_sec = 0;
tvt.tv_usec = 0;
- switch (up->mode) {
+ switch (shm->mode) {
case 0: {
- tvr.tv_sec=up->receiveTimeStampSec;
- tvr.tv_usec=up->receiveTimeStampUSec;
- tvt.tv_sec=up->clockTimeStampSec;
- tvt.tv_usec=up->clockTimeStampUSec;
+ tvr.tv_sec=shm->receiveTimeStampSec;
+ tvr.tv_usec=shm->receiveTimeStampUSec;
+ tvt.tv_sec=shm->clockTimeStampSec;
+ tvt.tv_usec=shm->clockTimeStampUSec;
}
break;
case 1: {
- int cnt=up->count;
- tvr.tv_sec=up->receiveTimeStampSec;
- tvr.tv_usec=up->receiveTimeStampUSec;
- tvt.tv_sec=up->clockTimeStampSec;
- tvt.tv_usec=up->clockTimeStampUSec;
- ok=(cnt==up->count);
+ int cnt=shm->count;
+ tvr.tv_sec=shm->receiveTimeStampSec;
+ tvr.tv_usec=shm->receiveTimeStampUSec;
+ tvt.tv_sec=shm->clockTimeStampSec;
+ tvt.tv_usec=shm->clockTimeStampUSec;
+ ok=(cnt==shm->count);
}
break;
default:
- msyslog (LOG_ERR, "SHM: bad mode found in shared memory: %d",up->mode);
+ msyslog (LOG_ERR, "SHM: bad mode found in shared memory: %d",shm->mode);
}
- up->valid=0;
+ shm->valid=0;
if (ok) {
time_t help; /* XXX NetBSD has incompatible tv_sec */
pp->minute=t->tm_min;
pp->second=t->tm_sec;
pp->nsec=tvt.tv_usec * 1000;
- peer->precision=up->precision;
- pp->leap=up->leap;
+ peer->precision=shm->precision;
+ pp->leap=shm->leap;
}
else {
refclock_report(peer, CEVNT_FAULT);
msyslog (LOG_NOTICE, "SHM: access clash in shared memory");
- return;
+ up->clash++;
+ return(0);
}
}
else {
refclock_report(peer, CEVNT_TIMEOUT);
- /*
- msyslog (LOG_NOTICE, "SHM: no new value found in shared memory");
- */
- return;
+ up->notready++;
+ return(0);
}
if (!refclock_process(pp)) {
refclock_report(peer, CEVNT_BADTIME);
- return;
+ up->bad++;
+ return(0);
}
- pp->lastref = pp->lastrec;
- refclock_receive(peer);
+ up->good++;
+ return(1);
+}
+
+/*
+ * shm_clockstats - dump and reset counters
+ */
+void shm_clockstats(
+ int unit,
+ struct peer *peer
+ )
+{
+ struct refclockproc *pp;
+ struct shmunit *up;
+ char logbuf[256];
+
+ pp = peer->procptr;
+ up = (struct shmunit*)pp->unitptr;
+
+ if (!(pp->sloppyclockflag & CLK_FLAG4)) return;
+
+ snprintf(logbuf, sizeof(logbuf), "%3d %3d %3d %3d %3d",
+ up->ticks, up->good, up->notready, up->bad, up->clash);
+ record_clock_stats(&peer->srcadr, logbuf);
+
+ up->ticks = up->good = up->notready =up->bad = up->clash = 0;
+
}
#else