src/share/vm/code/nmethod.cpp

Thu, 24 May 2018 17:06:56 +0800

author
aoqi
date
Thu, 24 May 2018 17:06:56 +0800
changeset 8604
04d83ba48607
parent 8185
5cece4584b8e
parent 7535
7ae4e26cb1e0
child 8856
ac27a9c85bea
permissions
-rw-r--r--

Merge

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

mercurial