• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# encoding=utf-8
3# ============================================================================
4
5# Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17# Description: Provides dma driver source \n
18#
19# History: \n
20# 2023-01-16, Create file. \n
21# ============================================================================
22import os
23import shutil
24import re
25import sys
26root_dir = os.path.split(os.path.realpath(__file__))[0]
27sys.path.append(os.path.join(root_dir, '..', 'config'))
28sys.path.append(root_dir)
29from enviroment import TargetEnvironment
30from utils.build_utils import output_root
31
32class statistics(object):
33    def __init__(self, file):
34        self.name = []
35        self.text_size = []
36        self.data_size = []
37        self.bss_size = []
38        self.group = [[0 for i in range(6)] for j in range(100)]
39        with open(file, encoding="utf-8") as f:
40            cluster = f.read()
41            component_name = cluster[cluster.find("[")+1:cluster.find("]")]
42            component_name = component_name.replace("\n", '')
43            component_name = component_name.replace(" ", '')
44            component_name = component_name.replace("'", '')
45            self.name = component_name.split(",")
46            for i in range(0, len(self.name)):
47                self.text_size.append(0)
48                self.data_size.append(0)
49                self.bss_size.append(0)
50            group_name = cluster[cluster.find("group_details = {")+20:cluster.find("}")]
51            group_name = group_name.split("\n")
52            for i in range(0, len(group_name)):
53                name_g = group_name[i].replace("[", '')
54                name_g = name_g.replace("]", '')
55                name_g = name_g.replace(" ", '')
56                name_g = name_g.replace("'", '')
57                name_g = re.split(':|,', name_g)
58                if (len(name_g) > 0):
59                    self.group[i] = name_g
60
61    def data_set(self, file_name, index):
62        cmds = 'size ' + file_name
63        string = os.popen(cmds).read()
64        for line in string.splitlines():
65            number = re.findall("\d+",line)
66            if len(number) > 0:
67                self.text_size[index] = int(self.text_size[index]) + int(number[0])
68                self.data_size[index] = int(self.data_size[index]) + int(number[1])
69                self.bss_size[index] = int(self.bss_size[index]) + int(number[2])
70
71    def data_find(self, file_name):
72        file_name = file_name.replace(".a", '')
73        file_name = file_name.replace("lib", '')
74        for i in range(0, len(self.group)):
75            if len(self.group[i]) == 0:
76                break
77            for j in range(1, len(self.group[i])):
78                if (file_name == self.group[i][j]):
79                    file_name = self.group[i][0]
80        for i in range(0, len(self.name)):
81            if self.name[i] == file_name:
82                return i
83        return -1
84
85    def creat(self, path):
86        with open(path + '/codesize_statistic.html', "w") as f:
87            f.write('<h1>Codesize Statistics</h1>')
88            f.write('<table border="1" width = "40%" cellspacing="0" cellpadding="0" align="left">')
89            f.write('<tr><th>drivers</th><th>text</th><th>data</th><th>bss</th><tr>\n')
90
91            total_text_size = 0
92            total_data_size = 0
93            total_bss_size = 0
94
95            for i in range(0, len(self.name)):
96
97                total = self.text_size[i] + self.data_size[i] + self.bss_size[i]
98                if not self.name[i] == 0 and total > 0:
99                    total_text_size += self.text_size[i]
100                    total_data_size += self.data_size[i]
101                    total_bss_size += self.bss_size[i]
102
103                    result = '<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n' % \
104                        (str(self.name[i]), str(self.text_size[i]), str(self.data_size[i]), str(self.bss_size[i]))
105                    f.write(result)
106            result = '<tr><td>Total</td><td>%s</td><td>%s</td><td>%s</td></tr>\n' % \
107                (str(total_text_size), str(total_data_size), str(total_bss_size))
108            f.write(result)
109            f.write('</table>')
110
111def find_listdir(stat, path):
112    if os.path.exists(path):
113        file_list = os.listdir(path)
114        for file in file_list:
115            fulldir = os.path.join(path, file)
116            if os.path.isdir(fulldir):
117                find_listdir(stat, fulldir)
118            if os.path.isfile(fulldir):
119                bool = file.endswith(".a")
120                if bool:
121                    i = stat.data_find(file)
122                    if i >= 0:
123                        org_work_path = os.getcwd()
124                        os.chdir(path)
125                        stat.data_set(file, i)
126                        os.chdir(org_work_path)
127
128if __name__ == '__main__':
129    root_dir = os.path.split(os.path.realpath(__file__))[0]
130    root_dir = os.path.join(root_dir, '..', '..')
131    os.chdir(root_dir)
132    sys.path.append(os.path.join(root_dir, 'build', 'config'))
133    sys.path.append(os.path.join(root_dir, 'build', 'script'))
134
135    env = TargetEnvironment(sys.argv[1])
136    chip = env.get('chip')
137    core = env.get('core')
138    stat = statistics("./build/config/target_config/%s/codesize_statistic_config.json" % (chip))
139    path = os.path.join(output_root, chip, core, sys.argv[1])
140    find_listdir(stat, path)
141    stat.creat(path)
142
143    print('codesize statistics done')
144