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