Initial Commit
This commit is contained in:
706 files changed
+135430
No files matched your search
@@ -0,0 +1,145 @@
|
||||
# Reads a callgrind profile of an executable built with `sakura build -g` and prints
|
||||
# its costs with inlined code counted where it came from.
|
||||
#
|
||||
# valgrind --tool=callgrind --dump-instr=yes --compress-pos=no --compress-strings=no \
|
||||
# --callgrind-out-file=PROFILE EXECUTABLE ARGS...
|
||||
# python3 costs.py PROFILE EXECUTABLE [EVENT [MODE [COUNT [FUNCTION]]]]
|
||||
#
|
||||
# EVENT is one callgrind counted, `Ir` by default; with `--cache-sim=yes
|
||||
# --branch-sim=yes` there are also `D1mr`, `DLmw`, `I1mr`, `Bcm`, and the others.
|
||||
# MODE is one of:
|
||||
# fn self cost by source function
|
||||
# incl inclusive cost by source function (recursion counts more than once)
|
||||
# line self cost by source line
|
||||
# sym self cost by symbol, the function the code was emitted in
|
||||
# callees F what F's code calls, inlined code included
|
||||
# callers F the call sites of F, where it is called rather than inlined
|
||||
# inlined F where F's code was inlined
|
||||
# lines F the lines of F
|
||||
# users REGEX the cost of functions matching REGEX by the first frame outside
|
||||
# them and std that led there, which is the analyzer code using them
|
||||
# calls out-of-line calls of each function, and its instructions per call
|
||||
# The mapping is cached beside the profile, as PROFILE.pkl.
|
||||
import collections, os, pickle, re, sys
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
import inlined
|
||||
|
||||
profile, executable = sys.argv[1], sys.argv[2]
|
||||
event = sys.argv[3] if len(sys.argv) > 3 else 'Ir'
|
||||
mode = sys.argv[4] if len(sys.argv) > 4 else 'fn'
|
||||
count = int(sys.argv[5]) if len(sys.argv) > 5 else 40
|
||||
target = sys.argv[6] if len(sys.argv) > 6 else None
|
||||
|
||||
cache = profile + '.pkl'
|
||||
if os.path.exists(cache) and os.path.getmtime(cache) > os.path.getmtime(profile):
|
||||
events, own, calls, counts, found = pickle.load(open(cache, 'rb'))
|
||||
else:
|
||||
events, own, calls, counts = [], {}, [], collections.Counter()
|
||||
callee = None
|
||||
pending = False
|
||||
positions = 1
|
||||
for line in open(profile):
|
||||
if line.startswith('positions:'):
|
||||
positions = len(line.split()) - 1
|
||||
elif line.startswith('events:'):
|
||||
events = line.split()[1:]
|
||||
elif line.startswith('cfn='):
|
||||
callee = line[4:].strip()
|
||||
elif line.startswith('calls='):
|
||||
counts[callee] += int(line.split()[0][6:])
|
||||
pending = True
|
||||
elif line.startswith('0x'):
|
||||
parts = line.split()
|
||||
address = int(parts[0], 16)
|
||||
# With --dump-line=yes a line number follows the address.
|
||||
numbers = [int(x) for x in parts[positions:]]
|
||||
numbers += [0] * (len(events) - len(numbers))
|
||||
if pending:
|
||||
calls.append((address, callee, numbers))
|
||||
pending = False
|
||||
elif address in own:
|
||||
own[address] = [a + b for a, b in zip(own[address], numbers)]
|
||||
else:
|
||||
own[address] = numbers
|
||||
found = inlined.chains(executable, list(own) + [a for a, _, _ in calls])
|
||||
pickle.dump((events, own, calls, counts, found), open(cache, 'wb'))
|
||||
|
||||
index = events.index(event)
|
||||
total = sum(c[index] for c in own.values()) or 1
|
||||
|
||||
def show(title, counter):
|
||||
print(f'== {title} ({event}, total {total:,})')
|
||||
for key, value in counter.most_common(count):
|
||||
print(f'{100 * value / total:6.2f}% {value:>13,} {key[:110]}')
|
||||
|
||||
def outside(chain, pattern):
|
||||
for function, location in chain:
|
||||
if not pattern.search(function) and not location.startswith('std/'):
|
||||
return function + ' @ ' + location
|
||||
return None
|
||||
|
||||
result = collections.Counter()
|
||||
if mode in ('fn', 'line', 'sym'):
|
||||
for address, cost in own.items():
|
||||
chain = inlined.frames(found, address)
|
||||
key = {'fn': chain[0][0], 'line': chain[0][1], 'sym': chain[-1][0]}[mode]
|
||||
result[key] += cost[index]
|
||||
show({'fn': 'self cost by source function', 'line': 'self cost by line', 'sym': 'self cost by symbol'}[mode], result)
|
||||
elif mode == 'incl':
|
||||
for address, cost in own.items():
|
||||
for function in set(f for f, _ in inlined.frames(found, address)):
|
||||
result[function] += cost[index]
|
||||
for address, called, cost in calls:
|
||||
for function in set(f for f, _ in inlined.frames(found, address)):
|
||||
if function != called:
|
||||
result[function] += cost[index]
|
||||
show('inclusive cost by source function', result)
|
||||
elif mode == 'callees':
|
||||
for address, called, cost in calls:
|
||||
names = [f for f, _ in inlined.frames(found, address)]
|
||||
if target in names:
|
||||
within = names[:names.index(target)]
|
||||
result[called + (' via ' + '/'.join(reversed(within)) if within else '')] += cost[index]
|
||||
show(f'calls made by {target}, inclusive', result)
|
||||
elif mode == 'callers':
|
||||
for address, called, cost in calls:
|
||||
if called == target:
|
||||
chain = inlined.frames(found, address)
|
||||
result[chain[0][0] + ' @ ' + chain[0][1]] += cost[index]
|
||||
show(f'calls of {target}, inclusive, by call site', result)
|
||||
elif mode == 'inlined':
|
||||
for address, cost in own.items():
|
||||
chain = inlined.frames(found, address)
|
||||
names = [f for f, _ in chain]
|
||||
if target in names and names.index(target) + 1 < len(names):
|
||||
outer = chain[names.index(target) + 1]
|
||||
result[outer[0] + ' @ ' + outer[1]] += cost[index]
|
||||
show(f'{target} inlined into', result)
|
||||
elif mode == 'lines':
|
||||
for address, cost in own.items():
|
||||
chain = inlined.frames(found, address)
|
||||
if chain[0][0] == target:
|
||||
result[chain[0][1]] += cost[index]
|
||||
show(f'lines of {target}', result)
|
||||
elif mode == 'users':
|
||||
pattern = re.compile(target)
|
||||
for address, cost in own.items():
|
||||
chain = inlined.frames(found, address)
|
||||
if pattern.search(chain[0][0]):
|
||||
user = outside(chain, pattern)
|
||||
if user:
|
||||
result[user] += cost[index]
|
||||
for address, called, cost in calls:
|
||||
chain = inlined.frames(found, address)
|
||||
if pattern.search(called) and not pattern.search(chain[-1][0]):
|
||||
user = outside(chain, pattern)
|
||||
if user:
|
||||
result[user] += cost[index]
|
||||
show(f'/{target}/ by the code using it', result)
|
||||
elif mode == 'calls':
|
||||
symbols = collections.Counter()
|
||||
for address, cost in own.items():
|
||||
symbols[inlined.frames(found, address)[-1][0]] += cost[0]
|
||||
print(' calls instructions per call function')
|
||||
for function, n in counts.most_common(count):
|
||||
print(f'{n:>10,} {symbols[function]:>12,} {symbols[function] / n:8.1f} {function}')
|
||||
Reference in new issue
Block a user