blob: 800835a9d3b693ea1656917c61a6b1d3e178be17 [file] [log] [blame]
Stefan Hajnoczi26f72272010-05-22 19:24:51 +01001#!/usr/bin/env python
2#
3# Pretty-printer for simple trace backend binary trace files
4#
5# Copyright IBM, Corp. 2010
6#
7# This work is licensed under the terms of the GNU GPL, version 2. See
8# the COPYING file in the top-level directory.
9#
10# For help see docs/tracing.txt
11
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010012import struct
13import re
Stefan Hajnoczi59da6682011-02-22 13:59:41 +000014import inspect
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053015from tracetool import _read_events, Event
16from tracetool.backend.simple import is_string
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010017
18header_event_id = 0xffffffffffffffff
19header_magic = 0xf2b177cb0aa429b4
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +000020dropped_event_id = 0xfffffffffffffffe
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010021
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053022log_header_fmt = '=QQQ'
23rec_header_fmt = '=QQII'
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010024
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053025def read_header(fobj, hfmt):
26 '''Read a trace record header'''
27 hlen = struct.calcsize(hfmt)
28 hdr = fobj.read(hlen)
29 if len(hdr) != hlen:
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010030 return None
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053031 return struct.unpack(hfmt, hdr)
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010032
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053033def get_record(edict, rechdr, fobj):
34 """Deserialize a trace record from a file into a tuple (event_num, timestamp, arg1, ..., arg6)."""
35 if rechdr is None:
36 return None
37 rec = (rechdr[0], rechdr[1])
38 if rechdr[0] != dropped_event_id:
39 event_id = rechdr[0]
40 event = edict[event_id]
41 for type, name in event.args:
42 if is_string(type):
43 l = fobj.read(4)
44 (len,) = struct.unpack('=L', l)
45 s = fobj.read(len)
46 rec = rec + (s,)
47 else:
48 (value,) = struct.unpack('=Q', fobj.read(8))
49 rec = rec + (value,)
50 else:
51 (value,) = struct.unpack('=Q', fobj.read(8))
52 rec = rec + (value,)
53 return rec
54
55
56def read_record(edict, fobj):
57 """Deserialize a trace record from a file into a tuple (event_num, timestamp, arg1, ..., arg6)."""
58 rechdr = read_header(fobj, rec_header_fmt)
59 return get_record(edict, rechdr, fobj) # return tuple of record elements
60
61def read_trace_file(edict, fobj):
Stefan Hajnoczi59da6682011-02-22 13:59:41 +000062 """Deserialize trace records from a file, yielding record tuples (event_num, timestamp, arg1, ..., arg6)."""
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053063 header = read_header(fobj, log_header_fmt)
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010064 if header is None or \
65 header[0] != header_event_id or \
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053066 header[1] != header_magic:
67 raise ValueError('Not a valid trace file!')
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053068
69 log_version = header[2]
Lluís Vilanovaef0bd3b2014-02-23 20:37:35 +010070 if log_version not in [0, 2, 3]:
71 raise ValueError('Unknown version of tracelog format!')
72 if log_version != 3:
73 raise ValueError('Log format %d not supported with this QEMU release!'
74 % log_version)
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010075
76 while True:
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053077 rec = read_record(edict, fobj)
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010078 if rec is None:
79 break
80
81 yield rec
82
Stefan Hajnoczi59da6682011-02-22 13:59:41 +000083class Analyzer(object):
84 """A trace file analyzer which processes trace records.
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010085
Stefan Hajnoczi59da6682011-02-22 13:59:41 +000086 An analyzer can be passed to run() or process(). The begin() method is
87 invoked, then each trace record is processed, and finally the end() method
88 is invoked.
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010089
Stefan Hajnoczi59da6682011-02-22 13:59:41 +000090 If a method matching a trace event name exists, it is invoked to process
91 that trace record. Otherwise the catchall() method is invoked."""
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010092
Stefan Hajnoczi59da6682011-02-22 13:59:41 +000093 def begin(self):
94 """Called at the start of the trace."""
95 pass
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010096
Stefan Hajnoczi59da6682011-02-22 13:59:41 +000097 def catchall(self, event, rec):
98 """Called if no specific method for processing a trace event has been found."""
99 pass
100
101 def end(self):
102 """Called at the end of the trace."""
103 pass
104
105def process(events, log, analyzer):
106 """Invoke an analyzer on each event in a log."""
107 if isinstance(events, str):
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530108 events = _read_events(open(events, 'r'))
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000109 if isinstance(log, str):
110 log = open(log, 'rb')
111
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530112 dropped_event = Event.build("Dropped_Event(uint64_t num_events_dropped)")
113 edict = {dropped_event_id: dropped_event}
114
Lluís Vilanova1dad2ce2014-02-23 20:37:40 +0100115 for num, event in enumerate(events):
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530116 edict[num] = event
117
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000118 def build_fn(analyzer, event):
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530119 if isinstance(event, str):
120 return analyzer.catchall
121
122 fn = getattr(analyzer, event.name, None)
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000123 if fn is None:
124 return analyzer.catchall
125
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530126 event_argcount = len(event.args)
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000127 fn_argcount = len(inspect.getargspec(fn)[0]) - 1
128 if fn_argcount == event_argcount + 1:
129 # Include timestamp as first argument
Stefan Hajnoczid8e8ef42011-08-25 18:03:49 +0100130 return lambda _, rec: fn(*rec[1:2 + event_argcount])
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000131 else:
132 # Just arguments, no timestamp
Stefan Hajnoczid8e8ef42011-08-25 18:03:49 +0100133 return lambda _, rec: fn(*rec[2:2 + event_argcount])
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000134
135 analyzer.begin()
136 fn_cache = {}
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530137 for rec in read_trace_file(edict, log):
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000138 event_num = rec[0]
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530139 event = edict[event_num]
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000140 if event_num not in fn_cache:
141 fn_cache[event_num] = build_fn(analyzer, event)
142 fn_cache[event_num](event, rec)
143 analyzer.end()
144
145def run(analyzer):
146 """Execute an analyzer on a trace file given on the command-line.
147
148 This function is useful as a driver for simple analysis scripts. More
149 advanced scripts will want to call process() instead."""
150 import sys
151
152 if len(sys.argv) != 3:
153 sys.stderr.write('usage: %s <trace-events> <trace-file>\n' % sys.argv[0])
154 sys.exit(1)
155
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530156 events = _read_events(open(sys.argv[1], 'r'))
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000157 process(events, sys.argv[2], analyzer)
158
159if __name__ == '__main__':
160 class Formatter(Analyzer):
161 def __init__(self):
162 self.last_timestamp = None
163
164 def catchall(self, event, rec):
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530165 i = 1
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000166 timestamp = rec[1]
167 if self.last_timestamp is None:
168 self.last_timestamp = timestamp
169 delta_ns = timestamp - self.last_timestamp
170 self.last_timestamp = timestamp
171
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530172 fields = [event.name, '%0.3f' % (delta_ns / 1000.0)]
173 for type, name in event.args:
174 if is_string(type):
175 fields.append('%s=%s' % (name, rec[i + 1]))
176 else:
177 fields.append('%s=0x%x' % (name, rec[i + 1]))
178 i += 1
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000179 print ' '.join(fields)
180
181 run(Formatter())