def main(): sys.setrecursionlimit(MAX_CALLSTACK_LENGTH * 2 + 50) parser = argparse.ArgumentParser(description='report profiling data') parser.add_argument('-i', '--record_file', nargs='+', default=['perf.data'], help=""" Set profiling data file to report. Default is perf.data.""" ) parser.add_argument('-o', '--report_path', default='report.html', help=""" Set output html file. Default is report.html.""") parser.add_argument('--min_func_percent', default=0.01, type=float, help=""" Set min percentage of functions shown in the report. For example, when set to 0.01, only functions taking >= 0.01%% of total event count are collected in the report. Default is 0.01.""" ) parser.add_argument('--min_callchain_percent', default=0.01, type=float, help=""" Set min percentage of callchains shown in the report. It is used to limit nodes shown in the function flamegraph. For example, when set to 0.01, only callchains taking >= 0.01%% of the event count of the starting function are collected in the report. Default is 0.01.""" ) parser.add_argument('--add_source_code', action='store_true', help='Add source code.') parser.add_argument('--source_dirs', nargs='+', help='Source code directories.') parser.add_argument('--add_disassembly', action='store_true', help='Add disassembled code.') parser.add_argument('--binary_filter', nargs='+', help="""Annotate source code and disassembly only for selected binaries.""") parser.add_argument('--ndk_path', nargs=1, help='Find tools in the ndk path.') parser.add_argument('--no_browser', action='store_true', help="Don't open report in browser.") parser.add_argument( '--show_art_frames', action='store_true', help='Show frames of internal methods in the ART Java interpreter.') parser.add_argument('--aggregate-by-thread-name', action='store_true', help="""aggregate samples by thread name instead of thread id. This is useful for showing multiple perf.data generated for the same app.""" ) args = parser.parse_args() # 1. Process args. binary_cache_path = 'binary_cache' if not os.path.isdir(binary_cache_path): if args.add_source_code or args.add_disassembly: log_exit( """binary_cache/ doesn't exist. Can't add source code or disassembled code without collected binaries. Please run binary_cache_builder.py to collect binaries for current profiling data, or run app_profiler.py without -nb option.""") binary_cache_path = None if args.add_source_code and not args.source_dirs: log_exit('--source_dirs is needed to add source code.') build_addr_hit_map = args.add_source_code or args.add_disassembly ndk_path = None if not args.ndk_path else args.ndk_path[0] # 2. Produce record data. record_data = RecordData(binary_cache_path, ndk_path, build_addr_hit_map) for record_file in args.record_file: record_data.load_record_file(record_file, args.show_art_frames) if args.aggregate_by_thread_name: record_data.aggregate_by_thread_name() record_data.limit_percents(args.min_func_percent, args.min_callchain_percent) def filter_lib(lib_name): if not args.binary_filter: return True for binary in args.binary_filter: if binary in lib_name: return True return False if args.add_source_code: record_data.add_source_code(args.source_dirs, filter_lib) if args.add_disassembly: record_data.add_disassembly(filter_lib) # 3. Generate report html. report_generator = ReportGenerator(args.report_path) report_generator.write_script() report_generator.write_content_div() report_generator.write_record_data(record_data.gen_record_info()) report_generator.finish() if not args.no_browser: open_report_in_browser(args.report_path) log_info("Report generated at '%s'." % args.report_path)
def main(): sys.setrecursionlimit(MAX_CALLSTACK_LENGTH * 2 + 50) parser = argparse.ArgumentParser(description='report profiling data') parser.add_argument('-i', '--record_file', nargs='+', default=['perf.data'], help=""" Set profiling data file to report. Default is perf.data.""") parser.add_argument('-o', '--report_path', default='report.html', help=""" Set output html file. Default is report.html.""") parser.add_argument('--min_func_percent', default=0.01, type=float, help=""" Set min percentage of functions shown in the report. For example, when set to 0.01, only functions taking >= 0.01%% of total event count are collected in the report. Default is 0.01.""") parser.add_argument('--min_callchain_percent', default=0.01, type=float, help=""" Set min percentage of callchains shown in the report. It is used to limit nodes shown in the function flamegraph. For example, when set to 0.01, only callchains taking >= 0.01%% of the event count of the starting function are collected in the report. Default is 0.01.""") parser.add_argument('--add_source_code', action='store_true', help='Add source code.') parser.add_argument('--source_dirs', nargs='+', help='Source code directories.') parser.add_argument('--add_disassembly', action='store_true', help='Add disassembled code.') parser.add_argument('--binary_filter', nargs='+', help="""Annotate source code and disassembly only for selected binaries.""") parser.add_argument('--ndk_path', nargs=1, help='Find tools in the ndk path.') parser.add_argument('--no_browser', action='store_true', help="Don't open report in browser.") parser.add_argument('--show_art_frames', action='store_true', help='Show frames of internal methods in the ART Java interpreter.') args = parser.parse_args() # 1. Process args. binary_cache_path = 'binary_cache' if not os.path.isdir(binary_cache_path): if args.add_source_code or args.add_disassembly: log_exit("""binary_cache/ doesn't exist. Can't add source code or disassembled code without collected binaries. Please run binary_cache_builder.py to collect binaries for current profiling data, or run app_profiler.py without -nb option.""") binary_cache_path = None if args.add_source_code and not args.source_dirs: log_exit('--source_dirs is needed to add source code.') build_addr_hit_map = args.add_source_code or args.add_disassembly ndk_path = None if not args.ndk_path else args.ndk_path[0] # 2. Produce record data. record_data = RecordData(binary_cache_path, ndk_path, build_addr_hit_map) for record_file in args.record_file: record_data.load_record_file(record_file, args.show_art_frames) record_data.limit_percents(args.min_func_percent, args.min_callchain_percent) def filter_lib(lib_name): if not args.binary_filter: return True for binary in args.binary_filter: if binary in lib_name: return True return False if args.add_source_code: record_data.add_source_code(args.source_dirs, filter_lib) if args.add_disassembly: record_data.add_disassembly(filter_lib) # 3. Generate report html. report_generator = ReportGenerator(args.report_path) report_generator.write_script() report_generator.write_content_div() report_generator.write_record_data(record_data.gen_record_info()) report_generator.finish() if not args.no_browser: open_report_in_browser(args.report_path) log_info("Report generated at '%s'." % args.report_path)
def main(): parser = argparse.ArgumentParser( description='Report samples in perf.data.') parser.add_argument( '--symfs', help="""Set the path to find binaries with symbols and debug info.""") parser.add_argument('--kallsyms', help='Set the path to find kernel symbols.') parser.add_argument('--record_file', default='perf.data', help='Default is perf.data.') parser.add_argument('-t', '--capture_duration', type=int, default=10, help="""Capture duration in seconds.""") parser.add_argument('-p', '--app', help="""Profile an Android app, given the package name. Like -p com.example.android.myapp.""") parser.add_argument( '-np', '--native_program', default="surfaceflinger", help="""Profile a native program. The program should be running on the device. Like -np surfaceflinger.""") parser.add_argument( '-c', '--color', default='hot', choices=['hot', 'dso', 'legacy'], help="""Color theme: hot=percentage of samples, dso=callsite DSO name, legacy=brendan style""") parser.add_argument('-sc', '--skip_collection', default=False, help='Skip data collection', action="store_true") parser.add_argument('-nc', '--skip_recompile', action='store_true', help="""When profiling an Android app, by default we recompile java bytecode to native instructions to profile java code. It takes some time. You can skip it if the code has been compiled or you don't need to profile java code.""" ) parser.add_argument('-f', '--sample_frequency', type=int, default=6000, help='Sample frequency') parser.add_argument('-du', '--dwarf_unwinding', help='Perform unwinding using dwarf instead of fp.', default=False, action='store_true') parser.add_argument('-e', '--events', help="""Sample based on event occurences instead of frequency. Format expected is "event_counts event_name". e.g: "10000 cpu-cyles". A few examples of event_name: cpu-cycles, cache-references, cache-misses, branch-instructions, branch-misses""", default="") parser.add_argument('--disable_adb_root', action='store_true', help="""Force adb to run in non root mode.""") parser.add_argument('-o', '--report_path', default='report.html', help="Set report path.") parser.add_argument('--embedded_flamegraph', action='store_true', help=""" Generate embedded flamegraph.""") parser.add_argument('--min_callchain_percentage', default=0.01, type=float, help=""" Set min percentage of callchains shown in the report. It is used to limit nodes shown in the flamegraph. For example, when set to 0.01, only callchains taking >= 0.01%% of the event count of the owner thread are collected in the report.""") parser.add_argument('--no_browser', action='store_true', help="Don't open report in browser.") args = parser.parse_args() process = Process("", 0) if not args.skip_collection: process.name = args.app or args.native_program log_info("Starting data collection stage for process '%s'." % process.name) if not collect_data(args): log_exit("Unable to collect data.") result, output = AdbHelper().run_and_return_output( ['shell', 'pidof', process.name]) if result: try: process.pid = int(output) except: process.pid = 0 collect_machine_info(process) else: args.capture_duration = 0 parse_samples(process, args) generate_threads_offsets(process) report_path = output_report(process, args) if not args.no_browser: open_report_in_browser(report_path) log_info("Flamegraph generated at '%s'." % report_path)
def main(): # Allow deep callchain with length >1000. sys.setrecursionlimit(1500) parser = argparse.ArgumentParser(description="""Report samples in perf.data. Default option is: "-np surfaceflinger -f 6000 -t 10".""") record_group = parser.add_argument_group('Record options') record_group.add_argument('-du', '--dwarf_unwinding', action='store_true', help="""Perform unwinding using dwarf instead of fp.""") record_group.add_argument('-e', '--events', default="", help="""Sample based on event occurences instead of frequency. Format expected is "event_counts event_name". e.g: "10000 cpu-cyles". A few examples of event_name: cpu-cycles, cache-references, cache-misses, branch-instructions, branch-misses""") record_group.add_argument('-f', '--sample_frequency', type=int, default=6000, help="""Sample frequency""") record_group.add_argument('--compile_java_code', action='store_true', help="""On Android N and Android O, we need to compile Java code into native instructions to profile Java code. Android O also needs wrap.sh in the apk to use the native instructions.""") record_group.add_argument('-np', '--native_program', default="surfaceflinger", help="""Profile a native program. The program should be running on the device. Like -np surfaceflinger.""") record_group.add_argument('-p', '--app', help="""Profile an Android app, given the package name. Like -p com.example.android.myapp.""") record_group.add_argument('--pid', type=int, default=-1, help="""Profile a native program with given pid, the pid should exist on the device.""") record_group.add_argument('--record_file', default='perf.data', help='Default is perf.data.') record_group.add_argument('-sc', '--skip_collection', action='store_true', help="""Skip data collection""") record_group.add_argument('--system_wide', action='store_true', help='Profile system wide.') record_group.add_argument('-t', '--capture_duration', type=int, default=10, help="""Capture duration in seconds.""") report_group = parser.add_argument_group('Report options') report_group.add_argument('-c', '--color', default='hot', choices=['hot', 'dso', 'legacy'], help="""Color theme: hot=percentage of samples, dso=callsite DSO name, legacy=brendan style""") report_group.add_argument('--embedded_flamegraph', action='store_true', help="""Generate embedded flamegraph.""") report_group.add_argument('--kallsyms', help='Set the path to find kernel symbols.') report_group.add_argument('--min_callchain_percentage', default=0.01, type=float, help=""" Set min percentage of callchains shown in the report. It is used to limit nodes shown in the flamegraph. For example, when set to 0.01, only callchains taking >= 0.01%% of the event count of the owner thread are collected in the report.""") report_group.add_argument('--no_browser', action='store_true', help="""Don't open report in browser.""") report_group.add_argument('-o', '--report_path', default='report.html', help="""Set report path.""") report_group.add_argument('--one-flamegraph', action='store_true', help="""Generate one flamegraph instead of one for each thread.""") report_group.add_argument('--symfs', help="""Set the path to find binaries with symbols and debug info.""") report_group.add_argument('--title', help='Show a title in the report.') report_group.add_argument('--show_art_frames', action='store_true', help='Show frames of internal methods in the ART Java interpreter.') debug_group = parser.add_argument_group('Debug options') debug_group.add_argument('--disable_adb_root', action='store_true', help="""Force adb to run in non root mode.""") args = parser.parse_args() process = Process("", 0) if not args.skip_collection: if args.pid != -1: process.pid = args.pid args.native_program = '' if args.system_wide: process.pid = -1 args.native_program = '' if args.system_wide: process.name = 'system_wide' else: process.name = args.app or args.native_program or ('Process %d' % args.pid) log_info("Starting data collection stage for '%s'." % process.name) if not collect_data(args): log_exit("Unable to collect data.") if process.pid == 0: result, output = AdbHelper().run_and_return_output(['shell', 'pidof', process.name]) if result: try: process.pid = int(output) except ValueError: process.pid = 0 collect_machine_info(process) else: args.capture_duration = 0 sample_filter_fn = None if args.one_flamegraph: def filter_fn(sample, _symbol, _callchain): sample.pid = sample.tid = process.pid return True sample_filter_fn = filter_fn if not args.title: args.title = '' args.title += '(One Flamegraph)' parse_samples(process, args, sample_filter_fn) generate_threads_offsets(process) report_path = output_report(process, args) if not args.no_browser: open_report_in_browser(report_path) log_info("Flamegraph generated at '%s'." % report_path)