Skip to content
Permalink
Browse files
8254102: use ProcessHandle::pid instead of ManagementFactory::getRunt…
…imeMXBean to get pid in tests

Reviewed-by: rriggs, shade
  • Loading branch information
iignatev committed Oct 7, 2020
1 parent 38159d5 commit 5a9bd41e79649916189ffdfddac57a26803c8c7f
@@ -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 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);
@@ -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.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 @@ static void redefineFoo() throws Exception {
}

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);
@@ -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.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 @@ protected void redefineFoo() throws Exception {
}

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);
@@ -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 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 @@ static public void main(String[] args) throws Exception {
// 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++) {
@@ -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
@@ -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 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 @@ static public void main(String[] args) throws Exception {
// 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++) {
@@ -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
@@ -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 @@
// 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 @@ static void doSamples() throws Exception {
}
}

// 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]");

1 comment on commit 5a9bd41

@bridgekeeper

This comment has been minimized.

Copy link

@bridgekeeper bridgekeeper bot commented on 5a9bd41 Oct 7, 2020

Please sign in to comment.