--- old/test/hotspot/jtreg/runtime/appcds/TestCommon.java 2019-03-24 20:44:09.202196785 -0700 +++ new/test/hotspot/jtreg/runtime/appcds/TestCommon.java 2019-03-24 20:44:09.050191408 -0700 @@ -149,6 +149,12 @@ // Execute JVM using AppCDS archive with specified AppCDSOptions public static OutputAnalyzer runWithArchive(AppCDSOptions opts) throws Exception { + ProcessBuilder pb = createProcessBuilderWithArchive(opts); + return executeAndLog(pb, "exec"); + } + + public static ProcessBuilder createProcessBuilderWithArchive(AppCDSOptions opts) + throws Exception { ArrayList cmd = new ArrayList(); @@ -168,7 +174,7 @@ String[] cmdLine = cmd.toArray(new String[cmd.size()]); ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, cmdLine); - return executeAndLog(pb, "exec"); + return pb; } @@ -205,6 +211,12 @@ return runWithArchive(opts); } + public static ProcessBuilder createProcessBuilderWithAutoArchive(String... suffix) throws Exception { + AppCDSOptions opts = (new AppCDSOptions()); + opts.addSuffix(suffix).setXShareMode("auto"); + return createProcessBuilderWithArchive(opts); + } + public static OutputAnalyzer execOff(String... suffix) throws Exception { AppCDSOptions opts = (new AppCDSOptions()); opts.addSuffix(suffix).setXShareMode("off"); --- old/test/hotspot/jtreg/runtime/appcds/jvmti/InstrumentationTest.java 2019-03-24 20:44:09.754216311 -0700 +++ new/test/hotspot/jtreg/runtime/appcds/jvmti/InstrumentationTest.java 2019-03-24 20:44:09.586210369 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2019, 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,9 +29,6 @@ * @library /test/lib /test/hotspot/jtreg/runtime/appcds /test/hotspot/jtreg/runtime/appcds/test-classes * @requires vm.cds * @requires vm.flavor != "minimal" - * @modules java.base/jdk.internal.misc - * jdk.jartool/sun.tools.jar - * java.management * @build sun.hotspot.WhiteBox * InstrumentationApp * InstrumentationClassFileTransformer @@ -40,13 +37,11 @@ */ // Note: TestCommon is from /test/hotspot/jtreg/runtime/appcds/TestCommon.java -// Note: Util is from /test/hotspot/jtreg/runtime/appcds/test-classes/TestCommon.java +// Note: Util is from /test/hotspot/jtreg/runtime/appcds/test-classes/Util.java import com.sun.tools.attach.VirtualMachine; -import com.sun.tools.attach.VirtualMachineDescriptor; import java.io.File; import java.io.FileOutputStream; -import java.util.List; import jdk.test.lib.Asserts; import jdk.test.lib.cds.CDSOptions; import jdk.test.lib.process.OutputAnalyzer; @@ -121,7 +116,11 @@ "-Xshare:off", agentCmdArg, "InstrumentationApp", flagFile, bootJar, appJar, custJar); - TestCommon.executeAndLog(pb, "no-sharing").shouldHaveExitValue(0); + Process process = pb.start(); + if (t != null) { + t.setPid(process.pid()); + } + TestCommon.executeAndLog(process, "no-sharing").shouldHaveExitValue(0); checkAttach(t); // Dump the AppCDS archive. On some platforms AppCDSv2 may not be enabled, so we @@ -150,13 +149,17 @@ flagFile = getFlagFile(attachAgent); t = doAttach(attachAgent, flagFile, agentJar); - out = TestCommon.execAuto("-cp", appJar, + pb = TestCommon.createProcessBuilderWithAutoArchive("-cp", appJar, bootCP, "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI", agentCmdArg, "InstrumentationApp", flagFile, bootJar, appJar, custJar); - + process = pb.start(); + if (t != null) { + t.setPid(process.pid()); + } + out = TestCommon.executeAndLog(process, "auto"); CDSOptions opts = (new CDSOptions()).setXShareMode("auto"); TestCommon.checkExec(out, opts); checkAttach(t); @@ -212,35 +215,36 @@ String flagFile; String agentJar; volatile boolean succeeded; + volatile long pid; + volatile boolean pidIsKnown; AgentAttachThread(String flagFile, String agentJar) { this.flagFile = flagFile; this.agentJar = agentJar; this.succeeded = false; + this.pidIsKnown = false; + } + + void setPid(long pid) { + this.pid = pid; + this.pidIsKnown = true; } - static String getPid(String flagFile) throws Throwable { - while (true) { - // Keep polling until the child process has been launched. If for some - // reason the child process fails to launch, this test will be terminated + String getPid() throws Throwable { + while (!pidIsKnown) { + // Keep polling until the child process has been launched, and its pid is known. + // If for some reason the child process fails to launch, this test will be terminated // by JTREG's time-out mechanism. Thread.sleep(100); - List vmds = VirtualMachine.list(); - for (VirtualMachineDescriptor vmd : vmds) { - if (vmd.displayName().contains(flagFile) && vmd.displayName().contains("InstrumentationApp")) { - // We use flagFile (which has the PID of this process) as a unique identifier - // to ident the child process, which we want to attach to. - System.out.println("Process found: " + vmd.id() + " " + vmd.displayName()); - return vmd.id(); - } - } } + System.out.println("pid = " + pid); + return Long.toString(pid); } public void run() { try { - String pid = getPid(flagFile); - VirtualMachine vm = VirtualMachine.attach(pid); + String pidStr = getPid(); + VirtualMachine vm = VirtualMachine.attach(pidStr); System.out.println(agentJar); vm.loadAgent(agentJar); } catch (Throwable t) { --- old/test/lib/jdk/test/lib/cds/CDSTestUtils.java 2019-03-24 20:44:10.290235272 -0700 +++ new/test/lib/jdk/test/lib/cds/CDSTestUtils.java 2019-03-24 20:44:10.138229895 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2019, 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 @@ -564,8 +564,12 @@ // ============================= Logging public static OutputAnalyzer executeAndLog(ProcessBuilder pb, String logName) throws Exception { + return executeAndLog(pb.start(), logName); + } + + public static OutputAnalyzer executeAndLog(Process process, String logName) throws Exception { long started = System.currentTimeMillis(); - OutputAnalyzer output = new OutputAnalyzer(pb.start()); + OutputAnalyzer output = new OutputAnalyzer(process); String outputFileNamePrefix = getTestName() + "-" + String.format("%04d", getNextLogCounter()) + "-" + logName;