From patchwork Tue Aug 12 13:37:40 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 379333 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 763CE1400AA for ; Tue, 12 Aug 2014 23:41:22 +1000 (EST) Received: from localhost ([::1]:42078 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XHCKD-0005Ll-Nt for incoming@patchwork.ozlabs.org; Tue, 12 Aug 2014 09:41:17 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39893) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XHCHG-0000Lj-Ti for qemu-devel@nongnu.org; Tue, 12 Aug 2014 09:38:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XHCHA-0007yu-9k for qemu-devel@nongnu.org; Tue, 12 Aug 2014 09:38:14 -0400 Received: from mx1.redhat.com ([209.132.183.28]:32749) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XHCHA-0007yn-3E for qemu-devel@nongnu.org; Tue, 12 Aug 2014 09:38:08 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s7CDc6wg021176 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 12 Aug 2014 09:38:06 -0400 Received: from localhost (ovpn-112-30.ams2.redhat.com [10.36.112.30]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s7CDc4qj009209; Tue, 12 Aug 2014 09:38:05 -0400 From: Stefan Hajnoczi To: Date: Tue, 12 Aug 2014 14:37:40 +0100 Message-Id: <1407850675-11890-4-git-send-email-stefanha@redhat.com> In-Reply-To: <1407850675-11890-1-git-send-email-stefanha@redhat.com> References: <1407850675-11890-1-git-send-email-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Peter Maydell , Stefan Hajnoczi Subject: [Qemu-devel] [PULL 03/18] simpletrace: add simpletrace.py --no-header option 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 It can be useful to read simpletrace files that have no header. For example, a ring buffer may not have a header record but can still be processed if the user is sure the file format version is compatible. $ scripts/simpletrace.py --no-header trace-events trace-file Signed-off-by: Stefan Hajnoczi --- scripts/simpletrace.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/scripts/simpletrace.py b/scripts/simpletrace.py index 1aa9460..3916c6d 100755 --- a/scripts/simpletrace.py +++ b/scripts/simpletrace.py @@ -58,8 +58,8 @@ def read_record(edict, fobj): rechdr = read_header(fobj, rec_header_fmt) return get_record(edict, rechdr, fobj) # return tuple of record elements -def read_trace_file(edict, fobj): - """Deserialize trace records from a file, yielding record tuples (event_num, timestamp, pid, arg1, ..., arg6).""" +def read_trace_header(fobj): + """Read and verify trace file header""" header = read_header(fobj, log_header_fmt) if header is None or \ header[0] != header_event_id or \ @@ -73,6 +73,8 @@ def read_trace_file(edict, fobj): raise ValueError('Log format %d not supported with this QEMU release!' % log_version) +def read_trace_records(edict, fobj): + """Deserialize trace records from a file, yielding record tuples (event_num, timestamp, pid, arg1, ..., arg6).""" while True: rec = read_record(edict, fobj) if rec is None: @@ -102,13 +104,16 @@ class Analyzer(object): """Called at the end of the trace.""" pass -def process(events, log, analyzer): +def process(events, log, analyzer, read_header=True): """Invoke an analyzer on each event in a log.""" if isinstance(events, str): events = _read_events(open(events, 'r')) if isinstance(log, str): log = open(log, 'rb') + if read_header: + read_trace_header(log) + dropped_event = Event.build("Dropped_Event(uint64_t num_events_dropped)") edict = {dropped_event_id: dropped_event} @@ -137,7 +142,7 @@ def process(events, log, analyzer): analyzer.begin() fn_cache = {} - for rec in read_trace_file(edict, log): + for rec in read_trace_records(edict, log): event_num = rec[0] event = edict[event_num] if event_num not in fn_cache: @@ -152,12 +157,17 @@ def run(analyzer): advanced scripts will want to call process() instead.""" import sys - if len(sys.argv) != 3: - sys.stderr.write('usage: %s \n' % sys.argv[0]) + read_header = True + if len(sys.argv) == 4 and sys.argv[1] == '--no-header': + read_header = False + del sys.argv[1] + elif len(sys.argv) != 3: + sys.stderr.write('usage: %s [--no-header] ' \ + '\n' % sys.argv[0]) sys.exit(1) events = _read_events(open(sys.argv[1], 'r')) - process(events, sys.argv[2], analyzer) + process(events, sys.argv[2], analyzer, read_header=read_header) if __name__ == '__main__': class Formatter(Analyzer):