def get_profile_type(file_path): (f, mime) = get_file(file_path) if mime == 'application/octet-stream': r = nflxprofile_pb2.Profile() r.ParseFromString(f.read()) f.close() return ('nflxprofile', r) elif is_perf_file(f): f.close() return ('perf_script', None) else: try: f.seek(0) r = json.load(f) f.close() if isinstance(r, list): if 'ph' in r[0]: return ('trace_event', r) elif 'nodes' in r: if isinstance(r['nodes'], list): return ('cpuprofile', r) raise InvalidFileError('Unknown JSON file.') except JSONDecodeError: f.close() raise InvalidFileError('Unknown file type.')
def get_file(file_path): # ensure the file is below PROFILE_DIR: if not abspath(file_path).startswith(abspath(config.PROFILE_DIR)): raise InvalidFileError("File %s is not in PROFILE_DIR" % file_path) if not validpath(file_path): raise InvalidFileError( "Invalid characters or file %s does not exist." % file_path) mime = get_file_mime(file_path) if mime in ['application/x-gzip', 'application/gzip']: return gzip.open(file_path, 'rt') elif mime == 'text/plain': return open(file_path, 'r') else: raise InvalidFileError('Unknown mime type.')
def get_profile_type(file_path): f = get_file(file_path) if is_perf_file(f): f.close() return ('perf_script', None) else: try: f.seek(0) r = json.load(f) f.close() if isinstance(r, list): if 'ph' in r[0]: return ('trace_event', r) elif 'nodes' in r: return ('cpuprofile', r) raise InvalidFileError('Unknown JSON file.') except JSONDecodeError: f.close() raise InvalidFileError('Unknown file type.')
def generate_flame_graph(filename, file_type, range_start, range_end, package_name=False): file_path = join(config.PROFILE_DIR, filename) mtime = getmtime(file_path) if file_type == 'perf': return perf_generate_flame_graph(file_path, range_start, range_end) elif file_type == 'cpuprofile': return cpuprofile_generate_flame_graph(file_path, range_start, range_end) elif file_type == 'trace_event': return trace_event_generate_flame_graph(file_path, mtime, range_start, range_end) elif file_type == 'nflxprofile': return nflxprofile_generate_flame_graph(file_path, range_start, range_end, package_name) else: raise InvalidFileError('Unknown file type.')
def generate_differential_flame_graph(filename, file_type, compare_filename, compare_type, start, end, compare_start, compare_end): file_path_1 = join(config.PROFILE_DIR, filename) mtime_1 = getmtime(file_path_1) flame_graph_1 = None if file_type == 'perf': flame_graph_1 = perf_generate_flame_graph(file_path_1, start, end) elif file_type == 'cpuprofile': flame_graph_1 = cpuprofile_generate_flame_graph( file_path_1, start, end) elif file_type == 'trace_event': flame_graph_1 = trace_event_generate_flame_graph( file_path_1, mtime_1, start, end) elif file_type == 'nflxprofile': flame_graph_1 = nflxprofile_generate_flame_graph( file_path_1, start, end) else: raise InvalidFileError('Unknown file type.') file_path_2 = join(config.PROFILE_DIR, compare_filename) mtime_2 = getmtime(file_path_2) flame_graph_2 = None if compare_type == 'perf': flame_graph_2 = perf_generate_flame_graph(file_path_2, compare_start, compare_end) elif compare_type == 'cpuprofile': flame_graph_2 = cpuprofile_generate_flame_graph( file_path_2, compare_start, compare_end) elif compare_type == 'trace_event': flame_graph_2 = trace_event_generate_flame_graph( file_path_2, mtime_2, compare_start, compare_end) elif compare_type == 'nflxprofile': flame_graph_2 = nflxprofile_generate_flame_graph( file_path_2, compare_start, compare_end) else: raise InvalidFileError('Unknown file type.') return get_differential_flame_graph(flame_graph_1, flame_graph_2)
def _read_offsets(file_path, file_type): # fetch modification timestamp and check cache mtime = getmtime(file_path) if file_path in offsets_cache: if mtime == offsets_mtimes[file_path]: # use cached heatmap return offsets_cache[file_path] if file_type == 'perf': return perf_read_offsets(file_path) elif file_type == 'cpuprofile': return cpuprofile_read_offsets(file_path) elif file_type == 'trace_event': return trace_event_read_offsets(file_path, mtime) elif file_type == 'nflxprofile': return nflxprofile_readoffsets(file_path) else: raise InvalidFileError('Unknown file type.')
def read_offsets(file_path): # fetch modification timestamp and check cache mtime = getmtime(file_path) if file_path in offsets_cache: if mtime == offsets_mtimes[file_path]: # use cached heatmap return offsets_cache[file_path] # find profile type and parse offsets (profile_type, parsed_profile) = get_profile_type(file_path) if profile_type == 'perf_script': return perf_read_offsets(file_path) elif profile_type == 'cpuprofile': return cpuprofile_read_offsets(parsed_profile) elif profile_type == 'trace_event': return trace_event_read_offsets(file_path, mtime, parsed_profile) else: raise InvalidFileError('Unknown file type.')
def generate_flame_graph(filename, range_start, range_end, profile_type=None): parsed_profile = None file_path = join(config.PROFILE_DIR, filename) mtime = getmtime(file_path) if not profile_type: file_path = join(config.PROFILE_DIR, filename) (profile_type, parsed_profile) = get_profile_type(file_path) if profile_type == 'perf_script': return perf_generate_flame_graph(filename, range_start, range_end) elif profile_type == 'cpuprofile': return cpuprofile_generate_flame_graph(filename, range_start, range_end, parsed_profile) elif profile_type == 'trace_event': return trace_event_generate_flame_graph(file_path, mtime, range_start, range_end, parsed_profile) elif profile_type == 'nflxprofile': return nflxprofile_generate_flame_graph(filename, range_start, range_end, parsed_profile) else: raise InvalidFileError('Unknown file type.')
def _read_offsets(file_path, file_type): # fetch modification timestamp and check cache mtime = getmtime(file_path) if file_path in offsets_cache: if mtime == offsets_mtimes[file_path]: # use cached heatmap return offsets_cache[file_path] offsets = None if file_type == 'perf': offsets = perf_read_offsets(file_path) elif file_type == 'cpuprofile': offsets = cpuprofile_read_offsets(file_path) elif file_type == 'trace_event': offsets = trace_event_read_offsets(file_path, mtime) elif file_type == 'nflxprofile': offsets = nflxprofile_readoffsets(file_path) else: raise InvalidFileError('Unknown file type.') if offsets is None: raise RuntimeError('Offsets parse error.') offsets_cache[file_path] = offsets offsets_mtimes[file_path] = mtime return offsets