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 parse_nodes(nodes): profile = nflxprofile_pb2.Profile() profile.nodes[0].function_name = 'fakenode' profile.nodes[0].hit_count = 0 for node in nodes: node_id = node['id'] function_name = node['callFrame']['functionName'] children = node.get('children', None) hit_count = node.get('hitCount', 0) profile.nodes[node_id].function_name = function_name profile.nodes[node_id].hit_count = hit_count profile.nodes[node_id].libtype = '' if children: for child_id in children: profile.nodes[node_id].children.append(child_id) return profile.nodes
def nflxprofile_generate_flame_graph(file_path, range_start, range_end): try: f = get_file(file_path) profile = nflxprofile_pb2.Profile() profile.ParseFromString(f.read()) except TypeError: abort(500, 'Failed to parse profile.') finally: f.close() start_time = profile.start_time if range_start is not None: adjusted_range_start = (math.floor(start_time) + range_start) if range_end is not None: adjusted_range_end = (math.floor(start_time) + range_end) return generate_flame_graph(profile.nodes, 0, profile.samples, profile.time_deltas, start_time, adjusted_range_start, adjusted_range_end, None)
def nflxprofile_generate_differential_flame_graph(file_path, range_start, range_end): try: f = get_file(file_path) profile = nflxprofile_pb2.Profile() profile.ParseFromString(f.read()) except TypeError: abort(500, 'Failed to parse profile.') finally: f.close() start_time = profile.start_time if range_start is not None: range_start = (math.floor(start_time) + range_start) if range_end is not None: range_end = (math.floor(start_time) + range_end) return generate_flame_graph([profile], [0], [None], range_start, range_end)
def nflxprofile_readoffsets(file_path): try: f = get_file(file_path) profile = nflxprofile_pb2.Profile() profile.ParseFromString(f.read()) except TypeError: abort(500, 'Failed to parse profile.') finally: f.close() offsets = [] current_time = profile.start_time for delta in profile.time_deltas: current_time += delta offsets.append(current_time) res = collections.namedtuple('offsets', ['start', 'end', 'offsets'])(profile.start_time, profile.end_time, offsets) return res