From 967ec03f2ce48d0c6159f0c93f9a3a61737e00a0 Mon Sep 17 00:00:00 2001 From: xuezhou_yan Date: Sat, 13 Jul 2024 15:14:50 +0800 Subject: [PATCH] =?UTF-8?q?#IACQL2=20rom=5Fram=5Fanalyzer=E8=93=9D?= =?UTF-8?q?=E5=8C=BA=E5=91=8A=E8=AD=A6=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xuezhou_yan --- tools/components/get_components.py | 61 ------------ .../pkgs/rom_ram_baseline_collector.py | 26 +++-- .../lite_small/src/rom_analysis.py | 94 ++++++++++--------- .../lite_small/src/template_processor.py | 7 +- 4 files changed, 68 insertions(+), 120 deletions(-) delete mode 100644 tools/components/get_components.py diff --git a/tools/components/get_components.py b/tools/components/get_components.py deleted file mode 100644 index bec57d7..0000000 --- a/tools/components/get_components.py +++ /dev/null @@ -1,61 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -# Copyright (c) 2023 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# This file finds components of config.json. - -import argparse -import json -import os - - -class Analyzer: - @classmethod - def get_components(cls, config: str, output_file: str): - mandatory_components = list() - optional_components = list() - components = dict() - with open(config, 'r', encoding='utf-8') as r: - config_json = json.load(r) - inherit = config_json['inherit'] - for json_name in inherit: - with open(json_name, 'r', encoding='utf-8') as r: - inherit_file = json.load(r) - for subsystem in inherit_file['subsystems']: - for component in subsystem['components']: - mandatory_components.append(component['component']) - for subsystem in config_json['subsystems']: - for component in subsystem['components']: - if component not in mandatory_components: - optional_components.append(component['component']) - components["components"] = mandatory_components + optional_components - with os.fdopen(os.open(output_file + ".json", os.O_WRONLY | os.O_CREAT, mode=0o640), "w") as fd: - json.dump(components, fd, indent=4) - - -def get_args(): - parser = argparse.ArgumentParser( - description=f"analyze components deps.\n") - parser.add_argument("-c", "--config_json", required=True, type=str, - help="path of root path of openharmony/vendor/hihope/{product_name}/config.json") - parser.add_argument("-o", "--output_file", type=str, default="components", - help="eg: name of output_json_file") - return parser.parse_args() - - -if __name__ == '__main__': - args = get_args() - config_json_path = args.config_json - output_file_name = args.output_file - Analyzer.get_components(config_json_path, output_file_name) \ No newline at end of file diff --git a/tools/rom_ram_analyzer/lite_small/pkgs/rom_ram_baseline_collector.py b/tools/rom_ram_analyzer/lite_small/pkgs/rom_ram_baseline_collector.py index dbf6273..3e57ad4 100644 --- a/tools/rom_ram_analyzer/lite_small/pkgs/rom_ram_baseline_collector.py +++ b/tools/rom_ram_analyzer/lite_small/pkgs/rom_ram_baseline_collector.py @@ -18,31 +18,26 @@ from typing import Dict, Any import json import logging + if __name__ == '__main__': from basic_tool import BasicTool else: from pkgs.basic_tool import BasicTool + class RomRamBaselineCollector: """collect baseline of rom and ram from bundle.json """ + @classmethod def collect(cls, oh_path: str) -> Dict[str, Dict]: - """ - 从bundle.json文件中收集rom、ram的baseline信息 - 返回结果: - "subsystem_name":{ - "component_name":{ - "rom":"123KB, # the value may be "" or null - "ram":"234KB" - } - } - """ def post_handler(x:str)->list: x = x.split("\n") y = [item for item in x if item] return y - bundle_list = BasicTool.execute(cmd=f"find {oh_path} -name bundle.json", post_processor=post_handler) + + bundle_list = BasicTool.execute( + cmd=f"find {oh_path} -name bundle.json", post_processor=post_handler) rom_ram_baseline_dict: Dict[str, Dict] = dict() for bundle in bundle_list: with open(bundle, 'r', encoding='utf-8') as f: @@ -56,12 +51,15 @@ class RomRamBaselineCollector: rom_baseline = component_info.get("rom") ram_baseline = component_info.get("ram") if not (subsystem_name or rom_baseline or ram_baseline): - logging.warning(f"subsystem=\"{subsystem_name}\", rom=\"{rom_baseline}\", ram=\"{ram_baseline}\" in {bundle}") - cls._put(rom_ram_baseline_dict, subsystem_name, component_name, rom_baseline, ram_baseline, bundle) + logging.warning( + f"subsystem=\"{subsystem_name}\", rom=\"{rom_baseline}\", ram=\"{ram_baseline}\" in {bundle}") + cls._put(rom_ram_baseline_dict, subsystem_name, + component_name, rom_baseline, ram_baseline, bundle) return rom_ram_baseline_dict @classmethod - def _put(cls, result_dict: Dict, subsystem_name: str, component_name: str, rom_size: str, ram_size: str, bundle_path:str) -> None: + def _put(cls, result_dict: Dict, subsystem_name: str, component_name: str, rom_size: str, ram_size: str, + bundle_path:str) -> None: if not result_dict.get(subsystem_name): result_dict[subsystem_name] = dict() result_dict[subsystem_name][component_name] = dict() diff --git a/tools/rom_ram_analyzer/lite_small/src/rom_analysis.py b/tools/rom_ram_analyzer/lite_small/src/rom_analysis.py index 72e1195..fb6391f 100644 --- a/tools/rom_ram_analyzer/lite_small/src/rom_analysis.py +++ b/tools/rom_ram_analyzer/lite_small/src/rom_analysis.py @@ -49,11 +49,11 @@ from misc import gn_lineno_collect class RomAnalysisTool: @classmethod - def analysis(cls, product_name: str, product_dict: Dict[str, List[str]], output_file_name: str): + def analysis(cls, product_info: str, product_dict: Dict[str, List[str]], output_file_name: str): """analysis the rom of lite/small product Args: - product_name (str): product name configured in the yaml + product_info (str): product name configured in the yaml product_dict (Dict[str, List[str]]): result dict of compiled product file format: "bin":[...], @@ -63,27 +63,30 @@ class RomAnalysisTool: logging.info("start analyzing...") rom_ram_baseline: Dict[str, Dict] = RomRamBaselineCollector.collect( project_path) - with os.fdopen(os.open("rom_ram_baseline.json", os.O_WRONLY | os.O_CREAT, mode=0o640), 'w', encoding='utf-8') as f: + with os.fdopen(os.open("rom_ram_baseline.json", os.O_WRONLY | os.O_CREAT, mode=0o640), 'w', + encoding='utf-8') as f: json.dump(rom_ram_baseline, f, indent=4) gn_info_file = configs["gn_info_file"] # filename to save gn_info with open(gn_info_file, 'r', encoding='utf-8') as f: gn_info = json.load(f) query_order: Dict[str, List[str] - ] = configs[product_name]["query_order"] # query order of the gn template to be matched + ] = configs[product_info]["query_order"] # query order of the gn template to be matched query_order["etc"] = configs["target_type"] # etc会查找所有的template rom_size_dict: Dict = dict() - if "manual_config" in configs[product_name].keys(): + if "manual_config" in configs[product_info].keys(): cls._match_manual_configured( - configs[product_name]["manual_config"], product_dict, configs[product_name]["product_dir"]["root"], rom_size_dict) + configs[product_info]["manual_config"], product_dict, configs[product_info]["product_dir"]["root"], + rom_size_dict) cls._subsystem_component_for_all_product_file( product_dict, query_order, gn_info, gn_info_file, rom_ram_baseline, rom_size_dict) if unit_adapt: cls._result_unit_adaptive(rom_size_dict) - with os.fdopen(os.open(output_file_name + ".json", os.O_WRONLY | os.O_CREAT, mode=0o640), 'w', encoding='utf-8') as f: + with os.fdopen(os.open(output_file_name + ".json", os.O_WRONLY | os.O_CREAT, mode=0o640), 'w', + encoding='utf-8') as f: json.dump(rom_size_dict, f, indent=4) - cls._save_as_xls(rom_size_dict, product_name, baseline) + cls._save_as_xls(rom_size_dict, product_info, baseline) logging.info("success") - + @classmethod def collect_gn_info(cls): logging.info("start scanning BUILD.gn") @@ -236,6 +239,7 @@ class RomAnalysisTool: p = os.path.join(project_path, item) t = list(filter(lambda x: p not in x, t)) return t + grep_result: List[str] = BasicTool.grep_ern( base_name, project_path, @@ -269,23 +273,27 @@ class RomAnalysisTool: return str(), str(), str() @classmethod - def _save_as_xls(cls, result_dict: Dict, product_name: str, baseline: bool) -> None: + def _get_one_line(cls, baseline_info, subsystem_name, component_name, component_baseline, file_name, file_size): + if baseline_info: + return [subsystem_name, component_name, + component_baseline, file_name, file_size] + else: + return [subsystem_name, component_name, + file_name, file_size] + + @classmethod + def _save_as_xls(cls, result_dict_info: Dict, product_name_info: str, baseline_info: bool) -> None: logging.info("saving as xls...") - header = ["subsystem_name", "component_name", - "output_file", "size(Byte)"] - if baseline: - header = ["subsystem_name", "component_name", "baseline", - "output_file", "size(Byte)"] - tmp_dict = copy.deepcopy(result_dict) + header = ["subsystem_name", "component_name", "output_file", "size(Byte)"] + if baseline_info: + header = ["subsystem_name", "component_name", "baseline", "output_file", "size(Byte)"] + tmp_dict = dict() + for key in result_dict_info.keys(): + tmp_dict[key] = result_dict_info[key] excel_writer = SimpleExcelWriter("rom") excel_writer.set_sheet_header(headers=header) - subsystem_start_row = 1 - subsystem_end_row = 0 - subsystem_col = 0 - component_start_row = 1 - component_end_row = 0 - component_col = 1 - baseline_col = 2 + (subsystem_start_row, subsystem_end_row, subsystem_col, component_start_row, component_end_row, + component_col, baseline_col) = (1, 0, 0, 1, 0, 1, 2) if "size" in tmp_dict.keys(): del tmp_dict["size"] for subsystem_name in tmp_dict.keys(): @@ -311,31 +319,28 @@ class RomAnalysisTool: for fileinfo in component_dict.get("filelist"): file_name = fileinfo.get("file_name") file_size = fileinfo.get("size") - line = [subsystem_name, component_name, - file_name, file_size] - if baseline: - line = [subsystem_name, component_name, - component_baseline, file_name, file_size] + line = cls._get_one_line(baseline_info, subsystem_name, component_name, component_baseline, + file_name, file_size) excel_writer.append_line(line) excel_writer.write_merge(component_start_row, component_col, component_end_row, component_col, component_name) - if baseline: + if baseline_info: excel_writer.write_merge(component_start_row, baseline_col, component_end_row, baseline_col, component_baseline) component_start_row = component_end_row + 1 excel_writer.write_merge(subsystem_start_row, subsystem_col, subsystem_end_row, subsystem_col, subsystem_name) subsystem_start_row = subsystem_end_row + 1 - output_name: str = configs[product_name]["output_name"] + output_name: str = configs[product_name_info]["output_name"] output_name = output_name.replace(".json", ".xls") excel_writer.save(output_name) logging.info("save as xls success.") @classmethod - def _result_unit_adaptive(cls, result_dict: Dict[str, Dict]) -> None: - total_size = unit_adaptive(result_dict["size"]) - del result_dict["size"] - for subsystem_name, subsystem_info in result_dict.items(): + def _result_unit_adaptive(cls, output_result_dict: Dict[str, Dict]) -> None: + total_size = unit_adaptive(output_result_dict["size"]) + del output_result_dict["size"] + for subsystem_name, subsystem_info in output_result_dict.items(): sub_size = unit_adaptive(subsystem_info["size"]) count = subsystem_info["count"] del subsystem_info["size"] @@ -344,10 +349,11 @@ class RomAnalysisTool: component_info["size"] = unit_adaptive(component_info["size"]) subsystem_info["size"] = sub_size subsystem_info["count"] = count - result_dict["size"] = total_size + output_result_dict["size"] = total_size @classmethod - def _match_manual_configured(cls, manual_config_info: Dict[str, Dict], compiled_files: Dict[str, List], compiled_root_path: str, result_dict: Dict[str, Dict]) -> None: + def _match_manual_configured(cls, manual_config_info: Dict[str, Dict], compiled_files: Dict[str, List], + compiled_root_path: str, output_result_dict: Dict[str, Dict]) -> None: for file_path, file_info in manual_config_info.items(): full_path = os.path.join( project_path, compiled_root_path, file_path) @@ -357,7 +363,7 @@ class RomAnalysisTool: file_info["size"] = os.path.getsize(full_path) file_info["file_name"] = full_path cls._put(file_info["subsystem"], - file_info["component"], file_info, result_dict) + file_info["component"], file_info, output_result_dict) for _, v in compiled_files.items(): if full_path not in v: continue @@ -366,7 +372,8 @@ class RomAnalysisTool: break @classmethod - def _iterate_all_template_type(cls, type_list: List[str], gn_info: Dict, gn_info_file: str, base_name: str, rom_ram_baseline: Dict, rom_size_dict: Dict, f: str, size: int): + def _iterate_all_template_type(cls, type_list: List[str], gn_info: Dict, gn_info_file: str, base_name: str, + rom_ram_baseline: Dict, rom_size_dict: Dict, f: str, size: int): find_flag = False component_rom_baseline = None for tn in type_list: # tn example: ohos_shared_library @@ -383,7 +390,8 @@ class RomAnalysisTool: continue d["size"] = size d["file_name"] = f.replace(project_path, "") - if rom_ram_baseline.get(d["subsystem_name"]) and rom_ram_baseline.get(d["subsystem_name"]).get(d["component_name"]): + if rom_ram_baseline.get(d["subsystem_name"]) and rom_ram_baseline.get(d["subsystem_name"]).get( + d["component_name"]): component_rom_baseline = rom_ram_baseline.get( d["subsystem_name"]).get(d["component_name"]).get("rom") cls._put(d["subsystem_name"], @@ -412,8 +420,10 @@ class RomAnalysisTool: }, rom_size_dict) @classmethod - def _subsystem_component_for_all_product_file(cls, product_dict: Dict[str, List[str]], query_order: Dict[str, List[str]], - gn_info: Dict, gn_info_file: str, rom_ram_baseline: Dict, rom_size_dict: Dict): + def _subsystem_component_for_all_product_file(cls, product_dict: Dict[str, List[str]], + query_order: Dict[str, List[str]], + gn_info: Dict, gn_info_file: str, rom_ram_baseline: Dict, + rom_size_dict: Dict): for t, l in product_dict.items(): for f in l: # 遍历所有文件 if os.path.isdir(f): @@ -433,7 +443,7 @@ def main(): if recollect_gn: RomAnalysisTool.collect_gn_info() product_dict: Dict[str, List[str] - ] = RomAnalysisTool.collect_product_info(product_name) + ] = RomAnalysisTool.collect_product_info(product_name) RomAnalysisTool.analysis(product_name, product_dict, output_file) diff --git a/tools/rom_ram_analyzer/lite_small/src/template_processor.py b/tools/rom_ram_analyzer/lite_small/src/template_processor.py index 1f898be..d96fd04 100644 --- a/tools/rom_ram_analyzer/lite_small/src/template_processor.py +++ b/tools/rom_ram_analyzer/lite_small/src/template_processor.py @@ -110,10 +110,11 @@ class BaseProcessor(ABC): if not alter_list: return str(), str() alter_list.sort(key=lambda x: len(x), reverse=True) - return self.sc_dict[alter_list[0]].get("subsystem"), self.sc_dict[alter_list[0]].get("component") + return self.sc_dict[alter_list[0]].get("subsystem"), self.sc_dict[alter_list[0]].get("component") -def _gn_var_process(project_path: str, gn_v: str, alt_v: str, gn_path: str, ifrom: str, efrom: str, strip_quote: bool = False) -> Tuple[str, str]: +def _gn_var_process(project_path: str, gn_v: str, alt_v: str, gn_path: str, ifrom: str, efrom: str, + strip_quote: bool = False) -> Tuple[str, str]: """ :param project_path:项目根路径 :gn_v:gn中的值(可能为变量或空) @@ -138,7 +139,7 @@ def _gn_var_process(project_path: str, gn_v: str, alt_v: str, gn_path: str, ifro class DefaultProcessor(BaseProcessor): - + @property def undefined(self): return "UNDEFINED" -- Gitee