Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

now can sandbox on JVMs that do not support ThreadMXBean #84

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions src/main/java/delight/nashornsandbox/internal/ThreadMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public class ThreadMonitor {

private Thread threadToMonitor;

private final ThreadMXBean threadBean;
private ThreadMXBean threadBean;

private final com.sun.management.ThreadMXBean memoryCouter;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed typo memoryCouter -> memoryCounter

private final com.sun.management.ThreadMXBean memoryCounter;

ThreadMonitor(final long maxCPUTime, final long maxMemory) {
this.maxMemory = maxMemory;
Expand All @@ -55,18 +55,27 @@ public class ThreadMonitor {
cpuLimitExceeded = new AtomicBoolean(false);
memoryLimitExceeded = new AtomicBoolean(false);
monitor = new Object();
threadBean = ManagementFactory.getThreadMXBean();
// ensure this feature is enabled
threadBean.setThreadCpuTimeEnabled(true);
if (threadBean instanceof com.sun.management.ThreadMXBean) {
memoryCouter = (com.sun.management.ThreadMXBean) threadBean;

// ensure the ThreadMXBean is supported in the JVM
try {
threadBean = ManagementFactory.getThreadMXBean();
// ensure the ThreadMXBean is enabled for CPU time measurement
threadBean.setThreadCpuTimeEnabled(true);
} catch (UnsupportedOperationException ex) {
if(maxCPUTime > 0) {
throw new UnsupportedOperationException("JVM does not support thread CPU time measurement");
}
}

if ((threadBean != null) && (threadBean instanceof com.sun.management.ThreadMXBean)) {
memoryCounter = (com.sun.management.ThreadMXBean) threadBean;
// ensure this feature is enabled
memoryCouter.setThreadAllocatedMemoryEnabled(true);
memoryCounter.setThreadAllocatedMemoryEnabled(true);
} else {
if (maxMemory > 0) {
throw new UnsupportedOperationException("JVM does not support thread memory counting");
}
memoryCouter = null;
memoryCounter = null;
}
}

Expand Down Expand Up @@ -161,14 +170,18 @@ private boolean isMemoryExided(final long memory) {
* @return current memory usage
*/
private long getCurrentMemory() {
if (maxMemory == 0 || memoryCouter != null) {
return memoryCouter.getThreadAllocatedBytes(threadToMonitor.getId());
if ((maxMemory > 0) && (memoryCounter != null)) {
return memoryCounter.getThreadAllocatedBytes(threadToMonitor.getId());
}
return 0L;
}

private long getCPUTime() {
return threadBean.getThreadCpuTime(threadToMonitor.getId());
if ((maxCPUTime > 0) && (threadBean != null)) {
return threadBean.getThreadCpuTime(threadToMonitor.getId());
} else {
return 0L;
}
}

public void stopMonitor() {
Expand Down