Search in sources :

Example 6 with GcInfo

use of com.sun.management.GcInfo in project jdk8u_jdk by JetBrains.

the class GarbageCollectionNotificationContentTest method checkGarbageCollectionNotificationInfoContent.

private static void checkGarbageCollectionNotificationInfoContent(GarbageCollectionNotificationInfo notif) throws Exception {
    System.out.println("GC notification for " + notif.getGcName());
    System.out.print("Action: " + notif.getGcAction());
    System.out.println(" Cause: " + notif.getGcCause());
    GcInfo info = notif.getGcInfo();
    System.out.print("GC Info #" + info.getId());
    System.out.print(" start:" + info.getStartTime());
    System.out.print(" end:" + info.getEndTime());
    System.out.println(" (" + info.getDuration() + "ms)");
    Map<String, MemoryUsage> usage = info.getMemoryUsageBeforeGc();
    List<String> pnames = new ArrayList<String>();
    for (Map.Entry entry : usage.entrySet()) {
        String poolname = (String) entry.getKey();
        pnames.add(poolname);
        MemoryUsage busage = (MemoryUsage) entry.getValue();
        MemoryUsage ausage = (MemoryUsage) info.getMemoryUsageAfterGc().get(poolname);
        if (ausage == null) {
            throw new RuntimeException("After Gc Memory does not exist" + " for " + poolname);
        }
        System.out.println("Usage for pool " + poolname);
        System.out.println("   Before GC: " + busage);
        System.out.println("   After GC: " + ausage);
    }
    // check if memory usage for all memory pools are returned
    List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean p : pools) {
        if (!pnames.contains(p.getName())) {
            throw new RuntimeException("GcInfo does not contain " + "memory usage for pool " + p.getName());
        }
    }
}
Also used : GcInfo(com.sun.management.GcInfo)

Example 7 with GcInfo

use of com.sun.management.GcInfo in project micrometer by micrometer-metrics.

the class JvmGcMetrics method bindTo.

@Override
public void bindTo(MeterRegistry registry) {
    AtomicLong maxDataSize = new AtomicLong(0L);
    Gauge.builder("jvm.gc.max.data.size", maxDataSize, AtomicLong::get).tags(tags).description("Max size of old generation memory pool").baseUnit("bytes").register(registry);
    AtomicLong liveDataSize = new AtomicLong(0L);
    Gauge.builder("jvm.gc.live.data.size", liveDataSize, AtomicLong::get).tags(tags).description("Size of old generation memory pool after a full GC").baseUnit("bytes").register(registry);
    Counter promotedBytes = Counter.builder("jvm.gc.memory.promoted").tags(tags).baseUnit("bytes").description("Count of positive increases in the size of the old generation memory pool before GC to after GC").register(registry);
    Counter allocatedBytes = Counter.builder("jvm.gc.memory.allocated").tags(tags).baseUnit("bytes").description("Incremented for an increase in the size of the young generation memory pool after one GC to before the next").register(registry);
    if (this.managementExtensionsPresent) {
        // start watching for GC notifications
        final AtomicLong youngGenSizeAfter = new AtomicLong(0L);
        for (GarbageCollectorMXBean mbean : ManagementFactory.getGarbageCollectorMXBeans()) {
            if (mbean instanceof NotificationEmitter) {
                ((NotificationEmitter) mbean).addNotificationListener((notification, ref) -> {
                    final String type = notification.getType();
                    if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
                        CompositeData cd = (CompositeData) notification.getUserData();
                        GarbageCollectionNotificationInfo notificationInfo = GarbageCollectionNotificationInfo.from(cd);
                        if (isConcurrentPhase(notificationInfo.getGcCause())) {
                            Timer.builder("jvm.gc.concurrent.phase.time").tags(tags).tags("action", notificationInfo.getGcAction(), "cause", notificationInfo.getGcCause()).description("Time spent in concurrent phase").register(registry).record(notificationInfo.getGcInfo().getDuration(), TimeUnit.MILLISECONDS);
                        } else {
                            Timer.builder("jvm.gc.pause").tags(tags).tags("action", notificationInfo.getGcAction(), "cause", notificationInfo.getGcCause()).description("Time spent in GC pause").register(registry).record(notificationInfo.getGcInfo().getDuration(), TimeUnit.MILLISECONDS);
                        }
                        GcInfo gcInfo = notificationInfo.getGcInfo();
                        // Update promotion and allocation counters
                        final Map<String, MemoryUsage> before = gcInfo.getMemoryUsageBeforeGc();
                        final Map<String, MemoryUsage> after = gcInfo.getMemoryUsageAfterGc();
                        if (oldGenPoolName != null) {
                            final long oldBefore = before.get(oldGenPoolName).getUsed();
                            final long oldAfter = after.get(oldGenPoolName).getUsed();
                            final long delta = oldAfter - oldBefore;
                            if (delta > 0L) {
                                promotedBytes.increment(delta);
                            }
                            // after a major GC.
                            if (oldAfter < oldBefore || GcGenerationAge.fromName(notificationInfo.getGcName()) == GcGenerationAge.OLD) {
                                liveDataSize.set(oldAfter);
                                final long oldMaxAfter = after.get(oldGenPoolName).getMax();
                                maxDataSize.set(oldMaxAfter);
                            }
                        }
                        if (youngGenPoolName != null) {
                            final long youngBefore = before.get(youngGenPoolName).getUsed();
                            final long youngAfter = after.get(youngGenPoolName).getUsed();
                            final long delta = youngBefore - youngGenSizeAfter.get();
                            youngGenSizeAfter.set(youngAfter);
                            if (delta > 0L) {
                                allocatedBytes.increment(delta);
                            }
                        }
                    }
                }, null, null);
            }
        }
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) GarbageCollectionNotificationInfo(com.sun.management.GarbageCollectionNotificationInfo) NotificationEmitter(javax.management.NotificationEmitter) GcInfo(com.sun.management.GcInfo) CompositeData(javax.management.openmbean.CompositeData) GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) MemoryUsage(java.lang.management.MemoryUsage)

