< prev index next >

src/hotspot/share/oops/methodData.cpp

Print this page

   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"

  26 #include "ci/ciMethodData.hpp"
  27 #include "classfile/vmSymbols.hpp"
  28 #include "compiler/compilationPolicy.hpp"
  29 #include "compiler/compilerDefinitions.inline.hpp"
  30 #include "compiler/compilerOracle.hpp"
  31 #include "interpreter/bytecode.hpp"
  32 #include "interpreter/bytecodeStream.hpp"
  33 #include "interpreter/linkResolver.hpp"
  34 #include "memory/metaspaceClosure.hpp"
  35 #include "memory/resourceArea.hpp"
  36 #include "oops/klass.inline.hpp"
  37 #include "oops/methodData.inline.hpp"
  38 #include "prims/jvmtiRedefineClasses.hpp"
  39 #include "runtime/atomic.hpp"
  40 #include "runtime/deoptimization.hpp"
  41 #include "runtime/handles.inline.hpp"
  42 #include "runtime/orderAccess.hpp"
  43 #include "runtime/safepointVerifiers.hpp"
  44 #include "runtime/signature.hpp"
  45 #include "utilities/align.hpp"

 301 #ifdef ASSERT
 302     ResourceMark rm;
 303     ReferenceArgumentCount rac(inv.signature());
 304     int count = MIN2(rac.count(), (int)TypeProfileArgsLimit);
 305     assert(count > 0, "room for args type but none found?");
 306     check_number_of_arguments(count);
 307 #endif
 308     _args.post_initialize(inv.signature(), inv.has_receiver(), false);
 309   }
 310 
 311   if (has_return()) {
 312     assert(is_reference_type(inv.result_type()), "room for a ret type but doesn't return obj?");
 313     _ret.post_initialize();
 314   }
 315 }
 316 
 317 void TypeStackSlotEntries::clean_weak_klass_links(bool always_clean) {
 318   for (int i = 0; i < _number_of_entries; i++) {
 319     intptr_t p = type(i);
 320     Klass* k = (Klass*)klass_part(p);
 321     if (k != nullptr && (always_clean || !k->is_loader_alive())) {
 322       set_type(i, with_status((Klass*)nullptr, p));





 323     }
 324   }
 325 }
 326 









 327 void ReturnTypeEntry::clean_weak_klass_links(bool always_clean) {
 328   intptr_t p = type();
 329   Klass* k = (Klass*)klass_part(p);
 330   if (k != nullptr && (always_clean || !k->is_loader_alive())) {
 331     set_type(with_status((Klass*)nullptr, p));





 332   }
 333 }
 334 







 335 bool TypeEntriesAtCall::return_profiling_enabled() {
 336   return MethodData::profile_return();
 337 }
 338 
 339 bool TypeEntriesAtCall::arguments_profiling_enabled() {
 340   return MethodData::profile_arguments();
 341 }
 342 
 343 void TypeEntries::print_klass(outputStream* st, intptr_t k) {
 344   if (is_type_none(k)) {
 345     st->print("none");
 346   } else if (is_type_unknown(k)) {
 347     st->print("unknown");
 348   } else {
 349     valid_klass(k)->print_value_on(st);
 350   }
 351   if (was_null_seen(k)) {
 352     st->print(" (null seen)");
 353   }
 354 }

 390     _args.print_data_on(st);
 391   }
 392   if (has_return()) {
 393     tab(st, true);
 394     st->print("return type");
 395     _ret.print_data_on(st);
 396   }
 397 }
 398 
 399 // ==================================================================
 400 // ReceiverTypeData
 401 //
 402 // A ReceiverTypeData is used to access profiling information about a
 403 // dynamic type check.  It consists of a counter which counts the total times
 404 // that the check is reached, and a series of (Klass*, count) pairs
 405 // which are used to store a type profile for the receiver of the check.
 406 
 407 void ReceiverTypeData::clean_weak_klass_links(bool always_clean) {
 408     for (uint row = 0; row < row_limit(); row++) {
 409     Klass* p = receiver(row);
 410     if (p != nullptr && (always_clean || !p->is_loader_alive())) {
 411       clear_row(row);





 412     }
 413   }
 414 }
 415 







 416 void ReceiverTypeData::print_receiver_data_on(outputStream* st) const {
 417   uint row;
 418   int entries = 0;
 419   for (row = 0; row < row_limit(); row++) {
 420     if (receiver(row) != nullptr)  entries++;
 421   }
 422   st->print_cr("count(%u) entries(%u)", count(), entries);
 423   int total = count();
 424   for (row = 0; row < row_limit(); row++) {
 425     if (receiver(row) != nullptr) {
 426       total += receiver_count(row);
 427     }
 428   }
 429   for (row = 0; row < row_limit(); row++) {
 430     if (receiver(row) != nullptr) {
 431       tab(st);
 432       receiver(row)->print_value_on(st);
 433       st->print_cr("(%u %4.2f)", receiver_count(row), (float) receiver_count(row) / (float) total);
 434     }
 435   }

 624     return obj_args + 1; // 1 cell for array len
 625   }
 626   return 0;
 627 }
 628 
 629 void ParametersTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
 630   _parameters.post_initialize(mdo->method()->signature(), !mdo->method()->is_static(), true);
 631 }
 632 
 633 bool ParametersTypeData::profiling_enabled() {
 634   return MethodData::profile_parameters();
 635 }
 636 
 637 void ParametersTypeData::print_data_on(outputStream* st, const char* extra) const {
 638   print_shared(st, "ParametersTypeData", extra);
 639   tab(st);
 640   _parameters.print_data_on(st);
 641   st->cr();
 642 }
 643 





 644 void SpeculativeTrapData::print_data_on(outputStream* st, const char* extra) const {
 645   print_shared(st, "SpeculativeTrapData", extra);
 646   tab(st);
 647   method()->print_short_name(st);
 648   st->cr();
 649 }
 650 
 651 // ==================================================================
 652 // MethodData*
 653 //
 654 // A MethodData* holds information which has been collected about
 655 // a method.
 656 
 657 MethodData* MethodData::allocate(ClassLoaderData* loader_data, const methodHandle& method, TRAPS) {
 658   assert(!THREAD->owns_locks(), "Should not own any locks");
 659   int size = MethodData::compute_allocation_size_in_words(method);
 660 
 661   return new (loader_data, size, MetaspaceObj::MethodDataType, THREAD)
 662     MethodData(method);
 663 }

