1 /*
   2  * Copyright (c) 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
  26  * @bug 8184765
  27  * @summary make sure the SystemDictionary gets resized when load factor is too high
  28  * @library /test/lib
  29  * @modules java.base/jdk.internal.misc
  30  *          java.management
  31  * @compile TriggerResize.java
  32  * @run driver TestResize
  33  */
  34 
  35 import java.lang.ProcessBuilder;
  36 import java.lang.Process;
  37 import jdk.test.lib.process.ProcessTools;
  38 import java.io.BufferedReader;
  39 import java.io.InputStreamReader;
  40 import java.util.Scanner;
  41 
  42 public class TestResize {
  43 
  44   static double MAX_LOAD_FACTOR = 5.0; // see _resize_load_trigger in dictionary.cpp
  45 
  46   static int getInt(String string) {
  47     int start = 0;
  48     for (int i = 0; i < string.length(); i++) {
  49       if (!Character.isDigit(string.charAt(i))) {
  50         start++;
  51       } else {
  52         break;
  53       }
  54     }
  55     int end = start;
  56     for (int i = end; i < string.length(); i++) {
  57       if (Character.isDigit(string.charAt(i))) {
  58         end++;
  59       } else {
  60         break;
  61       }
  62     }
  63     return Integer.parseInt(string.substring(start, end));
  64   }
  65 
  66   static void analyzeOutputOn(ProcessBuilder pb) throws Exception {
  67     pb.redirectErrorStream(true);
  68     Process process = pb.start();
  69     BufferedReader rd = new BufferedReader(new InputStreamReader(process.getInputStream()));
  70     String line = rd.readLine();
  71     while (line != null) {
  72       if (line.startsWith("Java dictionary (")) {
  73         // ex. "Java dictionary (table_size=107, classes=6)"
  74         // ex. "Java dictionary (table_size=20201, classes=50002)"
  75         Scanner scanner = new Scanner(line);
  76         scanner.next();
  77         scanner.next();
  78         int table_size = getInt(scanner.next());
  79         int classes = getInt(scanner.next());
  80         scanner.close();
  81 
  82         double loadFactor = (double)classes / (double)table_size;
  83         if (loadFactor > MAX_LOAD_FACTOR) {
  84           throw new RuntimeException("Load factor too high, expected MAX "+MAX_LOAD_FACTOR+", got "+loadFactor);
  85         } else {
  86           System.out.println("PASS table_size:"+table_size+", classes:"+classes+" OK");
  87         }
  88       }
  89       line = rd.readLine();
  90     }
  91     int retval = process.waitFor();
  92     if (retval != 0) {
  93       throw new RuntimeException("Error: test returned non-zero value");
  94     }
  95   }
  96 
  97   public static void main(String[] args) throws Exception {
  98     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+PrintSystemDictionaryAtExit",
  99                                                               "TriggerResize",
 100                                                               "50000");
 101     analyzeOutputOn(pb);
 102   }
 103 }