Search in sources :

Example 16 with AttachProvider

use of com.sun.tools.attach.spi.AttachProvider in project jdk8u_jdk by JetBrains.

the class VirtualMachine method list.

/**
     * Return a list of Java virtual machines.
     *
     * <p> This method returns a list of Java {@link
     * com.sun.tools.attach.VirtualMachineDescriptor} elements.
     * The list is an aggregation of the virtual machine
     * descriptor lists obtained by invoking the {@link
     * com.sun.tools.attach.spi.AttachProvider#listVirtualMachines
     * listVirtualMachines} method of all installed
     * {@link com.sun.tools.attach.spi.AttachProvider attach providers}.
     * If there are no Java virtual machines known to any provider
     * then an empty list is returned.
     *
     * @return  The list of virtual machine descriptors.
     */
public static List<VirtualMachineDescriptor> list() {
    ArrayList<VirtualMachineDescriptor> l = new ArrayList<VirtualMachineDescriptor>();
    List<AttachProvider> providers = AttachProvider.providers();
    for (AttachProvider provider : providers) {
        l.addAll(provider.listVirtualMachines());
    }
    return l;
}
Also used : ArrayList(java.util.ArrayList) AttachProvider(com.sun.tools.attach.spi.AttachProvider)

Example 17 with AttachProvider

use of com.sun.tools.attach.spi.AttachProvider in project oozie by apache.

the class TestMetricsInstrumentation method testJMXInstrumentation.

public void testJMXInstrumentation() throws Exception {
    final AttachProvider attachProvider = AttachProvider.providers().get(0);
    VirtualMachineDescriptor descriptor = null;
    // Setting the id of the VM unique, so we can find it.
    String uniqueId = UUID.randomUUID().toString();
    System.setProperty("processSettings.unique.id", uniqueId);
    // Finding our own VM by the id.
    for (VirtualMachineDescriptor d : VirtualMachine.list()) {
        String remoteUniqueId = VirtualMachine.attach(d).getSystemProperties().getProperty("processSettings.unique.id");
        if (remoteUniqueId != null && remoteUniqueId.equals(uniqueId)) {
            descriptor = d;
            break;
        }
    }
    assertNotNull("Could not find own virtual machine", descriptor);
    // Attaching JMX agent to our own VM
    final VirtualMachine virtualMachine = attachProvider.attachVirtualMachine(descriptor);
    String agent = virtualMachine.getSystemProperties().getProperty("java.home") + File.separator + "lib" + File.separator + "management-agent.jar";
    virtualMachine.loadAgent(agent);
    final Object portObject = virtualMachine.getAgentProperties().get("com.sun.management.jmxremote.localConnectorAddress");
    final JMXServiceURL target = new JMXServiceURL(portObject + "");
    JMXConnector jmxc = JMXConnectorFactory.connect(target);
    MBeanServerConnection conn = jmxc.getMBeanServerConnection();
    // Query a value through JMX from our own VM
    Object value = null;
    try {
        value = conn.getAttribute(new ObjectName("metrics:name=jvm.memory.heap.committed"), "Value");
    } catch (Exception e) {
        fail("Could not fetch metric");
    }
    assertNotNull("JMX service error", value);
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) AttachProvider(com.sun.tools.attach.spi.AttachProvider) MBeanServerConnection(javax.management.MBeanServerConnection) VirtualMachine(com.sun.tools.attach.VirtualMachine) ObjectName(javax.management.ObjectName)

Example 18 with AttachProvider

use of com.sun.tools.attach.spi.AttachProvider in project openj9 by eclipse.

the class AttachAPISanity method testAttachToSelfAndOthers.

/**
 * Test attaching to this VM when there are other VMs running
 */
