src/hotspot/share/memory/metaspaceClosure.hpp
author stuefe
Tue, 26 Nov 2019 16:21:29 +0100
branchstuefe-new-metaspace-branch
changeset 59272 54750b448264
parent 54927 1512d88b24c6
child 59070 22ee476cc664
permissions -rw-r--r--
Metadatatype back to metaspace.hpp to reduce patch size
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
     1
/*
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 52631
diff changeset
     2
 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
     4
 *
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
     8
 *
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    13
 * accompanied this code).
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    14
 *
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    18
 *
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    21
 * questions.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    22
 *
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    23
 */
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    24
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 52631
diff changeset
    25
#ifndef SHARE_MEMORY_METASPACECLOSURE_HPP
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 52631
diff changeset
    26
#define SHARE_MEMORY_METASPACECLOSURE_HPP
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    27
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    28
#include "logging/log.hpp"
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    29
#include "memory/allocation.hpp"
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    30
#include "oops/array.hpp"
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    31
#include "utilities/growableArray.hpp"
52631
3009ca99de32 8213587: Speed up CDS dump time by using resizable hashtables
iklam
parents: 47216
diff changeset
    32
#include "utilities/hashtable.inline.hpp"
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    33
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    34
// The metadata hierarchy is separate from the oop hierarchy
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    35
  class MetaspaceObj;        // no C++ vtable
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    36
//class   Array;             // no C++ vtable
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    37
  class   Annotations;       // no C++ vtable
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    38
  class   ConstantPoolCache; // no C++ vtable
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    39
  class   ConstMethod;       // no C++ vtable
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    40
  class   MethodCounters;    // no C++ vtable
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    41
  class   Symbol;            // no C++ vtable
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    42
  class   Metadata;          // has C++ vtable (so do all subclasses)
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    43
  class     ConstantPool;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    44
  class     MethodData;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    45
  class     Method;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    46
  class     Klass;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    47
  class       InstanceKlass;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    48
  class         InstanceMirrorKlass;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    49
  class         InstanceClassLoaderKlass;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    50
  class         InstanceRefKlass;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    51
  class       ArrayKlass;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    52
  class         ObjArrayKlass;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    53
  class         TypeArrayKlass;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    54
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    55
// class MetaspaceClosure --
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    56
//
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    57
// This class is used for iterating the objects in the HotSpot Metaspaces. It
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    58
// provides an API to walk all the reachable objects starting from a set of
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    59
// root references (such as all Klass'es in the SystemDictionary).
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    60
//
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    61
// Currently it is used for compacting the CDS archive by eliminate temporary
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    62
// objects allocated during archive creation time. See ArchiveCompactor in
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    63
// metaspaceShared.cpp for an example.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    64
//
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    65
// To support MetaspaceClosure, each subclass of MetaspaceObj must provide
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    66
// a method of the type void metaspace_pointers_do(MetaspaceClosure*). This method
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    67
// should call MetaspaceClosure::push() on every pointer fields of this
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    68
// class that points to a MetaspaceObj. See Annotations::metaspace_pointers_do()
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    69
// for an example.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    70
class MetaspaceClosure {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    71
public:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    72
  enum Writability {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    73
    _writable,
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    74
    _not_writable,
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    75
    _default
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    76
  };
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    77
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    78
  // class MetaspaceClosure::Ref --
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    79
  //
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    80
  // MetaspaceClosure can be viewed as a very simple type of copying garbage
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    81
  // collector. For it to function properly, it requires each subclass of
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    82
  // MetaspaceObj to provide two methods:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    83
  //
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    84
  //  size_t size();                                 -- to determine how much data to copy
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    85
  //  void metaspace_pointers_do(MetaspaceClosure*); -- to locate all the embedded pointers
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    86
  //
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    87
  // Calling these methods would be trivial if these two were virtual methods.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    88
  // However, to save space, MetaspaceObj has NO vtable. The vtable is introduced
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    89
  // only in the Metadata class.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    90
  //
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    91
  // To work around the lack of a vtable, we use Ref class with templates
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    92
  // (see ObjectRef, PrimitiveArrayRef and PointerArrayRef)
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    93
  // so that we can statically discover the type of a object. The use of Ref
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    94
  // depends on the fact that:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    95
  //
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    96
  // [1] We don't use polymorphic pointers for MetaspaceObj's that are not subclasses
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    97
  //     of Metadata. I.e., we don't do this:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    98
  //     class Klass {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    99
  //         MetaspaceObj *_obj;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   100
  //         Array<int>* foo() { return (Array<int>*)_obj; }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   101
  //         Symbol*     bar() { return (Symbol*)    _obj; }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   102
  //
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   103
  // [2] All Array<T> dimensions are statically declared.
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   104
  class Ref : public CHeapObj<mtInternal> {
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   105
    Writability _writability;
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   106
    Ref* _next;
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   107
    // Noncopyable.
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   108
    Ref(const Ref&);
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   109
    Ref& operator=(const Ref&);
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   110
  protected:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   111
    virtual void** mpp() const = 0;
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   112
    Ref(Writability w) : _writability(w), _next(NULL) {}
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   113
  public:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   114
    virtual bool not_null() const = 0;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   115
    virtual int size() const = 0;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   116
    virtual void metaspace_pointers_do(MetaspaceClosure *it) const = 0;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   117
    virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const = 0;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   118
    virtual MetaspaceObj::Type msotype() const = 0;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   119
    virtual bool is_read_only_by_default() const = 0;
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   120
    virtual ~Ref() {}
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   121
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   122
    address obj() const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   123
      // In some rare cases (see CPSlot in constantPool.hpp) we store some flags in the lowest
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   124
      // 2 bits of a MetaspaceObj pointer. Unmask these when manipulating the pointer.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   125
      uintx p = (uintx)*mpp();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   126
      return (address)(p & (~FLAG_MASK));
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   127
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   128
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   129
    address* addr() const {
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   130
      return (address*)mpp();
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   131
    }
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   132
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   133
    void update(address new_loc) const;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   134
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   135
    Writability writability() const { return _writability; };
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   136
    void set_next(Ref* n)           { _next = n; }
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   137
    Ref* next() const               { return _next; }
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   138
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   139
  private:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   140
    static const uintx FLAG_MASK = 0x03;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   141
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   142
    int flag_bits() const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   143
      uintx p = (uintx)*mpp();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   144
      return (int)(p & FLAG_MASK);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   145
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   146
  };
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   147
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   148
private:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   149
  // -------------------------------------------------- ObjectRef
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   150
  template <class T> class ObjectRef : public Ref {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   151
    T** _mpp;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   152
    T* dereference() const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   153
      return *_mpp;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   154
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   155
  protected:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   156
    virtual void** mpp() const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   157
      return (void**)_mpp;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   158
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   159
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   160
  public:
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   161
    ObjectRef(T** mpp, Writability w) : Ref(w), _mpp(mpp) {}
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   162
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   163
    virtual bool is_read_only_by_default() const { return T::is_read_only_by_default(); }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   164
    virtual bool not_null()                const { return dereference() != NULL; }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   165
    virtual int size()                     const { return dereference()->size(); }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   166
    virtual MetaspaceObj::Type msotype()   const { return dereference()->type(); }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   167
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   168
    virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   169
      dereference()->metaspace_pointers_do(it);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   170
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   171
    virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   172
      ((T*)new_loc)->metaspace_pointers_do(it);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   173
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   174
  };
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   175
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   176
  // -------------------------------------------------- PrimitiveArrayRef
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   177
  template <class T> class PrimitiveArrayRef : public Ref {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   178
    Array<T>** _mpp;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   179
    Array<T>* dereference() const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   180
      return *_mpp;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   181
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   182
  protected:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   183
    virtual void** mpp() const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   184
      return (void**)_mpp;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   185
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   186
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   187
  public:
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   188
    PrimitiveArrayRef(Array<T>** mpp, Writability w) : Ref(w), _mpp(mpp) {}
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   189
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   190
    // all Arrays are read-only by default
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   191
    virtual bool is_read_only_by_default() const { return true; }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   192
    virtual bool not_null()                const { return dereference() != NULL;  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   193
    virtual int size()                     const { return dereference()->size(); }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   194
    virtual MetaspaceObj::Type msotype()   const { return MetaspaceObj::array_type(sizeof(T)); }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   195
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   196
    virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   197
      Array<T>* array = dereference();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   198
      log_trace(cds)("Iter(PrimitiveArray): %p [%d]", array, array->length());
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   199
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   200
    virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   201
      Array<T>* array = (Array<T>*)new_loc;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   202
      log_trace(cds)("Iter(PrimitiveArray): %p [%d]", array, array->length());
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   203
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   204
  };
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   205
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   206
  // -------------------------------------------------- PointerArrayRef
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   207
  template <class T> class PointerArrayRef : public Ref {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   208
    Array<T*>** _mpp;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   209
    Array<T*>* dereference() const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   210
      return *_mpp;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   211
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   212
  protected:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   213
    virtual void** mpp() const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   214
      return (void**)_mpp;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   215
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   216
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   217
  public:
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   218
    PointerArrayRef(Array<T*>** mpp, Writability w) : Ref(w), _mpp(mpp) {}
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   219
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   220
    // all Arrays are read-only by default
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   221
    virtual bool is_read_only_by_default() const { return true; }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   222
    virtual bool not_null()                const { return dereference() != NULL; }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   223
    virtual int size()                     const { return dereference()->size(); }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   224
    virtual MetaspaceObj::Type msotype()   const { return MetaspaceObj::array_type(sizeof(T*)); }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   225
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   226
    virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   227
      metaspace_pointers_do_at_impl(it, dereference());
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   228
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   229
    virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   230
      metaspace_pointers_do_at_impl(it, (Array<T*>*)new_loc);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   231
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   232
  private:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   233
    void metaspace_pointers_do_at_impl(MetaspaceClosure *it, Array<T*>* array) const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   234
      log_trace(cds)("Iter(ObjectArray): %p [%d]", array, array->length());
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   235
      for (int i = 0; i < array->length(); i++) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   236
        T** mpp = array->adr_at(i);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   237
        it->push(mpp);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   238
      }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   239
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   240
  };
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   241
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   242
  // If recursion is too deep, save the Refs in _pending_refs, and push them later using
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   243
  // MetaspaceClosure::finish()
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   244
  static const int MAX_NEST_LEVEL = 5;
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   245
  Ref* _pending_refs;
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   246
  int _nest_level;
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   247
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   248
  void push_impl(Ref* ref);
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   249
  void do_push(Ref* ref);
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   250
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   251
public:
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   252
  MetaspaceClosure(): _pending_refs(NULL), _nest_level(0) {}
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   253
  ~MetaspaceClosure();
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   254
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   255
  void finish();
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   256
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   257
  // returns true if we want to keep iterating the pointers embedded inside <ref>
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   258
  virtual bool do_ref(Ref* ref, bool read_only) = 0;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   259
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   260
  // When you do:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   261
  //     void MyType::metaspace_pointers_do(MetaspaceClosure* it) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   262
  //       it->push(_my_field)
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   263
  //
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   264
  // C++ will try to match the "most specific" template function. This one will
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   265
  // will be matched if possible (if mpp is an Array<> of any pointer type).
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   266
  template <typename T> void push(Array<T*>** mpp, Writability w = _default) {
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   267
    push_impl(new PointerArrayRef<T>(mpp, w));
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   268
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   269
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   270
  // If the above function doesn't match (mpp is an Array<>, but T is not a pointer type), then
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   271
  // this is the second choice.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   272
  template <typename T> void push(Array<T>** mpp, Writability w = _default) {
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   273
    push_impl(new PrimitiveArrayRef<T>(mpp, w));
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   274
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   275
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   276
  // If the above function doesn't match (mpp is not an Array<> type), then
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   277
  // this will be matched by default.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   278
  template <class T> void push(T** mpp, Writability w = _default) {
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   279
    push_impl(new ObjectRef<T>(mpp, w));
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   280
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   281
};
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   282
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   283
// This is a special MetaspaceClosure that visits each unique MetaspaceObj once.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   284
class UniqueMetaspaceClosure : public MetaspaceClosure {
52631
3009ca99de32 8213587: Speed up CDS dump time by using resizable hashtables
iklam
parents: 47216
diff changeset
   285
  static const int INITIAL_TABLE_SIZE = 15889;
3009ca99de32 8213587: Speed up CDS dump time by using resizable hashtables
iklam
parents: 47216
diff changeset
   286
  static const int MAX_TABLE_SIZE     = 1000000;
3009ca99de32 8213587: Speed up CDS dump time by using resizable hashtables
iklam
parents: 47216
diff changeset
   287
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   288
  // Do not override. Returns true if we are discovering ref->obj() for the first time.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   289
  virtual bool do_ref(Ref* ref, bool read_only);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   290
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   291
public:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   292
  // Gets called the first time we discover an object.
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 53244
diff changeset
   293
  virtual bool do_unique_ref(Ref* ref, bool read_only) = 0;
52631
3009ca99de32 8213587: Speed up CDS dump time by using resizable hashtables
iklam
parents: 47216
diff changeset
   294
  UniqueMetaspaceClosure() : _has_been_visited(INITIAL_TABLE_SIZE) {}
3009ca99de32 8213587: Speed up CDS dump time by using resizable hashtables
iklam
parents: 47216
diff changeset
   295
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   296
private:
52631
3009ca99de32 8213587: Speed up CDS dump time by using resizable hashtables
iklam
parents: 47216
diff changeset
   297
  KVHashtable<address, bool, mtInternal> _has_been_visited;
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   298
};
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   299
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 52631
diff changeset
   300
#endif // SHARE_MEMORY_METASPACECLOSURE_HPP