Search in sources :

Example 6 with NotificationEmitter

use of javax.management.NotificationEmitter in project jdk8u_jdk by JetBrains.

the class CounterMonitorThresholdTest method runTest.

public static void runTest(int offset, int[] counter, int[] derivedGauge, int[] threshold) throws Exception {
    // Retrieve the platform MBean server
    //
    System.out.println("\nRetrieve the platform MBean server");
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    String domain = mbs.getDefaultDomain();
    // Create and register TestMBean
    //
    ObjectName name = new ObjectName(domain + ":type=" + Test.class.getName() + ",offset=" + offset);
    mbs.createMBean(Test.class.getName(), name);
    TestMBean mbean = (TestMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, name, TestMBean.class, false);
    // Create and register CounterMonitorMBean
    //
    ObjectName cmn = new ObjectName(domain + ":type=" + CounterMonitor.class.getName() + ",offset=" + offset);
    CounterMonitor m = new CounterMonitor();
    mbs.registerMBean(m, cmn);
    CounterMonitorMBean cm = (CounterMonitorMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, cmn, CounterMonitorMBean.class, true);
    ((NotificationEmitter) cm).addNotificationListener(new Listener(), null, null);
    cm.addObservedObject(name);
    cm.setObservedAttribute("Counter");
    cm.setGranularityPeriod(100);
    cm.setInitThreshold(1);
    cm.setOffset(offset);
    cm.setModulus(5);
    cm.setNotify(true);
    // Start the monitor
    //
    System.out.println("\nStart monitoring...");
    cm.start();
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // see 8025207
        do {
            Thread.sleep(150);
            derivedGaugeValue = (Integer) cm.getDerivedGauge(name);
        } while (derivedGaugeValue.intValue() != derivedGauge[i]);
        Number thresholdValue = cm.getThreshold(name);
        System.out.println("Threshold = " + thresholdValue);
        if (thresholdValue.intValue() != threshold[i]) {
            System.out.println("Wrong threshold! Current value = " + thresholdValue + " Expected value = " + threshold[i]);
            System.out.println("\nStop monitoring...");
            cm.stop();
            throw new IllegalArgumentException("wrong threshold");
        }
    }
    // Stop the monitor
    //
    System.out.println("\nStop monitoring...");
    cm.stop();
}
Also used : NotificationListener(javax.management.NotificationListener) ObjectName(javax.management.ObjectName) NotificationEmitter(javax.management.NotificationEmitter) CounterMonitorMBean(javax.management.monitor.CounterMonitorMBean) CounterMonitor(javax.management.monitor.CounterMonitor) MBeanServer(javax.management.MBeanServer)

Example 7 with NotificationEmitter

use of javax.management.NotificationEmitter in project geode by apache.

the class HeapMemoryMonitor method startJVMThresholdListener.

/**
   * Register with the JVM to get threshold events.
   * 
   * Package private for testing.
   */
void startJVMThresholdListener() {
    final MemoryPoolMXBean memoryPoolMXBean = getTenuredMemoryPoolMXBean();
    // appropriate value.
    if (!testDisableMemoryUpdates) {
        memoryPoolMXBean.setCollectionUsageThreshold(1);
    }
    final long usageThreshold = memoryPoolMXBean.getUsageThreshold();
    this.cache.getLoggerI18n().info(LocalizedStrings.HeapMemoryMonitor_OVERRIDDING_MEMORYPOOLMXBEAN_HEAP_0_NAME_1, new Object[] { usageThreshold, memoryPoolMXBean.getName() });
    MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
    NotificationEmitter emitter = (NotificationEmitter) mbean;
    emitter.addNotificationListener(this, null, null);
}
Also used : MemoryMXBean(java.lang.management.MemoryMXBean) NotificationEmitter(javax.management.NotificationEmitter) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 8 with NotificationEmitter

use of javax.management.NotificationEmitter in project bazel by bazelbuild.

the class RetainedHeapLimiter method install.

void install() {
    Preconditions.checkState(!installed, "RetainedHeapLimiter installed twice");
    installed = true;
    List<GarbageCollectorMXBean> gcbeans = ManagementFactory.getGarbageCollectorMXBeans();
    boolean foundTenured = false;
    // space. Normally there is one such collector.
    for (GarbageCollectorMXBean gcbean : gcbeans) {
        boolean collectsTenured = false;
        for (String name : gcbean.getMemoryPoolNames()) {
            collectsTenured |= isTenuredSpace(name);
        }
        if (collectsTenured) {
            foundTenured = true;
            NotificationEmitter emitter = (NotificationEmitter) gcbean;
            emitter.addNotificationListener(this, null, null);
        }
    }
    if (!foundTenured) {
        throw new IllegalStateException("Can't find tenured space; update this class for a new collector");
    }
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean)

