File: astc_size_binary.py

package info (click to toggle)
astc-encoder 5.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 43,352 kB
  • sloc: ansic: 44,622; cpp: 24,142; python: 3,403; sh: 78; makefile: 24
file content (207 lines) | stat: -rw-r--r-- 5,994 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
# -----------------------------------------------------------------------------
# Copyright 2019-2020 Arm Limited
#
# 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.
# -----------------------------------------------------------------------------
"""
The ``astc_size_binary`` utility provides a wrapper around the Linux ``size``
utility to view binary section sizes, and optionally compare the section sizes
of two binaries. Section sizes are given for code (``.text``), read-only data
(``.rodata``), and zero initialized data (``.bss``). All other sections are
ignored.

A typical report comparing the size of a new binary against a reference looks
like this:

.. code-block::

              Code   RO Data   ZI Data
     Ref    411298    374560    128576
     New    560530     89552     31744
   Abs D    149232   -285008    -96832
   Rel D    36.28%   -76.09%   -75.31%
"""


import argparse
import platform
import shutil
import subprocess as sp
import sys


def run_size_linux(binary):
    """
    Run size on a single binary.

    Args:
        binary (str): The path of the binary file to process.

    Returns:
        tuple(int, int, int): A triplet of code size, read-only data size, and
        zero-init data size, all in bytes.

    Raises:
        CalledProcessException: The ``size`` subprocess failed for any reason.
    """
    args = ["size", "--format=sysv", binary]
    result = sp.run(args, stdout=sp.PIPE, stderr=sp.PIPE,
                    check=True, universal_newlines=True)

    data = {}
    patterns = {"Code": ".text", "RO": ".rodata", "ZI": ".bss"}

    lines = result.stdout.splitlines()
    for line in lines:
        for key, value in patterns.items():
            if line.startswith(value):
                size = float(line.split()[1])
                data[key] = size

    return (data["Code"], data["RO"], data["ZI"])


def run_size_macos(binary):
    """
    Run size on a single binary.

    Args:
        binary (str): The path of the binary file to process.

    Returns:
        tuple(int, int, int): A triplet of code size, read-only data size, and
        zero-init data size, all in bytes.

    Raises:
        CalledProcessException: The ``size`` subprocess failed for any reason.
    """
    args = ["size", "-m", binary]
    result = sp.run(args, stdout=sp.PIPE, stderr=sp.PIPE,
                    check=True, universal_newlines=True)

    code = 0
    dataRO = 0
    dataZI = 0

    currentSegment = None

    lines = result.stdout.splitlines()
    for line in lines:
        line = line.strip()

        if line.startswith("Segment"):
            parts = line.split()
            assert len(parts) >= 3, parts

            currentSegment = parts[1]
            size = int(parts[2])

            if currentSegment == "__TEXT:":
                code += size

            if currentSegment == "__DATA_CONST:":
                dataRO += size

            if currentSegment == "__DATA:":
                dataZI += size

        if line.startswith("Section"):
            parts = line.split()
            assert len(parts) >= 3, parts

            section = parts[1]
            size = int(parts[2])

            if currentSegment == "__TEXT:" and section == "__const:":
                code -= size
                dataRO += size

    return (code, dataRO, dataZI)


def parse_command_line():
    """
    Parse the command line.

    Returns:
        Namespace: The parsed command line container.
    """
    parser = argparse.ArgumentParser()

    parser.add_argument("bin", type=argparse.FileType("r"),
                        help="The new binary to size")

    parser.add_argument("ref", nargs="?", type=argparse.FileType("r"),
                        help="The reference binary to compare against")

    return parser.parse_args()


def main():
    """
    The main function.

    Returns:
        int: The process return code.
    """
    args = parse_command_line()

    # Preflight - check that size exists. Note that size might still fail at
    # runtime later, e.g. if the binary is not of the correct format
    path = shutil.which("size")
    if not path:
        print("ERROR: The 'size' utility is not installed on the PATH")
        return 1

    if platform.system() == "Darwin":
        run_size = run_size_macos
    else:
        run_size = run_size_linux

    # Collect the data
    try:
        newSize = run_size(args.bin.name)
        if args.ref:
            refSize = run_size(args.ref.name)
    except sp.CalledProcessError as ex:
        print("ERROR: The 'size' utility failed")
        print("       %s" % ex.stderr.strip())
        return 1

    # Print the basic table of absolute values
    print("%8s  % 8s  % 8s  % 8s" % ("", "Code", "RO Data", "ZI Data"))
    if args.ref:
        print("%8s  % 8u  % 8u  % 8u" % ("Ref", *refSize))
    print("%8s  % 8u  % 8u  % 8u" % ("New", *newSize))

    # Print the difference if we have a reference
    if args.ref:
        diffAbs = []
        diffRel = []
        for refVal, newVal in zip(refSize, newSize):
            diff = newVal - refVal
            diffAbs.append(diff)
            diffRel.append((diff / refVal) * 100.0)

        dat = ("Abs D", diffAbs[0], diffAbs[1], diffAbs[2])
        print("%8s  % 8u  % 8u  % 8u" % dat)
        dat = ("Rel D", diffRel[0], diffRel[1], diffRel[2])
        print("%8s  % 7.2f%%  % 7.2f%%  % 7.2f%%" % dat)

    return 0


if __name__ == "__main__":
    sys.exit(main())