Tue, 02 Apr 2013 11:28:33 +0200
8009763: Add WB test for String.intern()
Summary: Add convenience method in StringTable, add WhiteBox method and simple sanity test
Reviewed-by: mgerdin, zgu
Contributed-by: leonid.mesnik@oracle.com
1.1 --- a/src/share/vm/classfile/symbolTable.cpp Thu Mar 28 15:39:52 2013 +0100 1.2 +++ b/src/share/vm/classfile/symbolTable.cpp Tue Apr 02 11:28:33 2013 +0200 1.3 @@ -677,9 +677,14 @@ 1.4 ResourceMark rm; 1.5 int length; 1.6 jchar* chars = symbol->as_unicode(length); 1.7 - unsigned int hashValue = hash_string(chars, length); 1.8 - int index = the_table()->hash_to_index(hashValue); 1.9 - return the_table()->lookup(index, chars, length, hashValue); 1.10 + return lookup(chars, length); 1.11 +} 1.12 + 1.13 + 1.14 +oop StringTable::lookup(jchar* name, int len) { 1.15 + unsigned int hash = hash_string(name, len); 1.16 + int index = the_table()->hash_to_index(hash); 1.17 + return the_table()->lookup(index, name, len, hash); 1.18 } 1.19 1.20
2.1 --- a/src/share/vm/classfile/symbolTable.hpp Thu Mar 28 15:39:52 2013 +0100 2.2 +++ b/src/share/vm/classfile/symbolTable.hpp Tue Apr 02 11:28:33 2013 +0200 2.3 @@ -287,6 +287,7 @@ 2.4 2.5 // Probing 2.6 static oop lookup(Symbol* symbol); 2.7 + static oop lookup(jchar* chars, int length); 2.8 2.9 // Interning 2.10 static oop intern(Symbol* symbol, TRAPS);
3.1 --- a/src/share/vm/prims/whitebox.cpp Thu Mar 28 15:39:52 2013 +0100 3.2 +++ b/src/share/vm/prims/whitebox.cpp Tue Apr 02 11:28:33 2013 +0200 3.3 @@ -254,6 +254,24 @@ 3.4 CompileBroker::queue_size(CompLevel_full_profile) /* C1 */; 3.5 WB_END 3.6 3.7 +WB_ENTRY(jboolean, WB_IsInStringTable(JNIEnv* env, jobject o, jstring javaString)) 3.8 + ResourceMark rm(THREAD); 3.9 + int len; 3.10 + jchar* name = java_lang_String::as_unicode_string(JNIHandles::resolve(javaString), len); 3.11 + oop found_string = StringTable::the_table()->lookup(name, len); 3.12 + if (found_string == NULL) { 3.13 + return false; 3.14 + } 3.15 + return true; 3.16 +WB_END 3.17 + 3.18 + 3.19 +WB_ENTRY(void, WB_FullGC(JNIEnv* env, jobject o)) 3.20 + Universe::heap()->collector_policy()->set_should_clear_all_soft_refs(true); 3.21 + Universe::heap()->collect(GCCause::_last_ditch_collection); 3.22 +WB_END 3.23 + 3.24 + 3.25 //Some convenience methods to deal with objects from java 3.26 int WhiteBox::offset_for_field(const char* field_name, oop object, 3.27 Symbol* signature_symbol) { 3.28 @@ -343,6 +361,8 @@ 3.29 CC"(Ljava/lang/reflect/Method;)I", (void*)&WB_GetMethodCompilationLevel}, 3.30 {CC"getCompileQueuesSize", 3.31 CC"()I", (void*)&WB_GetCompileQueuesSize}, 3.32 + {CC"isInStringTable", CC"(Ljava/lang/String;)Z", (void*)&WB_IsInStringTable }, 3.33 + {CC"fullGC", CC"()V", (void*)&WB_FullGC }, 3.34 }; 3.35 3.36 #undef CC
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/runtime/interned/SanityTest.java Tue Apr 02 11:28:33 2013 +0200 4.3 @@ -0,0 +1,59 @@ 4.4 +/* 4.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.23 + * or visit www.oracle.com if you need additional information or have any 4.24 + * questions. 4.25 + */ 4.26 + 4.27 +/* 4.28 + * @test SanityTest 4.29 + * @summary Sanity check of String.intern() & GC 4.30 + * @library /testlibrary /testlibrary/whitebox 4.31 + * @build SanityTest 4.32 + * @run main ClassFileInstaller sun.hotspot.WhiteBox 4.33 + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI SanityTest 4.34 + */ 4.35 + 4.36 +import java.util.*; 4.37 +import sun.hotspot.WhiteBox; 4.38 + 4.39 + 4.40 +public class SanityTest { 4.41 + public static Object tmp; 4.42 + public static void main(String... args) { 4.43 + 4.44 + WhiteBox wb = WhiteBox.getWhiteBox(); 4.45 + StringBuilder sb = new StringBuilder(); 4.46 + sb.append("1234x"); sb.append("x56789"); 4.47 + String str = sb.toString(); 4.48 + 4.49 + if (wb.isInStringTable(str)) { 4.50 + throw new RuntimeException("String " + str + " is already interned"); 4.51 + } 4.52 + str.intern(); 4.53 + if (!wb.isInStringTable(str)) { 4.54 + throw new RuntimeException("String " + str + " is not interned"); 4.55 + } 4.56 + str = sb.toString(); 4.57 + wb.fullGC(); 4.58 + if (wb.isInStringTable(str)) { 4.59 + throw new RuntimeException("String " + str + " is in StringTable even after GC"); 4.60 + } 4.61 + } 4.62 +}
5.1 --- a/test/testlibrary/whitebox/sun/hotspot/WhiteBox.java Thu Mar 28 15:39:52 2013 +0100 5.2 +++ b/test/testlibrary/whitebox/sun/hotspot/WhiteBox.java Tue Apr 02 11:28:33 2013 +0200 5.3 @@ -94,4 +94,10 @@ 5.4 public native int getMethodCompilationLevel(Method method); 5.5 public native boolean setDontInlineMethod(Method method, boolean value); 5.6 public native int getCompileQueuesSize(); 5.7 + 5.8 + //Intered strings 5.9 + public native boolean isInStringTable(String str); 5.10 + 5.11 + // force Full GC 5.12 + public native void fullGC(); 5.13 }