Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/cds/archiveBuilder.hpp
41144 views
1
/*
2
* Copyright (c) 2020, 2021, 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
*/
24
25
#ifndef SHARE_CDS_ARCHIVEBUILDER_HPP
26
#define SHARE_CDS_ARCHIVEBUILDER_HPP
27
28
#include "cds/archiveUtils.hpp"
29
#include "cds/dumpAllocStats.hpp"
30
#include "memory/metaspaceClosure.hpp"
31
#include "oops/array.hpp"
32
#include "oops/klass.hpp"
33
#include "runtime/os.hpp"
34
#include "utilities/bitMap.hpp"
35
#include "utilities/growableArray.hpp"
36
#include "utilities/hashtable.hpp"
37
#include "utilities/resourceHash.hpp"
38
39
struct ArchiveHeapOopmapInfo;
40
class CHeapBitMap;
41
class FileMapInfo;
42
class Klass;
43
class MemRegion;
44
class Symbol;
45
46
// Metaspace::allocate() requires that all blocks must be aligned with KlassAlignmentInBytes.
47
// We enforce the same alignment rule in blocks allocated from the shared space.
48
const int SharedSpaceObjectAlignment = KlassAlignmentInBytes;
49
50
// Overview of CDS archive creation (for both static and dynamic dump):
51
//
52
// [1] Load all classes (static dump: from the classlist, dynamic dump: as part of app execution)
53
// [2] Allocate "output buffer"
54
// [3] Copy contents of the 2 "core" regions (rw/ro) into the output buffer.
55
// - allocate the cpp vtables in rw (static dump only)
56
// - memcpy the MetaspaceObjs into rw/ro:
57
// dump_rw_region();
58
// dump_ro_region();
59
// - fix all the pointers in the MetaspaceObjs to point to the copies
60
// relocate_metaspaceobj_embedded_pointers()
61
// [4] Copy symbol table, dictionary, etc, into the ro region
62
// [5] Relocate all the pointers in rw/ro, so that the archive can be mapped to
63
// the "requested" location without runtime relocation. See relocate_to_requested()
64
class ArchiveBuilder : public StackObj {
65
protected:
66
DumpRegion* _current_dump_space;
67
address _buffer_bottom; // for writing the contents of rw/ro regions
68
address _last_verified_top;
69
int _num_dump_regions_used;
70
size_t _other_region_used_bytes;
71
72
// These are the addresses where we will request the static and dynamic archives to be
73
// mapped at run time. If the request fails (due to ASLR), we will map the archives at
74
// os-selected addresses.
75
address _requested_static_archive_bottom; // This is determined solely by the value of
76
// SharedBaseAddress during -Xshare:dump.
77
address _requested_static_archive_top;
78
address _requested_dynamic_archive_bottom; // Used only during dynamic dump. It's placed
79
// immediately above _requested_static_archive_top.
80
address _requested_dynamic_archive_top;
81
82
// (Used only during dynamic dump) where the static archive is actually mapped. This
83
// may be different than _requested_static_archive_{bottom,top} due to ASLR
84
address _mapped_static_archive_bottom;
85
address _mapped_static_archive_top;
86
87
intx _buffer_to_requested_delta;
88
89
DumpRegion* current_dump_space() const { return _current_dump_space; }
90
91
public:
92
enum FollowMode {
93
make_a_copy, point_to_it, set_to_null
94
};
95
96
private:
97
class SpecialRefInfo {
98
// We have a "special pointer" of the given _type at _field_offset of _src_obj.
99
// See MetaspaceClosure::push_special().
100
MetaspaceClosure::SpecialRef _type;
101
address _src_obj;
102
size_t _field_offset;
103
104
public:
105
SpecialRefInfo() {}
106
SpecialRefInfo(MetaspaceClosure::SpecialRef type, address src_obj, size_t field_offset)
107
: _type(type), _src_obj(src_obj), _field_offset(field_offset) {}
108
109
MetaspaceClosure::SpecialRef type() const { return _type; }
110
address src_obj() const { return _src_obj; }
111
size_t field_offset() const { return _field_offset; }
112
};
113
114
class SourceObjInfo {
115
MetaspaceClosure::Ref* _ref;
116
uintx _ptrmap_start; // The bit-offset of the start of this object (inclusive)
117
uintx _ptrmap_end; // The bit-offset of the end of this object (exclusive)
118
bool _read_only;
119
FollowMode _follow_mode;
120
int _size_in_bytes;
121
MetaspaceObj::Type _msotype;
122
address _dumped_addr; // Address this->obj(), as used by the dumped archive.
123
address _orig_obj; // The value of the original object (_ref->obj()) when this
124
// SourceObjInfo was created. Note that _ref->obj() may change
125
// later if _ref is relocated.
126
127
public:
128
SourceObjInfo(MetaspaceClosure::Ref* ref, bool read_only, FollowMode follow_mode) :
129
_ref(ref), _ptrmap_start(0), _ptrmap_end(0), _read_only(read_only), _follow_mode(follow_mode),
130
_size_in_bytes(ref->size() * BytesPerWord), _msotype(ref->msotype()),
131
_orig_obj(ref->obj()) {
132
if (follow_mode == point_to_it) {
133
_dumped_addr = ref->obj();
134
} else {
135
_dumped_addr = NULL;
136
}
137
}
138
139
bool should_copy() const { return _follow_mode == make_a_copy; }
140
MetaspaceClosure::Ref* ref() const { return _ref; }
141
void set_dumped_addr(address dumped_addr) {
142
assert(should_copy(), "must be");
143
assert(_dumped_addr == NULL, "cannot be copied twice");
144
assert(dumped_addr != NULL, "must be a valid copy");
145
_dumped_addr = dumped_addr;
146
}
147
void set_ptrmap_start(uintx v) { _ptrmap_start = v; }
148
void set_ptrmap_end(uintx v) { _ptrmap_end = v; }
149
uintx ptrmap_start() const { return _ptrmap_start; } // inclusive
150
uintx ptrmap_end() const { return _ptrmap_end; } // exclusive
151
bool read_only() const { return _read_only; }
152
int size_in_bytes() const { return _size_in_bytes; }
153
address orig_obj() const { return _orig_obj; }
154
address dumped_addr() const { return _dumped_addr; }
155
MetaspaceObj::Type msotype() const { return _msotype; }
156
157
// convenience accessor
158
address obj() const { return ref()->obj(); }
159
};
160
161
class SourceObjList {
162
uintx _total_bytes;
163
GrowableArray<SourceObjInfo*>* _objs; // Source objects to be archived
164
CHeapBitMap _ptrmap; // Marks the addresses of the pointer fields
165
// in the source objects
166
public:
167
SourceObjList();
168
~SourceObjList();
169
170
GrowableArray<SourceObjInfo*>* objs() const { return _objs; }
171
172
void append(MetaspaceClosure::Ref* enclosing_ref, SourceObjInfo* src_info);
173
void remember_embedded_pointer(SourceObjInfo* pointing_obj, MetaspaceClosure::Ref* ref);
174
void relocate(int i, ArchiveBuilder* builder);
175
176
// convenience accessor
177
SourceObjInfo* at(int i) const { return objs()->at(i); }
178
};
179
180
class SrcObjTableCleaner {
181
public:
182
bool do_entry(address key, const SourceObjInfo* value) {
183
delete value->ref();
184
return true;
185
}
186
};
187
188
class CDSMapLogger;
189
190
static const int INITIAL_TABLE_SIZE = 15889;
191
static const int MAX_TABLE_SIZE = 1000000;
192
193
ReservedSpace _shared_rs;
194
VirtualSpace _shared_vs;
195
196
DumpRegion _rw_region;
197
DumpRegion _ro_region;
198
CHeapBitMap _ptrmap; // bitmap used by ArchivePtrMarker
199
200
SourceObjList _rw_src_objs; // objs to put in rw region
201
SourceObjList _ro_src_objs; // objs to put in ro region
202
KVHashtable<address, SourceObjInfo, mtClassShared> _src_obj_table;
203
GrowableArray<Klass*>* _klasses;
204
GrowableArray<Symbol*>* _symbols;
205
GrowableArray<SpecialRefInfo>* _special_refs;
206
207
// statistics
208
int _num_instance_klasses;
209
int _num_obj_array_klasses;
210
int _num_type_array_klasses;
211
DumpAllocStats _alloc_stats;
212
size_t _total_closed_heap_region_size;
213
size_t _total_open_heap_region_size;
214
215
void print_region_stats(FileMapInfo *map_info,
216
GrowableArray<MemRegion>* closed_heap_regions,
217
GrowableArray<MemRegion>* open_heap_regions);
218
void print_bitmap_region_stats(size_t size, size_t total_size);
219
void print_heap_region_stats(GrowableArray<MemRegion> *heap_mem,
220
const char *name, size_t total_size);
221
222
// For global access.
223
static ArchiveBuilder* _current;
224
225
public:
226
// Use this when you allocate space outside of ArchiveBuilder::dump_{rw,ro}_region.
227
// These are usually for misc tables that are allocated in the RO space.
228
class OtherROAllocMark {
229
char* _oldtop;
230
public:
231
OtherROAllocMark() {
232
_oldtop = _current->_ro_region.top();
233
}
234
~OtherROAllocMark();
235
};
236
237
private:
238
bool is_dumping_full_module_graph();
239
FollowMode get_follow_mode(MetaspaceClosure::Ref *ref);
240
241
void iterate_sorted_roots(MetaspaceClosure* it, bool is_relocating_pointers);
242
void sort_symbols_and_fix_hash();
243
void sort_klasses();
244
static int compare_symbols_by_address(Symbol** a, Symbol** b);
245
static int compare_klass_by_name(Klass** a, Klass** b);
246
247
void make_shallow_copies(DumpRegion *dump_region, const SourceObjList* src_objs);
248
void make_shallow_copy(DumpRegion *dump_region, SourceObjInfo* src_info);
249
250
void update_special_refs();
251
void relocate_embedded_pointers(SourceObjList* src_objs);
252
253
bool is_excluded(Klass* k);
254
void clean_up_src_obj_table();
255
256
protected:
257
virtual void iterate_roots(MetaspaceClosure* it, bool is_relocating_pointers) = 0;
258
259
// Conservative estimate for number of bytes needed for:
260
size_t _estimated_metaspaceobj_bytes; // all archived MetaspaceObj's.
261
size_t _estimated_hashtable_bytes; // symbol table and dictionaries
262
263
static const int _total_dump_regions = 2;
264
265
size_t estimate_archive_size();
266
267
void start_dump_space(DumpRegion* next);
268
void verify_estimate_size(size_t estimate, const char* which);
269
270
public:
271
address reserve_buffer();
272
273
address buffer_bottom() const { return _buffer_bottom; }
274
address buffer_top() const { return (address)current_dump_space()->top(); }
275
address requested_static_archive_bottom() const { return _requested_static_archive_bottom; }
276
address mapped_static_archive_bottom() const { return _mapped_static_archive_bottom; }
277
intx buffer_to_requested_delta() const { return _buffer_to_requested_delta; }
278
279
bool is_in_buffer_space(address p) const {
280
return (buffer_bottom() <= p && p < buffer_top());
281
}
282
283
template <typename T> bool is_in_requested_static_archive(T p) const {
284
return _requested_static_archive_bottom <= (address)p && (address)p < _requested_static_archive_top;
285
}
286
287
template <typename T> bool is_in_mapped_static_archive(T p) const {
288
return _mapped_static_archive_bottom <= (address)p && (address)p < _mapped_static_archive_top;
289
}
290
291
template <typename T> bool is_in_buffer_space(T obj) const {
292
return is_in_buffer_space(address(obj));
293
}
294
295
template <typename T> T to_requested(T obj) const {
296
assert(is_in_buffer_space(obj), "must be");
297
return (T)(address(obj) + _buffer_to_requested_delta);
298
}
299
300
static intx get_buffer_to_requested_delta() {
301
return current()->buffer_to_requested_delta();
302
}
303
304
public:
305
static const uintx MAX_SHARED_DELTA = 0x7FFFFFFF;
306
307
// The address p points to an object inside the output buffer. When the archive is mapped
308
// at the requested address, what's the offset of this object from _requested_static_archive_bottom?
309
uintx buffer_to_offset(address p) const;
310
311
// Same as buffer_to_offset, except that the address p points to either (a) an object
312
// inside the output buffer, or (b), an object in the currently mapped static archive.
313
uintx any_to_offset(address p) const;
314
315
template <typename T>
316
u4 buffer_to_offset_u4(T p) const {
317
uintx offset = buffer_to_offset((address)p);
318
guarantee(offset <= MAX_SHARED_DELTA, "must be 32-bit offset");
319
return (u4)offset;
320
}
321
322
template <typename T>
323
u4 any_to_offset_u4(T p) const {
324
uintx offset = any_to_offset((address)p);
325
guarantee(offset <= MAX_SHARED_DELTA, "must be 32-bit offset");
326
return (u4)offset;
327
}
328
329
static void assert_is_vm_thread() PRODUCT_RETURN;
330
331
public:
332
ArchiveBuilder();
333
~ArchiveBuilder();
334
335
void gather_klasses_and_symbols();
336
void gather_source_objs();
337
bool gather_klass_and_symbol(MetaspaceClosure::Ref* ref, bool read_only);
338
bool gather_one_source_obj(MetaspaceClosure::Ref* enclosing_ref, MetaspaceClosure::Ref* ref, bool read_only);
339
void add_special_ref(MetaspaceClosure::SpecialRef type, address src_obj, size_t field_offset);
340
void remember_embedded_pointer_in_copied_obj(MetaspaceClosure::Ref* enclosing_ref, MetaspaceClosure::Ref* ref);
341
342
DumpRegion* rw_region() { return &_rw_region; }
343
DumpRegion* ro_region() { return &_ro_region; }
344
345
static char* rw_region_alloc(size_t num_bytes) {
346
return current()->rw_region()->allocate(num_bytes);
347
}
348
static char* ro_region_alloc(size_t num_bytes) {
349
return current()->ro_region()->allocate(num_bytes);
350
}
351
352
template <typename T>
353
static Array<T>* new_ro_array(int length) {
354
size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
355
Array<T>* array = (Array<T>*)ro_region_alloc(byte_size);
356
array->initialize(length);
357
return array;
358
}
359
360
template <typename T>
361
static Array<T>* new_rw_array(int length) {
362
size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
363
Array<T>* array = (Array<T>*)rw_region_alloc(byte_size);
364
array->initialize(length);
365
return array;
366
}
367
368
template <typename T>
369
static size_t ro_array_bytesize(int length) {
370
size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
371
return align_up(byte_size, SharedSpaceObjectAlignment);
372
}
373
374
void dump_rw_metadata();
375
void dump_ro_metadata();
376
void relocate_metaspaceobj_embedded_pointers();
377
void relocate_roots();
378
void relocate_vm_classes();
379
void make_klasses_shareable();
380
void relocate_to_requested();
381
void write_archive(FileMapInfo* mapinfo,
382
GrowableArray<MemRegion>* closed_heap_regions,
383
GrowableArray<MemRegion>* open_heap_regions,
384
GrowableArray<ArchiveHeapOopmapInfo>* closed_heap_oopmaps,
385
GrowableArray<ArchiveHeapOopmapInfo>* open_heap_oopmaps);
386
void write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region,
387
bool read_only, bool allow_exec);
388
389
address get_dumped_addr(address src_obj) const;
390
391
// All klasses and symbols that will be copied into the archive
392
GrowableArray<Klass*>* klasses() const { return _klasses; }
393
GrowableArray<Symbol*>* symbols() const { return _symbols; }
394
395
static bool is_active() {
396
return (_current != NULL);
397
}
398
399
static ArchiveBuilder* current() {
400
assert_is_vm_thread();
401
assert(_current != NULL, "ArchiveBuilder must be active");
402
return _current;
403
}
404
405
static DumpAllocStats* alloc_stats() {
406
return &(current()->_alloc_stats);
407
}
408
409
static CompactHashtableStats* symbol_stats() {
410
return alloc_stats()->symbol_stats();
411
}
412
413
static CompactHashtableStats* string_stats() {
414
return alloc_stats()->string_stats();
415
}
416
417
void relocate_klass_ptr(oop o);
418
419
static Klass* get_relocated_klass(Klass* orig_klass) {
420
Klass* klass = (Klass*)current()->get_dumped_addr((address)orig_klass);
421
assert(klass != NULL && klass->is_klass(), "must be");
422
return klass;
423
}
424
425
static Symbol* get_relocated_symbol(Symbol* orig_symbol) {
426
return (Symbol*)current()->get_dumped_addr((address)orig_symbol);
427
}
428
429
void print_stats();
430
void report_out_of_space(const char* name, size_t needed_bytes);
431
};
432
433
#endif // SHARE_CDS_ARCHIVEBUILDER_HPP
434
435