Path: blob/master/test/hotspot/jtreg/gc/shenandoah/TestStringDedup.java
40942 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 TestStringDedup26* @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 -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication35* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive36* -XX:+ShenandoahDegeneratedGC -DGCCount=137* TestStringDedup38*39* @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication40* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive41* -XX:-ShenandoahDegeneratedGC -DGCCount=142* TestStringDedup43*/4445/*46* @test TestStringDedup47* @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 -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication56* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive -XX:StringDeduplicationAgeThreshold=357* TestStringDedup58*59* @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication60* -XX:+UseShenandoahGC -XX:StringDeduplicationAgeThreshold=361* TestStringDedup62*63* @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication64* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact -XX:StringDeduplicationAgeThreshold=365* TestStringDedup66*/6768/*69* @test TestStringDedup70* @summary Test Shenandoah string deduplication implementation71* @key randomness72* @requires vm.gc.Shenandoah73* @library /test/lib74* @modules java.base/jdk.internal.misc:open75* @modules java.base/java.lang:open76* java.management77*78* @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication79* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:StringDeduplicationAgeThreshold=380* TestStringDedup81*82* @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication83* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive -XX:StringDeduplicationAgeThreshold=384* TestStringDedup85*/8687import java.lang.reflect.*;88import java.util.*;89import jdk.test.lib.Utils;9091import sun.misc.*;9293public class TestStringDedup {94private static Field valueField;95private static Unsafe unsafe;9697private static final int UniqueStrings = 20;98// How many GC cycles are needed to complete deduplication.99private static final int GCCount = Integer.getInteger("GCCount", 3);100101static {102try {103Field field = Unsafe.class.getDeclaredField("theUnsafe");104field.setAccessible(true);105unsafe = (Unsafe) field.get(null);106107valueField = String.class.getDeclaredField("value");108valueField.setAccessible(true);109} catch (Exception e) {110throw new RuntimeException(e);111}112}113114private static Object getValue(String string) {115try {116return valueField.get(string);117} catch (Exception e) {118throw new RuntimeException(e);119}120}121122static class StringAndId {123private String str;124private int id;125126public StringAndId(String str, int id) {127this.str = str;128this.id = id;129}130131public String str() {132return str;133}134135public int id() {136return id;137}138}139140private static void generateStrings(ArrayList<StringAndId> strs, int unique_strs) {141Random rn = Utils.getRandomInstance();142for (int u = 0; u < unique_strs; u++) {143int n = rn.nextInt() % 10;144n = Math.max(n, 2);145for (int index = 0; index < n; index++) {146strs.add(new StringAndId("Unique String " + u, u));147}148}149}150151private static int verifyDedepString(ArrayList<StringAndId> strs) {152HashMap<Object, StringAndId> seen = new HashMap<>();153int total = 0;154int dedup = 0;155156for (StringAndId item : strs) {157total++;158StringAndId existing_item = seen.get(getValue(item.str()));159if (existing_item == null) {160seen.put(getValue(item.str()), item);161} else {162if (item.id() != existing_item.id() ||163!item.str().equals(existing_item.str())) {164System.out.println("StringDedup error:");165System.out.println("String: " + item.str() + " != " + existing_item.str());166throw new RuntimeException("StringDedup Test failed");167} else {168dedup++;169}170}171}172return (total - dedup);173}174175public static void main(String[] args) {176ArrayList<StringAndId> astrs = new ArrayList<>();177generateStrings(astrs, UniqueStrings);178for (int count = 0; count < GCCount; count ++) {179System.gc();180}181182int unique_count = 0;183for (int waitCount = 0; waitCount < 3; waitCount ++) {184// Let concurrent string dedup thread to run185try {186Thread.sleep(1000);187} catch (InterruptedException e) {188}189190// All deduplicated, done.191unique_count = verifyDedepString(astrs);192if ( unique_count == UniqueStrings) {193return;194}195}196197throw new RuntimeException("Expecting " + UniqueStrings + " unique strings, but got " + unique_count);198}199}200201202