Path: blob/master/test/hotspot/jtreg/vmTestbase/metaspace/shrink_grow/ShrinkGrowMultiJVM/ShrinkGrowMultiJVM.java
41155 views
/*1* Copyright (c) 2013, 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*/222324/*25* @test26*27* @summary converted from VM Testbase metaspace/shrink_grow/ShrinkGrowMultiJVM.28* VM Testbase keywords: [nonconcurrent]29*30* @requires vm.opt.final.ClassUnloading31* @library /vmTestbase /test/lib32* @build metaspace.shrink_grow.ShrinkGrowMultiJVM.ShrinkGrowMultiJVM33* @run driver metaspace.shrink_grow.ShrinkGrowMultiJVM.ShrinkGrowMultiJVM34*/3536package metaspace.shrink_grow.ShrinkGrowMultiJVM;3738import jdk.test.lib.Utils;3940import java.io.IOException;41import java.io.InputStream;42import java.io.OutputStream;43import java.nio.file.Paths;44import java.util.ArrayList;45import java.util.List;4647/**48* This test starts several JVMs and run ShrinkGrow metaspace test.49*50* It expected that all the parameters on start new processes are given51* in the command line.52*/53public class ShrinkGrowMultiJVM {54private static final String[] TEST_ARGS = {55Paths.get(Utils.TEST_JDK)56.resolve("bin")57.resolve("java")58.toAbsolutePath()59.toString(),60"-Xlog:gc:gc_$i.log", // LOG_GC_ARG_INDEX61"-XX:MetaspaceSize=10m",62"-XX:MaxMetaspaceSize=20m",63"-cp",64Utils.TEST_CLASS_PATH65};66private static final int LOG_GC_ARG_INDEX = 1;67public static void main(String argv[]) {68String[] testJavaOpts = Utils.getTestJavaOpts();69String[] args = new String[TEST_ARGS.length + testJavaOpts.length + 2];70System.arraycopy(TEST_ARGS, 0, args, 0, TEST_ARGS.length);71System.arraycopy(testJavaOpts, 0, args, TEST_ARGS.length, testJavaOpts.length);72args[args.length - 2] = metaspace.shrink_grow.ShrinkGrowTest.ShrinkGrowTest.class.getCanonicalName();73args[args.length - 1] = "jvm#$i";7475for (int i = 0; i < args.length; ++i) {76System.out.println("%arg #" + i + ": " + args[i]);77}7879List<Process> list = new ArrayList<>();80for (int i = 0; i < 5; i++) {81// will be used as jvm id82args[args.length - 1] = "jvm#" + i;83args[LOG_GC_ARG_INDEX] = "-Xlog:gc:gc_" + i + ".log";84ProcessBuilder pb = new ProcessBuilder(args);85try {86Process p = pb.start();87// Redirect.INHERIT doesn't work w/ @run driver88new Thread(() -> copy(p.getInputStream(), System.out)).start();89new Thread(() -> copy(p.getErrorStream(), System.out)).start();90list.add(p);91System.out.println("=== process #" + i + " started");92} catch (IOException e) {93throw new Error("Failed to start process " + i, e);94}95}96int failedCount = 0;97for (int i = 0; i < list.size(); i++) {98Process p = list.get(i);99try {100int exitCode = p.waitFor();101if (exitCode != 0) {102failedCount++;103System.out.println("=== process #" + i + " exitCode=" + exitCode);104}105} catch (InterruptedException e) {106failedCount++;107System.out.println("=== process #" + i + " waitFor failed");108e.printStackTrace(System.out);109}110}111if (failedCount != 0) {112throw new AssertionError(failedCount + " out of " + list.size() + " tests failed");113}114}115116private static void copy(InputStream is, OutputStream os) {117byte[] buffer = new byte[1024];118int n;119try (InputStream close = is) {120while ((n = is.read(buffer)) != -1) {121os.write(buffer, 0, n);122}123os.flush();124} catch (IOException e) {125e.printStackTrace();126}127}128}129130131