REORG: time/activity: move activity measurements to activity.{c,h}

At the moment the situation with activity measurement is quite tricky
because the struct activity is defined in global.h and declared in
haproxy.c, with operations made in time.h and relying on freq_ctr
which are defined in freq_ctr.h which itself includes time.h. It's
barely possible to touch any of these files without breaking all the
circular dependency.

Let's move all this stuff to activity.{c,h} and be done with it. The
measurement of active and stolen time is now done in a dedicated
function called just after tv_before_poll() instead of mixing the two,
which used to be a lazy (but convenient) decision.

No code was changed, stuff was just moved around.
diff --git a/include/common/time.h b/include/common/time.h
index a1664b4..b9efd71 100644
--- a/include/common/time.h
+++ b/include/common/time.h
@@ -85,11 +85,6 @@
  */
 REGPRM2 int tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2);
 
-/* Updates the current thread's statistics about stolen CPU time. The unit for
- * <stolen> is half-milliseconds.
- */
-REGPRM1 void report_stolen_time(uint64_t stolen);
-
 /**** general purpose functions and macros *******************************/
 
 
@@ -581,26 +576,6 @@
  */
 static inline void tv_entering_poll()
 {
-	uint64_t new_mono_time;
-	uint64_t new_cpu_time;
-	int64_t stolen;
-
-	new_cpu_time   = now_cpu_time();
-	new_mono_time  = now_mono_time();
-
-	if (prev_cpu_time && prev_mono_time) {
-		new_cpu_time  -= prev_cpu_time;
-		new_mono_time -= prev_mono_time;
-		stolen = new_mono_time - new_cpu_time;
-		if (stolen >= 500000) {
-			stolen /= 500000;
-			/* more than half a millisecond difference might
-			 * indicate an undesired preemption.
-			 */
-			report_stolen_time(stolen);
-		}
-	}
-
 	gettimeofday(&before_poll, NULL);
 }
 
diff --git a/include/proto/activity.h b/include/proto/activity.h
new file mode 100644
index 0000000..4cf9c8d
--- /dev/null
+++ b/include/proto/activity.h
@@ -0,0 +1,69 @@
+/*
+ * include/proto/activity.h
+ * This file contains macros and inline functions for activity measurements.
+ *
+ * Copyright (C) 2000-2018 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 _PROTO_ACTIVITY_H
+#define _PROTO_ACTIVITY_H
+
+#include <common/config.h>
+#include <common/hathreads.h>
+#include <common/time.h>
+#include <types/activity.h>
+
+extern struct activity activity[MAX_THREADS];
+
+
+void report_stolen_time(uint64_t stolen);
+
+/* 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 activity_count_runtime()
+{
+	uint64_t new_mono_time;
+	uint64_t new_cpu_time;
+	int64_t stolen;
+
+	new_cpu_time   = now_cpu_time();
+	new_mono_time  = now_mono_time();
+
+	if (prev_cpu_time && prev_mono_time) {
+		new_cpu_time  -= prev_cpu_time;
+		new_mono_time -= prev_mono_time;
+		stolen = new_mono_time - new_cpu_time;
+		if (unlikely(stolen >= 500000)) {
+			stolen /= 500000;
+			/* more than half a millisecond difference might
+			 * indicate an undesired preemption.
+			 */
+			report_stolen_time(stolen);
+		}
+	}
+}
+
+
+#endif /* _PROTO_ACTIVITY_H */
+
+/*
+ * Local variables:
+ *  c-indent-level: 8
+ *  c-basic-offset: 8
+ * End:
+ */
diff --git a/include/proto/fd.h b/include/proto/fd.h
index 03e3182..81aea87 100644
--- a/include/proto/fd.h
+++ b/include/proto/fd.h
@@ -30,9 +30,8 @@
 #include <common/config.h>
 #include <common/ticks.h>
 #include <common/time.h>
-
 #include <types/fd.h>
-#include <types/global.h>
+#include <proto/activity.h>
 
 /* public variables */
 
