Fri, 10 Jul 2009 11:10:00 -0700
6857194: Add hotspot perf counters to aid class loading performance measurement
Summary: Add new jvmstat counters to measure detailed class loading time
Reviewed-by: acorn, kamg
1 /*
2 * Copyright 1997-2009 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 # include "incls/_precompiled.incl"
26 # include "incls/_nativeLookup.cpp.incl"
29 static void mangle_name_on(outputStream* st, symbolOop name, int begin, int end) {
30 char* bytes = (char*)name->bytes() + begin;
31 char* end_bytes = (char*)name->bytes() + end;
32 while (bytes < end_bytes) {
33 jchar c;
34 bytes = UTF8::next(bytes, &c);
35 if (c <= 0x7f && isalnum(c)) {
36 st->put((char) c);
37 } else {
38 if (c == '_') st->print("_1");
39 else if (c == '/') st->print("_");
40 else if (c == ';') st->print("_2");
41 else if (c == '[') st->print("_3");
42 else st->print("_%.5x", c);
43 }
44 }
45 }
48 static void mangle_name_on(outputStream* st, symbolOop name) {
49 mangle_name_on(st, name, 0, name->utf8_length());
50 }
53 char* NativeLookup::pure_jni_name(methodHandle method) {
54 stringStream st;
55 // Prefix
56 st.print("Java_");
57 // Klass name
58 mangle_name_on(&st, method->klass_name());
59 st.print("_");
60 // Method name
61 mangle_name_on(&st, method->name());
62 return st.as_string();
63 }
66 char* NativeLookup::long_jni_name(methodHandle method) {
67 // Signature ignore the wrapping parenteses and the trailing return type
68 stringStream st;
69 symbolOop signature = method->signature();
70 st.print("__");
71 // find ')'
72 int end;
73 for (end = 0; end < signature->utf8_length() && signature->byte_at(end) != ')'; end++);
74 // skip first '('
75 mangle_name_on(&st, signature, 1, end);
76 return st.as_string();
77 }
79 extern "C" {
80 void JNICALL JVM_RegisterUnsafeMethods(JNIEnv *env, jclass unsafecls);
81 void JNICALL JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass unsafecls);
82 void JNICALL JVM_RegisterPerfMethods(JNIEnv *env, jclass perfclass);
83 }
85 static address lookup_special_native(char* jni_name) {
86 // NB: To ignore the jni prefix and jni postfix strstr is used matching.
87 if (!JDK_Version::is_gte_jdk14x_version()) {
88 // These functions only exist for compatibility with 1.3.1 and earlier
89 // Intercept ObjectOutputStream getPrimitiveFieldValues for faster serialization
90 if (strstr(jni_name, "Java_java_io_ObjectOutputStream_getPrimitiveFieldValues") != NULL) {
91 return CAST_FROM_FN_PTR(address, JVM_GetPrimitiveFieldValues);
92 }
93 // Intercept ObjectInputStream setPrimitiveFieldValues for faster serialization
94 if (strstr(jni_name, "Java_java_io_ObjectInputStream_setPrimitiveFieldValues") != NULL) {
95 return CAST_FROM_FN_PTR(address, JVM_SetPrimitiveFieldValues);
96 }
97 }
98 if (strstr(jni_name, "Java_sun_misc_Unsafe_registerNatives") != NULL) {
99 return CAST_FROM_FN_PTR(address, JVM_RegisterUnsafeMethods);
100 }
101 if (strstr(jni_name, "Java_sun_dyn_MethodHandleNatives_registerNatives") != NULL) {
102 return CAST_FROM_FN_PTR(address, JVM_RegisterMethodHandleMethods);
103 }
104 if (strstr(jni_name, "Java_sun_misc_Perf_registerNatives") != NULL) {
105 return CAST_FROM_FN_PTR(address, JVM_RegisterPerfMethods);
106 }
108 return NULL;
109 }
111 address NativeLookup::lookup_style(methodHandle method, char* pure_name, const char* long_name, int args_size, bool os_style, bool& in_base_library, TRAPS) {
112 address entry;
113 // Compute complete JNI name for style
114 stringStream st;
115 if (os_style) os::print_jni_name_prefix_on(&st, args_size);
116 st.print_raw(pure_name);
117 st.print_raw(long_name);
118 if (os_style) os::print_jni_name_suffix_on(&st, args_size);
119 char* jni_name = st.as_string();
121 // If the loader is null we have a system class, so we attempt a lookup in
122 // the native Java library. This takes care of any bootstrapping problems.
123 // Note: It is critical for bootstrapping that Java_java_lang_ClassLoader_00024NativeLibrary_find
124 // gets found the first time around - otherwise an infinite loop can occure. This is
125 // another VM/library dependency
126 Handle loader(THREAD,
127 instanceKlass::cast(method->method_holder())->class_loader());
128 if (loader.is_null()) {
129 entry = lookup_special_native(jni_name);
130 if (entry == NULL) {
131 entry = (address) hpi::dll_lookup(os::native_java_library(), jni_name);
132 }
133 if (entry != NULL) {
134 in_base_library = true;
135 return entry;
136 }
137 }
139 // Otherwise call static method findNative in ClassLoader
140 KlassHandle klass (THREAD, SystemDictionary::classloader_klass());
141 Handle name_arg = java_lang_String::create_from_str(jni_name, CHECK_NULL);
143 JavaValue result(T_LONG);
144 JavaCalls::call_static(&result,
145 klass,
146 vmSymbolHandles::findNative_name(),
147 vmSymbolHandles::classloader_string_long_signature(),
148 // Arguments
149 loader,
150 name_arg,
151 CHECK_NULL);
152 entry = (address) (intptr_t) result.get_jlong();
154 if (entry == NULL) {
155 // findNative didn't find it, if there are any agent libraries look in them
156 AgentLibrary* agent;
157 for (agent = Arguments::agents(); agent != NULL; agent = agent->next()) {
158 entry = (address) hpi::dll_lookup(agent->os_lib(), jni_name);
159 if (entry != NULL) {
160 return entry;
161 }
162 }
163 }
165 return entry;
166 }
169 // Check all the formats of native implementation name to see if there is one
170 // for the specified method.
171 address NativeLookup::lookup_entry(methodHandle method, bool& in_base_library, TRAPS) {
172 address entry = NULL;
173 in_base_library = false;
174 // Compute pure name
175 char* pure_name = pure_jni_name(method);
177 // Compute argument size
178 int args_size = 1 // JNIEnv
179 + (method->is_static() ? 1 : 0) // class for static methods
180 + method->size_of_parameters(); // actual parameters
183 // 1) Try JNI short style
184 entry = lookup_style(method, pure_name, "", args_size, true, in_base_library, CHECK_NULL);
185 if (entry != NULL) return entry;
187 // Compute long name
188 char* long_name = long_jni_name(method);
190 // 2) Try JNI long style
191 entry = lookup_style(method, pure_name, long_name, args_size, true, in_base_library, CHECK_NULL);
192 if (entry != NULL) return entry;
194 // 3) Try JNI short style without os prefix/suffix
195 entry = lookup_style(method, pure_name, "", args_size, false, in_base_library, CHECK_NULL);
196 if (entry != NULL) return entry;
198 // 4) Try JNI long style without os prefix/suffix
199 entry = lookup_style(method, pure_name, long_name, args_size, false, in_base_library, CHECK_NULL);
201 return entry; // NULL indicates not found
202 }
204 // Check if there are any JVM TI prefixes which have been applied to the native method name.
205 // If any are found, remove them before attemping the look up of the
206 // native implementation again.
207 // See SetNativeMethodPrefix in the JVM TI Spec for more details.
208 address NativeLookup::lookup_entry_prefixed(methodHandle method, bool& in_base_library, TRAPS) {
209 ResourceMark rm(THREAD);
211 int prefix_count;
212 char** prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count);
213 char* in_name = method->name()->as_C_string();
214 char* wrapper_name = in_name;
215 // last applied prefix will be first -- go backwards
216 for (int i = prefix_count-1; i >= 0; i--) {
217 char* prefix = prefixes[i];
218 size_t prefix_len = strlen(prefix);
219 if (strncmp(prefix, wrapper_name, prefix_len) == 0) {
220 // has this prefix remove it
221 wrapper_name += prefix_len;
222 }
223 }
224 if (wrapper_name != in_name) {
225 // we have a name for a wrapping method
226 int wrapper_name_len = (int)strlen(wrapper_name);
227 symbolHandle wrapper_symbol(THREAD, SymbolTable::probe(wrapper_name, wrapper_name_len));
228 if (!wrapper_symbol.is_null()) {
229 KlassHandle kh(method->method_holder());
230 methodOop wrapper_method = Klass::cast(kh())->lookup_method(wrapper_symbol(),
231 method->signature());
232 if (wrapper_method != NULL && !wrapper_method->is_native()) {
233 // we found a wrapper method, use its native entry
234 method->set_is_prefixed_native();
235 return lookup_entry(wrapper_method, in_base_library, THREAD);
236 }
237 }
238 }
239 return NULL;
240 }
242 address NativeLookup::lookup_base(methodHandle method, bool& in_base_library, TRAPS) {
243 address entry = NULL;
244 ResourceMark rm(THREAD);
246 entry = lookup_entry(method, in_base_library, THREAD);
247 if (entry != NULL) return entry;
249 // standard native method resolution has failed. Check if there are any
250 // JVM TI prefixes which have been applied to the native method name.
251 entry = lookup_entry_prefixed(method, in_base_library, THREAD);
252 if (entry != NULL) return entry;
254 // Native function not found, throw UnsatisfiedLinkError
255 THROW_MSG_0(vmSymbols::java_lang_UnsatisfiedLinkError(),
256 method->name_and_sig_as_C_string());
257 }
260 address NativeLookup::lookup(methodHandle method, bool& in_base_library, TRAPS) {
261 if (!method->has_native_function()) {
262 address entry = lookup_base(method, in_base_library, CHECK_NULL);
263 method->set_native_function(entry,
264 methodOopDesc::native_bind_event_is_interesting);
265 // -verbose:jni printing
266 if (PrintJNIResolving) {
267 ResourceMark rm(THREAD);
268 tty->print_cr("[Dynamic-linking native method %s.%s ... JNI]",
269 Klass::cast(method->method_holder())->external_name(),
270 method->name()->as_C_string());
271 }
272 }
273 return method->native_function();
274 }
276 address NativeLookup::base_library_lookup(const char* class_name, const char* method_name, const char* signature) {
277 EXCEPTION_MARK;
278 bool in_base_library = true; // SharedRuntime inits some math methods.
279 symbolHandle c_name = oopFactory::new_symbol_handle(class_name, CATCH);
280 symbolHandle m_name = oopFactory::new_symbol_handle(method_name, CATCH);
281 symbolHandle s_name = oopFactory::new_symbol_handle(signature, CATCH);
283 // Find the class
284 klassOop k = SystemDictionary::resolve_or_fail(c_name, true, CATCH);
285 instanceKlassHandle klass (THREAD, k);
287 // Find method and invoke standard lookup
288 methodHandle method (THREAD,
289 klass->uncached_lookup_method(m_name(), s_name()));
290 address result = lookup(method, in_base_library, CATCH);
291 assert(in_base_library, "must be in basic library");
292 guarantee(result != NULL, "must be non NULL");
293 return result;
294 }