def produce_timeline_profile(profile_dir, resources_dir, profile_cnt, options): """Produces a timeline profile.""" timeline_path = os.path.join(resources_dir, PROFILER_COMMON_PREFIX + 'timeline') if not os.path.isfile(timeline_path): profiles = {} log_path = os.path.join(PROFILER_TMP_DIR, PROFILER_TMP_NAME) options['output'] = 'timeline:outfile=' + log_path opts = model_analyzer._build_options(options) # pylint: disable=protected-access for idx, prof in enumerate(gfile.ListDirectory(profile_dir)): prof_file = os.path.join(profile_dir, prof) if not os.path.isfile(prof_file): continue chosen_profile = os.path.join(resources_dir, PROFILER_COMMON_PREFIX + 'timeline_' + prof) profiles[prof] = chosen_profile if os.path.isfile(chosen_profile): if idx == 0: target_ts = get_timestamp(chosen_profile) continue tf.logging.info("Parse profile context %r" % prof_file) remove_tmp_files() # Parse profile context ProfilerFromFile(prof_file.encode('utf-8')) pwtf.Profile(options['view'].encode('utf-8'), opts.SerializeToString()) DeleteProfiler() if idx == 0: prof_names = get_informative_profiles(PROFILER_TMP_DIR, profile_cnt) target_ts = get_timestamp(os.path.join(PROFILER_TMP_DIR, prof_names[0])) else: prof_names = get_profiles_by_timestamp(PROFILER_TMP_DIR, target_ts, profile_cnt) tf.logging.info("Choose %r as the most informative profile context for %r" % (prof_names, prof)) gen_profile([os.path.join(PROFILER_TMP_DIR, name) for name in prof_names], chosen_profile) merge_profiles(profiles, timeline_path) return load_profile(timeline_path)
def serialize_data(self, graph, run_metadata_list): devnull = open(os.devnull, 'w') f = io.BytesIO() with stdout_redirector(f): #remove 'Parsing Inputs...' with RedirectStdStreams(stderr=devnull): #this stops a meaningless error on stderr profiler = Profiler(graph) for i, run_metadata in enumerate(run_metadata_list): profiler.add_step(i+1, run_metadata) #use these to print to stdout # profiler.profile_name_scope(ALL_OPTIONS_PRINT) all_opts = _build_options(ALL_OPTIONS) perf_opts = _build_options(PERF_OPTIONS) with stderr_redirector(f): #this stops some meaningless errors on stderr self._scope_all_stats_str = print_mdl.Profile('scope'.encode('utf-8'), all_opts.SerializeToString()) self._op_all_stats_str = print_mdl.Profile('op'.encode('utf-8'), all_opts.SerializeToString()) self._op_perf_stats_str = print_mdl.Profile('op'.encode('utf-8'), perf_opts.SerializeToString())
def produce_other_profile(profile_dir, resources_dir, profile_cnt, options): other_path = os.path.join(resources_dir, PROFILER_COMMON_PREFIX + options['view']) options['output'] = 'file:outfile=' + other_path opts = model_analyzer._build_options(options) # pylint: disable=protected-access # Create profiler from the first profile context. profile_context = get_first_profile_context(profile_dir) ProfilerFromFile(profile_context.encode('utf-8')) pwtf.Profile(options['view'].encode('utf-8'), opts.SerializeToString()) DeleteProfiler() return load_profile(other_path)
def produce_pprof_profile(profile_dir, resources_dir, profile_cnt, options): image_path = os.path.join(resources_dir, PROFILER_PPROF_IMAGE_NAME) if not os.path.isfile(image_path): tmp_path = os.path.join(resources_dir, PROFILER_COMMON_PREFIX + options['view']) if not os.path.isfile(tmp_path): options['view'] = 'code' options['output'] = 'pprof:outfile=' + tmp_path opts = model_analyzer._build_options(options) # pylint: disable=protected-access # Create profiler from the first profile context. profile_context = get_first_profile_context(profile_dir) ProfilerFromFile(profile_context.encode('utf-8')) pwtf.Profile(options['view'].encode('utf-8'), opts.SerializeToString()) DeleteProfiler() """Produces a pprof profile.""" subprocess.call([ 'pprof', '-svg', '--nodecount=100', '--sample_index=1', '-output={}'.format(image_path), tmp_path ]) return load_profile(image_path)
def handle_profile_api(): """Handles profile API requests.""" options = json.loads(flask.request.args.get('options')) # Determine view and output format. if options['view'] == 'pprof': output_format = 'pprof' options['view'] = 'code' elif options['view'] == 'graph': output_format = 'timeline' else: output_format = 'file' # Produce a profile. options['output'] = output_format + ':outfile=' + PROFILER_LOG_PATH opts = model_analyzer._build_options(options) # pylint: disable=protected-access pwtf.Profile(options['view'].encode('utf-8'), opts.SerializeToString()) if output_format == 'pprof': return produce_pprof_profile() elif output_format == 'timeline': return produce_timeline_profile() else: return load_profile(PROFILER_LOG_PATH)