Search in sources :

Example 16 with VirtualMachineDescriptor

use of com.sun.tools.attach.VirtualMachineDescriptor in project sts4 by spring-projects.

the class SpringBootAppCache method refresh.

private void refresh() {
    List<VirtualMachineDescriptor> currentVms = VirtualMachine.list();
    ImmutableMap.Builder<VirtualMachineDescriptor, SpringBootApp> newAppsBuilder = ImmutableMap.builder();
    for (VirtualMachineDescriptor vm : currentVms) {
        SpringBootApp existingApp = apps.get(vm);
        if (existingApp != null) {
            newAppsBuilder.put(vm, existingApp);
        } else {
            try {
                newAppsBuilder.put(vm, new SpringBootApp(vm));
            } catch (Exception e) {
            // Ignore problems attaching to a VM. We will try again on next polling loop, if vm still exists.
            // The most likely cause is that the VM already died since we obtained a reference to it.
            }
        }
    }
    HashSet<VirtualMachineDescriptor> oldVms = new HashSet<>(apps.keySet());
    ImmutableMap<VirtualMachineDescriptor, SpringBootApp> newApps = newAppsBuilder.build();
    oldVms.removeAll(newApps.keySet());
    for (VirtualMachineDescriptor oldVm : oldVms) {
        apps.get(oldVm).dispose();
    }
    apps = newApps;
    nextRefreshAfter = System.currentTimeMillis() + EXPIRE_AFTER.toMillis();
}
Also used : VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) ImmutableMap(com.google.common.collect.ImmutableMap) HashSet(java.util.HashSet)

Example 17 with VirtualMachineDescriptor

use of com.sun.tools.attach.VirtualMachineDescriptor in project Aeron by real-logic.

the class ClusterNetworkTopologyTest method setUp.

@BeforeEach
void setUp() {
    Tests.await(() -> {
        final List<VirtualMachineDescriptor> list = VirtualMachine.list();
        final List<VirtualMachineDescriptor> echoServices = list.stream().filter((vm) -> EchoServiceNode.class.getName().equals(vm.displayName())).collect(Collectors.toList());
        if (!echoServices.isEmpty()) {
            System.out.println(echoServices);
            Tests.sleep(200, () -> "Failed to shutdown EchoServiceNode");
        }
        return echoServices.isEmpty();
    }, SECONDS.toNanos(10));
    baseDir = FileResolveUtil.resolveClusterScriptDir();
    IoUtil.delete(new File(baseDir, "node0"), true);
    IoUtil.delete(new File(baseDir, "node1"), true);
    IoUtil.delete(new File(baseDir, "node2"), true);
}
Also used : VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) AeronCluster(io.aeron.cluster.client.AeronCluster) BeforeEach(org.junit.jupiter.api.BeforeEach) java.util(java.util) io.aeron.test(io.aeron.test) Selector(java.nio.channels.Selector) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) IoUtil(org.agrona.IoUtil) FileResolveUtil(io.aeron.test.launcher.FileResolveUtil) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult) MutableReference(org.agrona.collections.MutableReference) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) SocketChannel(java.nio.channels.SocketChannel) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Duration(java.time.Duration) BasicAuctionClusterClient(io.aeron.samples.cluster.tutorial.BasicAuctionClusterClient) RemoteLaunchClient(io.aeron.test.launcher.RemoteLaunchClient) MethodSource(org.junit.jupiter.params.provider.MethodSource) EchoServiceNode(io.aeron.samples.cluster.EchoServiceNode) MediaDriver(io.aeron.driver.MediaDriver) ReadableByteChannel(java.nio.channels.ReadableByteChannel) VirtualMachine(com.sun.tools.attach.VirtualMachine) CharBuffer(java.nio.CharBuffer) SelectionKey(java.nio.channels.SelectionKey) IOException(java.io.IOException) CharsetDecoder(java.nio.charset.CharsetDecoder) CommonContext(io.aeron.CommonContext) Arguments(org.junit.jupiter.params.provider.Arguments) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) UncheckedIOException(java.io.UncheckedIOException) TimeUnit(java.util.concurrent.TimeUnit) EgressListener(io.aeron.cluster.client.EgressListener) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) ThreadingMode(io.aeron.driver.ThreadingMode) Assertions(org.junit.jupiter.api.Assertions) ClusterConfig(io.aeron.samples.cluster.ClusterConfig) MutableDirectBuffer(org.agrona.MutableDirectBuffer) Pattern(java.util.regex.Pattern) SECONDS(java.util.concurrent.TimeUnit.SECONDS) JRE(org.junit.jupiter.api.condition.JRE) EchoServiceNode(io.aeron.samples.cluster.EchoServiceNode) VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) File(java.io.File) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 18 with VirtualMachineDescriptor

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

