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