tracker.py (4138B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\pympler\tracker.py 2 from __future__ import print_function 3 import gc 4 import inspect 5 from pympler import muppy, summary 6 from pympler.util import compat 7 8 class SummaryTracker(object): 9 10 def __init__(self, ignore_self = True): 11 self.s0 = summary.summarize(muppy.get_objects()) 12 self.summaries = {} 13 self.ignore_self = ignore_self 14 15 def create_summary(self): 16 if not self.ignore_self: 17 res = summary.summarize(muppy.get_objects()) 18 else: 19 all_of_them = [] 20 ref_counter = {} 21 22 def store_info(o): 23 all_of_them.append(o) 24 if id(o) in ref_counter: 25 ref_counter[id(o)] += 1 26 else: 27 ref_counter[id(o)] = 1 28 29 store_info(self.summaries) 30 for k, v in self.summaries.items(): 31 store_info(k) 32 summary._traverse(v, store_info) 33 34 res = summary.summarize(muppy.get_objects()) 35 for _id in ref_counter: 36 if len(gc.get_referrers(_id)) == 3: 37 summary._subtract(res, _id) 38 39 for o in all_of_them: 40 if len(gc.get_referrers(o)) == ref_counter[id(o)] + 2: 41 summary._subtract(res, o) 42 43 return res 44 45 def diff(self, summary1 = None, summary2 = None): 46 res = None 47 if summary2 is None: 48 self.s1 = self.create_summary() 49 if summary1 is None: 50 res = summary.get_diff(self.s0, self.s1) 51 else: 52 res = summary.get_diff(summary1, self.s1) 53 self.s0 = self.s1 54 elif summary1 is not None: 55 res = summary.get_diff(summary1, summary2) 56 else: 57 raise ValueError('You cannot provide summary2 without summary1.') 58 return summary._sweep(res) 59 60 def print_diff(self, summary1 = None, summary2 = None): 61 summary.print_(self.diff(summary1=summary1, summary2=summary2)) 62 63 def format_diff(self, summary1 = None, summary2 = None): 64 return summary.format_(self.diff(summary1=summary1, summary2=summary2)) 65 66 def store_summary(self, key): 67 self.summaries[key] = self.create_summary() 68 69 70 class ObjectTracker(object): 71 72 def __init__(self): 73 self.o0 = self._get_objects(ignore=(inspect.currentframe(),)) 74 75 def _get_objects(self, ignore = ()): 76 77 def remove_ignore(objects, ignore = ()): 78 res = [] 79 for o in objects: 80 if not compat.object_in_list(o, ignore): 81 res.append(o) 82 83 return res 84 85 tmp = gc.get_objects() 86 ignore += (inspect.currentframe(), 87 self, 88 ignore, 89 remove_ignore) 90 if hasattr(self, 'o0'): 91 ignore += (self.o0,) 92 if hasattr(self, 'o1'): 93 ignore += (self.o1,) 94 tmp = remove_ignore(tmp, ignore) 95 res = [] 96 for o in tmp: 97 refs = muppy.get_referents(o) 98 for ref in refs: 99 if not muppy._is_containerobject(ref): 100 res.append(ref) 101 102 res.extend(tmp) 103 res = muppy._remove_duplicates(res) 104 if ignore is not None: 105 res = remove_ignore(res, ignore) 106 del ignore 107 return res 108 109 def get_diff(self, ignore = ()): 110 self.o1 = self._get_objects(ignore + (inspect.currentframe(),)) 111 diff = muppy.get_diff(self.o0, self.o1) 112 self.o0 = self.o1 113 return diff 114 115 def print_diff(self, ignore = ()): 116 for line in self.format_diff(ignore + (inspect.currentframe(),)): 117 print(line) 118 119 def format_diff(self, ignore = ()): 120 lines = [] 121 diff = self.get_diff(ignore + (inspect.currentframe(),)) 122 lines.append('Added objects:') 123 for line in summary.format_(summary.summarize(diff['+'])): 124 lines.append(line) 125 126 lines.append('Removed objects:') 127 for line in summary.format_(summary.summarize(diff['-'])): 128 lines.append(line) 129 130 return lines