From 0e2ce4a7ca68628dfcb3ca0bcaba81600cedfca2 Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Fri, 11 Jul 2014 17:18:28 +0200 Subject: [PATCH 40/44] simpletrace: add simpletrace.py --no-header option RH-Author: Stefan Hajnoczi Message-id: <1405099110-10975-6-git-send-email-stefanha@redhat.com> Patchwork-id: 59862 O-Subject: [RHEL6.6 qemu-kvm PATCH 5/7] simpletrace: add simpletrace.py --no-header option Bugzilla: 905761 RH-Acked-by: Laszlo Ersek RH-Acked-by: Miroslav Rezanina RH-Acked-by: Paolo Bonzini 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(-) Signed-off-by: Miroslav Rezanina --- scripts/simpletrace.py | 24 +++++++++++++++++------- 1 files changed, 17 insertions(+), 7 deletions(-) diff --git a/scripts/simpletrace.py b/scripts/simpletrace.py index 2d388ee..5239a5f 100755 --- a/scripts/simpletrace.py +++ b/scripts/simpletrace.py @@ -230,8 +230,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 \ @@ -245,6 +245,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: @@ -274,13 +276,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} @@ -309,7 +314,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: @@ -324,12 +329,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): -- 1.7.1