annotate src/share/vm/memory/metaspaceShared.cpp @ 10487:c558850fac57

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