| 1 | /* | 
|---|
| 2 | * Copyright (c) 2018, 2019, 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_MEMORY_HEAPSHARED_HPP | 
|---|
| 26 | #define SHARE_MEMORY_HEAPSHARED_HPP | 
|---|
| 27 |  | 
|---|
| 28 | #include "classfile/compactHashtable.hpp" | 
|---|
| 29 | #include "classfile/systemDictionary.hpp" | 
|---|
| 30 | #include "memory/allocation.hpp" | 
|---|
| 31 | #include "memory/metaspaceShared.hpp" | 
|---|
| 32 | #include "oops/compressedOops.hpp" | 
|---|
| 33 | #include "oops/objArrayKlass.hpp" | 
|---|
| 34 | #include "oops/oop.hpp" | 
|---|
| 35 | #include "oops/typeArrayKlass.hpp" | 
|---|
| 36 | #include "utilities/bitMap.hpp" | 
|---|
| 37 | #include "utilities/growableArray.hpp" | 
|---|
| 38 | #include "utilities/resourceHash.hpp" | 
|---|
| 39 |  | 
|---|
| 40 | #if INCLUDE_CDS_JAVA_HEAP | 
|---|
| 41 | struct ArchivableStaticFieldInfo { | 
|---|
| 42 | const char* klass_name; | 
|---|
| 43 | const char* field_name; | 
|---|
| 44 | InstanceKlass* klass; | 
|---|
| 45 | int offset; | 
|---|
| 46 | BasicType type; | 
|---|
| 47 | }; | 
|---|
| 48 |  | 
|---|
| 49 | // A dump time sub-graph info for Klass _k. It includes the entry points | 
|---|
| 50 | // (static fields in _k's mirror) of the archived sub-graphs reachable | 
|---|
| 51 | // from _k's mirror. It also contains a list of Klasses of the objects | 
|---|
| 52 | // within the sub-graphs. | 
|---|
| 53 | class KlassSubGraphInfo: public CHeapObj<mtClass> { | 
|---|
| 54 | private: | 
|---|
| 55 | // The class that contains the static field(s) as the entry point(s) | 
|---|
| 56 | // of archived object sub-graph(s). | 
|---|
| 57 | Klass* _k; | 
|---|
| 58 | // A list of classes need to be loaded and initialized before the archived | 
|---|
| 59 | // object sub-graphs can be accessed at runtime. | 
|---|
| 60 | GrowableArray<Klass*>* _subgraph_object_klasses; | 
|---|
| 61 | // A list of _k's static fields as the entry points of archived sub-graphs. | 
|---|
| 62 | // For each entry field, it is a tuple of field_offset, field_value and | 
|---|
| 63 | // is_closed_archive flag. | 
|---|
| 64 | GrowableArray<juint>*  _subgraph_entry_fields; | 
|---|
| 65 |  | 
|---|
| 66 | public: | 
|---|
| 67 | KlassSubGraphInfo(Klass* k) : | 
|---|
| 68 | _k(k),  _subgraph_object_klasses(NULL), | 
|---|
| 69 | _subgraph_entry_fields(NULL) {} | 
|---|
| 70 | ~KlassSubGraphInfo() { | 
|---|
| 71 | if (_subgraph_object_klasses != NULL) { | 
|---|
| 72 | delete _subgraph_object_klasses; | 
|---|
| 73 | } | 
|---|
| 74 | if (_subgraph_entry_fields != NULL) { | 
|---|
| 75 | delete _subgraph_entry_fields; | 
|---|
| 76 | } | 
|---|
| 77 | }; | 
|---|
| 78 |  | 
|---|
| 79 | Klass* klass()            { return _k; } | 
|---|
| 80 | GrowableArray<Klass*>* subgraph_object_klasses() { | 
|---|
| 81 | return _subgraph_object_klasses; | 
|---|
| 82 | } | 
|---|
| 83 | GrowableArray<juint>*  subgraph_entry_fields() { | 
|---|
| 84 | return _subgraph_entry_fields; | 
|---|
| 85 | } | 
|---|
| 86 | void add_subgraph_entry_field(int static_field_offset, oop v, | 
|---|
| 87 | bool is_closed_archive); | 
|---|
| 88 | void add_subgraph_object_klass(Klass *orig_k, Klass *relocated_k); | 
|---|
| 89 | int num_subgraph_object_klasses() { | 
|---|
| 90 | return _subgraph_object_klasses == NULL ? 0 : | 
|---|
| 91 | _subgraph_object_klasses->length(); | 
|---|
| 92 | } | 
|---|
| 93 | }; | 
|---|
| 94 |  | 
|---|
| 95 | // An archived record of object sub-graphs reachable from static | 
|---|
| 96 | // fields within _k's mirror. The record is reloaded from the archive | 
|---|
| 97 | // at runtime. | 
|---|
| 98 | class ArchivedKlassSubGraphInfoRecord { | 
|---|
| 99 | private: | 
|---|
| 100 | Klass* _k; | 
|---|
| 101 |  | 
|---|
| 102 | // contains pairs of field offset and value for each subgraph entry field | 
|---|
| 103 | Array<juint>* _entry_field_records; | 
|---|
| 104 |  | 
|---|
| 105 | // klasses of objects in archived sub-graphs referenced from the entry points | 
|---|
| 106 | // (static fields) in the containing class | 
|---|
| 107 | Array<Klass*>* _subgraph_object_klasses; | 
|---|
| 108 | public: | 
|---|
| 109 | ArchivedKlassSubGraphInfoRecord() : | 
|---|
| 110 | _k(NULL), _entry_field_records(NULL), _subgraph_object_klasses(NULL) {} | 
|---|
| 111 | void init(KlassSubGraphInfo* info); | 
|---|
| 112 | Klass* klass() const { return _k; } | 
|---|
| 113 | Array<juint>*  entry_field_records() const { return _entry_field_records; } | 
|---|
| 114 | Array<Klass*>* subgraph_object_klasses() const { return _subgraph_object_klasses; } | 
|---|
| 115 | }; | 
|---|
| 116 | #endif // INCLUDE_CDS_JAVA_HEAP | 
|---|
| 117 |  | 
|---|
| 118 | class HeapShared: AllStatic { | 
|---|
| 119 | friend class VerifySharedOopClosure; | 
|---|
| 120 | private: | 
|---|
| 121 |  | 
|---|
| 122 | #if INCLUDE_CDS_JAVA_HEAP | 
|---|
| 123 | static bool _closed_archive_heap_region_mapped; | 
|---|
| 124 | static bool _open_archive_heap_region_mapped; | 
|---|
| 125 | static bool _archive_heap_region_fixed; | 
|---|
| 126 |  | 
|---|
| 127 | static bool oop_equals(oop const& p1, oop const& p2) { | 
|---|
| 128 | return oopDesc::equals(p1, p2); | 
|---|
| 129 | } | 
|---|
| 130 | static unsigned oop_hash(oop const& p); | 
|---|
| 131 |  | 
|---|
| 132 | typedef ResourceHashtable<oop, oop, | 
|---|
| 133 | HeapShared::oop_hash, | 
|---|
| 134 | HeapShared::oop_equals, | 
|---|
| 135 | 15889, // prime number | 
|---|
| 136 | ResourceObj::C_HEAP> ArchivedObjectCache; | 
|---|
| 137 | static ArchivedObjectCache* _archived_object_cache; | 
|---|
| 138 |  | 
|---|
| 139 | static bool klass_equals(Klass* const& p1, Klass* const& p2) { | 
|---|
| 140 | return primitive_equals<Klass*>(p1, p2); | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | static unsigned klass_hash(Klass* const& klass) { | 
|---|
| 144 | return primitive_hash<address>((address)klass); | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | class DumpTimeKlassSubGraphInfoTable | 
|---|
| 148 | : public ResourceHashtable<Klass*, KlassSubGraphInfo, | 
|---|
| 149 | HeapShared::klass_hash, | 
|---|
| 150 | HeapShared::klass_equals, | 
|---|
| 151 | 137, // prime number | 
|---|
| 152 | ResourceObj::C_HEAP> { | 
|---|
| 153 | public: | 
|---|
| 154 | int _count; | 
|---|
| 155 | }; | 
|---|
| 156 |  | 
|---|
| 157 | public: // solaris compiler wants this for RunTimeKlassSubGraphInfoTable | 
|---|
| 158 | inline static bool record_equals_compact_hashtable_entry( | 
|---|
| 159 | const ArchivedKlassSubGraphInfoRecord* value, const Klass* key, int len_unused) { | 
|---|
| 160 | return (value->klass() == key); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | private: | 
|---|
| 164 | typedef OffsetCompactHashtable< | 
|---|
| 165 | const Klass*, | 
|---|
| 166 | const ArchivedKlassSubGraphInfoRecord*, | 
|---|
| 167 | record_equals_compact_hashtable_entry | 
|---|
| 168 | > RunTimeKlassSubGraphInfoTable; | 
|---|
| 169 |  | 
|---|
| 170 | static DumpTimeKlassSubGraphInfoTable* _dump_time_subgraph_info_table; | 
|---|
| 171 | static RunTimeKlassSubGraphInfoTable _run_time_subgraph_info_table; | 
|---|
| 172 |  | 
|---|
| 173 | static void check_closed_archive_heap_region_object(InstanceKlass* k, | 
|---|
| 174 | Thread* THREAD); | 
|---|
| 175 |  | 
|---|
| 176 | static void archive_object_subgraphs(ArchivableStaticFieldInfo fields[], | 
|---|
| 177 | int num, | 
|---|
| 178 | bool is_closed_archive, | 
|---|
| 179 | Thread* THREAD); | 
|---|
| 180 |  | 
|---|
| 181 | // Archive object sub-graph starting from the given static field | 
|---|
| 182 | // in Klass k's mirror. | 
|---|
| 183 | static void archive_reachable_objects_from_static_field( | 
|---|
| 184 | InstanceKlass* k, const char* klass_name, | 
|---|
| 185 | int field_offset, const char* field_name, | 
|---|
| 186 | bool is_closed_archive, TRAPS); | 
|---|
| 187 |  | 
|---|
| 188 | static void verify_subgraph_from_static_field( | 
|---|
| 189 | InstanceKlass* k, int field_offset) PRODUCT_RETURN; | 
|---|
| 190 | static void verify_reachable_objects_from(oop obj, bool is_archived) PRODUCT_RETURN; | 
|---|
| 191 | static void verify_subgraph_from(oop orig_obj) PRODUCT_RETURN; | 
|---|
| 192 |  | 
|---|
| 193 | static KlassSubGraphInfo* get_subgraph_info(Klass *k); | 
|---|
| 194 |  | 
|---|
| 195 | static void init_subgraph_entry_fields(ArchivableStaticFieldInfo fields[], | 
|---|
| 196 | int num, Thread* THREAD); | 
|---|
| 197 |  | 
|---|
| 198 | // Used by decode_from_archive | 
|---|
| 199 | static address _narrow_oop_base; | 
|---|
| 200 | static int     _narrow_oop_shift; | 
|---|
| 201 |  | 
|---|
| 202 | typedef ResourceHashtable<oop, bool, | 
|---|
| 203 | HeapShared::oop_hash, | 
|---|
| 204 | HeapShared::oop_equals, | 
|---|
| 205 | 15889, // prime number | 
|---|
| 206 | ResourceObj::C_HEAP> SeenObjectsTable; | 
|---|
| 207 |  | 
|---|
| 208 | static SeenObjectsTable *_seen_objects_table; | 
|---|
| 209 |  | 
|---|
| 210 | static void init_seen_objects_table() { | 
|---|
| 211 | assert(_seen_objects_table == NULL, "must be"); | 
|---|
| 212 | _seen_objects_table = new (ResourceObj::C_HEAP, mtClass)SeenObjectsTable(); | 
|---|
| 213 | } | 
|---|
| 214 | static void delete_seen_objects_table() { | 
|---|
| 215 | assert(_seen_objects_table != NULL, "must be"); | 
|---|
| 216 | delete _seen_objects_table; | 
|---|
| 217 | _seen_objects_table = NULL; | 
|---|
| 218 | } | 
|---|
| 219 |  | 
|---|
| 220 | // Statistics (for one round of start_recording_subgraph ... done_recording_subgraph) | 
|---|
| 221 | static int _num_new_walked_objs; | 
|---|
| 222 | static int _num_new_archived_objs; | 
|---|
| 223 | static int _num_old_recorded_klasses; | 
|---|
| 224 |  | 
|---|
| 225 | // Statistics (for all archived subgraphs) | 
|---|
| 226 | static int _num_total_subgraph_recordings; | 
|---|
| 227 | static int _num_total_walked_objs; | 
|---|
| 228 | static int _num_total_archived_objs; | 
|---|
| 229 | static int _num_total_recorded_klasses; | 
|---|
| 230 | static int _num_total_verifications; | 
|---|
| 231 |  | 
|---|
| 232 | static void start_recording_subgraph(InstanceKlass *k, const char* klass_name); | 
|---|
| 233 | static void done_recording_subgraph(InstanceKlass *k, const char* klass_name); | 
|---|
| 234 |  | 
|---|
| 235 | static bool has_been_seen_during_subgraph_recording(oop obj); | 
|---|
| 236 | static void set_has_been_seen_during_subgraph_recording(oop obj); | 
|---|
| 237 |  | 
|---|
| 238 | public: | 
|---|
| 239 | static void create_archived_object_cache() { | 
|---|
| 240 | _archived_object_cache = | 
|---|
| 241 | new (ResourceObj::C_HEAP, mtClass)ArchivedObjectCache(); | 
|---|
| 242 | } | 
|---|
| 243 | static void destroy_archived_object_cache() { | 
|---|
| 244 | delete _archived_object_cache; | 
|---|
| 245 | _archived_object_cache = NULL; | 
|---|
| 246 | } | 
|---|
| 247 | static ArchivedObjectCache* archived_object_cache() { | 
|---|
| 248 | return _archived_object_cache; | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 | static oop find_archived_heap_object(oop obj); | 
|---|
| 252 | static oop archive_heap_object(oop obj, Thread* THREAD); | 
|---|
| 253 | static oop materialize_archived_object(narrowOop v); | 
|---|
| 254 |  | 
|---|
| 255 | static void archive_klass_objects(Thread* THREAD); | 
|---|
| 256 |  | 
|---|
| 257 | static void set_archive_heap_region_fixed() { | 
|---|
| 258 | _archive_heap_region_fixed = true; | 
|---|
| 259 | } | 
|---|
| 260 | static bool archive_heap_region_fixed() { | 
|---|
| 261 | return _archive_heap_region_fixed; | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 | static void archive_java_heap_objects(GrowableArray<MemRegion> *closed, | 
|---|
| 265 | GrowableArray<MemRegion> *open); | 
|---|
| 266 | static void copy_closed_archive_heap_objects(GrowableArray<MemRegion> * closed_archive); | 
|---|
| 267 | static void copy_open_archive_heap_objects(GrowableArray<MemRegion> * open_archive); | 
|---|
| 268 |  | 
|---|
| 269 | static oop archive_reachable_objects_from(int level, | 
|---|
| 270 | KlassSubGraphInfo* subgraph_info, | 
|---|
| 271 | oop orig_obj, | 
|---|
| 272 | bool is_closed_archive, | 
|---|
| 273 | TRAPS); | 
|---|
| 274 |  | 
|---|
| 275 | static ResourceBitMap calculate_oopmap(MemRegion region); | 
|---|
| 276 | #endif // INCLUDE_CDS_JAVA_HEAP | 
|---|
| 277 |  | 
|---|
| 278 | public: | 
|---|
| 279 | static bool is_heap_object_archiving_allowed() { | 
|---|
| 280 | CDS_JAVA_HEAP_ONLY(return (UseG1GC && UseCompressedOops && UseCompressedClassPointers);) | 
|---|
| 281 | NOT_CDS_JAVA_HEAP(return false;) | 
|---|
| 282 | } | 
|---|
| 283 |  | 
|---|
| 284 | static bool is_heap_region(int idx) { | 
|---|
| 285 | CDS_JAVA_HEAP_ONLY(return (idx >= MetaspaceShared::first_closed_archive_heap_region && | 
|---|
| 286 | idx <= MetaspaceShared::last_open_archive_heap_region)); | 
|---|
| 287 | NOT_CDS_JAVA_HEAP_RETURN_(false); | 
|---|
| 288 | } | 
|---|
| 289 |  | 
|---|
| 290 | static void set_closed_archive_heap_region_mapped() { | 
|---|
| 291 | CDS_JAVA_HEAP_ONLY(_closed_archive_heap_region_mapped = true); | 
|---|
| 292 | NOT_CDS_JAVA_HEAP_RETURN; | 
|---|
| 293 | } | 
|---|
| 294 | static bool closed_archive_heap_region_mapped() { | 
|---|
| 295 | CDS_JAVA_HEAP_ONLY(return _closed_archive_heap_region_mapped); | 
|---|
| 296 | NOT_CDS_JAVA_HEAP_RETURN_(false); | 
|---|
| 297 | } | 
|---|
| 298 | static void set_open_archive_heap_region_mapped() { | 
|---|
| 299 | CDS_JAVA_HEAP_ONLY(_open_archive_heap_region_mapped = true); | 
|---|
| 300 | NOT_CDS_JAVA_HEAP_RETURN; | 
|---|
| 301 | } | 
|---|
| 302 | static bool open_archive_heap_region_mapped() { | 
|---|
| 303 | CDS_JAVA_HEAP_ONLY(return _open_archive_heap_region_mapped); | 
|---|
| 304 | NOT_CDS_JAVA_HEAP_RETURN_(false); | 
|---|
| 305 | } | 
|---|
| 306 |  | 
|---|
| 307 | static void fixup_mapped_heap_regions() NOT_CDS_JAVA_HEAP_RETURN; | 
|---|
| 308 |  | 
|---|
| 309 | inline static bool is_archived_object(oop p) NOT_CDS_JAVA_HEAP_RETURN_(false); | 
|---|
| 310 |  | 
|---|
| 311 | static void initialize_from_archived_subgraph(Klass* k) NOT_CDS_JAVA_HEAP_RETURN; | 
|---|
| 312 |  | 
|---|
| 313 | // NarrowOops stored in the CDS archive may use a different encoding scheme | 
|---|
| 314 | // than CompressedOops::{base,shift} -- see FileMapInfo::map_heap_regions_impl. | 
|---|
| 315 | // To decode them, do not use CompressedOops::decode_not_null. Use this | 
|---|
| 316 | // function instead. | 
|---|
| 317 | inline static oop decode_from_archive(narrowOop v) NOT_CDS_JAVA_HEAP_RETURN_(NULL); | 
|---|
| 318 |  | 
|---|
| 319 | static void init_narrow_oop_decoding(address base, int shift) NOT_CDS_JAVA_HEAP_RETURN; | 
|---|
| 320 |  | 
|---|
| 321 | static void patch_archived_heap_embedded_pointers(MemRegion mem, address  oopmap, | 
|---|
| 322 | size_t oopmap_in_bits) NOT_CDS_JAVA_HEAP_RETURN; | 
|---|
| 323 |  | 
|---|
| 324 | static void init_subgraph_entry_fields(Thread* THREAD) NOT_CDS_JAVA_HEAP_RETURN; | 
|---|
| 325 | static void write_subgraph_info_table() NOT_CDS_JAVA_HEAP_RETURN; | 
|---|
| 326 | static void (SerializeClosure* soc) NOT_CDS_JAVA_HEAP_RETURN; | 
|---|
| 327 | }; | 
|---|
| 328 | #endif // SHARE_MEMORY_HEAPSHARED_HPP | 
|---|
| 329 |  | 
|---|