| |
| """Map the hot instruction-fetch cache lines reported by drcachesim's histogram |
| tool onto functions of the traced binary. |
| |
| The histogram tool reports instruction-fetch addresses at 64-byte cache-line |
| granularity, so this is a corroborating view of the traces themselves; the |
| exhaustive, instruction-exact attribution lives in |
| validation/steady_state/function_profile-segment-*.txt. |
| |
| The binary is built -no-pie, so a runtime PC inside the main module equals its |
| link-time address and can be looked up directly in `nm` output; modules.log |
| (shipped in traces_simp/bin/) carries the base address of every module for the |
| general case. |
| |
| Usage: trace_pc_funcs.py <histogram-output> <nm-file> [modules.log] |
| """ |
| import bisect |
| import re |
| import sys |
| from collections import defaultdict |
|
|
|
|
| def load_symbols(nm_path): |
| syms = [] |
| for line in open(nm_path): |
| parts = line.split(None, 2) |
| if len(parts) < 3: |
| continue |
| addr, typ, name = parts[0], parts[1], parts[2].strip() |
| if typ.lower() not in ("t", "w", "i"): |
| continue |
| try: |
| syms.append((int(addr, 16), name)) |
| except ValueError: |
| pass |
| syms.sort() |
| return [a for a, _ in syms], [n for _, n in syms] |
|
|
|
|
| def load_modules(path): |
| """modules.log -> list of (base, end, name) for attribution of non-main PCs.""" |
| mods = [] |
| for line in open(path, errors="replace"): |
| fields = [f.strip() for f in line.split(",")] |
| if len(fields) < 6: |
| continue |
| try: |
| base = int(fields[2], 16) if fields[2].startswith("0x") else int(fields[2]) |
| end = int(fields[3], 16) if fields[3].startswith("0x") else int(fields[3]) |
| except ValueError: |
| continue |
| mods.append((base, end, fields[-1])) |
| return mods |
|
|
|
|
| def main(): |
| hist_path, nm_path = sys.argv[1], sys.argv[2] |
| modules = load_modules(sys.argv[3]) if len(sys.argv) > 3 else [] |
| addrs, names = load_symbols(nm_path) |
| lo, hi = addrs[0], addrs[-1] + 0x10000 |
|
|
| per_func = defaultdict(int) |
| total = 0 |
| in_instr_section = False |
| for line in open(hist_path): |
| if re.match(r"\s*icache top", line, re.I): |
| in_instr_section = True |
| continue |
| if re.match(r"\s*dcache top", line, re.I): |
| in_instr_section = False |
| continue |
| if not in_instr_section: |
| continue |
| m = re.match(r"\s*(0x[0-9a-fA-F]+)\s*:\s*([0-9]+)", line) |
| if not m: |
| continue |
| pc, cnt = int(m.group(1), 16), int(m.group(2)) |
| total += cnt |
| if lo <= pc <= hi: |
| i = bisect.bisect_right(addrs, pc) - 1 |
| per_func[names[i]] += cnt |
| else: |
| label = "[non-main module]" |
| for base, end, name in modules: |
| if base <= pc < end: |
| label = f"[{name}]" |
| break |
| per_func[label] += cnt |
|
|
| print(f"# hot instruction-fetch entries attributed: {total}") |
| print(f"{'share%':>8} {'count':>12} function") |
| for name, n in sorted(per_func.items(), key=lambda kv: -kv[1]): |
| print(f"{100.0 * n / total:8.3f} {n:12d} {name}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|