def test_read_profile(here): prof_path = os.path.join(here, 'test.prof') prof_content = open(prof_path, 'rb') period, profiles, symbols, ranges = read_prof(prof_content) assert ranges[5].name == '/lib/x86_64-linux-gnu/liblzma.so.5.0.0' assert ranges[5].start == 140682901667840 assert ranges[5].end == 140682901803008 # symbols contains all kinds of crap, reverse it sym_dict = {} for k, v in symbols: sym_dict[v] = k assert 'py:f:7:x.py' in sym_dict assert 'py:g:4:x.py' in sym_dict assert 'py:<module>:2:x.py' in sym_dict addrspace = AddressSpace([ LibraryData('virtual', 0x7000000000000000, 0x7000000000010000, True, symbols=symbols) ]) name, start_addr, is_virtual = addrspace.lookup( sym_dict['py:<module>:2:x.py']) assert name == 'py:<module>:2:x.py' assert is_virtual
def read_profile(prof_filename, lib_cache={}, extra_libs=None, virtual_only=True): prof = open(prof_filename, 'rb') period, profiles, virtual_symbols, libs = read_prof(prof) if not virtual_only: for i, lib in enumerate(libs): if lib.name in lib_cache: libs[i] = lib_cache[lib.name] else: lib.read_object_data(lib.start) lib_cache[lib.name] = lib libs.append( LibraryData('<virtual>', 0x7000000000000000, 0x7fffffffffffffff, True, symbols=virtual_symbols)) if extra_libs: libs += extra_libs addrspace = AddressSpace(libs) filtered_profiles, addr_set = addrspace.filter_addr(profiles, virtual_only) d = {} for addr in addr_set: name, _, _ = addrspace.lookup(addr) d[addr] = name return Stats(filtered_profiles, d)
def read_profile(prof_filename, lib_cache={}, extra_libs=None, virtual_only=True, include_extra_info=True): prof = open(str(prof_filename), 'rb') period, profiles, virtual_symbols, libs, interp_name = read_prof(prof) if not virtual_only or include_extra_info: for i, lib in enumerate(libs): if lib.name in lib_cache: libs[i].get_symbols_from(lib_cache[lib.name]) else: lib.read_object_data(lib.start) lib_cache[lib.name] = lib libs.append( LibraryData( '<virtual>', 0x7000000000000000, 0x7fffffffffffffff, True, symbols=virtual_symbols) ) if extra_libs: libs += extra_libs addrspace = AddressSpace(libs) filtered_profiles, addr_set, jit_frames = addrspace.filter_addr(profiles, virtual_only, include_extra_info, interp_name) d = {} for addr in addr_set: name, _, _, _ = addrspace.lookup(addr) d[addr] = name if include_extra_info: d.update(addrspace.meta_data) s = Stats(filtered_profiles, d, jit_frames, interp_name) s.addrspace = addrspace return s
def test_read_profile(here): prof_path = os.path.join(here, 'test.prof') prof_content = open(prof_path, 'rb') period, profiles, symbols, ranges, interp_name = read_prof(prof_content) assert ranges[5].name == '/lib/x86_64-linux-gnu/liblzma.so.5.0.0' assert ranges[5].start == 140682901667840 assert ranges[5].end == 140682901803008 # symbols contains all kinds of crap, reverse it sym_dict = {} for k, v in symbols: sym_dict[v] = k assert 'py:f:7:x.py' in sym_dict assert 'py:g:4:x.py' in sym_dict assert 'py:<module>:2:x.py' in sym_dict addrspace = AddressSpace([ LibraryData( 'virtual', 0x7000000000000000, 0x7000000000010000, True, symbols=symbols) ]) name, start_addr, is_virtual, _ = addrspace.lookup(sym_dict['py:<module>:2:x.py']) assert name == 'py:<module>:2:x.py' assert is_virtual
def dump_native_symbols(fileno): # native symbols cannot be resolved in the signal handler. # it would take far too long. Thus this method should be called # just after the sampling finished and before the file descriptor # is closed. # called from C with the fileno that has been used for this profile # duplicates are avoided if this function is only called once for a profile fileobj = io.open(fileno, mode='rb', closefd=False) fileobj.seek(0) _, profiles, _, _, _, _, _ = read_prof(fileobj) duplicates = set() fileobj = io.open(fileno, mode='ab', closefd=False) for profile in profiles: addrs = profile[0] for addr in addrs: if addr in duplicates: continue duplicates.add(addr) if addr & 0x1 and addr > 1: name, lineno, srcfile = _vmprof.resolve_addr(addr) if name == "" and srcfile == '-': name = "<native symbol 0x%x>" % addr str = "n:%s:%d:%s" % (name, lineno, srcfile) if PY3: str = str.encode() out = [ MARKER_NATIVE_SYMBOLS, struct.pack("l", addr), struct.pack("l", len(str)), str ] fileobj.write(b''.join(out))
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 read_profile(prof_filename): prof = open(str(prof_filename), 'rb') period, profiles, virtual_symbols, interp_name = read_prof(prof) jit_frames = {} d = dict(virtual_symbols) s = Stats(profiles, d, jit_frames, interp_name) return s
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 read_profile(prof_file): if not hasattr(prof_file, 'read'): prof_file = open(str(prof_file), 'rb') period, profiles, virtual_symbols, interp_name = read_prof(prof_file) jit_frames = {} d = dict(virtual_symbols) s = Stats(profiles, d, jit_frames, interp=interp_name) return s
def read_profile(prof_file): file_to_close = None if not hasattr(prof_file, 'read'): prof_file = file_to_close = open(str(prof_file), 'rb') period, profiles, virtual_symbols, interp_name, meta, start_time, end_time = read_prof( prof_file) if file_to_close: file_to_close.close() jit_frames = {} d = dict(virtual_symbols) s = Stats(profiles, d, jit_frames, interp=interp_name, start_time=start_time, end_time=end_time, meta=meta) return s
def read_profile(prof_filename, lib_cache={}, extra_libs=None, virtual_only=True, include_extra_info=True): prof = open(str(prof_filename), 'rb') period, profiles, virtual_symbols, libs, interp_name = read_prof(prof) if not virtual_only or include_extra_info: exe_name = libs[0].name for lib in libs: executable = lib.name == exe_name if lib.name in lib_cache: lib.get_symbols_from(lib_cache[lib.name], executable) else: lib.read_object_data(executable) lib_cache[lib.name] = lib libs.append( LibraryData('<virtual>', 0x7000000000000000, 0x7fffffffffffffff, True, symbols=virtual_symbols)) if extra_libs: libs += extra_libs addrspace = AddressSpace(libs) filtered_profiles, addr_set, jit_frames = addrspace.filter_addr( profiles, virtual_only, include_extra_info, interp_name) d = {} for addr in addr_set: name, _, _, lib = addrspace.lookup(addr) if lib is None: name = 'jit:' + name d[addr] = name if include_extra_info: d.update(addrspace.meta_data) s = Stats(filtered_profiles, d, jit_frames, interp_name) s.addrspace = addrspace return s