ยปCore Development>Code coverage>Lib/tracemalloc.py

Python code coverage for Lib/tracemalloc.py

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