Fri, 26 Sep 2014 17:48:10 -0400
8047125: (ref) More phantom object references
Reviewed-by: mchung, dfuchs, ahgross, jmasa, brutisso, mgerdin
Contributed-by: kim.barrett@oracle.com
1 /*
2 * Copyright (c) 2012, 2014, 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 */
25 #ifndef SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
26 #define SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
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"
35 #if INCLUDE_TRACE
36 # include "utilities/ticks.hpp"
37 #endif
39 //
40 // A class loader represents a linkset. Conceptually, a linkset identifies
41 // the complete transitive closure of resolved links that a dynamic linker can
42 // produce.
43 //
44 // A ClassLoaderData also encapsulates the allocation space, called a metaspace,
45 // used by the dynamic linker to allocate the runtime representation of all
46 // the types it defines.
47 //
48 // ClassLoaderData are stored in the runtime representation of classes and the
49 // system dictionary, are roots of garbage collection, and provides iterators
50 // for root tracing and other GC operations.
52 class ClassLoaderData;
53 class JNIMethodBlock;
54 class JNIHandleBlock;
55 class Metadebug;
57 // GC root for walking class loader data created
59 class ClassLoaderDataGraph : public AllStatic {
60 friend class ClassLoaderData;
61 friend class ClassLoaderDataGraphMetaspaceIterator;
62 friend class VMStructs;
63 private:
64 // All CLDs (except the null CLD) can be reached by walking _head->_next->...
65 static ClassLoaderData* _head;
66 static ClassLoaderData* _unloading;
67 // CMS support.
68 static ClassLoaderData* _saved_head;
69 static ClassLoaderData* _saved_unloading;
70 static bool _should_purge;
72 static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS);
73 static void post_class_unload_events(void);
74 public:
75 static ClassLoaderData* find_or_create(Handle class_loader, TRAPS);
76 static void purge();
77 static void clear_claimed_marks();
78 static void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
79 static void always_strong_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
80 static void keep_alive_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
81 static void classes_do(KlassClosure* klass_closure);
82 static void classes_do(void f(Klass* const));
83 static void loaded_classes_do(KlassClosure* klass_closure);
84 static void classes_unloading_do(void f(Klass* const));
85 static bool do_unloading(BoolObjectClosure* is_alive);
87 // CMS support.
88 static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
89 static GrowableArray<ClassLoaderData*>* new_clds();
91 static void set_should_purge(bool b) { _should_purge = b; }
92 static void purge_if_needed() {
93 // Only purge the CLDG for CMS if concurrent sweep is complete.
94 if (_should_purge) {
95 purge();
96 // reset for next time.
97 set_should_purge(false);
98 }
99 }
101 static void dump_on(outputStream * const out) PRODUCT_RETURN;
102 static void dump() { dump_on(tty); }
103 static void verify();
105 #ifndef PRODUCT
106 static bool contains_loader_data(ClassLoaderData* loader_data);
107 #endif
109 #if INCLUDE_TRACE
110 private:
111 static Ticks _class_unload_time;
112 static void class_unload_event(Klass* const k);
113 #endif
114 };
116 // ClassLoaderData class
118 class ClassLoaderData : public CHeapObj<mtClass> {
119 friend class VMStructs;
120 private:
121 class Dependencies VALUE_OBJ_CLASS_SPEC {
122 objArrayOop _list_head;
123 void locked_add(objArrayHandle last,
124 objArrayHandle new_dependency,
125 Thread* THREAD);
126 public:
127 Dependencies() : _list_head(NULL) {}
128 Dependencies(TRAPS) : _list_head(NULL) {
129 init(CHECK);
130 }
131 void add(Handle dependency, TRAPS);
132 void init(TRAPS);
133 void oops_do(OopClosure* f);
134 };
136 friend class ClassLoaderDataGraph;
137 friend class ClassLoaderDataGraphMetaspaceIterator;
138 friend class MetaDataFactory;
139 friend class Method;
141 static ClassLoaderData * _the_null_class_loader_data;
143 oop _class_loader; // oop used to uniquely identify a class loader
144 // class loader or a canonical class path
145 Dependencies _dependencies; // holds dependencies from this class loader
146 // data to others.
148 Metaspace * _metaspace; // Meta-space where meta-data defined by the
149 // classes in the class loader are allocated.
150 Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup.
151 bool _unloading; // true if this class loader goes away
152 bool _keep_alive; // if this CLD can be unloaded for anonymous loaders
153 bool _is_anonymous; // if this CLD is for an anonymous class
154 volatile int _claimed; // true if claimed, for example during GC traces.
155 // To avoid applying oop closure more than once.
156 // Has to be an int because we cas it.
157 Klass* _klasses; // The classes defined by the class loader.
159 JNIHandleBlock* _handles; // Handles to constant pool arrays
161 // These method IDs are created for the class loader and set to NULL when the
162 // class loader is unloaded. They are rarely freed, only for redefine classes
163 // and if they lose a data race in InstanceKlass.
164 JNIMethodBlock* _jmethod_ids;
166 // Metadata to be deallocated when it's safe at class unloading, when
167 // this class loader isn't unloaded itself.
168 GrowableArray<Metadata*>* _deallocate_list;
170 // Support for walking class loader data objects
171 ClassLoaderData* _next; /// Next loader_datas created
173 // ReadOnly and ReadWrite metaspaces (static because only on the null
174 // class loader for now).
175 static Metaspace* _ro_metaspace;
176 static Metaspace* _rw_metaspace;
178 void set_next(ClassLoaderData* next) { _next = next; }
179 ClassLoaderData* next() const { return _next; }
181 ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies);
182 ~ClassLoaderData();
184 void set_metaspace(Metaspace* m) { _metaspace = m; }
186 JNIHandleBlock* handles() const;
187 void set_handles(JNIHandleBlock* handles);
189 Mutex* metaspace_lock() const { return _metaspace_lock; }
191 // GC interface.
192 void clear_claimed() { _claimed = 0; }
193 bool claimed() const { return _claimed == 1; }
194 bool claim();
196 void unload();
197 bool keep_alive() const { return _keep_alive; }
198 bool is_alive(BoolObjectClosure* is_alive_closure) const;
199 void classes_do(void f(Klass*));
200 void loaded_classes_do(KlassClosure* klass_closure);
201 void classes_do(void f(InstanceKlass*));
203 // Deallocate free list during class unloading.
204 void free_deallocate_list();
206 // Allocate out of this class loader data
207 MetaWord* allocate(size_t size);
209 public:
210 // Accessors
211 Metaspace* metaspace_or_null() const { return _metaspace; }
213 static ClassLoaderData* the_null_class_loader_data() {
214 return _the_null_class_loader_data;
215 }
217 bool is_anonymous() const { return _is_anonymous; }
219 static void init_null_class_loader_data() {
220 assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
221 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
223 // We explicitly initialize the Dependencies object at a later phase in the initialization
224 _the_null_class_loader_data = new ClassLoaderData((oop)NULL, false, Dependencies());
225 ClassLoaderDataGraph::_head = _the_null_class_loader_data;
226 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
227 if (DumpSharedSpaces) {
228 _the_null_class_loader_data->initialize_shared_metaspaces();
229 }
230 }
232 bool is_the_null_class_loader_data() const {
233 return this == _the_null_class_loader_data;
234 }
235 bool is_ext_class_loader_data() const;
237 // The Metaspace is created lazily so may be NULL. This
238 // method will allocate a Metaspace if needed.
239 Metaspace* metaspace_non_null();
241 oop class_loader() const { return _class_loader; }
243 // Returns true if this class loader data is for a loader going away.
244 bool is_unloading() const {
245 assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");
246 return _unloading;
247 }
248 // Anonymous class loader data doesn't have anything to keep them from
249 // being unloaded during parsing the anonymous class.
250 void set_keep_alive(bool value) { _keep_alive = value; }
252 unsigned int identity_hash() {
253 return _class_loader == NULL ? 0 : _class_loader->identity_hash();
254 }
256 // Used when tracing from klasses.
257 void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
259 void classes_do(KlassClosure* klass_closure);
261 JNIMethodBlock* jmethod_ids() const { return _jmethod_ids; }
262 void set_jmethod_ids(JNIMethodBlock* new_block) { _jmethod_ids = new_block; }
264 void print_value() { print_value_on(tty); }
265 void print_value_on(outputStream* out) const;
266 void dump(outputStream * const out) PRODUCT_RETURN;
267 void verify();
268 const char* loader_name();
270 jobject add_handle(Handle h);
271 void add_class(Klass* k);
272 void remove_class(Klass* k);
273 bool contains_klass(Klass* k);
274 void record_dependency(Klass* to, TRAPS);
275 void init_dependencies(TRAPS);
277 void add_to_deallocate_list(Metadata* m);
279 static ClassLoaderData* class_loader_data(oop loader);
280 static ClassLoaderData* class_loader_data_or_null(oop loader);
281 static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);
282 static void print_loader(ClassLoaderData *loader_data, outputStream *out);
284 // CDS support
285 Metaspace* ro_metaspace();
286 Metaspace* rw_metaspace();
287 void initialize_shared_metaspaces();
288 };
290 class ClassLoaderDataGraphMetaspaceIterator : public StackObj {
291 ClassLoaderData* _data;
292 public:
293 ClassLoaderDataGraphMetaspaceIterator();
294 ~ClassLoaderDataGraphMetaspaceIterator();
295 bool repeat() { return _data != NULL; }
296 Metaspace* get_next() {
297 assert(_data != NULL, "Should not be NULL in call to the iterator");
298 Metaspace* result = _data->metaspace_or_null();
299 _data = _data->next();
300 // This result might be NULL for class loaders without metaspace
301 // yet. It would be nice to return only non-null results but
302 // there is no guarantee that there will be a non-null result
303 // down the list so the caller is going to have to check.
304 return result;
305 }
306 };
307 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP