added load and deadlock detector in Memory util

This commit is contained in:
orbiter 2013-07-16 10:49:20 +02:00
parent 290e24564b
commit b71d13a014

View File

@ -20,6 +20,9 @@
package net.yacy.cora.util;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
public class Memory {
private static final Runtime runtime = Runtime.getRuntime();
@ -45,7 +48,7 @@ public class Memory {
* @return bytes
*/
public static final long maxMemory() {
return runtime.maxMemory();
return runtime.maxMemory(); // can be Long.MAX_VALUE if unlimited
}
/**
@ -63,5 +66,34 @@ public class Memory {
public static final long used() {
return total() - free();
}
/**
* get the system load within the last minute
* @return the system load or a negative number if the load is not available
*/
public static double load() {
return ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage();
}
/**
* find out the number of thread deadlocks. WARNING: this is a time-consuming task
* @return the number of deadlocked threads
*/
public static long deadlocks() {
long[] deadlockIDs = ManagementFactory.getThreadMXBean().findDeadlockedThreads();
if (deadlockIDs == null) return 0;
return deadlockIDs.length;
}
/**
* write deadlocked threads as to the log as warning
*/
public static void logDeadlocks() {
long[] deadlockIDs = ManagementFactory.getThreadMXBean().findDeadlockedThreads();
if (deadlockIDs == null) return;
ThreadInfo[] infos = ManagementFactory.getThreadMXBean().getThreadInfo(deadlockIDs, true, true);
for (ThreadInfo ti : infos) {
ConcurrentLog.warn("DEADLOCKREPORT", ti.toString());
}
}
}