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