25
#include "precompiled.hpp"
26
#include "cds/archiveBuilder.hpp"
27
#include "cds/archiveHeapLoader.inline.hpp"
28
#include "cds/archiveUtils.hpp"
29
#include "cds/cdsConfig.hpp"
30
#include "cds/classListParser.hpp"
31
#include "cds/classListWriter.hpp"
32
#include "cds/dynamicArchive.hpp"
33
#include "cds/filemap.hpp"
34
#include "cds/heapShared.hpp"
35
#include "cds/metaspaceShared.hpp"
36
#include "classfile/systemDictionaryShared.hpp"
37
#include "classfile/vmClasses.hpp"
38
#include "interpreter/bootstrapInfo.hpp"
39
#include "memory/metaspaceUtils.hpp"
40
#include "memory/resourceArea.hpp"
41
#include "oops/compressedOops.inline.hpp"
42
#include "runtime/arguments.hpp"
43
#include "utilities/bitMap.inline.hpp"
44
#include "utilities/debug.hpp"
45
#include "utilities/formatBuffer.hpp"
46
#include "utilities/globalDefinitions.hpp"
48
CHeapBitMap* ArchivePtrMarker::_ptrmap = nullptr;
49
CHeapBitMap* ArchivePtrMarker::_rw_ptrmap = nullptr;
50
CHeapBitMap* ArchivePtrMarker::_ro_ptrmap = nullptr;
51
VirtualSpace* ArchivePtrMarker::_vs;
53
bool ArchivePtrMarker::_compacted;
55
void ArchivePtrMarker::initialize(CHeapBitMap* ptrmap, VirtualSpace* vs) {
56
assert(_ptrmap == nullptr, "initialize only once");
57
assert(_rw_ptrmap == nullptr, "initialize only once");
58
assert(_ro_ptrmap == nullptr, "initialize only once");
65
size_t estimated_archive_size = MetaspaceGC::capacity_until_GC();
68
DEBUG_ONLY(estimated_archive_size = 6 * M);
71
_ptrmap->initialize(estimated_archive_size / sizeof(intptr_t));
74
void ArchivePtrMarker::initialize_rw_ro_maps(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap) {
75
address* rw_bottom = (address*)ArchiveBuilder::current()->rw_region()->base();
76
address* ro_bottom = (address*)ArchiveBuilder::current()->ro_region()->base();
78
_rw_ptrmap = rw_ptrmap;
79
_ro_ptrmap = ro_ptrmap;
81
size_t rw_size = ArchiveBuilder::current()->rw_region()->used() / sizeof(address);
82
size_t ro_size = ArchiveBuilder::current()->ro_region()->used() / sizeof(address);
88
size_t ro_start = ro_bottom - rw_bottom;
92
_rw_ptrmap->initialize(rw_size);
93
_ro_ptrmap->initialize(_ptrmap->size() - ro_start);
95
for (size_t rw_bit = 0; rw_bit < _rw_ptrmap->size(); rw_bit++) {
96
_rw_ptrmap->at_put(rw_bit, _ptrmap->at(rw_bit));
99
for(size_t ro_bit = ro_start; ro_bit < _ptrmap->size(); ro_bit++) {
100
_ro_ptrmap->at_put(ro_bit-ro_start, _ptrmap->at(ro_bit));
102
assert(_ptrmap->size() - ro_start == _ro_ptrmap->size(), "must be");
105
void ArchivePtrMarker::mark_pointer(address* ptr_loc) {
106
assert(_ptrmap != nullptr, "not initialized");
107
assert(!_compacted, "cannot mark anymore");
109
if (ptr_base() <= ptr_loc && ptr_loc < ptr_end()) {
110
address value = *ptr_loc;
115
assert(value != (address)ptr_base(), "don't point to the bottom of the archive");
117
if (value != nullptr) {
118
assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses");
119
size_t idx = ptr_loc - ptr_base();
120
if (_ptrmap->size() <= idx) {
121
_ptrmap->resize((idx + 1) * 2);
123
assert(idx < _ptrmap->size(), "must be");
124
_ptrmap->set_bit(idx);
130
void ArchivePtrMarker::clear_pointer(address* ptr_loc) {
131
assert(_ptrmap != nullptr, "not initialized");
132
assert(!_compacted, "cannot clear anymore");
134
assert(ptr_base() <= ptr_loc && ptr_loc < ptr_end(), "must be");
135
assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses");
136
size_t idx = ptr_loc - ptr_base();
137
assert(idx < _ptrmap->size(), "cannot clear pointers that have not been marked");
138
_ptrmap->clear_bit(idx);
142
class ArchivePtrBitmapCleaner: public BitMapClosure {
143
CHeapBitMap* _ptrmap;
145
address _relocatable_base;
146
address _relocatable_end;
147
size_t _max_non_null_offset;
150
ArchivePtrBitmapCleaner(CHeapBitMap* ptrmap, address* ptr_base, address relocatable_base, address relocatable_end) :
151
_ptrmap(ptrmap), _ptr_base(ptr_base),
152
_relocatable_base(relocatable_base), _relocatable_end(relocatable_end), _max_non_null_offset(0) {}
154
bool do_bit(size_t offset) {
155
address* ptr_loc = _ptr_base + offset;
156
address ptr_value = *ptr_loc;
157
if (ptr_value != nullptr) {
158
assert(_relocatable_base <= ptr_value && ptr_value < _relocatable_end, "do not point to arbitrary locations!");
159
if (_max_non_null_offset < offset) {
160
_max_non_null_offset = offset;
163
_ptrmap->clear_bit(offset);
164
DEBUG_ONLY(log_trace(cds, reloc)("Clearing pointer [" PTR_FORMAT "] -> null @ " SIZE_FORMAT_W(9), p2i(ptr_loc), offset));
170
size_t max_non_null_offset() const { return _max_non_null_offset; }
173
void ArchivePtrMarker::compact(address relocatable_base, address relocatable_end) {
174
assert(!_compacted, "cannot compact again");
175
ArchivePtrBitmapCleaner cleaner(_ptrmap, ptr_base(), relocatable_base, relocatable_end);
176
_ptrmap->iterate(&cleaner);
177
compact(cleaner.max_non_null_offset());
180
void ArchivePtrMarker::compact(size_t max_non_null_offset) {
181
assert(!_compacted, "cannot compact again");
182
_ptrmap->resize(max_non_null_offset + 1);
186
char* DumpRegion::expand_top_to(char* newtop) {
187
assert(is_allocatable(), "must be initialized and not packed");
188
assert(newtop >= _top, "must not grow backwards");
190
ArchiveBuilder::current()->report_out_of_space(_name, newtop - _top);
191
ShouldNotReachHere();
197
if (_max_delta > 0) {
198
uintx delta = ArchiveBuilder::current()->buffer_to_offset((address)(newtop-1));
199
if (delta > _max_delta) {
203
log_error(cds)("Out of memory in the CDS archive: Please reduce the number of shared classes.");
204
MetaspaceShared::unrecoverable_writing_error();
211
void DumpRegion::commit_to(char* newtop) {
212
assert(CDSConfig::is_dumping_archive(), "sanity");
213
char* base = _rs->base();
214
size_t need_committed_size = newtop - base;
215
size_t has_committed_size = _vs->committed_size();
216
if (need_committed_size < has_committed_size) {
220
size_t min_bytes = need_committed_size - has_committed_size;
221
size_t preferred_bytes = 1 * M;
222
size_t uncommitted = _vs->reserved_size() - has_committed_size;
224
size_t commit = MAX2(min_bytes, preferred_bytes);
225
commit = MIN2(commit, uncommitted);
226
assert(commit <= uncommitted, "sanity");
228
if (!_vs->expand_by(commit, false)) {
229
log_error(cds)("Failed to expand shared space to " SIZE_FORMAT " bytes",
230
need_committed_size);
231
MetaspaceShared::unrecoverable_writing_error();
235
if (_rs->base() == (char*)MetaspaceShared::symbol_rs_base()) {
240
log_debug(cds)("Expanding %s spaces by " SIZE_FORMAT_W(7) " bytes [total " SIZE_FORMAT_W(9) " bytes ending at %p]",
241
which, commit, _vs->actual_committed_size(), _vs->high());
245
char* DumpRegion::allocate(size_t num_bytes) {
246
char* p = (char*)align_up(_top, (size_t)SharedSpaceObjectAlignment);
247
char* newtop = p + align_up(num_bytes, (size_t)SharedSpaceObjectAlignment);
248
expand_top_to(newtop);
249
memset(p, 0, newtop - p);
253
void DumpRegion::append_intptr_t(intptr_t n, bool need_to_mark) {
254
assert(is_aligned(_top, sizeof(intptr_t)), "bad alignment");
255
intptr_t *p = (intptr_t*)_top;
256
char* newtop = _top + sizeof(intptr_t);
257
expand_top_to(newtop);
260
ArchivePtrMarker::mark_pointer(p);
264
void DumpRegion::print(size_t total_bytes) const {
265
log_debug(cds)("%s space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used] at " INTPTR_FORMAT,
266
_name, used(), percent_of(used(), total_bytes), reserved(), percent_of(used(), reserved()),
267
p2i(ArchiveBuilder::current()->to_requested(_base)));
270
void DumpRegion::print_out_of_space_msg(const char* failing_region, size_t needed_bytes) {
271
log_error(cds)("[%-8s] " PTR_FORMAT " - " PTR_FORMAT " capacity =%9d, allocated =%9d",
272
_name, p2i(_base), p2i(_top), int(_end - _base), int(_top - _base));
273
if (strcmp(_name, failing_region) == 0) {
274
log_error(cds)(" required = %d", int(needed_bytes));
278
void DumpRegion::init(ReservedSpace* rs, VirtualSpace* vs) {
282
if (!_vs->initialize(*_rs, 0)) {
283
fatal("Unable to allocate memory for shared space");
285
_base = _top = _rs->base();
289
void DumpRegion::pack(DumpRegion* next) {
290
assert(!is_packed(), "sanity");
291
_end = (char*)align_up(_top, MetaspaceShared::core_region_alignment());
293
if (next != nullptr) {
296
next->_base = next->_top = this->_end;
297
next->_end = _rs->end();
301
void WriteClosure::do_ptr(void** p) {
309
address ptr = *(address*)p;
310
if (ptr != nullptr && !ArchiveBuilder::current()->is_in_buffer_space(ptr)) {
311
ptr = ArchiveBuilder::current()->get_buffered_addr(ptr);
314
if (ptr != nullptr) {
315
ptr = (address)ArchiveBuilder::current()->buffer_to_offset(ptr);
317
_dump_region->append_intptr_t((intptr_t)ptr, false);
320
void ReadClosure::do_ptr(void** p) {
321
assert(*p == nullptr, "initializing previous initialized pointer.");
322
intptr_t obj = nextPtr();
323
assert((intptr_t)obj >= 0 || (intptr_t)obj < -100,
324
"hit tag while initializing ptrs.");
325
*p = (void*)obj != nullptr ? (void*)(SharedBaseAddress + obj) : (void*)obj;
328
void ReadClosure::do_u4(u4* p) {
329
intptr_t obj = nextPtr();
330
*p = (u4)(uintx(obj));
333
void ReadClosure::do_int(int* p) {
334
intptr_t obj = nextPtr();
335
*p = (int)(intx(obj));
338
void ReadClosure::do_bool(bool* p) {
339
intptr_t obj = nextPtr();
340
*p = (bool)(uintx(obj));
343
void ReadClosure::do_tag(int tag) {
345
old_tag = (int)(intptr_t)nextPtr();
347
assert(tag == old_tag, "old tag doesn't match");
348
FileMapInfo::assert_mark(tag == old_tag);
351
void ArchiveUtils::log_to_classlist(BootstrapInfo* bootstrap_specifier, TRAPS) {
352
if (ClassListWriter::is_enabled()) {
353
if (SystemDictionaryShared::is_supported_invokedynamic(bootstrap_specifier)) {
354
const constantPoolHandle& pool = bootstrap_specifier->pool();
355
if (SystemDictionaryShared::is_builtin_loader(pool->pool_holder()->class_loader_data())) {
357
ResourceMark rm(THREAD);
358
int pool_index = bootstrap_specifier->bss_index();
360
w.stream()->print("%s %s", ClassListParser::lambda_proxy_tag(), pool->pool_holder()->name()->as_C_string());
362
ClassListParser::populate_cds_indy_info(pool, pool_index, &cii, CHECK);
363
GrowableArray<const char*>* indy_items = cii.items();
364
for (int i = 0; i < indy_items->length(); i++) {
365
w.stream()->print(" %s", indy_items->at(i));