src/share/vm/memory/metaspaceShared.cpp

Thu, 21 Aug 2014 13:57:51 -0700

author
iklam
date
Thu, 21 Aug 2014 13:57:51 -0700
changeset 7089
6e0cb14ce59b
parent 6680
78bbf4d43a14
child 7103
622c6e0ad4d6
permissions
-rw-r--r--

8046070: Class Data Sharing clean up and refactoring
Summary: Cleaned up CDS to be more configurable, maintainable and extensible
Reviewed-by: dholmes, coleenp, acorn, mchung

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

mercurial