@Test(groups = { "level.sanity" })
public void testAttachToSelfAndOthers() {
    final int numberOfTargets = 4;
    String myVmid = com.ibm.tools.attach.target.AttachHandler.getVmId();
    logger.debug("starting " + testName);
    TargetManager[] tgts = new TargetManager[numberOfTargets];
    for (int i = 0; i < numberOfTargets; ++i) {
        tgts[i] = new TargetManager(TestConstants.TARGET_VM_CLASS, null);
        tgts[i].syncWithTarget();
    }
    List<AttachProvider> providers = AttachProvider.providers();
    AttachProvider myProvider = providers.get(0);
    /*
		 * there is only one
		 * provider currently
		 */
    try {
        for (TargetManager t : tgts) {
            logger.debug("attach to other VM: " + t.targetId);
            VirtualMachine vm = myProvider.attachVirtualMachine(t.targetId);
            vm.detach();
            logger.debug("attach to self: " + myVmid);
            vm = myProvider.attachVirtualMachine(myVmid);
            vm.detach();
        }
    } catch (AttachNotSupportedException | IOException e) {
        logExceptionInfoAndFail(e);
    }
}
Also used : IOException(java.io.IOException) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException) AttachProvider(com.sun.tools.attach.spi.AttachProvider) VirtualMachine(com.sun.tools.attach.VirtualMachine) Test(org.testng.annotations.Test)

Example 19 with AttachProvider

use of com.sun.tools.attach.spi.AttachProvider in project openj9 by eclipse.

the class TestAttachAPI method test_vmname08.

@Test
public void test_vmname08() {
    logger.debug("starting " + testName);
    TargetManager target = null;
    VirtualMachine vm = null;
    try {
        String vmId = testName;
        target = new TargetManager(TestConstants.TARGET_VM_CLASS, vmId, vmId, null, null);
        target.syncWithTarget();
        assertTrue(vmIdExists(testName));
        List<AttachProvider> providers = com.sun.tools.attach.spi.AttachProvider.providers();
        AttachProvider myProvider = providers.get(0);
        logger.debug("attaching to " + vmId);
        vm = myProvider.attachVirtualMachine(vmId);
        Properties props = vm.getSystemProperties();
        String vendor = props.getProperty(TestConstants.VENDOR_NAME_PROP);
        String myVendor = System.getProperty(TestConstants.VENDOR_NAME_PROP);
        AssertJUnit.assertEquals(myVendor, vendor);
    } catch (AttachNotSupportedException | IOException e) {
        listIpcDir();
        logExceptionInfoAndFail(e);
    } finally {
        if (null != target) {
            target.terminateTarget();
        }
    }
}
Also used : IOException(java.io.IOException) Properties(java.util.Properties) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException) AttachProvider(com.sun.tools.attach.spi.AttachProvider) VirtualMachine(com.sun.tools.attach.VirtualMachine) Test(org.testng.annotations.Test)

Example 20 with AttachProvider

use of com.sun.tools.attach.spi.AttachProvider in project powermock by powermock.

the class VirtualMachine method list.

/**
 * <p>
 * Return a list of Java virtual machines.
 * </p>
 * <p>
 * This method returns a list of Java {@link com.sun.tools.attach.VirtualMachineDescriptor}
 * elements.  The list is an aggregation of the virtual machine descriptor lists obtained by
 * invoking the {@link com.sun.tools.attach.spi.AttachProvider#listVirtualMachines
 * listVirtualMachines} method of all installed {@link com.sun.tools.attach.spi.AttachProvider
 * attach providers}.  If there are no Java virtual machines known to any provider then an empty
 * list is returned.
 * </p>
 * @return The list of virtual machine descriptors.
 */
public static List<VirtualMachineDescriptor> list() {
    List<VirtualMachineDescriptor> l = new ArrayList<VirtualMachineDescriptor>();
    List<AttachProvider> providers = AttachProvider.providers();
    for (AttachProvider provider : providers) {
        l.addAll(provider.listVirtualMachines());
    }
    return l;
}
Also used : ArrayList(java.util.ArrayList) AttachProvider(com.sun.tools.attach.spi.AttachProvider)

Aggregations

AttachProvider (com.sun.tools.attach.spi.AttachProvider)20 VirtualMachineDescriptor (com.sun.tools.attach.VirtualMachineDescriptor)12 VirtualMachine (com.sun.tools.attach.VirtualMachine)9 Test (org.testng.annotations.Test)9 AttachNotSupportedException (com.sun.tools.attach.AttachNotSupportedException)8 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)4 Properties (java.util.Properties)2 HashMap (java.util.HashMap)1 MBeanServerConnection (javax.management.MBeanServerConnection)1 ObjectName (javax.management.ObjectName)1 JMXConnector (javax.management.remote.JMXConnector)1 JMXServiceURL (javax.management.remote.JMXServiceURL)1