This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
dynamicArchive.cpp
1151 lines (968 loc) · 41.5 KB
/
dynamicArchive.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "jvm.h"
#include "classfile/classLoaderData.inline.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/systemDictionaryShared.hpp"
#include "logging/log.hpp"
#include "memory/archiveUtils.inline.hpp"
#include "memory/dynamicArchive.hpp"
#include "memory/metadataFactory.hpp"
#include "memory/metaspace.hpp"
#include "memory/metaspaceClosure.hpp"
#include "memory/metaspaceShared.hpp"
#include "memory/resourceArea.hpp"
#include "oops/compressedOops.hpp"
#include "oops/objArrayKlass.hpp"
#include "prims/jvmtiRedefineClasses.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/os.inline.hpp"
#include "runtime/sharedRuntime.hpp"
#include "runtime/vmThread.hpp"
#include "runtime/vmOperations.hpp"
#include "utilities/bitMap.inline.hpp"
#ifndef O_BINARY // if defined (Win32) use binary files.
#define O_BINARY 0 // otherwise do nothing.
#endif
class DynamicArchiveBuilder : ResourceObj {
static unsigned my_hash(const address& a) {
return primitive_hash<address>(a);
}
static bool my_equals(const address& a0, const address& a1) {
return primitive_equals<address>(a0, a1);
}
typedef ResourceHashtable<
address, address,
DynamicArchiveBuilder::my_hash, // solaris compiler doesn't like: primitive_hash<address>
DynamicArchiveBuilder::my_equals, // solaris compiler doesn't like: primitive_equals<address>
16384, ResourceObj::C_HEAP> RelocationTable;
RelocationTable _new_loc_table;
static intx _buffer_to_target_delta;
DumpRegion* _current_dump_space;
static size_t reserve_alignment() {
return Metaspace::reserve_alignment();
}
static const int _total_dump_regions = 3;
int _num_dump_regions_used;
public:
void mark_pointer(address* ptr_loc) {
ArchivePtrMarker::mark_pointer(ptr_loc);
}
DumpRegion* current_dump_space() const {
return _current_dump_space;
}
bool is_in_buffer_space(address p) const {
return (_alloc_bottom <= p && p < (address)current_dump_space()->top());
}
template <typename T> bool is_in_target_space(T target_obj) const {
address buff_obj = address(target_obj) - _buffer_to_target_delta;
return is_in_buffer_space(buff_obj);
}
template <typename T> bool is_in_buffer_space(T obj) const {
return is_in_buffer_space(address(obj));
}
template <typename T> T to_target_no_check(T obj) const {
return (T)(address(obj) + _buffer_to_target_delta);
}
template <typename T> T to_target(T obj) const {
assert(is_in_buffer_space(obj), "must be");
return (T)(address(obj) + _buffer_to_target_delta);
}
template <typename T> T get_new_loc(T obj) {
address* pp = _new_loc_table.get((address)obj);
if (pp == NULL) {
// Excluded klasses are not copied
return NULL;
} else {
return (T)*pp;
}
}
address get_new_loc(MetaspaceClosure::Ref* ref) {
return get_new_loc(ref->obj());
}
template <typename T> bool has_new_loc(T obj) {
address* pp = _new_loc_table.get((address)obj);
return pp != NULL;
}
static int dynamic_dump_method_comparator(Method* a, Method* b) {
Symbol* a_name = a->name();
Symbol* b_name = b->name();
if (a_name == b_name) {
return 0;
}
if (!MetaspaceShared::is_in_shared_metaspace(a_name)) {
// a_name points to a Symbol in the top archive.
// When this method is called, a_name is still pointing to the output space.
// Translate it to point to the output space, so that it can be compared with
// Symbols in the base archive.
a_name = (Symbol*)(address(a_name) + _buffer_to_target_delta);
}
if (!MetaspaceShared::is_in_shared_metaspace(b_name)) {
b_name = (Symbol*)(address(b_name) + _buffer_to_target_delta);
}
return a_name->fast_compare(b_name);
}
protected:
enum FollowMode {
make_a_copy, point_to_it, set_to_null
};
public:
void copy(MetaspaceClosure::Ref* ref, bool read_only) {
int bytes = ref->size() * BytesPerWord;
address old_obj = ref->obj();
address new_obj = copy_impl(ref, read_only, bytes);
assert(new_obj != NULL, "must be");
assert(new_obj != old_obj, "must be");
bool isnew = _new_loc_table.put(old_obj, new_obj);
assert(isnew, "must be");
}
// Make a shallow copy of each eligible MetaspaceObj into the buffer.
class ShallowCopier: public UniqueMetaspaceClosure {
DynamicArchiveBuilder* _builder;
bool _read_only;
public:
ShallowCopier(DynamicArchiveBuilder* shuffler, bool read_only)
: _builder(shuffler), _read_only(read_only) {}
virtual bool do_unique_ref(Ref* orig_obj, bool read_only) {
// This method gets called on each *original* object
// reachable from _builder->iterate_roots(). Each orig_obj is
// called exactly once.
FollowMode mode = _builder->follow_ref(orig_obj);
if (mode == point_to_it) {
if (read_only == _read_only) {
log_debug(cds, dynamic)("ptr : " PTR_FORMAT " %s", p2i(orig_obj->obj()),
MetaspaceObj::type_name(orig_obj->msotype()));
address p = orig_obj->obj();
bool isnew = _builder->_new_loc_table.put(p, p);
assert(isnew, "must be");
}
return false;
}
if (mode == set_to_null) {
log_debug(cds, dynamic)("nul : " PTR_FORMAT " %s", p2i(orig_obj->obj()),
MetaspaceObj::type_name(orig_obj->msotype()));
return false;
}
if (read_only == _read_only) {
// Make a shallow copy of orig_obj in a buffer (maintained
// by copy_impl in a subclass of DynamicArchiveBuilder).
_builder->copy(orig_obj, read_only);
}
return true;
}
};
// Relocate all embedded pointer fields within a MetaspaceObj's shallow copy
class ShallowCopyEmbeddedRefRelocator: public UniqueMetaspaceClosure {
DynamicArchiveBuilder* _builder;
public:
ShallowCopyEmbeddedRefRelocator(DynamicArchiveBuilder* shuffler)
: _builder(shuffler) {}
// This method gets called on each *original* object reachable
// from _builder->iterate_roots(). Each orig_obj is
// called exactly once.
virtual bool do_unique_ref(Ref* orig_ref, bool read_only) {
FollowMode mode = _builder->follow_ref(orig_ref);
if (mode == point_to_it) {
// We did not make a copy of this object
// and we have nothing to update
assert(_builder->get_new_loc(orig_ref) == NULL ||
_builder->get_new_loc(orig_ref) == orig_ref->obj(), "must be");
return false;
}
if (mode == set_to_null) {
// We did not make a copy of this object
// and we have nothing to update
assert(!_builder->has_new_loc(orig_ref->obj()), "must not be copied or pointed to");
return false;
}
// - orig_obj points to the original object.
// - new_obj points to the shallow copy (created by ShallowCopier)
// of orig_obj. new_obj is NULL if the orig_obj is excluded
address orig_obj = orig_ref->obj();
address new_obj = _builder->get_new_loc(orig_ref);
assert(new_obj != orig_obj, "must be");
#ifdef ASSERT
if (new_obj == NULL) {
if (orig_ref->msotype() == MetaspaceObj::ClassType) {
Klass* k = (Klass*)orig_obj;
assert(k->is_instance_klass() &&
SystemDictionaryShared::is_excluded_class(InstanceKlass::cast(k)),
"orig_obj must be excluded Class");
}
}
#endif
log_debug(cds, dynamic)("Relocating " PTR_FORMAT " %s", p2i(new_obj),
MetaspaceObj::type_name(orig_ref->msotype()));
if (new_obj != NULL) {
EmbeddedRefUpdater updater(_builder, orig_obj, new_obj);
orig_ref->metaspace_pointers_do(&updater);
}
return true; // keep recursing until every object is visited exactly once.
}
virtual void push_special(SpecialRef type, Ref* ref, intptr_t* p) {
assert(type == _method_entry_ref, "only special type allowed for now");
address obj = ref->obj();
address new_obj = _builder->get_new_loc(ref);
size_t offset = pointer_delta(p, obj, sizeof(u1));
intptr_t* new_p = (intptr_t*)(new_obj + offset);
assert(*p == *new_p, "must be a copy");
ArchivePtrMarker::mark_pointer((address*)new_p);
}
};
class EmbeddedRefUpdater: public MetaspaceClosure {
DynamicArchiveBuilder* _builder;
address _orig_obj;
address _new_obj;
public:
EmbeddedRefUpdater(DynamicArchiveBuilder* shuffler, address orig_obj, address new_obj) :
_builder(shuffler), _orig_obj(orig_obj), _new_obj(new_obj) {}
// This method gets called once for each pointer field F of orig_obj.
// We update new_obj->F to point to the new location of orig_obj->F.
//
// Example: Klass* 0x100 is copied to 0x400
// Symbol* 0x200 is copied to 0x500
//
// Let orig_obj == 0x100; and
// new_obj == 0x400; and
// ((Klass*)orig_obj)->_name == 0x200;
// Then this function effectively assigns
// ((Klass*)new_obj)->_name = 0x500;
virtual bool do_ref(Ref* ref, bool read_only) {
address new_pointee = NULL;
if (ref->not_null()) {
address old_pointee = ref->obj();
FollowMode mode = _builder->follow_ref(ref);
if (mode == point_to_it) {
new_pointee = old_pointee;
} else if (mode == set_to_null) {
new_pointee = NULL;
} else {
new_pointee = _builder->get_new_loc(old_pointee);
}
}
const char* kind = MetaspaceObj::type_name(ref->msotype());
// offset of this field inside the original object
intx offset = (address)ref->addr() - _orig_obj;
_builder->update_pointer((address*)(_new_obj + offset), new_pointee, kind, offset);
// We can't mark the pointer here, because DynamicArchiveBuilder::sort_methods
// may re-layout the [iv]tables, which would change the offset(s) in an InstanceKlass
// that would contain pointers. Therefore, we must mark the pointers after
// sort_methods(), using PointerMarker.
return false; // Do not recurse.
}
};
class ExternalRefUpdater: public MetaspaceClosure {
DynamicArchiveBuilder* _builder;
public:
ExternalRefUpdater(DynamicArchiveBuilder* shuffler) : _builder(shuffler) {}
virtual bool do_ref(Ref* ref, bool read_only) {
// ref is a pointer that lives OUTSIDE of the buffer, but points to an object inside the buffer
if (ref->not_null()) {
address new_loc = _builder->get_new_loc(ref);
const char* kind = MetaspaceObj::type_name(ref->msotype());
_builder->update_pointer(ref->addr(), new_loc, kind, 0);
_builder->mark_pointer(ref->addr());
}
return false; // Do not recurse.
}
};
class PointerMarker: public UniqueMetaspaceClosure {
DynamicArchiveBuilder* _builder;
public:
PointerMarker(DynamicArchiveBuilder* shuffler) : _builder(shuffler) {}
virtual bool do_unique_ref(Ref* ref, bool read_only) {
if (_builder->is_in_buffer_space(ref->obj())) {
EmbeddedRefMarker ref_marker(_builder);
ref->metaspace_pointers_do(&ref_marker);
return true; // keep recursing until every buffered object is visited exactly once.
} else {
return false;
}
}
};
class EmbeddedRefMarker: public MetaspaceClosure {
DynamicArchiveBuilder* _builder;
public:
EmbeddedRefMarker(DynamicArchiveBuilder* shuffler) : _builder(shuffler) {}
virtual bool do_ref(Ref* ref, bool read_only) {
if (ref->not_null()) {
_builder->mark_pointer(ref->addr());
}
return false; // Do not recurse.
}
};
void update_pointer(address* addr, address value, const char* kind, uintx offset, bool is_mso_pointer=true) {
// Propagate the the mask bits to the new value -- see comments above MetaspaceClosure::obj()
if (is_mso_pointer) {
const uintx FLAG_MASK = 0x03;
uintx mask_bits = uintx(*addr) & FLAG_MASK;
value = (address)(uintx(value) | mask_bits);
}
if (*addr != value) {
log_debug(cds, dynamic)("Update (%18s*) %3d [" PTR_FORMAT "] " PTR_FORMAT " -> " PTR_FORMAT,
kind, int(offset), p2i(addr), p2i(*addr), p2i(value));
*addr = value;
}
}
private:
GrowableArray<Symbol*>* _symbols; // symbols to dump
GrowableArray<InstanceKlass*>* _klasses; // klasses to dump
void append(InstanceKlass* k) { _klasses->append(k); }
void append(Symbol* s) { _symbols->append(s); }
class GatherKlassesAndSymbols : public UniqueMetaspaceClosure {
DynamicArchiveBuilder* _builder;
bool _read_only;
public:
GatherKlassesAndSymbols(DynamicArchiveBuilder* builder)
: _builder(builder) {}
virtual bool do_unique_ref(Ref* ref, bool read_only) {
if (_builder->follow_ref(ref) != make_a_copy) {
return false;
}
if (ref->msotype() == MetaspaceObj::ClassType) {
Klass* klass = (Klass*)ref->obj();
assert(klass->is_klass(), "must be");
if (klass->is_instance_klass()) {
InstanceKlass* ik = InstanceKlass::cast(klass);
assert(!SystemDictionaryShared::is_excluded_class(ik), "must be");
_builder->append(ik);
_builder->_estimated_metsapceobj_bytes += BytesPerWord; // See RunTimeSharedClassInfo::get_for()
}
} else if (ref->msotype() == MetaspaceObj::SymbolType) {
_builder->append((Symbol*)ref->obj());
}
int bytes = ref->size() * BytesPerWord;
_builder->_estimated_metsapceobj_bytes += bytes;
return true;
}
};
FollowMode follow_ref(MetaspaceClosure::Ref *ref) {
address obj = ref->obj();
if (MetaspaceShared::is_in_shared_metaspace(obj)) {
// Don't dump existing shared metadata again.
return point_to_it;
} else if (ref->msotype() == MetaspaceObj::MethodDataType) {
return set_to_null;
} else {
if (ref->msotype() == MetaspaceObj::ClassType) {
Klass* klass = (Klass*)ref->obj();
assert(klass->is_klass(), "must be");
if (klass->is_instance_klass()) {
InstanceKlass* ik = InstanceKlass::cast(klass);
if (SystemDictionaryShared::is_excluded_class(ik)) {
ResourceMark rm;
log_debug(cds, dynamic)("Skipping class (excluded): %s", klass->external_name());
return set_to_null;
}
} else if (klass->is_array_klass()) {
// Don't support archiving of array klasses for now.
ResourceMark rm;
log_debug(cds, dynamic)("Skipping class (array): %s", klass->external_name());
return set_to_null;
}
}
return make_a_copy;
}
}
address copy_impl(MetaspaceClosure::Ref* ref, bool read_only, int bytes) {
if (ref->msotype() == MetaspaceObj::ClassType) {
// Save a pointer immediate in front of an InstanceKlass, so
// we can do a quick lookup from InstanceKlass* -> RunTimeSharedClassInfo*
// without building another hashtable. See RunTimeSharedClassInfo::get_for()
// in systemDictionaryShared.cpp.
address obj = ref->obj();
Klass* klass = (Klass*)obj;
if (klass->is_instance_klass()) {
SystemDictionaryShared::validate_before_archiving(InstanceKlass::cast(klass));
current_dump_space()->allocate(sizeof(address), BytesPerWord);
}
}
address p = (address)current_dump_space()->allocate(bytes);
address obj = ref->obj();
log_debug(cds, dynamic)("COPY: " PTR_FORMAT " ==> " PTR_FORMAT " %5d %s",
p2i(obj), p2i(p), bytes,
MetaspaceObj::type_name(ref->msotype()));
memcpy(p, obj, bytes);
intptr_t* cloned_vtable = MetaspaceShared::fix_cpp_vtable_for_dynamic_archive(ref->msotype(), p);
if (cloned_vtable != NULL) {
update_pointer((address*)p, (address)cloned_vtable, "vtb", 0, /*is_mso_pointer*/false);
mark_pointer((address*)p);
}
return (address)p;
}
DynamicArchiveHeader *_header;
address _alloc_bottom;
address _last_verified_top;
size_t _other_region_used_bytes;
// Conservative estimate for number of bytes needed for:
size_t _estimated_metsapceobj_bytes; // all archived MetsapceObj's.
size_t _estimated_hashtable_bytes; // symbol table and dictionaries
size_t _estimated_trampoline_bytes; // method entry trampolines
size_t estimate_archive_size();
size_t estimate_trampoline_size();
size_t estimate_class_file_size();
address reserve_space_and_init_buffer_to_target_delta();
void init_header(address addr);
void release_header();
void make_trampolines();
void make_klasses_shareable();
void sort_methods(InstanceKlass* ik) const;
void set_symbols_permanent();
void relocate_buffer_to_target();
void write_archive(char* serialized_data);
void init_first_dump_space(address reserved_bottom) {
address first_space_base = reserved_bottom;
DumpRegion* mc_space = MetaspaceShared::misc_code_dump_space();
DumpRegion* rw_space = MetaspaceShared::read_write_dump_space();
// Use the same MC->RW->RO ordering as in the base archive.
MetaspaceShared::init_shared_dump_space(mc_space, first_space_base);
_current_dump_space = mc_space;
_last_verified_top = first_space_base;
_num_dump_regions_used = 1;
}
void reserve_buffers_for_trampolines() {
size_t n = _estimated_trampoline_bytes;
assert(n >= SharedRuntime::trampoline_size(), "dont want to be empty");
MetaspaceShared::misc_code_space_alloc(n);
}
public:
DynamicArchiveBuilder() {
_klasses = new (ResourceObj::C_HEAP, mtClass) GrowableArray<InstanceKlass*>(100, true, mtInternal);
_symbols = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Symbol*>(1000, true, mtInternal);
_estimated_metsapceobj_bytes = 0;
_estimated_hashtable_bytes = 0;
_estimated_trampoline_bytes = 0;
_num_dump_regions_used = 0;
}
void start_dump_space(DumpRegion* next) {
address bottom = _last_verified_top;
address top = (address)(current_dump_space()->top());
_other_region_used_bytes += size_t(top - bottom);
MetaspaceShared::pack_dump_space(current_dump_space(), next, MetaspaceShared::shared_rs());
_current_dump_space = next;
_num_dump_regions_used ++;
_last_verified_top = (address)(current_dump_space()->top());
}
void verify_estimate_size(size_t estimate, const char* which) {
address bottom = _last_verified_top;
address top = (address)(current_dump_space()->top());
size_t used = size_t(top - bottom) + _other_region_used_bytes;
int diff = int(estimate) - int(used);
log_info(cds)("%s estimate = " SIZE_FORMAT " used = " SIZE_FORMAT "; diff = %d bytes", which, estimate, used, diff);
assert(diff >= 0, "Estimate is too small");
_last_verified_top = top;
_other_region_used_bytes = 0;
}
// Do this before and after the archive dump to see if any corruption
// is caused by dynamic dumping.
void verify_universe(const char* info) {
if (VerifyBeforeExit) {
log_info(cds)("Verify %s", info);
HandleMark hm;
// Among other things, this ensures that Eden top is correct.
Universe::heap()->prepare_for_verify();
Universe::verify(info);
}
}
void doit() {
verify_universe("Before CDS dynamic dump");
DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm);
SystemDictionaryShared::check_excluded_classes();
{
ResourceMark rm;
GatherKlassesAndSymbols gatherer(this);
SystemDictionaryShared::dumptime_classes_do(&gatherer);
SymbolTable::metaspace_pointers_do(&gatherer);
FileMapInfo::metaspace_pointers_do(&gatherer);
gatherer.finish();
}
// rw space starts ...
address reserved_bottom = reserve_space_and_init_buffer_to_target_delta();
init_header(reserved_bottom);
CHeapBitMap ptrmap;
ArchivePtrMarker::initialize(&ptrmap, (address*)reserved_bottom, (address*)current_dump_space()->top());
reserve_buffers_for_trampolines();
verify_estimate_size(_estimated_trampoline_bytes, "Trampolines");
start_dump_space(MetaspaceShared::read_write_dump_space());
log_info(cds, dynamic)("Copying %d klasses and %d symbols",
_klasses->length(), _symbols->length());
{
assert(current_dump_space() == MetaspaceShared::read_write_dump_space(),
"Current dump space is not rw space");
// shallow-copy RW objects, if necessary
ResourceMark rm;
ShallowCopier rw_copier(this, false);
iterate_roots(&rw_copier);
}
// ro space starts ...
DumpRegion* ro_space = MetaspaceShared::read_only_dump_space();
{
start_dump_space(ro_space);
// shallow-copy RO objects, if necessary
ResourceMark rm;
ShallowCopier ro_copier(this, true);
iterate_roots(&ro_copier);
}
{
log_info(cds)("Relocating embedded pointers ... ");
ResourceMark rm;
ShallowCopyEmbeddedRefRelocator emb_reloc(this);
iterate_roots(&emb_reloc);
}
{
log_info(cds)("Relocating external roots ... ");
ResourceMark rm;
ExternalRefUpdater ext_reloc(this);
iterate_roots(&ext_reloc);
}
verify_estimate_size(_estimated_metsapceobj_bytes, "MetaspaceObjs");
char* serialized_data;
{
set_symbols_permanent();
// Write the symbol table and system dictionaries to the RO space.
// Note that these tables still point to the *original* objects
// (because they were not processed by ExternalRefUpdater), so
// they would need to call DynamicArchive::original_to_target() to
// get the correct addresses.
assert(current_dump_space() == ro_space, "Must be RO space");
SymbolTable::write_to_archive(false);
SystemDictionaryShared::write_to_archive(false);
serialized_data = ro_space->top();
WriteClosure wc(ro_space);
SymbolTable::serialize_shared_table_header(&wc, false);
SystemDictionaryShared::serialize_dictionary_headers(&wc, false);
}
verify_estimate_size(_estimated_hashtable_bytes, "Hashtables");
make_trampolines();
make_klasses_shareable();
{
log_info(cds)("Final relocation of pointers ... ");
ResourceMark rm;
PointerMarker marker(this);
iterate_roots(&marker);
relocate_buffer_to_target();
}
write_archive(serialized_data);
release_header();
assert(_num_dump_regions_used == _total_dump_regions, "must be");
verify_universe("After CDS dynamic dump");
}
void iterate_roots(MetaspaceClosure* it) {
int i;
int num_klasses = _klasses->length();
for (i = 0; i < num_klasses; i++) {
it->push(&_klasses->at(i));
}
int num_symbols = _symbols->length();
for (i = 0; i < num_symbols; i++) {
it->push(&_symbols->at(i));
}
FileMapInfo::metaspace_pointers_do(it);
// Do not call these again, as we have already collected all the classes and symbols
// that we want to archive. Also, these calls would corrupt the tables when
// ExternalRefUpdater is used.
//
// SystemDictionaryShared::dumptime_classes_do(it);
// SymbolTable::metaspace_pointers_do(it);
it->finish();
}
};
intx DynamicArchiveBuilder::_buffer_to_target_delta;
size_t DynamicArchiveBuilder::estimate_archive_size() {
// size of the symbol table and two dictionaries, plus the RunTimeSharedClassInfo's
_estimated_hashtable_bytes = 0;
_estimated_hashtable_bytes += SymbolTable::estimate_size_for_archive();
_estimated_hashtable_bytes += SystemDictionaryShared::estimate_size_for_archive();
_estimated_trampoline_bytes = estimate_trampoline_size();
size_t total = 0;
total += _estimated_metsapceobj_bytes;
total += _estimated_hashtable_bytes;
total += _estimated_trampoline_bytes;
// allow fragmentation at the end of each dump region
total += _total_dump_regions * reserve_alignment();
return align_up(total, reserve_alignment());
}
address DynamicArchiveBuilder::reserve_space_and_init_buffer_to_target_delta() {
size_t total = estimate_archive_size();
ReservedSpace rs = MetaspaceShared::reserve_shared_space(total);
if (!rs.is_reserved()) {
log_error(cds, dynamic)("Failed to reserve %d bytes of output buffer.", (int)total);
vm_direct_exit(0);
}
address buffer_base = (address)rs.base();
log_info(cds, dynamic)("Reserved output buffer space at : " PTR_FORMAT " [%d bytes]",
p2i(buffer_base), (int)total);
MetaspaceShared::set_shared_rs(rs);
// At run time, we will mmap the dynamic archive at target_space_bottom.
// However, at dump time, we may not be able to write into the target_space,
// as it's occupied by dynamically loaded Klasses. So we allocate a buffer
// at an arbitrary location chosen by the OS. We will write all the dynamically
// archived classes into this buffer. At the final stage of dumping, we relocate
// all pointers that are inside the buffer_space to point to their (runtime)
// target location inside thetarget_space.
address target_space_bottom =
(address)align_up(MetaspaceShared::shared_metaspace_top(), reserve_alignment());
_buffer_to_target_delta = intx(target_space_bottom) - intx(buffer_base);
log_info(cds, dynamic)("Target archive space at : " PTR_FORMAT, p2i(target_space_bottom));
log_info(cds, dynamic)("Buffer-space to target-space delta : " PTR_FORMAT, p2i((address)_buffer_to_target_delta));
return buffer_base;
}
void DynamicArchiveBuilder::init_header(address reserved_bottom) {
_alloc_bottom = reserved_bottom;
_last_verified_top = reserved_bottom;
_other_region_used_bytes = 0;
init_first_dump_space(reserved_bottom);
FileMapInfo* mapinfo = new FileMapInfo(false);
assert(FileMapInfo::dynamic_info() == mapinfo, "must be");
_header = mapinfo->dynamic_header();
Thread* THREAD = Thread::current();
FileMapInfo* base_info = FileMapInfo::current_info();
_header->set_base_header_crc(base_info->crc());
for (int i = 0; i < MetaspaceShared::n_regions; i++) {
_header->set_base_region_crc(i, base_info->space_crc(i));
}
_header->populate(base_info, os::vm_allocation_granularity());
}
void DynamicArchiveBuilder::release_header() {
// We temporarily allocated a dynamic FileMapInfo for dumping, which makes it appear we
// have mapped a dynamic archive, but we actually have not. We are in a safepoint now.
// Let's free it so that if class loading happens after we leave the safepoint, nothing
// bad will happen.
assert(SafepointSynchronize::is_at_safepoint(), "must be");
FileMapInfo *mapinfo = FileMapInfo::dynamic_info();
assert(mapinfo != NULL && _header == mapinfo->dynamic_header(), "must be");
delete mapinfo;
assert(!DynamicArchive::is_mapped(), "must be");
_header = NULL;
}
size_t DynamicArchiveBuilder::estimate_trampoline_size() {
size_t total = 0;
size_t each_method_bytes =
align_up(SharedRuntime::trampoline_size(), BytesPerWord) +
align_up(sizeof(AdapterHandlerEntry*), BytesPerWord);
for (int i = 0; i < _klasses->length(); i++) {
InstanceKlass* ik = _klasses->at(i);
Array<Method*>* methods = ik->methods();
total += each_method_bytes * methods->length();
}
if (total == 0) {
// We have nothing to archive, but let's avoid having an empty region.
total = SharedRuntime::trampoline_size();
}
return total;
}
void DynamicArchiveBuilder::make_trampolines() {
DumpRegion* mc_space = MetaspaceShared::misc_code_dump_space();
char* p = mc_space->base();
for (int i = 0; i < _klasses->length(); i++) {
InstanceKlass* ik = _klasses->at(i);
Array<Method*>* methods = ik->methods();
for (int j = 0; j < methods->length(); j++) {
Method* m = methods->at(j);
address c2i_entry_trampoline = (address)p;
p += SharedRuntime::trampoline_size();
assert(p >= mc_space->base() && p <= mc_space->top(), "must be");
m->set_from_compiled_entry(to_target(c2i_entry_trampoline));
AdapterHandlerEntry** adapter_trampoline =(AdapterHandlerEntry**)p;
p += sizeof(AdapterHandlerEntry*);
assert(p >= mc_space->base() && p <= mc_space->top(), "must be");
*adapter_trampoline = NULL;
m->set_adapter_trampoline(to_target(adapter_trampoline));
}
}
guarantee(p <= mc_space->top(), "Estimate of trampoline size is insufficient");
}
void DynamicArchiveBuilder::make_klasses_shareable() {
int i, count = _klasses->length();
InstanceKlass::disable_method_binary_search();
for (i = 0; i < count; i++) {
InstanceKlass* ik = _klasses->at(i);
sort_methods(ik);
}
for (i = 0; i < count; i++) {
InstanceKlass* ik = _klasses->at(i);
ClassLoaderData *cld = ik->class_loader_data();
if (cld->is_boot_class_loader_data()) {
ik->set_shared_class_loader_type(ClassLoader::BOOT_LOADER);
}
else if (cld->is_platform_class_loader_data()) {
ik->set_shared_class_loader_type(ClassLoader::PLATFORM_LOADER);
}
else if (cld->is_system_class_loader_data()) {
ik->set_shared_class_loader_type(ClassLoader::APP_LOADER);
}
MetaspaceShared::rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread::current(), ik);
ik->remove_unshareable_info();
assert(ik->array_klasses() == NULL, "sanity");
if (log_is_enabled(Debug, cds, dynamic)) {
ResourceMark rm;
log_debug(cds, dynamic)("klasses[%4i] = " PTR_FORMAT " %s", i, p2i(to_target(ik)), ik->external_name());
}
}
}
// The address order of the copied Symbols may be different than when the original
// klasses were created. Re-sort all the tables. See Method::sort_methods().
void DynamicArchiveBuilder::sort_methods(InstanceKlass* ik) const {
assert(ik != NULL, "DynamicArchiveBuilder currently doesn't support dumping the base archive");
if (MetaspaceShared::is_in_shared_metaspace(ik)) {
// We have reached a supertype that's already in the base archive
return;
}
if (ik->java_mirror() == NULL) {
// NULL mirror means this class has already been visited and methods are already sorted
return;
}
ik->remove_java_mirror();
if (log_is_enabled(Debug, cds, dynamic)) {
ResourceMark rm;
log_debug(cds, dynamic)("sorting methods for " PTR_FORMAT " %s", p2i(to_target(ik)), ik->external_name());
}
// Make sure all supertypes have been sorted
sort_methods(ik->java_super());
Array<InstanceKlass*>* interfaces = ik->local_interfaces();
int len = interfaces->length();
for (int i = 0; i < len; i++) {
sort_methods(interfaces->at(i));
}
#ifdef ASSERT
if (ik->methods() != NULL) {
for (int m = 0; m < ik->methods()->length(); m++) {
Symbol* name = ik->methods()->at(m)->name();
assert(MetaspaceShared::is_in_shared_metaspace(name) || is_in_buffer_space(name), "must be");
}
}
if (ik->default_methods() != NULL) {
for (int m = 0; m < ik->default_methods()->length(); m++) {
Symbol* name = ik->default_methods()->at(m)->name();
assert(MetaspaceShared::is_in_shared_metaspace(name) || is_in_buffer_space(name), "must be");
}
}
#endif
Thread* THREAD = Thread::current();
Method::sort_methods(ik->methods(), /*set_idnums=*/true, dynamic_dump_method_comparator);
if (ik->default_methods() != NULL) {
Method::sort_methods(ik->default_methods(), /*set_idnums=*/false, dynamic_dump_method_comparator);
}
ik->vtable().initialize_vtable(true, THREAD); assert(!HAS_PENDING_EXCEPTION, "cannot fail");
ik->itable().initialize_itable(true, THREAD); assert(!HAS_PENDING_EXCEPTION, "cannot fail");
}
void DynamicArchiveBuilder::set_symbols_permanent() {
int count = _symbols->length();
for (int i=0; i<count; i++) {
Symbol* s = _symbols->at(i);
s->set_permanent();
if (log_is_enabled(Trace, cds, dynamic)) {
ResourceMark rm;
log_trace(cds, dynamic)("symbols[%4i] = " PTR_FORMAT " %s", i, p2i(to_target(s)), s->as_quoted_ascii());
}
}
}
class RelocateBufferToTarget: public BitMapClosure {
DynamicArchiveBuilder *_builder;
address* _buffer_bottom;
intx _buffer_to_target_delta;
public:
RelocateBufferToTarget(DynamicArchiveBuilder* builder, address* bottom, intx delta) :
_builder(builder), _buffer_bottom(bottom), _buffer_to_target_delta(delta) {}
bool do_bit(size_t offset) {
address* p = _buffer_bottom + offset;
assert(_builder->is_in_buffer_space(p), "pointer must live in buffer space");
address old_ptr = *p;
if (_builder->is_in_buffer_space(old_ptr)) {
address new_ptr = old_ptr + _buffer_to_target_delta;
log_trace(cds, dynamic)("Final patch: @%6d [" PTR_FORMAT " -> " PTR_FORMAT "] " PTR_FORMAT " => " PTR_FORMAT,
(int)offset, p2i(p), p2i(_builder->to_target(p)),
p2i(old_ptr), p2i(new_ptr));
*p = new_ptr;
}
return true; // keep iterating
}
};
void DynamicArchiveBuilder::relocate_buffer_to_target() {
RelocateBufferToTarget patcher(this, (address*)_alloc_bottom, _buffer_to_target_delta);
ArchivePtrMarker::ptrmap()->iterate(&patcher);
Array<u8>* table = FileMapInfo::shared_path_table().table();
SharedPathTable runtime_table(to_target(table), FileMapInfo::shared_path_table().size());
_header->set_shared_path_table(runtime_table);
address relocatable_base = (address)SharedBaseAddress;
address relocatable_end = (address)(current_dump_space()->top()) + _buffer_to_target_delta;
intx addr_delta = MetaspaceShared::final_delta();
if (addr_delta == 0) {
ArchivePtrMarker::compact(relocatable_base, relocatable_end);
} else {
// The base archive is NOT mapped at Arguments::default_SharedBaseAddress() (due to ASLR).
// This means that the current content of the dynamic archive is based on a random
// address. Let's relocate all the pointers, so that it can be mapped to
// Arguments::default_SharedBaseAddress() without runtime relocation.
//
// Note: both the base and dynamic archive are written with
// FileMapHeader::_shared_base_address == Arguments::default_SharedBaseAddress()
// Patch all pointers that are marked by ptrmap within this region,
// where we have just dumped all the metaspace data.
address patch_base = (address)_alloc_bottom;
address patch_end = (address)current_dump_space()->top();
// the current value of the pointers to be patched must be within this
// range (i.e., must point to either the top archive (as currently mapped), or to the
// (targeted address of) the top archive)
address valid_old_base = relocatable_base;
address valid_old_end = relocatable_end;
size_t base_plus_top_size = valid_old_end - valid_old_base;
size_t top_size = patch_end - patch_base;
size_t base_size = base_plus_top_size - top_size;
assert(base_plus_top_size > base_size, "no overflow");
assert(base_plus_top_size > top_size, "no overflow");
// after patching, the pointers must point inside this range
// (the requested location of the archive, as mapped at runtime).
address valid_new_base = (address)Arguments::default_SharedBaseAddress();
address valid_new_end = valid_new_base + base_plus_top_size;
log_debug(cds)("Relocating archive from [" INTPTR_FORMAT " - " INTPTR_FORMAT "] to "
"[" INTPTR_FORMAT " - " INTPTR_FORMAT "], delta = " INTX_FORMAT " bytes",