1 | n/a | from collections import Sequence, Iterable |
---|
2 | n/a | from functools import total_ordering |
---|
3 | n/a | import fnmatch |
---|
4 | n/a | import linecache |
---|
5 | n/a | import os.path |
---|
6 | n/a | import pickle |
---|
7 | n/a | |
---|
8 | n/a | # Import types and functions implemented in C |
---|
9 | n/a | from _tracemalloc import * |
---|
10 | n/a | from _tracemalloc import _get_object_traceback, _get_traces |
---|
11 | n/a | |
---|
12 | n/a | |
---|
13 | n/a | def _format_size(size, sign): |
---|
14 | n/a | for unit in ('B', 'KiB', 'MiB', 'GiB', 'TiB'): |
---|
15 | n/a | if abs(size) < 100 and unit != 'B': |
---|
16 | n/a | # 3 digits (xx.x UNIT) |
---|
17 | n/a | if sign: |
---|
18 | n/a | return "%+.1f %s" % (size, unit) |
---|
19 | n/a | else: |
---|
20 | n/a | return "%.1f %s" % (size, unit) |
---|
21 | n/a | if abs(size) < 10 * 1024 or unit == 'TiB': |
---|
22 | n/a | # 4 or 5 digits (xxxx UNIT) |
---|
23 | n/a | if sign: |
---|
24 | n/a | return "%+.0f %s" % (size, unit) |
---|
25 | n/a | else: |
---|
26 | n/a | return "%.0f %s" % (size, unit) |
---|
27 | n/a | size /= 1024 |
---|
28 | n/a | |
---|
29 | n/a | |
---|
30 | n/a | class Statistic: |
---|
31 | n/a | """ |
---|
32 | n/a | Statistic difference on memory allocations between two Snapshot instance. |
---|
33 | n/a | """ |
---|
34 | n/a | |
---|
35 | n/a | __slots__ = ('traceback', 'size', 'count') |
---|
36 | n/a | |
---|
37 | n/a | def __init__(self, traceback, size, count): |
---|
38 | n/a | self.traceback = traceback |
---|
39 | n/a | self.size = size |
---|
40 | n/a | self.count = count |
---|
41 | n/a | |
---|
42 | n/a | def __hash__(self): |
---|
43 | n/a | return hash((self.traceback, self.size, self.count)) |
---|
44 | n/a | |
---|
45 | n/a | def __eq__(self, other): |
---|
46 | n/a | return (self.traceback == other.traceback |
---|
47 | n/a | and self.size == other.size |
---|
48 | n/a | and self.count == other.count) |
---|
49 | n/a | |
---|
50 | n/a | def __str__(self): |
---|
51 | n/a | text = ("%s: size=%s, count=%i" |
---|
52 | n/a | % (self.traceback, |
---|
53 | n/a | _format_size(self.size, False), |
---|
54 | n/a | self.count)) |
---|
55 | n/a | if self.count: |
---|
56 | n/a | average = self.size / self.count |
---|
57 | n/a | text += ", average=%s" % _format_size(average, False) |
---|
58 | n/a | return text |
---|
59 | n/a | |
---|
60 | n/a | def __repr__(self): |
---|
61 | n/a | return ('<Statistic traceback=%r size=%i count=%i>' |
---|
62 | n/a | % (self.traceback, self.size, self.count)) |
---|
63 | n/a | |
---|
64 | n/a | def _sort_key(self): |
---|
65 | n/a | return (self.size, self.count, self.traceback) |
---|
66 | n/a | |
---|
67 | n/a | |
---|
68 | n/a | class StatisticDiff: |
---|
69 | n/a | """ |
---|
70 | n/a | Statistic difference on memory allocations between an old and a new |
---|
71 | n/a | Snapshot instance. |
---|
72 | n/a | """ |
---|
73 | n/a | __slots__ = ('traceback', 'size', 'size_diff', 'count', 'count_diff') |
---|
74 | n/a | |
---|
75 | n/a | def __init__(self, traceback, size, size_diff, count, count_diff): |
---|
76 | n/a | self.traceback = traceback |
---|
77 | n/a | self.size = size |
---|
78 | n/a | self.size_diff = size_diff |
---|
79 | n/a | self.count = count |
---|
80 | n/a | self.count_diff = count_diff |
---|
81 | n/a | |
---|
82 | n/a | def __hash__(self): |
---|
83 | n/a | return hash((self.traceback, self.size, self.size_diff, |
---|
84 | n/a | self.count, self.count_diff)) |
---|
85 | n/a | |
---|
86 | n/a | def __eq__(self, other): |
---|
87 | n/a | return (self.traceback == other.traceback |
---|
88 | n/a | and self.size == other.size |
---|
89 | n/a | and self.size_diff == other.size_diff |
---|
90 | n/a | and self.count == other.count |
---|
91 | n/a | and self.count_diff == other.count_diff) |
---|
92 | n/a | |
---|
93 | n/a | def __str__(self): |
---|
94 | n/a | text = ("%s: size=%s (%s), count=%i (%+i)" |
---|
95 | n/a | % (self.traceback, |
---|
96 | n/a | _format_size(self.size, False), |
---|
97 | n/a | _format_size(self.size_diff, True), |
---|
98 | n/a | self.count, |
---|
99 | n/a | self.count_diff)) |
---|
100 | n/a | if self.count: |
---|
101 | n/a | average = self.size / self.count |
---|
102 | n/a | text += ", average=%s" % _format_size(average, False) |
---|
103 | n/a | return text |
---|
104 | n/a | |
---|
105 | n/a | def __repr__(self): |
---|
106 | n/a | return ('<StatisticDiff traceback=%r size=%i (%+i) count=%i (%+i)>' |
---|
107 | n/a | % (self.traceback, self.size, self.size_diff, |
---|
108 | n/a | self.count, self.count_diff)) |
---|
109 | n/a | |
---|
110 | n/a | def _sort_key(self): |
---|
111 | n/a | return (abs(self.size_diff), self.size, |
---|
112 | n/a | abs(self.count_diff), self.count, |
---|
113 | n/a | self.traceback) |
---|
114 | n/a | |
---|
115 | n/a | |
---|
116 | n/a | def _compare_grouped_stats(old_group, new_group): |
---|
117 | n/a | statistics = [] |
---|
118 | n/a | for traceback, stat in new_group.items(): |
---|
119 | n/a | previous = old_group.pop(traceback, None) |
---|
120 | n/a | if previous is not None: |
---|
121 | n/a | stat = StatisticDiff(traceback, |
---|
122 | n/a | stat.size, stat.size - previous.size, |
---|
123 | n/a | stat.count, stat.count - previous.count) |
---|
124 | n/a | else: |
---|
125 | n/a | stat = StatisticDiff(traceback, |
---|
126 | n/a | stat.size, stat.size, |
---|
127 | n/a | stat.count, stat.count) |
---|
128 | n/a | statistics.append(stat) |
---|
129 | n/a | |
---|
130 | n/a | for traceback, stat in old_group.items(): |
---|
131 | n/a | stat = StatisticDiff(traceback, 0, -stat.size, 0, -stat.count) |
---|
132 | n/a | statistics.append(stat) |
---|
133 | n/a | return statistics |
---|
134 | n/a | |
---|
135 | n/a | |
---|
136 | n/a | @total_ordering |
---|
137 | n/a | class Frame: |
---|
138 | n/a | """ |
---|
139 | n/a | Frame of a traceback. |
---|
140 | n/a | """ |
---|
141 | n/a | __slots__ = ("_frame",) |
---|
142 | n/a | |
---|
143 | n/a | def __init__(self, frame): |
---|
144 | n/a | # frame is a tuple: (filename: str, lineno: int) |
---|
145 | n/a | self._frame = frame |
---|
146 | n/a | |
---|
147 | n/a | @property |
---|
148 | n/a | def filename(self): |
---|
149 | n/a | return self._frame[0] |
---|
150 | n/a | |
---|
151 | n/a | @property |
---|
152 | n/a | def lineno(self): |
---|
153 | n/a | return self._frame[1] |
---|
154 | n/a | |
---|
155 | n/a | def __eq__(self, other): |
---|
156 | n/a | return (self._frame == other._frame) |
---|
157 | n/a | |
---|
158 | n/a | def __lt__(self, other): |
---|
159 | n/a | return (self._frame < other._frame) |
---|
160 | n/a | |
---|
161 | n/a | def __hash__(self): |
---|
162 | n/a | return hash(self._frame) |
---|
163 | n/a | |
---|
164 | n/a | def __str__(self): |
---|
165 | n/a | return "%s:%s" % (self.filename, self.lineno) |
---|
166 | n/a | |
---|
167 | n/a | def __repr__(self): |
---|
168 | n/a | return "<Frame filename=%r lineno=%r>" % (self.filename, self.lineno) |
---|
169 | n/a | |
---|
170 | n/a | |
---|
171 | n/a | @total_ordering |
---|
172 | n/a | class Traceback(Sequence): |
---|
173 | n/a | """ |
---|
174 | n/a | Sequence of Frame instances sorted from the most recent frame |
---|
175 | n/a | to the oldest frame. |
---|
176 | n/a | """ |
---|
177 | n/a | __slots__ = ("_frames",) |
---|
178 | n/a | |
---|
179 | n/a | def __init__(self, frames): |
---|
180 | n/a | Sequence.__init__(self) |
---|
181 | n/a | # frames is a tuple of frame tuples: see Frame constructor for the |
---|
182 | n/a | # format of a frame tuple |
---|
183 | n/a | self._frames = frames |
---|
184 | n/a | |
---|
185 | n/a | def __len__(self): |
---|
186 | n/a | return len(self._frames) |
---|
187 | n/a | |
---|
188 | n/a | def __getitem__(self, index): |
---|
189 | n/a | if isinstance(index, slice): |
---|
190 | n/a | return tuple(Frame(trace) for trace in self._frames[index]) |
---|
191 | n/a | else: |
---|
192 | n/a | return Frame(self._frames[index]) |
---|
193 | n/a | |
---|
194 | n/a | def __contains__(self, frame): |
---|
195 | n/a | return frame._frame in self._frames |
---|
196 | n/a | |
---|
197 | n/a | def __hash__(self): |
---|
198 | n/a | return hash(self._frames) |
---|
199 | n/a | |
---|
200 | n/a | def __eq__(self, other): |
---|
201 | n/a | return (self._frames == other._frames) |
---|
202 | n/a | |
---|
203 | n/a | def __lt__(self, other): |
---|
204 | n/a | return (self._frames < other._frames) |
---|
205 | n/a | |
---|
206 | n/a | def __str__(self): |
---|
207 | n/a | return str(self[0]) |
---|
208 | n/a | |
---|
209 | n/a | def __repr__(self): |
---|
210 | n/a | return "<Traceback %r>" % (tuple(self),) |
---|
211 | n/a | |
---|
212 | n/a | def format(self, limit=None): |
---|
213 | n/a | lines = [] |
---|
214 | n/a | if limit is not None and limit < 0: |
---|
215 | n/a | return lines |
---|
216 | n/a | for frame in self[:limit]: |
---|
217 | n/a | lines.append(' File "%s", line %s' |
---|
218 | n/a | % (frame.filename, frame.lineno)) |
---|
219 | n/a | line = linecache.getline(frame.filename, frame.lineno).strip() |
---|
220 | n/a | if line: |
---|
221 | n/a | lines.append(' %s' % line) |
---|
222 | n/a | return lines |
---|
223 | n/a | |
---|
224 | n/a | |
---|
225 | n/a | def get_object_traceback(obj): |
---|
226 | n/a | """ |
---|
227 | n/a | Get the traceback where the Python object *obj* was allocated. |
---|
228 | n/a | Return a Traceback instance. |
---|
229 | n/a | |
---|
230 | n/a | Return None if the tracemalloc module is not tracing memory allocations or |
---|
231 | n/a | did not trace the allocation of the object. |
---|
232 | n/a | """ |
---|
233 | n/a | frames = _get_object_traceback(obj) |
---|
234 | n/a | if frames is not None: |
---|
235 | n/a | return Traceback(frames) |
---|
236 | n/a | else: |
---|
237 | n/a | return None |
---|
238 | n/a | |
---|
239 | n/a | |
---|
240 | n/a | class Trace: |
---|
241 | n/a | """ |
---|
242 | n/a | Trace of a memory block. |
---|
243 | n/a | """ |
---|
244 | n/a | __slots__ = ("_trace",) |
---|
245 | n/a | |
---|
246 | n/a | def __init__(self, trace): |
---|
247 | n/a | # trace is a tuple: (domain: int, size: int, traceback: tuple). |
---|
248 | n/a | # See Traceback constructor for the format of the traceback tuple. |
---|
249 | n/a | self._trace = trace |
---|
250 | n/a | |
---|
251 | n/a | @property |
---|
252 | n/a | def domain(self): |
---|
253 | n/a | return self._trace[0] |
---|
254 | n/a | |
---|
255 | n/a | @property |
---|
256 | n/a | def size(self): |
---|
257 | n/a | return self._trace[1] |
---|
258 | n/a | |
---|
259 | n/a | @property |
---|
260 | n/a | def traceback(self): |
---|
261 | n/a | return Traceback(self._trace[2]) |
---|
262 | n/a | |
---|
263 | n/a | def __eq__(self, other): |
---|
264 | n/a | return (self._trace == other._trace) |
---|
265 | n/a | |
---|
266 | n/a | def __hash__(self): |
---|
267 | n/a | return hash(self._trace) |
---|
268 | n/a | |
---|
269 | n/a | def __str__(self): |
---|
270 | n/a | return "%s: %s" % (self.traceback, _format_size(self.size, False)) |
---|
271 | n/a | |
---|
272 | n/a | def __repr__(self): |
---|
273 | n/a | return ("<Trace domain=%s size=%s, traceback=%r>" |
---|
274 | n/a | % (self.domain, _format_size(self.size, False), self.traceback)) |
---|
275 | n/a | |
---|
276 | n/a | |
---|
277 | n/a | class _Traces(Sequence): |
---|
278 | n/a | def __init__(self, traces): |
---|
279 | n/a | Sequence.__init__(self) |
---|
280 | n/a | # traces is a tuple of trace tuples: see Trace constructor |
---|
281 | n/a | self._traces = traces |
---|
282 | n/a | |
---|
283 | n/a | def __len__(self): |
---|
284 | n/a | return len(self._traces) |
---|
285 | n/a | |
---|
286 | n/a | def __getitem__(self, index): |
---|
287 | n/a | if isinstance(index, slice): |
---|
288 | n/a | return tuple(Trace(trace) for trace in self._traces[index]) |
---|
289 | n/a | else: |
---|
290 | n/a | return Trace(self._traces[index]) |
---|
291 | n/a | |
---|
292 | n/a | def __contains__(self, trace): |
---|
293 | n/a | return trace._trace in self._traces |
---|
294 | n/a | |
---|
295 | n/a | def __eq__(self, other): |
---|
296 | n/a | return (self._traces == other._traces) |
---|
297 | n/a | |
---|
298 | n/a | def __repr__(self): |
---|
299 | n/a | return "<Traces len=%s>" % len(self) |
---|
300 | n/a | |
---|
301 | n/a | |
---|
302 | n/a | def _normalize_filename(filename): |
---|
303 | n/a | filename = os.path.normcase(filename) |
---|
304 | n/a | if filename.endswith('.pyc'): |
---|
305 | n/a | filename = filename[:-1] |
---|
306 | n/a | return filename |
---|
307 | n/a | |
---|
308 | n/a | |
---|
309 | n/a | class BaseFilter: |
---|
310 | n/a | def __init__(self, inclusive): |
---|
311 | n/a | self.inclusive = inclusive |
---|
312 | n/a | |
---|
313 | n/a | def _match(self, trace): |
---|
314 | n/a | raise NotImplementedError |
---|
315 | n/a | |
---|
316 | n/a | |
---|
317 | n/a | class Filter(BaseFilter): |
---|
318 | n/a | def __init__(self, inclusive, filename_pattern, |
---|
319 | n/a | lineno=None, all_frames=False, domain=None): |
---|
320 | n/a | super().__init__(inclusive) |
---|
321 | n/a | self.inclusive = inclusive |
---|
322 | n/a | self._filename_pattern = _normalize_filename(filename_pattern) |
---|
323 | n/a | self.lineno = lineno |
---|
324 | n/a | self.all_frames = all_frames |
---|
325 | n/a | self.domain = domain |
---|
326 | n/a | |
---|
327 | n/a | @property |
---|
328 | n/a | def filename_pattern(self): |
---|
329 | n/a | return self._filename_pattern |
---|
330 | n/a | |
---|
331 | n/a | def _match_frame_impl(self, filename, lineno): |
---|
332 | n/a | filename = _normalize_filename(filename) |
---|
333 | n/a | if not fnmatch.fnmatch(filename, self._filename_pattern): |
---|
334 | n/a | return False |
---|
335 | n/a | if self.lineno is None: |
---|
336 | n/a | return True |
---|
337 | n/a | else: |
---|
338 | n/a | return (lineno == self.lineno) |
---|
339 | n/a | |
---|
340 | n/a | def _match_frame(self, filename, lineno): |
---|
341 | n/a | return self._match_frame_impl(filename, lineno) ^ (not self.inclusive) |
---|
342 | n/a | |
---|
343 | n/a | def _match_traceback(self, traceback): |
---|
344 | n/a | if self.all_frames: |
---|
345 | n/a | if any(self._match_frame_impl(filename, lineno) |
---|
346 | n/a | for filename, lineno in traceback): |
---|
347 | n/a | return self.inclusive |
---|
348 | n/a | else: |
---|
349 | n/a | return (not self.inclusive) |
---|
350 | n/a | else: |
---|
351 | n/a | filename, lineno = traceback[0] |
---|
352 | n/a | return self._match_frame(filename, lineno) |
---|
353 | n/a | |
---|
354 | n/a | def _match(self, trace): |
---|
355 | n/a | domain, size, traceback = trace |
---|
356 | n/a | res = self._match_traceback(traceback) |
---|
357 | n/a | if self.domain is not None: |
---|
358 | n/a | if self.inclusive: |
---|
359 | n/a | return res and (domain == self.domain) |
---|
360 | n/a | else: |
---|
361 | n/a | return res or (domain != self.domain) |
---|
362 | n/a | return res |
---|
363 | n/a | |
---|
364 | n/a | |
---|
365 | n/a | class DomainFilter(BaseFilter): |
---|
366 | n/a | def __init__(self, inclusive, domain): |
---|
367 | n/a | super().__init__(inclusive) |
---|
368 | n/a | self._domain = domain |
---|
369 | n/a | |
---|
370 | n/a | @property |
---|
371 | n/a | def domain(self): |
---|
372 | n/a | return self._domain |
---|
373 | n/a | |
---|
374 | n/a | def _match(self, trace): |
---|
375 | n/a | domain, size, traceback = trace |
---|
376 | n/a | return (domain == self.domain) ^ (not self.inclusive) |
---|
377 | n/a | |
---|
378 | n/a | |
---|
379 | n/a | class Snapshot: |
---|
380 | n/a | """ |
---|
381 | n/a | Snapshot of traces of memory blocks allocated by Python. |
---|
382 | n/a | """ |
---|
383 | n/a | |
---|
384 | n/a | def __init__(self, traces, traceback_limit): |
---|
385 | n/a | # traces is a tuple of trace tuples: see _Traces constructor for |
---|
386 | n/a | # the exact format |
---|
387 | n/a | self.traces = _Traces(traces) |
---|
388 | n/a | self.traceback_limit = traceback_limit |
---|
389 | n/a | |
---|
390 | n/a | def dump(self, filename): |
---|
391 | n/a | """ |
---|
392 | n/a | Write the snapshot into a file. |
---|
393 | n/a | """ |
---|
394 | n/a | with open(filename, "wb") as fp: |
---|
395 | n/a | pickle.dump(self, fp, pickle.HIGHEST_PROTOCOL) |
---|
396 | n/a | |
---|
397 | n/a | @staticmethod |
---|
398 | n/a | def load(filename): |
---|
399 | n/a | """ |
---|
400 | n/a | Load a snapshot from a file. |
---|
401 | n/a | """ |
---|
402 | n/a | with open(filename, "rb") as fp: |
---|
403 | n/a | return pickle.load(fp) |
---|
404 | n/a | |
---|
405 | n/a | def _filter_trace(self, include_filters, exclude_filters, trace): |
---|
406 | n/a | if include_filters: |
---|
407 | n/a | if not any(trace_filter._match(trace) |
---|
408 | n/a | for trace_filter in include_filters): |
---|
409 | n/a | return False |
---|
410 | n/a | if exclude_filters: |
---|
411 | n/a | if any(not trace_filter._match(trace) |
---|
412 | n/a | for trace_filter in exclude_filters): |
---|
413 | n/a | return False |
---|
414 | n/a | return True |
---|
415 | n/a | |
---|
416 | n/a | def filter_traces(self, filters): |
---|
417 | n/a | """ |
---|
418 | n/a | Create a new Snapshot instance with a filtered traces sequence, filters |
---|
419 | n/a | is a list of Filter or DomainFilter instances. If filters is an empty |
---|
420 | n/a | list, return a new Snapshot instance with a copy of the traces. |
---|
421 | n/a | """ |
---|
422 | n/a | if not isinstance(filters, Iterable): |
---|
423 | n/a | raise TypeError("filters must be a list of filters, not %s" |
---|
424 | n/a | % type(filters).__name__) |
---|
425 | n/a | if filters: |
---|
426 | n/a | include_filters = [] |
---|
427 | n/a | exclude_filters = [] |
---|
428 | n/a | for trace_filter in filters: |
---|
429 | n/a | if trace_filter.inclusive: |
---|
430 | n/a | include_filters.append(trace_filter) |
---|
431 | n/a | else: |
---|
432 | n/a | exclude_filters.append(trace_filter) |
---|
433 | n/a | new_traces = [trace for trace in self.traces._traces |
---|
434 | n/a | if self._filter_trace(include_filters, |
---|
435 | n/a | exclude_filters, |
---|
436 | n/a | trace)] |
---|
437 | n/a | else: |
---|
438 | n/a | new_traces = self.traces._traces.copy() |
---|
439 | n/a | return Snapshot(new_traces, self.traceback_limit) |
---|
440 | n/a | |
---|
441 | n/a | def _group_by(self, key_type, cumulative): |
---|
442 | n/a | if key_type not in ('traceback', 'filename', 'lineno'): |
---|
443 | n/a | raise ValueError("unknown key_type: %r" % (key_type,)) |
---|
444 | n/a | if cumulative and key_type not in ('lineno', 'filename'): |
---|
445 | n/a | raise ValueError("cumulative mode cannot by used " |
---|
446 | n/a | "with key type %r" % key_type) |
---|
447 | n/a | |
---|
448 | n/a | stats = {} |
---|
449 | n/a | tracebacks = {} |
---|
450 | n/a | if not cumulative: |
---|
451 | n/a | for trace in self.traces._traces: |
---|
452 | n/a | domain, size, trace_traceback = trace |
---|
453 | n/a | try: |
---|
454 | n/a | traceback = tracebacks[trace_traceback] |
---|
455 | n/a | except KeyError: |
---|
456 | n/a | if key_type == 'traceback': |
---|
457 | n/a | frames = trace_traceback |
---|
458 | n/a | elif key_type == 'lineno': |
---|
459 | n/a | frames = trace_traceback[:1] |
---|
460 | n/a | else: # key_type == 'filename': |
---|
461 | n/a | frames = ((trace_traceback[0][0], 0),) |
---|
462 | n/a | traceback = Traceback(frames) |
---|
463 | n/a | tracebacks[trace_traceback] = traceback |
---|
464 | n/a | try: |
---|
465 | n/a | stat = stats[traceback] |
---|
466 | n/a | stat.size += size |
---|
467 | n/a | stat.count += 1 |
---|
468 | n/a | except KeyError: |
---|
469 | n/a | stats[traceback] = Statistic(traceback, size, 1) |
---|
470 | n/a | else: |
---|
471 | n/a | # cumulative statistics |
---|
472 | n/a | for trace in self.traces._traces: |
---|
473 | n/a | domain, size, trace_traceback = trace |
---|
474 | n/a | for frame in trace_traceback: |
---|
475 | n/a | try: |
---|
476 | n/a | traceback = tracebacks[frame] |
---|
477 | n/a | except KeyError: |
---|
478 | n/a | if key_type == 'lineno': |
---|
479 | n/a | frames = (frame,) |
---|
480 | n/a | else: # key_type == 'filename': |
---|
481 | n/a | frames = ((frame[0], 0),) |
---|
482 | n/a | traceback = Traceback(frames) |
---|
483 | n/a | tracebacks[frame] = traceback |
---|
484 | n/a | try: |
---|
485 | n/a | stat = stats[traceback] |
---|
486 | n/a | stat.size += size |
---|
487 | n/a | stat.count += 1 |
---|
488 | n/a | except KeyError: |
---|
489 | n/a | stats[traceback] = Statistic(traceback, size, 1) |
---|
490 | n/a | return stats |
---|
491 | n/a | |
---|
492 | n/a | def statistics(self, key_type, cumulative=False): |
---|
493 | n/a | """ |
---|
494 | n/a | Group statistics by key_type. Return a sorted list of Statistic |
---|
495 | n/a | instances. |
---|
496 | n/a | """ |
---|
497 | n/a | grouped = self._group_by(key_type, cumulative) |
---|
498 | n/a | statistics = list(grouped.values()) |
---|
499 | n/a | statistics.sort(reverse=True, key=Statistic._sort_key) |
---|
500 | n/a | return statistics |
---|
501 | n/a | |
---|
502 | n/a | def compare_to(self, old_snapshot, key_type, cumulative=False): |
---|
503 | n/a | """ |
---|
504 | n/a | Compute the differences with an old snapshot old_snapshot. Get |
---|
505 | n/a | statistics as a sorted list of StatisticDiff instances, grouped by |
---|
506 | n/a | group_by. |
---|
507 | n/a | """ |
---|
508 | n/a | new_group = self._group_by(key_type, cumulative) |
---|
509 | n/a | old_group = old_snapshot._group_by(key_type, cumulative) |
---|
510 | n/a | statistics = _compare_grouped_stats(old_group, new_group) |
---|
511 | n/a | statistics.sort(reverse=True, key=StatisticDiff._sort_key) |
---|
512 | n/a | return statistics |
---|
513 | n/a | |
---|
514 | n/a | |
---|
515 | n/a | def take_snapshot(): |
---|
516 | n/a | """ |
---|
517 | n/a | Take a snapshot of traces of memory blocks allocated by Python. |
---|
518 | n/a | """ |
---|
519 | n/a | if not is_tracing(): |
---|
520 | n/a | raise RuntimeError("the tracemalloc module must be tracing memory " |
---|
521 | n/a | "allocations to take a snapshot") |
---|
522 | n/a | traces = _get_traces() |
---|
523 | n/a | traceback_limit = get_traceback_limit() |
---|
524 | n/a | return Snapshot(traces, traceback_limit) |
---|