Example 8 with GcInfo

use of com.sun.management.GcInfo in project openj9 by eclipse.

the class MemoryNotificationThread method dispatchGCNotificationHelper.

/**
 * A helper used by processNotificationLoop to construct and dispatch
 * garbage collection notification objects
 *
 * @param gcName
 *            the name of garbage collector which we are sending notifications on behalf of
 * @param gcAction
 *            the action of the performed by the garbage collector
 * @param gcCause
 *            the cause the garbage collection
 * @param index
 *            the identifier of this garbage collection which is the number of collections that this collector has done
 * @param startTime
 *            the start time of this GC in milliseconds since the Java virtual machine was started
 * @param endTime
 *            the end time of this GC in milliseconds since the Java virtual machine was started
 * @param poolNames
 *            the name of all memory pools (includes non-heap memory pools)
 * @param initialSize
 *            the initial amount of memory of all memory pools
 * @param preUsed
 *            the amounts of memory used of all memory pools before the garbage collection
 * @param preCommitted
 *            the amounts of all memory pools that is guaranteed to be available for use before the garbage collection
 * @param preMax
 *            the maximum amounts of memory pools that can be used before the garbage collection
 * @param postUsed
 *            the amounts of memory used of all memory pools after the garbage collection
 * @param postCommitted
 *            the amounts of all memory pools that is guaranteed to be available for use after the garbage collection
 * @param postMax
 *            the maximum amounts of memory pools that can be used after the garbage collection
 * @param sequenceNumber
 *            the sequence identifier of the current notification
 */
private void dispatchGCNotificationHelper(String gcName, String gcAction, String gcCause, long index, long startTime, long endTime, String[] poolNames, long[] initialSize, long[] preUsed, long[] preCommitted, long[] preMax, long[] postUsed, long[] postCommitted, long[] postMax, long sequenceNumber) {
    GcInfo gcInfo = ExtendedGarbageCollectorMXBeanImpl.buildGcInfo(index, startTime, endTime, poolNames, initialSize, preUsed, preCommitted, preMax, postUsed, postCommitted, postMax);
    GarbageCollectionNotificationInfo info = new GarbageCollectionNotificationInfo(gcName, gcAction, gcCause, gcInfo);
    for (MemoryManagerMXBean bean : memBean.getMemoryManagerMXBeans(false)) {
        if (bean instanceof GarbageCollectorMXBeanImpl && bean.getName().equals(gcName)) {
            Notification notification = new Notification(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION, // $NON-NLS-1$
            "java.lang:type=GarbageCollector", sequenceNumber);
            notification.setUserData(GarbageCollectionNotificationInfoUtil.toCompositeData(info));
            ((GarbageCollectorMXBeanImpl) bean).sendNotification(notification);
            break;
        }
    }
}
Also used : GarbageCollectionNotificationInfo(com.sun.management.GarbageCollectionNotificationInfo) GcInfo(com.sun.management.GcInfo) GarbageCollectorMXBeanImpl(com.ibm.java.lang.management.internal.GarbageCollectorMXBeanImpl) MemoryManagerMXBean(java.lang.management.MemoryManagerMXBean) Notification(javax.management.Notification)

Example 9 with GcInfo

use of com.sun.management.GcInfo in project openj9 by eclipse.

the class GcInfoUtil method newGcInfoInstance.

/**
 * @param index
 * 			  the identifier of this garbage collection which is the number of collections that this collector has done
 * @param startTime
 * 			  the start time of the collection in milliseconds since the Java virtual machine was started.
 * @param endTime
 * 			  the end time of the collection in milliseconds since the Java virtual machine was started.
 * @param usageBeforeGc
 * 			  the memory usage of all memory pools at the beginning of this GC.
 * @param usageAfterGc
 * 			  the memory usage of all memory pools at the end of this GC.
 * @return a <code>GcInfo</code> object
 */
public static GcInfo newGcInfoInstance(long index, long startTime, long endTime, Map<String, MemoryUsage> usageBeforeGc, Map<String, MemoryUsage> usageAfterGc) {
    GcInfo gcInfo = null;
    Constructor<GcInfo> gcInfoConstructor = getGcInfoPrivateConstructor();
    try {
        gcInfo = gcInfoConstructor.newInstance(index, startTime, endTime, usageBeforeGc, usageAfterGc);
    } catch (Exception e) {
        /*[MSG "K0661", "Internal error while obtaining GcInfo instance."]*/
        // $NON-NLS-1$
        InternalError error = new InternalError(com.ibm.oti.util.Msg.getString("K0661"));
        error.initCause(e);
        throw error;
    }
    return gcInfo;
}
Also used : GcInfo(com.sun.management.GcInfo) OpenDataException(javax.management.openmbean.OpenDataException)

Example 10 with GcInfo

use of com.sun.management.GcInfo in project cassandra by apache.

the class GCInspector method handleNotification.

public void handleNotification(final Notification notification, final Object handback) {
    String type = notification.getType();
    if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
        // retrieve the garbage collection notification information
        CompositeData cd = (CompositeData) notification.getUserData();
        GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from(cd);
        String gcName = info.getGcName();
        GcInfo gcInfo = info.getGcInfo();
        long duration = gcInfo.getDuration();
        /*
             * The duration supplied in the notification info includes more than just
             * application stopped time for concurrent GCs. Try and do a better job coming up with a good stopped time
             * value by asking for and tracking cumulative time spent blocked in GC.
             */
        GCState gcState = gcStates.get(gcName);
        if (gcState.assumeGCIsPartiallyConcurrent) {
            long previousTotal = gcState.lastGcTotalDuration;
            long total = gcState.gcBean.getCollectionTime();
            gcState.lastGcTotalDuration = total;
            // may be zero for a really fast collection
            duration = total - previousTotal;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(info.getGcName()).append(" GC in ").append(duration).append("ms.  ");
        long bytes = 0;
        Map<String, MemoryUsage> beforeMemoryUsage = gcInfo.getMemoryUsageBeforeGc();
        Map<String, MemoryUsage> afterMemoryUsage = gcInfo.getMemoryUsageAfterGc();
        for (String key : gcState.keys(info)) {
            MemoryUsage before = beforeMemoryUsage.get(key);
            MemoryUsage after = afterMemoryUsage.get(key);
            if (after != null && after.getUsed() != before.getUsed()) {
                sb.append(key).append(": ").append(before.getUsed());
                sb.append(" -> ");
                sb.append(after.getUsed());
                if (!key.equals(gcState.keys[gcState.keys.length - 1]))
                    sb.append("; ");
                bytes += before.getUsed() - after.getUsed();
            }
        }
        while (true) {
            State prev = state.get();
            if (state.compareAndSet(prev, new State(duration, bytes, prev)))
                break;
        }
        if (gcWarnThreasholdInMs != 0 && duration > gcWarnThreasholdInMs)
            logger.warn(sb.toString());
        else if (duration > gcLogThreshholdInMs)
            logger.info(sb.toString());
        else if (logger.isTraceEnabled())
            logger.trace(sb.toString());
        if (duration > this.getStatusThresholdInMs())
            StatusLogger.log();
        // if we just finished an old gen collection and we're still using a lot of memory, try to reduce the pressure
        if (gcState.assumeGCIsOldGen)
            LifecycleTransaction.rescheduleFailedDeletions();
    }
}
Also used : GarbageCollectionNotificationInfo(com.sun.management.GarbageCollectionNotificationInfo) GcInfo(com.sun.management.GcInfo) CompositeData(javax.management.openmbean.CompositeData) MemoryUsage(java.lang.management.MemoryUsage)

Aggregations

GcInfo (com.sun.management.GcInfo)12 GarbageCollectionNotificationInfo (com.sun.management.GarbageCollectionNotificationInfo)5 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)3 MemoryUsage (java.lang.management.MemoryUsage)3 NotificationEmitter (javax.management.NotificationEmitter)3 CompositeData (javax.management.openmbean.CompositeData)3 Map (java.util.Map)2 Notification (javax.management.Notification)2 NotificationListener (javax.management.NotificationListener)2 Duration (com.github.joschi.jadconfig.util.Duration)1 GarbageCollectorMXBeanImpl (com.ibm.java.lang.management.internal.GarbageCollectorMXBeanImpl)1 GarbageCollectorMXBean (com.sun.management.GarbageCollectorMXBean)1 IOException (java.io.IOException)1 java.lang.management (java.lang.management)1 MemoryManagerMXBean (java.lang.management.MemoryManagerMXBean)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 javax.management (javax.management)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1