qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH 1/2][Tracing] Specify trace file name
@ 2010-07-13  8:26 Stefan Hajnoczi
  2010-07-13  8:26 ` [Qemu-devel] [PATCH 2/2][Tracing] Add trace-file command to open/close/flush trace file Stefan Hajnoczi
  0 siblings, 1 reply; 2+ messages in thread
From: Stefan Hajnoczi @ 2010-07-13  8:26 UTC (permalink / raw)
  To: qemu-devel; +Cc: Prerna Saxena

From: Prerna Saxena <prerna@linux.vnet.ibm.com>

Allow users to specify a file for trace-outputs at configuration.
Also, allow trace files to be annotated by <pid> so each qemu instance has
unique traces.

The trace file name can be passed as a config option:
--trace-file=/path/to/file
(Default : /tmp/trace )
At runtime, the pid of the qemu process is appended to the filename so
that mutiple qemu instances do not have overlapping logs.

Eg : /tmp/trace-1234 for qemu launched with pid 1234.

I have yet to test this on windows. getpid() is used at many places
in code(including vnc.c), so I'm hoping this would be okay too.

Edited-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
---
Prerna, I have modified this patch that you wrote and together with patch 2/2
it provides control over the trace file.  The changes include:

 * Set trace_file earlier in ./configure so $trace_file-<pid> displays correctly.
 * Introduce open_trace_file() helper function to handle file naming.

 configure     |   13 +++++++++++++
 simpletrace.c |   16 ++++++++++++++--
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/configure b/configure
index 02bf602..fe1b027 100755
--- a/configure
+++ b/configure
@@ -313,6 +313,7 @@ check_utests="no"
 user_pie="no"
 zero_malloc=""
 trace_backend="nop"
+trace_file="/tmp/trace"
 
 # OS specific
 if check_define __linux__ ; then
@@ -470,6 +471,7 @@ if test "$mingw32" = "yes" ; then
   bindir="\${prefix}"
   sysconfdir="\${prefix}"
   confsuffix=""
+  trace_file="trace"
 fi
 
 # find source path
@@ -517,6 +519,8 @@ for opt do
   ;;
   --trace-backend=*) trace_backend="$optarg"
   ;;
+  --trace-file=*) trace_file="$optarg"
+  ;;
   --enable-gprof) gprof="yes"
   ;;
   --static)
@@ -876,6 +880,9 @@ echo "  --disable-docs           disable documentation build"
 echo "  --disable-vhost-net      disable vhost-net acceleration support"
 echo "  --enable-vhost-net       enable vhost-net acceleration support"
 echo "  --trace-backend=B        Trace backend nop simple ust"
+echo "  --trace-file=NAME        Full PATH,NAME of file to store traces"
+echo "                           Default:/tmp/trace-<pid>"
+echo "                           Default:trace-<pid> on Windows"
 echo ""
 echo "NOTE: The object files are built at the place where configure is launched"
 exit 1
@@ -2132,6 +2139,7 @@ echo "fdatasync         $fdatasync"
 echo "uuid support      $uuid"
 echo "vhost-net support $vhost_net"
 echo "Trace backend     $trace_backend"
+echo "Trace output file $trace_file-<pid>"
 
 if test $sdl_too_old = "yes"; then
 echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -2387,6 +2395,11 @@ fi
 if test "$trace_backend" = "ust"; then
   LIBS="-lust $LIBS"
 fi
+# Set the appropriate trace file.
+if test "$trace_backend" = "simple"; then
+  trace_file="\"$trace_file-%u\""
+fi
+echo "CONFIG_TRACE_FILE=$trace_file" >> $config_host_mak
 echo "TOOLS=$tools" >> $config_host_mak
 echo "ROMS=$roms" >> $config_host_mak
 echo "MAKE=$make" >> $config_host_mak
diff --git a/simpletrace.c b/simpletrace.c
index ed20d36..29fd6a9 100644
--- a/simpletrace.c
+++ b/simpletrace.c
@@ -21,11 +21,23 @@ static TraceRecord trace_buf[TRACE_BUF_LEN];
 static unsigned int trace_idx;
 static FILE *trace_fp;
 
+static bool open_trace_file(void)
+{
+    char *filename;
+
+    if (asprintf(&filename, CONFIG_TRACE_FILE, getpid()) < 0) {
+        return false;
+    }
+
+    trace_fp = fopen(filename, "w");
+    free(filename);
+    return trace_fp != NULL;
+}
+
 static void flush_trace_buffer(void)
 {
     if (!trace_fp) {
-        trace_fp = fopen("/tmp/trace.log", "w");
-        if (trace_fp) {
+        if (open_trace_file()) {
             atexit(flush_trace_buffer);
         }
     }
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [Qemu-devel] [PATCH 2/2][Tracing] Add trace-file command to open/close/flush trace file
  2010-07-13  8:26 [Qemu-devel] [PATCH 1/2][Tracing] Specify trace file name Stefan Hajnoczi
@ 2010-07-13  8:26 ` Stefan Hajnoczi
  0 siblings, 0 replies; 2+ messages in thread
From: Stefan Hajnoczi @ 2010-07-13  8:26 UTC (permalink / raw)
  To: qemu-devel; +Cc: Stefan Hajnoczi, Prerna Saxena

This patch adds the trace-file command:

  trace-file [on|off|flush]

  Open, close, or flush the trace file.  If no argument is given,
  the status of the trace file is displayed.

The trace file is turned on by default but is only written out when the
trace buffer becomes full.  The flush operation can be used to force
write out at any time.

