automatically enable last GC if higher or equal to 1.7.0 u4

This commit is contained in:
Shay Banon 2012-02-07 23:16:23 +02:00
parent c9107889a0
commit 49fd3ff470
2 changed files with 71 additions and 1 deletions

View File

@ -123,6 +123,68 @@ public class JvmInfo implements Streamable, Serializable, ToXContent {
return this.version;
}
public int versionAsInteger() {
try {
int i = 0;
String sVersion = "";
for (; i < version.length(); i++) {
if (!Character.isDigit(version.charAt(i)) && version.charAt(i) != '.') {
break;
}
if (version.charAt(i) != '.') {
sVersion += version.charAt(i);
}
}
if (i == 0) {
return -1;
}
return Integer.parseInt(sVersion);
} catch (Exception e) {
return -1;
}
}
public int versionUpdatePack() {
try {
int i = 0;
String sVersion = "";
for (; i < version.length(); i++) {
if (!Character.isDigit(version.charAt(i)) && version.charAt(i) != '.') {
break;
}
if (version.charAt(i) != '.') {
sVersion += version.charAt(i);
}
}
if (i == 0) {
return -1;
}
Integer.parseInt(sVersion);
int from;
if (version.charAt(i) == '_') {
// 1.7.0_4
from = ++i;
} else if (version.charAt(i) == '-' && version.charAt(i + 1) == 'u') {
// 1.7.0-u2-b21
i = i + 2;
from = i;
} else {
return -1;
}
for (; i < version.length(); i++) {
if (!Character.isDigit(version.charAt(i)) && version.charAt(i) != '.') {
break;
}
}
if (from == i) {
return -1;
}
return Integer.parseInt(version.substring(from, i));
} catch (Exception e) {
return -1;
}
}
public String vmName() {
return vmName;
}

View File

@ -66,7 +66,15 @@ public class JvmStats implements Streamable, Serializable, ToXContent {
memoryMXBean = ManagementFactory.getMemoryMXBean();
threadMXBean = ManagementFactory.getThreadMXBean();
boolean enableLastGc = Booleans.parseBoolean(System.getProperty("monitor.jvm.enable_last_gc"), false);
JvmInfo info = JvmInfo.jvmInfo();
boolean defaultEnableLastGc = false;
if (info.versionAsInteger() == 170) {
defaultEnableLastGc = info.versionUpdatePack() >= 4;
} else if (info.versionAsInteger() > 170) {
defaultEnableLastGc = true;
}
boolean enableLastGc = Booleans.parseBoolean(System.getProperty("monitor.jvm.enable_last_gc"), defaultEnableLastGc);
if (enableLastGc) {
try {
Class sunGcClass = Class.forName("com.sun.management.GarbageCollectorMXBean");