+ if (enabled != enable) {
+ ovs_mutex_lock(&mutex);
+ if (enable) {
+ if (!started) {
+ ovs_thread_create("system_stats",
+ system_stats_thread_func, NULL);
+ latch_init(&latch);
+ started = true;
+ }
+ discard_stats();
+ xpthread_cond_signal(&cond);
+ }
+ enabled = enable;
+ ovs_mutex_unlock(&mutex);
+ }
+}
+
+/* Tries to obtain a new snapshot of system stats every SYSTEM_STATS_INTERVAL
+ * milliseconds.
+ *
+ * When a new snapshot is available (which only occurs if system stats are
+ * enabled), returns it as an smap owned by the caller. The caller must use
+ * both smap_destroy() and free() to completely free the returned data.
+ *
+ * When no new snapshot is available, returns NULL. */
+struct smap *
+system_stats_run(void)
+{
+ struct smap *stats = NULL;
+
+ ovs_mutex_lock(&mutex);
+ if (system_stats) {
+ latch_poll(&latch);
+
+ if (enabled) {
+ stats = system_stats;
+ system_stats = NULL;
+ } else {
+ discard_stats();
+ }
+ }
+ ovs_mutex_unlock(&mutex);
+
+ return stats;
+}
+
+/* Causes poll_block() to wake up when system_stats_run() needs to be
+ * called. */
+void
+system_stats_wait(void)
+{
+ if (enabled) {
+ latch_wait(&latch);
+ }
+}
+
+static void
+discard_stats(void) OVS_REQUIRES(mutex)
+{
+ if (system_stats) {
+ smap_destroy(system_stats);
+ free(system_stats);
+ system_stats = NULL;
+ }
+}
+
+static void * NO_RETURN
+system_stats_thread_func(void *arg OVS_UNUSED)
+{
+ pthread_detach(pthread_self());
+
+ for (;;) {
+ long long int next_refresh;
+ struct smap *stats;
+
+ ovs_mutex_lock(&mutex);
+ while (!enabled) {
+ ovs_mutex_cond_wait(&cond, &mutex);
+ }
+ ovs_mutex_unlock(&mutex);
+
+ stats = xmalloc(sizeof *stats);
+ smap_init(stats);
+ get_cpu_cores(stats);
+ get_load_average(stats);
+ get_memory_stats(stats);
+ get_process_stats(stats);
+ get_filesys_stats(stats);
+
+ ovs_mutex_lock(&mutex);
+ discard_stats();
+ system_stats = stats;
+ latch_set(&latch);
+ ovs_mutex_unlock(&mutex);
+
+ next_refresh = time_msec() + SYSTEM_STATS_INTERVAL;
+ do {
+ poll_timer_wait_until(next_refresh);
+ poll_block();
+ } while (time_msec() < next_refresh);
+ }