Patchwork [Tracing,3/4] Add a header to the trace file format

login
register
mail settings
Submitter Stefan Hajnoczi
Date July 22, 2010, 3:58 p.m.
Message ID <1279814326-9422-3-git-send-email-stefanha@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/59612/
State New
Headers show

Comments

Stefan Hajnoczi - July 22, 2010, 3:58 p.m.
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.

Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
---
 simpletrace.c  |   17 ++++++++++++++++-
 simpletrace.py |   33 ++++++++++++++++++++++++++-------
 2 files changed, 42 insertions(+), 8 deletions(-)

Patch

diff --git a/simpletrace.c b/simpletrace.c
index 71110b3..df21609 100644
--- a/simpletrace.c
+++ b/simpletrace.c
@@ -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)
diff --git a/simpletrace.py b/simpletrace.py
index c951023..16ab2e7 100755
--- a/simpletrace.py
+++ b/simpletrace.py
@@ -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)