Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-19 04:54:17 -06:00
commit f086abcea6
789 files changed
+149185

No files matched your search

+52
View File
@@ -0,0 +1,52 @@
# Classifies the instructions a callgrind profile counted by what they do, which
# shows how much of the code is moving values between frame slots and registers,
# calling, and checking, rather than computing.
#
# python3 kinds.py PROFILE EXECUTABLE
#
# The profile is one costs.py has read.
import collections, os, pickle, re, subprocess, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
profile, executable = sys.argv[1], sys.argv[2]
events, own, calls, counts, found = pickle.load(open(profile + '.pkl', 'rb'))
listing = subprocess.run(['objdump', '-d', '--no-show-raw-insn', executable], capture_output=True, text=True).stdout
instructions, order = {}, []
for line in listing.splitlines():
m = re.match(r'\s*([0-9a-f]+):\s+(.*)$', line)
if m:
address = int(m.group(1), 16)
instructions[address] = m.group(2).strip()
order.append(address)
following = {order[i]: order[i + 1] for i in range(len(order) - 1)}
def kind(text, after):
if text.startswith('rep stos'): return 'zeroing: rep stos'
if text.startswith('rep movs'): return 'copying: rep movs'
if re.match(r'movq\s+\$0x0,-?(0x[0-9a-f]+)?\(%rbx\)', text): return 'zeroing: slot'
if text.startswith(('push %rbp', 'mov %rsp,%rbp', 'leave', 'ret')): return 'call: frame link'
if re.match(r'(add|sub)\s+\$0x[0-9a-f]+,%rbx$', text): return 'call: moving rbx'
if text.startswith('call'): return 'call: call'
# The prologue's check of the frames' limit: lea from rbx, then cmp with a global.
if re.match(r'lea\s+-?(0x[0-9a-f]+)?\(%rbx\),%rax$', text) and re.match(r'cmp\s+0x[0-9a-f]+,%rax$', after): return 'call: stack check'
if re.match(r'cmp\s+0x[0-9a-f]+,%rax$', text): return 'call: stack check'
if re.match(r'test\s+(%\w+),\1$', text) and after.startswith('jne'): return 'null check'
if re.search(r'\(%rbx\)', text) and text.startswith('mov'):
return 'slot: store' if re.match(r'mov\w*\s+%\w+,', text) else 'slot: load'
if re.match(r'lea\s+-?(0x[0-9a-f]+)?\(%rbx\)', text): return 'slot: address'
if re.match(r'mov\s+%\w+,%\w+$', text): return 'register copy'
if re.match(r'mov\w*\s+\$', text): return 'constant'
if re.match(r'mov\w*\s+[^,]*\([^)]*\),%\w+$', text): return 'load through a pointer'
if re.match(r'mov\w*\s+%\w+,[^,]*\(', text): return 'store through a pointer'
if text.startswith('j'): return 'branch'
if text.startswith(('cmp', 'test')): return 'compare'
return 'computing: ' + text.split()[0]
result = collections.Counter()
for address, cost in own.items():
text = instructions.get(address, '?')
result[kind(text, instructions.get(following.get(address), ''))] += cost[0]
total = sum(result.values()) or 1
for name, value in result.most_common(30):
print(f'{value:>14,} {100 * value / total:5.1f}% {name}')
print(f'{total:>14,} total')