bios: print memory usage
authorFranck Jullien <franck.jullien@gmail.com>
Sat, 25 Apr 2020 21:22:38 +0000 (23:22 +0200)
committerFranck Jullien <franck.jullien@gmail.com>
Mon, 27 Apr 2020 14:33:34 +0000 (16:33 +0200)
Print memory usage during the compilation of bios.elf.

litex/soc/software/bios/Makefile
litex/soc/software/memusage.py [new file with mode: 0644]

index 66be372c70c21ef37562d1587155ccb9226046eb..12510a71266454bd567a38fb81a5dd39a2d75774 100755 (executable)
@@ -13,6 +13,7 @@ endif
 OBJECTS=isr.o sdram.o sdcard.o main.o boot-helper-$(CPU).o boot.o
 
 all: bios.bin
+       $(PYTHON) -m litex.soc.software.memusage bios.elf $(CURDIR)/../include/generated/regions.ld $(TRIPLE)
 
 %.bin: %.elf
        $(OBJCOPY) -O binary $< $@
diff --git a/litex/soc/software/memusage.py b/litex/soc/software/memusage.py
new file mode 100644 (file)
index 0000000..3c4dc70
--- /dev/null
@@ -0,0 +1,63 @@
+#!/usr/bin/env python3
+
+# This file is Copyright (c) 2020 Franck Jullien <franck.jullien@gmail.com>
+# License: BSD
+
+import subprocess
+import argparse
+
+def print_usage(bios,regions, triple):
+    rom_usage = 0
+    ram_usage = 0
+
+    readelf = triple + "-readelf"
+
+    result = subprocess.run([readelf, '-e', '-W', bios], stdout=subprocess.PIPE)
+    result = result.stdout.decode('utf-8')
+    result = result.split('\n')
+
+    with open(regions, "r") as regfile:
+        for line in regfile:
+           if line == 0:
+               break
+           if 'rom' in line:
+               rom_size = int(line.split(' ')[-1], 16)
+           if 'sram' in line:
+               ram_size = int(line.split(' ')[-1], 16)
+
+    for line in result:
+        if '.text' in line:
+            if 'PROGBITS' in line:
+                tokens = list(filter(None,line.split(' ')))
+                rom_usage += int(tokens[6], 16)
+        if '.rodata' in line:
+            if 'PROGBITS' in line:
+                tokens = list(filter(None,line.split(' ')))
+                rom_usage += int(tokens[6], 16)
+        if '.data' in line:
+            if 'PROGBITS' in line:
+                tokens = list(filter(None,line.split(' ')))
+                rom_usage += int(tokens[6], 16)
+        if '.commands' in line:
+            if 'PROGBITS' in line:
+                tokens = list(filter(None,line.split(' ')))
+                rom_usage += int(tokens[6], 16)
+        if '.bss' in line:
+            if 'NOBITS' in line:
+                tokens = list(filter(None,line.split(' ')))
+                ram_usage += int(tokens[6], 16)
+
+    print("\nROM usage: {:.2f}KiB \t({:.2f}%)".format(rom_usage / 1024.0, rom_usage / rom_size * 100.0))
+    print("RAM usage: {:.2f}KiB \t({:.2f}%)\n".format(ram_usage / 1024.0, ram_usage / ram_size * 100.0))
+
+def main():
+    parser = argparse.ArgumentParser(description="Print bios memory usage")
+    parser.add_argument("input", help="input file")
+    parser.add_argument("regions", help="regions definitions")
+    parser.add_argument("triple", help="toolchain triple")
+    args = parser.parse_args()
+    print_usage(args.input, args.regions, args.triple)
+
+
+if __name__ == "__main__":
+    main()