1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/dictionary.hpp"
  27 #include "classfile/loaderConstraints.hpp"
  28 #include "classfile/placeholders.hpp"
  29 #include "classfile/symbolTable.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "code/codeCache.hpp"
  32 #include "memory/filemap.hpp"
  33 #include "memory/gcLocker.hpp"
  34 #include "memory/metaspace.hpp"
  35 #include "memory/metaspaceShared.hpp"
  36 #include "oops/objArrayOop.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "runtime/signature.hpp"
  39 #include "runtime/vm_operations.hpp"
  40 #include "runtime/vmThread.hpp"
  41 #include "utilities/hashtable.inline.hpp"
  42 
  43 
  44 int MetaspaceShared::_max_alignment = 0;
  45 
  46 ReservedSpace* MetaspaceShared::_shared_rs = NULL;
  47 
  48 // Read/write a data stream for restoring/preserving metadata pointers and
  49 // miscellaneous data from/to the shared archive file.
  50 
  51 void MetaspaceShared::serialize(SerializeClosure* soc) {
  52   int tag = 0;
  53   soc->do_tag(--tag);
  54 
  55   // Verify the sizes of various metadata in the system.
  56   soc->do_tag(sizeof(Method));
  57   soc->do_tag(sizeof(ConstMethod));
  58   soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE));
  59   soc->do_tag(sizeof(ConstantPool));
  60   soc->do_tag(sizeof(ConstantPoolCache));
  61   soc->do_tag(objArrayOopDesc::base_offset_in_bytes());
  62   soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE));
  63   soc->do_tag(sizeof(Symbol));
  64 
  65   // Dump/restore miscellaneous metadata.
  66   Universe::serialize(soc, true);
  67   soc->do_tag(--tag);
  68 
  69   // Dump/restore references to commonly used names and signatures.
  70   vmSymbols::serialize(soc);
  71   soc->do_tag(--tag);
  72 
  73   soc->do_tag(666);
  74 }
  75 
  76 
  77 // CDS code for dumping shared archive.
  78 
  79 // Global object for holding classes that have been loaded.  Since this
  80 // is run at a safepoint just before exit, this is the entire set of classes.
  81 static GrowableArray<Klass*>* _global_klass_objects;
  82 static void collect_classes(Klass* k) {
  83   _global_klass_objects->append_if_missing(k);
  84   if (k->oop_is_instance()) {
  85     // Add in the array classes too
  86     InstanceKlass* ik = InstanceKlass::cast(k);
  87     ik->array_klasses_do(collect_classes);
  88   }
  89 }
  90 
  91 static void remove_unshareable_in_classes() {
  92   for (int i = 0; i < _global_klass_objects->length(); i++) {
  93     Klass* k = _global_klass_objects->at(i);
  94     k->remove_unshareable_info();
  95   }
  96 }
  97 
  98 // Walk all methods in the class list and assign a fingerprint.
  99 // so that this part of the ConstMethod* is read only.
 100 static void calculate_fingerprints() {
 101   for (int i = 0; i < _global_klass_objects->length(); i++) {
 102     Klass* k = _global_klass_objects->at(i);
 103     if (k->oop_is_instance()) {
 104       InstanceKlass* ik = InstanceKlass::cast(k);
 105       for (int i = 0; i < ik->methods()->length(); i++) {
 106         Method* m = ik->methods()->at(i);
 107         Fingerprinter fp(m);
 108         // The side effect of this call sets method's fingerprint field.
 109         fp.fingerprint();
 110       }
 111     }
 112   }
 113 }
 114 
 115 // Patch C++ vtable pointer in metadata.
 116 
 117 // Klass and other metadata objects contain references to c++ vtables in the
 118 // JVM library.
 119 // Fix them to point to our constructed vtables.  However, don't iterate
 120 // across the space while doing this, as that causes the vtables to be
 121 // patched, undoing our useful work.  Instead, iterate to make a list,
 122 // then use the list to do the fixing.
 123 //
 124 // Our constructed vtables:
 125 // Dump time:
 126 //  1. init_self_patching_vtbl_list: table of pointers to current virtual method addrs
 127 //  2. generate_vtable_methods: create jump table, appended to above vtbl_list
 128 //  3. patch_klass_vtables: for Klass list, patch the vtable entry in klass and
 129 //     associated metadata to point to jump table rather than to current vtbl
 130 // Table layout: NOTE FIXED SIZE
 131 //   1. vtbl pointers
 132 //   2. #Klass X #virtual methods per Klass
 133 //   1 entry for each, in the order:
 134 //   Klass1:method1 entry, Klass1:method2 entry, ... Klass1:method<num_virtuals> entry
 135 //   Klass2:method1 entry, Klass2:method2 entry, ... Klass2:method<num_virtuals> entry
 136 //   ...
 137 //   Klass<vtbl_list_size>:method1 entry, Klass<vtbl_list_size>:method2 entry,
 138 //       ... Klass<vtbl_list_size>:method<num_virtuals> entry
 139 //  Sample entry: (Sparc):
 140 //   save(sp, -256, sp)
 141 //   ba,pt common_code
 142 //   mov XXX, %L0       %L0 gets: Klass index <<8 + method index (note: max method index 255)
 143 //
 144 // Restore time:
 145 //   1. initialize_shared_space: reserve space for table
 146 //   2. init_self_patching_vtbl_list: update pointers to NEW virtual method addrs in text
 147 //
 148 // Execution time:
 149 //   First virtual method call for any object of these metadata types:
 150 //   1. object->klass
 151 //   2. vtable entry for that klass points to the jump table entries
 152 //   3. branches to common_code with %O0/klass, %L0: Klass index <<8 + method index
 153 //   4. common_code:
 154 //      Get address of new vtbl pointer for this Klass from updated table
 155 //      Update new vtbl pointer in the Klass: future virtual calls go direct
 156 //      Jump to method, using new vtbl pointer and method index
 157 
 158 
 159 static void* find_matching_vtbl_ptr(void** vtbl_list, void* new_vtable_start, void* obj) {
 160   void* old_vtbl_ptr = *(void**)obj;
 161   for (int i = 0; i < MetaspaceShared::vtbl_list_size; i++) {
 162     if (vtbl_list[i] == old_vtbl_ptr) {
 163       return (void**)new_vtable_start + i * MetaspaceShared::num_virtuals;
 164     }
 165   }
 166   ShouldNotReachHere();
 167   return NULL;
 168 }
 169 
 170 // Assumes the vtable is in first slot in object.
 171 static void patch_klass_vtables(void** vtbl_list, void* new_vtable_start) {
 172   int n = _global_klass_objects->length();
 173   for (int i = 0; i < n; i++) {
 174     Klass* obj = _global_klass_objects->at(i);
 175     // Note oop_is_instance() is a virtual call.  After patching vtables
 176     // all virtual calls on the dummy vtables will restore the original!
 177     if (obj->oop_is_instance()) {
 178       InstanceKlass* ik = InstanceKlass::cast(obj);
 179       *(void**)ik = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, ik);
 180       ConstantPool* cp = ik->constants();
 181       *(void**)cp = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, cp);
 182       for (int j = 0; j < ik->methods()->length(); j++) {
 183         Method* m = ik->methods()->at(j);
 184         *(void**)m = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, m);
 185       }
 186     } else {
 187       // Array klasses
 188       Klass* k = obj;
 189       *(void**)k = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, k);
 190     }
 191   }
 192 }
 193 
 194 // Closure for serializing initialization data out to a data area to be
 195 // written to the shared file.
 196 
 197 class WriteClosure : public SerializeClosure {
 198 private:
 199   intptr_t* top;
 200   char* end;
 201 
 202   inline void check_space() {
 203     if ((char*)top + sizeof(intptr_t) > end) {
 204       report_out_of_shared_space(SharedMiscData);
 205     }
 206   }
 207 
 208 public:
 209   WriteClosure(char* md_top, char* md_end) {
 210     top = (intptr_t*)md_top;
 211     end = md_end;
 212   }
 213 
 214   char* get_top() { return (char*)top; }
 215 
 216   void do_ptr(void** p) {
 217     check_space();
 218     *top = (intptr_t)*p;
 219     ++top;
 220   }
 221 
 222   void do_tag(int tag) {
 223     check_space();
 224     *top = (intptr_t)tag;
 225     ++top;
 226   }
 227 
 228   void do_region(u_char* start, size_t size) {
 229     if ((char*)top + size > end) {
 230       report_out_of_shared_space(SharedMiscData);
 231     }
 232     assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
 233     assert(size % sizeof(intptr_t) == 0, "bad size");
 234     do_tag((int)size);
 235     while (size > 0) {
 236       *top = *(intptr_t*)start;
 237       ++top;
 238       start += sizeof(intptr_t);
 239       size -= sizeof(intptr_t);
 240     }
 241   }
 242 
 243   bool reading() const { return false; }
 244 };
 245 
 246 // This is for dumping detailed statistics for the allocations
 247 // in the shared spaces.
 248 class DumpAllocClosure : public Metaspace::AllocRecordClosure {
 249 public:
 250 
 251   // Here's poor man's enum inheritance
 252 #define SHAREDSPACE_OBJ_TYPES_DO(f) \
 253   METASPACE_OBJ_TYPES_DO(f) \
 254   f(SymbolHashentry) \
 255   f(SymbolBuckets) \
 256   f(Other)
 257 
 258 #define SHAREDSPACE_OBJ_TYPE_DECLARE(name) name ## Type,
 259 #define SHAREDSPACE_OBJ_TYPE_NAME_CASE(name) case name ## Type: return #name;
 260 
 261   enum Type {
 262     // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc
 263     SHAREDSPACE_OBJ_TYPES_DO(SHAREDSPACE_OBJ_TYPE_DECLARE)
 264     _number_of_types
 265   };
 266 
 267   static const char * type_name(Type type) {
 268     switch(type) {
 269     SHAREDSPACE_OBJ_TYPES_DO(SHAREDSPACE_OBJ_TYPE_NAME_CASE)
 270     default:
 271       ShouldNotReachHere();
 272       return NULL;
 273     }
 274   }
 275 
 276 public:
 277   enum {
 278     RO = 0,
 279     RW = 1
 280   };
 281 
 282   int _counts[2][_number_of_types];
 283   int _bytes [2][_number_of_types];
 284   int _which;
 285 
 286   DumpAllocClosure() {
 287     memset(_counts, 0, sizeof(_counts));
 288     memset(_bytes,  0, sizeof(_bytes));
 289   };
 290 
 291   void iterate_metaspace(Metaspace* space, int which) {
 292     assert(which == RO || which == RW, "sanity");
 293     _which = which;
 294     space->iterate(this);
 295   }
 296 
 297   virtual void doit(address ptr, MetaspaceObj::Type type, int byte_size) {
 298     assert(int(type) >= 0 && type < MetaspaceObj::_number_of_types, "sanity");
 299     _counts[_which][type] ++;
 300     _bytes [_which][type] += byte_size;
 301   }
 302 
 303   void dump_stats(int ro_all, int rw_all, int md_all, int mc_all);
 304 };
 305 
 306 void DumpAllocClosure::dump_stats(int ro_all, int rw_all, int md_all, int mc_all) {
 307   rw_all += (md_all + mc_all); // md and mc are all mapped Read/Write
 308   int other_bytes = md_all + mc_all;
 309 
 310   // Calculate size of data that was not allocated by Metaspace::allocate()
 311   int symbol_count = _counts[RO][MetaspaceObj::SymbolType];
 312   int symhash_bytes = symbol_count * sizeof (HashtableEntry<Symbol*, mtSymbol>);
 313   int symbuck_count = SymbolTable::the_table()->table_size();
 314   int symbuck_bytes = symbuck_count * sizeof(HashtableBucket<mtSymbol>);
 315 
 316   _counts[RW][SymbolHashentryType] = symbol_count;
 317   _bytes [RW][SymbolHashentryType] = symhash_bytes;
 318   other_bytes -= symhash_bytes;
 319 
 320   _counts[RW][SymbolBucketsType] = symbuck_count;
 321   _bytes [RW][SymbolBucketsType] = symbuck_bytes;
 322   other_bytes -= symbuck_bytes;
 323 
 324   // TODO: count things like dictionary, vtable, etc
 325   _bytes[RW][OtherType] =  other_bytes;
 326 
 327   // prevent divide-by-zero
 328   if (ro_all < 1) {
 329     ro_all = 1;
 330   }
 331   if (rw_all < 1) {
 332     rw_all = 1;
 333   }
 334 
 335   int all_ro_count = 0;
 336   int all_ro_bytes = 0;
 337   int all_rw_count = 0;
 338   int all_rw_bytes = 0;
 339 
 340   const char *fmt = "%-20s: %8d %10d %5.1f | %8d %10d %5.1f | %8d %10d %5.1f";
 341   const char *sep = "--------------------+---------------------------+---------------------------+--------------------------";
 342   const char *hdr = "                        ro_cnt   ro_bytes     % |   rw_cnt   rw_bytes     % |  all_cnt  all_bytes     %";
 343 
 344   tty->print_cr("Detailed metadata info (rw includes md and mc):");
 345   tty->print_cr(hdr);
 346   tty->print_cr(sep);
 347   for (int type = 0; type < int(_number_of_types); type ++) {
 348     const char *name = type_name((Type)type);
 349     int ro_count = _counts[RO][type];
 350     int ro_bytes = _bytes [RO][type];
 351     int rw_count = _counts[RW][type];
 352     int rw_bytes = _bytes [RW][type];
 353     int count = ro_count + rw_count;
 354     int bytes = ro_bytes + rw_bytes;
 355 
 356     double ro_perc = 100.0 * double(ro_bytes) / double(ro_all);
 357     double rw_perc = 100.0 * double(rw_bytes) / double(rw_all);
 358     double perc    = 100.0 * double(bytes)    / double(ro_all + rw_all);
 359 
 360     tty->print_cr(fmt, name,
 361                   ro_count, ro_bytes, ro_perc,
 362                   rw_count, rw_bytes, rw_perc,
 363                   count, bytes, perc);
 364 
 365     all_ro_count += ro_count;
 366     all_ro_bytes += ro_bytes;
 367     all_rw_count += rw_count;
 368     all_rw_bytes += rw_bytes;
 369   }
 370 
 371   int all_count = all_ro_count + all_rw_count;
 372   int all_bytes = all_ro_bytes + all_rw_bytes;
 373 
 374   double all_ro_perc = 100.0 * double(all_ro_bytes) / double(ro_all);
 375   double all_rw_perc = 100.0 * double(all_rw_bytes) / double(rw_all);
 376   double all_perc    = 100.0 * double(all_bytes)    / double(ro_all + rw_all);
 377 
 378   tty->print_cr(sep);
 379   tty->print_cr(fmt, "Total",
 380                 all_ro_count, all_ro_bytes, all_ro_perc,
 381                 all_rw_count, all_rw_bytes, all_rw_perc,
 382                 all_count, all_bytes, all_perc);
 383 
 384   assert(all_ro_bytes == ro_all, "everything should have been counted");
 385   assert(all_rw_bytes == rw_all, "everything should have been counted");
 386 }
 387 
 388 // Populate the shared space.
 389 
 390 class VM_PopulateDumpSharedSpace: public VM_Operation {
 391 private:
 392   ClassLoaderData* _loader_data;
 393   GrowableArray<Klass*> *_class_promote_order;
 394   VirtualSpace _md_vs;
 395   VirtualSpace _mc_vs;
 396 
 397 public:
 398   VM_PopulateDumpSharedSpace(ClassLoaderData* loader_data,
 399                              GrowableArray<Klass*> *class_promote_order) :
 400     _loader_data(loader_data) {
 401 
 402     // Split up and initialize the misc code and data spaces
 403     ReservedSpace* shared_rs = MetaspaceShared::shared_rs();
 404     int metadata_size = SharedReadOnlySize+SharedReadWriteSize;
 405     ReservedSpace shared_ro_rw = shared_rs->first_part(metadata_size);
 406     ReservedSpace misc_section = shared_rs->last_part(metadata_size);
 407 
 408     // Now split into misc sections.
 409     ReservedSpace md_rs   = misc_section.first_part(SharedMiscDataSize);
 410     ReservedSpace mc_rs   = misc_section.last_part(SharedMiscDataSize);
 411     _md_vs.initialize(md_rs, SharedMiscDataSize);
 412     _mc_vs.initialize(mc_rs, SharedMiscCodeSize);
 413     _class_promote_order = class_promote_order;
 414   }
 415 
 416   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
 417   void doit();   // outline because gdb sucks
 418 }; // class VM_PopulateDumpSharedSpace
 419 
 420 
 421 void VM_PopulateDumpSharedSpace::doit() {
 422   Thread* THREAD = VMThread::vm_thread();
 423   NOT_PRODUCT(SystemDictionary::verify();)
 424   // The following guarantee is meant to ensure that no loader constraints
 425   // exist yet, since the constraints table is not shared.  This becomes
 426   // more important now that we don't re-initialize vtables/itables for
 427   // shared classes at runtime, where constraints were previously created.
 428   guarantee(SystemDictionary::constraints()->number_of_entries() == 0,
 429             "loader constraints are not saved");
 430   guarantee(SystemDictionary::placeholders()->number_of_entries() == 0,
 431           "placeholders are not saved");
 432   // Revisit and implement this if we prelink method handle call sites:
 433   guarantee(SystemDictionary::invoke_method_table() == NULL ||
 434             SystemDictionary::invoke_method_table()->number_of_entries() == 0,
 435             "invoke method table is not saved");
 436 
 437   // At this point, many classes have been loaded.
 438   // Gather systemDictionary classes in a global array and do everything to
 439   // that so we don't have to walk the SystemDictionary again.
 440   _global_klass_objects = new GrowableArray<Klass*>(1000);
 441   Universe::basic_type_classes_do(collect_classes);
 442   SystemDictionary::classes_do(collect_classes);
 443 
 444   tty->print_cr("Number of classes %d", _global_klass_objects->length());
 445 
 446   // Update all the fingerprints in the shared methods.
 447   tty->print("Calculating fingerprints ... ");
 448   calculate_fingerprints();
 449   tty->print_cr("done. ");
 450 
 451   // Remove all references outside the metadata
 452   tty->print("Removing unshareable information ... ");
 453   remove_unshareable_in_classes();
 454   tty->print_cr("done. ");
 455 
 456   // Set up the share data and shared code segments.
 457   char* md_low = _md_vs.low();
 458   char* md_top = md_low;
 459   char* md_end = _md_vs.high();
 460   char* mc_low = _mc_vs.low();
 461   char* mc_top = mc_low;
 462   char* mc_end = _mc_vs.high();
 463 
 464   // Reserve space for the list of Klass*s whose vtables are used
 465   // for patching others as needed.
 466 
 467   void** vtbl_list = (void**)md_top;
 468   int vtbl_list_size = MetaspaceShared::vtbl_list_size;
 469   Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size);
 470 
 471   md_top += vtbl_list_size * sizeof(void*);
 472   void* vtable = md_top;
 473 
 474   // Reserve space for a new dummy vtable for klass objects in the
 475   // heap.  Generate self-patching vtable entries.
 476 
 477   MetaspaceShared::generate_vtable_methods(vtbl_list, &vtable,
 478                                      &md_top, md_end,
 479                                      &mc_top, mc_end);
 480 
 481   // Reorder the system dictionary.  (Moving the symbols affects
 482   // how the hash table indices are calculated.)
 483   // Not doing this either.
 484 
 485   SystemDictionary::reorder_dictionary();
 486 
 487   NOT_PRODUCT(SystemDictionary::verify();)
 488 
 489   // Copy the the symbol table, and the system dictionary to the shared
 490   // space in usable form.  Copy the hashtable
 491   // buckets first [read-write], then copy the linked lists of entries
 492   // [read-only].
 493 
 494   SymbolTable::reverse(md_top);
 495   NOT_PRODUCT(SymbolTable::verify());
 496   SymbolTable::copy_buckets(&md_top, md_end);
 497 
 498   SystemDictionary::reverse();
 499   SystemDictionary::copy_buckets(&md_top, md_end);
 500 
 501   ClassLoader::verify();
 502   ClassLoader::copy_package_info_buckets(&md_top, md_end);
 503   ClassLoader::verify();
 504 
 505   SymbolTable::copy_table(&md_top, md_end);
 506   SystemDictionary::copy_table(&md_top, md_end);
 507   ClassLoader::verify();
 508   ClassLoader::copy_package_info_table(&md_top, md_end);
 509   ClassLoader::verify();
 510 
 511   // Write the other data to the output array.
 512   WriteClosure wc(md_top, md_end);
 513   MetaspaceShared::serialize(&wc);
 514   md_top = wc.get_top();
 515 
 516   // Print shared spaces all the time
 517   const char* fmt = "%s space: %9d [ %4.1f%% of total] out of %9d bytes [%4.1f%% used] at " PTR_FORMAT;
 518   Metaspace* ro_space = _loader_data->ro_metaspace();
 519   Metaspace* rw_space = _loader_data->rw_metaspace();
 520 
 521   // Allocated size of each space (may not be all occupied)
 522   const size_t ro_alloced = ro_space->capacity_bytes_slow(Metaspace::NonClassType);
 523   const size_t rw_alloced = rw_space->capacity_bytes_slow(Metaspace::NonClassType);
 524   const size_t md_alloced = md_end-md_low;
 525   const size_t mc_alloced = mc_end-mc_low;
 526   const size_t total_alloced = ro_alloced + rw_alloced + md_alloced + mc_alloced;
 527 
 528   // Occupied size of each space.
 529   const size_t ro_bytes = ro_space->used_bytes_slow(Metaspace::NonClassType);
 530   const size_t rw_bytes = rw_space->used_bytes_slow(Metaspace::NonClassType);
 531   const size_t md_bytes = size_t(md_top - md_low);
 532   const size_t mc_bytes = size_t(mc_top - mc_low);
 533 
 534   // Percent of total size
 535   const size_t total_bytes = ro_bytes + rw_bytes + md_bytes + mc_bytes;
 536   const double ro_t_perc = ro_bytes / double(total_bytes) * 100.0;
 537   const double rw_t_perc = rw_bytes / double(total_bytes) * 100.0;
 538   const double md_t_perc = md_bytes / double(total_bytes) * 100.0;
 539   const double mc_t_perc = mc_bytes / double(total_bytes) * 100.0;
 540 
 541   // Percent of fullness of each space
 542   const double ro_u_perc = ro_bytes / double(ro_alloced) * 100.0;
 543   const double rw_u_perc = rw_bytes / double(rw_alloced) * 100.0;
 544   const double md_u_perc = md_bytes / double(md_alloced) * 100.0;
 545   const double mc_u_perc = mc_bytes / double(mc_alloced) * 100.0;
 546   const double total_u_perc = total_bytes / double(total_alloced) * 100.0;
 547 
 548   tty->print_cr(fmt, "ro", ro_bytes, ro_t_perc, ro_alloced, ro_u_perc, ro_space->bottom());
 549   tty->print_cr(fmt, "rw", rw_bytes, rw_t_perc, rw_alloced, rw_u_perc, rw_space->bottom());
 550   tty->print_cr(fmt, "md", md_bytes, md_t_perc, md_alloced, md_u_perc, md_low);
 551   tty->print_cr(fmt, "mc", mc_bytes, mc_t_perc, mc_alloced, mc_u_perc, mc_low);
 552   tty->print_cr("total   : %9d [100.0%% of total] out of %9d bytes [%4.1f%% used]",
 553                  total_bytes, total_alloced, total_u_perc);
 554 
 555   // Update the vtable pointers in all of the Klass objects in the
 556   // heap. They should point to newly generated vtable.
 557   patch_klass_vtables(vtbl_list, vtable);
 558 
 559   // dunno what this is for.
 560   char* saved_vtbl = (char*)os::malloc(vtbl_list_size * sizeof(void*), mtClass);
 561   memmove(saved_vtbl, vtbl_list, vtbl_list_size * sizeof(void*));
 562   memset(vtbl_list, 0, vtbl_list_size * sizeof(void*));
 563 
 564   // Create and write the archive file that maps the shared spaces.
 565 
 566   FileMapInfo* mapinfo = new FileMapInfo();
 567   mapinfo->populate_header(MetaspaceShared::max_alignment());
 568 
 569   // Pass 1 - update file offsets in header.
 570   mapinfo->write_header();
 571   mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true);
 572   mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false);
 573   mapinfo->write_region(MetaspaceShared::md, _md_vs.low(),
 574                         pointer_delta(md_top, _md_vs.low(), sizeof(char)),
 575                         SharedMiscDataSize,
 576                         false, false);
 577   mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(),
 578                         pointer_delta(mc_top, _mc_vs.low(), sizeof(char)),
 579                         SharedMiscCodeSize,
 580                         true, true);
 581 
 582   // Pass 2 - write data.
 583   mapinfo->open_for_write();
 584   mapinfo->write_header();
 585   mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true);
 586   mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false);
 587   mapinfo->write_region(MetaspaceShared::md, _md_vs.low(),
 588                         pointer_delta(md_top, _md_vs.low(), sizeof(char)),
 589                         SharedMiscDataSize,
 590                         false, false);
 591   mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(),
 592                         pointer_delta(mc_top, _mc_vs.low(), sizeof(char)),
 593                         SharedMiscCodeSize,
 594                         true, true);
 595   mapinfo->close();
 596 
 597   memmove(vtbl_list, saved_vtbl, vtbl_list_size * sizeof(void*));
 598 
 599   if (PrintSharedSpaces) {
 600     DumpAllocClosure dac;
 601     dac.iterate_metaspace(_loader_data->ro_metaspace(), DumpAllocClosure::RO);
 602     dac.iterate_metaspace(_loader_data->rw_metaspace(), DumpAllocClosure::RW);
 603 
 604     dac.dump_stats(int(ro_bytes), int(rw_bytes), int(md_bytes), int(mc_bytes));
 605   }
 606 }
 607 
 608 static void link_shared_classes(Klass* obj, TRAPS) {
 609   Klass* k = obj;
 610   if (k->oop_is_instance()) {
 611     InstanceKlass* ik = (InstanceKlass*) k;
 612     // Link the class to cause the bytecodes to be rewritten and the
 613     // cpcache to be created.
 614     if (ik->init_state() < InstanceKlass::linked) {
 615       ik->link_class(THREAD);
 616       guarantee(!HAS_PENDING_EXCEPTION, "exception in class rewriting");
 617     }
 618   }
 619 }
 620 
 621 
 622 // Support for a simple checksum of the contents of the class list
 623 // file to prevent trivial tampering. The algorithm matches that in
 624 // the MakeClassList program used by the J2SE build process.
 625 #define JSUM_SEED ((jlong)CONST64(0xcafebabebabecafe))
 626 static jlong
 627 jsum(jlong start, const char *buf, const int len)
 628 {
 629     jlong h = start;
 630     char *p = (char *)buf, *e = p + len;
 631     while (p < e) {
 632         char c = *p++;
 633         if (c <= ' ') {
 634             /* Skip spaces and control characters */
 635             continue;
 636         }
 637         h = 31 * h + c;
 638     }
 639     return h;
 640 }
 641 
 642 // Preload classes from a list, populate the shared spaces and dump to a
 643 // file.
 644 void MetaspaceShared::preload_and_dump(TRAPS) {
 645   TraceTime timer("Dump Shared Spaces", TraceStartupTime);
 646   ResourceMark rm;
 647 
 648   // Preload classes to be shared.
 649   // Should use some os:: method rather than fopen() here. aB.
 650   // Construct the path to the class list (in jre/lib)
 651   // Walk up two directories from the location of the VM and
 652   // optionally tack on "lib" (depending on platform)
 653   char class_list_path[JVM_MAXPATHLEN];
 654   os::jvm_path(class_list_path, sizeof(class_list_path));
 655   for (int i = 0; i < 3; i++) {
 656     char *end = strrchr(class_list_path, *os::file_separator());
 657     if (end != NULL) *end = '\0';
 658   }
 659   int class_list_path_len = (int)strlen(class_list_path);
 660   if (class_list_path_len >= 3) {
 661     if (strcmp(class_list_path + class_list_path_len - 3, "lib") != 0) {
 662       strcat(class_list_path, os::file_separator());
 663       strcat(class_list_path, "lib");
 664     }
 665   }
 666   strcat(class_list_path, os::file_separator());
 667   strcat(class_list_path, "classlist");
 668 
 669   FILE* file = fopen(class_list_path, "r");
 670   if (file != NULL) {
 671     jlong computed_jsum  = JSUM_SEED;
 672     jlong file_jsum      = 0;
 673 
 674     char class_name[256];
 675     int class_count = 0;
 676     GrowableArray<Klass*>* class_promote_order = new GrowableArray<Klass*>();
 677 
 678     // sun.io.Converters
 679     static const char obj_array_sig[] = "[[Ljava/lang/Object;";
 680     SymbolTable::new_permanent_symbol(obj_array_sig, THREAD);
 681 
 682     // java.util.HashMap
 683     static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;";
 684     SymbolTable::new_permanent_symbol(map_entry_array_sig, THREAD);
 685 
 686     tty->print("Loading classes to share ... ");
 687     while ((fgets(class_name, sizeof class_name, file)) != NULL) {
 688       if (*class_name == '#') {
 689         jint fsh, fsl;
 690         if (sscanf(class_name, "# %8x%8x\n", &fsh, &fsl) == 2) {
 691           file_jsum = ((jlong)(fsh) << 32) | (fsl & 0xffffffff);
 692         }
 693 
 694         continue;
 695       }
 696       // Remove trailing newline
 697       size_t name_len = strlen(class_name);
 698       class_name[name_len-1] = '\0';
 699 
 700       computed_jsum = jsum(computed_jsum, class_name, (const int)name_len - 1);
 701 
 702       // Got a class name - load it.
 703       TempNewSymbol class_name_symbol = SymbolTable::new_permanent_symbol(class_name, THREAD);
 704       guarantee(!HAS_PENDING_EXCEPTION, "Exception creating a symbol.");
 705       Klass* klass = SystemDictionary::resolve_or_null(class_name_symbol,
 706                                                          THREAD);
 707       guarantee(!HAS_PENDING_EXCEPTION, "Exception resolving a class.");
 708       if (klass != NULL) {
 709         if (PrintSharedSpaces && Verbose && WizardMode) {
 710           tty->print_cr("Shared spaces preloaded: %s", class_name);
 711         }
 712 
 713 
 714         InstanceKlass* ik = InstanceKlass::cast(klass);
 715 
 716         // Should be class load order as per -XX:+TraceClassLoadingPreorder
 717         class_promote_order->append(ik);
 718 
 719         // Link the class to cause the bytecodes to be rewritten and the
 720         // cpcache to be created. The linking is done as soon as classes
 721         // are loaded in order that the related data structures (klass and
 722         // cpCache) are located together.
 723 
 724         if (ik->init_state() < InstanceKlass::linked) {
 725           ik->link_class(THREAD);
 726           guarantee(!(HAS_PENDING_EXCEPTION), "exception in class rewriting");
 727         }
 728 
 729         // TODO: Resolve klasses in constant pool
 730         ik->constants()->resolve_class_constants(THREAD);
 731 
 732         class_count++;
 733       } else {
 734         if (PrintSharedSpaces && Verbose && WizardMode) {
 735           tty->cr();
 736           tty->print_cr(" Preload failed: %s", class_name);
 737         }
 738       }
 739       file_jsum = 0; // Checksum must be on last line of file
 740     }
 741     if (computed_jsum != file_jsum) {
 742       tty->cr();
 743       tty->print_cr("Preload failed: checksum of class list was incorrect.");
 744       exit(1);
 745     }
 746 
 747     tty->print_cr("done. ");
 748 
 749     if (PrintSharedSpaces) {
 750       tty->print_cr("Shared spaces: preloaded %d classes", class_count);
 751     }
 752 
 753     // Rewrite and unlink classes.
 754     tty->print("Rewriting and linking classes ... ");
 755 
 756     // Link any classes which got missed.  (It's not quite clear why
 757     // they got missed.)  This iteration would be unsafe if we weren't
 758     // single-threaded at this point; however we can't do it on the VM
 759     // thread because it requires object allocation.
 760     SystemDictionary::classes_do(link_shared_classes, CATCH);
 761     tty->print_cr("done. ");
 762 
 763     // Create and dump the shared spaces.   Everything so far is loaded
 764     // with the null class loader.
 765     ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
 766     VM_PopulateDumpSharedSpace op(loader_data, class_promote_order);
 767     VMThread::execute(&op);
 768 
 769   } else {
 770     char errmsg[JVM_MAXPATHLEN];
 771     os::lasterror(errmsg, JVM_MAXPATHLEN);
 772     tty->print_cr("Loading classlist failed: %s", errmsg);
 773     exit(1);
 774   }
 775 
 776   // Since various initialization steps have been undone by this process,
 777   // it is not reasonable to continue running a java process.
 778   exit(0);
 779 }
 780 
 781 
 782 // Closure for serializing initialization data in from a data area
 783 // (ptr_array) read from the shared file.
 784 
 785 class ReadClosure : public SerializeClosure {
 786 private:
 787   intptr_t** _ptr_array;
 788 
 789   inline intptr_t nextPtr() {
 790     return *(*_ptr_array)++;
 791   }
 792 
 793 public:
 794   ReadClosure(intptr_t** ptr_array) { _ptr_array = ptr_array; }
 795 
 796   void do_ptr(void** p) {
 797     assert(*p == NULL, "initializing previous initialized pointer.");
 798     intptr_t obj = nextPtr();
 799     assert((intptr_t)obj >= 0 || (intptr_t)obj < -100,
 800            "hit tag while initializing ptrs.");
 801     *p = (void*)obj;
 802   }
 803 
 804   void do_tag(int tag) {
 805     int old_tag;
 806     old_tag = (int)(intptr_t)nextPtr();
 807     // do_int(&old_tag);
 808     assert(tag == old_tag, "old tag doesn't match");
 809     FileMapInfo::assert_mark(tag == old_tag);
 810   }
 811 
 812   void do_region(u_char* start, size_t size) {
 813     assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
 814     assert(size % sizeof(intptr_t) == 0, "bad size");
 815     do_tag((int)size);
 816     while (size > 0) {
 817       *(intptr_t*)start = nextPtr();
 818       start += sizeof(intptr_t);
 819       size -= sizeof(intptr_t);
 820     }
 821   }
 822 
 823   bool reading() const { return true; }
 824 };
 825 
 826 // Return true if given address is in the mapped shared space.
 827 bool MetaspaceShared::is_in_shared_space(const void* p) {
 828   return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_space(p);
 829 }
 830 
 831 void MetaspaceShared::print_shared_spaces() {
 832   if (UseSharedSpaces) {
 833     FileMapInfo::current_info()->print_shared_spaces();
 834   }
 835 }
 836 
 837 
 838 // Map shared spaces at requested addresses and return if succeeded.
 839 // Need to keep the bounds of the ro and rw space for the Metaspace::contains
 840 // call, or is_in_shared_space.
 841 bool MetaspaceShared::map_shared_spaces(FileMapInfo* mapinfo) {
 842   size_t image_alignment = mapinfo->alignment();
 843 
 844 #ifndef _WINDOWS
 845   // Map in the shared memory and then map the regions on top of it.
 846   // On Windows, don't map the memory here because it will cause the
 847   // mappings of the regions to fail.
 848   ReservedSpace shared_rs = mapinfo->reserve_shared_memory();
 849   if (!shared_rs.is_reserved()) return false;
 850 #endif
 851 
 852   assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces");
 853 
 854   char* _ro_base = NULL;
 855   char* _rw_base = NULL;
 856   char* _md_base = NULL;
 857   char* _mc_base = NULL;
 858 
 859   // Map each shared region
 860   if ((_ro_base = mapinfo->map_region(ro)) != NULL &&
 861       (_rw_base = mapinfo->map_region(rw)) != NULL &&
 862       (_md_base = mapinfo->map_region(md)) != NULL &&
 863       (_mc_base = mapinfo->map_region(mc)) != NULL &&
 864       (image_alignment == (size_t)max_alignment())) {
 865     // Success (no need to do anything)
 866     return true;
 867   } else {
 868     // If there was a failure in mapping any of the spaces, unmap the ones
 869     // that succeeded
 870     if (_ro_base != NULL) mapinfo->unmap_region(ro);
 871     if (_rw_base != NULL) mapinfo->unmap_region(rw);
 872     if (_md_base != NULL) mapinfo->unmap_region(md);
 873     if (_mc_base != NULL) mapinfo->unmap_region(mc);
 874 #ifndef _WINDOWS
 875     // Release the entire mapped region
 876     shared_rs.release();
 877 #endif
 878     // If -Xshare:on is specified, print out the error message and exit VM,
 879     // otherwise, set UseSharedSpaces to false and continue.
 880     if (RequireSharedSpaces) {
 881       vm_exit_during_initialization("Unable to use shared archive.", NULL);
 882     } else {
 883       FLAG_SET_DEFAULT(UseSharedSpaces, false);
 884     }
 885     return false;
 886   }
 887 }
 888 
 889 // Read the miscellaneous data from the shared file, and
 890 // serialize it out to its various destinations.
 891 
 892 void MetaspaceShared::initialize_shared_spaces() {
 893   FileMapInfo *mapinfo = FileMapInfo::current_info();
 894 
 895   char* buffer = mapinfo->region_base(md);
 896 
 897   // Skip over (reserve space for) a list of addresses of C++ vtables
 898   // for Klass objects.  They get filled in later.
 899 
 900   void** vtbl_list = (void**)buffer;
 901   buffer += MetaspaceShared::vtbl_list_size * sizeof(void*);
 902   Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size);
 903 
 904   // Skip over (reserve space for) dummy C++ vtables Klass objects.
 905   // They are used as is.
 906 
 907   intptr_t vtable_size = *(intptr_t*)buffer;
 908   buffer += sizeof(intptr_t);
 909   buffer += vtable_size;
 910 
 911   // Create the symbol table using the bucket array at this spot in the
 912   // misc data space.  Since the symbol table is often modified, this
 913   // region (of mapped pages) will be copy-on-write.
 914 
 915   int symbolTableLen = *(intptr_t*)buffer;
 916   buffer += sizeof(intptr_t);
 917   int number_of_entries = *(intptr_t*)buffer;
 918   buffer += sizeof(intptr_t);
 919   SymbolTable::create_table((HashtableBucket<mtSymbol>*)buffer, symbolTableLen,
 920                             number_of_entries);
 921   buffer += symbolTableLen;
 922 
 923   // Create the shared dictionary using the bucket array at this spot in
 924   // the misc data space.  Since the shared dictionary table is never
 925   // modified, this region (of mapped pages) will be (effectively, if
 926   // not explicitly) read-only.
 927 
 928   int sharedDictionaryLen = *(intptr_t*)buffer;
 929   buffer += sizeof(intptr_t);
 930   number_of_entries = *(intptr_t*)buffer;
 931   buffer += sizeof(intptr_t);
 932   SystemDictionary::set_shared_dictionary((HashtableBucket<mtClass>*)buffer,
 933                                           sharedDictionaryLen,
 934                                           number_of_entries);
 935   buffer += sharedDictionaryLen;
 936 
 937   // Create the package info table using the bucket array at this spot in
 938   // the misc data space.  Since the package info table is never
 939   // modified, this region (of mapped pages) will be (effectively, if
 940   // not explicitly) read-only.
 941 
 942   int pkgInfoLen = *(intptr_t*)buffer;
 943   buffer += sizeof(intptr_t);
 944   number_of_entries = *(intptr_t*)buffer;
 945   buffer += sizeof(intptr_t);
 946   ClassLoader::create_package_info_table((HashtableBucket<mtClass>*)buffer, pkgInfoLen,
 947                                          number_of_entries);
 948   buffer += pkgInfoLen;
 949   ClassLoader::verify();
 950 
 951   // The following data in the shared misc data region are the linked
 952   // list elements (HashtableEntry objects) for the symbol table, string
 953   // table, and shared dictionary.  The heap objects referred to by the
 954   // symbol table, string table, and shared dictionary are permanent and
 955   // unmovable.  Since new entries added to the string and symbol tables
 956   // are always added at the beginning of the linked lists, THESE LINKED
 957   // LIST ELEMENTS ARE READ-ONLY.
 958 
 959   int len = *(intptr_t*)buffer; // skip over symbol table entries
 960   buffer += sizeof(intptr_t);
 961   buffer += len;
 962 
 963   len = *(intptr_t*)buffer;     // skip over shared dictionary entries
 964   buffer += sizeof(intptr_t);
 965   buffer += len;
 966 
 967   len = *(intptr_t*)buffer;     // skip over package info table entries
 968   buffer += sizeof(intptr_t);
 969   buffer += len;
 970 
 971   len = *(intptr_t*)buffer;     // skip over package info table char[] arrays.
 972   buffer += sizeof(intptr_t);
 973   buffer += len;
 974 
 975   intptr_t* array = (intptr_t*)buffer;
 976   ReadClosure rc(&array);
 977   serialize(&rc);
 978 
 979   // Close the mapinfo file
 980   mapinfo->close();
 981 }
 982 
 983 // JVM/TI RedefineClasses() support:
 984 bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
 985   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 986 
 987   if (UseSharedSpaces) {
 988     // remap the shared readonly space to shared readwrite, private
 989     FileMapInfo* mapinfo = FileMapInfo::current_info();
 990     if (!mapinfo->remap_shared_readonly_as_readwrite()) {
 991       return false;
 992     }
 993   }
 994   return true;
 995 }