Java Code Examples for sun.tools.attach.HotSpotVirtualMachine#detach()

The following examples show how to use sun.tools.attach.HotSpotVirtualMachine#detach() . 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: RuntimeMXBeanTest.java    From code with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException, AttachNotSupportedException {

        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
        String name = bean.getName();
        int index = name.indexOf('@');
        String pid = name.substring(0, index);
        //这里要区分操作系统
        HotSpotVirtualMachine machine = (HotSpotVirtualMachine) new sun.tools.attach.WindowsAttachProvider().attachVirtualMachine(pid);
        InputStream is = machine.heapHisto("-all");
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        int len;
        byte[] buff = new byte[1024];
        while ((len = is.read(buff)) > 0)
            os.write(buff, 0, len);
        is.close();
        machine.detach();
        System.out.println(os);

    }
 
Example 2
Source File: TestLoggerWeakRefLeak.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 'vm.heapHisto("-live")' will request a full GC
 */
private static int getInstanceCountFromHeapHisto() throws AttachNotSupportedException, Exception {
    int instanceCount = 0;

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine
            .attach(Integer.toString(ProcessTools.getProcessId()));
    try {
        try (InputStream heapHistoStream = vm.heapHisto("-live");
                BufferedReader in = new BufferedReader(new InputStreamReader(heapHistoStream))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                if (inputLine.contains(TARGET_CLASS)) {
                    instanceCount = Integer.parseInt(inputLine
                            .split("[ ]+")[2]);
                    System.out.println("instance count: " + instanceCount);
                    break;
                }
            }
        }
    } finally {
        vm.detach();
    }

    assertGreaterThan(instanceCount, 0, "No instances of " + TARGET_CLASS + " are found");

    return instanceCount;
}
 
Example 3
Source File: AttachWithStalePidFile.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static boolean runTest() throws Exception {
  ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
    "-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget");
  Process target = pb.start();
  Path pidFile = null;

  try {
    int pid = getUnixProcessId(target);

    // create the stale .java_pid file. use hard-coded /tmp path as in th VM
    pidFile = createJavaPidFile(pid);
    if(pidFile == null) {
      return false;
    }

    // wait for vm.paused file to be created and delete it once we find it.
    waitForAndResumeVM(pid);

    // unfortunately there's no reliable way to know the VM is ready to receive the
    // attach request so we have to do an arbitrary sleep.
    Thread.sleep(5000);

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
    BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));
    String line = null;
    while((line = remoteDataReader.readLine()) != null);

    vm.detach();
    return true;
  }
  finally {
    target.destroy();
    target.waitFor();

    if(pidFile != null && Files.exists(pidFile)) {
      Files.delete(pidFile);
    }
  }
}
 
Example 4
Source File: TestLoggerWeakRefLeak.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 'vm.heapHisto("-live")' will request a full GC
 */
private static int getInstanceCountFromHeapHisto() throws AttachNotSupportedException, Exception {
    int instanceCount = 0;

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine
            .attach(Integer.toString(ProcessTools.getProcessId()));
    try {
        try (InputStream heapHistoStream = vm.heapHisto("-live");
                BufferedReader in = new BufferedReader(new InputStreamReader(heapHistoStream))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                if (inputLine.contains(TARGET_CLASS)) {
                    instanceCount = Integer.parseInt(inputLine
                            .split("[ ]+")[2]);
                    System.out.println("instance count: " + instanceCount);
                    break;
                }
            }
        }
    } finally {
        vm.detach();
    }

    assertGreaterThan(instanceCount, 0, "No instances of " + TARGET_CLASS + " are found");

    return instanceCount;
}
 
Example 5
Source File: AttachWithStalePidFile.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static boolean runTest() throws Exception {
  ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
    "-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget");
  Process target = pb.start();
  Path pidFile = null;

  try {
    int pid = getUnixProcessId(target);

    // create the stale .java_pid file. use hard-coded /tmp path as in th VM
    pidFile = createJavaPidFile(pid);
    if(pidFile == null) {
      return false;
    }

    // wait for vm.paused file to be created and delete it once we find it.
    waitForAndResumeVM(pid);

    // unfortunately there's no reliable way to know the VM is ready to receive the
    // attach request so we have to do an arbitrary sleep.
    Thread.sleep(5000);

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
    BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));
    String line = null;
    while((line = remoteDataReader.readLine()) != null);

    vm.detach();
    return true;
  }
  finally {
    target.destroy();
    target.waitFor();

    if(pidFile != null && Files.exists(pidFile)) {
      Files.delete(pidFile);
    }
  }
}
 
