Path: blob/master/test/hotspot/jtreg/serviceability/attach/ConcAttachTest.java
41152 views
/*1* Copyright (c) 2019, 2020, 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*/2223/*24* @test25* @bug 822569026* @requires os.family != "windows"27* @library /test/lib28* @modules jdk.attach/com.sun.tools.attach29* @run main ConcAttachTest30*/3132import java.io.IOException;33import java.util.concurrent.CountDownLatch;34import java.util.concurrent.Executors;35import java.util.concurrent.ExecutorService;36import java.util.concurrent.TimeUnit;3738import com.sun.tools.attach.VirtualMachine;39import com.sun.tools.attach.AttachNotSupportedException;4041import jdk.test.lib.Utils;42import jdk.test.lib.apps.LingeredApp;43import jdk.test.lib.Asserts;44import jdk.test.lib.JDKToolLauncher;45import jdk.test.lib.process.OutputAnalyzer;4647public class ConcAttachTest implements Runnable {4849private static final int NUM_CONC_REQUESTS = 100;5051private static final int THREAD_POOL_TIMEOUT_IN_SEC = 30;5253private static CountDownLatch latch;5455private static String strPID;5657// Attach to LingeredApp concurrently.58public void run() {59VirtualMachine vm = null;6061try {62latch.countDown();63latch.await();64} catch (InterruptedException e) {65throw new RuntimeException(e);66}6768try {69vm = VirtualMachine.attach(strPID);70} catch (AttachNotSupportedException | IOException e) {71throw new RuntimeException(e);72} finally {73try {74vm.detach();75} catch (IOException e) {76throw new RuntimeException(e);77}78}79}8081private static void checkAttachListenerThread() throws InterruptedException, IOException {82JDKToolLauncher jcmd = JDKToolLauncher.createUsingTestJDK("jcmd");83jcmd.addVMArgs(Utils.getTestJavaOpts());84jcmd.addToolArg(strPID);85jcmd.addToolArg("Thread.print");8687ProcessBuilder pb = new ProcessBuilder(jcmd.getCommand());88Process jcmdProc = pb.start();8990OutputAnalyzer out = new OutputAnalyzer(jcmdProc);9192jcmdProc.waitFor();9394System.out.println(out.getStdout());95System.err.println(out.getStderr());9697long numOfAttachListener = out.asLines()98.stream()99.filter(l -> l.contains("Attach Listener"))100.count();101102Asserts.assertEquals(1L, numOfAttachListener, "AttachListener should exist only 1 thread.");103}104105public static void main(String... args) throws Exception {106LingeredApp app = null;107latch = new CountDownLatch(NUM_CONC_REQUESTS);108ExecutorService pool = Executors.newFixedThreadPool(NUM_CONC_REQUESTS);109110try {111app = LingeredApp.startApp();112strPID = Long.toString(app.getPid());113114for (int i = 0; i < NUM_CONC_REQUESTS; i++) {115pool.submit(new ConcAttachTest());116}117118pool.shutdown();119pool.awaitTermination(THREAD_POOL_TIMEOUT_IN_SEC, TimeUnit.SECONDS);120121checkAttachListenerThread();122} finally {123LingeredApp.stopApp(app);124}125}126127}128129130