def test_heap(): max_nframe = 32 _memalloc.start(max_nframe, 10, 1024) x = _allocate_1k() # Check that at least one sample comes from the main thread thread_found = False for (stack, nframe, thread_id), size in _memalloc.heap(): assert 0 < len(stack) <= max_nframe assert size > 0 if thread_id == nogevent.main_thread_id: thread_found = True assert isinstance(thread_id, int) if (stack[0][0] == __file__ and stack[0][1] == _ALLOC_LINE_NUMBER and stack[0][2] == "<listcomp>" and stack[1][0] == __file__ and stack[1][1] == _ALLOC_LINE_NUMBER and stack[1][2] == "_allocate_1k" and stack[2][0] == __file__ and stack[2][2] == "test_heap"): break else: pytest.fail("No trace of allocation in heap") assert thread_found, "Main thread not found" y = _pre_allocate_1k() for (stack, nframe, thread_id), size in _memalloc.heap(): assert 0 < len(stack) <= max_nframe assert size > 0 assert isinstance(thread_id, int) if (stack[0][0] == __file__ and stack[0][1] == _ALLOC_LINE_NUMBER and stack[0][2] == "<listcomp>" and stack[1][0] == __file__ and stack[1][1] == _ALLOC_LINE_NUMBER and stack[1][2] == "_allocate_1k" and stack[2][0] == __file__ and stack[2][2] == "_pre_allocate_1k"): break else: pytest.fail("No trace of allocation in heap") del x gc.collect() for (stack, nframe, thread_id), size in _memalloc.heap(): assert 0 < len(stack) <= max_nframe assert size > 0 assert isinstance(thread_id, int) if (stack[0][0] == __file__ and stack[0][1] == _ALLOC_LINE_NUMBER and stack[0][2] == "<listcomp>" and stack[1][0] == __file__ and stack[1][1] == _ALLOC_LINE_NUMBER and stack[1][2] == "_allocate_1k" and stack[2][0] == __file__ and stack[2][2] == "test_heap"): pytest.fail("Allocated memory still in heap") del y gc.collect() for (stack, nframe, thread_id), size in _memalloc.heap(): assert 0 < len(stack) <= max_nframe assert size > 0 assert isinstance(thread_id, int) if (stack[0][0] == __file__ and stack[0][1] == _ALLOC_LINE_NUMBER and stack[0][2] == "<listcomp>" and stack[1][0] == __file__ and stack[1][1] == _ALLOC_LINE_NUMBER and stack[1][2] == "_allocate_1k" and stack[2][0] == __file__ and stack[2][2] == "_pre_allocate_1k"): pytest.fail("Allocated memory still in heap") _memalloc.stop()
def test_heap_stress(): # This should run for a few seconds, and is enough to spot potential segfaults. _memalloc.start(64, 64, 1024) x = [] for _ in range(20): for _ in range(1000): x.append(object()) _memalloc.heap() del x[:100] _memalloc.stop()
def snapshot(self): thread_id_ignore_set = self._get_thread_id_ignore_set() return (tuple( MemoryHeapSampleEvent( thread_id=thread_id, thread_name=_threading.get_thread_name(thread_id), thread_native_id=_threading.get_thread_native_id(thread_id), frames=stack, nframes=nframes, size=size, sample_size=self.heap_sample_size, ) for (stack, nframes, thread_id), size in _memalloc.heap() if not self.ignore_profiler or thread_id not in thread_id_ignore_set), )
def snapshot(self): return ( tuple( MemoryHeapSampleEvent( thread_id=thread_id, thread_name=_threading.get_thread_name(thread_id), thread_native_id=_threading.get_thread_native_id( thread_id), frames=stack, nframes=nframes, size=size, sample_size=self.heap_sample_size, ) for (stack, nframes, thread_id), size in _memalloc.heap() # TODO: this should probably be implemented in _memalloc directly for speed if not self.ignore_profiler or not any( frame[0].startswith(_MODULE_TOP_DIR) for frame in stack)), )