use of com.sun.btrace.shared.LowMemoryHandler in project btrace by btraceio.
the class BTraceRuntime method startImpl.
private void startImpl() {
if (timerHandlers != null) {
timer = new Timer(true);
TimerTask[] timerTasks = new TimerTask[timerHandlers.length];
wrapToTimerTasks(timerTasks);
for (int index = 0; index < timerHandlers.length; index++) {
final TimerHandler th = timerHandlers[index];
long period = th.period;
String periodArg = th.periodArg;
if (periodArg != null) {
period = parseLong(args.template(periodArg), period);
}
timer.schedule(timerTasks[index], period, period);
}
}
if (lowMemoryHandlers != null) {
initMemoryMBean();
initMemoryListener();
initMemoryPoolList();
lowMemoryHandlerMap = new HashMap<>();
for (LowMemoryHandler lmh : lowMemoryHandlers) {
String poolName = args.template(lmh.pool);
lowMemoryHandlerMap.put(poolName, lmh);
}
for (MemoryPoolMXBean mpoolBean : memPoolList) {
String name = mpoolBean.getName();
LowMemoryHandler lmh = lowMemoryHandlerMap.get(name);
if (lmh != null) {
if (mpoolBean.isUsageThresholdSupported()) {
mpoolBean.setUsageThreshold(lmh.threshold);
}
}
}
NotificationEmitter emitter = (NotificationEmitter) memoryMBean;
emitter.addNotificationListener(memoryListener, null, null);
}
leave();
}
use of com.sun.btrace.shared.LowMemoryHandler in project btrace by btraceio.
the class BTraceRuntime method initMemoryListener.
private void initMemoryListener() {
initThreadPool();
memoryListener = new NotificationListener() {
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void handleNotification(Notification notif, Object handback) {
boolean entered = BTraceRuntime.enter(BTraceRuntime.this);
try {
String notifType = notif.getType();
if (notifType.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
CompositeData cd = (CompositeData) notif.getUserData();
final MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);
String name = info.getPoolName();
final LowMemoryHandler handler = lowMemoryHandlerMap.get(name);
if (handler != null) {
threadPool.submit(new Runnable() {
@Override
public void run() {
boolean entered = BTraceRuntime.enter(BTraceRuntime.this);
try {
if (handler.trackUsage) {
handler.invoke(clazz, null, info.getUsage());
} else {
handler.invoke(clazz, null, (Object[]) null);
}
} catch (Throwable th) {
} finally {
if (entered) {
BTraceRuntime.leave();
}
}
}
});
}
}
} finally {
if (entered) {
BTraceRuntime.leave();
}
}
}
};
}
Aggregations