1 /* 2 * Copyright (c) 2012, 2014, 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/dictionary.hpp" 27 #include "classfile/loaderConstraints.hpp" 28 #include "classfile/placeholders.hpp" 29 #include "classfile/sharedClassUtil.hpp" 30 #include "classfile/symbolTable.hpp" 31 #include "classfile/systemDictionary.hpp" 32 #include "code/codeCache.hpp" 33 #include "memory/filemap.hpp" 34 #include "memory/gcLocker.hpp" 35 #include "memory/metaspace.hpp" 36 #include "memory/metaspaceShared.hpp" 37 #include "oops/objArrayOop.hpp" 38 #include "oops/oop.inline.hpp" 39 #include "runtime/os.hpp" 40 #include "runtime/signature.hpp" 41 #include "runtime/vm_operations.hpp" 42 #include "runtime/vmThread.hpp" 43 #include "utilities/hashtable.inline.hpp" 44 45 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 46 47 int MetaspaceShared::_max_alignment = 0; 48 49 ReservedSpace* MetaspaceShared::_shared_rs = NULL; 50 51 MetaspaceSharedStats MetaspaceShared::_stats; 52 53 bool MetaspaceShared::_link_classes_made_progress; 54 bool MetaspaceShared::_check_classes_made_progress; 55 bool MetaspaceShared::_has_error_classes; 56 bool MetaspaceShared::_archive_loading_failed = false; 57 // Read/write a data stream for restoring/preserving metadata pointers and 58 // miscellaneous data from/to the shared archive file. 59 60 void MetaspaceShared::serialize(SerializeClosure* soc) { 61 int tag = 0; 62 soc->do_tag(--tag); 63 64 // Verify the sizes of various metadata in the system. 65 soc->do_tag(sizeof(Method)); 66 soc->do_tag(sizeof(ConstMethod)); 67 soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE)); 68 soc->do_tag(sizeof(ConstantPool)); 69 soc->do_tag(sizeof(ConstantPoolCache)); 70 soc->do_tag(objArrayOopDesc::base_offset_in_bytes()); 71 soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE)); 72 soc->do_tag(sizeof(Symbol)); 73 74 // Dump/restore miscellaneous metadata. 75 Universe::serialize(soc, true); 76 soc->do_tag(--tag); 77 78 // Dump/restore references to commonly used names and signatures. 79 vmSymbols::serialize(soc); 80 soc->do_tag(--tag); 81 82 soc->do_tag(666); 83 } 84 85 86 // CDS code for dumping shared archive. 87 88 // Global object for holding classes that have been loaded. Since this 89 // is run at a safepoint just before exit, this is the entire set of classes. 90 static GrowableArray<Klass*>* _global_klass_objects; 91 static void collect_classes(Klass* k) { 92 _global_klass_objects->append_if_missing(k); 93 if (k->oop_is_instance()) { 94 // Add in the array classes too 95 InstanceKlass* ik = InstanceKlass::cast(k); 96 ik->array_klasses_do(collect_classes); 97 } 98 } 99 100 static void remove_unshareable_in_classes() { 101 for (int i = 0; i < _global_klass_objects->length(); i++) { 102 Klass* k = _global_klass_objects->at(i); 103 k->remove_unshareable_info(); 104 } 105 } 106 107 // Walk all methods in the class list and assign a fingerprint. 108 // so that this part of the ConstMethod* is read only. 109 static void calculate_fingerprints() { 110 for (int i = 0; i < _global_klass_objects->length(); i++) { 111 Klass* k = _global_klass_objects->at(i); 112 if (k->oop_is_instance()) { 113 InstanceKlass* ik = InstanceKlass::cast(k); 114 for (int i = 0; i < ik->methods()->length(); i++) { 115 Method* m = ik->methods()->at(i); 116 Fingerprinter fp(m); 117 // The side effect of this call sets method's fingerprint field. 118 fp.fingerprint(); 119 } 120 } 121 } 122 } 123 124 // Patch C++ vtable pointer in metadata. 125 126 // Klass and other metadata objects contain references to c++ vtables in the 127 // JVM library. 128 // Fix them to point to our constructed vtables. However, don't iterate 129 // across the space while doing this, as that causes the vtables to be 130 // patched, undoing our useful work. Instead, iterate to make a list, 131 // then use the list to do the fixing. 132 // 133 // Our constructed vtables: 134 // Dump time: 135 // 1. init_self_patching_vtbl_list: table of pointers to current virtual method addrs 136 // 2. generate_vtable_methods: create jump table, appended to above vtbl_list 137 // 3. patch_klass_vtables: for Klass list, patch the vtable entry in klass and 138 // associated metadata to point to jump table rather than to current vtbl 139 // Table layout: NOTE FIXED SIZE 140 // 1. vtbl pointers 141 // 2. #Klass X #virtual methods per Klass 142 // 1 entry for each, in the order: 143 // Klass1:method1 entry, Klass1:method2 entry, ... Klass1:method<num_virtuals> entry 144 // Klass2:method1 entry, Klass2:method2 entry, ... Klass2:method<num_virtuals> entry 145 // ... 146 // Klass<vtbl_list_size>:method1 entry, Klass<vtbl_list_size>:method2 entry, 147 // ... Klass<vtbl_list_size>:method<num_virtuals> entry 148 // Sample entry: (Sparc): 149 // save(sp, -256, sp) 150 // ba,pt common_code 151 // mov XXX, %L0 %L0 gets: Klass index <<8 + method index (note: max method index 255) 152 // 153 // Restore time: 154 // 1. initialize_shared_space: reserve space for table 155 // 2. init_self_patching_vtbl_list: update pointers to NEW virtual method addrs in text 156 // 157 // Execution time: 158 // First virtual method call for any object of these metadata types: 159 // 1. object->klass 160 // 2. vtable entry for that klass points to the jump table entries 161 // 3. branches to common_code with %O0/klass, %L0: Klass index <<8 + method index 162 // 4. common_code: 163 // Get address of new vtbl pointer for this Klass from updated table 164 // Update new vtbl pointer in the Klass: future virtual calls go direct 165 // Jump to method, using new vtbl pointer and method index 166 167 168 static void* find_matching_vtbl_ptr(void** vtbl_list, void* new_vtable_start, void* obj) { 169 void* old_vtbl_ptr = *(void**)obj; 170 for (int i = 0; i < MetaspaceShared::vtbl_list_size; i++) { 171 if (vtbl_list[i] == old_vtbl_ptr) { 172 return (void**)new_vtable_start + i * MetaspaceShared::num_virtuals; 173 } 174 } 175 ShouldNotReachHere(); 176 return NULL; 177 } 178 179 // Assumes the vtable is in first slot in object. 180 static void patch_klass_vtables(void** vtbl_list, void* new_vtable_start) { 181 int n = _global_klass_objects->length(); 182 for (int i = 0; i < n; i++) { 183 Klass* obj = _global_klass_objects->at(i); 184 // Note oop_is_instance() is a virtual call. After patching vtables 185 // all virtual calls on the dummy vtables will restore the original! 186 if (obj->oop_is_instance()) { 187 InstanceKlass* ik = InstanceKlass::cast(obj); 188 *(void**)ik = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, ik); 189 ConstantPool* cp = ik->constants(); 190 *(void**)cp = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, cp); 191 for (int j = 0; j < ik->methods()->length(); j++) { 192 Method* m = ik->methods()->at(j); 193 *(void**)m = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, m); 194 } 195 } else { 196 // Array klasses 197 Klass* k = obj; 198 *(void**)k = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, k); 199 } 200 } 201 } 202 203 // Closure for serializing initialization data out to a data area to be 204 // written to the shared file. 205 206 class WriteClosure : public SerializeClosure { 207 private: 208 intptr_t* top; 209 char* end; 210 211 inline void check_space() { 212 if ((char*)top + sizeof(intptr_t) > end) { 213 report_out_of_shared_space(SharedMiscData); 214 } 215 } 216 217 public: 218 WriteClosure(char* md_top, char* md_end) { 219 top = (intptr_t*)md_top; 220 end = md_end; 221 } 222 223 char* get_top() { return (char*)top; } 224 225 void do_ptr(void** p) { 226 check_space(); 227 *top = (intptr_t)*p; 228 ++top; 229 } 230 231 void do_tag(int tag) { 232 check_space(); 233 *top = (intptr_t)tag; 234 ++top; 235 } 236 237 void do_region(u_char* start, size_t size) { 238 if ((char*)top + size > end) { 239 report_out_of_shared_space(SharedMiscData); 240 } 241 assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment"); 242 assert(size % sizeof(intptr_t) == 0, "bad size"); 243 do_tag((int)size); 244 while (size > 0) { 245 *top = *(intptr_t*)start; 246 ++top; 247 start += sizeof(intptr_t); 248 size -= sizeof(intptr_t); 249 } 250 } 251 252 bool reading() const { return false; } 253 }; 254 255 // This is for dumping detailed statistics for the allocations 256 // in the shared spaces. 257 class DumpAllocClosure : public Metaspace::AllocRecordClosure { 258 public: 259 260 // Here's poor man's enum inheritance 261 #define SHAREDSPACE_OBJ_TYPES_DO(f) \ 262 METASPACE_OBJ_TYPES_DO(f) \ 263 f(SymbolHashentry) \ 264 f(SymbolBucket) \ 265 f(Other) 266 267 #define SHAREDSPACE_OBJ_TYPE_DECLARE(name) name ## Type, 268 #define SHAREDSPACE_OBJ_TYPE_NAME_CASE(name) case name ## Type: return #name; 269 270 enum Type { 271 // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc 272 SHAREDSPACE_OBJ_TYPES_DO(SHAREDSPACE_OBJ_TYPE_DECLARE) 273 _number_of_types 274 }; 275 276 static const char * type_name(Type type) { 277 switch(type) { 278 SHAREDSPACE_OBJ_TYPES_DO(SHAREDSPACE_OBJ_TYPE_NAME_CASE) 279 default: 280 ShouldNotReachHere(); 281 return NULL; 282 } 283 } 284 285 public: 286 enum { 287 RO = 0, 288 RW = 1 289 }; 290 291 int _counts[2][_number_of_types]; 292 int _bytes [2][_number_of_types]; 293 int _which; 294 295 DumpAllocClosure() { 296 memset(_counts, 0, sizeof(_counts)); 297 memset(_bytes, 0, sizeof(_bytes)); 298 }; 299 300 void iterate_metaspace(Metaspace* space, int which) { 301 assert(which == RO || which == RW, "sanity"); 302 _which = which; 303 space->iterate(this); 304 } 305 306 virtual void doit(address ptr, MetaspaceObj::Type type, int byte_size) { 307 assert(int(type) >= 0 && type < MetaspaceObj::_number_of_types, "sanity"); 308 _counts[_which][type] ++; 309 _bytes [_which][type] += byte_size; 310 } 311 312 void dump_stats(int ro_all, int rw_all, int md_all, int mc_all); 313 }; 314 315 void DumpAllocClosure::dump_stats(int ro_all, int rw_all, int md_all, int mc_all) { 316 rw_all += (md_all + mc_all); // md and mc are all mapped Read/Write 317 int other_bytes = md_all + mc_all; 318 319 // Calculate size of data that was not allocated by Metaspace::allocate() 320 MetaspaceSharedStats *stats = MetaspaceShared::stats(); 321 322 // symbols 323 _counts[RW][SymbolHashentryType] = stats->symbol.hashentry_count; 324 _bytes [RW][SymbolHashentryType] = stats->symbol.hashentry_bytes; 325 other_bytes -= stats->symbol.hashentry_bytes; 326 327 _counts[RW][SymbolBucketType] = stats->symbol.bucket_count; 328 _bytes [RW][SymbolBucketType] = stats->symbol.bucket_bytes; 329 other_bytes -= stats->symbol.bucket_bytes; 330 331 // TODO: count things like dictionary, vtable, etc 332 _bytes[RW][OtherType] = other_bytes; 333 334 // prevent divide-by-zero 335 if (ro_all < 1) { 336 ro_all = 1; 337 } 338 if (rw_all < 1) { 339 rw_all = 1; 340 } 341 342 int all_ro_count = 0; 343 int all_ro_bytes = 0; 344 int all_rw_count = 0; 345 int all_rw_bytes = 0; 346 347 // To make fmt_stats be a syntactic constant (for format warnings), use #define. 348 #define fmt_stats "%-20s: %8d %10d %5.1f | %8d %10d %5.1f | %8d %10d %5.1f" 349 const char *sep = "--------------------+---------------------------+---------------------------+--------------------------"; 350 const char *hdr = " ro_cnt ro_bytes % | rw_cnt rw_bytes % | all_cnt all_bytes %"; 351 352 tty->print_cr("Detailed metadata info (rw includes md and mc):"); 353 tty->print_cr("%s", hdr); 354 tty->print_cr("%s", sep); 355 for (int type = 0; type < int(_number_of_types); type ++) { 356 const char *name = type_name((Type)type); 357 int ro_count = _counts[RO][type]; 358 int ro_bytes = _bytes [RO][type]; 359 int rw_count = _counts[RW][type]; 360 int rw_bytes = _bytes [RW][type]; 361 int count = ro_count + rw_count; 362 int bytes = ro_bytes + rw_bytes; 363 364 double ro_perc = 100.0 * double(ro_bytes) / double(ro_all); 365 double rw_perc = 100.0 * double(rw_bytes) / double(rw_all); 366 double perc = 100.0 * double(bytes) / double(ro_all + rw_all); 367 368 tty->print_cr(fmt_stats, name, 369 ro_count, ro_bytes, ro_perc, 370 rw_count, rw_bytes, rw_perc, 371 count, bytes, perc); 372 373 all_ro_count += ro_count; 374 all_ro_bytes += ro_bytes; 375 all_rw_count += rw_count; 376 all_rw_bytes += rw_bytes; 377 } 378 379 int all_count = all_ro_count + all_rw_count; 380 int all_bytes = all_ro_bytes + all_rw_bytes; 381 382 double all_ro_perc = 100.0 * double(all_ro_bytes) / double(ro_all); 383 double all_rw_perc = 100.0 * double(all_rw_bytes) / double(rw_all); 384 double all_perc = 100.0 * double(all_bytes) / double(ro_all + rw_all); 385 386 tty->print_cr("%s", sep); 387 tty->print_cr(fmt_stats, "Total", 388 all_ro_count, all_ro_bytes, all_ro_perc, 389 all_rw_count, all_rw_bytes, all_rw_perc, 390 all_count, all_bytes, all_perc); 391 392 assert(all_ro_bytes == ro_all, "everything should have been counted"); 393 assert(all_rw_bytes == rw_all, "everything should have been counted"); 394 #undef fmt_stats 395 } 396 397 // Populate the shared space. 398 399 class VM_PopulateDumpSharedSpace: public VM_Operation { 400 private: 401 ClassLoaderData* _loader_data; 402 GrowableArray<Klass*> *_class_promote_order; 403 VirtualSpace _md_vs; 404 VirtualSpace _mc_vs; 405 406 public: 407 VM_PopulateDumpSharedSpace(ClassLoaderData* loader_data, 408 GrowableArray<Klass*> *class_promote_order) : 409 _loader_data(loader_data) { 410 411 // Split up and initialize the misc code and data spaces 412 ReservedSpace* shared_rs = MetaspaceShared::shared_rs(); 413 int metadata_size = SharedReadOnlySize+SharedReadWriteSize; 414 ReservedSpace shared_ro_rw = shared_rs->first_part(metadata_size); 415 ReservedSpace misc_section = shared_rs->last_part(metadata_size); 416 417 // Now split into misc sections. 418 ReservedSpace md_rs = misc_section.first_part(SharedMiscDataSize); 419 ReservedSpace mc_rs = misc_section.last_part(SharedMiscDataSize); 420 _md_vs.initialize(md_rs, SharedMiscDataSize); 421 _mc_vs.initialize(mc_rs, SharedMiscCodeSize); 422 _class_promote_order = class_promote_order; 423 } 424 425 VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; } 426 void doit(); // outline because gdb sucks 427 428 private: 429 void handle_failure(bool success, const char* what) { 430 if (!success) { 431 tty->print_cr("Insufficient shared space: please increase -XX:%s= to increase its size", what); 432 exit(1); 433 } 434 } 435 void handle_misc_data_space_failure(bool success) { 436 handle_failure(success, "SharedMiscDataSize"); 437 } 438 }; // class VM_PopulateDumpSharedSpace 439 440 441 void VM_PopulateDumpSharedSpace::doit() { 442 Thread* THREAD = VMThread::vm_thread(); 443 NOT_PRODUCT(SystemDictionary::verify();) 444 // The following guarantee is meant to ensure that no loader constraints 445 // exist yet, since the constraints table is not shared. This becomes 446 // more important now that we don't re-initialize vtables/itables for 447 // shared classes at runtime, where constraints were previously created. 448 guarantee(SystemDictionary::constraints()->number_of_entries() == 0, 449 "loader constraints are not saved"); 450 guarantee(SystemDictionary::placeholders()->number_of_entries() == 0, 451 "placeholders are not saved"); 452 // Revisit and implement this if we prelink method handle call sites: 453 guarantee(SystemDictionary::invoke_method_table() == NULL || 454 SystemDictionary::invoke_method_table()->number_of_entries() == 0, 455 "invoke method table is not saved"); 456 457 // At this point, many classes have been loaded. 458 // Gather systemDictionary classes in a global array and do everything to 459 // that so we don't have to walk the SystemDictionary again. 460 _global_klass_objects = new GrowableArray<Klass*>(1000); 461 Universe::basic_type_classes_do(collect_classes); 462 SystemDictionary::classes_do(collect_classes); 463 464 tty->print_cr("Number of classes %d", _global_klass_objects->length()); 465 { 466 int num_type_array = 0, num_obj_array = 0, num_inst = 0; 467 for (int i = 0; i < _global_klass_objects->length(); i++) { 468 Klass* k = _global_klass_objects->at(i); 469 if (k->oop_is_instance()) { 470 num_inst ++; 471 } else if (k->oop_is_objArray()) { 472 num_obj_array ++; 473 } else { 474 assert(k->oop_is_typeArray(), "sanity"); 475 num_type_array ++; 476 } 477 } 478 tty->print_cr(" instance classes = %5d", num_inst); 479 tty->print_cr(" obj array classes = %5d", num_obj_array); 480 tty->print_cr(" type array classes = %5d", num_type_array); 481 } 482 483 // Update all the fingerprints in the shared methods. 484 tty->print("Calculating fingerprints ... "); 485 calculate_fingerprints(); 486 tty->print_cr("done. "); 487 488 // Remove all references outside the metadata 489 tty->print("Removing unshareable information ... "); 490 remove_unshareable_in_classes(); 491 tty->print_cr("done. "); 492 493 // Set up the share data and shared code segments. 494 char* md_low = _md_vs.low(); 495 char* md_top = md_low; 496 char* md_end = _md_vs.high(); 497 char* mc_low = _mc_vs.low(); 498 char* mc_top = mc_low; 499 char* mc_end = _mc_vs.high(); 500 501 // Reserve space for the list of Klass*s whose vtables are used 502 // for patching others as needed. 503 504 void** vtbl_list = (void**)md_top; 505 int vtbl_list_size = MetaspaceShared::vtbl_list_size; 506 Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size); 507 508 md_top += vtbl_list_size * sizeof(void*); 509 void* vtable = md_top; 510 511 // Reserve space for a new dummy vtable for klass objects in the 512 // heap. Generate self-patching vtable entries. 513 514 MetaspaceShared::generate_vtable_methods(vtbl_list, &vtable, 515 &md_top, md_end, 516 &mc_top, mc_end); 517 518 // Reorder the system dictionary. (Moving the symbols affects 519 // how the hash table indices are calculated.) 520 // Not doing this either. 521 522 SystemDictionary::reorder_dictionary(); 523 524 NOT_PRODUCT(SystemDictionary::verify();) 525 526 // Copy the the symbol table, and the system dictionary to the shared 527 // space in usable form. Copy the hashtable 528 // buckets first [read-write], then copy the linked lists of entries 529 // [read-only]. 530 531 NOT_PRODUCT(SymbolTable::verify()); 532 handle_misc_data_space_failure(SymbolTable::copy_compact_table(&md_top, md_end)); 533 534 SystemDictionary::reverse(); 535 SystemDictionary::copy_buckets(&md_top, md_end); 536 537 ClassLoader::verify(); 538 ClassLoader::copy_package_info_buckets(&md_top, md_end); 539 ClassLoader::verify(); 540 541 SystemDictionary::copy_table(&md_top, md_end); 542 ClassLoader::verify(); 543 ClassLoader::copy_package_info_table(&md_top, md_end); 544 ClassLoader::verify(); 545 546 // Write the other data to the output array. 547 WriteClosure wc(md_top, md_end); 548 MetaspaceShared::serialize(&wc); 549 md_top = wc.get_top(); 550 551 // Print shared spaces all the time 552 // To make fmt_space be a syntactic constant (for format warnings), use #define. 553 #define fmt_space "%s space: %9d [ %4.1f%% of total] out of %9d bytes [%4.1f%% used] at " INTPTR_FORMAT 554 Metaspace* ro_space = _loader_data->ro_metaspace(); 555 Metaspace* rw_space = _loader_data->rw_metaspace(); 556 557 // Allocated size of each space (may not be all occupied) 558 const size_t ro_alloced = ro_space->capacity_bytes_slow(Metaspace::NonClassType); 559 const size_t rw_alloced = rw_space->capacity_bytes_slow(Metaspace::NonClassType); 560 const size_t md_alloced = md_end-md_low; 561 const size_t mc_alloced = mc_end-mc_low; 562 const size_t total_alloced = ro_alloced + rw_alloced + md_alloced + mc_alloced; 563 564 // Occupied size of each space. 565 const size_t ro_bytes = ro_space->used_bytes_slow(Metaspace::NonClassType); 566 const size_t rw_bytes = rw_space->used_bytes_slow(Metaspace::NonClassType); 567 const size_t md_bytes = size_t(md_top - md_low); 568 const size_t mc_bytes = size_t(mc_top - mc_low); 569 570 // Percent of total size 571 const size_t total_bytes = ro_bytes + rw_bytes + md_bytes + mc_bytes; 572 const double ro_t_perc = ro_bytes / double(total_bytes) * 100.0; 573 const double rw_t_perc = rw_bytes / double(total_bytes) * 100.0; 574 const double md_t_perc = md_bytes / double(total_bytes) * 100.0; 575 const double mc_t_perc = mc_bytes / double(total_bytes) * 100.0; 576 577 // Percent of fullness of each space 578 const double ro_u_perc = ro_bytes / double(ro_alloced) * 100.0; 579 const double rw_u_perc = rw_bytes / double(rw_alloced) * 100.0; 580 const double md_u_perc = md_bytes / double(md_alloced) * 100.0; 581 const double mc_u_perc = mc_bytes / double(mc_alloced) * 100.0; 582 const double total_u_perc = total_bytes / double(total_alloced) * 100.0; 583 584 tty->print_cr(fmt_space, "ro", ro_bytes, ro_t_perc, ro_alloced, ro_u_perc, ro_space->bottom()); 585 tty->print_cr(fmt_space, "rw", rw_bytes, rw_t_perc, rw_alloced, rw_u_perc, rw_space->bottom()); 586 tty->print_cr(fmt_space, "md", md_bytes, md_t_perc, md_alloced, md_u_perc, md_low); 587 tty->print_cr(fmt_space, "mc", mc_bytes, mc_t_perc, mc_alloced, mc_u_perc, mc_low); 588 tty->print_cr("total : %9d [100.0%% of total] out of %9d bytes [%4.1f%% used]", 589 total_bytes, total_alloced, total_u_perc); 590 591 // Update the vtable pointers in all of the Klass objects in the 592 // heap. They should point to newly generated vtable. 593 patch_klass_vtables(vtbl_list, vtable); 594 595 // dunno what this is for. 596 char* saved_vtbl = (char*)os::malloc(vtbl_list_size * sizeof(void*), mtClass); 597 memmove(saved_vtbl, vtbl_list, vtbl_list_size * sizeof(void*)); 598 memset(vtbl_list, 0, vtbl_list_size * sizeof(void*)); 599 600 // Create and write the archive file that maps the shared spaces. 601 602 FileMapInfo* mapinfo = new FileMapInfo(); 603 mapinfo->populate_header(MetaspaceShared::max_alignment()); 604 605 // Pass 1 - update file offsets in header. 606 mapinfo->write_header(); 607 mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true); 608 mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false); 609 mapinfo->write_region(MetaspaceShared::md, _md_vs.low(), 610 pointer_delta(md_top, _md_vs.low(), sizeof(char)), 611 SharedMiscDataSize, 612 false, false); 613 mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(), 614 pointer_delta(mc_top, _mc_vs.low(), sizeof(char)), 615 SharedMiscCodeSize, 616 true, true); 617 618 // Pass 2 - write data. 619 mapinfo->open_for_write(); 620 mapinfo->set_header_crc(mapinfo->compute_header_crc()); 621 mapinfo->write_header(); 622 mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true); 623 mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false); 624 mapinfo->write_region(MetaspaceShared::md, _md_vs.low(), 625 pointer_delta(md_top, _md_vs.low(), sizeof(char)), 626 SharedMiscDataSize, 627 false, false); 628 mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(), 629 pointer_delta(mc_top, _mc_vs.low(), sizeof(char)), 630 SharedMiscCodeSize, 631 true, true); 632 mapinfo->close(); 633 634 memmove(vtbl_list, saved_vtbl, vtbl_list_size * sizeof(void*)); 635 636 if (PrintSharedSpaces) { 637 DumpAllocClosure dac; 638 dac.iterate_metaspace(_loader_data->ro_metaspace(), DumpAllocClosure::RO); 639 dac.iterate_metaspace(_loader_data->rw_metaspace(), DumpAllocClosure::RW); 640 641 dac.dump_stats(int(ro_bytes), int(rw_bytes), int(md_bytes), int(mc_bytes)); 642 } 643 #undef fmt_space 644 } 645 646 647 void MetaspaceShared::link_one_shared_class(Klass* obj, TRAPS) { 648 Klass* k = obj; 649 if (k->oop_is_instance()) { 650 InstanceKlass* ik = (InstanceKlass*) k; 651 // Link the class to cause the bytecodes to be rewritten and the 652 // cpcache to be created. Class verification is done according 653 // to -Xverify setting. 654 _link_classes_made_progress |= try_link_class(ik, THREAD); 655 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class"); 656 } 657 } 658 659 void MetaspaceShared::check_one_shared_class(Klass* k) { 660 if (k->oop_is_instance() && InstanceKlass::cast(k)->check_sharing_error_state()) { 661 _check_classes_made_progress = true; 662 } 663 } 664 665 void MetaspaceShared::link_and_cleanup_shared_classes(TRAPS) { 666 // We need to iterate because verification may cause additional classes 667 // to be loaded. 668 do { 669 _link_classes_made_progress = false; 670 SystemDictionary::classes_do(link_one_shared_class, THREAD); 671 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class"); 672 } while (_link_classes_made_progress); 673 674 if (_has_error_classes) { 675 // Mark all classes whose super class or interfaces failed verification. 676 do { 677 // Not completely sure if we need to do this iteratively. Anyway, 678 // we should come here only if there are unverifiable classes, which 679 // shouldn't happen in normal cases. So better safe than sorry. 680 _check_classes_made_progress = false; 681 SystemDictionary::classes_do(check_one_shared_class); 682 } while (_check_classes_made_progress); 683 684 if (IgnoreUnverifiableClassesDuringDump) { 685 // This is useful when running JCK or SQE tests. You should not 686 // enable this when running real apps. 687 SystemDictionary::remove_classes_in_error_state(); 688 } else { 689 tty->print_cr("Please remove the unverifiable classes from your class list and try again"); 690 exit(1); 691 } 692 } 693 } 694 695 void MetaspaceShared::prepare_for_dumping() { 696 ClassLoader::initialize_shared_path(); 697 FileMapInfo::allocate_classpath_entry_table(); 698 } 699 700 // Preload classes from a list, populate the shared spaces and dump to a 701 // file. 702 void MetaspaceShared::preload_and_dump(TRAPS) { 703 TraceTime timer("Dump Shared Spaces", TraceStartupTime); 704 ResourceMark rm; 705 706 tty->print_cr("Allocated shared space: %d bytes at " PTR_FORMAT, 707 MetaspaceShared::shared_rs()->size(), 708 MetaspaceShared::shared_rs()->base()); 709 710 // Preload classes to be shared. 711 // Should use some os:: method rather than fopen() here. aB. 712 const char* class_list_path; 713 if (SharedClassListFile == NULL) { 714 // Construct the path to the class list (in jre/lib) 715 // Walk up two directories from the location of the VM and 716 // optionally tack on "lib" (depending on platform) 717 char class_list_path_str[JVM_MAXPATHLEN]; 718 os::jvm_path(class_list_path_str, sizeof(class_list_path_str)); 719 for (int i = 0; i < 3; i++) { 720 char *end = strrchr(class_list_path_str, *os::file_separator()); 721 if (end != NULL) *end = '\0'; 722 } 723 int class_list_path_len = (int)strlen(class_list_path_str); 724 if (class_list_path_len >= 3) { 725 if (strcmp(class_list_path_str + class_list_path_len - 3, "lib") != 0) { 726 if (class_list_path_len < JVM_MAXPATHLEN - 4) { 727 jio_snprintf(class_list_path_str + class_list_path_len, 728 sizeof(class_list_path_str) - class_list_path_len, 729 "%slib", os::file_separator()); 730 class_list_path_len += 4; 731 } 732 } 733 } 734 if (class_list_path_len < JVM_MAXPATHLEN - 10) { 735 jio_snprintf(class_list_path_str + class_list_path_len, 736 sizeof(class_list_path_str) - class_list_path_len, 737 "%sclasslist", os::file_separator()); 738 } 739 class_list_path = class_list_path_str; 740 } else { 741 class_list_path = SharedClassListFile; 742 } 743 744 int class_count = 0; 745 GrowableArray<Klass*>* class_promote_order = new GrowableArray<Klass*>(); 746 747 // sun.io.Converters 748 static const char obj_array_sig[] = "[[Ljava/lang/Object;"; 749 SymbolTable::new_permanent_symbol(obj_array_sig, THREAD); 750 751 // java.util.HashMap 752 static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;"; 753 SymbolTable::new_permanent_symbol(map_entry_array_sig, THREAD); 754 755 tty->print_cr("Loading classes to share ..."); 756 _has_error_classes = false; 757 class_count += preload_and_dump(class_list_path, class_promote_order, 758 THREAD); 759 if (ExtraSharedClassListFile) { 760 class_count += preload_and_dump(ExtraSharedClassListFile, class_promote_order, 761 THREAD); 762 } 763 tty->print_cr("Loading classes to share: done."); 764 765 if (PrintSharedSpaces) { 766 tty->print_cr("Shared spaces: preloaded %d classes", class_count); 767 } 768 769 // Rewrite and link classes 770 tty->print_cr("Rewriting and linking classes ..."); 771 772 // Link any classes which got missed. This would happen if we have loaded classes that 773 // were not explicitly specified in the classlist. E.g., if an interface implemented by class K 774 // fails verification, all other interfaces that were not specified in the classlist but 775 // are implemented by K are not verified. 776 link_and_cleanup_shared_classes(CATCH); 777 tty->print_cr("Rewriting and linking classes: done"); 778 779 // Create and dump the shared spaces. Everything so far is loaded 780 // with the null class loader. 781 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); 782 VM_PopulateDumpSharedSpace op(loader_data, class_promote_order); 783 VMThread::execute(&op); 784 785 // Since various initialization steps have been undone by this process, 786 // it is not reasonable to continue running a java process. 787 exit(0); 788 } 789 790 int MetaspaceShared::preload_and_dump(const char * class_list_path, 791 GrowableArray<Klass*>* class_promote_order, 792 TRAPS) { 793 FILE* file = fopen(class_list_path, "r"); 794 char class_name[256]; 795 int class_count = 0; 796 797 if (file != NULL) { 798 while ((fgets(class_name, sizeof class_name, file)) != NULL) { 799 if (*class_name == '#') { // comment 800 continue; 801 } 802 // Remove trailing newline 803 size_t name_len = strlen(class_name); 804 if (class_name[name_len-1] == '\n') { 805 class_name[name_len-1] = '\0'; 806 } 807 808 // Got a class name - load it. 809 TempNewSymbol class_name_symbol = SymbolTable::new_permanent_symbol(class_name, THREAD); 810 guarantee(!HAS_PENDING_EXCEPTION, "Exception creating a symbol."); 811 Klass* klass = SystemDictionary::resolve_or_null(class_name_symbol, 812 THREAD); 813 CLEAR_PENDING_EXCEPTION; 814 if (klass != NULL) { 815 if (PrintSharedSpaces && Verbose && WizardMode) { 816 tty->print_cr("Shared spaces preloaded: %s", class_name); 817 } 818 819 InstanceKlass* ik = InstanceKlass::cast(klass); 820 821 // Should be class load order as per -XX:+TraceClassLoadingPreorder 822 class_promote_order->append(ik); 823 824 // Link the class to cause the bytecodes to be rewritten and the 825 // cpcache to be created. The linking is done as soon as classes 826 // are loaded in order that the related data structures (klass and 827 // cpCache) are located together. 828 try_link_class(ik, THREAD); 829 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class"); 830 831 class_count++; 832 } else { 833 //tty->print_cr("Preload failed: %s", class_name); 834 } 835 } 836 fclose(file); 837 } else { 838 char errmsg[JVM_MAXPATHLEN]; 839 os::lasterror(errmsg, JVM_MAXPATHLEN); 840 tty->print_cr("Loading classlist failed: %s", errmsg); 841 exit(1); 842 } 843 844 return class_count; 845 } 846 847 // Returns true if the class's status has changed 848 bool MetaspaceShared::try_link_class(InstanceKlass* ik, TRAPS) { 849 assert(DumpSharedSpaces, "should only be called during dumping"); 850 if (ik->init_state() < InstanceKlass::linked) { 851 bool saved = BytecodeVerificationLocal; 852 if (!SharedClassUtil::is_shared_boot_class(ik)) { 853 // The verification decision is based on BytecodeVerificationRemote 854 // for non-system classes. Since we are using the NULL classloader 855 // to load non-system classes during dumping, we need to temporarily 856 // change BytecodeVerificationLocal to be the same as 857 // BytecodeVerificationRemote. Note this can cause the parent system 858 // classes also being verified. The extra overhead is acceptable during 859 // dumping. 860 BytecodeVerificationLocal = BytecodeVerificationRemote; 861 } 862 ik->link_class(THREAD); 863 if (HAS_PENDING_EXCEPTION) { 864 ResourceMark rm; 865 tty->print_cr("Preload Warning: Verification failed for %s", 866 ik->external_name()); 867 CLEAR_PENDING_EXCEPTION; 868 ik->set_in_error_state(); 869 _has_error_classes = true; 870 } 871 BytecodeVerificationLocal = saved; 872 return true; 873 } else { 874 return false; 875 } 876 } 877 878 // Closure for serializing initialization data in from a data area 879 // (ptr_array) read from the shared file. 880 881 class ReadClosure : public SerializeClosure { 882 private: 883 intptr_t** _ptr_array; 884 885 inline intptr_t nextPtr() { 886 return *(*_ptr_array)++; 887 } 888 889 public: 890 ReadClosure(intptr_t** ptr_array) { _ptr_array = ptr_array; } 891 892 void do_ptr(void** p) { 893 assert(*p == NULL, "initializing previous initialized pointer."); 894 intptr_t obj = nextPtr(); 895 assert((intptr_t)obj >= 0 || (intptr_t)obj < -100, 896 "hit tag while initializing ptrs."); 897 *p = (void*)obj; 898 } 899 900 void do_tag(int tag) { 901 int old_tag; 902 old_tag = (int)(intptr_t)nextPtr(); 903 // do_int(&old_tag); 904 assert(tag == old_tag, "old tag doesn't match"); 905 FileMapInfo::assert_mark(tag == old_tag); 906 } 907 908 void do_region(u_char* start, size_t size) { 909 assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment"); 910 assert(size % sizeof(intptr_t) == 0, "bad size"); 911 do_tag((int)size); 912 while (size > 0) { 913 *(intptr_t*)start = nextPtr(); 914 start += sizeof(intptr_t); 915 size -= sizeof(intptr_t); 916 } 917 } 918 919 bool reading() const { return true; } 920 }; 921 922 // Return true if given address is in the mapped shared space. 923 bool MetaspaceShared::is_in_shared_space(const void* p) { 924 return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_space(p); 925 } 926 927 void MetaspaceShared::print_shared_spaces() { 928 if (UseSharedSpaces) { 929 FileMapInfo::current_info()->print_shared_spaces(); 930 } 931 } 932 933 934 // Map shared spaces at requested addresses and return if succeeded. 935 // Need to keep the bounds of the ro and rw space for the Metaspace::contains 936 // call, or is_in_shared_space. 937 bool MetaspaceShared::map_shared_spaces(FileMapInfo* mapinfo) { 938 size_t image_alignment = mapinfo->alignment(); 939 940 #ifndef _WINDOWS 941 // Map in the shared memory and then map the regions on top of it. 942 // On Windows, don't map the memory here because it will cause the 943 // mappings of the regions to fail. 944 ReservedSpace shared_rs = mapinfo->reserve_shared_memory(); 945 if (!shared_rs.is_reserved()) return false; 946 #endif 947 948 assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces"); 949 950 char* _ro_base = NULL; 951 char* _rw_base = NULL; 952 char* _md_base = NULL; 953 char* _mc_base = NULL; 954 955 // Map each shared region 956 if ((_ro_base = mapinfo->map_region(ro)) != NULL && 957 mapinfo->verify_region_checksum(ro) && 958 (_rw_base = mapinfo->map_region(rw)) != NULL && 959 mapinfo->verify_region_checksum(rw) && 960 (_md_base = mapinfo->map_region(md)) != NULL && 961 mapinfo->verify_region_checksum(md) && 962 (_mc_base = mapinfo->map_region(mc)) != NULL && 963 mapinfo->verify_region_checksum(mc) && 964 (image_alignment == (size_t)max_alignment()) && 965 mapinfo->validate_classpath_entry_table()) { 966 // Success (no need to do anything) 967 return true; 968 } else { 969 // If there was a failure in mapping any of the spaces, unmap the ones 970 // that succeeded 971 if (_ro_base != NULL) mapinfo->unmap_region(ro); 972 if (_rw_base != NULL) mapinfo->unmap_region(rw); 973 if (_md_base != NULL) mapinfo->unmap_region(md); 974 if (_mc_base != NULL) mapinfo->unmap_region(mc); 975 #ifndef _WINDOWS 976 // Release the entire mapped region 977 shared_rs.release(); 978 #endif 979 // If -Xshare:on is specified, print out the error message and exit VM, 980 // otherwise, set UseSharedSpaces to false and continue. 981 if (RequireSharedSpaces) { 982 vm_exit_during_initialization("Unable to use shared archive.", "Failed map_region for using -Xshare:on."); 983 } else { 984 FLAG_SET_DEFAULT(UseSharedSpaces, false); 985 } 986 return false; 987 } 988 } 989 990 // Read the miscellaneous data from the shared file, and 991 // serialize it out to its various destinations. 992 993 void MetaspaceShared::initialize_shared_spaces() { 994 FileMapInfo *mapinfo = FileMapInfo::current_info(); 995 996 char* buffer = mapinfo->region_base(md); 997 998 // Skip over (reserve space for) a list of addresses of C++ vtables 999 // for Klass objects. They get filled in later. 1000 1001 void** vtbl_list = (void**)buffer; 1002 buffer += MetaspaceShared::vtbl_list_size * sizeof(void*); 1003 Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size); 1004 1005 // Skip over (reserve space for) dummy C++ vtables Klass objects. 1006 // They are used as is. 1007 1008 intptr_t vtable_size = *(intptr_t*)buffer; 1009 buffer += sizeof(intptr_t); 1010 buffer += vtable_size; 1011 1012 // Create the shared symbol table using the bucket array at this spot in the 1013 // misc data space. (Todo: move this to read-only space. Currently 1014 // this is mapped copy-on-write but will never be written into). 1015 1016 buffer = (char*)SymbolTable::init_shared_table(buffer); 1017 SymbolTable::create_table(); 1018 1019 // Create the shared dictionary using the bucket array at this spot in 1020 // the misc data space. Since the shared dictionary table is never 1021 // modified, this region (of mapped pages) will be (effectively, if 1022 // not explicitly) read-only. 1023 1024 int sharedDictionaryLen = *(intptr_t*)buffer; 1025 buffer += sizeof(intptr_t); 1026 int number_of_entries = *(intptr_t*)buffer; 1027 buffer += sizeof(intptr_t); 1028 SystemDictionary::set_shared_dictionary((HashtableBucket<mtClass>*)buffer, 1029 sharedDictionaryLen, 1030 number_of_entries); 1031 buffer += sharedDictionaryLen; 1032 1033 // Create the package info table using the bucket array at this spot in 1034 // the misc data space. Since the package info table is never 1035 // modified, this region (of mapped pages) will be (effectively, if 1036 // not explicitly) read-only. 1037 1038 int pkgInfoLen = *(intptr_t*)buffer; 1039 buffer += sizeof(intptr_t); 1040 number_of_entries = *(intptr_t*)buffer; 1041 buffer += sizeof(intptr_t); 1042 ClassLoader::create_package_info_table((HashtableBucket<mtClass>*)buffer, pkgInfoLen, 1043 number_of_entries); 1044 buffer += pkgInfoLen; 1045 ClassLoader::verify(); 1046 1047 // The following data in the shared misc data region are the linked 1048 // list elements (HashtableEntry objects) for the shared dictionary 1049 // and package info table. 1050 1051 int len = *(intptr_t*)buffer; // skip over shared dictionary entries 1052 buffer += sizeof(intptr_t); 1053 buffer += len; 1054 1055 len = *(intptr_t*)buffer; // skip over package info table entries 1056 buffer += sizeof(intptr_t); 1057 buffer += len; 1058 1059 len = *(intptr_t*)buffer; // skip over package info table char[] arrays. 1060 buffer += sizeof(intptr_t); 1061 buffer += len; 1062 1063 intptr_t* array = (intptr_t*)buffer; 1064 ReadClosure rc(&array); 1065 serialize(&rc); 1066 1067 // Close the mapinfo file 1068 mapinfo->close(); 1069 1070 if (PrintSharedArchiveAndExit) { 1071 if (PrintSharedDictionary) { 1072 tty->print_cr("\nShared classes:\n"); 1073 SystemDictionary::print_shared(false); 1074 } 1075 if (_archive_loading_failed) { 1076 tty->print_cr("archive is invalid"); 1077 vm_exit(1); 1078 } else { 1079 tty->print_cr("archive is valid"); 1080 vm_exit(0); 1081 } 1082 } 1083 } 1084 1085 // JVM/TI RedefineClasses() support: 1086 bool MetaspaceShared::remap_shared_readonly_as_readwrite() { 1087 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 1088 1089 if (UseSharedSpaces) { 1090 // remap the shared readonly space to shared readwrite, private 1091 FileMapInfo* mapinfo = FileMapInfo::current_info(); 1092 if (!mapinfo->remap_shared_readonly_as_readwrite()) { 1093 return false; 1094 } 1095 } 1096 return true; 1097 } 1098 1099 int MetaspaceShared::count_class(const char* classlist_file) { 1100 if (classlist_file == NULL) { 1101 return 0; 1102 } 1103 char class_name[256]; 1104 int class_count = 0; 1105 FILE* file = fopen(classlist_file, "r"); 1106 if (file != NULL) { 1107 while ((fgets(class_name, sizeof class_name, file)) != NULL) { 1108 if (*class_name == '#') { // comment 1109 continue; 1110 } 1111 class_count++; 1112 } 1113 fclose(file); 1114 } else { 1115 char errmsg[JVM_MAXPATHLEN]; 1116 os::lasterror(errmsg, JVM_MAXPATHLEN); 1117 tty->print_cr("Loading classlist failed: %s", errmsg); 1118 exit(1); 1119 } 1120 1121 return class_count; 1122 } 1123 1124 // the sizes are good for typical large applications that have a lot of shared 1125 // classes 1126 void MetaspaceShared::estimate_regions_size() { 1127 int class_count = count_class(SharedClassListFile); 1128 class_count += count_class(ExtraSharedClassListFile); 1129 1130 if (class_count > LargeThresholdClassCount) { 1131 if (class_count < HugeThresholdClassCount) { 1132 SET_ESTIMATED_SIZE(Large, ReadOnly); 1133 SET_ESTIMATED_SIZE(Large, ReadWrite); 1134 SET_ESTIMATED_SIZE(Large, MiscData); 1135 SET_ESTIMATED_SIZE(Large, MiscCode); 1136 } else { 1137 SET_ESTIMATED_SIZE(Huge, ReadOnly); 1138 SET_ESTIMATED_SIZE(Huge, ReadWrite); 1139 SET_ESTIMATED_SIZE(Huge, MiscData); 1140 SET_ESTIMATED_SIZE(Huge, MiscCode); 1141 } 1142 } 1143 }