1 /*
   2  * Copyright (c) 2018, 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_VM_MEMORY_HEAPSHARED_HPP
  26 #define SHARE_VM_MEMORY_HEAPSHARED_HPP
  27 
  28 #include "classfile/compactHashtable.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "memory/allocation.hpp"
  31 #include "memory/universe.hpp"
  32 #include "oops/objArrayKlass.hpp"
  33 #include "oops/oop.hpp"
  34 #include "oops/typeArrayKlass.hpp"
  35 #include "utilities/bitMap.hpp"
  36 #include "utilities/growableArray.hpp"
  37 #include "utilities/resourceHash.hpp"
  38 
  39 #if INCLUDE_CDS_JAVA_HEAP
  40 // A dump time sub-graph info for Klass _k. It includes the entry points
  41 // (static fields in _k's mirror) of the archived sub-graphs reachable
  42 // from _k's mirror. It also contains a list of Klasses of the objects
  43 // within the sub-graphs.
  44 class KlassSubGraphInfo: public CHeapObj<mtClass> {
  45  private:
  46   // The class that contains the static field(s) as the entry point(s)
  47   // of archived object sub-graph(s).
  48   Klass* _k;
  49   // A list of classes need to be loaded and initialized before the archived
  50   // object sub-graphs can be accessed at runtime.
  51   GrowableArray<Klass*>* _subgraph_object_klasses;
  52   // A list of _k's static fields as the entry points of archived sub-graphs.
  53   // For each entry field, it is a pair of field_offset and field_value.
  54   GrowableArray<juint>*  _subgraph_entry_fields;
  55 
  56  public:
  57   KlassSubGraphInfo(Klass* k) :
  58     _k(k),  _subgraph_object_klasses(NULL),
  59     _subgraph_entry_fields(NULL) {}
  60   ~KlassSubGraphInfo() {
  61     if (_subgraph_object_klasses != NULL) {
  62       delete _subgraph_object_klasses;
  63     }
  64     if (_subgraph_entry_fields != NULL) {
  65       delete _subgraph_entry_fields;
  66     }
  67   };
  68 
  69   Klass* klass()            { return _k; }
  70   GrowableArray<Klass*>* subgraph_object_klasses() {
  71     return _subgraph_object_klasses;
  72   }
  73   GrowableArray<juint>*  subgraph_entry_fields() {
  74     return _subgraph_entry_fields;
  75   }
  76   void add_subgraph_entry_field(int static_field_offset, oop v);
  77   void add_subgraph_object_klass(Klass *orig_k, Klass *relocated_k);
  78   int num_subgraph_object_klasses() {
  79     return _subgraph_object_klasses == NULL ? 0 :
  80            _subgraph_object_klasses->length();
  81   }
  82 };
  83 
  84 // An archived record of object sub-graphs reachable from static
  85 // fields within _k's mirror. The record is reloaded from the archive
  86 // at runtime.
  87 class ArchivedKlassSubGraphInfoRecord {
  88  private:
  89   Klass* _k;
  90 
  91   // contains pairs of field offset and value for each subgraph entry field
  92   Array<juint>* _entry_field_records;
  93 
  94   // klasses of objects in archived sub-graphs referenced from the entry points
  95   // (static fields) in the containing class
  96   Array<Klass*>* _subgraph_object_klasses;
  97  public:
  98   ArchivedKlassSubGraphInfoRecord() :
  99     _k(NULL), _entry_field_records(NULL), _subgraph_object_klasses(NULL) {}
 100   void init(KlassSubGraphInfo* info);
 101   Klass* klass() { return _k; }
 102   Array<juint>*  entry_field_records() { return _entry_field_records; }
 103   Array<Klass*>* subgraph_object_klasses() { return _subgraph_object_klasses; }
 104 };
 105 #endif // INCLUDE_CDS_JAVA_HEAP
 106 
 107 class HeapShared: AllStatic {
 108   friend class VerifySharedOopClosure;
 109  private:
 110 #if INCLUDE_CDS_JAVA_HEAP
 111 
 112   static bool klass_equals(Klass* const& p1, Klass* const& p2) {
 113     return primitive_equals<Klass*>(p1, p2);
 114   }
 115 
 116   static unsigned klass_hash(Klass* const& klass) {
 117     return primitive_hash<address>((address)klass);
 118   }
 119 
 120   class DumpTimeKlassSubGraphInfoTable
 121     : public ResourceHashtable<Klass*, KlassSubGraphInfo,
 122                                HeapShared::klass_hash,
 123                                HeapShared::klass_equals,
 124                                137, // prime number
 125                                ResourceObj::C_HEAP> {
 126   public:
 127     int _count;
 128   };
 129 
 130   inline static ArchivedKlassSubGraphInfoRecord* read_record_from_compact_hashtable(address base_address, u4 offset) {
 131     return (ArchivedKlassSubGraphInfoRecord*)(base_address + offset);
 132   }
 133 
 134   inline static bool record_equals_compact_hashtable_entry(ArchivedKlassSubGraphInfoRecord* value, const Klass* key, int len_unused) {
 135     return (value->klass() == key);
 136   }
 137 
 138   typedef CompactHashtable<
 139     const Klass*,
 140     ArchivedKlassSubGraphInfoRecord*,
 141     read_record_from_compact_hashtable,
 142     record_equals_compact_hashtable_entry
 143     > RunTimeKlassSubGraphInfoTable;
 144 
 145   static DumpTimeKlassSubGraphInfoTable* _dump_time_subgraph_info_table;
 146   static RunTimeKlassSubGraphInfoTable _run_time_subgraph_info_table;
 147 
 148   // Archive object sub-graph starting from the given static field
 149   // in Klass k's mirror.
 150   static void archive_reachable_objects_from_static_field(
 151     InstanceKlass* k, const char* klass_name,
 152     int field_offset, const char* field_name, TRAPS);
 153   static void verify_subgraph_from_static_field(
 154     InstanceKlass* k, int field_offset) PRODUCT_RETURN;
 155 
 156   static void verify_reachable_objects_from(oop obj, bool is_archived) PRODUCT_RETURN;
 157 
 158   static KlassSubGraphInfo* get_subgraph_info(Klass *k);
 159   static int num_of_subgraph_infos();
 160 
 161   static void build_archived_subgraph_info_records(int num_records);
 162 
 163   // Used by decode_from_archive
 164   static address _narrow_oop_base;
 165   static int     _narrow_oop_shift;
 166 
 167   static bool oop_equals(oop const& p1, oop const& p2) {
 168     return primitive_equals<oop>(p1, p2);
 169   }
 170 
 171   static unsigned oop_hash(oop const& p) {
 172     return primitive_hash<address>((address)p);
 173   }
 174 
 175   typedef ResourceHashtable<oop, bool,
 176       HeapShared::oop_hash,
 177       HeapShared::oop_equals,
 178       15889, // prime number
 179       ResourceObj::C_HEAP> SeenObjectsTable;
 180 
 181   static SeenObjectsTable *_seen_objects_table;
 182 
 183   static void init_seen_objects_table() {
 184     assert(_seen_objects_table == NULL, "must be");
 185     _seen_objects_table = new (ResourceObj::C_HEAP, mtClass)SeenObjectsTable();
 186   }
 187   static void delete_seen_objects_table() {
 188     assert(_seen_objects_table != NULL, "must be");
 189     delete _seen_objects_table;
 190     _seen_objects_table = NULL;
 191   }
 192 
 193   // Statistics (for one round of start_recording_subgraph ... done_recording_subgraph)
 194   static int _num_new_walked_objs;
 195   static int _num_new_archived_objs;
 196   static int _num_old_recorded_klasses;
 197 
 198   // Statistics (for all archived subgraphs)
 199   static int _num_total_subgraph_recordings;
 200   static int _num_total_walked_objs;
 201   static int _num_total_archived_objs;
 202   static int _num_total_recorded_klasses;
 203   static int _num_total_verifications;
 204 
 205   static void start_recording_subgraph(InstanceKlass *k, const char* klass_name);
 206   static void done_recording_subgraph(InstanceKlass *k, const char* klass_name);
 207 
 208   static bool has_been_seen_during_subgraph_recording(oop obj);
 209   static void set_has_been_seen_during_subgraph_recording(oop obj);
 210 #endif // INCLUDE_CDS_JAVA_HEAP
 211  public:
 212   static char* read_archived_subgraph_infos(char* buffer) NOT_CDS_JAVA_HEAP_RETURN_(buffer);
 213   static void write_archived_subgraph_infos() NOT_CDS_JAVA_HEAP_RETURN;
 214   static void initialize_from_archived_subgraph(Klass* k) NOT_CDS_JAVA_HEAP_RETURN;
 215 
 216   // NarrowOops stored in the CDS archive may use a different encoding scheme
 217   // than Universe::narrow_oop_{base,shift} -- see FileMapInfo::map_heap_regions_impl.
 218   // To decode them, do not use CompressedOops::decode_not_null. Use this
 219   // function instead.
 220   inline static oop decode_from_archive(narrowOop v) NOT_CDS_JAVA_HEAP_RETURN_(NULL);
 221 
 222   static void init_narrow_oop_decoding(address base, int shift) NOT_CDS_JAVA_HEAP_RETURN;
 223 
 224   static void patch_archived_heap_embedded_pointers(MemRegion mem, address  oopmap,
 225                                                     size_t oopmap_in_bits) NOT_CDS_JAVA_HEAP_RETURN;
 226 
 227   static void init_archivable_static_fields(Thread* THREAD) NOT_CDS_JAVA_HEAP_RETURN;
 228   static void archive_static_fields(Thread* THREAD) NOT_CDS_JAVA_HEAP_RETURN;
 229   static void write_subgraph_info_table() NOT_CDS_JAVA_HEAP_RETURN;
 230   static void serialize_subgraph_info_table_header(SerializeClosure* soc) NOT_CDS_JAVA_HEAP_RETURN;
 231 
 232 #if INCLUDE_CDS_JAVA_HEAP
 233   static ResourceBitMap calculate_oopmap(MemRegion region);
 234   static oop archive_reachable_objects_from(int level, KlassSubGraphInfo* subgraph_info, oop orig_obj, TRAPS);
 235   static void verify_subgraph_from(oop orig_obj) PRODUCT_RETURN;
 236 #endif
 237 };
 238 #endif // SHARE_VM_MEMORY_HEAPSHARED_HPP