Skip to content

Commit

Permalink
Add gcStats system primitive (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
smarr authored Apr 4, 2024
2 parents bfcddf5 + c71acc0 commit b13e93f
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/som/primitives/SystemPrimitives.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@
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.SArray;
import som.vmobjects.SClass;
import som.vmobjects.SInteger;
import som.vmobjects.SPrimitive;
Expand Down Expand Up @@ -162,6 +167,42 @@ 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<GarbageCollectorMXBean> 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
Expand Down

0 comments on commit b13e93f

Please sign in to comment.