Java Code Examples for com.sun.management.GarbageCollectionNotificationInfo#getGcCause()

The following examples show how to use com.sun.management.GarbageCollectionNotificationInfo#getGcCause() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestExcessGCLockerCollections.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void handleNotification(Notification notification, Object handback) {
    try {
        if (notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
            GarbageCollectionNotificationInfo info =
                    GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData());

            String gc_cause = info.getGcCause();

            if (gc_cause.equals(TestExcessGCLockerCollectionsStringConstants.GCLOCKER_CAUSE)) {
                Map<String, MemoryUsage> memory_before_gc = info.getGcInfo().getMemoryUsageBeforeGc();

                for (String newGenPoolName : newGenPoolNames) {
                    MemoryUsage usage = memory_before_gc.get(newGenPoolName);
                    if (usage == null) continue;

                    double startTime = ((double) info.getGcInfo().getStartTime()) / 1000.0;
                    long used = usage.getUsed();
                    long committed = usage.getCommitted();
                    long max = usage.getMax();
                    double used_percent = (((double) used) / Math.max(committed, max)) * 100.0;

                    System.out.printf("%6.3f: (%s) %d/%d/%d, %8.4f%% (%s)\n",
                                      startTime, gc_cause, used, committed, max, used_percent,
                                      ((used_percent < MIN_USED_PERCENT) ? TestExcessGCLockerCollectionsStringConstants.USED_TOO_LOW
                                                                         : TestExcessGCLockerCollectionsStringConstants.USED_OK));
                }
            }
        }
    } catch (RuntimeException ex) {
        System.err.println("Exception during notification processing:" + ex);
        ex.printStackTrace();
    }
}
 
Example 2
Source File: LogGCNotifications.java    From garmadon with Apache License 2.0 5 votes vote down vote up
static void handleHSNotification(Notification notification, Object handback) {
    BiConsumer<Long, String> printer = (BiConsumer<Long, String>) handback;
    GarbageCollectionNotificationInfo gcNotifInfo = GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData());
    GcInfo gcInfo = gcNotifInfo.getGcInfo();
    long start = gcInfo.getStartTime();
    long end = gcInfo.getEndTime();
    StringBuilder sb = new StringBuilder();
    sb.append(gcNotifInfo.getGcName());
    sb.append(" occurred at ");
    long serverStartTime = ManagementFactory.getRuntimeMXBean().getStartTime();
    sb.append(GC_TIME_FORMAT.get().format(new Date(start + serverStartTime)));
    sb.append(", took ");
    sb.append(end - start);
    sb.append("ms");
    String cause = gcNotifInfo.getGcCause();
    if (cause != null) {
        sb.append(" (");
        sb.append(cause);
        sb.append(") ");
    }
    Map<String, MemoryUsage> memoryUsageBeforeGc = gcInfo.getMemoryUsageBeforeGc();
    Map<String, MemoryUsage> memoryUsageAfterGc = gcInfo.getMemoryUsageAfterGc();
    for (Map.Entry<String, MemoryUsage> entry : memoryUsageAfterGc.entrySet()) {
        MemoryUsage before = memoryUsageBeforeGc.get(entry.getKey());
        MemoryUsage after = entry.getValue();
        long usedDelta = (before != null) ? (after.getUsed() - before.getUsed()) / 1024 : 0;
        if (usedDelta != 0) {
            sb.append(" ");
            sb.append(MXBeanHelper.normalizeName(entry.getKey()));
            sb.append("[").append(usedDelta > 0 ? "+" : "").append(usedDelta).append("]");
            sb.append("(").append(before.getUsed() / 1024).append("->").append(after.getUsed() / 1024).append(")");
        }
    }
    printer.accept(start, sb.toString());
}
 
