src/share/vm/classfile/classLoaderData.hpp

Thu, 24 May 2018 18:41:44 +0800

author
aoqi
date
Thu, 24 May 2018 18:41:44 +0800
changeset 8856
ac27a9c85bea
parent 8762
654eaca01d61
parent 7994
04ff2f6cd0eb
child 9931
fd44df5e3bc3
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
shshahma@8762 2 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
aoqi@0 26 #define SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "memory/memRegion.hpp"
aoqi@0 30 #include "memory/metaspace.hpp"
aoqi@0 31 #include "memory/metaspaceCounters.hpp"
aoqi@0 32 #include "runtime/mutex.hpp"
aoqi@0 33 #include "utilities/growableArray.hpp"
mgronlun@7367 34 #include "utilities/macros.hpp"
aoqi@0 35 #if INCLUDE_TRACE
mgronlun@7367 36 #include "utilities/ticks.hpp"
aoqi@0 37 #endif
aoqi@0 38
aoqi@0 39 //
aoqi@0 40 // A class loader represents a linkset. Conceptually, a linkset identifies
aoqi@0 41 // the complete transitive closure of resolved links that a dynamic linker can
aoqi@0 42 // produce.
aoqi@0 43 //
aoqi@0 44 // A ClassLoaderData also encapsulates the allocation space, called a metaspace,
aoqi@0 45 // used by the dynamic linker to allocate the runtime representation of all
aoqi@0 46 // the types it defines.
aoqi@0 47 //
aoqi@0 48 // ClassLoaderData are stored in the runtime representation of classes and the
aoqi@0 49 // system dictionary, are roots of garbage collection, and provides iterators
aoqi@0 50 // for root tracing and other GC operations.
aoqi@0 51
aoqi@0 52 class ClassLoaderData;
aoqi@0 53 class JNIMethodBlock;
aoqi@0 54 class Metadebug;
aoqi@0 55
aoqi@0 56 // GC root for walking class loader data created
aoqi@0 57
aoqi@0 58 class ClassLoaderDataGraph : public AllStatic {
aoqi@0 59 friend class ClassLoaderData;
aoqi@0 60 friend class ClassLoaderDataGraphMetaspaceIterator;
stefank@6992 61 friend class ClassLoaderDataGraphKlassIteratorAtomic;
aoqi@0 62 friend class VMStructs;
aoqi@0 63 private:
aoqi@0 64 // All CLDs (except the null CLD) can be reached by walking _head->_next->...
aoqi@0 65 static ClassLoaderData* _head;
aoqi@0 66 static ClassLoaderData* _unloading;
aoqi@0 67 // CMS support.
aoqi@0 68 static ClassLoaderData* _saved_head;
aoqi@0 69 static ClassLoaderData* _saved_unloading;
aoqi@0 70 static bool _should_purge;
aoqi@0 71
aoqi@0 72 static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS);
aoqi@0 73 static void post_class_unload_events(void);
stefank@7333 74 static void clean_metaspaces();
aoqi@0 75 public:
aoqi@0 76 static ClassLoaderData* find_or_create(Handle class_loader, TRAPS);
aoqi@0 77 static void purge();
aoqi@0 78 static void clear_claimed_marks();
stefank@6992 79 // oops do
aoqi@0 80 static void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
stefank@6992 81 static void keep_alive_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
aoqi@0 82 static void always_strong_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
stefank@6992 83 // cld do
stefank@6992 84 static void cld_do(CLDClosure* cl);
stefank@6992 85 static void roots_cld_do(CLDClosure* strong, CLDClosure* weak);
stefank@6992 86 static void keep_alive_cld_do(CLDClosure* cl);
stefank@6992 87 static void always_strong_cld_do(CLDClosure* cl);
stefank@6992 88 // klass do
aoqi@0 89 static void classes_do(KlassClosure* klass_closure);
aoqi@0 90 static void classes_do(void f(Klass* const));
aoqi@0 91 static void loaded_classes_do(KlassClosure* klass_closure);
aoqi@0 92 static void classes_unloading_do(void f(Klass* const));
stefank@7333 93 static bool do_unloading(BoolObjectClosure* is_alive, bool clean_alive);
aoqi@0 94
aoqi@0 95 // CMS support.
aoqi@0 96 static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
aoqi@0 97 static GrowableArray<ClassLoaderData*>* new_clds();
aoqi@0 98
aoqi@0 99 static void set_should_purge(bool b) { _should_purge = b; }
aoqi@0 100 static void purge_if_needed() {
aoqi@0 101 // Only purge the CLDG for CMS if concurrent sweep is complete.
aoqi@0 102 if (_should_purge) {
aoqi@0 103 purge();
aoqi@0 104 // reset for next time.
aoqi@0 105 set_should_purge(false);
aoqi@0 106 }
aoqi@0 107 }
aoqi@0 108
stefank@7333 109 static void free_deallocate_lists();
stefank@7333 110
aoqi@0 111 static void dump_on(outputStream * const out) PRODUCT_RETURN;
aoqi@0 112 static void dump() { dump_on(tty); }
aoqi@0 113 static void verify();
aoqi@0 114
stefank@6992 115 static bool unload_list_contains(const void* x);
aoqi@0 116 #ifndef PRODUCT
aoqi@0 117 static bool contains_loader_data(ClassLoaderData* loader_data);
aoqi@0 118 #endif
aoqi@0 119
aoqi@0 120 #if INCLUDE_TRACE
aoqi@0 121 private:
aoqi@0 122 static Ticks _class_unload_time;
aoqi@0 123 static void class_unload_event(Klass* const k);
aoqi@0 124 #endif
aoqi@0 125 };
aoqi@0 126
aoqi@0 127 // ClassLoaderData class
aoqi@0 128
aoqi@0 129 class ClassLoaderData : public CHeapObj<mtClass> {
aoqi@0 130 friend class VMStructs;
aoqi@0 131 private:
aoqi@0 132 class Dependencies VALUE_OBJ_CLASS_SPEC {
aoqi@0 133 objArrayOop _list_head;
aoqi@0 134 void locked_add(objArrayHandle last,
aoqi@0 135 objArrayHandle new_dependency,
aoqi@0 136 Thread* THREAD);
aoqi@0 137 public:
aoqi@0 138 Dependencies() : _list_head(NULL) {}
aoqi@0 139 Dependencies(TRAPS) : _list_head(NULL) {
aoqi@0 140 init(CHECK);
aoqi@0 141 }
aoqi@0 142 void add(Handle dependency, TRAPS);
aoqi@0 143 void init(TRAPS);
aoqi@0 144 void oops_do(OopClosure* f);
aoqi@0 145 };
aoqi@0 146
shshahma@8762 147 class ChunkedHandleList VALUE_OBJ_CLASS_SPEC {
shshahma@8762 148 struct Chunk : public CHeapObj<mtClass> {
shshahma@8762 149 static const size_t CAPACITY = 32;
shshahma@8762 150
shshahma@8762 151 oop _data[CAPACITY];
shshahma@8762 152 volatile juint _size;
shshahma@8762 153 Chunk* _next;
shshahma@8762 154
shshahma@8762 155 Chunk(Chunk* c) : _next(c), _size(0) { }
shshahma@8762 156 };
shshahma@8762 157
shshahma@8762 158 Chunk* _head;
shshahma@8762 159
shshahma@8762 160 void oops_do_chunk(OopClosure* f, Chunk* c, const juint size);
shshahma@8762 161
shshahma@8762 162 public:
shshahma@8762 163 ChunkedHandleList() : _head(NULL) {}
shshahma@8762 164 ~ChunkedHandleList();
shshahma@8762 165
shshahma@8762 166 // Only one thread at a time can add, guarded by ClassLoaderData::metaspace_lock().
shshahma@8762 167 // However, multiple threads can execute oops_do concurrently with add.
shshahma@8762 168 oop* add(oop o);
shshahma@8762 169 void oops_do(OopClosure* f);
shshahma@8762 170 };
shshahma@8762 171
aoqi@0 172 friend class ClassLoaderDataGraph;
stefank@6992 173 friend class ClassLoaderDataGraphKlassIteratorAtomic;
aoqi@0 174 friend class ClassLoaderDataGraphMetaspaceIterator;
aoqi@0 175 friend class MetaDataFactory;
aoqi@0 176 friend class Method;
aoqi@0 177
aoqi@0 178 static ClassLoaderData * _the_null_class_loader_data;
aoqi@0 179
aoqi@0 180 oop _class_loader; // oop used to uniquely identify a class loader
aoqi@0 181 // class loader or a canonical class path
aoqi@0 182 Dependencies _dependencies; // holds dependencies from this class loader
aoqi@0 183 // data to others.
aoqi@0 184
aoqi@0 185 Metaspace * _metaspace; // Meta-space where meta-data defined by the
aoqi@0 186 // classes in the class loader are allocated.
aoqi@0 187 Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup.
aoqi@0 188 bool _unloading; // true if this class loader goes away
stefank@6974 189 bool _keep_alive; // if this CLD is kept alive without a keep_alive_object().
aoqi@0 190 bool _is_anonymous; // if this CLD is for an anonymous class
aoqi@0 191 volatile int _claimed; // true if claimed, for example during GC traces.
aoqi@0 192 // To avoid applying oop closure more than once.
aoqi@0 193 // Has to be an int because we cas it.
aoqi@0 194 Klass* _klasses; // The classes defined by the class loader.
aoqi@0 195
shshahma@8762 196 ChunkedHandleList _handles; // Handles to constant pool arrays, etc, which
shshahma@8762 197 // have the same life cycle of the corresponding ClassLoader.
aoqi@0 198
aoqi@0 199 // These method IDs are created for the class loader and set to NULL when the
aoqi@0 200 // class loader is unloaded. They are rarely freed, only for redefine classes
aoqi@0 201 // and if they lose a data race in InstanceKlass.
aoqi@0 202 JNIMethodBlock* _jmethod_ids;
aoqi@0 203
aoqi@0 204 // Metadata to be deallocated when it's safe at class unloading, when
aoqi@0 205 // this class loader isn't unloaded itself.
aoqi@0 206 GrowableArray<Metadata*>* _deallocate_list;
aoqi@0 207
aoqi@0 208 // Support for walking class loader data objects
aoqi@0 209 ClassLoaderData* _next; /// Next loader_datas created
aoqi@0 210
aoqi@0 211 // ReadOnly and ReadWrite metaspaces (static because only on the null
aoqi@0 212 // class loader for now).
aoqi@0 213 static Metaspace* _ro_metaspace;
aoqi@0 214 static Metaspace* _rw_metaspace;
aoqi@0 215
aoqi@0 216 void set_next(ClassLoaderData* next) { _next = next; }
aoqi@0 217 ClassLoaderData* next() const { return _next; }
aoqi@0 218
aoqi@0 219 ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies);
aoqi@0 220 ~ClassLoaderData();
aoqi@0 221
aoqi@0 222 void set_metaspace(Metaspace* m) { _metaspace = m; }
aoqi@0 223
aoqi@0 224 Mutex* metaspace_lock() const { return _metaspace_lock; }
aoqi@0 225
aoqi@0 226 // GC interface.
aoqi@0 227 void clear_claimed() { _claimed = 0; }
aoqi@0 228 bool claimed() const { return _claimed == 1; }
aoqi@0 229 bool claim();
aoqi@0 230
aoqi@0 231 void unload();
aoqi@0 232 bool keep_alive() const { return _keep_alive; }
aoqi@0 233 void classes_do(void f(Klass*));
aoqi@0 234 void loaded_classes_do(KlassClosure* klass_closure);
aoqi@0 235 void classes_do(void f(InstanceKlass*));
aoqi@0 236
aoqi@0 237 // Deallocate free list during class unloading.
aoqi@0 238 void free_deallocate_list();
aoqi@0 239
aoqi@0 240 // Allocate out of this class loader data
aoqi@0 241 MetaWord* allocate(size_t size);
aoqi@0 242
aoqi@0 243 public:
stefank@6992 244
stefank@6992 245 bool is_alive(BoolObjectClosure* is_alive_closure) const;
stefank@6992 246
aoqi@0 247 // Accessors
aoqi@0 248 Metaspace* metaspace_or_null() const { return _metaspace; }
aoqi@0 249
aoqi@0 250 static ClassLoaderData* the_null_class_loader_data() {
aoqi@0 251 return _the_null_class_loader_data;
aoqi@0 252 }
aoqi@0 253
aoqi@0 254 bool is_anonymous() const { return _is_anonymous; }
aoqi@0 255
aoqi@0 256 static void init_null_class_loader_data() {
aoqi@0 257 assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
aoqi@0 258 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
aoqi@0 259
aoqi@0 260 // We explicitly initialize the Dependencies object at a later phase in the initialization
aoqi@0 261 _the_null_class_loader_data = new ClassLoaderData((oop)NULL, false, Dependencies());
aoqi@0 262 ClassLoaderDataGraph::_head = _the_null_class_loader_data;
aoqi@0 263 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
aoqi@0 264 if (DumpSharedSpaces) {
aoqi@0 265 _the_null_class_loader_data->initialize_shared_metaspaces();
aoqi@0 266 }
aoqi@0 267 }
aoqi@0 268
aoqi@0 269 bool is_the_null_class_loader_data() const {
aoqi@0 270 return this == _the_null_class_loader_data;
aoqi@0 271 }
aoqi@0 272 bool is_ext_class_loader_data() const;
aoqi@0 273
aoqi@0 274 // The Metaspace is created lazily so may be NULL. This
aoqi@0 275 // method will allocate a Metaspace if needed.
aoqi@0 276 Metaspace* metaspace_non_null();
aoqi@0 277
aoqi@0 278 oop class_loader() const { return _class_loader; }
aoqi@0 279
stefank@6974 280 // The object the GC is using to keep this ClassLoaderData alive.
stefank@6974 281 oop keep_alive_object() const;
stefank@6974 282
aoqi@0 283 // Returns true if this class loader data is for a loader going away.
aoqi@0 284 bool is_unloading() const {
aoqi@0 285 assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");
aoqi@0 286 return _unloading;
aoqi@0 287 }
stefank@6974 288
stefank@6974 289 // Used to make sure that this CLD is not unloaded.
aoqi@0 290 void set_keep_alive(bool value) { _keep_alive = value; }
aoqi@0 291
aoqi@0 292 unsigned int identity_hash() {
aoqi@0 293 return _class_loader == NULL ? 0 : _class_loader->identity_hash();
aoqi@0 294 }
aoqi@0 295
aoqi@0 296 // Used when tracing from klasses.
aoqi@0 297 void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
aoqi@0 298
aoqi@0 299 void classes_do(KlassClosure* klass_closure);
aoqi@0 300
aoqi@0 301 JNIMethodBlock* jmethod_ids() const { return _jmethod_ids; }
aoqi@0 302 void set_jmethod_ids(JNIMethodBlock* new_block) { _jmethod_ids = new_block; }
aoqi@0 303
aoqi@0 304 void print_value() { print_value_on(tty); }
aoqi@0 305 void print_value_on(outputStream* out) const;
aoqi@0 306 void dump(outputStream * const out) PRODUCT_RETURN;
aoqi@0 307 void verify();
aoqi@0 308 const char* loader_name();
aoqi@0 309
aoqi@0 310 jobject add_handle(Handle h);
aoqi@0 311 void add_class(Klass* k);
aoqi@0 312 void remove_class(Klass* k);
aoqi@0 313 bool contains_klass(Klass* k);
aoqi@0 314 void record_dependency(Klass* to, TRAPS);
aoqi@0 315 void init_dependencies(TRAPS);
aoqi@0 316
aoqi@0 317 void add_to_deallocate_list(Metadata* m);
aoqi@0 318
aoqi@0 319 static ClassLoaderData* class_loader_data(oop loader);
aoqi@0 320 static ClassLoaderData* class_loader_data_or_null(oop loader);
aoqi@0 321 static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);
aoqi@0 322 static void print_loader(ClassLoaderData *loader_data, outputStream *out);
aoqi@0 323
aoqi@0 324 // CDS support
aoqi@0 325 Metaspace* ro_metaspace();
aoqi@0 326 Metaspace* rw_metaspace();
aoqi@0 327 void initialize_shared_metaspaces();
aoqi@0 328 };
aoqi@0 329
stefank@6992 330 // An iterator that distributes Klasses to parallel worker threads.
stefank@6992 331 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj {
asiebenborn@7765 332 Klass* volatile _next_klass;
stefank@6992 333 public:
stefank@6992 334 ClassLoaderDataGraphKlassIteratorAtomic();
stefank@6992 335 Klass* next_klass();
stefank@6992 336 private:
stefank@6992 337 static Klass* next_klass_in_cldg(Klass* klass);
stefank@6992 338 };
stefank@6992 339
aoqi@0 340 class ClassLoaderDataGraphMetaspaceIterator : public StackObj {
aoqi@0 341 ClassLoaderData* _data;
aoqi@0 342 public:
aoqi@0 343 ClassLoaderDataGraphMetaspaceIterator();
aoqi@0 344 ~ClassLoaderDataGraphMetaspaceIterator();
aoqi@0 345 bool repeat() { return _data != NULL; }
aoqi@0 346 Metaspace* get_next() {
aoqi@0 347 assert(_data != NULL, "Should not be NULL in call to the iterator");
aoqi@0 348 Metaspace* result = _data->metaspace_or_null();
aoqi@0 349 _data = _data->next();
aoqi@0 350 // This result might be NULL for class loaders without metaspace
aoqi@0 351 // yet. It would be nice to return only non-null results but
aoqi@0 352 // there is no guarantee that there will be a non-null result
aoqi@0 353 // down the list so the caller is going to have to check.
aoqi@0 354 return result;
aoqi@0 355 }
aoqi@0 356 };
aoqi@0 357 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP

mercurial