xref: /aosp_15_r20/build/soong/bloaty/bloaty_merger.py (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker# Copyright 2021 Google Inc. All rights reserved.
2*333d2b36SAndroid Build Coastguard Worker#
3*333d2b36SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*333d2b36SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*333d2b36SAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*333d2b36SAndroid Build Coastguard Worker#
7*333d2b36SAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*333d2b36SAndroid Build Coastguard Worker#
9*333d2b36SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*333d2b36SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*333d2b36SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*333d2b36SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*333d2b36SAndroid Build Coastguard Worker# limitations under the License.
14*333d2b36SAndroid Build Coastguard Worker"""Bloaty CSV Merger
15*333d2b36SAndroid Build Coastguard Worker
16*333d2b36SAndroid Build Coastguard WorkerMerges a list of .csv files from Bloaty into a protobuf.  It takes the list as
17*333d2b36SAndroid Build Coastguard Workera first argument and the output as second. For instance:
18*333d2b36SAndroid Build Coastguard Worker
19*333d2b36SAndroid Build Coastguard Worker    $ bloaty_merger binary_sizes.lst binary_sizes.pb.gz
20*333d2b36SAndroid Build Coastguard Worker
21*333d2b36SAndroid Build Coastguard Worker"""
22*333d2b36SAndroid Build Coastguard Worker
23*333d2b36SAndroid Build Coastguard Workerimport argparse
24*333d2b36SAndroid Build Coastguard Workerimport csv
25*333d2b36SAndroid Build Coastguard Workerimport gzip
26*333d2b36SAndroid Build Coastguard Worker
27*333d2b36SAndroid Build Coastguard Worker# pylint: disable=import-error
28*333d2b36SAndroid Build Coastguard Workerimport ninja_rsp
29*333d2b36SAndroid Build Coastguard Worker
30*333d2b36SAndroid Build Coastguard Workerimport file_sections_pb2
31*333d2b36SAndroid Build Coastguard Worker
32*333d2b36SAndroid Build Coastguard WorkerBLOATY_EXTENSION = ".bloaty.csv"
33*333d2b36SAndroid Build Coastguard Worker
34*333d2b36SAndroid Build Coastguard Worker
35*333d2b36SAndroid Build Coastguard Workerdef parse_csv(path):
36*333d2b36SAndroid Build Coastguard Worker    """Parses a Bloaty-generated CSV file into a protobuf.
37*333d2b36SAndroid Build Coastguard Worker
38*333d2b36SAndroid Build Coastguard Worker    Args:
39*333d2b36SAndroid Build Coastguard Worker      path: The filepath to the CSV file, relative to $ANDROID_TOP.
40*333d2b36SAndroid Build Coastguard Worker
41*333d2b36SAndroid Build Coastguard Worker    Returns:
42*333d2b36SAndroid Build Coastguard Worker      A file_sections_pb2.File if the file was found; None otherwise.
43*333d2b36SAndroid Build Coastguard Worker    """
44*333d2b36SAndroid Build Coastguard Worker    file_proto = None
45*333d2b36SAndroid Build Coastguard Worker    with open(path, newline='') as csv_file:
46*333d2b36SAndroid Build Coastguard Worker        file_proto = file_sections_pb2.File()
47*333d2b36SAndroid Build Coastguard Worker        if path.endswith(BLOATY_EXTENSION):
48*333d2b36SAndroid Build Coastguard Worker            file_proto.path = path[: -len(BLOATY_EXTENSION)]
49*333d2b36SAndroid Build Coastguard Worker        section_reader = csv.DictReader(csv_file)
50*333d2b36SAndroid Build Coastguard Worker        for row in section_reader:
51*333d2b36SAndroid Build Coastguard Worker            section = file_proto.sections.add()
52*333d2b36SAndroid Build Coastguard Worker            section.name = row["sections"]
53*333d2b36SAndroid Build Coastguard Worker            section.vm_size = int(row["vmsize"])
54*333d2b36SAndroid Build Coastguard Worker            section.file_size = int(row["filesize"])
55*333d2b36SAndroid Build Coastguard Worker    return file_proto
56*333d2b36SAndroid Build Coastguard Worker
57*333d2b36SAndroid Build Coastguard Worker
58*333d2b36SAndroid Build Coastguard Workerdef create_file_size_metrics(input_list, output_proto):
59*333d2b36SAndroid Build Coastguard Worker    """Creates a FileSizeMetrics proto from a list of CSV files.
60*333d2b36SAndroid Build Coastguard Worker
61*333d2b36SAndroid Build Coastguard Worker    Args:
62*333d2b36SAndroid Build Coastguard Worker      input_list: The path to the file which contains the list of CSV files.
63*333d2b36SAndroid Build Coastguard Worker          Each filepath is separated by a space.
64*333d2b36SAndroid Build Coastguard Worker      output_proto: The path for the output protobuf. It will be compressed
65*333d2b36SAndroid Build Coastguard Worker          using gzip.
66*333d2b36SAndroid Build Coastguard Worker    """
67*333d2b36SAndroid Build Coastguard Worker    metrics = file_sections_pb2.FileSizeMetrics()
68*333d2b36SAndroid Build Coastguard Worker    reader = ninja_rsp.NinjaRspFileReader(input_list)
69*333d2b36SAndroid Build Coastguard Worker    for csv_path in reader:
70*333d2b36SAndroid Build Coastguard Worker        file_proto = parse_csv(csv_path)
71*333d2b36SAndroid Build Coastguard Worker        if file_proto:
72*333d2b36SAndroid Build Coastguard Worker            metrics.files.append(file_proto)
73*333d2b36SAndroid Build Coastguard Worker    with gzip.open(output_proto, "wb") as output:
74*333d2b36SAndroid Build Coastguard Worker        output.write(metrics.SerializeToString())
75*333d2b36SAndroid Build Coastguard Worker
76*333d2b36SAndroid Build Coastguard Worker
77*333d2b36SAndroid Build Coastguard Workerdef main():
78*333d2b36SAndroid Build Coastguard Worker    parser = argparse.ArgumentParser()
79*333d2b36SAndroid Build Coastguard Worker    parser.add_argument("input_list_file", help="List of bloaty csv files.")
80*333d2b36SAndroid Build Coastguard Worker    parser.add_argument("output_proto", help="Output proto.")
81*333d2b36SAndroid Build Coastguard Worker    args = parser.parse_args()
82*333d2b36SAndroid Build Coastguard Worker    create_file_size_metrics(args.input_list_file, args.output_proto)
83*333d2b36SAndroid Build Coastguard Worker
84*333d2b36SAndroid Build Coastguard Worker
85*333d2b36SAndroid Build Coastguard Workerif __name__ == '__main__':
86*333d2b36SAndroid Build Coastguard Worker    main()
87