Example 3
Source File: TestExcessGCLockerCollections.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void handleNotification(Notification notification, Object handback) {
    try {
        if (notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
            GarbageCollectionNotificationInfo info =
                    GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData());

            String gc_cause = info.getGcCause();

            if (gc_cause.equals(TestExcessGCLockerCollectionsStringConstants.GCLOCKER_CAUSE)) {
                Map<String, MemoryUsage> memory_before_gc = info.getGcInfo().getMemoryUsageBeforeGc();

                for (String newGenPoolName : newGenPoolNames) {
                    MemoryUsage usage = memory_before_gc.get(newGenPoolName);
                    if (usage == null) continue;

                    double startTime = ((double) info.getGcInfo().getStartTime()) / 1000.0;
                    long used = usage.getUsed();
                    long committed = usage.getCommitted();
                    long max = usage.getMax();
                    double used_percent = (((double) used) / Math.max(committed, max)) * 100.0;

                    System.out.printf("%6.3f: (%s) %d/%d/%d, %8.4f%% (%s)\n",
                                      startTime, gc_cause, used, committed, max, used_percent,
                                      ((used_percent < MIN_USED_PERCENT) ? TestExcessGCLockerCollectionsStringConstants.USED_TOO_LOW
                                                                         : TestExcessGCLockerCollectionsStringConstants.USED_OK));
                }
            }
        }
    } catch (RuntimeException ex) {
        System.err.println("Exception during notification processing:" + ex);
        ex.printStackTrace();
    }
}
 
Example 4
Source File: JvmHeapPressureMetrics.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private void monitor() {
    double maxOldGen = JvmMemory.getOldGen().map(mem -> JvmMemory.getUsageValue(mem, MemoryUsage::getMax)).orElse(0.0);

    for (GarbageCollectorMXBean mbean : ManagementFactory.getGarbageCollectorMXBeans()) {
        if (!(mbean instanceof NotificationEmitter)) {
            continue;
        }
        NotificationListener notificationListener = (notification, ref) -> {
            if (!notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
                return;
            }

            CompositeData cd = (CompositeData) notification.getUserData();
            GarbageCollectionNotificationInfo notificationInfo = GarbageCollectionNotificationInfo.from(cd);

            String gcCause = notificationInfo.getGcCause();
            GcInfo gcInfo = notificationInfo.getGcInfo();
            long duration = gcInfo.getDuration();

            if (!JvmMemory.isConcurrentPhase(gcCause)) {
                gcPauseSum.record(duration);
            }

            Map<String, MemoryUsage> after = gcInfo.getMemoryUsageAfterGc();

            if (oldGenPoolName != null) {
                final long oldAfter = after.get(oldGenPoolName).getUsed();
                lastOldGenUsageAfterGc.set(oldAfter / maxOldGen);
            }
        };
        NotificationEmitter notificationEmitter = (NotificationEmitter) mbean;
        notificationEmitter.addNotificationListener(notificationListener, null, null);
        notificationListenerCleanUpRunnables.add(() -> {
            try {
                notificationEmitter.removeNotificationListener(notificationListener);
            } catch (ListenerNotFoundException ignore) {
            }
        });
    }
}
 
Example 5
Source File: GarbageCollection.java    From tracing-framework with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void handleNotification(Notification notification, Object handback) {
    // Make sure it's the right notification type
    if (!notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION))
        return;

    // Extract the GcInfo as appropriate, working around the OpenJDK GcInfo
    // cycles/milliseconds bug
    GarbageCollectorMXBean bean = (GarbageCollectorMXBean) handback;
    GarbageCollectionNotificationInfo info = getInfo(notification);
    GcInfo gc = getGcInfo(info, bean);

    // Extract the fields we want to include in the X-Trace report
    String startTime = Long.toString(JVMStartTimeMillis + gc.getStartTime());
    String duration = Long.toString(gc.getDuration());
    String action = info.getGcAction();
    String cause = info.getGcCause();
    String name = info.getGcName();

    XTraceReport report = XTraceReport.create();
    report.addStandardFields();
    report.builder.setTaskId(taskid);
    report.applyDecorators();
    report.setMessage("Garbage Collection Event");
    report.builder.addKey("Operation").addValue("GC");
    report.builder.addKey("GcStart").addValue(startTime);
    report.builder.addKey("GcDuration").addValue(duration);
    report.builder.addKey("GcAction").addValue(action);
    report.builder.addKey("GcCause").addValue(cause);
    report.builder.addKey("GcName").addValue(name);
    report.builder.addTags("GarbageCollection");
    report.builder.addTags(Utils.getProcessName());
    XTrace.getDefaultReporter().send(report);
}
 
