comparison src/share/vm/classfile/classLoaderData.hpp @ 6725:da91efe96a93

6964458: Reimplement class meta-data storage to use native memory Summary: Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland Contributed-by: jmasa <jon.masamitsu@oracle.com>, stefank <stefan.karlsson@oracle.com>, mgerdin <mikael.gerdin@oracle.com>, never <tom.rodriguez@oracle.com>
author coleenp
date Sat, 01 Sep 2012 13:25:18 -0400
parents
children e861d44e0c9c
comparison
equal deleted inserted replaced
6724:36d1d483d5d6 6725:da91efe96a93
1 /*
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #ifndef SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
26 #define SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
27
28 #include "memory/allocation.hpp"
29 #include "memory/memRegion.hpp"
30 #include "memory/metaspace.hpp"
31 #include "memory/metaspaceCounters.hpp"
32 #include "runtime/mutex.hpp"
33 #include "utilities/growableArray.hpp"
34
35 //
36 // A class loader represents a linkset. Conceptually, a linkset identifies
37 // the complete transitive closure of resolved links that a dynamic linker can
38 // produce.
39 //
40 // A ClassLoaderData also encapsulates the allocation space, called a metaspace,
41 // used by the dynamic linker to allocate the runtime representation of all
42 // the types it defines.
43 //
44 // ClassLoaderData are stored in the runtime representation of classes and the
45 // system dictionary, are roots of garbage collection, and provides iterators
46 // for root tracing and other GC operations.
47
48 class ClassLoaderData;
49 class JNIMethodBlock;
50 class JNIHandleBlock;
51 class Metadebug;
52 // GC root for walking class loader data created
53
54 class ClassLoaderDataGraph : public AllStatic {
55 friend class ClassLoaderData;
56 friend class ClassLoaderDataGraphMetaspaceIterator;
57 friend class VMStructs;
58 private:
59 // All CLDs (except the null CLD) can be reached by walking _head->_next->...
60 static ClassLoaderData* _head;
61 static ClassLoaderData* _unloading;
62 // CMS support.
63 static ClassLoaderData* _saved_head;
64
65 static ClassLoaderData* add(ClassLoaderData** loader_data_addr, Handle class_loader);
66 public:
67 static ClassLoaderData* find_or_create(Handle class_loader);
68 static void purge();
69 static void clear_claimed_marks();
70 static void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
71 static void always_strong_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
72 static void classes_do(KlassClosure* klass_closure);
73 static bool do_unloading(BoolObjectClosure* is_alive);
74
75 // CMS support.
76 static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
77 static GrowableArray<ClassLoaderData*>* new_clds();
78
79 static void dump_on(outputStream * const out) PRODUCT_RETURN;
80 static void dump() { dump_on(tty); }
81 static void verify();
82
83 #ifndef PRODUCT
84 // expensive test for pointer in metaspace for debugging
85 static bool contains(address x);
86 static bool contains_loader_data(ClassLoaderData* loader_data);
87 #endif
88 };
89
90 // ClassLoaderData class
91
92 class ClassLoaderData : public CHeapObj<mtClass> {
93 friend class VMStructs;
94 private:
95 friend class ClassLoaderDataGraph;
96 friend class ClassLoaderDataGraphMetaspaceIterator;
97 friend class MetaDataFactory;
98 friend class Method;
99
100 static ClassLoaderData * _the_null_class_loader_data;
101
102 oop _class_loader; // oop used to uniquely identify a class loader
103 // class loader or a canonical class path
104 Metaspace * _metaspace; // Meta-space where meta-data defined by the
105 // classes in the class loader are allocated.
106 Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup.
107 bool _unloading; // true if this class loader goes away
108 volatile int _claimed; // true if claimed, for example during GC traces.
109 // To avoid applying oop closure more than once.
110 // Has to be an int because we cas it.
111 Klass* _klasses; // The classes defined by the class loader.
112
113 JNIHandleBlock* _handles; // Handles to constant pool arrays
114
115 // These method IDs are created for the class loader and set to NULL when the
116 // class loader is unloaded. They are rarely freed, only for redefine classes
117 // and if they lose a data race in InstanceKlass.
118 JNIMethodBlock* _jmethod_ids;
119
120 // Metadata to be deallocated when it's safe at class unloading, when
121 // this class loader isn't unloaded itself.
122 GrowableArray<Metadata*>* _deallocate_list;
123
124 // Support for walking class loader data objects
125 ClassLoaderData* _next; /// Next loader_datas created
126
127 // ReadOnly and ReadWrite metaspaces (static because only on the null
128 // class loader for now).
129 static Metaspace* _ro_metaspace;
130 static Metaspace* _rw_metaspace;
131
132 bool has_dependency(ClassLoaderData* cld);
133 void add_dependency(ClassLoaderData* to_loader_data, TRAPS);
134
135 void set_next(ClassLoaderData* next) { _next = next; }
136 ClassLoaderData* next() const { return _next; }
137
138 ClassLoaderData(Handle h_class_loader);
139 ~ClassLoaderData();
140
141 void set_metaspace(Metaspace* m) { _metaspace = m; }
142
143 JNIHandleBlock* handles() const;
144 void set_handles(JNIHandleBlock* handles);
145
146 Mutex* metaspace_lock() const { return _metaspace_lock; }
147
148 // GC interface.
149 void clear_claimed() { _claimed = 0; }
150 bool claimed() const { return _claimed == 1; }
151 bool claim();
152
153 void mark_for_unload() { _unloading = true; }
154
155 void classes_do(void f(InstanceKlass*));
156
157 // Deallocate free list during class unloading.
158 void free_deallocate_list();
159
160 // Allocate out of this class loader data
161 MetaWord* allocate(size_t size);
162
163 public:
164 // Accessors
165 Metaspace* metaspace_or_null() const { return _metaspace; }
166
167 static ClassLoaderData* the_null_class_loader_data() {
168 return _the_null_class_loader_data;
169 }
170
171 static void init_null_class_loader_data() {
172 assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
173 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
174 _the_null_class_loader_data = new ClassLoaderData((oop)NULL);
175 ClassLoaderDataGraph::_head = _the_null_class_loader_data;
176 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
177 if (DumpSharedSpaces) {
178 _the_null_class_loader_data->initialize_shared_metaspaces();
179 }
180 }
181
182 bool is_the_null_class_loader_data() const {
183 return this == _the_null_class_loader_data;
184 }
185
186 // The Metaspace is created lazily so may be NULL. This
187 // method will allocate a Metaspace if needed.
188 Metaspace* metaspace_non_null();
189
190 oop class_loader() const { return _class_loader; }
191
192 // Returns true if this class loader data is for a loader going away.
193 bool is_unloading() const {
194 assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");
195 return _unloading;
196 }
197
198 unsigned int identity_hash() {
199 return _class_loader == NULL ? 0 : _class_loader->identity_hash();
200 }
201
202 // Used when tracing from klasses.
203 void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
204
205 void classes_do(KlassClosure* klass_closure);
206
207 bool has_defined(Klass* klass) {
208 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
209 if (klass == k) {
210 return true;
211 }
212 }
213 return false;
214 }
215
216 JNIMethodBlock* jmethod_ids() const { return _jmethod_ids; }
217 void set_jmethod_ids(JNIMethodBlock* new_block) { _jmethod_ids = new_block; }
218
219 void print_value() { print_value_on(tty); }
220 void print_value_on(outputStream* out) const PRODUCT_RETURN;
221 void dump(outputStream * const out) PRODUCT_RETURN;
222 void verify();
223
224 jobject add_handle(Handle h);
225 void add_class(Klass* k);
226 void remove_class(Klass* k);
227 void record_dependency(Klass* to, TRAPS);
228
229 void add_to_deallocate_list(Metadata* m);
230
231 static ClassLoaderData* class_loader_data(oop loader);
232 static void print_loader(ClassLoaderData *loader_data, outputStream *out);
233
234 // CDS support
235 Metaspace* ro_metaspace();
236 Metaspace* rw_metaspace();
237 void initialize_shared_metaspaces();
238 };
239
240 class ClassLoaderDataGraphMetaspaceIterator : public StackObj {
241 ClassLoaderData* _data;
242 public:
243 ClassLoaderDataGraphMetaspaceIterator();
244 ~ClassLoaderDataGraphMetaspaceIterator();
245 bool repeat() { return _data != NULL; }
246 Metaspace* get_next() {
247 assert(_data != NULL, "Should not be NULL in call to the iterator");
248 Metaspace* result = _data->metaspace_or_null();
249 _data = _data->next();
250 // This result might be NULL for class loaders without metaspace
251 // yet. It would be nice to return only non-null results but
252 // there is no guarantee that there will be a non-null result
253 // down the list so the caller is going to have to check.
254 return result;
255 }
256 };
257 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP