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  * @requires vm.opt.final.ClassUnloading
32  * @library /vmTestbase /test/lib
33  * @run driver jdk.test.lib.FileInstaller . .
34  * @build metaspace.shrink_grow.ShrinkGrowMultiJVM.ShrinkGrowMultiJVM
35  * @run driver metaspace.shrink_grow.ShrinkGrowMultiJVM.ShrinkGrowMultiJVM
36  */
37 
38 package metaspace.shrink_grow.ShrinkGrowMultiJVM;
39 
40 import jdk.test.lib.Utils;
41 
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.io.OutputStream;
45 import java.nio.file.Paths;
46 import java.util.ArrayList;
47 import java.util.List;
48 
49 /**
50  * This test starts several JVMs and run ShrinkGrow metaspace test.
51  *
52  * It expected that all the parameters on start new processes are given
53  * in the command line.
54  */
55 public class ShrinkGrowMultiJVM {
56     private static final String[] TEST_ARGS = {
57             Paths.get(Utils.TEST_JDK)
58                  .resolve("bin")
59                  .resolve("java")
60                  .toAbsolutePath()
61                  .toString(),
62             "-Xlog:gc:gc_$i.log", // LOG_GC_ARG_INDEX
63             "-XX:MetaspaceSize=10m",
64             "-XX:MaxMetaspaceSize=20m",
65             "-cp",
66             Utils.TEST_CLASS_PATH
67     };
68     private static final int LOG_GC_ARG_INDEX = 1;
main(String argv[])69     public static void main(String argv[]) {
70         String[] testJavaOpts = Utils.getTestJavaOpts();
71         String[] args = new String[TEST_ARGS.length + testJavaOpts.length + 2];
72         System.arraycopy(TEST_ARGS, 0, args, 0, TEST_ARGS.length);
73         System.arraycopy(testJavaOpts, 0, args, TEST_ARGS.length, testJavaOpts.length);
74         args[args.length - 2] = metaspace.shrink_grow.ShrinkGrowTest.ShrinkGrowTest.class.getCanonicalName();
75         args[args.length - 1] = "jvm#$i";
76 
77         for (int i = 0; i < args.length; ++i) {
78             System.out.println("%arg #" + i + ": " + args[i]);
79         }
80 
81         List<Process> list = new ArrayList<>();
82         for (int i = 0; i < 5; i++) {
83             // will be used as jvm id
84             args[args.length - 1] = "jvm#" + i;
85             args[LOG_GC_ARG_INDEX] = "-Xlog:gc:gc_" + i + ".log";
86             ProcessBuilder pb = new ProcessBuilder(args);
87             try {
88                 Process p = pb.start();
89                 // Redirect.INHERIT doesn't work w/ @run driver
90                 new Thread(() -> copy(p.getInputStream(), System.out)).start();
91                 new Thread(() -> copy(p.getErrorStream(), System.out)).start();
92                 list.add(p);
93                 System.out.println("=== process #" + i + " started");
94             } catch (IOException e) {
95                 throw new Error("Failed to start process " + i, e);
96             }
97         }
98         int failedCount = 0;
99         for (int i = 0; i < list.size(); i++) {
100             Process p = list.get(i);
101             try {
102                 int exitCode = p.waitFor();
103                 if (exitCode != 0) {
104                     failedCount++;
105                     System.out.println("=== process #" + i + " exitCode=" + exitCode);
106                 }
107             } catch (InterruptedException e) {
108                 failedCount++;
109                 System.out.println("=== process #" + i + " waitFor failed");
110                 e.printStackTrace(System.out);
111             }
112         }
113         if (failedCount != 0) {
114             throw new AssertionError(failedCount + " out of " + list.size() + " tests failed");
115         }
116     }
117 
copy(InputStream is, OutputStream os)118     private static void copy(InputStream is, OutputStream os) {
119         byte[] buffer = new byte[1024];
120         int n;
121         try (InputStream close = is) {
122             while ((n = is.read(buffer)) != -1) {
123                 os.write(buffer, 0, n);
124             }
125             os.flush();
126         } catch (IOException e) {
127             e.printStackTrace();
128         }
129     }
130 }
131