import subprocess,sys,os
TV=os.path.dirname(os.path.abspath(__file__)); A=TV+'/a'
L=sys.argv[1]
o=subprocess.run([A,'shell','dumpsys','SurfaceFlinger','--latency',"'%s'"%L],capture_output=True,text=True).stdout.split('\n')
t=[]
for x in o[1:]:
    p=x.split()
    if len(p)==3 and p[1]!='0' and int(p[1])<2**62: t.append(int(p[1])/1e9)
t.sort()
print(o[0].strip(),len(t),'frames, span',round(t[-1]-t[0],2) if t else 0)
g=[b-a for a,b in zip(t,t[1:])]
import statistics
if g:
    g2=sorted(g); print('median gap ms',round(statistics.median(g)*1000),'p90',round(g2[int(len(g2)*.9)]*1000),'max',round(g2[-1]*1000), 'fps',round(len(g)/(t[-1]-t[0]),1))
