coleenp@4037: /* coleenp@6316: * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. coleenp@4037: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. coleenp@4037: * coleenp@4037: * This code is free software; you can redistribute it and/or modify it coleenp@4037: * under the terms of the GNU General Public License version 2 only, as coleenp@4037: * published by the Free Software Foundation. coleenp@4037: * coleenp@4037: * This code is distributed in the hope that it will be useful, but WITHOUT coleenp@4037: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or katleman@4376: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License coleenp@4037: * version 2 for more details (a copy is included in the LICENSE file that coleenp@4037: * accompanied this code). coleenp@4037: * coleenp@4037: * You should have received a copy of the GNU General Public License version coleenp@4037: * 2 along with this work; if not, write to the Free Software Foundation, coleenp@4037: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. coleenp@4037: * coleenp@4037: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA coleenp@4037: * or visit www.oracle.com if you need additional information or have any coleenp@4037: * questions. coleenp@4037: * coleenp@4037: */ coleenp@4037: coleenp@4037: #ifndef SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP coleenp@4037: #define SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP coleenp@4037: coleenp@4037: #include "memory/allocation.hpp" coleenp@4037: #include "memory/memRegion.hpp" coleenp@4037: #include "memory/metaspace.hpp" coleenp@4037: #include "memory/metaspaceCounters.hpp" coleenp@4037: #include "runtime/mutex.hpp" coleenp@4037: #include "utilities/growableArray.hpp" sla@5237: #if INCLUDE_TRACE mgronlun@6131: # include "utilities/ticks.hpp" sla@5237: #endif sla@5237: coleenp@4037: // coleenp@4037: // A class loader represents a linkset. Conceptually, a linkset identifies coleenp@4037: // the complete transitive closure of resolved links that a dynamic linker can coleenp@4037: // produce. coleenp@4037: // coleenp@4037: // A ClassLoaderData also encapsulates the allocation space, called a metaspace, coleenp@4037: // used by the dynamic linker to allocate the runtime representation of all coleenp@4037: // the types it defines. coleenp@4037: // coleenp@4037: // ClassLoaderData are stored in the runtime representation of classes and the coleenp@4037: // system dictionary, are roots of garbage collection, and provides iterators coleenp@4037: // for root tracing and other GC operations. coleenp@4037: coleenp@4037: class ClassLoaderData; coleenp@4037: class JNIMethodBlock; coleenp@4037: class JNIHandleBlock; coleenp@4037: class Metadebug; sla@5237: coleenp@4037: // GC root for walking class loader data created coleenp@4037: coleenp@4037: class ClassLoaderDataGraph : public AllStatic { coleenp@4037: friend class ClassLoaderData; coleenp@4037: friend class ClassLoaderDataGraphMetaspaceIterator; stefank@6992: friend class ClassLoaderDataGraphKlassIteratorAtomic; coleenp@4037: friend class VMStructs; coleenp@4037: private: coleenp@4037: // All CLDs (except the null CLD) can be reached by walking _head->_next->... coleenp@4037: static ClassLoaderData* _head; coleenp@4037: static ClassLoaderData* _unloading; coleenp@4037: // CMS support. coleenp@4037: static ClassLoaderData* _saved_head; mgronlun@6742: static ClassLoaderData* _saved_unloading; coleenp@6678: static bool _should_purge; coleenp@4037: mgerdin@5013: static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS); sla@5237: static void post_class_unload_events(void); coleenp@4037: public: coleenp@4304: static ClassLoaderData* find_or_create(Handle class_loader, TRAPS); coleenp@4037: static void purge(); coleenp@4037: static void clear_claimed_marks(); stefank@6992: // oops do coleenp@4037: static void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim); stefank@6992: static void keep_alive_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim); coleenp@4037: static void always_strong_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim); stefank@6992: // cld do stefank@6992: static void cld_do(CLDClosure* cl); stefank@6992: static void roots_cld_do(CLDClosure* strong, CLDClosure* weak); stefank@6992: static void keep_alive_cld_do(CLDClosure* cl); stefank@6992: static void always_strong_cld_do(CLDClosure* cl); stefank@6992: // klass do coleenp@4037: static void classes_do(KlassClosure* klass_closure); sla@5237: static void classes_do(void f(Klass* const)); farvidsson@6024: static void loaded_classes_do(KlassClosure* klass_closure); sla@5237: static void classes_unloading_do(void f(Klass* const)); coleenp@4037: static bool do_unloading(BoolObjectClosure* is_alive); coleenp@4037: coleenp@4037: // CMS support. coleenp@4037: static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); } coleenp@4037: static GrowableArray* new_clds(); coleenp@4037: coleenp@6678: static void set_should_purge(bool b) { _should_purge = b; } coleenp@6678: static void purge_if_needed() { coleenp@6678: // Only purge the CLDG for CMS if concurrent sweep is complete. coleenp@6678: if (_should_purge) { coleenp@6678: purge(); coleenp@6678: // reset for next time. coleenp@6678: set_should_purge(false); coleenp@6678: } coleenp@6678: } coleenp@6678: coleenp@4037: static void dump_on(outputStream * const out) PRODUCT_RETURN; coleenp@4037: static void dump() { dump_on(tty); } coleenp@4037: static void verify(); coleenp@4037: stefank@6992: static bool unload_list_contains(const void* x); coleenp@4037: #ifndef PRODUCT coleenp@4037: static bool contains_loader_data(ClassLoaderData* loader_data); coleenp@4037: #endif sla@5237: sla@5237: #if INCLUDE_TRACE sla@5237: private: mgronlun@6131: static Ticks _class_unload_time; sla@5237: static void class_unload_event(Klass* const k); sla@5237: #endif coleenp@4037: }; coleenp@4037: coleenp@4037: // ClassLoaderData class coleenp@4037: coleenp@4037: class ClassLoaderData : public CHeapObj { coleenp@4037: friend class VMStructs; coleenp@4037: private: mgerdin@4903: class Dependencies VALUE_OBJ_CLASS_SPEC { mgerdin@4903: objArrayOop _list_head; mgerdin@4903: void locked_add(objArrayHandle last, mgerdin@4903: objArrayHandle new_dependency, mgerdin@4903: Thread* THREAD); mgerdin@4903: public: mgerdin@4903: Dependencies() : _list_head(NULL) {} mgerdin@5016: Dependencies(TRAPS) : _list_head(NULL) { mgerdin@5016: init(CHECK); mgerdin@5016: } mgerdin@4903: void add(Handle dependency, TRAPS); mgerdin@4903: void init(TRAPS); mgerdin@4903: void oops_do(OopClosure* f); mgerdin@4903: }; mgerdin@4903: coleenp@4037: friend class ClassLoaderDataGraph; stefank@6992: friend class ClassLoaderDataGraphKlassIteratorAtomic; coleenp@4037: friend class ClassLoaderDataGraphMetaspaceIterator; coleenp@4037: friend class MetaDataFactory; coleenp@4037: friend class Method; coleenp@4037: coleenp@4037: static ClassLoaderData * _the_null_class_loader_data; coleenp@4037: mgerdin@4903: oop _class_loader; // oop used to uniquely identify a class loader mgerdin@4903: // class loader or a canonical class path mgerdin@4903: Dependencies _dependencies; // holds dependencies from this class loader mgerdin@4903: // data to others. mgerdin@4903: coleenp@4037: Metaspace * _metaspace; // Meta-space where meta-data defined by the coleenp@4037: // classes in the class loader are allocated. coleenp@4037: Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup. coleenp@4037: bool _unloading; // true if this class loader goes away stefank@6974: bool _keep_alive; // if this CLD is kept alive without a keep_alive_object(). coleenp@4345: bool _is_anonymous; // if this CLD is for an anonymous class coleenp@4037: volatile int _claimed; // true if claimed, for example during GC traces. coleenp@4037: // To avoid applying oop closure more than once. coleenp@4037: // Has to be an int because we cas it. coleenp@4037: Klass* _klasses; // The classes defined by the class loader. coleenp@4037: coleenp@4037: JNIHandleBlock* _handles; // Handles to constant pool arrays coleenp@4037: coleenp@4037: // These method IDs are created for the class loader and set to NULL when the coleenp@4037: // class loader is unloaded. They are rarely freed, only for redefine classes coleenp@4037: // and if they lose a data race in InstanceKlass. coleenp@4037: JNIMethodBlock* _jmethod_ids; coleenp@4037: coleenp@4037: // Metadata to be deallocated when it's safe at class unloading, when coleenp@4037: // this class loader isn't unloaded itself. coleenp@4037: GrowableArray* _deallocate_list; coleenp@4037: coleenp@4037: // Support for walking class loader data objects coleenp@4037: ClassLoaderData* _next; /// Next loader_datas created coleenp@4037: coleenp@4037: // ReadOnly and ReadWrite metaspaces (static because only on the null coleenp@4037: // class loader for now). coleenp@4037: static Metaspace* _ro_metaspace; coleenp@4037: static Metaspace* _rw_metaspace; coleenp@4037: coleenp@4037: void set_next(ClassLoaderData* next) { _next = next; } coleenp@4037: ClassLoaderData* next() const { return _next; } coleenp@4037: mgerdin@5016: ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies); coleenp@4037: ~ClassLoaderData(); coleenp@4037: coleenp@4037: void set_metaspace(Metaspace* m) { _metaspace = m; } coleenp@4037: coleenp@4037: JNIHandleBlock* handles() const; coleenp@4037: void set_handles(JNIHandleBlock* handles); coleenp@4037: coleenp@4037: Mutex* metaspace_lock() const { return _metaspace_lock; } coleenp@4037: coleenp@4037: // GC interface. coleenp@4037: void clear_claimed() { _claimed = 0; } coleenp@4037: bool claimed() const { return _claimed == 1; } coleenp@4037: bool claim(); coleenp@4037: coleenp@4304: void unload(); coleenp@4304: bool keep_alive() const { return _keep_alive; } sla@5237: void classes_do(void f(Klass*)); farvidsson@6024: void loaded_classes_do(KlassClosure* klass_closure); coleenp@4037: void classes_do(void f(InstanceKlass*)); coleenp@4037: coleenp@4037: // Deallocate free list during class unloading. coleenp@4037: void free_deallocate_list(); coleenp@4037: coleenp@4037: // Allocate out of this class loader data coleenp@4037: MetaWord* allocate(size_t size); coleenp@4037: coleenp@4037: public: stefank@6992: stefank@6992: bool is_alive(BoolObjectClosure* is_alive_closure) const; stefank@6992: coleenp@4037: // Accessors coleenp@4037: Metaspace* metaspace_or_null() const { return _metaspace; } coleenp@4037: coleenp@4037: static ClassLoaderData* the_null_class_loader_data() { coleenp@4037: return _the_null_class_loader_data; coleenp@4037: } coleenp@4037: coleenp@4345: bool is_anonymous() const { return _is_anonymous; } coleenp@4304: coleenp@4037: static void init_null_class_loader_data() { coleenp@4037: assert(_the_null_class_loader_data == NULL, "cannot initialize twice"); coleenp@4037: assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice"); mgerdin@5016: mgerdin@5016: // We explicitly initialize the Dependencies object at a later phase in the initialization mgerdin@5016: _the_null_class_loader_data = new ClassLoaderData((oop)NULL, false, Dependencies()); coleenp@4037: ClassLoaderDataGraph::_head = _the_null_class_loader_data; coleenp@4037: assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be"); coleenp@4037: if (DumpSharedSpaces) { coleenp@4037: _the_null_class_loader_data->initialize_shared_metaspaces(); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: bool is_the_null_class_loader_data() const { coleenp@4037: return this == _the_null_class_loader_data; coleenp@4037: } twisti@4866: bool is_ext_class_loader_data() const; coleenp@4037: coleenp@4037: // The Metaspace is created lazily so may be NULL. This coleenp@4037: // method will allocate a Metaspace if needed. coleenp@4037: Metaspace* metaspace_non_null(); coleenp@4037: coleenp@4037: oop class_loader() const { return _class_loader; } coleenp@4037: stefank@6974: // The object the GC is using to keep this ClassLoaderData alive. stefank@6974: oop keep_alive_object() const; stefank@6974: coleenp@4037: // Returns true if this class loader data is for a loader going away. coleenp@4037: bool is_unloading() const { coleenp@4037: assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded"); coleenp@4037: return _unloading; coleenp@4037: } stefank@6974: stefank@6974: // Used to make sure that this CLD is not unloaded. coleenp@4304: void set_keep_alive(bool value) { _keep_alive = value; } coleenp@4037: coleenp@4037: unsigned int identity_hash() { coleenp@4037: return _class_loader == NULL ? 0 : _class_loader->identity_hash(); coleenp@4037: } coleenp@4037: coleenp@4037: // Used when tracing from klasses. coleenp@4037: void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim); coleenp@4037: coleenp@4037: void classes_do(KlassClosure* klass_closure); coleenp@4037: coleenp@4037: JNIMethodBlock* jmethod_ids() const { return _jmethod_ids; } coleenp@4037: void set_jmethod_ids(JNIMethodBlock* new_block) { _jmethod_ids = new_block; } coleenp@4037: coleenp@4037: void print_value() { print_value_on(tty); } acorn@4497: void print_value_on(outputStream* out) const; coleenp@4037: void dump(outputStream * const out) PRODUCT_RETURN; coleenp@4037: void verify(); coleenp@4304: const char* loader_name(); coleenp@4037: coleenp@4037: jobject add_handle(Handle h); coleenp@4037: void add_class(Klass* k); coleenp@4037: void remove_class(Klass* k); coleenp@6316: bool contains_klass(Klass* k); coleenp@4037: void record_dependency(Klass* to, TRAPS); coleenp@4304: void init_dependencies(TRAPS); coleenp@4037: coleenp@4037: void add_to_deallocate_list(Metadata* m); coleenp@4037: coleenp@4037: static ClassLoaderData* class_loader_data(oop loader); stefank@4667: static ClassLoaderData* class_loader_data_or_null(oop loader); coleenp@4304: static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS); coleenp@4037: static void print_loader(ClassLoaderData *loader_data, outputStream *out); coleenp@4037: coleenp@4037: // CDS support coleenp@4037: Metaspace* ro_metaspace(); coleenp@4037: Metaspace* rw_metaspace(); coleenp@4037: void initialize_shared_metaspaces(); coleenp@4037: }; coleenp@4037: stefank@6992: // An iterator that distributes Klasses to parallel worker threads. stefank@6992: class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj { stefank@6992: volatile Klass* _next_klass; stefank@6992: public: stefank@6992: ClassLoaderDataGraphKlassIteratorAtomic(); stefank@6992: Klass* next_klass(); stefank@6992: private: stefank@6992: static Klass* next_klass_in_cldg(Klass* klass); stefank@6992: }; stefank@6992: coleenp@4037: class ClassLoaderDataGraphMetaspaceIterator : public StackObj { coleenp@4037: ClassLoaderData* _data; coleenp@4037: public: coleenp@4037: ClassLoaderDataGraphMetaspaceIterator(); coleenp@4037: ~ClassLoaderDataGraphMetaspaceIterator(); coleenp@4037: bool repeat() { return _data != NULL; } coleenp@4037: Metaspace* get_next() { coleenp@4037: assert(_data != NULL, "Should not be NULL in call to the iterator"); coleenp@4037: Metaspace* result = _data->metaspace_or_null(); coleenp@4037: _data = _data->next(); coleenp@4037: // This result might be NULL for class loaders without metaspace coleenp@4037: // yet. It would be nice to return only non-null results but coleenp@4037: // there is no guarantee that there will be a non-null result coleenp@4037: // down the list so the caller is going to have to check. coleenp@4037: return result; coleenp@4037: } coleenp@4037: }; coleenp@4037: #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP