]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
REORG: time: move time-keeping code and variables to clock.c
authorWilly Tarreau <w@1wt.eu>
Fri, 8 Oct 2021 07:33:24 +0000 (09:33 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 8 Oct 2021 15:22:26 +0000 (17:22 +0200)
There is currently a problem related to time keeping. We're mixing
the functions to perform calculations with the os-dependent code
needed to retrieve and adjust the local time.

This patch extracts from time.{c,h} the parts that are solely dedicated
to time keeping. These are the "now" or "before_poll" variables for
example, as well as the various now_*() functions that make use of
gettimeofday() and clock_gettime() to retrieve the current time.

The "tv_*" functions moved there were also more appropriately renamed
to "clock_*".

Other parts used to compute stolen time are in other files, they will
have to be picked next.

24 files changed:
Makefile
addons/ot/include/include.h
include/haproxy/backend.h
include/haproxy/clock.h [new file with mode: 0644]
include/haproxy/task.h
include/haproxy/time.h
src/activity.c
src/cfgparse.c
src/clock.c [new file with mode: 0644]
src/debug.c
src/ev_epoll.c
src/ev_evports.c
src/ev_kqueue.c
src/ev_poll.c
src/ev_select.c
src/haproxy.c
src/hlua.c
src/log.c
src/sample.c
src/stats.c
src/task.c
src/thread.c
src/time.c
src/wdt.c

index 998a17dc3bd9bbcc5fca58659935ddf03534e9f4..9401237815fa60140f7d88c43eb325a1bdbd6b76 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -886,7 +886,7 @@ OBJS += src/mux_h2.o src/mux_fcgi.o src/http_ana.o src/mux_h1.o src/stream.o   \
         src/time.o src/signal.o src/mworker-prog.o src/hpack-dec.o src/fix.o   \
         src/arg.o src/eb64tree.o src/chunk.o src/shctx.o src/regex.o           \
         src/fcgi.o src/eb32tree.o src/eb32sctree.o src/dynbuf.o src/uri_auth.o \
-        src/hpack-tbl.o src/ebimtree.o src/auth.o src/ebsttree.o               \
+        src/hpack-tbl.o src/ebimtree.o src/auth.o src/ebsttree.o src/clock.o   \
         src/ebistree.o src/base64.o src/wdt.o src/pipe.o src/http_acl.o        \
         src/hpack-enc.o src/dict.o src/dgram.o src/init.o src/hpack-huff.o     \
         src/freq_ctr.o src/ebtree.o src/hash.o src/version.o src/errors.o      \
index f185a53b213ff61d19ba710289952eab4efef22f..f1a567240a5e9616e7d10f13cd3b3a9336f0bea3 100644 (file)
@@ -27,6 +27,7 @@
 #include <haproxy/cfgparse.h>
 #include <haproxy/acl.h>
 #include <haproxy/cli.h>
+#include <haproxy/clock.h>
 #include <haproxy/filters.h>
 #include <haproxy/http_htx.h>
 #include <haproxy/http_rules.h>
index 8a763bfbdc4b29a4d4bc7ba2daeca5ac324128b8..17dffac8a9907103ed7eb234249b0ce6995d0367 100644 (file)
 
 #include <haproxy/api.h>
 #include <haproxy/backend-t.h>
+#include <haproxy/clock.h>
 #include <haproxy/proxy-t.h>
 #include <haproxy/server-t.h>
 #include <haproxy/stream-t.h>
-#include <haproxy/time.h>
 
 int assign_server(struct stream *s);
 int assign_server_address(struct stream *s);
diff --git a/include/haproxy/clock.h b/include/haproxy/clock.h
new file mode 100644 (file)
index 0000000..045ab5a
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * include/haproxy/clock.h
+ * Exported parts for time-keeping
+ *
+ * Copyright (C) 2000-2021 Willy Tarreau - w@1wt.eu
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, version 2.1
+ * exclusively.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef _HAPROXY_CLOCK_H
+#define _HAPROXY_CLOCK_H
+
+#include <sys/time.h>
+#include <haproxy/api.h>
+#include <haproxy/tinfo-t.h>
+
+extern struct timeval              start_date;    /* the process's start date in wall-clock time */
+extern volatile ullong             global_now;    /* common monotonic date between all threads (32:32) */
+
+extern THREAD_LOCAL struct timeval now;           /* internal monotonic date derived from real clock */
+extern THREAD_LOCAL struct timeval date;          /* the real current date (wall-clock time) */
+extern THREAD_LOCAL struct timeval before_poll;   /* system date before calling poll() */
+extern THREAD_LOCAL struct timeval after_poll;    /* system date after leaving poll() */
+
+uint64_t now_cpu_time_thread(const struct thread_info *thr);
+uint64_t now_mono_time(void);
+uint64_t now_cpu_time(void);
+void clock_update_date(int max_wait, int interrupted);
+void clock_init_process_date(void);
+void clock_init_thread_date(void);
+char *timeofday_as_iso_us(int pad);
+
+#endif
index e2c8a9909e323894378bc8eab71df0f1d011e545..4acbcad9647915cecd362ae604ac86cd0e524593 100644 (file)
@@ -30,6 +30,7 @@
 
 #include <haproxy/activity.h>
 #include <haproxy/api.h>
+#include <haproxy/clock.h>
 #include <haproxy/fd.h>
 #include <haproxy/global.h>
 #include <haproxy/intops.h>
@@ -38,7 +39,6 @@
 #include <haproxy/task-t.h>
 #include <haproxy/thread.h>
 #include <haproxy/ticks.h>
-#include <haproxy/time.h>
 
 
 /* Principle of the wait queue.
index d1f181c7b9377dbe4e9ed2c4133afa2f13019a49..97b4ee256b9b756873a0bdbf657a940d1fe49a4b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * include/haproxy/time.h
- * Time calculation functions and macros.
+ * timeval-based time calculation functions and macros.
  *
  * Copyright (C) 2000-2020 Willy Tarreau - w@1wt.eu
  *
 #define _HAPROXY_TIME_H
 
 #include <sys/time.h>
-#include <time.h>
 #include <haproxy/api.h>
-#include <haproxy/thread.h>
 
 #define TIME_ETERNITY   (TV_ETERNITY_MS)
 
-/* returns the lowest delay amongst <old> and <new>, and respects TIME_ETERNITY */
-#define MINTIME(old, new)      (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old)))
-#define SETNOW(a)              (*a=now)
-
-extern THREAD_LOCAL struct timeval now;              /* internal date is a monotonic function of real clock */
-extern THREAD_LOCAL struct timeval date;             /* the real current date */
-extern struct timeval start_date;       /* the process's start date */
-extern THREAD_LOCAL struct timeval before_poll;      /* system date before calling poll() */
-extern THREAD_LOCAL struct timeval after_poll;       /* system date after leaving poll() */
-extern volatile unsigned long long global_now;
 
 
 /**** exported functions *************************************************/
@@ -59,30 +47,10 @@ int tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2);
  */
 int tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2);
 
-/* tv_udpate_date: sets <date> to system time, and sets <now> to something as
- * close as possible to real time, following a monotonic function. The main
- * principle consists in detecting backwards and forwards time jumps and adjust
- * an offset to correct them. This function should be called only once after
- * each poll. The poll's timeout should be passed in <max_wait>, and the return
- * value in <interrupted> (a non-zero value means that we have not expired the
- * timeout).
- */
-void tv_update_date(int max_wait, int interrupted);
-void tv_init_process_date(void);
-void tv_init_thread_date(void);
-
-char *timeofday_as_iso_us(int pad);
 
 /**** general purpose functions and macros *******************************/
 
 
-/* tv_now: sets <tv> to the current time */
-static inline struct timeval *tv_now(struct timeval *tv)
-{
-       gettimeofday(tv, NULL);
-       return tv;
-}
-
 /*
  * sets a struct timeval to its highest value so that it can never happen
  * note that only tv_usec is necessary to detect it since a tv_usec > 999999
@@ -496,42 +464,6 @@ static inline struct timeval *__tv_ms_add(struct timeval *tv, const struct timev
         tv1;                       \
 })
 
-/* returns the system's monotonic time in nanoseconds if supported, otherwise zero */
-static inline uint64_t now_mono_time()
-{
-#if (_POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK)
-       struct timespec ts;
-       clock_gettime(CLOCK_MONOTONIC, &ts);
-       return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
-#else
-       return 0;
-#endif
-}
-
-/* returns the current thread's cumulated CPU time in nanoseconds if supported, otherwise zero */
-static inline uint64_t now_cpu_time()
-{
-#if (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
-       struct timespec ts;
-       clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
-       return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
-#else
-       return 0;
-#endif
-}
-
-/* returns another thread's cumulated CPU time in nanoseconds if supported, otherwise zero */
-static inline uint64_t now_cpu_time_thread(const struct thread_info *thr)
-{
-#if (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
-       struct timespec ts;
-       clock_gettime(thr->clock_id, &ts);
-       return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
-#else
-       return 0;
-#endif
-}
-
 #endif /* _HAPROXY_TIME_H */
 
 /*
index c3010c5f3a5bcde472572ec788664839f3ff2fdd..a2d31898c278ab53dbed4c9c373f85b1c24471d1 100644 (file)
 #include <haproxy/activity-t.h>
 #include <haproxy/api.h>
 #include <haproxy/cfgparse.h>
+#include <haproxy/clock.h>
 #include <haproxy/channel.h>
 #include <haproxy/cli.h>
 #include <haproxy/freq_ctr.h>
 #include <haproxy/stream_interface.h>
-#include <haproxy/time.h>
 #include <haproxy/tools.h>
 #include <haproxy/xxhash.h>
 
index 2ef62afbe1a835e02774c106401a314cc725ccdf..7aeffaaf4f656dfcae2f10d1371de1ee6a6b2061 100644 (file)
@@ -46,6 +46,7 @@
 #include <haproxy/channel.h>
 #include <haproxy/check.h>
 #include <haproxy/chunk.h>
+#include <haproxy/clock.h>
 #ifdef USE_CPU_AFFINITY
 #include <haproxy/cpuset.h>
 #endif
@@ -82,7 +83,6 @@
 #include <haproxy/tcp_rules.h>
 #include <haproxy/tcpcheck.h>
 #include <haproxy/thread.h>
-#include <haproxy/time.h>
 #include <haproxy/tools.h>
 #include <haproxy/uri_auth-t.h>
 #include <haproxy/xprt_quic.h>
@@ -2403,7 +2403,7 @@ int check_config_validity()
         */
 
        /* will be needed further to delay some tasks */
-       tv_update_date(0,1);
+       clock_update_date(0,1);
 
        if (!global.tune.max_http_hdr)
                global.tune.max_http_hdr = MAX_HTTP_HDR;
diff --git a/src/clock.c b/src/clock.c
new file mode 100644 (file)
index 0000000..ecaeca9
--- /dev/null
@@ -0,0 +1,253 @@
+/*
+ * General time-keeping code and variables
+ *
+ * Copyright 2000-2021 Willy Tarreau <w@1wt.eu>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ */
+
+#include <sys/time.h>
+#include <time.h>
+
+#include <haproxy/api.h>
+#include <haproxy/clock.h>
+#include <haproxy/time.h>
+#include <haproxy/tinfo-t.h>
+#include <haproxy/tools.h>
+
+struct timeval                   start_date;      /* the process's start date in wall-clock time */
+volatile ullong                  global_now;      /* common monotonic date between all threads (32:32) */
+volatile uint                    global_now_ms;   /* common monotonic date in milliseconds (may wrap) */
+
+THREAD_ALIGNED(64) static ullong now_offset;      /* global offset between system time and global time */
+
+THREAD_LOCAL uint                now_ms;          /* internal monotonic date in milliseconds (may wrap) */
+THREAD_LOCAL struct timeval      now;             /* internal monotonic date derived from real clock */
+THREAD_LOCAL struct timeval      date;            /* the real current date (wall-clock time) */
+THREAD_LOCAL struct timeval      before_poll;     /* system date before calling poll() */
+THREAD_LOCAL struct timeval      after_poll;      /* system date after leaving poll() */
+
+static THREAD_LOCAL unsigned int iso_time_sec;     /* last iso time value for this thread */
+static THREAD_LOCAL char         iso_time_str[34]; /* ISO time representation of gettimeofday() */
+
+/* returns the system's monotonic time in nanoseconds if supported, otherwise zero */
+uint64_t now_mono_time(void)
+{
+       uint64_t ret = 0;
+#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK)
+       struct timespec ts;
+       clock_gettime(CLOCK_MONOTONIC, &ts);
+       ret = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
+#endif
+       return ret;
+}
+
+/* returns the current thread's cumulated CPU time in nanoseconds if supported, otherwise zero */
+uint64_t now_cpu_time(void)
+{
+       uint64_t ret = 0;
+#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
+       struct timespec ts;
+       clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
+       ret = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
+#endif
+       return ret;
+}
+
+/* returns another thread's cumulated CPU time in nanoseconds if supported, otherwise zero */
+uint64_t now_cpu_time_thread(const struct thread_info *thr)
+{
+       uint64_t ret = 0;
+#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
+       struct timespec ts;
+       clock_gettime(thr->clock_id, &ts);
+       ret = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
+#endif
+       return ret;
+}
+
+/* clock_update_date: sets <date> to system time, and sets <now> to something as
+ * close as possible to real time, following a monotonic function. The main
+ * principle consists in detecting backwards and forwards time jumps and adjust
+ * an offset to correct them. This function should be called once after each
+ * poll, and never farther apart than MAX_DELAY_MS*2. The poll's timeout should
+ * be passed in <max_wait>, and the return value in <interrupted> (a non-zero
+ * value means that we have not expired the timeout).
+ *
+ * clock_init_process_date() must have been called once first, and
+ * clock_init_thread_date() must also have been called once for each thread.
+ *
+ * An offset is used to adjust the current time (date), to figure a monotonic
+ * local time (now). The offset is not critical, as it is only updated after a
+ * clock jump is detected. From this point all threads will apply it to their
+ * locally measured time, and will then agree around a common monotonic
+ * global_now value that serves to further refine their local time. As it is
+ * not possible to atomically update a timeval, both global_now and the
+ * now_offset values are instead stored as 64-bit integers made of two 32 bit
+ * values for the tv_sec and tv_usec parts. The offset is made of two signed
+ * ints so that the clock can be adjusted in the two directions.
+ */
+void clock_update_date(int max_wait, int interrupted)
+{
+       struct timeval min_deadline, max_deadline, tmp_now;
+       uint old_now_ms;
+       ullong old_now;
+       ullong new_now;
+       ullong ofs, ofs_new;
+       uint sec_ofs, usec_ofs;
+
+       gettimeofday(&date, NULL);
+
+       /* compute the minimum and maximum local date we may have reached based
+        * on our past date and the associated timeout. There are three possible
+        * extremities:
+        *    - the new date cannot be older than before_poll
+        *    - if not interrupted, the new date cannot be older than
+        *      before_poll+max_wait
+        *    - in any case the new date cannot be newer than
+        *      before_poll+max_wait+some margin (100ms used here).
+        * In case of violation, we'll ignore the current date and instead
+        * restart from the last date we knew.
+        */
+       _tv_ms_add(&min_deadline, &before_poll, max_wait);
+       _tv_ms_add(&max_deadline, &before_poll, max_wait + 100);
+
+       ofs = HA_ATOMIC_LOAD(&now_offset);
+
+       if (unlikely(__tv_islt(&date, &before_poll)                    || // big jump backwards
+                    (!interrupted && __tv_islt(&date, &min_deadline)) || // small jump backwards
+                    __tv_islt(&max_deadline, &date))) {                  // big jump forwards
+               if (!interrupted)
+                       _tv_ms_add(&now, &now, max_wait);
+       } else {
+               /* The date is still within expectations. Let's apply the
+                * now_offset to the system date. Note: ofs if made of two
+                * independent signed ints.
+                */
+               now.tv_sec  = date.tv_sec  + (int)(ofs >> 32); // note: may be positive or negative
+               now.tv_usec = date.tv_usec + (int)ofs;         // note: may be positive or negative
+               if ((int)now.tv_usec < 0) {
+                       now.tv_usec += 1000000;
+                       now.tv_sec  -= 1;
+               } else if (now.tv_usec >= 1000000) {
+                       now.tv_usec -= 1000000;
+                       now.tv_sec  += 1;
+               }
+       }
+
+       /* now that we have bounded the local time, let's check if it's
+        * realistic regarding the global date, which only moves forward,
+        * otherwise catch up.
+        */
+       old_now    = global_now;
+       old_now_ms = global_now_ms;
+
+       do {
+               tmp_now.tv_sec  = (unsigned int)(old_now >> 32);
+               tmp_now.tv_usec = old_now & 0xFFFFFFFFU;
+
+               if (__tv_islt(&now, &tmp_now))
+                       now = tmp_now;
+
+               /* now <now> is expected to be the most accurate date,
+                * equal to <global_now> or newer.
+                */
+               new_now = ((ullong)now.tv_sec << 32) + (uint)now.tv_usec;
+               now_ms = __tv_to_ms(&now);
+
+               /* let's try to update the global <now> (both in timeval
+                * and ms forms) or loop again.
+                */
+       } while (((new_now != old_now    && !_HA_ATOMIC_CAS(&global_now, &old_now, new_now)) ||
+                 (now_ms  != old_now_ms && !_HA_ATOMIC_CAS(&global_now_ms, &old_now_ms, now_ms))) &&
+                __ha_cpu_relax());
+
+       /* <now> and <now_ms> are now updated to the last value of global_now
+        * and global_now_ms, which were also monotonically updated. We can
+        * compute the latest offset, we don't care who writes it last, the
+        * variations will not break the monotonic property.
+        */
+
+       sec_ofs  = now.tv_sec  - date.tv_sec;
+       usec_ofs = now.tv_usec - date.tv_usec;
+       if ((int)usec_ofs < 0) {
+               usec_ofs += 1000000;
+               sec_ofs  -= 1;
+       }
+       ofs_new = ((ullong)sec_ofs << 32) + usec_ofs;
+       if (ofs_new != ofs)
+               HA_ATOMIC_STORE(&now_offset, ofs_new);
+}
+
+/* must be called once at boot to initialize some global variables */
+void clock_init_process_date(void)
+{
+       now_offset = 0;
+       gettimeofday(&date, NULL);
+       now = after_poll = before_poll = date;
+       global_now = ((ullong)date.tv_sec << 32) + (uint)date.tv_usec;
+       global_now_ms = now.tv_sec * 1000 + now.tv_usec / 1000;
+       ti->idle_pct = 100;
+       clock_update_date(0, 1);
+}
+
+/* must be called once per thread to initialize their thread-local variables.
+ * Note that other threads might also be initializing and running in parallel.
+ */
+void clock_init_thread_date(void)
+{
+       ullong old_now;
+
+       gettimeofday(&date, NULL);
+       after_poll = before_poll = date;
+
+       old_now = _HA_ATOMIC_LOAD(&global_now);
+       now.tv_sec = old_now >> 32;
+       now.tv_usec = (uint)old_now;
+       ti->idle_pct = 100;
+       clock_update_date(0, 1);
+}
+
+/* returns the current date as returned by gettimeofday() in ISO+microsecond
+ * format. It uses a thread-local static variable that the reader can consume
+ * for as long as it wants until next call. Thus, do not call it from a signal
+ * handler. If <pad> is non-0, a trailing space will be added. It will always
+ * return exactly 32 or 33 characters (depending on padding) and will always be
+ * zero-terminated, thus it will always fit into a 34 bytes buffer.
+ * This also always include the local timezone (in +/-HH:mm format) .
+ */
+char *timeofday_as_iso_us(int pad)
+{
+       struct timeval new_date;
+       struct tm tm;
+       const char *offset;
+       char c;
+
+       gettimeofday(&new_date, NULL);
+       if (new_date.tv_sec != iso_time_sec || !new_date.tv_sec) {
+               get_localtime(new_date.tv_sec, &tm);
+               offset = get_gmt_offset(new_date.tv_sec, &tm);
+               if (unlikely(strftime(iso_time_str, sizeof(iso_time_str), "%Y-%m-%dT%H:%M:%S.000000+00:00", &tm) != 32))
+                       strcpy(iso_time_str, "YYYY-mm-ddTHH:MM:SS.000000-00:00"); // make the failure visible but respect format.
+               iso_time_str[26] = offset[0];
+               iso_time_str[27] = offset[1];
+               iso_time_str[28] = offset[2];
+               iso_time_str[30] = offset[3];
+               iso_time_str[31] = offset[4];
+               iso_time_sec = new_date.tv_sec;
+       }
+
+       /* utoa_pad adds a trailing 0 so we save the char for restore */
+       c = iso_time_str[26];
+       utoa_pad(new_date.tv_usec, iso_time_str + 20, 7);
+       iso_time_str[26] = c;
+       if (pad) {
+               iso_time_str[32] = ' ';
+               iso_time_str[33] = 0;
+       }
+       return iso_time_str;
+}
index 303e28269fba12462a86b08c76ec753075d81aac..c5b78edc7bd3cf02ea41b9b879e9f27efd27fb83 100644 (file)
@@ -23,6 +23,7 @@
 #include <haproxy/api.h>
 #include <haproxy/buf.h>
 #include <haproxy/cli.h>
+#include <haproxy/clock.h>
 #include <haproxy/debug.h>
 #include <haproxy/fd.h>
 #include <haproxy/global.h>
@@ -33,6 +34,7 @@
 #include <haproxy/stream_interface.h>
 #include <haproxy/task.h>
 #include <haproxy/thread.h>
+#include <haproxy/time.h>
 #include <haproxy/tools.h>
 #include <import/ist.h>
 
index d376cd994a4de5943df9e67d3dd29a53b6052df4..03888d0a391e42ddcf73c972453e3867dabc8292 100644 (file)
 
 #include <haproxy/activity.h>
 #include <haproxy/api.h>
+#include <haproxy/clock.h>
 #include <haproxy/fd.h>
 #include <haproxy/global.h>
 #include <haproxy/signal.h>
 #include <haproxy/ticks.h>
 #include <haproxy/task.h>
-#include <haproxy/time.h>
 #include <haproxy/tools.h>
 
 
@@ -195,7 +195,7 @@ static void _do_poll(struct poller *p, int exp, int wake)
                int timeout = (global.tune.options & GTUNE_BUSY_POLLING) ? 0 : wait_time;
 
                status = epoll_wait(epoll_fd[tid], epoll_events, global.tune.maxpollevents, timeout);
-               tv_update_date(timeout, status);
+               clock_update_date(timeout, status);
 
                if (status) {
                        activity[tid].poll_io++;
index f8c7f34720a0e59e67d7302a8ce043997f4b62c5..23bf11ffa08a3c2e128c8a60ac1eb506eb534eb5 100644 (file)
 
 #include <haproxy/activity.h>
 #include <haproxy/api.h>
+#include <haproxy/clock.h>
 #include <haproxy/fd.h>
 #include <haproxy/global.h>
 #include <haproxy/signal.h>
 #include <haproxy/task.h>
 #include <haproxy/ticks.h>
-#include <haproxy/time.h>
 
 /*
  * Private data:
@@ -191,7 +191,7 @@ static void _do_poll(struct poller *p, int exp, int wake)
                                break;
                        }
                }
-               tv_update_date(timeout, nevlist);
+               clock_update_date(timeout, nevlist);
 
                if (nevlist || interrupted)
                        break;
index 3a53e07d67e11a03e4a6e28888ce5645e258fc84..df8eae263fc49ac16ecba175d2ee5fc156b19a3b 100644 (file)
 
 #include <haproxy/activity.h>
 #include <haproxy/api.h>
+#include <haproxy/clock.h>
 #include <haproxy/fd.h>
 #include <haproxy/global.h>
 #include <haproxy/signal.h>
 #include <haproxy/task.h>
 #include <haproxy/ticks.h>
-#include <haproxy/time.h>
 
 
 /* private data */
@@ -161,7 +161,7 @@ static void _do_poll(struct poller *p, int exp, int wake)
                                kev,       // struct kevent *eventlist
                                fd,        // int nevents
                                &timeout_ts); // const struct timespec *timeout
