| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- # paint_cgself.py (round 190tz, used by scripts/paint-callgrind.sh): self cost per (object, function) from a callgrind.out file, plus call counts
- import re,sys,collections
- def parse(p):
- names={}; objs={}; selfc=collections.Counter(); calls=collections.Counter()
- ob=fn=None; cfn=None; after_calls=False; fnobj={}
- for l in open(p,errors='replace'):
- l=l.rstrip('\n')
- m=re.match(r'(ob|fl|fi|fe|cob|cfi|cfl)=\((\d+)\)(?: (.*))?$',l)
- if m:
- k,i,n=m.groups()
- if k in('ob','cob'):
- if n: objs[i]=n
- if k=='ob': ob=objs[i]
- continue
- m=re.match(r'(c?fn)=\((\d+)\)(?: (.*))?$',l)
- if m:
- k,i,n=m.groups()
- if n: names[i]=n
- if k=='fn': fn=names[i]; fnobj[fn]=ob
- else: cfn=names[i]
- continue
- m=re.match(r'calls=(\d+)',l)
- if m: calls[cfn]+=int(m.group(1)); after_calls=True; continue
- m=re.match(r'^[+-]?(?:\d+|\*|0x[0-9a-f]+)?\s+(\d+)$',l) or re.match(r'^(?:[+-]?\d+|\*)\s+(\d+)$',l)
- if m and fn is not None:
- if after_calls: after_calls=False
- else: selfc[fn]+=int(m.group(1))
- return selfc,calls,fnobj
- def short(o): o=(o or '???').split('/')[-1]; return 'LYNX' if o.startswith('LYNX') else o
- if __name__=='__main__':
- a,ca,oa=parse(sys.argv[1]); b,cb,ob=parse(sys.argv[2]); n=int(sys.argv[3]) if len(sys.argv)>3 else 15
- agg=collections.defaultdict(lambda:[0,0])
- for f,v in a.items(): agg[short(oa.get(f))][0]+=v
- for f,v in b.items(): agg[short(ob.get(f))][1]+=v
- ta=sum(a.values()); tb=sum(b.values())
- print(f"{'object (self cost)':28} {'before':>13} {'after':>13} {'diff':>11}")
- for o,(x,y) in sorted(agg.items(),key=lambda r:-r[1][0]):
- print(f"{o:28} {x:>13,} {y:>13,} {y-x:+11,} {100*(y-x)/max(1,x):+.3f}%")
- print(f"{'TOTAL':28} {ta:>13,} {tb:>13,} {tb-ta:+11,} {100*(tb-ta)/max(1,ta):+.3f}%")
- print("top functions by self-cost change:")
- rows=sorted(((b.get(f,0)-a.get(f,0),f) for f in set(a)|set(b)),key=lambda r:-abs(r[0]))[:n]
- for d,f in rows: print(f" {a.get(f,0):>12,} {b.get(f,0):>12,} {d:+10,} [{short((ob.get(f) or oa.get(f)))}] {f[:110]}")
- print("call counts:", ', '.join(f"{k} {ca[k]:,}->{cb[k]:,}" for k in ['malloc','free','realloc','operator new(unsigned long)','pthread_mutex_lock']))
- dc=[(cb[k]-ca[k],k) for k in set(ca)|set(cb) if ca[k]!=cb[k] and not k.startswith('0x')]
- print("named functions with a different call count:", len(dc), dc[:8])
|