diff --git a/DiSLProject2022/src-profiler/ex11/Instrumentation.java b/DiSLProject2022/src-profiler/ex11/Instrumentation.java index 37d0e0b..3863df6 100644 --- a/DiSLProject2022/src-profiler/ex11/Instrumentation.java +++ b/DiSLProject2022/src-profiler/ex11/Instrumentation.java @@ -38,8 +38,7 @@ public class Instrumentation { @Before(marker = BytecodeMarker.class, args = "invokedynamic") static void atObjectRefCallSiteDynamic(final InstructionStaticContext isc, - final MethodStaticContext msc, - final InvocationStaticContext ivc) { + final MethodStaticContext msc) { opCode = isc.getOpcode(); index = isc.getIndex(); caller = msc.thisMethodFullName().concat(msc.thisMethodDescriptor()); @@ -47,11 +46,11 @@ public class Instrumentation { initialized = true; } - @Before(marker = BodyMarker.class, guard = IsNotConstructorOrPrivateMethod.class) + @Before(marker = BodyMarker.class, guard = IsDynamicTargetGuard.class) static void beforeMethod(MethodStaticContext msc) { // we can ignore here constructors and private methods since we know they must be invoked with "invokespecial" - // which performs static binding. Instrumenting the constructor of java.lang.Object would actually cause a - // segmentation fault of the JVM. + // and static methods since they are called with "invokestatic" which performs static binding. + // Instrumenting the constructor of java.lang.Object would actually cause a segmentation fault of the JVM. if (initialized) { Profiler.registerCall(new CallInfo(opCode, index, caller, callee, msc.getUniqueInternalName())); initialized = false; diff --git a/DiSLProject2022/src-profiler/ex11/IsNotConstructorOrPrivateMethod.java b/DiSLProject2022/src-profiler/ex11/IsDynamicTargetGuard.java similarity index 50% rename from DiSLProject2022/src-profiler/ex11/IsNotConstructorOrPrivateMethod.java rename to DiSLProject2022/src-profiler/ex11/IsDynamicTargetGuard.java index 047d1d8..e9c2f98 100644 --- a/DiSLProject2022/src-profiler/ex11/IsNotConstructorOrPrivateMethod.java +++ b/DiSLProject2022/src-profiler/ex11/IsDynamicTargetGuard.java @@ -1,12 +1,11 @@ package ex11; import ch.usi.dag.disl.annotation.GuardMethod; -import ch.usi.dag.disl.staticcontext.ClassStaticContext; import ch.usi.dag.disl.staticcontext.MethodStaticContext; -public class IsNotConstructorOrPrivateMethod { +public class IsDynamicTargetGuard { @GuardMethod - public static boolean guard(final ClassStaticContext csc, final MethodStaticContext msc) { - return !msc.isMethodConstructor() && !msc.isMethodPrivate(); + public static boolean guard(final MethodStaticContext msc) { + return !msc.isMethodConstructor() && !msc.isMethodPrivate() && !msc.isMethodStatic(); } } diff --git a/DiSLProject2022/src-profiler/ex4/Profiler.java b/DiSLProject2022/src-profiler/ex4/Profiler.java index d5712d9..1c0277e 100644 --- a/DiSLProject2022/src-profiler/ex4/Profiler.java +++ b/DiSLProject2022/src-profiler/ex4/Profiler.java @@ -21,8 +21,7 @@ public final class Profiler { public static void countException(Throwable e) { final String exceptionName = e.getClass().getName(); - exceptionNameToCount.computeIfAbsent(exceptionName, k -> new LongAdder()); - exceptionNameToCount.get(exceptionName).increment(); + exceptionNameToCount.computeIfAbsent(exceptionName, k -> new LongAdder()).increment(); } diff --git a/DiSLProject2022/src-profiler/ex5/Profiler.java b/DiSLProject2022/src-profiler/ex5/Profiler.java index 4c1e4b7..5d775cf 100644 --- a/DiSLProject2022/src-profiler/ex5/Profiler.java +++ b/DiSLProject2022/src-profiler/ex5/Profiler.java @@ -9,8 +9,8 @@ public final class Profiler { static { Runtime.getRuntime().addShutdownHook(new Thread(() -> { - System.out.printf("Safe allocation: %d%n", safeAllocations.intValue()); - System.out.printf("Unsafe allocation: %d%n", unsafeAllocation.intValue()); + System.out.printf("Safe allocation: %d%n", safeAllocations.longValue()); + System.out.printf("Unsafe allocation: %d%n", unsafeAllocation.longValue()); })); } diff --git a/DiSLProject2022/src-profiler/ex6/Instrumentation.java b/DiSLProject2022/src-profiler/ex6/Instrumentation.java index e86003d..2c127c1 100644 --- a/DiSLProject2022/src-profiler/ex6/Instrumentation.java +++ b/DiSLProject2022/src-profiler/ex6/Instrumentation.java @@ -2,8 +2,6 @@ package ex6; import ch.usi.dag.disl.annotation.Before; import ch.usi.dag.disl.marker.BasicBlockMarker; -import ch.usi.dag.disl.marker.BytecodeMarker; -import ch.usi.dag.disl.staticcontext.LoopStaticContext; import ch.usi.dag.disl.staticcontext.MethodStaticContext; public class Instrumentation { @@ -11,7 +9,7 @@ public class Instrumentation { @Before(marker = BasicBlockMarker.class, scope = "ex6.MainThread.*", guard = IsFirstInLoopGuard.class) - static void handleLoopInstruction(final LoopStaticContext lsc, final MethodStaticContext msc) { + static void handleLoopInstruction(final MethodStaticContext msc) { Profiler.countLoop(msc.thisMethodFullName()); } } diff --git a/DiSLProject2022/src-profiler/ex7/MonitorEntry.java b/DiSLProject2022/src-profiler/ex7/MonitorEntry.java new file mode 100644 index 0000000..9cdf8c5 --- /dev/null +++ b/DiSLProject2022/src-profiler/ex7/MonitorEntry.java @@ -0,0 +1,3 @@ +package ex7; + +public record MonitorEntry (int monitorHashCode, String name){} diff --git a/DiSLProject2022/src-profiler/ex7/Profiler.java b/DiSLProject2022/src-profiler/ex7/Profiler.java index a98e9de..25ec383 100644 --- a/DiSLProject2022/src-profiler/ex7/Profiler.java +++ b/DiSLProject2022/src-profiler/ex7/Profiler.java @@ -1,51 +1,19 @@ package ex7; import java.util.Map; -import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.LongAdder; public final class Profiler { - private static final class MonitorEntry { - private final int hashCode; - private final String name; - - public MonitorEntry(int hashCode, String name) { - this.hashCode = hashCode; - this.name = name; - } - - public int getHashCode() { - return hashCode; - } - - public String getName() { - return name; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - MonitorEntry that = (MonitorEntry) o; - return hashCode == that.hashCode && Objects.equals(name, that.name); - } - - @Override - public int hashCode() { - return Objects.hash(hashCode, name); - } - } - private static final Map monitorToAccessCount = new ConcurrentHashMap<>(); static { Runtime.getRuntime().addShutdownHook(new Thread(() -> { for (final var entry : monitorToAccessCount.entrySet()) { System.out.printf("%d - %s - #Locks: %d%n", - entry.getKey().getHashCode(), - entry.getKey().getName(), + entry.getKey().monitorHashCode(), + entry.getKey().name(), entry.getValue().intValue() ); } @@ -58,8 +26,7 @@ public final class Profiler { public static void countMonitorAccess(final Object obj) { final MonitorEntry monitorEntry = new MonitorEntry(obj.hashCode(), obj.getClass().getName()); - monitorToAccessCount.computeIfAbsent(monitorEntry, k -> new LongAdder()); - monitorToAccessCount.get(monitorEntry).increment(); + monitorToAccessCount.computeIfAbsent(monitorEntry, k -> new LongAdder()).increment(); }