Path: blob/jdk8u272-b10-aarch32-20201026/hotspot/src/share/vm/classfile/classLoaderData.hpp
48773 views
/*1* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP25#define SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP2627#include "memory/allocation.hpp"28#include "memory/memRegion.hpp"29#include "memory/metaspace.hpp"30#include "memory/metaspaceCounters.hpp"31#include "runtime/mutex.hpp"32#include "utilities/growableArray.hpp"33#include "utilities/macros.hpp"34#if INCLUDE_JFR35#include "jfr/support/jfrTraceIdExtension.hpp"36#endif3738//39// A class loader represents a linkset. Conceptually, a linkset identifies40// the complete transitive closure of resolved links that a dynamic linker can41// 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 all45// the types it defines.46//47// ClassLoaderData are stored in the runtime representation of classes and the48// system dictionary, are roots of garbage collection, and provides iterators49// for root tracing and other GC operations.5051class ClassLoaderData;52class JNIMethodBlock;53class Metadebug;5455// GC root for walking class loader data created5657class ClassLoaderDataGraph : public AllStatic {58friend class ClassLoaderData;59friend class ClassLoaderDataGraphMetaspaceIterator;60friend class ClassLoaderDataGraphKlassIteratorAtomic;61friend class VMStructs;62private:63// All CLDs (except the null CLD) can be reached by walking _head->_next->...64static ClassLoaderData* _head;65static ClassLoaderData* _unloading;66// CMS support.67static ClassLoaderData* _saved_head;68static ClassLoaderData* _saved_unloading;69static bool _should_purge;7071static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS);72static void clean_metaspaces();73public:74static ClassLoaderData* find_or_create(Handle class_loader, TRAPS);75static void purge();76static void clear_claimed_marks();77// oops do78static void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);79static void keep_alive_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);80static void always_strong_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);81// cld do82static void cld_do(CLDClosure* cl);83static void cld_unloading_do(CLDClosure* cl);84static void roots_cld_do(CLDClosure* strong, CLDClosure* weak);85static void keep_alive_cld_do(CLDClosure* cl);86static void always_strong_cld_do(CLDClosure* cl);87// klass do88static void classes_do(KlassClosure* klass_closure);89static void classes_do(void f(Klass* const));90static void loaded_classes_do(KlassClosure* klass_closure);91static void classes_unloading_do(void f(Klass* const));92static bool do_unloading(BoolObjectClosure* is_alive, bool clean_alive);9394// CMS support.95static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }96static GrowableArray<ClassLoaderData*>* new_clds();9798static void set_should_purge(bool b) { _should_purge = b; }99static void purge_if_needed() {100// Only purge the CLDG for CMS if concurrent sweep is complete.101if (_should_purge) {102purge();103// reset for next time.104set_should_purge(false);105}106}107108static void free_deallocate_lists();109110static void dump_on(outputStream * const out) PRODUCT_RETURN;111static void dump() { dump_on(tty); }112static void verify();113114static bool unload_list_contains(const void* x);115#ifndef PRODUCT116static bool contains_loader_data(ClassLoaderData* loader_data);117#endif118};119120// ClassLoaderData class121122class ClassLoaderData : public CHeapObj<mtClass> {123friend class VMStructs;124private:125class Dependencies VALUE_OBJ_CLASS_SPEC {126objArrayOop _list_head;127void locked_add(objArrayHandle last,128objArrayHandle new_dependency,129Thread* THREAD);130public:131Dependencies() : _list_head(NULL) {}132Dependencies(TRAPS) : _list_head(NULL) {133init(CHECK);134}135void add(Handle dependency, TRAPS);136void init(TRAPS);137void oops_do(OopClosure* f);138};139140class ChunkedHandleList VALUE_OBJ_CLASS_SPEC {141struct Chunk : public CHeapObj<mtClass> {142static const size_t CAPACITY = 32;143144oop _data[CAPACITY];145volatile juint _size;146Chunk* _next;147148Chunk(Chunk* c) : _next(c), _size(0) { }149};150151Chunk* _head;152153void oops_do_chunk(OopClosure* f, Chunk* c, const juint size);154155public:156ChunkedHandleList() : _head(NULL) {}157~ChunkedHandleList();158159// Only one thread at a time can add, guarded by ClassLoaderData::metaspace_lock().160// However, multiple threads can execute oops_do concurrently with add.161oop* add(oop o);162void oops_do(OopClosure* f);163};164165friend class ClassLoaderDataGraph;166friend class ClassLoaderDataGraphKlassIteratorAtomic;167friend class ClassLoaderDataGraphMetaspaceIterator;168friend class MetaDataFactory;169friend class Method;170171static ClassLoaderData * _the_null_class_loader_data;172173oop _class_loader; // oop used to uniquely identify a class loader174// class loader or a canonical class path175Dependencies _dependencies; // holds dependencies from this class loader176// data to others.177178Metaspace * _metaspace; // Meta-space where meta-data defined by the179// classes in the class loader are allocated.180Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup.181bool _unloading; // true if this class loader goes away182bool _keep_alive; // if this CLD is kept alive without a keep_alive_object().183bool _is_anonymous; // if this CLD is for an anonymous class184volatile int _claimed; // true if claimed, for example during GC traces.185// To avoid applying oop closure more than once.186// Has to be an int because we cas it.187Klass* _klasses; // The classes defined by the class loader.188189ChunkedHandleList _handles; // Handles to constant pool arrays, etc, which190// have the same life cycle of the corresponding ClassLoader.191192// These method IDs are created for the class loader and set to NULL when the193// class loader is unloaded. They are rarely freed, only for redefine classes194// and if they lose a data race in InstanceKlass.195JNIMethodBlock* _jmethod_ids;196197// Metadata to be deallocated when it's safe at class unloading, when198// this class loader isn't unloaded itself.199GrowableArray<Metadata*>* _deallocate_list;200201// Support for walking class loader data objects202ClassLoaderData* _next; /// Next loader_datas created203204// ReadOnly and ReadWrite metaspaces (static because only on the null205// class loader for now).206static Metaspace* _ro_metaspace;207static Metaspace* _rw_metaspace;208209JFR_ONLY(DEFINE_TRACE_ID_FIELD;)210211void set_next(ClassLoaderData* next) { _next = next; }212ClassLoaderData* next() const { return _next; }213214ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies);215~ClassLoaderData();216217void set_metaspace(Metaspace* m) { _metaspace = m; }218219Mutex* metaspace_lock() const { return _metaspace_lock; }220221void unload();222bool keep_alive() const { return _keep_alive; }223void classes_do(void f(Klass*));224void loaded_classes_do(KlassClosure* klass_closure);225void classes_do(void f(InstanceKlass*));226227// Deallocate free list during class unloading.228void free_deallocate_list();229230// Allocate out of this class loader data231MetaWord* allocate(size_t size);232233public:234235// GC interface.236void clear_claimed() { _claimed = 0; }237bool claimed() const { return _claimed == 1; }238bool claim();239240bool is_alive(BoolObjectClosure* is_alive_closure) const;241242// Accessors243Metaspace* metaspace_or_null() const { return _metaspace; }244245static ClassLoaderData* the_null_class_loader_data() {246return _the_null_class_loader_data;247}248249bool is_anonymous() const { return _is_anonymous; }250251static void init_null_class_loader_data() {252assert(_the_null_class_loader_data == NULL, "cannot initialize twice");253assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");254255// We explicitly initialize the Dependencies object at a later phase in the initialization256_the_null_class_loader_data = new ClassLoaderData((oop)NULL, false, Dependencies());257ClassLoaderDataGraph::_head = _the_null_class_loader_data;258assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");259if (DumpSharedSpaces) {260_the_null_class_loader_data->initialize_shared_metaspaces();261}262}263264bool is_the_null_class_loader_data() const {265return this == _the_null_class_loader_data;266}267bool is_ext_class_loader_data() const;268269// The Metaspace is created lazily so may be NULL. This270// method will allocate a Metaspace if needed.271Metaspace* metaspace_non_null();272273oop class_loader() const { return _class_loader; }274275// The object the GC is using to keep this ClassLoaderData alive.276oop keep_alive_object() const;277278// Returns true if this class loader data is for a loader going away.279bool is_unloading() const {280assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");281return _unloading;282}283284// Used to make sure that this CLD is not unloaded.285void set_keep_alive(bool value) { _keep_alive = value; }286287unsigned int identity_hash() {288return _class_loader == NULL ? 0 : _class_loader->identity_hash();289}290291// Used when tracing from klasses.292void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);293294void classes_do(KlassClosure* klass_closure);295296JNIMethodBlock* jmethod_ids() const { return _jmethod_ids; }297void set_jmethod_ids(JNIMethodBlock* new_block) { _jmethod_ids = new_block; }298299void print_value() { print_value_on(tty); }300void print_value_on(outputStream* out) const;301void dump(outputStream * const out) PRODUCT_RETURN;302void verify();303const char* loader_name();304305jobject add_handle(Handle h);306void add_class(Klass* k);307void remove_class(Klass* k);308bool contains_klass(Klass* k);309void record_dependency(Klass* to, TRAPS);310void init_dependencies(TRAPS);311312void add_to_deallocate_list(Metadata* m);313314static ClassLoaderData* class_loader_data(oop loader);315static ClassLoaderData* class_loader_data_or_null(oop loader);316static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);317static void print_loader(ClassLoaderData *loader_data, outputStream *out);318319// CDS support320Metaspace* ro_metaspace();321Metaspace* rw_metaspace();322void initialize_shared_metaspaces();323324JFR_ONLY(DEFINE_TRACE_ID_METHODS;)325};326327// An iterator that distributes Klasses to parallel worker threads.328class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj {329Klass* volatile _next_klass;330public:331ClassLoaderDataGraphKlassIteratorAtomic();332Klass* next_klass();333private:334static Klass* next_klass_in_cldg(Klass* klass);335};336337class ClassLoaderDataGraphMetaspaceIterator : public StackObj {338ClassLoaderData* _data;339public:340ClassLoaderDataGraphMetaspaceIterator();341~ClassLoaderDataGraphMetaspaceIterator();342bool repeat() { return _data != NULL; }343Metaspace* get_next() {344assert(_data != NULL, "Should not be NULL in call to the iterator");345Metaspace* result = _data->metaspace_or_null();346_data = _data->next();347// This result might be NULL for class loaders without metaspace348// yet. It would be nice to return only non-null results but349// there is no guarantee that there will be a non-null result350// down the list so the caller is going to have to check.351return result;352}353};354#endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP355356357