Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/cds/heapShared.hpp
41144 views
1
/*
2
* Copyright (c) 2018, 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_HEAPSHARED_HPP
26
#define SHARE_CDS_HEAPSHARED_HPP
27
28
#include "cds/metaspaceShared.hpp"
29
#include "classfile/compactHashtable.hpp"
30
#include "classfile/javaClasses.hpp"
31
#include "classfile/systemDictionary.hpp"
32
#include "gc/shared/gc_globals.hpp"
33
#include "memory/allocation.hpp"
34
#include "oops/compressedOops.hpp"
35
#include "oops/objArrayKlass.hpp"
36
#include "oops/oop.hpp"
37
#include "oops/oopHandle.hpp"
38
#include "oops/typeArrayKlass.hpp"
39
#include "utilities/bitMap.hpp"
40
#include "utilities/growableArray.hpp"
41
#include "utilities/resourceHash.hpp"
42
43
#if INCLUDE_CDS_JAVA_HEAP
44
class DumpedInternedStrings;
45
46
struct ArchivableStaticFieldInfo {
47
const char* klass_name;
48
const char* field_name;
49
InstanceKlass* klass;
50
int offset;
51
BasicType type;
52
};
53
54
// A dump time sub-graph info for Klass _k. It includes the entry points
55
// (static fields in _k's mirror) of the archived sub-graphs reachable
56
// from _k's mirror. It also contains a list of Klasses of the objects
57
// within the sub-graphs.
58
class KlassSubGraphInfo: public CHeapObj<mtClass> {
59
private:
60
// The class that contains the static field(s) as the entry point(s)
61
// of archived object sub-graph(s).
62
Klass* _k;
63
// A list of classes need to be loaded and initialized before the archived
64
// object sub-graphs can be accessed at runtime.
65
GrowableArray<Klass*>* _subgraph_object_klasses;
66
// A list of _k's static fields as the entry points of archived sub-graphs.
67
// For each entry field, it is a tuple of field_offset, field_value and
68
// is_closed_archive flag.
69
GrowableArray<int>* _subgraph_entry_fields;
70
71
// Does this KlassSubGraphInfo belong to the archived full module graph
72
bool _is_full_module_graph;
73
74
// Does this KlassSubGraphInfo references any classes that were loaded while
75
// JvmtiExport::is_early_phase()!=true. If so, this KlassSubGraphInfo cannot be
76
// used at runtime if JVMTI ClassFileLoadHook is enabled.
77
bool _has_non_early_klasses;
78
static bool is_non_early_klass(Klass* k);
79
80
public:
81
KlassSubGraphInfo(Klass* k, bool is_full_module_graph) :
82
_k(k), _subgraph_object_klasses(NULL),
83
_subgraph_entry_fields(NULL),
84
_is_full_module_graph(is_full_module_graph),
85
_has_non_early_klasses(false) {}
86
87
~KlassSubGraphInfo() {
88
if (_subgraph_object_klasses != NULL) {
89
delete _subgraph_object_klasses;
90
}
91
if (_subgraph_entry_fields != NULL) {
92
delete _subgraph_entry_fields;
93
}
94
};
95
96
Klass* klass() { return _k; }
97
GrowableArray<Klass*>* subgraph_object_klasses() {
98
return _subgraph_object_klasses;
99
}
100
GrowableArray<int>* subgraph_entry_fields() {
101
return _subgraph_entry_fields;
102
}
103
void add_subgraph_entry_field(int static_field_offset, oop v,
104
bool is_closed_archive);
105
void add_subgraph_object_klass(Klass *orig_k);
106
int num_subgraph_object_klasses() {
107
return _subgraph_object_klasses == NULL ? 0 :
108
_subgraph_object_klasses->length();
109
}
110
bool is_full_module_graph() const { return _is_full_module_graph; }
111
bool has_non_early_klasses() const { return _has_non_early_klasses; }
112
};
113
114
// An archived record of object sub-graphs reachable from static
115
// fields within _k's mirror. The record is reloaded from the archive
116
// at runtime.
117
class ArchivedKlassSubGraphInfoRecord {
118
private:
119
Klass* _k;
120
bool _is_full_module_graph;
121
bool _has_non_early_klasses;
122
123
// contains pairs of field offset and value for each subgraph entry field
124
Array<int>* _entry_field_records;
125
126
// klasses of objects in archived sub-graphs referenced from the entry points
127
// (static fields) in the containing class
128
Array<Klass*>* _subgraph_object_klasses;
129
public:
130
ArchivedKlassSubGraphInfoRecord() :
131
_k(NULL), _entry_field_records(NULL), _subgraph_object_klasses(NULL) {}
132
void init(KlassSubGraphInfo* info);
133
Klass* klass() const { return _k; }
134
Array<int>* entry_field_records() const { return _entry_field_records; }
135
Array<Klass*>* subgraph_object_klasses() const { return _subgraph_object_klasses; }
136
bool is_full_module_graph() const { return _is_full_module_graph; }
137
bool has_non_early_klasses() const { return _has_non_early_klasses; }
138
};
139
#endif // INCLUDE_CDS_JAVA_HEAP
140
141
class HeapShared: AllStatic {
142
friend class VerifySharedOopClosure;
143
private:
144
145
#if INCLUDE_CDS_JAVA_HEAP
146
static bool _closed_archive_heap_region_mapped;
147
static bool _open_archive_heap_region_mapped;
148
static bool _archive_heap_region_fixed;
149
static DumpedInternedStrings *_dumped_interned_strings;
150
151
public:
152
static bool oop_equals(oop const& p1, oop const& p2) {
153
return p1 == p2;
154
}
155
static unsigned oop_hash(oop const& p);
156
static unsigned string_oop_hash(oop const& string) {
157
return java_lang_String::hash_code(string);
158
}
159
160
private:
161
typedef ResourceHashtable<oop, oop,
162
HeapShared::oop_hash,
163
HeapShared::oop_equals,
164
15889, // prime number
165
ResourceObj::C_HEAP> ArchivedObjectCache;
166
static ArchivedObjectCache* _archived_object_cache;
167
168
static bool klass_equals(Klass* const& p1, Klass* const& p2) {
169
return primitive_equals<Klass*>(p1, p2);
170
}
171
172
static unsigned klass_hash(Klass* const& klass) {
173
// Generate deterministic hashcode even if SharedBaseAddress is changed due to ASLR.
174
return primitive_hash<address>(address(klass) - SharedBaseAddress);
175
}
176
177
class DumpTimeKlassSubGraphInfoTable
178
: public ResourceHashtable<Klass*, KlassSubGraphInfo,
179
HeapShared::klass_hash,
180
HeapShared::klass_equals,
181
137, // prime number
182
ResourceObj::C_HEAP> {
183
public:
184
int _count;
185
};
186
187
public: // solaris compiler wants this for RunTimeKlassSubGraphInfoTable
188
inline static bool record_equals_compact_hashtable_entry(
189
const ArchivedKlassSubGraphInfoRecord* value, const Klass* key, int len_unused) {
190
return (value->klass() == key);
191
}
192
193
private:
194
typedef OffsetCompactHashtable<
195
const Klass*,
196
const ArchivedKlassSubGraphInfoRecord*,
197
record_equals_compact_hashtable_entry
198
> RunTimeKlassSubGraphInfoTable;
199
200
static DumpTimeKlassSubGraphInfoTable* _dump_time_subgraph_info_table;
201
static RunTimeKlassSubGraphInfoTable _run_time_subgraph_info_table;
202
203
static void check_closed_archive_heap_region_object(InstanceKlass* k);
204
205
static void archive_object_subgraphs(ArchivableStaticFieldInfo fields[],
206
int num,
207
bool is_closed_archive,
208
bool is_full_module_graph);
209
210
// Archive object sub-graph starting from the given static field
211
// in Klass k's mirror.
212
static void archive_reachable_objects_from_static_field(
213
InstanceKlass* k, const char* klass_name,
214
int field_offset, const char* field_name,
215
bool is_closed_archive);
216
217
static void verify_subgraph_from_static_field(
218
InstanceKlass* k, int field_offset) PRODUCT_RETURN;
219
static void verify_reachable_objects_from(oop obj, bool is_archived) PRODUCT_RETURN;
220
static void verify_subgraph_from(oop orig_obj) PRODUCT_RETURN;
221
222
static KlassSubGraphInfo* init_subgraph_info(Klass *k, bool is_full_module_graph);
223
static KlassSubGraphInfo* get_subgraph_info(Klass *k);
224
225
static void init_subgraph_entry_fields(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
226
static void init_subgraph_entry_fields(ArchivableStaticFieldInfo fields[],
227
int num, TRAPS);
228
229
// Used by decode_from_archive
230
static address _narrow_oop_base;
231
static int _narrow_oop_shift;
232
233
typedef ResourceHashtable<oop, bool,
234
HeapShared::oop_hash,
235
HeapShared::oop_equals,
236
15889, // prime number
237
ResourceObj::C_HEAP> SeenObjectsTable;
238
239
static SeenObjectsTable *_seen_objects_table;
240
241
static GrowableArrayCHeap<oop, mtClassShared>* _pending_roots;
242
static narrowOop _roots_narrow;
243
static OopHandle _roots;
244
245
static void init_seen_objects_table() {
246
assert(_seen_objects_table == NULL, "must be");
247
_seen_objects_table = new (ResourceObj::C_HEAP, mtClass)SeenObjectsTable();
248
}
249
static void delete_seen_objects_table() {
250
assert(_seen_objects_table != NULL, "must be");
251
delete _seen_objects_table;
252
_seen_objects_table = NULL;
253
}
254
255
// Statistics (for one round of start_recording_subgraph ... done_recording_subgraph)
256
static int _num_new_walked_objs;
257
static int _num_new_archived_objs;
258
static int _num_old_recorded_klasses;
259
260
// Statistics (for all archived subgraphs)
261
static int _num_total_subgraph_recordings;
262
static int _num_total_walked_objs;
263
static int _num_total_archived_objs;
264
static int _num_total_recorded_klasses;
265
static int _num_total_verifications;
266
267
static void start_recording_subgraph(InstanceKlass *k, const char* klass_name,
268
bool is_full_module_graph);
269
static void done_recording_subgraph(InstanceKlass *k, const char* klass_name);
270
271
static bool has_been_seen_during_subgraph_recording(oop obj);
272
static void set_has_been_seen_during_subgraph_recording(oop obj);
273
274
static void check_module_oop(oop orig_module_obj);
275
static void copy_roots();
276
277
static void resolve_classes_for_subgraphs(ArchivableStaticFieldInfo fields[],
278
int num, JavaThread* THREAD);
279
static void resolve_classes_for_subgraph_of(Klass* k, JavaThread* THREAD);
280
static void clear_archived_roots_of(Klass* k);
281
static const ArchivedKlassSubGraphInfoRecord*
282
resolve_or_init_classes_for_subgraph_of(Klass* k, bool do_init, TRAPS);
283
static void resolve_or_init(Klass* k, bool do_init, TRAPS);
284
static void init_archived_fields_for(Klass* k, const ArchivedKlassSubGraphInfoRecord* record);
285
public:
286
static void reset_archived_object_states(TRAPS);
287
static void create_archived_object_cache() {
288
_archived_object_cache =
289
new (ResourceObj::C_HEAP, mtClass)ArchivedObjectCache();
290
}
291
static void destroy_archived_object_cache() {
292
delete _archived_object_cache;
293
_archived_object_cache = NULL;
294
}
295
static ArchivedObjectCache* archived_object_cache() {
296
return _archived_object_cache;
297
}
298
299
static oop find_archived_heap_object(oop obj);
300
static oop archive_heap_object(oop obj);
301
302
static void archive_klass_objects();
303
304
static void set_archive_heap_region_fixed() {
305
_archive_heap_region_fixed = true;
306
}
307
static bool archive_heap_region_fixed() {
308
return _archive_heap_region_fixed;
309
}
310
311
static void archive_java_heap_objects(GrowableArray<MemRegion> *closed,
312
GrowableArray<MemRegion> *open);
313
static void copy_closed_archive_heap_objects(GrowableArray<MemRegion> * closed_archive);
314
static void copy_open_archive_heap_objects(GrowableArray<MemRegion> * open_archive);
315
316
static oop archive_reachable_objects_from(int level,
317
KlassSubGraphInfo* subgraph_info,
318
oop orig_obj,
319
bool is_closed_archive);
320
321
static ResourceBitMap calculate_oopmap(MemRegion region);
322
static void add_to_dumped_interned_strings(oop string);
323
324
// We use the HeapShared::roots() array to make sure that objects stored in the
325
// archived heap regions are not prematurely collected. These roots include:
326
//
327
// - mirrors of classes that have not yet been loaded.
328
// - ConstantPool::resolved_references() of classes that have not yet been loaded.
329
// - ArchivedKlassSubGraphInfoRecords that have not been initialized
330
// - java.lang.Module objects that have not yet been added to the module graph
331
//
332
// When a mirror M becomes referenced by a newly loaded class K, M will be removed
333
// from HeapShared::roots() via clear_root(), and K will be responsible for
334
// keeping M alive.
335
//
336
// Other types of roots are also cleared similarly when they become referenced.
337
338
// Dump-time only. Returns the index of the root, which can be used at run time to read
339
// the root using get_root(index, ...).
340
static int append_root(oop obj);
341
342
// Dump-time and runtime
343
static objArrayOop roots();
344
static oop get_root(int index, bool clear=false);
345
346
// Run-time only
347
static void set_roots(narrowOop roots);
348
static void clear_root(int index);
349
#endif // INCLUDE_CDS_JAVA_HEAP
350
351
public:
352
static void run_full_gc_in_vm_thread() NOT_CDS_JAVA_HEAP_RETURN;
353
354
static bool is_heap_object_archiving_allowed() {
355
CDS_JAVA_HEAP_ONLY(return (UseG1GC && UseCompressedOops && UseCompressedClassPointers);)
356
NOT_CDS_JAVA_HEAP(return false;)
357
}
358
359
static bool is_heap_region(int idx) {
360
CDS_JAVA_HEAP_ONLY(return (idx >= MetaspaceShared::first_closed_archive_heap_region &&
361
idx <= MetaspaceShared::last_open_archive_heap_region);)
362
NOT_CDS_JAVA_HEAP_RETURN_(false);
363
}
364
365
static void set_closed_archive_heap_region_mapped() {
366
CDS_JAVA_HEAP_ONLY(_closed_archive_heap_region_mapped = true;)
367
NOT_CDS_JAVA_HEAP_RETURN;
368
}
369
static bool closed_archive_heap_region_mapped() {
370
CDS_JAVA_HEAP_ONLY(return _closed_archive_heap_region_mapped;)
371
NOT_CDS_JAVA_HEAP_RETURN_(false);
372
}
373
static void set_open_archive_heap_region_mapped() {
374
CDS_JAVA_HEAP_ONLY(_open_archive_heap_region_mapped = true;)
375
NOT_CDS_JAVA_HEAP_RETURN;
376
}
377
static bool open_archive_heap_region_mapped() {
378
CDS_JAVA_HEAP_ONLY(return _open_archive_heap_region_mapped;)
379
NOT_CDS_JAVA_HEAP_RETURN_(false);
380
}
381
static bool is_mapped() {
382
return closed_archive_heap_region_mapped() && open_archive_heap_region_mapped();
383
}
384
385
static void fixup_mapped_heap_regions() NOT_CDS_JAVA_HEAP_RETURN;
386
387
inline static bool is_archived_object(oop p) NOT_CDS_JAVA_HEAP_RETURN_(false);
388
389
static void resolve_classes(JavaThread* THREAD) NOT_CDS_JAVA_HEAP_RETURN;
390
static void initialize_from_archived_subgraph(Klass* k, JavaThread* THREAD) NOT_CDS_JAVA_HEAP_RETURN;
391
392
// NarrowOops stored in the CDS archive may use a different encoding scheme
393
// than CompressedOops::{base,shift} -- see FileMapInfo::map_heap_regions_impl.
394
// To decode them, do not use CompressedOops::decode_not_null. Use this
395
// function instead.
396
inline static oop decode_from_archive(narrowOop v) NOT_CDS_JAVA_HEAP_RETURN_(NULL);
397
398
static void init_narrow_oop_decoding(address base, int shift) NOT_CDS_JAVA_HEAP_RETURN;
399
400
static void patch_archived_heap_embedded_pointers(MemRegion mem, address oopmap,
401
size_t oopmap_in_bits) NOT_CDS_JAVA_HEAP_RETURN;
402
403
static void init_for_dumping(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
404
static void write_subgraph_info_table() NOT_CDS_JAVA_HEAP_RETURN;
405
static void serialize_subgraph_info_table_header(SerializeClosure* soc) NOT_CDS_JAVA_HEAP_RETURN;
406
};
407
408
#if INCLUDE_CDS_JAVA_HEAP
409
class DumpedInternedStrings :
410
public ResourceHashtable<oop, bool,
411
HeapShared::string_oop_hash,
412
HeapShared::oop_equals,
413
15889, // prime number
414
ResourceObj::C_HEAP>
415
{};
416
#endif
417
418
#endif // SHARE_CDS_HEAPSHARED_HPP
419
420