Example 6
Source File: ProtobufGCNotifications.java    From garmadon with Apache License 2.0 4 votes vote down vote up
static void handleHSNotification(Notification notification, Object handback) {
    BiConsumer<Long, JVMStatisticsEventsProtos.GCStatisticsData> printer = (BiConsumer<Long, JVMStatisticsEventsProtos.GCStatisticsData>) handback;
    GarbageCollectionNotificationInfo gcNotifInfo = GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData());
    GcInfo gcInfo = gcNotifInfo.getGcInfo();
    GcEvent gcEvent = new GcEvent(gcInfo.getStartTime(), gcInfo.getEndTime());
    long pauseTime = gcEvent.getPauseDuration();
    String collectorName = gcNotifInfo.getGcName();
    long serverStartTime = ManagementFactory.getRuntimeMXBean().getStartTime();
    long timestamp = gcInfo.getStartTime() + serverStartTime;
    String cause = gcNotifInfo.getGcCause();
    JVMStatisticsEventsProtos.GCStatisticsData.Builder builder = JVMStatisticsEventsProtos.GCStatisticsData.newBuilder();
    builder.setPauseTime(pauseTime);

    builder.setGcPauseRatio1Min((float) computeTotalPauseTime(gcEvents, gcEvent) / MILLIS_MINUTE * 100);
    gcEvents.add(gcEvent);

    builder.setCollectorName(collectorName);
    builder.setCause(cause);
    Map<String, MemoryUsage> memoryUsageBeforeGc = gcInfo.getMemoryUsageBeforeGc();
    Map<String, MemoryUsage> memoryUsageAfterGc = gcInfo.getMemoryUsageAfterGc();
    for (Map.Entry<String, MemoryUsage> entry : memoryUsageAfterGc.entrySet()) {
        MemoryUsage before = memoryUsageBeforeGc.get(entry.getKey());
        MemoryUsage after = entry.getValue();
        switch (MXBeanHelper.normalizeName(entry.getKey())) {
            case MXBeanHelper.MEMORY_POOL_CODE_HEADER:
                builder.setCodeBefore(before.getUsed());
                builder.setCodeAfter(after.getUsed());
                break;
            case MXBeanHelper.MEMORY_POOL_PERM_HEADER:
            case MXBeanHelper.MEMORY_POOL_METASPACE_HEADER:
                builder.setMetaspaceBefore(before.getUsed());
                builder.setMetaspaceAfter(after.getUsed());
                break;
            case MXBeanHelper.MEMORY_POOL_EDEN_HEADER:
                builder.setEdenBefore(before.getUsed());
                builder.setEdenAfter(after.getUsed());
                break;
            case MXBeanHelper.MEMORY_POOL_SURVIVOR_HEADER:
                builder.setSurvivorBefore(before.getUsed());
                builder.setSurvivorAfter(after.getUsed());
                break;
            case MXBeanHelper.MEMORY_POOL_OLD_HEADER:
                builder.setOldBefore(before.getUsed());
                builder.setOldAfter(after.getUsed());
                break;
            case MXBeanHelper.MEMORY_POOL_COMPRESSEDCLASSPACE_HEADER:
                // ignore
                break;
            default:
                throw new UnsupportedOperationException(entry.getKey() + " not supported");
        }
    }
    printer.accept(timestamp, builder.build());
}
 
