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();
}
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);
}
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");
}
}
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);
}
}
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);
}
Aggregations