Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/classfile/classLoaderData.hpp
32285 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/handles.hpp"32#include "runtime/mutex.hpp"33#include "utilities/growableArray.hpp"34#include "utilities/macros.hpp"35#if INCLUDE_JFR36#include "jfr/support/jfrTraceIdExtension.hpp"37#endif3839//40// A class loader represents a linkset. Conceptually, a linkset identifies41// the complete transitive closure of resolved links that a dynamic linker can42// 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 all46// the types it defines.47//48// ClassLoaderData are stored in the runtime representation of classes and the49// system dictionary, are roots of garbage collection, and provides iterators50// for root tracing and other GC operations.5152class ClassLoaderData;53class JNIMethodBlock;54class Metadebug;5556// GC root for walking class loader data created5758class ClassLoaderDataGraph : public AllStatic {59friend class ClassLoaderData;60friend class ClassLoaderDataGraphMetaspaceIterator;61friend class ClassLoaderDataGraphKlassIteratorAtomic;62friend class VMStructs;63private:64// All CLDs (except the null CLD) can be reached by walking _head->_next->...65static ClassLoaderData* _head;66static ClassLoaderData* _unloading;67// CMS support.68static ClassLoaderData* _saved_head;69static ClassLoaderData* _saved_unloading;70static bool _should_purge;7172static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS);73static void clean_metaspaces();74public:75static ClassLoaderData* find_or_create(Handle class_loader, TRAPS);76static void purge();77static void clear_claimed_marks();78// oops do79static void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);80static void keep_alive_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);81static void always_strong_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);82// cld do83static void cld_do(CLDClosure* cl);84static void cld_unloading_do(CLDClosure* cl);85static void roots_cld_do(CLDClosure* strong, CLDClosure* weak);86static void keep_alive_cld_do(CLDClosure* cl);87static void always_strong_cld_do(CLDClosure* cl);88// klass do89static void classes_do(KlassClosure* klass_closure);90static void classes_do(void f(Klass* const));91static void loaded_classes_do(KlassClosure* klass_closure);92static void classes_unloading_do(void f(Klass* const));93static bool do_unloading(BoolObjectClosure* is_alive, bool clean_alive);9495// CMS support.96static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }97static GrowableArray<ClassLoaderData*>* new_clds();9899static void set_should_purge(bool b) { _should_purge = b; }100static void purge_if_needed() {101// Only purge the CLDG for CMS if concurrent sweep is complete.102if (_should_purge) {103purge();104// reset for next time.105set_should_purge(false);106}107}108109static void free_deallocate_lists();110111static void dump_on(outputStream * const out) PRODUCT_RETURN;112static void dump() { dump_on(tty); }113static void verify();114115static bool unload_list_contains(const void* x);116#ifndef PRODUCT117static bool contains_loader_data(ClassLoaderData* loader_data);118#endif119};120121// ClassLoaderData class122123class ClassLoaderData : public CHeapObj<mtClass> {124friend class VMStructs;125private:126class Dependencies VALUE_OBJ_CLASS_SPEC {127objArrayOop _list_head;128void locked_add(objArrayHandle last,129objArrayHandle new_dependency,130Thread* THREAD);131public:132Dependencies() : _list_head(NULL) {}133Dependencies(TRAPS) : _list_head(NULL) {134init(CHECK);135}136void add(Handle dependency, TRAPS);137void init(TRAPS);138void oops_do(OopClosure* f);139};140141class ChunkedHandleList VALUE_OBJ_CLASS_SPEC {142struct Chunk : public CHeapObj<mtClass> {143static const size_t CAPACITY = 32;144145oop _data[CAPACITY];146volatile juint _size;147Chunk* _next;148149Chunk(Chunk* c) : _next(c), _size(0) { }150};151152Chunk* _head;153154void oops_do_chunk(OopClosure* f, Chunk* c, const juint size);155156public:157ChunkedHandleList() : _head(NULL) {}158~ChunkedHandleList();159160// Only one thread at a time can add, guarded by ClassLoaderData::metaspace_lock().161// However, multiple threads can execute oops_do concurrently with add.162oop* add(oop o);163void oops_do(OopClosure* f);164};165166friend class ClassLoaderDataGraph;167friend class ClassLoaderDataGraphKlassIteratorAtomic;168friend class ClassLoaderDataGraphMetaspaceIterator;169friend class MetaDataFactory;170friend class Method;171172static ClassLoaderData * _the_null_class_loader_data;173174oop _class_loader; // oop used to uniquely identify a class loader175// class loader or a canonical class path176Dependencies _dependencies; // holds dependencies from this class loader177// data to others.178179Metaspace * _metaspace; // Meta-space where meta-data defined by the180// classes in the class loader are allocated.181Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup.182bool _unloading; // true if this class loader goes away183bool _keep_alive; // if this CLD is kept alive without a keep_alive_object().184bool _is_anonymous; // if this CLD is for an anonymous class185volatile int _claimed; // true if claimed, for example during GC traces.186// To avoid applying oop closure more than once.187// Has to be an int because we cas it.188Klass* _klasses; // The classes defined by the class loader.189190ChunkedHandleList _handles; // Handles to constant pool arrays, etc, which191// have the same life cycle of the corresponding ClassLoader.192193// These method IDs are created for the class loader and set to NULL when the194// class loader is unloaded. They are rarely freed, only for redefine classes195// and if they lose a data race in InstanceKlass.196JNIMethodBlock* _jmethod_ids;197198// Metadata to be deallocated when it's safe at class unloading, when199// this class loader isn't unloaded itself.200GrowableArray<Metadata*>* _deallocate_list;201202// Support for walking class loader data objects203ClassLoaderData* _next; /// Next loader_datas created204205// ReadOnly and ReadWrite metaspaces (static because only on the null206// class loader for now).207static Metaspace* _ro_metaspace;208static Metaspace* _rw_metaspace;209210JFR_ONLY(DEFINE_TRACE_ID_FIELD;)211212void set_next(ClassLoaderData* next) { _next = next; }213ClassLoaderData* next() const { return _next; }214215ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies);216~ClassLoaderData();217218void set_metaspace(Metaspace* m) { _metaspace = m; }219220Mutex* metaspace_lock() const { return _metaspace_lock; }221222void unload();223bool keep_alive() const { return _keep_alive; }224void classes_do(void f(Klass*));225void loaded_classes_do(KlassClosure* klass_closure);226void classes_do(void f(InstanceKlass*));227228// Deallocate free list during class unloading.229void free_deallocate_list();230231// Allocate out of this class loader data232MetaWord* allocate(size_t size);233234public:235236// GC interface.237void clear_claimed() { _claimed = 0; }238bool claimed() const { return _claimed == 1; }239bool claim();240241bool is_alive(BoolObjectClosure* is_alive_closure) const;242243// Accessors244Metaspace* metaspace_or_null() const { return _metaspace; }245246static ClassLoaderData* the_null_class_loader_data() {247return _the_null_class_loader_data;248}249250bool is_anonymous() const { return _is_anonymous; }251252static void init_null_class_loader_data() {253assert(_the_null_class_loader_data == NULL, "cannot initialize twice");254assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");255256// We explicitly initialize the Dependencies object at a later phase in the initialization257_the_null_class_loader_data = new ClassLoaderData((oop)NULL, false, Dependencies());258ClassLoaderDataGraph::_head = _the_null_class_loader_data;259assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");260if (DumpSharedSpaces) {261_the_null_class_loader_data->initialize_shared_metaspaces();262}263}264265bool is_the_null_class_loader_data() const {266return this == _the_null_class_loader_data;267}268bool is_ext_class_loader_data() const;269270// The Metaspace is created lazily so may be NULL. This271// method will allocate a Metaspace if needed.272Metaspace* metaspace_non_null();273274oop class_loader() const { return _class_loader; }275276// The object the GC is using to keep this ClassLoaderData alive.277oop keep_alive_object() const;278279// Returns true if this class loader data is for a loader going away.280bool is_unloading() const {281assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");282return _unloading;283}284285// Used to make sure that this CLD is not unloaded.286void set_keep_alive(bool value) { _keep_alive = value; }287288unsigned int identity_hash() {289return _class_loader == NULL ? 0 : _class_loader->identity_hash();290}291292// Used when tracing from klasses.293void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);294295void classes_do(KlassClosure* klass_closure);296297JNIMethodBlock* jmethod_ids() const { return _jmethod_ids; }298void set_jmethod_ids(JNIMethodBlock* new_block) { _jmethod_ids = new_block; }299300void print_value() { print_value_on(tty); }301void print_value_on(outputStream* out) const;302void dump(outputStream * const out) PRODUCT_RETURN;303void verify();304const char* loader_name();305306jobject add_handle(Handle h);307void add_class(Klass* k);308void remove_class(Klass* k);309bool contains_klass(Klass* k);310void record_dependency(Klass* to, TRAPS);311void init_dependencies(TRAPS);312313void add_to_deallocate_list(Metadata* m);314315static ClassLoaderData* class_loader_data(oop loader);316static ClassLoaderData* class_loader_data_or_null(oop loader);317static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);318static void print_loader(ClassLoaderData *loader_data, outputStream *out);319320// CDS support321Metaspace* ro_metaspace();322Metaspace* rw_metaspace();323void initialize_shared_metaspaces();324325JFR_ONLY(DEFINE_TRACE_ID_METHODS;)326};327328// An iterator that distributes Klasses to parallel worker threads.329class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj {330Klass* volatile _next_klass;331public:332ClassLoaderDataGraphKlassIteratorAtomic();333Klass* next_klass();334private:335static Klass* next_klass_in_cldg(Klass* klass);336};337338class ClassLoaderDataGraphMetaspaceIterator : public StackObj {339ClassLoaderData* _data;340public:341ClassLoaderDataGraphMetaspaceIterator();342~ClassLoaderDataGraphMetaspaceIterator();343bool repeat() { return _data != NULL; }344Metaspace* get_next() {345assert(_data != NULL, "Should not be NULL in call to the iterator");346Metaspace* result = _data->metaspace_or_null();347_data = _data->next();348// This result might be NULL for class loaders without metaspace349// yet. It would be nice to return only non-null results but350// there is no guarantee that there will be a non-null result351// down the list so the caller is going to have to check.352return result;353}354};355#endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP356357358