Path: blob/master/test/hotspot/jtreg/gc/shenandoah/TestStringDedup.java
64479 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/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 id=default47* @summary Test Shenandoah string deduplication implementation48* @key randomness49* @requires vm.gc.Shenandoah50* @library /test/lib51* @modules java.base/java.lang:open52* java.management53*54* @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication55* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive -XX:StringDeduplicationAgeThreshold=356* TestStringDedup57*58* @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication59* -XX:+UseShenandoahGC -XX:StringDeduplicationAgeThreshold=360* TestStringDedup61*62* @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication63* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact -XX:StringDeduplicationAgeThreshold=364* TestStringDedup65*/6667/*68* @test id=iu69* @summary Test Shenandoah string deduplication implementation70* @key randomness71* @requires vm.gc.Shenandoah72* @library /test/lib73* @modules java.base/java.lang:open74* java.management75*76* @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication77* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:StringDeduplicationAgeThreshold=378* TestStringDedup79*80* @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication81* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive -XX:StringDeduplicationAgeThreshold=382* TestStringDedup83*/8485import java.lang.reflect.*;86import java.util.*;87import jdk.test.lib.Utils;8889public class TestStringDedup {90private static Field valueField;9192private static final int UniqueStrings = 20;93// How many GC cycles are needed to complete deduplication.94private static final int GCCount = Integer.getInteger("GCCount", 3);9596static {97try {98valueField = String.class.getDeclaredField("value");99valueField.setAccessible(true);100} catch (Exception e) {101throw new RuntimeException(e);102}103}104105private static Object getValue(String string) {106try {107return valueField.get(string);108} catch (Exception e) {109throw new RuntimeException(e);110}111}112113static class StringAndId {114private String str;115private int id;116117public StringAndId(String str, int id) {118this.str = str;119this.id = id;120}121122public String str() {123return str;124}125126public int id() {127return id;128}129}130131private static void generateStrings(ArrayList<StringAndId> strs, int unique_strs) {132Random rn = Utils.getRandomInstance();133for (int u = 0; u < unique_strs; u++) {134int n = rn.nextInt() % 10;135n = Math.max(n, 2);136for (int index = 0; index < n; index++) {137strs.add(new StringAndId("Unique String " + u, u));138}139}140}141142private static int verifyDedepString(ArrayList<StringAndId> strs) {143HashMap<Object, StringAndId> seen = new HashMap<>();144int total = 0;145int dedup = 0;146147for (StringAndId item : strs) {148total++;149StringAndId existing_item = seen.get(getValue(item.str()));150if (existing_item == null) {151seen.put(getValue(item.str()), item);152} else {153if (item.id() != existing_item.id() ||154!item.str().equals(existing_item.str())) {155System.out.println("StringDedup error:");156System.out.println("String: " + item.str() + " != " + existing_item.str());157throw new RuntimeException("StringDedup Test failed");158} else {159dedup++;160}161}162}163return (total - dedup);164}165166public static void main(String[] args) {167ArrayList<StringAndId> astrs = new ArrayList<>();168generateStrings(astrs, UniqueStrings);169for (int count = 0; count < GCCount; count ++) {170System.gc();171}172173int unique_count = 0;174for (int waitCount = 0; waitCount < 3; waitCount ++) {175// Let concurrent string dedup thread to run176try {177Thread.sleep(1000);178} catch (InterruptedException e) {179}180181// All deduplicated, done.182unique_count = verifyDedepString(astrs);183if ( unique_count == UniqueStrings) {184return;185}186}187188throw new RuntimeException("Expecting " + UniqueStrings + " unique strings, but got " + unique_count);189}190}191192193