1 /*
   2  * Copyright (c) 2014, 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 SpaceUtilizationCheck
  26  * @requires vm.cds
  27  * @summary Check if the space utilization for shared spaces is adequate
  28  * @requires (vm.opt.UseCompressedOops == null) | (vm.opt.UseCompressedOops == true)
  29  * @library /test/lib
  30  * @modules java.base/jdk.internal.misc
  31  *          java.management
  32  * @build sun.hotspot.WhiteBox
  33  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  34  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  35  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI SpaceUtilizationCheck
  36  */
  37 
  38 import jdk.test.lib.cds.CDSTestUtils;
  39 import jdk.test.lib.process.OutputAnalyzer;
  40 import sun.hotspot.WhiteBox;
  41 
  42 import java.util.regex.Pattern;
  43 import java.util.regex.Matcher;
  44 import java.util.ArrayList;
  45 import java.util.Hashtable;
  46 import java.lang.Integer;
  47 
  48 public class SpaceUtilizationCheck {
  49     // [1] Each region must have strictly less than
  50     //     WhiteBox.metaspaceReserveAlignment() bytes of unused space.
  51     // [2] There must be no gap between two consecutive regions.
  52 
  53     public static void main(String[] args) throws Exception {
  54         // (1) Default VM arguments
  55         test();
  56 
  57         // (2) Use the now deprecated VM arguments. They should have no effect.
  58         test("-XX:SharedReadWriteSize=128M",
  59              "-XX:SharedReadOnlySize=128M",
  60              "-XX:SharedMiscDataSize=128M",
  61              "-XX:SharedMiscCodeSize=128M");
  62     }
  63 
  64     static void test(String... extra_options) throws Exception {
  65         OutputAnalyzer output = CDSTestUtils.createArchive(extra_options);
  66         CDSTestUtils.checkDump(output);
  67         Pattern pattern = Pattern.compile("^(..) space: *([0-9]+).* out of *([0-9]+) bytes .* at 0x([0-9a0-f]+)");
  68         WhiteBox wb = WhiteBox.getWhiteBox();
  69         long reserve_alignment = wb.metaspaceReserveAlignment();
  70         System.out.println("Metaspace::reserve_alignment() = " + reserve_alignment);
  71 
  72         long last_region = -1;
  73         Hashtable<String,String> checked = new Hashtable<>();
  74         for (String line : output.getStdout().split("\n")) {
  75             if (line.contains(" space:") && !line.contains("st space:")) {
  76                 Matcher matcher = pattern.matcher(line);
  77                 if (matcher.find()) {
  78                     String name = matcher.group(1);
  79                     if (name.equals("s0") || name.equals("s1")) {
  80                       // String regions are listed at the end and they may not be fully occupied.
  81                       break;
  82                     } else {
  83                       System.out.println("Checking " + name + " in : " + line);
  84                       checked.put(name, name);
  85                     }
  86                     long used = Long.parseLong(matcher.group(2));
  87                     long capacity = Long.parseLong(matcher.group(3));
  88                     long address = Long.parseLong(matcher.group(4), 16);
  89                     long unused = capacity - used;
  90                     if (unused < 0) {
  91                         throw new RuntimeException("Unused space (" + unused + ") less than 0");
  92                     }
  93                     if (unused > reserve_alignment) {
  94                         // [1] Check for unused space
  95                         throw new RuntimeException("Unused space (" + unused + ") must be smaller than Metaspace::reserve_alignment() (" +
  96                                                    reserve_alignment + ")");
  97                     }
  98                     if (last_region >= 0 && address != last_region) {
  99                         // [2] Check for no-gap
 100                         throw new RuntimeException("Region 0x" + address + " should have started at 0x" + Long.toString(last_region, 16));
 101                     }
 102                     last_region = address + capacity;
 103                 }
 104             }
 105         }
 106         if (checked.size() != 5) {
 107           throw new RuntimeException("Must have 5 consecutive, fully utilized regions");
 108         }
 109     }
 110 }