"""Offline profile of a thread sampled with HOS_SAMPLE + HOS_SAMPLE_FILE (host/mkw_glue.c, mkw-recomp work/prof.py): each line is a stack of module+offset frames, leaf first. Frames in hookshot_vk.dll / hookshot.exe are named from the linker maps beside them; on Linux, frames in hookshot_vk.so / hookshot from the binaries' own symbols (nm). RX_MAPS=: another build's maps or binaries (on Linux, the game folder). python -I scripts/prof.py work/runs//stacks.txt [top] [skip] [only] (skip: the first fraction of the samples, e.g. 0.5; only: samples with that function in their stack, e.g. bench_replay) Prints the functions by inclusive share (in the stack at all) and by self share (the leaf), and the leaf modules. """ import bisect, collections, itertools, os, re, shutil, stat, subprocess, sys from contextlib import ExitStack from tempfile import TemporaryFile BUILD = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "work", "sa", "build") def load_map(path, base): syms = [] pat = re.compile(r"^\s*0001:[0-9a-f]+\s+(\S+)\s+([0-9a-f]{16})\s", re.I) with open(path, encoding="utf-8", errors="replace") as f: for line in f: m = pat.match(line) if m: syms.append((int(m.group(2), 16) - base, m.group(1))) syms.sort() return [a for a, _ in syms], demangle_all([n for _, n in syms]) def demangler(): """((Triforce, 11-24, after Source A's) A C++ demangler for the Itanium names of a Windows build's maps (llvm-mingw's, as on Linux): llvm-cxxfilt or c++filt on the PATH, else HOOKSHOT_MINGW's. None: there is none""" for name in ("llvm-cxxfilt", "c++filt"): p = shutil.which(name) if p: return p d = os.environ.get("HOOKSHOT_MINGW", "") for exe in ("llvm-cxxfilt.exe", "llvm-cxxfilt"): p = os.path.join(d, "bin", exe) if d else "" if p and os.path.isfile(p): return p return None def demangle_all(names): """((Triforce, 11-24, after Source A's) names with their Itanium C++ names (_Z...) demangled and their parameter lists left out, as load_elf's nm -C gives them; any other name (C, MSVC's) as it is. Without a demangler (demangler()), all as they are""" idx = [i for i, n in enumerate(names) if n.startswith("_Z")] tool = demangler() if idx else None if not tool: return names try: r = subprocess.run([tool], input="\n".join(names[i] for i in idx) + "\n", capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=120) except (OSError, subprocess.SubprocessError): return names out = r.stdout.splitlines() if r.returncode or len(out) != len(idx): return names names = list(names) for i, n in zip(idx, out): names[i] = bare(n) return names def bare(name): """a demangled name without its parameter list (and const, noexcept after it): "ns::f(int) const" -> "ns::f"; one with none as it is ("(anonymous namespace)" inside a name stays)""" n = re.sub(r"(\s+(const|volatile|noexcept|&&|&))+$", "", name) if not n.endswith(")"): return name depth = 0 for i in range(len(n) - 1, -1, -1): depth += {")": 1, "(": -1}.get(n[i], 0) if depth == 0: return n[:i] if i else name return name # The modules whose frames are Hookshot's own code (the game and the runtime, the renderer): Windows, then Linux GAME_MODS = ("Triforce.exe", "triforce", "hookshot.exe", "hookshot") # (Triforce's names first; the older names kept for their samples) VK_MODS = ("triforce_vk.dll", "triforce_vk.so", "hookshot_vk.dll", "hookshot_vk.so") RUN_MODS = GAME_MODS + VK_MODS def load_elf(path): """An ELF's function symbols by their offset in the file's address space, which is the offset from the load address that a sample of a position-independent executable or a shared object gives (binutils' nm, demangled)""" out = subprocess.run(["nm", "--defined-only", "-C", path], capture_output=True, text=True, encoding="utf-8", errors="replace").stdout syms = [] for line in out.splitlines(): parts = line.split(" ", 2) if len(parts) == 3 and parts[1] in ("t", "T", "w", "W") and len(parts[0]) >= 8: syms.append((int(parts[0], 16), parts[2].split("(")[0])) syms.sort() return [a for a, _ in syms], [n for _, n in syms] def load_symbols(folder=None): """{module: (addresses, names)} for the modules found in folder (RX_MAPS, else the build): the linker maps of the Windows build, the binaries of the Linux one""" folder = folder or os.environ.get("RX_MAPS", BUILD) maps = {} for mod in ("triforce_vk.dll", "Triforce.exe", "hookshot_vk.dll", "hookshot.exe"): mp = os.path.join(folder, mod[:-4] + ".map") if os.path.exists(mp): maps[mod] = load_map(mp, 0x180000000 if mod.endswith(".dll") else 0x140000000) for mod in ("triforce_vk.so", "triforce", "hookshot_vk.so", "hookshot"): # ((Triforce, 11-24) its Linux names first b = os.path.join(folder, mod) if os.path.isfile(b): try: maps[mod] = load_elf(b) except OSError: # (no nm: frames stay module+offset) pass return maps def module_name(mod): """hookshot_vk / hookshot, whichever system the sample came from""" return mod.split(".")[0] def demangle(n): # ?name@ns@@... -> ns::name (enough to read) m = re.match(r"\?([^@]+)@([^@]*)@", n) if m and not n.startswith("??"): return "%s::%s" % (m.group(2), m.group(1)) if m.group(2) else m.group(1) m = re.match(r"\?\?\$?([^@]+)@", n) return m.group(1) if m else n def profile_lines(path, skip): """Keep the old line-based slice without holding the capture in memory.""" with open(path, encoding="utf-8", errors="replace") as f, ExitStack() as files: start = 0 if skip: # Windows can report a pipe as seekable, even though it cannot be replayed. if stat.S_ISREG(os.fstat(f.fileno()).st_mode) and f.seekable(): count = sum(1 for _ in f) else: # A pipe can only be read once. Save its lines on disk to keep memory bounded. spool = files.enter_context(TemporaryFile(mode="w+t", encoding="utf-8", newline="\n")) count = 0 for count, line in enumerate(f, 1): spool.write(line) f = spool start = int(count * skip) if start < 0: start = max(0, count + start) else: start = min(start, count) f.seek(0) yield from itertools.islice(f, start, None) def main(): path = sys.argv[1] top = int(sys.argv[2]) if len(sys.argv) > 2 else 40 skip = float(sys.argv[3]) if len(sys.argv) > 3 else 0.0 only = sys.argv[4] if len(sys.argv) > 4 else None maps = load_symbols() def name(frame): mod, _, off = frame.rpartition("+") if mod in maps: addrs, names = maps[mod] i = bisect.bisect_right(addrs, int(off, 16)) - 1 if i >= 0: return module_name(mod) + ":" + demangle(names[i]) return mod incl, self_, leafmod = collections.Counter(), collections.Counter(), collections.Counter() n = 0 for line in profile_lines(path, skip): frames = line.strip().split(";") if not frames or not frames[0]: continue named = [name(f) for f in frames] if only and not any(only in f for f in named): continue n += 1 self_[named[0]] += 1 leafmod[frames[0].rpartition("+")[0]] += 1 for f in set(named): incl[f] += 1 print("%d samples" % n) print("-- leaf modules") for k, v in leafmod.most_common(10): print("%6.1f%% %s" % (100.0 * v / n, k)) print("-- inclusive") for k, v in incl.most_common(top): print("%6.1f%% %s" % (100.0 * v / n, k)) print("-- self") for k, v in self_.most_common(top): print("%6.1f%% %s" % (100.0 * v / n, k)) if __name__ == "__main__": main()