src/share/vm/code/nmethod.cpp

Thu, 24 May 2018 18:41:44 +0800

author
aoqi
date
Thu, 24 May 2018 18:41:44 +0800
changeset 8856
ac27a9c85bea
parent 8734
c73c5d205d0a
parent 8604
04d83ba48607
child 9041
95a08233f46c
permissions
-rw-r--r--

Merge

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

mercurial