Example 7
Source File: GcMonitoringModule.java    From spark with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onGc(GarbageCollectionNotificationInfo data) {
    String gcType;
    if (data.getGcAction().equals("end of minor GC")) {
        gcType = "Young Gen";
    } else if (data.getGcAction().equals("end of major GC")) {
        gcType = "Old Gen";
    } else {
        gcType = data.getGcAction();
    }

    String gcCause = data.getGcCause() != null ? " (cause = " + data.getGcCause() + ")" : "";

    Map<String, MemoryUsage> beforeUsages = data.getGcInfo().getMemoryUsageBeforeGc();
    Map<String, MemoryUsage> afterUsages = data.getGcInfo().getMemoryUsageAfterGc();

    this.platform.getPlugin().executeAsync(() -> {
        List<Component> report = new LinkedList<>();
        report.add(CommandResponseHandler.applyPrefix(TextComponent.builder("").color(TextColor.GRAY)
                .append(TextComponent.of(gcType + " "))
                .append(TextComponent.of("GC", TextColor.RED))
                .append(TextComponent.of(" lasting "))
                .append(TextComponent.of(df.format(data.getGcInfo().getDuration()), TextColor.GOLD))
                .append(TextComponent.of(" ms." + gcCause))
                .build()
        ));

        for (Map.Entry<String, MemoryUsage> entry : afterUsages.entrySet()) {
            String type = entry.getKey();
            MemoryUsage after = entry.getValue();
            MemoryUsage before = beforeUsages.get(type);

            if (before == null) {
                continue;
            }

            long diff = before.getUsed() - after.getUsed();
            if (diff == 0) {
                continue;
            }

            if (diff > 0) {
                report.add(TextComponent.builder("  ")
                        .append(TextComponent.of(FormatUtil.formatBytes(diff), TextColor.GOLD))
                        .append(TextComponent.of(" freed from ", TextColor.DARK_GRAY))
                        .append(TextComponent.of(type, TextColor.GRAY))
                        .build()
                );
                report.add(TextComponent.builder("    ")
                        .append(TextComponent.of(FormatUtil.formatBytes(before.getUsed()), TextColor.GRAY))
                        .append(TextComponent.of(" → ", TextColor.DARK_GRAY))
                        .append(TextComponent.of(FormatUtil.formatBytes(after.getUsed()), TextColor.GRAY))
                        .append(TextComponent.space())
                        .append(TextComponent.of("(", TextColor.DARK_GRAY))
                        .append(TextComponent.of(FormatUtil.percent(diff, before.getUsed()), TextColor.WHITE))
                        .append(TextComponent.of(")", TextColor.DARK_GRAY))
                        .build()
                );
            } else {
                report.add(TextComponent.builder("  ")
                        .append(TextComponent.of(FormatUtil.formatBytes(-diff), TextColor.GOLD))
                        .append(TextComponent.of(" moved to ", TextColor.DARK_GRAY))
                        .append(TextComponent.of(type, TextColor.GRAY))
                        .build()
                );
                report.add(TextComponent.builder("    ")
                        .append(TextComponent.of(FormatUtil.formatBytes(before.getUsed()), TextColor.GRAY))
                        .append(TextComponent.of(" → ", TextColor.DARK_GRAY))
                        .append(TextComponent.of(FormatUtil.formatBytes(after.getUsed()), TextColor.GRAY))
                        .build()
                );
            }
        }

        report.forEach(this.resp::broadcast);
    });
}
 
Example 8
Source File: RingBufferGarbageCollectionLog.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void handleNotification(final Notification notification, final Object handback) {
    if (!notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
        return;
    }

    final CompositeData compositeData = (CompositeData) notification.getUserData();
    final GarbageCollectionNotificationInfo gcNotification = GarbageCollectionNotificationInfo.from(compositeData);
    final GcInfo gcInfo = gcNotification.getGcInfo();

    final String gcName = gcNotification.getGcName();
    final String action = gcNotification.getGcAction();
    final String cause = gcNotification.getGcCause();

    final long startTime = jvmStartTime + gcInfo.getStartTime();
    final long endTime = jvmStartTime + gcInfo.getEndTime();

    final Map<String, MemoryUsage> usageAfter = gcInfo.getMemoryUsageAfterGc();
    final Map<String, MemoryUsage> usageBefore = gcInfo.getMemoryUsageBeforeGc();

    final List<GarbageCollectionEvent.GarbageCollectionHeapSize> heapSizes = new ArrayList<>();
    for (final Map.Entry<String, MemoryUsage> entry : usageAfter.entrySet()) {
        final MemoryUsage before = usageBefore.get(entry.getKey());
        if (before == null) {
            continue;
        }

        final MemoryUsage after = entry.getValue();
        if (after.getUsed() == before.getUsed()) {
            continue;
        }

        heapSizes.add(new StandardGarbageCollectionEvent.StandardGarbageCollectionHeapSize(entry.getKey(), before.getUsed(), after.getUsed()));
    }

    final GarbageCollectionEvent event = new StandardGarbageCollectionEvent(gcName, action, cause, startTime, endTime, heapSizes);

    if (gcInfo.getDuration() >= minDurationThreshold) {
        events.add(event);
    }

    synchronized (this) {
        final Tuple<Long, Long> previousTuple = timeAndCountPerAction.get(action);
        if (previousTuple == null){
            timeAndCountPerAction.put(action, new Tuple<>(gcInfo.getDuration(), 1L));
        } else {
            timeAndCountPerAction.put(action, new Tuple<>(gcInfo.getDuration() + previousTuple.getKey(), 1L + previousTuple.getValue()));
        }

        if (maxDurationEvent == null || event.getDuration() > maxDurationEvent.getDuration()) {
            maxDurationEvent = event;
        }
    }
}