Skip to contentSkip to conversation

Move simple trace prototypes to header file

PATCH New Stefan Hajnoczi 1 participant 4 messages Jul 22, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 Move simple trace prototypes to header file

Simple trace prototypes should not be in the tracetool code generator. It is easier to modify and debug the code if they are in a regular C header file.

---
This patch is against the tracing branch:
http://repo.or.cz/w/qemu/stefanha.git/shortlog/refs/heads/tracing

 simpletrace.h |   37 +++++++++++++++++++++++++++++++++++++
 tracetool     |   22 +---------------------
 2 files changed, 38 insertions(+), 21 deletions(-)
 create mode 100644 simpletrace.h
simpletrace.h new file +37 -0
new file mode 100644
index 0000000..bec8d62
@@ -0,0 +1,37 @@
+/*
+ * Simple trace backend
+ *
+ * Copyright IBM, Corp. 2010
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef SIMPLETRACE_H
+#define SIMPLETRACE_H
+
+#include <stdbool.h>
+#include <stdio.h>
+
+typedef unsigned int TraceEventID;
+
+typedef struct {
+    const char *tp_name;
+    bool state;
+} TraceEvent;
+
+void trace0(TraceEventID event);
+void trace1(TraceEventID event, unsigned long x1);
+void trace2(TraceEventID event, unsigned long x1, unsigned long x2);
+void trace3(TraceEventID event, unsigned long x1, unsigned long x2, unsigned long x3);
+void trace4(TraceEventID event, unsigned long x1, unsigned long x2, unsigned long x3, unsigned long x4);
+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);
+
+#endif /* SIMPLETRACE_H */
tracetool +1 -21
index ac832af..4890e66 100755
@@ -138,27 +138,7 @@ linetoc_end_nop()
 linetoh_begin_simple()
 {
     cat <<EOF
-#include <stdbool.h>
-
-typedef unsigned int TraceEventID;
-
-typedef struct {
-    const char *tp_name;
-    bool state;
-} TraceEvent;
-
-void trace0(TraceEventID event);
-void trace1(TraceEventID event, unsigned long x1);
-void trace2(TraceEventID event, unsigned long x1, unsigned long x2);
-void trace3(TraceEventID event, unsigned long x1, unsigned long x2, unsigned long x3);
-void trace4(TraceEventID event, unsigned long x1, unsigned long x2, unsigned long x3, unsigned long x4);
-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);
+#include "simpletrace.h"
 EOF
 
     simple_event_num=0
Signature · 1 line
1.7.1
#1 lore raw

PATCH 2/4 Add copyright and doc comments to simpletrace.py

---
 simpletrace.py |   17 +++++++++++++++++
 1 files changed, 17 insertions(+), 0 deletions(-)
simpletrace.py +17 -0
index 2271860..c951023 100755
@@ -1,4 +1,14 @@
 #!/usr/bin/env python
+#
+# Pretty-printer for simple trace backend binary trace files
+#
+# Copyright IBM, Corp. 2010
+#
+# This work is licensed under the terms of the GNU GPL, version 2.  See
+# the COPYING file in the top-level directory.
+#
+# For help see docs/tracing.txt
+
 import sys
 import struct
 import re
@@ -8,7 +18,10 @@ trace_len = struct.calcsize(trace_fmt)
 event_re  = re.compile(r'(disable\s+)?([a-zA-Z0-9_]+)\(([^)]*)\)\s+"([^"]*)"')
 
 def parse_events(fobj):
+    """Parse a trace-events file."""
+
     def get_argnames(args):
+        """Extract argument names from a parameter list."""
         return tuple(arg.split()[-1].lstrip('*') for arg in args.split(','))
 
     events = {}
@@ -27,17 +40,21 @@ def parse_events(fobj):
     return events
 
 def read_record(fobj):
+    """Deserialize a trace record from a file."""
     s = fobj.read(trace_len)
     if len(s) != trace_len:
         return None
     return struct.unpack(trace_fmt, s)
 
 class Formatter(object):
+    """Trace record pretty-printer"""
+
     def __init__(self, events):
         self.events = events
         self.last_timestamp = None
 
     def format_record(self, rec):
+        """Return a string describing a given trace record."""
         if self.last_timestamp is None:
             self.last_timestamp = rec[1]
         delta_ns = rec[1] - self.last_timestamp
Signature · 1 line
1.7.1
#2 lore raw