Example 9 with NotificationEmitter

use of javax.management.NotificationEmitter in project graylog2-server by Graylog2.

the class GarbageCollectionWarningThread method doRun.

@Override
public void doRun() {
    for (final GarbageCollectorMXBean gc : garbageCollectors) {
        switch(gc.getName()) {
            case "ParNew":
            case "ConcurrentMarkSweep":
                LOG.debug("Skipping GC warning listener for concurrent collector {}.", gc.getName());
                continue;
        }
        LOG.debug("Installing GC warning listener for collector {}, total runtime threshold is {}.", gc.getName(), gcWarningThreshold);
        final NotificationEmitter emitter = (NotificationEmitter) gc;
        final NotificationListener listener = new NotificationListener() {

            @Override
            public void handleNotification(javax.management.Notification notification, Object handback) {
                if (GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION.equals(notification.getType())) {
                    final GcInfo gcInfo = GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData()).getGcInfo();
                    final Duration duration = Duration.milliseconds(gcInfo.getDuration());
                    if (duration.compareTo(gcWarningThreshold) > 0) {
                        LOG.warn("Last GC run with {} took longer than {} (last duration={})", gc.getName(), gcWarningThreshold, duration);
                        final Notification systemNotification = notificationService.buildNow().addNode(nodeId.toString()).addTimestamp(Tools.nowUTC()).addSeverity(Notification.Severity.URGENT).addType(Notification.Type.GC_TOO_LONG).addDetail("gc_name", gc.getName()).addDetail("gc_duration_ms", duration.toMilliseconds()).addDetail("gc_threshold_ms", gcWarningThreshold.toMilliseconds()).addDetail("gc_collection_count", gc.getCollectionCount()).addDetail("gc_collection_time", gc.getCollectionTime());
                        if (!notificationService.publishIfFirst(systemNotification)) {
                            LOG.debug("Couldn't publish notification: {}", notification);
                        }
                    }
                }
            }
        };
        emitter.addNotificationListener(listener, null, null);
    }
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) GcInfo(com.sun.management.GcInfo) CompositeData(javax.management.openmbean.CompositeData) GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) Duration(com.github.joschi.jadconfig.util.Duration) Notification(org.graylog2.notifications.Notification) NotificationListener(javax.management.NotificationListener)

Example 10 with NotificationEmitter

use of javax.management.NotificationEmitter in project intellij-community by JetBrains.

the class PerformanceWatcher method watchCodeCache.

private void watchCodeCache(final MemoryPoolMXBean bean) {
    final long threshold = bean.getUsage().getMax() - 5 * 1024 * 1024;
    if (!bean.isUsageThresholdSupported() || threshold <= 0)
        return;
    bean.setUsageThreshold(threshold);
    final NotificationEmitter emitter = (NotificationEmitter) ManagementFactory.getMemoryMXBean();
    emitter.addNotificationListener(new NotificationListener() {

        @Override
        public void handleNotification(Notification n, Object hb) {
            if (bean.getUsage().getUsed() > threshold) {
                LOG.info("Code Cache is almost full");
                dumpThreads("codeCacheFull", true);
                try {
                    emitter.removeNotificationListener(this);
                } catch (ListenerNotFoundException e) {
                    LOG.error(e);
                }
            }
        }
    }, null, null);
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) ListenerNotFoundException(javax.management.ListenerNotFoundException) Notification(javax.management.Notification) NotificationListener(javax.management.NotificationListener)

Aggregations

NotificationEmitter (javax.management.NotificationEmitter)16 NotificationListener (javax.management.NotificationListener)7 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)5 ListenerNotFoundException (javax.management.ListenerNotFoundException)5 MemoryMXBean (java.lang.management.MemoryMXBean)3 Notification (javax.management.Notification)3 ObjectName (javax.management.ObjectName)3 ScanState (com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState)2 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 AttributeChangeNotification (javax.management.AttributeChangeNotification)2 MBeanServer (javax.management.MBeanServer)2 NotificationFilter (javax.management.NotificationFilter)2 CounterMonitor (javax.management.monitor.CounterMonitor)2 CounterMonitorMBean (javax.management.monitor.CounterMonitorMBean)2 Duration (com.github.joschi.jadconfig.util.Duration)1 GcInfo (com.sun.management.GcInfo)1 IOException (java.io.IOException)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Proxy (java.lang.reflect.Proxy)1