Path: blob/master/test/hotspot/jtreg/gc/shenandoah/TestStringDedupStress.java
64478 views
/*1* Copyright (c) 2017, 2021, Red Hat, Inc. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/*25* @test id=passive26* @summary Test Shenandoah string deduplication implementation27* @key randomness28* @requires vm.gc.Shenandoah29* @library /test/lib30* @modules java.base/java.lang:open31* java.management32*33* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication34* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive35* -XX:+ShenandoahDegeneratedGC36* TestStringDedupStress37*38* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication39* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive40* -XX:-ShenandoahDegeneratedGC41* TestStringDedupStress42*/4344/*45* @test id=default46* @summary Test Shenandoah string deduplication implementation47* @key randomness48* @requires vm.gc.Shenandoah49* @library /test/lib50* @modules java.base/java.lang:open51* java.management52*53* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication54* -XX:+UseShenandoahGC55* -DtargetStrings=300000056* TestStringDedupStress57*58* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication59* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive60* -DtargetStrings=200000061* TestStringDedupStress62*63* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication64* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive65* -XX:+ShenandoahOOMDuringEvacALot66* -DtargetStrings=200000067* TestStringDedupStress68*69* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication70* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact71* TestStringDedupStress72*/7374/*75* @test id=iu76* @summary Test Shenandoah string deduplication implementation77* @key randomness78* @requires vm.gc.Shenandoah79* @library /test/lib80* @modules java.base/java.lang:open81* java.management82*83* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication84* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu85* TestStringDedupStress86*87* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication88* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive89* -DtargetStrings=200000090* TestStringDedupStress91*92* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication93* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu94* -XX:+ShenandoahOOMDuringEvacALot95* -DtargetStrings=200000096* TestStringDedupStress97*98* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication99* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive100* -XX:+ShenandoahOOMDuringEvacALot101* -DtargetStrings=2000000102* TestStringDedupStress103*/104105import java.lang.management.*;106import java.lang.reflect.*;107import java.util.*;108import jdk.test.lib.Utils;109110public class TestStringDedupStress {111private static Field valueField;112113private static final int TARGET_STRINGS = Integer.getInteger("targetStrings", 2_500_000);114private static final long MAX_REWRITE_GC_CYCLES = 6;115private static final long MAX_REWRITE_TIME = 30*1000; // ms116117private static final int UNIQUE_STRINGS = 20;118119static {120try {121valueField = String.class.getDeclaredField("value");122valueField.setAccessible(true);123} catch (Exception e) {124throw new RuntimeException(e);125}126}127128private static Object getValue(String string) {129try {130return valueField.get(string);131} catch (Exception e) {132throw new RuntimeException(e);133}134}135136static class StringAndId {137private String str;138private int id;139140public StringAndId(String str, int id) {141this.str = str;142this.id = id;143}144145public String str() {146return str;147}148149public int id() {150return id;151}152}153154// Generate uniqueStrings number of strings155private static void generateStrings(ArrayList<StringAndId> strs, int uniqueStrings) {156Random rn = Utils.getRandomInstance();157for (int u = 0; u < uniqueStrings; u++) {158int n = rn.nextInt(uniqueStrings);159strs.add(new StringAndId("Unique String " + n, n));160}161}162163private static int verifyDedupString(ArrayList<StringAndId> strs) {164Map<Object, StringAndId> seen = new HashMap<>(TARGET_STRINGS*2);165int total = 0;166int dedup = 0;167168for (StringAndId item : strs) {169total++;170StringAndId existingItem = seen.get(getValue(item.str()));171if (existingItem == null) {172seen.put(getValue(item.str()), item);173} else {174if (item.id() != existingItem.id() ||175!item.str().equals(existingItem.str())) {176System.out.println("StringDedup error:");177System.out.println("id: " + item.id() + " != " + existingItem.id());178System.out.println("or String: " + item.str() + " != " + existingItem.str());179throw new RuntimeException("StringDedup Test failed");180} else {181dedup++;182}183}184}185System.out.println("Dedup: " + dedup + "/" + total + " unique: " + (total - dedup));186return (total - dedup);187}188189static volatile ArrayList<StringAndId> astrs = new ArrayList<>();190static GarbageCollectorMXBean gcCycleMBean;191192public static void main(String[] args) {193Random rn = Utils.getRandomInstance();194195for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {196if ("Shenandoah Cycles".equals(bean.getName())) {197gcCycleMBean = bean;198break;199}200}201202if (gcCycleMBean == null) {203throw new RuntimeException("Can not find Shenandoah GC cycle mbean");204}205206// Generate roughly TARGET_STRINGS strings, only UNIQUE_STRINGS are unique207int genIters = TARGET_STRINGS / UNIQUE_STRINGS;208for (int index = 0; index < genIters; index++) {209generateStrings(astrs, UNIQUE_STRINGS);210}211212long cycleBeforeRewrite = gcCycleMBean.getCollectionCount();213long timeBeforeRewrite = System.currentTimeMillis();214215long loop = 1;216while (true) {217int arrSize = astrs.size();218int index = rn.nextInt(arrSize);219StringAndId item = astrs.get(index);220int n = rn.nextInt(UNIQUE_STRINGS);221item.str = "Unique String " + n;222item.id = n;223224if (loop++ % 1000 == 0) {225// enough GC cycles for rewritten strings to be deduplicated226if (gcCycleMBean.getCollectionCount() - cycleBeforeRewrite >= MAX_REWRITE_GC_CYCLES) {227break;228}229230// enough time is spent waiting for GC to happen231if (System.currentTimeMillis() - timeBeforeRewrite >= MAX_REWRITE_TIME) {232break;233}234}235}236verifyDedupString(astrs);237}238}239240241