# SPDX-License-Identifier: MIT """Summarise a MAME trace written by scripts/mame_trace.py (plain or .gz). python -I scripts/trace_stats.py reference/mame/salmndr2-main/error.log --clock 24e6 Prints the instructions traced, the cycle span, the frames covered, instructions per emulated second (cycles / clock gives the time), cycles per instruction, the share of instructions whose PC lies in each 1 MB page, and the most frequent PCs (idle loops show up there). A closed-box observation of MAME's run, not of the hardware. """ from __future__ import annotations import argparse import collections import gzip def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("log") ap.add_argument("--clock", type=float, required=True, help="the CPU's clock in Hz") ap.add_argument("--top", type=int, default=8) a = ap.parse_args() opener = gzip.open if a.log.endswith(".gz") else open n = 0 first = last = None pages = collections.Counter() pcs = collections.Counter() with opener(a.log, "rt", errors="replace") as handle: for line in handle: if not line.startswith("T "): continue f = line.split() pc = int(f[1], 16) cycles, frame = int(f[-3]), int(f[-2]) if first is None: first = (cycles, frame) last = (cycles, frame) n += 1 pages[pc >> 20] += 1 pcs[pc] += 1 if not n: print("no T lines") return 1 span = last[0] - first[0] seconds = span / a.clock print(f"instructions {n:,} cycles {span:,} frames {first[1]}-{last[1]} " f"emulated {seconds:.3f} s") print(f"instructions/s {n / seconds:,.0f} cycles/instruction {span / n:.2f}") print("pages (MB): " + ", ".join(f"{p:X}xxxxx {c / n:.1%}" for p, c in sorted(pages.items()))) print("top PCs: " + ", ".join(f"{pc:X} {c / n:.1%}" for pc, c in pcs.most_common(a.top))) return 0 if __name__ == "__main__": raise SystemExit(main())