1 /*
   2  * Copyright (c) 2014, 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 /**
  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.cds.CDSOptions;
  37 import jdk.test.lib.process.OutputAnalyzer;
  38 import sun.hotspot.WhiteBox;
  39 
  40 import java.util.regex.Pattern;
  41 import java.util.regex.Matcher;
  42 import java.util.ArrayList;
  43 import java.util.Hashtable;
  44 import java.lang.Integer;
  45 
  46 public class SpaceUtilizationCheck {
  47     // For the MC/RW/RO regions:
  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("-Xlog:cds=debug");
  55 
  56         // (2) Use the now deprecated VM arguments. They should have no effect.
  57         test("-Xlog:cds=debug",
  58              "-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         CDSOptions opts = new CDSOptions();
  66         opts.addSuffix(extra_options);
  67         OutputAnalyzer output = CDSTestUtils.createArchive(opts);
  68         CDSTestUtils.checkDump(output);
  69         Pattern pattern = Pattern.compile("(..)  space: *([0-9]+).* out of *([0-9]+) bytes .* at 0x([0-9a0-f]+)");
  70         WhiteBox wb = WhiteBox.getWhiteBox();
  71         long reserve_alignment = wb.metaspaceReserveAlignment();
  72         System.out.println("Metaspace::reserve_alignment() = " + reserve_alignment);
  73 
  74         // Look for output like this. The pattern will only match the first 3 regions, which is what we need to check
  75         //
  76         // [4.682s][debug][cds] mc  space:     24912 [  0.2% of total] out of     28672 bytes [ 86.9% used] at 0x0000000800000000
  77         // [4.682s][debug][cds] rw  space:   4391632 [ 33.7% of total] out of   4395008 bytes [ 99.9% used] at 0x0000000800007000
  78         // [4.682s][debug][cds] ro  space:   7570632 [ 58.0% of total] out of   7573504 bytes [100.0% used] at 0x0000000800438000
  79         // [4.682s][debug][cds] bm  space:    213528 [  1.6% of total] out of    213528 bytes [100.0% used]
  80         // [4.682s][debug][cds] ca0 space:    507904 [  3.9% of total] out of    507904 bytes [100.0% used] at 0x00000000fff00000
  81         // [4.682s][debug][cds] oa0 space:    327680 [  2.5% of total] out of    327680 bytes [100.0% used] at 0x00000000ffe00000
  82         // [4.682s][debug][cds] total    :  13036288 [100.0% of total] out of  13049856 bytes [ 99.9% used]
  83 
  84         long last_region = -1;
  85         Hashtable<String,String> checked = new Hashtable<>();
  86         for (String line : output.getStdout().split("\n")) {
  87             if (line.contains(" space:") && !line.contains("st space:")) {
  88                 Matcher matcher = pattern.matcher(line);
  89                 if (matcher.find()) {
  90                     String name = matcher.group(1);
  91                     System.out.println("Checking " + name + " in : " + line);
  92                     checked.put(name, name);
  93                     long used = Long.parseLong(matcher.group(2));
  94                     long capacity = Long.parseLong(matcher.group(3));
  95                     long address = Long.parseLong(matcher.group(4), 16);
  96                     long unused = capacity - used;
  97                     if (unused < 0) {
  98                         throw new RuntimeException("Unused space (" + unused + ") less than 0");
  99                     }
 100                     if (unused > reserve_alignment) {
 101                         // [1] Check for unused space
 102                         throw new RuntimeException("Unused space (" + unused + ") must be smaller than Metaspace::reserve_alignment() (" +
 103                                                    reserve_alignment + ")");
 104                     }
 105                     if (last_region >= 0 && address != last_region) {
 106                         // [2] Check for no-gap
 107                         throw new RuntimeException("Region 0x" + address + " should have started at 0x" + Long.toString(last_region, 16));
 108                     }
 109                     last_region = address + capacity;
 110                 }
 111             }
 112         }
 113         if (checked.size() != 3) {
 114           throw new RuntimeException("Must have 3 consecutive, fully utilized regions"); // MC,RW,RO
 115         }
 116     }
 117 }