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_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_klasses(NULL),
  59     _subgraph_entry_fields(NULL) {}
  60   ~KlassSubGraphInfo() {
  61     if (_subgraph_klasses != NULL) {
  62       delete _subgraph_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_klasses() {
  72     return _subgraph_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_klass(Klass *orig_k, Klass *relocated_k);
  79 };
  80 
  81 // An archived record of object sub-graphs reachable from static
  82 // fields within _k's mirror. The record is reloaded from the archive
  83 // at runtime.
  84 class ArchivedKlassSubGraphInfoRecord {
  85  private:
  86   ArchivedKlassSubGraphInfoRecord* _next;
  87   Klass* _k;
  88 
  89   // contains pairs of field offset and value for each subgraph entry field
  90   Array<juint>* _entry_field_records;
  91 
  92   // klasses of objects in archived sub-graphs referenced from the entry points
  93   // (static fields) in the containing class
  94   Array<Klass*>* _subgraph_klasses;
  95  public:
  96   ArchivedKlassSubGraphInfoRecord() :
  97     _next(NULL), _k(NULL), _entry_field_records(NULL), _subgraph_klasses(NULL) {}
  98   void init(KlassSubGraphInfo* info);
  99   Klass* klass() { return _k; }
 100   ArchivedKlassSubGraphInfoRecord* next() { return _next; }
 101   void set_next(ArchivedKlassSubGraphInfoRecord* next) { _next = next; }
 102   Array<juint>*  entry_field_records() { return _entry_field_records; }
 103   Array<Klass*>* subgraph_klasses() { return _subgraph_klasses; }
 104 };
 105 #endif // INCLUDE_CDS_JAVA_HEAP
 106 
 107 class HeapShared: AllStatic {
 108  private:
 109 #if INCLUDE_CDS_JAVA_HEAP
 110   // This is a list of subgraph infos built at dump time while
 111   // archiving object subgraphs.
 112   static KlassSubGraphInfo* _subgraph_info_list;
 113 
 114   // Contains a list of ArchivedKlassSubGraphInfoRecords that is stored
 115   // in the archive file and reloaded at runtime.
 116   static int _num_archived_subgraph_info_records;
 117   static Array<ArchivedKlassSubGraphInfoRecord>* _archived_subgraph_info_records;
 118 
 119   // Archive object sub-graph starting from the given static field
 120   // in Klass k's mirror.
 121   static void archive_reachable_objects_from_static_field(
 122     InstanceKlass* k, const char* klass_name,
 123     int field_ofset, const char* field_name, TRAPS);
 124 
 125   static KlassSubGraphInfo* find_subgraph_info(Klass *k);
 126   static KlassSubGraphInfo* get_subgraph_info(Klass *k);
 127   static int num_of_subgraph_infos();
 128 
 129   static size_t build_archived_subgraph_info_records(int num_records);
 130 
 131   // Used by decode_with_archived_oop_encoding_mode
 132   static address _narrow_oop_base;
 133   static int     _narrow_oop_shift;
 134 
 135   static bool oop_equals(oop const& p1, oop const& p2) {
 136     return primitive_equals<oop>(p1, p2);
 137   }
 138 
 139   static unsigned oop_hash(oop const& p) {
 140     return primitive_hash<address>((address)p);
 141   }
 142 
 143   typedef ResourceHashtable<oop, bool,
 144       HeapShared::oop_hash,
 145       HeapShared::oop_equals,
 146       15889, // prime number
 147       ResourceObj::C_HEAP> SeenObjectsTable;
 148 
 149   static SeenObjectsTable *_seen_objects_table;
 150 
 151   static void record_subgraph_klasses();
 152   static void start_recording_subgraph_klasses(InstanceKlass *k, const char* klass_name);
 153   static void record_subgraph_klasses_from_static_field(InstanceKlass *k,
 154                                                         const char* klass_name,
 155                                                         int field_offset,
 156                                                         const char* field_name);
 157   static void done_recording_subgraph_klasses(InstanceKlass *k);
 158 
 159 #endif // INCLUDE_CDS_JAVA_HEAP
 160  public:
 161   static char* read_archived_subgraph_infos(char* buffer) NOT_CDS_JAVA_HEAP_RETURN_(buffer);
 162   static void write_archived_subgraph_infos() NOT_CDS_JAVA_HEAP_RETURN;
 163   static void initialize_from_archived_subgraph(Klass* k) NOT_CDS_JAVA_HEAP_RETURN;
 164 
 165   // NarrowOops stored in the CDS archive may use a different encoding scheme
 166   // than Universe::narrow_oop_{base,shift} -- see FileMapInfo::map_heap_regions_impl.
 167   // To decode them, do not use CompressedOops::decode_not_null. Use this
 168   // function instead.
 169   inline static oop decode_with_archived_oop_encoding_mode(narrowOop v) NOT_CDS_JAVA_HEAP_RETURN_(NULL);
 170 
 171   static void init_narrow_oop_decoding(address base, int shift) NOT_CDS_JAVA_HEAP_RETURN;
 172 
 173   static void patch_archived_heap_embedded_pointers(MemRegion mem, address  oopmap,
 174                                                     size_t oopmap_in_bits) NOT_CDS_JAVA_HEAP_RETURN;
 175 
 176   static void init_archivable_static_fields(Thread* THREAD) NOT_CDS_JAVA_HEAP_RETURN;
 177   static void archive_module_graph_objects(Thread* THREAD) NOT_CDS_JAVA_HEAP_RETURN;
 178 
 179 #if INCLUDE_CDS_JAVA_HEAP
 180   static ResourceBitMap calculate_oopmap(MemRegion region);
 181   static bool has_been_seen_during_subgraph_klasses_recording(oop obj);
 182   static void record_subgraph_klasses_for(KlassSubGraphInfo* subgraph_info, oop obj);
 183 #endif
 184 };
 185 #endif // SHARE_VM_MEMORY_HEAPSHARED_HPP