Example 6
Source File: AttachWithStalePidFile.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static boolean runTest() throws Exception {
  ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
    "-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget");
  Process target = pb.start();
  Path pidFile = null;

  try {
    int pid = getUnixProcessId(target);

    // create the stale .java_pid file. use hard-coded /tmp path as in th VM
    pidFile = createJavaPidFile(pid);
    if(pidFile == null) {
      return false;
    }

    // wait for vm.paused file to be created and delete it once we find it.
    waitForAndResumeVM(pid);

    // unfortunately there's no reliable way to know the VM is ready to receive the
    // attach request so we have to do an arbitrary sleep.
    Thread.sleep(5000);

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
    BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));
    String line = null;
    while((line = remoteDataReader.readLine()) != null);

    vm.detach();
    return true;
  }
  finally {
    target.destroy();
    target.waitFor();

    if(pidFile != null && Files.exists(pidFile)) {
      Files.delete(pidFile);
    }
  }
}
 
Example 7
Source File: TestLoggerWeakRefLeak.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 'vm.heapHisto("-live")' will request a full GC
 */
private static int getInstanceCountFromHeapHisto() throws AttachNotSupportedException, Exception {
    int instanceCount = 0;

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine
            .attach(Integer.toString(ProcessTools.getProcessId()));
    try {
        try (InputStream heapHistoStream = vm.heapHisto("-live");
                BufferedReader in = new BufferedReader(new InputStreamReader(heapHistoStream))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                if (inputLine.contains(TARGET_CLASS)) {
                    instanceCount = Integer.parseInt(inputLine
                            .split("[ ]+")[2]);
                    System.out.println("instance count: " + instanceCount);
                    break;
                }
            }
        }
    } finally {
        vm.detach();
    }

    assertGreaterThan(instanceCount, 0, "No instances of " + TARGET_CLASS + " are found");

    return instanceCount;
}
 
Example 8
Source File: AttachWithStalePidFile.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static boolean runTest() throws Exception {
  ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
    "-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget");
  Process target = pb.start();
  Path pidFile = null;

  try {
    int pid = getUnixProcessId(target);

    // create the stale .java_pid file. use hard-coded /tmp path as in th VM
    pidFile = createJavaPidFile(pid);
    if(pidFile == null) {
      return false;
    }

    // wait for vm.paused file to be created and delete it once we find it.
    waitForAndResumeVM(pid);

    // unfortunately there's no reliable way to know the VM is ready to receive the
    // attach request so we have to do an arbitrary sleep.
    Thread.sleep(5000);

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
    BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));
    String line = null;
    while((line = remoteDataReader.readLine()) != null);

    vm.detach();
    return true;
  }
  finally {
    target.destroy();
    target.waitFor();

    if(pidFile != null && Files.exists(pidFile)) {
      Files.delete(pidFile);
    }
  }
}
 
Example 9
Source File: TestLoggerWeakRefLeak.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 'vm.heapHisto("-live")' will request a full GC
 */
private static int getInstanceCountFromHeapHisto() throws AttachNotSupportedException, Exception {
    int instanceCount = 0;

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine
            .attach(Integer.toString(ProcessTools.getProcessId()));
    try {
        try (InputStream heapHistoStream = vm.heapHisto("-live");
                BufferedReader in = new BufferedReader(new InputStreamReader(heapHistoStream))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                if (inputLine.contains(TARGET_CLASS)) {
                    instanceCount = Integer.parseInt(inputLine
                            .split("[ ]+")[2]);
                    System.out.println("instance count: " + instanceCount);
                    break;
                }
            }
        }
    } finally {
        vm.detach();
    }

    assertGreaterThan(instanceCount, 0, "No instances of " + TARGET_CLASS + " are found");

    return instanceCount;
}
 
Example 10
Source File: AttachWithStalePidFile.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static boolean runTest() throws Exception {
  ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
    "-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget");
  Process target = pb.start();
  Path pidFile = null;

  try {
    int pid = getUnixProcessId(target);

    // create the stale .java_pid file. use hard-coded /tmp path as in th VM
    pidFile = createJavaPidFile(pid);
    if(pidFile == null) {
      return false;
    }

    // wait for vm.paused file to be created and delete it once we find it.
    waitForAndResumeVM(pid);

    waitForTargetReady(target);

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
    BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));
    String line = null;
    while((line = remoteDataReader.readLine()) != null);

    vm.detach();
    return true;
  }
  finally {
    target.destroy();
    target.waitFor();

    if(pidFile != null && Files.exists(pidFile)) {
      Files.delete(pidFile);
    }
  }
}
 
Example 11
Source File: AttachSetGetFlag.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void testGetFlag(String flagName, String flagValue) throws Exception {
  ProcessBuilder pb = runTarget(flagName, flagValue);

  Process target = pb.start();

  try {
    waitForReady(target);

    int pid = (int)target.pid();

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());

    // Test Get
    BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(
        vm.printFlag(flagName)));

    boolean foundExpectedLine = false;

    String line = null;
    while((line = remoteDataReader.readLine()) != null) {
      System.out.println("printFlag: " + line);
      if (line.equals("-XX:" + flagName + "=" + flagValue)) {
        foundExpectedLine = true;
      }
    }

    Asserts.assertTrue(foundExpectedLine, "Didn't get the expected output: '-XX:" + flagName + "=" + flagValue + "'");

    vm.detach();
  }
  finally {
    target.destroy();
    target.waitFor();
  }
}
 