the class JCmd method main.

public static void main(String[] args) {
    Arguments arg = null;
    try {
        arg = new Arguments(args);
    } catch (IllegalArgumentException ex) {
        System.err.println("Error parsing arguments: " + ex.getMessage() + "\n");
        Arguments.usage();
        System.exit(1);
    }
    if (arg.isShowUsage()) {
        Arguments.usage();
        System.exit(1);
    }
    if (arg.isListProcesses()) {
        List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
        for (VirtualMachineDescriptor vmd : vmds) {
            System.out.println(vmd.id() + " " + vmd.displayName());
        }
        System.exit(0);
    }
    List<String> pids = new ArrayList<String>();
    if (arg.getPid() == 0) {
        // find all VMs
        List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
        for (VirtualMachineDescriptor vmd : vmds) {
            if (!isJCmdProcess(vmd)) {
                pids.add(vmd.id());
            }
        }
    } else if (arg.getProcessSubstring() != null) {
        // use the partial class-name match
        List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
        for (VirtualMachineDescriptor vmd : vmds) {
            if (isJCmdProcess(vmd)) {
                continue;
            }
            try {
                String mainClass = getMainClass(vmd);
                if (mainClass != null && mainClass.indexOf(arg.getProcessSubstring()) != -1) {
                    pids.add(vmd.id());
                }
            } catch (MonitorException | URISyntaxException e) {
                if (e.getMessage() != null) {
                    System.err.println(e.getMessage());
                } else {
                    Throwable cause = e.getCause();
                    if ((cause != null) && (cause.getMessage() != null)) {
                        System.err.println(cause.getMessage());
                    } else {
                        e.printStackTrace();
                    }
                }
            }
        }
        if (pids.isEmpty()) {
            System.err.println("Could not find any processes matching : '" + arg.getProcessSubstring() + "'");
            System.exit(1);
        }
    } else if (arg.getPid() == -1) {
        System.err.println("Invalid pid specified");
        System.exit(1);
    } else {
        // Use the found pid
        pids.add(arg.getPid() + "");
    }
    boolean success = true;
    for (String pid : pids) {
        System.out.println(pid + ":");
        if (arg.isListCounters()) {
            listCounters(pid);
        } else {
            try {
                executeCommandForPid(pid, arg.getCommand());
            } catch (AttachOperationFailedException ex) {
                System.err.println(ex.getMessage());
                success = false;
            } catch (Exception ex) {
                ex.printStackTrace();
                success = false;
            }
        }
    }
    System.exit(success ? 0 : 1);
}
Also used : AttachOperationFailedException(com.sun.tools.attach.AttachOperationFailedException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException) AgentLoadException(com.sun.tools.attach.AgentLoadException) MonitorException(sun.jvmstat.monitor.MonitorException) URISyntaxException(java.net.URISyntaxException) AttachOperationFailedException(com.sun.tools.attach.AttachOperationFailedException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 19 with VirtualMachineDescriptor

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

the class HotSpotAttachProvider method listVirtualMachines.

/*
     * This listVirtualMachines implementation is based on jvmstat. Can override
     * this in platform implementations when there is a more efficient mechanism
     * available.
     */
public List<VirtualMachineDescriptor> listVirtualMachines() {
    ArrayList<VirtualMachineDescriptor> result = new ArrayList<VirtualMachineDescriptor>();
    MonitoredHost host;
    Set<Integer> vms;
    try {
        host = MonitoredHost.getMonitoredHost(new HostIdentifier((String) null));
        vms = host.activeVms();
    } catch (Throwable t) {
        if (t instanceof ExceptionInInitializerError) {
            t = t.getCause();
        }
        if (t instanceof ThreadDeath) {
            throw (ThreadDeath) t;
        }
        if (t instanceof SecurityException) {
            return result;
        }
        // shouldn't happen
        throw new InternalError(t);
    }
    for (Integer vmid : vms) {
        String pid = vmid.toString();
        // default to pid if name not available
        String name = pid;
        boolean isAttachable = false;
        MonitoredVm mvm = null;
        try {
            mvm = host.getMonitoredVm(new VmIdentifier(pid));
            try {
                isAttachable = MonitoredVmUtil.isAttachable(mvm);
                // use the command line as the display name
                name = MonitoredVmUtil.commandLine(mvm);
            } catch (Exception e) {
            }
            if (isAttachable) {
                result.add(new HotSpotVirtualMachineDescriptor(this, pid, name));
            }
        } catch (Throwable t) {
            if (t instanceof ThreadDeath) {
                throw (ThreadDeath) t;
            }
        } finally {
            if (mvm != null) {
                mvm.detach();
            }
        }
    }
    return result;
}
Also used : MonitoredVm(sun.jvmstat.monitor.MonitoredVm) ArrayList(java.util.ArrayList) HostIdentifier(sun.jvmstat.monitor.HostIdentifier) VmIdentifier(sun.jvmstat.monitor.VmIdentifier) VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException) MonitorException(sun.jvmstat.monitor.MonitorException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) MonitoredHost(sun.jvmstat.monitor.MonitoredHost)

