]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
REORG: sched: move idle time calculation from time.h to task.h
authorWilly Tarreau <w@1wt.eu>
Thu, 30 Sep 2021 15:53:22 +0000 (17:53 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 1 Oct 2021 16:37:51 +0000 (18:37 +0200)
time.h is a horrible place to put activity calculation, it's a
historical mistake because the functions were there. We already have
most of the parts in sched.{c,h} and these ones make an exception in
the middle, forcing time.h to include some thread stuff and to access
the before/after_poll and idle_pct values.

Let's move these 3 functions to task.h with the other ones. They were
prefixed with "sched_" instead of the historical "tv_" which already
made no sense anymore.

include/haproxy/task.h
include/haproxy/time.h
src/ev_epoll.c
src/ev_evports.c
src/ev_kqueue.c
src/ev_poll.c
src/ev_select.c

index f7aebedf494d36fa92a6bd799af373ca279504f0..a38573771d1eb53efa1bf87a8904554b28184f7f 100644 (file)
@@ -614,6 +614,56 @@ static inline void task_schedule(struct task *task, int when)
        }
 }
 
+/* Update the idle time value twice a second, to be called after
+ * tv_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().
+ */
+static inline void sched_measure_idle()
+{
+       /* Let's compute the idle to work ratio. We worked between after_poll
+        * and before_poll, and slept between before_poll and date. The idle_pct
+        * is updated at most twice every second. Note that the current second
+        * rarely changes so we avoid a multiply when not needed.
+        */
+       int delta;
+
+       if ((delta = date.tv_sec - before_poll.tv_sec))
+               delta *= 1000000;
+       idle_time += delta + (date.tv_usec - before_poll.tv_usec);
+
+       if ((delta = date.tv_sec - after_poll.tv_sec))
+               delta *= 1000000;
+       samp_time += delta + (date.tv_usec - after_poll.tv_usec);
+
+       after_poll.tv_sec = date.tv_sec; after_poll.tv_usec = date.tv_usec;
+       if (samp_time < 500000)
+               return;
+
+       HA_ATOMIC_STORE(&ti->idle_pct, (100ULL * idle_time + samp_time / 2) / samp_time);
+       idle_time = samp_time = 0;
+}
+
+/* Collect date and time information after leaving poll(). <timeout> must be
+ * set to the maximum sleep time passed to poll (in milliseconds), and
+ * <interrupted> must be zero if the poller reached the timeout or non-zero
+ * otherwise, which generally is provided by the poller's return value.
+ */
+static inline void sched_leaving_poll(int timeout, int interrupted)
+{
+       sched_measure_idle();
+       ti->prev_cpu_time  = now_cpu_time();
+       ti->prev_mono_time = now_mono_time();
+}
+
+/* Collect date and time information before calling poll(). This will be used
+ * to count the run time of the past loop and the sleep time of the next poll.
+ */
+static inline void sched_entering_poll()
+{
+       gettimeofday(&before_poll, NULL);
+}
+
 /* This function register a new signal. "lua" is the current lua
  * execution context. It contains a pointer to the associated task.
  * "link" is a list head attached to an other task that must be wake
index ad3a1dbfd1f7597bdd50b2ab3e96a8c3f7c1b61f..be9a2bb4b4b9da3921df4244bc70900c4b0fd88b 100644 (file)
@@ -552,55 +552,6 @@ static inline uint64_t now_cpu_time_thread(const struct thread_info *thr)
 #endif
 }
 
-/* Update the idle time value twice a second, to be called after
- * tv_update_date() when called after poll(). It relies on <before_poll> to be
- * updated to the system time before calling poll().
- */
-static inline void measure_idle()
-{
-       /* Let's compute the idle to work ratio. We worked between after_poll
-        * and before_poll, and slept between before_poll and date. The idle_pct
-        * is updated at most twice every second. Note that the current second
-        * rarely changes so we avoid a multiply when not needed.
-        */
-       int delta;
-
-       if ((delta = date.tv_sec - before_poll.tv_sec))
-               delta *= 1000000;
-       idle_time += delta + (date.tv_usec - before_poll.tv_usec);
-
-       if ((delta = date.tv_sec - after_poll.tv_sec))
-               delta *= 1000000;
-       samp_time += delta + (date.tv_usec - after_poll.tv_usec);
-
-       after_poll.tv_sec = date.tv_sec; after_poll.tv_usec = date.tv_usec;
-       if (samp_time < 500000)
-               return;
-
-       HA_ATOMIC_STORE(&ti->idle_pct, (100ULL * idle_time + samp_time / 2) / samp_time);
-       idle_time = samp_time = 0;
-}
-
-/* Collect date and time information before calling poll(). This will be used
- * to count the run time of the past loop and the sleep time of the next poll.
- */
-static inline void tv_entering_poll()
-{
-       gettimeofday(&before_poll, NULL);
-}
-
-/* Collect date and time information after leaving poll(). <timeout> must be
- * set to the maximum sleep time passed to poll (in milliseconds), and
- * <interrupted> must be zero if the poller reached the timeout or non-zero
- * otherwise, which generally is provided by the poller's return value.
- */
-static inline void tv_leaving_poll(int timeout, int interrupted)
-{
-       measure_idle();
-       ti->prev_cpu_time  = now_cpu_time();
-       ti->prev_mono_time = now_mono_time();
-}
-
 #endif /* _HAPROXY_TIME_H */
 
 /*
index 35a8a9c79e3739b19cca094467cce30e895aedb9..d376cd994a4de5943df9e67d3dd29a53b6052df4 100644 (file)
@@ -20,6 +20,7 @@
 #include <haproxy/global.h>
 #include <haproxy/signal.h>
 #include <haproxy/ticks.h>
+#include <haproxy/task.h>
 #include <haproxy/time.h>
 #include <haproxy/tools.h>
 
@@ -188,7 +189,7 @@ static void _do_poll(struct poller *p, int exp, int wake)
 
        /* now let's wait for polled events */
        wait_time = wake ? 0 : compute_poll_timeout(exp);
