From d767e7d7711e440cd188d3df88166837df604e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Mon, 16 Dec 2024 06:46:37 +0100 Subject: [PATCH] Add an option to print the java memory before/after mojo execution (cherry picked from commit d8237000a14609a3b4a14f7625703f74fe3ff758) --- .../tycho/helper/MojoExecutionHelper.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tycho-spi/src/main/java/org/eclipse/tycho/helper/MojoExecutionHelper.java b/tycho-spi/src/main/java/org/eclipse/tycho/helper/MojoExecutionHelper.java index 62ca69bbe5..f54f8d87f2 100644 --- a/tycho-spi/src/main/java/org/eclipse/tycho/helper/MojoExecutionHelper.java +++ b/tycho-spi/src/main/java/org/eclipse/tycho/helper/MojoExecutionHelper.java @@ -19,25 +19,41 @@ import org.apache.maven.plugin.MojoExecution; import org.apache.maven.plugin.MojoExecutionException; import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.component.annotations.Requirement; +import org.codehaus.plexus.logging.Logger; @Component(role = MojoExecutionListener.class, hint = "helper") public class MojoExecutionHelper implements MojoExecutionListener { + private static boolean printMemoryInfo = Boolean.getBoolean("tycho.printMemory"); + private static boolean gc = Boolean.getBoolean("tycho.printMemory.gc"); private static final ThreadLocal EVENT = new ThreadLocal(); + @Requirement + private Logger logger; + @Override public void beforeMojoExecution(MojoExecutionEvent event) throws MojoExecutionException { EVENT.set(event); + if (printMemoryInfo) { + printMemory("Before", gc, event); + } } @Override public void afterMojoExecutionSuccess(MojoExecutionEvent event) throws MojoExecutionException { EVENT.remove(); + if (printMemoryInfo) { + printMemory("After", gc, event); + } } @Override public void afterExecutionFailure(MojoExecutionEvent event) { EVENT.remove(); + if (printMemoryInfo) { + printMemory("After", gc, event); + } } public static Optional getExecution() { @@ -48,4 +64,50 @@ public static Optional getExecution() { return Optional.ofNullable(executionEvent.getExecution()); } + private void printMemory(String state, boolean gc, MojoExecutionEvent evt) { + Runtime runtime = Runtime.getRuntime(); + double usedBefore; + if (gc) { + usedBefore = (runtime.totalMemory() - runtime.freeMemory()) / 1024 / 1024; + System.gc(); + } else { + usedBefore = 0; + } + double totalMemory = runtime.totalMemory() / 1024 / 1024; + double freeMemory = runtime.freeMemory() / 1024 / 1024; + double usedMemory = totalMemory - freeMemory; + StringBuilder builder = new StringBuilder(); + builder.append("---- "); + builder.append(state); + builder.append(" "); + builder.append(evt.getProject().getId()); + builder.append(" - "); + MojoExecution execution = evt.getExecution(); + builder.append(execution.getMojoDescriptor().getId()); + builder.append(" ["); + builder.append(execution.getExecutionId()); + builder.append("] ----"); + builder.append(System.lineSeparator()); + builder.append("Total Memory: "); + builder.append(String.format("%.2f", totalMemory)); + builder.append("mb"); + builder.append(System.lineSeparator()); + builder.append("Free Memory: "); + builder.append(String.format("%.2f", freeMemory)); + builder.append("mb"); + builder.append(System.lineSeparator()); + builder.append("Used Memory: "); + builder.append(String.format("%.2f", usedMemory)); + builder.append("mb"); + builder.append(System.lineSeparator()); + double reclaimed = usedBefore - usedMemory; + if (reclaimed > 0) { + builder.append("GC Reclaimed: "); + builder.append(String.format("%.2f", reclaimed)); + builder.append("mb"); + builder.append(System.lineSeparator()); + } + logger.info(builder.toString()); + } + }