2010-05-22 22:24:51 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# For help see docs/tracing.txt
|
|
|
|
|
|
|
|
import struct
|
|
|
|
import re
|
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 16:59:41 +03:00
|
|
|
import inspect
|
2012-07-18 13:46:00 +04:00
|
|
|
from tracetool import _read_events, Event
|
|
|
|
from tracetool.backend.simple import is_string
|
2010-05-22 22:24:51 +04:00
|
|
|
|
|
|
|
header_event_id = 0xffffffffffffffff
|
|
|
|
header_magic = 0xf2b177cb0aa429b4
|
2011-02-26 21:38:39 +03:00
|
|
|
dropped_event_id = 0xfffffffffffffffe
|
2010-05-22 22:24:51 +04:00
|
|
|
|
2012-07-18 13:46:00 +04:00
|
|
|
log_header_fmt = '=QQQ'
|
|
|
|
rec_header_fmt = '=QQII'
|
2010-05-22 22:24:51 +04:00
|
|
|
|
2012-07-18 13:46:00 +04: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 22:24:51 +04:00
|
|
|
|
2012-07-18 13:46:00 +04:00
|
|
|
def get_record(edict, rechdr, fobj):
|
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 16:59:41 +03:00
|
|
|
"""Deserialize a trace record from a file into a tuple (event_num, timestamp, arg1, ..., arg6)."""
|
2012-07-18 13:46:00 +04:00
|
|
|
if rechdr is None:
|
2010-05-22 22:24:51 +04:00
|
|
|
return None
|
2012-07-18 13:46:00 +04:00
|
|
|
rec = (rechdr[0], rechdr[1])
|
|
|
|
if rechdr[0] != dropped_event_id:
|
|
|
|
event_id = rechdr[0]
|
|
|
|
event = edict[event_id]
|
|
|
|
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:
|
|
|
|
(value,) = struct.unpack('=Q', fobj.read(8))
|
|
|
|
rec = rec + (value,)
|
|
|
|
return rec
|
|
|
|
|
|
|
|
|
|
|
|
def read_record(edict, fobj):
|
|
|
|
"""Deserialize a trace record from a file into a tuple (event_num, timestamp, arg1, ..., arg6)."""
|
|
|
|
rechdr = read_header(fobj, rec_header_fmt)
|
|
|
|
return get_record(edict, rechdr, fobj) # return tuple of record elements
|
2010-05-22 22:24:51 +04:00
|
|
|
|
2012-07-18 13:46:00 +04:00
|
|
|
def read_trace_file(edict, fobj):
|
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 16:59:41 +03:00
|
|
|
"""Deserialize trace records from a file, yielding record tuples (event_num, timestamp, arg1, ..., arg6)."""
|
2012-07-18 13:46:00 +04:00
|
|
|
header = read_header(fobj, log_header_fmt)
|
2010-05-22 22:24:51 +04:00
|
|
|
if header is None or \
|
|
|
|
header[0] != header_event_id or \
|
2012-07-18 13:46:00 +04:00
|
|
|
header[1] != header_magic:
|
|
|
|
raise ValueError('Not a valid trace file!')
|
|
|
|
if header[2] != 0 and \
|
|
|
|
header[2] != 2:
|
|
|
|
raise ValueError('Unknown version of tracelog format!')
|
|
|
|
|
|
|
|
log_version = header[2]
|
|
|
|
if log_version == 0:
|
|
|
|
raise ValueError('Older log format, not supported with this Qemu release!')
|
2010-05-22 22:24:51 +04:00
|
|
|
|
|
|
|
while True:
|
2012-07-18 13:46:00 +04:00
|
|
|
rec = read_record(edict, fobj)
|
2010-05-22 22:24:51 +04:00
|
|
|
if rec is None:
|
|
|
|
break
|
|
|
|
|
|
|
|
yield rec
|
|
|
|
|
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 16:59:41 +03: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
|
|
|
|
that trace record. Otherwise the catchall() method is invoked."""
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def process(events, log, analyzer):
|
|
|
|
"""Invoke an analyzer on each event in a log."""
|
|
|
|
if isinstance(events, str):
|
2012-07-18 13:46:00 +04:00
|
|
|
events = _read_events(open(events, 'r'))
|
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 16:59:41 +03:00
|
|
|
if isinstance(log, str):
|
|
|
|
log = open(log, 'rb')
|
|
|
|
|
2012-07-18 13:46:00 +04:00
|
|
|
enabled_events = []
|
|
|
|
dropped_event = Event.build("Dropped_Event(uint64_t num_events_dropped)")
|
|
|
|
edict = {dropped_event_id: dropped_event}
|
|
|
|
|
|
|
|
for e in events:
|
|
|
|
if 'disable' not in e.properties:
|
|
|
|
enabled_events.append(e)
|
|
|
|
for num, event in enumerate(enabled_events):
|
|
|
|
edict[num] = event
|
|
|
|
|
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 16:59:41 +03:00
|
|
|
def build_fn(analyzer, event):
|
2012-07-18 13:46:00 +04: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 16:59:41 +03:00
|
|
|
if fn is None:
|
|
|
|
return analyzer.catchall
|
|
|
|
|
2012-07-18 13:46:00 +04: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 16:59:41 +03:00
|
|
|
fn_argcount = len(inspect.getargspec(fn)[0]) - 1
|
|
|
|
if fn_argcount == event_argcount + 1:
|
|
|
|
# Include timestamp as first argument
|
2011-08-25 21:03:49 +04:00
|
|
|
return lambda _, rec: fn(*rec[1:2 + 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 16:59:41 +03:00
|
|
|
else:
|
|
|
|
# Just arguments, no timestamp
|
2011-08-25 21:03:49 +04:00
|
|
|
return lambda _, rec: fn(*rec[2:2 + 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 16:59:41 +03:00
|
|
|
|
|
|
|
analyzer.begin()
|
|
|
|
fn_cache = {}
|
2012-07-18 13:46:00 +04:00
|
|
|
for rec in read_trace_file(edict, 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 16:59:41 +03:00
|
|
|
event_num = rec[0]
|
2012-07-18 13:46:00 +04: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 16:59:41 +03: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
|
|
|
|
|
|
|
|
if len(sys.argv) != 3:
|
|
|
|
sys.stderr.write('usage: %s <trace-events> <trace-file>\n' % sys.argv[0])
|
|
|
|
sys.exit(1)
|
|
|
|
|
2012-07-18 13:46:00 +04:00
|
|
|
events = _read_events(open(sys.argv[1], 'r'))
|
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 16:59:41 +03:00
|
|
|
process(events, sys.argv[2], analyzer)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
class Formatter(Analyzer):
|
|
|
|
def __init__(self):
|
|
|
|
self.last_timestamp = None
|
|
|
|
|
|
|
|
def catchall(self, event, rec):
|
2012-07-18 13:46:00 +04:00
|
|
|
i = 1
|
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 16:59:41 +03:00
|
|
|
timestamp = rec[1]
|
|
|
|
if self.last_timestamp is None:
|
|
|
|
self.last_timestamp = timestamp
|
|
|
|
delta_ns = timestamp - self.last_timestamp
|
|
|
|
self.last_timestamp = timestamp
|
|
|
|
|
2012-07-18 13:46:00 +04:00
|
|
|
fields = [event.name, '%0.3f' % (delta_ns / 1000.0)]
|
|
|
|
for type, name in event.args:
|
|
|
|
if is_string(type):
|
|
|
|
fields.append('%s=%s' % (name, rec[i + 1]))
|
|
|
|
else:
|
|
|
|
fields.append('%s=0x%x' % (name, rec[i + 1]))
|
|
|
|
i += 1
|
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 16:59:41 +03:00
|
|
|
print ' '.join(fields)
|
|
|
|
|
|
|
|
run(Formatter())
|