From 9ac6ba45fdb78076e6382a662e50ef4db6e2872c Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Tue, 27 Aug 2024 11:42:05 +0200 Subject: [PATCH 1/7] runtime & memory usage to body interceptors --- .../src/main/java/sootup/core/util/Utils.java | 47 +++++++++++++++++-- .../bytecode/RuntimeJarConversionTests.java | 21 ++++++++- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/util/Utils.java b/sootup.core/src/main/java/sootup/core/util/Utils.java index a32a8c99849..8b5dc98051a 100644 --- a/sootup.core/src/main/java/sootup/core/util/Utils.java +++ b/sootup.core/src/main/java/sootup/core/util/Utils.java @@ -27,10 +27,8 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.*; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -70,6 +68,47 @@ public static List wrapEachBodyInterceptorWith( return interceptors; } + /** + * e.g. to measure Runtime (Time and Memory Usage) of every interceptor + */ + public static AbstractMap.SimpleEntry, List> wrapEachBodyInterceptorWithPerformance( + @Nonnull List bodyInterceptors) { + Map runtimeMap = new HashMap<>(); + List interceptors = new ArrayList<>(bodyInterceptors.size() * 2 + 1); + bodyInterceptors.stream() + .map( + b -> { + AtomicReference duration = new AtomicReference<>(0L); + // Initialize the duration for each interceptor + runtimeMap.putIfAbsent(b, 0L); + BodyInterceptor bodyInterceptor = (builder, view) -> { + System.out.println(builder.getMethodSignature()); + Long startTime = System.currentTimeMillis(); // Start time + Runtime runtime = Runtime.getRuntime(); + Integer dataSize = 1024 * 1024; + Long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory(); + //System.out.println("Used Memory before" + usedMemoryBefore/dataSize + " MB"); + try { + b.interceptBody(builder, view); + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + Long endTime = System.currentTimeMillis(); // End time + duration.set(endTime - startTime); // Calculate duration + System.out.println("Interceptor " + b.getClass().getSimpleName() + " took " + duration + " ms."); + Long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory(); + System.out.println("Interceptor " + b.getClass().getSimpleName() + " used memory: " + (usedMemoryAfter - usedMemoryBefore) / dataSize + " MB"); + runtimeMap.merge(b, duration.get(), Long::sum); + //System.out.println(runtimeMap); + } + }; + return bodyInterceptor; + }) + .forEach(interceptors::add); + AbstractMap.SimpleEntry, List> res = new AbstractMap.SimpleEntry<>(runtimeMap, interceptors); + return res; + } + List compileJavaOTF(String className, String javaSourceContent) { File sourceFile; try { diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java index 63b933f07cc..9e5489e688d 100644 --- a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java @@ -3,9 +3,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.util.Collections; -import java.util.List; +import java.util.*; import java.util.function.BiFunction; +import java.util.stream.Collectors; + +import com.google.common.collect.Lists; +import org.apache.commons.lang3.tuple.Pair; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @@ -17,9 +20,11 @@ import sootup.core.util.DotExporter; import sootup.core.util.Utils; import sootup.java.bytecode.inputlocation.DefaultRTJarAnalysisInputLocation; +import sootup.java.bytecode.inputlocation.JavaClassPathAnalysisInputLocation; import sootup.java.core.interceptors.BytecodeBodyInterceptors; import sootup.java.core.interceptors.CopyPropagator; import sootup.java.core.interceptors.DeadAssignmentEliminator; +import sootup.java.core.interceptors.TypeAssigner; import sootup.java.core.views.JavaView; @Tag("Java8") @@ -121,4 +126,16 @@ public void testExample() { /* Example to start quickly */ convertMethod(""); } + + @Test + public void runTimeOfBodyInterceptorOnJar() { + //Note: mrjar.jar used just for test purpose, you can put any jar file. + String baseDir = "../shared-test-resources/multi-release-jar/mrjar.jar"; + //List bodyInterceptors = BytecodeBodyInterceptors.Default.getBodyInterceptors(); + List bodyInterceptors = Collections.singletonList(new TypeAssigner()); + AbstractMap.SimpleEntry, List> result = Utils.wrapEachBodyInterceptorWithPerformance(bodyInterceptors); + AnalysisInputLocation inputLocation = new JavaClassPathAnalysisInputLocation(baseDir, SourceType.Library, result.getValue()); + JavaView view = new JavaView(inputLocation); + view.getClasses().forEach(javaSootClass -> javaSootClass.getMethods().forEach(SootMethod::getBody)); + } } From dc92f549c9b6df2af33cd729396337c2dddb5c20 Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Tue, 27 Aug 2024 13:06:35 +0200 Subject: [PATCH 2/7] format style --- .../src/main/java/sootup/core/util/Utils.java | 64 +++++++++++-------- .../bytecode/RuntimeJarConversionTests.java | 24 +++---- 2 files changed, 49 insertions(+), 39 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/util/Utils.java b/sootup.core/src/main/java/sootup/core/util/Utils.java index 8b5dc98051a..e9b0f206d61 100644 --- a/sootup.core/src/main/java/sootup/core/util/Utils.java +++ b/sootup.core/src/main/java/sootup/core/util/Utils.java @@ -68,11 +68,9 @@ public static List wrapEachBodyInterceptorWith( return interceptors; } - /** - * e.g. to measure Runtime (Time and Memory Usage) of every interceptor - */ - public static AbstractMap.SimpleEntry, List> wrapEachBodyInterceptorWithPerformance( - @Nonnull List bodyInterceptors) { + /** e.g. to measure Runtime (Time and Memory Usage) of every interceptor */ + public static AbstractMap.SimpleEntry, List> + wrapEachBodyInterceptorWithPerformance(@Nonnull List bodyInterceptors) { Map runtimeMap = new HashMap<>(); List interceptors = new ArrayList<>(bodyInterceptors.size() * 2 + 1); bodyInterceptors.stream() @@ -81,31 +79,43 @@ public static AbstractMap.SimpleEntry, List duration = new AtomicReference<>(0L); // Initialize the duration for each interceptor runtimeMap.putIfAbsent(b, 0L); - BodyInterceptor bodyInterceptor = (builder, view) -> { - System.out.println(builder.getMethodSignature()); - Long startTime = System.currentTimeMillis(); // Start time - Runtime runtime = Runtime.getRuntime(); - Integer dataSize = 1024 * 1024; - Long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory(); - //System.out.println("Used Memory before" + usedMemoryBefore/dataSize + " MB"); - try { - b.interceptBody(builder, view); - } catch (Exception e) { - throw new RuntimeException(e); - } finally { - Long endTime = System.currentTimeMillis(); // End time - duration.set(endTime - startTime); // Calculate duration - System.out.println("Interceptor " + b.getClass().getSimpleName() + " took " + duration + " ms."); - Long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory(); - System.out.println("Interceptor " + b.getClass().getSimpleName() + " used memory: " + (usedMemoryAfter - usedMemoryBefore) / dataSize + " MB"); - runtimeMap.merge(b, duration.get(), Long::sum); - //System.out.println(runtimeMap); - } - }; + BodyInterceptor bodyInterceptor = + (builder, view) -> { + System.out.println(builder.getMethodSignature()); + Long startTime = System.currentTimeMillis(); // Start time + Runtime runtime = Runtime.getRuntime(); + Integer dataSize = 1024 * 1024; + Long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory(); + // System.out.println("Used Memory before" + usedMemoryBefore/dataSize + " MB"); + try { + b.interceptBody(builder, view); + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + Long endTime = System.currentTimeMillis(); // End time + duration.set(endTime - startTime); // Calculate duration + System.out.println( + "Interceptor " + + b.getClass().getSimpleName() + + " took " + + duration + + " ms."); + Long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory(); + System.out.println( + "Interceptor " + + b.getClass().getSimpleName() + + " used memory: " + + (usedMemoryAfter - usedMemoryBefore) / dataSize + + " MB"); + runtimeMap.merge(b, duration.get(), Long::sum); + // System.out.println(runtimeMap); + } + }; return bodyInterceptor; }) .forEach(interceptors::add); - AbstractMap.SimpleEntry, List> res = new AbstractMap.SimpleEntry<>(runtimeMap, interceptors); + AbstractMap.SimpleEntry, List> res = + new AbstractMap.SimpleEntry<>(runtimeMap, interceptors); return res; } diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java index 9e5489e688d..1da198770b1 100644 --- a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java @@ -5,10 +5,6 @@ import java.util.*; import java.util.function.BiFunction; -import java.util.stream.Collectors; - -import com.google.common.collect.Lists; -import org.apache.commons.lang3.tuple.Pair; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @@ -129,13 +125,17 @@ public void testExample() { @Test public void runTimeOfBodyInterceptorOnJar() { - //Note: mrjar.jar used just for test purpose, you can put any jar file. - String baseDir = "../shared-test-resources/multi-release-jar/mrjar.jar"; - //List bodyInterceptors = BytecodeBodyInterceptors.Default.getBodyInterceptors(); - List bodyInterceptors = Collections.singletonList(new TypeAssigner()); - AbstractMap.SimpleEntry, List> result = Utils.wrapEachBodyInterceptorWithPerformance(bodyInterceptors); - AnalysisInputLocation inputLocation = new JavaClassPathAnalysisInputLocation(baseDir, SourceType.Library, result.getValue()); - JavaView view = new JavaView(inputLocation); - view.getClasses().forEach(javaSootClass -> javaSootClass.getMethods().forEach(SootMethod::getBody)); + // Note: mrjar.jar used just for test purpose, you can put any jar file. + String baseDir = "../shared-test-resources/multi-release-jar/mrjar.jar"; + // List bodyInterceptors = + // BytecodeBodyInterceptors.Default.getBodyInterceptors(); + List bodyInterceptors = Collections.singletonList(new TypeAssigner()); + AbstractMap.SimpleEntry, List> result = + Utils.wrapEachBodyInterceptorWithPerformance(bodyInterceptors); + AnalysisInputLocation inputLocation = + new JavaClassPathAnalysisInputLocation(baseDir, SourceType.Library, result.getValue()); + JavaView view = new JavaView(inputLocation); + view.getClasses() + .forEach(javaSootClass -> javaSootClass.getMethods().forEach(SootMethod::getBody)); } } From 421abf33f4746b00292856cce2fc01afe0d570e6 Mon Sep 17 00:00:00 2001 From: Palaniappan Muthuraman Date: Tue, 27 Aug 2024 16:58:06 +0200 Subject: [PATCH 3/7] Refactored code --- .../src/main/java/sootup/core/util/Utils.java | 86 +++++++++---------- 1 file changed, 39 insertions(+), 47 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/util/Utils.java b/sootup.core/src/main/java/sootup/core/util/Utils.java index e9b0f206d61..6112c37f804 100644 --- a/sootup.core/src/main/java/sootup/core/util/Utils.java +++ b/sootup.core/src/main/java/sootup/core/util/Utils.java @@ -28,7 +28,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; -import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -71,52 +71,44 @@ public static List wrapEachBodyInterceptorWith( /** e.g. to measure Runtime (Time and Memory Usage) of every interceptor */ public static AbstractMap.SimpleEntry, List> wrapEachBodyInterceptorWithPerformance(@Nonnull List bodyInterceptors) { - Map runtimeMap = new HashMap<>(); - List interceptors = new ArrayList<>(bodyInterceptors.size() * 2 + 1); - bodyInterceptors.stream() - .map( - b -> { - AtomicReference duration = new AtomicReference<>(0L); - // Initialize the duration for each interceptor - runtimeMap.putIfAbsent(b, 0L); - BodyInterceptor bodyInterceptor = - (builder, view) -> { - System.out.println(builder.getMethodSignature()); - Long startTime = System.currentTimeMillis(); // Start time - Runtime runtime = Runtime.getRuntime(); - Integer dataSize = 1024 * 1024; - Long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory(); - // System.out.println("Used Memory before" + usedMemoryBefore/dataSize + " MB"); - try { - b.interceptBody(builder, view); - } catch (Exception e) { - throw new RuntimeException(e); - } finally { - Long endTime = System.currentTimeMillis(); // End time - duration.set(endTime - startTime); // Calculate duration - System.out.println( - "Interceptor " - + b.getClass().getSimpleName() - + " took " - + duration - + " ms."); - Long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory(); - System.out.println( - "Interceptor " - + b.getClass().getSimpleName() - + " used memory: " - + (usedMemoryAfter - usedMemoryBefore) / dataSize - + " MB"); - runtimeMap.merge(b, duration.get(), Long::sum); - // System.out.println(runtimeMap); - } - }; - return bodyInterceptor; - }) - .forEach(interceptors::add); - AbstractMap.SimpleEntry, List> res = - new AbstractMap.SimpleEntry<>(runtimeMap, interceptors); - return res; + ConcurrentHashMap runtimeMap = new ConcurrentHashMap<>(); + final int MB = 1024 * 1024; + Runtime runtime = Runtime.getRuntime(); + bodyInterceptors.forEach(b -> runtimeMap.put(b, 0L)); + List interceptors = + bodyInterceptors.stream() + .map( + b -> + (BodyInterceptor) + (builder, view) -> { + System.out.println(builder.getMethodSignature()); + long startTime = System.currentTimeMillis(); + long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory(); + try { + b.interceptBody(builder, view); + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + long duration = System.currentTimeMillis() - startTime; + long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory(); + long memoryUsed = usedMemoryAfter - usedMemoryBefore; + System.out.println( + "Interceptor " + + b.getClass().getSimpleName() + + " took " + + duration + + " ms."); + System.out.println( + "Interceptor " + + b.getClass().getSimpleName() + + " used memory: " + + memoryUsed / MB + + " MB"); + runtimeMap.merge(b, duration, Long::sum); + } + }) + .collect(Collectors.toList()); + return new AbstractMap.SimpleEntry<>(runtimeMap, interceptors); } List compileJavaOTF(String className, String javaSourceContent) { From 86355e7e885256be9bd079614ab60c4f2ed18709 Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Wed, 28 Aug 2024 14:22:33 +0200 Subject: [PATCH 4/7] updated changes --- .../core/transform/BodyInterceptorMetric.java | 52 +++++++++++++ .../transform/RunTimeBodyInterceptor.java | 74 +++++++++++++++++++ .../src/main/java/sootup/core/util/Utils.java | 44 ----------- .../bytecode/RuntimeJarConversionTests.java | 30 ++++++-- 4 files changed, 151 insertions(+), 49 deletions(-) create mode 100644 sootup.core/src/main/java/sootup/core/transform/BodyInterceptorMetric.java create mode 100644 sootup.core/src/main/java/sootup/core/transform/RunTimeBodyInterceptor.java diff --git a/sootup.core/src/main/java/sootup/core/transform/BodyInterceptorMetric.java b/sootup.core/src/main/java/sootup/core/transform/BodyInterceptorMetric.java new file mode 100644 index 00000000000..d61c4f2daa2 --- /dev/null +++ b/sootup.core/src/main/java/sootup/core/transform/BodyInterceptorMetric.java @@ -0,0 +1,52 @@ +package sootup.core.transform; + +/*- + * #%L + * Soot - a J*va Optimization Framework + * %% + * Copyright (C) 2024 Sahil Agichani + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * . + * #L% + */ + +public class BodyInterceptorMetric { + + private Long runtime; + private Long memoryUsage; + + public BodyInterceptorMetric() {} + + public BodyInterceptorMetric(Long runtime, Long memoryUsage) { + this.runtime = runtime; + this.memoryUsage = memoryUsage; + } + + public Long getRuntime() { + return runtime; + } + + public void setRuntime(Long runtime) { + this.runtime = runtime; + } + + public Long getMemoryUsage() { + return memoryUsage; + } + + public void setMemoryUsage(Long memoryUsage) { + this.memoryUsage = memoryUsage; + } +} diff --git a/sootup.core/src/main/java/sootup/core/transform/RunTimeBodyInterceptor.java b/sootup.core/src/main/java/sootup/core/transform/RunTimeBodyInterceptor.java new file mode 100644 index 00000000000..2f100468480 --- /dev/null +++ b/sootup.core/src/main/java/sootup/core/transform/RunTimeBodyInterceptor.java @@ -0,0 +1,74 @@ +package sootup.core.transform; + +/*- + * #%L + * Soot - a J*va Optimization Framework + * %% + * Copyright (C) 2024 Sahil Agichani + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * . + * #L% + */ + +import java.util.HashMap; +import java.util.Map; +import javax.annotation.Nonnull; +import sootup.core.model.Body; +import sootup.core.views.View; + +public class RunTimeBodyInterceptor implements BodyInterceptor { + + private Map biMetricMap = new HashMap<>(); + + private final BodyInterceptor bodyInterceptor; + + public RunTimeBodyInterceptor(BodyInterceptor bodyInterceptor) { + this.bodyInterceptor = bodyInterceptor; + } + + public Map getBiMetricMap() { + return biMetricMap; + } + + public void setBiMetricMap(Map biMetricMap) { + this.biMetricMap = biMetricMap; + } + + public BodyInterceptor getBodyInterceptor() { + return bodyInterceptor; + } + + @Override + public void interceptBody(@Nonnull Body.BodyBuilder builder, @Nonnull View view) { + // Initialise biMetricMap + BodyInterceptorMetric biMetric = new BodyInterceptorMetric(0L, 0L); + biMetricMap.putIfAbsent(bodyInterceptor, biMetric); + long startTime = System.currentTimeMillis(); // Start time + final int MB = 1024 * 1024; + Runtime runtime = Runtime.getRuntime(); + long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory(); + + bodyInterceptor.interceptBody(builder, view); + + long endTime = System.currentTimeMillis(); // End time + long duration = endTime - startTime; + long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory(); + long memoryUsed = (usedMemoryAfter - usedMemoryBefore) / MB; + + biMetric.setRuntime(biMetricMap.get(bodyInterceptor).getRuntime() + duration); + biMetric.setMemoryUsage(biMetricMap.get(bodyInterceptor).getMemoryUsage() + memoryUsed); + biMetricMap.put(bodyInterceptor, biMetric); + } +} diff --git a/sootup.core/src/main/java/sootup/core/util/Utils.java b/sootup.core/src/main/java/sootup/core/util/Utils.java index 6112c37f804..b80ab6c40d8 100644 --- a/sootup.core/src/main/java/sootup/core/util/Utils.java +++ b/sootup.core/src/main/java/sootup/core/util/Utils.java @@ -28,7 +28,6 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; -import java.util.concurrent.ConcurrentHashMap; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -68,49 +67,6 @@ public static List wrapEachBodyInterceptorWith( return interceptors; } - /** e.g. to measure Runtime (Time and Memory Usage) of every interceptor */ - public static AbstractMap.SimpleEntry, List> - wrapEachBodyInterceptorWithPerformance(@Nonnull List bodyInterceptors) { - ConcurrentHashMap runtimeMap = new ConcurrentHashMap<>(); - final int MB = 1024 * 1024; - Runtime runtime = Runtime.getRuntime(); - bodyInterceptors.forEach(b -> runtimeMap.put(b, 0L)); - List interceptors = - bodyInterceptors.stream() - .map( - b -> - (BodyInterceptor) - (builder, view) -> { - System.out.println(builder.getMethodSignature()); - long startTime = System.currentTimeMillis(); - long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory(); - try { - b.interceptBody(builder, view); - } catch (Exception e) { - throw new RuntimeException(e); - } finally { - long duration = System.currentTimeMillis() - startTime; - long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory(); - long memoryUsed = usedMemoryAfter - usedMemoryBefore; - System.out.println( - "Interceptor " - + b.getClass().getSimpleName() - + " took " - + duration - + " ms."); - System.out.println( - "Interceptor " - + b.getClass().getSimpleName() - + " used memory: " - + memoryUsed / MB - + " MB"); - runtimeMap.merge(b, duration, Long::sum); - } - }) - .collect(Collectors.toList()); - return new AbstractMap.SimpleEntry<>(runtimeMap, interceptors); - } - List compileJavaOTF(String className, String javaSourceContent) { File sourceFile; try { diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java index 1da198770b1..034dfc6268d 100644 --- a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java @@ -13,6 +13,7 @@ import sootup.core.model.SootMethod; import sootup.core.model.SourceType; import sootup.core.transform.BodyInterceptor; +import sootup.core.transform.RunTimeBodyInterceptor; import sootup.core.util.DotExporter; import sootup.core.util.Utils; import sootup.java.bytecode.inputlocation.DefaultRTJarAnalysisInputLocation; @@ -123,19 +124,38 @@ public void testExample() { convertMethod(""); } + /** e.g. to measure Runtime (Time and Memory Usage) of every interceptor */ @Test public void runTimeOfBodyInterceptorOnJar() { // Note: mrjar.jar used just for test purpose, you can put any jar file. String baseDir = "../shared-test-resources/multi-release-jar/mrjar.jar"; - // List bodyInterceptors = + // List bodyInterceptorsList = // BytecodeBodyInterceptors.Default.getBodyInterceptors(); - List bodyInterceptors = Collections.singletonList(new TypeAssigner()); - AbstractMap.SimpleEntry, List> result = - Utils.wrapEachBodyInterceptorWithPerformance(bodyInterceptors); + List bodyInterceptorsList = Collections.singletonList(new TypeAssigner()); + List runTimeBodyInterceptorsList = new ArrayList<>(); + for (BodyInterceptor bodyInterceptor : bodyInterceptorsList) { + RunTimeBodyInterceptor runTimeBodyInterceptor = new RunTimeBodyInterceptor(bodyInterceptor); + runTimeBodyInterceptorsList.add(runTimeBodyInterceptor); + } AnalysisInputLocation inputLocation = - new JavaClassPathAnalysisInputLocation(baseDir, SourceType.Library, result.getValue()); + new JavaClassPathAnalysisInputLocation( + baseDir, SourceType.Library, Collections.unmodifiableList(runTimeBodyInterceptorsList)); JavaView view = new JavaView(inputLocation); view.getClasses() .forEach(javaSootClass -> javaSootClass.getMethods().forEach(SootMethod::getBody)); + runTimeBodyInterceptorsList.forEach( + runTimeBodyInterceptor -> { + runTimeBodyInterceptor + .getBiMetricMap() + .forEach( + (key, value) -> + System.out.println( + key + + " " + + value.getRuntime() + + " ms and " + + value.getMemoryUsage() + + " MB")); + }); } } From 62de894d9cc933753e35aeeba22270d51ace9a2d Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Thu, 29 Aug 2024 17:11:34 +0200 Subject: [PATCH 5/7] Long to primitive, removed Map --- .../core/transform/BodyInterceptorMetric.java | 14 +++++----- .../transform/RunTimeBodyInterceptor.java | 20 +++++--------- .../bytecode/RuntimeJarConversionTests.java | 26 ++++++++++--------- 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/transform/BodyInterceptorMetric.java b/sootup.core/src/main/java/sootup/core/transform/BodyInterceptorMetric.java index d61c4f2daa2..a1cbd1c5399 100644 --- a/sootup.core/src/main/java/sootup/core/transform/BodyInterceptorMetric.java +++ b/sootup.core/src/main/java/sootup/core/transform/BodyInterceptorMetric.java @@ -24,29 +24,29 @@ public class BodyInterceptorMetric { - private Long runtime; - private Long memoryUsage; + private long runtime; + private long memoryUsage; public BodyInterceptorMetric() {} - public BodyInterceptorMetric(Long runtime, Long memoryUsage) { + public BodyInterceptorMetric(long runtime, long memoryUsage) { this.runtime = runtime; this.memoryUsage = memoryUsage; } - public Long getRuntime() { + public long getRuntime() { return runtime; } - public void setRuntime(Long runtime) { + public void setRuntime(long runtime) { this.runtime = runtime; } - public Long getMemoryUsage() { + public long getMemoryUsage() { return memoryUsage; } - public void setMemoryUsage(Long memoryUsage) { + public void setMemoryUsage(long memoryUsage) { this.memoryUsage = memoryUsage; } } diff --git a/sootup.core/src/main/java/sootup/core/transform/RunTimeBodyInterceptor.java b/sootup.core/src/main/java/sootup/core/transform/RunTimeBodyInterceptor.java index 2f100468480..a60a387aa47 100644 --- a/sootup.core/src/main/java/sootup/core/transform/RunTimeBodyInterceptor.java +++ b/sootup.core/src/main/java/sootup/core/transform/RunTimeBodyInterceptor.java @@ -22,15 +22,13 @@ * #L% */ -import java.util.HashMap; -import java.util.Map; import javax.annotation.Nonnull; import sootup.core.model.Body; import sootup.core.views.View; public class RunTimeBodyInterceptor implements BodyInterceptor { - private Map biMetricMap = new HashMap<>(); + private BodyInterceptorMetric biMetric = new BodyInterceptorMetric(0L, 0L); private final BodyInterceptor bodyInterceptor; @@ -38,12 +36,12 @@ public RunTimeBodyInterceptor(BodyInterceptor bodyInterceptor) { this.bodyInterceptor = bodyInterceptor; } - public Map getBiMetricMap() { - return biMetricMap; + public BodyInterceptorMetric getBiMetric() { + return biMetric; } - public void setBiMetricMap(Map biMetricMap) { - this.biMetricMap = biMetricMap; + public void setBiMetric(BodyInterceptorMetric biMetric) { + this.biMetric = biMetric; } public BodyInterceptor getBodyInterceptor() { @@ -52,9 +50,6 @@ public BodyInterceptor getBodyInterceptor() { @Override public void interceptBody(@Nonnull Body.BodyBuilder builder, @Nonnull View view) { - // Initialise biMetricMap - BodyInterceptorMetric biMetric = new BodyInterceptorMetric(0L, 0L); - biMetricMap.putIfAbsent(bodyInterceptor, biMetric); long startTime = System.currentTimeMillis(); // Start time final int MB = 1024 * 1024; Runtime runtime = Runtime.getRuntime(); @@ -67,8 +62,7 @@ public void interceptBody(@Nonnull Body.BodyBuilder builder, @Nonnull View view) long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory(); long memoryUsed = (usedMemoryAfter - usedMemoryBefore) / MB; - biMetric.setRuntime(biMetricMap.get(bodyInterceptor).getRuntime() + duration); - biMetric.setMemoryUsage(biMetricMap.get(bodyInterceptor).getMemoryUsage() + memoryUsed); - biMetricMap.put(bodyInterceptor, biMetric); + biMetric.setRuntime(biMetric.getRuntime() + duration); + biMetric.setMemoryUsage(biMetric.getMemoryUsage() + memoryUsed); } } diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java index 034dfc6268d..bc46ef82766 100644 --- a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java @@ -13,6 +13,7 @@ import sootup.core.model.SootMethod; import sootup.core.model.SourceType; import sootup.core.transform.BodyInterceptor; +import sootup.core.transform.BodyInterceptorMetric; import sootup.core.transform.RunTimeBodyInterceptor; import sootup.core.util.DotExporter; import sootup.core.util.Utils; @@ -131,7 +132,8 @@ public void runTimeOfBodyInterceptorOnJar() { String baseDir = "../shared-test-resources/multi-release-jar/mrjar.jar"; // List bodyInterceptorsList = // BytecodeBodyInterceptors.Default.getBodyInterceptors(); - List bodyInterceptorsList = Collections.singletonList(new TypeAssigner()); + List bodyInterceptorsList = + Arrays.asList(new TypeAssigner(), new CopyPropagator()); List runTimeBodyInterceptorsList = new ArrayList<>(); for (BodyInterceptor bodyInterceptor : bodyInterceptorsList) { RunTimeBodyInterceptor runTimeBodyInterceptor = new RunTimeBodyInterceptor(bodyInterceptor); @@ -145,17 +147,17 @@ public void runTimeOfBodyInterceptorOnJar() { .forEach(javaSootClass -> javaSootClass.getMethods().forEach(SootMethod::getBody)); runTimeBodyInterceptorsList.forEach( runTimeBodyInterceptor -> { - runTimeBodyInterceptor - .getBiMetricMap() - .forEach( - (key, value) -> - System.out.println( - key - + " " - + value.getRuntime() - + " ms and " - + value.getMemoryUsage() - + " MB")); + BodyInterceptorMetric biMetric = runTimeBodyInterceptor.getBiMetric(); + System.out.println( + runTimeBodyInterceptor.getBodyInterceptor() + + " took " + + biMetric.getRuntime() + + " ms."); + System.out.println( + runTimeBodyInterceptor.getBodyInterceptor() + + " consumed " + + biMetric.getMemoryUsage() + + " MB."); }); } } From 954b29e5105af5b9e9b43921be8b7b6fad90fcb5 Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Thu, 29 Aug 2024 17:34:33 +0200 Subject: [PATCH 6/7] made codecov happy --- .../java/sootup/core/transform/BodyInterceptorMetric.java | 2 -- .../java/sootup/core/transform/RunTimeBodyInterceptor.java | 4 ---- 2 files changed, 6 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/transform/BodyInterceptorMetric.java b/sootup.core/src/main/java/sootup/core/transform/BodyInterceptorMetric.java index a1cbd1c5399..2e8e923c863 100644 --- a/sootup.core/src/main/java/sootup/core/transform/BodyInterceptorMetric.java +++ b/sootup.core/src/main/java/sootup/core/transform/BodyInterceptorMetric.java @@ -27,8 +27,6 @@ public class BodyInterceptorMetric { private long runtime; private long memoryUsage; - public BodyInterceptorMetric() {} - public BodyInterceptorMetric(long runtime, long memoryUsage) { this.runtime = runtime; this.memoryUsage = memoryUsage; diff --git a/sootup.core/src/main/java/sootup/core/transform/RunTimeBodyInterceptor.java b/sootup.core/src/main/java/sootup/core/transform/RunTimeBodyInterceptor.java index a60a387aa47..b9d54d77175 100644 --- a/sootup.core/src/main/java/sootup/core/transform/RunTimeBodyInterceptor.java +++ b/sootup.core/src/main/java/sootup/core/transform/RunTimeBodyInterceptor.java @@ -40,10 +40,6 @@ public BodyInterceptorMetric getBiMetric() { return biMetric; } - public void setBiMetric(BodyInterceptorMetric biMetric) { - this.biMetric = biMetric; - } - public BodyInterceptor getBodyInterceptor() { return bodyInterceptor; } From 27a066affc7ce60f0e21422c28f47d3486623888 Mon Sep 17 00:00:00 2001 From: Palaniappan Muthuraman Date: Thu, 29 Aug 2024 18:45:02 +0200 Subject: [PATCH 7/7] import cleanup --- sootup.core/src/main/java/sootup/core/util/Utils.java | 5 ++++- .../java/sootup/java/bytecode/RuntimeJarConversionTests.java | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/util/Utils.java b/sootup.core/src/main/java/sootup/core/util/Utils.java index b80ab6c40d8..a32a8c99849 100644 --- a/sootup.core/src/main/java/sootup/core/util/Utils.java +++ b/sootup.core/src/main/java/sootup/core/util/Utils.java @@ -27,7 +27,10 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.stream.Collectors; diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java index bc46ef82766..fcf56f32ac0 100644 --- a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java @@ -3,7 +3,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; import java.util.function.BiFunction; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Tag;