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 #if INCLUDE_TRACE
36 # include "utilities/ticks.hpp"
37 #endif
38
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.
51
52 class ClassLoaderData;
53 class JNIMethodBlock;
54 class JNIHandleBlock;
55 class Metadebug;
56
57 // GC root for walking class loader data created
58
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 bool _should_purge;
70
71 static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS);
72 static void post_class_unload_events(void);
73 public:
74 static ClassLoaderData* find_or_create(Handle class_loader, TRAPS);
75 static void purge();
76 static void clear_claimed_marks();
77 static void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
78 static void always_strong_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
79 static void keep_alive_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
80 static void cld_do(CLDClosure* cl);
81 static void classes_do(KlassClosure* klass_closure);
82 static void classes_do(void f(Klass* const));
83 static void methods_do(void f(Method*));
84 static void loaded_classes_do(KlassClosure* klass_closure);
85 static void classes_unloading_do(void f(Klass* const));
86 static bool do_unloading(BoolObjectClosure* is_alive);
87
88 // CMS support.
89 static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
90 static GrowableArray<ClassLoaderData*>* new_clds();
91
92 static void set_should_purge(bool b) { _should_purge = b; }
93 static void purge_if_needed() {
94 // Only purge the CLDG for CMS if concurrent sweep is complete.
95 if (_should_purge) {
96 purge();
97 // reset for next time.
98 set_should_purge(false);
99 }
100 }
101
102 static void dump_on(outputStream * const out) PRODUCT_RETURN;
103 static void dump() { dump_on(tty); }
104 static void verify();
105
106 #ifndef PRODUCT
107 static bool contains_loader_data(ClassLoaderData* loader_data);
108 #endif
109
110 #if INCLUDE_TRACE
111 private:
112 static Ticks _class_unload_time;
113 static void class_unload_event(Klass* const k);
114 #endif
115 };
116
117 // ClassLoaderData class
118
119 class ClassLoaderData : public CHeapObj<mtClass> {
120 friend class VMStructs;
121 private:
122 class Dependencies VALUE_OBJ_CLASS_SPEC {
123 objArrayOop _list_head;
124 void locked_add(objArrayHandle last,
125 objArrayHandle new_dependency,
126 Thread* THREAD);
127 public:
128 Dependencies() : _list_head(NULL) {}
129 Dependencies(TRAPS) : _list_head(NULL) {
130 init(CHECK);
131 }
132 void add(Handle dependency, TRAPS);
133 void init(TRAPS);
134 void oops_do(OopClosure* f);
135 };
136
137 friend class ClassLoaderDataGraph;
138 friend class ClassLoaderDataGraphMetaspaceIterator;
139 friend class MetaDataFactory;
140 friend class Method;
141
142 static ClassLoaderData * _the_null_class_loader_data;
143
144 oop _class_loader; // oop used to uniquely identify a class loader
145 // class loader or a canonical class path
146 Dependencies _dependencies; // holds dependencies from this class loader
147 // data to others.
148
149 Metaspace * _metaspace; // Meta-space where meta-data defined by the
150 // classes in the class loader are allocated.
151 Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup.
152 bool _unloading; // true if this class loader goes away
153 bool _keep_alive; // if this CLD is kept alive without a keep_alive_object().
154 bool _is_anonymous; // if this CLD is for an anonymous class
155 volatile int _claimed; // true if claimed, for example during GC traces.
156 // To avoid applying oop closure more than once.
157 // Has to be an int because we cas it.
177 static Metaspace* _rw_metaspace;
178
179 void set_next(ClassLoaderData* next) { _next = next; }
180 ClassLoaderData* next() const { return _next; }
181
182 ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies);
183 ~ClassLoaderData();
184
185 void set_metaspace(Metaspace* m) { _metaspace = m; }
186
187 JNIHandleBlock* handles() const;
188 void set_handles(JNIHandleBlock* handles);
189
190 // GC interface.
191 void clear_claimed() { _claimed = 0; }
192 bool claimed() const { return _claimed == 1; }
193 bool claim();
194
195 void unload();
196 bool keep_alive() const { return _keep_alive; }
197 bool is_alive(BoolObjectClosure* is_alive_closure) const;
198 void classes_do(void f(Klass*));
199 void loaded_classes_do(KlassClosure* klass_closure);
200 void classes_do(void f(InstanceKlass*));
201 void methods_do(void f(Method*));
202
203 // Deallocate free list during class unloading.
204 void free_deallocate_list();
205
206 // Allocate out of this class loader data
207 MetaWord* allocate(size_t size);
208
209 public:
210 // Accessors
211 Metaspace* metaspace_or_null() const { return _metaspace; }
212
213 static ClassLoaderData* the_null_class_loader_data() {
214 return _the_null_class_loader_data;
215 }
216
217 Mutex* metaspace_lock() const { return _metaspace_lock; }
218
219 bool is_anonymous() const { return _is_anonymous; }
220
221 static void init_null_class_loader_data() {
222 assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
223 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
224
225 // We explicitly initialize the Dependencies object at a later phase in the initialization
226 _the_null_class_loader_data = new ClassLoaderData((oop)NULL, false, Dependencies());
227 ClassLoaderDataGraph::_head = _the_null_class_loader_data;
228 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
229 if (DumpSharedSpaces) {
273 const char* loader_name();
274
275 jobject add_handle(Handle h);
276 void add_class(Klass* k);
277 void remove_class(Klass* k);
278 bool contains_klass(Klass* k);
279 void record_dependency(Klass* to, TRAPS);
280 void init_dependencies(TRAPS);
281
282 void add_to_deallocate_list(Metadata* m);
283
284 static ClassLoaderData* class_loader_data(oop loader);
285 static ClassLoaderData* class_loader_data_or_null(oop loader);
286 static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);
287 static void print_loader(ClassLoaderData *loader_data, outputStream *out);
288
289 // CDS support
290 Metaspace* ro_metaspace();
291 Metaspace* rw_metaspace();
292 void initialize_shared_metaspaces();
293 };
294
295 class ClassLoaderDataGraphMetaspaceIterator : public StackObj {
296 ClassLoaderData* _data;
297 public:
298 ClassLoaderDataGraphMetaspaceIterator();
299 ~ClassLoaderDataGraphMetaspaceIterator();
300 bool repeat() { return _data != NULL; }
301 Metaspace* get_next() {
302 assert(_data != NULL, "Should not be NULL in call to the iterator");
303 Metaspace* result = _data->metaspace_or_null();
304 _data = _data->next();
305 // This result might be NULL for class loaders without metaspace
306 // yet. It would be nice to return only non-null results but
307 // there is no guarantee that there will be a non-null result
308 // down the list so the caller is going to have to check.
309 return result;
310 }
311 };
312 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
|
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 #if INCLUDE_TRACE
35 # include "utilities/ticks.hpp"
36 #endif
37
38 //
39 // A class loader represents a linkset. Conceptually, a linkset identifies
40 // the complete transitive closure of resolved links that a dynamic linker can
41 // produce.
42 //
43 // A ClassLoaderData also encapsulates the allocation space, called a metaspace,
44 // used by the dynamic linker to allocate the runtime representation of all
45 // the types it defines.
46 //
47 // ClassLoaderData are stored in the runtime representation of classes and the
48 // system dictionary, are roots of garbage collection, and provides iterators
49 // for root tracing and other GC operations.
50
51 class ClassLoaderData;
52 class JNIMethodBlock;
53 class JNIHandleBlock;
54 class Metadebug;
55
56 // GC root for walking class loader data created
57
58 class ClassLoaderDataGraph : public AllStatic {
59 friend class ClassLoaderData;
60 friend class ClassLoaderDataGraphMetaspaceIterator;
61 friend class ClassLoaderDataGraphKlassIteratorAtomic;
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 bool _should_purge;
70
71 static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS);
72 static void post_class_unload_events(void);
73 public:
74 static ClassLoaderData* find_or_create(Handle class_loader, TRAPS);
75 static void purge();
76 static void clear_claimed_marks();
77 // oops do
78 static void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
79 static void keep_alive_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
80 static void always_strong_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
81 // cld do
82 static void cld_do(CLDClosure* cl);
83 static void roots_cld_do(CLDClosure* strong, CLDClosure* weak);
84 static void keep_alive_cld_do(CLDClosure* cl);
85 static void always_strong_cld_do(CLDClosure* cl);
86 // klass do
87 static void classes_do(KlassClosure* klass_closure);
88 static void classes_do(void f(Klass* const));
89 static void methods_do(void f(Method*));
90 static void loaded_classes_do(KlassClosure* klass_closure);
91 static void classes_unloading_do(void f(Klass* const));
92 static bool do_unloading(BoolObjectClosure* is_alive);
93
94 // CMS support.
95 static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
96 static GrowableArray<ClassLoaderData*>* new_clds();
97
98 static void set_should_purge(bool b) { _should_purge = b; }
99 static void purge_if_needed() {
100 // Only purge the CLDG for CMS if concurrent sweep is complete.
101 if (_should_purge) {
102 purge();
103 // reset for next time.
104 set_should_purge(false);
105 }
106 }
107
108 static void dump_on(outputStream * const out) PRODUCT_RETURN;
109 static void dump() { dump_on(tty); }
110 static void verify();
111
112 static bool unload_list_contains(const void* x);
113 #ifndef PRODUCT
114 static bool contains_loader_data(ClassLoaderData* loader_data);
115 #endif
116
117 #if INCLUDE_TRACE
118 private:
119 static Ticks _class_unload_time;
120 static void class_unload_event(Klass* const k);
121 #endif
122 };
123
124 // ClassLoaderData class
125
126 class ClassLoaderData : public CHeapObj<mtClass> {
127 friend class VMStructs;
128 private:
129 class Dependencies VALUE_OBJ_CLASS_SPEC {
130 objArrayOop _list_head;
131 void locked_add(objArrayHandle last,
132 objArrayHandle new_dependency,
133 Thread* THREAD);
134 public:
135 Dependencies() : _list_head(NULL) {}
136 Dependencies(TRAPS) : _list_head(NULL) {
137 init(CHECK);
138 }
139 void add(Handle dependency, TRAPS);
140 void init(TRAPS);
141 void oops_do(OopClosure* f);
142 };
143
144 friend class ClassLoaderDataGraph;
145 friend class ClassLoaderDataGraphKlassIteratorAtomic;
146 friend class ClassLoaderDataGraphMetaspaceIterator;
147 friend class MetaDataFactory;
148 friend class Method;
149
150 static ClassLoaderData * _the_null_class_loader_data;
151
152 oop _class_loader; // oop used to uniquely identify a class loader
153 // class loader or a canonical class path
154 Dependencies _dependencies; // holds dependencies from this class loader
155 // data to others.
156
157 Metaspace * _metaspace; // Meta-space where meta-data defined by the
158 // classes in the class loader are allocated.
159 Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup.
160 bool _unloading; // true if this class loader goes away
161 bool _keep_alive; // if this CLD is kept alive without a keep_alive_object().
162 bool _is_anonymous; // if this CLD is for an anonymous class
163 volatile int _claimed; // true if claimed, for example during GC traces.
164 // To avoid applying oop closure more than once.
165 // Has to be an int because we cas it.
185 static Metaspace* _rw_metaspace;
186
187 void set_next(ClassLoaderData* next) { _next = next; }
188 ClassLoaderData* next() const { return _next; }
189
190 ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies);
191 ~ClassLoaderData();
192
193 void set_metaspace(Metaspace* m) { _metaspace = m; }
194
195 JNIHandleBlock* handles() const;
196 void set_handles(JNIHandleBlock* handles);
197
198 // GC interface.
199 void clear_claimed() { _claimed = 0; }
200 bool claimed() const { return _claimed == 1; }
201 bool claim();
202
203 void unload();
204 bool keep_alive() const { return _keep_alive; }
205 void classes_do(void f(Klass*));
206 void loaded_classes_do(KlassClosure* klass_closure);
207 void classes_do(void f(InstanceKlass*));
208 void methods_do(void f(Method*));
209
210 // Deallocate free list during class unloading.
211 void free_deallocate_list();
212
213 // Allocate out of this class loader data
214 MetaWord* allocate(size_t size);
215
216 public:
217
218 bool is_alive(BoolObjectClosure* is_alive_closure) const;
219
220 // Accessors
221 Metaspace* metaspace_or_null() const { return _metaspace; }
222
223 static ClassLoaderData* the_null_class_loader_data() {
224 return _the_null_class_loader_data;
225 }
226
227 Mutex* metaspace_lock() const { return _metaspace_lock; }
228
229 bool is_anonymous() const { return _is_anonymous; }
230
231 static void init_null_class_loader_data() {
232 assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
233 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
234
235 // We explicitly initialize the Dependencies object at a later phase in the initialization
236 _the_null_class_loader_data = new ClassLoaderData((oop)NULL, false, Dependencies());
237 ClassLoaderDataGraph::_head = _the_null_class_loader_data;
238 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
239 if (DumpSharedSpaces) {
283 const char* loader_name();
284
285 jobject add_handle(Handle h);
286 void add_class(Klass* k);
287 void remove_class(Klass* k);
288 bool contains_klass(Klass* k);
289 void record_dependency(Klass* to, TRAPS);
290 void init_dependencies(TRAPS);
291
292 void add_to_deallocate_list(Metadata* m);
293
294 static ClassLoaderData* class_loader_data(oop loader);
295 static ClassLoaderData* class_loader_data_or_null(oop loader);
296 static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);
297 static void print_loader(ClassLoaderData *loader_data, outputStream *out);
298
299 // CDS support
300 Metaspace* ro_metaspace();
301 Metaspace* rw_metaspace();
302 void initialize_shared_metaspaces();
303 };
304
305 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj {
306 volatile Klass* _next_klass;
307 public:
308 ClassLoaderDataGraphKlassIteratorAtomic();
309 Klass* next_klass();
310 private:
311 static Klass* next_klass_in_cldg(Klass* klass);
312 };
313
314 class ClassLoaderDataGraphMetaspaceIterator : public StackObj {
315 ClassLoaderData* _data;
316 public:
317 ClassLoaderDataGraphMetaspaceIterator();
318 ~ClassLoaderDataGraphMetaspaceIterator();
319 bool repeat() { return _data != NULL; }
320 Metaspace* get_next() {
321 assert(_data != NULL, "Should not be NULL in call to the iterator");
322 Metaspace* result = _data->metaspace_or_null();
323 _data = _data->next();
324 // This result might be NULL for class loaders without metaspace
325 // yet. It would be nice to return only non-null results but
326 // there is no guarantee that there will be a non-null result
327 // down the list so the caller is going to have to check.
328 return result;
329 }
330 };
331 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
|