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 disable(): global _virtual_ips_so_far global _prof_fileno _vmprof.disable() f = os.fdopen(os.dup(_prof_fileno), "r") f.seek(0) _virtual_ips_so_far = read_prof(f, virtual_ips_only=True) _prof_fileno = -1
def disable(): global _virtual_ips_so_far global _prof_fileno _vmprof.disable() f = os.fdopen(os.dup(_prof_fileno)) f.seek(0) _virtual_ips_so_far = read_prof(f, virtual_ips_only=True) _prof_fileno = -1
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 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 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 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))
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
#!/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 disable(): try: _vmprof.disable() _gzip_finish() except IOError as e: raise Exception("Error while writing profile: " + str(e))
def disable(): _vmprof.disable()
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