From f06c002123b529b14535f2de1a1648140d49c5ea Mon Sep 17 00:00:00 2001 From: Kristoffer Richardsson Date: Wed, 13 May 2020 22:56:25 +0200 Subject: [PATCH] #546 Pretty print of memory usage at the end of build --- Makefile | 5 ++++- tools/make/size.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100755 tools/make/size.py diff --git a/Makefile b/Makefile index 9f318ce0b2..57fd9b0572 100644 --- a/Makefile +++ b/Makefile @@ -85,6 +85,9 @@ CFLAGS += -DSTM32F4XX -DSTM32F40_41xxx -DHSE_VALUE=8000000 -DUSE_STDPERIPH_DRIVE LOAD_ADDRESS_stm32f4 = 0x8000000 LOAD_ADDRESS_CLOAD_stm32f4 = 0x8004000 +MEM_SIZE_FLASH_K = 1008 +MEM_SIZE_RAM_K = 128 +MEM_SIZE_CCM_K = 64 endif ################ Build configuration ################## @@ -393,7 +396,7 @@ ifeq ($(FATFS_DISKIO_TESTS), 1) endif size: - @$(SIZE) -B $(PROG).elf + @$(PYTHON) $(CRAZYFLIE_BASE)/tools/make/size.py $(SIZE) $(PROG).elf $(MEM_SIZE_FLASH_K) $(MEM_SIZE_RAM_K) $(MEM_SIZE_CCM_K) #Radio bootloader cload: diff --git a/tools/make/size.py b/tools/make/size.py new file mode 100755 index 0000000000..88805bd7ed --- /dev/null +++ b/tools/make/size.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python + +import argparse +import subprocess + +# Calls the size progeam and prints pretty memory usage + +def check_output(*args): + """A wrapper for subprocess.check_output() to handle differences in python 2 and 3. + Returns a string. + """ + result = subprocess.check_output(*args) + + if isinstance(result, bytes): + return result.decode('utf-8') + else: + return result + + +if __name__ == "__main__": + + parser = argparse.ArgumentParser() + parser.add_argument("size_app", help="path to the size program") + parser.add_argument("source", help=".elf file to use") + parser.add_argument("flash_size_k", help="size of the flash, in k bytes", type=int) + parser.add_argument("ram_size_k", help="size of the RAM, in k bytes", type=int) + parser.add_argument("ccm_size_k", help="size of the CCM, in k bytes", type=int) + args = parser.parse_args() + + output = check_output([args.size_app, '-A', args.source]) + sizes = {} + for line in output.splitlines(): + parts = line.split() + if len(parts) == 3 and parts[0] != 'section': + sizes[parts[0]] = int(parts[1]) + + flash_available = args.flash_size_k * 1024 + flash_used = sizes['.text'] + sizes['.data'] + sizes['.ccmdata'] + flash_free = flash_available - flash_used + flash_fill = 100 * flash_used / flash_available + + ram_available = args.ram_size_k * 1024 + ram_used = sizes['.bss'] + sizes['.data'] + ram_free = ram_available - ram_used + ram_fill = 100 * ram_used / ram_available + + ccm_available = args.ccm_size_k * 1024 + ccm_used = sizes['.ccmbss'] + sizes['.ccmdata'] + ccm_free = ccm_available - ccm_used + ccm_fill = 100 * ccm_used / ccm_available + + print("Flash | {:7d}/{:<7d} ({:2.0f}%), {:7d} free | text: {}, data: {}, ccmdata: {}".format(flash_used, flash_available, flash_fill, flash_free, sizes['.text'], sizes['.data'], sizes['.ccmdata'])) + print("RAM | {:7d}/{:<7d} ({:2.0f}%), {:7d} free | bss: {}, data: {}".format(ram_used, ram_available, ram_fill, ram_free, sizes['.bss'], sizes['.data'])) + print("CCM | {:7d}/{:<7d} ({:2.0f}%), {:7d} free | ccmbss: {}, ccmdata: {}".format(ccm_used, ccm_available, ccm_fill, ccm_free, sizes['.ccmbss'], sizes['.ccmdata']))