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