The loop in ntp_main() calls getmonotime() and hence clock_gettime()
more than it needs to.  Since we only have second granularity there's
no point in checking the time repeatedly.

We could reduce the calls further by setting the value of "now"
outside some of the TAILQ_FOREACH peer loop but since querying the
peer could, in theory, take more than a second I haven't done that.

 - todd

Index: ntp.c
===================================================================
RCS file: /cvs/src/usr.sbin/ntpd/ntp.c,v
retrieving revision 1.135
diff -u -p -u -r1.135 ntp.c
--- ntp.c       14 Aug 2015 02:00:18 -0000      1.135
+++ ntp.c       14 Aug 2015 21:40:30 -0000
@@ -269,13 +269,14 @@ ntp_main(int pipe_prnt[2], int fd_ctl, s
                        if (constraint_cnt && conf->constraint_median == 0)
                                continue;
 
-                       if (p->next > 0 && p->next <= getmonotime()) {
+                       now = getmonotime();
+                       if (p->next > 0 && p->next <= now) {
                                if (p->state > STATE_DNS_INPROGRESS)
                                        trial_cnt++;
                                if (client_query(p) == 0)
                                        sent_cnt++;
                        }
-                       if (p->deadline > 0 && p->deadline <= getmonotime()) {
+                       if (p->deadline > 0 && p->deadline <= now) {
                                timeout = 300;
                                log_debug("no reply from %s received in time, "
                                    "next query %ds %s", log_sockaddr(
@@ -314,10 +315,11 @@ ntp_main(int pipe_prnt[2], int fd_ctl, s
                idx_clients = i;
 
                if (!TAILQ_EMPTY(&conf->ntp_conf_sensors)) {
+                       now = getmonotime();
                        if (last_sensor_scan == 0 ||
-                           last_sensor_scan + SENSOR_SCAN_INTERVAL <= 
getmonotime()) {
+                           last_sensor_scan + SENSOR_SCAN_INTERVAL <= now) {
                                sensors_cnt = sensor_scan();
-                               last_sensor_scan = getmonotime();
+                               last_sensor_scan = now;
                        }
                        if (sensors_cnt == 0 &&
                            nextaction > last_sensor_scan + 
SENSOR_SCAN_INTERVAL)

Reply via email to