1201 
1202 // Give each of the data entries a chance to perform specific
1203 // data initialization.
1204 void MethodData::post_initialize(BytecodeStream* stream) {
1205   ResourceMark rm;
1206   ProfileData* data;
1207   for (data = first_data(); is_valid(data); data = next_data(data)) {
1208     stream->set_start(data->bci());
1209     stream->next();
1210     data->post_initialize(stream, this);
1211   }
1212   if (_parameters_type_data_di != no_parameters) {
1213     parameters_type_data()->post_initialize(nullptr, this);
1214   }
1215 }
1216 
1217 // Initialize the MethodData* corresponding to a given method.
1218 MethodData::MethodData(const methodHandle& method)
1219   : _method(method()),
1220     // Holds Compile_lock
1221     _extra_data_lock(Mutex::nosafepoint, "MDOExtraData_lock"),
1222     _compiler_counters(),
1223     _parameters_type_data_di(parameters_uninitialized) {
1224   initialize();





1225 }
1226 
1227 void MethodData::initialize() {
1228   Thread* thread = Thread::current();
1229   NoSafepointVerifier no_safepoint;  // init function atomic wrt GC
1230   ResourceMark rm(thread);
1231 
1232   init();
1233   set_creation_mileage(mileage_of(method()));
1234 
1235   // Go through the bytecodes and allocate and initialize the
1236   // corresponding data cells.
1237   int data_size = 0;
1238   int empty_bc_count = 0;  // number of bytecodes lacking data
1239   _data[0] = 0;  // apparently not set below.
1240   BytecodeStream stream(methodHandle(thread, method()));
1241   Bytecodes::Code c;
1242   bool needs_speculative_traps = false;
1243   while ((c = stream.next()) >= 0) {
1244     int size_in_bytes = initialize_data(&stream, data_size);

1341       // Generate RTM lock eliding code without abort ratio calculation code.
1342       _rtm_state = UseRTM;
1343     } else if (UseRTMDeopt) {
1344       // Generate RTM lock eliding code and include abort ratio calculation
1345       // code if UseRTMDeopt is on.
1346       _rtm_state = ProfileRTM;
1347     }
1348   }
1349 #endif
1350 
1351   // Initialize escape flags.
1352   clear_escape_info();
1353 }
1354 
1355 // Get a measure of how much mileage the method has on it.
1356 int MethodData::mileage_of(Method* method) {
1357   return MAX2(method->invocation_count(), method->backedge_count());
1358 }
1359 
1360 bool MethodData::is_mature() const {
1361   return CompilationPolicy::is_mature(_method);
1362 }
1363 
1364 // Translate a bci to its corresponding data index (di).
1365 address MethodData::bci_to_dp(int bci) {
1366   ResourceMark rm;
1367   DataLayout* data = data_layout_before(bci);
1368   DataLayout* prev = nullptr;
1369   for ( ; is_valid(data); data = next_data_layout(data)) {
1370     if (data->bci() >= bci) {
1371       if (data->bci() == bci)  set_hint_di(dp_to_di((address)data));
1372       else if (prev != nullptr)   set_hint_di(dp_to_di((address)prev));
1373       return (address)data;
1374     }
1375     prev = data;
1376   }
1377   return (address)limit_data_position();
1378 }
1379 
1380 // Translate a bci to its corresponding data, or null.
1381 ProfileData* MethodData::bci_to_data(int bci) {

1699 }
1700 
1701 bool MethodData::profile_all_parameters() {
1702   return profile_parameters_flag() == type_profile_all;
1703 }
1704 
1705 bool MethodData::profile_parameters_for_method(const methodHandle& m) {
1706   if (!profile_parameters()) {
1707     return false;
1708   }
1709 
1710   if (profile_all_parameters()) {
1711     return true;
1712   }
1713 
1714   assert(profile_parameters_jsr292_only(), "inconsistent");
1715   return m->is_compiled_lambda_form();
1716 }
1717 
1718 void MethodData::metaspace_pointers_do(MetaspaceClosure* it) {
1719   log_trace(cds)("Iter(MethodData): %p", this);
1720   it->push(&_method);


















1721 }
1722 
1723 void MethodData::clean_extra_data_helper(DataLayout* dp, int shift, bool reset) {
1724   check_extra_data_locked();
1725 
1726   if (shift == 0) {
1727     return;
1728   }
1729   if (!reset) {
1730     // Move all cells of trap entry at dp left by "shift" cells
1731     intptr_t* start = (intptr_t*)dp;
1732     intptr_t* end = (intptr_t*)next_extra(dp);
1733     for (intptr_t* ptr = start; ptr < end; ptr++) {
1734       *(ptr-shift) = *ptr;
1735     }
1736   } else {
1737     // Reset "shift" cells stopping at dp
1738     intptr_t* start = ((intptr_t*)dp) - shift;
1739     intptr_t* end = (intptr_t*)dp;
1740     for (intptr_t* ptr = start; ptr < end; ptr++) {
1741       *ptr = 0;
1742     }
1743   }
1744 }
1745 
1746 // Check for entries that reference an unloaded method
1747 class CleanExtraDataKlassClosure : public CleanExtraDataClosure {
1748   bool _always_clean;
1749 public:
1750   CleanExtraDataKlassClosure(bool always_clean) : _always_clean(always_clean) {}
1751   bool is_live(Method* m) {



1752     return !(_always_clean) && m->method_holder()->is_loader_alive();
1753   }
1754 };
1755 
1756 // Check for entries that reference a redefined method
1757 class CleanExtraDataMethodClosure : public CleanExtraDataClosure {
1758 public:
1759   CleanExtraDataMethodClosure() {}
1760   bool is_live(Method* m) { return !m->is_old(); }
1761 };
1762 













