diff --git a/src/main/java/org/elasticsearch/monitor/jvm/JvmInfo.java b/src/main/java/org/elasticsearch/monitor/jvm/JvmInfo.java index 9aea763ff8f..5b7cd07b717 100644 --- a/src/main/java/org/elasticsearch/monitor/jvm/JvmInfo.java +++ b/src/main/java/org/elasticsearch/monitor/jvm/JvmInfo.java @@ -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; } diff --git a/src/main/java/org/elasticsearch/monitor/jvm/JvmStats.java b/src/main/java/org/elasticsearch/monitor/jvm/JvmStats.java index ba9bfb94827..c4c488b5e6d 100644 --- a/src/main/java/org/elasticsearch/monitor/jvm/JvmStats.java +++ b/src/main/java/org/elasticsearch/monitor/jvm/JvmStats.java @@ -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");