Skip to content

Instantly share code, notes, and snippets.

@stormouse
Last active November 25, 2021 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stormouse/00fe58f6f047326698d040a0787abc5a to your computer and use it in GitHub Desktop.
Save stormouse/00fe58f6f047326698d040a0787abc5a to your computer and use it in GitHub Desktop.
Parse output of perf script
# usage: python this.py <(perf script -i perf.data -F comm,tid,time,ip,sym,cpu,event)
import fileinput
proc_sample_total = 0
cpu_inc = {}
cpu_ext = {}
comm = '<your-process-name>'[:15].lower()
interested = True
expect_header = True
top_of_frame = False
for input_line in fileinput.input():
line = input_line.strip()
if len(line) == 0:
interested = True
expect_header = True
continue
if expect_header:
interested = line.lower().startswith(comm)
if interested:
proc_sample_total += 1
expect_header = False
top_of_frame = True
continue
if interested:
segments = line.split()
if segments[1] == '[unknown]':
top_of_frame = False
continue
signature = '_'.join(segments[1:])
cpu_inc.setdefault(signature, 0)
cpu_inc[signature] += 1
if top_of_frame:
cpu_ext.setdefault(signature, 0)
cpu_ext[signature] += 1
top_of_frame = False
def stats(x):
items = sorted(x.items(), key=lambda x: x[1], reverse=True)
for item in items:
print(' - {}: {:.3f}'.format(item[0], 100.0 * item[1] / proc_sample_total))
print('')
print('Inclusion %:')
stats(cpu_inc)
print('Exclusion %:')
stats(cpu_ext)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment