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
19import os
20import stat
21
22from js_profiler_config import key_word
23from js_profiler_config import label_name
24from js_profiler_config import profiler_name
25
26sys.path.append('./js_profiler_config.py')
27
28
29def add_label(index):
30    return label_name[index]
31
32
33def add_parent_label(index):
34    parent_info = profiler_name[label_name[index]]
35    return 'UNKNOWN' if parent_info == 'P_UNKNOWN' else parent_info
36
37
38def add_component_info(component_id):
39    return key_word[component_id]
40
41
42def add_description_info(description_id):
43    return '' if description_id == 0 else key_word[description_id]
44
45
46def parser(opt):
47    source_file = opt.source
48    try:
49        flags = os.O_WRONLY | os.O_CREAT
50        modes = stat.S_IWUSR | stat.S_IRUSR
51        with os.fdopen(os.open(source_file, flags, modes), 'w') as source:
52            for line in source.readlines():
53                strlist = line.split(' ')
54                if len(strlist) == 5:
55                    msg = 'COST ' + add_component_info(int(strlist[0]))
56                    msg = msg + ' ' + add_label(int(strlist[1]))
57                    msg = msg + ' ' + strlist[2] + 'ms'
58                    msg = msg + ' [' + \
59                          add_description_info(int(strlist[3])) + ']'
60                    msg = msg + ' ' + add_parent_label(int(strlist[4]))
61                    print(msg)
62                    output(msg, opt.destination)
63    except Exception:
64        print('open source file error')
65
66
67def output(msg, dest):
68    try:
69        flags = os.O_WRONLY | os.O_CREAT | os.O_APPEND
70        modes = stat.S_IWUSR | stat.S_IRUSR
71        with os.fdopen(os.open(dest, flags, modes), 'a+') as destination:
72            destination.write(msg + '\n')
73    except Exception:
74        print('open destination file error')
75
76
77def get_parameters():
78    parser_config = argparse.ArgumentParser()
79    parser_config.add_argument('--source', type=str, default='',
80                               help='input file')
81    parser_config.add_argument('--destination', type=str, default='.',
82                               help='output file')
83    config = parser_config.parse_args()
84    return config
85
86
87if __name__ == '__main__':
88    parser(get_parameters())
89