1 /*
   2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 /*
  26  * @test
  27  *
  28  * @summary converted from VM Testbase metaspace/shrink_grow/ShrinkGrowMultiJVM.
  29  * VM Testbase keywords: [nonconcurrent]
  30  *
  31  * @library /vmTestbase /test/lib
  32  * @run driver jdk.test.lib.FileInstaller . .
  33  * @build metaspace.shrink_grow.ShrinkGrowMultiJVM.ShrinkGrowMultiJVM
  34  * @run driver metaspace.shrink_grow.ShrinkGrowMultiJVM.ShrinkGrowMultiJVM
  35  */
  36 
  37 package metaspace.shrink_grow.ShrinkGrowMultiJVM;
  38 
  39 import jdk.test.lib.Utils;
  40 
  41 import java.io.IOException;
  42 import java.io.InputStream;
  43 import java.io.OutputStream;
  44 import java.nio.file.Paths;
  45 import java.util.ArrayList;
  46 import java.util.List;
  47 
  48 /**
  49  * This test starts several JVMs and run ShrinkGrow metaspace test.
  50  *
  51  * It expected that all the parameters on start new processes are given
  52  * in the command line.
  53  */
  54 public class ShrinkGrowMultiJVM {
  55     private static final String[] TEST_ARGS = {
  56             Paths.get(Utils.TEST_JDK)
  57                  .resolve("bin")
  58                  .resolve("java")
  59                  .toAbsolutePath()
  60                  .toString(),
  61             "-Xlog:gc:gc_$i.log", // LOG_GC_ARG_INDEX
  62             "-XX:MetaspaceSize=10m",
  63             "-XX:MaxMetaspaceSize=20m",
  64             "-cp",
  65             Utils.TEST_CLASS_PATH
  66     };
  67     private static final int LOG_GC_ARG_INDEX = 1;
  68     public static void main(String argv[]) {
  69         String[] testJavaOpts = Utils.getTestJavaOpts();
  70         String[] args = new String[TEST_ARGS.length + testJavaOpts.length + 2];
  71         System.arraycopy(TEST_ARGS, 0, args, 0, TEST_ARGS.length);
  72         System.arraycopy(testJavaOpts, 0, args, TEST_ARGS.length, testJavaOpts.length);
  73         args[args.length - 2] = metaspace.shrink_grow.ShrinkGrowTest.ShrinkGrowTest.class.getCanonicalName();
  74         args[args.length - 1] = "jvm#$i";
  75 
  76         for (int i = 0; i < args.length; ++i) {
  77             System.out.println("%arg #" + i + ": " + args[i]);
  78         }
  79 
  80         List<Process> list = new ArrayList<>();
  81         for (int i = 0; i < 5; i++) {
  82             // will be used as jvm id
  83             args[args.length - 1] = "jvm#" + i;
  84             args[LOG_GC_ARG_INDEX] = "-Xlog:gc:gc_" + i + ".log";
  85             ProcessBuilder pb = new ProcessBuilder(args);
  86             try {
  87                 Process p = pb.start();
  88                 // Redirect.INHERIT doesn't work w/ @run driver
  89                 new Thread(() -> copy(p.getInputStream(), System.out)).start();
  90                 new Thread(() -> copy(p.getErrorStream(), System.out)).start();
  91                 list.add(p);
  92                 System.out.println("=== process #" + i + " started");
  93             } catch (IOException e) {
  94                 throw new Error("Failed to start process " + i, e);
  95             }
  96         }
  97         int failedCount = 0;
  98         for (int i = 0; i < list.size(); i++) {
  99             Process p = list.get(i);
 100             try {
 101                 int exitCode = p.waitFor();
 102                 if (exitCode != 0) {
 103                     failedCount++;
 104                     System.out.println("=== process #" + i + " exitCode=" + exitCode);
 105                 }
 106             } catch (InterruptedException e) {
 107                 failedCount++;
 108                 System.out.println("=== process #" + i + " waitFor failed");
 109                 e.printStackTrace(System.out);
 110             }
 111         }
 112         if (failedCount != 0) {
 113             throw new AssertionError(failedCount + " out of " + list.size() + " tests failed");
 114         }
 115     }
 116 
 117     private static void copy(InputStream is, OutputStream os) {
 118         byte[] buffer = new byte[1024];
 119         int n;
 120         try (InputStream close = is) {
 121             while ((n = is.read(buffer)) != -1) {
 122                 os.write(buffer, 0, n);
 123             }
 124             os.flush();
 125         } catch (IOException e) {
 126             e.printStackTrace();
 127         }
 128     }
 129 }