Mon, 08 Sep 2014 15:24:10 +0200
8057752: WhiteBox extension support for testing
Summary: Refactored parts of whitebox.cpp to enable registration of whitebox methods defined outside this file.
Reviewed-by: mikael, ctornqvi, jmasa
1.1 --- a/src/share/vm/prims/whitebox.cpp Wed Sep 10 16:06:53 2014 -0700 1.2 +++ b/src/share/vm/prims/whitebox.cpp Mon Sep 08 15:24:10 2014 +0200 1.3 @@ -695,6 +695,12 @@ 1.4 WB_ENTRY(void, WB_FullGC(JNIEnv* env, jobject o)) 1.5 Universe::heap()->collector_policy()->set_should_clear_all_soft_refs(true); 1.6 Universe::heap()->collect(GCCause::_last_ditch_collection); 1.7 +#if INCLUDE_ALL_GCS 1.8 + if (UseG1GC) { 1.9 + // Needs to be cleared explicitly for G1 1.10 + Universe::heap()->collector_policy()->set_should_clear_all_soft_refs(false); 1.11 + } 1.12 +#endif // INCLUDE_ALL_GCS 1.13 WB_END 1.14 1.15 WB_ENTRY(void, WB_YoungGC(JNIEnv* env, jobject o)) 1.16 @@ -838,6 +844,36 @@ 1.17 return ret; 1.18 } 1.19 1.20 +void WhiteBox::register_methods(JNIEnv* env, jclass wbclass, JavaThread* thread, JNINativeMethod* method_array, int method_count) { 1.21 + ResourceMark rm; 1.22 + ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI 1.23 + 1.24 + // one by one registration natives for exception catching 1.25 + jclass no_such_method_error_klass = env->FindClass(vmSymbols::java_lang_NoSuchMethodError()->as_C_string()); 1.26 + CHECK_JNI_EXCEPTION(env); 1.27 + for (int i = 0, n = method_count; i < n; ++i) { 1.28 + // Skip dummy entries 1.29 + if (method_array[i].fnPtr == NULL) continue; 1.30 + if (env->RegisterNatives(wbclass, &method_array[i], 1) != 0) { 1.31 + jthrowable throwable_obj = env->ExceptionOccurred(); 1.32 + if (throwable_obj != NULL) { 1.33 + env->ExceptionClear(); 1.34 + if (env->IsInstanceOf(throwable_obj, no_such_method_error_klass)) { 1.35 + // NoSuchMethodError is thrown when a method can't be found or a method is not native. 1.36 + // Ignoring the exception since it is not preventing use of other WhiteBox methods. 1.37 + tty->print_cr("Warning: 'NoSuchMethodError' on register of sun.hotspot.WhiteBox::%s%s", 1.38 + method_array[i].name, method_array[i].signature); 1.39 + } 1.40 + } else { 1.41 + // Registration failed unexpectedly. 1.42 + tty->print_cr("Warning: unexpected error on register of sun.hotspot.WhiteBox::%s%s. All methods will be unregistered", 1.43 + method_array[i].name, method_array[i].signature); 1.44 + env->UnregisterNatives(wbclass); 1.45 + break; 1.46 + } 1.47 + } 1.48 + } 1.49 +} 1.50 1.51 #define CC (char*) 1.52 1.53 @@ -940,35 +976,9 @@ 1.54 instanceKlassHandle ikh = instanceKlassHandle(JNIHandles::resolve(wbclass)->klass()); 1.55 Handle loader(ikh->class_loader()); 1.56 if (loader.is_null()) { 1.57 - ResourceMark rm; 1.58 - ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI 1.59 - bool result = true; 1.60 - // one by one registration natives for exception catching 1.61 - jclass exceptionKlass = env->FindClass(vmSymbols::java_lang_NoSuchMethodError()->as_C_string()); 1.62 - CHECK_JNI_EXCEPTION(env); 1.63 - for (int i = 0, n = sizeof(methods) / sizeof(methods[0]); i < n; ++i) { 1.64 - if (env->RegisterNatives(wbclass, methods + i, 1) != 0) { 1.65 - result = false; 1.66 - jthrowable throwable_obj = env->ExceptionOccurred(); 1.67 - if (throwable_obj != NULL) { 1.68 - env->ExceptionClear(); 1.69 - if (env->IsInstanceOf(throwable_obj, exceptionKlass)) { 1.70 - // j.l.NoSuchMethodError is thrown when a method can't be found or a method is not native 1.71 - // ignoring the exception 1.72 - tty->print_cr("Warning: 'NoSuchMethodError' on register of sun.hotspot.WhiteBox::%s%s", methods[i].name, methods[i].signature); 1.73 - } 1.74 - } else { 1.75 - // register is failed w/o exception or w/ unexpected exception 1.76 - tty->print_cr("Warning: unexpected error on register of sun.hotspot.WhiteBox::%s%s. All methods will be unregistered", methods[i].name, methods[i].signature); 1.77 - env->UnregisterNatives(wbclass); 1.78 - break; 1.79 - } 1.80 - } 1.81 - } 1.82 - 1.83 - if (result) { 1.84 - WhiteBox::set_used(); 1.85 - } 1.86 + WhiteBox::register_methods(env, wbclass, thread, methods, sizeof(methods) / sizeof(methods[0])); 1.87 + WhiteBox::register_extended(env, wbclass, thread); 1.88 + WhiteBox::set_used(); 1.89 } 1.90 } 1.91 }
2.1 --- a/src/share/vm/prims/whitebox.hpp Wed Sep 10 16:06:53 2014 -0700 2.2 +++ b/src/share/vm/prims/whitebox.hpp Mon Sep 08 15:24:10 2014 +0200 2.3 @@ -29,6 +29,8 @@ 2.4 2.5 #include "memory/allocation.hpp" 2.6 #include "oops/oopsHierarchy.hpp" 2.7 +#include "oops/symbol.hpp" 2.8 +#include "runtime/interfaceSupport.hpp" 2.9 2.10 // Entry macro to transition from JNI to VM state. 2.11 2.12 @@ -64,6 +66,9 @@ 2.13 static bool lookup_bool(const char* field_name, oop object); 2.14 2.15 static int array_bytes_to_length(size_t bytes); 2.16 + static void register_methods(JNIEnv* env, jclass wbclass, JavaThread* thread, 2.17 + JNINativeMethod* method_array, int method_count); 2.18 + static void register_extended(JNIEnv* env, jclass wbclass, JavaThread* thread); 2.19 }; 2.20 2.21
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/share/vm/prims/whitebox_ext.cpp Mon Sep 08 15:24:10 2014 +0200 3.3 @@ -0,0 +1,29 @@ 3.4 +/* 3.5 + * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 + * or visit www.oracle.com if you need additional information or have any 3.24 + * questions. 3.25 + * 3.26 + */ 3.27 + 3.28 +#include "precompiled.hpp" 3.29 + 3.30 +#include "prims/whitebox.hpp" 3.31 + 3.32 +void WhiteBox::register_extended(JNIEnv* env, jclass wbclass, JavaThread* thread) { }
4.1 --- a/test/testlibrary/whitebox/sun/hotspot/WhiteBox.java Wed Sep 10 16:06:53 2014 -0700 4.2 +++ b/test/testlibrary/whitebox/sun/hotspot/WhiteBox.java Mon Sep 08 15:24:10 2014 +0200 4.3 @@ -30,6 +30,7 @@ 4.4 import java.util.function.Function; 4.5 import java.util.stream.Stream; 4.6 import java.security.BasicPermission; 4.7 + 4.8 import sun.hotspot.parser.DiagnosticCommand; 4.9 4.10 public class WhiteBox { 4.11 @@ -168,6 +169,12 @@ 4.12 // CPU features 4.13 public native String getCPUFeatures(); 4.14 4.15 + // Native extensions 4.16 + public native long getHeapUsageForContext(int context); 4.17 + public native long getHeapRegionCountForContext(int context); 4.18 + public native int getContextForObject(Object obj); 4.19 + public native void printRegionInfo(int context); 4.20 + 4.21 // VM flags 4.22 public native void setBooleanVMFlag(String name, boolean value); 4.23 public native void setIntxVMFlag(String name, long value);