Java Code Examples for javax.management.monitor.CounterMonitorMBean#addObservedObject()

The following examples show how to use javax.management.monitor.CounterMonitorMBean#addObservedObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CounterMonitorDeadlockTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
void run() throws Exception {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ObjectName observedName = new ObjectName("a:b=c");
    final ObjectName monitorName = new ObjectName("a:type=Monitor");
    mbs.registerMBean(new CounterMonitor(), monitorName);
    final CounterMonitorMBean monitorProxy =
        JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class);
    final TestMBean observedProxy =
        JMX.newMBeanProxy(mbs, observedName, TestMBean.class);

    final Runnable sensitiveThing = new Runnable() {
        public void run() {
            doSensitiveThing(monitorProxy, observedName);
        }
    };

    final Runnable nothing = new Runnable() {
        public void run() {}
    };

    final Runnable withinGetAttribute =
        (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;

    mbs.registerMBean(new Test(withinGetAttribute), observedName);
    monitorProxy.addObservedObject(observedName);
    monitorProxy.setObservedAttribute("Thing");
    monitorProxy.setInitThreshold(100);
    monitorProxy.setGranularityPeriod(10L); // 10 ms
    monitorProxy.setNotify(true);

    final int initGetCount = observedProxy.getGetCount();
    monitorProxy.start();

    System.out.println("Checking GetCount, possible deadlock if timeout.");
    do { // 8038322. Until timeout of testing harness
        Thread.sleep(200);
    } while ((observedProxy.getGetCount()) == initGetCount);
    System.out.println("Done!");

    // This won't show up as a deadlock in CTRL-\ or in
    // ThreadMXBean.findDeadlockedThreads(), because they don't
    // see that thread A is waiting for thread B (B.join()), and
    // thread B is waiting for a lock held by thread A

    // Now we know the monitor has observed the initial value,
    // so if we want to test notify behaviour we can trigger by
    // exceeding the threshold.
    if (when == When.IN_NOTIFY) {
        final AtomicInteger notifCount = new AtomicInteger();
        final NotificationListener listener = new NotificationListener() {
            public void handleNotification(Notification n, Object h) {
                Thread t = new Thread(sensitiveThing);
                t.start();
                try {
                    t.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                notifCount.incrementAndGet();
            }
        };
        mbs.addNotificationListener(monitorName, listener, null, null);
        observedProxy.setThing(1000);
        System.out.println("Waiting notifCount.get() != 0, possible deadlock if timeout.");
        do {
            Thread.sleep(200);
        } while(notifCount.get() == 0); // 8038322. Until timeout of testing harness
        System.out.println("Done");
    }

}
 
Example 2
Source File: CounterMonitorThresholdTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
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();

    // Play with counter
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // either pass or test timeout (killed by test harness)
        // 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();
}
 
Example 3
Source File: CounterMonitorThresholdTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
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();

    // Play with counter
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // either pass or test timeout (killed by test harness)
        // 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();
}
 
Example 4
Source File: CounterMonitorDeadlockTest.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
void run() throws Exception {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ObjectName observedName = new ObjectName("a:b=c");
    final ObjectName monitorName = new ObjectName("a:type=Monitor");
    mbs.registerMBean(new CounterMonitor(), monitorName);
    final CounterMonitorMBean monitorProxy =
        JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class);
    final TestMBean observedProxy =
        JMX.newMBeanProxy(mbs, observedName, TestMBean.class);

    final Runnable sensitiveThing = new Runnable() {
        public void run() {
            doSensitiveThing(monitorProxy, observedName);
        }
    };

    final Runnable nothing = new Runnable() {
        public void run() {}
    };

    final Runnable withinGetAttribute =
        (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;

    mbs.registerMBean(new Test(withinGetAttribute), observedName);
    monitorProxy.addObservedObject(observedName);
    monitorProxy.setObservedAttribute("Thing");
    monitorProxy.setInitThreshold(100);
    monitorProxy.setGranularityPeriod(10L); // 10 ms
    monitorProxy.setNotify(true);
    monitorProxy.start();

    final int initGetCount = observedProxy.getGetCount();
    int getCount = initGetCount;
    for (int i = 0; i < 500; i++) { // 500 * 10 = 5 seconds
        getCount = observedProxy.getGetCount();
        if (getCount != initGetCount)
            break;
        Thread.sleep(10);
    }
    if (getCount <= initGetCount)
        throw new Exception("Test failed: presumable deadlock");
    // This won't show up as a deadlock in CTRL-\ or in
    // ThreadMXBean.findDeadlockedThreads(), because they don't
    // see that thread A is waiting for thread B (B.join()), and
    // thread B is waiting for a lock held by thread A

    // Now we know the monitor has observed the initial value,
    // so if we want to test notify behaviour we can trigger by
    // exceeding the threshold.
    if (when == When.IN_NOTIFY) {
        final AtomicInteger notifCount = new AtomicInteger();
        final NotificationListener listener = new NotificationListener() {
            public void handleNotification(Notification n, Object h) {
                Thread t = new Thread(sensitiveThing);
                t.start();
                try {
                    t.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                notifCount.incrementAndGet();
            }
        };
        mbs.addNotificationListener(monitorName, listener, null, null);
        observedProxy.setThing(1000);
        for (int i = 0; i < 500 && notifCount.get() == 0; i++)
            Thread.sleep(10);
        if (notifCount.get() == 0)
            throw new Exception("Test failed: presumable deadlock");
    }

}
 
Example 5
Source File: CounterMonitorThresholdTest.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
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();

    // Play with counter
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // either pass or test timeout (killed by test harness)
        // 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();
}
 
