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 class DictionaryEntry;
61 class Dictionary;
62
63 // GC root for walking class loader data created
64
65 class ClassLoaderDataGraph : public AllStatic {
66 friend class ClassLoaderData;
67 friend class ClassLoaderDataGraphMetaspaceIterator;
68 friend class ClassLoaderDataGraphKlassIteratorAtomic;
69 friend class ClassLoaderDataGraphKlassIteratorStatic;
70 friend class VMStructs;
71 private:
224 Dependencies _dependencies; // holds dependencies from this class loader
225 // data to others.
226
227 Metaspace * volatile _metaspace; // Meta-space where meta-data defined by the
228 // classes in the class loader are allocated.
229 Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup.
230 bool _unloading; // true if this class loader goes away
231 bool _is_anonymous; // if this CLD is for an anonymous class
232 s2 _keep_alive; // if this CLD is kept alive without a keep_alive_object().
233 // Used for anonymous classes and the boot class
234 // loader. _keep_alive does not need to be volatile or
235 // atomic since there is one unique CLD per anonymous class.
236 volatile int _claimed; // true if claimed, for example during GC traces.
237 // To avoid applying oop closure more than once.
238 // Has to be an int because we cas it.
239 ChunkedHandleList _handles; // Handles to constant pool arrays, Modules, etc, which
240 // have the same life cycle of the corresponding ClassLoader.
241
242 Klass* volatile _klasses; // The classes defined by the class loader.
243 PackageEntryTable* volatile _packages; // The packages defined by the class loader.
244 ModuleEntry* _unnamed_module; // This class loader's unnamed module.
245 ModuleEntryTable* volatile _modules; // The modules defined by the class loader.
246 Dictionary* volatile _dictionary; // The loaded InstanceKlasses, including initiated by this class loader
247
248 // These method IDs are created for the class loader and set to NULL when the
249 // class loader is unloaded. They are rarely freed, only for redefine classes
250 // and if they lose a data race in InstanceKlass.
251 JNIMethodBlock* _jmethod_ids;
252
253 // Metadata to be deallocated when it's safe at class unloading, when
254 // this class loader isn't unloaded itself.
255 GrowableArray<Metadata*>* _deallocate_list;
256
257 // Support for walking class loader data objects
258 ClassLoaderData* _next; /// Next loader_datas created
259
260 // ReadOnly and ReadWrite metaspaces (static because only on the null
261 // class loader for now).
262 static Metaspace* _ro_metaspace;
263 static Metaspace* _rw_metaspace;
264
265 TRACE_DEFINE_TRACE_ID_FIELD;
266
273 // GC interface.
274 void clear_claimed() { _claimed = 0; }
275 bool claimed() const { return _claimed == 1; }
276 bool claim();
277
278 void unload();
279 bool keep_alive() const { return _keep_alive > 0; }
280 void classes_do(void f(Klass*));
281 void loaded_classes_do(KlassClosure* klass_closure);
282 void classes_do(void f(InstanceKlass*));
283 void methods_do(void f(Method*));
284 void modules_do(void f(ModuleEntry*));
285 void packages_do(void f(PackageEntry*));
286
287 // Deallocate free list during class unloading.
288 void free_deallocate_list();
289
290 // Allocate out of this class loader data
291 MetaWord* allocate(size_t size);
292
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
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();
381 Dictionary* dictionary_or_null();
382
383 void add_to_deallocate_list(Metadata* m);
384
385 static ClassLoaderData* class_loader_data(oop loader);
386 static ClassLoaderData* class_loader_data_or_null(oop loader);
387 static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);
388 static void print_loader(ClassLoaderData *loader_data, outputStream *out);
389
390 // CDS support
391 Metaspace* ro_metaspace();
392 Metaspace* rw_metaspace();
393 void initialize_shared_metaspaces();
394
395 TRACE_DEFINE_TRACE_ID_METHODS;
396 };
397
398 // An iterator that distributes Klasses to parallel worker threads.
399 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj {
400 Klass* volatile _next_klass;
401 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:
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
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:
|