+\f
+#define SYSTEM_STATS_INTERVAL (5 * 1000) /* In milliseconds. */
+
+/* Whether the client wants us to report system stats. */
+static bool enabled;
+
+static enum {
+ S_DISABLED, /* Not enabled, nothing going on. */
+ S_WAITING, /* Sleeping for SYSTEM_STATS_INTERVAL ms. */
+ S_REQUEST_SENT, /* Sent a request to worker. */
+ S_REPLY_RECEIVED /* Received a reply from worker. */
+} state;
+
+/* In S_WAITING state: the next time to wake up.
+ * In other states: not meaningful. */
+static long long int next_refresh;
+
+/* In S_REPLY_RECEIVED: the stats that have just been received.
+ * In other states: not meaningful. */
+static struct smap *received_stats;
+
+static worker_request_func system_stats_request_cb;
+static worker_reply_func system_stats_reply_cb;
+
+/* Enables or disables system stats collection, according to 'new_enable'.
+ *
+ * Even if system stats are disabled, the caller should still periodically call
+ * system_stats_run(). */
+void
+system_stats_enable(bool new_enable)
+{
+ if (new_enable != enabled) {
+ if (new_enable) {
+ if (state == S_DISABLED) {
+ state = S_WAITING;
+ next_refresh = time_msec();
+ }
+ } else {
+ if (state == S_WAITING) {
+ state = S_DISABLED;
+ }
+ }
+ enabled = new_enable;
+ }
+}