src/share/vm/memory/metaspaceShared.cpp

Fri, 29 Apr 2016 00:06:10 +0800

author
aoqi
date
Fri, 29 Apr 2016 00:06:10 +0800
changeset 1
2d8a650513c2
parent 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Added MIPS 64-bit port.

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

mercurial