From f1bb43abb5881debadecad1b61978488b421a057 Mon Sep 17 00:00:00 2001 From: rusefi Date: Sat, 30 Mar 2019 10:20:08 -0400 Subject: [PATCH] generate_memory_usage_report/gcc_map_reader BUG? #721 unit test showing the issue --- .../src/rusefi/GccMapReader.java | 32 ++++++------ .../src/rusefi/GccMapReaderTest.java | 51 ++++++++++++++++++- 2 files changed, 67 insertions(+), 16 deletions(-) diff --git a/java_tools/gcc_map_reader/src/rusefi/GccMapReader.java b/java_tools/gcc_map_reader/src/rusefi/GccMapReader.java index 5a119840c0..2f65214a5c 100644 --- a/java_tools/gcc_map_reader/src/rusefi/GccMapReader.java +++ b/java_tools/gcc_map_reader/src/rusefi/GccMapReader.java @@ -12,13 +12,14 @@ import java.util.regex.Pattern; * This is an utility to print the information from GCC linked .map file * * @author Andrey Belomutskiy - * 10/16/13 + * 10/16/13 */ public class GccMapReader { - private static final Pattern p2 = Pattern.compile(".*0x(\\S*)(.*)"); + private static final Pattern MULTI_LINE_PATTERN = Pattern.compile(".*0x(\\S*)(.*)"); + private static final Pattern SINGLE_LINE_PATTERN = Pattern.compile(".*\\.bss\\.(\\S*).*0x.*0x(\\S*)(.*)"); public static void main(String[] args) throws IOException { - if (args.length!=1) { + if (args.length != 1) { System.err.println("file name parameter expected"); System.exit(-1); } @@ -26,7 +27,7 @@ public class GccMapReader { BufferedReader fr = new BufferedReader(new FileReader(fileName)); String line; - List lines = new ArrayList(); + List lines = new ArrayList<>(); while ((line = fr.readLine()) != null) lines.add(line); @@ -35,12 +36,7 @@ public class GccMapReader { List records = process(lines); - Collections.sort(records, new Comparator() { - @Override - public int compare(Record o1, Record o2) { - return o2.compareTo(o1); - } - }); + Collections.sort(records, Comparator.reverseOrder()); int totalSize = 0; for (Record record : records) { @@ -51,9 +47,7 @@ public class GccMapReader { System.out.println("Total size: " + totalSize); } - private static List process(List lines) { - Pattern p1 = Pattern.compile(".*\\.bss\\.(\\S*).*0x.*0x(\\S*)(.*)"); - + static List process(List lines) { List result = new ArrayList(); for (int i = 0; i < lines.size(); i++) { @@ -62,7 +56,7 @@ public class GccMapReader { continue; debug(line); - Matcher m1 = p1.matcher(line); + Matcher m1 = SINGLE_LINE_PATTERN.matcher(line); if (m1.matches()) { parseSingleLine(result, line, m1, i); @@ -78,7 +72,7 @@ public class GccMapReader { String suffix = line; line = lines.get(++lineIndex); - Matcher m2 = p2.matcher(line); + Matcher m2 = MULTI_LINE_PATTERN.matcher(line); if (!m2.matches()) { debug("Skipping " + line); @@ -161,5 +155,13 @@ public class GccMapReader { ", name='" + name + '\'' + '}'; } + + public int getSize() { + return size; + } + + public String getName() { + return name; + } } } diff --git a/java_tools/gcc_map_reader/src/rusefi/GccMapReaderTest.java b/java_tools/gcc_map_reader/src/rusefi/GccMapReaderTest.java index c0a0f2e639..e0bf44e890 100644 --- a/java_tools/gcc_map_reader/src/rusefi/GccMapReaderTest.java +++ b/java_tools/gcc_map_reader/src/rusefi/GccMapReaderTest.java @@ -2,9 +2,58 @@ package rusefi; import org.junit.Test; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + public class GccMapReaderTest { @Test - public void testMapFile() { + public void testSingleLine() { + List r = GccMapReader.process(Collections.singletonList(" .bss.PWMD1 0x1fff9a0c 0x18 build_kinetis/obj/hal_pwm_lld.o")); + assertNotNull(r); + assertEquals(1, r.size()); + assertEquals(0x18, r.get(0).getSize()); + } + @Test + public void testMultiLine() { + List r = GccMapReader.process(Arrays.asList( + " .bss._ZL12turnOffEvent", + "0x1fff9db8 0x60 build_kinetis/obj/aux_valves.o")); + assertNotNull(r); + assertEquals(1, r.size()); + assertEquals(0x60, r.get(0).getSize()); + } + + @Test + public void testThreeLine() { + List r = GccMapReader.process(Arrays.asList( + " .bss.ch_idle_thread_wa", + " 0x1fff8d10 0x610 build_kinetis/obj/chsys.o", + " 0x1fff8d10 ch_idle_thread_wa")); + assertNotNull(r); + assertEquals(1, r.size()); + assertEquals(0x610, r.get(0).getSize()); + } + + @Test + public void testIssue() { + List r = GccMapReader.process(Arrays.asList( + ".bss 0x1fff8d10 0xf2e4 load address 0x0002b1bc", + " 0x1fff8d10 . = ALIGN (0x4)", + " 0x1fff8d10 _bss_start = .", + " *(.bss)", + " *(.bss.*)", + " .bss.ch_idle_thread_wa", + " 0x1fff8d10 0x610 build_kinetis/obj/chsys.o", + " 0x1fff8d10 ch_idle_thread_wa", + " .bss.ch 0x1fff9320 0x80 build_kinetis/obj/chschd.o", + " 0x1fff9320 ch")); + assertNotNull(r); + assertEquals(1, r.size()); + assertEquals(0x80, r.get(0).getSize()); } }