1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # Copyright (c) 2021 Huawei Device Co., Ltd. 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 import os 17 import sys 18 import time 19 import argparse 20 from hiperf_utils import get_lib 21 from hiperf_utils import dir_check 22 from hiperf_utils import file_check 23 24 25 def get_used_binaries(perf_data, report_file, local_lib_dir): 26 if local_lib_dir: 27 get_lib().ReportUnwindJson(perf_data.encode("utf-8"), 28 'json.txt'.encode("utf-8"), 29 local_lib_dir.encode("utf-8")) 30 else: 31 get_lib().ReportJson(perf_data.encode("utf-8"), 32 'json.txt'.encode("utf-8")) 33 time.sleep(2) 34 with open('json.txt', 'r') as json_file: 35 all_json = json_file.read() 36 with open('report.html', 'r', encoding='utf-8') as html_file: 37 html_str = html_file.read() 38 with open(report_file, 'w', encoding='utf-8') as report_html_file: 39 report_html_file.write(html_str + all_json + '</script>' 40 ' </body>' 41 ' </html>') 42 dirname, _ = os.path.split(os.path.abspath(sys.argv[0])) 43 abs_path = os.path.join(dirname, report_file) 44 print("save to %s success" % abs_path) 45 os.remove('json.txt') 46 47 48 def main(): 49 parser = argparse.ArgumentParser(description=""" To make a report, you 50 need to enter the data source and the path of the report.""") 51 parser.add_argument('-i', '--perf_data', default='perf.data', 52 type=file_check, help=""" The path of profiling 53 data.""") 54 parser.add_argument('-r', '--report_html', default='hiperf_report.html', 55 help="""the path of the report.""") 56 parser.add_argument('-l', '--local_lib_dir', type=dir_check, default='./binary_cache', 57 help="""Path to find symbol dir use to 58 do offline unwind stack""") 59 args = parser.parse_args() 60 61 get_used_binaries(args.perf_data, args.report_html, args.local_lib_dir) 62 63 64 if __name__ == '__main__': 65 main() 66