1 /*
2 * Copyright (c) 2012, 2017, 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 #include "utilities/macros.hpp"
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 Metadebug;
55
56 // GC root for walking class loader data created
57
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;
71
72 static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS);
73 static void post_class_unload_events(void);
74 static void clean_metaspaces();
75 public:
76 static ClassLoaderData* find_or_create(Handle class_loader, TRAPS);
77 static void purge();
78 static void clear_claimed_marks();
79 // oops do
80 static void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
81 static void keep_alive_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
82 static void always_strong_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
83 // cld do
84 static void cld_do(CLDClosure* cl);
85 static void roots_cld_do(CLDClosure* strong, CLDClosure* weak);
86 static void keep_alive_cld_do(CLDClosure* cl);
87 static void always_strong_cld_do(CLDClosure* cl);
88 // klass do
89 static void classes_do(KlassClosure* klass_closure);
90 static void classes_do(void f(Klass* const));
91 static void loaded_classes_do(KlassClosure* klass_closure);
92 static void classes_unloading_do(void f(Klass* const));
93 static bool do_unloading(BoolObjectClosure* is_alive, bool clean_alive);
94
95 // CMS support.
96 static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
97 static GrowableArray<ClassLoaderData*>* new_clds();
98
99 static void set_should_purge(bool b) { _should_purge = b; }
100 static void purge_if_needed() {
101 // Only purge the CLDG for CMS if concurrent sweep is complete.
102 if (_should_purge) {
103 purge();
104 // reset for next time.
111 static void dump_on(outputStream * const out) PRODUCT_RETURN;
112 static void dump() { dump_on(tty); }
113 static void verify();
114
115 static bool unload_list_contains(const void* x);
116 #ifndef PRODUCT
117 static bool contains_loader_data(ClassLoaderData* loader_data);
118 #endif
119
120 #if INCLUDE_TRACE
121 private:
122 static Ticks _class_unload_time;
123 static void class_unload_event(Klass* const k);
124 #endif
125 };
126
127 // ClassLoaderData class
128
129 class ClassLoaderData : public CHeapObj<mtClass> {
130 friend class VMStructs;
131 private:
132 class Dependencies VALUE_OBJ_CLASS_SPEC {
133 objArrayOop _list_head;
134 void locked_add(objArrayHandle last,
135 objArrayHandle new_dependency,
136 Thread* THREAD);
137 public:
138 Dependencies() : _list_head(NULL) {}
139 Dependencies(TRAPS) : _list_head(NULL) {
140 init(CHECK);
141 }
142 void add(Handle dependency, TRAPS);
143 void init(TRAPS);
144 void oops_do(OopClosure* f);
145 };
146
147 class ChunkedHandleList VALUE_OBJ_CLASS_SPEC {
148 struct Chunk : public CHeapObj<mtClass> {
149 static const size_t CAPACITY = 32;
150
196 ChunkedHandleList _handles; // Handles to constant pool arrays, etc, which
197 // have the same life cycle of the corresponding ClassLoader.
198
199 // These method IDs are created for the class loader and set to NULL when the
200 // class loader is unloaded. They are rarely freed, only for redefine classes
201 // and if they lose a data race in InstanceKlass.
202 JNIMethodBlock* _jmethod_ids;
203
204 // Metadata to be deallocated when it's safe at class unloading, when
205 // this class loader isn't unloaded itself.
206 GrowableArray<Metadata*>* _deallocate_list;
207
208 // Support for walking class loader data objects
209 ClassLoaderData* _next; /// Next loader_datas created
210
211 // ReadOnly and ReadWrite metaspaces (static because only on the null
212 // class loader for now).
213 static Metaspace* _ro_metaspace;
214 static Metaspace* _rw_metaspace;
215
216 void set_next(ClassLoaderData* next) { _next = next; }
217 ClassLoaderData* next() const { return _next; }
218
219 ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies);
220 ~ClassLoaderData();
221
222 void set_metaspace(Metaspace* m) { _metaspace = m; }
223
224 Mutex* metaspace_lock() const { return _metaspace_lock; }
225
226 // GC interface.
227 void clear_claimed() { _claimed = 0; }
228 bool claimed() const { return _claimed == 1; }
229 bool claim();
230
231 void unload();
232 bool keep_alive() const { return _keep_alive; }
233 void classes_do(void f(Klass*));
234 void loaded_classes_do(KlassClosure* klass_closure);
235 void classes_do(void f(InstanceKlass*));
308 const char* loader_name();
309
310 jobject add_handle(Handle h);
311 void add_class(Klass* k);
312 void remove_class(Klass* k);
313 bool contains_klass(Klass* k);
314 void record_dependency(Klass* to, TRAPS);
315 void init_dependencies(TRAPS);
316
317 void add_to_deallocate_list(Metadata* m);
318
319 static ClassLoaderData* class_loader_data(oop loader);
320 static ClassLoaderData* class_loader_data_or_null(oop loader);
321 static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);
322 static void print_loader(ClassLoaderData *loader_data, outputStream *out);
323
324 // CDS support
325 Metaspace* ro_metaspace();
326 Metaspace* rw_metaspace();
327 void initialize_shared_metaspaces();
328 };
329
330 // An iterator that distributes Klasses to parallel worker threads.
331 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj {
332 Klass* volatile _next_klass;
333 public:
334 ClassLoaderDataGraphKlassIteratorAtomic();
335 Klass* next_klass();
336 private:
337 static Klass* next_klass_in_cldg(Klass* klass);
338 };
339
340 class ClassLoaderDataGraphMetaspaceIterator : public StackObj {
341 ClassLoaderData* _data;
342 public:
343 ClassLoaderDataGraphMetaspaceIterator();
344 ~ClassLoaderDataGraphMetaspaceIterator();
345 bool repeat() { return _data != NULL; }
346 Metaspace* get_next() {
347 assert(_data != NULL, "Should not be NULL in call to the iterator");
|
1 /*
2 * Copyright (c) 2012, 2019, 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 #include "utilities/macros.hpp"
35 #if INCLUDE_TRACE
36 #include "utilities/ticks.hpp"
37 #endif
38 #include "jfr/utilities/jfrLog.hpp"
39
40 //
41 // A class loader represents a linkset. Conceptually, a linkset identifies
42 // the complete transitive closure of resolved links that a dynamic linker can
43 // produce.
44 //
45 // A ClassLoaderData also encapsulates the allocation space, called a metaspace,
46 // used by the dynamic linker to allocate the runtime representation of all
47 // the types it defines.
48 //
49 // ClassLoaderData are stored in the runtime representation of classes and the
50 // system dictionary, are roots of garbage collection, and provides iterators
51 // for root tracing and other GC operations.
52
53 class ClassLoaderData;
54 class JNIMethodBlock;
55 class Metadebug;
56
57 // GC root for walking class loader data created
58
66 static ClassLoaderData* _head;
67 static ClassLoaderData* _unloading;
68 // CMS support.
69 static ClassLoaderData* _saved_head;
70 static ClassLoaderData* _saved_unloading;
71 static bool _should_purge;
72
73 static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS);
74 static void post_class_unload_events(void);
75 static void clean_metaspaces();
76 public:
77 static ClassLoaderData* find_or_create(Handle class_loader, TRAPS);
78 static void purge();
79 static void clear_claimed_marks();
80 // oops do
81 static void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
82 static void keep_alive_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
83 static void always_strong_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
84 // cld do
85 static void cld_do(CLDClosure* cl);
86 static void cld_unloading_do(CLDClosure* cl);
87 static void roots_cld_do(CLDClosure* strong, CLDClosure* weak);
88 static void keep_alive_cld_do(CLDClosure* cl);
89 static void always_strong_cld_do(CLDClosure* cl);
90 // klass do
91 static void classes_do(KlassClosure* klass_closure);
92 static void classes_do(void f(Klass* const));
93 static void loaded_classes_do(KlassClosure* klass_closure);
94 static void classes_unloading_do(void f(Klass* const));
95 static bool do_unloading(BoolObjectClosure* is_alive, bool clean_alive);
96
97 // CMS support.
98 static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
99 static GrowableArray<ClassLoaderData*>* new_clds();
100
101 static void set_should_purge(bool b) { _should_purge = b; }
102 static void purge_if_needed() {
103 // Only purge the CLDG for CMS if concurrent sweep is complete.
104 if (_should_purge) {
105 purge();
106 // reset for next time.
113 static void dump_on(outputStream * const out) PRODUCT_RETURN;
114 static void dump() { dump_on(tty); }
115 static void verify();
116
117 static bool unload_list_contains(const void* x);
118 #ifndef PRODUCT
119 static bool contains_loader_data(ClassLoaderData* loader_data);
120 #endif
121
122 #if INCLUDE_TRACE
123 private:
124 static Ticks _class_unload_time;
125 static void class_unload_event(Klass* const k);
126 #endif
127 };
128
129 // ClassLoaderData class
130
131 class ClassLoaderData : public CHeapObj<mtClass> {
132 friend class VMStructs;
133 friend class CLDClaimContext;
134 friend class CLDClaimStateClosure;
135 private:
136 class Dependencies VALUE_OBJ_CLASS_SPEC {
137 objArrayOop _list_head;
138 void locked_add(objArrayHandle last,
139 objArrayHandle new_dependency,
140 Thread* THREAD);
141 public:
142 Dependencies() : _list_head(NULL) {}
143 Dependencies(TRAPS) : _list_head(NULL) {
144 init(CHECK);
145 }
146 void add(Handle dependency, TRAPS);
147 void init(TRAPS);
148 void oops_do(OopClosure* f);
149 };
150
151 class ChunkedHandleList VALUE_OBJ_CLASS_SPEC {
152 struct Chunk : public CHeapObj<mtClass> {
153 static const size_t CAPACITY = 32;
154
200 ChunkedHandleList _handles; // Handles to constant pool arrays, etc, which
201 // have the same life cycle of the corresponding ClassLoader.
202
203 // These method IDs are created for the class loader and set to NULL when the
204 // class loader is unloaded. They are rarely freed, only for redefine classes
205 // and if they lose a data race in InstanceKlass.
206 JNIMethodBlock* _jmethod_ids;
207
208 // Metadata to be deallocated when it's safe at class unloading, when
209 // this class loader isn't unloaded itself.
210 GrowableArray<Metadata*>* _deallocate_list;
211
212 // Support for walking class loader data objects
213 ClassLoaderData* _next; /// Next loader_datas created
214
215 // ReadOnly and ReadWrite metaspaces (static because only on the null
216 // class loader for now).
217 static Metaspace* _ro_metaspace;
218 static Metaspace* _rw_metaspace;
219
220 TRACE_DEFINE_TRACE_ID_FIELD;
221
222 void set_next(ClassLoaderData* next) { _next = next; }
223 ClassLoaderData* next() const { return _next; }
224
225 ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies);
226 ~ClassLoaderData();
227
228 void set_metaspace(Metaspace* m) { _metaspace = m; }
229
230 Mutex* metaspace_lock() const { return _metaspace_lock; }
231
232 // GC interface.
233 void clear_claimed() { _claimed = 0; }
234 bool claimed() const { return _claimed == 1; }
235 bool claim();
236
237 void unload();
238 bool keep_alive() const { return _keep_alive; }
239 void classes_do(void f(Klass*));
240 void loaded_classes_do(KlassClosure* klass_closure);
241 void classes_do(void f(InstanceKlass*));
314 const char* loader_name();
315
316 jobject add_handle(Handle h);
317 void add_class(Klass* k);
318 void remove_class(Klass* k);
319 bool contains_klass(Klass* k);
320 void record_dependency(Klass* to, TRAPS);
321 void init_dependencies(TRAPS);
322
323 void add_to_deallocate_list(Metadata* m);
324
325 static ClassLoaderData* class_loader_data(oop loader);
326 static ClassLoaderData* class_loader_data_or_null(oop loader);
327 static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);
328 static void print_loader(ClassLoaderData *loader_data, outputStream *out);
329
330 // CDS support
331 Metaspace* ro_metaspace();
332 Metaspace* rw_metaspace();
333 void initialize_shared_metaspaces();
334
335 TRACE_DEFINE_TRACE_ID_METHODS;
336 };
337
338 // An iterator that distributes Klasses to parallel worker threads.
339 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj {
340 Klass* volatile _next_klass;
341 public:
342 ClassLoaderDataGraphKlassIteratorAtomic();
343 Klass* next_klass();
344 private:
345 static Klass* next_klass_in_cldg(Klass* klass);
346 };
347
348 class ClassLoaderDataGraphMetaspaceIterator : public StackObj {
349 ClassLoaderData* _data;
350 public:
351 ClassLoaderDataGraphMetaspaceIterator();
352 ~ClassLoaderDataGraphMetaspaceIterator();
353 bool repeat() { return _data != NULL; }
354 Metaspace* get_next() {
355 assert(_data != NULL, "Should not be NULL in call to the iterator");
|