-               tv_update_date(timeout, status);
+               clock_update_date(timeout, status);
 
                if (status) {
                        activity[tid].poll_io++;
index b08bfcac2d49a5ca21d14a8220e4782ce3be78ca..9b93c92e5d3ad40d9fadab57caae163851d7604a 100644 (file)
 
 #include <haproxy/activity.h>
 #include <haproxy/api.h>
+#include <haproxy/clock.h>
 #include <haproxy/fd.h>
 #include <haproxy/global.h>
 #include <haproxy/task.h>
 #include <haproxy/ticks.h>
-#include <haproxy/time.h>
 
 
 #ifndef POLLRDHUP
@@ -205,7 +205,7 @@ static void _do_poll(struct poller *p, int exp, int wake)
        sched_entering_poll();
        activity_count_runtime();
        status = poll(poll_events, nbfd, wait_time);
-       tv_update_date(wait_time, status);
+       clock_update_date(wait_time, status);
        sched_leaving_poll(wait_time, status);
 
        thread_harmless_end();
index 12c7341dd4bf0f9bf7087e3050cad71f826a81ad..47da75dacf518e1ecb5b43f50741b26f8f87e2e9 100644 (file)
 
 #include <haproxy/activity.h>
 #include <haproxy/api.h>
+#include <haproxy/clock.h>
 #include <haproxy/fd.h>
 #include <haproxy/global.h>
 #include <haproxy/task.h>
 #include <haproxy/ticks.h>
-#include <haproxy/time.h>
 
 
 /* private data */
@@ -180,7 +180,7 @@ static void _do_poll(struct poller *p, int exp, int wake)
                        writenotnull ? tmp_evts[DIR_WR] : NULL,
                        NULL,
                        &delta);
-       tv_update_date(delta_ms, status);
+       clock_update_date(delta_ms, status);
        sched_leaving_poll(delta_ms, status);
 
        thread_harmless_end();
