def test_stop_sampling(self): if not self.plain: skip("unreliable test except on CPython without -A") import os import _vmprof tmpfile = open(self.tmpfilename, 'wb') native = 1 def f(): import sys import math j = sys.maxsize for i in range(500): j = math.sqrt(j) _vmprof.enable(tmpfile.fileno(), 0.01, 0, native, 0, 0) # get_vmprof_stack() always returns 0 here! # see vmprof_common.c and assume RPYTHON_LL2CTYPES is defined! f() fileno = _vmprof.stop_sampling() pos = os.lseek(fileno, 0, os.SEEK_CUR) f() pos2 = os.lseek(fileno, 0, os.SEEK_CUR) assert pos == pos2 _vmprof.start_sampling() f() fileno = _vmprof.stop_sampling() pos3 = os.lseek(fileno, 0, os.SEEK_CUR) assert pos3 > pos _vmprof.disable()
def disable(): try: # fish the file descriptor that is still open! fileno = _vmprof.stop_sampling() if fileno >= 0: _vmprof.flush_buffers() # TODO does fileobj leak the fd? I dont think so, but need to check fileobj = FdWrapper(fileno) l = LogReaderDumpNative(fileobj, LogReaderState()) l.read_all() _vmprof.write_all_code_objects(l.dedup) _vmprof.disable() except IOError as e: raise Exception("Error while writing profile: " + str(e))