8254102: use ProcessHandle::pid instead of ManagementFactory::getRuntimeMXBean to get pid in tests

Reviewed-by: rriggs, shade
This commit is contained in:
Igor Ignatyev 2020-10-07 18:51:52 +00:00
parent 38159d52d4
commit 5a9bd41e79
8 changed files with 16 additions and 57 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -21,10 +21,6 @@
* questions.
*/
import java.lang.management.ManagementFactory;
/*
* @test
* @summary Suicide test
@ -34,13 +30,12 @@ public class Suicide {
public static void main(String[] args) {
String cmd = null;
try {
String pidStr = ManagementFactory.getRuntimeMXBean().getName()
.split("@")[0];
long pid = ProcessHandle.current().pid();
String osName = System.getProperty("os.name");
if (osName.contains("Windows")) {
cmd = "taskkill.exe /F /PID " + pidStr;
cmd = "taskkill.exe /F /PID " + pid;
} else {
cmd = "kill -9 " + pidStr;
cmd = "kill -9 " + pid;
}
System.out.printf("executing `%s'%n", cmd);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -29,7 +29,6 @@
* @modules java.base/jdk.internal.org.objectweb.asm
* java.compiler
* java.instrument
* java.management
* jdk.attach
* @requires vm.jvmti
*
@ -53,7 +52,6 @@ import java.lang.instrument.Instrumentation;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
@ -128,10 +126,7 @@ public class RedefineMethodUsedByMultipleMethodHandles {
}
public static void runAgent(Path agent) throws Exception {
String vmName = ManagementFactory.getRuntimeMXBean().getName();
int p = vmName.indexOf('@');
assert p != -1 : "VM name not in <pid>@<host> format: " + vmName;
String pid = vmName.substring(0, p);
String pid = Long.toString(ProcessHandle.current().pid());
ClassLoader cl = ClassLoader.getSystemClassLoader();
Class<?> c = Class.forName("com.sun.tools.attach.VirtualMachine", true, cl);
Method attach = c.getDeclaredMethod("attach", String.class);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -45,7 +45,6 @@ import java.io.InputStream;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
@ -134,10 +133,7 @@ public class RedefineClassTest extends TypeUniverse {
}
public static void loadAgent(Path agent) throws Exception {
String vmName = ManagementFactory.getRuntimeMXBean().getName();
int p = vmName.indexOf('@');
assumeTrue(p != -1);
String pid = vmName.substring(0, p);
String pid = Long.toString(ProcessHandle.current().pid());
ClassLoader cl = ClassLoader.getSystemClassLoader();
Class<?> c = Class.forName("com.sun.tools.attach.VirtualMachine", true, cl);
Method attach = c.getDeclaredMethod("attach", String.class);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -28,7 +28,6 @@ import jdk.test.lib.Utils;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
import java.lang.management.ManagementFactory;
import java.nio.file.Paths;
import java.security.ProtectionDomain;
@ -84,9 +83,7 @@ public class Agent implements ClassFileTransformer {
// Create speculative trap entries
Test.m();
String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
int p = nameOfRunningVM.indexOf('@');
String pid = nameOfRunningVM.substring(0, p);
String pid = Long.toString(ProcessHandle.current().pid());
// Make the nmethod go away
for (int i = 0; i < 10; i++) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -27,7 +27,6 @@
* @library /test/lib
* @modules java.base/jdk.internal.misc
* java.instrument
* java.management
* @requires vm.jvmti
* @build compiler.profiling.spectrapredefineclass.Agent
* @run driver ClassFileInstaller compiler.profiling.spectrapredefineclass.Agent

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -28,7 +28,6 @@ import jdk.test.lib.Utils;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
@ -66,9 +65,7 @@ public class Agent implements ClassFileTransformer {
// references m4() (loaded by loader2).
m3.invoke(Test_class.newInstance(), loader1);
String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
int p = nameOfRunningVM.indexOf('@');
String pid = nameOfRunningVM.substring(0, p);
String pid = Long.toString(ProcessHandle.current().pid());
// Make the nmethod go away
for (int i = 0; i < 10; i++) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -27,7 +27,6 @@
* @library /test/lib
* @modules java.base/jdk.internal.misc
* java.instrument
* java.management
* @requires vm.jvmti
* @build compiler.profiling.spectrapredefineclass_classloaders.Agent
* compiler.profiling.spectrapredefineclass_classloaders.Test

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -30,14 +30,11 @@
*
* @library /test/lib
* @modules java.base/jdk.internal.misc
* java.management
* @run main/othervm TestThreadDumpMonitorContention
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
@ -52,7 +49,7 @@ public class TestThreadDumpMonitorContention {
// so use getTestJDKTool() instead of getCompileJDKTool() or even
// getJDKTool() which can fall back to "compile.jdk".
final static String JSTACK = JDKToolFinder.getTestJDKTool("jstack");
final static String PID = getPid();
final static String PID = Long.toString(ProcessHandle.current().pid());
// looking for header lines with these patterns:
// "ContendingThread-1" #19 prio=5 os_prio=64 tid=0x000000000079c000 nid=0x23 runnable [0xffff80ffb8b87000]
@ -514,22 +511,6 @@ public class TestThreadDumpMonitorContention {
}
}
// This helper relies on RuntimeMXBean.getName() returning a string
// that looks like this: 5436@mt-haku
//
// The testlibrary has tryFindJvmPid(), but that uses a separate
// process which is much more expensive for finding out your own PID.
//
static String getPid() {
RuntimeMXBean runtimebean = ManagementFactory.getRuntimeMXBean();
String vmname = runtimebean.getName();
int i = vmname.indexOf('@');
if (i != -1) {
vmname = vmname.substring(0, i);
}
return vmname;
}
static void usage() {
System.err.println("Usage: " +
"java TestThreadDumpMonitorContention [-v] [n_samples]");