367 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
97 lines
5.8 KiB
Python
97 lines
5.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Summarize only the three numeric native packet-counter streams after play.
|
|
|
|
Counts in these streams are cumulative per (opcode,size); summing lines would
|
|
count the same packets repeatedly. No other session, cache or account files open.
|
|
"""
|
|
import argparse
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import stat
|
|
|
|
STREAMS=('packets_unhandled','packets_error','client_dropped')
|
|
MAX_BYTES=64*1024*1024
|
|
MAX_LINE=65536
|
|
|
|
def integer(value,low,high):
|
|
return type(value)is int and low<=value<=high
|
|
|
|
def read_lines(path):
|
|
info=path.lstat()
|
|
if not stat.S_ISREG(info.st_mode) or stat.S_ISLNK(info.st_mode) or getattr(info,'st_file_attributes',0)&getattr(stat,'FILE_ATTRIBUTE_REPARSE_POINT',0x400):
|
|
raise ValueError('Counter input must be an ordinary file')
|
|
if info.st_size>MAX_BYTES:raise ValueError('Counter stream exceeds the native64MiB limit')
|
|
fd=os.open(path,os.O_RDONLY|getattr(os,'O_NOFOLLOW',0))
|
|
with os.fdopen(fd,'rb')as source:
|
|
opened=os.fstat(source.fileno())
|
|
if (opened.st_dev,opened.st_ino)!=(info.st_dev,info.st_ino):raise ValueError('Counter file identity changed')
|
|
read_bytes=0
|
|
for index,raw in enumerate(iter(lambda:source.readline(MAX_LINE+1),b''),1):
|
|
read_bytes+=len(raw)
|
|
if read_bytes>MAX_BYTES:raise ValueError('Counter stream grew beyond native size limit')
|
|
if len(raw)>MAX_LINE:raise ValueError('Counter line exceeds metadata bound')
|
|
if not raw.endswith(b'\n'):
|
|
yield index,None
|
|
break
|
|
try:value=json.loads(raw)
|
|
except (ValueError,UnicodeDecodeError):raise ValueError('Malformed numeric counter JSON at line '+str(index))from None
|
|
if not isinstance(value,dict):raise ValueError('Counter JSON object required')
|
|
yield index,value
|
|
|
|
def summarize(session):
|
|
root=Path(session)
|
|
if root.is_symlink()or not root.is_dir():raise ValueError('Ordinary explicit session directory required')
|
|
result={'schema':1,'kind':'native-packet-counter-summary','count_semantics':'latest cumulative count per(opcode,body_size); lines are not summed','streams':{},'missing_streams':[]}
|
|
shared=None
|
|
for name in STREAMS:
|
|
path=root/(name+'.jsonl')
|
|
if not path.exists()and not path.is_symlink():result['missing_streams'].append(name);continue
|
|
header=None;counts={};tail=False
|
|
for index,row in read_lines(path):
|
|
if row is None:tail=True;break
|
|
if index==1:
|
|
if row.get('schema')!=1 or row.get('stream')!=name or row.get('client_build')!='1.60.1.70334':raise ValueError('Native counter header identity differs')
|
|
kind=row.get('server_kind');realm=row.get('realm_id');commit=row.get('stack_commit')
|
|
if kind not in (None,'local','octo')or not integer(realm,1,65535):raise ValueError('Counter header scope differs')
|
|
if commit is not None and (not isinstance(commit,str)or not 7<=len(commit)<=40 or any(c not in '0123456789abcdef'for c in commit)):raise ValueError('Counter source commit is invalid')
|
|
header={'server_kind':kind,'realm_id':realm,'stack_commit':commit}
|
|
if shared is None:shared=header
|
|
elif shared!=header:raise ValueError('Counter streams belong to different scopes')
|
|
continue
|
|
if set(row)!={'t','opcode','size','count'}:raise ValueError('Numeric counter record fields differ; payload fields are never accepted')
|
|
if type(row['t'])not in (int,float)or not 0<=row['t']<=1e12:raise ValueError('Counter timestamp invalid')
|
|
if not integer(row['opcode'],0,0xffffffff)or not integer(row['size'],0,0xffffffff)or not integer(row['count'],1,0xffffffffffffffff):raise ValueError('Counter numeric bound differs')
|
|
key=(row['opcode'],row['size']);previous=counts.get(key,0)
|
|
if row['count']<previous:raise ValueError('Cumulative packet count moved backwards')
|
|
counts[key]=row['count']
|
|
if header is None:raise ValueError('Complete native counter header required')
|
|
rows=[{'opcode':op,'opcode_hex':hex(op),'body_size':size,'count':count}for(op,size),count in counts.items()]
|
|
rows.sort(key=lambda r:(-r['count'],r['opcode'],r['body_size']))
|
|
result['streams'][name]={'packets':sum(r['count']for r in rows),'body_bytes':sum(r['count']*r['body_size']for r in rows),'distinct_opcode_size_pairs':len(rows),'truncated_tail_ignored':tail,'rows':rows}
|
|
result['scope']=shared
|
|
result['qualifications']=[
|
|
'These are diagnostic-boundary counters, not total network traffic or counts of broken features.',
|
|
'A zero error count means no error reached this recorded boundary; it does not prove every feature worked.'
|
|
]
|
|
if shared and shared.get('stack_commit','') and shared['stack_commit'].startswith('dc11050'):
|
|
result['qualifications'].append(
|
|
'This build counted main-switch misses before downstream entity/inventory dispatch; '
|
|
'packets_unhandled includes recognized movement and definition traffic. '
|
|
'Do not treat its total as unsupported packets.'
|
|
)
|
|
# Existence only. The writer updates this marker after an explicit close
|
|
# flush; its absence is not proof that packet collection failed.
|
|
marker=root/'packet-counters-end'
|
|
result['transport_end_marker_present']=not marker.is_symlink()and marker.is_file()
|
|
return result
|
|
|
|
def main():
|
|
p=argparse.ArgumentParser(description=__doc__);p.add_argument('session',type=Path);p.add_argument('--output',type=Path);a=p.parse_args()
|
|
try:report=summarize(a.session)
|
|
except (ValueError,OSError)as e:p.exit(2,str(e)+'\n')
|
|
text=json.dumps(report,indent=2)+'\n'
|
|
if a.output:a.output.write_text(text)
|
|
else:print(text,end='')
|
|
if __name__=='__main__':main()
|