1 /* 2 * Copyright (c) 2012, 2017, 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 #include "precompiled.hpp" 26 #include "classfile/classListParser.hpp" 27 #include "classfile/classLoaderExt.hpp" 28 #include "classfile/dictionary.hpp" 29 #include "classfile/loaderConstraints.hpp" 30 #include "classfile/placeholders.hpp" 31 #include "classfile/sharedClassUtil.hpp" 32 #include "classfile/symbolTable.hpp" 33 #include "classfile/systemDictionary.hpp" 34 #include "classfile/systemDictionaryShared.hpp" 35 #include "code/codeCache.hpp" 36 #include "gc/shared/gcLocker.hpp" 37 #include "interpreter/bytecodeStream.hpp" 38 #include "interpreter/bytecodes.hpp" 39 #include "logging/log.hpp" 40 #include "logging/logMessage.hpp" 41 #include "memory/filemap.hpp" 42 #include "memory/metaspace.hpp" 43 #include "memory/metaspaceShared.hpp" 44 #include "memory/resourceArea.hpp" 45 #include "oops/instanceClassLoaderKlass.hpp" 46 #include "oops/instanceMirrorKlass.hpp" 47 #include "oops/instanceRefKlass.hpp" 48 #include "oops/objArrayKlass.hpp" 49 #include "oops/objArrayOop.hpp" 50 #include "oops/oop.inline.hpp" 51 #include "oops/typeArrayKlass.hpp" 52 #include "prims/jvm.h" 53 #include "runtime/timerTrace.hpp" 54 #include "runtime/os.hpp" 55 #include "runtime/signature.hpp" 56 #include "runtime/vmThread.hpp" 57 #include "runtime/vm_operations.hpp" 58 #include "utilities/align.hpp" 59 #include "utilities/defaultStream.hpp" 60 #include "utilities/hashtable.inline.hpp" 61 62 int MetaspaceShared::_max_alignment = 0; 63 64 ReservedSpace* MetaspaceShared::_shared_rs = NULL; 65 66 MetaspaceSharedStats MetaspaceShared::_stats; 67 68 bool MetaspaceShared::_has_error_classes; 69 bool MetaspaceShared::_archive_loading_failed = false; 70 bool MetaspaceShared::_remapped_readwrite = false; 71 address MetaspaceShared::_cds_i2i_entry_code_buffers = NULL; 72 size_t MetaspaceShared::_cds_i2i_entry_code_buffers_size = 0; 73 SharedMiscRegion MetaspaceShared::_mc; 74 SharedMiscRegion MetaspaceShared::_md; 75 SharedMiscRegion MetaspaceShared::_od; 76 77 void SharedMiscRegion::initialize(ReservedSpace rs, size_t committed_byte_size, SharedSpaceType space_type) { 78 _vs.initialize(rs, committed_byte_size); 79 _alloc_top = _vs.low(); 80 _space_type = space_type; 81 } 82 83 // NOT thread-safe, but this is called during dump time in single-threaded mode. 84 char* SharedMiscRegion::alloc(size_t num_bytes) { 85 assert(DumpSharedSpaces, "dump time only"); 86 size_t alignment = sizeof(char*); 87 num_bytes = align_up(num_bytes, alignment); 88 _alloc_top = align_up(_alloc_top, alignment); 89 if (_alloc_top + num_bytes > _vs.high()) { 90 report_out_of_shared_space(_space_type); 91 } 92 93 char* p = _alloc_top; 94 _alloc_top += num_bytes; 95 96 memset(p, 0, num_bytes); 97 return p; 98 } 99 100 void MetaspaceShared::initialize_shared_rs(ReservedSpace* rs) { 101 assert(DumpSharedSpaces, "dump time only"); 102 _shared_rs = rs; 103 104 size_t core_spaces_size = FileMapInfo::core_spaces_size(); 105 size_t metadata_size = SharedReadOnlySize + SharedReadWriteSize; 106 107 // Split into the core and optional sections 108 ReservedSpace core_data = _shared_rs->first_part(core_spaces_size); 109 ReservedSpace optional_data = _shared_rs->last_part(core_spaces_size); 110 111 // The RO/RW and the misc sections 112 ReservedSpace shared_ro_rw = core_data.first_part(metadata_size); 113 ReservedSpace misc_section = core_data.last_part(metadata_size); 114 115 // Now split the misc code and misc data sections. 116 ReservedSpace md_rs = misc_section.first_part(SharedMiscDataSize); 117 ReservedSpace mc_rs = misc_section.last_part(SharedMiscDataSize); 118 119 _md.initialize(md_rs, SharedMiscDataSize, SharedMiscData); 120 _mc.initialize(mc_rs, SharedMiscCodeSize, SharedMiscCode); 121 _od.initialize(optional_data, metadata_size, SharedOptional); 122 } 123 124 // Read/write a data stream for restoring/preserving metadata pointers and 125 // miscellaneous data from/to the shared archive file. 126 127 void MetaspaceShared::serialize(SerializeClosure* soc, GrowableArray<MemRegion> *string_space, 128 size_t* space_size) { 129 int tag = 0; 130 soc->do_tag(--tag); 131 132 // Verify the sizes of various metadata in the system. 133 soc->do_tag(sizeof(Method)); 134 soc->do_tag(sizeof(ConstMethod)); 135 soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE)); 136 soc->do_tag(sizeof(ConstantPool)); 137 soc->do_tag(sizeof(ConstantPoolCache)); 138 soc->do_tag(objArrayOopDesc::base_offset_in_bytes()); 139 soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE)); 140 soc->do_tag(sizeof(Symbol)); 141 142 // Dump/restore miscellaneous metadata. 143 Universe::serialize(soc, true); 144 soc->do_tag(--tag); 145 146 // Dump/restore references to commonly used names and signatures. 147 vmSymbols::serialize(soc); 148 soc->do_tag(--tag); 149 150 // Dump/restore the symbol and string tables 151 SymbolTable::serialize(soc); 152 StringTable::serialize(soc, string_space, space_size); 153 soc->do_tag(--tag); 154 155 soc->do_tag(666); 156 } 157 158 address MetaspaceShared::cds_i2i_entry_code_buffers(size_t total_size) { 159 if (DumpSharedSpaces) { 160 if (_cds_i2i_entry_code_buffers == NULL) { 161 _cds_i2i_entry_code_buffers = (address)misc_data_space_alloc(total_size); 162 _cds_i2i_entry_code_buffers_size = total_size; 163 } 164 } else if (UseSharedSpaces) { 165 assert(_cds_i2i_entry_code_buffers != NULL, "must already been initialized"); 166 } else { 167 return NULL; 168 } 169 170 assert(_cds_i2i_entry_code_buffers_size == total_size, "must not change"); 171 return _cds_i2i_entry_code_buffers; 172 } 173 174 // CDS code for dumping shared archive. 175 176 // Global object for holding classes that have been loaded. Since this 177 // is run at a safepoint just before exit, this is the entire set of classes. 178 static GrowableArray<Klass*>* _global_klass_objects; 179 class CollectClassesClosure : public KlassClosure { 180 void do_klass(Klass* k) { 181 _global_klass_objects->append_if_missing(k); 182 } 183 }; 184 185 static void remove_unshareable_in_classes() { 186 for (int i = 0; i < _global_klass_objects->length(); i++) { 187 Klass* k = _global_klass_objects->at(i); 188 k->remove_unshareable_info(); 189 } 190 } 191 192 static void rewrite_nofast_bytecode(Method* method) { 193 RawBytecodeStream bcs(method); 194 while (!bcs.is_last_bytecode()) { 195 Bytecodes::Code opcode = bcs.raw_next(); 196 switch (opcode) { 197 case Bytecodes::_getfield: *bcs.bcp() = Bytecodes::_nofast_getfield; break; 198 case Bytecodes::_putfield: *bcs.bcp() = Bytecodes::_nofast_putfield; break; 199 case Bytecodes::_aload_0: *bcs.bcp() = Bytecodes::_nofast_aload_0; break; 200 case Bytecodes::_iload: { 201 if (!bcs.is_wide()) { 202 *bcs.bcp() = Bytecodes::_nofast_iload; 203 } 204 break; 205 } 206 default: break; 207 } 208 } 209 } 210 211 // Walk all methods in the class list to ensure that they won't be modified at 212 // run time. This includes: 213 // [1] Rewrite all bytecodes as needed, so that the ConstMethod* will not be modified 214 // at run time by RewriteBytecodes/RewriteFrequentPairs 215 // [2] Assign a fingerprint, so one doesn't need to be assigned at run-time. 216 static void rewrite_nofast_bytecodes_and_calculate_fingerprints() { 217 for (int i = 0; i < _global_klass_objects->length(); i++) { 218 Klass* k = _global_klass_objects->at(i); 219 if (k->is_instance_klass()) { 220 InstanceKlass* ik = InstanceKlass::cast(k); 221 for (int i = 0; i < ik->methods()->length(); i++) { 222 Method* m = ik->methods()->at(i); 223 rewrite_nofast_bytecode(m); 224 Fingerprinter fp(m); 225 // The side effect of this call sets method's fingerprint field. 226 fp.fingerprint(); 227 } 228 } 229 } 230 } 231 232 // Objects of the Metadata types (such as Klass and ConstantPool) have C++ vtables. 233 // (In GCC this is the field <Type>::_vptr, i.e., first word in the object.) 234 // 235 // Addresses of the vtables and the methods may be different across JVM runs, 236 // if libjvm.so is dynamically loaded at a different base address. 237 // 238 // To ensure that the Metadata objects in the CDS archive always have the correct vtable: 239 // 240 // + at dump time: we redirect the _vptr to point to our own vtables inside 241 // the CDS image 242 // + at run time: we clone the actual contents of the vtables from libjvm.so 243 // into our own tables. 244 245 // Currently, the archive contain ONLY the following types of objects that have C++ vtables. 246 #define CPP_VTABLE_PATCH_TYPES_DO(f) \ 247 f(ConstantPool) \ 248 f(InstanceKlass) \ 249 f(InstanceClassLoaderKlass) \ 250 f(InstanceMirrorKlass) \ 251 f(InstanceRefKlass) \ 252 f(Method) \ 253 f(ObjArrayKlass) \ 254 f(TypeArrayKlass) 255 256 class CppVtableInfo { 257 intptr_t _vtable_size; 258 intptr_t _cloned_vtable[1]; 259 public: 260 static int num_slots(int vtable_size) { 261 return 1 + vtable_size; // Need to add the space occupied by _vtable_size; 262 } 263 int vtable_size() { return int(uintx(_vtable_size)); } 264 void set_vtable_size(int n) { _vtable_size = intptr_t(n); } 265 intptr_t* cloned_vtable() { return &_cloned_vtable[0]; } 266 void zero() { memset(_cloned_vtable, 0, sizeof(intptr_t) * vtable_size()); } 267 // Returns the address of the next CppVtableInfo that can be placed immediately after this CppVtableInfo 268 intptr_t* next(int vtable_size) { 269 return &_cloned_vtable[vtable_size]; 270 } 271 }; 272 273 template <class T> class CppVtableCloner : public T { 274 static intptr_t* vtable_of(Metadata& m) { 275 return *((intptr_t**)&m); 276 } 277 static CppVtableInfo* _info; 278 279 static int get_vtable_length(const char* name); 280 281 public: 282 // Allocate and initialize the C++ vtable, starting from top, but do not go past end. 283 static intptr_t* allocate(const char* name, intptr_t* top, intptr_t* end); 284 285 // Clone the vtable to ... 286 static intptr_t* clone_vtable(const char* name, CppVtableInfo* info); 287 288 static void zero_vtable_clone() { 289 assert(DumpSharedSpaces, "dump-time only"); 290 _info->zero(); 291 } 292 293 // Switch the vtable pointer to point to the cloned vtable. 294 static void patch(Metadata* obj) { 295 assert(DumpSharedSpaces, "dump-time only"); 296 *(void**)obj = (void*)(_info->cloned_vtable()); 297 } 298 299 static bool is_valid_shared_object(const T* obj) { 300 intptr_t* vptr = *(intptr_t**)obj; 301 return vptr == _info->cloned_vtable(); 302 } 303 }; 304 305 template <class T> CppVtableInfo* CppVtableCloner<T>::_info = NULL; 306 307 template <class T> 308 intptr_t* CppVtableCloner<T>::allocate(const char* name, intptr_t* top, intptr_t* end) { 309 int n = get_vtable_length(name); 310 _info = (CppVtableInfo*)top; 311 intptr_t* next = _info->next(n); 312 313 if (next > end) { 314 report_out_of_shared_space(SharedMiscData); 315 } 316 _info->set_vtable_size(n); 317 318 intptr_t* p = clone_vtable(name, _info); 319 assert(p == next, "must be"); 320 321 return p; 322 } 323 324 template <class T> 325 intptr_t* CppVtableCloner<T>::clone_vtable(const char* name, CppVtableInfo* info) { 326 if (!DumpSharedSpaces) { 327 assert(_info == 0, "_info is initialized only at dump time"); 328 _info = info; // Remember it -- it will be used by MetaspaceShared::is_valid_shared_method() 329 } 330 T tmp; // Allocate temporary dummy metadata object to get to the original vtable. 331 int n = info->vtable_size(); 332 intptr_t* srcvtable = vtable_of(tmp); 333 intptr_t* dstvtable = info->cloned_vtable(); 334 335 // We already checked (and, if necessary, adjusted n) when the vtables were allocated, so we are 336 // safe to do memcpy. 337 log_debug(cds, vtables)("Copying %3d vtable entries for %s", n, name); 338 memcpy(dstvtable, srcvtable, sizeof(intptr_t) * n); 339 return dstvtable + n; 340 } 341 342 // To determine the size of the vtable for each type, we use the following 343 // trick by declaring 2 subclasses: 344 // 345 // class CppVtableTesterA: public InstanceKlass {virtual int last_virtual_method() {return 1;} }; 346 // class CppVtableTesterB: public InstanceKlass {virtual void* last_virtual_method() {return NULL}; }; 347 // 348 // CppVtableTesterA and CppVtableTesterB's vtables have the following properties: 349 // - Their size (N+1) is exactly one more than the size of InstanceKlass's vtable (N) 350 // - The first N entries have are exactly the same as in InstanceKlass's vtable. 351 // - Their last entry is different. 352 // 353 // So to determine the value of N, we just walk CppVtableTesterA and CppVtableTesterB's tables 354 // and find the first entry that's different. 355 // 356 // This works on all C++ compilers supported by Oracle, but you may need to tweak it for more 357 // esoteric compilers. 358 359 template <class T> class CppVtableTesterB: public T { 360 public: 361 virtual int last_virtual_method() {return 1;} 362 }; 363 364 template <class T> class CppVtableTesterA : public T { 365 public: 366 virtual void* last_virtual_method() { 367 // Make this different than CppVtableTesterB::last_virtual_method so the C++ 368 // compiler/linker won't alias the two functions. 369 return NULL; 370 } 371 }; 372 373 template <class T> 374 int CppVtableCloner<T>::get_vtable_length(const char* name) { 375 CppVtableTesterA<T> a; 376 CppVtableTesterB<T> b; 377 378 intptr_t* avtable = vtable_of(a); 379 intptr_t* bvtable = vtable_of(b); 380 381 // Start at slot 1, because slot 0 may be RTTI (on Solaris/Sparc) 382 int vtable_len = 1; 383 for (; ; vtable_len++) { 384 if (avtable[vtable_len] != bvtable[vtable_len]) { 385 break; 386 } 387 } 388 log_debug(cds, vtables)("Found %3d vtable entries for %s", vtable_len, name); 389 390 return vtable_len; 391 } 392 393 #define ALLOC_CPP_VTABLE_CLONE(c) \ 394 top = CppVtableCloner<c>::allocate(#c, top, end); 395 396 #define CLONE_CPP_VTABLE(c) \ 397 p = CppVtableCloner<c>::clone_vtable(#c, (CppVtableInfo*)p); 398 399 #define ZERO_CPP_VTABLE(c) \ 400 CppVtableCloner<c>::zero_vtable_clone(); 401 402 // This can be called at both dump time and run time. 403 intptr_t* MetaspaceShared::clone_cpp_vtables(intptr_t* p) { 404 assert(DumpSharedSpaces || UseSharedSpaces, "sanity"); 405 CPP_VTABLE_PATCH_TYPES_DO(CLONE_CPP_VTABLE); 406 return p; 407 } 408 409 void MetaspaceShared::zero_cpp_vtable_clones_for_writing() { 410 assert(DumpSharedSpaces, "dump-time only"); 411 CPP_VTABLE_PATCH_TYPES_DO(ZERO_CPP_VTABLE); 412 } 413 414 // Allocate and initialize the C++ vtables, starting from top, but do not go past end. 415 intptr_t* MetaspaceShared::allocate_cpp_vtable_clones(intptr_t* top, intptr_t* end) { 416 assert(DumpSharedSpaces, "dump-time only"); 417 // Layout (each slot is a intptr_t): 418 // [number of slots in the first vtable = n1] 419 // [ <n1> slots for the first vtable] 420 // [number of slots in the first second = n2] 421 // [ <n2> slots for the second vtable] 422 // ... 423 // The order of the vtables is the same as the CPP_VTAB_PATCH_TYPES_DO macro. 424 CPP_VTABLE_PATCH_TYPES_DO(ALLOC_CPP_VTABLE_CLONE); 425 return top; 426 } 427 428 // Switch the vtable pointer to point to the cloned vtable. We assume the 429 // vtable pointer is in first slot in object. 430 void MetaspaceShared::patch_cpp_vtable_pointers() { 431 int n = _global_klass_objects->length(); 432 for (int i = 0; i < n; i++) { 433 Klass* obj = _global_klass_objects->at(i); 434 if (obj->is_instance_klass()) { 435 InstanceKlass* ik = InstanceKlass::cast(obj); 436 if (ik->is_class_loader_instance_klass()) { 437 CppVtableCloner<InstanceClassLoaderKlass>::patch(ik); 438 } else if (ik->is_reference_instance_klass()) { 439 CppVtableCloner<InstanceRefKlass>::patch(ik); 440 } else if (ik->is_mirror_instance_klass()) { 441 CppVtableCloner<InstanceMirrorKlass>::patch(ik); 442 } else { 443 CppVtableCloner<InstanceKlass>::patch(ik); 444 } 445 ConstantPool* cp = ik->constants(); 446 CppVtableCloner<ConstantPool>::patch(cp); 447 for (int j = 0; j < ik->methods()->length(); j++) { 448 Method* m = ik->methods()->at(j); 449 CppVtableCloner<Method>::patch(m); 450 assert(CppVtableCloner<Method>::is_valid_shared_object(m), "must be"); 451 } 452 } else if (obj->is_objArray_klass()) { 453 CppVtableCloner<ObjArrayKlass>::patch(obj); 454 } else { 455 assert(obj->is_typeArray_klass(), "sanity"); 456 CppVtableCloner<TypeArrayKlass>::patch(obj); 457 } 458 } 459 } 460 461 bool MetaspaceShared::is_valid_shared_method(const Method* m) { 462 assert(is_in_shared_space(m), "must be"); 463 return CppVtableCloner<Method>::is_valid_shared_object(m); 464 } 465 466 // Closure for serializing initialization data out to a data area to be 467 // written to the shared file. 468 469 class WriteClosure : public SerializeClosure { 470 private: 471 intptr_t* top; 472 char* end; 473 474 inline void check_space() { 475 if ((char*)top + sizeof(intptr_t) > end) { 476 report_out_of_shared_space(SharedMiscData); 477 } 478 } 479 480 public: 481 WriteClosure(char* md_top, char* md_end) { 482 top = (intptr_t*)md_top; 483 end = md_end; 484 } 485 486 char* get_top() { return (char*)top; } 487 488 void do_ptr(void** p) { 489 check_space(); 490 *top = (intptr_t)*p; 491 ++top; 492 } 493 494 void do_u4(u4* p) { 495 void* ptr = (void*)(uintx(*p)); 496 do_ptr(&ptr); 497 } 498 499 void do_tag(int tag) { 500 check_space(); 501 *top = (intptr_t)tag; 502 ++top; 503 } 504 505 void do_region(u_char* start, size_t size) { 506 if ((char*)top + size > end) { 507 report_out_of_shared_space(SharedMiscData); 508 } 509 assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment"); 510 assert(size % sizeof(intptr_t) == 0, "bad size"); 511 do_tag((int)size); 512 while (size > 0) { 513 *top = *(intptr_t*)start; 514 ++top; 515 start += sizeof(intptr_t); 516 size -= sizeof(intptr_t); 517 } 518 } 519 520 bool reading() const { return false; } 521 }; 522 523 // This is for dumping detailed statistics for the allocations 524 // in the shared spaces. 525 class DumpAllocClosure : public Metaspace::AllocRecordClosure { 526 public: 527 528 // Here's poor man's enum inheritance 529 #define SHAREDSPACE_OBJ_TYPES_DO(f) \ 530 METASPACE_OBJ_TYPES_DO(f) \ 531 f(SymbolHashentry) \ 532 f(SymbolBucket) \ 533 f(StringHashentry) \ 534 f(StringBucket) \ 535 f(Other) 536 537 #define SHAREDSPACE_OBJ_TYPE_DECLARE(name) name ## Type, 538 #define SHAREDSPACE_OBJ_TYPE_NAME_CASE(name) case name ## Type: return #name; 539 540 enum Type { 541 // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc 542 SHAREDSPACE_OBJ_TYPES_DO(SHAREDSPACE_OBJ_TYPE_DECLARE) 543 _number_of_types 544 }; 545 546 static const char * type_name(Type type) { 547 switch(type) { 548 SHAREDSPACE_OBJ_TYPES_DO(SHAREDSPACE_OBJ_TYPE_NAME_CASE) 549 default: 550 ShouldNotReachHere(); 551 return NULL; 552 } 553 } 554 555 public: 556 enum { 557 RO = 0, 558 RW = 1 559 }; 560 561 int _counts[2][_number_of_types]; 562 int _bytes [2][_number_of_types]; 563 int _which; 564 565 DumpAllocClosure() { 566 memset(_counts, 0, sizeof(_counts)); 567 memset(_bytes, 0, sizeof(_bytes)); 568 }; 569 570 void iterate_metaspace(Metaspace* space, int which) { 571 assert(which == RO || which == RW, "sanity"); 572 _which = which; 573 space->iterate(this); 574 } 575 576 virtual void doit(address ptr, MetaspaceObj::Type type, int byte_size) { 577 assert(int(type) >= 0 && type < MetaspaceObj::_number_of_types, "sanity"); 578 _counts[_which][type] ++; 579 _bytes [_which][type] += byte_size; 580 } 581 582 void dump_stats(int ro_all, int rw_all, int md_all, int mc_all); 583 }; 584 585 void DumpAllocClosure::dump_stats(int ro_all, int rw_all, int md_all, int mc_all) { 586 rw_all += (md_all + mc_all); // md and mc are all mapped Read/Write 587 int other_bytes = md_all + mc_all; 588 589 // Calculate size of data that was not allocated by Metaspace::allocate() 590 MetaspaceSharedStats *stats = MetaspaceShared::stats(); 591 592 // symbols 593 _counts[RO][SymbolHashentryType] = stats->symbol.hashentry_count; 594 _bytes [RO][SymbolHashentryType] = stats->symbol.hashentry_bytes; 595 _bytes [RO][TypeArrayU4Type] -= stats->symbol.hashentry_bytes; 596 597 _counts[RO][SymbolBucketType] = stats->symbol.bucket_count; 598 _bytes [RO][SymbolBucketType] = stats->symbol.bucket_bytes; 599 _bytes [RO][TypeArrayU4Type] -= stats->symbol.bucket_bytes; 600 601 // strings 602 _counts[RO][StringHashentryType] = stats->string.hashentry_count; 603 _bytes [RO][StringHashentryType] = stats->string.hashentry_bytes; 604 _bytes [RO][TypeArrayU4Type] -= stats->string.hashentry_bytes; 605 606 _counts[RO][StringBucketType] = stats->string.bucket_count; 607 _bytes [RO][StringBucketType] = stats->string.bucket_bytes; 608 _bytes [RO][TypeArrayU4Type] -= stats->string.bucket_bytes; 609 610 // TODO: count things like dictionary, vtable, etc 611 _bytes[RW][OtherType] = other_bytes; 612 613 // prevent divide-by-zero 614 if (ro_all < 1) { 615 ro_all = 1; 616 } 617 if (rw_all < 1) { 618 rw_all = 1; 619 } 620 621 int all_ro_count = 0; 622 int all_ro_bytes = 0; 623 int all_rw_count = 0; 624 int all_rw_bytes = 0; 625 626 // To make fmt_stats be a syntactic constant (for format warnings), use #define. 627 #define fmt_stats "%-20s: %8d %10d %5.1f | %8d %10d %5.1f | %8d %10d %5.1f" 628 const char *sep = "--------------------+---------------------------+---------------------------+--------------------------"; 629 const char *hdr = " ro_cnt ro_bytes % | rw_cnt rw_bytes % | all_cnt all_bytes %"; 630 631 ResourceMark rm; 632 LogMessage(cds) msg; 633 stringStream info_stream; 634 635 info_stream.print_cr("Detailed metadata info (rw includes md and mc):"); 636 info_stream.print_cr("%s", hdr); 637 info_stream.print_cr("%s", sep); 638 for (int type = 0; type < int(_number_of_types); type ++) { 639 const char *name = type_name((Type)type); 640 int ro_count = _counts[RO][type]; 641 int ro_bytes = _bytes [RO][type]; 642 int rw_count = _counts[RW][type]; 643 int rw_bytes = _bytes [RW][type]; 644 int count = ro_count + rw_count; 645 int bytes = ro_bytes + rw_bytes; 646 647 double ro_perc = 100.0 * double(ro_bytes) / double(ro_all); 648 double rw_perc = 100.0 * double(rw_bytes) / double(rw_all); 649 double perc = 100.0 * double(bytes) / double(ro_all + rw_all); 650 651 info_stream.print_cr(fmt_stats, name, 652 ro_count, ro_bytes, ro_perc, 653 rw_count, rw_bytes, rw_perc, 654 count, bytes, perc); 655 656 all_ro_count += ro_count; 657 all_ro_bytes += ro_bytes; 658 all_rw_count += rw_count; 659 all_rw_bytes += rw_bytes; 660 } 661 662 int all_count = all_ro_count + all_rw_count; 663 int all_bytes = all_ro_bytes + all_rw_bytes; 664 665 double all_ro_perc = 100.0 * double(all_ro_bytes) / double(ro_all); 666 double all_rw_perc = 100.0 * double(all_rw_bytes) / double(rw_all); 667 double all_perc = 100.0 * double(all_bytes) / double(ro_all + rw_all); 668 669 info_stream.print_cr("%s", sep); 670 info_stream.print_cr(fmt_stats, "Total", 671 all_ro_count, all_ro_bytes, all_ro_perc, 672 all_rw_count, all_rw_bytes, all_rw_perc, 673 all_count, all_bytes, all_perc); 674 675 assert(all_ro_bytes == ro_all, "everything should have been counted"); 676 assert(all_rw_bytes == rw_all, "everything should have been counted"); 677 678 msg.info("%s", info_stream.as_string()); 679 #undef fmt_stats 680 } 681 682 // Populate the shared space. 683 684 class VM_PopulateDumpSharedSpace: public VM_Operation { 685 private: 686 ClassLoaderData* _loader_data; 687 GrowableArray<Klass*> *_class_promote_order; 688 VirtualSpace _md_vs; 689 VirtualSpace _mc_vs; 690 VirtualSpace _od_vs; 691 GrowableArray<MemRegion> *_string_regions; 692 693 public: 694 VM_PopulateDumpSharedSpace(ClassLoaderData* loader_data, 695 GrowableArray<Klass*> *class_promote_order) : 696 _loader_data(loader_data) { 697 _class_promote_order = class_promote_order; 698 } 699 700 VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; } 701 void doit(); // outline because gdb sucks 702 703 private: 704 void handle_misc_data_space_failure(bool success) { 705 if (!success) { 706 report_out_of_shared_space(SharedMiscData); 707 } 708 } 709 }; // class VM_PopulateDumpSharedSpace 710 711 void VM_PopulateDumpSharedSpace::doit() { 712 Thread* THREAD = VMThread::vm_thread(); 713 NOT_PRODUCT(SystemDictionary::verify();) 714 // The following guarantee is meant to ensure that no loader constraints 715 // exist yet, since the constraints table is not shared. This becomes 716 // more important now that we don't re-initialize vtables/itables for 717 // shared classes at runtime, where constraints were previously created. 718 guarantee(SystemDictionary::constraints()->number_of_entries() == 0, 719 "loader constraints are not saved"); 720 guarantee(SystemDictionary::placeholders()->number_of_entries() == 0, 721 "placeholders are not saved"); 722 // Revisit and implement this if we prelink method handle call sites: 723 guarantee(SystemDictionary::invoke_method_table() == NULL || 724 SystemDictionary::invoke_method_table()->number_of_entries() == 0, 725 "invoke method table is not saved"); 726 727 // At this point, many classes have been loaded. 728 // Gather systemDictionary classes in a global array and do everything to 729 // that so we don't have to walk the SystemDictionary again. 730 _global_klass_objects = new GrowableArray<Klass*>(1000); 731 CollectClassesClosure collect_classes; 732 ClassLoaderDataGraph::loaded_classes_do(&collect_classes); 733 734 tty->print_cr("Number of classes %d", _global_klass_objects->length()); 735 { 736 int num_type_array = 0, num_obj_array = 0, num_inst = 0; 737 for (int i = 0; i < _global_klass_objects->length(); i++) { 738 Klass* k = _global_klass_objects->at(i); 739 if (k->is_instance_klass()) { 740 num_inst ++; 741 } else if (k->is_objArray_klass()) { 742 num_obj_array ++; 743 } else { 744 assert(k->is_typeArray_klass(), "sanity"); 745 num_type_array ++; 746 } 747 } 748 tty->print_cr(" instance classes = %5d", num_inst); 749 tty->print_cr(" obj array classes = %5d", num_obj_array); 750 tty->print_cr(" type array classes = %5d", num_type_array); 751 } 752 753 754 // Ensure the ConstMethods won't be modified at run-time 755 tty->print("Updating ConstMethods ... "); 756 rewrite_nofast_bytecodes_and_calculate_fingerprints(); 757 tty->print_cr("done. "); 758 759 // Remove all references outside the metadata 760 tty->print("Removing unshareable information ... "); 761 remove_unshareable_in_classes(); 762 tty->print_cr("done. "); 763 764 // Set up the misc data, misc code and optional data segments. 765 _md_vs = *MetaspaceShared::misc_data_region()->virtual_space(); 766 _mc_vs = *MetaspaceShared::misc_code_region()->virtual_space(); 767 _od_vs = *MetaspaceShared::optional_data_region()->virtual_space(); 768 char* md_low = _md_vs.low(); 769 char* md_top = MetaspaceShared::misc_data_region()->alloc_top(); 770 char* md_end = _md_vs.high(); 771 char* mc_low = _mc_vs.low(); 772 char* mc_top = MetaspaceShared::misc_code_region()->alloc_top(); 773 char* mc_end = _mc_vs.high(); 774 char* od_low = _od_vs.low(); 775 char* od_top = MetaspaceShared::optional_data_region()->alloc_top(); 776 char* od_end = _od_vs.high(); 777 778 char* vtbl_list = md_top; 779 md_top = (char*)MetaspaceShared::allocate_cpp_vtable_clones((intptr_t*)md_top, (intptr_t*)md_end); 780 781 // We don't use MC section anymore. We will remove it in a future RFE. For now, put one 782 // byte inside so the region writing/mapping code works. 783 mc_top ++; 784 785 // Reorder the system dictionary. (Moving the symbols affects 786 // how the hash table indices are calculated.) 787 // Not doing this either. 788 789 SystemDictionary::reorder_dictionary(); 790 NOT_PRODUCT(SystemDictionary::verify();) 791 SystemDictionary::copy_buckets(&md_top, md_end); 792 793 SystemDictionary::copy_table(&md_top, md_end); 794 795 // Write the other data to the output array. 796 // SymbolTable, StringTable and extra information for system dictionary 797 NOT_PRODUCT(SymbolTable::verify()); 798 NOT_PRODUCT(StringTable::verify()); 799 size_t ss_bytes = 0; 800 char* ss_low; 801 // The string space has maximum two regions. See FileMapInfo::write_string_regions() for details. 802 _string_regions = new GrowableArray<MemRegion>(2); 803 804 WriteClosure wc(md_top, md_end); 805 MetaspaceShared::serialize(&wc, _string_regions, &ss_bytes); 806 md_top = wc.get_top(); 807 ss_low = _string_regions->is_empty() ? NULL : (char*)_string_regions->first().start(); 808 809 // Print shared spaces all the time 810 Metaspace* ro_space = _loader_data->ro_metaspace(); 811 Metaspace* rw_space = _loader_data->rw_metaspace(); 812 813 // Allocated size of each space (may not be all occupied) 814 const size_t ro_alloced = ro_space->capacity_bytes_slow(Metaspace::NonClassType); 815 const size_t rw_alloced = rw_space->capacity_bytes_slow(Metaspace::NonClassType); 816 const size_t md_alloced = md_end-md_low; 817 const size_t mc_alloced = mc_end-mc_low; 818 const size_t od_alloced = od_end-od_low; 819 const size_t total_alloced = ro_alloced + rw_alloced + md_alloced + mc_alloced 820 + ss_bytes + od_alloced; 821 822 // Occupied size of each space. 823 const size_t ro_bytes = ro_space->used_bytes_slow(Metaspace::NonClassType); 824 const size_t rw_bytes = rw_space->used_bytes_slow(Metaspace::NonClassType); 825 const size_t md_bytes = size_t(md_top - md_low); 826 const size_t mc_bytes = size_t(mc_top - mc_low); 827 const size_t od_bytes = size_t(od_top - od_low); 828 829 // Percent of total size 830 const size_t total_bytes = ro_bytes + rw_bytes + md_bytes + mc_bytes + ss_bytes + od_bytes; 831 const double ro_t_perc = ro_bytes / double(total_bytes) * 100.0; 832 const double rw_t_perc = rw_bytes / double(total_bytes) * 100.0; 833 const double md_t_perc = md_bytes / double(total_bytes) * 100.0; 834 const double mc_t_perc = mc_bytes / double(total_bytes) * 100.0; 835 const double ss_t_perc = ss_bytes / double(total_bytes) * 100.0; 836 const double od_t_perc = od_bytes / double(total_bytes) * 100.0; 837 838 // Percent of fullness of each space 839 const double ro_u_perc = ro_bytes / double(ro_alloced) * 100.0; 840 const double rw_u_perc = rw_bytes / double(rw_alloced) * 100.0; 841 const double md_u_perc = md_bytes / double(md_alloced) * 100.0; 842 const double mc_u_perc = mc_bytes / double(mc_alloced) * 100.0; 843 const double od_u_perc = od_bytes / double(od_alloced) * 100.0; 844 const double total_u_perc = total_bytes / double(total_alloced) * 100.0; 845 846 #define fmt_space "%s space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used] at " INTPTR_FORMAT 847 tty->print_cr(fmt_space, "ro", ro_bytes, ro_t_perc, ro_alloced, ro_u_perc, p2i(ro_space->bottom())); 848 tty->print_cr(fmt_space, "rw", rw_bytes, rw_t_perc, rw_alloced, rw_u_perc, p2i(rw_space->bottom())); 849 tty->print_cr(fmt_space, "md", md_bytes, md_t_perc, md_alloced, md_u_perc, p2i(md_low)); 850 tty->print_cr(fmt_space, "mc", mc_bytes, mc_t_perc, mc_alloced, mc_u_perc, p2i(mc_low)); 851 tty->print_cr(fmt_space, "st", ss_bytes, ss_t_perc, ss_bytes, 100.0, p2i(ss_low)); 852 tty->print_cr(fmt_space, "od", od_bytes, od_t_perc, od_alloced, od_u_perc, p2i(od_low)); 853 tty->print_cr("total : " SIZE_FORMAT_W(9) " [100.0%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used]", 854 total_bytes, total_alloced, total_u_perc); 855 856 // During patching, some virtual methods may be called, so at this point 857 // the vtables must contain valid methods (as filled in by CppVtableCloner::allocate). 858 MetaspaceShared::patch_cpp_vtable_pointers(); 859 860 // The vtable clones contain addresses of the current process. 861 // We don't want to write these addresses into the archive. 862 MetaspaceShared::zero_cpp_vtable_clones_for_writing(); 863 864 // Create and write the archive file that maps the shared spaces. 865 866 FileMapInfo* mapinfo = new FileMapInfo(); 867 mapinfo->populate_header(MetaspaceShared::max_alignment()); 868 mapinfo->set_misc_data_patching_start(vtbl_list); 869 mapinfo->set_cds_i2i_entry_code_buffers(MetaspaceShared::cds_i2i_entry_code_buffers()); 870 mapinfo->set_cds_i2i_entry_code_buffers_size(MetaspaceShared::cds_i2i_entry_code_buffers_size()); 871 872 for (int pass=1; pass<=2; pass++) { 873 if (pass == 1) { 874 // The first pass doesn't actually write the data to disk. All it 875 // does is to update the fields in the mapinfo->_header. 876 } else { 877 // After the first pass, the contents of mapinfo->_header are finalized, 878 // so we can compute the header's CRC, and write the contents of the header 879 // and the regions into disk. 880 mapinfo->open_for_write(); 881 mapinfo->set_header_crc(mapinfo->compute_header_crc()); 882 } 883 mapinfo->write_header(); 884 mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true); 885 mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false); 886 mapinfo->write_region(MetaspaceShared::md, _md_vs.low(), 887 pointer_delta(md_top, _md_vs.low(), sizeof(char)), 888 SharedMiscDataSize, 889 false, true); 890 mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(), 891 pointer_delta(mc_top, _mc_vs.low(), sizeof(char)), 892 SharedMiscCodeSize, 893 true, true); 894 mapinfo->write_string_regions(_string_regions); 895 mapinfo->write_region(MetaspaceShared::od, _od_vs.low(), 896 pointer_delta(od_top, _od_vs.low(), sizeof(char)), 897 pointer_delta(od_end, _od_vs.low(), sizeof(char)), 898 true, false); 899 } 900 901 mapinfo->close(); 902 903 // Restore the vtable in case we invoke any virtual methods. 904 MetaspaceShared::clone_cpp_vtables((intptr_t*)vtbl_list); 905 906 if (log_is_enabled(Info, cds)) { 907 DumpAllocClosure dac; 908 dac.iterate_metaspace(_loader_data->ro_metaspace(), DumpAllocClosure::RO); 909 dac.iterate_metaspace(_loader_data->rw_metaspace(), DumpAllocClosure::RW); 910 911 dac.dump_stats(int(ro_bytes), int(rw_bytes), int(md_bytes), int(mc_bytes)); 912 } 913 #undef fmt_space 914 } 915 916 class LinkSharedClassesClosure : public KlassClosure { 917 Thread* THREAD; 918 bool _made_progress; 919 public: 920 LinkSharedClassesClosure(Thread* thread) : THREAD(thread), _made_progress(false) {} 921 922 void reset() { _made_progress = false; } 923 bool made_progress() const { return _made_progress; } 924 925 void do_klass(Klass* k) { 926 if (k->is_instance_klass()) { 927 InstanceKlass* ik = InstanceKlass::cast(k); 928 // Link the class to cause the bytecodes to be rewritten and the 929 // cpcache to be created. Class verification is done according 930 // to -Xverify setting. 931 _made_progress |= MetaspaceShared::try_link_class(ik, THREAD); 932 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class"); 933 } 934 } 935 }; 936 937 class CheckSharedClassesClosure : public KlassClosure { 938 bool _made_progress; 939 public: 940 CheckSharedClassesClosure() : _made_progress(false) {} 941 942 void reset() { _made_progress = false; } 943 bool made_progress() const { return _made_progress; } 944 void do_klass(Klass* k) { 945 if (k->is_instance_klass() && InstanceKlass::cast(k)->check_sharing_error_state()) { 946 _made_progress = true; 947 } 948 } 949 }; 950 951 void MetaspaceShared::check_shared_class_loader_type(Klass* k) { 952 if (k->is_instance_klass()) { 953 InstanceKlass* ik = InstanceKlass::cast(k); 954 u2 loader_type = ik->loader_type(); 955 ResourceMark rm; 956 guarantee(loader_type != 0, 957 "Class loader type is not set for this class %s", ik->name()->as_C_string()); 958 } 959 } 960 961 void MetaspaceShared::link_and_cleanup_shared_classes(TRAPS) { 962 // We need to iterate because verification may cause additional classes 963 // to be loaded. 964 LinkSharedClassesClosure link_closure(THREAD); 965 do { 966 link_closure.reset(); 967 ClassLoaderDataGraph::loaded_classes_do(&link_closure); 968 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class"); 969 } while (link_closure.made_progress()); 970 971 if (_has_error_classes) { 972 // Mark all classes whose super class or interfaces failed verification. 973 CheckSharedClassesClosure check_closure; 974 do { 975 // Not completely sure if we need to do this iteratively. Anyway, 976 // we should come here only if there are unverifiable classes, which 977 // shouldn't happen in normal cases. So better safe than sorry. 978 check_closure.reset(); 979 ClassLoaderDataGraph::loaded_classes_do(&check_closure); 980 } while (check_closure.made_progress()); 981 982 if (IgnoreUnverifiableClassesDuringDump) { 983 // This is useful when running JCK or SQE tests. You should not 984 // enable this when running real apps. 985 SystemDictionary::remove_classes_in_error_state(); 986 } else { 987 tty->print_cr("Please remove the unverifiable classes from your class list and try again"); 988 exit(1); 989 } 990 } 991 992 // Copy the verification constraints from C_HEAP-alloced GrowableArrays to RO-alloced 993 // Arrays 994 SystemDictionaryShared::finalize_verification_constraints(); 995 } 996 997 void MetaspaceShared::prepare_for_dumping() { 998 Arguments::check_unsupported_dumping_properties(); 999 ClassLoader::initialize_shared_path(); 1000 FileMapInfo::allocate_classpath_entry_table(); 1001 } 1002 1003 // Preload classes from a list, populate the shared spaces and dump to a 1004 // file. 1005 void MetaspaceShared::preload_and_dump(TRAPS) { 1006 { TraceTime timer("Dump Shared Spaces", TRACETIME_LOG(Info, startuptime)); 1007 ResourceMark rm; 1008 char class_list_path_str[JVM_MAXPATHLEN]; 1009 1010 tty->print_cr("Allocated shared space: " SIZE_FORMAT " bytes at " PTR_FORMAT, 1011 MetaspaceShared::shared_rs()->size(), 1012 p2i(MetaspaceShared::shared_rs()->base())); 1013 1014 // Preload classes to be shared. 1015 // Should use some os:: method rather than fopen() here. aB. 1016 const char* class_list_path; 1017 if (SharedClassListFile == NULL) { 1018 // Construct the path to the class list (in jre/lib) 1019 // Walk up two directories from the location of the VM and 1020 // optionally tack on "lib" (depending on platform) 1021 os::jvm_path(class_list_path_str, sizeof(class_list_path_str)); 1022 for (int i = 0; i < 3; i++) { 1023 char *end = strrchr(class_list_path_str, *os::file_separator()); 1024 if (end != NULL) *end = '\0'; 1025 } 1026 int class_list_path_len = (int)strlen(class_list_path_str); 1027 if (class_list_path_len >= 3) { 1028 if (strcmp(class_list_path_str + class_list_path_len - 3, "lib") != 0) { 1029 if (class_list_path_len < JVM_MAXPATHLEN - 4) { 1030 jio_snprintf(class_list_path_str + class_list_path_len, 1031 sizeof(class_list_path_str) - class_list_path_len, 1032 "%slib", os::file_separator()); 1033 class_list_path_len += 4; 1034 } 1035 } 1036 } 1037 if (class_list_path_len < JVM_MAXPATHLEN - 10) { 1038 jio_snprintf(class_list_path_str + class_list_path_len, 1039 sizeof(class_list_path_str) - class_list_path_len, 1040 "%sclasslist", os::file_separator()); 1041 } 1042 class_list_path = class_list_path_str; 1043 } else { 1044 class_list_path = SharedClassListFile; 1045 } 1046 1047 int class_count = 0; 1048 GrowableArray<Klass*>* class_promote_order = new GrowableArray<Klass*>(); 1049 1050 // sun.io.Converters 1051 static const char obj_array_sig[] = "[[Ljava/lang/Object;"; 1052 SymbolTable::new_permanent_symbol(obj_array_sig, THREAD); 1053 1054 // java.util.HashMap 1055 static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;"; 1056 SymbolTable::new_permanent_symbol(map_entry_array_sig, THREAD); 1057 1058 // Need to allocate the op here: 1059 // op.misc_data_space_alloc() will be called during preload_and_dump(). 1060 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); 1061 VM_PopulateDumpSharedSpace op(loader_data, class_promote_order); 1062 1063 tty->print_cr("Loading classes to share ..."); 1064 _has_error_classes = false; 1065 class_count += preload_and_dump(class_list_path, class_promote_order, 1066 THREAD); 1067 if (ExtraSharedClassListFile) { 1068 class_count += preload_and_dump(ExtraSharedClassListFile, class_promote_order, 1069 THREAD); 1070 } 1071 tty->print_cr("Loading classes to share: done."); 1072 1073 log_info(cds)("Shared spaces: preloaded %d classes", class_count); 1074 1075 // Rewrite and link classes 1076 tty->print_cr("Rewriting and linking classes ..."); 1077 1078 // Link any classes which got missed. This would happen if we have loaded classes that 1079 // were not explicitly specified in the classlist. E.g., if an interface implemented by class K 1080 // fails verification, all other interfaces that were not specified in the classlist but 1081 // are implemented by K are not verified. 1082 link_and_cleanup_shared_classes(CATCH); 1083 tty->print_cr("Rewriting and linking classes: done"); 1084 1085 VMThread::execute(&op); 1086 } 1087 1088 if (PrintSystemDictionaryAtExit) { 1089 SystemDictionary::print(); 1090 } 1091 1092 // Since various initialization steps have been undone by this process, 1093 // it is not reasonable to continue running a java process. 1094 exit(0); 1095 } 1096 1097 1098 int MetaspaceShared::preload_and_dump(const char* class_list_path, 1099 GrowableArray<Klass*>* class_promote_order, 1100 TRAPS) { 1101 ClassListParser parser(class_list_path); 1102 int class_count = 0; 1103 1104 while (parser.parse_one_line()) { 1105 Klass* klass = ClassLoaderExt::load_one_class(&parser, THREAD); 1106 1107 CLEAR_PENDING_EXCEPTION; 1108 if (klass != NULL) { 1109 if (log_is_enabled(Trace, cds)) { 1110 ResourceMark rm; 1111 log_trace(cds)("Shared spaces preloaded: %s", klass->external_name()); 1112 } 1113 1114 InstanceKlass* ik = InstanceKlass::cast(klass); 1115 1116 // Should be class load order as per -Xlog:class+preorder 1117 class_promote_order->append(ik); 1118 1119 // Link the class to cause the bytecodes to be rewritten and the 1120 // cpcache to be created. The linking is done as soon as classes 1121 // are loaded in order that the related data structures (klass and 1122 // cpCache) are located together. 1123 try_link_class(ik, THREAD); 1124 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class"); 1125 1126 class_count++; 1127 } 1128 } 1129 1130 return class_count; 1131 } 1132 1133 // Returns true if the class's status has changed 1134 bool MetaspaceShared::try_link_class(InstanceKlass* ik, TRAPS) { 1135 assert(DumpSharedSpaces, "should only be called during dumping"); 1136 if (ik->init_state() < InstanceKlass::linked) { 1137 bool saved = BytecodeVerificationLocal; 1138 if (!(ik->is_shared_boot_class())) { 1139 // The verification decision is based on BytecodeVerificationRemote 1140 // for non-system classes. Since we are using the NULL classloader 1141 // to load non-system classes during dumping, we need to temporarily 1142 // change BytecodeVerificationLocal to be the same as 1143 // BytecodeVerificationRemote. Note this can cause the parent system 1144 // classes also being verified. The extra overhead is acceptable during 1145 // dumping. 1146 BytecodeVerificationLocal = BytecodeVerificationRemote; 1147 } 1148 ik->link_class(THREAD); 1149 if (HAS_PENDING_EXCEPTION) { 1150 ResourceMark rm; 1151 tty->print_cr("Preload Warning: Verification failed for %s", 1152 ik->external_name()); 1153 CLEAR_PENDING_EXCEPTION; 1154 ik->set_in_error_state(); 1155 _has_error_classes = true; 1156 } 1157 BytecodeVerificationLocal = saved; 1158 return true; 1159 } else { 1160 return false; 1161 } 1162 } 1163 1164 // Closure for serializing initialization data in from a data area 1165 // (ptr_array) read from the shared file. 1166 1167 class ReadClosure : public SerializeClosure { 1168 private: 1169 intptr_t** _ptr_array; 1170 1171 inline intptr_t nextPtr() { 1172 return *(*_ptr_array)++; 1173 } 1174 1175 public: 1176 ReadClosure(intptr_t** ptr_array) { _ptr_array = ptr_array; } 1177 1178 void do_ptr(void** p) { 1179 assert(*p == NULL, "initializing previous initialized pointer."); 1180 intptr_t obj = nextPtr(); 1181 assert((intptr_t)obj >= 0 || (intptr_t)obj < -100, 1182 "hit tag while initializing ptrs."); 1183 *p = (void*)obj; 1184 } 1185 1186 void do_u4(u4* p) { 1187 intptr_t obj = nextPtr(); 1188 *p = (u4)(uintx(obj)); 1189 } 1190 1191 void do_tag(int tag) { 1192 int old_tag; 1193 old_tag = (int)(intptr_t)nextPtr(); 1194 // do_int(&old_tag); 1195 assert(tag == old_tag, "old tag doesn't match"); 1196 FileMapInfo::assert_mark(tag == old_tag); 1197 } 1198 1199 void do_region(u_char* start, size_t size) { 1200 assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment"); 1201 assert(size % sizeof(intptr_t) == 0, "bad size"); 1202 do_tag((int)size); 1203 while (size > 0) { 1204 *(intptr_t*)start = nextPtr(); 1205 start += sizeof(intptr_t); 1206 size -= sizeof(intptr_t); 1207 } 1208 } 1209 1210 bool reading() const { return true; } 1211 }; 1212 1213 // Return true if given address is in the mapped shared space. 1214 bool MetaspaceShared::is_in_shared_space(const void* p) { 1215 return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_space(p); 1216 } 1217 1218 // Return true if given address is in the misc data region 1219 bool MetaspaceShared::is_in_shared_region(const void* p, int idx) { 1220 return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_region(p, idx); 1221 } 1222 1223 void MetaspaceShared::print_shared_spaces() { 1224 if (UseSharedSpaces) { 1225 FileMapInfo::current_info()->print_shared_spaces(); 1226 } 1227 } 1228 1229 1230 // Map shared spaces at requested addresses and return if succeeded. 1231 bool MetaspaceShared::map_shared_spaces(FileMapInfo* mapinfo) { 1232 size_t image_alignment = mapinfo->alignment(); 1233 1234 #ifndef _WINDOWS 1235 // Map in the shared memory and then map the regions on top of it. 1236 // On Windows, don't map the memory here because it will cause the 1237 // mappings of the regions to fail. 1238 ReservedSpace shared_rs = mapinfo->reserve_shared_memory(); 1239 if (!shared_rs.is_reserved()) return false; 1240 #endif 1241 1242 assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces"); 1243 1244 char* _ro_base = NULL; 1245 char* _rw_base = NULL; 1246 char* _md_base = NULL; 1247 char* _mc_base = NULL; 1248 char* _od_base = NULL; 1249 1250 // Map each shared region 1251 if ((_ro_base = mapinfo->map_region(ro)) != NULL && 1252 mapinfo->verify_region_checksum(ro) && 1253 (_rw_base = mapinfo->map_region(rw)) != NULL && 1254 mapinfo->verify_region_checksum(rw) && 1255 (_md_base = mapinfo->map_region(md)) != NULL && 1256 mapinfo->verify_region_checksum(md) && 1257 (_mc_base = mapinfo->map_region(mc)) != NULL && 1258 mapinfo->verify_region_checksum(mc) && 1259 (_od_base = mapinfo->map_region(od)) != NULL && 1260 mapinfo->verify_region_checksum(od) && 1261 (image_alignment == (size_t)max_alignment()) && 1262 mapinfo->validate_classpath_entry_table()) { 1263 // Success (no need to do anything) 1264 return true; 1265 } else { 1266 // If there was a failure in mapping any of the spaces, unmap the ones 1267 // that succeeded 1268 if (_ro_base != NULL) mapinfo->unmap_region(ro); 1269 if (_rw_base != NULL) mapinfo->unmap_region(rw); 1270 if (_md_base != NULL) mapinfo->unmap_region(md); 1271 if (_mc_base != NULL) mapinfo->unmap_region(mc); 1272 if (_od_base != NULL) mapinfo->unmap_region(od); 1273 #ifndef _WINDOWS 1274 // Release the entire mapped region 1275 shared_rs.release(); 1276 #endif 1277 // If -Xshare:on is specified, print out the error message and exit VM, 1278 // otherwise, set UseSharedSpaces to false and continue. 1279 if (RequireSharedSpaces || PrintSharedArchiveAndExit) { 1280 vm_exit_during_initialization("Unable to use shared archive.", "Failed map_region for using -Xshare:on."); 1281 } else { 1282 FLAG_SET_DEFAULT(UseSharedSpaces, false); 1283 } 1284 return false; 1285 } 1286 } 1287 1288 // Read the miscellaneous data from the shared file, and 1289 // serialize it out to its various destinations. 1290 1291 void MetaspaceShared::initialize_shared_spaces() { 1292 FileMapInfo *mapinfo = FileMapInfo::current_info(); 1293 _cds_i2i_entry_code_buffers = mapinfo->cds_i2i_entry_code_buffers(); 1294 _cds_i2i_entry_code_buffers_size = mapinfo->cds_i2i_entry_code_buffers_size(); 1295 char* buffer = mapinfo->misc_data_patching_start(); 1296 1297 buffer = (char*)clone_cpp_vtables((intptr_t*)buffer); 1298 1299 int sharedDictionaryLen = *(intptr_t*)buffer; 1300 buffer += sizeof(intptr_t); 1301 int number_of_entries = *(intptr_t*)buffer; 1302 buffer += sizeof(intptr_t); 1303 SystemDictionary::set_shared_dictionary((HashtableBucket<mtClass>*)buffer, 1304 sharedDictionaryLen, 1305 number_of_entries); 1306 buffer += sharedDictionaryLen; 1307 1308 // The following data in the shared misc data region are the linked 1309 // list elements (HashtableEntry objects) for the shared dictionary 1310 // table. 1311 1312 int len = *(intptr_t*)buffer; // skip over shared dictionary entries 1313 buffer += sizeof(intptr_t); 1314 buffer += len; 1315 1316 // Verify various attributes of the archive, plus initialize the 1317 // shared string/symbol tables 1318 intptr_t* array = (intptr_t*)buffer; 1319 ReadClosure rc(&array); 1320 serialize(&rc, NULL, NULL); 1321 1322 // Initialize the run-time symbol table. 1323 SymbolTable::create_table(); 1324 1325 // Close the mapinfo file 1326 mapinfo->close(); 1327 1328 if (PrintSharedArchiveAndExit) { 1329 if (PrintSharedDictionary) { 1330 tty->print_cr("\nShared classes:\n"); 1331 SystemDictionary::print_shared(false); 1332 } 1333 if (_archive_loading_failed) { 1334 tty->print_cr("archive is invalid"); 1335 vm_exit(1); 1336 } else { 1337 tty->print_cr("archive is valid"); 1338 vm_exit(0); 1339 } 1340 } 1341 } 1342 1343 void MetaspaceShared::fixup_shared_string_regions() { 1344 FileMapInfo *mapinfo = FileMapInfo::current_info(); 1345 mapinfo->fixup_string_regions(); 1346 } 1347 1348 // JVM/TI RedefineClasses() support: 1349 bool MetaspaceShared::remap_shared_readonly_as_readwrite() { 1350 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 1351 1352 if (UseSharedSpaces) { 1353 // remap the shared readonly space to shared readwrite, private 1354 FileMapInfo* mapinfo = FileMapInfo::current_info(); 1355 if (!mapinfo->remap_shared_readonly_as_readwrite()) { 1356 return false; 1357 } 1358 _remapped_readwrite = true; 1359 } 1360 return true; 1361 } 1362 1363 int MetaspaceShared::count_class(const char* classlist_file) { 1364 if (classlist_file == NULL) { 1365 return 0; 1366 } 1367 char class_name[256]; 1368 int class_count = 0; 1369 FILE* file = fopen(classlist_file, "r"); 1370 if (file != NULL) { 1371 while ((fgets(class_name, sizeof class_name, file)) != NULL) { 1372 if (*class_name == '#') { // comment 1373 continue; 1374 } 1375 class_count++; 1376 } 1377 fclose(file); 1378 } else { 1379 char errmsg[JVM_MAXPATHLEN]; 1380 os::lasterror(errmsg, JVM_MAXPATHLEN); 1381 tty->print_cr("Loading classlist failed: %s", errmsg); 1382 exit(1); 1383 } 1384 1385 return class_count; 1386 } 1387 1388 // the sizes are good for typical large applications that have a lot of shared 1389 // classes 1390 void MetaspaceShared::estimate_regions_size() { 1391 int class_count = count_class(SharedClassListFile); 1392 class_count += count_class(ExtraSharedClassListFile); 1393 1394 if (class_count > LargeThresholdClassCount) { 1395 if (class_count < HugeThresholdClassCount) { 1396 SET_ESTIMATED_SIZE(Large, ReadOnly); 1397 SET_ESTIMATED_SIZE(Large, ReadWrite); 1398 SET_ESTIMATED_SIZE(Large, MiscData); 1399 SET_ESTIMATED_SIZE(Large, MiscCode); 1400 } else { 1401 SET_ESTIMATED_SIZE(Huge, ReadOnly); 1402 SET_ESTIMATED_SIZE(Huge, ReadWrite); 1403 SET_ESTIMATED_SIZE(Huge, MiscData); 1404 SET_ESTIMATED_SIZE(Huge, MiscCode); 1405 } 1406 } 1407 }