test/hotspot/jtreg/runtime/LoadClass/TestResize.java
author gziemski
Thu, 02 Nov 2017 11:00:34 -0500
changeset 47774 69c081ca110a
child 48013 2098785677fb
permissions -rw-r--r--
8184765: Dynamically resize SystemDictionary Summary: Implemented dynamic resizing, which triggers when load factor is too high Reviewed-by: coleenp, rehn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47774
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
     1
/*
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
     4
 *
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
     7
 * published by the Free Software Foundation.
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
     8
 *
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    13
 * accompanied this code).
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    14
 *
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    18
 *
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    21
 * questions.
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    22
 */
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    23
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    24
/*
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    25
 * @test
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    26
 * @bug 8184765
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    27
 * @summary make sure the SystemDictionary gets resized when load factor is too high
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    28
 * @library /test/lib
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    29
 * @modules java.base/jdk.internal.misc
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    30
 *          java.management
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    31
 * @compile TriggerResize.java
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    32
 * @run driver TestResize
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    33
 */
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    34
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    35
import java.lang.ProcessBuilder;
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    36
import java.lang.Process;
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    37
import jdk.test.lib.process.ProcessTools;
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    38
import java.io.BufferedReader;
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    39
import java.io.InputStreamReader;
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    40
import java.util.Scanner;
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    41
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    42
public class TestResize {
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    43
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    44
  static double MAX_LOAD_FACTOR = 5.0; // see _resize_load_trigger in dictionary.cpp
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    45
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    46
  static int getInt(String string) {
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    47
    int start = 0;
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    48
    for (int i = 0; i < string.length(); i++) {
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    49
      if (!Character.isDigit(string.charAt(i))) {
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    50
        start++;
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    51
      } else {
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    52
        break;
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    53
      }
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    54
    }
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    55
    int end = start;
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    56
    for (int i = end; i < string.length(); i++) {
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    57
      if (Character.isDigit(string.charAt(i))) {
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    58
        end++;
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    59
      } else {
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    60
        break;
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    61
      }
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    62
    }
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    63
    return Integer.parseInt(string.substring(start, end));
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    64
  }
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    65
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    66
  static void analyzeOutputOn(ProcessBuilder pb) throws Exception {
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    67
    pb.redirectErrorStream(true);
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    68
    Process process = pb.start();
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    69
    BufferedReader rd = new BufferedReader(new InputStreamReader(process.getInputStream()));
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    70
    String line = rd.readLine();
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    71
    while (line != null) {
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    72
      if (line.startsWith("Java dictionary (")) {
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    73
        // ex. "Java dictionary (table_size=107, classes=6)"
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    74
        // ex. "Java dictionary (table_size=20201, classes=50002)"
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    75
        Scanner scanner = new Scanner(line);
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    76
        scanner.next();
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    77
        scanner.next();
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    78
        int table_size = getInt(scanner.next());
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    79
        int classes = getInt(scanner.next());
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    80
        scanner.close();
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    81
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    82
        double loadFactor = (double)classes / (double)table_size;
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    83
        if (loadFactor > MAX_LOAD_FACTOR) {
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    84
          throw new RuntimeException("Load factor too high, expected MAX "+MAX_LOAD_FACTOR+", got "+loadFactor);
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    85
        } else {
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    86
          System.out.println("PASS table_size:"+table_size+", classes:"+classes+" OK");
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    87
        }
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    88
      }
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    89
      line = rd.readLine();
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    90
    }
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    91
    int retval = process.waitFor();
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    92
    if (retval != 0) {
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    93
      throw new RuntimeException("Error: test returned non-zero value");
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    94
    }
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    95
  }
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    96
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    97
  public static void main(String[] args) throws Exception {
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    98
    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+PrintSystemDictionaryAtExit",
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
    99
                                                              "TriggerResize",
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
   100
                                                              "50000");
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
   101
    analyzeOutputOn(pb);
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
   102
  }
69c081ca110a 8184765: Dynamically resize SystemDictionary
gziemski
parents:
diff changeset
   103
}