Debian Codegraph @

Lib/test/test_tracemalloc.py

raw
1 import contextlib
2 import os
3 import sys
4 import textwrap
5 import tracemalloc
6 import unittest
7 from unittest.mock import patch
8 from test.support.script_helper import (assert_python_ok, assert_python_failure,
9 interpreter_requires_environment)
10 from test import support
11 from test.support import force_not_colorized
12 from test.support import os_helper
13 from test.support import threading_helper
14
15 try:
16 import _testcapi
17 import _testinternalcapi
18 except ImportError:
19 _testcapi = None
20 _testinternalcapi = None
21
22
23 DEFAULT_DOMAIN = 0
24 EMPTY_STRING_SIZE = sys.getsizeof(b'')
25 INVALID_NFRAME = (-1, 2**30)
26
27
28 def get_frames(nframe, lineno_delta):
29 frames = []
30 frame = sys._getframe(1)
31 for index in range(nframe):
32 code = frame.f_code
33 lineno = frame.f_lineno + lineno_delta
34 frames.append((code.co_filename, lineno))
35 lineno_delta = 0
36 frame = frame.f_back
37 if frame is None:
38 break
39 return tuple(frames)
40
41 def allocate_bytes(size):
42 nframe = tracemalloc.get_traceback_limit()
43 bytes_len = (size - EMPTY_STRING_SIZE)
44 frames = get_frames(nframe, 1)
45 data = b'x' * bytes_len
46 return data, tracemalloc.Traceback(frames, min(len(frames), nframe))
47
48 def create_snapshots():
49 traceback_limit = 2
50
51 # _tracemalloc._get_traces() returns a list of (domain, size,
52 # traceback_frames) tuples. traceback_frames is a tuple of (filename,
53 # line_number) tuples.
54 raw_traces = [
55 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
56 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
57 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
58
59 (1, 2, (('a.py', 5), ('b.py', 4)), 3),
60
61 (2, 66, (('b.py', 1),), 1),
62
63 (3, 7, (('<unknown>', 0),), 1),
64 ]
65 snapshot = tracemalloc.Snapshot(raw_traces, traceback_limit)
66
67 raw_traces2 = [
68 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
69 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
70 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
71
72 (2, 2, (('a.py', 5), ('b.py', 4)), 3),
73 (2, 5000, (('a.py', 5), ('b.py', 4)), 3),
74
75 (4, 400, (('c.py', 578),), 1),
76 ]
77 snapshot2 = tracemalloc.Snapshot(raw_traces2, traceback_limit)
78
79 return (snapshot, snapshot2)
80
81 def frame(filename, lineno):
82 return tracemalloc._Frame((filename, lineno))
83
84 def traceback(*frames):
85 return tracemalloc.Traceback(frames)
86
87 def traceback_lineno(filename, lineno):
88 return traceback((filename, lineno))
89
90 def traceback_filename(filename):
91 return traceback_lineno(filename, 0)
92
93
94 class TestTraceback(unittest.TestCase):
95 def test_repr(self):
96 def get_repr(*args) -> str:
97 return repr(tracemalloc.Traceback(*args))
98
99 self.assertEqual(get_repr(()), "<Traceback ()>")
100 self.assertEqual(get_repr((), 0), "<Traceback () total_nframe=0>")
101
102 frames = (("f1", 1), ("f2", 2))
103 exp_repr_frames = (
104 "(<Frame filename='f2' lineno=2>,"
105 " <Frame filename='f1' lineno=1>)"
106 )
107 self.assertEqual(get_repr(frames),
108 f"<Traceback {exp_repr_frames}>")
109 self.assertEqual(get_repr(frames, 2),
110 f"<Traceback {exp_repr_frames} total_nframe=2>")
111
112
113 class TestTracemallocEnabled(unittest.TestCase):
114 def setUp(self):
115 if tracemalloc.is_tracing():
116 self.skipTest("tracemalloc must be stopped before the test")
117
118 tracemalloc.start(1)
119
120 def tearDown(self):
121 tracemalloc.stop()
122
123 def test_get_tracemalloc_memory(self):
124 data = [allocate_bytes(123) for count in range(1000)]
125 size = tracemalloc.get_tracemalloc_memory()
126 self.assertGreaterEqual(size, 0)
127
128 tracemalloc.clear_traces()
129 size2 = tracemalloc.get_tracemalloc_memory()
130 self.assertGreaterEqual(size2, 0)
131 self.assertLessEqual(size2, size)
132
133 def test_get_object_traceback(self):
134 tracemalloc.clear_traces()
135 obj_size = 12345
136 obj, obj_traceback = allocate_bytes(obj_size)
137 traceback = tracemalloc.get_object_traceback(obj)
138 self.assertEqual(traceback, obj_traceback)
139
140 def test_new_reference(self):
141 tracemalloc.clear_traces()
142 # gc.collect() indirectly calls PyList_ClearFreeList()
143 support.gc_collect()
144
145 # Create a list and "destroy it": put it in the PyListObject free list
146 obj = []
147 obj = None
148
149 # Create a list which should reuse the previously created empty list
150 obj = []
151
152 nframe = tracemalloc.get_traceback_limit()
153 frames = get_frames(nframe, -3)
154 obj_traceback = tracemalloc.Traceback(frames, min(len(frames), nframe))
155
156 traceback = tracemalloc.get_object_traceback(obj)
157 self.assertIsNotNone(traceback)
158 self.assertEqual(traceback, obj_traceback)
159
160 def test_set_traceback_limit(self):
161 obj_size = 10
162
163 tracemalloc.stop()
164 self.assertRaises(ValueError, tracemalloc.start, -1)
165
166 tracemalloc.stop()
167 tracemalloc.start(10)
168 obj2, obj2_traceback = allocate_bytes(obj_size)
169 traceback = tracemalloc.get_object_traceback(obj2)
170 self.assertEqual(len(traceback), 10)
171 self.assertEqual(traceback, obj2_traceback)
172
173 tracemalloc.stop()
174 tracemalloc.start(1)
175 obj, obj_traceback = allocate_bytes(obj_size)
176 traceback = tracemalloc.get_object_traceback(obj)
177 self.assertEqual(len(traceback), 1)
178 self.assertEqual(traceback, obj_traceback)
179
180 def find_trace(self, traces, traceback, size):
181 # filter also by size to ignore the memory allocated by
182 # _PyRefchain_Trace() if Python is built with Py_TRACE_REFS.
183 for trace in traces:
184 if trace[2] == traceback._frames and trace[1] == size:
185 return trace
186
187 self.fail("trace not found")
188
189 def test_get_traces(self):
190 tracemalloc.clear_traces()
191 obj_size = 12345
192 obj, obj_traceback = allocate_bytes(obj_size)
193
194 traces = tracemalloc._get_traces()
195 trace = self.find_trace(traces, obj_traceback, obj_size)
196
197 self.assertIsInstance(trace, tuple)
198 domain, size, traceback, length = trace
199 self.assertEqual(traceback, obj_traceback._frames)
200
201 tracemalloc.stop()
202 self.assertEqual(tracemalloc._get_traces(), [])
203
204 def test_get_traces_intern_traceback(self):
205 # dummy wrappers to get more useful and identical frames in the traceback
206 def allocate_bytes2(size):
207 return allocate_bytes(size)
208 def allocate_bytes3(size):
209 return allocate_bytes2(size)
210 def allocate_bytes4(size):
211 return allocate_bytes3(size)
212
213 # Ensure that two identical tracebacks are not duplicated
214 tracemalloc.stop()
215 tracemalloc.start(4)
216 obj1_size = 123
217 obj2_size = 125
218 obj1, obj1_traceback = allocate_bytes4(obj1_size)
219 obj2, obj2_traceback = allocate_bytes4(obj2_size)
220
221 traces = tracemalloc._get_traces()
222
223 obj1_traceback._frames = tuple(reversed(obj1_traceback._frames))
224 obj2_traceback._frames = tuple(reversed(obj2_traceback._frames))
225
226 trace1 = self.find_trace(traces, obj1_traceback, obj1_size)
227 trace2 = self.find_trace(traces, obj2_traceback, obj2_size)
228 domain1, size1, traceback1, length1 = trace1
229 domain2, size2, traceback2, length2 = trace2
230 self.assertIs(traceback2, traceback1)
231
232 def test_get_traced_memory(self):
233 # Python allocates some internals objects, so the test must tolerate
234 # a small difference between the expected size and the real usage
235 max_error = 2048
236
237 # allocate one object
238 obj_size = 1024 * 1024
239 tracemalloc.clear_traces()
240 obj, obj_traceback = allocate_bytes(obj_size)
241 size, peak_size = tracemalloc.get_traced_memory()
242 self.assertGreaterEqual(size, obj_size)
243 self.assertGreaterEqual(peak_size, size)
244
245 self.assertLessEqual(size - obj_size, max_error)
246 self.assertLessEqual(peak_size - size, max_error)
247
248 # destroy the object
249 obj = None
250 size2, peak_size2 = tracemalloc.get_traced_memory()
251 self.assertLess(size2, size)
252 self.assertGreaterEqual(size - size2, obj_size - max_error)
253 self.assertGreaterEqual(peak_size2, peak_size)
254
255 # clear_traces() must reset traced memory counters
256 tracemalloc.clear_traces()
257 self.assertEqual(tracemalloc.get_traced_memory(), (0, 0))
258
259 # allocate another object
260 obj, obj_traceback = allocate_bytes(obj_size)
261 size, peak_size = tracemalloc.get_traced_memory()
262 self.assertGreaterEqual(size, obj_size)
263
264 # stop() also resets traced memory counters
265 tracemalloc.stop()
266 self.assertEqual(tracemalloc.get_traced_memory(), (0, 0))
267
268 def test_clear_traces(self):
269 obj, obj_traceback = allocate_bytes(123)
270 traceback = tracemalloc.get_object_traceback(obj)
271 self.assertIsNotNone(traceback)
272
273 tracemalloc.clear_traces()
274 traceback2 = tracemalloc.get_object_traceback(obj)
275 self.assertIsNone(traceback2)
276
277 def test_reset_peak(self):
278 # Python allocates some internals objects, so the test must tolerate
279 # a small difference between the expected size and the real usage
280 tracemalloc.clear_traces()
281
282 # Example: allocate a large piece of memory, temporarily
283 large_sum = sum(list(range(100000)))
284 size1, peak1 = tracemalloc.get_traced_memory()
285
286 # reset_peak() resets peak to traced memory: peak2 < peak1
287 tracemalloc.reset_peak()
288 size2, peak2 = tracemalloc.get_traced_memory()
289 self.assertGreaterEqual(peak2, size2)
290 self.assertLess(peak2, peak1)
291
292 # check that peak continue to be updated if new memory is allocated:
293 # peak3 > peak2
294 obj_size = 1024 * 1024
295 obj, obj_traceback = allocate_bytes(obj_size)
296 size3, peak3 = tracemalloc.get_traced_memory()
297 self.assertGreaterEqual(peak3, size3)
298 self.assertGreater(peak3, peak2)
299 self.assertGreaterEqual(peak3 - peak2, obj_size)
300
301 def test_is_tracing(self):
302 tracemalloc.stop()
303 self.assertFalse(tracemalloc.is_tracing())
304
305 tracemalloc.start()
306 self.assertTrue(tracemalloc.is_tracing())
307
308 def test_snapshot(self):
309 obj, source = allocate_bytes(123)
310
311 # take a snapshot
312 snapshot = tracemalloc.take_snapshot()
313
314 # This can vary
315 self.assertGreater(snapshot.traces[1].traceback.total_nframe, 10)
316
317 # write on disk
318 snapshot.dump(os_helper.TESTFN)
319 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
320
321 # load from disk
322 snapshot2 = tracemalloc.Snapshot.load(os_helper.TESTFN)
323 self.assertEqual(snapshot2.traces, snapshot.traces)
324
325 # tracemalloc must be tracing memory allocations to take a snapshot
326 tracemalloc.stop()
327 with self.assertRaises(RuntimeError) as cm:
328 tracemalloc.take_snapshot()
329 self.assertEqual(str(cm.exception),
330 "the tracemalloc module must be tracing memory "
331 "allocations to take a snapshot")
332
333 def test_snapshot_save_attr(self):
334 # take a snapshot with a new attribute
335 snapshot = tracemalloc.take_snapshot()
336 snapshot.test_attr = "new"
337 snapshot.dump(os_helper.TESTFN)
338 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
339
340 # load() should recreate the attribute
341 snapshot2 = tracemalloc.Snapshot.load(os_helper.TESTFN)
342 self.assertEqual(snapshot2.test_attr, "new")
343
344 def fork_child(self):
345 if not tracemalloc.is_tracing():
346 return 2
347
348 obj_size = 12345
349 obj, obj_traceback = allocate_bytes(obj_size)
350 traceback = tracemalloc.get_object_traceback(obj)
351 if traceback is None:
352 return 3
353
354 # everything is fine
355 return 0
356
357 @support.requires_fork()
358 def test_fork(self):
359 # check that tracemalloc is still working after fork
360 pid = os.fork()
361 if not pid:
362 # child
363 exitcode = 1
364 try:
365 exitcode = self.fork_child()
366 finally:
367 os._exit(exitcode)
368 else:
369 support.wait_process(pid, exitcode=0)
370
371 def test_no_incomplete_frames(self):
372 tracemalloc.stop()
373 tracemalloc.start(8)
374
375 def f(x):
376 def g():
377 return x
378 return g
379
380 obj = f(0).__closure__[0]
381 traceback = tracemalloc.get_object_traceback(obj)
382 self.assertIn("test_tracemalloc", traceback[-1].filename)
383 self.assertNotIn("test_tracemalloc", traceback[-2].filename)
384
385
386 class TestSnapshot(unittest.TestCase):
387 maxDiff = 4000
388
389 def test_create_snapshot(self):
390 raw_traces = [(0, 5, (('a.py', 2),), 10)]
391
392 with contextlib.ExitStack() as stack:
393 stack.enter_context(patch.object(tracemalloc, 'is_tracing',
394 return_value=True))
395 stack.enter_context(patch.object(tracemalloc, 'get_traceback_limit',
396 return_value=5))
397 stack.enter_context(patch.object(tracemalloc, '_get_traces',
398 return_value=raw_traces))
399
400 snapshot = tracemalloc.take_snapshot()
401 self.assertEqual(snapshot.traceback_limit, 5)
402 self.assertEqual(len(snapshot.traces), 1)
403 trace = snapshot.traces[0]
404 self.assertEqual(trace.size, 5)
405 self.assertEqual(trace.traceback.total_nframe, 10)
406 self.assertEqual(len(trace.traceback), 1)
407 self.assertEqual(trace.traceback[0].filename, 'a.py')
408 self.assertEqual(trace.traceback[0].lineno, 2)
409
410 def test_filter_traces(self):
411 snapshot, snapshot2 = create_snapshots()
412 filter1 = tracemalloc.Filter(False, "b.py")
413 filter2 = tracemalloc.Filter(True, "a.py", 2)
414 filter3 = tracemalloc.Filter(True, "a.py", 5)
415
416 original_traces = list(snapshot.traces._traces)
417
418 # exclude b.py
419 snapshot3 = snapshot.filter_traces((filter1,))
420 self.assertEqual(snapshot3.traces._traces, [
421 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
422 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
423 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
424 (1, 2, (('a.py', 5), ('b.py', 4)), 3),
425 (3, 7, (('<unknown>', 0),), 1),
426 ])
427
428 # filter_traces() must not touch the original snapshot
429 self.assertEqual(snapshot.traces._traces, original_traces)
430
431 # only include two lines of a.py
432 snapshot4 = snapshot3.filter_traces((filter2, filter3))
433 self.assertEqual(snapshot4.traces._traces, [
434 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
435 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
436 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
437 (1, 2, (('a.py', 5), ('b.py', 4)), 3),
438 ])
439
440 # No filter: just duplicate the snapshot
441 snapshot5 = snapshot.filter_traces(())
442 self.assertIsNot(snapshot5, snapshot)
443 self.assertIsNot(snapshot5.traces, snapshot.traces)
444 self.assertEqual(snapshot5.traces, snapshot.traces)
445
446 self.assertRaises(TypeError, snapshot.filter_traces, filter1)
447
448 def test_filter_traces_domain(self):
449 snapshot, snapshot2 = create_snapshots()
450 filter1 = tracemalloc.Filter(False, "a.py", domain=1)
451 filter2 = tracemalloc.Filter(True, "a.py", domain=1)
452
453 original_traces = list(snapshot.traces._traces)
454
455 # exclude a.py of domain 1
456 snapshot3 = snapshot.filter_traces((filter1,))
457 self.assertEqual(snapshot3.traces._traces, [
458 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
459 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
460 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
461 (2, 66, (('b.py', 1),), 1),
462 (3, 7, (('<unknown>', 0),), 1),
463 ])
464
465 # include domain 1
466 snapshot3 = snapshot.filter_traces((filter1,))
467 self.assertEqual(snapshot3.traces._traces, [
468 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
469 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
470 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
471 (2, 66, (('b.py', 1),), 1),
472 (3, 7, (('<unknown>', 0),), 1),
473 ])
474
475 def test_filter_traces_domain_filter(self):
476 snapshot, snapshot2 = create_snapshots()
477 filter1 = tracemalloc.DomainFilter(False, domain=3)
478 filter2 = tracemalloc.DomainFilter(True, domain=3)
479
480 # exclude domain 2
481 snapshot3 = snapshot.filter_traces((filter1,))
482 self.assertEqual(snapshot3.traces._traces, [
483 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
484 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
485 (0, 10, (('a.py', 2), ('b.py', 4)), 3),
486 (1, 2, (('a.py', 5), ('b.py', 4)), 3),
487 (2, 66, (('b.py', 1),), 1),
488 ])
489
490 # include domain 2
491 snapshot3 = snapshot.filter_traces((filter2,))
492 self.assertEqual(snapshot3.traces._traces, [
493 (3, 7, (('<unknown>', 0),), 1),
494 ])
495
496 def test_snapshot_group_by_line(self):
497 snapshot, snapshot2 = create_snapshots()
498 tb_0 = traceback_lineno('<unknown>', 0)
499 tb_a_2 = traceback_lineno('a.py', 2)
500 tb_a_5 = traceback_lineno('a.py', 5)
501 tb_b_1 = traceback_lineno('b.py', 1)
502 tb_c_578 = traceback_lineno('c.py', 578)
503
504 # stats per file and line
505 stats1 = snapshot.statistics('lineno')
506 self.assertEqual(stats1, [
507 tracemalloc.Statistic(tb_b_1, 66, 1),
508 tracemalloc.Statistic(tb_a_2, 30, 3),
509 tracemalloc.Statistic(tb_0, 7, 1),
510 tracemalloc.Statistic(tb_a_5, 2, 1),
511 ])
512
513 # stats per file and line (2)
514 stats2 = snapshot2.statistics('lineno')
515 self.assertEqual(stats2, [
516 tracemalloc.Statistic(tb_a_5, 5002, 2),
517 tracemalloc.Statistic(tb_c_578, 400, 1),
518 tracemalloc.Statistic(tb_a_2, 30, 3),
519 ])
520
521 # stats diff per file and line
522 statistics = snapshot2.compare_to(snapshot, 'lineno')
523 self.assertEqual(statistics, [
524 tracemalloc.StatisticDiff(tb_a_5, 5002, 5000, 2, 1),
525 tracemalloc.StatisticDiff(tb_c_578, 400, 400, 1, 1),
526 tracemalloc.StatisticDiff(tb_b_1, 0, -66, 0, -1),
527 tracemalloc.StatisticDiff(tb_0, 0, -7, 0, -1),
528 tracemalloc.StatisticDiff(tb_a_2, 30, 0, 3, 0),
529 ])
530
531 def test_snapshot_group_by_file(self):
532 snapshot, snapshot2 = create_snapshots()
533 tb_0 = traceback_filename('<unknown>')
534 tb_a = traceback_filename('a.py')
535 tb_b = traceback_filename('b.py')
536 tb_c = traceback_filename('c.py')
537
538 # stats per file
539 stats1 = snapshot.statistics('filename')
540 self.assertEqual(stats1, [
541 tracemalloc.Statistic(tb_b, 66, 1),
542 tracemalloc.Statistic(tb_a, 32, 4),
543 tracemalloc.Statistic(tb_0, 7, 1),
544 ])
545
546 # stats per file (2)
547 stats2 = snapshot2.statistics('filename')
548 self.assertEqual(stats2, [
549 tracemalloc.Statistic(tb_a, 5032, 5),
550 tracemalloc.Statistic(tb_c, 400, 1),
551 ])
552
553 # stats diff per file
554 diff = snapshot2.compare_to(snapshot, 'filename')
555 self.assertEqual(diff, [
556 tracemalloc.StatisticDiff(tb_a, 5032, 5000, 5, 1),
557 tracemalloc.StatisticDiff(tb_c, 400, 400, 1, 1),
558 tracemalloc.StatisticDiff(tb_b, 0, -66, 0, -1),
559 tracemalloc.StatisticDiff(tb_0, 0, -7, 0, -1),
560 ])
561
562 def test_snapshot_group_by_traceback(self):
563 snapshot, snapshot2 = create_snapshots()
564
565 # stats per file
566 tb1 = traceback(('a.py', 2), ('b.py', 4))
567 tb2 = traceback(('a.py', 5), ('b.py', 4))
568 tb3 = traceback(('b.py', 1))
569 tb4 = traceback(('<unknown>', 0))
570 stats1 = snapshot.statistics('traceback')
571 self.assertEqual(stats1, [
572 tracemalloc.Statistic(tb3, 66, 1),
573 tracemalloc.Statistic(tb1, 30, 3),
574 tracemalloc.Statistic(tb4, 7, 1),
575 tracemalloc.Statistic(tb2, 2, 1),
576 ])
577
578 # stats per file (2)
579 tb5 = traceback(('c.py', 578))
580 stats2 = snapshot2.statistics('traceback')
581 self.assertEqual(stats2, [
582 tracemalloc.Statistic(tb2, 5002, 2),
583 tracemalloc.Statistic(tb5, 400, 1),
584 tracemalloc.Statistic(tb1, 30, 3),
585 ])
586
587 # stats diff per file
588 diff = snapshot2.compare_to(snapshot, 'traceback')
589 self.assertEqual(diff, [
590 tracemalloc.StatisticDiff(tb2, 5002, 5000, 2, 1),
591 tracemalloc.StatisticDiff(tb5, 400, 400, 1, 1),
592 tracemalloc.StatisticDiff(tb3, 0, -66, 0, -1),
593 tracemalloc.StatisticDiff(tb4, 0, -7, 0, -1),
594 tracemalloc.StatisticDiff(tb1, 30, 0, 3, 0),
595 ])
596
597 self.assertRaises(ValueError,
598 snapshot.statistics, 'traceback', cumulative=True)
599
600 def test_snapshot_group_by_cumulative(self):
601 snapshot, snapshot2 = create_snapshots()
602 tb_0 = traceback_filename('<unknown>')
603 tb_a = traceback_filename('a.py')
604 tb_b = traceback_filename('b.py')
605 tb_a_2 = traceback_lineno('a.py', 2)
606 tb_a_5 = traceback_lineno('a.py', 5)
607 tb_b_1 = traceback_lineno('b.py', 1)
608 tb_b_4 = traceback_lineno('b.py', 4)
609
610 # per file
611 stats = snapshot.statistics('filename', True)
612 self.assertEqual(stats, [
613 tracemalloc.Statistic(tb_b, 98, 5),
614 tracemalloc.Statistic(tb_a, 32, 4),
615 tracemalloc.Statistic(tb_0, 7, 1),
616 ])
617
618 # per line
619 stats = snapshot.statistics('lineno', True)
620 self.assertEqual(stats, [
621 tracemalloc.Statistic(tb_b_1, 66, 1),
622 tracemalloc.Statistic(tb_b_4, 32, 4),
623 tracemalloc.Statistic(tb_a_2, 30, 3),
624 tracemalloc.Statistic(tb_0, 7, 1),
625 tracemalloc.Statistic(tb_a_5, 2, 1),
626 ])
627
628 def test_trace_format(self):
629 snapshot, snapshot2 = create_snapshots()
630 trace = snapshot.traces[0]
631 self.assertEqual(str(trace), 'b.py:4: 10 B')
632 traceback = trace.traceback
633 self.assertEqual(str(traceback), 'b.py:4')
634 frame = traceback[0]
635 self.assertEqual(str(frame), 'b.py:4')
636
637 def test_statistic_format(self):
638 snapshot, snapshot2 = create_snapshots()
639 stats = snapshot.statistics('lineno')
640 stat = stats[0]
641 self.assertEqual(str(stat),
642 'b.py:1: size=66 B, count=1, average=66 B')
643
644 def test_statistic_diff_format(self):
645 snapshot, snapshot2 = create_snapshots()
646 stats = snapshot2.compare_to(snapshot, 'lineno')
647 stat = stats[0]
648 self.assertEqual(str(stat),
649 'a.py:5: size=5002 B (+5000 B), count=2 (+1), average=2501 B')
650
651 def test_slices(self):
652 snapshot, snapshot2 = create_snapshots()
653 self.assertEqual(snapshot.traces[:2],
654 (snapshot.traces[0], snapshot.traces[1]))
655
656 traceback = snapshot.traces[0].traceback
657 self.assertEqual(traceback[:2],
658 (traceback[0], traceback[1]))
659
660 def test_format_traceback(self):
661 snapshot, snapshot2 = create_snapshots()
662 def getline(filename, lineno):
663 return ' <%s, %s>' % (filename, lineno)
664 with unittest.mock.patch('tracemalloc.linecache.getline',
665 side_effect=getline):
666 tb = snapshot.traces[0].traceback
667 self.assertEqual(tb.format(),
668 [' File "b.py", line 4',
669 ' <b.py, 4>',
670 ' File "a.py", line 2',
671 ' <a.py, 2>'])
672
673 self.assertEqual(tb.format(limit=1),
674 [' File "a.py", line 2',
675 ' <a.py, 2>'])
676
677 self.assertEqual(tb.format(limit=-1),
678 [' File "b.py", line 4',
679 ' <b.py, 4>'])
680
681 self.assertEqual(tb.format(most_recent_first=True),
682 [' File "a.py", line 2',
683 ' <a.py, 2>',
684 ' File "b.py", line 4',
685 ' <b.py, 4>'])
686
687 self.assertEqual(tb.format(limit=1, most_recent_first=True),
688 [' File "a.py", line 2',
689 ' <a.py, 2>'])
690
691 self.assertEqual(tb.format(limit=-1, most_recent_first=True),
692 [' File "b.py", line 4',
693 ' <b.py, 4>'])
694
695
696 class TestFilters(unittest.TestCase):
697 maxDiff = 2048
698
699 def test_filter_attributes(self):
700 # test default values
701 f = tracemalloc.Filter(True, "abc")
702 self.assertEqual(f.inclusive, True)
703 self.assertEqual(f.filename_pattern, "abc")
704 self.assertIsNone(f.lineno)
705 self.assertEqual(f.all_frames, False)
706
707 # test custom values
708 f = tracemalloc.Filter(False, "test.py", 123, True)
709 self.assertEqual(f.inclusive, False)
710 self.assertEqual(f.filename_pattern, "test.py")
711 self.assertEqual(f.lineno, 123)
712 self.assertEqual(f.all_frames, True)
713
714 # parameters passed by keyword
715 f = tracemalloc.Filter(inclusive=False, filename_pattern="test.py", lineno=123, all_frames=True)
716 self.assertEqual(f.inclusive, False)
717 self.assertEqual(f.filename_pattern, "test.py")
718 self.assertEqual(f.lineno, 123)
719 self.assertEqual(f.all_frames, True)
720
721 # read-only attribute
722 self.assertRaises(AttributeError, setattr, f, "filename_pattern", "abc")
723
724 def test_filter_match(self):
725 # filter without line number
726 f = tracemalloc.Filter(True, "abc")
727 self.assertTrue(f._match_frame("abc", 0))
728 self.assertTrue(f._match_frame("abc", 5))
729 self.assertTrue(f._match_frame("abc", 10))
730 self.assertFalse(f._match_frame("12356", 0))
731 self.assertFalse(f._match_frame("12356", 5))
732 self.assertFalse(f._match_frame("12356", 10))
733
734 f = tracemalloc.Filter(False, "abc")
735 self.assertFalse(f._match_frame("abc", 0))
736 self.assertFalse(f._match_frame("abc", 5))
737 self.assertFalse(f._match_frame("abc", 10))
738 self.assertTrue(f._match_frame("12356", 0))
739 self.assertTrue(f._match_frame("12356", 5))
740 self.assertTrue(f._match_frame("12356", 10))
741
742 # filter with line number > 0
743 f = tracemalloc.Filter(True, "abc", 5)
744 self.assertFalse(f._match_frame("abc", 0))
745 self.assertTrue(f._match_frame("abc", 5))
746 self.assertFalse(f._match_frame("abc", 10))
747 self.assertFalse(f._match_frame("12356", 0))
748 self.assertFalse(f._match_frame("12356", 5))
749 self.assertFalse(f._match_frame("12356", 10))
750
751 f = tracemalloc.Filter(False, "abc", 5)
752 self.assertTrue(f._match_frame("abc", 0))
753 self.assertFalse(f._match_frame("abc", 5))
754 self.assertTrue(f._match_frame("abc", 10))
755 self.assertTrue(f._match_frame("12356", 0))
756 self.assertTrue(f._match_frame("12356", 5))
757 self.assertTrue(f._match_frame("12356", 10))
758
759 # filter with line number 0
760 f = tracemalloc.Filter(True, "abc", 0)
761 self.assertTrue(f._match_frame("abc", 0))
762 self.assertFalse(f._match_frame("abc", 5))
763 self.assertFalse(f._match_frame("abc", 10))
764 self.assertFalse(f._match_frame("12356", 0))
765 self.assertFalse(f._match_frame("12356", 5))
766 self.assertFalse(f._match_frame("12356", 10))
767
768 f = tracemalloc.Filter(False, "abc", 0)
769 self.assertFalse(f._match_frame("abc", 0))
770 self.assertTrue(f._match_frame("abc", 5))
771 self.assertTrue(f._match_frame("abc", 10))
772 self.assertTrue(f._match_frame("12356", 0))
773 self.assertTrue(f._match_frame("12356", 5))
774 self.assertTrue(f._match_frame("12356", 10))
775
776 def test_filter_match_filename(self):
777 def fnmatch(inclusive, filename, pattern):
778 f = tracemalloc.Filter(inclusive, pattern)
779 return f._match_frame(filename, 0)
780
781 self.assertTrue(fnmatch(True, "abc", "abc"))
782 self.assertFalse(fnmatch(True, "12356", "abc"))
783 self.assertFalse(fnmatch(True, "<unknown>", "abc"))
784
785 self.assertFalse(fnmatch(False, "abc", "abc"))
786 self.assertTrue(fnmatch(False, "12356", "abc"))
787 self.assertTrue(fnmatch(False, "<unknown>", "abc"))
788
789 def test_filter_match_filename_joker(self):
790 def fnmatch(filename, pattern):
791 filter = tracemalloc.Filter(True, pattern)
792 return filter._match_frame(filename, 0)
793
794 # empty string
795 self.assertFalse(fnmatch('abc', ''))
796 self.assertFalse(fnmatch('', 'abc'))
797 self.assertTrue(fnmatch('', ''))
798 self.assertTrue(fnmatch('', '*'))
799
800 # no *
801 self.assertTrue(fnmatch('abc', 'abc'))
802 self.assertFalse(fnmatch('abc', 'abcd'))
803 self.assertFalse(fnmatch('abc', 'def'))
804
805 # a*
806 self.assertTrue(fnmatch('abc', 'a*'))
807 self.assertTrue(fnmatch('abc', 'abc*'))
808 self.assertFalse(fnmatch('abc', 'b*'))
809 self.assertFalse(fnmatch('abc', 'abcd*'))
810
811 # a*b
812 self.assertTrue(fnmatch('abc', 'a*c'))
813 self.assertTrue(fnmatch('abcdcx', 'a*cx'))
814 self.assertFalse(fnmatch('abb', 'a*c'))
815 self.assertFalse(fnmatch('abcdce', 'a*cx'))
816
817 # a*b*c
818 self.assertTrue(fnmatch('abcde', 'a*c*e'))
819 self.assertTrue(fnmatch('abcbdefeg', 'a*bd*eg'))
820 self.assertFalse(fnmatch('abcdd', 'a*c*e'))
821 self.assertFalse(fnmatch('abcbdefef', 'a*bd*eg'))
822
823 # replace .pyc suffix with .py
824 self.assertTrue(fnmatch('a.pyc', 'a.py'))
825 self.assertTrue(fnmatch('a.py', 'a.pyc'))
826
827 if os.name == 'nt':
828 # case insensitive
829 self.assertTrue(fnmatch('aBC', 'ABc'))
830 self.assertTrue(fnmatch('aBcDe', 'Ab*dE'))
831
832 self.assertTrue(fnmatch('a.pyc', 'a.PY'))
833 self.assertTrue(fnmatch('a.py', 'a.PYC'))
834 else:
835 # case sensitive
836 self.assertFalse(fnmatch('aBC', 'ABc'))
837 self.assertFalse(fnmatch('aBcDe', 'Ab*dE'))
838
839 self.assertFalse(fnmatch('a.pyc', 'a.PY'))
840 self.assertFalse(fnmatch('a.py', 'a.PYC'))
841
842 if os.name == 'nt':
843 # normalize alternate separator "/" to the standard separator "\"
844 self.assertTrue(fnmatch(r'a/b', r'a\b'))
845 self.assertTrue(fnmatch(r'a\b', r'a/b'))
846 self.assertTrue(fnmatch(r'a/b\c', r'a\b/c'))
847 self.assertTrue(fnmatch(r'a/b/c', r'a\b\c'))
848 else:
849 # there is no alternate separator
850 self.assertFalse(fnmatch(r'a/b', r'a\b'))
851 self.assertFalse(fnmatch(r'a\b', r'a/b'))
852 self.assertFalse(fnmatch(r'a/b\c', r'a\b/c'))
853 self.assertFalse(fnmatch(r'a/b/c', r'a\b\c'))
854
855 # as of 3.5, .pyo is no longer munged to .py
856 self.assertFalse(fnmatch('a.pyo', 'a.py'))
857
858 def test_filter_match_trace(self):
859 t1 = (("a.py", 2), ("b.py", 3))
860 t2 = (("b.py", 4), ("b.py", 5))
861 t3 = (("c.py", 5), ('<unknown>', 0))
862 unknown = (('<unknown>', 0),)
863
864 f = tracemalloc.Filter(True, "b.py", all_frames=True)
865 self.assertTrue(f._match_traceback(t1))
866 self.assertTrue(f._match_traceback(t2))
867 self.assertFalse(f._match_traceback(t3))
868 self.assertFalse(f._match_traceback(unknown))
869
870 f = tracemalloc.Filter(True, "b.py", all_frames=False)
871 self.assertFalse(f._match_traceback(t1))
872 self.assertTrue(f._match_traceback(t2))
873 self.assertFalse(f._match_traceback(t3))
874 self.assertFalse(f._match_traceback(unknown))
875
876 f = tracemalloc.Filter(False, "b.py", all_frames=True)
877 self.assertFalse(f._match_traceback(t1))
878 self.assertFalse(f._match_traceback(t2))
879 self.assertTrue(f._match_traceback(t3))
880 self.assertTrue(f._match_traceback(unknown))
881
882 f = tracemalloc.Filter(False, "b.py", all_frames=False)
883 self.assertTrue(f._match_traceback(t1))
884 self.assertFalse(f._match_traceback(t2))
885 self.assertTrue(f._match_traceback(t3))
886 self.assertTrue(f._match_traceback(unknown))
887
888 f = tracemalloc.Filter(False, "<unknown>", all_frames=False)
889 self.assertTrue(f._match_traceback(t1))
890 self.assertTrue(f._match_traceback(t2))
891 self.assertTrue(f._match_traceback(t3))
892 self.assertFalse(f._match_traceback(unknown))
893
894 f = tracemalloc.Filter(True, "<unknown>", all_frames=True)
895 self.assertFalse(f._match_traceback(t1))
896 self.assertFalse(f._match_traceback(t2))
897 self.assertTrue(f._match_traceback(t3))
898 self.assertTrue(f._match_traceback(unknown))
899
900 f = tracemalloc.Filter(False, "<unknown>", all_frames=True)
901 self.assertTrue(f._match_traceback(t1))
902 self.assertTrue(f._match_traceback(t2))
903 self.assertFalse(f._match_traceback(t3))
904 self.assertFalse(f._match_traceback(unknown))
905
906
907 class TestCommandLine(unittest.TestCase):
908 def test_env_var_disabled_by_default(self):
909 # not tracing by default
910 code = 'import tracemalloc; print(tracemalloc.is_tracing())'
911 ok, stdout, stderr = assert_python_ok('-c', code)
912 stdout = stdout.rstrip()
913 self.assertEqual(stdout, b'False')
914
915 @unittest.skipIf(interpreter_requires_environment(),
916 'Cannot run -E tests when PYTHON env vars are required.')
917 def test_env_var_ignored_with_E(self):
918 """PYTHON* environment variables must be ignored when -E is present."""
919 code = 'import tracemalloc; print(tracemalloc.is_tracing())'
920 ok, stdout, stderr = assert_python_ok('-E', '-c', code, PYTHONTRACEMALLOC='1')
921 stdout = stdout.rstrip()
922 self.assertEqual(stdout, b'False')
923
924 def test_env_var_disabled(self):
925 # tracing at startup
926 code = 'import tracemalloc; print(tracemalloc.is_tracing())'
927 ok, stdout, stderr = assert_python_ok('-c', code, PYTHONTRACEMALLOC='0')
928 stdout = stdout.rstrip()
929 self.assertEqual(stdout, b'False')
930
931 def test_env_var_enabled_at_startup(self):
932 # tracing at startup
933 code = 'import tracemalloc; print(tracemalloc.is_tracing())'
934 ok, stdout, stderr = assert_python_ok('-c', code, PYTHONTRACEMALLOC='1')
935 stdout = stdout.rstrip()
936 self.assertEqual(stdout, b'True')
937
938 def test_env_limit(self):
939 # start and set the number of frames
940 code = 'import tracemalloc; print(tracemalloc.get_traceback_limit())'
941 ok, stdout, stderr = assert_python_ok('-c', code, PYTHONTRACEMALLOC='10')
942 stdout = stdout.rstrip()
943 self.assertEqual(stdout, b'10')
944
945 @force_not_colorized
946 def check_env_var_invalid(self, nframe):
947 with support.SuppressCrashReport():
948 ok, stdout, stderr = assert_python_failure(
949 '-c', 'pass',
950 PYTHONTRACEMALLOC=str(nframe))
951
952 if b'ValueError: the number of frames must be in range' in stderr:
953 return
954 if b'PYTHONTRACEMALLOC: invalid number of frames' in stderr:
955 return
956 self.fail(f"unexpected output: {stderr!a}")
957
958 def test_env_var_invalid(self):
959 for nframe in INVALID_NFRAME:
960 with self.subTest(nframe=nframe):
961 self.check_env_var_invalid(nframe)
962
963 def test_sys_xoptions(self):
964 for xoptions, nframe in (
965 ('tracemalloc', 1),
966 ('tracemalloc=1', 1),
967 ('tracemalloc=15', 15),
968 ):
969 with self.subTest(xoptions=xoptions, nframe=nframe):
970 code = 'import tracemalloc; print(tracemalloc.get_traceback_limit())'
971 ok, stdout, stderr = assert_python_ok('-X', xoptions, '-c', code)
972 stdout = stdout.rstrip()
973 self.assertEqual(stdout, str(nframe).encode('ascii'))
974
975 def check_sys_xoptions_invalid(self, nframe):
976 args = ('-X', 'tracemalloc=%s' % nframe, '-c', 'pass')
977 with support.SuppressCrashReport():
978 ok, stdout, stderr = assert_python_failure(*args)
979
980 if b'ValueError: the number of frames must be in range' in stderr:
981 return
982 if b'-X tracemalloc=NFRAME: invalid number of frames' in stderr:
983 return
984 self.fail(f"unexpected output: {stderr!a}")
985
986 @force_not_colorized
987 def test_sys_xoptions_invalid(self):
988 for nframe in INVALID_NFRAME:
989 with self.subTest(nframe=nframe):
990 self.check_sys_xoptions_invalid(nframe)
991
992 @unittest.skipIf(_testcapi is None, 'need _testcapi')
993 def test_pymem_alloc0(self):
994 # Issue #21639: Check that PyMem_Malloc(0) with tracemalloc enabled
995 # does not crash.
996 code = 'import _testcapi; _testcapi.test_pymem_alloc0(); 1'
997 assert_python_ok('-X', 'tracemalloc', '-c', code)
998
999
1000 @unittest.skipIf(_testcapi is None, 'need _testcapi')
1001 class TestCAPI(unittest.TestCase):
1002 maxDiff = 80 * 20
1003
1004 def setUp(self):
1005 if tracemalloc.is_tracing():
1006 self.skipTest("tracemalloc must be stopped before the test")
1007
1008 self.domain = 5
1009 self.size = 123
1010 self.obj = allocate_bytes(self.size)[0]
1011
1012 # for the type "object", id(obj) is the address of its memory block.
1013 # This type is not tracked by the garbage collector
1014 self.ptr = id(self.obj)
1015
1016 def tearDown(self):
1017 tracemalloc.stop()
1018
1019 def get_traceback(self):
1020 frames = _testinternalcapi._PyTraceMalloc_GetTraceback(self.domain, self.ptr)
1021 if frames is not None:
1022 return tracemalloc.Traceback(frames)
1023 else:
1024 return None
1025
1026 def track(self, release_gil=False, nframe=1):
1027 frames = get_frames(nframe, 1)
1028 _testcapi.tracemalloc_track(self.domain, self.ptr, self.size,
1029 release_gil)
1030 return frames
1031
1032 def untrack(self, release_gil=False):
1033 _testcapi.tracemalloc_untrack(self.domain, self.ptr, release_gil)
1034
1035 def get_traced_memory(self):
1036 # Get the traced size in the domain
1037 snapshot = tracemalloc.take_snapshot()
1038 domain_filter = tracemalloc.DomainFilter(True, self.domain)
1039 snapshot = snapshot.filter_traces([domain_filter])
1040 return sum(trace.size for trace in snapshot.traces)
1041
1042 def check_track(self, release_gil):
1043 nframe = 5
1044 tracemalloc.start(nframe)
1045
1046 size = tracemalloc.get_traced_memory()[0]
1047
1048 frames = self.track(release_gil, nframe)
1049 self.assertEqual(self.get_traceback(),
1050 tracemalloc.Traceback(frames))
1051
1052 self.assertEqual(self.get_traced_memory(), self.size)
1053
1054 def test_track(self):
1055 self.check_track(False)
1056
1057 def test_track_without_gil(self):
1058 # check that calling _PyTraceMalloc_Track() without holding the GIL
1059 # works too
1060 self.check_track(True)
1061
1062 def test_track_already_tracked(self):
1063 nframe = 5
1064 tracemalloc.start(nframe)
1065
1066 # track a first time
1067 self.track()
1068
1069 # calling _PyTraceMalloc_Track() must remove the old trace and add
1070 # a new trace with the new traceback
1071 frames = self.track(nframe=nframe)
1072 self.assertEqual(self.get_traceback(),
1073 tracemalloc.Traceback(frames))
1074
1075 def check_untrack(self, release_gil):
1076 tracemalloc.start()
1077
1078 self.track()
1079 self.assertIsNotNone(self.get_traceback())
1080 self.assertEqual(self.get_traced_memory(), self.size)
1081
1082 # untrack must remove the trace
1083 self.untrack(release_gil)
1084 self.assertIsNone(self.get_traceback())
1085 self.assertEqual(self.get_traced_memory(), 0)
1086
1087 # calling _PyTraceMalloc_Untrack() multiple times must not crash
1088 self.untrack(release_gil)
1089 self.untrack(release_gil)
1090
1091 def test_untrack(self):
1092 self.check_untrack(False)
1093
1094 def test_untrack_without_gil(self):
1095 self.check_untrack(True)
1096
1097 def test_stop_track(self):
1098 tracemalloc.start()
1099 tracemalloc.stop()
1100
1101 with self.assertRaises(RuntimeError):
1102 self.track()
1103 self.assertIsNone(self.get_traceback())
1104
1105 def test_stop_untrack(self):
1106 tracemalloc.start()
1107 self.track()
1108
1109 tracemalloc.stop()
1110 with self.assertRaises(RuntimeError):
1111 self.untrack()
1112
1113 @unittest.skipIf(_testcapi is None, 'need _testcapi')
1114 @threading_helper.requires_working_threading()
1115 # gh-128679: Test crash on a debug build (especially on FreeBSD).
1116 @unittest.skipIf(support.Py_DEBUG, 'need release build')
1117 @support.skip_if_sanitizer('gh-131566: race when setting allocator', thread=True)
1118 def test_tracemalloc_track_race(self):
1119 # gh-128679: Test fix for tracemalloc.stop() race condition
1120 _testcapi.tracemalloc_track_race()
1121
1122 def test_late_untrack(self):
1123 code = textwrap.dedent(f"""
1124 from test import support
1125 import tracemalloc
1126 import _testcapi
1127
1128 class Tracked:
1129 def __init__(self, domain, size):
1130 self.domain = domain
1131 self.ptr = id(self)
1132 self.size = size
1133 _testcapi.tracemalloc_track(self.domain, self.ptr, self.size)
1134
1135 def __del__(self, untrack=_testcapi.tracemalloc_untrack):
1136 untrack(self.domain, self.ptr, 1)
1137
1138 domain = {DEFAULT_DOMAIN}
1139 tracemalloc.start()
1140 obj = Tracked(domain, 1024 * 1024)
1141 support.late_deletion(obj)
1142 """)
1143 assert_python_ok("-c", code)
1144
1145
1146 if __name__ == "__main__":
1147 unittest.main()