]> git.proxmox.com Git - mirror_qemu.git/blame - scripts/simpletrace.py
Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[mirror_qemu.git] / scripts / simpletrace.py
CommitLineData
26f7227b
SH
1#!/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
26f7227b
SH
12import struct
13import re
59da6684 14import inspect
d1b97bce 15from tracetool import read_events, Event
90a147a2 16from tracetool.backend.simple import is_string
26f7227b
SH
17
18header_event_id = 0xffffffffffffffff
19header_magic = 0xf2b177cb0aa429b4
0b5538c3 20dropped_event_id = 0xfffffffffffffffe
26f7227b 21
7f1b588f
DB
22record_type_mapping = 0
23record_type_event = 1
24
90a147a2
HPB
25log_header_fmt = '=QQQ'
26rec_header_fmt = '=QQII'
26f7227b 27
90a147a2
HPB
28def 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:
33 return None
34 return struct.unpack(hfmt, hdr)
26f7227b 35
7f1b588f
DB
36def get_record(edict, idtoname, rechdr, fobj):
37 """Deserialize a trace record from a file into a tuple
38 (name, timestamp, pid, arg1, ..., arg6)."""
90a147a2 39 if rechdr is None:
26f7227b 40 return None
90a147a2
HPB
41 if rechdr[0] != dropped_event_id:
42 event_id = rechdr[0]
7f1b588f
DB
43 name = idtoname[event_id]
44 rec = (name, rechdr[1], rechdr[3])
45 event = edict[name]
90a147a2
HPB
46 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:
7f1b588f 56 rec = ("dropped", rechdr[1], rechdr[3])
90a147a2
HPB
57 (value,) = struct.unpack('=Q', fobj.read(8))
58 rec = rec + (value,)
59 return rec
60
7f1b588f
DB
61def 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)
90a147a2 65
7f1b588f
DB
66 return (event_id, name)
67
68def read_record(edict, idtoname, fobj):
80ff35cd 69 """Deserialize a trace record from a file into a tuple (event_num, timestamp, pid, arg1, ..., arg6)."""
90a147a2 70 rechdr = read_header(fobj, rec_header_fmt)
7f1b588f 71 return get_record(edict, idtoname, rechdr, fobj)
26f7227b 72
15327c3d
SH
73def read_trace_header(fobj):
74 """Read and verify trace file header"""
90a147a2 75 header = read_header(fobj, log_header_fmt)
25d54654 76 if header is None:
90a147a2 77 raise ValueError('Not a valid trace file!')
25d54654
DB
78 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))
90a147a2
HPB
84
85 log_version = header[2]
7f1b588f 86 if log_version not in [0, 2, 3, 4]:
ef0bd3bb 87 raise ValueError('Unknown version of tracelog format!')
7f1b588f 88 if log_version != 4:
ef0bd3bb
LV
89 raise ValueError('Log format %d not supported with this QEMU release!'
90 % log_version)
26f7227b 91
15327c3d
SH
92def read_trace_records(edict, fobj):
93 """Deserialize trace records from a file, yielding record tuples (event_num, timestamp, pid, arg1, ..., arg6)."""
7f1b588f
DB
94 idtoname = {
95 dropped_event_id: "dropped"
96 }
26f7227b 97 while True:
7f1b588f
DB
98 t = fobj.read(8)
99 if len(t) == 0:
26f7227b
SH
100 break
101
7f1b588f
DB
102 (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
26f7227b 110
59da6684
SH
111class Analyzer(object):
112 """A trace file analyzer which processes trace records.
113
114 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.
117
118 If a method matching a trace event name exists, it is invoked to process
119 that trace record. Otherwise the catchall() method is invoked."""
120
121 def begin(self):
122 """Called at the start of the trace."""
123 pass
124
125 def catchall(self, event, rec):
126 """Called if no specific method for processing a trace event has been found."""
127 pass
128
129 def end(self):
130 """Called at the end of the trace."""
131 pass
132
15327c3d 133def process(events, log, analyzer, read_header=True):
59da6684
SH
134 """Invoke an analyzer on each event in a log."""
135 if isinstance(events, str):
d1b97bce 136 events = read_events(open(events, 'r'))
59da6684
SH
137 if isinstance(log, str):
138 log = open(log, 'rb')
139
15327c3d
SH
140 if read_header:
141 read_trace_header(log)
142
90a147a2 143 dropped_event = Event.build("Dropped_Event(uint64_t num_events_dropped)")
7f1b588f 144 edict = {"dropped": dropped_event}
90a147a2 145
7f1b588f
DB
146 for event in events:
147 edict[event.name] = event
90a147a2 148
59da6684 149 def build_fn(analyzer, event):
90a147a2
HPB
150 if isinstance(event, str):
151 return analyzer.catchall
152
153 fn = getattr(analyzer, event.name, None)
59da6684
SH
154 if fn is None:
155 return analyzer.catchall
156
90a147a2 157 event_argcount = len(event.args)
59da6684
SH
158 fn_argcount = len(inspect.getargspec(fn)[0]) - 1
159 if fn_argcount == event_argcount + 1:
160 # Include timestamp as first argument
80ff35cd
SH
161 return lambda _, rec: fn(*((rec[1:2],) + rec[3:3 + event_argcount]))
162 elif fn_argcount == event_argcount + 2:
163 # Include timestamp and pid
164 return lambda _, rec: fn(*rec[1:3 + event_argcount])
59da6684 165 else:
80ff35cd
SH
166 # Just arguments, no timestamp or pid
167 return lambda _, rec: fn(*rec[3:3 + event_argcount])
59da6684
SH
168
169 analyzer.begin()
170 fn_cache = {}
15327c3d 171 for rec in read_trace_records(edict, log):
59da6684 172 event_num = rec[0]
90a147a2 173 event = edict[event_num]
59da6684
SH
174 if event_num not in fn_cache:
175 fn_cache[event_num] = build_fn(analyzer, event)
176 fn_cache[event_num](event, rec)
177 analyzer.end()
178
179def run(analyzer):
180 """Execute an analyzer on a trace file given on the command-line.
181
182 This function is useful as a driver for simple analysis scripts. More
183 advanced scripts will want to call process() instead."""
184 import sys
185
15327c3d
SH
186 read_header = True
187 if len(sys.argv) == 4 and sys.argv[1] == '--no-header':
188 read_header = False
189 del sys.argv[1]
190 elif len(sys.argv) != 3:
191 sys.stderr.write('usage: %s [--no-header] <trace-events> ' \
192 '<trace-file>\n' % sys.argv[0])
59da6684
SH
193 sys.exit(1)
194
d1b97bce 195 events = read_events(open(sys.argv[1], 'r'))
15327c3d 196 process(events, sys.argv[2], analyzer, read_header=read_header)
59da6684
SH
197
198if __name__ == '__main__':
199 class Formatter(Analyzer):
200 def __init__(self):
201 self.last_timestamp = None
202
203 def catchall(self, event, rec):
204 timestamp = rec[1]
205 if self.last_timestamp is None:
206 self.last_timestamp = timestamp
207 delta_ns = timestamp - self.last_timestamp
208 self.last_timestamp = timestamp
209
80ff35cd
SH
210 fields = [event.name, '%0.3f' % (delta_ns / 1000.0),
211 'pid=%d' % rec[2]]
212 i = 3
90a147a2
HPB
213 for type, name in event.args:
214 if is_string(type):
80ff35cd 215 fields.append('%s=%s' % (name, rec[i]))
90a147a2 216 else:
80ff35cd 217 fields.append('%s=0x%x' % (name, rec[i]))
90a147a2 218 i += 1
59da6684
SH
219 print ' '.join(fields)
220
221 run(Formatter())