From patchwork Tue Sep 20 12:17:06 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 115517 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id AC82AB71D7 for ; Tue, 20 Sep 2011 22:17:33 +1000 (EST) Received: from localhost ([::1]:41657 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R5zGM-0008JZ-3d for incoming@patchwork.ozlabs.org; Tue, 20 Sep 2011 08:17:22 -0400 Received: from eggs.gnu.org ([140.186.70.92]:36617) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R5zGE-0008It-Pd for qemu-devel@nongnu.org; Tue, 20 Sep 2011 08:17:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1R5zGD-0005eJ-M8 for qemu-devel@nongnu.org; Tue, 20 Sep 2011 08:17:14 -0400 Received: from mtagate2.uk.ibm.com ([194.196.100.162]:35102) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R5zGD-0005e8-Ex for qemu-devel@nongnu.org; Tue, 20 Sep 2011 08:17:13 -0400 Received: from d06nrmr1707.portsmouth.uk.ibm.com (d06nrmr1707.portsmouth.uk.ibm.com [9.149.39.225]) by mtagate2.uk.ibm.com (8.13.1/8.13.1) with ESMTP id p8KCHBvQ025500 for ; Tue, 20 Sep 2011 12:17:11 GMT Received: from d06av05.portsmouth.uk.ibm.com (d06av05.portsmouth.uk.ibm.com [9.149.37.229]) by d06nrmr1707.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p8KCHBDL1839284 for ; Tue, 20 Sep 2011 13:17:11 +0100 Received: from d06av05.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av05.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p8KCHArP014635 for ; Tue, 20 Sep 2011 06:17:11 -0600 Received: from localhost (dyn-9-174-219-31.manchester-maybrook.uk.ibm.com [9.174.219.31]) by d06av05.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p8KCHA5d014631; Tue, 20 Sep 2011 06:17:10 -0600 From: stefanha@linux.vnet.ibm.com To: Date: Tue, 20 Sep 2011 13:17:06 +0100 Message-Id: <1316521027-18707-2-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.5.4 In-Reply-To: <1316521027-18707-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1316521027-18707-1-git-send-email-stefanha@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 194.196.100.162 Cc: Paolo Bonzini , Stefan Hajnoczi , Jan Kiszka Subject: [Qemu-devel] [PATCH v3 1/2] trace: portable simple trace backend using glib X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Stefan Hajnoczi Convert the simple trace backend to glib so that it works under Windows. We cannot use pthread directly but glib provides portable abstractions. Also use glib atomics instead of newish gcc builtins which may not be supported on Windows toolchains. Signed-off-by: Stefan Hajnoczi --- trace/simple.c | 74 ++++++++++++++++++++++++++++++++++++------------------- 1 files changed, 48 insertions(+), 26 deletions(-) diff --git a/trace/simple.c b/trace/simple.c index a609368..885764a 100644 --- a/trace/simple.c +++ b/trace/simple.c @@ -12,8 +12,10 @@ #include #include #include +#ifndef _WIN32 #include #include +#endif #include "qemu-timer.h" #include "trace.h" #include "trace/control.h" @@ -54,9 +56,9 @@ enum { * Trace records are written out by a dedicated thread. The thread waits for * records to become available, writes them out, and then waits again. */ -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 GStaticMutex trace_lock = G_STATIC_MUTEX_INIT; +static GCond *trace_available_cond; +static GCond *trace_empty_cond; static bool trace_available; static bool trace_writeout_enabled; @@ -93,29 +95,30 @@ static bool get_trace_record(unsigned int idx, TraceRecord *record) */ static void flush_trace_file(bool wait) { - pthread_mutex_lock(&trace_lock); + g_static_mutex_lock(&trace_lock); trace_available = true; - pthread_cond_signal(&trace_available_cond); + g_cond_signal(trace_available_cond); if (wait) { - pthread_cond_wait(&trace_empty_cond, &trace_lock); + g_cond_wait(trace_empty_cond, g_static_mutex_get_mutex(&trace_lock)); } - pthread_mutex_unlock(&trace_lock); + g_static_mutex_unlock(&trace_lock); } static void wait_for_trace_records_available(void) { - pthread_mutex_lock(&trace_lock); + g_static_mutex_lock(&trace_lock); while (!(trace_available && trace_writeout_enabled)) { - pthread_cond_signal(&trace_empty_cond); - pthread_cond_wait(&trace_available_cond, &trace_lock); + g_cond_signal(trace_empty_cond); + g_cond_wait(trace_available_cond, + g_static_mutex_get_mutex(&trace_lock)); } trace_available = false; - pthread_mutex_unlock(&trace_lock); + g_static_mutex_unlock(&trace_lock); } -static void *writeout_thread(void *opaque) +static gpointer writeout_thread(gpointer opaque) { TraceRecord record; unsigned int writeout_idx = 0; @@ -159,7 +162,7 @@ static void trace(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, timestamp = get_clock(); - idx = __sync_fetch_and_add(&trace_idx, 1) % TRACE_BUF_LEN; + idx = g_atomic_int_exchange_and_add((gint *)&trace_idx, 1) % TRACE_BUF_LEN; trace_buf[idx] = (TraceRecord){ .event = event, .timestamp_ns = timestamp, @@ -331,28 +334,47 @@ bool trace_event_set_state(const char *name, bool state) return false; } -bool trace_backend_init(const char *events, const char *file) +/* Helper function to create a thread with signals blocked. Use glib's + * portable threads since QEMU abstractions cannot be used due to reentrancy in + * the tracer. Also note the signal masking on POSIX hosts so that the thread + * does not steal signals when the rest of the program wants them blocked. + */ +static GThread *trace_thread_create(GThreadFunc fn) { - pthread_t thread; - pthread_attr_t attr; + GThread *thread; +#ifndef _WIN32 sigset_t set, oldset; - int ret; - - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); sigfillset(&set); pthread_sigmask(SIG_SETMASK, &set, &oldset); - ret = pthread_create(&thread, &attr, writeout_thread, NULL); +#endif + thread = g_thread_create(writeout_thread, NULL, FALSE, NULL); +#ifndef _WIN32 pthread_sigmask(SIG_SETMASK, &oldset, NULL); +#endif - if (ret != 0) { + return thread; +} + +bool trace_backend_init(const char *events, const char *file) +{ + GThread *thread; + + if (!g_thread_supported()) { + g_thread_init(NULL); + } + + trace_available_cond = g_cond_new(); + trace_empty_cond = g_cond_new(); + + thread = trace_thread_create(writeout_thread); + if (!thread) { fprintf(stderr, "warning: unable to initialize simple trace backend\n"); - } else { - atexit(st_flush_trace_buffer); - trace_backend_init_events(events); - st_set_trace_file(file); + return false; } + atexit(st_flush_trace_buffer); + trace_backend_init_events(events); + st_set_trace_file(file); return true; }