1763 
1764 // Remove SpeculativeTrapData entries that reference an unloaded or
1765 // redefined method
1766 void MethodData::clean_extra_data(CleanExtraDataClosure* cl) {
1767   check_extra_data_locked();
1768 
1769   DataLayout* dp  = extra_data_base();
1770   DataLayout* end = args_data_limit();
1771 
1772   int shift = 0;
1773   for (; dp < end; dp = next_extra(dp)) {
1774     switch(dp->tag()) {
1775     case DataLayout::speculative_trap_data_tag: {
1776       SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1777       Method* m = data->method();
1778       assert(m != nullptr, "should have a method");
1779       if (!cl->is_live(m)) {
1780         // "shift" accumulates the number of cells for dead
1781         // SpeculativeTrapData entries that have been seen so
1782         // far. Following entries must be shifted left by that many

1863   ResourceMark rm;
1864   CleanExtraDataMethodClosure cl;
1865 
1866   // Lock to modify extra data, and prevent Safepoint from breaking the lock
1867   MutexLocker ml(extra_data_lock(), Mutex::_no_safepoint_check_flag);
1868 
1869   clean_extra_data(&cl);
1870   verify_extra_data_clean(&cl);
1871 }
1872 
1873 void MethodData::deallocate_contents(ClassLoaderData* loader_data) {
1874   release_C_heap_structures();
1875 }
1876 
1877 void MethodData::release_C_heap_structures() {
1878 #if INCLUDE_JVMCI
1879   FailedSpeculation::free_failed_speculations(get_failed_speculations_address());
1880 #endif
1881 }
1882 










1883 #ifdef ASSERT
1884 void MethodData::check_extra_data_locked() const {
1885     // Cast const away, just to be able to verify the lock
1886     // Usually we only want non-const accesses on the lock,
1887     // so this here is an exception.
1888     MethodData* self = (MethodData*)this;
1889     assert(self->extra_data_lock()->owned_by_self(), "must have lock");
1890     assert(!Thread::current()->is_Java_thread() ||
1891            JavaThread::current()->is_in_no_safepoint_scope(),
1892            "JavaThread must have NoSafepointVerifier inside lock scope");
1893 }
1894 #endif

   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "cds/cdsConfig.hpp"
  27 #include "ci/ciMethodData.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "compiler/compilationPolicy.hpp"
  30 #include "compiler/compilerDefinitions.inline.hpp"
  31 #include "compiler/compilerOracle.hpp"
  32 #include "interpreter/bytecode.hpp"
  33 #include "interpreter/bytecodeStream.hpp"
  34 #include "interpreter/linkResolver.hpp"
  35 #include "memory/metaspaceClosure.hpp"
  36 #include "memory/resourceArea.hpp"
  37 #include "oops/klass.inline.hpp"
  38 #include "oops/methodData.inline.hpp"
  39 #include "prims/jvmtiRedefineClasses.hpp"
  40 #include "runtime/atomic.hpp"
  41 #include "runtime/deoptimization.hpp"
  42 #include "runtime/handles.inline.hpp"
  43 #include "runtime/orderAccess.hpp"
  44 #include "runtime/safepointVerifiers.hpp"
  45 #include "runtime/signature.hpp"
  46 #include "utilities/align.hpp"

 302 #ifdef ASSERT
 303     ResourceMark rm;
 304     ReferenceArgumentCount rac(inv.signature());
 305     int count = MIN2(rac.count(), (int)TypeProfileArgsLimit);
 306     assert(count > 0, "room for args type but none found?");
 307     check_number_of_arguments(count);
 308 #endif
 309     _args.post_initialize(inv.signature(), inv.has_receiver(), false);
 310   }
 311 
 312   if (has_return()) {
 313     assert(is_reference_type(inv.result_type()), "room for a ret type but doesn't return obj?");
 314     _ret.post_initialize();
 315   }
 316 }
 317 
 318 void TypeStackSlotEntries::clean_weak_klass_links(bool always_clean) {
 319   for (int i = 0; i < _number_of_entries; i++) {
 320     intptr_t p = type(i);
 321     Klass* k = (Klass*)klass_part(p);
 322     if (k != nullptr) {
 323       if (!always_clean && k->is_instance_klass() && InstanceKlass::cast(k)->is_not_initialized()) {
 324         continue; // skip not-yet-initialized classes // TODO: maybe clear the slot instead?
 325       }
 326       if (always_clean || !k->is_loader_alive()) {
 327         set_type(i, with_status((Klass*)nullptr, p));
 328       }
 329     }
 330   }
 331 }
 332 
 333 void TypeStackSlotEntries::metaspace_pointers_do(MetaspaceClosure* it) {
 334   for (int i = 0; i < _number_of_entries; i++) {
 335     set_type(i, klass_part(type(i))); // reset tag; FIXME: properly handle tagged pointers
 336     Klass** k = (Klass**)type_adr(i);
 337     it->push(k);
 338 //    it->push_tagged(k);
 339   }
 340 }
 341 
 342 void ReturnTypeEntry::clean_weak_klass_links(bool always_clean) {
 343   intptr_t p = type();
 344   Klass* k = (Klass*)klass_part(p);
 345   if (k != nullptr) {
 346     if (!always_clean && k->is_instance_klass() && InstanceKlass::cast(k)->is_not_initialized()) {
 347       return; // skip not-yet-initialized classes // TODO: maybe clear the slot instead?
 348     }
 349     if (always_clean || !k->is_loader_alive()) {
 350       set_type(with_status((Klass*)nullptr, p));
 351     }
 352   }
 353 }
 354 
 355 void ReturnTypeEntry::metaspace_pointers_do(MetaspaceClosure* it) {
 356   Klass** k = (Klass**)type_adr(); // tagged
 357   set_type(klass_part(type())); // reset tag; FIXME: properly handle tagged pointers
 358   it->push(k);
 359 //  it->push_tagged(k);
 360 }
 361 
 362 bool TypeEntriesAtCall::return_profiling_enabled() {
 363   return MethodData::profile_return();
 364 }
 365 
 366 bool TypeEntriesAtCall::arguments_profiling_enabled() {
 367   return MethodData::profile_arguments();
 368 }
 369 
 370 void TypeEntries::print_klass(outputStream* st, intptr_t k) {
 371   if (is_type_none(k)) {
 372     st->print("none");
 373   } else if (is_type_unknown(k)) {
 374     st->print("unknown");
 375   } else {
 376     valid_klass(k)->print_value_on(st);
 377   }
 378   if (was_null_seen(k)) {
 379     st->print(" (null seen)");
 380   }
 381 }

 417     _args.print_data_on(st);
 418   }
 419   if (has_return()) {
 420     tab(st, true);
 421     st->print("return type");
 422     _ret.print_data_on(st);
 423   }
 424 }
 425 
 426 // ==================================================================
 427 // ReceiverTypeData
 428 //
 429 // A ReceiverTypeData is used to access profiling information about a
 430 // dynamic type check.  It consists of a counter which counts the total times
 431 // that the check is reached, and a series of (Klass*, count) pairs
 432 // which are used to store a type profile for the receiver of the check.
 433 
 434 void ReceiverTypeData::clean_weak_klass_links(bool always_clean) {
 435     for (uint row = 0; row < row_limit(); row++) {
 436     Klass* p = receiver(row);
 437     if (p != nullptr) {
 438       if (!always_clean && p->is_instance_klass() && InstanceKlass::cast(p)->is_not_initialized()) {
 439         continue; // skip not-yet-initialized classes // TODO: maybe clear the slot instead?
 440       }
 441       if (always_clean || !p->is_loader_alive()) {
 442         clear_row(row);
 443       }
 444     }
 445   }
 446 }
 447 
 448 void ReceiverTypeData::metaspace_pointers_do(MetaspaceClosure *it) {
 449   for (uint row = 0; row < row_limit(); row++) {
 450     Klass** recv = (Klass**)intptr_at_adr(receiver_cell_index(row));
 451     it->push(recv);
 452   }
 453 }
 454 
 455 void ReceiverTypeData::print_receiver_data_on(outputStream* st) const {
 456   uint row;
 457   int entries = 0;
 458   for (row = 0; row < row_limit(); row++) {
 459     if (receiver(row) != nullptr)  entries++;
 460   }
 461   st->print_cr("count(%u) entries(%u)", count(), entries);
 462   int total = count();
 463   for (row = 0; row < row_limit(); row++) {
 464     if (receiver(row) != nullptr) {
 465       total += receiver_count(row);
 466     }
 467   }
 468   for (row = 0; row < row_limit(); row++) {
 469     if (receiver(row) != nullptr) {
 470       tab(st);
 471       receiver(row)->print_value_on(st);
 472       st->print_cr("(%u %4.2f)", receiver_count(row), (float) receiver_count(row) / (float) total);
 473     }
 474   }

 663     return obj_args + 1; // 1 cell for array len
 664   }
 665   return 0;
 666 }
 667 
 668 void ParametersTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
 669   _parameters.post_initialize(mdo->method()->signature(), !mdo->method()->is_static(), true);
 670 }
 671 
 672 bool ParametersTypeData::profiling_enabled() {
 673   return MethodData::profile_parameters();
 674 }
 675 
 676 void ParametersTypeData::print_data_on(outputStream* st, const char* extra) const {
 677   print_shared(st, "ParametersTypeData", extra);
 678   tab(st);
 679   _parameters.print_data_on(st);
 680   st->cr();
 681 }
 682 
 683 void SpeculativeTrapData::metaspace_pointers_do(MetaspaceClosure* it) {
 684   Method** m = (Method**)intptr_at_adr(speculative_trap_method);
 685   it->push(m);
 686 }
 687 
 688 void SpeculativeTrapData::print_data_on(outputStream* st, const char* extra) const {
 689   print_shared(st, "SpeculativeTrapData", extra);
 690   tab(st);
 691   method()->print_short_name(st);
 692   st->cr();
 693 }
 694 
 695 // ==================================================================
 696 // MethodData*
 697 //
 698 // A MethodData* holds information which has been collected about
 699 // a method.
 700 
 701 MethodData* MethodData::allocate(ClassLoaderData* loader_data, const methodHandle& method, TRAPS) {
 702   assert(!THREAD->owns_locks(), "Should not own any locks");
 703   int size = MethodData::compute_allocation_size_in_words(method);
 704 
 705   return new (loader_data, size, MetaspaceObj::MethodDataType, THREAD)
 706     MethodData(method);
 707 }