diff --git a/include/types/activity.h b/include/types/activity.h
new file mode 100644
index 0000000..99922b4
--- /dev/null
+++ b/include/types/activity.h
@@ -0,0 +1,63 @@
+/*
+ * include/types/activity.h
+ * This file contains structure declarations for activity measurements.
+ *
+ * Copyright (C) 2000-2018 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 _TYPES_ACTIVITY_H
+#define _TYPES_ACTIVITY_H
+
+#include <common/config.h>
+#include <types/freq_ctr.h>
+
+/* per-thread activity reports. It's important that it's aligned on cache lines
+ * because some elements will be updated very often. Most counters are OK on
+ * 32-bit since this will be used during debugging sessions for troubleshooting
+ * in iterative mode.
+ */
+struct activity {
+	unsigned int loops;        // complete loops in run_poll_loop()
+	unsigned int wake_cache;   // active fd_cache prevented poll() from sleeping
+	unsigned int wake_tasks;   // active tasks prevented poll() from sleeping
+	unsigned int wake_signal;  // pending signal prevented poll() from sleeping
+	unsigned int poll_exp;     // number of times poll() sees an expired timeout (includes wake_*)
+	unsigned int poll_drop;    // poller dropped a dead FD from the update list
+	unsigned int poll_dead;    // poller woke up with a dead FD
+	unsigned int poll_skip;    // poller skipped another thread's FD
+	unsigned int fd_skip;      // fd cache skipped another thread's FD
+	unsigned int fd_lock;      // fd cache skipped a locked FD
+	unsigned int fd_del;       // fd cache detected a deleted FD
+	unsigned int conn_dead;    // conn_fd_handler woke up on an FD indicating a dead connection
+	unsigned int stream;       // calls to process_stream()
+	unsigned int empty_rq;     // calls to process_runnable_tasks() with nothing for the thread
+	unsigned int long_rq;      // process_runnable_tasks() left with tasks in the run queue
+	unsigned int cpust_total;  // sum of half-ms stolen per thread
+	struct freq_ctr cpust_1s;  // avg amount of half-ms stolen over last second
+	struct freq_ctr_period cpust_15s; // avg amount of half-ms stolen over last 15s
+	char __pad[0]; // unused except to check remaining room
+	char __end[0] __attribute__((aligned(64))); // align size to 64.
+};
+
+#endif /* _TYPES_ACTIVITY_H */
+
+/*
+ * Local variables:
+ *  c-indent-level: 8
+ *  c-basic-offset: 8
+ * End:
+ */
diff --git a/include/types/global.h b/include/types/global.h
index 7c6f954..5a3f338 100644
--- a/include/types/global.h
+++ b/include/types/global.h
@@ -28,7 +28,6 @@
 #include <common/standard.h>
 #include <common/hathreads.h>
 
-#include <types/freq_ctr.h>
 #include <types/listener.h>
 #include <types/proxy.h>
 #include <types/task.h>
@@ -177,34 +176,6 @@
 #endif
 };
 
-/* per-thread activity reports. It's important that it's aligned on cache lines
- * because some elements will be updated very often. Most counters are OK on
- * 32-bit since this will be used during debugging sessions for troubleshooting
- * in iterative mode.
- */
-struct activity {
-	unsigned int loops;        // complete loops in run_poll_loop()
-	unsigned int wake_cache;   // active fd_cache prevented poll() from sleeping
-	unsigned int wake_tasks;   // active tasks prevented poll() from sleeping
-	unsigned int wake_signal;  // pending signal prevented poll() from sleeping
-	unsigned int poll_exp;     // number of times poll() sees an expired timeout (includes wake_*)
-	unsigned int poll_drop;    // poller dropped a dead FD from the update list
-	unsigned int poll_dead;    // poller woke up with a dead FD
-	unsigned int poll_skip;    // poller skipped another thread's FD
-	unsigned int fd_skip;      // fd cache skipped another thread's FD
-	unsigned int fd_lock;      // fd cache skipped a locked FD
-	unsigned int fd_del;       // fd cache detected a deleted FD
-	unsigned int conn_dead;    // conn_fd_handler woke up on an FD indicating a dead connection
-	unsigned int stream;       // calls to process_stream()
-	unsigned int empty_rq;     // calls to process_runnable_tasks() with nothing for the thread
-	unsigned int long_rq;      // process_runnable_tasks() left with tasks in the run queue
-	unsigned int cpust_total;  // sum of half-ms stolen per thread
-	struct freq_ctr cpust_1s;  // avg amount of half-ms stolen over last second
-	struct freq_ctr_period cpust_15s; // avg amount of half-ms stolen over last 15s
-	char __pad[0]; // unused except to check remaining room
-	char __end[0] __attribute__((aligned(64))); // align size to 64.
-};
-
 /*
  * Structure used to describe the processes in master worker mode
  */
@@ -221,7 +192,6 @@
 };
 
 extern struct global global;
-extern struct activity activity[MAX_THREADS];
 extern int  pid;                /* current process id */
 extern int  relative_pid;       /* process id starting at 1 */
 extern unsigned long pid_bit;   /* bit corresponding to the process id */