src/hotspot/share/memory/metaspaceClosure.hpp
author iklam
Tue, 20 Nov 2018 20:00:15 -0800
changeset 52631 3009ca99de32
parent 47216 71c04702a3d5
child 53244 9807daeb47c4
permissions -rw-r--r--
8213587: Speed up CDS dump time by using resizable hashtables Reviewed-by: jiangli, coleenp, gziemski
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
/*
52631
3009ca99de32 8213587: Speed up CDS dump time by using resizable hashtables
iklam
parents: 47216
diff changeset
     2
 * Copyright (c) 2017, 2018, 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
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    25
#ifndef SHARE_VM_MEMORY_METASPACE_ITERATOR_HPP
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
    26
#define SHARE_VM_MEMORY_METASPACE_ITERATOR_HPP
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.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   104
  class Ref {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   105
  protected:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   106
    virtual void** mpp() const = 0;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   107
  public:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   108
    virtual bool not_null() const = 0;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   109
    virtual int size() const = 0;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   110
    virtual void metaspace_pointers_do(MetaspaceClosure *it) const = 0;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   111
    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
   112
    virtual MetaspaceObj::Type msotype() const = 0;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   113
    virtual bool is_read_only_by_default() const = 0;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   114
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   115
    address obj() const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   116
      // 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
   117
      // 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
   118
      uintx p = (uintx)*mpp();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   119
      return (address)(p & (~FLAG_MASK));
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   120
    }
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
    void update(address new_loc) const;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   123
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   124
  private:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   125
    static const uintx FLAG_MASK = 0x03;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   126
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   127
    int flag_bits() const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   128
      uintx p = (uintx)*mpp();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   129
      return (int)(p & FLAG_MASK);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   130
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   131
  };
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   132
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   133
private:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   134
  // -------------------------------------------------- ObjectRef
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   135
  template <class T> class ObjectRef : public Ref {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   136
    T** _mpp;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   137
    T* dereference() const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   138
      return *_mpp;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   139
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   140
  protected:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   141
    virtual void** mpp() const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   142
      return (void**)_mpp;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   143
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   144
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   145
  public:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   146
    ObjectRef(T** mpp) : _mpp(mpp) {}
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
    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
   149
    virtual bool not_null()                const { return dereference() != NULL; }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   150
    virtual int size()                     const { return dereference()->size(); }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   151
    virtual MetaspaceObj::Type msotype()   const { return dereference()->type(); }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   152
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   153
    virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   154
      dereference()->metaspace_pointers_do(it);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   155
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   156
    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
   157
      ((T*)new_loc)->metaspace_pointers_do(it);
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
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   161
  // -------------------------------------------------- PrimitiveArrayRef
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   162
  template <class T> class PrimitiveArrayRef : public Ref {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   163
    Array<T>** _mpp;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   164
    Array<T>* dereference() const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   165
      return *_mpp;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   166
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   167
  protected:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   168
    virtual void** mpp() const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   169
      return (void**)_mpp;
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
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   172
  public:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   173
    PrimitiveArrayRef(Array<T>** mpp) : _mpp(mpp) {}
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
    // all Arrays are read-only by default
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   176
    virtual bool is_read_only_by_default() const { return true; }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   177
    virtual bool not_null()                const { return dereference() != NULL;  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   178
    virtual int size()                     const { return dereference()->size(); }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   179
    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
   180
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   181
    virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   182
      Array<T>* array = dereference();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   183
      log_trace(cds)("Iter(PrimitiveArray): %p [%d]", array, array->length());
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   184
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   185
    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
   186
      Array<T>* array = (Array<T>*)new_loc;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   187
      log_trace(cds)("Iter(PrimitiveArray): %p [%d]", array, array->length());
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   188
    }
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
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   191
  // -------------------------------------------------- PointerArrayRef
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   192
  template <class T> class PointerArrayRef : public Ref {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   193
    Array<T*>** _mpp;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   194
    Array<T*>* dereference() const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   195
      return *_mpp;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   196
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   197
  protected:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   198
    virtual void** mpp() const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   199
      return (void**)_mpp;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   200
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   201
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   202
  public:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   203
    PointerArrayRef(Array<T*>** mpp) : _mpp(mpp) {}
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
    // all Arrays are read-only by default
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   206
    virtual bool is_read_only_by_default() const { return true; }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   207
    virtual bool not_null()                const { return dereference() != NULL; }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   208
    virtual int size()                     const { return dereference()->size(); }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   209
    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
   210
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   211
    virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   212
      metaspace_pointers_do_at_impl(it, dereference());
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   213
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   214
    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
   215
      metaspace_pointers_do_at_impl(it, (Array<T*>*)new_loc);
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
  private:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   218
    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
   219
      log_trace(cds)("Iter(ObjectArray): %p [%d]", array, array->length());
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   220
      for (int i = 0; i < array->length(); i++) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   221
        T** mpp = array->adr_at(i);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   222
        it->push(mpp);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   223
      }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   224
    }
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
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   227
  void push_impl(Ref* ref, Writability w);
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
public:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   230
  // 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
   231
  virtual bool do_ref(Ref* ref, bool read_only) = 0;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   232
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   233
  // When you do:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   234
  //     void MyType::metaspace_pointers_do(MetaspaceClosure* it) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   235
  //       it->push(_my_field)
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   236
  //
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   237
  // 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
   238
  // 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
   239
  template <typename T> void push(Array<T*>** mpp, Writability w = _default) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   240
    PointerArrayRef<T> ref(mpp);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   241
    push_impl(&ref, w);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   242
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   243
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   244
  // 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
   245
  // this is the second choice.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   246
  template <typename T> void push(Array<T>** mpp, Writability w = _default) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   247
    PrimitiveArrayRef<T> ref(mpp);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   248
    push_impl(&ref, w);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   249
  }
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
  // 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
   252
  // this will be matched by default.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   253
  template <class T> void push(T** mpp, Writability w = _default) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   254
    ObjectRef<T> ref(mpp);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   255
    push_impl(&ref, w);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   256
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   257
};
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   258
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   259
// 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
   260
class UniqueMetaspaceClosure : public MetaspaceClosure {
52631
3009ca99de32 8213587: Speed up CDS dump time by using resizable hashtables
iklam
parents: 47216
diff changeset
   261
  static const int INITIAL_TABLE_SIZE = 15889;
3009ca99de32 8213587: Speed up CDS dump time by using resizable hashtables
iklam
parents: 47216
diff changeset
   262
  static const int MAX_TABLE_SIZE     = 1000000;
3009ca99de32 8213587: Speed up CDS dump time by using resizable hashtables
iklam
parents: 47216
diff changeset
   263
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   264
  // 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
   265
  virtual bool do_ref(Ref* ref, bool read_only);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   266
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   267
public:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   268
  // Gets called the first time we discover an object.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   269
  virtual void 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
   270
  UniqueMetaspaceClosure() : _has_been_visited(INITIAL_TABLE_SIZE) {}
3009ca99de32 8213587: Speed up CDS dump time by using resizable hashtables
iklam
parents: 47216
diff changeset
   271
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents:
diff changeset
   272
private:
52631
3009ca99de32 8213587: Speed up CDS dump time by using resizable hashtables
iklam
parents: 47216
diff changeset
   273
  KVHashtable<address, bool, mtInternal> _has_been_visited;
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
#endif // SHARE_VM_MEMORY_METASPACE_ITERATOR_HPP