1 /*
2  * Copyright (c) 2016, 2019, 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 package gc.g1;
25 
26 /**
27  * @test
28  * @bug 8169703
29  * @summary Verifies that dumping and loading a CDS archive succeeds with AlwaysPreTouch
30  * @requires vm.gc.G1
31  * @key gc regression
32  * @requires vm.cds
33  * @library /test/lib
34  * @modules java.base/jdk.internal.misc
35  *          java.management
36  * @run main gc.g1.TestSharedArchiveWithPreTouch
37  */
38 
39 import java.util.List;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 
43 import jdk.test.lib.Platform;
44 import jdk.test.lib.process.ProcessTools;
45 import jdk.test.lib.process.OutputAnalyzer;
46 
47 public class TestSharedArchiveWithPreTouch {
main(String[] args)48     public static void main(String[] args) throws Exception {
49         final String ArchiveFileName = "./SharedArchiveWithPreTouch.jsa";
50 
51         final List<String> BaseOptions = Arrays.asList(new String[] {"-XX:+UseG1GC", "-XX:+AlwaysPreTouch",
52             "-XX:+UnlockDiagnosticVMOptions", "-XX:SharedArchiveFile=" + ArchiveFileName });
53 
54         ProcessBuilder pb;
55 
56         List<String> dump_args = new ArrayList<String>(BaseOptions);
57 
58         if (Platform.is64bit()) {
59           dump_args.addAll(0, Arrays.asList(new String[] { "-XX:+UseCompressedClassPointers", "-XX:+UseCompressedOops" }));
60         }
61         dump_args.addAll(Arrays.asList(new String[] { "-Xshare:dump" }));
62 
63         pb = ProcessTools.createJavaProcessBuilder(dump_args.toArray(new String[0]));
64         OutputAnalyzer output = new OutputAnalyzer(pb.start());
65         try {
66             output.shouldContain("Loading classes to share");
67             output.shouldHaveExitValue(0);
68 
69             List<String> load_args = new ArrayList<String>(BaseOptions);
70 
71             if (Platform.is64bit()) {
72                 load_args.addAll(0, Arrays.asList(new String[] { "-XX:+UseCompressedClassPointers", "-XX:+UseCompressedOops" }));
73             }
74             load_args.addAll(Arrays.asList(new String[] { "-Xshare:on", "-version" }));
75 
76             pb = ProcessTools.createJavaProcessBuilder(load_args.toArray(new String[0]));
77             output = new OutputAnalyzer(pb.start());
78             output.shouldContain("sharing");
79             output.shouldHaveExitValue(0);
80         } catch (RuntimeException e) {
81             // Report 'passed' if CDS was turned off.
82             output.shouldContain("Unable to use shared archive");
83             output.shouldHaveExitValue(1);
84         }
85     }
86 }
87