Initial Commit
This commit is contained in:
782 files changed
+145156
No files matched your search
@@ -0,0 +1,87 @@
|
||||
# Reads the samples `sampler` wrote of an executable built with `sakura build -g`,
|
||||
# and prints where the time went, with inlined code counted where it came from.
|
||||
#
|
||||
# python3 samples.py SAMPLES EXECUTABLE [MODE [COUNT [REGEX]]] [--profile PROFILE]
|
||||
#
|
||||
# MODE is one of:
|
||||
# fn time by source function; with `--profile`, a callgrind profile of
|
||||
# the same executable that costs.py has read, also its share of
|
||||
# instructions and the ratio of the two, which is high where the
|
||||
# code waits on memory, mispredicted branches, or the kernel
|
||||
# incl time by every function on the stack
|
||||
# line time by source line
|
||||
# users REGEX time in functions matching REGEX by the first frame outside them
|
||||
# and std that led there
|
||||
import collections, os, pickle, re, sys
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
import inlined
|
||||
|
||||
arguments = sys.argv[1:]
|
||||
profile = None
|
||||
if '--profile' in arguments:
|
||||
at = arguments.index('--profile')
|
||||
profile = arguments[at + 1]
|
||||
del arguments[at:at + 2]
|
||||
samples, executable = arguments[0], arguments[1]
|
||||
mode = arguments[2] if len(arguments) > 2 else 'fn'
|
||||
count = int(arguments[3]) if len(arguments) > 3 else 40
|
||||
target = arguments[4] if len(arguments) > 4 else None
|
||||
|
||||
rows = [[int(x, 16) for x in line.split()] for line in open(samples) if line.strip()]
|
||||
addresses = set()
|
||||
for row in rows:
|
||||
addresses.add(row[0])
|
||||
addresses.update(a - 1 for a in row[1:])
|
||||
found = inlined.chains(executable, addresses)
|
||||
n = len(rows) or 1
|
||||
|
||||
def stack(row):
|
||||
chain = list(inlined.frames(found, row[0]))
|
||||
for a in row[1:]:
|
||||
chain += found.get(a - 1) or []
|
||||
return chain
|
||||
|
||||
result = collections.Counter()
|
||||
if mode == 'fn':
|
||||
for row in rows:
|
||||
result[inlined.frames(found, row[0])[0][0]] += 1
|
||||
shares = collections.Counter()
|
||||
if profile:
|
||||
events, own, calls, counts, mapped = pickle.load(open(profile + '.pkl', 'rb'))
|
||||
for address, cost in own.items():
|
||||
shares[inlined.frames(mapped, address)[0][0]] += cost[0]
|
||||
whole = sum(shares.values()) or 1
|
||||
print(f'{len(rows)} samples time instr ratio function')
|
||||
for function, c in result.most_common(count):
|
||||
t, s = 100 * c / n, 100 * shares[function] / whole
|
||||
print(f' {t:6.2f}% {s:6.2f}% {t / s if s else 0:5.2f} {function[:90]}')
|
||||
else:
|
||||
for function, c in result.most_common(count):
|
||||
print(f'{100 * c / n:6.2f}% {function[:100]}')
|
||||
elif mode == 'incl':
|
||||
for row in rows:
|
||||
for function in set(f for f, _ in stack(row)):
|
||||
result[function] += 1
|
||||
for function, c in result.most_common(count):
|
||||
print(f'{100 * c / n:6.2f}% {function[:100]}')
|
||||
elif mode == 'line':
|
||||
for row in rows:
|
||||
result[inlined.frames(found, row[0])[0][1]] += 1
|
||||
for location, c in result.most_common(count):
|
||||
print(f'{100 * c / n:6.2f}% {location[:100]}')
|
||||
elif mode == 'users':
|
||||
pattern = re.compile(target)
|
||||
hits = 0
|
||||
for row in rows:
|
||||
if not pattern.search(inlined.frames(found, row[0])[0][0]):
|
||||
continue
|
||||
hits += 1
|
||||
user = '?'
|
||||
for function, location in stack(row):
|
||||
if not pattern.search(function) and not location.startswith('std/'):
|
||||
user = function + ' @ ' + location
|
||||
break
|
||||
result[user] += 1
|
||||
print(f'== {100 * hits / n:.2f}% of the time in /{target}/, by the code using it')
|
||||
for user, c in result.most_common(count):
|
||||
print(f'{100 * c / n:6.2f}% {user[:110]}')
|
||||
Reference in new issue
Block a user