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