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