index 1ab2bf618a159ff7d227c4ba444601a026b32143..618efb362f1295ef0a1a89f07d320f39c72c291d 100644 (file)
@@ -95,6 +95,7 @@
 #include <haproxy/cfgparse.h>
 #include <haproxy/chunk.h>
 #include <haproxy/cli.h>
+#include <haproxy/clock.h>
 #include <haproxy/connection.h>
 #ifdef USE_CPU_AFFINITY
 #include <haproxy/cpuset.h>
@@ -1506,7 +1507,7 @@ static void init(int argc, char **argv)
 #endif
 
        tzset();
-       tv_init_process_date();
+       clock_init_process_date();
        start_date = now;
 
        ha_random_boot(argv);
@@ -2615,7 +2616,7 @@ void run_poll_loop()
 {
        int next, wake;
 
-       tv_update_date(0,1);
+       clock_update_date(0,1);
        while (1) {
                wake_expired_tasks();
 
@@ -2720,7 +2721,7 @@ static void *run_thread_poll_loop(void *data)
                init_left = global.nbthread;
        init_left--;
 
-       tv_init_thread_date();
+       clock_init_thread_date();
 
        /* per-thread alloc calls performed here are not allowed to snoop on
         * other threads, so they are free to initialize at their own rhythm
index 7e759548cf71ad0c262ecfd1b802bc25e6a9f9c9..d5d3f599fa3b9abe8f8b99adc4d5f9a624b660b9 100644 (file)
@@ -32,6 +32,7 @@
 #include <haproxy/cfgparse.h>
 #include <haproxy/channel.h>
 #include <haproxy/cli.h>
+#include <haproxy/clock.h>
 #include <haproxy/connection.h>
 #include <haproxy/filters.h>
 #include <haproxy/h1.h>
@@ -1325,7 +1326,7 @@ void hlua_hook(lua_State *L, lua_Debug *ar)
                MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
 
        /* If we cannot yield, update the clock and check the timeout. */
-       tv_update_date(0, 1);
+       clock_update_date(0, 1);
        hlua->run_time += now_ms - hlua->start_time;
        if (hlua->max_time && hlua->run_time >= hlua->max_time) {
                lua_pushfstring(L, "execution timeout");
@@ -1411,7 +1412,7 @@ resume_execution:
                /* Check if the execution timeout is expired. It it is the case, we
                 * break the Lua execution.
                 */
-               tv_update_date(0, 1);
+               clock_update_date(0, 1);
                lua->run_time += now_ms - lua->start_time;
                if (lua->max_time && lua->run_time > lua->max_time) {
                        lua_settop(lua->T, 0); /* Empty the stack. */
index 0e880c9d60aa3498b2051c54dc4f1fdb12029774..d15377d747df9d903e76dae8bfe3422788b18180 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -27,6 +27,7 @@
 #include <haproxy/api.h>
 #include <haproxy/applet-t.h>
 #include <haproxy/cfgparse.h>
+#include <haproxy/clock.h>
 #include <haproxy/fd.h>
 #include <haproxy/frontend.h>
 #include <haproxy/global.h>
index bda590875c457c029ecf98f57c46dc180587ea6a..970dabb5bb9bf8993c4a25802db44971e36f8d62 100644 (file)
@@ -25,6 +25,7 @@
 #include <haproxy/base64.h>
 #include <haproxy/buf.h>
 #include <haproxy/chunk.h>
+#include <haproxy/clock.h>
 #include <haproxy/errors.h>
 #include <haproxy/fix.h>
 #include <haproxy/global.h>
@@ -39,7 +40,6 @@
 #include <haproxy/sample.h>
 #include <haproxy/sink.h>
 #include <haproxy/stick_table.h>
-#include <haproxy/time.h>
 #include <haproxy/tools.h>
 #include <haproxy/uri_auth-t.h>
 #include <haproxy/vars.h>
index 4c8ea0c8d5dc84afa55a63f5729d0dbaaa0015ed..da6fc363a2dc65ad54dcc5127cf24cd9bc145fe7 100644 (file)
@@ -33,6 +33,7 @@
 #include <haproxy/channel.h>
 #include <haproxy/check.h>
 #include <haproxy/cli.h>
+#include <haproxy/clock.h>
 #include <haproxy/compression.h>
 #include <haproxy/debug.h>
 #include <haproxy/errors.h>
index 27a4ca745d26bb37788bd5cb59812f1a43a1913a..d5473596551d499ee6e307377ed005e3123ff3c1 100644 (file)
 #include <haproxy/api.h>
 #include <haproxy/activity.h>
 #include <haproxy/cfgparse.h>
+#include <haproxy/clock.h>
 #include <haproxy/fd.h>
 #include <haproxy/list.h>
 #include <haproxy/pool.h>
 #include <haproxy/task.h>
-#include <haproxy/time.h>
 #include <haproxy/tools.h>
 
 extern struct task *process_stream(struct task *t, void *context, unsigned int state);
@@ -882,7 +882,7 @@ uint sched_report_idle()
 }
 
 /* Update the idle time value twice a second, to be called after
- * tv_update_date() when called after poll(), and currently called only by
+ * clock_update_date() when called after poll(), and currently called only by
  * sched_leaving_poll() below. It relies on <before_poll> to be updated to
  * the system time before calling poll().
  */
index a3400010bc3a52bff0798834b10f35ba490a128c..ad5327aed20677b82908fdefcbd2fb9a0ba08421 100644 (file)
 #endif
 
 #include <haproxy/cfgparse.h>
+#include <haproxy/clock.h>
 #include <haproxy/fd.h>
 #include <haproxy/global.h>
 #include <haproxy/log.h>
 #include <haproxy/thread.h>
-#include <haproxy/time.h>
 #include <haproxy/tools.h>
 
 struct thread_info ha_thread_info[MAX_THREADS] = { };
index 99aed11f843174cf3b39cd96689d18624fbc5db0..280b522b8287bc88ae99840beb95140e03e23869 100644 (file)
  *
  */
 
-#include <unistd.h>
 #include <sys/time.h>
 
 #include <haproxy/api.h>
 #include <haproxy/time.h>
-#include <haproxy/ticks.h>
-#include <haproxy/tools.h>
 
-THREAD_LOCAL unsigned int   now_ms;          /* internal date in milliseconds (may wrap) */
-THREAD_LOCAL struct timeval now;             /* internal date is a monotonic function of real clock */
-THREAD_LOCAL struct timeval date;            /* the real current date */
-struct timeval start_date;      /* the process's start date */
-THREAD_LOCAL struct timeval before_poll;     /* system date before calling poll() */
-THREAD_LOCAL struct timeval after_poll;      /* system date after leaving poll() */
-
-static unsigned long long now_offset;        /* global offset between system time and global time */
-volatile unsigned long long global_now;      /* common monotonic date between all threads (32:32) */
-volatile unsigned int global_now_ms;         /* common monotonic date in milliseconds (may wrap) */
-
-static THREAD_LOCAL unsigned int iso_time_sec;     /* last iso time value for this thread */
-static THREAD_LOCAL char         iso_time_str[34]; /* ISO time representation of gettimeofday() */
 
 /*
  * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
@@ -155,186 +139,6 @@ int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
        return __tv_isgt(tv1, tv2);
 }
 
-/* tv_update_date: sets <date> to system time, and sets <now> to something as
- * close as possible to real time, following a monotonic function. The main
- * principle consists in detecting backwards and forwards time jumps and adjust
- * an offset to correct them. This function should be called once after each
- * poll, and never farther apart than MAX_DELAY_MS*2. The poll's timeout should
- * be passed in <max_wait>, and the return value in <interrupted> (a non-zero
- * value means that we have not expired the timeout).
- *
- * tv_init_process_date() must have been called once first, and
- * tv_init_thread_date() must also have been called once for each thread.
- *
- * An offset is used to adjust the current time (date), to figure a monotonic
- * local time (now). The offset is not critical, as it is only updated after a
- * clock jump is detected. From this point all threads will apply it to their
- * locally measured time, and will then agree around a common monotonic
- * global_now value that serves to further refine their local time. As it is
- * not possible to atomically update a timeval, both global_now and the
- * now_offset values are instead stored as 64-bit integers made of two 32 bit
- * values for the tv_sec and tv_usec parts. The offset is made of two signed
- * ints so that the clock can be adjusted in the two directions.
- */
-void tv_update_date(int max_wait, int interrupted)
-{
-       struct timeval min_deadline, max_deadline, tmp_now;
-       unsigned int old_now_ms;
-       unsigned long long old_now;
-       unsigned long long new_now;
-       ullong ofs, ofs_new;
-       uint sec_ofs, usec_ofs;
-
-       gettimeofday(&date, NULL);
-
-       /* compute the minimum and maximum local date we may have reached based
-        * on our past date and the associated timeout. There are three possible
-        * extremities:
-        *    - the new date cannot be older than before_poll
-        *    - if not interrupted, the new date cannot be older than
-        *      before_poll+max_wait
-        *    - in any case the new date cannot be newer than
-        *      before_poll+max_wait+some margin (100ms used here).
-        * In case of violation, we'll ignore the current date and instead
-        * restart from the last date we knew.
-        */
-       _tv_ms_add(&min_deadline, &before_poll, max_wait);
-       _tv_ms_add(&max_deadline, &before_poll, max_wait + 100);
-
-       ofs = HA_ATOMIC_LOAD(&now_offset);
-
-       if (unlikely(__tv_islt(&date, &before_poll)                    || // big jump backwards
-                    (!interrupted && __tv_islt(&date, &min_deadline)) || // small jump backwards
-                    __tv_islt(&max_deadline, &date))) {                  // big jump forwards
-               if (!interrupted)
-                       _tv_ms_add(&now, &now, max_wait);
-       } else {
-               /* The date is still within expectations. Let's apply the
-                * now_offset to the system date. Note: ofs if made of two
-                * independent signed ints.
-                */
-               now.tv_sec  = date.tv_sec  + (int)(ofs >> 32); // note: may be positive or negative
-               now.tv_usec = date.tv_usec + (int)ofs;         // note: may be positive or negative
-               if ((int)now.tv_usec < 0) {
-                       now.tv_usec += 1000000;
-                       now.tv_sec  -= 1;
-               } else if (now.tv_usec >= 1000000) {
-                       now.tv_usec -= 1000000;
-                       now.tv_sec  += 1;
-               }
-       }
-
-       /* now that we have bounded the local time, let's check if it's
-        * realistic regarding the global date, which only moves forward,
-        * otherwise catch up.
-        */
-       old_now = global_now;
-       old_now_ms = global_now_ms;
-
-       do {
-               tmp_now.tv_sec  = (unsigned int)(old_now >> 32);
-               tmp_now.tv_usec = old_now & 0xFFFFFFFFU;
-
-               if (__tv_islt(&now, &tmp_now))
-                       now = tmp_now;
-
-               /* now <now> is expected to be the most accurate date,
-                * equal to <global_now> or newer.
-                */
-               new_now = ((ullong)now.tv_sec << 32) + (uint)now.tv_usec;
-               now_ms = __tv_to_ms(&now);
-
-               /* let's try to update the global <now> (both in timeval
-                * and ms forms) or loop again.
-                */
-       } while (((new_now != old_now    && !_HA_ATOMIC_CAS(&global_now, &old_now, new_now)) ||
-                 (now_ms  != old_now_ms && !_HA_ATOMIC_CAS(&global_now_ms, &old_now_ms, now_ms))) &&
-                __ha_cpu_relax());
-
-       /* <now> and <now_ms> are now updated to the last value of global_now
-        * and global_now_ms, which were also monotonically updated. We can
-        * compute the latest offset, we don't care who writes it last, the
-        * variations will not break the monotonic property.
-        */
-
-       sec_ofs  = now.tv_sec  - date.tv_sec;
-       usec_ofs = now.tv_usec - date.tv_usec;
-       if ((int)usec_ofs < 0) {
-               usec_ofs += 1000000;
-               sec_ofs  -= 1;
-       }
-       ofs_new = ((ullong)sec_ofs << 32) + usec_ofs;
-       if (ofs_new != ofs)
-               HA_ATOMIC_STORE(&now_offset, ofs_new);
-}
-
-/* must be called once at boot to initialize some global variables */
-void tv_init_process_date()
-{
-       now_offset = 0;
-       gettimeofday(&date, NULL);
-       now = after_poll = before_poll = date;
-       global_now = ((ullong)date.tv_sec << 32) + (uint)date.tv_usec;
-       global_now_ms = now.tv_sec * 1000 + now.tv_usec / 1000;
-       ti->idle_pct = 100;
-       tv_update_date(0, 1);
-}
-
-/* must be called once per thread to initialize their thread-local variables.
- * Note that other threads might also be initializing and running in parallel.
- */
-void tv_init_thread_date()
-{
-       ullong old_now;
-
-       gettimeofday(&date, NULL);
-       after_poll = before_poll = date;
-
-       old_now = _HA_ATOMIC_LOAD(&global_now);
-       now.tv_sec = old_now >> 32;
-       now.tv_usec = (uint)old_now;
-       ti->idle_pct = 100;
-       tv_update_date(0, 1);
-}
-
-/* returns the current date as returned by gettimeofday() in ISO+microsecond
- * format. It uses a thread-local static variable that the reader can consume
- * for as long as it wants until next call. Thus, do not call it from a signal
- * handler. If <pad> is non-0, a trailing space will be added. It will always
- * return exactly 32 or 33 characters (depending on padding) and will always be
- * zero-terminated, thus it will always fit into a 34 bytes buffer.
- * This also always include the local timezone (in +/-HH:mm format) .
- */
-char *timeofday_as_iso_us(int pad)
-{
-       struct timeval new_date;
-       struct tm tm;
-       const char *offset;
-       char c;
-       gettimeofday(&new_date, NULL);
-       if (new_date.tv_sec != iso_time_sec || !new_date.tv_sec) {
-               get_localtime(new_date.tv_sec, &tm);
-               offset = get_gmt_offset(new_date.tv_sec, &tm);
-               if (unlikely(strftime(iso_time_str, sizeof(iso_time_str), "%Y-%m-%dT%H:%M:%S.000000+00:00", &tm) != 32))
-                       strcpy(iso_time_str, "YYYY-mm-ddTHH:MM:SS.000000-00:00"); // make the failure visible but respect format.
-               iso_time_str[26] = offset[0];
-               iso_time_str[27] = offset[1];
-               iso_time_str[28] = offset[2];
-               iso_time_str[30] = offset[3];
-               iso_time_str[31] = offset[4];
-               iso_time_sec = new_date.tv_sec;
-       }
-       /* utoa_pad adds a trailing 0 so we save the char for restore */
-       c = iso_time_str[26];
-       utoa_pad(new_date.tv_usec, iso_time_str + 20, 7);
-       iso_time_str[26] = c;
-       if (pad) {
-               iso_time_str[32] = ' ';
-               iso_time_str[33] = 0;
-       }
-       return iso_time_str;
-}
-
 /*
  * Local variables:
  *  c-indent-level: 8
index f1aa3ec8686150ad411c533bc52a6014bd8af43a..066f109ab7ef3fa2bb943f76294f75ec709fe376 100644 (file)
--- a/src/wdt.c
+++ b/src/wdt.c
 #include <time.h>
 
 #include <haproxy/api.h>
+#include <haproxy/clock.h>
 #include <haproxy/debug.h>
 #include <haproxy/errors.h>
 #include <haproxy/global.h>
 #include <haproxy/signal-t.h>
 #include <haproxy/thread.h>
-#include <haproxy/time.h>
 #include <haproxy/tools.h>