Example 6
Source File: CounterMonitorDeadlockTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
void run() throws Exception {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ObjectName observedName = new ObjectName("a:b=c");
    final ObjectName monitorName = new ObjectName("a:type=Monitor");
    mbs.registerMBean(new CounterMonitor(), monitorName);
    final CounterMonitorMBean monitorProxy =
        JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class);
    final TestMBean observedProxy =
        JMX.newMBeanProxy(mbs, observedName, TestMBean.class);

    final Runnable sensitiveThing = new Runnable() {
        public void run() {
            doSensitiveThing(monitorProxy, observedName);
        }
    };

    final Runnable nothing = new Runnable() {
        public void run() {}
    };

    final Runnable withinGetAttribute =
        (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;

    mbs.registerMBean(new Test(withinGetAttribute), observedName);
    monitorProxy.addObservedObject(observedName);
    monitorProxy.setObservedAttribute("Thing");
    monitorProxy.setInitThreshold(100);
    monitorProxy.setGranularityPeriod(10L); // 10 ms
    monitorProxy.setNotify(true);

    final int initGetCount = observedProxy.getGetCount();
    monitorProxy.start();

    System.out.println("Checking GetCount, possible deadlock if timeout.");
    do { // 8038322. Until timeout of testing harness
        Thread.sleep(200);
    } while ((observedProxy.getGetCount()) == initGetCount);
    System.out.println("Done!");

    // This won't show up as a deadlock in CTRL-\ or in
    // ThreadMXBean.findDeadlockedThreads(), because they don't
    // see that thread A is waiting for thread B (B.join()), and
    // thread B is waiting for a lock held by thread A

    // Now we know the monitor has observed the initial value,
    // so if we want to test notify behaviour we can trigger by
    // exceeding the threshold.
    if (when == When.IN_NOTIFY) {
        final AtomicInteger notifCount = new AtomicInteger();
        final NotificationListener listener = new NotificationListener() {
            public void handleNotification(Notification n, Object h) {
                Thread t = new Thread(sensitiveThing);
                t.start();
                try {
                    t.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                notifCount.incrementAndGet();
            }
        };
        mbs.addNotificationListener(monitorName, listener, null, null);
        observedProxy.setThing(1000);
        System.out.println("Waiting notifCount.get() != 0, possible deadlock if timeout.");
        do {
            Thread.sleep(200);
        } while(notifCount.get() == 0); // 8038322. Until timeout of testing harness
        System.out.println("Done");
    }

}
 
Example 7
Source File: CounterMonitorThresholdTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
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();

    // Play with counter
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // either pass or test timeout (killed by test harness)
        // 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();
}
 
