mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
116 lines
3.9 KiB
Python
116 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Bound PT decode output/errors; preserve raw data and declare incomplete decode."""
|
|
import argparse
|
|
import collections
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import signal
|
|
import subprocess
|
|
import time
|
|
|
|
SYMBOLS = [
|
|
"_nv000790rm", "_nv030241rm", "_nv033663rm", "_nv033665rm",
|
|
"_nv016741rm", "_nv016753rm", "_nv028470rm", "_nv043236rm",
|
|
"_nv043182rm", "_nv027605rm", "_nv029864rm", "_nv033231rm",
|
|
"_nv033300rm", "_nv055403rm", "_nv028309rm", "_nv028308rm",
|
|
"_nv043609rm",
|
|
]
|
|
|
|
|
|
def _positive_int(value):
|
|
parsed = int(value)
|
|
if parsed < 1:
|
|
raise argparse.ArgumentTypeError("must be >= 1")
|
|
return parsed
|
|
|
|
|
|
def build_parser():
|
|
p = argparse.ArgumentParser(description=__doc__)
|
|
p.add_argument("--input", required=True)
|
|
p.add_argument("--kallsyms", required=True)
|
|
p.add_argument("--output", type=Path, required=True)
|
|
p.add_argument("--time")
|
|
p.add_argument("--max-lines", type=_positive_int, default=20000)
|
|
p.add_argument("--max-errors", type=_positive_int, default=100)
|
|
p.add_argument("--all-symbols", action="store_true")
|
|
p.add_argument("--itrace", choices=["ce", "be"], default="ce")
|
|
return p
|
|
|
|
|
|
def main(argv=None):
|
|
a = build_parser().parse_args(argv)
|
|
a.output.parent.mkdir(parents=True, exist_ok=True)
|
|
command = [
|
|
"perf", "script", "-i", a.input, "--itrace=" + a.itrace, "--ns",
|
|
"-F", "comm,pid,tid,time,ip,sym,symoff,addr,dso,flags",
|
|
"--kallsyms", a.kallsyms,
|
|
]
|
|
if not a.all_symbols:
|
|
command += ["-S", ",".join(SYMBOLS)]
|
|
if a.time:
|
|
command += ["--time", a.time]
|
|
|
|
lines = 0
|
|
errors = 0
|
|
error_codes = collections.Counter()
|
|
stop = None
|
|
status = -1
|
|
start = time.monotonic()
|
|
errpath = a.output.with_suffix(".stderr")
|
|
reportpath = a.output.with_suffix(".json")
|
|
|
|
with a.output.open("w") as f, errpath.open("w") as err:
|
|
try:
|
|
child = subprocess.Popen(
|
|
command, stdout=subprocess.PIPE, stderr=err, text=True, start_new_session=True
|
|
)
|
|
except OSError as exc:
|
|
raise SystemExit(f"failed to launch perf: {exc}") from exc
|
|
try:
|
|
if child.stdout is None:
|
|
raise RuntimeError("perf subprocess stdout pipe unavailable")
|
|
for line in child.stdout:
|
|
f.write(line)
|
|
lines += 1
|
|
if "instruction trace error" in line:
|
|
errors += 1
|
|
match = re.search(r"code (\d+):", line)
|
|
error_codes[match.group(1) if match else "unknown"] += 1
|
|
if errors >= a.max_errors or lines >= a.max_lines:
|
|
stop = "error_limit" if errors >= a.max_errors else "line_limit"
|
|
child.send_signal(signal.SIGINT)
|
|
break
|
|
finally:
|
|
if child.stdout is not None:
|
|
child.stdout.close()
|
|
try:
|
|
status = child.wait(timeout=10)
|
|
except subprocess.TimeoutExpired:
|
|
child.kill()
|
|
status = child.wait()
|
|
stop = "decoder_shutdown_timeout"
|
|
|
|
report = {
|
|
"command": command,
|
|
"exit_code": status,
|
|
"stop": stop,
|
|
"output_lines": lines,
|
|
"trace_errors": errors,
|
|
"error_codes": dict(error_codes),
|
|
"instruction_mismatch_errors": error_codes.get("6", 0),
|
|
"elapsed_seconds": time.monotonic() - start,
|
|
"scope": (
|
|
"Bounded decoder observation only. A size-limited source capture, "
|
|
"interrupted/error decode or limited requested time cannot establish absent events."
|
|
),
|
|
}
|
|
reportpath.write_text(json.dumps(report, indent=2) + "\n")
|
|
print(json.dumps(report))
|
|
if stop == "decoder_shutdown_timeout" or (stop not in ("line_limit", "error_limit") and status != 0):
|
|
raise SystemExit(status if status > 0 else 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|