perf python: Convert tracepoint example to python 3
PATCH 1/1 perf python: Convert tracepoint example to python 3
Make the tracepoint example compatible with python 3 and fix string handling of the prev_comm and next_comm fields so they are properly truncated.
Signed-off-by Harald Seiler <[email protected]>
--- tools/perf/python/tracepoint.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
tools/perf/python/tracepoint.py +14 -4
@@ -3,6 +3,8 @@ # -*- python -*- # -*- coding: utf-8 -*- +from __future__ import print_function + import perf class tracepoint(perf.evsel):
@@ -34,15 +36,23 @@ def main(): if not isinstance(event, perf.sample_event): continue - print "time %u prev_comm=%s prev_pid=%d prev_prio=%d prev_state=0x%x ==> next_comm=%s next_pid=%d next_prio=%d" % ( + # The prev_comm and next_comm fields are `bytes` objects but they + # are not truncated to the first \x00. This is due to the + # underlying code treating the strings as just an array of + # arbitrary bytes. Convert them to properly truncated python + # strings. + prev_comm_str = event.prev_comm.split(b'\x00', 1)[0].decode(errors="ignore") + next_comm_str = event.next_comm.split(b'\x00', 1)[0].decode(errors="ignore") + + print("time {} prev_comm={} prev_pid={} prev_prio={} prev_state=0x{:x} ==> next_comm={} next_pid={} next_prio={}".format( event.sample_time, - event.prev_comm, + prev_comm_str, event.prev_pid, event.prev_prio, event.prev_state, - event.next_comm, + next_comm_str, event.next_pid, - event.next_prio) + event.next_prio)) if __name__ == '__main__': main()
Signature · 1 line
2.20.1