Merge branch 'mainstream'
[sliver-openvswitch.git] / lib / timeval.c
index b36203a..2f00355 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
 
 #include <config.h>
 #include "timeval.h"
-#include <assert.h>
 #include <errno.h>
 #if HAVE_EXECINFO_H
 #include <execinfo.h>
@@ -121,6 +120,16 @@ time_init(void)
     }
     inited = true;
 
+    /* The implementation of backtrace() in glibc does some one time
+     * initialization which is not signal safe.  This can cause deadlocks if
+     * run from the signal handler.  As a workaround, force the initialization
+     * to happen here. */
+    if (HAVE_EXECINFO_H) {
+        void *bt[1];
+
+        backtrace(bt, ARRAY_SIZE(bt));
+    }
+
     memset(traces, 0, sizeof traces);
 
     if (HAVE_EXECINFO_H && CACHE_TIME) {
@@ -319,7 +328,7 @@ time_alarm(unsigned int secs)
     time_refresh();
 
     now = time_msec();
-    msecs = secs * 1000;
+    msecs = secs * 1000LL;
 
     block_sigalrm(&oldsigs);
     deadline = now < LLONG_MAX - msecs ? now + msecs : LLONG_MAX;
@@ -507,7 +516,7 @@ log_poll_interval(long long int last_wakeup)
 {
     long long int interval = time_msec() - last_wakeup;
 
-    if (interval >= 1000) {
+    if (interval >= 1000 && !warp_offset.tv_sec && !warp_offset.tv_nsec) {
         const struct rusage *last_rusage = get_recent_rusage();
         struct rusage rusage;
 
@@ -615,9 +624,12 @@ trace_map_lookup(struct hmap *trace_map, struct trace *key)
     return NULL;
 }
 
-
-static void
-format_backtraces(struct ds *ds)
+/*  Appends a string to 'ds' representing backtraces recorded at regular
+ *  intervals in the recent past.  This information can be used to get a sense
+ *  of what the process has been spending the majority of time doing.  Will
+ *  ommit any backtraces which have not occurred at least 'min_count' times. */
+void
+format_backtraces(struct ds *ds, size_t min_count)
 {
     time_init();
 
@@ -652,6 +664,10 @@ format_backtraces(struct ds *ds)
 
             hmap_remove(&trace_map, &trace->node);
 
+            if (trace->count < min_count) {
+                continue;
+            }
+
             frame_strs = backtrace_symbols(trace->backtrace, trace->n_frames);
 
             ds_put_format(ds, "Count %zu\n", trace->count);
@@ -714,8 +730,8 @@ backtrace_cb(struct unixctl_conn *conn,
 {
     struct ds ds = DS_EMPTY_INITIALIZER;
 
-    assert(HAVE_EXECINFO_H && CACHE_TIME);
-    format_backtraces(&ds);
+    ovs_assert(HAVE_EXECINFO_H && CACHE_TIME);
+    format_backtraces(&ds, 0);
     unixctl_command_reply(conn, ds_cstr(&ds));
     ds_destroy(&ds);
 }