src/share/vm/memory/dump.cpp

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 435
a61af66fc99e
child 548
ba764ed4b6f2
permissions
-rw-r--r--

Initial load

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

mercurial