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