148f512ceSopenharmony_ci#!/usr/bin/env python
248f512ceSopenharmony_ci# -*- coding: utf-8 -*-
348f512ceSopenharmony_ci#   Copyright (c) 2021 Huawei Device Co., Ltd.
448f512ceSopenharmony_ci#   Licensed under the Apache License, Version 2.0 (the "License");
548f512ceSopenharmony_ci#   you may not use this file except in compliance with the License.
648f512ceSopenharmony_ci#   You may obtain a copy of the License at
748f512ceSopenharmony_ci#
848f512ceSopenharmony_ci#       http://www.apache.org/licenses/LICENSE-2.0
948f512ceSopenharmony_ci#
1048f512ceSopenharmony_ci#   Unless required by applicable law or agreed to in writing, software
1148f512ceSopenharmony_ci#   distributed under the License is distributed on an "AS IS" BASIS,
1248f512ceSopenharmony_ci#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1348f512ceSopenharmony_ci#   See the License for the specific language governing permissions and
1448f512ceSopenharmony_ci#   limitations under the License.
1548f512ceSopenharmony_ci
1648f512ceSopenharmony_ciimport os
1748f512ceSopenharmony_ciimport sys
1848f512ceSopenharmony_ciimport time
1948f512ceSopenharmony_ciimport argparse
2048f512ceSopenharmony_cifrom hiperf_utils import get_lib
2148f512ceSopenharmony_cifrom hiperf_utils import dir_check
2248f512ceSopenharmony_cifrom hiperf_utils import file_check
2348f512ceSopenharmony_ci
2448f512ceSopenharmony_ci
2548f512ceSopenharmony_cidef get_used_binaries(perf_data, report_file, local_lib_dir, html_template):
2648f512ceSopenharmony_ci    if local_lib_dir:
2748f512ceSopenharmony_ci        get_lib().ReportUnwindJson(perf_data.encode("utf-8"),
2848f512ceSopenharmony_ci                                   'json.txt'.encode("utf-8"),
2948f512ceSopenharmony_ci                                   local_lib_dir.encode("utf-8"))
3048f512ceSopenharmony_ci    else:
3148f512ceSopenharmony_ci        get_lib().ReportJson(perf_data.encode("utf-8"),
3248f512ceSopenharmony_ci                             'json.txt'.encode("utf-8"))
3348f512ceSopenharmony_ci    time.sleep(2)
3448f512ceSopenharmony_ci    with open('json.txt', 'r') as json_file:
3548f512ceSopenharmony_ci        all_json = json_file.read()
3648f512ceSopenharmony_ci        template = os.path.join(html_template, 'report.html')
3748f512ceSopenharmony_ci    with open(template, 'r', encoding='utf-8') as html_file:
3848f512ceSopenharmony_ci        html_str = html_file.read()
3948f512ceSopenharmony_ci    with open(report_file, 'w', encoding='utf-8') as report_html_file:
4048f512ceSopenharmony_ci        report_html_file.write(html_str + all_json + '</script>'
4148f512ceSopenharmony_ci                                      ' </body>'
4248f512ceSopenharmony_ci                                      ' </html>')
4348f512ceSopenharmony_ci    dirname, _ = os.path.split(os.path.abspath(sys.argv[0]))
4448f512ceSopenharmony_ci    abs_path = os.path.join(dirname, report_file)
4548f512ceSopenharmony_ci    print("save to %s success" % abs_path)
4648f512ceSopenharmony_ci    os.remove('json.txt')
4748f512ceSopenharmony_ci
4848f512ceSopenharmony_ci
4948f512ceSopenharmony_cidef main():
5048f512ceSopenharmony_ci    parser = argparse.ArgumentParser(description=""" To make a report, you
5148f512ceSopenharmony_ci    need to enter the data source and the path of the report.""")
5248f512ceSopenharmony_ci    parser.add_argument('-i', '--perf_data', default='perf.data',
5348f512ceSopenharmony_ci                        type=file_check, help=""" The path of profiling
5448f512ceSopenharmony_ci                        data.""")
5548f512ceSopenharmony_ci    parser.add_argument('-r', '--report_html', default='hiperf_report.html',
5648f512ceSopenharmony_ci                        help="""the path of the report.""")
5748f512ceSopenharmony_ci    parser.add_argument('-l', '--local_lib_dir', type=dir_check, default='./binary_cache',
5848f512ceSopenharmony_ci                        help="""Path to find symbol dir use to
5948f512ceSopenharmony_ci                         do offline unwind stack""")
6048f512ceSopenharmony_ci    parser.add_argument('-t', '--html_template', default='./',
6148f512ceSopenharmony_ci                        type=dir_check, help=""" The path of report html template
6248f512ceSopenharmony_ci                        """)
6348f512ceSopenharmony_ci    args = parser.parse_args()
6448f512ceSopenharmony_ci
6548f512ceSopenharmony_ci    get_used_binaries(args.perf_data, args.report_html, args.local_lib_dir, args.html_template)
6648f512ceSopenharmony_ci
6748f512ceSopenharmony_ci
6848f512ceSopenharmony_ciif __name__ == '__main__':
6948f512ceSopenharmony_ci    main()
70