1 /* 2 * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 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 #ifndef SHARE_CODE_AOTCODECACHE_HPP 26 #define SHARE_CODE_AOTCODECACHE_HPP 27 28 #include "compiler/compilerDefinitions.hpp" 29 #include "memory/allocation.hpp" 30 #include "nmt/memTag.hpp" 31 #include "oops/oopsHierarchy.hpp" 32 #include "utilities/exceptions.hpp" 33 34 /* 35 * AOT Code Cache collects code from Code Cache and corresponding metadata 36 * during application training run. 37 * In following "production" runs this code and data can be loaded into 38 * Code Cache skipping its generation. 39 * Additionaly special compiled code "preload" is generated with class initialization 40 * barriers which can be called on first Java method invocation. 41 */ 42 43 class AbstractCompiler; 44 class AOTCodeCache; 45 class AsmRemarks; 46 class ciConstant; 47 class ciEnv; 48 class ciMethod; 49 class CodeBlob; 50 class CodeOffsets; 51 class CompileTask; 52 class DbgStrings; 53 class DebugInformationRecorder; 54 class Dependencies; 55 class ExceptionTable; 56 class ExceptionHandlerTable; 57 template<typename E> 58 class GrowableArray; 59 class ImmutableOopMapSet; 60 class ImplicitExceptionTable; 61 class JavaThread; 62 class Klass; 63 class methodHandle; 64 class Metadata; 65 class Method; 66 class nmethod; 67 class OopMapSet; 68 class OopRecorder; 69 class outputStream; 70 class RelocIterator; 71 class StubCodeGenerator; 72 73 enum class vmIntrinsicID : int; 74 75 #define DO_AOTCODEENTRY_KIND(Fn) \ 76 Fn(None) \ 77 Fn(Adapter) \ 78 Fn(Stub) \ 79 Fn(SharedBlob) \ 80 Fn(C1Blob) \ 81 Fn(C2Blob) \ 82 Fn(Code) \ 83 84 // Descriptor of AOT Code Cache's entry 85 class AOTCodeEntry { 86 public: 87 enum Kind : s1 { 88 #define DECL_KIND_ENUM(kind) kind, 89 DO_AOTCODEENTRY_KIND(DECL_KIND_ENUM) 90 #undef DECL_KIND_ENUM 91 Kind_count 92 }; 93 94 private: 95 AOTCodeEntry* _next; 96 Method* _method; 97 uint _method_offset; 98 Kind _kind; 99 uint _id; // Adapter's id, vmIntrinsic::ID for stub or name's hash for nmethod 100 uint _offset; // Offset to entry 101 uint _size; // Entry size 102 uint _name_offset; // Method's or intrinsic name 103 uint _name_size; 104 uint _num_inlined_bytecodes; 105 106 uint _code_offset; // Start of code in cache 107 uint _code_size; // Total size of all code sections 108 109 uint _comp_level; // compilation level 110 uint _comp_id; // compilation id 111 bool _has_oop_maps; 112 bool _has_clinit_barriers; // Generated code has class init checks 113 bool _for_preload; // Code can be used for preload 114 bool _loaded; // Code was loaded 115 bool _not_entrant; // Deoptimized 116 bool _load_fail; // Failed to load due to some klass state 117 address _dumptime_content_start_addr; // CodeBlob::content_begin() at dump time; used for applying relocations 118 119 public: 120 // this constructor is used only by AOTCodeEntry::Stub 121 AOTCodeEntry(uint offset, uint size, uint name_offset, uint name_size, 122 uint code_offset, uint code_size, 123 Kind kind, uint id) { 124 assert(kind == AOTCodeEntry::Stub, "sanity check"); 125 _next = nullptr; 126 _method = nullptr; 127 _kind = kind; 128 _id = id; 129 _offset = offset; 130 _size = size; 131 _name_offset = name_offset; 132 _name_size = name_size; 133 _code_offset = code_offset; 134 _code_size = code_size; 135 136 _dumptime_content_start_addr = nullptr; 137 _num_inlined_bytecodes = 0; 138 _comp_level = 0; 139 _comp_id = 0; 140 _has_oop_maps = false; // unused here 141 _has_clinit_barriers = false; 142 _for_preload = false; 143 _loaded = false; 144 _not_entrant = false; 145 _load_fail = false; 146 } 147 148 AOTCodeEntry(Kind kind, uint id, 149 uint offset, uint size, 150 uint name_offset, uint name_size, 151 uint blob_offset, bool has_oop_maps, 152 address dumptime_content_start_addr, 153 uint comp_level = 0, 154 uint comp_id = 0, 155 bool has_clinit_barriers = false, 156 bool for_preload = false) { 157 _next = nullptr; 158 _method = nullptr; 159 _kind = kind; 160 _id = id; 161 _offset = offset; 162 _size = size; 163 _name_offset = name_offset; 164 _name_size = name_size; 165 _code_offset = blob_offset; 166 _code_size = 0; // unused 167 168 _dumptime_content_start_addr = dumptime_content_start_addr; 169 _num_inlined_bytecodes = 0; 170 171 _comp_level = comp_level; 172 _comp_id = comp_id; 173 _has_oop_maps = has_oop_maps; 174 _has_clinit_barriers = has_clinit_barriers; 175 _for_preload = for_preload; 176 _loaded = false; 177 _not_entrant = false; 178 _load_fail = false; 179 180 _loaded = false; 181 _not_entrant = false; 182 _load_fail = false; 183 } 184 185 void* operator new(size_t x, AOTCodeCache* cache); 186 // Delete is a NOP 187 void operator delete( void *ptr ) {} 188 189 AOTCodeEntry* next() const { return _next; } 190 void set_next(AOTCodeEntry* next) { _next = next; } 191 192 Method* method() const { return _method; } 193 void set_method(Method* method) { _method = method; } 194 void update_method_for_writing(); 195 uint method_offset() const { return _method_offset; } 196 197 Kind kind() const { return _kind; } 198 uint id() const { return _id; } 199 200 uint offset() const { return _offset; } 201 void set_offset(uint off) { _offset = off; } 202 203 uint size() const { return _size; } 204 uint name_offset() const { return _name_offset; } 205 uint name_size() const { return _name_size; } 206 uint code_offset() const { return _code_offset; } 207 uint code_size() const { return _code_size; } 208 209 bool has_oop_maps() const { return _has_oop_maps; } 210 address dumptime_content_start_addr() const { return _dumptime_content_start_addr; } 211 uint num_inlined_bytecodes() const { return _num_inlined_bytecodes; } 212 void set_inlined_bytecodes(int bytes) { _num_inlined_bytecodes = bytes; } 213 214 uint comp_level() const { return _comp_level; } 215 uint comp_id() const { return _comp_id; } 216 217 bool has_clinit_barriers() const { return _has_clinit_barriers; } 218 bool for_preload() const { return _for_preload; } 219 bool is_loaded() const { return _loaded; } 220 void set_loaded() { _loaded = true; } 221 222 bool not_entrant() const { return _not_entrant; } 223 void set_not_entrant() { _not_entrant = true; } 224 void set_entrant() { _not_entrant = false; } 225 226 bool load_fail() const { return _load_fail; } 227 void set_load_fail() { _load_fail = true; } 228 229 void print(outputStream* st) const; 230 231 static bool is_valid_entry_kind(Kind kind) { return kind > None && kind < Kind_count; } 232 static bool is_blob(Kind kind) { return kind == SharedBlob || kind == C1Blob || kind == C2Blob; } 233 static bool is_adapter(Kind kind) { return kind == Adapter; } 234 bool is_code() { return _kind == Code; } 235 }; 236 237 // Addresses of stubs, blobs and runtime finctions called from compiled code. 238 class AOTCodeAddressTable : public CHeapObj<mtCode> { 239 private: 240 address* _extrs_addr; 241 address* _stubs_addr; 242 address* _shared_blobs_addr; 243 address* _C1_blobs_addr; 244 address* _C2_blobs_addr; 245 uint _extrs_length; 246 uint _stubs_length; 247 uint _shared_blobs_length; 248 uint _C1_blobs_length; 249 uint _C2_blobs_length; 250 251 bool _extrs_complete; 252 bool _early_stubs_complete; 253 bool _shared_blobs_complete; 254 bool _early_c1_complete; 255 bool _c1_complete; 256 bool _c2_complete; 257 bool _complete; 258 259 public: 260 AOTCodeAddressTable() : 261 _extrs_addr(nullptr), 262 _shared_blobs_addr(nullptr), 263 _C1_blobs_addr(nullptr), 264 _C2_blobs_addr(nullptr), 265 _extrs_length(0), 266 _stubs_length(0), 267 _shared_blobs_length(0), 268 _C1_blobs_length(0), 269 _C2_blobs_length(0), 270 _extrs_complete(false), 271 _early_stubs_complete(false), 272 _shared_blobs_complete(false), 273 _early_c1_complete(false), 274 _c1_complete(false), 275 _c2_complete(false), 276 _complete(false) 277 { } 278 ~AOTCodeAddressTable(); 279 void init_extrs(); 280 void init_early_stubs(); 281 void init_shared_blobs(); 282 void init_stubs(); 283 void init_early_c1(); 284 void init_c1(); 285 void init_c2(); 286 const char* add_C_string(const char* str); 287 int id_for_C_string(address str); 288 address address_for_C_string(int idx); 289 int id_for_address(address addr, RelocIterator iter, CodeBlob* blob); 290 address address_for_id(int id); 291 bool c2_complete() const { return _c2_complete; } 292 bool c1_complete() const { return _c1_complete; } 293 }; 294 295 struct AOTCodeSection { 296 public: 297 address _origin_address; 298 uint _size; 299 uint _offset; 300 }; 301 302 enum class DataKind: int { 303 No_Data = -1, 304 Null = 0, 305 Klass = 1, 306 Method = 2, 307 String = 3, 308 Primitive = 4, // primitive Class object 309 SysLoader = 5, // java_system_loader 310 PlaLoader = 6, // java_platform_loader 311 MethodCnts= 7, 312 Klass_Shared = 8, 313 Method_Shared = 9, 314 String_Shared = 10, 315 MH_Oop_Shared = 11 316 }; 317 318 class AOTCodeCache : public CHeapObj<mtCode> { 319 320 // Classes used to describe AOT code cache. 321 protected: 322 class Config { 323 address _compressedOopBase; 324 address _compressedKlassBase; 325 uint _compressedOopShift; 326 uint _compressedKlassShift; 327 uint _contendedPaddingWidth; 328 uint _objectAlignment; 329 uint _gc; 330 enum Flags { 331 none = 0, 332 debugVM = 2, 333 compressedOops = 4, 334 compressedClassPointers = 8, 335 useTLAB = 16, 336 systemClassAssertions = 32, 337 userClassAssertions = 64, 338 enableContendedPadding = 128, 339 restrictContendedPadding = 256, 340 }; 341 uint _flags; 342 343 public: 344 void record(); 345 bool verify() const; 346 }; 347 348 class Header : public CHeapObj<mtCode> { 349 private: 350 // Here should be version and other verification fields 351 enum { 352 AOT_CODE_VERSION = 1 353 }; 354 uint _version; // AOT code version (should match when reading code cache) 355 uint _cache_size; // cache size in bytes 356 uint _strings_count; // number of recorded C strings 357 uint _strings_offset; // offset to recorded C strings 358 uint _entries_count; // number of recorded entries 359 uint _entries_offset; // offset of AOTCodeEntry array describing entries 360 uint _preload_entries_count; // entries for pre-loading code 361 uint _preload_entries_offset; 362 uint _adapters_count; 363 uint _shared_blobs_count; 364 uint _C1_blobs_count; 365 uint _C2_blobs_count; 366 uint _stubs_count; 367 Config _config; 368 369 public: 370 void init(uint cache_size, 371 uint strings_count, uint strings_offset, 372 uint entries_count, uint entries_offset, 373 uint preload_entries_count, uint preload_entries_offset, 374 uint adapters_count, uint shared_blobs_count, 375 uint C1_blobs_count, uint C2_blobs_count, uint stubs_count) { 376 _version = AOT_CODE_VERSION; 377 _cache_size = cache_size; 378 _strings_count = strings_count; 379 _strings_offset = strings_offset; 380 _entries_count = entries_count; 381 _entries_offset = entries_offset; 382 _preload_entries_count = preload_entries_count; 383 _preload_entries_offset = preload_entries_offset; 384 _adapters_count = adapters_count; 385 _shared_blobs_count = shared_blobs_count; 386 _C1_blobs_count = C1_blobs_count; 387 _C2_blobs_count = C2_blobs_count; 388 _stubs_count = stubs_count; 389 390 _config.record(); 391 } 392 393 uint cache_size() const { return _cache_size; } 394 uint strings_count() const { return _strings_count; } 395 uint strings_offset() const { return _strings_offset; } 396 uint entries_count() const { return _entries_count; } 397 uint entries_offset() const { return _entries_offset; } 398 uint preload_entries_count() const { return _preload_entries_count; } 399 uint preload_entries_offset() const { return _preload_entries_offset; } 400 uint adapters_count() const { return _adapters_count; } 401 uint shared_blobs_count() const { return _shared_blobs_count; } 402 uint C1_blobs_count() const { return _C1_blobs_count; } 403 uint C2_blobs_count() const { return _C2_blobs_count; } 404 uint stubs_count() const { return _stubs_count; } 405 uint nmethods_count() const { return _entries_count 406 - _stubs_count 407 - _shared_blobs_count 408 - _C1_blobs_count 409 - _C2_blobs_count 410 - _adapters_count; } 411 412 bool verify_config(uint load_size) const; 413 bool verify_vm_config() const { // Called after Universe initialized 414 return _config.verify(); 415 } 416 }; 417 418 // Continue with AOTCodeCache class definition. 419 private: 420 Header* _load_header; 421 char* _load_buffer; // Aligned buffer for loading cached code 422 char* _store_buffer; // Aligned buffer for storing cached code 423 char* _C_store_buffer; // Original unaligned buffer 424 425 uint _write_position; // Position in _store_buffer 426 uint _load_size; // Used when reading cache 427 uint _store_size; // Used when writing cache 428 bool _for_use; // AOT cache is open for using AOT code 429 bool _for_dump; // AOT cache is open for dumping AOT code 430 bool _closing; // Closing cache file 431 bool _failed; // Failed read/write to/from cache (cache is broken?) 432 bool _lookup_failed; // Failed to lookup for info (skip only this code load) 433 434 bool _for_preload; // Code for preload 435 bool _gen_preload_code; // Generate pre-loading code 436 bool _has_clinit_barriers; // Code with clinit barriers 437 438 AOTCodeAddressTable* _table; 439 440 AOTCodeEntry* _load_entries; // Used when reading cache 441 uint* _search_entries; // sorted by ID table [id, index] 442 AOTCodeEntry* _store_entries; // Used when writing cache 443 const char* _C_strings_buf; // Loaded buffer for _C_strings[] table 444 uint _store_entries_cnt; 445 446 uint _compile_id; 447 uint _comp_level; 448 uint compile_id() const { return _compile_id; } 449 uint comp_level() const { return _comp_level; } 450 451 static AOTCodeCache* open_for_use(); 452 static AOTCodeCache* open_for_dump(); 453 454 bool set_write_position(uint pos); 455 bool align_write(); 456 457 address reserve_bytes(uint nbytes); 458 uint write_bytes(const void* buffer, uint nbytes); 459 const char* addr(uint offset) const { return _load_buffer + offset; } 460 static AOTCodeAddressTable* addr_table() { 461 return is_on() && (cache()->_table != nullptr) ? cache()->_table : nullptr; 462 } 463 464 void set_lookup_failed() { _lookup_failed = true; } 465 void clear_lookup_failed() { _lookup_failed = false; } 466 bool lookup_failed() const { return _lookup_failed; } 467 468 AOTCodeEntry* write_nmethod(nmethod* nm, bool for_preload); 469 470 // States: 471 // S >= 0: allow new readers, S readers are currently active 472 // S < 0: no new readers are allowed; (-S-1) readers are currently active 473 // (special case: S = -1 means no readers are active, and would never be active again) 474 static volatile int _nmethod_readers; 475 476 static void wait_for_no_nmethod_readers(); 477 478 class ReadingMark { 479 private: 480 bool _failed; 481 public: 482 ReadingMark(); 483 ~ReadingMark(); 484 bool failed() { 485 return _failed; 486 } 487 }; 488 489 public: 490 AOTCodeCache(bool is_dumping, bool is_using); 491 ~AOTCodeCache(); 492 493 const char* cache_buffer() const { return _load_buffer; } 494 bool failed() const { return _failed; } 495 void set_failed() { _failed = true; } 496 497 static bool is_address_in_aot_cache(address p) NOT_CDS_RETURN_(false); 498 static uint max_aot_code_size(); 499 500 uint load_size() const { return _load_size; } 501 uint write_position() const { return _write_position; } 502 503 void load_strings(); 504 int store_strings(); 505 506 static void init_extrs_table() NOT_CDS_RETURN; 507 static void init_early_stubs_table() NOT_CDS_RETURN; 508 static void init_shared_blobs_table() NOT_CDS_RETURN; 509 static void init_stubs_table() NOT_CDS_RETURN; 510 static void init_early_c1_table() NOT_CDS_RETURN; 511 static void init_c1_table() NOT_CDS_RETURN; 512 static void init_c2_table() NOT_CDS_RETURN; 513 514 address address_for_C_string(int idx) const { return _table->address_for_C_string(idx); } 515 address address_for_id(int id) const { return _table->address_for_id(id); } 516 517 bool for_use() const { return _for_use && !_failed; } 518 bool for_dump() const { return _for_dump && !_failed; } 519 520 bool closing() const { return _closing; } 521 bool gen_preload_code() const { return _gen_preload_code; } 522 523 AOTCodeEntry* add_entry() { 524 _store_entries_cnt++; 525 _store_entries -= 1; 526 return _store_entries; 527 } 528 void preload_startup_code(TRAPS); 529 530 AOTCodeEntry* find_entry(AOTCodeEntry::Kind kind, uint id, uint comp_level = 0); 531 void invalidate_entry(AOTCodeEntry* entry); 532 533 bool finish_write(); 534 535 void log_stats_on_exit(); 536 537 static bool load_stub(StubCodeGenerator* cgen, vmIntrinsicID id, const char* name, address start) NOT_CDS_RETURN_(false); 538 static bool store_stub(StubCodeGenerator* cgen, vmIntrinsicID id, const char* name, address start) NOT_CDS_RETURN_(false); 539 540 bool write_klass(Klass* klass); 541 bool write_method(Method* method); 542 543 bool write_relocations(CodeBlob& code_blob, GrowableArray<Handle>* oop_list = nullptr, GrowableArray<Metadata*>* metadata_list = nullptr); 544 545 bool write_oop_map_set(CodeBlob& cb); 546 bool write_nmethod_reloc_immediates(GrowableArray<Handle>& oop_list, GrowableArray<Metadata*>& metadata_list); 547 548 jobject read_oop(JavaThread* thread, const methodHandle& comp_method); 549 Metadata* read_metadata(const methodHandle& comp_method); 550 551 bool write_oop(jobject& jo); 552 bool write_oop(oop obj); 553 bool write_metadata(Metadata* m); 554 bool write_oops(nmethod* nm); 555 bool write_metadata(nmethod* nm); 556 557 #ifndef PRODUCT 558 bool write_asm_remarks(AsmRemarks& asm_remarks, bool use_string_table); 559 bool write_dbg_strings(DbgStrings& dbg_strings, bool use_string_table); 560 #endif // PRODUCT 561 562 static bool store_code_blob(CodeBlob& blob, 563 AOTCodeEntry::Kind entry_kind, 564 uint id, const char* name, 565 int entry_offset_count = 0, 566 int* entry_offsets = nullptr) NOT_CDS_RETURN_(false); 567 568 static CodeBlob* load_code_blob(AOTCodeEntry::Kind kind, 569 uint id, const char* name, 570 int entry_offset_count = 0, 571 int* entry_offsets = nullptr) NOT_CDS_RETURN_(nullptr); 572 573 static bool load_nmethod(ciEnv* env, ciMethod* target, int entry_bci, AbstractCompiler* compiler, CompLevel comp_level) NOT_CDS_RETURN_(false); 574 static AOTCodeEntry* store_nmethod(nmethod* nm, AbstractCompiler* compiler, bool for_preload) NOT_CDS_RETURN_(nullptr); 575 576 static uint store_entries_cnt() { 577 if (is_on_for_dump()) { 578 return cache()->_store_entries_cnt; 579 } 580 return -1; 581 } 582 583 // Static access 584 585 private: 586 static AOTCodeCache* _cache; 587 588 static bool open_cache(bool is_dumping, bool is_using); 589 static bool verify_vm_config() { 590 if (is_on_for_use()) { 591 return _cache->_load_header->verify_vm_config(); 592 } 593 return true; 594 } 595 public: 596 static AOTCodeCache* cache() { return _cache; } 597 static void initialize() NOT_CDS_RETURN; 598 static void init2() NOT_CDS_RETURN; 599 static void close() NOT_CDS_RETURN; 600 static bool is_on() CDS_ONLY({ return _cache != nullptr && !_cache->closing(); }) NOT_CDS_RETURN_(false); 601 static bool is_C3_on() NOT_CDS_RETURN_(false); 602 static bool is_code_load_thread_on() NOT_CDS_RETURN_(false); 603 static bool is_on_for_use() CDS_ONLY({ return is_on() && _cache->for_use(); }) NOT_CDS_RETURN_(false); 604 static bool is_on_for_dump() CDS_ONLY({ return is_on() && _cache->for_dump(); }) NOT_CDS_RETURN_(false); 605 static bool is_dumping_code() NOT_CDS_RETURN_(false); 606 static bool is_dumping_stub() NOT_CDS_RETURN_(false); 607 static bool is_dumping_adapter() NOT_CDS_RETURN_(false); 608 static bool is_using_code() NOT_CDS_RETURN_(false); 609 static bool is_using_stub() NOT_CDS_RETURN_(false); 610 static bool is_using_adapter() NOT_CDS_RETURN_(false); 611 static void enable_caching() NOT_CDS_RETURN; 612 static void disable_caching() NOT_CDS_RETURN; 613 static bool is_caching_enabled() NOT_CDS_RETURN_(false); 614 615 static bool gen_preload_code(ciMethod* m, int entry_bci) NOT_CDS_RETURN_(false); 616 static bool allow_const_field(ciConstant& value) NOT_CDS_RETURN_(false); 617 static void invalidate(AOTCodeEntry* entry) NOT_CDS_RETURN; 618 static bool is_loaded(AOTCodeEntry* entry); 619 static AOTCodeEntry* find_code_entry(const methodHandle& method, uint comp_level); 620 static void preload_code(JavaThread* thread) NOT_CDS_RETURN; 621 622 template<typename Function> 623 static void iterate(Function function) { // lambda enabled API 624 AOTCodeCache* cache = open_for_use(); 625 if (cache != nullptr) { 626 ReadingMark rdmk; 627 if (rdmk.failed()) { 628 // Cache is closed, cannot touch anything. 629 return; 630 } 631 632 uint count = cache->_load_header->entries_count(); 633 uint* search_entries = (uint*)cache->addr(cache->_load_header->entries_offset()); // [id, index] 634 AOTCodeEntry* load_entries = (AOTCodeEntry*)(search_entries + 2 * count); 635 636 for (uint i = 0; i < count; i++) { 637 int index = search_entries[2*i + 1]; 638 AOTCodeEntry* entry = &(load_entries[index]); 639 function(entry); 640 } 641 } 642 } 643 644 static const char* add_C_string(const char* str) NOT_CDS_RETURN_(str); 645 646 static void print_on(outputStream* st) NOT_CDS_RETURN; 647 static void print_statistics_on(outputStream* st) NOT_CDS_RETURN; 648 static void print_timers_on(outputStream* st) NOT_CDS_RETURN; 649 static void print_unused_entries_on(outputStream* st) NOT_CDS_RETURN; 650 }; 651 652 // Concurent AOT code reader 653 class AOTCodeReader { 654 private: 655 const AOTCodeCache* _cache; 656 const AOTCodeEntry* _entry; 657 const char* _load_buffer; // Loaded cached code buffer 658 uint _read_position; // Position in _load_buffer 659 uint read_position() const { return _read_position; } 660 void set_read_position(uint pos); 661 const char* addr(uint offset) const { return _load_buffer + offset; } 662 663 uint _compile_id; 664 uint _comp_level; 665 uint compile_id() const { return _compile_id; } 666 uint comp_level() const { return _comp_level; } 667 668 bool _preload; // Preloading code before method execution 669 bool _lookup_failed; // Failed to lookup for info (skip only this code load) 670 void set_lookup_failed() { _lookup_failed = true; } 671 void clear_lookup_failed() { _lookup_failed = false; } 672 bool lookup_failed() const { return _lookup_failed; } 673 674 public: 675 AOTCodeReader(AOTCodeCache* cache, AOTCodeEntry* entry, CompileTask* task); 676 677 AOTCodeEntry* aot_code_entry() { return (AOTCodeEntry*)_entry; } 678 679 // convenience method to convert offset in AOTCodeEntry data to its address 680 bool compile_nmethod(ciEnv* env, ciMethod* target, AbstractCompiler* compiler); 681 682 CodeBlob* compile_code_blob(const char* name, int entry_offset_count, int* entry_offsets); 683 684 Klass* read_klass(const methodHandle& comp_method, bool shared); 685 Method* read_method(const methodHandle& comp_method, bool shared); 686 687 oop read_oop(JavaThread* thread, const methodHandle& comp_method); 688 Metadata* read_metadata(const methodHandle& comp_method); 689 bool read_oops(OopRecorder* oop_recorder, ciMethod* target); 690 bool read_metadata(OopRecorder* oop_recorder, ciMethod* target); 691 692 bool read_oop_metadata_list(JavaThread* thread, ciMethod* target, GrowableArray<Handle> &oop_list, GrowableArray<Metadata*> &metadata_list, OopRecorder* oop_recorder); 693 void apply_relocations(nmethod* nm, GrowableArray<Handle> &oop_list, GrowableArray<Metadata*> &metadata_list) NOT_CDS_RETURN; 694 695 ImmutableOopMapSet* read_oop_map_set(); 696 697 void fix_relocations(CodeBlob* code_blob, GrowableArray<Handle>* oop_list = nullptr, GrowableArray<Metadata*>* metadata_list = nullptr) NOT_CDS_RETURN; 698 #ifndef PRODUCT 699 void read_asm_remarks(AsmRemarks& asm_remarks, bool use_string_table) NOT_CDS_RETURN; 700 void read_dbg_strings(DbgStrings& dbg_strings, bool use_string_table) NOT_CDS_RETURN; 701 #endif // PRODUCT 702 703 void print_on(outputStream* st); 704 }; 705 706 // +1 for preload code 707 const int AOTCompLevel_count = CompLevel_count + 1; // 6 levels indexed from 0 to 5 708 709 struct AOTCodeStats { 710 private: 711 struct { 712 uint _kind_cnt[AOTCodeEntry::Kind_count]; 713 uint _nmethod_cnt[AOTCompLevel_count]; 714 uint _clinit_barriers_cnt; 715 } ccstats; // ccstats = cached code stats 716 717 void check_kind(uint kind) { assert(kind >= AOTCodeEntry::None && kind < AOTCodeEntry::Kind_count, "Invalid AOTCodeEntry kind %d", kind); } 718 void check_complevel(uint lvl) { assert(lvl >= CompLevel_none && lvl < AOTCompLevel_count, "Invalid compilation level %d", lvl); } 719 720 public: 721 void inc_entry_cnt(uint kind) { check_kind(kind); ccstats._kind_cnt[kind] += 1; } 722 void inc_nmethod_cnt(uint lvl) { check_complevel(lvl); ccstats._nmethod_cnt[lvl] += 1; } 723 void inc_preload_cnt() { ccstats._nmethod_cnt[AOTCompLevel_count-1] += 1; } 724 void inc_clinit_barriers_cnt() { ccstats._clinit_barriers_cnt += 1; } 725 726 void collect_entry_stats(AOTCodeEntry* entry) { 727 inc_entry_cnt(entry->kind()); 728 if (entry->is_code()) { 729 entry->for_preload() ? inc_nmethod_cnt(AOTCompLevel_count-1) 730 : inc_nmethod_cnt(entry->comp_level()); 731 if (entry->has_clinit_barriers()) { 732 inc_clinit_barriers_cnt(); 733 } 734 } 735 } 736 737 uint entry_count(uint kind) { check_kind(kind); return ccstats._kind_cnt[kind]; } 738 uint nmethod_count(uint lvl) { check_complevel(lvl); return ccstats._nmethod_cnt[lvl]; } 739 uint preload_count() { return ccstats._nmethod_cnt[AOTCompLevel_count-1]; } 740 uint clinit_barriers_count() { return ccstats._clinit_barriers_cnt; } 741 742 uint total_count() { 743 uint total = 0; 744 for (int kind = AOTCodeEntry::None; kind < AOTCodeEntry::Kind_count; kind++) { 745 total += ccstats._kind_cnt[kind]; 746 } 747 return total; 748 } 749 750 static AOTCodeStats add_aot_code_stats(AOTCodeStats stats1, AOTCodeStats stats2); 751 752 // Runtime stats of the AOT code 753 private: 754 struct { 755 struct { 756 uint _loaded_cnt; 757 uint _invalidated_cnt; 758 uint _load_failed_cnt; 759 } _entry_kinds[AOTCodeEntry::Kind_count], 760 _nmethods[AOTCompLevel_count]; 761 } rs; // rs = runtime stats 762 763 public: 764 void inc_entry_loaded_cnt(uint kind) { check_kind(kind); rs._entry_kinds[kind]._loaded_cnt += 1; } 765 void inc_entry_invalidated_cnt(uint kind) { check_kind(kind); rs._entry_kinds[kind]._invalidated_cnt += 1; } 766 void inc_entry_load_failed_cnt(uint kind) { check_kind(kind); rs._entry_kinds[kind]._load_failed_cnt += 1; } 767 768 void inc_nmethod_loaded_cnt(uint lvl) { check_complevel(lvl); rs._nmethods[lvl]._loaded_cnt += 1; } 769 void inc_nmethod_invalidated_cnt(uint lvl) { check_complevel(lvl); rs._nmethods[lvl]._invalidated_cnt += 1; } 770 void inc_nmethod_load_failed_cnt(uint lvl) { check_complevel(lvl); rs._nmethods[lvl]._load_failed_cnt += 1; } 771 772 uint entry_loaded_count(uint kind) { check_kind(kind); return rs._entry_kinds[kind]._loaded_cnt; } 773 uint entry_invalidated_count(uint kind) { check_kind(kind); return rs._entry_kinds[kind]._invalidated_cnt; } 774 uint entry_load_failed_count(uint kind) { check_kind(kind); return rs._entry_kinds[kind]._load_failed_cnt; } 775 776 uint nmethod_loaded_count(uint lvl) { check_complevel(lvl); return rs._nmethods[lvl]._loaded_cnt; } 777 uint nmethod_invalidated_count(uint lvl) { check_complevel(lvl); return rs._nmethods[lvl]._invalidated_cnt; } 778 uint nmethod_load_failed_count(uint lvl) { check_complevel(lvl); return rs._nmethods[lvl]._load_failed_cnt; } 779 780 void inc_loaded_cnt(AOTCodeEntry* entry) { 781 inc_entry_loaded_cnt(entry->kind()); 782 if (entry->is_code()) { 783 entry->for_preload() ? inc_nmethod_loaded_cnt(AOTCompLevel_count-1) 784 : inc_nmethod_loaded_cnt(entry->comp_level()); 785 } 786 } 787 788 void inc_invalidated_cnt(AOTCodeEntry* entry) { 789 inc_entry_invalidated_cnt(entry->kind()); 790 if (entry->is_code()) { 791 entry->for_preload() ? inc_nmethod_invalidated_cnt(AOTCompLevel_count-1) 792 : inc_nmethod_invalidated_cnt(entry->comp_level()); 793 } 794 } 795 796 void inc_load_failed_cnt(AOTCodeEntry* entry) { 797 inc_entry_load_failed_cnt(entry->kind()); 798 if (entry->is_code()) { 799 entry->for_preload() ? inc_nmethod_load_failed_cnt(AOTCompLevel_count-1) 800 : inc_nmethod_load_failed_cnt(entry->comp_level()); 801 } 802 } 803 804 void collect_entry_runtime_stats(AOTCodeEntry* entry) { 805 if (entry->is_loaded()) { 806 inc_loaded_cnt(entry); 807 } 808 if (entry->not_entrant()) { 809 inc_invalidated_cnt(entry); 810 } 811 if (entry->load_fail()) { 812 inc_load_failed_cnt(entry); 813 } 814 } 815 816 void collect_all_stats(AOTCodeEntry* entry) { 817 collect_entry_stats(entry); 818 collect_entry_runtime_stats(entry); 819 } 820 821 AOTCodeStats() { 822 memset(this, 0, sizeof(AOTCodeStats)); 823 } 824 }; 825 826 // code cache internal runtime constants area used by AOT code 827 class AOTRuntimeConstants { 828 friend class AOTCodeCache; 829 private: 830 uint _grain_shift; 831 uint _card_shift; 832 static address _field_addresses_list[]; 833 static AOTRuntimeConstants _aot_runtime_constants; 834 // private constructor for unique singleton 835 AOTRuntimeConstants() { } 836 // private for use by friend class AOTCodeCache 837 static void initialize_from_runtime(); 838 public: 839 #if INCLUDE_CDS 840 static bool contains(address adr) { 841 address base = (address)&_aot_runtime_constants; 842 address hi = base + sizeof(AOTRuntimeConstants); 843 return (base <= adr && adr < hi); 844 } 845 static address grain_shift_address() { return (address)&_aot_runtime_constants._grain_shift; } 846 static address card_shift_address() { return (address)&_aot_runtime_constants._card_shift; } 847 static address* field_addresses_list() { 848 return _field_addresses_list; 849 } 850 #else 851 static bool contains(address adr) { return false; } 852 static address grain_shift_address() { return nullptr; } 853 static address card_shift_address() { return nullptr; } 854 static address* field_addresses_list() { return nullptr; } 855 #endif 856 }; 857 858 #endif // SHARE_CODE_AOTCODECACHE_HPP