src/share/vm/memory/dump.cpp

Tue, 13 Mar 2012 13:50:48 -0400

author
jiangli
date
Tue, 13 Mar 2012 13:50:48 -0400
changeset 3670
f7c4174b33ba
parent 3368
52b5d32fbfaf
child 3686
749b1464aa81
permissions
-rw-r--r--

7109878: The instanceKlass EnclosingMethhod attribute fields can be folded into the _inner_class field.
Summary: Fold instanceKlass::_enclosing_method_class_index and instanceKlass::_enclosing_method_method_index into the instanceKlass::_inner_classes array.
Reviewed-by: never, coleenp
Contributed-by: Jiangli Zhou <jiangli.zhou@oracle.com>

duke@435 1 /*
never@2700 2 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "classfile/javaClasses.hpp"
stefank@2314 27 #include "classfile/loaderConstraints.hpp"
stefank@2314 28 #include "classfile/symbolTable.hpp"
stefank@2314 29 #include "classfile/systemDictionary.hpp"
stefank@2314 30 #include "gc_implementation/shared/spaceDecorator.hpp"
stefank@2314 31 #include "memory/classify.hpp"
stefank@2314 32 #include "memory/filemap.hpp"
stefank@2314 33 #include "memory/oopFactory.hpp"
stefank@2314 34 #include "memory/resourceArea.hpp"
stefank@2314 35 #include "oops/methodDataOop.hpp"
stefank@2314 36 #include "oops/oop.inline.hpp"
stefank@2314 37 #include "runtime/javaCalls.hpp"
stefank@2314 38 #include "runtime/signature.hpp"
stefank@2314 39 #include "runtime/vmThread.hpp"
stefank@2314 40 #include "runtime/vm_operations.hpp"
stefank@2314 41 #include "utilities/copy.hpp"
duke@435 42
duke@435 43
duke@435 44 // Closure to set up the fingerprint field for all methods.
duke@435 45
duke@435 46 class FingerprintMethodsClosure: public ObjectClosure {
duke@435 47 public:
duke@435 48 void do_object(oop obj) {
duke@435 49 if (obj->is_method()) {
duke@435 50 methodOop mobj = (methodOop)obj;
duke@435 51 ResourceMark rm;
duke@435 52 (new Fingerprinter(mobj))->fingerprint();
duke@435 53 }
duke@435 54 }
duke@435 55 };
duke@435 56
duke@435 57
duke@435 58
duke@435 59 // Closure to set the hash value (String.hash field) in all of the
duke@435 60 // String objects in the heap. Setting the hash value is not required.
duke@435 61 // However, setting the value in advance prevents the value from being
duke@435 62 // written later, increasing the likelihood that the shared page contain
duke@435 63 // the hash can be shared.
duke@435 64 //
duke@435 65 // NOTE THAT the algorithm in StringTable::hash_string() MUST MATCH the
duke@435 66 // algorithm in java.lang.String.hashCode().
duke@435 67
duke@435 68 class StringHashCodeClosure: public OopClosure {
duke@435 69 private:
duke@435 70 Thread* THREAD;
duke@435 71 int hash_offset;
duke@435 72 public:
duke@435 73 StringHashCodeClosure(Thread* t) {
duke@435 74 THREAD = t;
duke@435 75 hash_offset = java_lang_String::hash_offset_in_bytes();
duke@435 76 }
duke@435 77
coleenp@548 78 void do_oop(oop* p) {
coleenp@548 79 if (p != NULL) {
coleenp@548 80 oop obj = *p;
never@1577 81 if (obj->klass() == SystemDictionary::String_klass()) {
duke@435 82
never@2700 83 int hash = java_lang_String::hash_string(obj);
duke@435 84 obj->int_field_put(hash_offset, hash);
duke@435 85 }
duke@435 86 }
duke@435 87 }
coleenp@548 88 void do_oop(narrowOop* p) { ShouldNotReachHere(); }
duke@435 89 };
duke@435 90
duke@435 91
duke@435 92 // Remove data from objects which should not appear in the shared file
duke@435 93 // (as it pertains only to the current JVM).
duke@435 94
duke@435 95 class RemoveUnshareableInfoClosure : public ObjectClosure {
duke@435 96 public:
duke@435 97 void do_object(oop obj) {
duke@435 98 // Zap data from the objects which is pertains only to this JVM. We
duke@435 99 // want that data recreated in new JVMs when the shared file is used.
duke@435 100 if (obj->is_method()) {
duke@435 101 ((methodOop)obj)->remove_unshareable_info();
duke@435 102 }
duke@435 103 else if (obj->is_klass()) {
duke@435 104 Klass::cast((klassOop)obj)->remove_unshareable_info();
duke@435 105 }
duke@435 106
duke@435 107 // Don't save compiler related special oops (shouldn't be any yet).
duke@435 108 if (obj->is_methodData() || obj->is_compiledICHolder()) {
duke@435 109 ShouldNotReachHere();
duke@435 110 }
duke@435 111 }
duke@435 112 };
duke@435 113
duke@435 114
duke@435 115 static bool mark_object(oop obj) {
duke@435 116 if (obj != NULL &&
duke@435 117 !obj->is_shared() &&
duke@435 118 !obj->is_forwarded() &&
duke@435 119 !obj->is_gc_marked()) {
duke@435 120 obj->set_mark(markOopDesc::prototype()->set_marked());
duke@435 121 return true;
duke@435 122 }
duke@435 123
duke@435 124 return false;
duke@435 125 }
duke@435 126
coleenp@2497 127
coleenp@2497 128 class MoveSymbols : public SymbolClosure {
coleenp@2497 129 private:
coleenp@2497 130 char* _start;
coleenp@2497 131 char* _end;
coleenp@2497 132 char* _top;
coleenp@2497 133 int _count;
coleenp@2497 134
coleenp@2497 135 bool in_shared_space(Symbol* sym) const {
coleenp@2497 136 return (char*)sym >= _start && (char*)sym < _end;
coleenp@2497 137 }
coleenp@2497 138
coleenp@2497 139 Symbol* get_shared_copy(Symbol* sym) {
coleenp@2497 140 return sym->refcount() > 0 ? NULL : (Symbol*)(_start - sym->refcount());
coleenp@2497 141 }
coleenp@2497 142
coleenp@2497 143 Symbol* make_shared_copy(Symbol* sym) {
coleenp@2497 144 Symbol* new_sym = (Symbol*)_top;
coleenp@2497 145 int size = sym->object_size();
coleenp@2497 146 _top += size * HeapWordSize;
coleenp@2497 147 if (_top <= _end) {
coleenp@2497 148 Copy::disjoint_words((HeapWord*)sym, (HeapWord*)new_sym, size);
coleenp@2497 149 // Encode a reference to the copy as a negative distance from _start
coleenp@2497 150 // When a symbol is being copied to a shared space
coleenp@2497 151 // during CDS archive creation, the original symbol is marked
coleenp@2497 152 // as relocated by putting a negative value to its _refcount field,
coleenp@2497 153 // This value is also used to find where exactly the shared copy is
coleenp@2497 154 // (see MoveSymbols::get_shared_copy), so that the other references
coleenp@2497 155 // to this symbol could be changed to point to the shared copy.
coleenp@2497 156 sym->_refcount = (int)(_start - (char*)new_sym);
coleenp@2497 157 // Mark the symbol in the shared archive as immortal so it is read only
coleenp@2497 158 // and not refcounted.
coleenp@2497 159 new_sym->_refcount = -1;
coleenp@2497 160 _count++;
coleenp@2497 161 } else {
coleenp@2497 162 report_out_of_shared_space(SharedMiscData);
coleenp@2497 163 }
coleenp@2497 164 return new_sym;
coleenp@2497 165 }
coleenp@2497 166
coleenp@2497 167 public:
coleenp@2497 168 MoveSymbols(char* top, char* end) :
coleenp@2497 169 _start(top), _end(end), _top(top), _count(0) { }
coleenp@2497 170
coleenp@2497 171 char* get_top() const { return _top; }
coleenp@2497 172 int count() const { return _count; }
coleenp@2497 173
coleenp@2497 174 void do_symbol(Symbol** p) {
coleenp@2497 175 Symbol* sym = load_symbol(p);
coleenp@2497 176 if (sym != NULL && !in_shared_space(sym)) {
coleenp@2497 177 Symbol* new_sym = get_shared_copy(sym);
coleenp@2497 178 if (new_sym == NULL) {
coleenp@2497 179 // The symbol has not been relocated yet; copy it to _top address
coleenp@2497 180 assert(sym->refcount() > 0, "should have positive reference count");
coleenp@2497 181 new_sym = make_shared_copy(sym);
coleenp@2497 182 }
coleenp@2497 183 // Make the reference point to the shared copy of the symbol
coleenp@2497 184 store_symbol(p, new_sym);
coleenp@2497 185 }
coleenp@2497 186 }
coleenp@2497 187 };
coleenp@2497 188
coleenp@2497 189
duke@435 190 // Closure: mark objects closure.
duke@435 191
duke@435 192 class MarkObjectsOopClosure : public OopClosure {
duke@435 193 public:
coleenp@548 194 void do_oop(oop* p) { mark_object(*p); }
coleenp@548 195 void do_oop(narrowOop* p) { ShouldNotReachHere(); }
duke@435 196 };
duke@435 197
duke@435 198
duke@435 199 class MarkObjectsSkippingKlassesOopClosure : public OopClosure {
duke@435 200 public:
duke@435 201 void do_oop(oop* pobj) {
duke@435 202 oop obj = *pobj;
duke@435 203 if (obj != NULL &&
duke@435 204 !obj->is_klass()) {
duke@435 205 mark_object(obj);
duke@435 206 }
duke@435 207 }
coleenp@548 208 void do_oop(narrowOop* pobj) { ShouldNotReachHere(); }
duke@435 209 };
duke@435 210
duke@435 211
duke@435 212 static void mark_object_recursive_skipping_klasses(oop obj) {
duke@435 213 mark_object(obj);
duke@435 214 if (obj != NULL) {
duke@435 215 MarkObjectsSkippingKlassesOopClosure mark_all;
duke@435 216 obj->oop_iterate(&mark_all);
duke@435 217 }
duke@435 218 }
duke@435 219
duke@435 220
coleenp@2497 221 // Closure: mark common read-only objects
duke@435 222
duke@435 223 class MarkCommonReadOnly : public ObjectClosure {
duke@435 224 private:
duke@435 225 MarkObjectsOopClosure mark_all;
duke@435 226 public:
duke@435 227 void do_object(oop obj) {
duke@435 228
duke@435 229 // Mark all constMethod objects.
duke@435 230
duke@435 231 if (obj->is_constMethod()) {
duke@435 232 mark_object(obj);
duke@435 233 mark_object(constMethodOop(obj)->stackmap_data());
duke@435 234 // Exception tables are needed by ci code during compilation.
duke@435 235 mark_object(constMethodOop(obj)->exception_table());
duke@435 236 }
duke@435 237
duke@435 238 // Mark objects referenced by klass objects which are read-only.
duke@435 239
duke@435 240 else if (obj->is_klass()) {
duke@435 241 Klass* k = Klass::cast((klassOop)obj);
duke@435 242 mark_object(k->secondary_supers());
duke@435 243
duke@435 244 // The METHODS() OBJARRAYS CANNOT BE MADE READ-ONLY, even though
duke@435 245 // it is never modified. Otherwise, they will be pre-marked; the
duke@435 246 // GC marking phase will skip them; and by skipping them will fail
duke@435 247 // to mark the methods objects referenced by the array.
duke@435 248
duke@435 249 if (obj->blueprint()->oop_is_instanceKlass()) {
duke@435 250 instanceKlass* ik = instanceKlass::cast((klassOop)obj);
duke@435 251 mark_object(ik->method_ordering());
duke@435 252 mark_object(ik->local_interfaces());
duke@435 253 mark_object(ik->transitive_interfaces());
duke@435 254 mark_object(ik->fields());
duke@435 255
duke@435 256 mark_object(ik->class_annotations());
duke@435 257
duke@435 258 mark_object_recursive_skipping_klasses(ik->fields_annotations());
duke@435 259 mark_object_recursive_skipping_klasses(ik->methods_annotations());
duke@435 260 mark_object_recursive_skipping_klasses(ik->methods_parameter_annotations());
duke@435 261 mark_object_recursive_skipping_klasses(ik->methods_default_annotations());
duke@435 262
duke@435 263 typeArrayOop inner_classes = ik->inner_classes();
duke@435 264 if (inner_classes != NULL) {
duke@435 265 mark_object(inner_classes);
duke@435 266 }
duke@435 267 }
duke@435 268 }
duke@435 269 }
duke@435 270 };
duke@435 271
duke@435 272
coleenp@2497 273 // Closure: find symbol references in Java Heap objects
duke@435 274
coleenp@2497 275 class CommonSymbolsClosure : public ObjectClosure {
duke@435 276 private:
coleenp@2497 277 SymbolClosure* _closure;
duke@435 278 public:
coleenp@2497 279 CommonSymbolsClosure(SymbolClosure* closure) : _closure(closure) { }
coleenp@2497 280
duke@435 281 void do_object(oop obj) {
duke@435 282
coleenp@2497 283 // Traverse symbols referenced by method objects.
duke@435 284
duke@435 285 if (obj->is_method()) {
duke@435 286 methodOop m = methodOop(obj);
coleenp@2497 287 constantPoolOop constants = m->constants();
coleenp@2497 288 _closure->do_symbol(constants->symbol_at_addr(m->name_index()));
coleenp@2497 289 _closure->do_symbol(constants->symbol_at_addr(m->signature_index()));
duke@435 290 }
duke@435 291
coleenp@2497 292 // Traverse symbols referenced by klass objects which are read-only.
duke@435 293
duke@435 294 else if (obj->is_klass()) {
coleenp@2497 295 Klass* k = Klass::cast((klassOop)obj);
coleenp@2497 296 k->shared_symbols_iterate(_closure);
duke@435 297
duke@435 298 if (obj->blueprint()->oop_is_instanceKlass()) {
duke@435 299 instanceKlass* ik = instanceKlass::cast((klassOop)obj);
jiangli@3670 300 instanceKlassHandle ik_h((klassOop)obj);
jiangli@3670 301 InnerClassesIterator iter(ik_h);
jiangli@3670 302 constantPoolOop constants = ik->constants();
jiangli@3670 303 for (; !iter.done(); iter.next()) {
jiangli@3670 304 int index = iter.inner_name_index();
jiangli@3670 305
jiangli@3670 306 if (index != 0) {
jiangli@3670 307 _closure->do_symbol(constants->symbol_at_addr(index));
duke@435 308 }
duke@435 309 }
duke@435 310 }
duke@435 311 }
duke@435 312
coleenp@2497 313 // Traverse symbols referenced by other constantpool entries.
duke@435 314
coleenp@2497 315 else if (obj->is_constantPool()) {
coleenp@2497 316 constantPoolOop(obj)->shared_symbols_iterate(_closure);
duke@435 317 }
duke@435 318 }
duke@435 319 };
duke@435 320
duke@435 321
duke@435 322 // Closure: mark char arrays used by strings
duke@435 323
duke@435 324 class MarkStringValues : public ObjectClosure {
duke@435 325 private:
duke@435 326 MarkObjectsOopClosure mark_all;
duke@435 327 public:
duke@435 328 void do_object(oop obj) {
duke@435 329
duke@435 330 // Character arrays referenced by String objects are read-only.
duke@435 331
duke@435 332 if (java_lang_String::is_instance(obj)) {
duke@435 333 mark_object(java_lang_String::value(obj));
duke@435 334 }
duke@435 335 }
duke@435 336 };
duke@435 337
duke@435 338
duke@435 339 #ifdef DEBUG
duke@435 340 // Closure: Check for objects left in the heap which have not been moved.
duke@435 341
duke@435 342 class CheckRemainingObjects : public ObjectClosure {
duke@435 343 private:
duke@435 344 int count;
duke@435 345
duke@435 346 public:
duke@435 347 CheckRemainingObjects() {
duke@435 348 count = 0;
duke@435 349 }
duke@435 350
duke@435 351 void do_object(oop obj) {
duke@435 352 if (!obj->is_shared() &&
duke@435 353 !obj->is_forwarded()) {
duke@435 354 ++count;
duke@435 355 if (Verbose) {
duke@435 356 tty->print("Unreferenced object: ");
duke@435 357 obj->print_on(tty);
duke@435 358 }
duke@435 359 }
duke@435 360 }
duke@435 361
duke@435 362 void status() {
duke@435 363 tty->print_cr("%d objects no longer referenced, not shared.", count);
duke@435 364 }
duke@435 365 };
duke@435 366 #endif
duke@435 367
duke@435 368
duke@435 369 // Closure: Mark remaining objects read-write, except Strings.
duke@435 370
duke@435 371 class MarkReadWriteObjects : public ObjectClosure {
duke@435 372 private:
duke@435 373 MarkObjectsOopClosure mark_objects;
duke@435 374 public:
duke@435 375 void do_object(oop obj) {
duke@435 376
duke@435 377 // The METHODS() OBJARRAYS CANNOT BE MADE READ-ONLY, even though
duke@435 378 // it is never modified. Otherwise, they will be pre-marked; the
duke@435 379 // GC marking phase will skip them; and by skipping them will fail
duke@435 380 // to mark the methods objects referenced by the array.
duke@435 381
duke@435 382 if (obj->is_klass()) {
duke@435 383 mark_object(obj);
duke@435 384 Klass* k = klassOop(obj)->klass_part();
duke@435 385 mark_object(k->java_mirror());
duke@435 386 if (obj->blueprint()->oop_is_instanceKlass()) {
duke@435 387 instanceKlass* ik = (instanceKlass*)k;
duke@435 388 mark_object(ik->methods());
duke@435 389 mark_object(ik->constants());
duke@435 390 }
duke@435 391 if (obj->blueprint()->oop_is_javaArray()) {
duke@435 392 arrayKlass* ak = (arrayKlass*)k;
duke@435 393 mark_object(ak->component_mirror());
duke@435 394 }
duke@435 395 return;
duke@435 396 }
duke@435 397
duke@435 398 // Mark constantPool tags and the constantPoolCache.
duke@435 399
duke@435 400 else if (obj->is_constantPool()) {
duke@435 401 constantPoolOop pool = constantPoolOop(obj);
duke@435 402 mark_object(pool->cache());
duke@435 403 pool->shared_tags_iterate(&mark_objects);
duke@435 404 return;
duke@435 405 }
duke@435 406
duke@435 407 // Mark all method objects.
duke@435 408
duke@435 409 if (obj->is_method()) {
duke@435 410 mark_object(obj);
duke@435 411 }
duke@435 412 }
duke@435 413 };
duke@435 414
duke@435 415
duke@435 416 // Closure: Mark String objects read-write.
duke@435 417
duke@435 418 class MarkStringObjects : public ObjectClosure {
duke@435 419 private:
duke@435 420 MarkObjectsOopClosure mark_objects;
duke@435 421 public:
duke@435 422 void do_object(oop obj) {
duke@435 423
duke@435 424 // Mark String objects referenced by constant pool entries.
duke@435 425
duke@435 426 if (obj->is_constantPool()) {
duke@435 427 constantPoolOop pool = constantPoolOop(obj);
duke@435 428 pool->shared_strings_iterate(&mark_objects);
duke@435 429 return;
duke@435 430 }
duke@435 431 }
duke@435 432 };
duke@435 433
duke@435 434
duke@435 435 // Move objects matching specified type (ie. lock_bits) to the specified
duke@435 436 // space.
duke@435 437
duke@435 438 class MoveMarkedObjects : public ObjectClosure {
duke@435 439 private:
duke@435 440 OffsetTableContigSpace* _space;
duke@435 441 bool _read_only;
duke@435 442
duke@435 443 public:
duke@435 444 MoveMarkedObjects(OffsetTableContigSpace* space, bool read_only) {
duke@435 445 _space = space;
duke@435 446 _read_only = read_only;
duke@435 447 }
duke@435 448
duke@435 449 void do_object(oop obj) {
duke@435 450 if (obj->is_shared()) {
duke@435 451 return;
duke@435 452 }
duke@435 453 if (obj->is_gc_marked() && obj->forwardee() == NULL) {
duke@435 454 int s = obj->size();
duke@435 455 oop sh_obj = (oop)_space->allocate(s);
duke@435 456 if (sh_obj == NULL) {
coleenp@2497 457 report_out_of_shared_space(_read_only ? SharedReadOnly : SharedReadWrite);
duke@435 458 }
duke@435 459 if (PrintSharedSpaces && Verbose && WizardMode) {
duke@435 460 tty->print_cr("\nMoveMarkedObjects: " PTR_FORMAT " -> " PTR_FORMAT " %s", obj, sh_obj,
duke@435 461 (_read_only ? "ro" : "rw"));
duke@435 462 }
duke@435 463 Copy::aligned_disjoint_words((HeapWord*)obj, (HeapWord*)sh_obj, s);
duke@435 464 obj->forward_to(sh_obj);
duke@435 465 if (_read_only) {
duke@435 466 // Readonly objects: set hash value to self pointer and make gc_marked.
duke@435 467 sh_obj->forward_to(sh_obj);
duke@435 468 } else {
duke@435 469 sh_obj->init_mark();
duke@435 470 }
duke@435 471 }
duke@435 472 }
duke@435 473 };
duke@435 474
duke@435 475 static void mark_and_move(oop obj, MoveMarkedObjects* move) {
duke@435 476 if (mark_object(obj)) move->do_object(obj);
duke@435 477 }
duke@435 478
duke@435 479 enum order_policy {
duke@435 480 OP_favor_startup = 0,
duke@435 481 OP_balanced = 1,
duke@435 482 OP_favor_runtime = 2
duke@435 483 };
duke@435 484
duke@435 485 static void mark_and_move_for_policy(order_policy policy, oop obj, MoveMarkedObjects* move) {
duke@435 486 if (SharedOptimizeColdStartPolicy >= policy) mark_and_move(obj, move);
duke@435 487 }
duke@435 488
duke@435 489 class MarkAndMoveOrderedReadOnly : public ObjectClosure {
duke@435 490 private:
duke@435 491 MoveMarkedObjects *_move_ro;
duke@435 492
duke@435 493 public:
duke@435 494 MarkAndMoveOrderedReadOnly(MoveMarkedObjects *move_ro) : _move_ro(move_ro) {}
duke@435 495
duke@435 496 void do_object(oop obj) {
duke@435 497 if (obj->is_klass() && obj->blueprint()->oop_is_instanceKlass()) {
duke@435 498 instanceKlass* ik = instanceKlass::cast((klassOop)obj);
duke@435 499 int i;
duke@435 500
duke@435 501 if (ik->super() != NULL) {
duke@435 502 do_object(ik->super());
duke@435 503 }
duke@435 504
duke@435 505 objArrayOop interfaces = ik->local_interfaces();
duke@435 506 mark_and_move_for_policy(OP_favor_startup, interfaces, _move_ro);
duke@435 507 for(i = 0; i < interfaces->length(); i++) {
duke@435 508 klassOop k = klassOop(interfaces->obj_at(i));
duke@435 509 do_object(k);
duke@435 510 }
duke@435 511
duke@435 512 objArrayOop methods = ik->methods();
duke@435 513 for(i = 0; i < methods->length(); i++) {
duke@435 514 methodOop m = methodOop(methods->obj_at(i));
duke@435 515 mark_and_move_for_policy(OP_favor_startup, m->constMethod(), _move_ro);
duke@435 516 mark_and_move_for_policy(OP_favor_runtime, m->constMethod()->exception_table(), _move_ro);
duke@435 517 mark_and_move_for_policy(OP_favor_runtime, m->constMethod()->stackmap_data(), _move_ro);
duke@435 518 }
duke@435 519
duke@435 520 mark_and_move_for_policy(OP_favor_startup, ik->transitive_interfaces(), _move_ro);
duke@435 521 mark_and_move_for_policy(OP_favor_startup, ik->fields(), _move_ro);
duke@435 522
duke@435 523 mark_and_move_for_policy(OP_favor_runtime, ik->secondary_supers(), _move_ro);
duke@435 524 mark_and_move_for_policy(OP_favor_runtime, ik->method_ordering(), _move_ro);
duke@435 525 mark_and_move_for_policy(OP_favor_runtime, ik->class_annotations(), _move_ro);
duke@435 526 mark_and_move_for_policy(OP_favor_runtime, ik->fields_annotations(), _move_ro);
duke@435 527 mark_and_move_for_policy(OP_favor_runtime, ik->methods_annotations(), _move_ro);
duke@435 528 mark_and_move_for_policy(OP_favor_runtime, ik->methods_parameter_annotations(), _move_ro);
duke@435 529 mark_and_move_for_policy(OP_favor_runtime, ik->methods_default_annotations(), _move_ro);
duke@435 530 mark_and_move_for_policy(OP_favor_runtime, ik->inner_classes(), _move_ro);
duke@435 531 mark_and_move_for_policy(OP_favor_runtime, ik->secondary_supers(), _move_ro);
duke@435 532 }
duke@435 533 }
duke@435 534 };
duke@435 535
duke@435 536 class MarkAndMoveOrderedReadWrite: public ObjectClosure {
duke@435 537 private:
duke@435 538 MoveMarkedObjects *_move_rw;
duke@435 539
duke@435 540 public:
duke@435 541 MarkAndMoveOrderedReadWrite(MoveMarkedObjects *move_rw) : _move_rw(move_rw) {}
duke@435 542
duke@435 543 void do_object(oop obj) {
duke@435 544 if (obj->is_klass() && obj->blueprint()->oop_is_instanceKlass()) {
duke@435 545 instanceKlass* ik = instanceKlass::cast((klassOop)obj);
duke@435 546 int i;
duke@435 547
duke@435 548 mark_and_move_for_policy(OP_favor_startup, ik->as_klassOop(), _move_rw);
duke@435 549
duke@435 550 if (ik->super() != NULL) {
duke@435 551 do_object(ik->super());
duke@435 552 }
duke@435 553
duke@435 554 objArrayOop interfaces = ik->local_interfaces();
duke@435 555 for(i = 0; i < interfaces->length(); i++) {
duke@435 556 klassOop k = klassOop(interfaces->obj_at(i));
duke@435 557 mark_and_move_for_policy(OP_favor_startup, k, _move_rw);
duke@435 558 do_object(k);
duke@435 559 }
duke@435 560
duke@435 561 objArrayOop methods = ik->methods();
duke@435 562 mark_and_move_for_policy(OP_favor_startup, methods, _move_rw);
duke@435 563 for(i = 0; i < methods->length(); i++) {
duke@435 564 methodOop m = methodOop(methods->obj_at(i));
duke@435 565 mark_and_move_for_policy(OP_favor_startup, m, _move_rw);
duke@435 566 mark_and_move_for_policy(OP_favor_startup, ik->constants(), _move_rw); // idempotent
duke@435 567 mark_and_move_for_policy(OP_balanced, ik->constants()->cache(), _move_rw); // idempotent
duke@435 568 mark_and_move_for_policy(OP_balanced, ik->constants()->tags(), _move_rw); // idempotent
duke@435 569 }
duke@435 570
duke@435 571 mark_and_move_for_policy(OP_favor_startup, ik->as_klassOop()->klass(), _move_rw);
duke@435 572 mark_and_move_for_policy(OP_favor_startup, ik->constants()->klass(), _move_rw);
duke@435 573
duke@435 574 // Although Java mirrors are marked in MarkReadWriteObjects,
duke@435 575 // apparently they were never moved into shared spaces since
duke@435 576 // MoveMarkedObjects skips marked instance oops. This may
duke@435 577 // be a bug in the original implementation or simply the vestige
duke@435 578 // of an abandoned experiment. Nevertheless we leave a hint
duke@435 579 // here in case this capability is ever correctly implemented.
duke@435 580 //
duke@435 581 // mark_and_move_for_policy(OP_favor_runtime, ik->java_mirror(), _move_rw);
duke@435 582 }
duke@435 583 }
duke@435 584
duke@435 585 };
duke@435 586
duke@435 587 // Adjust references in oops to refer to shared spaces.
duke@435 588
duke@435 589 class ResolveForwardingClosure: public OopClosure {
duke@435 590 public:
duke@435 591 void do_oop(oop* p) {
duke@435 592 oop obj = *p;
duke@435 593 if (!obj->is_shared()) {
duke@435 594 if (obj != NULL) {
duke@435 595 oop f = obj->forwardee();
duke@435 596 guarantee(f->is_shared(), "Oop doesn't refer to shared space.");
duke@435 597 *p = f;
duke@435 598 }
duke@435 599 }
duke@435 600 }
coleenp@548 601 void do_oop(narrowOop* pobj) { ShouldNotReachHere(); }
duke@435 602 };
duke@435 603
duke@435 604
coleenp@2497 605 // The methods array must be reordered by Symbol* address.
coleenp@2497 606 // (See classFileParser.cpp where methods in a class are originally
coleenp@2497 607 // sorted). The addresses of symbols have been changed as a result
coleenp@2497 608 // of moving to the shared space.
duke@435 609
duke@435 610 class SortMethodsClosure: public ObjectClosure {
coleenp@2497 611 public:
coleenp@2497 612 void do_object(oop obj) {
coleenp@2497 613 if (obj->blueprint()->oop_is_instanceKlass()) {
coleenp@2497 614 instanceKlass* ik = instanceKlass::cast((klassOop)obj);
coleenp@2497 615 methodOopDesc::sort_methods(ik->methods(),
coleenp@2497 616 ik->methods_annotations(),
coleenp@2497 617 ik->methods_parameter_annotations(),
coleenp@2497 618 ik->methods_default_annotations(),
coleenp@2497 619 true /* idempotent, slow */);
coleenp@2497 620 }
coleenp@2497 621 }
coleenp@2497 622 };
coleenp@2497 623
coleenp@2777 624 // Vtable and Itable indices are calculated based on methods array
coleenp@2777 625 // order (see klassItable::compute_itable_index()). Must reinitialize
coleenp@2497 626 // after ALL methods of ALL classes have been reordered.
coleenp@2497 627 // We assume that since checkconstraints is false, this method
coleenp@2497 628 // cannot throw an exception. An exception here would be
coleenp@2497 629 // problematic since this is the VMThread, not a JavaThread.
coleenp@2497 630
coleenp@2777 631 class ReinitializeTables: public ObjectClosure {
duke@435 632 private:
duke@435 633 Thread* _thread;
duke@435 634
duke@435 635 public:
coleenp@2777 636 ReinitializeTables(Thread* thread) : _thread(thread) {}
coleenp@2777 637
coleenp@2777 638 // Initialize super vtable first, check if already initialized to avoid
coleenp@2777 639 // quadradic behavior. The vtable is cleared in remove_unshareable_info.
coleenp@2777 640 void reinitialize_vtables(klassOop k) {
coleenp@2777 641 if (k->blueprint()->oop_is_instanceKlass()) {
coleenp@2777 642 instanceKlass* ik = instanceKlass::cast(k);
coleenp@2777 643 if (ik->vtable()->is_initialized()) return;
coleenp@2777 644 if (ik->super() != NULL) {
coleenp@2777 645 reinitialize_vtables(ik->super());
coleenp@2777 646 }
coleenp@2777 647 ik->vtable()->initialize_vtable(false, _thread);
coleenp@2777 648 }
coleenp@2777 649 }
duke@435 650
duke@435 651 void do_object(oop obj) {
duke@435 652 if (obj->blueprint()->oop_is_instanceKlass()) {
duke@435 653 instanceKlass* ik = instanceKlass::cast((klassOop)obj);
coleenp@2777 654 ResourceMark rm(_thread);
coleenp@2497 655 ik->itable()->initialize_itable(false, _thread);
coleenp@2777 656 reinitialize_vtables((klassOop)obj);
coleenp@2777 657 #ifdef ASSERT
coleenp@2777 658 ik->vtable()->verify(tty, true);
coleenp@2777 659 #endif // ASSERT
coleenp@2777 660 } else if (obj->blueprint()->oop_is_arrayKlass()) {
coleenp@2777 661 // The vtable for array klasses are that of its super class,
coleenp@2777 662 // ie. java.lang.Object.
coleenp@2777 663 arrayKlass* ak = arrayKlass::cast((klassOop)obj);
coleenp@2777 664 if (ak->vtable()->is_initialized()) return;
coleenp@2777 665 ak->vtable()->initialize_vtable(false, _thread);
duke@435 666 }
duke@435 667 }
duke@435 668 };
duke@435 669
duke@435 670
duke@435 671 // Adjust references in oops to refer to shared spaces.
duke@435 672
duke@435 673 class PatchOopsClosure: public ObjectClosure {
duke@435 674 private:
duke@435 675 Thread* _thread;
duke@435 676 ResolveForwardingClosure resolve;
duke@435 677
duke@435 678 public:
duke@435 679 PatchOopsClosure(Thread* thread) : _thread(thread) {}
duke@435 680
duke@435 681 void do_object(oop obj) {
duke@435 682 obj->oop_iterate_header(&resolve);
duke@435 683 obj->oop_iterate(&resolve);
duke@435 684
duke@435 685 assert(obj->klass()->is_shared(), "Klass not pointing into shared space.");
duke@435 686
duke@435 687 // If the object is a Java object or class which might (in the
duke@435 688 // future) contain a reference to a young gen object, add it to the
duke@435 689 // list.
duke@435 690
duke@435 691 if (obj->is_klass() || obj->is_instance()) {
duke@435 692 if (obj->is_klass() ||
never@1577 693 obj->is_a(SystemDictionary::Class_klass()) ||
never@1577 694 obj->is_a(SystemDictionary::Throwable_klass())) {
duke@435 695 // Do nothing
duke@435 696 }
never@1577 697 else if (obj->is_a(SystemDictionary::String_klass())) {
duke@435 698 // immutable objects.
duke@435 699 } else {
duke@435 700 // someone added an object we hadn't accounted for.
duke@435 701 ShouldNotReachHere();
duke@435 702 }
duke@435 703 }
duke@435 704 }
duke@435 705 };
duke@435 706
duke@435 707
duke@435 708 // Empty the young and old generations.
duke@435 709
duke@435 710 class ClearSpaceClosure : public SpaceClosure {
duke@435 711 public:
duke@435 712 void do_space(Space* s) {
jmasa@698 713 s->clear(SpaceDecorator::Mangle);
duke@435 714 }
duke@435 715 };
duke@435 716
duke@435 717
duke@435 718 // Closure for serializing initialization data out to a data area to be
duke@435 719 // written to the shared file.
duke@435 720
duke@435 721 class WriteClosure : public SerializeOopClosure {
duke@435 722 private:
duke@435 723 oop* top;
duke@435 724 char* end;
duke@435 725
duke@435 726 inline void check_space() {
duke@435 727 if ((char*)top + sizeof(oop) > end) {
coleenp@2497 728 report_out_of_shared_space(SharedMiscData);
duke@435 729 }
duke@435 730 }
duke@435 731
duke@435 732
duke@435 733 public:
duke@435 734 WriteClosure(char* md_top, char* md_end) {
duke@435 735 top = (oop*)md_top;
duke@435 736 end = md_end;
duke@435 737 }
duke@435 738
duke@435 739 char* get_top() { return (char*)top; }
duke@435 740
duke@435 741 void do_oop(oop* p) {
duke@435 742 check_space();
duke@435 743 oop obj = *p;
duke@435 744 assert(obj->is_oop_or_null(), "invalid oop");
duke@435 745 assert(obj == NULL || obj->is_shared(),
duke@435 746 "Oop in shared space not pointing into shared space.");
duke@435 747 *top = obj;
duke@435 748 ++top;
duke@435 749 }
duke@435 750
coleenp@548 751 void do_oop(narrowOop* pobj) { ShouldNotReachHere(); }
coleenp@548 752
duke@435 753 void do_int(int* p) {
duke@435 754 check_space();
duke@435 755 *top = (oop)(intptr_t)*p;
duke@435 756 ++top;
duke@435 757 }
duke@435 758
duke@435 759 void do_size_t(size_t* p) {
duke@435 760 check_space();
duke@435 761 *top = (oop)(intptr_t)*p;
duke@435 762 ++top;
duke@435 763 }
duke@435 764
duke@435 765 void do_ptr(void** p) {
duke@435 766 check_space();
duke@435 767 *top = (oop)*p;
duke@435 768 ++top;
duke@435 769 }
duke@435 770
duke@435 771 void do_ptr(HeapWord** p) { do_ptr((void **) p); }
duke@435 772
duke@435 773 void do_tag(int tag) {
duke@435 774 check_space();
duke@435 775 *top = (oop)(intptr_t)tag;
duke@435 776 ++top;
duke@435 777 }
duke@435 778
duke@435 779 void do_region(u_char* start, size_t size) {
duke@435 780 if ((char*)top + size > end) {
coleenp@2497 781 report_out_of_shared_space(SharedMiscData);
duke@435 782 }
duke@435 783 assert((intptr_t)start % sizeof(oop) == 0, "bad alignment");
duke@435 784 assert(size % sizeof(oop) == 0, "bad size");
duke@435 785 do_tag((int)size);
duke@435 786 while (size > 0) {
duke@435 787 *top = *(oop*)start;
duke@435 788 ++top;
duke@435 789 start += sizeof(oop);
duke@435 790 size -= sizeof(oop);
duke@435 791 }
duke@435 792 }
duke@435 793
duke@435 794 bool reading() const { return false; }
duke@435 795 };
duke@435 796
duke@435 797
duke@435 798 class ResolveConstantPoolsClosure : public ObjectClosure {
duke@435 799 private:
duke@435 800 TRAPS;
duke@435 801 public:
duke@435 802 ResolveConstantPoolsClosure(Thread *t) {
duke@435 803 __the_thread__ = t;
duke@435 804 }
duke@435 805 void do_object(oop obj) {
duke@435 806 if (obj->is_constantPool()) {
duke@435 807 constantPoolOop cpool = (constantPoolOop)obj;
duke@435 808 int unresolved = cpool->pre_resolve_shared_klasses(THREAD);
duke@435 809 }
duke@435 810 }
duke@435 811 };
duke@435 812
duke@435 813
duke@435 814 // Print a summary of the contents of the read/write spaces to help
duke@435 815 // identify objects which might be able to be made read-only. At this
duke@435 816 // point, the objects have been written, and we can trash them as
duke@435 817 // needed.
duke@435 818
duke@435 819 static void print_contents() {
duke@435 820 if (PrintSharedSpaces) {
duke@435 821 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 822 CompactingPermGenGen* gen = (CompactingPermGenGen*)gch->perm_gen();
duke@435 823
duke@435 824 // High level summary of the read-only space:
duke@435 825
duke@435 826 ClassifyObjectClosure coc;
duke@435 827 tty->cr(); tty->print_cr("ReadOnly space:");
duke@435 828 gen->ro_space()->object_iterate(&coc);
duke@435 829 coc.print();
duke@435 830
duke@435 831 // High level summary of the read-write space:
duke@435 832
duke@435 833 coc.reset();
duke@435 834 tty->cr(); tty->print_cr("ReadWrite space:");
duke@435 835 gen->rw_space()->object_iterate(&coc);
duke@435 836 coc.print();
duke@435 837
duke@435 838 // Reset counters
duke@435 839
duke@435 840 ClearAllocCountClosure cacc;
duke@435 841 gen->ro_space()->object_iterate(&cacc);
duke@435 842 gen->rw_space()->object_iterate(&cacc);
duke@435 843 coc.reset();
duke@435 844
duke@435 845 // Lower level summary of the read-only space:
duke@435 846
duke@435 847 gen->ro_space()->object_iterate(&coc);
duke@435 848 tty->cr(); tty->print_cr("ReadOnly space:");
duke@435 849 ClassifyInstanceKlassClosure cikc;
duke@435 850 gen->rw_space()->object_iterate(&cikc);
duke@435 851 cikc.print();
duke@435 852
duke@435 853 // Reset counters
duke@435 854
duke@435 855 gen->ro_space()->object_iterate(&cacc);
duke@435 856 gen->rw_space()->object_iterate(&cacc);
duke@435 857 coc.reset();
duke@435 858
duke@435 859 // Lower level summary of the read-write space:
duke@435 860
duke@435 861 gen->rw_space()->object_iterate(&coc);
duke@435 862 cikc.reset();
duke@435 863 tty->cr(); tty->print_cr("ReadWrite space:");
duke@435 864 gen->rw_space()->object_iterate(&cikc);
duke@435 865 cikc.print();
duke@435 866 }
duke@435 867 }
duke@435 868
duke@435 869
duke@435 870 // Patch C++ vtable pointer in klass oops.
duke@435 871
duke@435 872 // Klass objects contain references to c++ vtables in the JVM library.
duke@435 873 // Fix them to point to our constructed vtables. However, don't iterate
duke@435 874 // across the space while doing this, as that causes the vtables to be
duke@435 875 // patched, undoing our useful work. Instead, iterate to make a list,
duke@435 876 // then use the list to do the fixing.
acorn@843 877 //
acorn@843 878 // Our constructed vtables:
acorn@843 879 // Dump time:
acorn@843 880 // 1. init_self_patching_vtbl_list: table of pointers to current virtual method addrs
acorn@843 881 // 2. generate_vtable_methods: create jump table, appended to above vtbl_list
acorn@843 882 // 3. PatchKlassVtables: for Klass list, patch the vtable entry to point to jump table
acorn@843 883 // rather than to current vtbl
acorn@843 884 // Table layout: NOTE FIXED SIZE
acorn@843 885 // 1. vtbl pointers
acorn@843 886 // 2. #Klass X #virtual methods per Klass
acorn@843 887 // 1 entry for each, in the order:
acorn@843 888 // Klass1:method1 entry, Klass1:method2 entry, ... Klass1:method<num_virtuals> entry
acorn@843 889 // Klass2:method1 entry, Klass2:method2 entry, ... Klass2:method<num_virtuals> entry
acorn@843 890 // ...
acorn@843 891 // Klass<vtbl_list_size>:method1 entry, Klass<vtbl_list_size>:method2 entry,
acorn@843 892 // ... Klass<vtbl_list_size>:method<num_virtuals> entry
acorn@843 893 // Sample entry: (Sparc):
acorn@843 894 // save(sp, -256, sp)
acorn@843 895 // ba,pt common_code
acorn@843 896 // mov XXX, %L0 %L0 gets: Klass index <<8 + method index (note: max method index 255)
acorn@843 897 //
acorn@843 898 // Restore time:
acorn@843 899 // 1. initialize_oops: reserve space for table
acorn@843 900 // 2. init_self_patching_vtbl_list: update pointers to NEW virtual method addrs in text
acorn@843 901 //
acorn@843 902 // Execution time:
acorn@843 903 // First virtual method call for any object of these Klass types:
acorn@843 904 // 1. object->klass->klass_part
acorn@843 905 // 2. vtable entry for that klass_part points to the jump table entries
acorn@843 906 // 3. branches to common_code with %O0/klass_part, %L0: Klass index <<8 + method index
acorn@843 907 // 4. common_code:
acorn@843 908 // Get address of new vtbl pointer for this Klass from updated table
acorn@843 909 // Update new vtbl pointer in the Klass: future virtual calls go direct
acorn@843 910 // Jump to method, using new vtbl pointer and method index
duke@435 911
duke@435 912 class PatchKlassVtables: public ObjectClosure {
duke@435 913 private:
duke@435 914 GrowableArray<klassOop>* _klass_objects;
duke@435 915
duke@435 916 public:
coleenp@2497 917 PatchKlassVtables() {
duke@435 918 _klass_objects = new GrowableArray<klassOop>();
duke@435 919 }
duke@435 920
duke@435 921 void do_object(oop obj) {
duke@435 922 if (obj->is_klass()) {
duke@435 923 _klass_objects->append(klassOop(obj));
duke@435 924 }
duke@435 925 }
duke@435 926
coleenp@2497 927 void patch(void** vtbl_list, void* new_vtable_start) {
coleenp@2497 928 int n = _klass_objects->length();
coleenp@2497 929 for (int i = 0; i < n; i++) {
duke@435 930 klassOop obj = (klassOop)_klass_objects->at(i);
duke@435 931 Klass* k = obj->klass_part();
coleenp@2497 932 *(void**)k = CompactingPermGenGen::find_matching_vtbl_ptr(
coleenp@2497 933 vtbl_list, new_vtable_start, k);
duke@435 934 }
duke@435 935 }
duke@435 936 };
duke@435 937
coleenp@2497 938 // Walk through all symbols and patch their vtable pointers.
coleenp@2497 939 // Note that symbols have vtable pointers only in non-product builds
coleenp@2497 940 // (see allocation.hpp).
coleenp@2497 941
coleenp@2497 942 #ifndef PRODUCT
coleenp@2497 943 class PatchSymbolVtables: public SymbolClosure {
coleenp@2497 944 private:
coleenp@2497 945 void* _new_vtbl_ptr;
coleenp@2497 946
coleenp@2497 947 public:
coleenp@2497 948 PatchSymbolVtables(void** vtbl_list, void* new_vtable_start) {
coleenp@2497 949 Symbol s;
coleenp@2497 950 _new_vtbl_ptr = CompactingPermGenGen::find_matching_vtbl_ptr(
coleenp@2497 951 vtbl_list, new_vtable_start, &s);
coleenp@2497 952 }
coleenp@2497 953
coleenp@2497 954 void do_symbol(Symbol** p) {
coleenp@2497 955 Symbol* sym = load_symbol(p);
coleenp@2497 956 *(void**)sym = _new_vtbl_ptr;
coleenp@2497 957 }
coleenp@2497 958 };
coleenp@2497 959 #endif
coleenp@2497 960
duke@435 961
duke@435 962 // Populate the shared space.
duke@435 963
duke@435 964 class VM_PopulateDumpSharedSpace: public VM_Operation {
duke@435 965 private:
duke@435 966 GrowableArray<oop> *_class_promote_order;
duke@435 967 OffsetTableContigSpace* _ro_space;
duke@435 968 OffsetTableContigSpace* _rw_space;
duke@435 969 VirtualSpace* _md_vs;
duke@435 970 VirtualSpace* _mc_vs;
duke@435 971
duke@435 972 public:
duke@435 973 VM_PopulateDumpSharedSpace(GrowableArray<oop> *class_promote_order,
duke@435 974 OffsetTableContigSpace* ro_space,
duke@435 975 OffsetTableContigSpace* rw_space,
duke@435 976 VirtualSpace* md_vs, VirtualSpace* mc_vs) {
duke@435 977 _class_promote_order = class_promote_order;
duke@435 978 _ro_space = ro_space;
duke@435 979 _rw_space = rw_space;
duke@435 980 _md_vs = md_vs;
duke@435 981 _mc_vs = mc_vs;
duke@435 982 }
duke@435 983
duke@435 984 VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
duke@435 985 void doit() {
duke@435 986 Thread* THREAD = VMThread::vm_thread();
duke@435 987 NOT_PRODUCT(SystemDictionary::verify();)
duke@435 988 // The following guarantee is meant to ensure that no loader constraints
duke@435 989 // exist yet, since the constraints table is not shared. This becomes
duke@435 990 // more important now that we don't re-initialize vtables/itables for
duke@435 991 // shared classes at runtime, where constraints were previously created.
duke@435 992 guarantee(SystemDictionary::constraints()->number_of_entries() == 0,
duke@435 993 "loader constraints are not saved");
jrose@1145 994 // Revisit and implement this if we prelink method handle call sites:
never@1149 995 guarantee(SystemDictionary::invoke_method_table() == NULL ||
never@1149 996 SystemDictionary::invoke_method_table()->number_of_entries() == 0,
jrose@1145 997 "invoke method table is not saved");
duke@435 998 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 999
duke@435 1000 // At this point, many classes have been loaded.
duke@435 1001
duke@435 1002 // Update all the fingerprints in the shared methods.
duke@435 1003
duke@435 1004 tty->print("Calculating fingerprints ... ");
duke@435 1005 FingerprintMethodsClosure fpmc;
duke@435 1006 gch->object_iterate(&fpmc);
duke@435 1007 tty->print_cr("done. ");
duke@435 1008
duke@435 1009 // Remove all references outside the heap.
duke@435 1010
duke@435 1011 tty->print("Removing unshareable information ... ");
duke@435 1012 RemoveUnshareableInfoClosure ruic;
duke@435 1013 gch->object_iterate(&ruic);
duke@435 1014 tty->print_cr("done. ");
duke@435 1015
duke@435 1016 // Move the objects in three passes.
duke@435 1017
duke@435 1018 MarkObjectsOopClosure mark_all;
duke@435 1019 MarkCommonReadOnly mark_common_ro;
duke@435 1020 MarkStringValues mark_string_values;
duke@435 1021 MarkReadWriteObjects mark_rw;
duke@435 1022 MarkStringObjects mark_strings;
duke@435 1023 MoveMarkedObjects move_ro(_ro_space, true);
duke@435 1024 MoveMarkedObjects move_rw(_rw_space, false);
duke@435 1025
duke@435 1026 // The SharedOptimizeColdStart VM option governs the new layout
duke@435 1027 // algorithm for promoting classes into the shared archive.
duke@435 1028 // The general idea is to minimize cold start time by laying
duke@435 1029 // out the objects in the order they are accessed at startup time.
duke@435 1030 // By doing this we are trying to eliminate out-of-order accesses
duke@435 1031 // in the shared archive. This benefits cold startup time by making
duke@435 1032 // disk reads as sequential as possible during class loading and
duke@435 1033 // bootstrapping activities. There may also be a small secondary
duke@435 1034 // effect of better "packing" of more commonly used data on a smaller
duke@435 1035 // number of pages, although no direct benefit has been measured from
duke@435 1036 // this effect.
duke@435 1037 //
duke@435 1038 // At the class level of granularity, the promotion order is dictated
duke@435 1039 // by the classlist file whose generation is discussed elsewhere.
duke@435 1040 //
duke@435 1041 // At smaller granularity, optimal ordering was determined by an
duke@435 1042 // offline analysis of object access order in the shared archive.
duke@435 1043 // The dbx watchpoint facility, combined with SA post-processing,
duke@435 1044 // was used to observe common access patterns primarily during
duke@435 1045 // classloading. This information was used to craft the promotion
duke@435 1046 // order seen in the following closures.
duke@435 1047 //
duke@435 1048 // The observed access order is mostly governed by what happens
duke@435 1049 // in SystemDictionary::load_shared_class(). NOTE WELL - care
duke@435 1050 // should be taken when making changes to this method, because it
duke@435 1051 // may invalidate assumptions made about access order!
duke@435 1052 //
duke@435 1053 // (Ideally, there would be a better way to manage changes to
duke@435 1054 // the access order. Unfortunately a generic in-VM solution for
duke@435 1055 // dynamically observing access order and optimizing shared
duke@435 1056 // archive layout is pretty difficult. We go with the static
duke@435 1057 // analysis because the code is fairly mature at this point
duke@435 1058 // and we're betting that the access order won't change much.)
duke@435 1059
duke@435 1060 MarkAndMoveOrderedReadOnly mark_and_move_ordered_ro(&move_ro);
duke@435 1061 MarkAndMoveOrderedReadWrite mark_and_move_ordered_rw(&move_rw);
duke@435 1062
duke@435 1063 // Set up the share data and shared code segments.
duke@435 1064
duke@435 1065 char* md_top = _md_vs->low();
duke@435 1066 char* md_end = _md_vs->high();
duke@435 1067 char* mc_top = _mc_vs->low();
duke@435 1068 char* mc_end = _mc_vs->high();
duke@435 1069
duke@435 1070 // Reserve space for the list of klassOops whose vtables are used
duke@435 1071 // for patching others as needed.
duke@435 1072
duke@435 1073 void** vtbl_list = (void**)md_top;
duke@435 1074 int vtbl_list_size = CompactingPermGenGen::vtbl_list_size;
duke@435 1075 Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size);
duke@435 1076
duke@435 1077 md_top += vtbl_list_size * sizeof(void*);
duke@435 1078 void* vtable = md_top;
duke@435 1079
duke@435 1080 // Reserve space for a new dummy vtable for klass objects in the
duke@435 1081 // heap. Generate self-patching vtable entries.
duke@435 1082
duke@435 1083 CompactingPermGenGen::generate_vtable_methods(vtbl_list,
duke@435 1084 &vtable,
duke@435 1085 &md_top, md_end,
duke@435 1086 &mc_top, mc_end);
duke@435 1087
coleenp@2497 1088 // Reserve space for the total size and the number of stored symbols.
coleenp@2497 1089
coleenp@2497 1090 md_top += sizeof(intptr_t) * 2;
coleenp@2497 1091
coleenp@2497 1092 MoveSymbols move_symbols(md_top, md_end);
coleenp@2497 1093 CommonSymbolsClosure traverse_common_symbols(&move_symbols);
coleenp@2497 1094
coleenp@2497 1095 // Phase 1a: remove symbols with _refcount == 0
coleenp@2497 1096
coleenp@2497 1097 SymbolTable::unlink();
coleenp@2497 1098
coleenp@2497 1099 // Phase 1b: move commonly used symbols referenced by oop fields.
coleenp@2497 1100
coleenp@2497 1101 tty->print("Moving common symbols to metadata section at " PTR_FORMAT " ... ",
coleenp@2497 1102 move_symbols.get_top());
coleenp@2497 1103 gch->object_iterate(&traverse_common_symbols);
coleenp@2497 1104 tty->print_cr("done. ");
coleenp@2497 1105
coleenp@2497 1106 // Phase 1c: move known names and signatures.
coleenp@2497 1107
coleenp@2497 1108 tty->print("Moving vmSymbols to metadata section at " PTR_FORMAT " ... ",
coleenp@2497 1109 move_symbols.get_top());
coleenp@2497 1110 vmSymbols::symbols_do(&move_symbols);
coleenp@2497 1111 tty->print_cr("done. ");
coleenp@2497 1112
coleenp@2497 1113 // Phase 1d: move the remaining symbols by scanning the whole SymbolTable.
coleenp@2497 1114
coleenp@2497 1115 void* extra_symbols = move_symbols.get_top();
coleenp@2497 1116 tty->print("Moving the remaining symbols to metadata section at " PTR_FORMAT " ... ",
coleenp@2497 1117 move_symbols.get_top());
coleenp@2497 1118 SymbolTable::symbols_do(&move_symbols);
coleenp@2497 1119 tty->print_cr("done. ");
coleenp@2497 1120
coleenp@2497 1121 // Record the total length of all symbols at the beginning of the block.
coleenp@2497 1122 ((intptr_t*)md_top)[-2] = move_symbols.get_top() - md_top;
coleenp@2497 1123 ((intptr_t*)md_top)[-1] = move_symbols.count();
coleenp@2497 1124 tty->print_cr("Moved %d symbols, %d bytes.",
coleenp@2497 1125 move_symbols.count(), move_symbols.get_top() - md_top);
coleenp@2497 1126 // Advance the pointer to the end of symbol store.
coleenp@2497 1127 md_top = move_symbols.get_top();
coleenp@2497 1128
coleenp@2497 1129
coleenp@2497 1130 // Phase 2: move commonly used read-only objects to the read-only space.
coleenp@2497 1131
coleenp@2497 1132 if (SharedOptimizeColdStart) {
coleenp@2497 1133 tty->print("Moving pre-ordered read-only objects to shared space at " PTR_FORMAT " ... ",
coleenp@2497 1134 _ro_space->top());
coleenp@2497 1135 for (int i = 0; i < _class_promote_order->length(); i++) {
coleenp@2497 1136 oop obj = _class_promote_order->at(i);
coleenp@2497 1137 mark_and_move_ordered_ro.do_object(obj);
coleenp@2497 1138 }
coleenp@2497 1139 tty->print_cr("done. ");
coleenp@2497 1140 }
coleenp@2497 1141
coleenp@2497 1142 tty->print("Moving read-only objects to shared space at " PTR_FORMAT " ... ",
coleenp@2497 1143 _ro_space->top());
coleenp@2497 1144 gch->object_iterate(&mark_common_ro);
coleenp@2497 1145 gch->object_iterate(&move_ro);
coleenp@2497 1146 tty->print_cr("done. ");
coleenp@2497 1147
coleenp@2497 1148 // Phase 3: move String character arrays to the read-only space.
coleenp@2497 1149
coleenp@2497 1150 tty->print("Moving string char arrays to shared space at " PTR_FORMAT " ... ",
coleenp@2497 1151 _ro_space->top());
coleenp@2497 1152 gch->object_iterate(&mark_string_values);
coleenp@2497 1153 gch->object_iterate(&move_ro);
coleenp@2497 1154 tty->print_cr("done. ");
coleenp@2497 1155
coleenp@2497 1156 // Phase 4: move read-write objects to the read-write space, except
coleenp@2497 1157 // Strings.
coleenp@2497 1158
coleenp@2497 1159 if (SharedOptimizeColdStart) {
coleenp@2497 1160 tty->print("Moving pre-ordered read-write objects to shared space at " PTR_FORMAT " ... ",
coleenp@2497 1161 _rw_space->top());
coleenp@2497 1162 for (int i = 0; i < _class_promote_order->length(); i++) {
coleenp@2497 1163 oop obj = _class_promote_order->at(i);
coleenp@2497 1164 mark_and_move_ordered_rw.do_object(obj);
coleenp@2497 1165 }
coleenp@2497 1166 tty->print_cr("done. ");
coleenp@2497 1167 }
coleenp@2497 1168 tty->print("Moving read-write objects to shared space at " PTR_FORMAT " ... ",
coleenp@2497 1169 _rw_space->top());
coleenp@2497 1170 Universe::oops_do(&mark_all, true);
coleenp@2497 1171 SystemDictionary::oops_do(&mark_all);
coleenp@2497 1172 oop tmp = Universe::arithmetic_exception_instance();
coleenp@2497 1173 mark_object(java_lang_Throwable::message(tmp));
coleenp@2497 1174 gch->object_iterate(&mark_rw);
coleenp@2497 1175 gch->object_iterate(&move_rw);
coleenp@2497 1176 tty->print_cr("done. ");
coleenp@2497 1177
coleenp@2497 1178 // Phase 5: move String objects to the read-write space.
coleenp@2497 1179
coleenp@2497 1180 tty->print("Moving String objects to shared space at " PTR_FORMAT " ... ",
coleenp@2497 1181 _rw_space->top());
coleenp@2497 1182 StringTable::oops_do(&mark_all);
coleenp@2497 1183 gch->object_iterate(&mark_strings);
coleenp@2497 1184 gch->object_iterate(&move_rw);
coleenp@2497 1185 tty->print_cr("done. ");
coleenp@2497 1186 tty->print_cr("Read-write space ends at " PTR_FORMAT ", %d bytes.",
coleenp@2497 1187 _rw_space->top(), _rw_space->used());
coleenp@2497 1188
coleenp@2497 1189 #ifdef DEBUG
coleenp@2497 1190 // Check: scan for objects which were not moved.
coleenp@2497 1191
coleenp@2497 1192 CheckRemainingObjects check_objects;
coleenp@2497 1193 gch->object_iterate(&check_objects);
coleenp@2497 1194 check_objects.status();
coleenp@2497 1195 #endif
coleenp@2497 1196
coleenp@2497 1197 // Resolve forwarding in objects and saved C++ structures
coleenp@2497 1198 tty->print("Updating references to shared objects ... ");
coleenp@2497 1199 ResolveForwardingClosure resolve;
coleenp@2497 1200 Universe::oops_do(&resolve);
coleenp@2497 1201 SystemDictionary::oops_do(&resolve);
coleenp@2497 1202 StringTable::oops_do(&resolve);
coleenp@2497 1203
duke@435 1204 // Fix (forward) all of the references in these shared objects (which
duke@435 1205 // are required to point ONLY to objects in the shared spaces).
duke@435 1206 // Also, create a list of all objects which might later contain a
duke@435 1207 // reference to a younger generation object.
duke@435 1208
duke@435 1209 CompactingPermGenGen* gen = (CompactingPermGenGen*)gch->perm_gen();
duke@435 1210 PatchOopsClosure patch(THREAD);
duke@435 1211 gen->ro_space()->object_iterate(&patch);
duke@435 1212 gen->rw_space()->object_iterate(&patch);
duke@435 1213
duke@435 1214 // Previously method sorting was done concurrently with forwarding
duke@435 1215 // pointer resolution in the shared spaces. This imposed an ordering
duke@435 1216 // restriction in that methods were required to be promoted/patched
duke@435 1217 // before their holder classes. (Because constant pool pointers in
duke@435 1218 // methodKlasses are required to be resolved before their holder class
duke@435 1219 // is visited for sorting, otherwise methods are sorted by incorrect,
duke@435 1220 // pre-forwarding addresses.)
duke@435 1221 //
duke@435 1222 // Now, we reorder methods as a separate step after ALL forwarding
duke@435 1223 // pointer resolution, so that methods can be promoted in any order
duke@435 1224 // with respect to their holder classes.
duke@435 1225
coleenp@2497 1226 SortMethodsClosure sort;
duke@435 1227 gen->ro_space()->object_iterate(&sort);
duke@435 1228 gen->rw_space()->object_iterate(&sort);
coleenp@2497 1229
coleenp@2777 1230 ReinitializeTables reinit_tables(THREAD);
coleenp@2777 1231 gen->ro_space()->object_iterate(&reinit_tables);
coleenp@2777 1232 gen->rw_space()->object_iterate(&reinit_tables);
duke@435 1233 tty->print_cr("done. ");
duke@435 1234 tty->cr();
duke@435 1235
duke@435 1236 // Reorder the system dictionary. (Moving the symbols opps affects
duke@435 1237 // how the hash table indices are calculated.)
duke@435 1238
duke@435 1239 SystemDictionary::reorder_dictionary();
duke@435 1240
duke@435 1241 // Empty the non-shared heap (because most of the objects were
duke@435 1242 // copied out, and the remainder cannot be considered valid oops).
duke@435 1243
duke@435 1244 ClearSpaceClosure csc;
duke@435 1245 for (int i = 0; i < gch->n_gens(); ++i) {
duke@435 1246 gch->get_gen(i)->space_iterate(&csc);
duke@435 1247 }
duke@435 1248 csc.do_space(gen->the_space());
duke@435 1249 NOT_PRODUCT(SystemDictionary::verify();)
duke@435 1250
duke@435 1251 // Copy the String table, the symbol table, and the system
duke@435 1252 // dictionary to the shared space in usable form. Copy the hastable
duke@435 1253 // buckets first [read-write], then copy the linked lists of entries
duke@435 1254 // [read-only].
duke@435 1255
duke@435 1256 SymbolTable::reverse(extra_symbols);
duke@435 1257 NOT_PRODUCT(SymbolTable::verify());
duke@435 1258 SymbolTable::copy_buckets(&md_top, md_end);
duke@435 1259
duke@435 1260 StringTable::reverse();
duke@435 1261 NOT_PRODUCT(StringTable::verify());
duke@435 1262 StringTable::copy_buckets(&md_top, md_end);
duke@435 1263
duke@435 1264 SystemDictionary::reverse();
duke@435 1265 SystemDictionary::copy_buckets(&md_top, md_end);
duke@435 1266
duke@435 1267 ClassLoader::verify();
duke@435 1268 ClassLoader::copy_package_info_buckets(&md_top, md_end);
duke@435 1269 ClassLoader::verify();
duke@435 1270
duke@435 1271 SymbolTable::copy_table(&md_top, md_end);
duke@435 1272 StringTable::copy_table(&md_top, md_end);
duke@435 1273 SystemDictionary::copy_table(&md_top, md_end);
duke@435 1274 ClassLoader::verify();
duke@435 1275 ClassLoader::copy_package_info_table(&md_top, md_end);
duke@435 1276 ClassLoader::verify();
duke@435 1277
duke@435 1278 // Print debug data.
duke@435 1279
duke@435 1280 if (PrintSharedSpaces) {
duke@435 1281 const char* fmt = "%s space: " PTR_FORMAT " out of " PTR_FORMAT " bytes allocated at " PTR_FORMAT ".";
duke@435 1282 tty->print_cr(fmt, "ro", _ro_space->used(), _ro_space->capacity(),
duke@435 1283 _ro_space->bottom());
duke@435 1284 tty->print_cr(fmt, "rw", _rw_space->used(), _rw_space->capacity(),
duke@435 1285 _rw_space->bottom());
duke@435 1286 }
duke@435 1287
duke@435 1288 // Write the oop data to the output array.
duke@435 1289
duke@435 1290 WriteClosure wc(md_top, md_end);
duke@435 1291 CompactingPermGenGen::serialize_oops(&wc);
duke@435 1292 md_top = wc.get_top();
duke@435 1293
duke@435 1294 // Update the vtable pointers in all of the Klass objects in the
duke@435 1295 // heap. They should point to newly generated vtable.
duke@435 1296
coleenp@2497 1297 PatchKlassVtables pkvt;
duke@435 1298 _rw_space->object_iterate(&pkvt);
coleenp@2497 1299 pkvt.patch(vtbl_list, vtable);
coleenp@2497 1300
coleenp@2497 1301 #ifndef PRODUCT
coleenp@2497 1302 // Update the vtable pointers in all symbols,
coleenp@2497 1303 // but only in non-product builds where symbols DO have virtual methods.
coleenp@2497 1304 PatchSymbolVtables psvt(vtbl_list, vtable);
coleenp@2497 1305 SymbolTable::symbols_do(&psvt);
coleenp@2497 1306 #endif
duke@435 1307
duke@435 1308 char* saved_vtbl = (char*)malloc(vtbl_list_size * sizeof(void*));
duke@435 1309 memmove(saved_vtbl, vtbl_list, vtbl_list_size * sizeof(void*));
duke@435 1310 memset(vtbl_list, 0, vtbl_list_size * sizeof(void*));
duke@435 1311
duke@435 1312 // Create and write the archive file that maps the shared spaces.
duke@435 1313
duke@435 1314 FileMapInfo* mapinfo = new FileMapInfo();
duke@435 1315 mapinfo->populate_header(gch->gen_policy()->max_alignment());
duke@435 1316
duke@435 1317 // Pass 1 - update file offsets in header.
duke@435 1318 mapinfo->write_header();
duke@435 1319 mapinfo->write_space(CompactingPermGenGen::ro, _ro_space, true);
duke@435 1320 _ro_space->set_saved_mark();
duke@435 1321 mapinfo->write_space(CompactingPermGenGen::rw, _rw_space, false);
duke@435 1322 _rw_space->set_saved_mark();
duke@435 1323 mapinfo->write_region(CompactingPermGenGen::md, _md_vs->low(),
xlu@722 1324 pointer_delta(md_top, _md_vs->low(), sizeof(char)),
xlu@722 1325 SharedMiscDataSize,
duke@435 1326 false, false);
duke@435 1327 mapinfo->write_region(CompactingPermGenGen::mc, _mc_vs->low(),
xlu@722 1328 pointer_delta(mc_top, _mc_vs->low(), sizeof(char)),
xlu@722 1329 SharedMiscCodeSize,
duke@435 1330 true, true);
duke@435 1331
duke@435 1332 // Pass 2 - write data.
duke@435 1333 mapinfo->open_for_write();
duke@435 1334 mapinfo->write_header();
duke@435 1335 mapinfo->write_space(CompactingPermGenGen::ro, _ro_space, true);
duke@435 1336 mapinfo->write_space(CompactingPermGenGen::rw, _rw_space, false);
duke@435 1337 mapinfo->write_region(CompactingPermGenGen::md, _md_vs->low(),
xlu@722 1338 pointer_delta(md_top, _md_vs->low(), sizeof(char)),
xlu@722 1339 SharedMiscDataSize,
duke@435 1340 false, false);
duke@435 1341 mapinfo->write_region(CompactingPermGenGen::mc, _mc_vs->low(),
xlu@722 1342 pointer_delta(mc_top, _mc_vs->low(), sizeof(char)),
xlu@722 1343 SharedMiscCodeSize,
duke@435 1344 true, true);
duke@435 1345 mapinfo->close();
duke@435 1346
duke@435 1347 // Summarize heap.
duke@435 1348 memmove(vtbl_list, saved_vtbl, vtbl_list_size * sizeof(void*));
duke@435 1349 print_contents();
duke@435 1350 }
duke@435 1351 }; // class VM_PopulateDumpSharedSpace
duke@435 1352
duke@435 1353
duke@435 1354 // Populate the shared spaces and dump to a file.
duke@435 1355
duke@435 1356 jint CompactingPermGenGen::dump_shared(GrowableArray<oop>* class_promote_order, TRAPS) {
duke@435 1357 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 1358
duke@435 1359 // Calculate hash values for all of the (interned) strings to avoid
duke@435 1360 // writes to shared pages in the future.
duke@435 1361
duke@435 1362 tty->print("Calculating hash values for String objects .. ");
duke@435 1363 StringHashCodeClosure shcc(THREAD);
duke@435 1364 StringTable::oops_do(&shcc);
duke@435 1365 tty->print_cr("done. ");
duke@435 1366
duke@435 1367 CompactingPermGenGen* gen = (CompactingPermGenGen*)gch->perm_gen();
duke@435 1368 VM_PopulateDumpSharedSpace op(class_promote_order,
duke@435 1369 gen->ro_space(), gen->rw_space(),
duke@435 1370 gen->md_space(), gen->mc_space());
duke@435 1371 VMThread::execute(&op);
duke@435 1372 return JNI_OK;
duke@435 1373 }
duke@435 1374
coleenp@2497 1375 void* CompactingPermGenGen::find_matching_vtbl_ptr(void** vtbl_list,
coleenp@2497 1376 void* new_vtable_start,
coleenp@2497 1377 void* obj) {
coleenp@2497 1378 void* old_vtbl_ptr = *(void**)obj;
coleenp@2497 1379 for (int i = 0; i < vtbl_list_size; i++) {
coleenp@2497 1380 if (vtbl_list[i] == old_vtbl_ptr) {
coleenp@2497 1381 return (void**)new_vtable_start + i * num_virtuals;
coleenp@2497 1382 }
coleenp@2497 1383 }
coleenp@2497 1384 ShouldNotReachHere();
coleenp@2497 1385 return NULL;
coleenp@2497 1386 }
coleenp@2497 1387
duke@435 1388
duke@435 1389 class LinkClassesClosure : public ObjectClosure {
duke@435 1390 private:
duke@435 1391 Thread* THREAD;
duke@435 1392
duke@435 1393 public:
duke@435 1394 LinkClassesClosure(Thread* thread) : THREAD(thread) {}
duke@435 1395
duke@435 1396 void do_object(oop obj) {
duke@435 1397 if (obj->is_klass()) {
duke@435 1398 Klass* k = Klass::cast((klassOop) obj);
duke@435 1399 if (k->oop_is_instance()) {
duke@435 1400 instanceKlass* ik = (instanceKlass*) k;
duke@435 1401 // Link the class to cause the bytecodes to be rewritten and the
duke@435 1402 // cpcache to be created.
coleenp@3368 1403 if (ik->init_state() < instanceKlass::linked) {
duke@435 1404 ik->link_class(THREAD);
duke@435 1405 guarantee(!HAS_PENDING_EXCEPTION, "exception in class rewriting");
duke@435 1406 }
duke@435 1407
duke@435 1408 // Create String objects from string initializer symbols.
duke@435 1409 ik->constants()->resolve_string_constants(THREAD);
duke@435 1410 guarantee(!HAS_PENDING_EXCEPTION, "exception resolving string constants");
duke@435 1411 }
duke@435 1412 }
duke@435 1413 }
duke@435 1414 };
duke@435 1415
duke@435 1416
duke@435 1417 // Support for a simple checksum of the contents of the class list
duke@435 1418 // file to prevent trivial tampering. The algorithm matches that in
duke@435 1419 // the MakeClassList program used by the J2SE build process.
duke@435 1420 #define JSUM_SEED ((jlong)CONST64(0xcafebabebabecafe))
duke@435 1421 static jlong
duke@435 1422 jsum(jlong start, const char *buf, const int len)
duke@435 1423 {
duke@435 1424 jlong h = start;
duke@435 1425 char *p = (char *)buf, *e = p + len;
duke@435 1426 while (p < e) {
duke@435 1427 char c = *p++;
duke@435 1428 if (c <= ' ') {
duke@435 1429 /* Skip spaces and control characters */
duke@435 1430 continue;
duke@435 1431 }
duke@435 1432 h = 31 * h + c;
duke@435 1433 }
duke@435 1434 return h;
duke@435 1435 }
duke@435 1436
duke@435 1437
duke@435 1438
duke@435 1439
duke@435 1440
duke@435 1441 // Preload classes from a list, populate the shared spaces and dump to a
duke@435 1442 // file.
duke@435 1443
duke@435 1444 void GenCollectedHeap::preload_and_dump(TRAPS) {
duke@435 1445 TraceTime timer("Dump Shared Spaces", TraceStartupTime);
duke@435 1446 ResourceMark rm;
duke@435 1447
duke@435 1448 // Preload classes to be shared.
ikrylov@2322 1449 // Should use some os:: method rather than fopen() here. aB.
duke@435 1450 // Construct the path to the class list (in jre/lib)
duke@435 1451 // Walk up two directories from the location of the VM and
duke@435 1452 // optionally tack on "lib" (depending on platform)
duke@435 1453 char class_list_path[JVM_MAXPATHLEN];
duke@435 1454 os::jvm_path(class_list_path, sizeof(class_list_path));
duke@435 1455 for (int i = 0; i < 3; i++) {
duke@435 1456 char *end = strrchr(class_list_path, *os::file_separator());
duke@435 1457 if (end != NULL) *end = '\0';
duke@435 1458 }
duke@435 1459 int class_list_path_len = (int)strlen(class_list_path);
duke@435 1460 if (class_list_path_len >= 3) {
duke@435 1461 if (strcmp(class_list_path + class_list_path_len - 3, "lib") != 0) {
duke@435 1462 strcat(class_list_path, os::file_separator());
duke@435 1463 strcat(class_list_path, "lib");
duke@435 1464 }
duke@435 1465 }
duke@435 1466 strcat(class_list_path, os::file_separator());
duke@435 1467 strcat(class_list_path, "classlist");
duke@435 1468
duke@435 1469 FILE* file = fopen(class_list_path, "r");
duke@435 1470 if (file != NULL) {
duke@435 1471 jlong computed_jsum = JSUM_SEED;
duke@435 1472 jlong file_jsum = 0;
duke@435 1473
duke@435 1474 char class_name[256];
duke@435 1475 int class_count = 0;
duke@435 1476 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 1477 gch->_preloading_shared_classes = true;
duke@435 1478 GrowableArray<oop>* class_promote_order = new GrowableArray<oop>();
duke@435 1479
duke@435 1480 // Preload (and intern) strings which will be used later.
duke@435 1481
duke@435 1482 StringTable::intern("main", THREAD);
duke@435 1483 StringTable::intern("([Ljava/lang/String;)V", THREAD);
duke@435 1484 StringTable::intern("Ljava/lang/Class;", THREAD);
duke@435 1485
duke@435 1486 StringTable::intern("I", THREAD); // Needed for StringBuffer persistence?
duke@435 1487 StringTable::intern("Z", THREAD); // Needed for StringBuffer persistence?
duke@435 1488
duke@435 1489 // sun.io.Converters
duke@435 1490 static const char obj_array_sig[] = "[[Ljava/lang/Object;";
duke@435 1491 SymbolTable::lookup(obj_array_sig, (int)strlen(obj_array_sig), THREAD);
duke@435 1492
duke@435 1493 // java.util.HashMap
duke@435 1494 static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;";
duke@435 1495 SymbolTable::lookup(map_entry_array_sig, (int)strlen(map_entry_array_sig),
duke@435 1496 THREAD);
duke@435 1497
duke@435 1498 tty->print("Loading classes to share ... ");
duke@435 1499 while ((fgets(class_name, sizeof class_name, file)) != NULL) {
duke@435 1500 if (*class_name == '#') {
duke@435 1501 jint fsh, fsl;
duke@435 1502 if (sscanf(class_name, "# %8x%8x\n", &fsh, &fsl) == 2) {
duke@435 1503 file_jsum = ((jlong)(fsh) << 32) | (fsl & 0xffffffff);
duke@435 1504 }
duke@435 1505
duke@435 1506 continue;
duke@435 1507 }
duke@435 1508 // Remove trailing newline
duke@435 1509 size_t name_len = strlen(class_name);
duke@435 1510 class_name[name_len-1] = '\0';
duke@435 1511
duke@435 1512 computed_jsum = jsum(computed_jsum, class_name, (const int)name_len - 1);
duke@435 1513
duke@435 1514 // Got a class name - load it.
coleenp@2497 1515 TempNewSymbol class_name_symbol = SymbolTable::new_symbol(class_name, THREAD);
duke@435 1516 guarantee(!HAS_PENDING_EXCEPTION, "Exception creating a symbol.");
duke@435 1517 klassOop klass = SystemDictionary::resolve_or_null(class_name_symbol,
duke@435 1518 THREAD);
duke@435 1519 guarantee(!HAS_PENDING_EXCEPTION, "Exception resolving a class.");
duke@435 1520 if (klass != NULL) {
duke@435 1521 if (PrintSharedSpaces) {
duke@435 1522 tty->print_cr("Shared spaces preloaded: %s", class_name);
duke@435 1523 }
duke@435 1524
duke@435 1525
duke@435 1526 instanceKlass* ik = instanceKlass::cast(klass);
duke@435 1527
duke@435 1528 // Should be class load order as per -XX:+TraceClassLoadingPreorder
duke@435 1529 class_promote_order->append(ik->as_klassOop());
duke@435 1530
duke@435 1531 // Link the class to cause the bytecodes to be rewritten and the
duke@435 1532 // cpcache to be created. The linking is done as soon as classes
duke@435 1533 // are loaded in order that the related data structures (klass,
duke@435 1534 // cpCache, Sting constants) are located together.
duke@435 1535
coleenp@3368 1536 if (ik->init_state() < instanceKlass::linked) {
duke@435 1537 ik->link_class(THREAD);
duke@435 1538 guarantee(!(HAS_PENDING_EXCEPTION), "exception in class rewriting");
duke@435 1539 }
duke@435 1540
duke@435 1541 // Create String objects from string initializer symbols.
duke@435 1542
duke@435 1543 ik->constants()->resolve_string_constants(THREAD);
duke@435 1544
duke@435 1545 class_count++;
duke@435 1546 } else {
duke@435 1547 if (PrintSharedSpaces) {
duke@435 1548 tty->cr();
duke@435 1549 tty->print_cr(" Preload failed: %s", class_name);
duke@435 1550 }
duke@435 1551 }
duke@435 1552 file_jsum = 0; // Checksum must be on last line of file
duke@435 1553 }
duke@435 1554 if (computed_jsum != file_jsum) {
duke@435 1555 tty->cr();
duke@435 1556 tty->print_cr("Preload failed: checksum of class list was incorrect.");
duke@435 1557 exit(1);
duke@435 1558 }
duke@435 1559
duke@435 1560 tty->print_cr("done. ");
duke@435 1561
duke@435 1562 if (PrintSharedSpaces) {
duke@435 1563 tty->print_cr("Shared spaces: preloaded %d classes", class_count);
duke@435 1564 }
duke@435 1565
duke@435 1566 // Rewrite and unlink classes.
duke@435 1567 tty->print("Rewriting and unlinking classes ... ");
duke@435 1568 // Make heap parsable
duke@435 1569 ensure_parsability(false); // arg is actually don't care
duke@435 1570
duke@435 1571 // Link any classes which got missed. (It's not quite clear why
duke@435 1572 // they got missed.) This iteration would be unsafe if we weren't
duke@435 1573 // single-threaded at this point; however we can't do it on the VM
duke@435 1574 // thread because it requires object allocation.
duke@435 1575 LinkClassesClosure lcc(Thread::current());
duke@435 1576 object_iterate(&lcc);
jcoomes@2661 1577 ensure_parsability(false); // arg is actually don't care
duke@435 1578 tty->print_cr("done. ");
duke@435 1579
duke@435 1580 // Create and dump the shared spaces.
duke@435 1581 jint err = CompactingPermGenGen::dump_shared(class_promote_order, THREAD);
duke@435 1582 if (err != JNI_OK) {
duke@435 1583 fatal("Dumping shared spaces failed.");
duke@435 1584 }
duke@435 1585
duke@435 1586 } else {
duke@435 1587 char errmsg[JVM_MAXPATHLEN];
ikrylov@2322 1588 os::lasterror(errmsg, JVM_MAXPATHLEN);
duke@435 1589 tty->print_cr("Loading classlist failed: %s", errmsg);
duke@435 1590 exit(1);
duke@435 1591 }
duke@435 1592
duke@435 1593 // Since various initialization steps have been undone by this process,
duke@435 1594 // it is not reasonable to continue running a java process.
duke@435 1595 exit(0);
duke@435 1596 }

mercurial