2020-01-13 01:05:46 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
#
|
|
|
|
# The MIT License (MIT)
|
|
|
|
#
|
|
|
|
# Copyright (c) 2020 Damien P. George
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
# THE SOFTWARE.
|
|
|
|
|
|
|
|
"""
|
|
|
|
This script is used to compute metrics, like code size, of the various ports.
|
|
|
|
|
|
|
|
Typical usage is:
|
|
|
|
|
|
|
|
$ ./tools/metrics.py build | tee size0
|
|
|
|
<wait for build to complete>
|
|
|
|
$ git switch new-feature-branch
|
|
|
|
$ ./tools/metrics.py build | tee size1
|
|
|
|
<wait for build to complete>
|
|
|
|
$ ./tools/metrics.py diff size0 size1
|
|
|
|
|
|
|
|
Other commands:
|
|
|
|
|
|
|
|
$ ./tools/metrics.py sizes # print all firmware sizes
|
|
|
|
$ ./tools/metrics.py clean # clean all ports
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2020-04-19 07:53:59 -04:00
|
|
|
import collections, sys, re, subprocess
|
2020-01-13 01:05:46 -05:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
MAKE_FLAGS = ["-j3", "CFLAGS_EXTRA=-DNDEBUG"]
|
|
|
|
|
2020-01-13 01:05:46 -05:00
|
|
|
|
|
|
|
class PortData:
|
|
|
|
def __init__(self, name, dir, output, make_flags=None):
|
|
|
|
self.name = name
|
|
|
|
self.dir = dir
|
|
|
|
self.output = output
|
|
|
|
self.make_flags = make_flags
|
2020-04-19 07:09:22 -04:00
|
|
|
self.needs_mpy_cross = dir not in ("bare-arm", "minimal")
|
2020-01-13 01:05:46 -05:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2020-01-13 01:05:46 -05:00
|
|
|
port_data = {
|
2020-02-26 23:36:53 -05:00
|
|
|
"b": PortData("bare-arm", "bare-arm", "build/firmware.elf"),
|
|
|
|
"m": PortData("minimal x86", "minimal", "build/firmware.elf"),
|
2022-08-10 22:40:23 -04:00
|
|
|
"u": PortData("unix x64", "unix", "build-standard/micropython"),
|
|
|
|
"n": PortData("unix nanbox", "unix", "build-nanbox/micropython", "VARIANT=nanbox"),
|
2020-02-26 23:36:53 -05:00
|
|
|
"s": PortData("stm32", "stm32", "build-PYBV10/firmware.elf", "BOARD=PYBV10"),
|
|
|
|
"c": PortData("cc3200", "cc3200", "build/WIPY/release/application.axf", "BTARGET=application"),
|
|
|
|
"8": PortData("esp8266", "esp8266", "build-GENERIC/firmware.elf"),
|
2021-04-05 22:50:19 -04:00
|
|
|
"3": PortData("esp32", "esp32", "build-GENERIC/micropython.elf"),
|
2022-06-17 00:57:58 -04:00
|
|
|
"x": PortData("mimxrt", "mimxrt", "build-TEENSY40/firmware.elf"),
|
|
|
|
"e": PortData("renesas-ra", "renesas-ra", "build-RA6M2_EK/firmware.elf"),
|
2020-02-26 23:36:53 -05:00
|
|
|
"r": PortData("nrf", "nrf", "build-pca10040/firmware.elf"),
|
2021-04-20 07:39:53 -04:00
|
|
|
"p": PortData("rp2", "rp2", "build-PICO/firmware.elf"),
|
2020-02-26 23:36:53 -05:00
|
|
|
"d": PortData("samd", "samd", "build-ADAFRUIT_ITSYBITSY_M4_EXPRESS/firmware.elf"),
|
2020-01-13 01:05:46 -05:00
|
|
|
}
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2020-01-13 01:05:46 -05:00
|
|
|
def syscmd(*args):
|
|
|
|
sys.stdout.flush()
|
|
|
|
a2 = []
|
|
|
|
for a in args:
|
|
|
|
if isinstance(a, str):
|
|
|
|
a2.append(a)
|
|
|
|
elif a:
|
|
|
|
a2.extend(a)
|
2020-03-10 23:23:39 -04:00
|
|
|
subprocess.check_call(a2)
|
2020-01-13 01:05:46 -05:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2020-01-13 01:05:46 -05:00
|
|
|
def parse_port_list(args):
|
|
|
|
if not args:
|
|
|
|
return list(port_data.values())
|
|
|
|
else:
|
|
|
|
ports = []
|
|
|
|
for arg in args:
|
|
|
|
for port_char in arg:
|
|
|
|
try:
|
|
|
|
ports.append(port_data[port_char])
|
|
|
|
except KeyError:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("unknown port:", port_char)
|
2020-01-13 01:05:46 -05:00
|
|
|
sys.exit(1)
|
|
|
|
return ports
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2020-01-13 01:05:46 -05:00
|
|
|
def read_build_log(filename):
|
2020-04-19 07:53:59 -04:00
|
|
|
data = collections.OrderedDict()
|
2020-01-13 01:05:46 -05:00
|
|
|
lines = []
|
|
|
|
found_sizes = False
|
|
|
|
with open(filename) as f:
|
|
|
|
for line in f:
|
|
|
|
line = line.strip()
|
2020-02-26 23:36:53 -05:00
|
|
|
if line.strip() == "COMPUTING SIZES":
|
2020-01-13 01:05:46 -05:00
|
|
|
found_sizes = True
|
|
|
|
elif found_sizes:
|
|
|
|
lines.append(line)
|
|
|
|
is_size_line = False
|
|
|
|
for line in lines:
|
|
|
|
if is_size_line:
|
|
|
|
fields = line.split()
|
|
|
|
data[fields[-1]] = [int(f) for f in fields[:-2]]
|
|
|
|
is_size_line = False
|
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
is_size_line = line.startswith("text\t ")
|
2020-01-13 01:05:46 -05:00
|
|
|
return data
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2020-01-13 01:05:46 -05:00
|
|
|
def do_diff(args):
|
|
|
|
"""Compute the difference between firmware sizes."""
|
|
|
|
|
2020-04-19 02:14:01 -04:00
|
|
|
# Parse arguments.
|
|
|
|
error_threshold = None
|
|
|
|
if len(args) >= 2 and args[0] == "--error-threshold":
|
|
|
|
args.pop(0)
|
|
|
|
error_threshold = int(args.pop(0))
|
|
|
|
|
2020-01-13 01:05:46 -05:00
|
|
|
if len(args) != 2:
|
2020-04-19 02:14:01 -04:00
|
|
|
print("usage: %s diff [--error-threshold <x>] <out1> <out2>" % sys.argv[0])
|
2020-01-13 01:05:46 -05:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
data1 = read_build_log(args[0])
|
|
|
|
data2 = read_build_log(args[1])
|
|
|
|
|
2020-04-19 02:14:01 -04:00
|
|
|
max_delta = None
|
2020-01-13 01:05:46 -05:00
|
|
|
for key, value1 in data1.items():
|
|
|
|
value2 = data2[key]
|
|
|
|
for port in port_data.values():
|
2020-02-26 23:36:53 -05:00
|
|
|
if key == "ports/{}/{}".format(port.dir, port.output):
|
2020-01-13 01:05:46 -05:00
|
|
|
name = port.name
|
|
|
|
break
|
|
|
|
data = [v2 - v1 for v1, v2 in zip(value1, value2)]
|
2020-02-26 23:36:53 -05:00
|
|
|
warn = ""
|
|
|
|
board = re.search(r"/build-([A-Za-z0-9_]+)/", key)
|
2020-01-13 01:05:46 -05:00
|
|
|
if board:
|
|
|
|
board = board.group(1)
|
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
board = ""
|
|
|
|
if name == "cc3200":
|
2020-01-13 01:05:46 -05:00
|
|
|
delta = data[0]
|
|
|
|
percent = 100 * delta / value1[0]
|
|
|
|
if data[1] != 0:
|
2020-02-26 23:36:53 -05:00
|
|
|
warn += " %+u(data)" % data[1]
|
2020-01-13 01:05:46 -05:00
|
|
|
else:
|
|
|
|
delta = data[3]
|
|
|
|
percent = 100 * delta / value1[3]
|
|
|
|
if data[1] != 0:
|
2020-02-26 23:36:53 -05:00
|
|
|
warn += " %+u(data)" % data[1]
|
2020-01-13 01:05:46 -05:00
|
|
|
if data[2] != 0:
|
2020-02-26 23:36:53 -05:00
|
|
|
warn += " %+u(bss)" % data[2]
|
2020-01-13 01:05:46 -05:00
|
|
|
if warn:
|
2020-02-26 23:36:53 -05:00
|
|
|
warn = "[incl%s]" % warn
|
|
|
|
print("%11s: %+5u %+.3f%% %s%s" % (name, delta, percent, board, warn))
|
2020-04-19 02:14:01 -04:00
|
|
|
max_delta = delta if max_delta is None else max(max_delta, delta)
|
|
|
|
|
|
|
|
if error_threshold is not None and max_delta is not None:
|
|
|
|
if max_delta > error_threshold:
|
|
|
|
sys.exit(1)
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2020-01-13 01:05:46 -05:00
|
|
|
|
|
|
|
def do_clean(args):
|
|
|
|
"""Clean ports."""
|
|
|
|
|
|
|
|
ports = parse_port_list(args)
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
print("CLEANING")
|
2020-01-13 01:05:46 -05:00
|
|
|
for port in ports:
|
2020-02-26 23:36:53 -05:00
|
|
|
syscmd("make", "-C", "ports/{}".format(port.dir), port.make_flags, "clean")
|
|
|
|
|
2020-01-13 01:05:46 -05:00
|
|
|
|
|
|
|
def do_build(args):
|
|
|
|
"""Build ports and print firmware sizes."""
|
|
|
|
|
|
|
|
ports = parse_port_list(args)
|
|
|
|
|
2020-04-19 07:09:22 -04:00
|
|
|
if any(port.needs_mpy_cross for port in ports):
|
|
|
|
print("BUILDING MPY-CROSS")
|
|
|
|
syscmd("make", "-C", "mpy-cross", MAKE_FLAGS)
|
2020-01-13 01:05:46 -05:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
print("BUILDING PORTS")
|
2020-01-13 01:05:46 -05:00
|
|
|
for port in ports:
|
2020-02-26 23:36:53 -05:00
|
|
|
syscmd("make", "-C", "ports/{}".format(port.dir), MAKE_FLAGS, port.make_flags)
|
2020-01-13 01:05:46 -05:00
|
|
|
|
|
|
|
do_sizes(args)
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2020-01-13 01:05:46 -05:00
|
|
|
def do_sizes(args):
|
|
|
|
"""Compute and print sizes of firmware."""
|
|
|
|
|
|
|
|
ports = parse_port_list(args)
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
print("COMPUTING SIZES")
|
2020-01-13 01:05:46 -05:00
|
|
|
for port in ports:
|
2020-02-26 23:36:53 -05:00
|
|
|
syscmd("size", "ports/{}/{}".format(port.dir, port.output))
|
|
|
|
|
2020-01-13 01:05:46 -05:00
|
|
|
|
|
|
|
def main():
|
|
|
|
# Get command to execute
|
|
|
|
if len(sys.argv) == 1:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("Available commands:")
|
2020-01-13 01:05:46 -05:00
|
|
|
for cmd in globals():
|
2020-02-26 23:36:53 -05:00
|
|
|
if cmd.startswith("do_"):
|
|
|
|
print(" {:9} {}".format(cmd[3:], globals()[cmd].__doc__))
|
2020-01-13 01:05:46 -05:00
|
|
|
sys.exit(1)
|
|
|
|
cmd = sys.argv.pop(1)
|
|
|
|
|
|
|
|
# Dispatch to desired command
|
|
|
|
try:
|
2020-02-26 23:36:53 -05:00
|
|
|
cmd = globals()["do_{}".format(cmd)]
|
2020-01-13 01:05:46 -05:00
|
|
|
except KeyError:
|
|
|
|
print("{}: unknown command '{}'".format(sys.argv[0], cmd))
|
|
|
|
sys.exit(1)
|
|
|
|
cmd(sys.argv[1:])
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-01-13 01:05:46 -05:00
|
|
|
main()
|