Example 8
Source File: CounterMonitorDeadlockTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
void run() throws Exception {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ObjectName observedName = new ObjectName("a:b=c");
    final ObjectName monitorName = new ObjectName("a:type=Monitor");
    mbs.registerMBean(new CounterMonitor(), monitorName);
    final CounterMonitorMBean monitorProxy =
        JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class);
    final TestMBean observedProxy =
        JMX.newMBeanProxy(mbs, observedName, TestMBean.class);

    final Runnable sensitiveThing = new Runnable() {
        public void run() {
            doSensitiveThing(monitorProxy, observedName);
        }
    };

    final Runnable nothing = new Runnable() {
        public void run() {}
    };

    final Runnable withinGetAttribute =
        (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;

    mbs.registerMBean(new Test(withinGetAttribute), observedName);
    monitorProxy.addObservedObject(observedName);
    monitorProxy.setObservedAttribute("Thing");
    monitorProxy.setInitThreshold(100);
    monitorProxy.setGranularityPeriod(10L); // 10 ms
    monitorProxy.setNotify(true);

    final int initGetCount = observedProxy.getGetCount();
    monitorProxy.start();

    System.out.println("Checking GetCount, possible deadlock if timeout.");
    do { // 8038322. Until timeout of testing harness
        Thread.sleep(200);
    } while ((observedProxy.getGetCount()) == initGetCount);
    System.out.println("Done!");

    // This won't show up as a deadlock in CTRL-\ or in
    // ThreadMXBean.findDeadlockedThreads(), because they don't
    // see that thread A is waiting for thread B (B.join()), and
    // thread B is waiting for a lock held by thread A

    // Now we know the monitor has observed the initial value,
    // so if we want to test notify behaviour we can trigger by
    // exceeding the threshold.
    if (when == When.IN_NOTIFY) {
        final AtomicInteger notifCount = new AtomicInteger();
        final NotificationListener listener = new NotificationListener() {
            public void handleNotification(Notification n, Object h) {
                Thread t = new Thread(sensitiveThing);
                t.start();
                try {
                    t.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                notifCount.incrementAndGet();
            }
        };
        mbs.addNotificationListener(monitorName, listener, null, null);
        observedProxy.setThing(1000);
        System.out.println("Waiting notifCount.get() != 0, possible deadlock if timeout.");
        do {
            Thread.sleep(200);
        } while(notifCount.get() == 0); // 8038322. Until timeout of testing harness
        System.out.println("Done");
    }

}
 
Example 9
Source File: CounterMonitorThresholdTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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();

    // Play with counter
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // either pass or test timeout (killed by test harness)
        // 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();
}
 
Example 10
Source File: CounterMonitorDeadlockTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
void run() throws Exception {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ObjectName observedName = new ObjectName("a:b=c");
    final ObjectName monitorName = new ObjectName("a:type=Monitor");
    mbs.registerMBean(new CounterMonitor(), monitorName);
    final CounterMonitorMBean monitorProxy =
        JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class);
    final TestMBean observedProxy =
        JMX.newMBeanProxy(mbs, observedName, TestMBean.class);

    final Runnable sensitiveThing = new Runnable() {
        public void run() {
            doSensitiveThing(monitorProxy, observedName);
        }
    };

    final Runnable nothing = new Runnable() {
        public void run() {}
    };

    final Runnable withinGetAttribute =
        (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;

    mbs.registerMBean(new Test(withinGetAttribute), observedName);
    monitorProxy.addObservedObject(observedName);
    monitorProxy.setObservedAttribute("Thing");
    monitorProxy.setInitThreshold(100);
    monitorProxy.setGranularityPeriod(10L); // 10 ms
    monitorProxy.setNotify(true);

    final int initGetCount = observedProxy.getGetCount();
    monitorProxy.start();

    System.out.println("Checking GetCount, possible deadlock if timeout.");
    do { // 8038322. Until timeout of testing harness
        Thread.sleep(200);
    } while ((observedProxy.getGetCount()) == initGetCount);
    System.out.println("Done!");

    // This won't show up as a deadlock in CTRL-\ or in
    // ThreadMXBean.findDeadlockedThreads(), because they don't
    // see that thread A is waiting for thread B (B.join()), and
    // thread B is waiting for a lock held by thread A

    // Now we know the monitor has observed the initial value,
    // so if we want to test notify behaviour we can trigger by
    // exceeding the threshold.
    if (when == When.IN_NOTIFY) {
        final AtomicInteger notifCount = new AtomicInteger();
        final NotificationListener listener = new NotificationListener() {
            public void handleNotification(Notification n, Object h) {
                Thread t = new Thread(sensitiveThing);
                t.start();
                try {
                    t.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                notifCount.incrementAndGet();
            }
        };
        mbs.addNotificationListener(monitorName, listener, null, null);
        observedProxy.setThing(1000);
        System.out.println("Waiting notifCount.get() != 0, possible deadlock if timeout.");
        do {
            Thread.sleep(200);
        } while(notifCount.get() == 0); // 8038322. Until timeout of testing harness
        System.out.println("Done");
    }

}
 
Example 11
Source File: CounterMonitorThresholdTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
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();

    // Play with counter
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // either pass or test timeout (killed by test harness)
        // 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();
}
 
