Mercurial > hg > graal-jvmci-8
annotate src/share/vm/code/nmethod.cpp @ 7112:60bef2672469
changed System.err output to TTY to avoid that DaCapo benchmarks fail digest validation
author | Christian Haeubl <haeubl@ssw.jku.at> |
---|---|
date | Fri, 30 Nov 2012 10:40:07 +0100 |
parents | 97d0eae99568 |
children | 1baf7f1e3f23 |
rev | line source |
---|---|
7066 | 1 /* |
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 * | |
23 */ | |
24 | |
25 #include "precompiled.hpp" | |
26 #include "code/codeCache.hpp" | |
27 #include "code/compiledIC.hpp" | |
28 #include "code/dependencies.hpp" | |
29 #include "code/nmethod.hpp" | |
30 #include "code/scopeDesc.hpp" | |
31 #include "compiler/abstractCompiler.hpp" | |
32 #include "compiler/compileBroker.hpp" | |
33 #include "compiler/compileLog.hpp" | |
34 #include "compiler/compilerOracle.hpp" | |
35 #include "compiler/disassembler.hpp" | |
36 #include "interpreter/bytecode.hpp" | |
37 #include "oops/methodData.hpp" | |
38 #include "prims/jvmtiRedefineClassesTrace.hpp" | |
39 #include "prims/jvmtiImpl.hpp" | |
40 #include "runtime/sharedRuntime.hpp" | |
41 #include "runtime/sweeper.hpp" | |
42 #include "utilities/dtrace.hpp" | |
43 #include "utilities/events.hpp" | |
44 #include "utilities/xmlstream.hpp" | |
45 #include "utilities/debug.hpp" | |
46 #include "utilities/machineCodePrinter.hpp" | |
47 #ifdef SHARK | |
48 #include "shark/sharkCompiler.hpp" | |
49 #endif | |
50 #ifdef GRAAL | |
51 #include "graal/graalJavaAccess.hpp" | |
52 #endif | |
53 | |
54 #ifdef DTRACE_ENABLED | |
55 | |
56 // Only bother with this argument setup if dtrace is available | |
57 | |
58 #ifndef USDT2 | |
59 HS_DTRACE_PROBE_DECL8(hotspot, compiled__method__load, | |
60 const char*, int, const char*, int, const char*, int, void*, size_t); | |
61 | |
62 HS_DTRACE_PROBE_DECL6(hotspot, compiled__method__unload, | |
63 char*, int, char*, int, char*, int); | |
64 | |
65 #define DTRACE_METHOD_UNLOAD_PROBE(method) \ | |
66 { \ | |
67 Method* m = (method); \ | |
68 if (m != NULL) { \ | |
69 Symbol* klass_name = m->klass_name(); \ | |
70 Symbol* name = m->name(); \ | |
71 Symbol* signature = m->signature(); \ | |
72 HS_DTRACE_PROBE6(hotspot, compiled__method__unload, \ | |
73 klass_name->bytes(), klass_name->utf8_length(), \ | |
74 name->bytes(), name->utf8_length(), \ | |
75 signature->bytes(), signature->utf8_length()); \ | |
76 } \ | |
77 } | |
78 #else /* USDT2 */ | |
79 #define DTRACE_METHOD_UNLOAD_PROBE(method) \ | |
80 { \ | |
81 Method* m = (method); \ | |
82 if (m != NULL) { \ | |
83 Symbol* klass_name = m->klass_name(); \ | |
84 Symbol* name = m->name(); \ | |
85 Symbol* signature = m->signature(); \ | |
86 HOTSPOT_COMPILED_METHOD_UNLOAD( \ | |
87 (char *) klass_name->bytes(), klass_name->utf8_length(), \ | |
88 (char *) name->bytes(), name->utf8_length(), \ | |
89 (char *) signature->bytes(), signature->utf8_length()); \ | |
90 } \ | |
91 } | |
92 #endif /* USDT2 */ | |
93 | |
94 #else // ndef DTRACE_ENABLED | |
95 | |
96 #define DTRACE_METHOD_UNLOAD_PROBE(method) | |
97 | |
98 #endif | |
99 | |
100 bool nmethod::is_compiled_by_c1() const { | |
101 if (compiler() == NULL || method() == NULL) return false; // can happen during debug printing | |
102 if (is_native_method()) return false; | |
103 return compiler()->is_c1(); | |
104 } | |
105 bool nmethod::is_compiled_by_c2() const { | |
106 if (compiler() == NULL || method() == NULL) return false; // can happen during debug printing | |
107 if (is_native_method()) return false; | |
108 return compiler()->is_c2(); | |
109 } | |
110 bool nmethod::is_compiled_by_shark() const { | |
111 if (is_native_method()) return false; | |
112 assert(compiler() != NULL, "must be"); | |
113 return compiler()->is_shark(); | |
114 } | |
115 | |
116 | |
117 | |
118 //--------------------------------------------------------------------------------- | |
119 // NMethod statistics | |
120 // They are printed under various flags, including: | |
121 // PrintC1Statistics, PrintOptoStatistics, LogVMOutput, and LogCompilation. | |
122 // (In the latter two cases, they like other stats are printed to the log only.) | |
123 | |
124 // These variables are put into one block to reduce relocations | |
125 // and make it simpler to print from the debugger. | |
126 static | |
127 struct nmethod_stats_struct { | |
128 int nmethod_count; | |
129 int total_size; | |
130 int relocation_size; | |
131 int consts_size; | |
132 int insts_size; | |
133 int stub_size; | |
134 int scopes_data_size; | |
135 int scopes_pcs_size; | |
136 int dependencies_size; | |
137 int handler_table_size; | |
138 int nul_chk_table_size; | |
139 int oops_size; | |
140 | |
141 void note_nmethod(nmethod* nm) { | |
142 nmethod_count += 1; | |
143 total_size += nm->size(); | |
144 relocation_size += nm->relocation_size(); | |
145 consts_size += nm->consts_size(); | |
146 insts_size += nm->insts_size(); | |
147 stub_size += nm->stub_size(); | |
148 oops_size += nm->oops_size(); | |
149 scopes_data_size += nm->scopes_data_size(); | |
150 scopes_pcs_size += nm->scopes_pcs_size(); | |
151 dependencies_size += nm->dependencies_size(); | |
152 handler_table_size += nm->handler_table_size(); | |
153 nul_chk_table_size += nm->nul_chk_table_size(); | |
154 } | |
155 void print_nmethod_stats() { | |
156 if (nmethod_count == 0) return; | |
157 tty->print_cr("Statistics for %d bytecoded nmethods:", nmethod_count); | |
158 if (total_size != 0) tty->print_cr(" total in heap = %d", total_size); | |
159 if (relocation_size != 0) tty->print_cr(" relocation = %d", relocation_size); | |
160 if (consts_size != 0) tty->print_cr(" constants = %d", consts_size); | |
161 if (insts_size != 0) tty->print_cr(" main code = %d", insts_size); | |
162 if (stub_size != 0) tty->print_cr(" stub code = %d", stub_size); | |
163 if (oops_size != 0) tty->print_cr(" oops = %d", oops_size); | |
164 if (scopes_data_size != 0) tty->print_cr(" scopes data = %d", scopes_data_size); | |
165 if (scopes_pcs_size != 0) tty->print_cr(" scopes pcs = %d", scopes_pcs_size); | |
166 if (dependencies_size != 0) tty->print_cr(" dependencies = %d", dependencies_size); | |
167 if (handler_table_size != 0) tty->print_cr(" handler table = %d", handler_table_size); | |
168 if (nul_chk_table_size != 0) tty->print_cr(" nul chk table = %d", nul_chk_table_size); | |
169 } | |
170 | |
171 int native_nmethod_count; | |
172 int native_total_size; | |
173 int native_relocation_size; | |
174 int native_insts_size; | |
175 int native_oops_size; | |
176 void note_native_nmethod(nmethod* nm) { | |
177 native_nmethod_count += 1; | |
178 native_total_size += nm->size(); | |
179 native_relocation_size += nm->relocation_size(); | |
180 native_insts_size += nm->insts_size(); | |
181 native_oops_size += nm->oops_size(); | |
182 } | |
183 void print_native_nmethod_stats() { | |
184 if (native_nmethod_count == 0) return; | |
185 tty->print_cr("Statistics for %d native nmethods:", native_nmethod_count); | |
186 if (native_total_size != 0) tty->print_cr(" N. total size = %d", native_total_size); | |
187 if (native_relocation_size != 0) tty->print_cr(" N. relocation = %d", native_relocation_size); | |
188 if (native_insts_size != 0) tty->print_cr(" N. main code = %d", native_insts_size); | |
189 if (native_oops_size != 0) tty->print_cr(" N. oops = %d", native_oops_size); | |
190 } | |
191 | |
192 int pc_desc_resets; // number of resets (= number of caches) | |
193 int pc_desc_queries; // queries to nmethod::find_pc_desc | |
194 int pc_desc_approx; // number of those which have approximate true | |
195 int pc_desc_repeats; // number of _pc_descs[0] hits | |
196 int pc_desc_hits; // number of LRU cache hits | |
197 int pc_desc_tests; // total number of PcDesc examinations | |
198 int pc_desc_searches; // total number of quasi-binary search steps | |
199 int pc_desc_adds; // number of LUR cache insertions | |
200 | |
201 void print_pc_stats() { | |
202 tty->print_cr("PcDesc Statistics: %d queries, %.2f comparisons per query", | |
203 pc_desc_queries, | |
204 (double)(pc_desc_tests + pc_desc_searches) | |
205 / pc_desc_queries); | |
206 tty->print_cr(" caches=%d queries=%d/%d, hits=%d+%d, tests=%d+%d, adds=%d", | |
207 pc_desc_resets, | |
208 pc_desc_queries, pc_desc_approx, | |
209 pc_desc_repeats, pc_desc_hits, | |
210 pc_desc_tests, pc_desc_searches, pc_desc_adds); | |
211 } | |
212 } nmethod_stats; | |
213 | |
214 | |
215 //--------------------------------------------------------------------------------- | |
216 | |
217 | |
218 ExceptionCache::ExceptionCache(Handle exception, address pc, address handler) { | |
219 assert(pc != NULL, "Must be non null"); | |
220 assert(exception.not_null(), "Must be non null"); | |
221 assert(handler != NULL, "Must be non null"); | |
222 | |
223 _count = 0; | |
224 _exception_type = exception->klass(); | |
225 _next = NULL; | |
226 | |
227 add_address_and_handler(pc,handler); | |
228 } | |
229 | |
230 | |
231 address ExceptionCache::match(Handle exception, address pc) { | |
232 assert(pc != NULL,"Must be non null"); | |
233 assert(exception.not_null(),"Must be non null"); | |
234 if (exception->klass() == exception_type()) { | |
235 return (test_address(pc)); | |
236 } | |
237 | |
238 return NULL; | |
239 } | |
240 | |
241 | |
242 bool ExceptionCache::match_exception_with_space(Handle exception) { | |
243 assert(exception.not_null(),"Must be non null"); | |
244 if (exception->klass() == exception_type() && count() < cache_size) { | |
245 return true; | |
246 } | |
247 return false; | |
248 } | |
249 | |
250 | |
251 address ExceptionCache::test_address(address addr) { | |
252 for (int i=0; i<count(); i++) { | |
253 if (pc_at(i) == addr) { | |
254 return handler_at(i); | |
255 } | |
256 } | |
257 return NULL; | |
258 } | |
259 | |
260 | |
261 bool ExceptionCache::add_address_and_handler(address addr, address handler) { | |
262 if (test_address(addr) == handler) return true; | |
263 if (count() < cache_size) { | |
264 set_pc_at(count(),addr); | |
265 set_handler_at(count(), handler); | |
266 increment_count(); | |
267 return true; | |
268 } | |
269 return false; | |
270 } | |
271 | |
272 | |
273 // private method for handling exception cache | |
274 // These methods are private, and used to manipulate the exception cache | |
275 // directly. | |
276 ExceptionCache* nmethod::exception_cache_entry_for_exception(Handle exception) { | |
277 ExceptionCache* ec = exception_cache(); | |
278 while (ec != NULL) { | |
279 if (ec->match_exception_with_space(exception)) { | |
280 return ec; | |
281 } | |
282 ec = ec->next(); | |
283 } | |
284 return NULL; | |
285 } | |
286 | |
287 | |
288 //----------------------------------------------------------------------------- | |
289 | |
290 | |
291 // Helper used by both find_pc_desc methods. | |
292 static inline bool match_desc(PcDesc* pc, int pc_offset, bool approximate) { | |
293 NOT_PRODUCT(++nmethod_stats.pc_desc_tests); | |
294 if (!approximate) | |
295 return pc->pc_offset() == pc_offset; | |
296 else | |
297 return (pc-1)->pc_offset() < pc_offset && pc_offset <= pc->pc_offset(); | |
298 } | |
299 | |
300 void PcDescCache::reset_to(PcDesc* initial_pc_desc) { | |
301 if (initial_pc_desc == NULL) { | |
302 _pc_descs[0] = NULL; // native method; no PcDescs at all | |
303 return; | |
304 } | |
305 NOT_PRODUCT(++nmethod_stats.pc_desc_resets); | |
306 // reset the cache by filling it with benign (non-null) values | |
307 assert(initial_pc_desc->pc_offset() < 0, "must be sentinel"); | |
308 for (int i = 0; i < cache_size; i++) | |
309 _pc_descs[i] = initial_pc_desc; | |
310 } | |
311 | |
312 PcDesc* PcDescCache::find_pc_desc(int pc_offset, bool approximate) { | |
313 NOT_PRODUCT(++nmethod_stats.pc_desc_queries); | |
314 NOT_PRODUCT(if (approximate) ++nmethod_stats.pc_desc_approx); | |
315 | |
316 // Note: one might think that caching the most recently | |
317 // read value separately would be a win, but one would be | |
318 // wrong. When many threads are updating it, the cache | |
319 // line it's in would bounce between caches, negating | |
320 // any benefit. | |
321 | |
322 // In order to prevent race conditions do not load cache elements | |
323 // repeatedly, but use a local copy: | |
324 PcDesc* res; | |
325 | |
326 // Step one: Check the most recently added value. | |
327 res = _pc_descs[0]; | |
328 if (res == NULL) return NULL; // native method; no PcDescs at all | |
329 if (match_desc(res, pc_offset, approximate)) { | |
330 NOT_PRODUCT(++nmethod_stats.pc_desc_repeats); | |
331 return res; | |
332 } | |
333 | |
334 // Step two: Check the rest of the LRU cache. | |
335 for (int i = 1; i < cache_size; ++i) { | |
336 res = _pc_descs[i]; | |
337 if (res->pc_offset() < 0) break; // optimization: skip empty cache | |
338 if (match_desc(res, pc_offset, approximate)) { | |
339 NOT_PRODUCT(++nmethod_stats.pc_desc_hits); | |
340 return res; | |
341 } | |
342 } | |
343 | |
344 // Report failure. | |
345 return NULL; | |
346 } | |
347 | |
348 void PcDescCache::add_pc_desc(PcDesc* pc_desc) { | |
349 NOT_PRODUCT(++nmethod_stats.pc_desc_adds); | |
350 // Update the LRU cache by shifting pc_desc forward. | |
351 for (int i = 0; i < cache_size; i++) { | |
352 PcDesc* next = _pc_descs[i]; | |
353 _pc_descs[i] = pc_desc; | |
354 pc_desc = next; | |
355 } | |
356 } | |
357 | |
358 // adjust pcs_size so that it is a multiple of both oopSize and | |
359 // sizeof(PcDesc) (assumes that if sizeof(PcDesc) is not a multiple | |
360 // of oopSize, then 2*sizeof(PcDesc) is) | |
361 static int adjust_pcs_size(int pcs_size) { | |
362 int nsize = round_to(pcs_size, oopSize); | |
363 if ((nsize % sizeof(PcDesc)) != 0) { | |
364 nsize = pcs_size + sizeof(PcDesc); | |
365 } | |
366 assert((nsize % oopSize) == 0, "correct alignment"); | |
367 return nsize; | |
368 } | |
369 | |
370 //----------------------------------------------------------------------------- | |
371 | |
372 | |
373 void nmethod::add_exception_cache_entry(ExceptionCache* new_entry) { | |
374 assert(ExceptionCache_lock->owned_by_self(),"Must hold the ExceptionCache_lock"); | |
375 assert(new_entry != NULL,"Must be non null"); | |
376 assert(new_entry->next() == NULL, "Must be null"); | |
377 | |
378 if (exception_cache() != NULL) { | |
379 new_entry->set_next(exception_cache()); | |
380 } | |
381 set_exception_cache(new_entry); | |
382 } | |
383 | |
384 void nmethod::remove_from_exception_cache(ExceptionCache* ec) { | |
385 ExceptionCache* prev = NULL; | |
386 ExceptionCache* curr = exception_cache(); | |
387 assert(curr != NULL, "nothing to remove"); | |
388 // find the previous and next entry of ec | |
389 while (curr != ec) { | |
390 prev = curr; | |
391 curr = curr->next(); | |
392 assert(curr != NULL, "ExceptionCache not found"); | |
393 } | |
394 // now: curr == ec | |
395 ExceptionCache* next = curr->next(); | |
396 if (prev == NULL) { | |
397 set_exception_cache(next); | |
398 } else { | |
399 prev->set_next(next); | |
400 } | |
401 delete curr; | |
402 } | |
403 | |
404 | |
405 // public method for accessing the exception cache | |
406 // These are the public access methods. | |
407 address nmethod::handler_for_exception_and_pc(Handle exception, address pc) { | |
408 // We never grab a lock to read the exception cache, so we may | |
409 // have false negatives. This is okay, as it can only happen during | |
410 // the first few exception lookups for a given nmethod. | |
411 ExceptionCache* ec = exception_cache(); | |
412 while (ec != NULL) { | |
413 address ret_val; | |
414 if ((ret_val = ec->match(exception,pc)) != NULL) { | |
415 return ret_val; | |
416 } | |
417 ec = ec->next(); | |
418 } | |
419 return NULL; | |
420 } | |
421 | |
422 | |
423 void nmethod::add_handler_for_exception_and_pc(Handle exception, address pc, address handler) { | |
424 // There are potential race conditions during exception cache updates, so we | |
425 // must own the ExceptionCache_lock before doing ANY modifications. Because | |
426 // we don't lock during reads, it is possible to have several threads attempt | |
427 // to update the cache with the same data. We need to check for already inserted | |
428 // copies of the current data before adding it. | |
429 | |
430 MutexLocker ml(ExceptionCache_lock); | |
431 ExceptionCache* target_entry = exception_cache_entry_for_exception(exception); | |
432 | |
433 if (target_entry == NULL || !target_entry->add_address_and_handler(pc,handler)) { | |
434 target_entry = new ExceptionCache(exception,pc,handler); | |
435 add_exception_cache_entry(target_entry); | |
436 } | |
437 } | |
438 | |
439 | |
440 //-------------end of code for ExceptionCache-------------- | |
441 | |
442 | |
443 int nmethod::total_size() const { | |
444 return | |
445 consts_size() + | |
446 insts_size() + | |
447 stub_size() + | |
448 scopes_data_size() + | |
449 scopes_pcs_size() + | |
450 handler_table_size() + | |
451 nul_chk_table_size(); | |
452 } | |
453 | |
454 const char* nmethod::compile_kind() const { | |
455 if (is_osr_method()) return "osr"; | |
456 if (method() != NULL && is_native_method()) return "c2n"; | |
457 return NULL; | |
458 } | |
459 | |
460 // Fill in default values for various flag fields | |
461 void nmethod::init_defaults() { | |
462 _state = alive; | |
463 _marked_for_reclamation = 0; | |
464 _has_flushed_dependencies = 0; | |
465 _speculatively_disconnected = 0; | |
466 _has_unsafe_access = 0; | |
467 _has_method_handle_invokes = 0; | |
468 _lazy_critical_native = 0; | |
469 _has_wide_vectors = 0; | |
470 _marked_for_deoptimization = 0; | |
471 _lock_count = 0; | |
472 _stack_traversal_mark = 0; | |
473 _unload_reported = false; // jvmti state | |
474 | |
475 #ifdef ASSERT | |
476 _oops_are_stale = false; | |
477 #endif | |
478 | |
479 _oops_do_mark_link = NULL; | |
480 _jmethod_id = NULL; | |
481 _osr_link = NULL; | |
482 _scavenge_root_link = NULL; | |
483 _scavenge_root_state = 0; | |
484 _saved_nmethod_link = NULL; | |
485 _compiler = NULL; | |
486 #ifdef GRAAL | |
487 _graal_installed_code = NULL; | |
488 #endif | |
489 #ifdef HAVE_DTRACE_H | |
490 _trap_offset = 0; | |
491 #endif // def HAVE_DTRACE_H | |
492 } | |
493 | |
494 | |
495 nmethod* nmethod::new_native_nmethod(methodHandle method, | |
496 int compile_id, | |
497 CodeBuffer *code_buffer, | |
498 int vep_offset, | |
499 int frame_complete, | |
500 int frame_size, | |
501 ByteSize basic_lock_owner_sp_offset, | |
502 ByteSize basic_lock_sp_offset, | |
503 OopMapSet* oop_maps) { | |
504 code_buffer->finalize_oop_references(method); | |
505 // create nmethod | |
506 nmethod* nm = NULL; | |
507 { | |
508 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); | |
509 int native_nmethod_size = allocation_size(code_buffer, sizeof(nmethod)); | |
510 CodeOffsets offsets; | |
511 offsets.set_value(CodeOffsets::Verified_Entry, vep_offset); | |
512 offsets.set_value(CodeOffsets::Frame_Complete, frame_complete); | |
513 nm = new (native_nmethod_size) | |
514 nmethod(method(), native_nmethod_size, compile_id, &offsets, | |
515 code_buffer, frame_size, | |
516 basic_lock_owner_sp_offset, basic_lock_sp_offset, | |
517 oop_maps); | |
518 if (nm != NULL) nmethod_stats.note_native_nmethod(nm); | |
519 if (PrintAssembly && nm != NULL) | |
520 Disassembler::decode(nm); | |
521 | |
7062
8c5333c80cfd
increased CompileThreshold to increase reliability of profiling information
Christian Haeubl <haeubl@ssw.jku.at>
parents:
6275
diff
changeset
|
522 if (PrintMachineCodeToFile) { |
8c5333c80cfd
increased CompileThreshold to increase reliability of profiling information
Christian Haeubl <haeubl@ssw.jku.at>
parents:
6275
diff
changeset
|
523 MachineCodePrinter::print(nm); |
7066 | 524 } |
525 } | |
526 // verify nmethod | |
527 debug_only(if (nm) nm->verify();) // might block | |
528 | |
529 if (nm != NULL) { | |
530 nm->log_new_nmethod(); | |
531 } | |
532 | |
533 return nm; | |
534 } | |
535 | |
536 #ifdef HAVE_DTRACE_H | |
537 nmethod* nmethod::new_dtrace_nmethod(methodHandle method, | |
538 CodeBuffer *code_buffer, | |
539 int vep_offset, | |
540 int trap_offset, | |
541 int frame_complete, | |
542 int frame_size) { | |
543 code_buffer->finalize_oop_references(method); | |
544 // create nmethod | |
545 nmethod* nm = NULL; | |
546 { | |
547 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); | |
548 int nmethod_size = allocation_size(code_buffer, sizeof(nmethod)); | |
549 CodeOffsets offsets; | |
550 offsets.set_value(CodeOffsets::Verified_Entry, vep_offset); | |
551 offsets.set_value(CodeOffsets::Dtrace_trap, trap_offset); | |
552 offsets.set_value(CodeOffsets::Frame_Complete, frame_complete); | |
553 | |
554 nm = new (nmethod_size) nmethod(method(), nmethod_size, &offsets, code_buffer, frame_size); | |
555 | |
556 if (nm != NULL) nmethod_stats.note_nmethod(nm); | |
557 if (PrintAssembly && nm != NULL) | |
558 Disassembler::decode(nm); | |
559 } | |
560 // verify nmethod | |
561 debug_only(if (nm) nm->verify();) // might block | |
562 | |
563 if (nm != NULL) { | |
564 nm->log_new_nmethod(); | |
565 } | |
566 | |
567 return nm; | |
568 } | |
569 | |
570 #endif // def HAVE_DTRACE_H | |
571 | |
572 nmethod* nmethod::new_nmethod(methodHandle method, | |
573 int compile_id, | |
574 int entry_bci, | |
575 CodeOffsets* offsets, | |
576 int orig_pc_offset, | |
577 DebugInformationRecorder* debug_info, | |
578 Dependencies* dependencies, | |
579 CodeBuffer* code_buffer, int frame_size, | |
580 OopMapSet* oop_maps, | |
581 ExceptionHandlerTable* handler_table, | |
582 ImplicitExceptionTable* nul_chk_table, | |
583 AbstractCompiler* compiler, | |
584 int comp_level | |
585 #ifdef GRAAL | |
586 , Handle installed_code | |
587 #endif | |
588 ) | |
589 { | |
590 assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR"); | |
591 code_buffer->finalize_oop_references(method); | |
592 // create nmethod | |
593 nmethod* nm = NULL; | |
594 { MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); | |
595 int nmethod_size = | |
596 allocation_size(code_buffer, sizeof(nmethod)) | |
597 + adjust_pcs_size(debug_info->pcs_size()) | |
598 + round_to(dependencies->size_in_bytes() , oopSize) | |
599 + round_to(handler_table->size_in_bytes(), oopSize) | |
600 + round_to(nul_chk_table->size_in_bytes(), oopSize) | |
601 + round_to(debug_info->data_size() , oopSize); | |
602 nm = new (nmethod_size) | |
603 nmethod(method(), nmethod_size, compile_id, entry_bci, offsets, | |
604 orig_pc_offset, debug_info, dependencies, code_buffer, frame_size, | |
605 oop_maps, | |
606 handler_table, | |
607 nul_chk_table, | |
608 compiler, | |
609 comp_level | |
610 #ifdef GRAAL | |
611 , installed_code | |
612 #endif | |
613 ); | |
614 if (nm != NULL) { | |
615 // To make dependency checking during class loading fast, record | |
616 // the nmethod dependencies in the classes it is dependent on. | |
617 // This allows the dependency checking code to simply walk the | |
618 // class hierarchy above the loaded class, checking only nmethods | |
619 // which are dependent on those classes. The slow way is to | |
620 // check every nmethod for dependencies which makes it linear in | |
621 // the number of methods compiled. For applications with a lot | |
622 // classes the slow way is too slow. | |
623 for (Dependencies::DepStream deps(nm); deps.next(); ) { | |
624 Klass* klass = deps.context_type(); | |
625 if (klass == NULL) continue; // ignore things like evol_method | |
626 | |
627 // record this nmethod as dependent on this klass | |
628 InstanceKlass::cast(klass)->add_dependent_nmethod(nm); | |
629 } | |
630 } | |
631 if (nm != NULL) nmethod_stats.note_nmethod(nm); | |
632 if (PrintAssembly && nm != NULL) | |
633 Disassembler::decode(nm); | |
634 | |
7088
97d0eae99568
guard against nmethod allocation failure in implementation of PrintMachineCodeToFile option
Doug Simon <doug.simon@oracle.com>
parents:
7066
diff
changeset
|
635 if (nm != NULL && PrintMachineCodeToFile) { |
7062
8c5333c80cfd
increased CompileThreshold to increase reliability of profiling information
Christian Haeubl <haeubl@ssw.jku.at>
parents:
6275
diff
changeset
|
636 MachineCodePrinter::print(nm); |
7066 | 637 } |
638 } | |
639 | |
640 // verify nmethod | |
641 debug_only(if (nm) nm->verify();) // might block | |
642 | |
643 if (nm != NULL) { | |
644 nm->log_new_nmethod(); | |
645 } | |
646 | |
647 // done | |
648 return nm; | |
649 } | |
650 | |
651 | |
652 // For native wrappers | |
653 nmethod::nmethod( | |
654 Method* method, | |
655 int nmethod_size, | |
656 int compile_id, | |
657 CodeOffsets* offsets, | |
658 CodeBuffer* code_buffer, | |
659 int frame_size, | |
660 ByteSize basic_lock_owner_sp_offset, | |
661 ByteSize basic_lock_sp_offset, | |
662 OopMapSet* oop_maps ) | |
663 : CodeBlob("native nmethod", code_buffer, sizeof(nmethod), | |
664 nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps), | |
665 _native_receiver_sp_offset(basic_lock_owner_sp_offset), | |
666 _native_basic_lock_sp_offset(basic_lock_sp_offset) | |
667 { | |
668 { | |
669 debug_only(No_Safepoint_Verifier nsv;) | |
670 assert_locked_or_safepoint(CodeCache_lock); | |
671 | |
672 init_defaults(); | |
673 _method = method; | |
674 _entry_bci = InvocationEntryBci; | |
675 // We have no exception handler or deopt handler make the | |
676 // values something that will never match a pc like the nmethod vtable entry | |
677 _exception_offset = 0; | |
678 _deoptimize_offset = 0; | |
679 _deoptimize_mh_offset = 0; | |
680 _orig_pc_offset = 0; | |
681 | |
682 _consts_offset = data_offset(); | |
683 _stub_offset = data_offset(); | |
684 _oops_offset = data_offset(); | |
685 _metadata_offset = _oops_offset + round_to(code_buffer->total_oop_size(), oopSize); | |
686 _scopes_data_offset = _metadata_offset + round_to(code_buffer->total_metadata_size(), wordSize); | |
687 _scopes_pcs_offset = _scopes_data_offset; | |
688 _dependencies_offset = _scopes_pcs_offset; | |
689 _handler_table_offset = _dependencies_offset; | |
690 _nul_chk_table_offset = _handler_table_offset; | |
691 _nmethod_end_offset = _nul_chk_table_offset; | |
692 _compile_id = compile_id; | |
693 _comp_level = CompLevel_none; | |
694 _entry_point = code_begin() + offsets->value(CodeOffsets::Entry); | |
695 _verified_entry_point = code_begin() + offsets->value(CodeOffsets::Verified_Entry); | |
696 _osr_entry_point = NULL; | |
697 _exception_cache = NULL; | |
698 _pc_desc_cache.reset_to(NULL); | |
699 | |
700 code_buffer->copy_values_to(this); | |
701 if (ScavengeRootsInCode && detect_scavenge_root_oops()) { | |
702 CodeCache::add_scavenge_root_nmethod(this); | |
703 } | |
704 debug_only(verify_scavenge_root_oops()); | |
705 CodeCache::commit(this); | |
706 } | |
707 | |
708 if (PrintNativeNMethods || PrintDebugInfo || PrintRelocations || PrintDependencies) { | |
709 ttyLocker ttyl; // keep the following output all in one block | |
710 // This output goes directly to the tty, not the compiler log. | |
711 // To enable tools to match it up with the compilation activity, | |
712 // be sure to tag this tty output with the compile ID. | |
713 if (xtty != NULL) { | |
714 xtty->begin_head("print_native_nmethod"); | |
715 xtty->method(_method); | |
716 xtty->stamp(); | |
717 xtty->end_head(" address='" INTPTR_FORMAT "'", (intptr_t) this); | |
718 } | |
719 // print the header part first | |
720 print(); | |
721 // then print the requested information | |
722 if (PrintNativeNMethods) { | |
723 print_code(); | |
724 if (oop_maps != NULL) { | |
725 oop_maps->print(); | |
726 } | |
727 } | |
728 if (PrintRelocations) { | |
729 print_relocations(); | |
730 } | |
731 if (xtty != NULL) { | |
732 xtty->tail("print_native_nmethod"); | |
733 } | |
734 } | |
735 } | |
736 | |
737 // For dtrace wrappers | |
738 #ifdef HAVE_DTRACE_H | |
739 nmethod::nmethod( | |
740 Method* method, | |
741 int nmethod_size, | |
742 CodeOffsets* offsets, | |
743 CodeBuffer* code_buffer, | |
744 int frame_size) | |
745 : CodeBlob("dtrace nmethod", code_buffer, sizeof(nmethod), | |
746 nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, NULL), | |
747 _native_receiver_sp_offset(in_ByteSize(-1)), | |
748 _native_basic_lock_sp_offset(in_ByteSize(-1)) | |
749 { | |
750 { | |
751 debug_only(No_Safepoint_Verifier nsv;) | |
752 assert_locked_or_safepoint(CodeCache_lock); | |
753 | |
754 init_defaults(); | |
755 _method = method; | |
756 _entry_bci = InvocationEntryBci; | |
757 // We have no exception handler or deopt handler make the | |
758 // values something that will never match a pc like the nmethod vtable entry | |
759 _exception_offset = 0; | |
760 _deoptimize_offset = 0; | |
761 _deoptimize_mh_offset = 0; | |
762 _unwind_handler_offset = -1; | |
763 _trap_offset = offsets->value(CodeOffsets::Dtrace_trap); | |
764 _orig_pc_offset = 0; | |
765 _consts_offset = data_offset(); | |
766 _stub_offset = data_offset(); | |
767 _oops_offset = data_offset(); | |
768 _metadata_offset = _oops_offset + round_to(code_buffer->total_oop_size(), oopSize); | |
769 _scopes_data_offset = _metadata_offset + round_to(code_buffer->total_metadata_size(), wordSize); | |
770 _scopes_pcs_offset = _scopes_data_offset; | |
771 _dependencies_offset = _scopes_pcs_offset; | |
772 _handler_table_offset = _dependencies_offset; | |
773 _nul_chk_table_offset = _handler_table_offset; | |
774 _nmethod_end_offset = _nul_chk_table_offset; | |
775 _compile_id = 0; // default | |
776 _comp_level = CompLevel_none; | |
777 _entry_point = code_begin() + offsets->value(CodeOffsets::Entry); | |
778 _verified_entry_point = code_begin() + offsets->value(CodeOffsets::Verified_Entry); | |
779 _osr_entry_point = NULL; | |
780 _exception_cache = NULL; | |
781 _pc_desc_cache.reset_to(NULL); | |
782 | |
783 code_buffer->copy_values_to(this); | |
784 debug_only(verify_scavenge_root_oops()); | |
785 CodeCache::commit(this); | |
786 } | |
787 | |
788 if (PrintNMethods || PrintDebugInfo || PrintRelocations || PrintDependencies) { | |
789 ttyLocker ttyl; // keep the following output all in one block | |
790 // This output goes directly to the tty, not the compiler log. | |
791 // To enable tools to match it up with the compilation activity, | |
792 // be sure to tag this tty output with the compile ID. | |
793 if (xtty != NULL) { | |
794 xtty->begin_head("print_dtrace_nmethod"); | |
795 xtty->method(_method); | |
796 xtty->stamp(); | |
797 xtty->end_head(" address='" INTPTR_FORMAT "'", (intptr_t) this); | |
798 } | |
799 // print the header part first | |
800 print(); | |
801 // then print the requested information | |
802 if (PrintNMethods) { | |
803 print_code(); | |
804 } | |
805 if (PrintRelocations) { | |
806 print_relocations(); | |
807 } | |
808 if (xtty != NULL) { | |
809 xtty->tail("print_dtrace_nmethod"); | |
810 } | |
811 } | |
812 } | |
813 #endif // def HAVE_DTRACE_H | |
814 | |
815 void* nmethod::operator new(size_t size, int nmethod_size) { | |
816 // Always leave some room in the CodeCache for I2C/C2I adapters | |
817 if (CodeCache::largest_free_block() < CodeCacheMinimumFreeSpace) return NULL; | |
818 return CodeCache::allocate(nmethod_size); | |
819 } | |
820 | |
821 | |
822 nmethod::nmethod( | |
823 Method* method, | |
824 int nmethod_size, | |
825 int compile_id, | |
826 int entry_bci, | |
827 CodeOffsets* offsets, | |
828 int orig_pc_offset, | |
829 DebugInformationRecorder* debug_info, | |
830 Dependencies* dependencies, | |
831 CodeBuffer *code_buffer, | |
832 int frame_size, | |
833 OopMapSet* oop_maps, | |
834 ExceptionHandlerTable* handler_table, | |
835 ImplicitExceptionTable* nul_chk_table, | |
836 AbstractCompiler* compiler, | |
837 int comp_level | |
838 #ifdef GRAAL | |
839 , Handle installed_code | |
840 #endif | |
841 ) | |
842 : CodeBlob("nmethod", code_buffer, sizeof(nmethod), | |
843 nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps), | |
844 _native_receiver_sp_offset(in_ByteSize(-1)), | |
845 _native_basic_lock_sp_offset(in_ByteSize(-1)) | |
846 { | |
847 assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR"); | |
848 { | |
849 debug_only(No_Safepoint_Verifier nsv;) | |
850 assert_locked_or_safepoint(CodeCache_lock); | |
851 | |
852 init_defaults(); | |
853 _method = method; | |
854 _entry_bci = entry_bci; | |
855 _compile_id = compile_id; | |
856 _comp_level = comp_level; | |
857 _compiler = compiler; | |
858 _orig_pc_offset = orig_pc_offset; | |
859 | |
860 // Section offsets | |
861 _consts_offset = content_offset() + code_buffer->total_offset_of(code_buffer->consts()); | |
862 _stub_offset = content_offset() + code_buffer->total_offset_of(code_buffer->stubs()); | |
863 | |
864 #ifdef GRAAL | |
865 _graal_installed_code = installed_code(); | |
866 | |
867 // graal produces no (!) stub section | |
868 if (offsets->value(CodeOffsets::Exceptions) != -1) { | |
869 _exception_offset = code_offset() + offsets->value(CodeOffsets::Exceptions); | |
870 } else { | |
871 _exception_offset = -1; | |
872 } | |
873 if (offsets->value(CodeOffsets::Deopt) != -1) { | |
874 _deoptimize_offset = code_offset() + offsets->value(CodeOffsets::Deopt); | |
875 } else { | |
876 _deoptimize_offset = -1; | |
877 } | |
878 if (offsets->value(CodeOffsets::DeoptMH) != -1) { | |
879 _deoptimize_mh_offset = code_offset() + offsets->value(CodeOffsets::DeoptMH); | |
880 } else { | |
881 _deoptimize_mh_offset = -1; | |
882 } | |
883 #else | |
884 // Exception handler and deopt handler are in the stub section | |
885 assert(offsets->value(CodeOffsets::Exceptions) != -1, "must be set"); | |
886 assert(offsets->value(CodeOffsets::Deopt ) != -1, "must be set"); | |
887 | |
888 _exception_offset = _stub_offset + offsets->value(CodeOffsets::Exceptions); | |
889 _deoptimize_offset = _stub_offset + offsets->value(CodeOffsets::Deopt); | |
890 if (offsets->value(CodeOffsets::DeoptMH) != -1) { | |
891 _deoptimize_mh_offset = _stub_offset + offsets->value(CodeOffsets::DeoptMH); | |
892 } else { | |
893 _deoptimize_mh_offset = -1; | |
894 } | |
895 #endif | |
896 if (offsets->value(CodeOffsets::UnwindHandler) != -1) { | |
897 _unwind_handler_offset = code_offset() + offsets->value(CodeOffsets::UnwindHandler); | |
898 } else { | |
899 _unwind_handler_offset = -1; | |
900 } | |
901 | |
902 _oops_offset = data_offset(); | |
903 _metadata_offset = _oops_offset + round_to(code_buffer->total_oop_size(), oopSize); | |
904 _scopes_data_offset = _metadata_offset + round_to(code_buffer->total_metadata_size(), wordSize); | |
905 | |
906 _scopes_pcs_offset = _scopes_data_offset + round_to(debug_info->data_size (), oopSize); | |
907 _dependencies_offset = _scopes_pcs_offset + adjust_pcs_size(debug_info->pcs_size()); | |
908 _handler_table_offset = _dependencies_offset + round_to(dependencies->size_in_bytes (), oopSize); | |
909 _nul_chk_table_offset = _handler_table_offset + round_to(handler_table->size_in_bytes(), oopSize); | |
910 _nmethod_end_offset = _nul_chk_table_offset + round_to(nul_chk_table->size_in_bytes(), oopSize); | |
911 | |
912 _entry_point = code_begin() + offsets->value(CodeOffsets::Entry); | |
913 _verified_entry_point = code_begin() + offsets->value(CodeOffsets::Verified_Entry); | |
914 _osr_entry_point = code_begin() + offsets->value(CodeOffsets::OSR_Entry); | |
915 _exception_cache = NULL; | |
916 _pc_desc_cache.reset_to(scopes_pcs_begin()); | |
917 | |
918 // Copy contents of ScopeDescRecorder to nmethod | |
919 code_buffer->copy_values_to(this); | |
920 debug_info->copy_to(this); | |
921 dependencies->copy_to(this); | |
922 if (ScavengeRootsInCode && detect_scavenge_root_oops()) { | |
923 CodeCache::add_scavenge_root_nmethod(this); | |
924 } | |
925 debug_only(verify_scavenge_root_oops()); | |
926 | |
927 CodeCache::commit(this); | |
928 | |
929 // Copy contents of ExceptionHandlerTable to nmethod | |
930 handler_table->copy_to(this); | |
931 nul_chk_table->copy_to(this); | |
932 | |
933 // we use the information of entry points to find out if a method is | |
934 // static or non static | |
935 assert(compiler->is_c2() || | |
936 _method->is_static() == (entry_point() == _verified_entry_point), | |
937 " entry points must be same for static methods and vice versa"); | |
938 } | |
939 | |
940 bool printnmethods = PrintNMethods | |
941 || CompilerOracle::should_print(_method) | |
942 || CompilerOracle::has_option_string(_method, "PrintNMethods"); | |
943 if (printnmethods || PrintDebugInfo || PrintRelocations || PrintDependencies || PrintExceptionHandlers) { | |
944 print_nmethod(printnmethods); | |
945 } | |
946 } | |
947 | |
948 | |
949 // Print a short set of xml attributes to identify this nmethod. The | |
950 // output should be embedded in some other element. | |
951 void nmethod::log_identity(xmlStream* log) const { | |
952 log->print(" compile_id='%d'", compile_id()); | |
953 const char* nm_kind = compile_kind(); | |
954 if (nm_kind != NULL) log->print(" compile_kind='%s'", nm_kind); | |
955 if (compiler() != NULL) { | |
956 log->print(" compiler='%s'", compiler()->name()); | |
957 } | |
958 if (TieredCompilation) { | |
959 log->print(" level='%d'", comp_level()); | |
960 } | |
961 } | |
962 | |
963 | |
964 #define LOG_OFFSET(log, name) \ | |
965 if ((intptr_t)name##_end() - (intptr_t)name##_begin()) \ | |
966 log->print(" " XSTR(name) "_offset='%d'" , \ | |
967 (intptr_t)name##_begin() - (intptr_t)this) | |
968 | |
969 | |
970 void nmethod::log_new_nmethod() const { | |
971 if (LogCompilation && xtty != NULL) { | |
972 ttyLocker ttyl; | |
973 HandleMark hm; | |
974 xtty->begin_elem("nmethod"); | |
975 log_identity(xtty); | |
976 xtty->print(" entry='" INTPTR_FORMAT "' size='%d'", code_begin(), size()); | |
977 xtty->print(" address='" INTPTR_FORMAT "'", (intptr_t) this); | |
978 | |
979 LOG_OFFSET(xtty, relocation); | |
980 LOG_OFFSET(xtty, consts); | |
981 LOG_OFFSET(xtty, insts); | |
982 LOG_OFFSET(xtty, stub); | |
983 LOG_OFFSET(xtty, scopes_data); | |
984 LOG_OFFSET(xtty, scopes_pcs); | |
985 LOG_OFFSET(xtty, dependencies); | |
986 LOG_OFFSET(xtty, handler_table); | |
987 LOG_OFFSET(xtty, nul_chk_table); | |
988 LOG_OFFSET(xtty, oops); | |
989 | |
990 xtty->method(method()); | |
991 xtty->stamp(); | |
992 xtty->end_elem(); | |
993 } | |
994 } | |
995 | |
996 #undef LOG_OFFSET | |
997 | |
998 | |
999 // Print out more verbose output usually for a newly created nmethod. | |
1000 void nmethod::print_on(outputStream* st, const char* msg) const { | |
1001 if (st != NULL) { | |
1002 ttyLocker ttyl; | |
1003 if (WizardMode) { | |
1004 CompileTask::print_compilation(st, this, msg, /*short_form:*/ true); | |
1005 st->print_cr(" (" INTPTR_FORMAT ")", this); | |
1006 } else { | |
1007 CompileTask::print_compilation(st, this, msg, /*short_form:*/ false); | |
1008 } | |
1009 } | |
1010 } | |
1011 | |
1012 | |
1013 void nmethod::print_nmethod(bool printmethod) { | |
1014 ttyLocker ttyl; // keep the following output all in one block | |
1015 if (xtty != NULL) { | |
1016 xtty->begin_head("print_nmethod"); | |
1017 xtty->stamp(); | |
1018 xtty->end_head(); | |
1019 } | |
1020 // print the header part first | |
1021 print(); | |
1022 // then print the requested information | |
1023 if (printmethod) { | |
1024 print_code(); | |
1025 print_pcs(); | |
1026 if (oop_maps()) { | |
1027 oop_maps()->print(); | |
1028 } | |
1029 } | |
1030 if (PrintDebugInfo) { | |
1031 print_scopes(); | |
1032 } | |
1033 if (PrintRelocations) { | |
1034 print_relocations(); | |
1035 } | |
1036 if (PrintDependencies) { | |
1037 print_dependencies(); | |
1038 } | |
1039 if (PrintExceptionHandlers) { | |
1040 print_handler_table(); | |
1041 print_nul_chk_table(); | |
1042 } | |
1043 if (xtty != NULL) { | |
1044 xtty->tail("print_nmethod"); | |
1045 } | |
1046 } | |
1047 | |
1048 | |
1049 // Promote one word from an assembly-time handle to a live embedded oop. | |
1050 inline void nmethod::initialize_immediate_oop(oop* dest, jobject handle) { | |
1051 if (handle == NULL || | |
1052 // As a special case, IC oops are initialized to 1 or -1. | |
1053 handle == (jobject) Universe::non_oop_word()) { | |
1054 (*dest) = (oop) handle; | |
1055 } else { | |
1056 (*dest) = JNIHandles::resolve_non_null(handle); | |
1057 } | |
1058 } | |
1059 | |
1060 | |
1061 // Have to have the same name because it's called by a template | |
1062 void nmethod::copy_values(GrowableArray<jobject>* array) { | |
1063 int length = array->length(); | |
1064 assert((address)(oops_begin() + length) <= (address)oops_end(), "oops big enough"); | |
1065 oop* dest = oops_begin(); | |
1066 for (int index = 0 ; index < length; index++) { | |
1067 initialize_immediate_oop(&dest[index], array->at(index)); | |
1068 } | |
1069 | |
1070 // Now we can fix up all the oops in the code. We need to do this | |
1071 // in the code because the assembler uses jobjects as placeholders. | |
1072 // The code and relocations have already been initialized by the | |
1073 // CodeBlob constructor, so it is valid even at this early point to | |
1074 // iterate over relocations and patch the code. | |
1075 fix_oop_relocations(NULL, NULL, /*initialize_immediates=*/ true); | |
1076 } | |
1077 | |
1078 void nmethod::copy_values(GrowableArray<Metadata*>* array) { | |
1079 int length = array->length(); | |
1080 assert((address)(metadata_begin() + length) <= (address)metadata_end(), "big enough"); | |
1081 Metadata** dest = metadata_begin(); | |
1082 for (int index = 0 ; index < length; index++) { | |
1083 dest[index] = array->at(index); | |
1084 } | |
1085 } | |
1086 | |
1087 bool nmethod::is_at_poll_return(address pc) { | |
1088 RelocIterator iter(this, pc, pc+1); | |
1089 while (iter.next()) { | |
1090 if (iter.type() == relocInfo::poll_return_type) | |
1091 return true; | |
1092 } | |
1093 return false; | |
1094 } | |
1095 | |
1096 | |
1097 bool nmethod::is_at_poll_or_poll_return(address pc) { | |
1098 RelocIterator iter(this, pc, pc+1); | |
1099 while (iter.next()) { | |
1100 relocInfo::relocType t = iter.type(); | |
1101 if (t == relocInfo::poll_return_type || t == relocInfo::poll_type) | |
1102 return true; | |
1103 } | |
1104 return false; | |
1105 } | |
1106 | |
1107 | |
1108 void nmethod::fix_oop_relocations(address begin, address end, bool initialize_immediates) { | |
1109 // re-patch all oop-bearing instructions, just in case some oops moved | |
1110 RelocIterator iter(this, begin, end); | |
1111 while (iter.next()) { | |
1112 if (iter.type() == relocInfo::oop_type) { | |
1113 oop_Relocation* reloc = iter.oop_reloc(); | |
1114 if (initialize_immediates && reloc->oop_is_immediate()) { | |
1115 oop* dest = reloc->oop_addr(); | |
1116 initialize_immediate_oop(dest, (jobject) *dest); | |
1117 } | |
1118 // Refresh the oop-related bits of this instruction. | |
1119 reloc->fix_oop_relocation(); | |
1120 } else if (iter.type() == relocInfo::metadata_type) { | |
1121 metadata_Relocation* reloc = iter.metadata_reloc(); | |
1122 reloc->fix_metadata_relocation(); | |
1123 } | |
1124 | |
1125 // There must not be any interfering patches or breakpoints. | |
1126 assert(!(iter.type() == relocInfo::breakpoint_type | |
1127 && iter.breakpoint_reloc()->active()), | |
1128 "no active breakpoint"); | |
1129 } | |
1130 } | |
1131 | |
1132 | |
1133 void nmethod::verify_oop_relocations() { | |
1134 // Ensure sure that the code matches the current oop values | |
1135 RelocIterator iter(this, NULL, NULL); | |
1136 while (iter.next()) { | |
1137 if (iter.type() == relocInfo::oop_type) { | |
1138 oop_Relocation* reloc = iter.oop_reloc(); | |
1139 if (!reloc->oop_is_immediate()) { | |
1140 reloc->verify_oop_relocation(); | |
1141 } | |
1142 } | |
1143 } | |
1144 } | |
1145 | |
1146 | |
1147 ScopeDesc* nmethod::scope_desc_at(address pc) { | |
1148 PcDesc* pd = pc_desc_at(pc); | |
1149 guarantee(pd != NULL, "scope must be present"); | |
1150 return new ScopeDesc(this, pd->scope_decode_offset(), | |
1151 pd->obj_decode_offset(), pd->should_reexecute(), pd->rethrow_exception(), | |
1152 pd->return_oop()); | |
1153 } | |
1154 | |
1155 | |
1156 void nmethod::clear_inline_caches() { | |
1157 assert(SafepointSynchronize::is_at_safepoint(), "cleaning of IC's only allowed at safepoint"); | |
1158 if (is_zombie()) { | |
1159 return; | |
1160 } | |
1161 | |
1162 RelocIterator iter(this); | |
1163 while (iter.next()) { | |
1164 iter.reloc()->clear_inline_cache(); | |
1165 } | |
1166 } | |
1167 | |
1168 | |
1169 void nmethod::cleanup_inline_caches() { | |
1170 | |
1171 assert_locked_or_safepoint(CompiledIC_lock); | |
1172 | |
1173 // If the method is not entrant or zombie then a JMP is plastered over the | |
1174 // first few bytes. If an oop in the old code was there, that oop | |
1175 // should not get GC'd. Skip the first few bytes of oops on | |
1176 // not-entrant methods. | |
1177 address low_boundary = verified_entry_point(); | |
1178 if (!is_in_use()) { | |
1179 low_boundary += NativeJump::instruction_size; | |
1180 // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. | |
1181 // This means that the low_boundary is going to be a little too high. | |
1182 // This shouldn't matter, since oops of non-entrant methods are never used. | |
1183 // In fact, why are we bothering to look at oops in a non-entrant method?? | |
1184 } | |
1185 | |
1186 // Find all calls in an nmethod, and clear the ones that points to zombie methods | |
1187 ResourceMark rm; | |
1188 RelocIterator iter(this, low_boundary); | |
1189 while(iter.next()) { | |
1190 switch(iter.type()) { | |
1191 case relocInfo::virtual_call_type: | |
1192 case relocInfo::opt_virtual_call_type: { | |
1193 CompiledIC *ic = CompiledIC_at(iter.reloc()); | |
1194 // Ok, to lookup references to zombies here | |
1195 CodeBlob *cb = CodeCache::find_blob_unsafe(ic->ic_destination()); | |
1196 if( cb != NULL && cb->is_nmethod() ) { | |
1197 nmethod* nm = (nmethod*)cb; | |
1198 // Clean inline caches pointing to both zombie and not_entrant methods | |
1199 if (!nm->is_in_use() || (nm->method()->code() != nm)) ic->set_to_clean(); | |
1200 } | |
1201 break; | |
1202 } | |
1203 case relocInfo::static_call_type: { | |
1204 CompiledStaticCall *csc = compiledStaticCall_at(iter.reloc()); | |
1205 CodeBlob *cb = CodeCache::find_blob_unsafe(csc->destination()); | |
1206 if( cb != NULL && cb->is_nmethod() ) { | |
1207 nmethod* nm = (nmethod*)cb; | |
1208 // Clean inline caches pointing to both zombie and not_entrant methods | |
1209 if (!nm->is_in_use() || (nm->method()->code() != nm)) csc->set_to_clean(); | |
1210 } | |
1211 break; | |
1212 } | |
1213 } | |
1214 } | |
1215 } | |
1216 | |
1217 // This is a private interface with the sweeper. | |
1218 void nmethod::mark_as_seen_on_stack() { | |
1219 assert(is_not_entrant(), "must be a non-entrant method"); | |
1220 // Set the traversal mark to ensure that the sweeper does 2 | |
1221 // cleaning passes before moving to zombie. | |
1222 set_stack_traversal_mark(NMethodSweeper::traversal_count()); | |
1223 } | |
1224 | |
1225 // Tell if a non-entrant method can be converted to a zombie (i.e., | |
1226 // there are no activations on the stack, not in use by the VM, | |
1227 // and not in use by the ServiceThread) | |
1228 bool nmethod::can_not_entrant_be_converted() { | |
1229 assert(is_not_entrant(), "must be a non-entrant method"); | |
1230 | |
1231 // Since the nmethod sweeper only does partial sweep the sweeper's traversal | |
1232 // count can be greater than the stack traversal count before it hits the | |
1233 // nmethod for the second time. | |
1234 return stack_traversal_mark()+1 < NMethodSweeper::traversal_count() && | |
1235 !is_locked_by_vm(); | |
1236 } | |
1237 | |
1238 void nmethod::inc_decompile_count() { | |
1239 #ifndef GRAAL | |
1240 if (!is_compiled_by_c2()) return; | |
1241 #endif | |
1242 // Could be gated by ProfileTraps, but do not bother... | |
1243 Method* m = method(); | |
1244 if (m == NULL) return; | |
1245 MethodData* mdo = m->method_data(); | |
1246 if (mdo == NULL) return; | |
1247 // There is a benign race here. See comments in methodData.hpp. | |
1248 mdo->inc_decompile_count(); | |
1249 } | |
1250 | |
1251 void nmethod::make_unloaded(BoolObjectClosure* is_alive, oop cause) { | |
1252 | |
1253 post_compiled_method_unload(); | |
1254 | |
1255 // Since this nmethod is being unloaded, make sure that dependencies | |
1256 // recorded in instanceKlasses get flushed and pass non-NULL closure to | |
1257 // indicate that this work is being done during a GC. | |
1258 assert(Universe::heap()->is_gc_active(), "should only be called during gc"); | |
1259 assert(is_alive != NULL, "Should be non-NULL"); | |
1260 // A non-NULL is_alive closure indicates that this is being called during GC. | |
1261 flush_dependencies(is_alive); | |
1262 | |
1263 // Break cycle between nmethod & method | |
1264 if (TraceClassUnloading && WizardMode) { | |
1265 tty->print_cr("[Class unloading: Making nmethod " INTPTR_FORMAT | |
1266 " unloadable], Method*(" INTPTR_FORMAT | |
1267 "), cause(" INTPTR_FORMAT ")", | |
1268 this, (address)_method, (address)cause); | |
1269 if (!Universe::heap()->is_gc_active()) | |
1270 cause->klass()->print(); | |
1271 } | |
1272 // Unlink the osr method, so we do not look this up again | |
1273 if (is_osr_method()) { | |
1274 invalidate_osr_method(); | |
1275 } | |
1276 // If _method is already NULL the Method* is about to be unloaded, | |
1277 // so we don't have to break the cycle. Note that it is possible to | |
1278 // have the Method* live here, in case we unload the nmethod because | |
1279 // it is pointing to some oop (other than the Method*) being unloaded. | |
1280 if (_method != NULL) { | |
1281 // OSR methods point to the Method*, but the Method* does not | |
1282 // point back! | |
1283 if (_method->code() == this) { | |
1284 _method->clear_code(); // Break a cycle | |
1285 } | |
1286 _method = NULL; // Clear the method of this dead nmethod | |
1287 } | |
1288 | |
1289 #ifdef GRAAL | |
1290 if (_graal_installed_code != NULL) { | |
1291 HotSpotInstalledCode::set_nmethod(_graal_installed_code, 0); | |
1292 _graal_installed_code = NULL; | |
1293 } | |
1294 #endif | |
1295 | |
1296 // Make the class unloaded - i.e., change state and notify sweeper | |
1297 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); | |
1298 if (is_in_use()) { | |
1299 // Transitioning directly from live to unloaded -- so | |
1300 // we need to force a cache clean-up; remember this | |
1301 // for later on. | |
1302 CodeCache::set_needs_cache_clean(true); | |
1303 } | |
1304 _state = unloaded; | |
1305 | |
1306 // Log the unloading. | |
1307 log_state_change(); | |
1308 | |
1309 // The Method* is gone at this point | |
1310 assert(_method == NULL, "Tautology"); | |
1311 | |
1312 set_osr_link(NULL); | |
1313 //set_scavenge_root_link(NULL); // done by prune_scavenge_root_nmethods | |
1314 NMethodSweeper::notify(this); | |
1315 } | |
1316 | |
1317 void nmethod::invalidate_osr_method() { | |
1318 assert(_entry_bci != InvocationEntryBci, "wrong kind of nmethod"); | |
1319 // Remove from list of active nmethods | |
1320 if (method() != NULL) | |
1321 method()->method_holder()->remove_osr_nmethod(this); | |
1322 // Set entry as invalid | |
1323 _entry_bci = InvalidOSREntryBci; | |
1324 } | |
1325 | |
1326 void nmethod::log_state_change() const { | |
1327 if (LogCompilation) { | |
1328 if (xtty != NULL) { | |
1329 ttyLocker ttyl; // keep the following output all in one block | |
1330 if (_state == unloaded) { | |
1331 xtty->begin_elem("make_unloaded thread='" UINTX_FORMAT "'", | |
1332 os::current_thread_id()); | |
1333 } else { | |
1334 xtty->begin_elem("make_not_entrant thread='" UINTX_FORMAT "'%s", | |
1335 os::current_thread_id(), | |
1336 (_state == zombie ? " zombie='1'" : "")); | |
1337 } | |
1338 log_identity(xtty); | |
1339 xtty->stamp(); | |
1340 xtty->end_elem(); | |
1341 } | |
1342 } | |
1343 if (PrintCompilation && _state != unloaded) { | |
1344 print_on(tty, _state == zombie ? "made zombie" : "made not entrant"); | |
1345 } | |
1346 } | |
1347 | |
1348 // Common functionality for both make_not_entrant and make_zombie | |
1349 bool nmethod::make_not_entrant_or_zombie(unsigned int state) { | |
1350 assert(state == zombie || state == not_entrant, "must be zombie or not_entrant"); | |
1351 assert(!is_zombie(), "should not already be a zombie"); | |
1352 | |
1353 // Make sure neither the nmethod nor the method is flushed in case of a safepoint in code below. | |
1354 nmethodLocker nml(this); | |
1355 methodHandle the_method(method()); | |
1356 No_Safepoint_Verifier nsv; | |
1357 | |
1358 { | |
1359 // invalidate osr nmethod before acquiring the patching lock since | |
1360 // they both acquire leaf locks and we don't want a deadlock. | |
1361 // This logic is equivalent to the logic below for patching the | |
1362 // verified entry point of regular methods. | |
1363 if (is_osr_method()) { | |
1364 // this effectively makes the osr nmethod not entrant | |
1365 invalidate_osr_method(); | |
1366 } | |
1367 | |
1368 // Enter critical section. Does not block for safepoint. | |
1369 MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag); | |
1370 | |
1371 if (_state == state) { | |
1372 // another thread already performed this transition so nothing | |
1373 // to do, but return false to indicate this. | |
1374 return false; | |
1375 } | |
1376 | |
1377 #ifdef GRAAL | |
1378 if (_graal_installed_code != NULL) { | |
1379 HotSpotInstalledCode::set_nmethod(_graal_installed_code, 0); | |
1380 _graal_installed_code = NULL; | |
1381 } | |
1382 #endif | |
1383 | |
1384 // The caller can be calling the method statically or through an inline | |
1385 // cache call. | |
1386 if (!is_osr_method() && !is_not_entrant()) { | |
1387 NativeJump::patch_verified_entry(entry_point(), verified_entry_point(), | |
1388 SharedRuntime::get_handle_wrong_method_stub()); | |
1389 } | |
1390 | |
1391 if (is_in_use()) { | |
1392 // It's a true state change, so mark the method as decompiled. | |
1393 // Do it only for transition from alive. | |
1394 inc_decompile_count(); | |
1395 } | |
1396 | |
1397 // Change state | |
1398 _state = state; | |
1399 | |
1400 // Log the transition once | |
1401 log_state_change(); | |
1402 | |
1403 // Remove nmethod from method. | |
1404 // We need to check if both the _code and _from_compiled_code_entry_point | |
1405 // refer to this nmethod because there is a race in setting these two fields | |
1406 // in Method* as seen in bugid 4947125. | |
1407 // If the vep() points to the zombie nmethod, the memory for the nmethod | |
1408 // could be flushed and the compiler and vtable stubs could still call | |
1409 // through it. | |
1410 if (method() != NULL && (method()->code() == this || | |
1411 method()->from_compiled_entry() == verified_entry_point())) { | |
1412 HandleMark hm; | |
1413 method()->clear_code(); | |
1414 } | |
1415 | |
1416 if (state == not_entrant) { | |
1417 mark_as_seen_on_stack(); | |
1418 } | |
1419 | |
1420 } // leave critical region under Patching_lock | |
1421 | |
1422 // When the nmethod becomes zombie it is no longer alive so the | |
1423 // dependencies must be flushed. nmethods in the not_entrant | |
1424 // state will be flushed later when the transition to zombie | |
1425 // happens or they get unloaded. | |
1426 if (state == zombie) { | |
1427 { | |
1428 // Flushing dependecies must be done before any possible | |
1429 // safepoint can sneak in, otherwise the oops used by the | |
1430 // dependency logic could have become stale. | |
1431 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); | |
1432 flush_dependencies(NULL); | |
1433 } | |
1434 | |
1435 // zombie only - if a JVMTI agent has enabled the CompiledMethodUnload | |
1436 // event and it hasn't already been reported for this nmethod then | |
1437 // report it now. The event may have been reported earilier if the GC | |
1438 // marked it for unloading). JvmtiDeferredEventQueue support means | |
1439 // we no longer go to a safepoint here. | |
1440 post_compiled_method_unload(); | |
1441 | |
1442 #ifdef ASSERT | |
1443 // It's no longer safe to access the oops section since zombie | |
1444 // nmethods aren't scanned for GC. | |
1445 _oops_are_stale = true; | |
1446 #endif | |
1447 } else { | |
1448 assert(state == not_entrant, "other cases may need to be handled differently"); | |
1449 } | |
1450 | |
1451 if (TraceCreateZombies) { | |
7112
60bef2672469
changed System.err output to TTY to avoid that DaCapo benchmarks fail digest validation
Christian Haeubl <haeubl@ssw.jku.at>
parents:
7088
diff
changeset
|
1452 ResourceMark m; |
60bef2672469
changed System.err output to TTY to avoid that DaCapo benchmarks fail digest validation
Christian Haeubl <haeubl@ssw.jku.at>
parents:
7088
diff
changeset
|
1453 tty->print_cr("nmethod <" INTPTR_FORMAT "> %s code made %s", this, this->method()->name_and_sig_as_C_string(), (state == not_entrant) ? "not entrant" : "zombie"); |
7066 | 1454 } |
1455 | |
1456 // Make sweeper aware that there is a zombie method that needs to be removed | |
1457 NMethodSweeper::notify(this); | |
1458 | |
1459 return true; | |
1460 } | |
1461 | |
1462 void nmethod::flush() { | |
1463 // Note that there are no valid oops in the nmethod anymore. | |
1464 assert(is_zombie() || (is_osr_method() && is_unloaded()), "must be a zombie method"); | |
1465 assert(is_marked_for_reclamation() || (is_osr_method() && is_unloaded()), "must be marked for reclamation"); | |
1466 | |
1467 assert (!is_locked_by_vm(), "locked methods shouldn't be flushed"); | |
1468 assert_locked_or_safepoint(CodeCache_lock); | |
1469 | |
1470 // completely deallocate this method | |
1471 Events::log(JavaThread::current(), "flushing nmethod " INTPTR_FORMAT, this); | |
1472 if (PrintMethodFlushing) { | |
1473 tty->print_cr("*flushing nmethod %3d/" INTPTR_FORMAT ". Live blobs:" UINT32_FORMAT "/Free CodeCache:" SIZE_FORMAT "Kb", | |
1474 _compile_id, this, CodeCache::nof_blobs(), CodeCache::unallocated_capacity()/1024); | |
1475 } | |
1476 | |
1477 // We need to deallocate any ExceptionCache data. | |
1478 // Note that we do not need to grab the nmethod lock for this, it | |
1479 // better be thread safe if we're disposing of it! | |
1480 ExceptionCache* ec = exception_cache(); | |
1481 set_exception_cache(NULL); | |
1482 while(ec != NULL) { | |
1483 ExceptionCache* next = ec->next(); | |
1484 delete ec; | |
1485 ec = next; | |
1486 } | |
1487 | |
1488 if (on_scavenge_root_list()) { | |
1489 CodeCache::drop_scavenge_root_nmethod(this); | |
1490 } | |
1491 | |
1492 if (is_speculatively_disconnected()) { | |
1493 CodeCache::remove_saved_code(this); | |
1494 } | |
1495 | |
1496 #ifdef SHARK | |
1497 ((SharkCompiler *) compiler())->free_compiled_method(insts_begin()); | |
1498 #endif // SHARK | |
1499 | |
1500 ((CodeBlob*)(this))->flush(); | |
1501 | |
1502 CodeCache::free(this); | |
1503 } | |
1504 | |
1505 | |
1506 // | |
1507 // Notify all classes this nmethod is dependent on that it is no | |
1508 // longer dependent. This should only be called in two situations. | |
1509 // First, when a nmethod transitions to a zombie all dependents need | |
1510 // to be clear. Since zombification happens at a safepoint there's no | |
1511 // synchronization issues. The second place is a little more tricky. | |
1512 // During phase 1 of mark sweep class unloading may happen and as a | |
1513 // result some nmethods may get unloaded. In this case the flushing | |
1514 // of dependencies must happen during phase 1 since after GC any | |
1515 // dependencies in the unloaded nmethod won't be updated, so | |
1516 // traversing the dependency information in unsafe. In that case this | |
1517 // function is called with a non-NULL argument and this function only | |
1518 // notifies instanceKlasses that are reachable | |
1519 | |
1520 void nmethod::flush_dependencies(BoolObjectClosure* is_alive) { | |
1521 assert_locked_or_safepoint(CodeCache_lock); | |
1522 assert(Universe::heap()->is_gc_active() == (is_alive != NULL), | |
1523 "is_alive is non-NULL if and only if we are called during GC"); | |
1524 if (!has_flushed_dependencies()) { | |
1525 set_has_flushed_dependencies(); | |
1526 for (Dependencies::DepStream deps(this); deps.next(); ) { | |
1527 Klass* klass = deps.context_type(); | |
1528 if (klass == NULL) continue; // ignore things like evol_method | |
1529 | |
1530 // During GC the is_alive closure is non-NULL, and is used to | |
1531 // determine liveness of dependees that need to be updated. | |
1532 if (is_alive == NULL || klass->is_loader_alive(is_alive)) { | |
1533 InstanceKlass::cast(klass)->remove_dependent_nmethod(this); | |
1534 } | |
1535 } | |
1536 } | |
1537 } | |
1538 | |
1539 | |
1540 // If this oop is not live, the nmethod can be unloaded. | |
1541 bool nmethod::can_unload(BoolObjectClosure* is_alive, oop* root, bool unloading_occurred) { | |
1542 assert(root != NULL, "just checking"); | |
1543 oop obj = *root; | |
1544 if (obj == NULL || is_alive->do_object_b(obj)) { | |
1545 return false; | |
1546 } | |
1547 | |
1548 // If ScavengeRootsInCode is true, an nmethod might be unloaded | |
1549 // simply because one of its constant oops has gone dead. | |
1550 // No actual classes need to be unloaded in order for this to occur. | |
1551 assert(unloading_occurred || ScavengeRootsInCode, "Inconsistency in unloading"); | |
1552 make_unloaded(is_alive, obj); | |
1553 return true; | |
1554 } | |
1555 | |
1556 // ------------------------------------------------------------------ | |
1557 // post_compiled_method_load_event | |
1558 // new method for install_code() path | |
1559 // Transfer information from compilation to jvmti | |
1560 void nmethod::post_compiled_method_load_event() { | |
1561 | |
1562 Method* moop = method(); | |
1563 #ifndef USDT2 | |
1564 HS_DTRACE_PROBE8(hotspot, compiled__method__load, | |
1565 moop->klass_name()->bytes(), | |
1566 moop->klass_name()->utf8_length(), | |
1567 moop->name()->bytes(), | |
1568 moop->name()->utf8_length(), | |
1569 moop->signature()->bytes(), | |
1570 moop->signature()->utf8_length(), | |
1571 insts_begin(), insts_size()); | |
1572 #else /* USDT2 */ | |
1573 HOTSPOT_COMPILED_METHOD_LOAD( | |
1574 (char *) moop->klass_name()->bytes(), | |
1575 moop->klass_name()->utf8_length(), | |
1576 (char *) moop->name()->bytes(), | |
1577 moop->name()->utf8_length(), | |
1578 (char *) moop->signature()->bytes(), | |
1579 moop->signature()->utf8_length(), | |
1580 insts_begin(), insts_size()); | |
1581 #endif /* USDT2 */ | |
1582 | |
1583 if (JvmtiExport::should_post_compiled_method_load() || | |
1584 JvmtiExport::should_post_compiled_method_unload()) { | |
1585 get_and_cache_jmethod_id(); | |
1586 } | |
1587 | |
1588 if (JvmtiExport::should_post_compiled_method_load()) { | |
1589 // Let the Service thread (which is a real Java thread) post the event | |
1590 MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); | |
1591 JvmtiDeferredEventQueue::enqueue( | |
1592 JvmtiDeferredEvent::compiled_method_load_event(this)); | |
1593 } | |
1594 } | |
1595 | |
1596 jmethodID nmethod::get_and_cache_jmethod_id() { | |
1597 if (_jmethod_id == NULL) { | |
1598 // Cache the jmethod_id since it can no longer be looked up once the | |
1599 // method itself has been marked for unloading. | |
1600 _jmethod_id = method()->jmethod_id(); | |
1601 } | |
1602 return _jmethod_id; | |
1603 } | |
1604 | |
1605 void nmethod::post_compiled_method_unload() { | |
1606 if (unload_reported()) { | |
1607 // During unloading we transition to unloaded and then to zombie | |
1608 // and the unloading is reported during the first transition. | |
1609 return; | |
1610 } | |
1611 | |
1612 assert(_method != NULL && !is_unloaded(), "just checking"); | |
1613 DTRACE_METHOD_UNLOAD_PROBE(method()); | |
1614 | |
1615 // If a JVMTI agent has enabled the CompiledMethodUnload event then | |
1616 // post the event. Sometime later this nmethod will be made a zombie | |
1617 // by the sweeper but the Method* will not be valid at that point. | |
1618 // If the _jmethod_id is null then no load event was ever requested | |
1619 // so don't bother posting the unload. The main reason for this is | |
1620 // that the jmethodID is a weak reference to the Method* so if | |
1621 // it's being unloaded there's no way to look it up since the weak | |
1622 // ref will have been cleared. | |
1623 if (_jmethod_id != NULL && JvmtiExport::should_post_compiled_method_unload()) { | |
1624 assert(!unload_reported(), "already unloaded"); | |
1625 JvmtiDeferredEvent event = | |
1626 JvmtiDeferredEvent::compiled_method_unload_event(this, | |
1627 _jmethod_id, insts_begin()); | |
1628 if (SafepointSynchronize::is_at_safepoint()) { | |
1629 // Don't want to take the queueing lock. Add it as pending and | |
1630 // it will get enqueued later. | |
1631 JvmtiDeferredEventQueue::add_pending_event(event); | |
1632 } else { | |
1633 MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); | |
1634 JvmtiDeferredEventQueue::enqueue(event); | |
1635 } | |
1636 } | |
1637 | |
1638 // The JVMTI CompiledMethodUnload event can be enabled or disabled at | |
1639 // any time. As the nmethod is being unloaded now we mark it has | |
1640 // having the unload event reported - this will ensure that we don't | |
1641 // attempt to report the event in the unlikely scenario where the | |
1642 // event is enabled at the time the nmethod is made a zombie. | |
1643 set_unload_reported(); | |
1644 } | |
1645 | |
1646 // This is called at the end of the strong tracing/marking phase of a | |
1647 // GC to unload an nmethod if it contains otherwise unreachable | |
1648 // oops. | |
1649 | |
1650 void nmethod::do_unloading(BoolObjectClosure* is_alive, bool unloading_occurred) { | |
1651 // Make sure the oop's ready to receive visitors | |
1652 assert(!is_zombie() && !is_unloaded(), | |
1653 "should not call follow on zombie or unloaded nmethod"); | |
1654 | |
1655 // If the method is not entrant then a JMP is plastered over the | |
1656 // first few bytes. If an oop in the old code was there, that oop | |
1657 // should not get GC'd. Skip the first few bytes of oops on | |
1658 // not-entrant methods. | |
1659 address low_boundary = verified_entry_point(); | |
1660 if (is_not_entrant()) { | |
1661 low_boundary += NativeJump::instruction_size; | |
1662 // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. | |
1663 // (See comment above.) | |
1664 } | |
1665 | |
1666 // The RedefineClasses() API can cause the class unloading invariant | |
1667 // to no longer be true. See jvmtiExport.hpp for details. | |
1668 // Also, leave a debugging breadcrumb in local flag. | |
1669 bool a_class_was_redefined = JvmtiExport::has_redefined_a_class(); | |
1670 if (a_class_was_redefined) { | |
1671 // This set of the unloading_occurred flag is done before the | |
1672 // call to post_compiled_method_unload() so that the unloading | |
1673 // of this nmethod is reported. | |
1674 unloading_occurred = true; | |
1675 } | |
1676 | |
1677 #ifdef GRAAL | |
1678 // Follow Graal method | |
1679 if (_graal_installed_code != NULL && can_unload(is_alive, (oop*)&_graal_installed_code, unloading_occurred)) { | |
1680 return; | |
1681 } | |
1682 #endif | |
1683 | |
1684 // Exception cache | |
1685 ExceptionCache* ec = exception_cache(); | |
1686 while (ec != NULL) { | |
1687 Klass* ex_klass = ec->exception_type(); | |
1688 ExceptionCache* next_ec = ec->next(); | |
1689 if (ex_klass != NULL && !ex_klass->is_loader_alive(is_alive)) { | |
1690 remove_from_exception_cache(ec); | |
1691 } | |
1692 ec = next_ec; | |
1693 } | |
1694 | |
1695 // If class unloading occurred we first iterate over all inline caches and | |
1696 // clear ICs where the cached oop is referring to an unloaded klass or method. | |
1697 // The remaining live cached oops will be traversed in the relocInfo::oop_type | |
1698 // iteration below. | |
1699 if (unloading_occurred) { | |
1700 RelocIterator iter(this, low_boundary); | |
1701 while(iter.next()) { | |
1702 if (iter.type() == relocInfo::virtual_call_type) { | |
1703 CompiledIC *ic = CompiledIC_at(iter.reloc()); | |
1704 if (ic->is_icholder_call()) { | |
1705 // The only exception is compiledICHolder oops which may | |
1706 // yet be marked below. (We check this further below). | |
1707 CompiledICHolder* cichk_oop = ic->cached_icholder(); | |
1708 if (cichk_oop->holder_method()->method_holder()->is_loader_alive(is_alive) && | |
1709 cichk_oop->holder_klass()->is_loader_alive(is_alive)) { | |
1710 continue; | |
1711 } | |
1712 } else { | |
1713 Metadata* ic_oop = ic->cached_metadata(); | |
1714 if (ic_oop != NULL) { | |
1715 if (ic_oop->is_klass()) { | |
1716 if (((Klass*)ic_oop)->is_loader_alive(is_alive)) { | |
1717 continue; | |
1718 } | |
1719 } else if (ic_oop->is_method()) { | |
1720 if (((Method*)ic_oop)->method_holder()->is_loader_alive(is_alive)) { | |
1721 continue; | |
1722 } | |
1723 } else { | |
1724 ShouldNotReachHere(); | |
1725 } | |
1726 } | |
1727 } | |
1728 ic->set_to_clean(); | |
1729 } | |
1730 } | |
1731 } | |
1732 | |
1733 // Compiled code | |
1734 { | |
1735 RelocIterator iter(this, low_boundary); | |
1736 while (iter.next()) { | |
1737 if (iter.type() == relocInfo::oop_type) { | |
1738 oop_Relocation* r = iter.oop_reloc(); | |
1739 // In this loop, we must only traverse those oops directly embedded in | |
1740 // the code. Other oops (oop_index>0) are seen as part of scopes_oops. | |
1741 assert(1 == (r->oop_is_immediate()) + | |
1742 (r->oop_addr() >= oops_begin() && r->oop_addr() < oops_end()), | |
1743 "oop must be found in exactly one place"); | |
1744 if (r->oop_is_immediate() && r->oop_value() != NULL) { | |
1745 if (can_unload(is_alive, r->oop_addr(), unloading_occurred)) { | |
1746 return; | |
1747 } | |
1748 } | |
1749 } | |
1750 } | |
1751 } | |
1752 | |
1753 | |
1754 // Scopes | |
1755 for (oop* p = oops_begin(); p < oops_end(); p++) { | |
1756 if (*p == Universe::non_oop_word()) continue; // skip non-oops | |
1757 if (can_unload(is_alive, p, unloading_occurred)) { | |
1758 return; | |
1759 } | |
1760 } | |
1761 | |
1762 // Ensure that all metadata is still alive | |
1763 verify_metadata_loaders(low_boundary, is_alive); | |
1764 } | |
1765 | |
1766 #ifdef ASSERT | |
1767 | |
1768 class CheckClass : AllStatic { | |
1769 static BoolObjectClosure* _is_alive; | |
1770 | |
1771 // Check class_loader is alive for this bit of metadata. | |
1772 static void check_class(Metadata* md) { | |
1773 Klass* klass = NULL; | |
1774 if (md->is_klass()) { | |
1775 klass = ((Klass*)md); | |
1776 } else if (md->is_method()) { | |
1777 klass = ((Method*)md)->method_holder(); | |
1778 } else if (md->is_methodData()) { | |
1779 klass = ((MethodData*)md)->method()->method_holder(); | |
1780 } else { | |
1781 md->print(); | |
1782 ShouldNotReachHere(); | |
1783 } | |
1784 assert(klass->is_loader_alive(_is_alive), "must be alive"); | |
1785 } | |
1786 public: | |
1787 static void do_check_class(BoolObjectClosure* is_alive, nmethod* nm) { | |
1788 assert(SafepointSynchronize::is_at_safepoint(), "this is only ok at safepoint"); | |
1789 _is_alive = is_alive; | |
1790 nm->metadata_do(check_class); | |
1791 } | |
1792 }; | |
1793 | |
1794 // This is called during a safepoint so can use static data | |
1795 BoolObjectClosure* CheckClass::_is_alive = NULL; | |
1796 #endif // ASSERT | |
1797 | |
1798 | |
1799 // Processing of oop references should have been sufficient to keep | |
1800 // all strong references alive. Any weak references should have been | |
1801 // cleared as well. Visit all the metadata and ensure that it's | |
1802 // really alive. | |
1803 void nmethod::verify_metadata_loaders(address low_boundary, BoolObjectClosure* is_alive) { | |
1804 #ifdef ASSERT | |
1805 RelocIterator iter(this, low_boundary); | |
1806 while (iter.next()) { | |
1807 // static_stub_Relocations may have dangling references to | |
1808 // Method*s so trim them out here. Otherwise it looks like | |
1809 // compiled code is maintaining a link to dead metadata. | |
1810 address static_call_addr = NULL; | |
1811 if (iter.type() == relocInfo::opt_virtual_call_type) { | |
1812 CompiledIC* cic = CompiledIC_at(iter.reloc()); | |
1813 if (!cic->is_call_to_interpreted()) { | |
1814 static_call_addr = iter.addr(); | |
1815 cic->set_to_clean(); | |
1816 } | |
1817 } else if (iter.type() == relocInfo::static_call_type) { | |
1818 CompiledStaticCall* csc = compiledStaticCall_at(iter.reloc()); | |
1819 if (!csc->is_call_to_interpreted()) { | |
1820 static_call_addr = iter.addr(); | |
1821 csc->set_to_clean(); | |
1822 } | |
1823 } | |
1824 if (static_call_addr != NULL) { | |
1825 RelocIterator sciter(this, low_boundary); | |
1826 while (sciter.next()) { | |
1827 if (sciter.type() == relocInfo::static_stub_type && | |
1828 sciter.static_stub_reloc()->static_call() == static_call_addr) { | |
1829 sciter.static_stub_reloc()->clear_inline_cache(); | |
1830 } | |
1831 } | |
1832 } | |
1833 } | |
1834 // Check that the metadata embedded in the nmethod is alive | |
1835 CheckClass::do_check_class(is_alive, this); | |
1836 #endif | |
1837 } | |
1838 | |
1839 | |
1840 // Iterate over metadata calling this function. Used by RedefineClasses | |
1841 void nmethod::metadata_do(void f(Metadata*)) { | |
1842 address low_boundary = verified_entry_point(); | |
1843 if (is_not_entrant()) { | |
1844 low_boundary += NativeJump::instruction_size; | |
1845 // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. | |
1846 // (See comment above.) | |
1847 } | |
1848 { | |
1849 // Visit all immediate references that are embedded in the instruction stream. | |
1850 RelocIterator iter(this, low_boundary); | |
1851 while (iter.next()) { | |
1852 if (iter.type() == relocInfo::metadata_type ) { | |
1853 metadata_Relocation* r = iter.metadata_reloc(); | |
1854 // In this lmetadata, we must only follow those metadatas directly embedded in | |
1855 // the code. Other metadatas (oop_index>0) are seen as part of | |
1856 // the metadata section below. | |
1857 assert(1 == (r->metadata_is_immediate()) + | |
1858 (r->metadata_addr() >= metadata_begin() && r->metadata_addr() < metadata_end()), | |
1859 "metadata must be found in exactly one place"); | |
1860 if (r->metadata_is_immediate() && r->metadata_value() != NULL) { | |
1861 Metadata* md = r->metadata_value(); | |
1862 f(md); | |
1863 } | |
1864 } | |
1865 } | |
1866 } | |
1867 | |
1868 // Visit the metadata section | |
1869 for (Metadata** p = metadata_begin(); p < metadata_end(); p++) { | |
1870 if (*p == Universe::non_oop_word() || *p == NULL) continue; // skip non-oops | |
1871 Metadata* md = *p; | |
1872 f(md); | |
1873 } | |
1874 // Call function Method*, not embedded in these other places. | |
1875 if (_method != NULL) f(_method); | |
1876 } | |
1877 | |
1878 | |
1879 // This method is called twice during GC -- once while | |
1880 // tracing the "active" nmethods on thread stacks during | |
1881 // the (strong) marking phase, and then again when walking | |
1882 // the code cache contents during the weak roots processing | |
1883 // phase. The two uses are distinguished by means of the | |
1884 // 'do_strong_roots_only' flag, which is true in the first | |
1885 // case. We want to walk the weak roots in the nmethod | |
1886 // only in the second case. The weak roots in the nmethod | |
1887 // are the oops in the ExceptionCache and the InlineCache | |
1888 // oops. | |
1889 void nmethod::oops_do(OopClosure* f, bool do_strong_roots_only) { | |
1890 // make sure the oops ready to receive visitors | |
1891 assert(!is_zombie() && !is_unloaded(), | |
1892 "should not call follow on zombie or unloaded nmethod"); | |
1893 | |
1894 // If the method is not entrant or zombie then a JMP is plastered over the | |
1895 // first few bytes. If an oop in the old code was there, that oop | |
1896 // should not get GC'd. Skip the first few bytes of oops on | |
1897 // not-entrant methods. | |
1898 address low_boundary = verified_entry_point(); | |
1899 if (is_not_entrant()) { | |
1900 low_boundary += NativeJump::instruction_size; | |
1901 // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. | |
1902 // (See comment above.) | |
1903 } | |
1904 | |
1905 #ifdef GRAAL | |
1906 if (_graal_installed_code != NULL) { | |
1907 f->do_oop((oop*) &_graal_installed_code); | |
1908 } | |
1909 #endif | |
1910 | |
1911 RelocIterator iter(this, low_boundary); | |
1912 | |
1913 while (iter.next()) { | |
1914 if (iter.type() == relocInfo::oop_type ) { | |
1915 oop_Relocation* r = iter.oop_reloc(); | |
1916 // In this loop, we must only follow those oops directly embedded in | |
1917 // the code. Other oops (oop_index>0) are seen as part of scopes_oops. | |
1918 assert(1 == (r->oop_is_immediate()) + | |
1919 (r->oop_addr() >= oops_begin() && r->oop_addr() < oops_end()), | |
1920 "oop must be found in exactly one place"); | |
1921 if (r->oop_is_immediate() && r->oop_value() != NULL) { | |
1922 f->do_oop(r->oop_addr()); | |
1923 } | |
1924 } | |
1925 } | |
1926 | |
1927 // Scopes | |
1928 // This includes oop constants not inlined in the code stream. | |
1929 for (oop* p = oops_begin(); p < oops_end(); p++) { | |
1930 if (*p == Universe::non_oop_word()) continue; // skip non-oops | |
1931 f->do_oop(p); | |
1932 } | |
1933 } | |
1934 | |
1935 #define NMETHOD_SENTINEL ((nmethod*)badAddress) | |
1936 | |
1937 nmethod* volatile nmethod::_oops_do_mark_nmethods; | |
1938 | |
1939 // An nmethod is "marked" if its _mark_link is set non-null. | |
1940 // Even if it is the end of the linked list, it will have a non-null link value, | |
1941 // as long as it is on the list. | |
1942 // This code must be MP safe, because it is used from parallel GC passes. | |
1943 bool nmethod::test_set_oops_do_mark() { | |
1944 assert(nmethod::oops_do_marking_is_active(), "oops_do_marking_prologue must be called"); | |
1945 nmethod* observed_mark_link = _oops_do_mark_link; | |
1946 if (observed_mark_link == NULL) { | |
1947 // Claim this nmethod for this thread to mark. | |
1948 observed_mark_link = (nmethod*) | |
1949 Atomic::cmpxchg_ptr(NMETHOD_SENTINEL, &_oops_do_mark_link, NULL); | |
1950 if (observed_mark_link == NULL) { | |
1951 | |
1952 // Atomically append this nmethod (now claimed) to the head of the list: | |
1953 nmethod* observed_mark_nmethods = _oops_do_mark_nmethods; | |
1954 for (;;) { | |
1955 nmethod* required_mark_nmethods = observed_mark_nmethods; | |
1956 _oops_do_mark_link = required_mark_nmethods; | |
1957 observed_mark_nmethods = (nmethod*) | |
1958 Atomic::cmpxchg_ptr(this, &_oops_do_mark_nmethods, required_mark_nmethods); | |
1959 if (observed_mark_nmethods == required_mark_nmethods) | |
1960 break; | |
1961 } | |
1962 // Mark was clear when we first saw this guy. | |
1963 NOT_PRODUCT(if (TraceScavenge) print_on(tty, "oops_do, mark")); | |
1964 return false; | |
1965 } | |
1966 } | |
1967 // On fall through, another racing thread marked this nmethod before we did. | |
1968 return true; | |
1969 } | |
1970 | |
1971 void nmethod::oops_do_marking_prologue() { | |
1972 NOT_PRODUCT(if (TraceScavenge) tty->print_cr("[oops_do_marking_prologue")); | |
1973 assert(_oops_do_mark_nmethods == NULL, "must not call oops_do_marking_prologue twice in a row"); | |
1974 // We use cmpxchg_ptr instead of regular assignment here because the user | |
1975 // may fork a bunch of threads, and we need them all to see the same state. | |
1976 void* observed = Atomic::cmpxchg_ptr(NMETHOD_SENTINEL, &_oops_do_mark_nmethods, NULL); | |
1977 guarantee(observed == NULL, "no races in this sequential code"); | |
1978 } | |
1979 | |
1980 void nmethod::oops_do_marking_epilogue() { | |
1981 assert(_oops_do_mark_nmethods != NULL, "must not call oops_do_marking_epilogue twice in a row"); | |
1982 nmethod* cur = _oops_do_mark_nmethods; | |
1983 while (cur != NMETHOD_SENTINEL) { | |
1984 assert(cur != NULL, "not NULL-terminated"); | |
1985 nmethod* next = cur->_oops_do_mark_link; | |
1986 cur->_oops_do_mark_link = NULL; | |
1987 cur->fix_oop_relocations(); | |
1988 NOT_PRODUCT(if (TraceScavenge) cur->print_on(tty, "oops_do, unmark")); | |
1989 cur = next; | |
1990 } | |
1991 void* required = _oops_do_mark_nmethods; | |
1992 void* observed = Atomic::cmpxchg_ptr(NULL, &_oops_do_mark_nmethods, required); | |
1993 guarantee(observed == required, "no races in this sequential code"); | |
1994 NOT_PRODUCT(if (TraceScavenge) tty->print_cr("oops_do_marking_epilogue]")); | |
1995 } | |
1996 | |
1997 class DetectScavengeRoot: public OopClosure { | |
1998 bool _detected_scavenge_root; | |
1999 public: | |
2000 DetectScavengeRoot() : _detected_scavenge_root(false) | |
2001 { NOT_PRODUCT(_print_nm = NULL); } | |
2002 bool detected_scavenge_root() { return _detected_scavenge_root; } | |
2003 virtual void do_oop(oop* p) { | |
2004 if ((*p) != NULL && (*p)->is_scavengable()) { | |
2005 NOT_PRODUCT(maybe_print(p)); | |
2006 _detected_scavenge_root = true; | |
2007 } | |
2008 } | |
2009 virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); } | |
2010 | |
2011 #ifndef PRODUCT | |
2012 nmethod* _print_nm; | |
2013 void maybe_print(oop* p) { | |
2014 if (_print_nm == NULL) return; | |
2015 if (!_detected_scavenge_root) _print_nm->print_on(tty, "new scavenge root"); | |
2016 tty->print_cr(""PTR_FORMAT"[offset=%d] detected scavengable oop "PTR_FORMAT" (found at "PTR_FORMAT")", | |
2017 _print_nm, (int)((intptr_t)p - (intptr_t)_print_nm), | |
2018 (intptr_t)(*p), (intptr_t)p); | |
2019 (*p)->print(); | |
2020 } | |
2021 #endif //PRODUCT | |
2022 }; | |
2023 | |
2024 bool nmethod::detect_scavenge_root_oops() { | |
2025 DetectScavengeRoot detect_scavenge_root; | |
2026 NOT_PRODUCT(if (TraceScavenge) detect_scavenge_root._print_nm = this); | |
2027 oops_do(&detect_scavenge_root); | |
2028 return detect_scavenge_root.detected_scavenge_root(); | |
2029 } | |
2030 | |
2031 // Method that knows how to preserve outgoing arguments at call. This method must be | |
2032 // called with a frame corresponding to a Java invoke | |
2033 void nmethod::preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f) { | |
2034 #ifndef SHARK | |
2035 if (!method()->is_native()) { | |
2036 SimpleScopeDesc ssd(this, fr.pc()); | |
2037 Bytecode_invoke call(ssd.method(), ssd.bci()); | |
2038 // compiled invokedynamic call sites have an implicit receiver at | |
2039 // resolution time, so make sure it gets GC'ed. | |
2040 bool has_receiver = !call.is_invokestatic(); | |
2041 Symbol* signature = call.signature(); | |
2042 fr.oops_compiled_arguments_do(signature, has_receiver, reg_map, f); | |
2043 } | |
2044 #endif // !SHARK | |
2045 } | |
2046 | |
2047 | |
2048 oop nmethod::embeddedOop_at(u_char* p) { | |
2049 RelocIterator iter(this, p, p + 1); | |
2050 while (iter.next()) | |
2051 if (iter.type() == relocInfo::oop_type) { | |
2052 return iter.oop_reloc()->oop_value(); | |
2053 } | |
2054 return NULL; | |
2055 } | |
2056 | |
2057 | |
2058 inline bool includes(void* p, void* from, void* to) { | |
2059 return from <= p && p < to; | |
2060 } | |
2061 | |
2062 | |
2063 void nmethod::copy_scopes_pcs(PcDesc* pcs, int count) { | |
2064 assert(count >= 2, "must be sentinel values, at least"); | |
2065 | |
2066 #ifdef ASSERT | |
2067 // must be sorted and unique; we do a binary search in find_pc_desc() | |
2068 int prev_offset = pcs[0].pc_offset(); | |
2069 assert(prev_offset == PcDesc::lower_offset_limit, | |
2070 "must start with a sentinel"); | |
2071 for (int i = 1; i < count; i++) { | |
2072 int this_offset = pcs[i].pc_offset(); | |
2073 assert(this_offset > prev_offset, "offsets must be sorted"); | |
2074 prev_offset = this_offset; | |
2075 } | |
2076 assert(prev_offset == PcDesc::upper_offset_limit, | |
2077 "must end with a sentinel"); | |
2078 #endif //ASSERT | |
2079 | |
2080 // Search for MethodHandle invokes and tag the nmethod. | |
2081 for (int i = 0; i < count; i++) { | |
2082 if (pcs[i].is_method_handle_invoke()) { | |
2083 set_has_method_handle_invokes(true); | |
2084 break; | |
2085 } | |
2086 } | |
2087 assert(has_method_handle_invokes() == (_deoptimize_mh_offset != -1), "must have deopt mh handler"); | |
2088 | |
2089 int size = count * sizeof(PcDesc); | |
2090 assert(scopes_pcs_size() >= size, "oob"); | |
2091 memcpy(scopes_pcs_begin(), pcs, size); | |
2092 | |
2093 // Adjust the final sentinel downward. | |
2094 PcDesc* last_pc = &scopes_pcs_begin()[count-1]; | |
2095 assert(last_pc->pc_offset() == PcDesc::upper_offset_limit, "sanity"); | |
2096 last_pc->set_pc_offset(content_size() + 1); | |
2097 for (; last_pc + 1 < scopes_pcs_end(); last_pc += 1) { | |
2098 // Fill any rounding gaps with copies of the last record. | |
2099 last_pc[1] = last_pc[0]; | |
2100 } | |
2101 // The following assert could fail if sizeof(PcDesc) is not | |
2102 // an integral multiple of oopSize (the rounding term). | |
2103 // If it fails, change the logic to always allocate a multiple | |
2104 // of sizeof(PcDesc), and fill unused words with copies of *last_pc. | |
2105 assert(last_pc + 1 == scopes_pcs_end(), "must match exactly"); | |
2106 } | |
2107 | |
2108 void nmethod::copy_scopes_data(u_char* buffer, int size) { | |
2109 assert(scopes_data_size() >= size, "oob"); | |
2110 memcpy(scopes_data_begin(), buffer, size); | |
2111 } | |
2112 | |
2113 | |
2114 #ifdef ASSERT | |
2115 static PcDesc* linear_search(nmethod* nm, int pc_offset, bool approximate) { | |
2116 PcDesc* lower = nm->scopes_pcs_begin(); | |
2117 PcDesc* upper = nm->scopes_pcs_end(); | |
2118 lower += 1; // exclude initial sentinel | |
2119 PcDesc* res = NULL; | |
2120 for (PcDesc* p = lower; p < upper; p++) { | |
2121 NOT_PRODUCT(--nmethod_stats.pc_desc_tests); // don't count this call to match_desc | |
2122 if (match_desc(p, pc_offset, approximate)) { | |
2123 if (res == NULL) | |
2124 res = p; | |
2125 else | |
2126 res = (PcDesc*) badAddress; | |
2127 } | |
2128 } | |
2129 return res; | |
2130 } | |
2131 #endif | |
2132 | |
2133 | |
2134 // Finds a PcDesc with real-pc equal to "pc" | |
2135 PcDesc* nmethod::find_pc_desc_internal(address pc, bool approximate) { | |
2136 address base_address = code_begin(); | |
2137 if ((pc < base_address) || | |
2138 (pc - base_address) >= (ptrdiff_t) PcDesc::upper_offset_limit) { | |
2139 return NULL; // PC is wildly out of range | |
2140 } | |
2141 int pc_offset = (int) (pc - base_address); | |
2142 | |
2143 // Check the PcDesc cache if it contains the desired PcDesc | |
2144 // (This as an almost 100% hit rate.) | |
2145 PcDesc* res = _pc_desc_cache.find_pc_desc(pc_offset, approximate); | |
2146 if (res != NULL) { | |
2147 assert(res == linear_search(this, pc_offset, approximate), "cache ok"); | |
2148 return res; | |
2149 } | |
2150 | |
2151 // Fallback algorithm: quasi-linear search for the PcDesc | |
2152 // Find the last pc_offset less than the given offset. | |
2153 // The successor must be the required match, if there is a match at all. | |
2154 // (Use a fixed radix to avoid expensive affine pointer arithmetic.) | |
2155 PcDesc* lower = scopes_pcs_begin(); | |
2156 PcDesc* upper = scopes_pcs_end(); | |
2157 upper -= 1; // exclude final sentinel | |
2158 if (lower >= upper) return NULL; // native method; no PcDescs at all | |
2159 | |
2160 #define assert_LU_OK \ | |
2161 /* invariant on lower..upper during the following search: */ \ | |
2162 assert(lower->pc_offset() < pc_offset, "sanity"); \ | |
2163 assert(upper->pc_offset() >= pc_offset, "sanity") | |
2164 assert_LU_OK; | |
2165 | |
2166 // Use the last successful return as a split point. | |
2167 PcDesc* mid = _pc_desc_cache.last_pc_desc(); | |
2168 NOT_PRODUCT(++nmethod_stats.pc_desc_searches); | |
2169 if (mid->pc_offset() < pc_offset) { | |
2170 lower = mid; | |
2171 } else { | |
2172 upper = mid; | |
2173 } | |
2174 | |
2175 // Take giant steps at first (4096, then 256, then 16, then 1) | |
2176 const int LOG2_RADIX = 4 /*smaller steps in debug mode:*/ debug_only(-1); | |
2177 const int RADIX = (1 << LOG2_RADIX); | |
2178 for (int step = (1 << (LOG2_RADIX*3)); step > 1; step >>= LOG2_RADIX) { | |
2179 while ((mid = lower + step) < upper) { | |
2180 assert_LU_OK; | |
2181 NOT_PRODUCT(++nmethod_stats.pc_desc_searches); | |
2182 if (mid->pc_offset() < pc_offset) { | |
2183 lower = mid; | |
2184 } else { | |
2185 upper = mid; | |
2186 break; | |
2187 } | |
2188 } | |
2189 assert_LU_OK; | |
2190 } | |
2191 | |
2192 // Sneak up on the value with a linear search of length ~16. | |
2193 while (true) { | |
2194 assert_LU_OK; | |
2195 mid = lower + 1; | |
2196 NOT_PRODUCT(++nmethod_stats.pc_desc_searches); | |
2197 if (mid->pc_offset() < pc_offset) { | |
2198 lower = mid; | |
2199 } else { | |
2200 upper = mid; | |
2201 break; | |
2202 } | |
2203 } | |
2204 #undef assert_LU_OK | |
2205 | |
2206 if (match_desc(upper, pc_offset, approximate)) { | |
2207 assert(upper == linear_search(this, pc_offset, approximate), "search ok"); | |
2208 _pc_desc_cache.add_pc_desc(upper); | |
2209 return upper; | |
2210 } else { | |
2211 assert(NULL == linear_search(this, pc_offset, approximate), "search ok"); | |
2212 return NULL; | |
2213 } | |
2214 } | |
2215 | |
2216 | |
2217 bool nmethod::check_all_dependencies() { | |
2218 bool found_check = false; | |
2219 // wholesale check of all dependencies | |
2220 for (Dependencies::DepStream deps(this); deps.next(); ) { | |
2221 if (deps.check_dependency() != NULL) { | |
2222 found_check = true; | |
2223 NOT_DEBUG(break); | |
2224 } | |
2225 } | |
2226 return found_check; // tell caller if we found anything | |
2227 } | |
2228 | |
2229 bool nmethod::check_dependency_on(DepChange& changes) { | |
2230 // What has happened: | |
2231 // 1) a new class dependee has been added | |
2232 // 2) dependee and all its super classes have been marked | |
2233 bool found_check = false; // set true if we are upset | |
2234 for (Dependencies::DepStream deps(this); deps.next(); ) { | |
2235 // Evaluate only relevant dependencies. | |
2236 if (deps.spot_check_dependency_at(changes) != NULL) { | |
2237 found_check = true; | |
2238 NOT_DEBUG(break); | |
2239 } | |
2240 } | |
2241 return found_check; | |
2242 } | |
2243 | |
2244 bool nmethod::is_evol_dependent_on(Klass* dependee) { | |
2245 InstanceKlass *dependee_ik = InstanceKlass::cast(dependee); | |
2246 Array<Method*>* dependee_methods = dependee_ik->methods(); | |
2247 for (Dependencies::DepStream deps(this); deps.next(); ) { | |
2248 if (deps.type() == Dependencies::evol_method) { | |
2249 Method* method = deps.method_argument(0); | |
2250 for (int j = 0; j < dependee_methods->length(); j++) { | |
2251 if (dependee_methods->at(j) == method) { | |
2252 // RC_TRACE macro has an embedded ResourceMark | |
2253 RC_TRACE(0x01000000, | |
2254 ("Found evol dependency of nmethod %s.%s(%s) compile_id=%d on method %s.%s(%s)", | |
2255 _method->method_holder()->external_name(), | |
2256 _method->name()->as_C_string(), | |
2257 _method->signature()->as_C_string(), compile_id(), | |
2258 method->method_holder()->external_name(), | |
2259 method->name()->as_C_string(), | |
2260 method->signature()->as_C_string())); | |
2261 if (TraceDependencies || LogCompilation) | |
2262 deps.log_dependency(dependee); | |
2263 return true; | |
2264 } | |
2265 } | |
2266 } | |
2267 } | |
2268 return false; | |
2269 } | |
2270 | |
2271 // Called from mark_for_deoptimization, when dependee is invalidated. | |
2272 bool nmethod::is_dependent_on_method(Method* dependee) { | |
2273 for (Dependencies::DepStream deps(this); deps.next(); ) { | |
2274 if (deps.type() != Dependencies::evol_method) | |
2275 continue; | |
2276 Method* method = deps.method_argument(0); | |
2277 if (method == dependee) return true; | |
2278 } | |
2279 return false; | |
2280 } | |
2281 | |
2282 | |
2283 bool nmethod::is_patchable_at(address instr_addr) { | |
2284 assert(insts_contains(instr_addr), "wrong nmethod used"); | |
2285 if (is_zombie()) { | |
2286 // a zombie may never be patched | |
2287 return false; | |
2288 } | |
2289 return true; | |
2290 } | |
2291 | |
2292 | |
2293 address nmethod::continuation_for_implicit_exception(address pc) { | |
2294 // Exception happened outside inline-cache check code => we are inside | |
2295 // an active nmethod => use cpc to determine a return address | |
2296 int exception_offset = pc - code_begin(); | |
2297 int cont_offset = ImplicitExceptionTable(this).at( exception_offset ); | |
2298 #ifdef ASSERT | |
2299 if (cont_offset == 0) { | |
2300 Thread* thread = ThreadLocalStorage::get_thread_slow(); | |
2301 ResetNoHandleMark rnm; // Might be called from LEAF/QUICK ENTRY | |
2302 HandleMark hm(thread); | |
2303 ResourceMark rm(thread); | |
2304 CodeBlob* cb = CodeCache::find_blob(pc); | |
2305 assert(cb != NULL && cb == this, ""); | |
2306 tty->print_cr("implicit exception happened at " INTPTR_FORMAT, pc); | |
2307 print(); | |
2308 method()->print_codes(); | |
2309 print_code(); | |
2310 print_pcs(); | |
2311 } | |
2312 #endif | |
2313 if (cont_offset == 0) { | |
2314 // Let the normal error handling report the exception | |
2315 return NULL; | |
2316 } | |
2317 return code_begin() + cont_offset; | |
2318 } | |
2319 | |
2320 | |
2321 | |
2322 void nmethod_init() { | |
2323 // make sure you didn't forget to adjust the filler fields | |
2324 assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word"); | |
2325 } | |
2326 | |
2327 | |
2328 //------------------------------------------------------------------------------------------- | |
2329 | |
2330 | |
2331 // QQQ might we make this work from a frame?? | |
2332 nmethodLocker::nmethodLocker(address pc) { | |
2333 CodeBlob* cb = CodeCache::find_blob(pc); | |
2334 guarantee(cb != NULL && cb->is_nmethod(), "bad pc for a nmethod found"); | |
2335 _nm = (nmethod*)cb; | |
2336 lock_nmethod(_nm); | |
2337 } | |
2338 | |
2339 // Only JvmtiDeferredEvent::compiled_method_unload_event() | |
2340 // should pass zombie_ok == true. | |
2341 void nmethodLocker::lock_nmethod(nmethod* nm, bool zombie_ok) { | |
2342 if (nm == NULL) return; | |
2343 Atomic::inc(&nm->_lock_count); | |
2344 guarantee(zombie_ok || !nm->is_zombie(), "cannot lock a zombie method"); | |
2345 } | |
2346 | |
2347 void nmethodLocker::unlock_nmethod(nmethod* nm) { | |
2348 if (nm == NULL) return; | |
2349 Atomic::dec(&nm->_lock_count); | |
2350 guarantee(nm->_lock_count >= 0, "unmatched nmethod lock/unlock"); | |
2351 } | |
2352 | |
2353 | |
2354 // ----------------------------------------------------------------------------- | |
2355 // nmethod::get_deopt_original_pc | |
2356 // | |
2357 // Return the original PC for the given PC if: | |
2358 // (a) the given PC belongs to a nmethod and | |
2359 // (b) it is a deopt PC | |
2360 address nmethod::get_deopt_original_pc(const frame* fr) { | |
2361 if (fr->cb() == NULL) return NULL; | |
2362 | |
2363 nmethod* nm = fr->cb()->as_nmethod_or_null(); | |
2364 if (nm != NULL && nm->is_deopt_pc(fr->pc())) | |
2365 return nm->get_original_pc(fr); | |
2366 | |
2367 return NULL; | |
2368 } | |
2369 | |
2370 | |
2371 // ----------------------------------------------------------------------------- | |
2372 // MethodHandle | |
2373 | |
2374 bool nmethod::is_method_handle_return(address return_pc) { | |
2375 if (!has_method_handle_invokes()) return false; | |
2376 PcDesc* pd = pc_desc_at(return_pc); | |
2377 if (pd == NULL) | |
2378 return false; | |
2379 return pd->is_method_handle_invoke(); | |
2380 } | |
2381 | |
2382 | |
2383 // ----------------------------------------------------------------------------- | |
2384 // Verification | |
2385 | |
2386 class VerifyOopsClosure: public OopClosure { | |
2387 nmethod* _nm; | |
2388 bool _ok; | |
2389 public: | |
2390 VerifyOopsClosure(nmethod* nm) : _nm(nm), _ok(true) { } | |
2391 bool ok() { return _ok; } | |
2392 virtual void do_oop(oop* p) { | |
2393 if ((*p) == NULL || (*p)->is_oop()) return; | |
2394 if (_ok) { | |
2395 _nm->print_nmethod(true); | |
2396 _ok = false; | |
2397 } | |
2398 tty->print_cr("*** non-oop "PTR_FORMAT" found at "PTR_FORMAT" (offset %d)", | |
2399 (intptr_t)(*p), (intptr_t)p, (int)((intptr_t)p - (intptr_t)_nm)); | |
2400 } | |
2401 virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); } | |
2402 }; | |
2403 | |
2404 void nmethod::verify() { | |
2405 | |
2406 // Hmm. OSR methods can be deopted but not marked as zombie or not_entrant | |
2407 // seems odd. | |
2408 | |
2409 if( is_zombie() || is_not_entrant() ) | |
2410 return; | |
2411 | |
2412 // Make sure all the entry points are correctly aligned for patching. | |
2413 NativeJump::check_verified_entry_alignment(entry_point(), verified_entry_point()); | |
2414 | |
2415 // assert(method()->is_oop(), "must be valid"); | |
2416 | |
2417 ResourceMark rm; | |
2418 | |
2419 if (!CodeCache::contains(this)) { | |
2420 fatal(err_msg("nmethod at " INTPTR_FORMAT " not in zone", this)); | |
2421 } | |
2422 | |
2423 if(is_native_method() ) | |
2424 return; | |
2425 | |
2426 nmethod* nm = CodeCache::find_nmethod(verified_entry_point()); | |
2427 if (nm != this) { | |
2428 fatal(err_msg("findNMethod did not find this nmethod (" INTPTR_FORMAT ")", | |
2429 this)); | |
2430 } | |
2431 | |
2432 for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) { | |
2433 if (! p->verify(this)) { | |
2434 tty->print_cr("\t\tin nmethod at " INTPTR_FORMAT " (pcs)", this); | |
2435 } | |
2436 } | |
2437 | |
2438 VerifyOopsClosure voc(this); | |
2439 oops_do(&voc); | |
2440 assert(voc.ok(), "embedded oops must be OK"); | |
2441 verify_scavenge_root_oops(); | |
2442 | |
2443 verify_scopes(); | |
2444 } | |
2445 | |
2446 | |
2447 void nmethod::verify_interrupt_point(address call_site) { | |
2448 // This code does not work in release mode since | |
2449 // owns_lock only is available in debug mode. | |
2450 CompiledIC* ic = NULL; | |
2451 Thread *cur = Thread::current(); | |
2452 if (CompiledIC_lock->owner() == cur || | |
2453 ((cur->is_VM_thread() || cur->is_ConcurrentGC_thread()) && | |
2454 SafepointSynchronize::is_at_safepoint())) { | |
2455 ic = CompiledIC_at(this, call_site); | |
2456 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); | |
2457 } else { | |
2458 MutexLocker ml_verify (CompiledIC_lock); | |
2459 ic = CompiledIC_at(this, call_site); | |
2460 } | |
2461 PcDesc* pd = pc_desc_at(ic->end_of_call()); | |
2462 assert(pd != NULL, "PcDesc must exist"); | |
2463 for (ScopeDesc* sd = new ScopeDesc(this, pd->scope_decode_offset(), | |
2464 pd->obj_decode_offset(), pd->should_reexecute(), pd->rethrow_exception(), | |
2465 pd->return_oop()); | |
2466 !sd->is_top(); sd = sd->sender()) { | |
2467 sd->verify(); | |
2468 } | |
2469 } | |
2470 | |
2471 void nmethod::verify_scopes() { | |
2472 if( !method() ) return; // Runtime stubs have no scope | |
2473 if (method()->is_native()) return; // Ignore stub methods. | |
2474 // iterate through all interrupt point | |
2475 // and verify the debug information is valid. | |
2476 RelocIterator iter((nmethod*)this); | |
2477 while (iter.next()) { | |
2478 address stub = NULL; | |
2479 switch (iter.type()) { | |
2480 case relocInfo::virtual_call_type: | |
2481 verify_interrupt_point(iter.addr()); | |
2482 break; | |
2483 case relocInfo::opt_virtual_call_type: | |
2484 stub = iter.opt_virtual_call_reloc()->static_stub(); | |
2485 verify_interrupt_point(iter.addr()); | |
2486 break; | |
2487 case relocInfo::static_call_type: | |
2488 stub = iter.static_call_reloc()->static_stub(); | |
2489 //verify_interrupt_point(iter.addr()); | |
2490 break; | |
2491 case relocInfo::runtime_call_type: | |
2492 address destination = iter.reloc()->value(); | |
2493 // Right now there is no way to find out which entries support | |
2494 // an interrupt point. It would be nice if we had this | |
2495 // information in a table. | |
2496 break; | |
2497 } | |
2498 #ifndef GRAAL | |
2499 assert(stub == NULL || stub_contains(stub), "static call stub outside stub section"); | |
2500 #endif | |
2501 } | |
2502 } | |
2503 | |
2504 | |
2505 // ----------------------------------------------------------------------------- | |
2506 // Non-product code | |
2507 #ifndef PRODUCT | |
2508 | |
2509 class DebugScavengeRoot: public OopClosure { | |
2510 nmethod* _nm; | |
2511 bool _ok; | |
2512 public: | |
2513 DebugScavengeRoot(nmethod* nm) : _nm(nm), _ok(true) { } | |
2514 bool ok() { return _ok; } | |
2515 virtual void do_oop(oop* p) { | |
2516 if ((*p) == NULL || !(*p)->is_scavengable()) return; | |
2517 if (_ok) { | |
2518 _nm->print_nmethod(true); | |
2519 _ok = false; | |
2520 } | |
2521 tty->print_cr("*** scavengable oop "PTR_FORMAT" found at "PTR_FORMAT" (offset %d)", | |
2522 (intptr_t)(*p), (intptr_t)p, (int)((intptr_t)p - (intptr_t)_nm)); | |
2523 (*p)->print(); | |
2524 } | |
2525 virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); } | |
2526 }; | |
2527 | |
2528 void nmethod::verify_scavenge_root_oops() { | |
2529 if (!on_scavenge_root_list()) { | |
2530 // Actually look inside, to verify the claim that it's clean. | |
2531 DebugScavengeRoot debug_scavenge_root(this); | |
2532 oops_do(&debug_scavenge_root); | |
2533 if (!debug_scavenge_root.ok()) | |
2534 fatal("found an unadvertised bad scavengable oop in the code cache"); | |
2535 } | |
2536 assert(scavenge_root_not_marked(), ""); | |
2537 } | |
2538 | |
2539 #endif // PRODUCT | |
2540 | |
2541 // Printing operations | |
2542 | |
2543 void nmethod::print() const { | |
2544 ResourceMark rm; | |
2545 ttyLocker ttyl; // keep the following output all in one block | |
2546 | |
2547 tty->print("Compiled method "); | |
2548 | |
2549 if (is_compiled_by_c1()) { | |
2550 tty->print("(c1) "); | |
2551 } else if (is_compiled_by_c2()) { | |
2552 tty->print("(c2) "); | |
2553 } else if (is_compiled_by_shark()) { | |
2554 tty->print("(shark) "); | |
2555 } else { | |
2556 tty->print("(nm) "); | |
2557 } | |
2558 | |
2559 print_on(tty, NULL); | |
2560 | |
2561 if (WizardMode) { | |
2562 tty->print("((nmethod*) "INTPTR_FORMAT ") ", this); | |
2563 tty->print(" for method " INTPTR_FORMAT , (address)method()); | |
2564 tty->print(" { "); | |
2565 if (is_in_use()) tty->print("in_use "); | |
2566 if (is_not_entrant()) tty->print("not_entrant "); | |
2567 if (is_zombie()) tty->print("zombie "); | |
2568 if (is_unloaded()) tty->print("unloaded "); | |
2569 if (on_scavenge_root_list()) tty->print("scavenge_root "); | |
2570 tty->print_cr("}:"); | |
2571 } | |
2572 if (size () > 0) tty->print_cr(" total in heap [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", | |
2573 (address)this, | |
2574 (address)this + size(), | |
2575 size()); | |
2576 if (relocation_size () > 0) tty->print_cr(" relocation [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", | |
2577 relocation_begin(), | |
2578 relocation_end(), | |
2579 relocation_size()); | |
2580 if (consts_size () > 0) tty->print_cr(" constants [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", | |
2581 consts_begin(), | |
2582 consts_end(), | |
2583 consts_size()); | |
2584 if (insts_size () > 0) tty->print_cr(" main code [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", | |
2585 insts_begin(), | |
2586 insts_end(), | |
2587 insts_size()); | |
2588 if (stub_size () > 0) tty->print_cr(" stub code [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", | |
2589 stub_begin(), | |
2590 stub_end(), | |
2591 stub_size()); | |
2592 if (oops_size () > 0) tty->print_cr(" oops [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", | |
2593 oops_begin(), | |
2594 oops_end(), | |
2595 oops_size()); | |
2596 if (metadata_size () > 0) tty->print_cr(" metadata [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", | |
2597 metadata_begin(), | |
2598 metadata_end(), | |
2599 metadata_size()); | |
2600 if (scopes_data_size () > 0) tty->print_cr(" scopes data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", | |
2601 scopes_data_begin(), | |
2602 scopes_data_end(), | |
2603 scopes_data_size()); | |
2604 if (scopes_pcs_size () > 0) tty->print_cr(" scopes pcs [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", | |
2605 scopes_pcs_begin(), | |
2606 scopes_pcs_end(), | |
2607 scopes_pcs_size()); | |
2608 if (dependencies_size () > 0) tty->print_cr(" dependencies [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", | |
2609 dependencies_begin(), | |
2610 dependencies_end(), | |
2611 dependencies_size()); | |
2612 if (handler_table_size() > 0) tty->print_cr(" handler table [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", | |
2613 handler_table_begin(), | |
2614 handler_table_end(), | |
2615 handler_table_size()); | |
2616 if (nul_chk_table_size() > 0) tty->print_cr(" nul chk table [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d", | |
2617 nul_chk_table_begin(), | |
2618 nul_chk_table_end(), | |
2619 nul_chk_table_size()); | |
2620 } | |
2621 | |
2622 void nmethod::print_code() { | |
2623 HandleMark hm; | |
2624 ResourceMark m; | |
2625 Disassembler::decode(this); | |
2626 } | |
2627 | |
2628 | |
2629 #ifndef PRODUCT | |
2630 | |
2631 void nmethod::print_scopes() { | |
2632 // Find the first pc desc for all scopes in the code and print it. | |
2633 ResourceMark rm; | |
2634 for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) { | |
2635 if (p->scope_decode_offset() == DebugInformationRecorder::serialized_null) | |
2636 continue; | |
2637 | |
2638 ScopeDesc* sd = scope_desc_at(p->real_pc(this)); | |
2639 sd->print_on(tty, p); | |
2640 } | |
2641 } | |
2642 | |
2643 void nmethod::print_dependencies() { | |
2644 ResourceMark rm; | |
2645 ttyLocker ttyl; // keep the following output all in one block | |
2646 tty->print_cr("Dependencies:"); | |
2647 for (Dependencies::DepStream deps(this); deps.next(); ) { | |
2648 deps.print_dependency(); | |
2649 Klass* ctxk = deps.context_type(); | |
2650 if (ctxk != NULL) { | |
2651 if (ctxk->oop_is_instance() && ((InstanceKlass*)ctxk)->is_dependent_nmethod(this)) { | |
2652 tty->print_cr(" [nmethod<=klass]%s", ctxk->external_name()); | |
2653 } | |
2654 } | |
2655 deps.log_dependency(); // put it into the xml log also | |
2656 } | |
2657 } | |
2658 | |
2659 | |
2660 void nmethod::print_relocations() { | |
2661 ResourceMark m; // in case methods get printed via the debugger | |
2662 tty->print_cr("relocations:"); | |
2663 RelocIterator iter(this); | |
2664 iter.print(); | |
2665 if (UseRelocIndex) { | |
2666 jint* index_end = (jint*)relocation_end() - 1; | |
2667 jint index_size = *index_end; | |
2668 jint* index_start = (jint*)( (address)index_end - index_size ); | |
2669 tty->print_cr(" index @" INTPTR_FORMAT ": index_size=%d", index_start, index_size); | |
2670 if (index_size > 0) { | |
2671 jint* ip; | |
2672 for (ip = index_start; ip+2 <= index_end; ip += 2) | |
2673 tty->print_cr(" (%d %d) addr=" INTPTR_FORMAT " @" INTPTR_FORMAT, | |
2674 ip[0], | |
2675 ip[1], | |
2676 header_end()+ip[0], | |
2677 relocation_begin()-1+ip[1]); | |
2678 for (; ip < index_end; ip++) | |
2679 tty->print_cr(" (%d ?)", ip[0]); | |
2680 tty->print_cr(" @" INTPTR_FORMAT ": index_size=%d", ip, *ip++); | |
2681 tty->print_cr("reloc_end @" INTPTR_FORMAT ":", ip); | |
2682 } | |
2683 } | |
2684 } | |
2685 | |
2686 | |
2687 void nmethod::print_pcs() { | |
2688 ResourceMark m; // in case methods get printed via debugger | |
2689 tty->print_cr("pc-bytecode offsets:"); | |
2690 for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) { | |
2691 p->print(this); | |
2692 } | |
2693 } | |
2694 | |
2695 #endif // PRODUCT | |
2696 | |
2697 const char* nmethod::reloc_string_for(u_char* begin, u_char* end) { | |
2698 RelocIterator iter(this, begin, end); | |
2699 bool have_one = false; | |
2700 while (iter.next()) { | |
2701 have_one = true; | |
2702 switch (iter.type()) { | |
2703 case relocInfo::none: return "no_reloc"; | |
2704 case relocInfo::oop_type: { | |
2705 stringStream st; | |
2706 oop_Relocation* r = iter.oop_reloc(); | |
2707 oop obj = r->oop_value(); | |
2708 st.print("oop("); | |
2709 if (obj == NULL) st.print("NULL"); | |
2710 else obj->print_value_on(&st); | |
2711 st.print(")"); | |
2712 return st.as_string(); | |
2713 } | |
2714 case relocInfo::metadata_type: { | |
2715 stringStream st; | |
2716 metadata_Relocation* r = iter.metadata_reloc(); | |
2717 Metadata* obj = r->metadata_value(); | |
2718 st.print("metadata("); | |
2719 if (obj == NULL) st.print("NULL"); | |
2720 else obj->print_value_on(&st); | |
2721 st.print(")"); | |
2722 return st.as_string(); | |
2723 } | |
2724 case relocInfo::virtual_call_type: return "virtual_call"; | |
2725 case relocInfo::opt_virtual_call_type: return "optimized virtual_call"; | |
2726 case relocInfo::static_call_type: return "static_call"; | |
2727 case relocInfo::static_stub_type: return "static_stub"; | |
2728 case relocInfo::runtime_call_type: return "runtime_call"; | |
2729 case relocInfo::external_word_type: return "external_word"; | |
2730 case relocInfo::internal_word_type: return "internal_word"; | |
2731 case relocInfo::section_word_type: return "section_word"; | |
2732 case relocInfo::poll_type: return "poll"; | |
2733 case relocInfo::poll_return_type: return "poll_return"; | |
2734 case relocInfo::type_mask: return "type_bit_mask"; | |
2735 } | |
2736 } | |
2737 return have_one ? "other" : NULL; | |
2738 } | |
2739 | |
2740 // Return a the last scope in (begin..end] | |
2741 ScopeDesc* nmethod::scope_desc_in(address begin, address end) { | |
2742 PcDesc* p = pc_desc_near(begin+1); | |
2743 if (p != NULL && p->real_pc(this) <= end) { | |
2744 return new ScopeDesc(this, p->scope_decode_offset(), | |
2745 p->obj_decode_offset(), p->should_reexecute(), p->rethrow_exception(), | |
2746 p->return_oop()); | |
2747 } | |
2748 return NULL; | |
2749 } | |
2750 | |
2751 void nmethod::print_nmethod_labels(outputStream* stream, address block_begin) const { | |
2752 if (block_begin == entry_point()) stream->print_cr("[Entry Point]"); | |
2753 if (block_begin == verified_entry_point()) stream->print_cr("[Verified Entry Point]"); | |
2754 if (GRAAL_ONLY(_exception_offset >= 0 &&) block_begin == exception_begin()) stream->print_cr("[Exception Handler]"); | |
2755 if (block_begin == stub_begin()) stream->print_cr("[Stub Code]"); | |
2756 if (GRAAL_ONLY(_deoptimize_offset >= 0 &&) block_begin == deopt_handler_begin()) stream->print_cr("[Deopt Handler Code]"); | |
2757 | |
2758 if (has_method_handle_invokes()) | |
2759 if (block_begin == deopt_mh_handler_begin()) stream->print_cr("[Deopt MH Handler Code]"); | |
2760 | |
2761 if (block_begin == consts_begin()) stream->print_cr("[Constants]"); | |
2762 | |
2763 if (block_begin == entry_point()) { | |
2764 methodHandle m = method(); | |
2765 if (m.not_null()) { | |
2766 stream->print(" # "); | |
2767 m->print_value_on(stream); | |
2768 stream->cr(); | |
2769 } | |
2770 if (m.not_null() && !is_osr_method()) { | |
2771 ResourceMark rm; | |
2772 int sizeargs = m->size_of_parameters(); | |
2773 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs); | |
2774 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs); | |
2775 { | |
2776 int sig_index = 0; | |
2777 if (!m->is_static()) | |
2778 sig_bt[sig_index++] = T_OBJECT; // 'this' | |
2779 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ss.next()) { | |
2780 BasicType t = ss.type(); | |
2781 sig_bt[sig_index++] = t; | |
2782 if (type2size[t] == 2) { | |
2783 sig_bt[sig_index++] = T_VOID; | |
2784 } else { | |
2785 assert(type2size[t] == 1, "size is 1 or 2"); | |
2786 } | |
2787 } | |
2788 assert(sig_index == sizeargs, ""); | |
2789 } | |
2790 const char* spname = "sp"; // make arch-specific? | |
2791 intptr_t out_preserve = SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs, false); | |
2792 int stack_slot_offset = this->frame_size() * wordSize; | |
2793 int tab1 = 14, tab2 = 24; | |
2794 int sig_index = 0; | |
2795 int arg_index = (m->is_static() ? 0 : -1); | |
2796 bool did_old_sp = false; | |
2797 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ) { | |
2798 bool at_this = (arg_index == -1); | |
2799 bool at_old_sp = false; | |
2800 BasicType t = (at_this ? T_OBJECT : ss.type()); | |
2801 assert(t == sig_bt[sig_index], "sigs in sync"); | |
2802 if (at_this) | |
2803 stream->print(" # this: "); | |
2804 else | |
2805 stream->print(" # parm%d: ", arg_index); | |
2806 stream->move_to(tab1); | |
2807 VMReg fst = regs[sig_index].first(); | |
2808 VMReg snd = regs[sig_index].second(); | |
2809 if (fst->is_reg()) { | |
2810 stream->print("%s", fst->name()); | |
2811 if (snd->is_valid()) { | |
2812 stream->print(":%s", snd->name()); | |
2813 } | |
2814 } else if (fst->is_stack()) { | |
2815 int offset = fst->reg2stack() * VMRegImpl::stack_slot_size + stack_slot_offset; | |
2816 if (offset == stack_slot_offset) at_old_sp = true; | |
2817 stream->print("[%s+0x%x]", spname, offset); | |
2818 } else { | |
2819 stream->print("reg%d:%d??", (int)(intptr_t)fst, (int)(intptr_t)snd); | |
2820 } | |
2821 stream->print(" "); | |
2822 stream->move_to(tab2); | |
2823 stream->print("= "); | |
2824 if (at_this) { | |
2825 m->method_holder()->print_value_on(stream); | |
2826 } else { | |
2827 bool did_name = false; | |
2828 if (!at_this && ss.is_object()) { | |
2829 Symbol* name = ss.as_symbol_or_null(); | |
2830 if (name != NULL) { | |
2831 name->print_value_on(stream); | |
2832 did_name = true; | |
2833 } | |
2834 } | |
2835 if (!did_name) | |
2836 stream->print("%s", type2name(t)); | |
2837 } | |
2838 if (at_old_sp) { | |
2839 stream->print(" (%s of caller)", spname); | |
2840 did_old_sp = true; | |
2841 } | |
2842 stream->cr(); | |
2843 sig_index += type2size[t]; | |
2844 arg_index += 1; | |
2845 if (!at_this) ss.next(); | |
2846 } | |
2847 if (!did_old_sp) { | |
2848 stream->print(" # "); | |
2849 stream->move_to(tab1); | |
2850 stream->print("[%s+0x%x]", spname, stack_slot_offset); | |
2851 stream->print(" (%s of caller)", spname); | |
2852 stream->cr(); | |
2853 } | |
2854 } | |
2855 } | |
2856 } | |
2857 | |
2858 void nmethod::print_code_comment_on(outputStream* st, int column, u_char* begin, u_char* end) { | |
2859 // First, find an oopmap in (begin, end]. | |
2860 // We use the odd half-closed interval so that oop maps and scope descs | |
2861 // which are tied to the byte after a call are printed with the call itself. | |
2862 address base = code_begin(); | |
2863 OopMapSet* oms = oop_maps(); | |
2864 if (oms != NULL) { | |
2865 for (int i = 0, imax = oms->size(); i < imax; i++) { | |
2866 OopMap* om = oms->at(i); | |
2867 address pc = base + om->offset(); | |
2868 if (pc > begin) { | |
2869 if (pc <= end) { | |
2870 st->move_to(column); | |
2871 st->print("; "); | |
2872 om->print_on(st); | |
2873 } | |
2874 break; | |
2875 } | |
2876 } | |
2877 } | |
2878 | |
2879 // Print any debug info present at this pc. | |
2880 ScopeDesc* sd = scope_desc_in(begin, end); | |
2881 if (sd != NULL) { | |
2882 st->move_to(column); | |
2883 if (sd->bci() == SynchronizationEntryBCI) { | |
2884 st->print(";*synchronization entry"); | |
2885 } else { | |
2886 if (sd->method() == NULL) { | |
2887 st->print("method is NULL"); | |
2888 } else if (sd->method()->is_native()) { | |
2889 st->print("method is native"); | |
2890 } else { | |
2891 Bytecodes::Code bc = sd->method()->java_code_at(sd->bci()); | |
2892 st->print(";*%s", Bytecodes::name(bc)); | |
2893 switch (bc) { | |
2894 case Bytecodes::_invokevirtual: | |
2895 case Bytecodes::_invokespecial: | |
2896 case Bytecodes::_invokestatic: | |
2897 case Bytecodes::_invokeinterface: | |
2898 { | |
2899 Bytecode_invoke invoke(sd->method(), sd->bci()); | |
2900 st->print(" "); | |
2901 if (invoke.name() != NULL) | |
2902 invoke.name()->print_symbol_on(st); | |
2903 else | |
2904 st->print("<UNKNOWN>"); | |
2905 break; | |
2906 } | |
2907 case Bytecodes::_getfield: | |
2908 case Bytecodes::_putfield: | |
2909 case Bytecodes::_getstatic: | |
2910 case Bytecodes::_putstatic: | |
2911 { | |
2912 Bytecode_field field(sd->method(), sd->bci()); | |
2913 st->print(" "); | |
2914 if (field.name() != NULL) | |
2915 field.name()->print_symbol_on(st); | |
2916 else | |
2917 st->print("<UNKNOWN>"); | |
2918 } | |
2919 } | |
2920 } | |
2921 } | |
2922 | |
2923 // Print all scopes | |
2924 for (;sd != NULL; sd = sd->sender()) { | |
2925 st->move_to(column); | |
2926 st->print("; -"); | |
2927 if (sd->method() == NULL) { | |
2928 st->print("method is NULL"); | |
2929 } else { | |
2930 sd->method()->print_short_name(st); | |
2931 } | |
2932 int lineno = sd->method()->line_number_from_bci(sd->bci()); | |
2933 if (lineno != -1) { | |
2934 st->print("@%d (line %d)", sd->bci(), lineno); | |
2935 } else { | |
2936 st->print("@%d", sd->bci()); | |
2937 } | |
2938 st->cr(); | |
2939 } | |
2940 } | |
2941 | |
2942 // Print relocation information | |
2943 const char* str = reloc_string_for(begin, end); | |
2944 if (str != NULL) { | |
2945 if (sd != NULL) st->cr(); | |
2946 st->move_to(column); | |
2947 st->print("; {%s}", str); | |
2948 } | |
2949 int cont_offset = ImplicitExceptionTable(this).at(begin - code_begin()); | |
2950 if (cont_offset != 0) { | |
2951 st->move_to(column); | |
2952 st->print("; implicit exception: dispatches to " INTPTR_FORMAT, code_begin() + cont_offset); | |
2953 } | |
2954 | |
2955 } | |
2956 | |
2957 #ifndef PRODUCT | |
2958 | |
2959 void nmethod::print_value_on(outputStream* st) const { | |
2960 st->print("nmethod"); | |
2961 print_on(st, NULL); | |
2962 } | |
2963 | |
2964 void nmethod::print_calls(outputStream* st) { | |
2965 RelocIterator iter(this); | |
2966 while (iter.next()) { | |
2967 switch (iter.type()) { | |
2968 case relocInfo::virtual_call_type: | |
2969 case relocInfo::opt_virtual_call_type: { | |
2970 VerifyMutexLocker mc(CompiledIC_lock); | |
2971 CompiledIC_at(iter.reloc())->print(); | |
2972 break; | |
2973 } | |
2974 case relocInfo::static_call_type: | |
2975 st->print_cr("Static call at " INTPTR_FORMAT, iter.reloc()->addr()); | |
2976 compiledStaticCall_at(iter.reloc())->print(); | |
2977 break; | |
2978 } | |
2979 } | |
2980 } | |
2981 | |
2982 void nmethod::print_handler_table() { | |
2983 ExceptionHandlerTable(this).print(); | |
2984 } | |
2985 | |
2986 void nmethod::print_nul_chk_table() { | |
2987 ImplicitExceptionTable(this).print(code_begin()); | |
2988 } | |
2989 | |
2990 #endif // PRODUCT | |
2991 | |
2992 void nmethod::print_statistics() { | |
2993 ttyLocker ttyl; | |
2994 if (xtty != NULL) xtty->head("statistics type='nmethod'"); | |
2995 nmethod_stats.print_native_nmethod_stats(); | |
2996 nmethod_stats.print_nmethod_stats(); | |
2997 DebugInformationRecorder::print_statistics(); | |
2998 nmethod_stats.print_pc_stats(); | |
2999 Dependencies::print_statistics(); | |
3000 if (xtty != NULL) xtty->tail("statistics"); | |
3001 } | |
3002 | |
3003 #ifdef GRAAL | |
3004 void DebugScopedNMethod::print_on(outputStream* st) { | |
3005 if (_nm != NULL) { | |
3006 st->print("nmethod@%p", _nm); | |
3007 Method* method = _nm->method(); | |
3008 if (method != NULL) { | |
3009 char holder[O_BUFLEN]; | |
3010 char nameAndSig[O_BUFLEN]; | |
3011 method->method_holder()->name()->as_C_string(holder, O_BUFLEN); | |
3012 method->name_and_sig_as_C_string(nameAndSig, O_BUFLEN); | |
3013 st->print(" - %s::%s", holder, nameAndSig); | |
3014 } | |
3015 } | |
3016 } | |
3017 #endif |