|
| 1 | +package io.avaje.metrics.core; |
| 2 | + |
| 3 | +import com.sun.management.GarbageCollectionNotificationInfo; |
| 4 | +import io.avaje.applog.AppLog; |
| 5 | +import io.avaje.metrics.Meter; |
| 6 | +import io.avaje.metrics.MetricRegistry; |
| 7 | + |
| 8 | +import javax.management.Notification; |
| 9 | +import javax.management.NotificationEmitter; |
| 10 | +import javax.management.NotificationFilter; |
| 11 | +import javax.management.NotificationListener; |
| 12 | +import javax.management.openmbean.CompositeData; |
| 13 | +import java.lang.management.GarbageCollectorMXBean; |
| 14 | +import java.lang.management.ManagementFactory; |
| 15 | +import java.lang.management.MemoryPoolMXBean; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +import static com.sun.management.GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION; |
| 21 | +import static java.lang.System.Logger.Level.INFO; |
| 22 | + |
| 23 | +final class JvmGCPause { |
| 24 | + |
| 25 | + private static final System.Logger log = AppLog.getLogger("io.avaje.metrics"); |
| 26 | + |
| 27 | + static void createMeters(MetricRegistry registry) { |
| 28 | + if (!extensionsPresent() || !hasNotifications()) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + final var listener = new Listener(registry); |
| 33 | + final var filter = new Filter(); |
| 34 | + for (GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) { |
| 35 | + if (gcBean instanceof NotificationEmitter) { |
| 36 | + final var emitter = (NotificationEmitter) gcBean; |
| 37 | + emitter.addNotificationListener(listener, filter, null); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + static final class Filter implements NotificationFilter { |
| 43 | + @Override |
| 44 | + public boolean isNotificationEnabled(Notification notification) { |
| 45 | + return GARBAGE_COLLECTION_NOTIFICATION.equals(notification.getType()); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + static final class Listener implements NotificationListener { |
| 50 | + |
| 51 | + private final Meter concurrent; |
| 52 | + private final Meter pause; |
| 53 | + |
| 54 | + Listener(MetricRegistry registry) { |
| 55 | + this.concurrent = registry.meter("jvm.gc.concurrent"); |
| 56 | + this.pause = registry.meter("jvm.gc.pause"); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void handleNotification(Notification notification, Object ref) { |
| 61 | + CompositeData cd = (CompositeData) notification.getUserData(); |
| 62 | + GarbageCollectionNotificationInfo notificationInfo = GarbageCollectionNotificationInfo.from(cd); |
| 63 | + |
| 64 | + String gcName = notificationInfo.getGcName(); |
| 65 | + String gcCause = notificationInfo.getGcCause(); |
| 66 | + long duration = notificationInfo.getGcInfo().getDuration(); |
| 67 | + |
| 68 | + if (isConcurrentPhase(gcCause, gcName)) { |
| 69 | + concurrent.addEvent(duration); |
| 70 | + } else { |
| 71 | + pause.addEvent(duration); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static boolean isConcurrentPhase(String cause, String name) { |
| 77 | + return "No GC".equals(cause) |
| 78 | + || "Shenandoah Cycles".equals(name) |
| 79 | + || (name.startsWith("ZGC") && name.endsWith("Cycles")) // ZGC |
| 80 | + || (name.startsWith("GPGC") && !name.endsWith("Pauses")) // Zing |
| 81 | + ; |
| 82 | + } |
| 83 | + |
| 84 | + private static boolean extensionsPresent() { |
| 85 | + if (ManagementFactory.getMemoryPoolMXBeans().isEmpty()) { |
| 86 | + // native-image Substrate VM |
| 87 | + log.log(INFO, "GC notifications not available, empty MemoryPoolMXBeans"); |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + try { |
| 92 | + Class.forName("com.sun.management.GarbageCollectionNotificationInfo", false, |
| 93 | + MemoryPoolMXBean.class.getClassLoader()); |
| 94 | + return true; |
| 95 | + } catch (Throwable e) { |
| 96 | + // We are operating in a JVM without access to this level of detail |
| 97 | + log.log(INFO, "GC notifications not available - no com.sun.management.GarbageCollectionNotificationInfo"); |
| 98 | + return false; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private static boolean hasNotifications() { |
| 103 | + List<String> gcsWithoutNotification = new ArrayList<>(); |
| 104 | + for (GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) { |
| 105 | + if (!(gcBean instanceof NotificationEmitter)) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + final var emitter = (NotificationEmitter) gcBean; |
| 109 | + boolean notificationAvailable = Arrays.stream(emitter.getNotificationInfo()) |
| 110 | + .anyMatch(notificationInfo -> Arrays.asList(notificationInfo.getNotifTypes()) |
| 111 | + .contains(GARBAGE_COLLECTION_NOTIFICATION)); |
| 112 | + if (notificationAvailable) { |
| 113 | + return true; |
| 114 | + } |
| 115 | + gcsWithoutNotification.add(gcBean.getName()); |
| 116 | + } |
| 117 | + log.log(INFO, "GC notifications not available GCs=" + gcsWithoutNotification); |
| 118 | + return false; |
| 119 | + } |
| 120 | +} |
0 commit comments