Skip to contentSkip to conversation

simpletrace: Thread-safe tracing

PATCH In review Stefan Hajnoczi 4 participants 7 messages Feb 27, 2011 – Feb 28, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 simpletrace: Thread-safe tracing

Trace events outside the global mutex cannot be used with the simple trace backend since it is not thread-safe. There is no check to prevent them being enabled so people sometimes learn this the hard way.

This patch restructures the simple trace backend with a ring buffer suitable for multiple concurrent writers. A writeout thread empties the trace buffer when threshold fill levels are reached. Should the writeout thread be unable to keep up with trace generation, records will simply be dropped.

---
 docs/tracing.txt |    5 -
 simpletrace.c    |  297 +++++++++++++++++++++++++++++++++++-------------------
 simpletrace.h    |    8 ++
 vl.c             |   16 +--
 4 files changed, 207 insertions(+), 119 deletions(-)
docs/tracing.txt +0 -5
index a6cc56f..f15069c 100644
@@ -141,11 +141,6 @@ source tree.  It may not be as powerful as platform-specific or third-party
 trace backends but it is portable.  This is the recommended trace backend
 unless you have specific needs for more advanced backends.
 
-Warning: the "simple" backend is not thread-safe so only enable trace events
-that are executed while the global mutex is held.  Much of QEMU meets this
-requirement but some utility functions like qemu_malloc() or thread-related
-code cannot be safely traced using the "simple" backend.
-
 ==== Monitor commands ====
 
 * info trace
simpletrace.c +195 -102

387 lines not on this page · raw message

simpletrace.h +8 -0
index 2f44ed3..3a5bd9f 100644
@@ -15,6 +15,7 @@
 #include <stdbool.h>
 #include <stdio.h>
 
+#ifdef CONFIG_SIMPLE_TRACE
 typedef uint64_t TraceEventID;
 
 typedef struct {
@@ -36,5 +37,12 @@ void st_print_trace_file_status(FILE *stream, fprintf_function stream_printf);
 void st_set_trace_file_enabled(bool enable);
 bool st_set_trace_file(const char *file);
 void st_flush_trace_buffer(void);
+void st_init(const char *file);
+#else
+static inline void st_init(const char *file)
+{
+    /* Do nothing */
+}
+#endif /* !CONFIG_SIMPLE_TRACE */
 
 #endif /* SIMPLETRACE_H */
vl.c +4 -12
index b436952..5e007a7 100644
@@ -47,9 +47,6 @@
 #include <dirent.h>
 #include <netdb.h>
 #include <sys/select.h>
-#ifdef CONFIG_SIMPLE_TRACE
-#include "trace.h"
-#endif
 
 #ifdef CONFIG_BSD
 #include <sys/stat.h>
@@ -159,6 +156,7 @@ int main(int argc, char **argv)
 #include "slirp/libslirp.h"
 
 #include "trace.h"
+#include "simpletrace.h"
 #include "qemu-queue.h"
 #include "cpus.h"
 #include "arch_init.h"
@@ -1941,10 +1939,8 @@ int main(int argc, char **argv, char **envp)
     const char *incoming = NULL;
     int show_vnc_port = 0;
     int defconfig = 1;
-
-#ifdef CONFIG_SIMPLE_TRACE
     const char *trace_file = NULL;
-#endif
+
     atexit(qemu_run_exit_notifiers);
     error_set_progname(argv[0]);
 
@@ -2770,6 +2766,8 @@ int main(int argc, char **argv, char **envp)
     }
     loc_set_none();
 
+    st_init(trace_file);
+
     /* If no data_dir is specified then try to find it relative to the
        executable path.  */
     if (!data_dir) {
@@ -2780,12 +2778,6 @@ int main(int argc, char **argv, char **envp)
         data_dir = CONFIG_QEMU_DATADIR;
     }
 
-#ifdef CONFIG_SIMPLE_TRACE
-    /*
-     * Set the trace file name, if specified.
-     */
-    st_set_trace_file(trace_file);
-#endif
     /*
      * Default to max_cpus = smp_cpus, in case the user doesn't
      * specify a max_cpus value.
Signature · 1 line
1.7.2.3
#1 lore raw

simpletrace.c typedef struct { · line 58

Quoted 8 lines · Stefan Hajnoczi

+ * Trace records are written out by a dedicated thread. The thread waits for
+ * records to become available, writes them out, and then waits again.
+ */

simpletrace.c +5 -0
+static pthread_mutex_t trace_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t trace_available_cond = PTHREAD_COND_INITIALIZER;
+static pthread_cond_t trace_empty_cond = PTHREAD_COND_INITIALIZER;
+static bool trace_available;
+static bool trace_writeout_enabled;

Please use QemuThread.