1245 
1246 // Give each of the data entries a chance to perform specific
1247 // data initialization.
1248 void MethodData::post_initialize(BytecodeStream* stream) {
1249   ResourceMark rm;
1250   ProfileData* data;
1251   for (data = first_data(); is_valid(data); data = next_data(data)) {
1252     stream->set_start(data->bci());
1253     stream->next();
1254     data->post_initialize(stream, this);
1255   }
1256   if (_parameters_type_data_di != no_parameters) {
1257     parameters_type_data()->post_initialize(nullptr, this);
1258   }
1259 }
1260 
1261 // Initialize the MethodData* corresponding to a given method.
1262 MethodData::MethodData(const methodHandle& method)
1263   : _method(method()),
1264     // Holds Compile_lock

1265     _compiler_counters(),
1266     _parameters_type_data_di(parameters_uninitialized) {
1267     _extra_data_lock = nullptr;
1268     initialize();
1269 }
1270 
1271 MethodData::MethodData() {
1272   assert(CDSConfig::is_dumping_static_archive() || UseSharedSpaces, "only for CDS");
1273 }
1274 
1275 void MethodData::initialize() {
1276   Thread* thread = Thread::current();
1277   NoSafepointVerifier no_safepoint;  // init function atomic wrt GC
1278   ResourceMark rm(thread);
1279 
1280   init();
1281   set_creation_mileage(mileage_of(method()));
1282 
1283   // Go through the bytecodes and allocate and initialize the
1284   // corresponding data cells.
1285   int data_size = 0;
1286   int empty_bc_count = 0;  // number of bytecodes lacking data
1287   _data[0] = 0;  // apparently not set below.
1288   BytecodeStream stream(methodHandle(thread, method()));
1289   Bytecodes::Code c;
1290   bool needs_speculative_traps = false;
1291   while ((c = stream.next()) >= 0) {
1292     int size_in_bytes = initialize_data(&stream, data_size);

1389       // Generate RTM lock eliding code without abort ratio calculation code.
1390       _rtm_state = UseRTM;
1391     } else if (UseRTMDeopt) {
1392       // Generate RTM lock eliding code and include abort ratio calculation
1393       // code if UseRTMDeopt is on.
1394       _rtm_state = ProfileRTM;
1395     }
1396   }
1397 #endif
1398 
1399   // Initialize escape flags.
1400   clear_escape_info();
1401 }
1402 
1403 // Get a measure of how much mileage the method has on it.
1404 int MethodData::mileage_of(Method* method) {
1405   return MAX2(method->invocation_count(), method->backedge_count());
1406 }
1407 
1408 bool MethodData::is_mature() const {
1409   return CompilationPolicy::is_mature((MethodData*)this);
1410 }
1411 
1412 // Translate a bci to its corresponding data index (di).
1413 address MethodData::bci_to_dp(int bci) {
1414   ResourceMark rm;
1415   DataLayout* data = data_layout_before(bci);
1416   DataLayout* prev = nullptr;
1417   for ( ; is_valid(data); data = next_data_layout(data)) {
1418     if (data->bci() >= bci) {
1419       if (data->bci() == bci)  set_hint_di(dp_to_di((address)data));
1420       else if (prev != nullptr)   set_hint_di(dp_to_di((address)prev));
1421       return (address)data;
1422     }
1423     prev = data;
1424   }
1425   return (address)limit_data_position();
1426 }
1427 
1428 // Translate a bci to its corresponding data, or null.
1429 ProfileData* MethodData::bci_to_data(int bci) {

1747 }
1748 
1749 bool MethodData::profile_all_parameters() {
1750   return profile_parameters_flag() == type_profile_all;
1751 }
1752 
1753 bool MethodData::profile_parameters_for_method(const methodHandle& m) {
1754   if (!profile_parameters()) {
1755     return false;
1756   }
1757 
1758   if (profile_all_parameters()) {
1759     return true;
1760   }
1761 
1762   assert(profile_parameters_jsr292_only(), "inconsistent");
1763   return m->is_compiled_lambda_form();
1764 }
1765 
1766 void MethodData::metaspace_pointers_do(MetaspaceClosure* it) {
1767   log_trace(cds)("Iter(MethodData): %p for %p %s", this, _method, _method->name_and_sig_as_C_string());
1768   it->push(&_method);
1769   if (_parameters_type_data_di != no_parameters) {
1770     parameters_type_data()->metaspace_pointers_do(it);
1771   }
1772   for (ProfileData* data = first_data(); is_valid(data); data = next_data(data)) {
1773     data->metaspace_pointers_do(it);
1774   }
1775   for (DataLayout* dp = extra_data_base();
1776                    dp < extra_data_limit();
1777                    dp = MethodData::next_extra(dp)) {
1778     if (dp->tag() == DataLayout::speculative_trap_data_tag) {
1779       ResourceMark rm;
1780       SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1781       data->metaspace_pointers_do(it);
1782     } else if (dp->tag() == DataLayout::no_tag ||
1783                dp->tag() == DataLayout::arg_info_data_tag) {
1784       break;
1785     }
1786   }
1787 }
1788 
1789 void MethodData::clean_extra_data_helper(DataLayout* dp, int shift, bool reset) {
1790   check_extra_data_locked();
1791 
1792   if (shift == 0) {
1793     return;
1794   }
1795   if (!reset) {
1796     // Move all cells of trap entry at dp left by "shift" cells
1797     intptr_t* start = (intptr_t*)dp;
1798     intptr_t* end = (intptr_t*)next_extra(dp);
1799     for (intptr_t* ptr = start; ptr < end; ptr++) {
1800       *(ptr-shift) = *ptr;
1801     }
1802   } else {
1803     // Reset "shift" cells stopping at dp
1804     intptr_t* start = ((intptr_t*)dp) - shift;
1805     intptr_t* end = (intptr_t*)dp;
1806     for (intptr_t* ptr = start; ptr < end; ptr++) {
1807       *ptr = 0;
1808     }
1809   }
1810 }
1811 
1812 // Check for entries that reference an unloaded method
1813 class CleanExtraDataKlassClosure : public CleanExtraDataClosure {
1814   bool _always_clean;
1815 public:
1816   CleanExtraDataKlassClosure(bool always_clean) : _always_clean(always_clean) {}
1817   bool is_live(Method* m) {
1818     if (!_always_clean && m->method_holder()->is_instance_klass() && InstanceKlass::cast(m->method_holder())->is_not_initialized()) {
1819       return true; // TODO: treat as unloaded instead?
1820     }
1821     return !(_always_clean) && m->method_holder()->is_loader_alive();
1822   }
1823 };
1824 
1825 // Check for entries that reference a redefined method
1826 class CleanExtraDataMethodClosure : public CleanExtraDataClosure {
1827 public:
1828   CleanExtraDataMethodClosure() {}
1829   bool is_live(Method* m) { return !m->is_old(); }
1830 };
1831 
1832 Mutex* MethodData::extra_data_lock() {
1833   Mutex* lock = Atomic::load(&_extra_data_lock);
1834   if (lock == nullptr) {
1835     lock = new Mutex(Mutex::nosafepoint, "MDOExtraData_lock");
1836     Mutex* old = Atomic::cmpxchg(&_extra_data_lock, (Mutex*)nullptr, lock);
1837     if (old != nullptr) {
1838       // Another thread created the lock before us. Use that lock instead.
1839       delete lock;
1840       return old;
1841     }
1842   }
1843   return lock;
1844 }
1845 
1846 // Remove SpeculativeTrapData entries that reference an unloaded or
1847 // redefined method
1848 void MethodData::clean_extra_data(CleanExtraDataClosure* cl) {
1849   check_extra_data_locked();
1850 
1851   DataLayout* dp  = extra_data_base();
1852   DataLayout* end = args_data_limit();
1853 
1854   int shift = 0;
1855   for (; dp < end; dp = next_extra(dp)) {
1856     switch(dp->tag()) {
1857     case DataLayout::speculative_trap_data_tag: {
1858       SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1859       Method* m = data->method();
1860       assert(m != nullptr, "should have a method");
1861       if (!cl->is_live(m)) {
1862         // "shift" accumulates the number of cells for dead
1863         // SpeculativeTrapData entries that have been seen so
1864         // far. Following entries must be shifted left by that many

1945   ResourceMark rm;
1946   CleanExtraDataMethodClosure cl;
1947 
1948   // Lock to modify extra data, and prevent Safepoint from breaking the lock
1949   MutexLocker ml(extra_data_lock(), Mutex::_no_safepoint_check_flag);
1950 
1951   clean_extra_data(&cl);
1952   verify_extra_data_clean(&cl);
1953 }
1954 
1955 void MethodData::deallocate_contents(ClassLoaderData* loader_data) {
1956   release_C_heap_structures();
1957 }
1958 
1959 void MethodData::release_C_heap_structures() {
1960 #if INCLUDE_JVMCI
1961   FailedSpeculation::free_failed_speculations(get_failed_speculations_address());
1962 #endif
1963 }
1964 
1965 #if INCLUDE_CDS
1966 void MethodData::remove_unshareable_info() {
1967   _extra_data_lock = nullptr;
1968 }
1969 
1970 void MethodData::restore_unshareable_info(TRAPS) {
1971   //_extra_data_lock = new Mutex(Mutex::nosafepoint, "MDOExtraData_lock");
1972 }
1973 #endif // INCLUDE_CDS
1974        
1975 #ifdef ASSERT
1976 void MethodData::check_extra_data_locked() const {
1977     // Cast const away, just to be able to verify the lock
1978     // Usually we only want non-const accesses on the lock,
1979     // so this here is an exception.
1980     MethodData* self = (MethodData*)this;
1981     assert(self->extra_data_lock()->owned_by_self() || CDSConfig::is_dumping_archive(), "must have lock");
1982     assert(!Thread::current()->is_Java_thread() ||
1983            JavaThread::current()->is_in_no_safepoint_scope(),
1984            "JavaThread must have NoSafepointVerifier inside lock scope");
1985 }
1986 #endif
< prev index next >