void delay_ms(unsigned int delay){ unsigned long long alermTime; alermTime = get_systime() + delay * 1000; while(get_systime() < alermTime); return; }
int main(int argc, char** argv) { int nreps = 1; if (argc != 2 && argc != 3) usage(argv[0]); char* filename = argv[1]; if (argc == 3) (void)sscanf(argv[2], "%d", &nreps); double s, e, t; int bc; for (int i = 0; i < nreps; i++) { s = get_systime(); bc = read_file_mlr_get_line(filename); e = get_systime(); t = e - s; printf("type=getdelim,t=%.6lf,n=%d\n", t, bc); fflush(stdout); s = get_systime(); bc = read_file_fgetc(filename); e = get_systime(); t = e - s; printf("type=fgetc,t=%.6lf,n=%d\n", t, bc); fflush(stdout); s = get_systime(); bc = read_file_fgetc_psb(filename); e = get_systime(); t = e - s; printf("type=fgetc_psb,t=%.6lf,n=%d\n", t, bc); fflush(stdout); s = get_systime(); bc = read_file_mmap_psb(filename); e = get_systime(); t = e - s; printf("type=mmap_psb,t=%.6lf,n=%d\n", t, bc); fflush(stdout); s = get_systime(); bc = read_file_pfr_psb(filename); e = get_systime(); t = e - s; printf("type=pfr_psb,t=%.6lf,n=%d\n", t, bc); fflush(stdout); } return 0; }
/* * record_loop_stats - write loop filter statistics to file * * file format: * day (MJD) * time (s past midnight) * offset * frequency (PPM) * jitter * wnder (PPM) * time constant (log2) */ void record_loop_stats( double offset, /* offset */ double freq, /* frequency (PPM) */ double jitter, /* jitter */ double wander, /* wander (PPM) */ int spoll ) { l_fp now; u_long day; if (!stats_control) return; get_systime(&now); filegen_setup(&loopstats, now.l_ui); day = now.l_ui / 86400 + MJD_1900; now.l_ui %= 86400; if (loopstats.fp != NULL) { fprintf(loopstats.fp, "%lu %s %.9f %.3f %.9f %.6f %d\n", day, ulfptoa(&now, 3), offset, freq * 1e6, jitter, wander * 1e6, spoll); fflush(loopstats.fp); } }
static RETSIGTYPE sigio_handler( int sig ) { int saved_errno = errno; l_fp ts; get_systime(&ts); # if defined(HAVE_SIGACTION) sigio_handler_active++; if (sigio_handler_active != 1) /* This should never happen! */ msyslog(LOG_ERR, "sigio_handler: sigio_handler_active != 1"); # endif INSIST(input_handler_callback != NULL); (*input_handler_callback)(&ts); # if defined(HAVE_SIGACTION) sigio_handler_active--; if (sigio_handler_active != 0) /* This should never happen! */ msyslog(LOG_ERR, "sigio_handler: sigio_handler_active != 0"); # endif errno = saved_errno; }
/* * record_peer_stats - write peer statistics to file * * file format: * day (MJD) * time (s past UTC midnight) * IP address * status word (hex) * offset * delay * dispersion * jitter */ void record_peer_stats( sockaddr_u *addr, int status, double offset, /* offset */ double delay, /* delay */ double dispersion, /* dispersion */ double jitter /* jitter */ ) { l_fp now; u_long day; if (!stats_control) return; get_systime(&now); filegen_setup(&peerstats, now.l_ui); day = now.l_ui / 86400 + MJD_1900; now.l_ui %= 86400; if (peerstats.fp != NULL) { fprintf(peerstats.fp, "%lu %s %s %x %.9f %.9f %.9f %.9f\n", day, ulfptoa(&now, 3), stoa(addr), status, offset, delay, dispersion, jitter); fflush(peerstats.fp); } }
/* * record_raw_stats - write raw timestamps to file * * file format * day (MJD) * time (s past midnight) * peer ip address * IP address * t1 t2 t3 t4 timestamps */ void record_raw_stats( sockaddr_u *srcadr, sockaddr_u *dstadr, l_fp *t1, /* originate timestamp */ l_fp *t2, /* receive timestamp */ l_fp *t3, /* transmit timestamp */ l_fp *t4 /* destination timestamp */ ) { l_fp now; u_long day; if (!stats_control) return; get_systime(&now); filegen_setup(&rawstats, now.l_ui); day = now.l_ui / 86400 + MJD_1900; now.l_ui %= 86400; if (rawstats.fp != NULL) { fprintf(rawstats.fp, "%lu %s %s %s %s %s %s %s\n", day, ulfptoa(&now, 3), stoa(srcadr), dstadr ? stoa(dstadr) : "-", ulfptoa(t1, 9), ulfptoa(t2, 9), ulfptoa(t3, 9), ulfptoa(t4, 9)); fflush(rawstats.fp); } }
/* * record_sys_stats - write system statistics to file * * file format * day (MJD) * time (s past midnight) * time since reset * packets recieved * packets for this host * current version * old version * access denied * bad length or format * bad authentication * declined * rate exceeded * KoD sent */ void record_sys_stats(void) { l_fp now; u_long day; if (!stats_control) return; get_systime(&now); filegen_setup(&sysstats, now.l_ui); day = now.l_ui / 86400 + MJD_1900; now.l_ui %= 86400; if (sysstats.fp != NULL) { fprintf(sysstats.fp, "%lu %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", day, ulfptoa(&now, 3), current_time - sys_stattime, sys_received, sys_processed, sys_newversion, sys_oldversion, sys_restricted, sys_badlength, sys_badauth, sys_declined, sys_limitrejected, sys_kodsent); fflush(sysstats.fp); proto_clr_stats(); } }
/* * record_crypto_stats - write crypto statistics to file * * file format: * day (mjd) * time (s past midnight) * peer ip address * text message */ void record_crypto_stats( sockaddr_u *addr, const char *text /* text message */ ) { l_fp now; u_long day; if (!stats_control) return; get_systime(&now); filegen_setup(&cryptostats, now.l_ui); day = now.l_ui / 86400 + MJD_1900; now.l_ui %= 86400; if (cryptostats.fp != NULL) { if (addr == NULL) fprintf(cryptostats.fp, "%lu %s 0.0.0.0 %s\n", day, ulfptoa(&now, 3), text); else fprintf(cryptostats.fp, "%lu %s %s %s\n", day, ulfptoa(&now, 3), stoa(addr), text); fflush(cryptostats.fp); } }
/* * as2201_poll - called by the transmit procedure * * We go to great pains to avoid changing state here, since there may be * more than one eavesdropper receiving the same timecode. */ static void as2201_poll( int unit, struct peer *peer ) { struct refclockproc *pp; /* * Send a "\r*toc\r" to get things going. We go to great pains * to avoid changing state, since there may be more than one * eavesdropper watching the radio. */ pp = peer->procptr; if (write(pp->io.fd, "\r*toc\r", 6) != 6) { refclock_report(peer, CEVNT_FAULT); } else { pp->polls++; if (!(pp->sloppyclockflag & CLK_FLAG2)) get_systime(&pp->lastrec); } if (pp->coderecv == pp->codeproc) { refclock_report(peer, CEVNT_TIMEOUT); return; } refclock_receive(peer); }
/* * hopfpci_start - attach to hopf PCI board 6039 */ static int hopfpci_start( int unit, struct peer *peer ) { struct refclockproc *pp; struct hopfclock_unit *up; /* * Allocate and initialize unit structure */ up = (struct hopfclock_unit *) emalloc(sizeof(struct hopfclock_unit)); if (!(up)) { msyslog(LOG_ERR, "hopfPCIClock(%d) emalloc: %m",unit); #ifdef DEBUG printf("hopfPCIClock(%d) emalloc\n",unit); #endif return (0); } memset((char *)up, 0, sizeof(struct hopfclock_unit)); #ifndef SYS_WINNT fd = open(DEVICE,O_RDWR); /* try to open hopf clock device */ #else if (!OpenHopfDevice()){ msyslog(LOG_ERR,"Start: %s unit: %d failed!",DEVICE,unit); return (0); } #endif pp = peer->procptr; pp->io.clock_recv = noentry; pp->io.srcclock = (caddr_t)peer; pp->io.datalen = 0; pp->io.fd = INVALID_SOCKET; pp->unitptr = (caddr_t)up; get_systime(&pp->lastrec); /* * Initialize miscellaneous peer variables */ if (pp->unitptr!=0) { memcpy((char *)&pp->refid, REFID, 4); peer->precision = PRECISION; pp->clockdesc = DESCRIPTION; up->leap_status = 0; up->unit = (short) unit; return (1); } else { return 0; } }
/* * refclock_transmit - simulate the transmit procedure * * This routine implements the NTP transmit procedure for a reference * clock. This provides a mechanism to call the driver at the NTP poll * interval, as well as provides a reachability mechanism to detect a * broken radio or other madness. */ void refclock_transmit( struct peer *peer /* peer structure pointer */ ) { u_char clktype; int unit; clktype = peer->refclktype; unit = peer->refclkunit; peer->sent++; get_systime(&peer->xmt); /* * This is a ripoff of the peer transmit routine, but * specialized for reference clocks. We do a little less * protocol here and call the driver-specific transmit routine. */ if (peer->burst == 0) { u_char oreach; #ifdef DEBUG if (debug) printf("refclock_transmit: at %ld %s\n", current_time, stoa(&(peer->srcadr))); #endif /* * Update reachability and poll variables like the * network code. */ oreach = peer->reach; peer->reach <<= 1; peer->outdate = current_time; if (!peer->reach) { if (oreach) { report_event(EVNT_UNREACH, peer); peer->timereachable = current_time; } } else { if (!(oreach & 0x07)) { clock_filter(peer, 0., 0., MAXDISPERSE); clock_select(); } if (peer->flags & FLAG_BURST) peer->burst = NSTAGE; } } else { peer->burst--; } if (refclock_conf[clktype]->clock_poll != noentry) (refclock_conf[clktype]->clock_poll)(unit, peer); poll_update(peer, peer->hpoll); }
/* psc_start: open device and initialize data for processing */ static int psc_start( int unit, struct peer *peer ) { char buf[BUFSIZE]; struct refclockproc *pp; struct psc_unit *up = emalloc(sizeof *up); if (unit < 0 || unit > 1) { /* support units 0 and 1 */ msyslog(LOG_ERR, "psc_start: bad unit: %d", unit); return 0; } if (!up) { msyslog(LOG_ERR, "psc_start: unit: %d, emalloc: %m", unit); return 0; } memset(up, '\0', sizeof *up); sprintf(buf, DEVICE, unit); /* dev file name */ fd[unit] = open(buf, O_RDONLY); /* open device file */ if (fd[unit] < 0) { msyslog(LOG_ERR, "psc_start: unit: %d, open failed. %m", unit); return 0; } /* get the address of the mapped regs */ if (ioctl(fd[unit], PSC_REGS, ®p[unit]) < 0) { msyslog(LOG_ERR, "psc_start: unit: %d, ioctl failed. %m", unit); return 0; } /* initialize peer variables */ 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) up; get_systime(&pp->lastrec); memcpy((char *)&pp->refid, REFID, 4); peer->precision = PRECISION; pp->clockdesc = DESCRIPTION; up->unit = unit; #ifdef __hpux rtprio(0,120); /* set real time priority */ plock(PROCLOCK); /* lock process in memory */ #endif /* __hpux */ return 1; }
/* * refclock_receive - simulate the receive and packet procedures * * This routine simulates the NTP receive and packet procedures for a * reference clock. This provides a mechanism in which the ordinary NTP * filter, selection and combining algorithms can be used to suppress * misbehaving radios and to mitigate between them when more than one is * available for backup. */ void refclock_receive( struct peer *peer /* peer structure pointer */ ) { struct refclockproc *pp; #ifdef DEBUG if (debug) printf("refclock_receive: at %lu %s\n", current_time, stoa(&peer->srcadr)); #endif /* * Do a little sanity dance and update the peer structure. Groom * the median filter samples and give the data to the clock * filter. */ pp = peer->procptr; peer->leap = pp->leap; if (peer->leap == LEAP_NOTINSYNC) return; peer->received++; peer->timereceived = current_time; if (!peer->reach) { report_event(EVNT_REACH, peer); peer->timereachable = current_time; } peer->reach |= 1; peer->reftime = pp->lastref; peer->org = pp->lastrec; peer->rootdispersion = pp->disp; get_systime(&peer->rec); if (!refclock_sample(pp)) return; clock_filter(peer, pp->offset, 0., pp->jitter); record_peer_stats(&peer->srcadr, ctlpeerstatus(peer), peer->offset, peer->delay, clock_phi * (current_time - peer->epoch), peer->jitter); if (cal_enable && last_offset < MINDISPERSE) { #ifdef KERNEL_PLL if (peer != sys_peer || pll_status & STA_PPSTIME) #else if (peer != sys_peer) #endif /* KERNEL_PLL */ pp->fudgetime1 -= pp->offset * FUDGEFAC; else pp->fudgetime1 -= pp->fudgetime1 * FUDGEFAC; } }
int ntpq_read_assoc_peervars( associd_t associd, char *resultbuf, int maxsize ) { const char *datap; int res; int dsize; u_short rstatus; l_fp rec; l_fp ts; char value[NTPQ_BUFLEN]; res = doquery(CTL_OP_READVAR, associd, 0, 0, NULL, &rstatus, &dsize, &datap); if (res != 0) return 0; get_systime(&ts); if (dsize == 0) { if (numhosts > 1) fprintf(stderr, "server=%s ", currenthost); fprintf(stderr, "***No information returned for association %d\n", associd); return 0; } else { if ( dsize > maxsize ) dsize = maxsize; memcpy(resultbuf, datap, dsize); resultbuf[dsize] = '\0'; ntpq_getvar(resultbuf, dsize, "rec", value, sizeof(value)); if (!decodets(value, &rec)) L_CLR(&rec); memcpy(resultbuf, value, maxsize); resultbuf[dsize] = '\0'; dsize = strlen(resultbuf); } return dsize; }
/* * hopfpci_start - attach to hopf PCI board 6039 */ static int hopfpci_start( int unit, struct peer *peer ) { struct refclockproc *pp; struct hopfclock_unit *up; /* * Allocate and initialize unit structure */ up = emalloc_zero(sizeof(*up)); #ifndef SYS_WINNT fd = open(DEVICE,O_RDWR); /* try to open hopf clock device */ #else if (!OpenHopfDevice()) { msyslog(LOG_ERR, "Start: %s unit: %d failed!", DEVICE, unit); free(up); return (0); } #endif pp = peer->procptr; pp->io.clock_recv = noentry; pp->io.srcclock = peer; pp->io.datalen = 0; pp->io.fd = INVALID_SOCKET; pp->unitptr = up; get_systime(&pp->lastrec); /* * Initialize miscellaneous peer variables */ memcpy((char *)&pp->refid, REFID, 4); peer->precision = PRECISION; pp->clockdesc = DESCRIPTION; up->leap_status = 0; up->unit = (short) unit; return (1); }
/* * refclock_receive - simulate the receive and packet procedures * * This routine simulates the NTP receive and packet procedures for a * reference clock. This provides a mechanism in which the ordinary NTP * filter, selection and combining algorithms can be used to suppress * misbehaving radios and to mitigate between them when more than one is * available for backup. */ void refclock_receive( struct peer *peer /* peer structure pointer */ ) { struct refclockproc *pp; #ifdef DEBUG if (debug) printf("refclock_receive: at %lu %s\n", current_time, stoa(&peer->srcadr)); #endif /* * Do a little sanity dance and update the peer structure. Groom * the median filter samples and give the data to the clock * filter. */ pp = peer->procptr; peer->leap = pp->leap; if (peer->leap == LEAP_NOTINSYNC) return; peer->received++; peer->timereceived = current_time; if (!peer->reach) { report_event(PEVNT_REACH, peer, NULL); peer->timereachable = current_time; } peer->reach |= 1; peer->reftime = pp->lastref; peer->aorg = pp->lastrec; peer->rootdisp = pp->disp; get_systime(&peer->dst); if (!refclock_sample(pp)) return; clock_filter(peer, pp->offset, 0., pp->jitter); if (cal_enable && fabs(last_offset) < sys_mindisp && sys_peer != NULL) { if (sys_peer->refclktype == REFCLK_ATOM_PPS && peer->refclktype != REFCLK_ATOM_PPS) pp->fudgetime1 -= pp->offset * FUDGEFAC; } }
void test_OffsetCalculationNegativeOffset(void) { struct pkt rpkt; l_fp reftime, tmp; struct timeval dst; double offset, precision, synch_distance; rpkt.precision = -1; rpkt.rootdelay = HTONS_FP(DTOUFP(0.5)); rpkt.rootdisp = HTONS_FP(DTOUFP(0.5)); /* Synch Distance is (0.5+0.5)/2.0, or 0.5 */ get_systime(&reftime); HTONL_FP(&reftime, &rpkt.reftime); /* T1 - Originate timestamp */ tmp.l_ui = 1000000001UL; tmp.l_uf = 0UL; HTONL_FP(&tmp, &rpkt.org); /* T2 - Receive timestamp */ tmp.l_ui = 1000000000UL; tmp.l_uf = 2147483648UL; HTONL_FP(&tmp, &rpkt.rec); /*/ T3 - Transmit timestamp */ tmp.l_ui = 1000000001UL; tmp.l_uf = 2147483648UL; HTONL_FP(&tmp, &rpkt.xmt); /* T4 - Destination timestamp as standard timeval */ tmp.l_ui = 1000000003UL; tmp.l_uf = 0UL; TSTOTV(&tmp, &dst); dst.tv_sec -= JAN_1970; offset_calculation(&rpkt, LEN_PKT_NOMAC, &dst, &offset, &precision, &synch_distance); TEST_ASSERT_EQUAL_DOUBLE(-1, offset); TEST_ASSERT_EQUAL_DOUBLE(1. / ULOGTOD(1), precision); TEST_ASSERT_EQUAL_DOUBLE(1.3333483333333334, synch_distance); }
void test_OffsetCalculationPositiveOffset(void) { struct pkt rpkt; rpkt.precision = -16; // 0,000015259 rpkt.rootdelay = HTONS_FP(DTOUFP(0.125)); rpkt.rootdisp = HTONS_FP(DTOUFP(0.25)); // Synch Distance: (0.125+0.25)/2.0 == 0.1875 l_fp reftime; get_systime(&reftime); HTONL_FP(&reftime, &rpkt.reftime); l_fp tmp; // T1 - Originate timestamp tmp.l_ui = 1000000000UL; tmp.l_uf = 0UL; HTONL_FP(&tmp, &rpkt.org); // T2 - Receive timestamp tmp.l_ui = 1000000001UL; tmp.l_uf = 2147483648UL; HTONL_FP(&tmp, &rpkt.rec); // T3 - Transmit timestamp tmp.l_ui = 1000000002UL; tmp.l_uf = 0UL; HTONL_FP(&tmp, &rpkt.xmt); // T4 - Destination timestamp as standard timeval tmp.l_ui = 1000000001UL; tmp.l_uf = 0UL; struct timeval dst; TSTOTV(&tmp, &dst); dst.tv_sec -= JAN_1970; double offset, precision, synch_distance; offset_calculation(&rpkt, LEN_PKT_NOMAC, &dst, &offset, &precision, &synch_distance); TEST_ASSERT_EQUAL_DOUBLE(1.25, offset); TEST_ASSERT_EQUAL_DOUBLE(1. / ULOGTOD(16), precision); // 1.1250150000000001 ? TEST_ASSERT_EQUAL_DOUBLE(1.125015, synch_distance); }
/** ****************************************************************************** * @brief esc_key_detect * @param[in] None * @param[out] None * @retval true-成功、false-失败 * * @details 按键检测 * * @note ****************************************************************************** */ LOCAL boolean esc_key_detect(void) { uint8 key; boolean result = false; while(true) { // 如果超过3秒,则结束等待 if (3 < get_systime()) break; // 检测是否已按下"ESC"键 if (uart_tryReceive(&key) && (27 == key)) { result = true; break; } } print("\r\n"); return (result); }
/* * wwvb_timer - called once per second by the transmit procedure */ static void wwvb_timer( int unit, struct peer *peer ) { register struct wwvbunit *up; struct refclockproc *pp; char pollchar; /* character sent to clock */ #ifdef DEBUG l_fp now; #endif /* * Time to poll the clock. The Spectracom clock responds to a * 'T' by returning a timecode in the format(s) specified above. * Note there is no checking on state, since this may not be the * only customer reading the clock. Only one customer need poll * the clock; all others just listen in. */ pp = peer->procptr; up = pp->unitptr; if (up->linect > 0) pollchar = 'R'; else pollchar = 'T'; if (write(pp->io.fd, &pollchar, 1) != 1) refclock_report(peer, CEVNT_FAULT); #ifdef DEBUG get_systime(&now); if (debug) printf("%c poll at %s\n", pollchar, prettydate(&now)); #endif #ifdef HAVE_PPSAPI if (up->ppsapi_lit && refclock_pps(peer, &up->atom, pp->sloppyclockflag) > 0) { up->pcount++, peer->flags |= FLAG_PPS; peer->precision = PPS_PRECISION; } #endif /* HAVE_PPSAPI */ }
TEST_F(mainTest, OffsetCalculationNegativeOffset) { pkt rpkt; rpkt.precision = -1; rpkt.rootdelay = HTONS_FP(DTOUFP(0.5)); rpkt.rootdisp = HTONS_FP(DTOUFP(0.5)); // Synch Distance is (0.5+0.5)/2.0, or 0.5 l_fp reftime; get_systime(&reftime); HTONL_FP(&reftime, &rpkt.reftime); l_fp tmp; // T1 - Originate timestamp tmp.l_ui = 1000000001UL; tmp.l_uf = 0UL; HTONL_FP(&tmp, &rpkt.org); // T2 - Receive timestamp tmp.l_ui = 1000000000UL; tmp.l_uf = 2147483648UL; HTONL_FP(&tmp, &rpkt.rec); // T3 - Transmit timestamp tmp.l_ui = 1000000001UL; tmp.l_uf = 2147483648UL; HTONL_FP(&tmp, &rpkt.xmt); // T4 - Destination timestamp as standard timeval tmp.l_ui = 1000000003UL; tmp.l_uf = 0UL; timeval dst; TSTOTV(&tmp, &dst); dst.tv_sec -= JAN_1970; double offset, precision, synch_distance; offset_calculation(&rpkt, LEN_PKT_NOMAC, &dst, &offset, &precision, &synch_distance); EXPECT_DOUBLE_EQ(-1, offset); EXPECT_DOUBLE_EQ(1. / ULOGTOD(1), precision); EXPECT_DOUBLE_EQ(1.3333483333333334, synch_distance); }
/* * record_proto_stats - write system statistics to file * * file format * day (MJD) * time (s past midnight) * text message */ void record_proto_stats( char *str /* text string */ ) { l_fp now; u_long day; if (!stats_control) return; get_systime(&now); filegen_setup(&protostats, now.l_ui); day = now.l_ui / 86400 + MJD_1900; now.l_ui %= 86400; if (protostats.fp != NULL) { fprintf(protostats.fp, "%lu %s %s\n", day, ulfptoa(&now, 3), str); fflush(protostats.fp); } }
TEST_F(mainTest, HandleCorrectPacket) { pkt rpkt; sockaddr_u host; int rpktl; l_fp now; // We don't want our testing code to actually change the system clock. ASSERT_FALSE(ENABLED_OPT(STEP)); ASSERT_FALSE(ENABLED_OPT(SLEW)); get_systime(&now); HTONL_FP(&now, &rpkt.reftime); HTONL_FP(&now, &rpkt.org); HTONL_FP(&now, &rpkt.rec); HTONL_FP(&now, &rpkt.xmt); rpktl = LEN_PKT_NOMAC; ZERO(host); AF(&host) = AF_INET; EXPECT_EQ(0, handle_pkt(rpktl, &rpkt, &host, "")); }
/* * record_raw_stats - write raw timestamps to file * * file format * day (MJD) * time (s past midnight) * peer ip address * IP address * t1 t2 t3 t4 timestamps */ void record_raw_stats( sockaddr_u *srcadr, sockaddr_u *dstadr, l_fp *t1, /* originate timestamp */ l_fp *t2, /* receive timestamp */ l_fp *t3, /* transmit timestamp */ l_fp *t4, /* destination timestamp */ int leap, int version, int mode, int stratum, int ppoll, int precision, double root_delay, /* seconds */ double root_dispersion,/* seconds */ u_int32 refid ) { l_fp now; u_long day; if (!stats_control) return; get_systime(&now); filegen_setup(&rawstats, now.l_ui); day = now.l_ui / 86400 + MJD_1900; now.l_ui %= 86400; if (rawstats.fp != NULL) { fprintf(rawstats.fp, "%lu %s %s %s %s %s %s %s %d %d %d %d %d %d %.6f %.6f %s\n", day, ulfptoa(&now, 3), stoa(srcadr), dstadr ? stoa(dstadr) : "-", ulfptoa(t1, 9), ulfptoa(t2, 9), ulfptoa(t3, 9), ulfptoa(t4, 9), leap, version, mode, stratum, ppoll, precision, root_delay, root_dispersion, refid_str(refid, stratum)); fflush(rawstats.fp); } }
/* * record_clock_stats - write clock statistics to file * * file format: * day (MJD) * time (s past midnight) * IP address * text message */ void record_clock_stats( sockaddr_u *addr, const char *text /* timecode string */ ) { l_fp now; u_long day; if (!stats_control) return; get_systime(&now); filegen_setup(&clockstats, now.l_ui); day = now.l_ui / 86400 + MJD_1900; now.l_ui %= 86400; if (clockstats.fp != NULL) { fprintf(clockstats.fp, "%lu %s %s %s\n", day, ulfptoa(&now, 3), stoa(addr), text); fflush(clockstats.fp); } }
void test_HandleCorrectPacket(void) { struct pkt rpkt; sockaddr_u host; int rpktl; l_fp now; /* We don't want our testing code to actually change the system clock. */ TEST_ASSERT_FALSE(ENABLED_OPT(STEP)); TEST_ASSERT_FALSE(ENABLED_OPT(SLEW)); get_systime(&now); HTONL_FP(&now, &rpkt.reftime); HTONL_FP(&now, &rpkt.org); HTONL_FP(&now, &rpkt.rec); HTONL_FP(&now, &rpkt.xmt); rpktl = LEN_PKT_NOMAC; ZERO(host); AF(&host) = AF_INET; TEST_ASSERT_EQUAL(0, handle_pkt(rpktl, &rpkt, &host, "")); }
/* * record_timing_stats - write timing statistics to file * * file format: * day (mjd) * time (s past midnight) * text message */ void record_timing_stats( const char *text /* text message */ ) { static unsigned int flshcnt; l_fp now; u_long day; if (!stats_control) return; get_systime(&now); filegen_setup(&timingstats, now.l_ui); day = now.l_ui / 86400 + MJD_1900; now.l_ui %= 86400; if (timingstats.fp != NULL) { fprintf(timingstats.fp, "%lu %s %s\n", day, lfptoa(&now, 3), text); if (++flshcnt % 100 == 0) fflush(timingstats.fp); } }
/* * refclock_gtraw - get next line/chunk of data * * This routine returns the raw data received from the clock in both * canonical or raw modes. The terminal interface routines map CR to LF. * In canonical mode this results in two lines, one containing data * followed by LF and another containing only LF. In raw mode the * interface routines can deliver arbitraty chunks of data from one * character to a maximum specified by the calling routine. In either * mode the routine returns the number of characters in the line * followed by a NULL character ('\0'), which is not included in the * count. * * If a timestamp is present in the timecode, as produced by the tty_clk * STREAMS module, it returns that as the timestamp; otherwise, it * returns the buffer timestamp. */ int refclock_gtraw( struct recvbuf *rbufp, /* receive buffer pointer */ char *lineptr, /* current line pointer */ int bmax, /* remaining characters in line */ l_fp *tsptr /* pointer to timestamp returned */ ) { char *dpt, *dpend, *dp; l_fp trtmp, tstmp; int i; /* * Check for the presence of a timestamp left by the tty_clock * module and, if present, use that instead of the buffer * timestamp captured by the I/O routines. We recognize a * timestamp by noting its value is earlier than the buffer * timestamp, but not more than one second earlier. */ dpt = (char *)rbufp->recv_buffer; dpend = dpt + rbufp->recv_length; trtmp = rbufp->recv_time; if (dpend >= dpt + 8) { if (buftvtots(dpend - 8, &tstmp)) { L_SUB(&trtmp, &tstmp); if (trtmp.l_ui == 0) { #ifdef DEBUG if (debug > 1) { printf( "refclock_gtlin: fd %d ldisc %s", rbufp->fd, lfptoa(&trtmp, 6)); get_systime(&trtmp); L_SUB(&trtmp, &tstmp); printf(" sigio %s\n", lfptoa(&trtmp, 6)); } #endif dpend -= 8; trtmp = tstmp; } else trtmp = rbufp->recv_time; } } /* * Copy the raw buffer to the user string. The string is padded * with a NULL, which is not included in the character count. */ if (dpend - dpt > bmax - 1) dpend = dpt + bmax - 1; for (dp = lineptr; dpt < dpend; dpt++) *dp++ = *dpt; *dp = '\0'; i = dp - lineptr; #ifdef DEBUG if (debug > 1) printf("refclock_gtraw: fd %d time %s timecode %d %s\n", rbufp->fd, ulfptoa(&trtmp, 6), i, lineptr); #endif *tsptr = trtmp; return (i); }
/* * tpro_poll - called by the transmit procedure */ static void tpro_poll( int unit, struct peer *peer ) { register struct tprounit *up; struct refclockproc *pp; struct tproval *tp; /* * This is the main routine. It snatches the time from the TPRO * board and tacks on a local timestamp. */ pp = peer->procptr; up = pp->unitptr; tp = &up->tprodata; if (read(pp->io.fd, (char *)tp, sizeof(struct tproval)) < 0) { refclock_report(peer, CEVNT_FAULT); return; } get_systime(&pp->lastrec); pp->polls++; /* * We get down to business, check the timecode format and decode * its contents. If the timecode has invalid length or is not in * proper format, we declare bad format and exit. Note: we * can't use the sec/usec conversion produced by the driver, * since the year may be suspect. All format error checking is * done by the snprintf() and sscanf() routines. * * Note that the refclockproc usec member has now become nsec. * We could either multiply the read-in usec value by 1000 or * we could pad the written string appropriately and read the * resulting value in already scaled. */ snprintf(pp->a_lastcode, sizeof(pp->a_lastcode), "%1x%1x%1x %1x%1x:%1x%1x:%1x%1x.%1x%1x%1x%1x%1x%1x %1x", tp->day100, tp->day10, tp->day1, tp->hour10, tp->hour1, tp->min10, tp->min1, tp->sec10, tp->sec1, tp->ms100, tp->ms10, tp->ms1, tp->usec100, tp->usec10, tp->usec1, tp->status); pp->lencode = strlen(pp->a_lastcode); #ifdef DEBUG if (debug) printf("tpro: time %s timecode %d %s\n", ulfptoa(&pp->lastrec, 6), pp->lencode, pp->a_lastcode); #endif if (sscanf(pp->a_lastcode, "%3d %2d:%2d:%2d.%6ld", &pp->day, &pp->hour, &pp->minute, &pp->second, &pp->nsec) != 5) { refclock_report(peer, CEVNT_BADTIME); return; } pp->nsec *= 1000; /* Convert usec to nsec */ if (!tp->status & 0x3) pp->leap = LEAP_NOTINSYNC; else pp->leap = LEAP_NOWARNING; if (!refclock_process(pp)) { refclock_report(peer, CEVNT_BADTIME); return; } if (pp->coderecv == pp->codeproc) { refclock_report(peer, CEVNT_TIMEOUT); return; } pp->lastref = pp->lastrec; record_clock_stats(&peer->srcadr, pp->a_lastcode); refclock_receive(peer); }
/* * stats_config - configure the stats operation */ void stats_config( int item, const char *invalue /* only one type so far */ ) { FILE *fp; const char *value; int len; char tbuf[80]; char str1[20], str2[20]; #ifndef VMS const char temp_ext[] = ".TEMP"; #else const char temp_ext[] = "-TEMP"; #endif /* * Expand environment strings under Windows NT, since the * command interpreter doesn't do this, the program must. */ #ifdef SYS_WINNT char newvalue[MAX_PATH], parameter[MAX_PATH]; if (!ExpandEnvironmentStrings(invalue, newvalue, MAX_PATH)) { switch(item) { case STATS_FREQ_FILE: strcpy(parameter,"STATS_FREQ_FILE"); break; case STATS_LEAP_FILE: strcpy(parameter,"STATS_LEAP_FILE"); break; case STATS_STATSDIR: strcpy(parameter,"STATS_STATSDIR"); break; case STATS_PID_FILE: strcpy(parameter,"STATS_PID_FILE"); break; default: strcpy(parameter,"UNKNOWN"); break; } value = invalue; msyslog(LOG_ERR, "ExpandEnvironmentStrings(%s) failed: %m\n", parameter); } else { value = newvalue; } #else value = invalue; #endif /* SYS_WINNT */ switch(item) { /* * Open and read frequency file. */ case STATS_FREQ_FILE: if (!value || (len = strlen(value)) == 0) break; stats_drift_file = erealloc(stats_drift_file, len + 1); stats_temp_file = erealloc(stats_temp_file, len + sizeof(".TEMP")); memcpy(stats_drift_file, value, (unsigned)(len+1)); memcpy(stats_temp_file, value, (unsigned)len); memcpy(stats_temp_file + len, temp_ext, sizeof(temp_ext)); /* * Open drift file and read frequency. If the file is * missing or contains errors, tell the loop to reset. */ if ((fp = fopen(stats_drift_file, "r")) == NULL) break; if (fscanf(fp, "%lf", &old_drift) != 1) { msyslog(LOG_ERR, "format error frequency file %s", stats_drift_file); fclose(fp); break; } fclose(fp); old_drift /= 1e6; prev_drift_comp = old_drift; break; /* * Specify statistics directory. */ case STATS_STATSDIR: /* * HMS: the following test is insufficient: * - value may be missing the DIR_SEP * - we still need the filename after it */ if (strlen(value) >= sizeof(statsdir)) { msyslog(LOG_ERR, "statsdir too long (>%d, sigh)", (int)sizeof(statsdir) - 1); } else { l_fp now; int add_dir_sep; int value_l = strlen(value); /* Add a DIR_SEP unless we already have one. */ if (value_l == 0) add_dir_sep = 0; else add_dir_sep = (DIR_SEP != value[value_l - 1]); if (add_dir_sep) snprintf(statsdir, sizeof(statsdir), "%s%c", value, DIR_SEP); else snprintf(statsdir, sizeof(statsdir), "%s", value); get_systime(&now); if(peerstats.prefix == &statsdir[0] && peerstats.fp != NULL) { fclose(peerstats.fp); peerstats.fp = NULL; filegen_setup(&peerstats, now.l_ui); } if(loopstats.prefix == &statsdir[0] && loopstats.fp != NULL) { fclose(loopstats.fp); loopstats.fp = NULL; filegen_setup(&loopstats, now.l_ui); } if(clockstats.prefix == &statsdir[0] && clockstats.fp != NULL) { fclose(clockstats.fp); clockstats.fp = NULL; filegen_setup(&clockstats, now.l_ui); } if(rawstats.prefix == &statsdir[0] && rawstats.fp != NULL) { fclose(rawstats.fp); rawstats.fp = NULL; filegen_setup(&rawstats, now.l_ui); } if(sysstats.prefix == &statsdir[0] && sysstats.fp != NULL) { fclose(sysstats.fp); sysstats.fp = NULL; filegen_setup(&sysstats, now.l_ui); } if(protostats.prefix == &statsdir[0] && protostats.fp != NULL) { fclose(protostats.fp); protostats.fp = NULL; filegen_setup(&protostats, now.l_ui); } #ifdef OPENSSL if(cryptostats.prefix == &statsdir[0] && cryptostats.fp != NULL) { fclose(cryptostats.fp); cryptostats.fp = NULL; filegen_setup(&cryptostats, now.l_ui); } #endif /* OPENSSL */ #ifdef DEBUG_TIMING if(timingstats.prefix == &statsdir[0] && timingstats.fp != NULL) { fclose(timingstats.fp); timingstats.fp = NULL; filegen_setup(&timingstats, now.l_ui); } #endif /* DEBUG_TIMING */ } break; /* * Open pid file. */ case STATS_PID_FILE: if ((fp = fopen(value, "w")) == NULL) { msyslog(LOG_ERR, "pid file %s: %m", value); break; } fprintf(fp, "%d", (int)getpid()); fclose(fp);; break; /* * Read leapseconds file. */ case STATS_LEAP_FILE: if ((fp = fopen(value, "r")) == NULL) { msyslog(LOG_ERR, "leapseconds file %s: %m", value); break; } if (leap_file(fp) < 0) { msyslog(LOG_ERR, "format error leapseconds file %s", value); } else { strcpy(str1, fstostr(leap_sec)); strcpy(str2, fstostr(leap_expire)); snprintf(tbuf, sizeof(tbuf), "%d leap %s expire %s", leap_tai, str1, str2); report_event(EVNT_TAI, NULL, tbuf); } fclose(fp); break; default: /* oh well */ break; } }