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