Turning off the trace file does not change the state of trace events;
tracing will continue to the trace buffer.  When the trace file is off,
use "info trace" to display the contents of the trace buffer in memory.

Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
---
 monitor.c       |   17 +++++++++++++++++
 qemu-monitor.hx |   14 ++++++++++++++
 simpletrace.c   |   45 +++++++++++++++++++++++++++++++++++++++++----
 tracetool       |    3 +++
 4 files changed, 75 insertions(+), 4 deletions(-)

diff --git a/monitor.c b/monitor.c
index 090e13c..1e35a6b 100644
--- a/monitor.c
+++ b/monitor.c
@@ -540,6 +540,23 @@ static void do_change_trace_event_state(Monitor *mon, const QDict *qdict)
     bool new_state = qdict_get_bool(qdict, "option");
     change_trace_event_state(tp_name, new_state);
 }
+
+static void do_trace_file(Monitor *mon, const QDict *qdict)
+{
+    const char *op = qdict_get_try_str(qdict, "op");
+
+    if (!op) {
+        st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
+    } else if (!strcmp(op, "on")) {
+        st_set_trace_file_enabled(true);
+    } else if (!strcmp(op, "off")) {
+        st_set_trace_file_enabled(false);
+    } else if (!strcmp(op, "flush")) {
+        st_flush_trace_buffer();
+    } else {
+        monitor_printf(mon, "unexpected argument \"%s\"\n", op);
+    }
+}
 #endif
 
 static void user_monitor_complete(void *opaque, QObject *ret_data)
diff --git a/qemu-monitor.hx b/qemu-monitor.hx
index c5ba3d2..25887bd 100644
--- a/qemu-monitor.hx
+++ b/qemu-monitor.hx
@@ -273,6 +273,20 @@ STEXI
 @findex trace-event
 changes status of a trace event
 ETEXI
+
+    {
+        .name       = "trace-file",
+        .args_type  = "op:s?",
+        .params     = "op [on|off|flush]",
+        .help       = "open, close, or flush trace file",
+        .mhandler.cmd = do_trace_file,
+    },
+
+STEXI
+@item trace-file on|off|flush
+@findex trace-file
+Open, close, or flush the trace file.  If no argument is given, the status of the trace file is displayed.
+ETEXI
 #endif
 
     {
diff --git a/simpletrace.c b/simpletrace.c
index 29fd6a9..71110b3 100644
--- a/simpletrace.c
+++ b/simpletrace.c
@@ -20,6 +20,13 @@ enum {
 static TraceRecord trace_buf[TRACE_BUF_LEN];
 static unsigned int trace_idx;
 static FILE *trace_fp;
+static bool trace_file_enabled = true;
+
+void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...))
+{
+    stream_printf(stream, "Trace file \"" CONFIG_TRACE_FILE "\" %s.\n",
+                  getpid(), trace_file_enabled ? "on" : "off");
+}
 
 static bool open_trace_file(void)
 {
@@ -34,22 +41,52 @@ static bool open_trace_file(void)
     return trace_fp != NULL;
 }
 
-static void flush_trace_buffer(void)
+static void flush_trace_file(void)
 {
+    /* If the trace file is not open yet, open it now */
     if (!trace_fp) {
-        if (open_trace_file()) {
-            atexit(flush_trace_buffer);
+        if (!open_trace_file()) {
+            /* Avoid repeatedly trying to open file on failure */
+            trace_file_enabled = false;
+            return;
         }
+        atexit(st_flush_trace_buffer);
     }
+
     if (trace_fp) {
         size_t unused; /* for when fwrite(3) is declared warn_unused_result */
         unused = fwrite(trace_buf, trace_idx * sizeof(trace_buf[0]), 1, trace_fp);
     }
+}
+
+void st_flush_trace_buffer(void)
+{
+    if (trace_file_enabled) {
+        flush_trace_file();
+    }
 
     /* Discard written trace records */
     trace_idx = 0;
 }
 
+void st_set_trace_file_enabled(bool enable)
+{
+    if (enable == trace_file_enabled) {
+        return; /* no change */
+    }
+
+    /* Flush/discard trace buffer */
+    st_flush_trace_buffer();
+
+    /* To disable, close trace file */
+    if (!enable) {
+        fclose(trace_fp);
+        trace_fp = NULL;
+    }
+
+    trace_file_enabled = enable;
+}
+
 static void trace(TraceEventID event, unsigned long x1,
                   unsigned long x2, unsigned long x3,
                   unsigned long x4, unsigned long x5)
@@ -76,7 +113,7 @@ static void trace(TraceEventID event, unsigned long x1,
     rec->x5 = x5;
 
     if (++trace_idx == TRACE_BUF_LEN) {
-        flush_trace_buffer();
+        st_flush_trace_buffer();
     }
 }
 
diff --git a/tracetool b/tracetool
index 8d8f27c..999ef7e 100755
--- a/tracetool
+++ b/tracetool
@@ -141,6 +141,9 @@ void trace4(TraceEventID event, unsigned long x1, unsigned long x2, unsigned lon
 void trace5(TraceEventID event, unsigned long x1, unsigned long x2, unsigned long x3, unsigned long x4, unsigned long x5);
 void st_print_trace(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...));
 void st_print_trace_events(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...));
+void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...));
+void st_flush_trace_buffer(void);
+void st_set_trace_file_enabled(bool enable);
 void change_trace_event_state(const char *tname, bool tstate);
 EOF
 
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2010-07-13  8:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-07-13  8:26 [Qemu-devel] [PATCH 1/2][Tracing] Specify trace file name Stefan Hajnoczi
2010-07-13  8:26 ` [Qemu-devel] [PATCH 2/2][Tracing] Add trace-file command to open/close/flush trace file Stefan Hajnoczi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).