simpletrace.c void st_print_trace_events(FILE *stream, int (*stream_printf)(FILE *stream, cons · line 339

Quoted 12 lines
simpletrace.c +7 -4
+
+    pthread_attr_init(&attr);
+    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 
-    tp = find_trace_event_by_name(tname);
-    if (tp) {
-        tp->state = tstate;
-        return true;
+    sigfillset(&set);
+    pthread_sigmask(SIG_SETMASK, &set, &oldset);
+    ret = pthread_create(&thread, &attr, writeout_thread, NULL);
+    pthread_sigmask(SIG_SETMASK, &oldset, NULL);

This is also taken care of by QemuThread.

Paolo

#2 lore raw
Quoted 13 lines · Paolo Bonzini

simpletrace.c typedef struct { · line 58

Quoted 10 lines · Stefan Hajnoczi

+ * Trace records are written out by a dedicated thread.  The thread waits for
+ * records to become available, writes them out, and then waits again.
+ */

simpletrace.c +5 -0
+static pthread_mutex_t trace_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t trace_available_cond = PTHREAD_COND_INITIALIZER;
+static pthread_cond_t trace_empty_cond = PTHREAD_COND_INITIALIZER;
+static bool trace_available;
+static bool trace_writeout_enabled;

Please use QemuThread.

The tracing code itself should use avoid core QEMU code. Otherwise we can't trace QemuThread - we'd have an infinite loop.

Stefan

#3 lore raw
Quoted 15 lines · Stefan Hajnoczi
Quoted 13 lines · Paolo Bonzini

simpletrace.c typedef struct { · line 58

Quoted 10 lines · Stefan Hajnoczi

+ * Trace records are written out by a dedicated thread. The thread waits for
+ * records to become available, writes them out, and then waits again.
+ */

simpletrace.c +5 -0
+static pthread_mutex_t trace_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t trace_available_cond = PTHREAD_COND_INITIALIZER;
+static pthread_cond_t trace_empty_cond = PTHREAD_COND_INITIALIZER;
+static bool trace_available;
+static bool trace_writeout_enabled;

Please use QemuThread.

Quoted 2 lines

The tracing code itself should use avoid core QEMU code. Otherwise we can't trace QemuThread - we'd have an infinite loop.

Hmm, right... they'll use stdio to trace Win32 then... :) I was actually thinking more of the code duplication.

But do you really need tracing at such a low level? I'd expect tracing wrappers like qemu_lock_mutex_iothread, not mutexes in general.

Paolo

#4 lore raw
Quoted 20 lines · Paolo Bonzini
Quoted 19 lines · Stefan Hajnoczi
Quoted 15 lines · Paolo Bonzini

simpletrace.c typedef struct { · line 58

Quoted 11 lines · Stefan Hajnoczi

+ * Trace records are written out by a dedicated thread.  The thread waits
for
+ * records to become available, writes them out, and then waits again.
+ */

simpletrace.c +5 -0
+static pthread_mutex_t trace_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t trace_available_cond = PTHREAD_COND_INITIALIZER;
+static pthread_cond_t trace_empty_cond = PTHREAD_COND_INITIALIZER;
+static bool trace_available;
+static bool trace_writeout_enabled;

Please use QemuThread.

Quoted 8 lines
Quoted 2 lines

The tracing code itself should use avoid core QEMU code.  Otherwise we can't trace QemuThread - we'd have an infinite loop.

Hmm, right... they'll use stdio to trace Win32 then... :)  I was actually thinking more of the code duplication.

But do you really need tracing at such a low level?  I'd expect tracing wrappers like qemu_lock_mutex_iothread, not mutexes in general.

We can get away with it for some codepaths but it adds new constraints that are hard to check against. For example, simpletrace uses get_clock() but we need to limit ourselves as much as possible. If that function calls any other core QEMU code like qemu_malloc(), then we risk infinite recursion because qemu_malloc() has a useful trace event.

Stefan

#5 lore raw
In reply to Stefan Hajnoczi
Quoted 9 lines · Stefan Hajnoczi

Trace events outside the global mutex cannot be used with the simple trace backend since it is not thread-safe. There is no check to prevent them being enabled so people sometimes learn this the hard way.

This patch restructures the simple trace backend with a ring buffer suitable for multiple concurrent writers. A writeout thread empties the trace buffer when threshold fill levels are reached. Should the writeout thread be unable to keep up with trace generation, records will simply be dropped.

It would be good to have an indication of the fact that records were dropped in the file.

Signature · 1 line
error compiling committee.c: too many arguments to function
#6 lore raw
Quoted 6 lines · Avi Kivity
Quoted 5 lines · Stefan Hajnoczi

Trace events outside the global mutex cannot be used with the simple trace backend since it is not thread-safe.  There is no check to prevent them being enabled so people sometimes learn this the hard way.

Quoted 8 lines
Quoted 5 lines

This patch restructures the simple trace backend with a ring buffer suitable for multiple concurrent writers.  A writeout thread empties the trace buffer when threshold fill levels are reached.  Should the writeout thread be unable to keep up with trace generation, records will simply be dropped.

It would be good to have an indication of the fact that records were dropped in the file.

Good idea. Trace files begin with a record that has a special ID. I'll look at extending this either by picking another special ID or by reusing the header record.

Stefan