2020-01-30 17:32:25 +01:00
|
|
|
#!/usr/bin/env python3
|
2010-05-22 20:24:51 +02:00
|
|
|
#
|
|
|
|
# Pretty-printer for simple trace backend binary trace files
|
|
|
|
#
|
|
|
|
# Copyright IBM, Corp. 2010
|
|
|
|
#
|
|
|
|
# This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
# the COPYING file in the top-level directory.
|
|
|
|
#
|
2017-07-29 00:46:05 +02:00
|
|
|
# For help see docs/devel/tracing.txt
|
2010-05-22 20:24:51 +02:00
|
|
|
|
|
|
|
import struct
|
simpletrace: Make simpletrace.py a Python module
The simpletrace.py script pretty-prints a binary trace file. Most of
the code can be reused by trace file analysis scripts, so turn it into a
module.
Here is an example script that uses the new simpletrace module:
#!/usr/bin/env python
# Print virtqueue elements that were never returned to the guest.
import simpletrace
class VirtqueueRequestTracker(simpletrace.Analyzer):
def __init__(self):
self.elems = set()
def virtqueue_pop(self, vq, elem, in_num, out_num):
self.elems.add(elem)
def virtqueue_fill(self, vq, elem, length, idx):
self.elems.remove(elem)
def end(self):
for elem in self.elems:
print hex(elem)
simpletrace.run(VirtqueueRequestTracker())
The simpletrace API is based around the Analyzer class. Users implement
an analyzer subclass and add methods for trace events they want to
process. A catchall() method is invoked for trace events which do not
have dedicated methods. Finally, there are also begin() and end()
methods like in sed that can be used to perform setup or print
statistics at the end.
A binary trace file is processed either with:
simpletrace.run(analyzer) # uses command-line args
or with:
simpletrace.process('path/to/trace-events',
'path/to/trace-file',
analyzer)
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-02-22 14:59:41 +01:00
|
|
|
import inspect
|
2016-10-04 15:35:56 +02:00
|
|
|
from tracetool import read_events, Event
|
2012-07-18 11:46:00 +02:00
|
|
|
from tracetool.backend.simple import is_string
|
2010-05-22 20:24:51 +02:00
|
|
|
|
|
|
|
header_event_id = 0xffffffffffffffff
|
|
|
|
header_magic = 0xf2b177cb0aa429b4
|
2011-02-26 19:38:39 +01:00
|
|
|
dropped_event_id = 0xfffffffffffffffe
|
2010-05-22 20:24:51 +02:00
|
|
|
|
2016-10-04 15:35:50 +02:00
|
|
|
record_type_mapping = 0
|
|
|
|
record_type_event = 1
|
|
|
|
|
2012-07-18 11:46:00 +02:00
|
|
|
log_header_fmt = '=QQQ'
|
|
|
|
rec_header_fmt = '=QQII'
|
2010-05-22 20:24:51 +02:00
|
|
|
|
2012-07-18 11:46:00 +02:00
|
|
|
def read_header(fobj, hfmt):
|
|
|
|
'''Read a trace record header'''
|
|
|
|
hlen = struct.calcsize(hfmt)
|
|
|
|
hdr = fobj.read(hlen)
|
|
|
|
if len(hdr) != hlen:
|
|
|
|
return None
|
|
|
|
return struct.unpack(hfmt, hdr)
|
2010-05-22 20:24:51 +02:00
|
|
|
|
2016-10-04 15:35:50 +02:00
|
|
|
def get_record(edict, idtoname, rechdr, fobj):
|
|
|
|
"""Deserialize a trace record from a file into a tuple
|
|
|
|
(name, timestamp, pid, arg1, ..., arg6)."""
|
2012-07-18 11:46:00 +02:00
|
|
|
if rechdr is None:
|
2010-05-22 20:24:51 +02:00
|
|
|
return None
|
2012-07-18 11:46:00 +02:00
|
|
|
if rechdr[0] != dropped_event_id:
|
|
|
|
event_id = rechdr[0]
|
2016-10-04 15:35:50 +02:00
|
|
|
name = idtoname[event_id]
|
|
|
|
rec = (name, rechdr[1], rechdr[3])
|
simpletrace: Improve the error message if event is not declared
Today, if we use a trace-event file which does not declare an event
existing in the log file we'll get the following error:
$ scripts/simpletrace.py trace-events trace-68508
Traceback (most recent call last):
File "scripts/simpletrace.py", line 242, in <module>
run(Formatter())
File "scripts/simpletrace.py", line 217, in run
process(events, sys.argv[2], analyzer, read_header=read_header)
File "scripts/simpletrace.py", line 192, in process
for rec in read_trace_records(edict, log):
File "scripts/simpletrace.py", line 107, in read_trace_records
rec = read_record(edict, idtoname, fobj)
File "scripts/simpletrace.py", line 71, in read_record
return get_record(edict, idtoname, rechdr, fobj)
File "scripts/simpletrace.py", line 45, in get_record
event = edict[name]
KeyError: 'qemu_mutex_locked'
This patch improves this error by adding a hint instead of just that
KeyError log:
$ scripts/simpletrace.py trace-events trace-68508
'qemu_mutex_locked' event is logged but is not declared in the trace
events file, try using trace-events-all instead.
Signed-off-by: Jose Ricardo Ziviani <joserz@linux.vnet.ibm.com>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Message-id: 1496075404-8845-1-git-send-email-joserz@linux.vnet.ibm.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2017-05-29 18:30:04 +02:00
|
|
|
try:
|
|
|
|
event = edict[name]
|
2018-06-08 14:29:51 +02:00
|
|
|
except KeyError as e:
|
simpletrace: Improve the error message if event is not declared
Today, if we use a trace-event file which does not declare an event
existing in the log file we'll get the following error:
$ scripts/simpletrace.py trace-events trace-68508
Traceback (most recent call last):
File "scripts/simpletrace.py", line 242, in <module>
run(Formatter())
File "scripts/simpletrace.py", line 217, in run
process(events, sys.argv[2], analyzer, read_header=read_header)
File "scripts/simpletrace.py", line 192, in process
for rec in read_trace_records(edict, log):
File "scripts/simpletrace.py", line 107, in read_trace_records
rec = read_record(edict, idtoname, fobj)
File "scripts/simpletrace.py", line 71, in read_record
return get_record(edict, idtoname, rechdr, fobj)
File "scripts/simpletrace.py", line 45, in get_record
event = edict[name]
KeyError: 'qemu_mutex_locked'
This patch improves this error by adding a hint instead of just that
KeyError log:
$ scripts/simpletrace.py trace-events trace-68508
'qemu_mutex_locked' event is logged but is not declared in the trace
events file, try using trace-events-all instead.
Signed-off-by: Jose Ricardo Ziviani <joserz@linux.vnet.ibm.com>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Message-id: 1496075404-8845-1-git-send-email-joserz@linux.vnet.ibm.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2017-05-29 18:30:04 +02:00
|
|
|
import sys
|
|
|
|
sys.stderr.write('%s event is logged but is not declared ' \
|
|
|
|
'in the trace events file, try using ' \
|
|
|
|
'trace-events-all instead.\n' % str(e))
|
|
|
|
sys.exit(1)
|
|
|
|
|
2012-07-18 11:46:00 +02:00
|
|
|
for type, name in event.args:
|
|
|
|
if is_string(type):
|
|
|
|
l = fobj.read(4)
|
|
|
|
(len,) = struct.unpack('=L', l)
|
|
|
|
s = fobj.read(len)
|
|
|
|
rec = rec + (s,)
|
|
|
|
else:
|
|
|
|
(value,) = struct.unpack('=Q', fobj.read(8))
|
|
|
|
rec = rec + (value,)
|
|
|
|
else:
|
2016-10-04 15:35:50 +02:00
|
|
|
rec = ("dropped", rechdr[1], rechdr[3])
|
2012-07-18 11:46:00 +02:00
|
|
|
(value,) = struct.unpack('=Q', fobj.read(8))
|
|
|
|
rec = rec + (value,)
|
|
|
|
return rec
|
|
|
|
|
2016-10-04 15:35:50 +02:00
|
|
|
def get_mapping(fobj):
|
|
|
|
(event_id, ) = struct.unpack('=Q', fobj.read(8))
|
|
|
|
(len, ) = struct.unpack('=L', fobj.read(4))
|
2018-06-19 21:45:49 +02:00
|
|
|
name = fobj.read(len).decode()
|
2012-07-18 11:46:00 +02:00
|
|
|
|
2016-10-04 15:35:50 +02:00
|
|
|
return (event_id, name)
|
|
|
|
|
|
|
|
def read_record(edict, idtoname, fobj):
|
simpletrace: add support for trace record pid field
Extract the pid field from the trace record and print it.
Change the trace record tuple from:
(event_num, timestamp, arg1, ..., arg6)
to:
(event_num, timestamp, pid, arg1, ..., arg6)
Trace event methods now support 3 prototypes:
1. <event-name>(arg1, arg2, arg3)
2. <event-name>(timestamp, arg1, arg2, arg3)
3. <event-name>(timestamp, pid, arg1, arg2, arg3)
Existing script continue to work without changes, they only know about
prototypes 1 and 2.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2014-05-07 19:24:11 +02:00
|
|
|
"""Deserialize a trace record from a file into a tuple (event_num, timestamp, pid, arg1, ..., arg6)."""
|
2012-07-18 11:46:00 +02:00
|
|
|
rechdr = read_header(fobj, rec_header_fmt)
|
2016-10-04 15:35:50 +02:00
|
|
|
return get_record(edict, idtoname, rechdr, fobj)
|
2010-05-22 20:24:51 +02:00
|
|
|
|
2014-06-22 15:46:06 +02:00
|
|
|
def read_trace_header(fobj):
|
|
|
|
"""Read and verify trace file header"""
|
2012-07-18 11:46:00 +02:00
|
|
|
header = read_header(fobj, log_header_fmt)
|
2017-01-25 17:14:17 +01:00
|
|
|
if header is None:
|
2012-07-18 11:46:00 +02:00
|
|
|
raise ValueError('Not a valid trace file!')
|
2017-01-25 17:14:17 +01:00
|
|
|
if header[0] != header_event_id:
|
|
|
|
raise ValueError('Not a valid trace file, header id %d != %d' %
|
|
|
|
(header[0], header_event_id))
|
|
|
|
if header[1] != header_magic:
|
|
|
|
raise ValueError('Not a valid trace file, header magic %d != %d' %
|
|
|
|
(header[1], header_magic))
|
2012-07-18 11:46:00 +02:00
|
|
|
|
|
|
|
log_version = header[2]
|
2016-10-04 15:35:50 +02:00
|
|
|
if log_version not in [0, 2, 3, 4]:
|
2014-02-23 20:37:35 +01:00
|
|
|
raise ValueError('Unknown version of tracelog format!')
|
2016-10-04 15:35:50 +02:00
|
|
|
if log_version != 4:
|
2014-02-23 20:37:35 +01:00
|
|
|
raise ValueError('Log format %d not supported with this QEMU release!'
|
|
|
|
% log_version)
|
2010-05-22 20:24:51 +02:00
|
|
|
|
2017-08-15 10:44:30 +02:00
|
|
|
def read_trace_records(edict, idtoname, fobj):
|
|
|
|
"""Deserialize trace records from a file, yielding record tuples (event_num, timestamp, pid, arg1, ..., arg6).
|
|
|
|
|
|
|
|
Note that `idtoname` is modified if the file contains mapping records.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
edict (str -> Event): events dict, indexed by name
|
|
|
|
idtoname (int -> str): event names dict, indexed by event ID
|
|
|
|
fobj (file): input file
|
|
|
|
|
|
|
|
"""
|
2010-05-22 20:24:51 +02:00
|
|
|
while True:
|
2016-10-04 15:35:50 +02:00
|
|
|
t = fobj.read(8)
|
|
|
|
if len(t) == 0:
|
2010-05-22 20:24:51 +02:00
|
|
|
break
|
|
|
|
|
2016-10-04 15:35:50 +02:00
|
|
|
(rectype, ) = struct.unpack('=Q', t)
|
|
|
|
if rectype == record_type_mapping:
|
|
|
|
event_id, name = get_mapping(fobj)
|
|
|
|
idtoname[event_id] = name
|
|
|
|
else:
|
|
|
|
rec = read_record(edict, idtoname, fobj)
|
|
|
|
|
|
|
|
yield rec
|
2010-05-22 20:24:51 +02:00
|
|
|
|
simpletrace: Make simpletrace.py a Python module
The simpletrace.py script pretty-prints a binary trace file. Most of
the code can be reused by trace file analysis scripts, so turn it into a
module.
Here is an example script that uses the new simpletrace module:
#!/usr/bin/env python
# Print virtqueue elements that were never returned to the guest.
import simpletrace
class VirtqueueRequestTracker(simpletrace.Analyzer):
def __init__(self):
self.elems = set()
def virtqueue_pop(self, vq, elem, in_num, out_num):
self.elems.add(elem)
def virtqueue_fill(self, vq, elem, length, idx):
self.elems.remove(elem)
def end(self):
for elem in self.elems:
print hex(elem)
simpletrace.run(VirtqueueRequestTracker())
The simpletrace API is based around the Analyzer class. Users implement
an analyzer subclass and add methods for trace events they want to
process. A catchall() method is invoked for trace events which do not
have dedicated methods. Finally, there are also begin() and end()
methods like in sed that can be used to perform setup or print
statistics at the end.
A binary trace file is processed either with:
simpletrace.run(analyzer) # uses command-line args
or with:
simpletrace.process('path/to/trace-events',
'path/to/trace-file',
analyzer)
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-02-22 14:59:41 +01:00
|
|
|
class Analyzer(object):
|
|
|
|
"""A trace file analyzer which processes trace records.
|
|
|
|
|
|
|
|
An analyzer can be passed to run() or process(). The begin() method is
|
|
|
|
invoked, then each trace record is processed, and finally the end() method
|
|
|
|
is invoked.
|
|
|
|
|
|
|
|
If a method matching a trace event name exists, it is invoked to process
|
2017-04-11 11:56:54 +02:00
|
|
|
that trace record. Otherwise the catchall() method is invoked.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
The following method handles the runstate_set(int new_state) trace event::
|
|
|
|
|
|
|
|
def runstate_set(self, new_state):
|
|
|
|
...
|
|
|
|
|
|
|
|
The method can also take a timestamp argument before the trace event
|
|
|
|
arguments::
|
|
|
|
|
|
|
|
def runstate_set(self, timestamp, new_state):
|
|
|
|
...
|
|
|
|
|
|
|
|
Timestamps have the uint64_t type and are in nanoseconds.
|
|
|
|
|
|
|
|
The pid can be included in addition to the timestamp and is useful when
|
|
|
|
dealing with traces from multiple processes::
|
|
|
|
|
|
|
|
def runstate_set(self, timestamp, pid, new_state):
|
|
|
|
...
|
|
|
|
"""
|
simpletrace: Make simpletrace.py a Python module
The simpletrace.py script pretty-prints a binary trace file. Most of
the code can be reused by trace file analysis scripts, so turn it into a
module.
Here is an example script that uses the new simpletrace module:
#!/usr/bin/env python
# Print virtqueue elements that were never returned to the guest.
import simpletrace
class VirtqueueRequestTracker(simpletrace.Analyzer):
def __init__(self):
self.elems = set()
def virtqueue_pop(self, vq, elem, in_num, out_num):
self.elems.add(elem)
def virtqueue_fill(self, vq, elem, length, idx):
self.elems.remove(elem)
def end(self):
for elem in self.elems:
print hex(elem)
simpletrace.run(VirtqueueRequestTracker())
The simpletrace API is based around the Analyzer class. Users implement
an analyzer subclass and add methods for trace events they want to
process. A catchall() method is invoked for trace events which do not
have dedicated methods. Finally, there are also begin() and end()
methods like in sed that can be used to perform setup or print
statistics at the end.
A binary trace file is processed either with:
simpletrace.run(analyzer) # uses command-line args
or with:
simpletrace.process('path/to/trace-events',
'path/to/trace-file',
analyzer)
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-02-22 14:59:41 +01:00
|
|
|
|
|
|
|
def begin(self):
|
|
|
|
"""Called at the start of the trace."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def catchall(self, event, rec):
|
|
|
|
"""Called if no specific method for processing a trace event has been found."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def end(self):
|
|
|
|
"""Called at the end of the trace."""
|
|
|
|
pass
|
|
|
|
|
2014-06-22 15:46:06 +02:00
|
|
|
def process(events, log, analyzer, read_header=True):
|
simpletrace: Make simpletrace.py a Python module
The simpletrace.py script pretty-prints a binary trace file. Most of
the code can be reused by trace file analysis scripts, so turn it into a
module.
Here is an example script that uses the new simpletrace module:
#!/usr/bin/env python
# Print virtqueue elements that were never returned to the guest.
import simpletrace
class VirtqueueRequestTracker(simpletrace.Analyzer):
def __init__(self):
self.elems = set()
def virtqueue_pop(self, vq, elem, in_num, out_num):
self.elems.add(elem)
def virtqueue_fill(self, vq, elem, length, idx):
self.elems.remove(elem)
def end(self):
for elem in self.elems:
print hex(elem)
simpletrace.run(VirtqueueRequestTracker())
The simpletrace API is based around the Analyzer class. Users implement
an analyzer subclass and add methods for trace events they want to
process. A catchall() method is invoked for trace events which do not
have dedicated methods. Finally, there are also begin() and end()
methods like in sed that can be used to perform setup or print
statistics at the end.
A binary trace file is processed either with:
simpletrace.run(analyzer) # uses command-line args
or with:
simpletrace.process('path/to/trace-events',
'path/to/trace-file',
analyzer)
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-02-22 14:59:41 +01:00
|
|
|
"""Invoke an analyzer on each event in a log."""
|
|
|
|
if isinstance(events, str):
|
2018-03-06 16:46:50 +01:00
|
|
|
events = read_events(open(events, 'r'), events)
|
simpletrace: Make simpletrace.py a Python module
The simpletrace.py script pretty-prints a binary trace file. Most of
the code can be reused by trace file analysis scripts, so turn it into a
module.
Here is an example script that uses the new simpletrace module:
#!/usr/bin/env python
# Print virtqueue elements that were never returned to the guest.
import simpletrace
class VirtqueueRequestTracker(simpletrace.Analyzer):
def __init__(self):
self.elems = set()
def virtqueue_pop(self, vq, elem, in_num, out_num):
self.elems.add(elem)
def virtqueue_fill(self, vq, elem, length, idx):
self.elems.remove(elem)
def end(self):
for elem in self.elems:
print hex(elem)
simpletrace.run(VirtqueueRequestTracker())
The simpletrace API is based around the Analyzer class. Users implement
an analyzer subclass and add methods for trace events they want to
process. A catchall() method is invoked for trace events which do not
have dedicated methods. Finally, there are also begin() and end()
methods like in sed that can be used to perform setup or print
statistics at the end.
A binary trace file is processed either with:
simpletrace.run(analyzer) # uses command-line args
or with:
simpletrace.process('path/to/trace-events',
'path/to/trace-file',
analyzer)
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-02-22 14:59:41 +01:00
|
|
|
if isinstance(log, str):
|
|
|
|
log = open(log, 'rb')
|
|
|
|
|
2014-06-22 15:46:06 +02:00
|
|
|
if read_header:
|
|
|
|
read_trace_header(log)
|
|
|
|
|
2012-07-18 11:46:00 +02:00
|
|
|
dropped_event = Event.build("Dropped_Event(uint64_t num_events_dropped)")
|
2016-10-04 15:35:50 +02:00
|
|
|
edict = {"dropped": dropped_event}
|
2017-08-15 10:44:30 +02:00
|
|
|
idtoname = {dropped_event_id: "dropped"}
|
2012-07-18 11:46:00 +02:00
|
|
|
|
2016-10-04 15:35:50 +02:00
|
|
|
for event in events:
|
|
|
|
edict[event.name] = event
|
2012-07-18 11:46:00 +02:00
|
|
|
|
2017-08-15 10:44:30 +02:00
|
|
|
# If there is no header assume event ID mapping matches events list
|
|
|
|
if not read_header:
|
|
|
|
for event_id, event in enumerate(events):
|
|
|
|
idtoname[event_id] = event.name
|
|
|
|
|
simpletrace: Make simpletrace.py a Python module
The simpletrace.py script pretty-prints a binary trace file. Most of
the code can be reused by trace file analysis scripts, so turn it into a
module.
Here is an example script that uses the new simpletrace module:
#!/usr/bin/env python
# Print virtqueue elements that were never returned to the guest.
import simpletrace
class VirtqueueRequestTracker(simpletrace.Analyzer):
def __init__(self):
self.elems = set()
def virtqueue_pop(self, vq, elem, in_num, out_num):
self.elems.add(elem)
def virtqueue_fill(self, vq, elem, length, idx):
self.elems.remove(elem)
def end(self):
for elem in self.elems:
print hex(elem)
simpletrace.run(VirtqueueRequestTracker())
The simpletrace API is based around the Analyzer class. Users implement
an analyzer subclass and add methods for trace events they want to
process. A catchall() method is invoked for trace events which do not
have dedicated methods. Finally, there are also begin() and end()
methods like in sed that can be used to perform setup or print
statistics at the end.
A binary trace file is processed either with:
simpletrace.run(analyzer) # uses command-line args
or with:
simpletrace.process('path/to/trace-events',
'path/to/trace-file',
analyzer)
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-02-22 14:59:41 +01:00
|
|
|
def build_fn(analyzer, event):
|
2012-07-18 11:46:00 +02:00
|
|
|
if isinstance(event, str):
|
|
|
|
return analyzer.catchall
|
|
|
|
|
|
|
|
fn = getattr(analyzer, event.name, None)
|
simpletrace: Make simpletrace.py a Python module
The simpletrace.py script pretty-prints a binary trace file. Most of
the code can be reused by trace file analysis scripts, so turn it into a
module.
Here is an example script that uses the new simpletrace module:
#!/usr/bin/env python
# Print virtqueue elements that were never returned to the guest.
import simpletrace
class VirtqueueRequestTracker(simpletrace.Analyzer):
def __init__(self):
self.elems = set()
def virtqueue_pop(self, vq, elem, in_num, out_num):
self.elems.add(elem)
def virtqueue_fill(self, vq, elem, length, idx):
self.elems.remove(elem)
def end(self):
for elem in self.elems:
print hex(elem)
simpletrace.run(VirtqueueRequestTracker())
The simpletrace API is based around the Analyzer class. Users implement
an analyzer subclass and add methods for trace events they want to
process. A catchall() method is invoked for trace events which do not
have dedicated methods. Finally, there are also begin() and end()
methods like in sed that can be used to perform setup or print
statistics at the end.
A binary trace file is processed either with:
simpletrace.run(analyzer) # uses command-line args
or with:
simpletrace.process('path/to/trace-events',
'path/to/trace-file',
analyzer)
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-02-22 14:59:41 +01:00
|
|
|
if fn is None:
|
|
|
|
return analyzer.catchall
|
|
|
|
|
2012-07-18 11:46:00 +02:00
|
|
|
event_argcount = len(event.args)
|
simpletrace: Make simpletrace.py a Python module
The simpletrace.py script pretty-prints a binary trace file. Most of
the code can be reused by trace file analysis scripts, so turn it into a
module.
Here is an example script that uses the new simpletrace module:
#!/usr/bin/env python
# Print virtqueue elements that were never returned to the guest.
import simpletrace
class VirtqueueRequestTracker(simpletrace.Analyzer):
def __init__(self):
self.elems = set()
def virtqueue_pop(self, vq, elem, in_num, out_num):
self.elems.add(elem)
def virtqueue_fill(self, vq, elem, length, idx):
self.elems.remove(elem)
def end(self):
for elem in self.elems:
print hex(elem)
simpletrace.run(VirtqueueRequestTracker())
The simpletrace API is based around the Analyzer class. Users implement
an analyzer subclass and add methods for trace events they want to
process. A catchall() method is invoked for trace events which do not
have dedicated methods. Finally, there are also begin() and end()
methods like in sed that can be used to perform setup or print
statistics at the end.
A binary trace file is processed either with:
simpletrace.run(analyzer) # uses command-line args
or with:
simpletrace.process('path/to/trace-events',
'path/to/trace-file',
analyzer)
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-02-22 14:59:41 +01:00
|
|
|
fn_argcount = len(inspect.getargspec(fn)[0]) - 1
|
|
|
|
if fn_argcount == event_argcount + 1:
|
|
|
|
# Include timestamp as first argument
|
2018-02-22 17:39:01 +01:00
|
|
|
return lambda _, rec: fn(*(rec[1:2] + rec[3:3 + event_argcount]))
|
simpletrace: add support for trace record pid field
Extract the pid field from the trace record and print it.
Change the trace record tuple from:
(event_num, timestamp, arg1, ..., arg6)
to:
(event_num, timestamp, pid, arg1, ..., arg6)
Trace event methods now support 3 prototypes:
1. <event-name>(arg1, arg2, arg3)
2. <event-name>(timestamp, arg1, arg2, arg3)
3. <event-name>(timestamp, pid, arg1, arg2, arg3)
Existing script continue to work without changes, they only know about
prototypes 1 and 2.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2014-05-07 19:24:11 +02:00
|
|
|
elif fn_argcount == event_argcount + 2:
|
|
|
|
# Include timestamp and pid
|
|
|
|
return lambda _, rec: fn(*rec[1:3 + event_argcount])
|
simpletrace: Make simpletrace.py a Python module
The simpletrace.py script pretty-prints a binary trace file. Most of
the code can be reused by trace file analysis scripts, so turn it into a
module.
Here is an example script that uses the new simpletrace module:
#!/usr/bin/env python
# Print virtqueue elements that were never returned to the guest.
import simpletrace
class VirtqueueRequestTracker(simpletrace.Analyzer):
def __init__(self):
self.elems = set()
def virtqueue_pop(self, vq, elem, in_num, out_num):
self.elems.add(elem)
def virtqueue_fill(self, vq, elem, length, idx):
self.elems.remove(elem)
def end(self):
for elem in self.elems:
print hex(elem)
simpletrace.run(VirtqueueRequestTracker())
The simpletrace API is based around the Analyzer class. Users implement
an analyzer subclass and add methods for trace events they want to
process. A catchall() method is invoked for trace events which do not
have dedicated methods. Finally, there are also begin() and end()
methods like in sed that can be used to perform setup or print
statistics at the end.
A binary trace file is processed either with:
simpletrace.run(analyzer) # uses command-line args
or with:
simpletrace.process('path/to/trace-events',
'path/to/trace-file',
analyzer)
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-02-22 14:59:41 +01:00
|
|
|
else:
|
simpletrace: add support for trace record pid field
Extract the pid field from the trace record and print it.
Change the trace record tuple from:
(event_num, timestamp, arg1, ..., arg6)
to:
(event_num, timestamp, pid, arg1, ..., arg6)
Trace event methods now support 3 prototypes:
1. <event-name>(arg1, arg2, arg3)
2. <event-name>(timestamp, arg1, arg2, arg3)
3. <event-name>(timestamp, pid, arg1, arg2, arg3)
Existing script continue to work without changes, they only know about
prototypes 1 and 2.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2014-05-07 19:24:11 +02:00
|
|
|
# Just arguments, no timestamp or pid
|
|
|
|
return lambda _, rec: fn(*rec[3:3 + event_argcount])
|
simpletrace: Make simpletrace.py a Python module
The simpletrace.py script pretty-prints a binary trace file. Most of
the code can be reused by trace file analysis scripts, so turn it into a
module.
Here is an example script that uses the new simpletrace module:
#!/usr/bin/env python
# Print virtqueue elements that were never returned to the guest.
import simpletrace
class VirtqueueRequestTracker(simpletrace.Analyzer):
def __init__(self):
self.elems = set()
def virtqueue_pop(self, vq, elem, in_num, out_num):
self.elems.add(elem)
def virtqueue_fill(self, vq, elem, length, idx):
self.elems.remove(elem)
def end(self):
for elem in self.elems:
print hex(elem)
simpletrace.run(VirtqueueRequestTracker())
The simpletrace API is based around the Analyzer class. Users implement
an analyzer subclass and add methods for trace events they want to
process. A catchall() method is invoked for trace events which do not
have dedicated methods. Finally, there are also begin() and end()
methods like in sed that can be used to perform setup or print
statistics at the end.
A binary trace file is processed either with:
simpletrace.run(analyzer) # uses command-line args
or with:
simpletrace.process('path/to/trace-events',
'path/to/trace-file',
analyzer)
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-02-22 14:59:41 +01:00
|
|
|
|
|
|
|
analyzer.begin()
|
|
|
|
fn_cache = {}
|
2017-08-15 10:44:30 +02:00
|
|
|
for rec in read_trace_records(edict, idtoname, log):
|
simpletrace: Make simpletrace.py a Python module
The simpletrace.py script pretty-prints a binary trace file. Most of
the code can be reused by trace file analysis scripts, so turn it into a
module.
Here is an example script that uses the new simpletrace module:
#!/usr/bin/env python
# Print virtqueue elements that were never returned to the guest.
import simpletrace
class VirtqueueRequestTracker(simpletrace.Analyzer):
def __init__(self):
self.elems = set()
def virtqueue_pop(self, vq, elem, in_num, out_num):
self.elems.add(elem)
def virtqueue_fill(self, vq, elem, length, idx):
self.elems.remove(elem)
def end(self):
for elem in self.elems:
print hex(elem)
simpletrace.run(VirtqueueRequestTracker())
The simpletrace API is based around the Analyzer class. Users implement
an analyzer subclass and add methods for trace events they want to
process. A catchall() method is invoked for trace events which do not
have dedicated methods. Finally, there are also begin() and end()
methods like in sed that can be used to perform setup or print
statistics at the end.
A binary trace file is processed either with:
simpletrace.run(analyzer) # uses command-line args
or with:
simpletrace.process('path/to/trace-events',
'path/to/trace-file',
analyzer)
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-02-22 14:59:41 +01:00
|
|
|
event_num = rec[0]
|
2012-07-18 11:46:00 +02:00
|
|
|
event = edict[event_num]
|
simpletrace: Make simpletrace.py a Python module
The simpletrace.py script pretty-prints a binary trace file. Most of
the code can be reused by trace file analysis scripts, so turn it into a
module.
Here is an example script that uses the new simpletrace module:
#!/usr/bin/env python
# Print virtqueue elements that were never returned to the guest.
import simpletrace
class VirtqueueRequestTracker(simpletrace.Analyzer):
def __init__(self):
self.elems = set()
def virtqueue_pop(self, vq, elem, in_num, out_num):
self.elems.add(elem)
def virtqueue_fill(self, vq, elem, length, idx):
self.elems.remove(elem)
def end(self):
for elem in self.elems:
print hex(elem)
simpletrace.run(VirtqueueRequestTracker())
The simpletrace API is based around the Analyzer class. Users implement
an analyzer subclass and add methods for trace events they want to
process. A catchall() method is invoked for trace events which do not
have dedicated methods. Finally, there are also begin() and end()
methods like in sed that can be used to perform setup or print
statistics at the end.
A binary trace file is processed either with:
simpletrace.run(analyzer) # uses command-line args
or with:
simpletrace.process('path/to/trace-events',
'path/to/trace-file',
analyzer)
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-02-22 14:59:41 +01:00
|
|
|
if event_num not in fn_cache:
|
|
|
|
fn_cache[event_num] = build_fn(analyzer, event)
|
|
|
|
fn_cache[event_num](event, rec)
|
|
|
|
analyzer.end()
|
|
|
|
|
|
|
|
def run(analyzer):
|
|
|
|
"""Execute an analyzer on a trace file given on the command-line.
|
|
|
|
|
|
|
|
This function is useful as a driver for simple analysis scripts. More
|
|
|
|
advanced scripts will want to call process() instead."""
|
|
|
|
import sys
|
|
|
|
|
2014-06-22 15:46:06 +02:00
|
|
|
read_header = True
|
|
|
|
if len(sys.argv) == 4 and sys.argv[1] == '--no-header':
|
|
|
|
read_header = False
|
|
|
|
del sys.argv[1]
|
|
|
|
elif len(sys.argv) != 3:
|
|
|
|
sys.stderr.write('usage: %s [--no-header] <trace-events> ' \
|
|
|
|
'<trace-file>\n' % sys.argv[0])
|
simpletrace: Make simpletrace.py a Python module
The simpletrace.py script pretty-prints a binary trace file. Most of
the code can be reused by trace file analysis scripts, so turn it into a
module.
Here is an example script that uses the new simpletrace module:
#!/usr/bin/env python
# Print virtqueue elements that were never returned to the guest.
import simpletrace
class VirtqueueRequestTracker(simpletrace.Analyzer):
def __init__(self):
self.elems = set()
def virtqueue_pop(self, vq, elem, in_num, out_num):
self.elems.add(elem)
def virtqueue_fill(self, vq, elem, length, idx):
self.elems.remove(elem)
def end(self):
for elem in self.elems:
print hex(elem)
simpletrace.run(VirtqueueRequestTracker())
The simpletrace API is based around the Analyzer class. Users implement
an analyzer subclass and add methods for trace events they want to
process. A catchall() method is invoked for trace events which do not
have dedicated methods. Finally, there are also begin() and end()
methods like in sed that can be used to perform setup or print
statistics at the end.
A binary trace file is processed either with:
simpletrace.run(analyzer) # uses command-line args
or with:
simpletrace.process('path/to/trace-events',
'path/to/trace-file',
analyzer)
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-02-22 14:59:41 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
2018-03-06 16:46:50 +01:00
|
|
|
events = read_events(open(sys.argv[1], 'r'), sys.argv[1])
|
2014-06-22 15:46:06 +02:00
|
|
|
process(events, sys.argv[2], analyzer, read_header=read_header)
|
simpletrace: Make simpletrace.py a Python module
The simpletrace.py script pretty-prints a binary trace file. Most of
the code can be reused by trace file analysis scripts, so turn it into a
module.
Here is an example script that uses the new simpletrace module:
#!/usr/bin/env python
# Print virtqueue elements that were never returned to the guest.
import simpletrace
class VirtqueueRequestTracker(simpletrace.Analyzer):
def __init__(self):
self.elems = set()
def virtqueue_pop(self, vq, elem, in_num, out_num):
self.elems.add(elem)
def virtqueue_fill(self, vq, elem, length, idx):
self.elems.remove(elem)
def end(self):
for elem in self.elems:
print hex(elem)
simpletrace.run(VirtqueueRequestTracker())
The simpletrace API is based around the Analyzer class. Users implement
an analyzer subclass and add methods for trace events they want to
process. A catchall() method is invoked for trace events which do not
have dedicated methods. Finally, there are also begin() and end()
methods like in sed that can be used to perform setup or print
statistics at the end.
A binary trace file is processed either with:
simpletrace.run(analyzer) # uses command-line args
or with:
simpletrace.process('path/to/trace-events',
'path/to/trace-file',
analyzer)
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-02-22 14:59:41 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
class Formatter(Analyzer):
|
|
|
|
def __init__(self):
|
|
|
|
self.last_timestamp = None
|
|
|
|
|
|
|
|
def catchall(self, event, rec):
|
|
|
|
timestamp = rec[1]
|
|
|
|
if self.last_timestamp is None:
|
|
|
|
self.last_timestamp = timestamp
|
|
|
|
delta_ns = timestamp - self.last_timestamp
|
|
|
|
self.last_timestamp = timestamp
|
|
|
|
|
simpletrace: add support for trace record pid field
Extract the pid field from the trace record and print it.
Change the trace record tuple from:
(event_num, timestamp, arg1, ..., arg6)
to:
(event_num, timestamp, pid, arg1, ..., arg6)
Trace event methods now support 3 prototypes:
1. <event-name>(arg1, arg2, arg3)
2. <event-name>(timestamp, arg1, arg2, arg3)
3. <event-name>(timestamp, pid, arg1, arg2, arg3)
Existing script continue to work without changes, they only know about
prototypes 1 and 2.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2014-05-07 19:24:11 +02:00
|
|
|
fields = [event.name, '%0.3f' % (delta_ns / 1000.0),
|
|
|
|
'pid=%d' % rec[2]]
|
|
|
|
i = 3
|
2012-07-18 11:46:00 +02:00
|
|
|
for type, name in event.args:
|
|
|
|
if is_string(type):
|
simpletrace: add support for trace record pid field
Extract the pid field from the trace record and print it.
Change the trace record tuple from:
(event_num, timestamp, arg1, ..., arg6)
to:
(event_num, timestamp, pid, arg1, ..., arg6)
Trace event methods now support 3 prototypes:
1. <event-name>(arg1, arg2, arg3)
2. <event-name>(timestamp, arg1, arg2, arg3)
3. <event-name>(timestamp, pid, arg1, arg2, arg3)
Existing script continue to work without changes, they only know about
prototypes 1 and 2.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2014-05-07 19:24:11 +02:00
|
|
|
fields.append('%s=%s' % (name, rec[i]))
|
2012-07-18 11:46:00 +02:00
|
|
|
else:
|
simpletrace: add support for trace record pid field
Extract the pid field from the trace record and print it.
Change the trace record tuple from:
(event_num, timestamp, arg1, ..., arg6)
to:
(event_num, timestamp, pid, arg1, ..., arg6)
Trace event methods now support 3 prototypes:
1. <event-name>(arg1, arg2, arg3)
2. <event-name>(timestamp, arg1, arg2, arg3)
3. <event-name>(timestamp, pid, arg1, arg2, arg3)
Existing script continue to work without changes, they only know about
prototypes 1 and 2.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2014-05-07 19:24:11 +02:00
|
|
|
fields.append('%s=0x%x' % (name, rec[i]))
|
2012-07-18 11:46:00 +02:00
|
|
|
i += 1
|
2018-06-08 14:29:43 +02:00
|
|
|
print(' '.join(fields))
|
simpletrace: Make simpletrace.py a Python module
The simpletrace.py script pretty-prints a binary trace file. Most of
the code can be reused by trace file analysis scripts, so turn it into a
module.
Here is an example script that uses the new simpletrace module:
#!/usr/bin/env python
# Print virtqueue elements that were never returned to the guest.
import simpletrace
class VirtqueueRequestTracker(simpletrace.Analyzer):
def __init__(self):
self.elems = set()
def virtqueue_pop(self, vq, elem, in_num, out_num):
self.elems.add(elem)
def virtqueue_fill(self, vq, elem, length, idx):
self.elems.remove(elem)
def end(self):
for elem in self.elems:
print hex(elem)
simpletrace.run(VirtqueueRequestTracker())
The simpletrace API is based around the Analyzer class. Users implement
an analyzer subclass and add methods for trace events they want to
process. A catchall() method is invoked for trace events which do not
have dedicated methods. Finally, there are also begin() and end()
methods like in sed that can be used to perform setup or print
statistics at the end.
A binary trace file is processed either with:
simpletrace.run(analyzer) # uses command-line args
or with:
simpletrace.process('path/to/trace-events',
'path/to/trace-file',
analyzer)
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-02-22 14:59:41 +01:00
|
|
|
|
|
|
|
run(Formatter())
|