qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>,
	Prerna Saxena <prerna@linux.vnet.ibm.com>
Subject: [Qemu-devel] [Tracing] [PATCH 3/4] Add a header to the trace file format
Date: Thu, 22 Jul 2010 16:58:45 +0100	[thread overview]
Message-ID: <1279814326-9422-3-git-send-email-stefanha@linux.vnet.ibm.com> (raw)
In-Reply-To: <1279814326-9422-1-git-send-email-stefanha@linux.vnet.ibm.com>

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(-)

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)
-- 
1.7.1

  parent reply	other threads:[~2010-07-22 15:59 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-22 15:58 [Qemu-devel] [Tracing] [PATCH 1/4] Move simple trace prototypes to header file Stefan Hajnoczi
2010-07-22 15:58 ` [Qemu-devel] [Tracing] [PATCH 2/4] Add copyright and doc comments to simpletrace.py Stefan Hajnoczi
2010-07-22 15:58 ` Stefan Hajnoczi [this message]
2010-07-22 15:58 ` [Qemu-devel] [Tracing] [PATCH 4/4] Disable posix_aio_process_queue by default Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1279814326-9422-3-git-send-email-stefanha@linux.vnet.ibm.com \
    --to=stefanha@linux.vnet.ibm.com \
    --cc=prerna@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).