"""TCP kernel counters only; never reads socket/application contents."""
import json,pathlib,subprocess,time
root=pathlib.Path(__file__).resolve().parent
end=time.monotonic()+140
fast_end=time.monotonic()+20
with (root/'net-trace-tcpinfo.jsonl').open('w') as stream:
    while time.monotonic()<end:
        start=time.monotonic_ns()
        state=subprocess.run(['ss','-tin','( sport = :18081 )'],capture_output=True,text=True,check=True).stdout
        stream.write(json.dumps(dict(wall_ns=time.time_ns(),mono_ns=start,state=state))+'\n')
        stream.flush()
        time.sleep(.1 if time.monotonic()<fast_end else .5)
