def test_thread_time_cache(): tt = stack._ThreadTime() lock = nogevent.Lock() lock.acquire() t = nogevent.Thread(target=lock.acquire) t.start() main_thread_id = threading.current_thread().ident threads = [ main_thread_id, t.ident, ] cpu_time = tt(threads) assert sorted(k[0] for k in cpu_time.keys()) == sorted( [main_thread_id, t.ident]) assert all(t >= 0 for t in cpu_time.values()) cpu_time = tt(threads) assert sorted(k[0] for k in cpu_time.keys()) == sorted( [main_thread_id, t.ident]) assert all(t >= 0 for t in cpu_time.values()) if stack.FEATURES["cpu-time"]: assert set(tt._get_last_thread_time().keys()) == set( (pthread_id, _threading.get_thread_native_id(pthread_id)) for pthread_id in threads) lock.release() threads = { main_thread_id: _threading.get_thread_native_id(main_thread_id), } cpu_time = tt(threads) assert sorted(k[0] for k in cpu_time.keys()) == sorted([main_thread_id]) assert all(t >= 0 for t in cpu_time.values()) if stack.FEATURES["cpu-time"]: assert set(tt._get_last_thread_time().keys()) == set( (pthread_id, _threading.get_thread_native_id(pthread_id)) for pthread_id in threads)
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)), )
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 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) ), )