Example 12
Source File: CounterMonitorDeadlockTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
void run() throws Exception {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ObjectName observedName = new ObjectName("a:b=c");
    final ObjectName monitorName = new ObjectName("a:type=Monitor");
    mbs.registerMBean(new CounterMonitor(), monitorName);
    final CounterMonitorMBean monitorProxy =
        JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class);
    final TestMBean observedProxy =
        JMX.newMBeanProxy(mbs, observedName, TestMBean.class);

    final Runnable sensitiveThing = new Runnable() {
        public void run() {
            doSensitiveThing(monitorProxy, observedName);
        }
    };

    final Runnable nothing = new Runnable() {
        public void run() {}
    };

    final Runnable withinGetAttribute =
        (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;

    mbs.registerMBean(new Test(withinGetAttribute), observedName);
    monitorProxy.addObservedObject(observedName);
    monitorProxy.setObservedAttribute("Thing");
    monitorProxy.setInitThreshold(100);
    monitorProxy.setGranularityPeriod(10L); // 10 ms
    monitorProxy.setNotify(true);

    final int initGetCount = observedProxy.getGetCount();
    monitorProxy.start();

    System.out.println("Checking GetCount, possible deadlock if timeout.");
    do { // 8038322. Until timeout of testing harness
        Thread.sleep(200);
    } while ((observedProxy.getGetCount()) == initGetCount);
    System.out.println("Done!");

    // This won't show up as a deadlock in CTRL-\ or in
    // ThreadMXBean.findDeadlockedThreads(), because they don't
    // see that thread A is waiting for thread B (B.join()), and
    // thread B is waiting for a lock held by thread A

    // Now we know the monitor has observed the initial value,
    // so if we want to test notify behaviour we can trigger by
    // exceeding the threshold.
    if (when == When.IN_NOTIFY) {
        final AtomicInteger notifCount = new AtomicInteger();
        final NotificationListener listener = new NotificationListener() {
            public void handleNotification(Notification n, Object h) {
                Thread t = new Thread(sensitiveThing);
                t.start();
                try {
                    t.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                notifCount.incrementAndGet();
            }
        };
        mbs.addNotificationListener(monitorName, listener, null, null);
        observedProxy.setThing(1000);
        System.out.println("Waiting notifCount.get() != 0, possible deadlock if timeout.");
        do {
            Thread.sleep(200);
        } while(notifCount.get() == 0); // 8038322. Until timeout of testing harness
        System.out.println("Done");
    }

}
 
Example 13
Source File: CounterMonitorThresholdTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
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();

    // Play with counter
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // either pass or test timeout (killed by test harness)
        // 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();
}
 
Example 14
Source File: CounterMonitorDeadlockTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
void run() throws Exception {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ObjectName observedName = new ObjectName("a:b=c");
    final ObjectName monitorName = new ObjectName("a:type=Monitor");
    mbs.registerMBean(new CounterMonitor(), monitorName);
    final CounterMonitorMBean monitorProxy =
        JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class);
    final TestMBean observedProxy =
        JMX.newMBeanProxy(mbs, observedName, TestMBean.class);

    final Runnable sensitiveThing = new Runnable() {
        public void run() {
            doSensitiveThing(monitorProxy, observedName);
        }
    };

    final Runnable nothing = new Runnable() {
        public void run() {}
    };

    final Runnable withinGetAttribute =
        (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;

    mbs.registerMBean(new Test(withinGetAttribute), observedName);
    monitorProxy.addObservedObject(observedName);
    monitorProxy.setObservedAttribute("Thing");
    monitorProxy.setInitThreshold(100);
    monitorProxy.setGranularityPeriod(10L); // 10 ms
    monitorProxy.setNotify(true);

    final int initGetCount = observedProxy.getGetCount();
    monitorProxy.start();

    System.out.println("Checking GetCount, possible deadlock if timeout.");
    do { // 8038322. Until timeout of testing harness
        Thread.sleep(200);
    } while ((observedProxy.getGetCount()) == initGetCount);
    System.out.println("Done!");

    // This won't show up as a deadlock in CTRL-\ or in
    // ThreadMXBean.findDeadlockedThreads(), because they don't
    // see that thread A is waiting for thread B (B.join()), and
    // thread B is waiting for a lock held by thread A

    // Now we know the monitor has observed the initial value,
    // so if we want to test notify behaviour we can trigger by
    // exceeding the threshold.
    if (when == When.IN_NOTIFY) {
        final AtomicInteger notifCount = new AtomicInteger();
        final NotificationListener listener = new NotificationListener() {
            public void handleNotification(Notification n, Object h) {
                Thread t = new Thread(sensitiveThing);
                t.start();
                try {
                    t.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                notifCount.incrementAndGet();
            }
        };
        mbs.addNotificationListener(monitorName, listener, null, null);
        observedProxy.setThing(1000);
        System.out.println("Waiting notifCount.get() != 0, possible deadlock if timeout.");
        do {
            Thread.sleep(200);
        } while(notifCount.get() == 0); // 8038322. Until timeout of testing harness
        System.out.println("Done");
    }

}
 
Example 15
Source File: CounterMonitorThresholdTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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();

    // Play with counter
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // either pass or test timeout (killed by test harness)
        // 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();
}
 
