def test_iter_events(): max_nframe = 32 _memalloc.start(max_nframe, 10000, 512 * 1024) _allocate_1k() events, count, alloc_count = _memalloc.iter_events() _memalloc.stop() assert count >= 1000 # Watchout: if we dropped samples the test will likely fail object_count = 0 for (stack, nframe, thread_id), size in events: assert 0 < len(stack) <= max_nframe assert nframe >= len(stack) last_call = stack[0] assert size >= 1 # size depends on the object size if last_call[2] == "<listcomp>" and last_call[1] == _ALLOC_LINE_NUMBER: assert thread_id == nogevent.main_thread_id assert last_call[0] == __file__ assert stack[1][0] == __file__ assert stack[1][1] == _ALLOC_LINE_NUMBER assert stack[1][2] == "_allocate_1k" object_count += 1 assert object_count >= 1000
def test_iter_events_dropped(): max_nframe = 32 _memalloc.start(max_nframe, 100, 512 * 1024) _allocate_1k() events, count, alloc_count = _memalloc.iter_events() _memalloc.stop() assert count == 100 assert alloc_count >= 1000
def collect(self): events, count, alloc_count = _memalloc.iter_events() capture_pct = 100 * count / alloc_count thread_id_ignore_set = self._get_thread_id_ignore_set() # TODO: The event timestamp is slightly off since it's going to be the time we copy the data from the # _memalloc buffer to our Recorder. This is fine for now, but we might want to store the nanoseconds # timestamp in C and then return it via iter_events. return (tuple( MemoryAllocSampleEvent( 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, capture_pct=capture_pct, nevents=alloc_count, ) for (stack, nframes, thread_id), size in events if not self.ignore_profiler or thread_id not in thread_id_ignore_set), )
def test_iter_events_multi_thread(): max_nframe = 32 t = threading.Thread(target=_allocate_1k) _memalloc.start(max_nframe, 10000) _allocate_1k() t.start() t.join() events, count, alloc_count = _memalloc.iter_events() _memalloc.stop() assert count >= 1000 # Watchout: if we dropped samples the test will likely fail count_object = 0 count_thread = 0 for (stack, nframe, thread_id), size in events: assert 0 < len(stack) <= max_nframe assert nframe >= len(stack) last_call = stack[0] assert size >= 1 # size depends on the object size if last_call[2] == "_allocate_1k" and last_call[ 1] == _ALLOC_LINE_NUMBER: assert last_call[0] == __file__ if thread_id == _nogevent.main_thread_id: count_object += 1 assert stack[1][0] == __file__ assert stack[1][1] == 105 assert stack[1][2] == "test_iter_events_multi_thread" elif thread_id == t.ident: count_thread += 1 assert stack[1][0] == threading.__file__ assert stack[1][1] > 0 assert stack[1][2] == "run" assert count_object == 1000 assert count_thread == 1000
def collect(self): events, count, alloc_count = _memalloc.iter_events() capture_pct = 100 * count / alloc_count # TODO: The event timestamp is slightly off since it's going to be the time we copy the data from the # _memalloc buffer to our Recorder. This is fine for now, but we might want to store the nanoseconds # timestamp in C and then return it via iter_events. return ( tuple( MemoryAllocSampleEvent( 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, capture_pct=capture_pct, nevents=alloc_count, ) for (stack, nframes, thread_id), size in events # TODO: this should be implemented in _memalloc directly so we have more space for samples # not coming from the profiler if not self.ignore_profiler or not any(frame[0].startswith(_MODULE_TOP_DIR) for frame in stack) ), )
def test_iter_events_not_started(): with pytest.raises(RuntimeError, match="the memalloc module was not started"): _memalloc.iter_events()