# Extract the per-run chip command sequence (netcmd lines) from the serial log. # usage: python MDK-ARM/netcmd_extract.py [max_lines] # run_index = 1-based run position in the session (TS lines in the runlog delimit # each run's device-time window; PC local = device UTC + 8h, 4s margins). import re, sys from datetime import datetime, timezone runlog, seriallog = sys.argv[1], sys.argv[2] base = int(sys.argv[3]) maxl = int(sys.argv[4]) if len(sys.argv) > 4 else 300 nextrunlog = sys.argv[5] if (len(sys.argv) > 5 and sys.argv[5] not in ("focus", "plain")) else None focus = any(a == "focus" for a in sys.argv[5:]) ts = [] for raw in open(runlog, encoding="utf-8-sig", errors="replace"): m = re.match(r"TS (\d+)", raw.strip()) if m: ts.append(int(m.group(1))) ts_next = None if nextrunlog: for raw in open(nextrunlog, encoding="utf-8-sig", errors="replace"): m = re.match(r"TS (\d+)", raw.strip()) if m: ts_next = int(m.group(1)) break if base - 1 >= len(ts): print("run_index out of range (%d TS lines)" % len(ts)) sys.exit(2) lo = ts[base - 1] - 4.0 hi = (ts_next - 4.0) if ts_next is not None else (lo + 600.0) CMD_RE = re.compile(r"netcmd: (\S+) fd=(-?\d+) t=(\d+) d=(\d+) x=(-?\d+)") DEV_RE = re.compile(r"\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\]\[") n = 0 for raw in open(seriallog, encoding="utf-8", errors="replace"): m = CMD_RE.search(raw) if not m: continue t = DEV_RE.search(raw) if not t: continue dt = datetime.strptime(t.group(1), "%Y-%m-%d %H:%M:%S").replace(tzinfo=timezone.utc).timestamp() if lo <= dt < hi: tag, fd, x = m.group(1), m.group(2), m.group(5) if focus: # 只留数据 socket 的 R/S 与 poll/事件行;A(各服务 accept 轮询洪水)与 # 稳态空读(phase6 每 tick 状态读)全部降噪 if tag == "R" and fd == "2" and x == "-1": continue if tag not in ("R", "S", "P", "p", "a", "d", "x"): continue if tag in ("R", "S") and fd != "2": continue n += 1 if n <= maxl: print("%s fd=%s t=%s d=%s x=%s" % (m.group(1), m.group(2), m.group(3), m.group(4), m.group(5))) print("netcmd lines in run window: %d" % n)