Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/aod/AODTestRunner.java
41161 views
/*1* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package nsk.share.aod;2324import java.io.*;25import nsk.share.*;26import nsk.share.jpda.SocketIOPipe;2728/*29Class AODTestRunner is part of the framework used in the AttachOnDemand tests30(tests against Attach API, API from package com.sun.tools.attach).3132AODTestRunner is used as main class in AttachOnDemand tests, it performs following33actions:34- starts target application3536- finds VM id for target VM (this id is needed for dynamic attach)3738- by default AODTestRunner tries to attach specified via command line agents to target VM39(subclasses can override this default behavior)4041- waits for target application completion4243Target application class, agents that should be attached, JDK used to run target application and44VM options passed to target VM should be specified via command line.45*/46public class AODTestRunner {4748public static final String targetAppIdProperty = "vmsqe.aod.targetAppId";49public static final String appIdProperty = "vmsqe.aod.AppId";5051public static final long TARGET_APP_CONNECT_TIMEOUT = 5 * 60 * 1000; // 5 min5253public static final long TARGET_APP_WORK_TIMEOUT = 30 * 60 * 1000; // 30 min (standard VM testbase test timeout)5455protected Log log;5657protected SocketIOPipe pipe;5859protected ProcessExecutor targetAppExecutor;6061// target application ready for attach62public static final String SIGNAL_READY_FOR_ATTACH = "ready";6364// target application may finish execution65public static final String SIGNAL_FINISH = "finish";6667protected AODRunnerArgParser argParser;6869protected AODTestRunner(String[] args) {70log = new Log(System.out, true);7172argParser = createArgParser(args);73}7475/*76* This method is introduced to let subclasses to create its own parsers77*/78protected AODRunnerArgParser createArgParser(String[] args) {79return new AODRunnerArgParser(args);80}8182protected void doTestActions(String targetVMId) throws Throwable {83AgentsAttacher attacher = new AgentsAttacher(targetVMId, argParser.getAgents(), log);84attacher.attachAgents();85}8687protected String getCurrentVMId() {88String currentVMId = "" + ProcessHandle.current().pid();89log.display("Current VM id was identified: " + currentVMId);9091return currentVMId;92}9394protected void runTest() {9596try {97String targetAppId = System.getProperty(targetAppIdProperty);98if(targetAppId == null || targetAppId.isEmpty()) {99// use PID as default appID100targetAppId = "" + ProcessHandle.current().pid();101}102/*103* Create target application id required by the Utils.findVMIdUsingJPS104*/105String targetAppCmd =106// path to java107argParser.getTestedJDK() + File.separator + "bin" + File.separator + "java " +108// VM property to identify VM running target application109"-D" + appIdProperty + "=" + targetAppId + " " +110// VM opts111argParser.getJavaOpts() + " -XX:+EnableDynamicAgentLoading " +112// target application class113argParser.getTargetApp() + " " +114// additional target application parameter - number of115// agents that will be attached116"-" + AODTargetArgParser.agentsNumberParam + " " + argParser.getAgents().size();117118pipe = SocketIOPipe.createServerIOPipe(log, 0, TARGET_APP_CONNECT_TIMEOUT);119targetAppCmd += " -" + AODTargetArgParser.socketPortParam + " " + pipe.getPort();120121log.display("Starting target application: " + targetAppCmd);122targetAppExecutor = new ProcessExecutor(targetAppCmd, TARGET_APP_WORK_TIMEOUT, "TargetApp");123targetAppExecutor.startProcess();124125/*126* Don't try to attach agents until target application isn't initialized127*/128String signal = pipe.readln();129log.display("Signal received: '" + signal + "'");130if ((signal == null) || !signal.equals(SIGNAL_READY_FOR_ATTACH))131throw new TestBug("Unexpected TargetApplication signal: '" + signal + "'");132133String targetVMId = Long.toString(targetAppExecutor.pid());134log.display("Target VM id was identified: " + targetVMId);135136doTestActions(targetVMId);137138/*139* When test actions finished let target application finish execution140*/141log.display("Sending signal: '" + SIGNAL_FINISH + "'");142pipe.println(SIGNAL_FINISH);143144targetAppExecutor.waitForProcess();145146File file = new File(targetAppId);147if (file.exists()) {148file.deleteOnExit();149}150151if (targetAppExecutor.getExitCode() != 0) {152throw new Failure("Target application finished with non-zero code " + targetAppExecutor.getExitCode());153}154155postTargetExitHook();156157} catch (Failure f) {158throw f;159} catch (Throwable t) {160throw new Failure("Unexpected exception during test execution: " + t, t);161} finally {162if (pipe != null) {163pipe.close();164}165if (targetAppExecutor != null) {166targetAppExecutor.destroyProcess();167}168}169}170171/*172* Allow users of this class to specify actions to be taken after the target exits173*/174protected void postTargetExitHook() {175// do nothing by default176}177178public static String createApplicationId() {179return Long.valueOf(System.currentTimeMillis()).toString();180}181182public static void main(String[] args) {183new AODTestRunner(args).runTest();184}185}186187188