From d8f4e21904f9d5b677458bc0e0e3f3dc91d36638 Mon Sep 17 00:00:00 2001 From: Jacob Collins <18370334+josrepo@users.noreply.github.com> Date: Wed, 18 Oct 2023 14:14:32 +0100 Subject: [PATCH 1/3] Implement gcStats --- src/som/primitives/SystemPrimitives.java | 46 ++++++++++++++++++++---- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/src/som/primitives/SystemPrimitives.java b/src/som/primitives/SystemPrimitives.java index bf87bb2..6a2bf17 100644 --- a/src/som/primitives/SystemPrimitives.java +++ b/src/som/primitives/SystemPrimitives.java @@ -25,20 +25,19 @@ package som.primitives; import java.io.IOException; +import java.lang.management.GarbageCollectorMXBean; +import java.lang.management.ManagementFactory; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.List; +import com.sun.management.ThreadMXBean; import som.compiler.ProgramDefinitionError; import som.interpreter.Frame; import som.interpreter.Interpreter; import som.vm.Universe; -import som.vmobjects.SAbstractObject; -import som.vmobjects.SClass; -import som.vmobjects.SInteger; -import som.vmobjects.SPrimitive; -import som.vmobjects.SString; -import som.vmobjects.SSymbol; +import som.vmobjects.*; public class SystemPrimitives extends Primitives { @@ -162,6 +161,41 @@ public void invoke(final Frame frame, final Interpreter interpreter) { } }); + installInstancePrimitive(new SPrimitive("gcStats", universe) { + + @Override + public void invoke(Frame frame, Interpreter interpreter) { + frame.pop(); + + final List gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); + final ThreadMXBean threadBean = (ThreadMXBean) ManagementFactory.getThreadMXBean(); + threadBean.setThreadAllocatedMemoryEnabled(true); + + final long allocatedBytes = threadBean.getCurrentThreadAllocatedBytes(); + long counts = 0; + long time = 0; + + for (GarbageCollectorMXBean b : gcBeans) { + long c = b.getCollectionCount(); + if (c != -1) { + counts += c; + } + + long t = b.getCollectionTime(); + if (t != -1) { + time += t; + } + } + + final SArray arr = new SArray(universe.nilObject, 3L); + arr.setIndexableField(0L, SInteger.getInteger(counts)); + arr.setIndexableField(1L, SInteger.getInteger(time)); + arr.setIndexableField(2L, SInteger.getInteger(allocatedBytes)); + + frame.push(arr); + } + }); + installInstancePrimitive(new SPrimitive("loadFile:", universe) { @Override From 52f7b5121073e53e8d40d46840ac9174c7064201 Mon Sep 17 00:00:00 2001 From: josrepo <18370334+josrepo@users.noreply.github.com> Date: Thu, 4 Apr 2024 16:50:07 +0100 Subject: [PATCH 2/3] Remove import wildcard --- src/som/primitives/SystemPrimitives.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/som/primitives/SystemPrimitives.java b/src/som/primitives/SystemPrimitives.java index 6a2bf17..cf37214 100644 --- a/src/som/primitives/SystemPrimitives.java +++ b/src/som/primitives/SystemPrimitives.java @@ -37,7 +37,13 @@ import som.interpreter.Frame; import som.interpreter.Interpreter; import som.vm.Universe; -import som.vmobjects.*; +import som.vmobjects.SAbstractObject; +import som.vmobjects.SArray; +import som.vmobjects.SClass; +import som.vmobjects.SInteger; +import som.vmobjects.SPrimitive; +import som.vmobjects.SString; +import som.vmobjects.SSymbol; public class SystemPrimitives extends Primitives { From c71acc0745f9c64cf54e50c4ece59c67321440e7 Mon Sep 17 00:00:00 2001 From: Stefan Marr Date: Fri, 5 Apr 2024 00:17:41 +0100 Subject: [PATCH 3/3] Run `ant eclipseformat` Signed-off-by: Stefan Marr --- src/som/primitives/SystemPrimitives.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/som/primitives/SystemPrimitives.java b/src/som/primitives/SystemPrimitives.java index cf37214..8ff2ba3 100644 --- a/src/som/primitives/SystemPrimitives.java +++ b/src/som/primitives/SystemPrimitives.java @@ -173,7 +173,8 @@ public void invoke(final Frame frame, final Interpreter interpreter) { public void invoke(Frame frame, Interpreter interpreter) { frame.pop(); - final List gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); + final List gcBeans = + ManagementFactory.getGarbageCollectorMXBeans(); final ThreadMXBean threadBean = (ThreadMXBean) ManagementFactory.getThreadMXBean(); threadBean.setThreadAllocatedMemoryEnabled(true);