refactor(test): 把 test/host 顶层 25 个台架脚本按域收进 bench/
原来 test/host 一层混了三类东西:51 个主机侧单测模块目录、25 个台架驱动 与探针、若干专用目录。台架脚本平铺在顶层,与单测目录并列,看不出哪个是 「连板跑一次的」,也看不出加新脚本该往哪放。 现在按被验对象分域归档,单测模块目录一律不动(22 根 pre-commit 钩子逐个 点名它们,50 个运行器按固定层数推仓库根): - bench/bench_defaults.py 台架连接默认值单一真源 - bench/ftp/ (10) FTP 功能、速率、鉴权、中止、完整性、背靠背复现 - bench/net/ (9) CH395F socket、keepalive 判死、挂机、复连压力、PASV、 UDP 队列、链路时延对端 - bench/iec104/ (2) 主站客户端与它的离线样帧自检 - bench/wave/ (1) 录波段文件 PC 侧解码 - bench/screenlink/ (2) 屏板链路顶替与拉帧压测 顶层条目 78 -> 53。逐脚本作用、运行前置与「加新脚本往哪放」写在新建的 test/host/bench/README.md;test/host/README.md 增目录地图表,把原平铺的 网络/FTP 两节压成指针。 配套修正(移动后不修就会坏): - 17 个脚本加上 bench/ 到 sys.path 的 bootstrap(sys.path[0] 是脚本自身 目录,不补就 import bench_defaults 失败) - cal/cal_write.py 本来就坏的(只插 cal/ 到自己 sys.path,import bench_defaults 直接 ModuleNotFoundError),补两段路径后可用 - e6_soak 与 wave_read 用 dirname 固定层数推仓库根,多两层后分别指向 test/test 与 test/ci-artifacts(后者静默写错目录、不报错),层数已改对 - scripts/board_ui_nav.py 与 MDK-ARM 两个探针的 bench_defaults 搜索路径 清理与标注:删掉 test/host/iec104/(旧自研协议层单测退役后只剩一个零引用 的 stub 桩头);文档里对 test/host/iec104 与 iec104_serv 的引用加上「已 删除/已不存在 2026-11-18」状态词。冻结历史证据(带日期戳的台架日志)与 编号别名登记表里的旧路径按原样保留。
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
# relisten 沉降段原语化受阻:芯片级复用时序台架主机端(与 test/board/ch395f_reuse_bench.c 共用变体表)
|
||||
# 用法: python ch395f_reuse_host.py [--host IP] [--cycles N] [--mode legacy|e4]
|
||||
# legacy: 按 idx 轮转连接 5000+idx 端口,记录收发结果与时刻。
|
||||
# e4: E4 sock_reset A/B/C 矩阵配套(2026-10-13):每轮两次连接——
|
||||
# ①污染:连上发 3000B 后 RST(SO_LINGER 0,TRAP-NET-18 中止形态);
|
||||
# ②载荷:立即重连发 4096B 图案(pat[i]=(seed+i)&0xFF, seed=(rg*97+13)&0xFF)后 FIN。
|
||||
# 端口 5100+rg,rg=0..(3*rounds-1),变体顺序 A/B/C(与固件 bench 表一致)。
|
||||
import os
|
||||
import argparse
|
||||
import socket
|
||||
import struct
|
||||
import sys
|
||||
import time
|
||||
|
||||
# 台架连接默认值单一真源已随域归档到 test/host/bench/bench_defaults.py:
|
||||
# Python 的 sys.path[0] 是**脚本自身所在目录**,本脚本在 bench/<域>/ 下直接
|
||||
# `import bench_defaults` 会 ModuleNotFoundError —— 先把 bench/ 补进 sys.path。
|
||||
_BENCH_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
if _BENCH_DIR not in sys.path:
|
||||
sys.path.insert(0, _BENCH_DIR)
|
||||
|
||||
import bench_defaults # noqa: E402 (台架连接默认值单一真源)
|
||||
|
||||
# {idx: (tx_len, close_mode)} 与固件表一致
|
||||
VARS = {
|
||||
0: (100, 0), # 0ms control
|
||||
1: (100, 0), # 300ms control
|
||||
2: (2000, 0), # 2KB control
|
||||
3: (100, 0), # 0ms + ghost-wait fix
|
||||
4: (100, 0), # 300ms + ghost-wait fix
|
||||
5: (2000, 0), # 2KB + ghost-wait fix
|
||||
}
|
||||
|
||||
|
||||
def ts():
|
||||
return time.strftime("%H:%M:%S") + ".%03d" % (int(time.time() * 1000) % 1000)
|
||||
|
||||
|
||||
def run_round(host, idx, tx_len, close_mode):
|
||||
port = 5000 + idx
|
||||
# 等端口监听(芯片还没 open 时连接被拒,重试)
|
||||
s = None
|
||||
t0 = time.time()
|
||||
while time.time() - t0 < 12.0:
|
||||
try:
|
||||
s = socket.create_connection((host, port), timeout=5)
|
||||
break
|
||||
except OSError:
|
||||
time.sleep(0.2)
|
||||
if s is None:
|
||||
print("%s r%u CONNECT timeout" % (ts(), idx))
|
||||
return False
|
||||
|
||||
ok = True
|
||||
try:
|
||||
s.settimeout(5)
|
||||
got = 0
|
||||
while got < tx_len:
|
||||
chunk = s.recv(min(1024, tx_len - got))
|
||||
if not chunk:
|
||||
break
|
||||
got += len(chunk)
|
||||
note = ""
|
||||
if got != tx_len:
|
||||
note = " <<<< SWALLOWED (want %d)" % tx_len
|
||||
ok = False
|
||||
if close_mode == 1:
|
||||
# 等芯片 FIN(mode1 芯片先关)
|
||||
try:
|
||||
fin = s.recv(1)
|
||||
if fin:
|
||||
note += " unexpected data after tx: %r" % fin
|
||||
except socket.timeout:
|
||||
note += " <<<< no FIN from chip (mode1)"
|
||||
ok = False
|
||||
print("%s r%u connected got=%d/%d%s" % (ts(), idx, got, tx_len, note))
|
||||
except socket.timeout:
|
||||
print("%s r%u recv timeout <<<< STALL" % (ts(), idx))
|
||||
ok = False
|
||||
except OSError as e:
|
||||
print("%s r%u sock error %r" % (ts(), idx, e))
|
||||
ok = False
|
||||
finally:
|
||||
s.close() # 主机关闭(mode0/2 主机先关)
|
||||
return ok
|
||||
|
||||
|
||||
def connect_retry(host, port, budget_s):
|
||||
"""connect with retry until budget_s elapses; None on give-up"""
|
||||
t0 = time.time()
|
||||
while time.time() - t0 < budget_s:
|
||||
try:
|
||||
return socket.create_connection((host, port), timeout=5)
|
||||
except OSError:
|
||||
time.sleep(0.2)
|
||||
return None
|
||||
|
||||
|
||||
def e4_round(host, rg, rounds_per_variant, first_budget):
|
||||
"""one E4 round: contaminate (send 3000B then RST) + payload (4096B pattern then FIN)"""
|
||||
port = 5100 + rg
|
||||
seed = (rg * 97 + 13) & 0xFF
|
||||
budget = first_budget if rg == 0 else 30
|
||||
s = connect_retry(host, port, budget)
|
||||
if s is None:
|
||||
print("%s E4 r%2d port%5d <<<< contam CONNECT timeout" % (ts(), rg, port))
|
||||
return False
|
||||
try:
|
||||
s.sendall(b"\x33" * 3000)
|
||||
except OSError as e:
|
||||
print("%s E4 r%2d contam send err %r" % (ts(), rg, e))
|
||||
try:
|
||||
s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0))
|
||||
except OSError:
|
||||
pass
|
||||
s.close() # RST: abort mid-session like ftp_abort_probe (TRAP-NET-18 form)
|
||||
time.sleep(0.05)
|
||||
|
||||
s = connect_retry(host, port, 20)
|
||||
if s is None:
|
||||
print("%s E4 r%2d port%5d <<<< payload CONNECT timeout" % (ts(), rg, port))
|
||||
return False
|
||||
payload = bytes(((seed + i) & 0xFF) for i in range(4096))
|
||||
try:
|
||||
s.sendall(payload)
|
||||
except OSError as e:
|
||||
print("%s E4 r%2d payload send err %r" % (ts(), rg, e))
|
||||
s.close()
|
||||
return False
|
||||
s.close() # FIN: full payload delivered, chip drains
|
||||
print("%s E4 r%2d var=%s port%5d contam+payload sent (seed=%02X)"
|
||||
% (ts(), rg, "ABC"[rg // rounds_per_variant], port, seed))
|
||||
return True
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--host", default=bench_defaults.DEVICE_IP)
|
||||
ap.add_argument("--cycles", type=int, default=3)
|
||||
ap.add_argument("--mode", choices=["legacy", "e4"], default="legacy")
|
||||
ap.add_argument("--rounds", type=int, default=10, help="e4: rounds per variant")
|
||||
ap.add_argument("--first-budget", type=int, default=300,
|
||||
help="e4: connect budget for the first round (bench runs E2/E3 first)")
|
||||
args = ap.parse_args()
|
||||
|
||||
if args.mode == "e4":
|
||||
fails = []
|
||||
total = 3 * args.rounds
|
||||
for rg in range(total):
|
||||
if not e4_round(args.host, rg, args.rounds, args.first_budget):
|
||||
fails.append(rg)
|
||||
print("E4 RESULT: %d/%d host-side ok, failures %s" % (total - len(fails), total, fails))
|
||||
return 1 if fails else 0
|
||||
|
||||
fails = []
|
||||
for c in range(args.cycles):
|
||||
for idx in sorted(VARS):
|
||||
tx_len, mode = VARS[idx]
|
||||
print("%s === cycle%u r%u (tx=%d mode=%d) ===" % (ts(), c, idx, tx_len, mode))
|
||||
if not run_round(args.host, idx, tx_len, mode):
|
||||
fails.append((c, idx))
|
||||
time.sleep(0.3)
|
||||
print("RESULT: %d failures %s" % (len(fails), fails))
|
||||
return 1 if fails else 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,194 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""E11:CH395F 服务器侧 KeepAlive —— "链路在、对端静默"判死验证(PC 侧注入)。
|
||||
|
||||
背景(E11):手册 §5.56 规定 TCP 服务器应在 SINT_STAT_CONNECT 后开启
|
||||
KeepAlive;本仓该调用 2026-10-12 被撤回,全仓仅剩客户端分支启用,而
|
||||
App/ 无任何 net_connect() → 产品上 keepalive 实际关闭。本脚本用于验证
|
||||
"开启后能否在有界时间内判死静默对端,且不误杀正常空闲会话"。
|
||||
|
||||
用法:
|
||||
# 场景「正常空闲存活」:连接后静默 hold 秒,末尾发 KEEPLIVE 验证会话仍在
|
||||
python e11_keepalive_probe.py --mode idle --hold-s 90
|
||||
|
||||
# 场景「静默对端」:连接后立即装 Windows 防火墙入站 DROP(仅本连接本地端口 +
|
||||
# 设备 IP)→ 芯片 keepalive 探测得不到 ACK → 期望设备在
|
||||
# IDLE + INTVL×CNT 内判死(本仓 60 + 5x3 = 75s)
|
||||
python e11_keepalive_probe.py --mode block --hold-s 120
|
||||
|
||||
判据(设备侧事件由串口日志核对,本脚本打印 PC 侧时间线):
|
||||
期望:串口出现 `[ERR][NET] sockN TIMEOUT`(耗时≈75s),随后
|
||||
`sockN pump/relisten` 恢复;本脚本移除规则后新连接必须成功。
|
||||
期望:hold 期间**无** TIMEOUT;末尾 KEEPLIVE 必须收到 0xBB 回显。
|
||||
|
||||
需要管理员权限(仅 block 模式装/卸防火墙规则;脚本自行清理)。
|
||||
"""
|
||||
|
||||
import os
|
||||
import argparse
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
# 台架连接默认值单一真源已随域归档到 test/host/bench/bench_defaults.py:
|
||||
# Python 的 sys.path[0] 是**脚本自身所在目录**,本脚本在 bench/<域>/ 下直接
|
||||
# `import bench_defaults` 会 ModuleNotFoundError —— 先把 bench/ 补进 sys.path。
|
||||
_BENCH_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
if _BENCH_DIR not in sys.path:
|
||||
sys.path.insert(0, _BENCH_DIR)
|
||||
|
||||
import bench_defaults # noqa: E402 (台架连接默认值单一真源)
|
||||
|
||||
TAG_C, TAG_D = 0xAA, 0xBB
|
||||
CMD_KEEPLIVE = 0
|
||||
RULE_NAME = "SELFHEAL-E11-DROP"
|
||||
|
||||
|
||||
def _crc(data: bytes) -> int:
|
||||
c = 0
|
||||
for b in data:
|
||||
c ^= b
|
||||
return c
|
||||
|
||||
|
||||
def build_frame(cmd: int, data: bytes = b"") -> bytes:
|
||||
n = len(data)
|
||||
return bytes([TAG_C, cmd, (n >> 8) & 0xFF, n & 0xFF]) + data + bytes([_crc(data)])
|
||||
|
||||
|
||||
def recv_frame(sock: socket.socket, timeout: float = 3.0):
|
||||
sock.settimeout(timeout)
|
||||
hdr = b""
|
||||
while len(hdr) < 4:
|
||||
chunk = sock.recv(4 - len(hdr))
|
||||
if not chunk:
|
||||
return None
|
||||
hdr += chunk
|
||||
tag, cmd, n = hdr[0], hdr[1], (hdr[2] << 8) | hdr[3]
|
||||
body = b""
|
||||
while len(body) < n + 1:
|
||||
chunk = sock.recv(n + 1 - len(body))
|
||||
if not chunk:
|
||||
return None
|
||||
body += chunk
|
||||
data, crcb = body[:n], body[n]
|
||||
return cmd, data, (tag == TAG_D) and (crcb == _crc(data))
|
||||
|
||||
|
||||
def firewall(action: str, local_port: int, remote_ip: str) -> bool:
|
||||
verb = "add" if action == "add" else "delete"
|
||||
cmd = ["netsh", "advfirewall", "firewall", verb, "rule",
|
||||
f"name={RULE_NAME}", "dir=in", "action=block", "protocol=TCP",
|
||||
f"localport={local_port}", f"remoteip={remote_ip}"]
|
||||
if verb == "delete":
|
||||
cmd = ["netsh", "advfirewall", "firewall", "delete", "rule",
|
||||
f"name={RULE_NAME}"]
|
||||
r = subprocess.run(cmd, capture_output=True, text=True)
|
||||
if r.returncode != 0:
|
||||
print(f"[WARN] 防火墙 {verb} 失败:{r.stdout.strip()} {r.stderr.strip()}")
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def connect(host: str, port: int, timeout: float = 5.0):
|
||||
s = socket.create_connection((host, port), timeout)
|
||||
return s
|
||||
|
||||
|
||||
def main() -> int:
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--host", default=bench_defaults.DEVICE_IP)
|
||||
ap.add_argument("--port", type=int, default=7003)
|
||||
ap.add_argument("--mode", choices=["idle", "block"], required=True)
|
||||
ap.add_argument("--hold-s", type=float, default=90.0)
|
||||
ap.add_argument("--expect-detect-s", type=float, default=75.0,
|
||||
help="判死上界(IDLE + INTVL*CNT),仅用于提示")
|
||||
args = ap.parse_args()
|
||||
|
||||
t0 = time.time()
|
||||
|
||||
def log(msg: str) -> None:
|
||||
print(f"[{time.strftime('%H:%M:%S')} +{time.time() - t0:6.1f}s] {msg}",
|
||||
flush=True)
|
||||
|
||||
log(f"模式={args.mode} 目标={args.host}:{args.port} hold={args.hold_s}s")
|
||||
log(f"(设备侧判死上界参考 = {args.expect_detect_s}s)")
|
||||
|
||||
try:
|
||||
s = connect(args.host, args.port)
|
||||
except OSError as e:
|
||||
print(f"[FAIL] 连接失败:{e}")
|
||||
return 2
|
||||
s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 0) # PC 侧不参与
|
||||
lport = s.getsockname()[1]
|
||||
log(f"已连接(本地端口 {lport})→ 进入静默")
|
||||
|
||||
rc = 0
|
||||
if args.mode == "block":
|
||||
# 入站 DROP:设备发来的 keepalive 探测包在 PC 侧被丢弃 → 永不回 ACK
|
||||
if not firewall("add", lport, args.host):
|
||||
s.close()
|
||||
return 2
|
||||
log(f"已装防火墙入站 DROP(本地端口 {lport} <- {args.host})→ 对端静默开始")
|
||||
|
||||
try:
|
||||
time.sleep(args.hold_s)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
if args.mode == "idle":
|
||||
# 存活探针:KEEPLIVE 必须被回显
|
||||
log("静默结束 → 发 KEEPLIVE 验证会话存活")
|
||||
try:
|
||||
s.sendall(build_frame(CMD_KEEPLIVE))
|
||||
r = recv_frame(s, 3.0)
|
||||
except OSError as e:
|
||||
r = None
|
||||
log(f"KEEPLIVE 发送/接收异常:{e}")
|
||||
if r and r[2]:
|
||||
log("PASS:收到 0xBB KEEPLIVE 回显 → 正常空闲会话未被误杀")
|
||||
else:
|
||||
log(f"FAIL:未收到有效回显({r})→ 空闲置的会话被断开(误杀)")
|
||||
rc = 1
|
||||
s.close()
|
||||
return rc
|
||||
|
||||
# block 模式:卸规则 → 验证旧连接已死 + 监听器已恢复
|
||||
log("静默结束 → 卸载防火墙规则")
|
||||
firewall("delete", lport, args.host)
|
||||
time.sleep(1.0)
|
||||
|
||||
# 旧连接是否已被设备判死(应读到 EOF/RST;读不到也算进程侧正常)
|
||||
try:
|
||||
s.settimeout(2.0)
|
||||
d = s.recv(16)
|
||||
log(f"旧连接 recv → {d!r}(b'' = 已收到 FIN,即设备已判死并释放)")
|
||||
except OSError as e:
|
||||
log(f"旧连接 recv 异常(设备已判死/连接被拆):{e}")
|
||||
s.close()
|
||||
|
||||
# 监听器恢复验证:新连接 + KEEPLIVE 回显
|
||||
log("验证监听器恢复:新建连接 + KEEPLIVE")
|
||||
ok = False
|
||||
for attempt in range(1, 6):
|
||||
try:
|
||||
s2 = connect(args.host, args.port)
|
||||
s2.sendall(build_frame(CMD_KEEPLIVE))
|
||||
r = recv_frame(s2, 3.0)
|
||||
s2.close()
|
||||
if r and r[2]:
|
||||
log(f"PASS:第 {attempt} 次新连接建立并收到回显 → 监听器已恢复")
|
||||
ok = True
|
||||
break
|
||||
except OSError as e:
|
||||
log(f"第 {attempt} 次新连接失败:{e}")
|
||||
time.sleep(2.0)
|
||||
if not ok:
|
||||
log("FAIL:监听器未恢复(端口仍不接客)")
|
||||
rc = 1
|
||||
return rc
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""E6 24h soak driver (U15 mismatch frequency, self-heal design anchor E6).
|
||||
|
||||
Protocol (docs/网络/网络自愈架构设计.md §4.8, anchor §5.2 E6):
|
||||
24h normal operation + periodic E1 stress injection:
|
||||
- every hour : relisten_stress :7003 n=10 gap=100 (UI-like churn)
|
||||
- every 4th hour : full E1 (:7003 n=50 gap=0 + :21 n=50 gap=100)
|
||||
Device-side confirmed hits are counted by the NET_U15_PROBE firmware and
|
||||
appear in the serial log as "U15 probe: sockN rx=... hits=N" (first hit
|
||||
immediately, then <=1/hour). Read the verdict from serial.log afterwards.
|
||||
|
||||
Usage: python e6_soak.py [--hours 24]
|
||||
Exit: 0 = soak completed; nonzero = driver error (device untouched).
|
||||
"""
|
||||
import argparse
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
# 本脚本归档在 test/host/bench/net/:向上 5 级才到仓库根(多出 bench/ 与域目录两层)。
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(
|
||||
os.path.abspath(__file__))))))
|
||||
STRESS = os.path.join(ROOT, "test", "host", "bench", "net", "relisten_stress.py")
|
||||
|
||||
|
||||
def log(msg):
|
||||
print("[%s] %s" % (time.strftime("%m-%d %H:%M:%S"), msg), flush=True)
|
||||
|
||||
|
||||
def burst(args_list, tag):
|
||||
t0 = time.time()
|
||||
try:
|
||||
r = subprocess.run([sys.executable, STRESS] + args_list,
|
||||
capture_output=True, text=True, timeout=900)
|
||||
out = (r.stdout or "") + (r.stderr or "")
|
||||
tail = [ln for ln in out.splitlines() if "ok=" in ln or "latency" in ln]
|
||||
dt = time.time() - t0
|
||||
log("%s rc=%d %.0fs %s" % (tag, r.returncode, dt, " | ".join(tail[-2:])))
|
||||
except Exception as e: # driver hiccup must not abort the soak
|
||||
log("%s ERROR %r" % (tag, e))
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--hours", type=int, default=24)
|
||||
a = ap.parse_args()
|
||||
|
||||
log("E6 soak start: %d h, hourly :7003x10, every-4h E1 full" % a.hours)
|
||||
t_start = time.time()
|
||||
for h in range(1, a.hours + 1):
|
||||
# sleep first: hour boundary cadence
|
||||
remain = t_start + h * 3600.0 - time.time()
|
||||
if remain > 0:
|
||||
time.sleep(remain)
|
||||
burst(["--port", "7003", "--n", "10", "--gap-ms", "100"], "hour%02d :7003x10" % h)
|
||||
if h % 4 == 0:
|
||||
burst(["--port", "7003", "--n", "50", "--gap-ms", "0"], "hour%02d E1 :7003x50g0" % h)
|
||||
burst(["--port", "21", "--n", "50", "--gap-ms", "100"], "hour%02d E1 :21x50g100" % h)
|
||||
log("E6 soak done: %.1f h elapsed" % ((time.time() - t_start) / 3600.0))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,848 @@
|
||||
#!/usr/bin/env python3
|
||||
"""latency_peer.py - Link latency test peer for Phase 13 (M2).
|
||||
|
||||
Server-driven protocol: the MCU (testTask, port 8080) sends one operation
|
||||
byte; the peer executes the corresponding traffic pattern. See
|
||||
docs/测试说明/专项验证/链路延迟测试说明.md §4.5 for the full protocol spec.
|
||||
|
||||
Ops (MCU -> peer):
|
||||
0x01 QUEUE_RT recv flood : peer sends 50 x 100B (5ms apart)
|
||||
0x02 QUEUE_RT send drain : peer drains 50 x 100B
|
||||
0x03 ECHO_RT : peer sends 10B request, expects 10B echo (timed)
|
||||
0x04 FRAME_RT : peer sends 9B LCDMEM request, expects 3209B frame
|
||||
0x05 CHUNK_WAIT send drain : peer drains 32 x 4096B
|
||||
0xFF done : session complete
|
||||
|
||||
The peer records host-side (TO_PC-side) timing: first byte / frame total /
|
||||
per-batch arrival curve / >=500ms spikes (TCP retransmission fingerprint).
|
||||
Both the serial log and this JSON carry HOST timestamps -> direct correlation.
|
||||
|
||||
Usage:
|
||||
python latency_peer.py --port 8080 --json m2_net.json
|
||||
python latency_peer.py --mode net --ftp-stress ... (head-of-line repro)
|
||||
|
||||
Pure stdlib (socket/json/time/argparse/threading). ASCII output only.
|
||||
"""
|
||||
|
||||
import os
|
||||
import argparse
|
||||
import json
|
||||
import socket
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
|
||||
# 台架连接默认值单一真源已随域归档到 test/host/bench/bench_defaults.py:
|
||||
# Python 的 sys.path[0] 是**脚本自身所在目录**,本脚本在 bench/<域>/ 下直接
|
||||
# `import bench_defaults` 会 ModuleNotFoundError —— 先把 bench/ 补进 sys.path。
|
||||
_BENCH_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
if _BENCH_DIR not in sys.path:
|
||||
sys.path.insert(0, _BENCH_DIR)
|
||||
|
||||
import bench_defaults # noqa: E402 (台架连接默认值单一真源)
|
||||
|
||||
# --- Protocol constants (mirror test/board/net_latency_test.c Phase 13 defines) ---
|
||||
HELLO = b"\x5a\xa5\x58\x01"
|
||||
OP_QUEUE_RT_RECV = 0x01
|
||||
OP_QUEUE_RT_SEND = 0x02
|
||||
OP_ECHO_RT = 0x03
|
||||
OP_FRAME_RT = 0x04
|
||||
OP_CHUNK_WAIT = 0x05
|
||||
OP_DONE = 0xFF
|
||||
|
||||
QUEUE_RT_CHUNKS = 20 # 2026-09-22 run1: 50 chunks saturated the chip RX buffer
|
||||
# (window close -> retransmit re-delivery -> RST); 20 @ 50ms
|
||||
# = 1KB/s quiet baseline (see 链路延迟测试说明.md §6.2)
|
||||
QUEUE_RT_CHUNK_LEN = 100
|
||||
ECHO_RT_SIZE = 10
|
||||
FRAME_RT_REQ_LEN = 9
|
||||
FRAME_RT_FRAME_LEN = 3209
|
||||
CHUNK_WAIT_SENDS = 16 # mirror firmware LAT_P13_CHUNK_WAIT_SENDS (run3: 32->16, 64 chunk samples)
|
||||
CHUNK_WAIT_LEN = 4096
|
||||
|
||||
SPIKE_MS = 500.0 # retransmission granularity fingerprint (D1)
|
||||
|
||||
# --- Production RemoDispBus constants (mirror drivers/BSP/UI/ui_net_sink.h) ---
|
||||
REMO_TAG_CLIENT = 0xAA
|
||||
REMO_TAG_DEVICE = 0xBB
|
||||
REMO_CMD_KEEP = 0
|
||||
REMO_CMD_INIT = 1
|
||||
REMO_CMD_KEY = 2
|
||||
REMO_CMD_LCDMEM = 3
|
||||
REMO_MEMSIZE = 3200 # 160x160 1bpp
|
||||
REMO_LCDMEM_FRAME = 4 + 4 + REMO_MEMSIZE + 1 # 3209 (header + offset + bitmap + CRC)
|
||||
REMO_CYCLE_DEADLINE_S = 45.0 # per-cycle recv deadline (head-of-line stress: up to 30s)
|
||||
|
||||
|
||||
def now_ns():
|
||||
return time.perf_counter_ns()
|
||||
|
||||
|
||||
def iso_now():
|
||||
return time.strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
|
||||
def recv_exact(sock, n, deadline_ns):
|
||||
buf = bytearray()
|
||||
while len(buf) < n:
|
||||
left = deadline_ns - now_ns()
|
||||
if left <= 0:
|
||||
raise TimeoutError("recv_exact deadline (%d/%d bytes)" % (len(buf), n))
|
||||
try:
|
||||
sock.settimeout(max(left, 1) / 1e9 / 5.0)
|
||||
d = sock.recv(n - len(buf))
|
||||
except socket.timeout:
|
||||
continue
|
||||
if not d:
|
||||
raise ConnectionError("peer connection closed")
|
||||
buf.extend(d)
|
||||
return bytes(buf)
|
||||
|
||||
|
||||
def frame_rt_request(seq=0):
|
||||
"""Production-shaped 9B LCDMEM request (ui_net_sink.c remo_handle):
|
||||
[TAG 0xAA][CMD 0x03][LEN=4 BE][offset u32 BE = 0][CRC = XOR of the 4 offset bytes].
|
||||
LEN is the DATA-segment length (4), NOT the requested bitmap size; the offset
|
||||
field must be 0 (device only serves full-frame requests)."""
|
||||
data = bytes([0, 0, 0, 0])
|
||||
crc = 0
|
||||
for b in data:
|
||||
crc ^= b
|
||||
return bytes([0xAA, 0x03, 0x00, 0x04]) + data + bytes([crc])
|
||||
|
||||
|
||||
def frame_rt_ok(data):
|
||||
"""Validate the 3209B production LCDMEM response (ui_net_sink.c remo_send):
|
||||
[TAG 0xBB][CMD 0x03][LEN=3204 BE][offset u32 = 0][1bpp bitmap 3200B]
|
||||
[CRC = XOR of the 3204 data bytes] -> 4 + 3204 + 1 = 3209B."""
|
||||
if len(data) != FRAME_RT_FRAME_LEN:
|
||||
return False
|
||||
if data[0] != 0xBB or data[1] != 0x03 or data[2] != 0x0C or data[3] != 0x84:
|
||||
return False
|
||||
payload = data[4:4 + 3204]
|
||||
crc = 0
|
||||
for b in payload:
|
||||
crc ^= b
|
||||
if data[4 + 3204] != crc:
|
||||
return False
|
||||
if payload[:4] != bytes(4):
|
||||
return False
|
||||
for i in range(3200):
|
||||
if payload[4 + i] != (i & 0xFF):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def pattern(n, seed=0):
|
||||
return bytes((seed + i) & 0xFF for i in range(n))
|
||||
|
||||
|
||||
# ================= RemoDispBus production client (M3, :7003) =================
|
||||
|
||||
def xor_bytes(data):
|
||||
crc = 0
|
||||
for b in data:
|
||||
crc ^= b
|
||||
return crc
|
||||
|
||||
|
||||
def remo_request(cmd, data=b""):
|
||||
"""Client->device frame: [0xAA][CMD][LEN_H][LEN_L][DATA][CRC = XOR(DATA)]."""
|
||||
body = bytes(data)
|
||||
return (bytes([REMO_TAG_CLIENT, cmd, (len(body) >> 8) & 0xFF, len(body) & 0xFF])
|
||||
+ body + bytes([xor_bytes(body)]))
|
||||
|
||||
|
||||
def remo_lcdmem_request():
|
||||
"""9B full-frame LCDMEM request (offset 0) - what the production client sends."""
|
||||
return remo_request(REMO_CMD_LCDMEM, bytes(4))
|
||||
|
||||
|
||||
def recv_upto(sock, want, deadline_ns):
|
||||
"""Bounded read: never consumes more than `want` bytes (no over-read of a
|
||||
following frame/op byte - the run10 lesson)."""
|
||||
left = deadline_ns - now_ns()
|
||||
if left <= 0:
|
||||
raise TimeoutError("frame deadline")
|
||||
try:
|
||||
sock.settimeout(max(left, 1) / 1e9 / 5.0)
|
||||
return sock.recv(want)
|
||||
except socket.timeout:
|
||||
return b""
|
||||
|
||||
|
||||
def read_remo_frame(sock, deadline_ns, ack_probe=False):
|
||||
"""Read exactly one device frame; returns tag/cmd/data/CRC + arrival curve.
|
||||
|
||||
The first read is bounded to 8B so the segment-arrival curve stays
|
||||
fine-grained; subsequent reads are bounded by the remaining frame length
|
||||
(device never pipelines: one response per request).
|
||||
|
||||
ack_probe=True: as soon as the frame is known to be incomplete, send one
|
||||
byte the device parser discards (0x00, not TAG 0xAA). The resulting TCP
|
||||
segment carries a piggybacked ACK for the data already received, which
|
||||
discriminates "sender holds the last partial segment waiting for an ACK
|
||||
(Nagle-style)" from "chip-internal fixed timer" - without any packet capture."""
|
||||
buf = bytearray()
|
||||
batches = []
|
||||
first_ns = None
|
||||
total = None
|
||||
while True:
|
||||
want = (8 - len(buf)) if total is None else (total - len(buf))
|
||||
if want <= 0:
|
||||
break
|
||||
d = recv_upto(sock, min(want, 4096), deadline_ns)
|
||||
if not d:
|
||||
if total is not None:
|
||||
raise ConnectionError("frame truncated (%d/%d bytes)" % (len(buf), total))
|
||||
continue # header still incomplete: wait until deadline
|
||||
t = now_ns()
|
||||
if first_ns is None:
|
||||
first_ns = t
|
||||
buf.extend(d)
|
||||
batches.append([t, len(d)])
|
||||
if total is None and len(buf) >= 4:
|
||||
total = 4 + (((buf[2] << 8) | buf[3])) + 1
|
||||
if ack_probe and total is not None and len(buf) < total:
|
||||
try:
|
||||
sock.sendall(b"\x00") # parser-safe: discarded while waiting for TAG 0xAA
|
||||
except OSError:
|
||||
pass
|
||||
data = bytes(buf[4:total - 1])
|
||||
crc = buf[total - 1]
|
||||
return {"tag": buf[0], "cmd": buf[1], "data": data, "total": total,
|
||||
"crc_ok": crc == xor_bytes(data), "first_ns": first_ns, "batches": batches}
|
||||
|
||||
|
||||
def enable_immediate_ack(sock):
|
||||
"""Windows: disable delayed ACK on this socket (SIO_TCP_SET_ACK_FREQUENCY=1).
|
||||
|
||||
Root-cause fix for the ~50ms tail: the CH395F (MSS=1024) holds the final
|
||||
sub-MSS segment of a 3209B frame per Nagle until the peer ACKs; Windows'
|
||||
delayed ACK defers that ACK ~50ms. Setting ACK frequency=1 makes every
|
||||
segment ACKed immediately -> tail 52ms -> ~4ms (measured). Returns True on
|
||||
success. Other platforms: no-op (Linux uses TCP_QUICKACK per recv)."""
|
||||
if sys.platform != "win32":
|
||||
return False
|
||||
try:
|
||||
import ctypes
|
||||
ws2 = ctypes.WinDLL("ws2_32", use_last_error=True)
|
||||
WSAIoctl = ws2.WSAIoctl
|
||||
WSAIoctl.argtypes = [ctypes.c_void_p, ctypes.c_uint32, ctypes.c_void_p, ctypes.c_uint32,
|
||||
ctypes.c_void_p, ctypes.c_uint32, ctypes.POINTER(ctypes.c_uint32),
|
||||
ctypes.c_void_p, ctypes.c_void_p]
|
||||
WSAIoctl.restype = ctypes.c_int
|
||||
val = ctypes.c_uint32(1) # 1 = ACK every segment
|
||||
ret = ctypes.c_uint32(0)
|
||||
rc = WSAIoctl(ctypes.c_void_p(sock.fileno()), 0x98000017,
|
||||
ctypes.byref(val), 4, None, 0, ctypes.byref(ret), None, None)
|
||||
return rc == 0
|
||||
except (OSError, AttributeError):
|
||||
return False
|
||||
|
||||
|
||||
def remo_bitmap_nontrivial(payload):
|
||||
"""Same 'non-trivial frame' criterion as simulator/tests/check_remo_sink.py:
|
||||
the 1bpp bitmap must not be a single repeated byte (blank/broken frame)."""
|
||||
bmp = payload[4:4 + REMO_MEMSIZE]
|
||||
return len(set(bmp)) > 2
|
||||
|
||||
|
||||
class RemoPeer:
|
||||
"""Production-path peer (M3): drives PROD_CYCLE/RELISTEN against :7003 (device) or the
|
||||
simulator, measuring the full client-observed frame cycle."""
|
||||
|
||||
def __init__(self, host, port, wait_s, json_path, samples, cadence_ms,
|
||||
stress_from, nodelay, reconnect_rounds=1, ack_probe=False, ack_fast=False):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.wait_s = wait_s
|
||||
self.json_path = json_path
|
||||
self.samples = samples
|
||||
self.cadence_s = cadence_ms / 1000.0
|
||||
self.stress_from = stress_from # cycle index where FTP stress starts
|
||||
self.nodelay = nodelay
|
||||
self.reconnect_rounds = max(1, reconnect_rounds)
|
||||
self.ack_probe = ack_probe # in-band ACK-release experiment (tail diagnosis)
|
||||
self.ack_fast = ack_fast # Windows immediate-ACK socket option
|
||||
self.ack_fast_ok = None
|
||||
self.sock = None
|
||||
self.t0_wall = None
|
||||
self.t0_ns = 0
|
||||
self.connect_ms = None
|
||||
self.reconnect_ms = None
|
||||
self.reconnect_list = []
|
||||
self.init_rtt_ms = None
|
||||
self.geom = {}
|
||||
self.frames = []
|
||||
|
||||
# --- connection ---
|
||||
def connect_once(self):
|
||||
t = now_ns()
|
||||
s = socket.create_connection((self.host, self.port), timeout=5.0)
|
||||
if self.nodelay:
|
||||
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
||||
if self.ack_fast:
|
||||
self.ack_fast_ok = enable_immediate_ack(s)
|
||||
print("[peer] immediate-ACK (SIO_TCP_SET_ACK_FREQUENCY=1): %s"
|
||||
% ("applied" if self.ack_fast_ok else "not available"))
|
||||
self.sock = s
|
||||
return (now_ns() - t) / 1e6
|
||||
|
||||
def connect_with_retry(self):
|
||||
t_end = time.time() + self.wait_s
|
||||
last = None
|
||||
while time.time() < t_end:
|
||||
try:
|
||||
return self.connect_once()
|
||||
except OSError as e:
|
||||
last = e
|
||||
time.sleep(1.0)
|
||||
raise ConnectionError("cannot connect to %s:%d within %ds (%s)" %
|
||||
(self.host, self.port, self.wait_s, last))
|
||||
|
||||
def init_roundtrip(self, deadline_s=5.0):
|
||||
"""INIT request -> geometry reply; returns round-trip ms."""
|
||||
t = now_ns()
|
||||
self.sock.sendall(remo_request(REMO_CMD_INIT))
|
||||
f = read_remo_frame(self.sock, t + int(deadline_s * 1e9))
|
||||
ms = (now_ns() - t) / 1e6
|
||||
if f["cmd"] == REMO_CMD_INIT and len(f["data"]) >= 6:
|
||||
# 8B (fixed): [W u16][H u16][MEMSIZE u32 BE]; 6B = 旧固件(u32 被截半 → 0)
|
||||
self.geom = {"w": (f["data"][0] << 8) | f["data"][1],
|
||||
"h": (f["data"][2] << 8) | f["data"][3],
|
||||
"framebytes": int.from_bytes(f["data"][4:8], "big")
|
||||
if len(f["data"]) >= 8
|
||||
else ((f["data"][4] << 8) | f["data"][5]),
|
||||
"payload_len": len(f["data"])}
|
||||
return ms
|
||||
|
||||
# --- FTP head-of-line stress ---
|
||||
def ftp_stress_worker(self, start_evt, stop_evt, hold_s):
|
||||
"""Wait for the stress window to open, then hold an idle FTP login:
|
||||
lftpd read_line -> blocking net_recv(0) inside netTask (old 30s default
|
||||
budget NET_RECV_TIMEOUT_MS, removed in self-heal phase 1.5; today the wait is
|
||||
a CHUNK_WAIT pending recv -> this scenario must NOT freeze the queue anymore)
|
||||
-> every queued message (incl. the LCDMEM response) waits behind it."""
|
||||
if not start_evt.wait(120.0):
|
||||
sys.stdout.write("[peer] ftp stress: window never opened\n")
|
||||
return
|
||||
try:
|
||||
s = socket.create_connection((self.host, 21), timeout=10.0)
|
||||
s.settimeout(10.0)
|
||||
s.sendall(b"USER latency\r\n")
|
||||
reply = b""
|
||||
while b"\r\n" not in reply:
|
||||
d = s.recv(64)
|
||||
if not d:
|
||||
break
|
||||
reply += d
|
||||
sys.stdout.write("[peer] ftp stress armed (331: %r)\n" % reply[:40])
|
||||
stop_evt.wait(hold_s)
|
||||
try:
|
||||
s.sendall(b"QUIT\r\n")
|
||||
except OSError:
|
||||
pass
|
||||
s.close()
|
||||
sys.stdout.write("[peer] ftp stress released\n")
|
||||
except OSError as e:
|
||||
sys.stdout.write("[peer] ftp stress FAILED: %s\n" % e)
|
||||
|
||||
# --- session ---
|
||||
def one_session(self):
|
||||
self.t0_wall = iso_now()
|
||||
self.t0_ns = now_ns()
|
||||
|
||||
self.connect_ms = self.connect_with_retry()
|
||||
print("[peer] connected %s:%d in %.1f ms" % (self.host, self.port, self.connect_ms))
|
||||
self.init_rtt_ms = self.init_roundtrip()
|
||||
print("[peer] INIT rtt=%.1f ms geom=%s" % (self.init_rtt_ms, self.geom))
|
||||
|
||||
stress_start = threading.Event()
|
||||
stress_stop = threading.Event()
|
||||
stress_thread = None
|
||||
if self.stress_from is not None:
|
||||
# hold long enough to cover the remaining cycles (+15s margin)
|
||||
hold_s = self.cadence_s * max(1, self.samples - self.stress_from) + 15.0
|
||||
stress_thread = threading.Thread(
|
||||
target=self.ftp_stress_worker,
|
||||
args=(stress_start, stress_stop, hold_s),
|
||||
daemon=True)
|
||||
stress_thread.start()
|
||||
|
||||
for i in range(self.samples):
|
||||
if self.stress_from is not None and i == self.stress_from:
|
||||
print("[peer] === FTP stress ON (head-of-line) at cycle %d ===" % i)
|
||||
stress_start.set()
|
||||
time.sleep(0.5) # let the FTP half-login block netTask
|
||||
|
||||
stressed = (self.stress_from is not None and i >= self.stress_from)
|
||||
t = now_ns()
|
||||
self.sock.sendall(remo_lcdmem_request())
|
||||
f = read_remo_frame(self.sock, t + int(REMO_CYCLE_DEADLINE_S * 1e9),
|
||||
ack_probe=self.ack_probe)
|
||||
t_end = now_ns()
|
||||
|
||||
first_ms = (f["first_ns"] - t) / 1e6
|
||||
total_ms = (t_end - t) / 1e6
|
||||
tail_ms = (f["batches"][-1][0] - f["batches"][0][0]) / 1e6 if len(f["batches"]) > 1 else 0.0
|
||||
rec = {
|
||||
"cycle": i,
|
||||
"stress": stressed,
|
||||
"first_ms": round(first_ms, 3),
|
||||
"total_ms": round(total_ms, 3),
|
||||
"tail_lag_ms": round(tail_ms, 3),
|
||||
"batches": [[round((b[0] - t) / 1e6, 3), b[1]] for b in f["batches"]],
|
||||
"bytes": f["total"],
|
||||
"tag": f["tag"],
|
||||
"cmd": f["cmd"],
|
||||
"crc_ok": bool(f["crc_ok"]),
|
||||
"nontrivial": bool(f["cmd"] == REMO_CMD_LCDMEM
|
||||
and len(f["data"]) == 4 + REMO_MEMSIZE
|
||||
and remo_bitmap_nontrivial(f["data"])),
|
||||
}
|
||||
self.frames.append(rec)
|
||||
if (i % 10) == 0 or stressed:
|
||||
print("[peer] cycle %3d %-7s first=%7.1f total=%8.1f ms batches=%d %s"
|
||||
% (i, "STRESS" if stressed else "quiet", first_ms, total_ms,
|
||||
len(f["batches"]),
|
||||
"OK" if (rec["crc_ok"] and rec["nontrivial"]) else "BAD"))
|
||||
if i + 1 < self.samples:
|
||||
time.sleep(self.cadence_s)
|
||||
|
||||
if stress_thread is not None:
|
||||
stress_stop.set()
|
||||
|
||||
self.dump_json()
|
||||
|
||||
# --- RELISTEN: close + reconnect rounds (each close drives the device auto-relisten
|
||||
# probe; each reconnect measures connect + INIT round trip) ---
|
||||
for r in range(self.reconnect_rounds):
|
||||
self.sock.close()
|
||||
print("[peer] round %d/%d: closed; reconnect in 1.5s (device relisten window) ..."
|
||||
% (r + 1, self.reconnect_rounds))
|
||||
time.sleep(1.5)
|
||||
t = now_ns()
|
||||
self.connect_once()
|
||||
self.init_rtt_ms = self.init_roundtrip()
|
||||
ms = (now_ns() - t) / 1e6
|
||||
self.reconnect_list.append(round(ms, 3))
|
||||
self.reconnect_ms = ms
|
||||
print("[peer] round %d: reconnect + INIT = %.1f ms" % (r + 1, ms))
|
||||
|
||||
rep = self.dump_json()
|
||||
self.print_summary(rep)
|
||||
self.sock.close()
|
||||
|
||||
# --- reporting ---
|
||||
def _split(self, key, stressed):
|
||||
return [f[key] for f in self.frames if f["stress"] is stressed]
|
||||
|
||||
def _spikes(self, vals):
|
||||
return sum(1 for x in vals if x >= SPIKE_MS)
|
||||
|
||||
def dump_json(self, error=None):
|
||||
all_total = [f["total_ms"] for f in self.frames]
|
||||
all_first = [f["first_ms"] for f in self.frames]
|
||||
all_tail = [f["tail_lag_ms"] for f in self.frames]
|
||||
quiet_total = self._split("total_ms", False)
|
||||
stress_total = self._split("total_ms", True)
|
||||
rep = {
|
||||
"mode": "remo",
|
||||
"host": self.host,
|
||||
"port": self.port,
|
||||
"ts_start": self.t0_wall,
|
||||
"ts_end": iso_now(),
|
||||
"duration_s": round((now_ns() - self.t0_ns) / 1e9, 2),
|
||||
"samples": self.samples,
|
||||
"cadence_ms": round(self.cadence_s * 1000.0, 1),
|
||||
"tcp_nodelay": bool(self.nodelay),
|
||||
"immediate_ack": (None if not self.ack_fast else bool(self.ack_fast_ok)),
|
||||
"in_band_ack_probe": bool(self.ack_probe),
|
||||
"stress_from": self.stress_from,
|
||||
"connect_ms": None if self.connect_ms is None else round(self.connect_ms, 3),
|
||||
"reconnect_ms": None if self.reconnect_ms is None else round(self.reconnect_ms, 3),
|
||||
"reconnect_list_ms": self.reconnect_list,
|
||||
"reconnect_stat": stat_block(self.reconnect_list),
|
||||
"init": {"rtt_ms": None if self.init_rtt_ms is None else round(self.init_rtt_ms, 3),
|
||||
"geom": self.geom},
|
||||
"cycle_all": stat_block(all_total, self._spikes(all_total)),
|
||||
"cycle_quiet": stat_block(quiet_total, self._spikes(quiet_total)),
|
||||
"cycle_stress": stat_block(stress_total, self._spikes(stress_total)),
|
||||
"first_byte_all": stat_block(all_first, self._spikes(all_first)),
|
||||
"tail_lag_all": stat_block(all_tail, self._spikes(all_tail)),
|
||||
"crc": {"ok": sum(1 for f in self.frames if f["crc_ok"]),
|
||||
"fail": sum(1 for f in self.frames if not f["crc_ok"])},
|
||||
"bitmap": {"nontrivial_ok": sum(1 for f in self.frames if f["nontrivial"]),
|
||||
"nontrivial_fail": sum(1 for f in self.frames if not f["nontrivial"])},
|
||||
"frames": self.frames,
|
||||
}
|
||||
if error:
|
||||
rep["error"] = error
|
||||
if self.json_path:
|
||||
with open(self.json_path, "w", encoding="utf-8") as fh:
|
||||
json.dump(rep, fh, ensure_ascii=False, indent=1)
|
||||
print("[peer] JSON report -> %s%s" % (self.json_path,
|
||||
" (PARTIAL - error)" if error else ""))
|
||||
return rep
|
||||
|
||||
def print_summary(self, rep):
|
||||
print("=== latency_peer summary (M3 / Phase 14, production RemoDispBus) ===")
|
||||
print("host=%s:%d samples=%d cadence=%.0fms nodelay=%d duration=%.1fs"
|
||||
% (self.host, self.port, rep["samples"], rep["cadence_ms"],
|
||||
int(rep["tcp_nodelay"]), rep["duration_s"]))
|
||||
print("connect=%s ms reconnect+INIT=%s ms geom=%s"
|
||||
% (rep["connect_ms"], rep["reconnect_ms"], rep["init"]["geom"]))
|
||||
for key in ("cycle_all", "cycle_quiet", "cycle_stress",
|
||||
"first_byte_all", "tail_lag_all"):
|
||||
b = rep[key]
|
||||
if b.get("n"):
|
||||
print("%-15s n=%-4d min=%9.1f med=%9.1f p95=%9.1f max=%9.1f ms spikes=%d"
|
||||
% (key, b["n"], b["min_ms"], b["med_ms"], b["p95_ms"], b["max_ms"],
|
||||
b.get("spikes500", 0)))
|
||||
else:
|
||||
print("%-15s (no samples)" % key)
|
||||
print("crc ok/fail = %d/%d bitmap nontrivial ok/fail = %d/%d"
|
||||
% (rep["crc"]["ok"], rep["crc"]["fail"],
|
||||
rep["bitmap"]["nontrivial_ok"], rep["bitmap"]["nontrivial_fail"]))
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
self.one_session()
|
||||
except (ConnectionError, TimeoutError, RuntimeError, OSError) as e:
|
||||
self.dump_json(error="%s: %s" % (type(e).__name__, e))
|
||||
raise
|
||||
|
||||
|
||||
def percentile(sorted_vals, pct):
|
||||
if not sorted_vals:
|
||||
return None
|
||||
idx = (len(sorted_vals) * pct) // 100
|
||||
if idx >= len(sorted_vals):
|
||||
idx = len(sorted_vals) - 1
|
||||
return sorted_vals[idx]
|
||||
|
||||
|
||||
def stat_block(ms_vals, spikes=0):
|
||||
if not ms_vals:
|
||||
return {"n": 0}
|
||||
s = sorted(ms_vals)
|
||||
return {
|
||||
"n": len(s),
|
||||
"min_ms": round(s[0], 3),
|
||||
"med_ms": round(s[len(s) // 2], 3),
|
||||
"p95_ms": round(percentile(s, 95), 3),
|
||||
"max_ms": round(s[-1], 3),
|
||||
"spikes500": spikes,
|
||||
}
|
||||
|
||||
|
||||
def fresh_reports():
|
||||
"""Per-session measurement containers (reset on each retry attempt)."""
|
||||
return {
|
||||
"l6": {"samples": [], "echo_ok": 0, "echo_fail": 0},
|
||||
"l7": {"frames": [], "first_ms": [], "total_ms": [], "spikes500": 0,
|
||||
"crc_ok": 0, "crc_fail": 0, "batches_per_frame": []},
|
||||
"l5_recv": {"bytes": 0, "window_ms": None},
|
||||
"l5_send": {"bytes": 0, "window_ms": None},
|
||||
"l3": {"bytes": 0, "window_ms": None},
|
||||
}
|
||||
|
||||
|
||||
class Peer:
|
||||
def __init__(self, host, port, wait_s, json_path, ftp_stress):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.wait_s = wait_s
|
||||
self.connect_wait_s = wait_s # per-attempt connect budget (retries shrink it)
|
||||
self.json_path = json_path
|
||||
self.ftp_stress = ftp_stress
|
||||
self.sock = None
|
||||
self.t0_wall = None
|
||||
self.t0_ns = 0
|
||||
self.n_ops = 0
|
||||
self.reports = fresh_reports()
|
||||
|
||||
def connect_with_retry(self):
|
||||
t_end = time.time() + self.connect_wait_s
|
||||
while time.time() < t_end:
|
||||
try:
|
||||
s = socket.create_connection((self.host, self.port), timeout=5.0)
|
||||
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
||||
self.sock = s
|
||||
return
|
||||
except OSError:
|
||||
time.sleep(1.0)
|
||||
raise ConnectionError("cannot connect to %s:%d within %ds" %
|
||||
(self.host, self.port, self.wait_s))
|
||||
|
||||
def ftp_stress_worker(self, start_evt, stop_evt):
|
||||
"""Hold an idle FTP login: lftpd read_line -> blocking net_recv(0) in
|
||||
netTask (30s timeout) -> all queued messages wait behind it."""
|
||||
try:
|
||||
s = socket.create_connection((self.host, 21), timeout=10.0)
|
||||
s.settimeout(10.0)
|
||||
s.sendall(b"USER latency\r\n")
|
||||
reply = b""
|
||||
while b"\r\n" not in reply:
|
||||
d = s.recv(64)
|
||||
if not d:
|
||||
break
|
||||
reply += d
|
||||
start_evt.set() # QUEUE_RT window starts under stress
|
||||
sys.stdout.write("[peer] ftp stress armed (331: %r)\n" % reply[:40])
|
||||
stop_evt.wait(35.0) # > 30s: netTask read times out (full freeze)
|
||||
try:
|
||||
s.sendall(b"QUIT\r\n")
|
||||
except OSError:
|
||||
pass
|
||||
s.close()
|
||||
sys.stdout.write("[peer] ftp stress released\n")
|
||||
except OSError as e:
|
||||
sys.stdout.write("[peer] ftp stress FAILED: %s\n" % e)
|
||||
start_evt.set()
|
||||
|
||||
def dump_json(self, error=None):
|
||||
"""Build the report dict and write it (idempotent; error => partial)."""
|
||||
rep = {
|
||||
"mode": "net",
|
||||
"host": self.host,
|
||||
"port": self.port,
|
||||
"ts_start": self.t0_wall,
|
||||
"ts_end": iso_now(),
|
||||
"duration_s": round((now_ns() - self.t0_ns) / 1e9, 2),
|
||||
"ops_seen": self.n_ops,
|
||||
"ftp_stress": self.ftp_stress,
|
||||
"seg_l6": stat_block(self.reports["l6"]["samples"], 0)
|
||||
| {"echo_ok": self.reports["l6"]["echo_ok"], "echo_fail": self.reports["l6"]["echo_fail"]},
|
||||
"seg_l7_first_byte": stat_block(self.reports["l7"]["first_ms"]),
|
||||
"seg_l7_total": stat_block(self.reports["l7"]["total_ms"], self.reports["l7"]["spikes500"]),
|
||||
"l7_crc": {"ok": self.reports["l7"]["crc_ok"], "fail": self.reports["l7"]["crc_fail"],
|
||||
"batches_per_frame": sorted(set(self.reports["l7"]["batches_per_frame"]))},
|
||||
"l5_recv": self.reports["l5_recv"],
|
||||
"l5_send": self.reports["l5_send"],
|
||||
"l3_drain": self.reports["l3"],
|
||||
"frames_l7": self.reports["l7"]["frames"],
|
||||
}
|
||||
if error:
|
||||
rep["error"] = error
|
||||
if self.json_path:
|
||||
with open(self.json_path, "w", encoding="utf-8") as f:
|
||||
json.dump(rep, f, indent=1, ensure_ascii=True)
|
||||
print("[peer] JSON report -> %s%s" % (self.json_path,
|
||||
" (PARTIAL - error)" if error else ""))
|
||||
return rep
|
||||
|
||||
def run(self, max_attempts=3):
|
||||
"""整会话重试:芯片对空闲连接自行 RST(MCU 栈慢于芯片握手完成时,连接
|
||||
建立后 15~30s 无流量即被芯片超时断开,2026-09-22 台架实证)→ 重连 +
|
||||
MCU 端 re-HELLO 重启 = 闭环。重试时缩短 connect 预算(设备已在运行)。"""
|
||||
last_err = None
|
||||
for attempt in range(1, max_attempts + 1):
|
||||
self.t0_wall = iso_now()
|
||||
self.t0_ns = now_ns()
|
||||
self.n_ops = 0
|
||||
self.reports = fresh_reports()
|
||||
self.connect_wait_s = self.wait_s if attempt == 1 else min(self.wait_s, 60)
|
||||
try:
|
||||
self.one_session()
|
||||
return
|
||||
except (ConnectionError, TimeoutError, RuntimeError, OSError) as e:
|
||||
last_err = e
|
||||
self.close_quiet()
|
||||
if attempt < max_attempts:
|
||||
print("[peer] session attempt %d/%d failed: %s" % (attempt, max_attempts, e))
|
||||
print("[peer] reconnect in 2s (MCU auto-relistens; re-HELLO restarts) ...")
|
||||
time.sleep(2.0)
|
||||
raise RuntimeError("all %d session attempts failed (last: %s)" % (max_attempts, last_err))
|
||||
|
||||
def close_quiet(self):
|
||||
try:
|
||||
if self.sock is not None:
|
||||
self.sock.close()
|
||||
except OSError:
|
||||
pass
|
||||
self.sock = None
|
||||
|
||||
def one_session(self):
|
||||
t0_wall = self.t0_wall
|
||||
t0_ns = self.t0_ns
|
||||
print("[peer] connecting %s:%d (wait up to %ds) ..." % (self.host, self.port, self.connect_wait_s))
|
||||
self.connect_with_retry()
|
||||
print("[peer] connected at %s" % iso_now())
|
||||
|
||||
queue_rt_start_evt = threading.Event()
|
||||
queue_rt_stop_evt = threading.Event()
|
||||
if self.ftp_stress:
|
||||
th = threading.Thread(target=self.ftp_stress_worker,
|
||||
args=(queue_rt_start_evt, queue_rt_stop_evt), daemon=True)
|
||||
th.start()
|
||||
|
||||
self.sock.sendall(HELLO)
|
||||
print("[peer] HELLO sent, awaiting MCU ops ...")
|
||||
|
||||
queue_rt_window_open = False
|
||||
queue_rt_t0 = 0
|
||||
chunk_wait_t0 = 0
|
||||
while True:
|
||||
op = recv_exact(self.sock, 1, t0_ns + 300 * 1000 * 1000000)
|
||||
op = op[0]
|
||||
self.n_ops += 1
|
||||
if op == OP_DONE:
|
||||
print("[peer] op=done after %d ops" % self.n_ops)
|
||||
break
|
||||
|
||||
if op == OP_QUEUE_RT_RECV:
|
||||
if self.ftp_stress:
|
||||
queue_rt_start_evt.wait(60.0) # start window only under full stress
|
||||
queue_rt_window_open = True
|
||||
queue_rt_t0 = now_ns()
|
||||
for i in range(QUEUE_RT_CHUNKS):
|
||||
self.sock.sendall(pattern(QUEUE_RT_CHUNK_LEN, seed=i))
|
||||
time.sleep(0.05) # 50ms: keep 1KB/s below MCU drain rate
|
||||
self.reports["l5_recv"]["window_ms"] = (now_ns() - queue_rt_t0) / 1e6
|
||||
self.reports["l5_recv"]["bytes"] = QUEUE_RT_CHUNKS * QUEUE_RT_CHUNK_LEN
|
||||
queue_rt_stop_evt.set()
|
||||
|
||||
elif op == OP_QUEUE_RT_SEND:
|
||||
t = now_ns()
|
||||
data = recv_exact(self.sock, QUEUE_RT_CHUNKS * QUEUE_RT_CHUNK_LEN, t + 120 * 1000 * 1000000)
|
||||
self.reports["l5_send"]["bytes"] = len(data)
|
||||
self.reports["l5_send"]["window_ms"] = (now_ns() - t) / 1e6
|
||||
|
||||
elif op == OP_ECHO_RT:
|
||||
t0 = now_ns()
|
||||
req = pattern(ECHO_RT_SIZE, seed=self.n_ops)
|
||||
self.sock.sendall(req)
|
||||
resp = recv_exact(self.sock, ECHO_RT_SIZE, t0 + 40 * 1000 * 1000000)
|
||||
ms = (now_ns() - t0) / 1e6
|
||||
self.reports["l6"]["samples"].append(ms)
|
||||
if resp == req:
|
||||
self.reports["l6"]["echo_ok"] += 1
|
||||
else:
|
||||
self.reports["l6"]["echo_fail"] += 1
|
||||
|
||||
elif op == OP_FRAME_RT:
|
||||
t_send = now_ns()
|
||||
self.sock.sendall(frame_rt_request(self.n_ops))
|
||||
got = bytearray()
|
||||
batches = []
|
||||
first_ns = None
|
||||
while len(got) < FRAME_RT_FRAME_LEN:
|
||||
# bounded read: never consume the next op byte (ASCII 0x04) that
|
||||
# follows the frame in the stream (run10: unbounded recv ate it ->
|
||||
# 3210-byte "frame" -> length check failed -> peer closed)
|
||||
d = self.sock.recv(min(4096, FRAME_RT_FRAME_LEN - len(got)))
|
||||
if not d:
|
||||
raise ConnectionError("FRAME_RT frame truncated (%d/%d)" %
|
||||
(len(got), FRAME_RT_FRAME_LEN))
|
||||
if first_ns is None:
|
||||
first_ns = now_ns()
|
||||
got.extend(d)
|
||||
batches.append([round((now_ns() - t_send) / 1e6, 3), len(d)])
|
||||
t_end = now_ns()
|
||||
first_ms = (first_ns - t_send) / 1e6 if first_ns else None
|
||||
total_ms = (t_end - t_send) / 1e6
|
||||
# inter-batch arrival gaps
|
||||
spikes = 0
|
||||
for k in range(1, len(batches)):
|
||||
if batches[k][0] - batches[k - 1][0] >= SPIKE_MS:
|
||||
spikes += 1
|
||||
self.reports["l7"]["first_ms"].append(first_ms if first_ms is not None else 0.0)
|
||||
self.reports["l7"]["total_ms"].append(total_ms)
|
||||
self.reports["l7"]["spikes500"] += spikes
|
||||
self.reports["l7"]["batches_per_frame"].append(len(batches))
|
||||
if frame_rt_ok(bytes(got)):
|
||||
self.reports["l7"]["crc_ok"] += 1
|
||||
else:
|
||||
self.reports["l7"]["crc_fail"] += 1
|
||||
self.reports["l7"]["frames"].append(
|
||||
{"first_ms": round(first_ms, 3) if first_ms is not None else None,
|
||||
"total_ms": round(total_ms, 3), "batches": batches})
|
||||
|
||||
elif op == OP_CHUNK_WAIT:
|
||||
chunk_wait_t0 = now_ns()
|
||||
recv_exact(self.sock, CHUNK_WAIT_SENDS * CHUNK_WAIT_LEN, chunk_wait_t0 + 120 * 1000 * 1000000)
|
||||
self.reports["l3"]["bytes"] = CHUNK_WAIT_SENDS * CHUNK_WAIT_LEN
|
||||
self.reports["l3"]["window_ms"] = (now_ns() - chunk_wait_t0) / 1e6
|
||||
|
||||
else:
|
||||
raise RuntimeError("unknown op byte 0x%02X (protocol desync)" % op)
|
||||
|
||||
# --- JSON report (also used on the error path: partial data kept) ---
|
||||
rep = self.dump_json()
|
||||
|
||||
print("=== latency_peer summary (M2 / Phase 13) ===")
|
||||
print("ops=%d duration=%.1fs ftp_stress=%d" % (self.n_ops, rep["duration_s"], int(self.ftp_stress)))
|
||||
for key in ("seg_l6", "seg_l7_first_byte", "seg_l7_total"):
|
||||
b = rep[key]
|
||||
if b.get("n"):
|
||||
print("%-18s n=%-4d min=%9.1f med=%9.1f p95=%9.1f max=%9.1f ms spikes=%d"
|
||||
% (key, b["n"], b["min_ms"], b["med_ms"], b["p95_ms"], b["max_ms"],
|
||||
b.get("spikes500", 0)))
|
||||
else:
|
||||
print("%-18s (no samples)" % key)
|
||||
print("l7 crc ok/fail = %d/%d batches/frame = %s"
|
||||
% (rep["l7_crc"]["ok"], rep["l7_crc"]["fail"], rep["l7_crc"]["batches_per_frame"]))
|
||||
self.sock.close()
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
ap = argparse.ArgumentParser(
|
||||
description="link latency test peer - M2 Phase 13 (net) / M3 Phase 14 (remo)")
|
||||
ap.add_argument("--mode", default="net", choices=["net", "remo"],
|
||||
help="net = Phase 13 test protocol :8080; "
|
||||
"remo = production RemoDispBus client :7003 (Phase 14 / simulator)")
|
||||
ap.add_argument("--host", default=bench_defaults.DEVICE_IP, help="device IP")
|
||||
ap.add_argument("--port", type=int, default=None,
|
||||
help="listen port (default: 8080 for net, 7003 for remo)")
|
||||
ap.add_argument("--wait", type=int, default=180,
|
||||
help="max seconds to retry connect (device boots after flash)")
|
||||
ap.add_argument("--json", default=None, help="write JSON report to this path")
|
||||
ap.add_argument("--ftp-stress", action="store_true",
|
||||
help="hold idle FTP login (head-of-line repro)")
|
||||
ap.add_argument("--samples", type=int, default=50,
|
||||
help="remo mode: LCDMEM frame cycles to measure (default 50)")
|
||||
ap.add_argument("--cadence-ms", type=int, default=100,
|
||||
help="remo mode: client poll cadence in ms (production client = 100)")
|
||||
ap.add_argument("--stress-from", type=int, default=None,
|
||||
help="remo mode: cycle index where FTP stress starts "
|
||||
"(default with --ftp-stress: half of --samples)")
|
||||
ap.add_argument("--nodelay", action="store_true",
|
||||
help="remo mode: set TCP_NODELAY on the client socket")
|
||||
ap.add_argument("--reconnect-rounds", type=int, default=1,
|
||||
help="remo mode: close+reconnect rounds at the end (each one drives "
|
||||
"the device auto-relisten probe; M3 uses 3)")
|
||||
ap.add_argument("--ack-probe", action="store_true",
|
||||
help="remo mode: when a frame is still incomplete, send one parser-safe "
|
||||
"byte so the TCP segment piggybacks an ACK (diagnoses whether the "
|
||||
"~50ms tail waits for an ACK; no packet capture needed)")
|
||||
ap.add_argument("--ack-fast", action="store_true",
|
||||
help="remo mode: Windows only - disable delayed ACK on this socket "
|
||||
"(SIO_TCP_SET_ACK_FREQUENCY=1); removes the ~50ms frame tail")
|
||||
args = ap.parse_args(argv)
|
||||
|
||||
if args.mode == "remo":
|
||||
port = args.port if args.port else 7003
|
||||
stress_from = args.stress_from
|
||||
if args.ftp_stress and stress_from is None:
|
||||
stress_from = max(1, args.samples // 2)
|
||||
peer = RemoPeer(args.host, port, args.wait, args.json, args.samples,
|
||||
args.cadence_ms, stress_from, args.nodelay,
|
||||
args.reconnect_rounds, args.ack_probe, args.ack_fast)
|
||||
else:
|
||||
peer = Peer(args.host, args.port or 8080, args.wait, args.json, args.ftp_stress)
|
||||
try:
|
||||
peer.run()
|
||||
return 0
|
||||
except (ConnectionError, TimeoutError, RuntimeError, OSError) as e:
|
||||
print("[peer] ERROR: %s" % e)
|
||||
try:
|
||||
peer.dump_json(error=str(e)) # partial report: keep measured data
|
||||
except OSError as e2:
|
||||
print("[peer] (JSON write failed: %s)" % e2)
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,133 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
pasv_probe.py — decisive probe for the "storm" signature (self-heal audit E8).
|
||||
|
||||
Per FTP round prints: device PASV port, PC data-socket SOURCE port, round
|
||||
result, and timing. Hypothesis under test: the CH395F reuses the same
|
||||
ephemeral PASV port for the next data listener while the previous data
|
||||
connection is still in chip-side TIME_WAIT with the same 4-tuple
|
||||
(PC source port reused too) -> chip reports CONNECT then RSTs ~10ms later
|
||||
(serial: "sock4 standalone accept" + "DISCONNECTED chip st=0x00/0x00").
|
||||
|
||||
Usage:
|
||||
python pasv_probe.py --rounds 5 --delay 2 # reproduce (fast rounds)
|
||||
python pasv_probe.py --rounds 3 --delay 35 # TIME_WAIT window test
|
||||
"""
|
||||
import os
|
||||
import argparse
|
||||
import re
|
||||
import socket
|
||||
import sys
|
||||
import time
|
||||
|
||||
# 台架连接默认值单一真源已随域归档到 test/host/bench/bench_defaults.py:
|
||||
# Python 的 sys.path[0] 是**脚本自身所在目录**,本脚本在 bench/<域>/ 下直接
|
||||
# `import bench_defaults` 会 ModuleNotFoundError —— 先把 bench/ 补进 sys.path。
|
||||
_BENCH_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
if _BENCH_DIR not in sys.path:
|
||||
sys.path.insert(0, _BENCH_DIR)
|
||||
|
||||
import bench_defaults # noqa: E402 (台架连接默认值单一真源)
|
||||
|
||||
IP_DEFAULT = bench_defaults.DEVICE_IP
|
||||
|
||||
|
||||
def ftp_stor_round(ip, user, pwd, fname, payload):
|
||||
"""One login -> PASV -> STOR -> verify transfer. Returns (ok, pasv_port,
|
||||
pc_src_port, detail)."""
|
||||
c = socket.create_connection((ip, 21), timeout=10)
|
||||
c.settimeout(10)
|
||||
pc_src = None
|
||||
pasv_port = None
|
||||
try:
|
||||
def readline():
|
||||
buf = b""
|
||||
while not buf.endswith(b"\n"):
|
||||
ch = c.recv(1)
|
||||
if not ch:
|
||||
break
|
||||
buf += ch
|
||||
return buf.decode(errors="replace").strip()
|
||||
|
||||
def cmd(line):
|
||||
c.sendall((line + "\r\n").encode())
|
||||
return readline()
|
||||
|
||||
greet = readline()
|
||||
if not greet.startswith("220"):
|
||||
return False, None, None, "no greeting: " + greet
|
||||
r = cmd("USER " + user)
|
||||
if not r.startswith("331"):
|
||||
return False, None, None, "USER: " + r
|
||||
r = cmd("PASS " + pwd)
|
||||
if not r.startswith("230"):
|
||||
return False, None, None, "PASS: " + r
|
||||
r = cmd("PASV")
|
||||
m = re.search(r"\((\d+),(\d+),(\d+),(\d+),(\d+),(\d+)\)", r)
|
||||
if not r.startswith("227") or not m:
|
||||
return False, None, None, "PASV: " + r
|
||||
g = [int(x) for x in m.groups()]
|
||||
pasv_port = g[4] * 256 + g[5]
|
||||
d = socket.create_connection((ip, pasv_port), timeout=8)
|
||||
pc_src = d.getsockname()[1]
|
||||
r = cmd("STOR " + fname)
|
||||
if not r.startswith(("125", "150")):
|
||||
d.close()
|
||||
return False, pasv_port, pc_src, "STOR: " + r
|
||||
d.sendall(payload)
|
||||
d.close()
|
||||
# drain until final 2xx (multi-line or single)
|
||||
final = ""
|
||||
t0 = time.time()
|
||||
while time.time() - t0 < 10:
|
||||
r = readline()
|
||||
if not r:
|
||||
break
|
||||
final = r
|
||||
if r[:1] in ("2", "4", "5"):
|
||||
break
|
||||
ok = final.startswith("2")
|
||||
cmd("QUIT")
|
||||
return ok, pasv_port, pc_src, ("OK" if ok else "final: " + final)
|
||||
finally:
|
||||
c.close()
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--ip", default=IP_DEFAULT)
|
||||
ap.add_argument("--rounds", type=int, default=5)
|
||||
ap.add_argument("--delay", type=float, default=2.0,
|
||||
help="seconds between rounds")
|
||||
ap.add_argument("--user", default="admin")
|
||||
ap.add_argument("--pwd", default="admin")
|
||||
ap.add_argument("--size", type=int, default=1024)
|
||||
args = ap.parse_args()
|
||||
|
||||
payload = bytes((i * 7 + 3) & 0xFF for i in range(args.size))
|
||||
ports, srcs, oks = [], [], []
|
||||
for i in range(args.rounds):
|
||||
t = time.strftime("%H:%M:%S")
|
||||
ok, port, src, detail = ftp_stor_round(args.ip, args.user, args.pwd,
|
||||
"PP%d.BIN" % i, payload)
|
||||
ports.append(port)
|
||||
srcs.append(src)
|
||||
oks.append(ok)
|
||||
print("[%s] round %d: ok=%-5s pasv_port=%-6s pc_src=%-6s %s"
|
||||
% (t, i + 1, ok, port, src, detail))
|
||||
if i + 1 < args.rounds:
|
||||
time.sleep(args.delay)
|
||||
|
||||
print("\nports :", ports)
|
||||
print("pc_src:", srcs)
|
||||
print("ok :", oks)
|
||||
same_port = len(set(p for p in ports if p)) == 1
|
||||
same_src = len(set(s for s in srcs if s)) == 1
|
||||
print("PASV port constant: %s | PC source port constant: %s"
|
||||
% (same_port, same_src))
|
||||
return 0 if all(oks) else 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,77 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""relisten_stress.py - listener reconnect stress (E1/E5 bench leg).
|
||||
|
||||
Connect -> optional hold -> immediate close, repeated at a fixed gap.
|
||||
Counts refusals and connect latency distribution; non-ok rounds listed.
|
||||
Pure stdlib, ASCII output only.
|
||||
|
||||
Usage:
|
||||
python relisten_stress.py --port 7003 --n 50 --gap-ms 100
|
||||
python relisten_stress.py --port 7003 --n 20 --gap-ms 0 --hold-ms 50
|
||||
Exit codes: 0 = zero refusals / 1 = refusals seen / 2 = usage.
|
||||
"""
|
||||
import os
|
||||
import argparse
|
||||
import socket
|
||||
import sys
|
||||
import time
|
||||
|
||||
# 台架连接默认值单一真源已随域归档到 test/host/bench/bench_defaults.py:
|
||||
# Python 的 sys.path[0] 是**脚本自身所在目录**,本脚本在 bench/<域>/ 下直接
|
||||
# `import bench_defaults` 会 ModuleNotFoundError —— 先把 bench/ 补进 sys.path。
|
||||
_BENCH_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
if _BENCH_DIR not in sys.path:
|
||||
sys.path.insert(0, _BENCH_DIR)
|
||||
|
||||
import bench_defaults # noqa: E402 (台架连接默认值单一真源)
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description="listener reconnect stress")
|
||||
ap.add_argument("--host", default=bench_defaults.DEVICE_IP)
|
||||
ap.add_argument("--port", type=int, required=True)
|
||||
ap.add_argument("--n", type=int, default=50)
|
||||
ap.add_argument("--gap-ms", type=int, default=100)
|
||||
ap.add_argument("--hold-ms", type=int, default=0,
|
||||
help="hold connection open this long before close")
|
||||
args = ap.parse_args()
|
||||
|
||||
results = []
|
||||
for i in range(args.n):
|
||||
t0 = time.time()
|
||||
try:
|
||||
s = socket.create_connection((args.host, args.port), timeout=3)
|
||||
lat = (time.time() - t0) * 1000.0
|
||||
if args.hold_ms:
|
||||
time.sleep(args.hold_ms / 1000.0)
|
||||
try:
|
||||
s.shutdown(socket.SHUT_RDWR)
|
||||
except OSError:
|
||||
pass
|
||||
s.close()
|
||||
results.append((i, "ok", lat))
|
||||
except ConnectionRefusedError:
|
||||
results.append((i, "refused", (time.time() - t0) * 1000.0))
|
||||
except Exception as e:
|
||||
results.append((i, type(e).__name__, (time.time() - t0) * 1000.0))
|
||||
if args.gap_ms:
|
||||
time.sleep(args.gap_ms / 1000.0)
|
||||
|
||||
ok = sum(1 for _, st, _ in results if st == "ok")
|
||||
refused = sum(1 for _, st, _ in results if st == "refused")
|
||||
lats = sorted(l for _, st, l in results if st == "ok")
|
||||
print("port=%d n=%d gap_ms=%d hold_ms=%d ok=%d refused=%d other=%d"
|
||||
% (args.port, args.n, args.gap_ms, args.hold_ms,
|
||||
ok, refused, args.n - ok - refused))
|
||||
if lats:
|
||||
print("connect latency ms: min=%.0f p50=%.0f p90=%.0f max=%.0f"
|
||||
% (lats[0], lats[len(lats) // 2], lats[int(len(lats) * 0.9)], lats[-1]))
|
||||
for i, st, l in results:
|
||||
if st != "ok":
|
||||
print(" round %d: %s (%.0f ms)" % (i, st, l))
|
||||
return 1 if refused else 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,336 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""UDP 数据面队列化真机回归 —— 板端阶段 16 的 PC 对端。
|
||||
|
||||
用途:配合 test/board/net_test_task.c 的 net_layer_udp_bench_test_main()
|
||||
(板端做 UDP 服务端,从收到的包里学对端地址再回显),把 PR #74「UDP 并入
|
||||
netMsgQueue」这条新链路放上真芯片逐步激励。之所以板端做服务端:台架 PC 是
|
||||
DHCP 地址,与 net_test_task.h 的 TEST_PC_PEER_IP 不一定一致,服务端在先就
|
||||
不需要 PC 侧固定 IP。
|
||||
|
||||
跑法(板子烧好阶段 16 固件后):
|
||||
python test/host/bench/net/udp_queue_bench.py --board 192.168.31.100
|
||||
# 可选:--first-wait 180 等板子重启;--late 12 调迟发时刻
|
||||
|
||||
判读:每步打印 [PASS]/[FAIL],末尾一行
|
||||
=== UDP Bench PC Peer: N/N PASSED ===
|
||||
任一步失败即非零退出。**板端的汇总行在串口日志里**(=== Phase 16: ... ===),
|
||||
两侧都要看:板端能看见"收到多少字节、模式切没切对",本端只能看见
|
||||
"回显是否完整、是不是一个数据报"。两端合起来才是完整判据。
|
||||
|
||||
与板端必须一致的约定(改一边就改另一边,见 net_test_task.c 的 Phase 16 头注释):
|
||||
载荷 p[0]=tag,p[i]=(i*7+0x11)&0xFF;板端原样回显。
|
||||
tag 次序:0x01(20B) 0x02(48B) 0x03(20B 换源端口) 0x04(200B 截断成 64)
|
||||
0x05(30B 残包排空) —静默超时探针— 0x06(24B 迟发包) 0x07(20B 换端口 6002)
|
||||
0x08/0x09/0x0A/0x0B/0x0C = 1/20/1024/1025/1472 字节往返。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import socket
|
||||
import sys
|
||||
import time
|
||||
|
||||
for _stream in (sys.stdout, sys.stderr):
|
||||
if hasattr(_stream, "reconfigure"):
|
||||
_stream.reconfigure(encoding="utf-8", errors="replace")
|
||||
|
||||
# 阶段 16 步骤表:tag -> (发往端口, 本次发送长度, 期望回显长度)
|
||||
# 期望回显长度 != 发送长度 的那两行就是截断步。
|
||||
STEPS = (
|
||||
("TC-NET-1602", 0x01, 6001, 20, 20),
|
||||
("TC-NET-1603", 0x02, 6001, 48, 48),
|
||||
("TC-NET-1604", 0x03, 6001, 20, 20),
|
||||
("TC-NET-1605a", 0x04, 6001, 200, 64),
|
||||
("TC-NET-1605b", 0x05, 6001, 30, 30),
|
||||
# TC-NET-1606 = 板端超时探针,本端不发数据(见 wait_quiet)
|
||||
("TC-NET-1607", 0x06, 6001, 24, 24),
|
||||
("TC-NET-1608", 0x07, 6002, 20, 20),
|
||||
("TC-NET-1609a", 0x08, 6002, 1, 1),
|
||||
("TC-NET-1609b", 0x09, 6002, 20, 20),
|
||||
("TC-NET-1609c", 0x0A, 6002, 1024, 1024),
|
||||
("TC-NET-1609d", 0x0B, 6002, 1025, 1025),
|
||||
("TC-NET-1609e", 0x0C, 6002, 1472, 1472),
|
||||
)
|
||||
|
||||
|
||||
def payload(tag: int, n: int) -> bytes:
|
||||
"""与板端 udp_bench_fill() 同式的自校验载荷。"""
|
||||
if n <= 0:
|
||||
return b""
|
||||
out = bytearray(n)
|
||||
out[0] = tag & 0xFF
|
||||
for i in range(1, n):
|
||||
out[i] = (i * 7 + 0x11) & 0xFF
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def new_peer_socket() -> socket.socket:
|
||||
"""建一条 UDP 源套接句;能关就关掉 Windows 的「ICMP 端口不可达回抛」。
|
||||
|
||||
真机第一次上台架(2026-11-03 20:54)就是栽在这里:板子还没把 UDP socket 打开时,
|
||||
我们发过去的第一颗包换来一个 ICMP 端口不可达,Windows 把它记在套接句上,随后的
|
||||
recvfrom 直接抛 `ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个
|
||||
现有的连接`——对端脚本当场死在第一条用例,板子白等到 25 秒超时,两边看到的都是
|
||||
「对端缺席」,分不清是谁的问题。
|
||||
|
||||
能关掉这个行为当然最好(SIO_UDP_CONNRESET=0),但实测本机 CPython 3.14.7 既没有
|
||||
`socket.SIO_UDP_CONNRESET` 这个常量,也不接受把它的数值 0x9800000C 直接喂给 ioctl
|
||||
(`ValueError: invalid ioctl command`)。所以**主力防线是 recv 侧把 WinError 10054
|
||||
当成「这一轮没收到」继续重试**(见 _recv_after / _recv_maybe 的 except),这里的
|
||||
ioctl 只是有常量时的锦上添花。判据由 udp_queue_bench_loopback.py 的冷端口探针守着。
|
||||
"""
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
flag = getattr(socket, "SIO_UDP_CONNRESET", None)
|
||||
if flag is not None:
|
||||
try:
|
||||
s.ioctl(flag, False)
|
||||
except (OSError, ValueError):
|
||||
pass
|
||||
return s
|
||||
|
||||
|
||||
class Peer:
|
||||
def __init__(self, board: str, timeout: float, step_budget: float = 35.0) -> None:
|
||||
self.board = board
|
||||
self.timeout = timeout
|
||||
# 单步总预算:必须盖过板端单步等待(UDP_BENCH_WAIT_MS = 25 s)再加余量,理由见
|
||||
# exchange() 里的说明。--first-wait 只覆盖第一步(板子要启动+起监听)。
|
||||
self.step_budget = step_budget
|
||||
self.total = 0
|
||||
self.failed = 0
|
||||
self.ports = {}
|
||||
self.t_start = time.time()
|
||||
|
||||
def stamp(self) -> str:
|
||||
return "[peer {0:7.2f}s]".format(time.time() - self.t_start)
|
||||
|
||||
def log(self, msg: str) -> None:
|
||||
print("{0} {1}".format(self.stamp(), msg), flush=True)
|
||||
|
||||
def note_port(self, sock: socket.socket, tag: int) -> None:
|
||||
"""首次用到某条源套接句时把它的临时源端口打出来(真机日志要和这个对得上)。"""
|
||||
key = id(sock)
|
||||
if key in self.ports:
|
||||
return
|
||||
try:
|
||||
self.ports[key] = sock.getsockname()[1]
|
||||
self.log("TC 0x{0:02X} 走源端口 {1}".format(tag, self.ports[key]))
|
||||
except OSError:
|
||||
self.ports[key] = 0
|
||||
|
||||
def check(self, cond: bool, name: str) -> bool:
|
||||
self.total += 1
|
||||
self.log("[{0}] {1}".format("PASS" if cond else "FAIL", name))
|
||||
if not cond:
|
||||
self.failed += 1
|
||||
return cond
|
||||
|
||||
def close(self) -> None:
|
||||
pass
|
||||
|
||||
def exchange(self, sock: socket.socket, port: int, tag: int, send_len: int,
|
||||
exp_len: int, first_wait: float = 0.0) -> bool:
|
||||
"""一个数据报进、一个数据报出;重发只在"完全没收到"时发生。"""
|
||||
dst = (self.board, port)
|
||||
data_out = payload(tag, send_len)
|
||||
want = payload(tag, exp_len)
|
||||
|
||||
# 先清掉上一步可能迟到的回显,避免把它当成本步的应答
|
||||
self._drain(sock)
|
||||
|
||||
# 每一步都重发,且单步预算必须盖过板端单步等待(UDP_BENCH_WAIT_MS = 25 s)再加
|
||||
# 余量:板端只要超时一次就会晚一步,本端若只等 8 秒,后面每一步都会踩进板子的
|
||||
# 上一步预算里,整条梯子一起错位(2026-11-03 第一次上台架就是这样全灭)。
|
||||
# 重发是安全的:板端按载荷第 0 字节的步号丢掉旧包(步序自愈),本端每步开头也
|
||||
# 会先 drain 掉迟到的回显。
|
||||
step_budget = first_wait if first_wait > 0 else self.step_budget
|
||||
deadline = time.time() + step_budget
|
||||
got = None
|
||||
addr = None
|
||||
while got is None:
|
||||
sock.sendto(data_out, dst)
|
||||
left = deadline - time.time()
|
||||
if left <= 0:
|
||||
return self.check(False, "TC 0x{0:02X} 端口 {1}: 板端 {2:.0f}s 内无回显"
|
||||
.format(tag, port, step_budget))
|
||||
attempt = 4.0 if first_wait > 0 else 6.0
|
||||
round_deadline = time.time() + min(attempt, left)
|
||||
resets = 0
|
||||
while got is None:
|
||||
remain = round_deadline - time.time()
|
||||
if remain <= 0:
|
||||
break
|
||||
try:
|
||||
got, addr = self._recv_after(sock, remain)
|
||||
self.note_port(sock, tag)
|
||||
except socket.timeout:
|
||||
break # 这一轮确实等满了,出去重发一次
|
||||
except ConnectionResetError:
|
||||
# ICMP 端口不可达(见 new_peer_socket 的说明)——它只说明「板端把
|
||||
# 某个回显发给了本端已经关掉的临时端口」,并不代表本端这一步该重发。
|
||||
# 2026-11-04 台架上的教训:把它当成「没收到」立刻重发,会在一秒里
|
||||
# 往芯片灌上百个包(日志 148 s 起每秒上百行),把整条梯子彻底冲乱。
|
||||
# 正确做法:本轮不再重发,安静地等完本轮预算,节奏仍由 attempt 决定。
|
||||
resets += 1
|
||||
if resets == 1 or resets % 20 == 0:
|
||||
self.log("TC 0x{0:02X}: 收到 {1} 次 ICMP 不可达(板端回显打到了本端"
|
||||
"已关闭的端口),本轮不重发,继续等到 {2:.0f}s"
|
||||
.format(tag, resets, round_deadline))
|
||||
time.sleep(0.2)
|
||||
if got is None and resets == 0:
|
||||
self.log("TC 0x{0:02X}: 无回显,重发(本步剩余 {1:.0f}s)".format(tag, left))
|
||||
# 一个数据报进、一个数据报出——唯一例外是本步载荷超过芯片发送缓冲(1KB):
|
||||
# 那是已登记的芯片限制(NET_SEND_CHUNK_MAX 本是 TCP 流的分块口径,UDP 上每块
|
||||
# 各发一包,改造前后同一条路径)。>1KB 的步骤按「预期拆包」判,并把实测拆分打
|
||||
# 出来:真机上哪天不拆了,这条一样会报——那就是芯片行为变了,值得知道。
|
||||
chip_chunk = 1024
|
||||
parts = [got]
|
||||
if exp_len > chip_chunk:
|
||||
while sum(len(x) for x in parts) < exp_len:
|
||||
more = self._recv_maybe(sock, 1.0)
|
||||
if more is None:
|
||||
break
|
||||
parts.append(more[0])
|
||||
else:
|
||||
again = self._recv_maybe(sock, 0.300)
|
||||
if again is not None:
|
||||
parts.append(again[0])
|
||||
total = b"".join(parts)
|
||||
|
||||
ok_len = (len(total) == exp_len)
|
||||
ok_pat = ok_len and (total == want)
|
||||
self.check(ok_len, "TC 0x{0:02X} 端口 {1}: 回显 {2} 字节(期望 {3},收到 {4} 片)"
|
||||
.format(tag, port, len(total), exp_len, len(parts)))
|
||||
if not ok_len:
|
||||
self.log(" 各片长度 = {0},首 16 字节 = {1}".format(
|
||||
[len(x) for x in parts], total[:16].hex()))
|
||||
self.check(ok_pat, "TC 0x{0:02X}: 载荷逐字节一致(模式 p[i]=(i*7+0x11)&0xFF)".format(tag))
|
||||
if not ok_pat and ok_len:
|
||||
bad = next((i for i in range(exp_len) if total[i] != want[i]), -1)
|
||||
self.log(" 首个差异 @ 字节 {0}: 收到 0x{1:02X},应为 0x{2:02X}"
|
||||
.format(bad, total[bad], want[bad]))
|
||||
if exp_len > chip_chunk:
|
||||
self.check(len(parts) > 1 and len(parts[0]) == chip_chunk,
|
||||
"TC 0x{0:02X}: >1KB 回显按芯片发送缓冲在 1024 处拆包(实测 {1})"
|
||||
.format(tag, [len(x) for x in parts]))
|
||||
elif len(parts) > 1:
|
||||
self.check(False, "TC 0x{0:02X}: 未超 1KB 却收到 {1} 个数据报 {2}"
|
||||
" —— 发送路径的分块口径变了"
|
||||
.format(tag, len(parts) - 1, [len(x) for x in parts[1:]]))
|
||||
return ok_len and ok_pat and (exp_len > chip_chunk or len(parts) == 1)
|
||||
|
||||
def wait_quiet(self, sock: socket.socket, seconds: float, name: str) -> bool:
|
||||
"""板端超时探针期间本端必须保持静默,且不能有过期包漂进来。"""
|
||||
time.sleep(seconds)
|
||||
stray = self._drain(sock)
|
||||
return self.check(stray == 0, "{0}: 静默期无残留数据报(清掉 {1} 个)".format(name, stray))
|
||||
|
||||
# ---- 内部 ----
|
||||
def _recv_after(self, sock: socket.socket, budget: float):
|
||||
sock.settimeout(max(budget, 0.05))
|
||||
data, addr = sock.recvfrom(4096)
|
||||
return data, addr
|
||||
|
||||
def _recv_maybe(self, sock: socket.socket, budget: float):
|
||||
try:
|
||||
sock.settimeout(budget)
|
||||
return sock.recvfrom(4096)
|
||||
except (socket.timeout, ConnectionResetError):
|
||||
return None
|
||||
finally:
|
||||
sock.settimeout(self.timeout)
|
||||
|
||||
def _drain(self, sock: socket.socket) -> int:
|
||||
n = 0
|
||||
while True:
|
||||
r = self._recv_maybe(sock, 0.050)
|
||||
if r is None:
|
||||
return n
|
||||
n += 1
|
||||
|
||||
|
||||
def main() -> int:
|
||||
ap = argparse.ArgumentParser(description="UDP 队列化真机回归 PC 对端(板端阶段 16)")
|
||||
ap.add_argument("--board", required=True, help="板子 IP,例如 192.168.31.100")
|
||||
ap.add_argument("--first-wait", type=float, default=120.0,
|
||||
help="首步最长等待(覆盖板子烧录后重启+起监听),默认 120 秒")
|
||||
ap.add_argument("--timeout", type=float, default=20.0, help="单步回显等待,默认 20 秒")
|
||||
ap.add_argument("--probe-silence", type=float, default=2.0,
|
||||
help="对应 TC-NET-1606 的静默时长(板端探针预算 1 秒)")
|
||||
ap.add_argument("--late", type=float, default=12.0,
|
||||
help="对应 TC-NET-1607 的迟发时长:板端在这一步无限等包")
|
||||
ap.add_argument("--step-budget", type=float, default=35.0,
|
||||
help="单步总预算(含重发),必须盖过板端 25 秒单步等待,默认 35 秒")
|
||||
ap.add_argument("--port", type=int, default=0,
|
||||
help="覆盖阶段 16 的首个监听端口(默认用板端 UDP_BENCH_PORT 6001)")
|
||||
ap.add_argument("--port2", type=int, default=0,
|
||||
help="覆盖重开 socket 后的第二个监听端口(默认用板端 UDP_BENCH_PORT2 6002)")
|
||||
args = ap.parse_args()
|
||||
# 板端 TC-NET-1607 把「等待时长」判据钉在 7000~25000 ms:本端迟发时刻必须落在窗内,
|
||||
# 且必须先盖过板端 TC-NET-1606 那 1 秒超时探针,否则两步会互相污染。
|
||||
if args.probe_silence <= 1.2:
|
||||
print("--probe-silence 必须大于板端 1 秒超时探针(默认 2.0)", file=sys.stderr)
|
||||
return 2
|
||||
if not (7.0 < args.late <= 22.0):
|
||||
print("--late 必须落在 (7, 22] 秒内,否则板端 7000~25000ms 判定窗会判失败", file=sys.stderr)
|
||||
return 2
|
||||
if args.step_budget <= 26.0:
|
||||
# 只提醒不拦死:自检脚本用假板子跑,预算给得很小;真机台架则会看到这条。
|
||||
print("[warn] --step-budget={0:.0f}s 小于板端单步等待 25s + 余量:"
|
||||
"板端一旦超时一次,本端后续每一步都会踩进板子的上一步预算里(错位全灭)"
|
||||
.format(args.step_budget), file=sys.stderr)
|
||||
|
||||
peer = Peer(args.board, args.timeout, args.step_budget)
|
||||
# 三条独立源套接句 = 三个不同临时源端口,1604「换源端口」就是这么来的。
|
||||
# 端口号不能在未 connect 时用 getsockname() 取(Windows 会 WinError 10022,
|
||||
# 端口要到第一次 sendto 才分配),也不能 connect()——connect 会让内核丢掉
|
||||
# 非对端的包,那正是这一步要验的东西。改成首次收包后再查,照样打得出来。
|
||||
socks = {"A": new_peer_socket(),
|
||||
"B": new_peer_socket(),
|
||||
"C": new_peer_socket()}
|
||||
peer.log("对端 = {0},三条源套接句 A/B/C 的临时源端口在首次收包后打印".format(args.board))
|
||||
|
||||
# 板端每一步用新源端口的条件:1604(换源端口验形态恢复)及其后
|
||||
step_sock = {
|
||||
"TC-NET-1602": "A", "TC-NET-1603": "A",
|
||||
"TC-NET-1604": "B", "TC-NET-1605a": "B", "TC-NET-1605b": "B",
|
||||
"TC-NET-1607": "B",
|
||||
"TC-NET-1608": "C", "TC-NET-1609a": "C", "TC-NET-1609b": "C",
|
||||
"TC-NET-1609c": "C", "TC-NET-1609d": "C", "TC-NET-1609e": "C",
|
||||
}
|
||||
|
||||
# 端口覆盖:只在显式给了 --port/--port2 时替换,默认沿用板端编译期的 UDP_BENCH_PORT(_2)。
|
||||
# 板端串口横幅会把自己的端口打出来,照抄那条命令时用它换掉非默认端口。
|
||||
port_map = {}
|
||||
if args.port:
|
||||
port_map[6001] = args.port
|
||||
if args.port2:
|
||||
port_map[6002] = args.port2
|
||||
|
||||
try:
|
||||
for name, tag, port, send_len, exp_len in STEPS:
|
||||
s = socks[step_sock[name]]
|
||||
port = port_map.get(port, port)
|
||||
fw = args.first_wait if name == "TC-NET-1602" else 0.0
|
||||
# 1607 之前先做 1606 的静默探针(板端自己判 -2,本端只需保持静默)
|
||||
if name == "TC-NET-1607":
|
||||
peer.wait_quiet(s, args.probe_silence, "TC-NET-1606")
|
||||
left = max(args.late - args.probe_silence, 0.0)
|
||||
peer.log("TC-NET-1607: 再静默 {0:.1f}s,等板子挂进无限 net_recvfrom".format(left))
|
||||
time.sleep(left)
|
||||
peer.exchange(s, port, tag, send_len, exp_len, fw)
|
||||
except KeyboardInterrupt:
|
||||
peer.log("用户中断")
|
||||
finally:
|
||||
for s in socks.values():
|
||||
s.close()
|
||||
|
||||
print("=== UDP Bench PC Peer: {0}/{1} PASSED (failed={2}) ===".format(
|
||||
peer.total - peer.failed, peer.total, peer.failed), flush=True)
|
||||
return 0 if peer.failed == 0 else 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,143 @@
|
||||
"""PC peer 自身的回路自检:不需要板子,用假板子把 udp_queue_bench.py 的步骤表跑一遍。
|
||||
|
||||
为什么要有这个文件:阶段 16 的真机台架是一次性的——板子通电、烧好台架镜像、PC 起对端,
|
||||
对端脚本自己要是有 bug(取端口、步骤表、拆包预期),一趟台架时间就白费了。这里在
|
||||
127.0.0.1 上放一个「按板端阶段 16 同样规矩说话」的假板子,把对端脚本的判定逻辑先验一遍。
|
||||
|
||||
假板子复刻的三条规矩(与 test/board/net_test_task.c 阶段 16 一致):
|
||||
1. 收到的数据报原样回显给发送方(tag 字节不动,所以源地址/剥头判据留在板端断言);
|
||||
2. tag 0x04 这一步板端故意只用 64 字节缓冲去读 200 字节的数据报,所以只回 64 字节;
|
||||
3. 回显超过芯片发送缓冲(1KB)时按 1024 拆成多个数据报发出(NET_SEND_CHUNK_MAX)。
|
||||
|
||||
用法(在本仓库根目录):
|
||||
python test/host/bench/net/udp_queue_bench_loopback.py
|
||||
退出码:0 = 对端脚本判定逻辑全绿;1 = 有问题,先别上台架。
|
||||
"""
|
||||
|
||||
import os
|
||||
import socket
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
if HERE not in sys.path:
|
||||
sys.path.insert(0, HERE)
|
||||
|
||||
import udp_queue_bench as peer # noqa: E402
|
||||
|
||||
CHIP_SEND_BUF = 1024 # NET_SEND_CHUNK_MAX
|
||||
BOARD_RX_CAP = 1600 # 板端 s_udp_buf 尺寸 UDP_BENCH_BUF_SIZE
|
||||
TRUNCATE_TAG = 0x04 # 这一步板端只用 64 字节缓冲
|
||||
TRUNCATE_CAP = 64
|
||||
RUNOUT_S = 90.0
|
||||
LISTEN_PORTS = (6001, 6002) # UDP_BENCH_PORT / UDP_BENCH_PORT2
|
||||
|
||||
|
||||
def echo_one(sock, deadline, stop):
|
||||
sock.settimeout(0.5)
|
||||
while (not stop.is_set()) and (time.time() < deadline):
|
||||
try:
|
||||
data, addr = sock.recvfrom(4096)
|
||||
except socket.timeout:
|
||||
continue
|
||||
except OSError:
|
||||
return
|
||||
if not data:
|
||||
continue
|
||||
tag = data[0]
|
||||
cap = TRUNCATE_CAP if tag == TRUNCATE_TAG else BOARD_RX_CAP
|
||||
echo = peer.payload(tag, min(len(data), cap))
|
||||
print("[board] rx 0x{0:02X} n={1} on :{2} -> echo {3}B".format(
|
||||
tag, len(data), sock.getsockname()[1], len(echo)), flush=True)
|
||||
for off in range(0, len(echo), CHIP_SEND_BUF):
|
||||
sock.sendto(echo[off:off + CHIP_SEND_BUF], addr)
|
||||
|
||||
|
||||
def check_cold_port() -> bool:
|
||||
"""对端脚本必须先扛住「板子还没把 socket 打开」这段时间——真机第一次上台架就死在这。
|
||||
|
||||
往一个确定没人监听的端口发一颗包,Windows 会收下 ICMP 端口不可达并记在套接句上,
|
||||
下一次 recvfrom 就抛 WinError 10054。这里走**脚本真正在用的那条路**(Peer._recv_maybe),
|
||||
要求它返回 None(= 这一轮没收到,继续重试),而不是把异常抛穿。裸套接句那一段只是
|
||||
证明这台机器确实会回 ICMP——不然这个探针没有牙,别把它当唯一证据。
|
||||
"""
|
||||
dead = 6099 # 假板子只绑 6001/6002,这个端口确定空着
|
||||
ok = False
|
||||
p = peer.Peer("127.0.0.1", 1.0)
|
||||
s = peer.new_peer_socket()
|
||||
try:
|
||||
s.settimeout(1.0)
|
||||
s.sendto(b"\x00", ("127.0.0.1", dead))
|
||||
try:
|
||||
r = p._recv_maybe(s, 1.0)
|
||||
if r is None:
|
||||
ok = True
|
||||
else:
|
||||
print("[selftest] 冷端口探针:竟然收到了数据报,探针失效", flush=True)
|
||||
except ConnectionResetError:
|
||||
print("[selftest] 冷端口探针:Peer._recv_maybe 把 WinError 10054 抛穿了"
|
||||
"(真机上对端会当场死在第一条用例)", flush=True)
|
||||
finally:
|
||||
s.close()
|
||||
except OSError as exc:
|
||||
print("[selftest] 冷端口探针建句/发送失败:{0}".format(exc), flush=True)
|
||||
|
||||
raw = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
try:
|
||||
raw.settimeout(1.0)
|
||||
raw.sendto(b"\x00", ("127.0.0.1", dead))
|
||||
try:
|
||||
raw.recvfrom(64)
|
||||
print("[selftest] 裸套接句也收到了数据报(探针无牙,仅记录)", flush=True)
|
||||
except ConnectionResetError:
|
||||
print("[selftest] 裸套接句抛 WinError 10054 —— 这台机器确实会踩,"
|
||||
"上面那条判定有牙", flush=True)
|
||||
except socket.timeout:
|
||||
print("[selftest] 裸套接句超时(本机没回 ICMP,探针无牙,仅记录)", flush=True)
|
||||
except OSError as exc:
|
||||
print("[selftest] 裸套接句探针失败:{0}".format(exc), flush=True)
|
||||
finally:
|
||||
raw.close()
|
||||
return ok
|
||||
|
||||
|
||||
def main() -> int:
|
||||
if hasattr(sys.stdout, "reconfigure"):
|
||||
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
||||
if not check_cold_port():
|
||||
print("=== UDP Peer Loopback Self-Test: FAIL(冷端口探针没过,先修 new_peer_socket)===",
|
||||
flush=True)
|
||||
return 1
|
||||
deadline = time.time() + RUNOUT_S
|
||||
stop = threading.Event()
|
||||
for listen_port in LISTEN_PORTS:
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
try:
|
||||
s.bind(("127.0.0.1", listen_port))
|
||||
except OSError as exc:
|
||||
print("无法绑定 127.0.0.1:{0}:{1}(端口被占用?先腾出来再跑)".format(listen_port, exc),
|
||||
file=sys.stderr)
|
||||
return 2
|
||||
threading.Thread(target=echo_one, args=(s, deadline, stop), daemon=True).start()
|
||||
time.sleep(0.3)
|
||||
|
||||
# --late 必须落在板端 7000~25000 ms 判定窗内;这里取 9 秒,比默认 12 秒省一点台架外时间
|
||||
# --step-budget 8:假板子秒回,用不到真机那 35 秒预算;真机台架千万别照抄这个值
|
||||
#(真机上必须盖过板端单步 25 秒等待,否则一次超时就整条梯子错位)。
|
||||
sys.argv = ["udp_queue_bench.py", "--board", "127.0.0.1",
|
||||
"--first-wait", "15", "--timeout", "6", "--step-budget", "8",
|
||||
"--probe-silence", "2.0", "--late", "9.0"]
|
||||
try:
|
||||
rc = peer.main()
|
||||
finally:
|
||||
stop.set()
|
||||
if rc == 0:
|
||||
print("=== UDP Peer Loopback Self-Test: PASS(对端脚本判定逻辑可用,可上台架)===", flush=True)
|
||||
else:
|
||||
print("=== UDP Peer Loopback Self-Test: FAIL(先修对端脚本,别浪费台架时间)===", flush=True)
|
||||
return rc
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user