Skip to content

Commit

Permalink
#546 Pretty print of memory usage at the end of build
Browse files Browse the repository at this point in the history
  • Loading branch information
krichardsson committed May 14, 2020
1 parent 7f95e7f commit f06c002
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ##################
Expand Down Expand Up @@ -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:
Expand Down
54 changes: 54 additions & 0 deletions tools/make/size.py
Original file line number Diff line number Diff line change
@@ -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']))

0 comments on commit f06c002

Please sign in to comment.