src/share/vm/code/nmethod.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 1
2d8a650513c2
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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

mercurial