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