src/share/vm/code/nmethod.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6680
78bbf4d43a14
parent 1
2d8a650513c2
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

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

mercurial