Example 16
Source File: CounterMonitorDeadlockTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
void run() throws Exception {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ObjectName observedName = new ObjectName("a:b=c");
    final ObjectName monitorName = new ObjectName("a:type=Monitor");
    mbs.registerMBean(new CounterMonitor(), monitorName);
    final CounterMonitorMBean monitorProxy =
        JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class);
    final TestMBean observedProxy =
        JMX.newMBeanProxy(mbs, observedName, TestMBean.class);

    final Runnable sensitiveThing = new Runnable() {
        public void run() {
            doSensitiveThing(monitorProxy, observedName);
        }
    };

    final Runnable nothing = new Runnable() {
        public void run() {}
    };

    final Runnable withinGetAttribute =
        (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;

    mbs.registerMBean(new Test(withinGetAttribute), observedName);
    monitorProxy.addObservedObject(observedName);
    monitorProxy.setObservedAttribute("Thing");
    monitorProxy.setInitThreshold(100);
    monitorProxy.setGranularityPeriod(10L); // 10 ms
    monitorProxy.setNotify(true);

    final int initGetCount = observedProxy.getGetCount();
    monitorProxy.start();

    System.out.println("Checking GetCount, possible deadlock if timeout.");
    do { // 8038322. Until timeout of testing harness
        Thread.sleep(200);
    } while ((observedProxy.getGetCount()) == initGetCount);
    System.out.println("Done!");

    // This won't show up as a deadlock in CTRL-\ or in
    // ThreadMXBean.findDeadlockedThreads(), because they don't
    // see that thread A is waiting for thread B (B.join()), and
    // thread B is waiting for a lock held by thread A

    // Now we know the monitor has observed the initial value,
    // so if we want to test notify behaviour we can trigger by
    // exceeding the threshold.
    if (when == When.IN_NOTIFY) {
        final AtomicInteger notifCount = new AtomicInteger();
        final NotificationListener listener = new NotificationListener() {
            public void handleNotification(Notification n, Object h) {
                Thread t = new Thread(sensitiveThing);
                t.start();
                try {
                    t.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                notifCount.incrementAndGet();
            }
        };
        mbs.addNotificationListener(monitorName, listener, null, null);
        observedProxy.setThing(1000);
        System.out.println("Waiting notifCount.get() != 0, possible deadlock if timeout.");
        do {
            Thread.sleep(200);
        } while(notifCount.get() == 0); // 8038322. Until timeout of testing harness
        System.out.println("Done");
    }

}
 
Example 17
Source File: CounterMonitorThresholdTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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();

    // Play with counter
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // either pass or test timeout (killed by test harness)
        // 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();
}
 
