blob: d60b3a08f7d4e8adce67dde8c092e5974b9d685f [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
Daniel P. Berranged1b97bc2016-10-04 14:35:56 +010015from tracetool import read_events, Event
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053016from 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
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +010022record_type_mapping = 0
23record_type_event = 1
24
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053025log_header_fmt = '=QQQ'
26rec_header_fmt = '=QQII'
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010027
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053028def read_header(fobj, hfmt):
29 '''Read a trace record header'''
30 hlen = struct.calcsize(hfmt)
31 hdr = fobj.read(hlen)
32 if len(hdr) != hlen:
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010033 return None
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053034 return struct.unpack(hfmt, hdr)
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010035
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +010036def get_record(edict, idtoname, rechdr, fobj):
37 """Deserialize a trace record from a file into a tuple
38 (name, timestamp, pid, arg1, ..., arg6)."""
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053039 if rechdr is None:
40 return None
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053041 if rechdr[0] != dropped_event_id:
42 event_id = rechdr[0]
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +010043 name = idtoname[event_id]
44 rec = (name, rechdr[1], rechdr[3])
45 event = edict[name]
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053046 for type, name in event.args:
47 if is_string(type):
48 l = fobj.read(4)
49 (len,) = struct.unpack('=L', l)
50 s = fobj.read(len)
51 rec = rec + (s,)
52 else:
53 (value,) = struct.unpack('=Q', fobj.read(8))
54 rec = rec + (value,)
55 else:
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +010056 rec = ("dropped", rechdr[1], rechdr[3])
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053057 (value,) = struct.unpack('=Q', fobj.read(8))
58 rec = rec + (value,)
59 return rec
60
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +010061def get_mapping(fobj):
62 (event_id, ) = struct.unpack('=Q', fobj.read(8))
63 (len, ) = struct.unpack('=L', fobj.read(4))
64 name = fobj.read(len)
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053065
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +010066 return (event_id, name)
67
68def read_record(edict, idtoname, fobj):
Stefan Hajnoczi80ff35c2014-05-07 19:24:11 +020069 """Deserialize a trace record from a file into a tuple (event_num, timestamp, pid, arg1, ..., arg6)."""
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053070 rechdr = read_header(fobj, rec_header_fmt)
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +010071 return get_record(edict, idtoname, rechdr, fobj)
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053072
Stefan Hajnoczi15327c32014-06-22 21:46:06 +080073def read_trace_header(fobj):
74 """Read and verify trace file header"""
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053075 header = read_header(fobj, log_header_fmt)
Daniel P. Berrange25d54652017-01-25 16:14:17 +000076 if header is None:
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053077 raise ValueError('Not a valid trace file!')
Daniel P. Berrange25d54652017-01-25 16:14:17 +000078 if header[0] != header_event_id:
79 raise ValueError('Not a valid trace file, header id %d != %d' %
80 (header[0], header_event_id))
81 if header[1] != header_magic:
82 raise ValueError('Not a valid trace file, header magic %d != %d' %
83 (header[1], header_magic))
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +053084
85 log_version = header[2]
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +010086 if log_version not in [0, 2, 3, 4]:
Lluís Vilanovaef0bd3b2014-02-23 20:37:35 +010087 raise ValueError('Unknown version of tracelog format!')
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +010088 if log_version != 4:
Lluís Vilanovaef0bd3b2014-02-23 20:37:35 +010089 raise ValueError('Log format %d not supported with this QEMU release!'
90 % log_version)
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010091
Stefan Hajnoczi15327c32014-06-22 21:46:06 +080092def read_trace_records(edict, fobj):
93 """Deserialize trace records from a file, yielding record tuples (event_num, timestamp, pid, arg1, ..., arg6)."""
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +010094 idtoname = {
95 dropped_event_id: "dropped"
96 }
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010097 while True:
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +010098 t = fobj.read(8)
99 if len(t) == 0:
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100100 break
101
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +0100102 (rectype, ) = struct.unpack('=Q', t)
103 if rectype == record_type_mapping:
104 event_id, name = get_mapping(fobj)
105 idtoname[event_id] = name
106 else:
107 rec = read_record(edict, idtoname, fobj)
108
109 yield rec
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100110
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000111class Analyzer(object):
112 """A trace file analyzer which processes trace records.
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100113
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000114 An analyzer can be passed to run() or process(). The begin() method is
115 invoked, then each trace record is processed, and finally the end() method
116 is invoked.
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100117
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000118 If a method matching a trace event name exists, it is invoked to process
Stefan Hajnoczi659370f2017-04-11 10:56:54 +0100119 that trace record. Otherwise the catchall() method is invoked.
120
121 Example:
122 The following method handles the runstate_set(int new_state) trace event::
123
124 def runstate_set(self, new_state):
125 ...
126
127 The method can also take a timestamp argument before the trace event
128 arguments::
129
130 def runstate_set(self, timestamp, new_state):
131 ...
132
133 Timestamps have the uint64_t type and are in nanoseconds.
134
135 The pid can be included in addition to the timestamp and is useful when
136 dealing with traces from multiple processes::
137
138 def runstate_set(self, timestamp, pid, new_state):
139 ...
140 """
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100141
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000142 def begin(self):
143 """Called at the start of the trace."""
144 pass
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100145
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000146 def catchall(self, event, rec):
147 """Called if no specific method for processing a trace event has been found."""
148 pass
149
150 def end(self):
151 """Called at the end of the trace."""
152 pass
153
Stefan Hajnoczi15327c32014-06-22 21:46:06 +0800154def process(events, log, analyzer, read_header=True):
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000155 """Invoke an analyzer on each event in a log."""
156 if isinstance(events, str):
Daniel P. Berranged1b97bc2016-10-04 14:35:56 +0100157 events = read_events(open(events, 'r'))
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000158 if isinstance(log, str):
159 log = open(log, 'rb')
160
Stefan Hajnoczi15327c32014-06-22 21:46:06 +0800161 if read_header:
162 read_trace_header(log)
163
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530164 dropped_event = Event.build("Dropped_Event(uint64_t num_events_dropped)")
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +0100165 edict = {"dropped": dropped_event}
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530166
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +0100167 for event in events:
168 edict[event.name] = event
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530169
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000170 def build_fn(analyzer, event):
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530171 if isinstance(event, str):
172 return analyzer.catchall
173
174 fn = getattr(analyzer, event.name, None)
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000175 if fn is None:
176 return analyzer.catchall
177
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530178 event_argcount = len(event.args)
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000179 fn_argcount = len(inspect.getargspec(fn)[0]) - 1
180 if fn_argcount == event_argcount + 1:
181 # Include timestamp as first argument
Stefan Hajnoczi80ff35c2014-05-07 19:24:11 +0200182 return lambda _, rec: fn(*((rec[1:2],) + rec[3:3 + event_argcount]))
183 elif fn_argcount == event_argcount + 2:
184 # Include timestamp and pid
185 return lambda _, rec: fn(*rec[1:3 + event_argcount])
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000186 else:
Stefan Hajnoczi80ff35c2014-05-07 19:24:11 +0200187 # Just arguments, no timestamp or pid
188 return lambda _, rec: fn(*rec[3:3 + event_argcount])
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000189
190 analyzer.begin()
191 fn_cache = {}
Stefan Hajnoczi15327c32014-06-22 21:46:06 +0800192 for rec in read_trace_records(edict, log):
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000193 event_num = rec[0]
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530194 event = edict[event_num]
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000195 if event_num not in fn_cache:
196 fn_cache[event_num] = build_fn(analyzer, event)
197 fn_cache[event_num](event, rec)
198 analyzer.end()
199
200def run(analyzer):
201 """Execute an analyzer on a trace file given on the command-line.
202
203 This function is useful as a driver for simple analysis scripts. More
204 advanced scripts will want to call process() instead."""
205 import sys
206
Stefan Hajnoczi15327c32014-06-22 21:46:06 +0800207 read_header = True
208 if len(sys.argv) == 4 and sys.argv[1] == '--no-header':
209 read_header = False
210 del sys.argv[1]
211 elif len(sys.argv) != 3:
212 sys.stderr.write('usage: %s [--no-header] <trace-events> ' \
213 '<trace-file>\n' % sys.argv[0])
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000214 sys.exit(1)
215
Daniel P. Berranged1b97bc2016-10-04 14:35:56 +0100216 events = read_events(open(sys.argv[1], 'r'))
Stefan Hajnoczi15327c32014-06-22 21:46:06 +0800217 process(events, sys.argv[2], analyzer, read_header=read_header)
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000218
219if __name__ == '__main__':
220 class Formatter(Analyzer):
221 def __init__(self):
222 self.last_timestamp = None
223
224 def catchall(self, event, rec):
225 timestamp = rec[1]
226 if self.last_timestamp is None:
227 self.last_timestamp = timestamp
228 delta_ns = timestamp - self.last_timestamp
229 self.last_timestamp = timestamp
230
Stefan Hajnoczi80ff35c2014-05-07 19:24:11 +0200231 fields = [event.name, '%0.3f' % (delta_ns / 1000.0),
232 'pid=%d' % rec[2]]
233 i = 3
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530234 for type, name in event.args:
235 if is_string(type):
Stefan Hajnoczi80ff35c2014-05-07 19:24:11 +0200236 fields.append('%s=%s' % (name, rec[i]))
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530237 else:
Stefan Hajnoczi80ff35c2014-05-07 19:24:11 +0200238 fields.append('%s=0x%x' % (name, rec[i]))
Harsh Prateek Bora90a147a2012-07-18 15:16:00 +0530239 i += 1
Stefan Hajnoczi59da6682011-02-22 13:59:41 +0000240 print ' '.join(fields)
241
242 run(Formatter())