Search in sources :

Example 1 with LowMemoryHandler

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();
}
Also used : TimerHandler(com.sun.btrace.shared.TimerHandler) NotificationEmitter(javax.management.NotificationEmitter) Timer(java.util.Timer) TimerTask(java.util.TimerTask) LowMemoryHandler(com.sun.btrace.shared.LowMemoryHandler) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 2 with LowMemoryHandler

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();
                }
            }
        }
    };
}
Also used : MemoryNotificationInfo(java.lang.management.MemoryNotificationInfo) CompositeData(javax.management.openmbean.CompositeData) LowMemoryHandler(com.sun.btrace.shared.LowMemoryHandler) Notification(javax.management.Notification) NotificationListener(javax.management.NotificationListener)

Aggregations

LowMemoryHandler (com.sun.btrace.shared.LowMemoryHandler)2 TimerHandler (com.sun.btrace.shared.TimerHandler)1 MemoryNotificationInfo (java.lang.management.MemoryNotificationInfo)1 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 Notification (javax.management.Notification)1 NotificationEmitter (javax.management.NotificationEmitter)1 NotificationListener (javax.management.NotificationListener)1 CompositeData (javax.management.openmbean.CompositeData)1