29 #include "memory/memRegion.hpp"
30 #include "memory/metaspace.hpp"
31 #include "memory/metaspaceCounters.hpp"
32 #include "runtime/mutex.hpp"
33 #include "trace/traceMacros.hpp"
34 #include "utilities/growableArray.hpp"
35 #include "utilities/macros.hpp"
36 #if INCLUDE_TRACE
37 #include "utilities/ticks.hpp"
38 #endif
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 class ModuleEntry;
57 class PackageEntry;
58 class ModuleEntryTable;
59 class PackageEntryTable;
60
61 // GC root for walking class loader data created
62
63 class ClassLoaderDataGraph : public AllStatic {
64 friend class ClassLoaderData;
65 friend class ClassLoaderDataGraphMetaspaceIterator;
66 friend class ClassLoaderDataGraphKlassIteratorAtomic;
67 friend class VMStructs;
68 private:
69 // All CLDs (except the null CLD) can be reached by walking _head->_next->...
70 static ClassLoaderData* _head;
71 static ClassLoaderData* _unloading;
72 // CMS support.
73 static ClassLoaderData* _saved_head;
74 static ClassLoaderData* _saved_unloading;
75 static bool _should_purge;
76 // OOM has been seen in metaspace allocation. Used to prevent some
77 // allocations until class unloading
78 static bool _metaspace_oom;
79
80 static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS);
81 static void post_class_unload_events();
82 public:
83 static ClassLoaderData* find_or_create(Handle class_loader, TRAPS);
84 static void purge();
85 static void clear_claimed_marks();
86 // oops do
92 static void cld_unloading_do(CLDClosure* cl);
93 static void roots_cld_do(CLDClosure* strong, CLDClosure* weak);
94 static void keep_alive_cld_do(CLDClosure* cl);
95 static void always_strong_cld_do(CLDClosure* cl);
96 // klass do
97 // Walking classes through the ClassLoaderDataGraph include array classes. It also includes
98 // classes that are allocated but not loaded, classes that have errors, and scratch classes
99 // for redefinition. These classes are removed during the next class unloading.
100 // Walking the ClassLoaderDataGraph also includes anonymous classes.
101 static void classes_do(KlassClosure* klass_closure);
102 static void classes_do(void f(Klass* const));
103 static void methods_do(void f(Method*));
104 static void modules_do(void f(ModuleEntry*));
105 static void modules_unloading_do(void f(ModuleEntry*));
106 static void packages_do(void f(PackageEntry*));
107 static void packages_unloading_do(void f(PackageEntry*));
108 static void loaded_classes_do(KlassClosure* klass_closure);
109 static void classes_unloading_do(void f(Klass* const));
110 static bool do_unloading(BoolObjectClosure* is_alive, bool clean_previous_versions);
111
112 // CMS support.
113 static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
114 static GrowableArray<ClassLoaderData*>* new_clds();
115
116 static void set_should_purge(bool b) { _should_purge = b; }
117 static void purge_if_needed() {
118 // Only purge the CLDG for CMS if concurrent sweep is complete.
119 if (_should_purge) {
120 purge();
121 // reset for next time.
122 set_should_purge(false);
123 }
124 }
125
126 static bool has_metaspace_oom() { return _metaspace_oom; }
127 static void set_metaspace_oom(bool value) { _metaspace_oom = value; }
128
129 static void dump_on(outputStream * const out) PRODUCT_RETURN;
130 static void dump() { dump_on(tty); }
131 static void verify();
176
177 Chunk* _head;
178
179 void oops_do_chunk(OopClosure* f, Chunk* c, const juint size);
180
181 public:
182 ChunkedHandleList() : _head(NULL) {}
183 ~ChunkedHandleList();
184
185 // Only one thread at a time can add, guarded by ClassLoaderData::metaspace_lock().
186 // However, multiple threads can execute oops_do concurrently with add.
187 oop* add(oop o);
188 #ifdef ASSERT
189 bool contains(oop* p);
190 #endif
191 void oops_do(OopClosure* f);
192 };
193
194 friend class ClassLoaderDataGraph;
195 friend class ClassLoaderDataGraphKlassIteratorAtomic;
196 friend class ClassLoaderDataGraphMetaspaceIterator;
197 friend class MetaDataFactory;
198 friend class Method;
199
200 static ClassLoaderData * _the_null_class_loader_data;
201
202 oop _class_loader; // oop used to uniquely identify a class loader
203 // class loader or a canonical class path
204 Dependencies _dependencies; // holds dependencies from this class loader
205 // data to others.
206
207 Metaspace * volatile _metaspace; // Meta-space where meta-data defined by the
208 // classes in the class loader are allocated.
209 Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup.
210 bool _unloading; // true if this class loader goes away
211 bool _is_anonymous; // if this CLD is for an anonymous class
212 s2 _keep_alive; // if this CLD is kept alive without a keep_alive_object().
213 // Used for anonymous classes and the boot class
214 // loader. _keep_alive does not need to be volatile or
215 // atomic since there is one unique CLD per anonymous class.
216 volatile int _claimed; // true if claimed, for example during GC traces.
217 // To avoid applying oop closure more than once.
218 // Has to be an int because we cas it.
219 ChunkedHandleList _handles; // Handles to constant pool arrays, Modules, etc, which
220 // have the same life cycle of the corresponding ClassLoader.
221
222 Klass* volatile _klasses; // The classes defined by the class loader.
223 PackageEntryTable* volatile _packages; // The packages defined by the class loader.
224 ModuleEntry* _unnamed_module; // This class loader's unnamed module.
225 ModuleEntryTable* volatile _modules; // The modules defined by the class loader.
226
227 // These method IDs are created for the class loader and set to NULL when the
228 // class loader is unloaded. They are rarely freed, only for redefine classes
229 // and if they lose a data race in InstanceKlass.
230 JNIMethodBlock* _jmethod_ids;
231
232 // Metadata to be deallocated when it's safe at class unloading, when
233 // this class loader isn't unloaded itself.
234 GrowableArray<Metadata*>* _deallocate_list;
235
236 // Support for walking class loader data objects
237 ClassLoaderData* _next; /// Next loader_datas created
238
239 // ReadOnly and ReadWrite metaspaces (static because only on the null
240 // class loader for now).
241 static Metaspace* _ro_metaspace;
242 static Metaspace* _rw_metaspace;
243
244 TRACE_DEFINE_TRACE_ID_FIELD;
245
252 // GC interface.
253 void clear_claimed() { _claimed = 0; }
254 bool claimed() const { return _claimed == 1; }
255 bool claim();
256
257 void unload();
258 bool keep_alive() const { return _keep_alive > 0; }
259 void classes_do(void f(Klass*));
260 void loaded_classes_do(KlassClosure* klass_closure);
261 void classes_do(void f(InstanceKlass*));
262 void methods_do(void f(Method*));
263 void modules_do(void f(ModuleEntry*));
264 void packages_do(void f(PackageEntry*));
265
266 // Deallocate free list during class unloading.
267 void free_deallocate_list();
268
269 // Allocate out of this class loader data
270 MetaWord* allocate(size_t size);
271
272 public:
273
274 bool is_alive(BoolObjectClosure* is_alive_closure) const;
275
276 // Accessors
277 Metaspace* metaspace_or_null() const { return _metaspace; }
278
279 static ClassLoaderData* the_null_class_loader_data() {
280 return _the_null_class_loader_data;
281 }
282
283 Mutex* metaspace_lock() const { return _metaspace_lock; }
284
285 bool is_anonymous() const { return _is_anonymous; }
286
287 static void init_null_class_loader_data() {
288 assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
289 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
290
291 // We explicitly initialize the Dependencies object at a later phase in the initialization
307 // The Metaspace is created lazily so may be NULL. This
308 // method will allocate a Metaspace if needed.
309 Metaspace* metaspace_non_null();
310
311 oop class_loader() const { return _class_loader; }
312
313 // The object the GC is using to keep this ClassLoaderData alive.
314 oop keep_alive_object() const;
315
316 // Returns true if this class loader data is for a loader going away.
317 bool is_unloading() const {
318 assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");
319 return _unloading;
320 }
321
322 // Used to refcount an anonymous class's CLD in order to
323 // indicate their aliveness without a keep_alive_object().
324 void inc_keep_alive();
325 void dec_keep_alive();
326
327 inline unsigned int identity_hash() const;
328
329 // Used when tracing from klasses.
330 void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
331
332 void classes_do(KlassClosure* klass_closure);
333
334 JNIMethodBlock* jmethod_ids() const { return _jmethod_ids; }
335 void set_jmethod_ids(JNIMethodBlock* new_block) { _jmethod_ids = new_block; }
336
337 void print_value() { print_value_on(tty); }
338 void print_value_on(outputStream* out) const;
339 void dump(outputStream * const out) PRODUCT_RETURN;
340 void verify();
341 const char* loader_name();
342
343 jobject add_handle(Handle h);
344 void remove_handle_unsafe(jobject h);
345 void add_class(Klass* k, bool publicize = true);
346 void remove_class(Klass* k);
347 bool contains_klass(Klass* k);
348 void record_dependency(const Klass* to, TRAPS);
349 void init_dependencies(TRAPS);
350 PackageEntryTable* packages() { return _packages; }
351 ModuleEntry* unnamed_module() { return _unnamed_module; }
352 ModuleEntryTable* modules();
353 bool modules_defined() { return (_modules != NULL); }
354
355 void add_to_deallocate_list(Metadata* m);
356
357 static ClassLoaderData* class_loader_data(oop loader);
358 static ClassLoaderData* class_loader_data_or_null(oop loader);
359 static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);
360 static void print_loader(ClassLoaderData *loader_data, outputStream *out);
361
362 // CDS support
363 Metaspace* ro_metaspace();
364 Metaspace* rw_metaspace();
365 void initialize_shared_metaspaces();
366
367 TRACE_DEFINE_TRACE_ID_METHODS;
368 };
369
370 // An iterator that distributes Klasses to parallel worker threads.
371 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj {
372 Klass* volatile _next_klass;
373 public:
|
29 #include "memory/memRegion.hpp"
30 #include "memory/metaspace.hpp"
31 #include "memory/metaspaceCounters.hpp"
32 #include "runtime/mutex.hpp"
33 #include "trace/traceMacros.hpp"
34 #include "utilities/growableArray.hpp"
35 #include "utilities/macros.hpp"
36 #if INCLUDE_TRACE
37 #include "utilities/ticks.hpp"
38 #endif
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,
50 // and provides iterators for root tracing and other GC operations.
51
52 class ClassLoaderData;
53 class JNIMethodBlock;
54 class Metadebug;
55 class ModuleEntry;
56 class PackageEntry;
57 class ModuleEntryTable;
58 class PackageEntryTable;
59 class DictionaryEntry;
60 class Dictionary;
61
62 // GC root for walking class loader data created
63
64 class ClassLoaderDataGraph : public AllStatic {
65 friend class ClassLoaderData;
66 friend class ClassLoaderDataGraphMetaspaceIterator;
67 friend class ClassLoaderDataGraphKlassIteratorAtomic;
68 friend class ClassLoaderDataGraphKlassIteratorStatic;
69 friend class VMStructs;
70 private:
71 // All CLDs (except the null CLD) can be reached by walking _head->_next->...
72 static ClassLoaderData* _head;
73 static ClassLoaderData* _unloading;
74 // CMS support.
75 static ClassLoaderData* _saved_head;
76 static ClassLoaderData* _saved_unloading;
77 static bool _should_purge;
78 // OOM has been seen in metaspace allocation. Used to prevent some
79 // allocations until class unloading
80 static bool _metaspace_oom;
81
82 static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS);
83 static void post_class_unload_events();
84 public:
85 static ClassLoaderData* find_or_create(Handle class_loader, TRAPS);
86 static void purge();
87 static void clear_claimed_marks();
88 // oops do
94 static void cld_unloading_do(CLDClosure* cl);
95 static void roots_cld_do(CLDClosure* strong, CLDClosure* weak);
96 static void keep_alive_cld_do(CLDClosure* cl);
97 static void always_strong_cld_do(CLDClosure* cl);
98 // klass do
99 // Walking classes through the ClassLoaderDataGraph include array classes. It also includes
100 // classes that are allocated but not loaded, classes that have errors, and scratch classes
101 // for redefinition. These classes are removed during the next class unloading.
102 // Walking the ClassLoaderDataGraph also includes anonymous classes.
103 static void classes_do(KlassClosure* klass_closure);
104 static void classes_do(void f(Klass* const));
105 static void methods_do(void f(Method*));
106 static void modules_do(void f(ModuleEntry*));
107 static void modules_unloading_do(void f(ModuleEntry*));
108 static void packages_do(void f(PackageEntry*));
109 static void packages_unloading_do(void f(PackageEntry*));
110 static void loaded_classes_do(KlassClosure* klass_closure);
111 static void classes_unloading_do(void f(Klass* const));
112 static bool do_unloading(BoolObjectClosure* is_alive, bool clean_previous_versions);
113
114 // dictionary do
115 // Iterate over all klasses in dictionary, but
116 // just the classes from defining class loaders.
117 static void dictionary_classes_do(void f(InstanceKlass*));
118 // Added for initialize_itable_for_klass to handle exceptions.
119 static void dictionary_classes_do(void f(InstanceKlass*, TRAPS), TRAPS);
120
121 // Iterate all classes and their class loaders, including initiating class loaders.
122 static void dictionary_all_entries_do(void f(InstanceKlass*, ClassLoaderData*));
123
124 // VM_CounterDecay iteration support
125 static InstanceKlass* try_get_next_class();
126
127 static void verify_dictionary();
128 static void print_dictionary(bool details);
129
130 // CMS support.
131 static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
132 static GrowableArray<ClassLoaderData*>* new_clds();
133
134 static void set_should_purge(bool b) { _should_purge = b; }
135 static void purge_if_needed() {
136 // Only purge the CLDG for CMS if concurrent sweep is complete.
137 if (_should_purge) {
138 purge();
139 // reset for next time.
140 set_should_purge(false);
141 }
142 }
143
144 static bool has_metaspace_oom() { return _metaspace_oom; }
145 static void set_metaspace_oom(bool value) { _metaspace_oom = value; }
146
147 static void dump_on(outputStream * const out) PRODUCT_RETURN;
148 static void dump() { dump_on(tty); }
149 static void verify();
194
195 Chunk* _head;
196
197 void oops_do_chunk(OopClosure* f, Chunk* c, const juint size);
198
199 public:
200 ChunkedHandleList() : _head(NULL) {}
201 ~ChunkedHandleList();
202
203 // Only one thread at a time can add, guarded by ClassLoaderData::metaspace_lock().
204 // However, multiple threads can execute oops_do concurrently with add.
205 oop* add(oop o);
206 #ifdef ASSERT
207 bool contains(oop* p);
208 #endif
209 void oops_do(OopClosure* f);
210 };
211
212 friend class ClassLoaderDataGraph;
213 friend class ClassLoaderDataGraphKlassIteratorAtomic;
214 friend class ClassLoaderDataGraphKlassIteratorStatic;
215 friend class ClassLoaderDataGraphMetaspaceIterator;
216 friend class MetaDataFactory;
217 friend class Method;
218
219 static ClassLoaderData * _the_null_class_loader_data;
220
221 oop _class_loader; // oop used to uniquely identify a class loader
222 // class loader or a canonical class path
223 Dependencies _dependencies; // holds dependencies from this class loader
224 // data to others.
225
226 Metaspace * volatile _metaspace; // Meta-space where meta-data defined by the
227 // classes in the class loader are allocated.
228 Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup.
229 bool _unloading; // true if this class loader goes away
230 bool _is_anonymous; // if this CLD is for an anonymous class
231 s2 _keep_alive; // if this CLD is kept alive without a keep_alive_object().
232 // Used for anonymous classes and the boot class
233 // loader. _keep_alive does not need to be volatile or
234 // atomic since there is one unique CLD per anonymous class.
235 volatile int _claimed; // true if claimed, for example during GC traces.
236 // To avoid applying oop closure more than once.
237 // Has to be an int because we cas it.
238 ChunkedHandleList _handles; // Handles to constant pool arrays, Modules, etc, which
239 // have the same life cycle of the corresponding ClassLoader.
240
241 Klass* volatile _klasses; // The classes defined by the class loader.
242 PackageEntryTable* volatile _packages; // The packages defined by the class loader.
243 ModuleEntryTable* volatile _modules; // The modules defined by the class loader.
244 ModuleEntry* _unnamed_module; // This class loader's unnamed module.
245 Dictionary* _dictionary; // The loaded InstanceKlasses, including initiated by this class loader
246
247 // These method IDs are created for the class loader and set to NULL when the
248 // class loader is unloaded. They are rarely freed, only for redefine classes
249 // and if they lose a data race in InstanceKlass.
250 JNIMethodBlock* _jmethod_ids;
251
252 // Metadata to be deallocated when it's safe at class unloading, when
253 // this class loader isn't unloaded itself.
254 GrowableArray<Metadata*>* _deallocate_list;
255
256 // Support for walking class loader data objects
257 ClassLoaderData* _next; /// Next loader_datas created
258
259 // ReadOnly and ReadWrite metaspaces (static because only on the null
260 // class loader for now).
261 static Metaspace* _ro_metaspace;
262 static Metaspace* _rw_metaspace;
263
264 TRACE_DEFINE_TRACE_ID_FIELD;
265
272 // GC interface.
273 void clear_claimed() { _claimed = 0; }
274 bool claimed() const { return _claimed == 1; }
275 bool claim();
276
277 void unload();
278 bool keep_alive() const { return _keep_alive > 0; }
279 void classes_do(void f(Klass*));
280 void loaded_classes_do(KlassClosure* klass_closure);
281 void classes_do(void f(InstanceKlass*));
282 void methods_do(void f(Method*));
283 void modules_do(void f(ModuleEntry*));
284 void packages_do(void f(PackageEntry*));
285
286 // Deallocate free list during class unloading.
287 void free_deallocate_list();
288
289 // Allocate out of this class loader data
290 MetaWord* allocate(size_t size);
291
292 Dictionary* create_dictionary();
293 public:
294
295 bool is_alive(BoolObjectClosure* is_alive_closure) const;
296
297 // Accessors
298 Metaspace* metaspace_or_null() const { return _metaspace; }
299
300 static ClassLoaderData* the_null_class_loader_data() {
301 return _the_null_class_loader_data;
302 }
303
304 Mutex* metaspace_lock() const { return _metaspace_lock; }
305
306 bool is_anonymous() const { return _is_anonymous; }
307
308 static void init_null_class_loader_data() {
309 assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
310 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
311
312 // We explicitly initialize the Dependencies object at a later phase in the initialization
328 // The Metaspace is created lazily so may be NULL. This
329 // method will allocate a Metaspace if needed.
330 Metaspace* metaspace_non_null();
331
332 oop class_loader() const { return _class_loader; }
333
334 // The object the GC is using to keep this ClassLoaderData alive.
335 oop keep_alive_object() const;
336
337 // Returns true if this class loader data is for a loader going away.
338 bool is_unloading() const {
339 assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");
340 return _unloading;
341 }
342
343 // Used to refcount an anonymous class's CLD in order to
344 // indicate their aliveness without a keep_alive_object().
345 void inc_keep_alive();
346 void dec_keep_alive();
347
348 inline unsigned int identity_hash() const { return (unsigned int)(((intptr_t)this) >> 3); }
349
350 // Used when tracing from klasses.
351 void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
352
353 void classes_do(KlassClosure* klass_closure);
354 Klass* klasses() { return _klasses; }
355
356 JNIMethodBlock* jmethod_ids() const { return _jmethod_ids; }
357 void set_jmethod_ids(JNIMethodBlock* new_block) { _jmethod_ids = new_block; }
358
359 void print() { print_on(tty); }
360 void print_on(outputStream* out) const;
361 void print_value() { print_value_on(tty); }
362 void print_value_on(outputStream* out) const;
363 void dump(outputStream * const out) PRODUCT_RETURN;
364 void verify();
365 const char* loader_name();
366
367 jobject add_handle(Handle h);
368 void remove_handle_unsafe(jobject h);
369 void add_class(Klass* k, bool publicize = true);
370 void remove_class(Klass* k);
371 bool contains_klass(Klass* k);
372 void record_dependency(const Klass* to, TRAPS);
373 void init_dependencies(TRAPS);
374 PackageEntryTable* packages() { return _packages; }
375 ModuleEntry* unnamed_module() { return _unnamed_module; }
376 ModuleEntryTable* modules();
377 bool modules_defined() { return (_modules != NULL); }
378
379 // Loaded class dictionary
380 Dictionary* dictionary() const { return _dictionary; }
381
382 void add_to_deallocate_list(Metadata* m);
383
384 static ClassLoaderData* class_loader_data(oop loader);
385 static ClassLoaderData* class_loader_data_or_null(oop loader);
386 static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);
387 static void print_loader(ClassLoaderData *loader_data, outputStream *out);
388
389 // CDS support
390 Metaspace* ro_metaspace();
391 Metaspace* rw_metaspace();
392 void initialize_shared_metaspaces();
393
394 TRACE_DEFINE_TRACE_ID_METHODS;
395 };
396
397 // An iterator that distributes Klasses to parallel worker threads.
398 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj {
399 Klass* volatile _next_klass;
400 public:
|