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  * @run main SpaceUtilizationCheck
  33  */
  34 
  35 import jdk.test.lib.cds.CDSTestUtils;
  36 import jdk.test.lib.process.OutputAnalyzer;
  37 
  38 import java.util.regex.Pattern;
  39 import java.util.regex.Matcher;
  40 import java.util.ArrayList;
  41 import java.lang.Integer;
  42 
  43 public class SpaceUtilizationCheck {
  44     // Minimum allowed utilization value (percent)
  45     // The goal is to have this number to be 50% for RO and RW regions
  46     // Once that feature is implemented, increase the MIN_UTILIZATION to 50
  47     private static final int MIN_UTILIZATION = 30;
  48 
  49     // Only RO and RW regions are considered for this check, since they
  50     // currently account for the bulk of the shared space
  51     private static final int NUMBER_OF_CHECKED_SHARED_REGIONS = 2;
  52 
  53     public static void main(String[] args) throws Exception {
  54         OutputAnalyzer output = CDSTestUtils.createArchive();
  55         CDSTestUtils.checkDump(output);
  56 
  57         String stdout = output.getStdout();
  58         ArrayList<String> utilization = findUtilization(stdout);
  59 
  60         if (utilization.size() != NUMBER_OF_CHECKED_SHARED_REGIONS )
  61             throw new RuntimeException("The output format of sharing summary has changed");
  62 
  63         for(String str : utilization) {
  64             int value = Integer.parseInt(str);
  65             if (value < MIN_UTILIZATION) {
  66                 System.out.println(stdout);
  67                 throw new RuntimeException("Utilization for one of the regions" +
  68                     "is below a threshold of " + MIN_UTILIZATION + "%");
  69             }
  70         }
  71     }
  72 
  73     public static ArrayList<String> findUtilization(String input) {
  74         ArrayList<String> regions = filterRegionsOfInterest(input.split("\n"));
  75         return filterByPattern(filterByPattern(regions, "bytes \\[.*% used\\]"), "\\d+");
  76     }
  77 
  78     private static ArrayList<String> filterByPattern(Iterable<String> input, String pattern) {
  79         ArrayList<String> result = new ArrayList<String>();
  80         for (String str : input) {
  81             Matcher matcher = Pattern.compile(pattern).matcher(str);
  82             if (matcher.find()) {
  83                 result.add(matcher.group());
  84             }
  85         }
  86         return result;
  87     }
  88 
  89     private static ArrayList<String> filterRegionsOfInterest(String[] inputLines) {
  90         ArrayList<String> result = new ArrayList<String>();
  91         for (String str : inputLines) {
  92             if (str.contains("ro space:") || str.contains("rw space:")) {
  93                 result.add(str);
  94             }
  95         }
  96         return result;
  97     }
  98 }