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