/* * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; import java.util.Arrays; import java.util.Objects; import com.sun.management.HotSpotDiagnosticMXBean; import com.sun.management.VMOption; /** * Helper class with enum representation of GC types. */ public final class GCTypes { private static T getCurrentGCType(Class type) { return ManagementFactory.getGarbageCollectorMXBeans().stream() .map(bean -> getGCTypeByName(type, bean.getName())) .filter(Objects::nonNull) .findFirst() .orElse(null); } private static T getGCTypeByName(Class type, String name) { return Arrays.stream(type.getEnumConstants()) .filter(e -> e.getGCName().equals(name)) .findFirst() .orElse(null); } private static GarbageCollectorMXBean getGCBeanByType(Class type) { return ManagementFactory.getGarbageCollectorMXBeans().stream() .filter(bean -> Arrays.stream(type.getEnumConstants()) .filter(enumName -> enumName.getGCName().equals(bean.getName())) .findFirst() .isPresent() ) .findFirst() .orElse(null); } public static class LegacyMonitoring { private static final boolean useLegacyMonitoring = getUseLegacyMonitoring(); private static boolean getUseLegacyMonitoring() { HotSpotDiagnosticMXBean diagnostic = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class); VMOption option = diagnostic.getVMOption("G1UseLegacyMonitoring"); return option.getValue().equals("true"); } public static boolean use() { return useLegacyMonitoring; } } /** * Helper interface used by GCTypes static methods * to get gcTypeName field of *GCType classes. */ private interface GCType { String getGCName(); } public static enum YoungGCType implements GCType { DefNew("Copy"), ParNew("ParNew"), PSNew("PS Scavenge"), G1("G1"); @Override public String getGCName() { return gcTypeName; } private final String gcTypeName; private YoungGCType(String name) { if (name.contains("G1")) { name = LegacyMonitoring.use() ? "G1 Young Generation" : "G1 Young"; } gcTypeName = name; } public static YoungGCType getYoungGCType() { return GCTypes.getCurrentGCType(YoungGCType.class); } public static GarbageCollectorMXBean getYoungGCBean() { return GCTypes.getGCBeanByType(YoungGCType.class); } } public static enum OldGCType implements GCType { Serial("MarkSweepCompact"), CMS("ConcurrentMarkSweep"), PSOld("PS MarkSweep"), G1("G1"); private final String gcTypeName; private OldGCType(String name) { if (name.contains("G1")) { name = LegacyMonitoring.use() ? "G1 Old Generation" : "G1 Full"; } gcTypeName = name; } public static OldGCType getOldGCType() { return GCTypes.getCurrentGCType(OldGCType.class); } @Override public String getGCName() { return gcTypeName; } } }