Path: blob/master/test/hotspot/jtreg/gc/shenandoah/TestStringDedupStress.java
40942 views
/*1* Copyright (c) 2017, 2018, 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 TestStringDedupStress26* @summary Test Shenandoah string deduplication implementation27* @key randomness28* @requires vm.gc.Shenandoah29* @library /test/lib30* @modules java.base/jdk.internal.misc:open31* @modules java.base/java.lang:open32* java.management33*34* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication35* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive36* -XX:+ShenandoahDegeneratedGC37* TestStringDedupStress38*39* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication40* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive41* -XX:-ShenandoahDegeneratedGC42* TestStringDedupStress43*/4445/*46* @test TestStringDedupStress47* @summary Test Shenandoah string deduplication implementation48* @key randomness49* @requires vm.gc.Shenandoah50* @library /test/lib51* @modules java.base/jdk.internal.misc:open52* @modules java.base/java.lang:open53* java.management54*55* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication56* -XX:+UseShenandoahGC57* -DtargetStrings=300000058* TestStringDedupStress59*60* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication61* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive62* -DtargetStrings=200000063* TestStringDedupStress64*65* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication66* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive67* -XX:+ShenandoahOOMDuringEvacALot68* -DtargetStrings=200000069* TestStringDedupStress70*71* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication72* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact73* TestStringDedupStress74*/7576/*77* @test TestStringDedupStress78* @summary Test Shenandoah string deduplication implementation79* @key randomness80* @requires vm.gc.Shenandoah81* @library /test/lib82* @modules java.base/jdk.internal.misc:open83* @modules java.base/java.lang:open84* java.management85*86* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication87* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu88* TestStringDedupStress89*90* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication91* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive92* -DtargetStrings=200000093* TestStringDedupStress94*95* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication96* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu97* -XX:+ShenandoahOOMDuringEvacALot98* -DtargetStrings=200000099* TestStringDedupStress100*101* @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication102* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive103* -XX:+ShenandoahOOMDuringEvacALot104* -DtargetStrings=2000000105* TestStringDedupStress106*/107108import java.lang.management.*;109import java.lang.reflect.*;110import java.util.*;111import jdk.test.lib.Utils;112113import sun.misc.*;114115public class TestStringDedupStress {116private static Field valueField;117private static Unsafe unsafe;118119private static final int TARGET_STRINGS = Integer.getInteger("targetStrings", 2_500_000);120private static final long MAX_REWRITE_GC_CYCLES = 6;121private static final long MAX_REWRITE_TIME = 30*1000; // ms122123private static final int UNIQUE_STRINGS = 20;124125static {126try {127Field field = Unsafe.class.getDeclaredField("theUnsafe");128field.setAccessible(true);129unsafe = (Unsafe) field.get(null);130131valueField = String.class.getDeclaredField("value");132valueField.setAccessible(true);133} catch (Exception e) {134throw new RuntimeException(e);135}136}137138private static Object getValue(String string) {139try {140return valueField.get(string);141} catch (Exception e) {142throw new RuntimeException(e);143}144}145146static class StringAndId {147private String str;148private int id;149150public StringAndId(String str, int id) {151this.str = str;152this.id = id;153}154155public String str() {156return str;157}158159public int id() {160return id;161}162}163164// Generate uniqueStrings number of strings165private static void generateStrings(ArrayList<StringAndId> strs, int uniqueStrings) {166Random rn = Utils.getRandomInstance();167for (int u = 0; u < uniqueStrings; u++) {168int n = rn.nextInt(uniqueStrings);169strs.add(new StringAndId("Unique String " + n, n));170}171}172173private static int verifyDedupString(ArrayList<StringAndId> strs) {174Map<Object, StringAndId> seen = new HashMap<>(TARGET_STRINGS*2);175int total = 0;176int dedup = 0;177178for (StringAndId item : strs) {179total++;180StringAndId existingItem = seen.get(getValue(item.str()));181if (existingItem == null) {182seen.put(getValue(item.str()), item);183} else {184if (item.id() != existingItem.id() ||185!item.str().equals(existingItem.str())) {186System.out.println("StringDedup error:");187System.out.println("id: " + item.id() + " != " + existingItem.id());188System.out.println("or String: " + item.str() + " != " + existingItem.str());189throw new RuntimeException("StringDedup Test failed");190} else {191dedup++;192}193}194}195System.out.println("Dedup: " + dedup + "/" + total + " unique: " + (total - dedup));196return (total - dedup);197}198199static volatile ArrayList<StringAndId> astrs = new ArrayList<>();200static GarbageCollectorMXBean gcCycleMBean;201202public static void main(String[] args) {203Random rn = Utils.getRandomInstance();204205for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {206if ("Shenandoah Cycles".equals(bean.getName())) {207gcCycleMBean = bean;208break;209}210}211212if (gcCycleMBean == null) {213throw new RuntimeException("Can not find Shenandoah GC cycle mbean");214}215216// Generate roughly TARGET_STRINGS strings, only UNIQUE_STRINGS are unique217int genIters = TARGET_STRINGS / UNIQUE_STRINGS;218for (int index = 0; index < genIters; index++) {219generateStrings(astrs, UNIQUE_STRINGS);220}221222long cycleBeforeRewrite = gcCycleMBean.getCollectionCount();223long timeBeforeRewrite = System.currentTimeMillis();224225long loop = 1;226while (true) {227int arrSize = astrs.size();228int index = rn.nextInt(arrSize);229StringAndId item = astrs.get(index);230int n = rn.nextInt(UNIQUE_STRINGS);231item.str = "Unique String " + n;232item.id = n;233234if (loop++ % 1000 == 0) {235// enough GC cycles for rewritten strings to be deduplicated236if (gcCycleMBean.getCollectionCount() - cycleBeforeRewrite >= MAX_REWRITE_GC_CYCLES) {237break;238}239240// enough time is spent waiting for GC to happen241if (System.currentTimeMillis() - timeBeforeRewrite >= MAX_REWRITE_TIME) {242break;243}244}245}246verifyDedupString(astrs);247}248}249250251