PATCH 3/4 Add a header to the trace file format

In reply to Stefan Hajnoczi

The trace file format needs a header so that future extension is possible. There is a version number that can be bumped when file format changes are made.

There is still the issue of endianness and sizeof(long). This patch does not address those.

---
 simpletrace.c  |   17 ++++++++++++++++-
 simpletrace.py |   33 ++++++++++++++++++++++++++-------
 2 files changed, 42 insertions(+), 8 deletions(-)
simpletrace.c +16 -1
index 71110b3..df21609 100644
@@ -28,6 +28,17 @@ void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream,
                   getpid(), trace_file_enabled ? "on" : "off");
 }
 
+static bool write_header(FILE *fp)
+{
+    TraceRecord header = {
+        .event = -1UL, /* max avoids conflicting with TraceEventIDs */
+        .timestamp_ns = 0xf2b177cb0aa429b4, /* magic number */
+        .x1 = 0, /* bump this version number if file format changes */
+    };
+
+    return fwrite(&header, sizeof header, 1, fp) == 1;
+}
+
 static bool open_trace_file(void)
 {
     char *filename;
@@ -38,7 +49,11 @@ static bool open_trace_file(void)
 
     trace_fp = fopen(filename, "w");
     free(filename);
-    return trace_fp != NULL;
+    if (!trace_fp) {
+        return false;
+    }
+
+    return write_header(trace_fp);
 }
 
 static void flush_trace_file(void)
simpletrace.py +26 -7
index c951023..16ab2e7 100755
@@ -13,10 +13,18 @@ import sys
 import struct
 import re
 
+header_event_id = 0xffffffffffffffff
+header_magic    = 0xf2b177cb0aa429b4
+header_version  = 0
+
 trace_fmt = 'LLLLLLL'
 trace_len = struct.calcsize(trace_fmt)
 event_re  = re.compile(r'(disable\s+)?([a-zA-Z0-9_]+)\(([^)]*)\)\s+"([^"]*)"')
 
+def err(msg):
+    sys.stderr.write(msg + '\n')
+    sys.exit(1)
+
 def parse_events(fobj):
     """Parse a trace-events file."""
 
@@ -46,6 +54,22 @@ def read_record(fobj):
         return None
     return struct.unpack(trace_fmt, s)
 
+def read_trace_file(fobj):
+    """Deserialize trace records from a file."""
+    header = read_record(fobj)
+    if header is None or \
+       header[0] != header_event_id or \
+       header[1] != header_magic or \
+       header[2] != header_version:
+        err('not a trace file or incompatible version')
+
+    while True:
+        rec = read_record(fobj)
+        if rec is None:
+            break
+
+        yield rec
+
 class Formatter(object):
     """Trace record pretty-printer"""
 
@@ -67,15 +91,10 @@ class Formatter(object):
         return ' '.join(fields)
 
 if len(sys.argv) != 3:
-    sys.stderr.write('usage: %s <trace-events> <trace-file>\n' % sys.argv[0])
-    sys.exit(1)
+    err('usage: %s <trace-events> <trace-file>' % sys.argv[0])
 
 events = parse_events(open(sys.argv[1], 'r'))
 f = open(sys.argv[2], 'rb')
 formatter = Formatter(events)
-while True:
-    rec = read_record(f)
-    if rec is None:
-        break
-
+for rec in read_trace_file(f):
     print formatter.format_record(rec)
Signature · 1 line
1.7.1
#3 lore raw

PATCH 4/4 Disable posix_aio_process_queue by default

In reply to Stefan Hajnoczi
---
 trace-events |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
trace-events +1 -1
index 5304826..a50215f 100644
@@ -63,4 +63,4 @@ disable virtio_blk_handle_write(void *req, unsigned long sector, unsigned long n
 
 # posix-aio-compat.c
 disable paio_submit(void *acb, void *opaque, unsigned long sector_num, unsigned long nb_sectors, unsigned long type) "acb %p opaque %p sector_num %lu nb_sectors %lu type %lu"
-posix_aio_process_queue(void *acb, void *opaque, int type, int ret, uint64_t blocking_duration) "acb %p opaque %p type %d ret %d blocking_duration %lx"
+disable posix_aio_process_queue(void *acb, void *opaque, int type, int ret, uint64_t blocking_duration) "acb %p opaque %p type %d ret %d blocking_duration %lx"
Signature · 1 line
1.7.1