]>
Commit | Line | Data |
---|---|---|
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 | # | |
87e0331c | 10 | # For help see docs/devel/tracing.txt |
26f7227b | 11 | |
f03868bd | 12 | from __future__ import print_function |
26f7227b | 13 | import struct |
59da6684 | 14 | import inspect |
d1b97bce | 15 | from tracetool import read_events, Event |
90a147a2 | 16 | from tracetool.backend.simple import is_string |
26f7227b SH |
17 | |
18 | header_event_id = 0xffffffffffffffff | |
19 | header_magic = 0xf2b177cb0aa429b4 | |
0b5538c3 | 20 | dropped_event_id = 0xfffffffffffffffe |
26f7227b | 21 | |
7f1b588f DB |
22 | record_type_mapping = 0 |
23 | record_type_event = 1 | |
24 | ||
90a147a2 HPB |
25 | log_header_fmt = '=QQQ' |
26 | rec_header_fmt = '=QQII' | |
26f7227b | 27 | |
90a147a2 HPB |
28 | def 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 |
36 | def 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]) | |
249e9f79 JRZ |
45 | try: |
46 | event = edict[name] | |
bd228083 | 47 | except KeyError as e: |
249e9f79 JRZ |
48 | import sys |
49 | sys.stderr.write('%s event is logged but is not declared ' \ | |
50 | 'in the trace events file, try using ' \ | |
51 | 'trace-events-all instead.\n' % str(e)) | |
52 | sys.exit(1) | |
53 | ||
90a147a2 HPB |
54 | for type, name in event.args: |
55 | if is_string(type): | |
56 | l = fobj.read(4) | |
57 | (len,) = struct.unpack('=L', l) | |
58 | s = fobj.read(len) | |
59 | rec = rec + (s,) | |
60 | else: | |
61 | (value,) = struct.unpack('=Q', fobj.read(8)) | |
62 | rec = rec + (value,) | |
63 | else: | |
7f1b588f | 64 | rec = ("dropped", rechdr[1], rechdr[3]) |
90a147a2 HPB |
65 | (value,) = struct.unpack('=Q', fobj.read(8)) |
66 | rec = rec + (value,) | |
67 | return rec | |
68 | ||
7f1b588f DB |
69 | def get_mapping(fobj): |
70 | (event_id, ) = struct.unpack('=Q', fobj.read(8)) | |
71 | (len, ) = struct.unpack('=L', fobj.read(4)) | |
749c1d8e | 72 | name = fobj.read(len).decode() |
90a147a2 | 73 | |
7f1b588f DB |
74 | return (event_id, name) |
75 | ||
76 | def read_record(edict, idtoname, fobj): | |
80ff35cd | 77 | """Deserialize a trace record from a file into a tuple (event_num, timestamp, pid, arg1, ..., arg6).""" |
90a147a2 | 78 | rechdr = read_header(fobj, rec_header_fmt) |
7f1b588f | 79 | return get_record(edict, idtoname, rechdr, fobj) |
26f7227b | 80 | |
15327c3d SH |
81 | def read_trace_header(fobj): |
82 | """Read and verify trace file header""" | |
90a147a2 | 83 | header = read_header(fobj, log_header_fmt) |
25d54654 | 84 | if header is None: |
90a147a2 | 85 | raise ValueError('Not a valid trace file!') |
25d54654 DB |
86 | if header[0] != header_event_id: |
87 | raise ValueError('Not a valid trace file, header id %d != %d' % | |
88 | (header[0], header_event_id)) | |
89 | if header[1] != header_magic: | |
90 | raise ValueError('Not a valid trace file, header magic %d != %d' % | |
91 | (header[1], header_magic)) | |
90a147a2 HPB |
92 | |
93 | log_version = header[2] | |
7f1b588f | 94 | if log_version not in [0, 2, 3, 4]: |
ef0bd3bb | 95 | raise ValueError('Unknown version of tracelog format!') |
7f1b588f | 96 | if log_version != 4: |
ef0bd3bb LV |
97 | raise ValueError('Log format %d not supported with this QEMU release!' |
98 | % log_version) | |
26f7227b | 99 | |
840d8351 SH |
100 | def read_trace_records(edict, idtoname, fobj): |
101 | """Deserialize trace records from a file, yielding record tuples (event_num, timestamp, pid, arg1, ..., arg6). | |
102 | ||
103 | Note that `idtoname` is modified if the file contains mapping records. | |
104 | ||
105 | Args: | |
106 | edict (str -> Event): events dict, indexed by name | |
107 | idtoname (int -> str): event names dict, indexed by event ID | |
108 | fobj (file): input file | |
109 | ||
110 | """ | |
26f7227b | 111 | while True: |
7f1b588f DB |
112 | t = fobj.read(8) |
113 | if len(t) == 0: | |
26f7227b SH |
114 | break |
115 | ||
7f1b588f DB |
116 | (rectype, ) = struct.unpack('=Q', t) |
117 | if rectype == record_type_mapping: | |
118 | event_id, name = get_mapping(fobj) | |
119 | idtoname[event_id] = name | |
120 | else: | |
121 | rec = read_record(edict, idtoname, fobj) | |
122 | ||
123 | yield rec | |
26f7227b | 124 | |
59da6684 SH |
125 | class Analyzer(object): |
126 | """A trace file analyzer which processes trace records. | |
127 | ||
128 | An analyzer can be passed to run() or process(). The begin() method is | |
129 | invoked, then each trace record is processed, and finally the end() method | |
130 | is invoked. | |
131 | ||
132 | If a method matching a trace event name exists, it is invoked to process | |
659370f7 SH |
133 | that trace record. Otherwise the catchall() method is invoked. |
134 | ||
135 | Example: | |
136 | The following method handles the runstate_set(int new_state) trace event:: | |
137 | ||
138 | def runstate_set(self, new_state): | |
139 | ... | |
140 | ||
141 | The method can also take a timestamp argument before the trace event | |
142 | arguments:: | |
143 | ||
144 | def runstate_set(self, timestamp, new_state): | |
145 | ... | |
146 | ||
147 | Timestamps have the uint64_t type and are in nanoseconds. | |
148 | ||
149 | The pid can be included in addition to the timestamp and is useful when | |
150 | dealing with traces from multiple processes:: | |
151 | ||
152 | def runstate_set(self, timestamp, pid, new_state): | |
153 | ... | |
154 | """ | |
59da6684 SH |
155 | |
156 | def begin(self): | |
157 | """Called at the start of the trace.""" | |
158 | pass | |
159 | ||
160 | def catchall(self, event, rec): | |
161 | """Called if no specific method for processing a trace event has been found.""" | |
162 | pass | |
163 | ||
164 | def end(self): | |
165 | """Called at the end of the trace.""" | |
166 | pass | |
167 | ||
15327c3d | 168 | def process(events, log, analyzer, read_header=True): |
59da6684 SH |
169 | """Invoke an analyzer on each event in a log.""" |
170 | if isinstance(events, str): | |
86b5aacf | 171 | events = read_events(open(events, 'r'), events) |
59da6684 SH |
172 | if isinstance(log, str): |
173 | log = open(log, 'rb') | |
174 | ||
15327c3d SH |
175 | if read_header: |
176 | read_trace_header(log) | |
177 | ||
90a147a2 | 178 | dropped_event = Event.build("Dropped_Event(uint64_t num_events_dropped)") |
7f1b588f | 179 | edict = {"dropped": dropped_event} |
840d8351 | 180 | idtoname = {dropped_event_id: "dropped"} |
90a147a2 | 181 | |
7f1b588f DB |
182 | for event in events: |
183 | edict[event.name] = event | |
90a147a2 | 184 | |
840d8351 SH |
185 | # If there is no header assume event ID mapping matches events list |
186 | if not read_header: | |
187 | for event_id, event in enumerate(events): | |
188 | idtoname[event_id] = event.name | |
189 | ||
59da6684 | 190 | def build_fn(analyzer, event): |
90a147a2 HPB |
191 | if isinstance(event, str): |
192 | return analyzer.catchall | |
193 | ||
194 | fn = getattr(analyzer, event.name, None) | |
59da6684 SH |
195 | if fn is None: |
196 | return analyzer.catchall | |
197 | ||
90a147a2 | 198 | event_argcount = len(event.args) |
59da6684 SH |
199 | fn_argcount = len(inspect.getargspec(fn)[0]) - 1 |
200 | if fn_argcount == event_argcount + 1: | |
201 | # Include timestamp as first argument | |
e42860ae | 202 | return lambda _, rec: fn(*(rec[1:2] + rec[3:3 + event_argcount])) |
80ff35cd SH |
203 | elif fn_argcount == event_argcount + 2: |
204 | # Include timestamp and pid | |
205 | return lambda _, rec: fn(*rec[1:3 + event_argcount]) | |
59da6684 | 206 | else: |
80ff35cd SH |
207 | # Just arguments, no timestamp or pid |
208 | return lambda _, rec: fn(*rec[3:3 + event_argcount]) | |
59da6684 SH |
209 | |
210 | analyzer.begin() | |
211 | fn_cache = {} | |
840d8351 | 212 | for rec in read_trace_records(edict, idtoname, log): |
59da6684 | 213 | event_num = rec[0] |
90a147a2 | 214 | event = edict[event_num] |
59da6684 SH |
215 | if event_num not in fn_cache: |
216 | fn_cache[event_num] = build_fn(analyzer, event) | |
217 | fn_cache[event_num](event, rec) | |
218 | analyzer.end() | |
219 | ||
220 | def run(analyzer): | |
221 | """Execute an analyzer on a trace file given on the command-line. | |
222 | ||
223 | This function is useful as a driver for simple analysis scripts. More | |
224 | advanced scripts will want to call process() instead.""" | |
225 | import sys | |
226 | ||
15327c3d SH |
227 | read_header = True |
228 | if len(sys.argv) == 4 and sys.argv[1] == '--no-header': | |
229 | read_header = False | |
230 | del sys.argv[1] | |
231 | elif len(sys.argv) != 3: | |
232 | sys.stderr.write('usage: %s [--no-header] <trace-events> ' \ | |
233 | '<trace-file>\n' % sys.argv[0]) | |
59da6684 SH |
234 | sys.exit(1) |
235 | ||
86b5aacf | 236 | events = read_events(open(sys.argv[1], 'r'), sys.argv[1]) |
15327c3d | 237 | process(events, sys.argv[2], analyzer, read_header=read_header) |
59da6684 SH |
238 | |
239 | if __name__ == '__main__': | |
240 | class Formatter(Analyzer): | |
241 | def __init__(self): | |
242 | self.last_timestamp = None | |
243 | ||
244 | def catchall(self, event, rec): | |
245 | timestamp = rec[1] | |
246 | if self.last_timestamp is None: | |
247 | self.last_timestamp = timestamp | |
248 | delta_ns = timestamp - self.last_timestamp | |
249 | self.last_timestamp = timestamp | |
250 | ||
80ff35cd SH |
251 | fields = [event.name, '%0.3f' % (delta_ns / 1000.0), |
252 | 'pid=%d' % rec[2]] | |
253 | i = 3 | |
90a147a2 HPB |
254 | for type, name in event.args: |
255 | if is_string(type): | |
80ff35cd | 256 | fields.append('%s=%s' % (name, rec[i])) |
90a147a2 | 257 | else: |
80ff35cd | 258 | fields.append('%s=0x%x' % (name, rec[i])) |
90a147a2 | 259 | i += 1 |
f03868bd | 260 | print(' '.join(fields)) |
59da6684 SH |
261 | |
262 | run(Formatter()) |