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/systemDictionary.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "memory/universe.hpp"
  31 #include "oops/objArrayKlass.hpp"
  32 #include "oops/oop.hpp"
  33 #include "oops/typeArrayKlass.hpp"
  34 #include "utilities/bitMap.hpp"
  35 #include "utilities/growableArray.hpp"
  36 #include "utilities/resourceHash.hpp"
  37 
  38 #if INCLUDE_CDS_JAVA_HEAP
  39 // A dump time sub-graph info for Klass _k. It includes the entry points
  40 // (static fields in _k's mirror) of the archived sub-graphs reachable
  41 // from _k's mirror. It also contains a list of Klasses of the objects
  42 // within the sub-graphs.
  43 class KlassSubGraphInfo: public CHeapObj<mtClass> {
  44  private:
  45   KlassSubGraphInfo* _next;
  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, KlassSubGraphInfo* next) :
  58     _next(next), _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   KlassSubGraphInfo* next() { return _next; }
  70   Klass* klass()            { return _k; }
  71   GrowableArray<Klass*>* subgraph_object_klasses() {
  72     return _subgraph_object_klasses;
  73   }
  74   GrowableArray<juint>*  subgraph_entry_fields() {
  75     return _subgraph_entry_fields;
  76   }
  77   void add_subgraph_entry_field(int static_field_offset, oop v);
  78   void add_subgraph_object_klass(Klass *orig_k, Klass *relocated_k);
  79   int num_subgraph_object_klasses() {
  80     return _subgraph_object_klasses == NULL ? 0 :
  81            _subgraph_object_klasses->length();
  82   }
  83 };
  84 
  85 // An archived record of object sub-graphs reachable from static
  86 // fields within _k's mirror. The record is reloaded from the archive
  87 // at runtime.
  88 class ArchivedKlassSubGraphInfoRecord {
  89  private:
  90   ArchivedKlassSubGraphInfoRecord* _next;
  91   Klass* _k;
  92 
  93   // contains pairs of field offset and value for each subgraph entry field
  94   Array<juint>* _entry_field_records;
  95 
  96   // klasses of objects in archived sub-graphs referenced from the entry points
  97   // (static fields) in the containing class
  98   Array<Klass*>* _subgraph_object_klasses;
  99  public:
 100   ArchivedKlassSubGraphInfoRecord() :
 101     _next(NULL), _k(NULL), _entry_field_records(NULL), _subgraph_object_klasses(NULL) {}
 102   void init(KlassSubGraphInfo* info);
 103   Klass* klass() { return _k; }
 104   ArchivedKlassSubGraphInfoRecord* next() { return _next; }
 105   void set_next(ArchivedKlassSubGraphInfoRecord* next) { _next = next; }
 106   Array<juint>*  entry_field_records() { return _entry_field_records; }
 107   Array<Klass*>* subgraph_object_klasses() { return _subgraph_object_klasses; }
 108 };
 109 #endif // INCLUDE_CDS_JAVA_HEAP
 110 
 111 class HeapShared: AllStatic {
 112   friend class VerifySharedOopClosure;
 113  private:
 114 #if INCLUDE_CDS_JAVA_HEAP
 115   // This is a list of subgraph infos built at dump time while
 116   // archiving object subgraphs.
 117   static KlassSubGraphInfo* _subgraph_info_list;
 118 
 119   // Contains a list of ArchivedKlassSubGraphInfoRecords that is stored
 120   // in the archive file and reloaded at runtime.
 121   static int _num_archived_subgraph_info_records;
 122   static Array<ArchivedKlassSubGraphInfoRecord>* _archived_subgraph_info_records;
 123 
 124   // Archive object sub-graph starting from the given static field
 125   // in Klass k's mirror.
 126   static void archive_reachable_objects_from_static_field(
 127     InstanceKlass* k, const char* klass_name,
 128     int field_offset, const char* field_name, TRAPS);
 129   static void verify_subgraph_from_static_field(
 130     InstanceKlass* k, int field_offset) PRODUCT_RETURN;
 131 
 132   static void verify_reachable_objects_from(oop obj, bool is_archived) PRODUCT_RETURN;
 133 
 134   static KlassSubGraphInfo* find_subgraph_info(Klass *k);
 135   static KlassSubGraphInfo* get_subgraph_info(Klass *k);
 136   static int num_of_subgraph_infos();
 137 
 138   static size_t build_archived_subgraph_info_records(int num_records);
 139 
 140   // Used by decode_from_archive
 141   static address _narrow_oop_base;
 142   static int     _narrow_oop_shift;
 143 
 144   static bool oop_equals(oop const& p1, oop const& p2) {
 145     return primitive_equals<oop>(p1, p2);
 146   }
 147 
 148   static unsigned oop_hash(oop const& p) {
 149     return primitive_hash<address>((address)p);
 150   }
 151 
 152   typedef ResourceHashtable<oop, bool,
 153       HeapShared::oop_hash,
 154       HeapShared::oop_equals,
 155       15889, // prime number
 156       ResourceObj::C_HEAP> SeenObjectsTable;
 157 
 158   static SeenObjectsTable *_seen_objects_table;
 159 
 160   static void init_seen_objects_table() {
 161     assert(_seen_objects_table == NULL, "must be");
 162     _seen_objects_table = new (ResourceObj::C_HEAP, mtClass)SeenObjectsTable();
 163   }
 164   static void delete_seen_objects_table() {
 165     assert(_seen_objects_table != NULL, "must be");
 166     delete _seen_objects_table;
 167     _seen_objects_table = NULL;
 168   }
 169 
 170   // Statistics (for one round of start_recording_subgraph ... done_recording_subgraph)
 171   static int _num_new_walked_objs;
 172   static int _num_new_archived_objs;
 173   static int _num_old_recorded_klasses;
 174 
 175   // Statistics (for all archived subgraphs)
 176   static int _num_total_subgraph_recordings;
 177   static int _num_total_walked_objs;
 178   static int _num_total_archived_objs;
 179   static int _num_total_recorded_klasses;
 180   static int _num_total_verifications;
 181 
 182   static void start_recording_subgraph(InstanceKlass *k, const char* klass_name);
 183   static void done_recording_subgraph(InstanceKlass *k, const char* klass_name);
 184 
 185   static bool has_been_seen_during_subgraph_recording(oop obj);
 186   static void set_has_been_seen_during_subgraph_recording(oop obj);
 187 #endif // INCLUDE_CDS_JAVA_HEAP
 188  public:
 189   static char* read_archived_subgraph_infos(char* buffer) NOT_CDS_JAVA_HEAP_RETURN_(buffer);
 190   static void write_archived_subgraph_infos() NOT_CDS_JAVA_HEAP_RETURN;
 191   static void initialize_from_archived_subgraph(Klass* k) NOT_CDS_JAVA_HEAP_RETURN;
 192 
 193   // NarrowOops stored in the CDS archive may use a different encoding scheme
 194   // than Universe::narrow_oop_{base,shift} -- see FileMapInfo::map_heap_regions_impl.
 195   // To decode them, do not use CompressedOops::decode_not_null. Use this
 196   // function instead.
 197   inline static oop decode_from_archive(narrowOop v) NOT_CDS_JAVA_HEAP_RETURN_(NULL);
 198 
 199   static void init_narrow_oop_decoding(address base, int shift) NOT_CDS_JAVA_HEAP_RETURN;
 200 
 201   static void patch_archived_heap_embedded_pointers(MemRegion mem, address  oopmap,
 202                                                     size_t oopmap_in_bits) NOT_CDS_JAVA_HEAP_RETURN;
 203 
 204   static void init_archivable_static_fields(Thread* THREAD) NOT_CDS_JAVA_HEAP_RETURN;
 205   static void archive_static_fields(Thread* THREAD) NOT_CDS_JAVA_HEAP_RETURN;
 206 
 207 #if INCLUDE_CDS_JAVA_HEAP
 208   static ResourceBitMap calculate_oopmap(MemRegion region);
 209   static oop archive_reachable_objects_from(int level, KlassSubGraphInfo* subgraph_info, oop orig_obj, TRAPS);
 210   static void verify_subgraph_from(oop orig_obj) PRODUCT_RETURN;
 211 #endif
 212 };
 213 #endif // SHARE_VM_MEMORY_HEAPSHARED_HPP