1 /* 2 * Copyright (c) 2003, 2012, 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/javaClasses.hpp" 27 #include "classfile/loaderConstraints.hpp" 28 #include "classfile/symbolTable.hpp" 29 #include "classfile/systemDictionary.hpp" 30 #include "gc_implementation/shared/spaceDecorator.hpp" 31 #include "memory/classify.hpp" 32 #include "memory/filemap.hpp" 33 #include "memory/oopFactory.hpp" 34 #include "memory/resourceArea.hpp" 35 #include "oops/methodDataOop.hpp" 36 #include "oops/oop.inline.hpp" 37 #include "runtime/javaCalls.hpp" 38 #include "runtime/signature.hpp" 39 #include "runtime/vmThread.hpp" 40 #include "runtime/vm_operations.hpp" 41 #include "utilities/copy.hpp" 42 43 44 // Closure to set up the fingerprint field for all methods. 45 46 class FingerprintMethodsClosure: public ObjectClosure { 47 public: 48 void do_object(oop obj) { 49 if (obj->is_method()) { 50 methodOop mobj = (methodOop)obj; 51 ResourceMark rm; 52 (new Fingerprinter(mobj))->fingerprint(); 53 } 54 } 55 }; 56 57 58 59 // Closure to set the hash value (String.hash field) in all of the 60 // String objects in the heap. Setting the hash value is not required. 61 // However, setting the value in advance prevents the value from being 62 // written later, increasing the likelihood that the shared page contain 63 // the hash can be shared. 64 // 65 // NOTE THAT the algorithm in StringTable::hash_string() MUST MATCH the 66 // algorithm in java.lang.String.hashCode(). 67 68 class StringHashCodeClosure: public OopClosure { 69 private: 70 Thread* THREAD; 71 int hash_offset; 72 public: 73 StringHashCodeClosure(Thread* t) { 74 THREAD = t; 75 hash_offset = java_lang_String::hash_offset_in_bytes(); 76 } 77 78 void do_oop(oop* p) { 79 if (p != NULL) { 80 oop obj = *p; 81 if (obj->klass() == SystemDictionary::String_klass()) { 82 83 int hash = java_lang_String::hash_string(obj); 84 obj->int_field_put(hash_offset, hash); 85 } 86 } 87 } 88 void do_oop(narrowOop* p) { ShouldNotReachHere(); } 89 }; 90 91 92 // Remove data from objects which should not appear in the shared file 93 // (as it pertains only to the current JVM). 94 95 class RemoveUnshareableInfoClosure : public ObjectClosure { 96 public: 97 void do_object(oop obj) { 98 // Zap data from the objects which is pertains only to this JVM. We 99 // want that data recreated in new JVMs when the shared file is used. 100 if (obj->is_method()) { 101 ((methodOop)obj)->remove_unshareable_info(); 102 } 103 else if (obj->is_klass()) { 104 Klass::cast((klassOop)obj)->remove_unshareable_info(); 105 } 106 107 // Don't save compiler related special oops (shouldn't be any yet). 108 if (obj->is_methodData() || obj->is_compiledICHolder()) { 109 ShouldNotReachHere(); 110 } 111 } 112 }; 113 114 115 static bool mark_object(oop obj) { 116 if (obj != NULL && 117 !obj->is_shared() && 118 !obj->is_forwarded() && 119 !obj->is_gc_marked()) { 120 obj->set_mark(markOopDesc::prototype()->set_marked()); 121 return true; 122 } 123 124 return false; 125 } 126 127 128 class MoveSymbols : public SymbolClosure { 129 private: 130 char* _start; 131 char* _end; 132 char* _top; 133 int _count; 134 135 bool in_shared_space(Symbol* sym) const { 136 return (char*)sym >= _start && (char*)sym < _end; 137 } 138 139 Symbol* get_shared_copy(Symbol* sym) { 140 return sym->refcount() > 0 ? NULL : (Symbol*)(_start - sym->refcount()); 141 } 142 143 Symbol* make_shared_copy(Symbol* sym) { 144 Symbol* new_sym = (Symbol*)_top; 145 int size = sym->object_size(); 146 _top += size * HeapWordSize; 147 if (_top <= _end) { 148 Copy::disjoint_words((HeapWord*)sym, (HeapWord*)new_sym, size); 149 // Encode a reference to the copy as a negative distance from _start 150 // When a symbol is being copied to a shared space 151 // during CDS archive creation, the original symbol is marked 152 // as relocated by putting a negative value to its _refcount field, 153 // This value is also used to find where exactly the shared copy is 154 // (see MoveSymbols::get_shared_copy), so that the other references 155 // to this symbol could be changed to point to the shared copy. 156 sym->_refcount = (int)(_start - (char*)new_sym); 157 // Mark the symbol in the shared archive as immortal so it is read only 158 // and not refcounted. 159 new_sym->_refcount = -1; 160 _count++; 161 } else { 162 report_out_of_shared_space(SharedMiscData); 163 } 164 return new_sym; 165 } 166 167 public: 168 MoveSymbols(char* top, char* end) : 169 _start(top), _end(end), _top(top), _count(0) { } 170 171 char* get_top() const { return _top; } 172 int count() const { return _count; } 173 174 void do_symbol(Symbol** p) { 175 Symbol* sym = load_symbol(p); 176 if (sym != NULL && !in_shared_space(sym)) { 177 Symbol* new_sym = get_shared_copy(sym); 178 if (new_sym == NULL) { 179 // The symbol has not been relocated yet; copy it to _top address 180 assert(sym->refcount() > 0, "should have positive reference count"); 181 new_sym = make_shared_copy(sym); 182 } 183 // Make the reference point to the shared copy of the symbol 184 store_symbol(p, new_sym); 185 } 186 } 187 }; 188 189 190 // Closure: mark objects closure. 191 192 class MarkObjectsOopClosure : public OopClosure { 193 public: 194 void do_oop(oop* p) { mark_object(*p); } 195 void do_oop(narrowOop* p) { ShouldNotReachHere(); } 196 }; 197 198 199 class MarkObjectsSkippingKlassesOopClosure : public OopClosure { 200 public: 201 void do_oop(oop* pobj) { 202 oop obj = *pobj; 203 if (obj != NULL && 204 !obj->is_klass()) { 205 mark_object(obj); 206 } 207 } 208 void do_oop(narrowOop* pobj) { ShouldNotReachHere(); } 209 }; 210 211 212 static void mark_object_recursive_skipping_klasses(oop obj) { 213 mark_object(obj); 214 if (obj != NULL) { 215 MarkObjectsSkippingKlassesOopClosure mark_all; 216 obj->oop_iterate(&mark_all); 217 } 218 } 219 220 221 // Closure: mark common read-only objects 222 223 class MarkCommonReadOnly : public ObjectClosure { 224 private: 225 MarkObjectsOopClosure mark_all; 226 public: 227 void do_object(oop obj) { 228 229 // Mark all constMethod objects. 230 231 if (obj->is_constMethod()) { 232 mark_object(obj); 233 mark_object(constMethodOop(obj)->stackmap_data()); 234 } 235 236 // Mark objects referenced by klass objects which are read-only. 237 238 else if (obj->is_klass()) { 239 Klass* k = Klass::cast((klassOop)obj); 240 mark_object(k->secondary_supers()); 241 242 // The METHODS() OBJARRAYS CANNOT BE MADE READ-ONLY, even though 243 // it is never modified. Otherwise, they will be pre-marked; the 244 // GC marking phase will skip them; and by skipping them will fail 245 // to mark the methods objects referenced by the array. 246 247 if (obj->blueprint()->oop_is_instanceKlass()) { 248 instanceKlass* ik = instanceKlass::cast((klassOop)obj); 249 mark_object(ik->method_ordering()); 250 mark_object(ik->local_interfaces()); 251 mark_object(ik->transitive_interfaces()); 252 mark_object(ik->fields()); 253 254 mark_object(ik->class_annotations()); 255 256 mark_object_recursive_skipping_klasses(ik->fields_annotations()); 257 mark_object_recursive_skipping_klasses(ik->methods_annotations()); 258 mark_object_recursive_skipping_klasses(ik->methods_parameter_annotations()); 259 mark_object_recursive_skipping_klasses(ik->methods_default_annotations()); 260 261 typeArrayOop inner_classes = ik->inner_classes(); 262 if (inner_classes != NULL) { 263 mark_object(inner_classes); 264 } 265 } 266 } 267 } 268 }; 269 270 271 // Closure: find symbol references in Java Heap objects 272 273 class CommonSymbolsClosure : public ObjectClosure { 274 private: 275 SymbolClosure* _closure; 276 public: 277 CommonSymbolsClosure(SymbolClosure* closure) : _closure(closure) { } 278 279 void do_object(oop obj) { 280 281 // Traverse symbols referenced by method objects. 282 283 if (obj->is_method()) { 284 methodOop m = methodOop(obj); 285 constantPoolOop constants = m->constants(); 286 _closure->do_symbol(constants->symbol_at_addr(m->name_index())); 287 _closure->do_symbol(constants->symbol_at_addr(m->signature_index())); 288 } 289 290 // Traverse symbols referenced by klass objects which are read-only. 291 292 else if (obj->is_klass()) { 293 Klass* k = Klass::cast((klassOop)obj); 294 k->shared_symbols_iterate(_closure); 295 296 if (obj->blueprint()->oop_is_instanceKlass()) { 297 instanceKlass* ik = instanceKlass::cast((klassOop)obj); 298 instanceKlassHandle ik_h((klassOop)obj); 299 InnerClassesIterator iter(ik_h); 300 constantPoolOop constants = ik->constants(); 301 for (; !iter.done(); iter.next()) { 302 int index = iter.inner_name_index(); 303 304 if (index != 0) { 305 _closure->do_symbol(constants->symbol_at_addr(index)); 306 } 307 } 308 } 309 } 310 311 // Traverse symbols referenced by other constantpool entries. 312 313 else if (obj->is_constantPool()) { 314 constantPoolOop(obj)->shared_symbols_iterate(_closure); 315 } 316 } 317 }; 318 319 320 // Closure: mark char arrays used by strings 321 322 class MarkStringValues : public ObjectClosure { 323 private: 324 MarkObjectsOopClosure mark_all; 325 public: 326 void do_object(oop obj) { 327 328 // Character arrays referenced by String objects are read-only. 329 330 if (java_lang_String::is_instance(obj)) { 331 mark_object(java_lang_String::value(obj)); 332 } 333 } 334 }; 335 336 337 #ifdef DEBUG 338 // Closure: Check for objects left in the heap which have not been moved. 339 340 class CheckRemainingObjects : public ObjectClosure { 341 private: 342 int count; 343 344 public: 345 CheckRemainingObjects() { 346 count = 0; 347 } 348 349 void do_object(oop obj) { 350 if (!obj->is_shared() && 351 !obj->is_forwarded()) { 352 ++count; 353 if (Verbose) { 354 tty->print("Unreferenced object: "); 355 obj->print_on(tty); 356 } 357 } 358 } 359 360 void status() { 361 tty->print_cr("%d objects no longer referenced, not shared.", count); 362 } 363 }; 364 #endif 365 366 367 // Closure: Mark remaining objects read-write, except Strings. 368 369 class MarkReadWriteObjects : public ObjectClosure { 370 private: 371 MarkObjectsOopClosure mark_objects; 372 public: 373 void do_object(oop obj) { 374 375 // The METHODS() OBJARRAYS CANNOT BE MADE READ-ONLY, even though 376 // it is never modified. Otherwise, they will be pre-marked; the 377 // GC marking phase will skip them; and by skipping them will fail 378 // to mark the methods objects referenced by the array. 379 380 if (obj->is_klass()) { 381 mark_object(obj); 382 Klass* k = klassOop(obj)->klass_part(); 383 mark_object(k->java_mirror()); 384 if (obj->blueprint()->oop_is_instanceKlass()) { 385 instanceKlass* ik = (instanceKlass*)k; 386 mark_object(ik->methods()); 387 mark_object(ik->constants()); 388 } 389 if (obj->blueprint()->oop_is_javaArray()) { 390 arrayKlass* ak = (arrayKlass*)k; 391 mark_object(ak->component_mirror()); 392 } 393 return; 394 } 395 396 // Mark constantPool tags and the constantPoolCache. 397 398 else if (obj->is_constantPool()) { 399 constantPoolOop pool = constantPoolOop(obj); 400 mark_object(pool->cache()); 401 pool->shared_tags_iterate(&mark_objects); 402 return; 403 } 404 405 // Mark all method objects. 406 407 if (obj->is_method()) { 408 mark_object(obj); 409 } 410 } 411 }; 412 413 414 // Closure: Mark String objects read-write. 415 416 class MarkStringObjects : public ObjectClosure { 417 private: 418 MarkObjectsOopClosure mark_objects; 419 public: 420 void do_object(oop obj) { 421 422 // Mark String objects referenced by constant pool entries. 423 424 if (obj->is_constantPool()) { 425 constantPoolOop pool = constantPoolOop(obj); 426 pool->shared_strings_iterate(&mark_objects); 427 return; 428 } 429 } 430 }; 431 432 433 // Move objects matching specified type (ie. lock_bits) to the specified 434 // space. 435 436 class MoveMarkedObjects : public ObjectClosure { 437 private: 438 OffsetTableContigSpace* _space; 439 bool _read_only; 440 441 public: 442 MoveMarkedObjects(OffsetTableContigSpace* space, bool read_only) { 443 _space = space; 444 _read_only = read_only; 445 } 446 447 void do_object(oop obj) { 448 if (obj->is_shared()) { 449 return; 450 } 451 if (obj->is_gc_marked() && obj->forwardee() == NULL) { 452 int s = obj->size(); 453 oop sh_obj = (oop)_space->allocate(s); 454 if (sh_obj == NULL) { 455 report_out_of_shared_space(_read_only ? SharedReadOnly : SharedReadWrite); 456 } 457 if (PrintSharedSpaces && Verbose && WizardMode) { 458 tty->print_cr("\nMoveMarkedObjects: " PTR_FORMAT " -> " PTR_FORMAT " %s", obj, sh_obj, 459 (_read_only ? "ro" : "rw")); 460 } 461 Copy::aligned_disjoint_words((HeapWord*)obj, (HeapWord*)sh_obj, s); 462 obj->forward_to(sh_obj); 463 if (_read_only) { 464 // Readonly objects: set hash value to self pointer and make gc_marked. 465 sh_obj->forward_to(sh_obj); 466 } else { 467 sh_obj->init_mark(); 468 } 469 } 470 } 471 }; 472 473 static void mark_and_move(oop obj, MoveMarkedObjects* move) { 474 if (mark_object(obj)) move->do_object(obj); 475 } 476 477 enum order_policy { 478 OP_favor_startup = 0, 479 OP_balanced = 1, 480 OP_favor_runtime = 2 481 }; 482 483 static void mark_and_move_for_policy(order_policy policy, oop obj, MoveMarkedObjects* move) { 484 if (SharedOptimizeColdStartPolicy >= policy) mark_and_move(obj, move); 485 } 486 487 class MarkAndMoveOrderedReadOnly : public ObjectClosure { 488 private: 489 MoveMarkedObjects *_move_ro; 490 491 public: 492 MarkAndMoveOrderedReadOnly(MoveMarkedObjects *move_ro) : _move_ro(move_ro) {} 493 494 void do_object(oop obj) { 495 if (obj->is_klass() && obj->blueprint()->oop_is_instanceKlass()) { 496 instanceKlass* ik = instanceKlass::cast((klassOop)obj); 497 int i; 498 499 if (ik->super() != NULL) { 500 do_object(ik->super()); 501 } 502 503 objArrayOop interfaces = ik->local_interfaces(); 504 mark_and_move_for_policy(OP_favor_startup, interfaces, _move_ro); 505 for(i = 0; i < interfaces->length(); i++) { 506 klassOop k = klassOop(interfaces->obj_at(i)); 507 do_object(k); 508 } 509 510 objArrayOop methods = ik->methods(); 511 for(i = 0; i < methods->length(); i++) { 512 methodOop m = methodOop(methods->obj_at(i)); 513 mark_and_move_for_policy(OP_favor_startup, m->constMethod(), _move_ro); 514 mark_and_move_for_policy(OP_favor_runtime, m->constMethod()->stackmap_data(), _move_ro); 515 } 516 517 mark_and_move_for_policy(OP_favor_startup, ik->transitive_interfaces(), _move_ro); 518 mark_and_move_for_policy(OP_favor_startup, ik->fields(), _move_ro); 519 520 mark_and_move_for_policy(OP_favor_runtime, ik->secondary_supers(), _move_ro); 521 mark_and_move_for_policy(OP_favor_runtime, ik->method_ordering(), _move_ro); 522 mark_and_move_for_policy(OP_favor_runtime, ik->class_annotations(), _move_ro); 523 mark_and_move_for_policy(OP_favor_runtime, ik->fields_annotations(), _move_ro); 524 mark_and_move_for_policy(OP_favor_runtime, ik->methods_annotations(), _move_ro); 525 mark_and_move_for_policy(OP_favor_runtime, ik->methods_parameter_annotations(), _move_ro); 526 mark_and_move_for_policy(OP_favor_runtime, ik->methods_default_annotations(), _move_ro); 527 mark_and_move_for_policy(OP_favor_runtime, ik->inner_classes(), _move_ro); 528 mark_and_move_for_policy(OP_favor_runtime, ik->secondary_supers(), _move_ro); 529 } 530 } 531 }; 532 533 class MarkAndMoveOrderedReadWrite: public ObjectClosure { 534 private: 535 MoveMarkedObjects *_move_rw; 536 537 public: 538 MarkAndMoveOrderedReadWrite(MoveMarkedObjects *move_rw) : _move_rw(move_rw) {} 539 540 void do_object(oop obj) { 541 if (obj->is_klass() && obj->blueprint()->oop_is_instanceKlass()) { 542 instanceKlass* ik = instanceKlass::cast((klassOop)obj); 543 int i; 544 545 mark_and_move_for_policy(OP_favor_startup, ik->as_klassOop(), _move_rw); 546 547 if (ik->super() != NULL) { 548 do_object(ik->super()); 549 } 550 551 objArrayOop interfaces = ik->local_interfaces(); 552 for(i = 0; i < interfaces->length(); i++) { 553 klassOop k = klassOop(interfaces->obj_at(i)); 554 mark_and_move_for_policy(OP_favor_startup, k, _move_rw); 555 do_object(k); 556 } 557 558 objArrayOop methods = ik->methods(); 559 mark_and_move_for_policy(OP_favor_startup, methods, _move_rw); 560 for(i = 0; i < methods->length(); i++) { 561 methodOop m = methodOop(methods->obj_at(i)); 562 mark_and_move_for_policy(OP_favor_startup, m, _move_rw); 563 mark_and_move_for_policy(OP_favor_startup, ik->constants(), _move_rw); // idempotent 564 mark_and_move_for_policy(OP_balanced, ik->constants()->cache(), _move_rw); // idempotent 565 mark_and_move_for_policy(OP_balanced, ik->constants()->tags(), _move_rw); // idempotent 566 } 567 568 mark_and_move_for_policy(OP_favor_startup, ik->as_klassOop()->klass(), _move_rw); 569 mark_and_move_for_policy(OP_favor_startup, ik->constants()->klass(), _move_rw); 570 571 // Although Java mirrors are marked in MarkReadWriteObjects, 572 // apparently they were never moved into shared spaces since 573 // MoveMarkedObjects skips marked instance oops. This may 574 // be a bug in the original implementation or simply the vestige 575 // of an abandoned experiment. Nevertheless we leave a hint 576 // here in case this capability is ever correctly implemented. 577 // 578 // mark_and_move_for_policy(OP_favor_runtime, ik->java_mirror(), _move_rw); 579 } 580 } 581 582 }; 583 584 // Adjust references in oops to refer to shared spaces. 585 586 class ResolveForwardingClosure: public OopClosure { 587 public: 588 void do_oop(oop* p) { 589 oop obj = *p; 590 if (!obj->is_shared()) { 591 if (obj != NULL) { 592 oop f = obj->forwardee(); 593 guarantee(f->is_shared(), "Oop doesn't refer to shared space."); 594 *p = f; 595 } 596 } 597 } 598 void do_oop(narrowOop* pobj) { ShouldNotReachHere(); } 599 }; 600 601 602 // The methods array must be reordered by Symbol* address. 603 // (See classFileParser.cpp where methods in a class are originally 604 // sorted). The addresses of symbols have been changed as a result 605 // of moving to the shared space. 606 607 class SortMethodsClosure: public ObjectClosure { 608 public: 609 void do_object(oop obj) { 610 if (obj->blueprint()->oop_is_instanceKlass()) { 611 instanceKlass* ik = instanceKlass::cast((klassOop)obj); 612 methodOopDesc::sort_methods(ik->methods(), 613 ik->methods_annotations(), 614 ik->methods_parameter_annotations(), 615 ik->methods_default_annotations(), 616 true /* idempotent, slow */); 617 } 618 } 619 }; 620 621 // Vtable and Itable indices are calculated based on methods array 622 // order (see klassItable::compute_itable_index()). Must reinitialize 623 // after ALL methods of ALL classes have been reordered. 624 // We assume that since checkconstraints is false, this method 625 // cannot throw an exception. An exception here would be 626 // problematic since this is the VMThread, not a JavaThread. 627 628 class ReinitializeTables: public ObjectClosure { 629 private: 630 Thread* _thread; 631 632 public: 633 ReinitializeTables(Thread* thread) : _thread(thread) {} 634 635 // Initialize super vtable first, check if already initialized to avoid 636 // quadradic behavior. The vtable is cleared in remove_unshareable_info. 637 void reinitialize_vtables(klassOop k) { 638 if (k->blueprint()->oop_is_instanceKlass()) { 639 instanceKlass* ik = instanceKlass::cast(k); 640 if (ik->vtable()->is_initialized()) return; 641 if (ik->super() != NULL) { 642 reinitialize_vtables(ik->super()); 643 } 644 ik->vtable()->initialize_vtable(false, _thread); 645 } 646 } 647 648 void do_object(oop obj) { 649 if (obj->blueprint()->oop_is_instanceKlass()) { 650 instanceKlass* ik = instanceKlass::cast((klassOop)obj); 651 ResourceMark rm(_thread); 652 ik->itable()->initialize_itable(false, _thread); 653 reinitialize_vtables((klassOop)obj); 654 #ifdef ASSERT 655 ik->vtable()->verify(tty, true); 656 #endif // ASSERT 657 } else if (obj->blueprint()->oop_is_arrayKlass()) { 658 // The vtable for array klasses are that of its super class, 659 // ie. java.lang.Object. 660 arrayKlass* ak = arrayKlass::cast((klassOop)obj); 661 if (ak->vtable()->is_initialized()) return; 662 ak->vtable()->initialize_vtable(false, _thread); 663 } 664 } 665 }; 666 667 668 // Adjust references in oops to refer to shared spaces. 669 670 class PatchOopsClosure: public ObjectClosure { 671 private: 672 Thread* _thread; 673 ResolveForwardingClosure resolve; 674 675 public: 676 PatchOopsClosure(Thread* thread) : _thread(thread) {} 677 678 void do_object(oop obj) { 679 obj->oop_iterate_header(&resolve); 680 obj->oop_iterate(&resolve); 681 682 assert(obj->klass()->is_shared(), "Klass not pointing into shared space."); 683 684 // If the object is a Java object or class which might (in the 685 // future) contain a reference to a young gen object, add it to the 686 // list. 687 688 if (obj->is_klass() || obj->is_instance()) { 689 if (obj->is_klass() || 690 obj->is_a(SystemDictionary::Class_klass()) || 691 obj->is_a(SystemDictionary::Throwable_klass())) { 692 // Do nothing 693 } 694 else if (obj->is_a(SystemDictionary::String_klass())) { 695 // immutable objects. 696 } else { 697 // someone added an object we hadn't accounted for. 698 ShouldNotReachHere(); 699 } 700 } 701 } 702 }; 703 704 705 // Empty the young and old generations. 706 707 class ClearSpaceClosure : public SpaceClosure { 708 public: 709 void do_space(Space* s) { 710 s->clear(SpaceDecorator::Mangle); 711 } 712 }; 713 714 715 // Closure for serializing initialization data out to a data area to be 716 // written to the shared file. 717 718 class WriteClosure : public SerializeOopClosure { 719 private: 720 oop* top; 721 char* end; 722 723 inline void check_space() { 724 if ((char*)top + sizeof(oop) > end) { 725 report_out_of_shared_space(SharedMiscData); 726 } 727 } 728 729 730 public: 731 WriteClosure(char* md_top, char* md_end) { 732 top = (oop*)md_top; 733 end = md_end; 734 } 735 736 char* get_top() { return (char*)top; } 737 738 void do_oop(oop* p) { 739 check_space(); 740 oop obj = *p; 741 assert(obj->is_oop_or_null(), "invalid oop"); 742 assert(obj == NULL || obj->is_shared(), 743 "Oop in shared space not pointing into shared space."); 744 *top = obj; 745 ++top; 746 } 747 748 void do_oop(narrowOop* pobj) { ShouldNotReachHere(); } 749 750 void do_int(int* p) { 751 check_space(); 752 *top = (oop)(intptr_t)*p; 753 ++top; 754 } 755 756 void do_size_t(size_t* p) { 757 check_space(); 758 *top = (oop)(intptr_t)*p; 759 ++top; 760 } 761 762 void do_ptr(void** p) { 763 check_space(); 764 *top = (oop)*p; 765 ++top; 766 } 767 768 void do_ptr(HeapWord** p) { do_ptr((void **) p); } 769 770 void do_tag(int tag) { 771 check_space(); 772 *top = (oop)(intptr_t)tag; 773 ++top; 774 } 775 776 void do_region(u_char* start, size_t size) { 777 if ((char*)top + size > end) { 778 report_out_of_shared_space(SharedMiscData); 779 } 780 assert((intptr_t)start % sizeof(oop) == 0, "bad alignment"); 781 assert(size % sizeof(oop) == 0, "bad size"); 782 do_tag((int)size); 783 while (size > 0) { 784 *top = *(oop*)start; 785 ++top; 786 start += sizeof(oop); 787 size -= sizeof(oop); 788 } 789 } 790 791 bool reading() const { return false; } 792 }; 793 794 795 class ResolveConstantPoolsClosure : public ObjectClosure { 796 private: 797 TRAPS; 798 public: 799 ResolveConstantPoolsClosure(Thread *t) { 800 __the_thread__ = t; 801 } 802 void do_object(oop obj) { 803 if (obj->is_constantPool()) { 804 constantPoolOop cpool = (constantPoolOop)obj; 805 int unresolved = cpool->pre_resolve_shared_klasses(THREAD); 806 } 807 } 808 }; 809 810 811 // Print a summary of the contents of the read/write spaces to help 812 // identify objects which might be able to be made read-only. At this 813 // point, the objects have been written, and we can trash them as 814 // needed. 815 816 static void print_contents() { 817 if (PrintSharedSpaces) { 818 GenCollectedHeap* gch = GenCollectedHeap::heap(); 819 CompactingPermGenGen* gen = (CompactingPermGenGen*)gch->perm_gen(); 820 821 // High level summary of the read-only space: 822 823 ClassifyObjectClosure coc; 824 tty->cr(); tty->print_cr("ReadOnly space:"); 825 gen->ro_space()->object_iterate(&coc); 826 coc.print(); 827 828 // High level summary of the read-write space: 829 830 coc.reset(); 831 tty->cr(); tty->print_cr("ReadWrite space:"); 832 gen->rw_space()->object_iterate(&coc); 833 coc.print(); 834 835 // Reset counters 836 837 ClearAllocCountClosure cacc; 838 gen->ro_space()->object_iterate(&cacc); 839 gen->rw_space()->object_iterate(&cacc); 840 coc.reset(); 841 842 // Lower level summary of the read-only space: 843 844 gen->ro_space()->object_iterate(&coc); 845 tty->cr(); tty->print_cr("ReadOnly space:"); 846 ClassifyInstanceKlassClosure cikc; 847 gen->rw_space()->object_iterate(&cikc); 848 cikc.print(); 849 850 // Reset counters 851 852 gen->ro_space()->object_iterate(&cacc); 853 gen->rw_space()->object_iterate(&cacc); 854 coc.reset(); 855 856 // Lower level summary of the read-write space: 857 858 gen->rw_space()->object_iterate(&coc); 859 cikc.reset(); 860 tty->cr(); tty->print_cr("ReadWrite space:"); 861 gen->rw_space()->object_iterate(&cikc); 862 cikc.print(); 863 } 864 } 865 866 867 // Patch C++ vtable pointer in klass oops. 868 869 // Klass objects contain references to c++ vtables in the JVM library. 870 // Fix them to point to our constructed vtables. However, don't iterate 871 // across the space while doing this, as that causes the vtables to be 872 // patched, undoing our useful work. Instead, iterate to make a list, 873 // then use the list to do the fixing. 874 // 875 // Our constructed vtables: 876 // Dump time: 877 // 1. init_self_patching_vtbl_list: table of pointers to current virtual method addrs 878 // 2. generate_vtable_methods: create jump table, appended to above vtbl_list 879 // 3. PatchKlassVtables: for Klass list, patch the vtable entry to point to jump table 880 // rather than to current vtbl 881 // Table layout: NOTE FIXED SIZE 882 // 1. vtbl pointers 883 // 2. #Klass X #virtual methods per Klass 884 // 1 entry for each, in the order: 885 // Klass1:method1 entry, Klass1:method2 entry, ... Klass1:method<num_virtuals> entry 886 // Klass2:method1 entry, Klass2:method2 entry, ... Klass2:method<num_virtuals> entry 887 // ... 888 // Klass<vtbl_list_size>:method1 entry, Klass<vtbl_list_size>:method2 entry, 889 // ... Klass<vtbl_list_size>:method<num_virtuals> entry 890 // Sample entry: (Sparc): 891 // save(sp, -256, sp) 892 // ba,pt common_code 893 // mov XXX, %L0 %L0 gets: Klass index <<8 + method index (note: max method index 255) 894 // 895 // Restore time: 896 // 1. initialize_oops: reserve space for table 897 // 2. init_self_patching_vtbl_list: update pointers to NEW virtual method addrs in text 898 // 899 // Execution time: 900 // First virtual method call for any object of these Klass types: 901 // 1. object->klass->klass_part 902 // 2. vtable entry for that klass_part points to the jump table entries 903 // 3. branches to common_code with %O0/klass_part, %L0: Klass index <<8 + method index 904 // 4. common_code: 905 // Get address of new vtbl pointer for this Klass from updated table 906 // Update new vtbl pointer in the Klass: future virtual calls go direct 907 // Jump to method, using new vtbl pointer and method index 908 909 class PatchKlassVtables: public ObjectClosure { 910 private: 911 GrowableArray<klassOop>* _klass_objects; 912 913 public: 914 PatchKlassVtables() { 915 _klass_objects = new GrowableArray<klassOop>(); 916 } 917 918 void do_object(oop obj) { 919 if (obj->is_klass()) { 920 _klass_objects->append(klassOop(obj)); 921 } 922 } 923 924 void patch(void** vtbl_list, void* new_vtable_start) { 925 int n = _klass_objects->length(); 926 for (int i = 0; i < n; i++) { 927 klassOop obj = (klassOop)_klass_objects->at(i); 928 Klass* k = obj->klass_part(); 929 *(void**)k = CompactingPermGenGen::find_matching_vtbl_ptr( 930 vtbl_list, new_vtable_start, k); 931 } 932 } 933 }; 934 935 // Walk through all symbols and patch their vtable pointers. 936 // Note that symbols have vtable pointers only in non-product builds 937 // (see allocation.hpp). 938 939 #ifndef PRODUCT 940 class PatchSymbolVtables: public SymbolClosure { 941 private: 942 void* _new_vtbl_ptr; 943 944 public: 945 PatchSymbolVtables(void** vtbl_list, void* new_vtable_start) { 946 Symbol s; 947 _new_vtbl_ptr = CompactingPermGenGen::find_matching_vtbl_ptr( 948 vtbl_list, new_vtable_start, &s); 949 } 950 951 void do_symbol(Symbol** p) { 952 Symbol* sym = load_symbol(p); 953 *(void**)sym = _new_vtbl_ptr; 954 } 955 }; 956 #endif 957 958 959 // Populate the shared space. 960 961 class VM_PopulateDumpSharedSpace: public VM_Operation { 962 private: 963 GrowableArray<oop> *_class_promote_order; 964 OffsetTableContigSpace* _ro_space; 965 OffsetTableContigSpace* _rw_space; 966 VirtualSpace* _md_vs; 967 VirtualSpace* _mc_vs; 968 969 public: 970 VM_PopulateDumpSharedSpace(GrowableArray<oop> *class_promote_order, 971 OffsetTableContigSpace* ro_space, 972 OffsetTableContigSpace* rw_space, 973 VirtualSpace* md_vs, VirtualSpace* mc_vs) { 974 _class_promote_order = class_promote_order; 975 _ro_space = ro_space; 976 _rw_space = rw_space; 977 _md_vs = md_vs; 978 _mc_vs = mc_vs; 979 } 980 981 VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; } 982 void doit() { 983 Thread* THREAD = VMThread::vm_thread(); 984 NOT_PRODUCT(SystemDictionary::verify();) 985 // The following guarantee is meant to ensure that no loader constraints 986 // exist yet, since the constraints table is not shared. This becomes 987 // more important now that we don't re-initialize vtables/itables for 988 // shared classes at runtime, where constraints were previously created. 989 guarantee(SystemDictionary::constraints()->number_of_entries() == 0, 990 "loader constraints are not saved"); 991 // Revisit and implement this if we prelink method handle call sites: 992 guarantee(SystemDictionary::invoke_method_table() == NULL || 993 SystemDictionary::invoke_method_table()->number_of_entries() == 0, 994 "invoke method table is not saved"); 995 GenCollectedHeap* gch = GenCollectedHeap::heap(); 996 997 // At this point, many classes have been loaded. 998 999 // Update all the fingerprints in the shared methods. 1000 1001 tty->print("Calculating fingerprints ... "); 1002 FingerprintMethodsClosure fpmc; 1003 gch->object_iterate(&fpmc); 1004 tty->print_cr("done. "); 1005 1006 // Remove all references outside the heap. 1007 1008 tty->print("Removing unshareable information ... "); 1009 RemoveUnshareableInfoClosure ruic; 1010 gch->object_iterate(&ruic); 1011 tty->print_cr("done. "); 1012 1013 // Move the objects in three passes. 1014 1015 MarkObjectsOopClosure mark_all; 1016 MarkCommonReadOnly mark_common_ro; 1017 MarkStringValues mark_string_values; 1018 MarkReadWriteObjects mark_rw; 1019 MarkStringObjects mark_strings; 1020 MoveMarkedObjects move_ro(_ro_space, true); 1021 MoveMarkedObjects move_rw(_rw_space, false); 1022 1023 // The SharedOptimizeColdStart VM option governs the new layout 1024 // algorithm for promoting classes into the shared archive. 1025 // The general idea is to minimize cold start time by laying 1026 // out the objects in the order they are accessed at startup time. 1027 // By doing this we are trying to eliminate out-of-order accesses 1028 // in the shared archive. This benefits cold startup time by making 1029 // disk reads as sequential as possible during class loading and 1030 // bootstrapping activities. There may also be a small secondary 1031 // effect of better "packing" of more commonly used data on a smaller 1032 // number of pages, although no direct benefit has been measured from 1033 // this effect. 1034 // 1035 // At the class level of granularity, the promotion order is dictated 1036 // by the classlist file whose generation is discussed elsewhere. 1037 // 1038 // At smaller granularity, optimal ordering was determined by an 1039 // offline analysis of object access order in the shared archive. 1040 // The dbx watchpoint facility, combined with SA post-processing, 1041 // was used to observe common access patterns primarily during 1042 // classloading. This information was used to craft the promotion 1043 // order seen in the following closures. 1044 // 1045 // The observed access order is mostly governed by what happens 1046 // in SystemDictionary::load_shared_class(). NOTE WELL - care 1047 // should be taken when making changes to this method, because it 1048 // may invalidate assumptions made about access order! 1049 // 1050 // (Ideally, there would be a better way to manage changes to 1051 // the access order. Unfortunately a generic in-VM solution for 1052 // dynamically observing access order and optimizing shared 1053 // archive layout is pretty difficult. We go with the static 1054 // analysis because the code is fairly mature at this point 1055 // and we're betting that the access order won't change much.) 1056 1057 MarkAndMoveOrderedReadOnly mark_and_move_ordered_ro(&move_ro); 1058 MarkAndMoveOrderedReadWrite mark_and_move_ordered_rw(&move_rw); 1059 1060 // Set up the share data and shared code segments. 1061 1062 char* md_top = _md_vs->low(); 1063 char* md_end = _md_vs->high(); 1064 char* mc_top = _mc_vs->low(); 1065 char* mc_end = _mc_vs->high(); 1066 1067 // Reserve space for the list of klassOops whose vtables are used 1068 // for patching others as needed. 1069 1070 void** vtbl_list = (void**)md_top; 1071 int vtbl_list_size = CompactingPermGenGen::vtbl_list_size; 1072 Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size); 1073 1074 md_top += vtbl_list_size * sizeof(void*); 1075 void* vtable = md_top; 1076 1077 // Reserve space for a new dummy vtable for klass objects in the 1078 // heap. Generate self-patching vtable entries. 1079 1080 CompactingPermGenGen::generate_vtable_methods(vtbl_list, 1081 &vtable, 1082 &md_top, md_end, 1083 &mc_top, mc_end); 1084 1085 // Reserve space for the total size and the number of stored symbols. 1086 1087 md_top += sizeof(intptr_t) * 2; 1088 1089 MoveSymbols move_symbols(md_top, md_end); 1090 CommonSymbolsClosure traverse_common_symbols(&move_symbols); 1091 1092 // Phase 1a: remove symbols with _refcount == 0 1093 1094 SymbolTable::unlink(); 1095 1096 // Phase 1b: move commonly used symbols referenced by oop fields. 1097 1098 tty->print("Moving common symbols to metadata section at " PTR_FORMAT " ... ", 1099 move_symbols.get_top()); 1100 gch->object_iterate(&traverse_common_symbols); 1101 tty->print_cr("done. "); 1102 1103 // Phase 1c: move known names and signatures. 1104 1105 tty->print("Moving vmSymbols to metadata section at " PTR_FORMAT " ... ", 1106 move_symbols.get_top()); 1107 vmSymbols::symbols_do(&move_symbols); 1108 tty->print_cr("done. "); 1109 1110 // Phase 1d: move the remaining symbols by scanning the whole SymbolTable. 1111 1112 void* extra_symbols = move_symbols.get_top(); 1113 tty->print("Moving the remaining symbols to metadata section at " PTR_FORMAT " ... ", 1114 move_symbols.get_top()); 1115 SymbolTable::symbols_do(&move_symbols); 1116 tty->print_cr("done. "); 1117 1118 // Record the total length of all symbols at the beginning of the block. 1119 ((intptr_t*)md_top)[-2] = move_symbols.get_top() - md_top; 1120 ((intptr_t*)md_top)[-1] = move_symbols.count(); 1121 tty->print_cr("Moved %d symbols, %d bytes.", 1122 move_symbols.count(), move_symbols.get_top() - md_top); 1123 // Advance the pointer to the end of symbol store. 1124 md_top = move_symbols.get_top(); 1125 1126 1127 // Phase 2: move commonly used read-only objects to the read-only space. 1128 1129 if (SharedOptimizeColdStart) { 1130 tty->print("Moving pre-ordered read-only objects to shared space at " PTR_FORMAT " ... ", 1131 _ro_space->top()); 1132 for (int i = 0; i < _class_promote_order->length(); i++) { 1133 oop obj = _class_promote_order->at(i); 1134 mark_and_move_ordered_ro.do_object(obj); 1135 } 1136 tty->print_cr("done. "); 1137 } 1138 1139 tty->print("Moving read-only objects to shared space at " PTR_FORMAT " ... ", 1140 _ro_space->top()); 1141 gch->object_iterate(&mark_common_ro); 1142 gch->object_iterate(&move_ro); 1143 tty->print_cr("done. "); 1144 1145 // Phase 3: move String character arrays to the read-only space. 1146 1147 tty->print("Moving string char arrays to shared space at " PTR_FORMAT " ... ", 1148 _ro_space->top()); 1149 gch->object_iterate(&mark_string_values); 1150 gch->object_iterate(&move_ro); 1151 tty->print_cr("done. "); 1152 1153 // Phase 4: move read-write objects to the read-write space, except 1154 // Strings. 1155 1156 if (SharedOptimizeColdStart) { 1157 tty->print("Moving pre-ordered read-write objects to shared space at " PTR_FORMAT " ... ", 1158 _rw_space->top()); 1159 for (int i = 0; i < _class_promote_order->length(); i++) { 1160 oop obj = _class_promote_order->at(i); 1161 mark_and_move_ordered_rw.do_object(obj); 1162 } 1163 tty->print_cr("done. "); 1164 } 1165 tty->print("Moving read-write objects to shared space at " PTR_FORMAT " ... ", 1166 _rw_space->top()); 1167 Universe::oops_do(&mark_all, true); 1168 SystemDictionary::oops_do(&mark_all); 1169 oop tmp = Universe::arithmetic_exception_instance(); 1170 mark_object(java_lang_Throwable::message(tmp)); 1171 gch->object_iterate(&mark_rw); 1172 gch->object_iterate(&move_rw); 1173 tty->print_cr("done. "); 1174 1175 // Phase 5: move String objects to the read-write space. 1176 1177 tty->print("Moving String objects to shared space at " PTR_FORMAT " ... ", 1178 _rw_space->top()); 1179 StringTable::oops_do(&mark_all); 1180 gch->object_iterate(&mark_strings); 1181 gch->object_iterate(&move_rw); 1182 tty->print_cr("done. "); 1183 tty->print_cr("Read-write space ends at " PTR_FORMAT ", %d bytes.", 1184 _rw_space->top(), _rw_space->used()); 1185 1186 #ifdef DEBUG 1187 // Check: scan for objects which were not moved. 1188 1189 CheckRemainingObjects check_objects; 1190 gch->object_iterate(&check_objects); 1191 check_objects.status(); 1192 #endif 1193 1194 // Resolve forwarding in objects and saved C++ structures 1195 tty->print("Updating references to shared objects ... "); 1196 ResolveForwardingClosure resolve; 1197 Universe::oops_do(&resolve); 1198 SystemDictionary::oops_do(&resolve); 1199 StringTable::oops_do(&resolve); 1200 1201 // Fix (forward) all of the references in these shared objects (which 1202 // are required to point ONLY to objects in the shared spaces). 1203 // Also, create a list of all objects which might later contain a 1204 // reference to a younger generation object. 1205 1206 CompactingPermGenGen* gen = (CompactingPermGenGen*)gch->perm_gen(); 1207 PatchOopsClosure patch(THREAD); 1208 gen->ro_space()->object_iterate(&patch); 1209 gen->rw_space()->object_iterate(&patch); 1210 1211 // Previously method sorting was done concurrently with forwarding 1212 // pointer resolution in the shared spaces. This imposed an ordering 1213 // restriction in that methods were required to be promoted/patched 1214 // before their holder classes. (Because constant pool pointers in 1215 // methodKlasses are required to be resolved before their holder class 1216 // is visited for sorting, otherwise methods are sorted by incorrect, 1217 // pre-forwarding addresses.) 1218 // 1219 // Now, we reorder methods as a separate step after ALL forwarding 1220 // pointer resolution, so that methods can be promoted in any order 1221 // with respect to their holder classes. 1222 1223 SortMethodsClosure sort; 1224 gen->ro_space()->object_iterate(&sort); 1225 gen->rw_space()->object_iterate(&sort); 1226 1227 ReinitializeTables reinit_tables(THREAD); 1228 gen->ro_space()->object_iterate(&reinit_tables); 1229 gen->rw_space()->object_iterate(&reinit_tables); 1230 tty->print_cr("done. "); 1231 tty->cr(); 1232 1233 // Reorder the system dictionary. (Moving the symbols opps affects 1234 // how the hash table indices are calculated.) 1235 1236 SystemDictionary::reorder_dictionary(); 1237 1238 // Empty the non-shared heap (because most of the objects were 1239 // copied out, and the remainder cannot be considered valid oops). 1240 1241 ClearSpaceClosure csc; 1242 for (int i = 0; i < gch->n_gens(); ++i) { 1243 gch->get_gen(i)->space_iterate(&csc); 1244 } 1245 csc.do_space(gen->the_space()); 1246 NOT_PRODUCT(SystemDictionary::verify();) 1247 1248 // Copy the String table, the symbol table, and the system 1249 // dictionary to the shared space in usable form. Copy the hastable 1250 // buckets first [read-write], then copy the linked lists of entries 1251 // [read-only]. 1252 1253 SymbolTable::reverse(extra_symbols); 1254 NOT_PRODUCT(SymbolTable::verify()); 1255 SymbolTable::copy_buckets(&md_top, md_end); 1256 1257 StringTable::reverse(); 1258 NOT_PRODUCT(StringTable::verify()); 1259 StringTable::copy_buckets(&md_top, md_end); 1260 1261 SystemDictionary::reverse(); 1262 SystemDictionary::copy_buckets(&md_top, md_end); 1263 1264 ClassLoader::verify(); 1265 ClassLoader::copy_package_info_buckets(&md_top, md_end); 1266 ClassLoader::verify(); 1267 1268 SymbolTable::copy_table(&md_top, md_end); 1269 StringTable::copy_table(&md_top, md_end); 1270 SystemDictionary::copy_table(&md_top, md_end); 1271 ClassLoader::verify(); 1272 ClassLoader::copy_package_info_table(&md_top, md_end); 1273 ClassLoader::verify(); 1274 1275 // Print debug data. 1276 1277 if (PrintSharedSpaces) { 1278 const char* fmt = "%s space: " PTR_FORMAT " out of " PTR_FORMAT " bytes allocated at " PTR_FORMAT "."; 1279 tty->print_cr(fmt, "ro", _ro_space->used(), _ro_space->capacity(), 1280 _ro_space->bottom()); 1281 tty->print_cr(fmt, "rw", _rw_space->used(), _rw_space->capacity(), 1282 _rw_space->bottom()); 1283 } 1284 1285 // Write the oop data to the output array. 1286 1287 WriteClosure wc(md_top, md_end); 1288 CompactingPermGenGen::serialize_oops(&wc); 1289 md_top = wc.get_top(); 1290 1291 // Update the vtable pointers in all of the Klass objects in the 1292 // heap. They should point to newly generated vtable. 1293 1294 PatchKlassVtables pkvt; 1295 _rw_space->object_iterate(&pkvt); 1296 pkvt.patch(vtbl_list, vtable); 1297 1298 #ifndef PRODUCT 1299 // Update the vtable pointers in all symbols, 1300 // but only in non-product builds where symbols DO have virtual methods. 1301 PatchSymbolVtables psvt(vtbl_list, vtable); 1302 SymbolTable::symbols_do(&psvt); 1303 #endif 1304 1305 char* saved_vtbl = (char*)malloc(vtbl_list_size * sizeof(void*)); 1306 memmove(saved_vtbl, vtbl_list, vtbl_list_size * sizeof(void*)); 1307 memset(vtbl_list, 0, vtbl_list_size * sizeof(void*)); 1308 1309 // Create and write the archive file that maps the shared spaces. 1310 1311 FileMapInfo* mapinfo = new FileMapInfo(); 1312 mapinfo->populate_header(gch->gen_policy()->max_alignment()); 1313 1314 // Pass 1 - update file offsets in header. 1315 mapinfo->write_header(); 1316 mapinfo->write_space(CompactingPermGenGen::ro, _ro_space, true); 1317 _ro_space->set_saved_mark(); 1318 mapinfo->write_space(CompactingPermGenGen::rw, _rw_space, false); 1319 _rw_space->set_saved_mark(); 1320 mapinfo->write_region(CompactingPermGenGen::md, _md_vs->low(), 1321 pointer_delta(md_top, _md_vs->low(), sizeof(char)), 1322 SharedMiscDataSize, 1323 false, false); 1324 mapinfo->write_region(CompactingPermGenGen::mc, _mc_vs->low(), 1325 pointer_delta(mc_top, _mc_vs->low(), sizeof(char)), 1326 SharedMiscCodeSize, 1327 true, true); 1328 1329 // Pass 2 - write data. 1330 mapinfo->open_for_write(); 1331 mapinfo->write_header(); 1332 mapinfo->write_space(CompactingPermGenGen::ro, _ro_space, true); 1333 mapinfo->write_space(CompactingPermGenGen::rw, _rw_space, false); 1334 mapinfo->write_region(CompactingPermGenGen::md, _md_vs->low(), 1335 pointer_delta(md_top, _md_vs->low(), sizeof(char)), 1336 SharedMiscDataSize, 1337 false, false); 1338 mapinfo->write_region(CompactingPermGenGen::mc, _mc_vs->low(), 1339 pointer_delta(mc_top, _mc_vs->low(), sizeof(char)), 1340 SharedMiscCodeSize, 1341 true, true); 1342 mapinfo->close(); 1343 1344 // Summarize heap. 1345 memmove(vtbl_list, saved_vtbl, vtbl_list_size * sizeof(void*)); 1346 print_contents(); 1347 } 1348 }; // class VM_PopulateDumpSharedSpace 1349 1350 1351 // Populate the shared spaces and dump to a file. 1352 1353 jint CompactingPermGenGen::dump_shared(GrowableArray<oop>* class_promote_order, TRAPS) { 1354 GenCollectedHeap* gch = GenCollectedHeap::heap(); 1355 1356 // Calculate hash values for all of the (interned) strings to avoid 1357 // writes to shared pages in the future. 1358 1359 tty->print("Calculating hash values for String objects .. "); 1360 StringHashCodeClosure shcc(THREAD); 1361 StringTable::oops_do(&shcc); 1362 tty->print_cr("done. "); 1363 1364 CompactingPermGenGen* gen = (CompactingPermGenGen*)gch->perm_gen(); 1365 VM_PopulateDumpSharedSpace op(class_promote_order, 1366 gen->ro_space(), gen->rw_space(), 1367 gen->md_space(), gen->mc_space()); 1368 VMThread::execute(&op); 1369 return JNI_OK; 1370 } 1371 1372 void* CompactingPermGenGen::find_matching_vtbl_ptr(void** vtbl_list, 1373 void* new_vtable_start, 1374 void* obj) { 1375 void* old_vtbl_ptr = *(void**)obj; 1376 for (int i = 0; i < vtbl_list_size; i++) { 1377 if (vtbl_list[i] == old_vtbl_ptr) { 1378 return (void**)new_vtable_start + i * num_virtuals; 1379 } 1380 } 1381 ShouldNotReachHere(); 1382 return NULL; 1383 } 1384 1385 1386 class LinkClassesClosure : public ObjectClosure { 1387 private: 1388 Thread* THREAD; 1389 1390 public: 1391 LinkClassesClosure(Thread* thread) : THREAD(thread) {} 1392 1393 void do_object(oop obj) { 1394 if (obj->is_klass()) { 1395 Klass* k = Klass::cast((klassOop) obj); 1396 if (k->oop_is_instance()) { 1397 instanceKlass* ik = (instanceKlass*) k; 1398 // Link the class to cause the bytecodes to be rewritten and the 1399 // cpcache to be created. 1400 if (ik->init_state() < instanceKlass::linked) { 1401 ik->link_class(THREAD); 1402 guarantee(!HAS_PENDING_EXCEPTION, "exception in class rewriting"); 1403 } 1404 1405 // Create String objects from string initializer symbols. 1406 ik->constants()->resolve_string_constants(THREAD); 1407 guarantee(!HAS_PENDING_EXCEPTION, "exception resolving string constants"); 1408 } 1409 } 1410 } 1411 }; 1412 1413 1414 // Support for a simple checksum of the contents of the class list 1415 // file to prevent trivial tampering. The algorithm matches that in 1416 // the MakeClassList program used by the J2SE build process. 1417 #define JSUM_SEED ((jlong)CONST64(0xcafebabebabecafe)) 1418 static jlong 1419 jsum(jlong start, const char *buf, const int len) 1420 { 1421 jlong h = start; 1422 char *p = (char *)buf, *e = p + len; 1423 while (p < e) { 1424 char c = *p++; 1425 if (c <= ' ') { 1426 /* Skip spaces and control characters */ 1427 continue; 1428 } 1429 h = 31 * h + c; 1430 } 1431 return h; 1432 } 1433 1434 1435 1436 1437 1438 // Preload classes from a list, populate the shared spaces and dump to a 1439 // file. 1440 1441 void GenCollectedHeap::preload_and_dump(TRAPS) { 1442 TraceTime timer("Dump Shared Spaces", TraceStartupTime); 1443 ResourceMark rm; 1444 1445 // Preload classes to be shared. 1446 // Should use some os:: method rather than fopen() here. aB. 1447 // Construct the path to the class list (in jre/lib) 1448 // Walk up two directories from the location of the VM and 1449 // optionally tack on "lib" (depending on platform) 1450 char class_list_path[JVM_MAXPATHLEN]; 1451 os::jvm_path(class_list_path, sizeof(class_list_path)); 1452 for (int i = 0; i < 3; i++) { 1453 char *end = strrchr(class_list_path, *os::file_separator()); 1454 if (end != NULL) *end = '\0'; 1455 } 1456 int class_list_path_len = (int)strlen(class_list_path); 1457 if (class_list_path_len >= 3) { 1458 if (strcmp(class_list_path + class_list_path_len - 3, "lib") != 0) { 1459 strcat(class_list_path, os::file_separator()); 1460 strcat(class_list_path, "lib"); 1461 } 1462 } 1463 strcat(class_list_path, os::file_separator()); 1464 strcat(class_list_path, "classlist"); 1465 1466 FILE* file = fopen(class_list_path, "r"); 1467 if (file != NULL) { 1468 jlong computed_jsum = JSUM_SEED; 1469 jlong file_jsum = 0; 1470 1471 char class_name[256]; 1472 int class_count = 0; 1473 GenCollectedHeap* gch = GenCollectedHeap::heap(); 1474 gch->_preloading_shared_classes = true; 1475 GrowableArray<oop>* class_promote_order = new GrowableArray<oop>(); 1476 1477 // Preload (and intern) strings which will be used later. 1478 1479 StringTable::intern("main", THREAD); 1480 StringTable::intern("([Ljava/lang/String;)V", THREAD); 1481 StringTable::intern("Ljava/lang/Class;", THREAD); 1482 1483 StringTable::intern("I", THREAD); // Needed for StringBuffer persistence? 1484 StringTable::intern("Z", THREAD); // Needed for StringBuffer persistence? 1485 1486 // sun.io.Converters 1487 static const char obj_array_sig[] = "[[Ljava/lang/Object;"; 1488 (void)SymbolTable::new_permanent_symbol(obj_array_sig, THREAD); 1489 1490 // java.util.HashMap 1491 static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;"; 1492 (void)SymbolTable::new_permanent_symbol(map_entry_array_sig, THREAD); 1493 1494 tty->print("Loading classes to share ... "); 1495 while ((fgets(class_name, sizeof class_name, file)) != NULL) { 1496 if (*class_name == '#') { 1497 jint fsh, fsl; 1498 if (sscanf(class_name, "# %8x%8x\n", &fsh, &fsl) == 2) { 1499 file_jsum = ((jlong)(fsh) << 32) | (fsl & 0xffffffff); 1500 } 1501 1502 continue; 1503 } 1504 // Remove trailing newline 1505 size_t name_len = strlen(class_name); 1506 class_name[name_len-1] = '\0'; 1507 1508 computed_jsum = jsum(computed_jsum, class_name, (const int)name_len - 1); 1509 1510 // Got a class name - load it. 1511 Symbol* class_name_symbol = SymbolTable::new_permanent_symbol(class_name, THREAD); 1512 guarantee(!HAS_PENDING_EXCEPTION, "Exception creating a symbol."); 1513 klassOop klass = SystemDictionary::resolve_or_null(class_name_symbol, 1514 THREAD); 1515 guarantee(!HAS_PENDING_EXCEPTION, "Exception resolving a class."); 1516 if (klass != NULL) { 1517 if (PrintSharedSpaces) { 1518 tty->print_cr("Shared spaces preloaded: %s", class_name); 1519 } 1520 1521 1522 instanceKlass* ik = instanceKlass::cast(klass); 1523 1524 // Should be class load order as per -XX:+TraceClassLoadingPreorder 1525 class_promote_order->append(ik->as_klassOop()); 1526 1527 // Link the class to cause the bytecodes to be rewritten and the 1528 // cpcache to be created. The linking is done as soon as classes 1529 // are loaded in order that the related data structures (klass, 1530 // cpCache, Sting constants) are located together. 1531 1532 if (ik->init_state() < instanceKlass::linked) { 1533 ik->link_class(THREAD); 1534 guarantee(!(HAS_PENDING_EXCEPTION), "exception in class rewriting"); 1535 } 1536 1537 // Create String objects from string initializer symbols. 1538 1539 ik->constants()->resolve_string_constants(THREAD); 1540 1541 class_count++; 1542 } else { 1543 if (PrintSharedSpaces) { 1544 tty->cr(); 1545 tty->print_cr(" Preload failed: %s", class_name); 1546 } 1547 } 1548 file_jsum = 0; // Checksum must be on last line of file 1549 } 1550 if (computed_jsum != file_jsum) { 1551 tty->cr(); 1552 tty->print_cr("Preload failed: checksum of class list was incorrect."); 1553 exit(1); 1554 } 1555 1556 tty->print_cr("done. "); 1557 1558 if (PrintSharedSpaces) { 1559 tty->print_cr("Shared spaces: preloaded %d classes", class_count); 1560 } 1561 1562 // Rewrite and unlink classes. 1563 tty->print("Rewriting and unlinking classes ... "); 1564 // Make heap parsable 1565 ensure_parsability(false); // arg is actually don't care 1566 1567 // Link any classes which got missed. (It's not quite clear why 1568 // they got missed.) This iteration would be unsafe if we weren't 1569 // single-threaded at this point; however we can't do it on the VM 1570 // thread because it requires object allocation. 1571 LinkClassesClosure lcc(Thread::current()); 1572 object_iterate(&lcc); 1573 ensure_parsability(false); // arg is actually don't care 1574 tty->print_cr("done. "); 1575 1576 // Create and dump the shared spaces. 1577 jint err = CompactingPermGenGen::dump_shared(class_promote_order, THREAD); 1578 if (err != JNI_OK) { 1579 fatal("Dumping shared spaces failed."); 1580 } 1581 1582 } else { 1583 char errmsg[JVM_MAXPATHLEN]; 1584 os::lasterror(errmsg, JVM_MAXPATHLEN); 1585 tty->print_cr("Loading classlist failed: %s", errmsg); 1586 exit(1); 1587 } 1588 1589 // Since various initialization steps have been undone by this process, 1590 // it is not reasonable to continue running a java process. 1591 exit(0); 1592 }