• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2020-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
17import sys
18import argparse
19
20from js_profiler_config import key_word
21from js_profiler_config import label_name
22from js_profiler_config import profiler_name
23
24sys.path.append('./js_profiler_config.py')
25
26
27def add_label(index):
28    return label_name[index]
29
30
31def add_parent_label(index):
32    parent_info = profiler_name[label_name[index]]
33    return 'UNKNOWN' if parent_info == 'P_UNKNOWN' else parent_info
34
35
36def add_component_info(component_id):
37    return key_word[component_id]
38
39
40def add_description_info(description_id):
41    return '' if description_id == 0 else key_word[description_id]
42
43
44def parser(opt):
45    source_file = opt.source
46    try:
47        with open(source_file, 'r') as source:
48            for line in source.readlines():
49                strlist = line.split(' ')
50                if len(strlist) == 5:
51                    msg = 'COST ' + add_component_info(int(strlist[0]))
52                    msg = msg + ' ' + add_label(int(strlist[1]))
53                    msg = msg + ' ' + strlist[2] + 'ms'
54                    msg = msg + ' [' + \
55                          add_description_info(int(strlist[3])) + ']'
56                    msg = msg + ' ' + add_parent_label(int(strlist[4]))
57                    print(msg)
58                    output(msg, opt.destination)
59    except Exception:
60        print('open source file error')
61
62
63def output(msg, dest):
64    try:
65        with open(dest, 'a+') as destination:
66            destination.write(msg + '\n')
67    except Exception:
68        print('open destination file error')
69
70
71def get_parameters():
72    parser_config = argparse.ArgumentParser()
73    parser_config.add_argument('--source', type=str, default='',
74                               help='input file')
75    parser_config.add_argument('--destination', type=str, default='.',
76                               help='output file')
77    config = parser_config.parse_args()
78    return config
79
80
81if __name__ == '__main__':
82    parser(get_parameters())
83