Example 18
Source File: CounterMonitorDeadlockTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
void run() throws Exception {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ObjectName observedName = new ObjectName("a:b=c");
    final ObjectName monitorName = new ObjectName("a:type=Monitor");
    mbs.registerMBean(new CounterMonitor(), monitorName);
    final CounterMonitorMBean monitorProxy =
        JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class);
    final TestMBean observedProxy =
        JMX.newMBeanProxy(mbs, observedName, TestMBean.class);

    final Runnable sensitiveThing = new Runnable() {
        public void run() {
            doSensitiveThing(monitorProxy, observedName);
        }
    };

    final Runnable nothing = new Runnable() {
        public void run() {}
    };

    final Runnable withinGetAttribute =
        (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;

    mbs.registerMBean(new Test(withinGetAttribute), observedName);
    monitorProxy.addObservedObject(observedName);
    monitorProxy.setObservedAttribute("Thing");
    monitorProxy.setInitThreshold(100);
    monitorProxy.setGranularityPeriod(10L); // 10 ms
    monitorProxy.setNotify(true);

    final int initGetCount = observedProxy.getGetCount();
    monitorProxy.start();

    System.out.println("Checking GetCount, possible deadlock if timeout.");
    do { // 8038322. Until timeout of testing harness
        Thread.sleep(200);
    } while ((observedProxy.getGetCount()) == initGetCount);
    System.out.println("Done!");

    // This won't show up as a deadlock in CTRL-\ or in
    // ThreadMXBean.findDeadlockedThreads(), because they don't
    // see that thread A is waiting for thread B (B.join()), and
    // thread B is waiting for a lock held by thread A

    // Now we know the monitor has observed the initial value,
    // so if we want to test notify behaviour we can trigger by
    // exceeding the threshold.
    if (when == When.IN_NOTIFY) {
        final AtomicInteger notifCount = new AtomicInteger();
        final NotificationListener listener = new NotificationListener() {
            public void handleNotification(Notification n, Object h) {
                Thread t = new Thread(sensitiveThing);
                t.start();
                try {
                    t.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                notifCount.incrementAndGet();
            }
        };
        mbs.addNotificationListener(monitorName, listener, null, null);
        observedProxy.setThing(1000);
        System.out.println("Waiting notifCount.get() != 0, possible deadlock if timeout.");
        do {
            Thread.sleep(200);
        } while(notifCount.get() == 0); // 8038322. Until timeout of testing harness
        System.out.println("Done");
    }

}
 
Example 19
Source File: CounterMonitorThresholdTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
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();

    // Play with counter
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // either pass or test timeout (killed by test harness)
        // 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();
}
 
Example 20
Source File: CounterMonitorDeadlockTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
void run() throws Exception {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ObjectName observedName = new ObjectName("a:b=c");
    final ObjectName monitorName = new ObjectName("a:type=Monitor");
    mbs.registerMBean(new CounterMonitor(), monitorName);
    final CounterMonitorMBean monitorProxy =
        JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class);
    final TestMBean observedProxy =
        JMX.newMBeanProxy(mbs, observedName, TestMBean.class);

    final Runnable sensitiveThing = new Runnable() {
        public void run() {
            doSensitiveThing(monitorProxy, observedName);
        }
    };

    final Runnable nothing = new Runnable() {
        public void run() {}
    };

    final Runnable withinGetAttribute =
        (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;

    mbs.registerMBean(new Test(withinGetAttribute), observedName);
    monitorProxy.addObservedObject(observedName);
    monitorProxy.setObservedAttribute("Thing");
    monitorProxy.setInitThreshold(100);
    monitorProxy.setGranularityPeriod(10L); // 10 ms
    monitorProxy.setNotify(true);

    final int initGetCount = observedProxy.getGetCount();
    monitorProxy.start();

    System.out.println("Checking GetCount, possible deadlock if timeout.");
    do { // 8038322. Until timeout of testing harness
        Thread.sleep(200);
    } while ((observedProxy.getGetCount()) == initGetCount);
    System.out.println("Done!");

    // This won't show up as a deadlock in CTRL-\ or in
    // ThreadMXBean.findDeadlockedThreads(), because they don't
    // see that thread A is waiting for thread B (B.join()), and
    // thread B is waiting for a lock held by thread A

    // Now we know the monitor has observed the initial value,
    // so if we want to test notify behaviour we can trigger by
    // exceeding the threshold.
    if (when == When.IN_NOTIFY) {
        final AtomicInteger notifCount = new AtomicInteger();
        final NotificationListener listener = new NotificationListener() {
            public void handleNotification(Notification n, Object h) {
                Thread t = new Thread(sensitiveThing);
                t.start();
                try {
                    t.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                notifCount.incrementAndGet();
            }
        };
        mbs.addNotificationListener(monitorName, listener, null, null);
        observedProxy.setThing(1000);
        System.out.println("Waiting notifCount.get() != 0, possible deadlock if timeout.");
        do {
            Thread.sleep(200);
        } while(notifCount.get() == 0); // 8038322. Until timeout of testing harness
        System.out.println("Done");
    }

}