Example 20 with VirtualMachineDescriptor

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

the class LocalVirtualMachine method getAttachableVMs.

private static void getAttachableVMs(Map<Integer, LocalVirtualMachine> map) {
    List<VirtualMachineDescriptor> vms = VirtualMachine.list();
    for (VirtualMachineDescriptor vmd : vms) {
        try {
            Integer vmid = Integer.valueOf(vmd.id());
            if (!map.containsKey(vmid)) {
                boolean attachable = false;
                String address = null;
                try {
                    VirtualMachine vm = VirtualMachine.attach(vmd);
                    attachable = true;
                    Properties agentProps = vm.getAgentProperties();
                    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
                    vm.detach();
                } catch (AttachNotSupportedException x) {
                // not attachable
                } catch (IOException x) {
                // ignore
                }
                map.put(vmid, new LocalVirtualMachine(vmid.intValue(), vmd.displayName(), attachable, address));
            }
        } catch (NumberFormatException e) {
        // do not support vmid different than pid
        }
    }
}
Also used : VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) IOException(java.io.IOException) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException) VirtualMachine(com.sun.tools.attach.VirtualMachine)

Aggregations

VirtualMachineDescriptor (com.sun.tools.attach.VirtualMachineDescriptor)26 IOException (java.io.IOException)16 AttachNotSupportedException (com.sun.tools.attach.AttachNotSupportedException)13 AttachProvider (com.sun.tools.attach.spi.AttachProvider)12 VirtualMachine (com.sun.tools.attach.VirtualMachine)11 Test (org.testng.annotations.Test)7 ArrayList (java.util.ArrayList)5 Msg.getString (com.ibm.oti.util.Msg.getString)2 AgentLoadException (com.sun.tools.attach.AgentLoadException)2 File (java.io.File)2 URISyntaxException (java.net.URISyntaxException)2 JMXServiceURL (javax.management.remote.JMXServiceURL)2 MonitorException (sun.jvmstat.monitor.MonitorException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 FileLock (com.ibm.tools.attach.target.FileLock)1 Reply (com.ibm.tools.attach.target.Reply)1 AgentInitializationException (com.sun.tools.attach.AgentInitializationException)1 AttachOperationFailedException (com.sun.tools.attach.AttachOperationFailedException)1 CommonContext (io.aeron.CommonContext)1 AeronCluster (io.aeron.cluster.client.AeronCluster)1