Example 12
Source File: TestLoggerWeakRefLeak.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 'vm.heapHisto("-live")' will request a full GC
 */
private static int getInstanceCountFromHeapHisto() throws AttachNotSupportedException, Exception {
    int instanceCount = 0;

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine
            .attach(Long.toString(ProcessTools.getProcessId()));
    try {
        try (InputStream heapHistoStream = vm.heapHisto("-live");
                BufferedReader in = new BufferedReader(new InputStreamReader(heapHistoStream))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                if (inputLine.contains(TARGET_CLASS)) {
                    instanceCount = Integer.parseInt(inputLine
                            .split("[ ]+")[2]);
                    System.out.println("instance count: " + instanceCount);
                    break;
                }
            }
        }
    } finally {
        vm.detach();
    }

    assertGreaterThan(instanceCount, 0, "No instances of " + TARGET_CLASS + " are found");

    return instanceCount;
}
 
Example 13
Source File: AttachWithStalePidFile.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static boolean runTest() throws Exception {
  ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
    "-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget");
  Process target = pb.start();
  Path pidFile = null;

  try {
    int pid = getUnixProcessId(target);

    // create the stale .java_pid file. use hard-coded /tmp path as in th VM
    pidFile = createJavaPidFile(pid);
    if(pidFile == null) {
      return false;
    }

    // wait for vm.paused file to be created and delete it once we find it.
    waitForAndResumeVM(pid);

    // unfortunately there's no reliable way to know the VM is ready to receive the
    // attach request so we have to do an arbitrary sleep.
    Thread.sleep(5000);

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
    BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));
    String line = null;
    while((line = remoteDataReader.readLine()) != null);

    vm.detach();
    return true;
  }
  finally {
    target.destroy();
    target.waitFor();

    if(pidFile != null && Files.exists(pidFile)) {
      Files.delete(pidFile);
    }
  }
}
 
Example 14
Source File: AttachWithStalePidFile.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static boolean runTest() throws Exception {
  ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
    "-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget");
  Process target = pb.start();
  Path pidFile = null;

  try {
    int pid = getUnixProcessId(target);

    // create the stale .java_pid file. use hard-coded /tmp path as in th VM
    pidFile = createJavaPidFile(pid);
    if(pidFile == null) {
      return false;
    }

    // wait for vm.paused file to be created and delete it once we find it.
    waitForAndResumeVM(pid);

    // unfortunately there's no reliable way to know the VM is ready to receive the
    // attach request so we have to do an arbitrary sleep.
    Thread.sleep(5000);

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
    BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));
    String line = null;
    while((line = remoteDataReader.readLine()) != null);

    vm.detach();
    return true;
  }
  finally {
    target.destroy();
    target.waitFor();

    if(pidFile != null && Files.exists(pidFile)) {
      Files.delete(pidFile);
    }
  }
}
 
Example 15
Source File: AttachWithStalePidFile.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static boolean runTest() throws Exception {
  ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
    "-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget");
  Process target = pb.start();
  Path pidFile = null;

  try {
    int pid = getUnixProcessId(target);

    // create the stale .java_pid file. use hard-coded /tmp path as in th VM
    pidFile = createJavaPidFile(pid);
    if(pidFile == null) {
      return false;
    }

    // wait for vm.paused file to be created and delete it once we find it.
    waitForAndResumeVM(pid);

    // unfortunately there's no reliable way to know the VM is ready to receive the
    // attach request so we have to do an arbitrary sleep.
    Thread.sleep(5000);

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
    BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));
    String line = null;
    while((line = remoteDataReader.readLine()) != null);

    vm.detach();
    return true;
  }
  finally {
    target.destroy();
    target.waitFor();

    if(pidFile != null && Files.exists(pidFile)) {
      Files.delete(pidFile);
    }
  }
}
 
Example 16
Source File: TestLoggerWeakRefLeak.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 'vm.heapHisto("-live")' will request a full GC
 */
private static int getInstanceCountFromHeapHisto() throws AttachNotSupportedException, Exception {
    int instanceCount = 0;

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine
            .attach(Integer.toString(ProcessTools.getProcessId()));
    try {
        try (InputStream heapHistoStream = vm.heapHisto("-live");
                BufferedReader in = new BufferedReader(new InputStreamReader(heapHistoStream))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                if (inputLine.contains(TARGET_CLASS)) {
                    instanceCount = Integer.parseInt(inputLine
                            .split("[ ]+")[2]);
                    System.out.println("instance count: " + instanceCount);
                    break;
                }
            }
        }
    } finally {
        vm.detach();
    }

    assertGreaterThan(instanceCount, 0, "No instances of " + TARGET_CLASS + " are found");

    return instanceCount;
}