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 test_is_enabled(self): import _vmprof tmpfile = open(self.tmpfilename, 'wb') assert _vmprof.is_enabled() is False _vmprof.enable(tmpfile.fileno(), 0.01, 0, 0, 0, 0) assert _vmprof.is_enabled() is True _vmprof.disable() assert _vmprof.is_enabled() is False
def test_import_vmprof(self): import struct, sys WORD = struct.calcsize('l') def count(s): i = 0 count = 0 i += 5 * WORD # header assert s[i] == 5 # MARKER_HEADER assert s[i + 1] == 0 # 0 assert s[i + 2] == 1 # VERSION_THREAD_ID assert s[i + 3] == 4 # len('pypy') assert s[i + 4:i + 8] == b'pypy' i += 8 while i < len(s): if s[i] == 3: break elif s[i] == 1: i += 1 _, size = struct.unpack("ll", s[i:i + 2 * WORD]) i += 2 * WORD + size * struct.calcsize("P") elif s[i] == 2: i += 1 _, size = struct.unpack("ll", s[i:i + 2 * WORD]) count += 1 i += 2 * WORD + size else: raise AssertionError(ord(s[i])) return count import _vmprof _vmprof.enable(self.tmpfileno, 0.01) _vmprof.disable() s = open(self.tmpfilename, 'rb').read() no_of_codes = count(s) assert no_of_codes > 10 d = {} def exec_(code, d): exec(code, d) exec_("""def foo(): pass """, d) _vmprof.enable(self.tmpfileno2, 0.01) exec_("""def foo2(): pass """, d) _vmprof.disable() s = open(self.tmpfilename2, 'rb').read() no_of_codes2 = count(s) assert b"py:foo:" in s assert b"py:foo2:" in s assert no_of_codes2 >= no_of_codes + 2 # some extra codes from tests
def test_import_vmprof(self): import struct, sys WORD = struct.calcsize('l') def count(s): i = 0 count = 0 i += 5 * WORD # header assert s[i ] == 5 # MARKER_HEADER assert s[i + 1] == 0 # 0 assert s[i + 2] == 1 # VERSION_THREAD_ID assert s[i + 3] == 4 # len('pypy') assert s[i + 4: i + 8] == b'pypy' i += 8 while i < len(s): if s[i] == 3: break elif s[i] == 1: i += 1 _, size = struct.unpack("ll", s[i:i + 2 * WORD]) i += 2 * WORD + size * struct.calcsize("P") elif s[i] == 2: i += 1 _, size = struct.unpack("ll", s[i:i + 2 * WORD]) count += 1 i += 2 * WORD + size else: raise AssertionError(ord(s[i])) return count import _vmprof _vmprof.enable(self.tmpfileno, 0.01) _vmprof.disable() s = open(self.tmpfilename, 'rb').read() no_of_codes = count(s) assert no_of_codes > 10 d = {} def exec_(code, d): exec(code, d) exec_("""def foo(): pass """, d) _vmprof.enable(self.tmpfileno2, 0.01) exec_("""def foo2(): pass """, d) _vmprof.disable() s = open(self.tmpfilename2, 'rb').read() no_of_codes2 = count(s) assert b"py:foo:" in s assert b"py:foo2:" in s assert no_of_codes2 >= no_of_codes + 2 # some extra codes from tests
def test_import_vmprof(self): import struct, sys WORD = struct.calcsize('l') def count(s): i = 0 count = 0 i += 5 * WORD # header assert s[i ] == '\x05' # MARKER_HEADER assert s[i + 1] == '\x00' # 0 assert s[i + 2] == '\x01' # VERSION_THREAD_ID assert s[i + 3] == chr(4) # len('pypy') assert s[i + 4: i + 8] == 'pypy' i += 8 while i < len(s): if s[i] == '\x03': break elif s[i] == '\x01': i += 1 _, size = struct.unpack("ll", s[i:i + 2 * WORD]) i += 2 * WORD + size * struct.calcsize("P") i += WORD # thread id elif s[i] == '\x02': i += 1 _, size = struct.unpack("ll", s[i:i + 2 * WORD]) count += 1 i += 2 * WORD + size else: raise AssertionError(ord(s[i])) return count import _vmprof _vmprof.enable(self.tmpfileno, 0.01) _vmprof.disable() s = open(self.tmpfilename, 'rb').read() no_of_codes = count(s) assert no_of_codes > 10 d = {} exec """def foo(): pass """ in d _vmprof.enable(self.tmpfileno2, 0.01) exec """def foo2(): pass """ in d _vmprof.disable() s = open(self.tmpfilename2, 'rb').read() no_of_codes2 = count(s) assert "py:foo:" in s assert "py:foo2:" in s assert no_of_codes2 >= no_of_codes + 2 # some extra codes from tests
def enable(fileno, period=DEFAULT_PERIOD, memory=False, lines=False, native=None, real_time=False): if not isinstance(period, float): raise ValueError("You need to pass a float as an argument") native = _is_native_enabled(native) _vmprof.enable(fileno, period, memory, lines, native, real_time)
def enable(fileno, period=DEFAULT_PERIOD, memory=False, lines=False, native=None, real_time=False): if not isinstance(period, float): raise ValueError("period must be a float, not %s" % type(period)) native = _is_native_enabled(native) _vmprof.enable(fileno, period, memory, lines, native, real_time)
def enable(fileno, period=DEFAULT_PERIOD, warn=True): if not isinstance(period, float): raise ValueError("You need to pass a float as an argument") if warn and sys.pypy_version_info[:3] <= (2, 6, 0): print ("PyPy 2.6.0 and below has a bug in vmprof where " "fork() would disable your profiling. " "Pass warn=False if you know what you're doing") raise Exception("PyPy 2.6.0 and below has a bug in vmprof where " "fork() would disable your profiling. " "Pass warn=False if you know what you're doing") _vmprof.enable(fileno, period)
def enable(fileno, period=DEFAULT_PERIOD, memory=False, lines=False, warn=True): if not isinstance(period, float): raise ValueError("You need to pass a float as an argument") if warn and sys.pypy_version_info[:3] < (4, 1, 0): raise Exception("PyPy <4.1 have various kinds of bugs, pass warn=False if you know what you're doing") if warn and memory: print("Memory profiling is currently unsupported for PyPy. Running without memory statistics.") if warn and lines: print('Line profiling is currently unsupported for PyPy. Running without lines statistics.\n') gz_fileno = _gzip_start(fileno) _vmprof.enable(gz_fileno, period)
def test_get_profile_path(self): import _vmprof tmpfile = open(self.tmpfilename, 'wb') assert _vmprof.get_profile_path() is None _vmprof.enable(tmpfile.fileno(), 0.01, 0, 0, 0, 0) path = _vmprof.get_profile_path() if path != tmpfile.name: with open(path, "rb") as fd1: assert fd1.read() == tmpfile.read() _vmprof.disable() assert _vmprof.get_profile_path() is None
def enable(fileno, period=DEFAULT_PERIOD, memory=False, warn=True): if not isinstance(period, float): raise ValueError("You need to pass a float as an argument") if warn and sys.pypy_version_info[:3] < (4, 1, 0): print( "PyPy <4.1 have various kinds of bugs, pass warn=False if you know what you're doing" ) raise Exception( "PyPy <4.1 have various kinds of bugs, pass warn=False if you know what you're doing" ) _vmprof.enable(fileno, period)
def test_import_vmprof(self): import struct, sys WORD = struct.calcsize('l') def count(s): i = 0 count = 0 i += 5 * WORD # header assert s[i] == '\x04' i += 1 # marker assert s[i] == '\x04' i += 1 # length i += len('pypy') while i < len(s): if s[i] == '\x03': break if s[i] == '\x01': xxx assert s[i] == '\x02' i += 1 _, size = struct.unpack("ll", s[i:i + 2 * WORD]) count += 1 i += 2 * WORD + size return count import _vmprof _vmprof.enable(self.tmpfileno) _vmprof.disable() s = open(self.tmpfilename).read() no_of_codes = count(s) assert no_of_codes > 10 d = {} exec """def foo(): pass """ in d _vmprof.enable(self.tmpfileno2) exec """def foo2(): pass """ in d _vmprof.disable() s = open(self.tmpfilename2).read() no_of_codes2 = count(s) assert "py:foo:" in s assert "py:foo2:" in s assert no_of_codes2 >= no_of_codes + 2 # some extra codes from tests
def enable(fileno, period_usec=-1): global _prof_fileno global _virtual_ips_so_far def pack_virtual_ips(tup): import struct l = [] for k, v in tup: l.append('\x02') l.append(struct.pack('QQ', k, len(v))) l.append(v) return "".join(l) _prof_fileno = fileno if _virtual_ips_so_far is not None: _vmprof.enable(fileno, period_usec, pack_virtual_ips(_virtual_ips_so_far)) else: _vmprof.enable(fileno, period_usec)
def enable(fileno, period=DEFAULT_PERIOD, memory=False, lines=False, native=None, real_time=False, warn=True): pypy_version_info = sys.pypy_version_info[:3] if not isinstance(period, float): raise ValueError("You need to pass a float as an argument") if warn and pypy_version_info < (4, 1, 0): raise Exception( "PyPy <4.1 have various kinds of bugs, pass warn=False if you know what you're doing" ) if warn and memory: print( "Memory profiling is currently unsupported for PyPy. Running without memory statistics." ) if warn and lines: print( 'Line profiling is currently unsupported for PyPy. Running without lines statistics.\n' ) if real_time: raise ValueError( 'real_time=True is currently not supported on PyPy.') native = _is_native_enabled(native) # if pypy_version_info > (5, 8, 0): _vmprof.enable(fileno, period, memory, lines, native, real_time) elif pypy_version_info >= (5, 8, 0): _vmprof.enable(fileno, period, memory, lines, native) else: _vmprof.enable(fileno, period)
def enable(fileno, period=0.001): if not isinstance(period, float): raise ValueError("You need to pass a float as an argument") global _prof_fileno global _virtual_ips_so_far def pack_virtual_ips(tup): import struct l = [] for k, v in tup: l.append('\x02') l.append(struct.pack('QQ', k, len(v))) l.append(v) return "".join(l) _prof_fileno = fileno if _virtual_ips_so_far is not None: _vmprof.enable(fileno, period, pack_virtual_ips(_virtual_ips_so_far)) else: _vmprof.enable(fileno, period)
def enable(fileno, period=DEFAULT_PERIOD): if not isinstance(period, float): raise ValueError("You need to pass a float as an argument") global _prof_fileno global _virtual_ips_so_far def pack_virtual_ips(tup): import struct l = [] for k, v in tup: l.append(b'\x02') l.append(struct.pack('QQ', k, len(v))) if not isinstance(v, bytes): v = v.encode('utf-8') l.append(v) return b"".join(l) _prof_fileno = fileno if _virtual_ips_so_far is not None: _vmprof.enable(fileno, period, pack_virtual_ips(_virtual_ips_so_far)) else: _vmprof.enable(fileno, period)
def enable(fileno, period=DEFAULT_PERIOD, memory=False, lines=False, native=None, real_time=False, warn=True): pypy_version_info = sys.pypy_version_info[:3] MAJOR = pypy_version_info[0] MINOR = pypy_version_info[1] PATCH = pypy_version_info[2] if not isinstance(period, float): raise ValueError("You need to pass a float as an argument") if warn and pypy_version_info < (4, 1, 0): raise Exception( "PyPy <4.1 have various kinds of bugs, pass warn=False if you know what you're doing" ) if warn and memory and pypy_version_info <= (7, 1, 0): print( "Memory profiling is currently unsupported for PyPy. Running without memory statistics." ) memory = False if warn and lines: print( 'Line profiling is currently unsupported for PyPy. Running without lines statistics.\n' ) native = _is_native_enabled(native) # if (MAJOR, MINOR, PATCH) >= (5, 9, 0): _vmprof.enable(fileno, period, memory, lines, native, real_time) return if real_time: raise ValueError('real_time=True requires PyPy >= 5.9') if MAJOR >= 5 and MINOR >= 8 and PATCH >= 0: _vmprof.enable(fileno, period, memory, lines, native) return _vmprof.enable(fileno, period)
def enable(fileno, period=0.001): if not isinstance(period, float): raise ValueError("You need to pass a float as an argument") _vmprof.enable(fileno, period)
def enable(fileno, period=DEFAULT_PERIOD, memory=False, lines=False): if not isinstance(period, float): raise ValueError("You need to pass a float as an argument") gz_fileno = _gzip_start(fileno) _vmprof.enable(gz_fileno, period, memory, lines)
def test_import_vmprof(self): tmpfile = open(self.tmpfilename, 'wb') tmpfileno = tmpfile.fileno() tmpfile2 = open(self.tmpfilename2, 'wb') tmpfileno2 = tmpfile2.fileno() import struct, sys, gc WORD = struct.calcsize('l') def count(s): i = 0 count = 0 i += 5 * WORD # header assert s[i ] == '\x05' # MARKER_HEADER assert s[i + 1] == '\x00' # 0 assert s[i + 2] == '\x06' # VERSION_TIMESTAMP assert s[i + 3] == '\x08' # PROFILE_RPYTHON assert s[i + 4] == chr(4) # len('pypy') assert s[i + 5: i + 9] == 'pypy' i += 9 while i < len(s): if s[i] == '\x03': break elif s[i] == '\x01': i += 1 _, size = struct.unpack("ll", s[i:i + 2 * WORD]) i += 2 * WORD + size * struct.calcsize("P") i += WORD # thread id elif s[i] == '\x02': i += 1 _, size = struct.unpack("ll", s[i:i + 2 * WORD]) count += 1 i += 2 * WORD + size elif s[i] == '\x06': print(s[i:i+24]) i += 1+8+8+8 elif s[i] == '\x07': i += 1 # skip string size, = struct.unpack("l", s[i:i + WORD]) i += WORD+size # skip string size, = struct.unpack("l", s[i:i + WORD]) i += WORD+size else: raise AssertionError(ord(s[i])) return count import _vmprof gc.collect() # try to make the weakref list deterministic gc.collect() # by freeing all dead code objects _vmprof.enable(tmpfileno, 0.01, 0, 0, 0, 0) _vmprof.disable() s = open(self.tmpfilename, 'rb').read() no_of_codes = count(s) assert no_of_codes > 10 d = {} exec """def foo(): pass """ in d gc.collect() gc.collect() _vmprof.enable(tmpfileno2, 0.01, 0, 0, 0, 0) exec """def foo2(): pass """ in d _vmprof.disable() s = open(self.tmpfilename2, 'rb').read() no_of_codes2 = count(s) assert "py:foo:" in s assert "py:foo2:" in s assert no_of_codes2 >= no_of_codes + 2 # some extra codes from tests
#!/usr/bin/env python """ Usage: dtrace_runner.py programe.py [args] """ import _vmprof import subprocess, os, sys dtrace_consumer = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'dtrace-consumer') p = subprocess.Popen([dtrace_consumer], stdin=subprocess.PIPE) fileno = p.stdin.fileno() sys.argv = sys.argv[1:] _vmprof.enable(fileno, 0.001) try: execfile(sys.argv[0]) finally: _vmprof.disable() p.stdin.close() p.wait()
def enable(fileno, period_usec=-1): _vmprof.enable(fileno, period_usec)
def test_import_vmprof(self): tmpfile = open(self.tmpfilename, 'wb') tmpfileno = tmpfile.fileno() tmpfile2 = open(self.tmpfilename2, 'wb') tmpfileno2 = tmpfile2.fileno() import struct, sys, gc WORD = struct.calcsize('l') def count(s): i = 0 count = 0 i += 5 * WORD # header assert s[i ] == '\x05' # MARKER_HEADER assert s[i + 1] == '\x00' # 0 assert s[i + 2] == '\x02' # VERSION_THREAD_ID assert s[i + 3] == chr(4) # len('pypy') assert s[i + 4: i + 8] == 'pypy' i += 8 while i < len(s): if s[i] == '\x03': break elif s[i] == '\x01': i += 1 _, size = struct.unpack("ll", s[i:i + 2 * WORD]) i += 2 * WORD + size * struct.calcsize("P") i += WORD # thread id elif s[i] == '\x02': i += 1 _, size = struct.unpack("ll", s[i:i + 2 * WORD]) count += 1 i += 2 * WORD + size else: raise AssertionError(ord(s[i])) return count import _vmprof gc.collect() # try to make the weakref list deterministic gc.collect() # by freeing all dead code objects _vmprof.enable(tmpfileno, 0.01) _vmprof.disable() s = open(self.tmpfilename, 'rb').read() no_of_codes = count(s) assert no_of_codes > 10 d = {} exec """def foo(): pass """ in d gc.collect() gc.collect() _vmprof.enable(tmpfileno2, 0.01) exec """def foo2(): pass """ in d _vmprof.disable() s = open(self.tmpfilename2, 'rb').read() no_of_codes2 = count(s) assert "py:foo:" in s assert "py:foo2:" in s assert no_of_codes2 >= no_of_codes + 2 # some extra codes from tests
def enable(fileno, period=DEFAULT_PERIOD, memory=False): if not isinstance(period, float): raise ValueError("You need to pass a float as an argument") _vmprof.enable(fileno, period, memory)