-       tv_entering_poll();
+       sched_entering_poll();
        activity_count_runtime();
        do {
                int timeout = (global.tune.options & GTUNE_BUSY_POLLING) ? 0 : wait_time;
@@ -208,7 +209,7 @@ static void _do_poll(struct poller *p, int exp, int wake)
                        break;
        } while (1);
 
-       tv_leaving_poll(wait_time, status);
+       sched_leaving_poll(wait_time, status);
 
        thread_harmless_end();
        thread_idle_end();
index a61392d54a8999435d346f241470f740d7f44c39..f8c7f34720a0e59e67d7302a8ce043997f4b62c5 100644 (file)
@@ -23,6 +23,7 @@
 #include <haproxy/fd.h>
 #include <haproxy/global.h>
 #include <haproxy/signal.h>
+#include <haproxy/task.h>
 #include <haproxy/ticks.h>
 #include <haproxy/time.h>
 
@@ -158,7 +159,7 @@ static void _do_poll(struct poller *p, int exp, int wake)
         * Determine how long to wait for events to materialise on the port.
         */
        wait_time = wake ? 0 : compute_poll_timeout(exp);
-       tv_entering_poll();
+       sched_entering_poll();
        activity_count_runtime();
 
        do {
@@ -202,7 +203,7 @@ static void _do_poll(struct poller *p, int exp, int wake)
                        break;
        } while(1);
 
-       tv_leaving_poll(wait_time, nevlist);
+       sched_leaving_poll(wait_time, nevlist);
 
        thread_harmless_end();
        thread_idle_end();
index 5ccaf159aa37f362716208c541b5f04cb9d453b0..3a53e07d67e11a03e4a6e28888ce5645e258fc84 100644 (file)
@@ -22,6 +22,7 @@
 #include <haproxy/fd.h>
 #include <haproxy/global.h>
 #include <haproxy/signal.h>
+#include <haproxy/task.h>
 #include <haproxy/ticks.h>
 #include <haproxy/time.h>
 
@@ -145,7 +146,7 @@ static void _do_poll(struct poller *p, int exp, int wake)
        /* now let's wait for events */
        wait_time = wake ? 0 : compute_poll_timeout(exp);
        fd = global.tune.maxpollevents;
-       tv_entering_poll();
+       sched_entering_poll();
        activity_count_runtime();
 
        do {
@@ -174,7 +175,7 @@ static void _do_poll(struct poller *p, int exp, int wake)
                        break;
        } while (1);
 
-       tv_leaving_poll(wait_time, status);
+       sched_leaving_poll(wait_time, status);
 
        thread_harmless_end();
        thread_idle_end();
index 627f4bf8e25c57860a9b99a7b6c0b27e2d8eaa4a..b08bfcac2d49a5ca21d14a8220e4782ce3be78ca 100644 (file)
@@ -21,6 +21,7 @@
 #include <haproxy/api.h>
 #include <haproxy/fd.h>
 #include <haproxy/global.h>
+#include <haproxy/task.h>
 #include <haproxy/ticks.h>
 #include <haproxy/time.h>
 
@@ -201,11 +202,11 @@ static void _do_poll(struct poller *p, int exp, int wake)
 
        /* now let's wait for events */
        wait_time = wake ? 0 : compute_poll_timeout(exp);
-       tv_entering_poll();
+       sched_entering_poll();
        activity_count_runtime();
        status = poll(poll_events, nbfd, wait_time);
        tv_update_date(wait_time, status);
-       tv_leaving_poll(wait_time, status);
+       sched_leaving_poll(wait_time, status);
 
        thread_harmless_end();
        thread_idle_end();
index 12fdaff39c6b0b5034d61a009a63a4bc8f7cc987..12c7341dd4bf0f9bf7087e3050cad71f826a81ad 100644 (file)
@@ -18,6 +18,7 @@
 #include <haproxy/api.h>
 #include <haproxy/fd.h>
 #include <haproxy/global.h>
+#include <haproxy/task.h>
 #include <haproxy/ticks.h>
 #include <haproxy/time.h>
 
@@ -172,7 +173,7 @@ static void _do_poll(struct poller *p, int exp, int wake)
        delta_ms = wake ? 0 : compute_poll_timeout(exp);
        delta.tv_sec  = (delta_ms / 1000);
        delta.tv_usec = (delta_ms % 1000) * 1000;
-       tv_entering_poll();
+       sched_entering_poll();
        activity_count_runtime();
        status = select(maxfd,
                        readnotnull ? tmp_evts[DIR_RD] : NULL,
@@ -180,7 +181,7 @@ static void _do_poll(struct poller *p, int exp, int wake)
                        NULL,
                        &delta);
        tv_update_date(delta_ms, status);
-       tv_leaving_poll(delta_ms, status);
+       sched_leaving_poll(delta_ms, status);
 
        thread_harmless_end();
        thread_idle_end();