1 /*
 2  * Copyright (c) 2016, 2020, 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  * @requires vm.cds
32  * @library /test/lib
33  * @modules java.base/jdk.internal.misc
34  *          java.management
35  * @run driver gc.g1.TestSharedArchiveWithPreTouch
36  */
37 
38 import java.util.List;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 
42 import jdk.test.lib.Platform;
43 import jdk.test.lib.process.ProcessTools;
44 import jdk.test.lib.process.OutputAnalyzer;
45 
46 public class TestSharedArchiveWithPreTouch {
47     public static void main(String[] args) throws Exception {
48         final String ArchiveFileName = "./SharedArchiveWithPreTouch.jsa";
49 
50         final List<String> BaseOptions = Arrays.asList(new String[] {"-XX:+UseG1GC", "-XX:+AlwaysPreTouch",
51             "-XX:+UnlockDiagnosticVMOptions", "-XX:SharedArchiveFile=" + ArchiveFileName });
52 
53         ProcessBuilder pb;
54 
55         List<String> dump_args = new ArrayList<String>(BaseOptions);
56 
57         if (Platform.is64bit()) {
58           dump_args.addAll(0, Arrays.asList(new String[] { "-XX:+UseCompressedClassPointers", "-XX:+UseCompressedOops" }));
59         }
60         dump_args.addAll(Arrays.asList(new String[] { "-Xshare:dump", "-Xlog:cds*", "-Xlog:metaspace*" }));
61 
62         pb = ProcessTools.createJavaProcessBuilder(dump_args);
63         OutputAnalyzer output = new OutputAnalyzer(pb.start());
64         output.reportDiagnosticSummary();
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", "-Xlog:cds*", "-Xlog:metaspace*", "-version" }));
75 
76             pb = ProcessTools.createJavaProcessBuilder(load_args.toArray(new String[0]));
77             output = new OutputAnalyzer(pb.start());
78             output.reportDiagnosticSummary();
79             output.shouldContain("sharing");
80             output.shouldHaveExitValue(0);
81         } catch (RuntimeException e) {
82             // Report 'passed' if CDS was turned off.
83             output.shouldContain("Unable to use shared archive");
84             output.shouldHaveExitValue(1);
85         }
86     }
87 }