This commit is contained in:
Michael Davidsaver
2019-08-30 19:55:14 -07:00
parent 1c6ce2aca9
commit 89ee280cd0

View File

@ -1,7 +1,12 @@
"""Python reference counter statistics. """Python reference counter statistics.
""" """
from __future__ import print_function
import sys, gc, inspect, time import sys, gc, inspect, time
from types import InstanceType try:
from types import InstanceType
except ImportError:
pass # py3
class StatsDelta(object): class StatsDelta(object):
"""GC statistics tracking. """GC statistics tracking.
@ -30,35 +35,35 @@ class StatsDelta(object):
Nprev = self.ntypes # may be less than len(prev) Nprev = self.ntypes # may be less than len(prev)
if Ncur!=Nprev: if Ncur!=Nprev:
print >>file,"# Types %d -> %d"%(Nprev,Ncur) print("# Types %d -> %d"%(Nprev,Ncur), file=file)
Scur, Sprev, first = set(cur), set(prev), True Scur, Sprev, first = set(cur), set(prev), True
for T in Scur-Sprev: # new types for T in Scur-Sprev: # new types
if first: if first:
print >>file,'New Types' print('New Types', file=file)
first=False first=False
print >>file,' ',T,cur[T] print(' ',T,cur[T], file=file)
first = True first = True
for T in Sprev-Scur: # collected types for T in Sprev-Scur: # collected types
if first: if first:
print >>file,'Cleaned Types' print('Cleaned Types', file=file)
first=False first=False
print >>file,' ',T,-prev[T] print(' ',T,-prev[T], file=file)
first = True first = True
for T in Scur&Sprev: for T in Scur&Sprev:
if cur[T]==prev[T]: if cur[T]==prev[T]:
continue continue
if first: if first:
print >>file,'Known Types' print('Known Types', file=file)
first=False first=False
print >>file,' ',T,cur[T],'delta',cur[T]-prev[T] print(' ',T,cur[T],'delta',cur[T]-prev[T], file=file)
else: # first call else: # first call
print >>file,"All Types" print("All Types", file=file)
for T,C in cur.items(): for T,C in cur.items():
print >>file,' ',T,C print(' ',T,C, file=file)
self.stats, self.ntypes = cur, len(cur) self.stats, self.ntypes = cur, len(cur)
#gc.collect() #gc.collect()
@ -76,7 +81,7 @@ def gcstats():
if K is StatsDelta: if K is StatsDelta:
continue # avoid counting ourselves continue # avoid counting ourselves
elif K is InstanceType: # instance of an old-style class elif InstanceType is not None and K is InstanceType: # instance of an old-style class
K = getattr(obj, '__class__', K) K = getattr(obj, '__class__', K)
# Track types as strings to avoid holding references # Track types as strings to avoid holding references
@ -119,9 +124,9 @@ def periodic(period=60.0, file=sys.stderr):
if __name__=='__main__': if __name__=='__main__':
#for T,C in gcstats().items(): #for T,C in gcstats().items():
# print T,C # print T,C
gc.set_debug(gc.DEBUG_COLLECTABLE|gc.DEBUG_INSTANCES|gc.DEBUG_OBJECTS) gc.set_debug(gc.DEBUG_COLLECTABLE)
S=StatsDelta() S=StatsDelta()
while True: while True:
print 'Iteration' print('Iteration')
S.collect() S.collect()
#gc.collect() #gc.collect()