src/share/vm/classfile/classLoaderData.hpp

Tue, 17 Oct 2017 12:58:25 +0800

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 7765
bd8725e80355
parent 7535
7ae4e26cb1e0
child 8856
ac27a9c85bea
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2012, 2014, 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 JNIHandleBlock;
aoqi@0 55 class Metadebug;
aoqi@0 56
aoqi@0 57 // GC root for walking class loader data created
aoqi@0 58
aoqi@0 59 class ClassLoaderDataGraph : public AllStatic {
aoqi@0 60 friend class ClassLoaderData;
aoqi@0 61 friend class ClassLoaderDataGraphMetaspaceIterator;
stefank@6992 62 friend class ClassLoaderDataGraphKlassIteratorAtomic;
aoqi@0 63 friend class VMStructs;
aoqi@0 64 private:
aoqi@0 65 // All CLDs (except the null CLD) can be reached by walking _head->_next->...
aoqi@0 66 static ClassLoaderData* _head;
aoqi@0 67 static ClassLoaderData* _unloading;
aoqi@0 68 // CMS support.
aoqi@0 69 static ClassLoaderData* _saved_head;
aoqi@0 70 static ClassLoaderData* _saved_unloading;
aoqi@0 71 static bool _should_purge;
aoqi@0 72
aoqi@0 73 static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS);
aoqi@0 74 static void post_class_unload_events(void);
stefank@7333 75 static void clean_metaspaces();
aoqi@0 76 public:
aoqi@0 77 static ClassLoaderData* find_or_create(Handle class_loader, TRAPS);
aoqi@0 78 static void purge();
aoqi@0 79 static void clear_claimed_marks();
stefank@6992 80 // oops do
aoqi@0 81 static void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
stefank@6992 82 static void keep_alive_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
aoqi@0 83 static void always_strong_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
stefank@6992 84 // cld do
stefank@6992 85 static void cld_do(CLDClosure* cl);
stefank@6992 86 static void roots_cld_do(CLDClosure* strong, CLDClosure* weak);
stefank@6992 87 static void keep_alive_cld_do(CLDClosure* cl);
stefank@6992 88 static void always_strong_cld_do(CLDClosure* cl);
stefank@6992 89 // klass do
aoqi@0 90 static void classes_do(KlassClosure* klass_closure);
aoqi@0 91 static void classes_do(void f(Klass* const));
aoqi@0 92 static void loaded_classes_do(KlassClosure* klass_closure);
aoqi@0 93 static void classes_unloading_do(void f(Klass* const));
stefank@7333 94 static bool do_unloading(BoolObjectClosure* is_alive, bool clean_alive);
aoqi@0 95
aoqi@0 96 // CMS support.
aoqi@0 97 static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
aoqi@0 98 static GrowableArray<ClassLoaderData*>* new_clds();
aoqi@0 99
aoqi@0 100 static void set_should_purge(bool b) { _should_purge = b; }
aoqi@0 101 static void purge_if_needed() {
aoqi@0 102 // Only purge the CLDG for CMS if concurrent sweep is complete.
aoqi@0 103 if (_should_purge) {
aoqi@0 104 purge();
aoqi@0 105 // reset for next time.
aoqi@0 106 set_should_purge(false);
aoqi@0 107 }
aoqi@0 108 }
aoqi@0 109
stefank@7333 110 static void free_deallocate_lists();
stefank@7333 111
aoqi@0 112 static void dump_on(outputStream * const out) PRODUCT_RETURN;
aoqi@0 113 static void dump() { dump_on(tty); }
aoqi@0 114 static void verify();
aoqi@0 115
stefank@6992 116 static bool unload_list_contains(const void* x);
aoqi@0 117 #ifndef PRODUCT
aoqi@0 118 static bool contains_loader_data(ClassLoaderData* loader_data);
aoqi@0 119 #endif
aoqi@0 120
aoqi@0 121 #if INCLUDE_TRACE
aoqi@0 122 private:
aoqi@0 123 static Ticks _class_unload_time;
aoqi@0 124 static void class_unload_event(Klass* const k);
aoqi@0 125 #endif
aoqi@0 126 };
aoqi@0 127
aoqi@0 128 // ClassLoaderData class
aoqi@0 129
aoqi@0 130 class ClassLoaderData : public CHeapObj<mtClass> {
aoqi@0 131 friend class VMStructs;
aoqi@0 132 private:
aoqi@0 133 class Dependencies VALUE_OBJ_CLASS_SPEC {
aoqi@0 134 objArrayOop _list_head;
aoqi@0 135 void locked_add(objArrayHandle last,
aoqi@0 136 objArrayHandle new_dependency,
aoqi@0 137 Thread* THREAD);
aoqi@0 138 public:
aoqi@0 139 Dependencies() : _list_head(NULL) {}
aoqi@0 140 Dependencies(TRAPS) : _list_head(NULL) {
aoqi@0 141 init(CHECK);
aoqi@0 142 }
aoqi@0 143 void add(Handle dependency, TRAPS);
aoqi@0 144 void init(TRAPS);
aoqi@0 145 void oops_do(OopClosure* f);
aoqi@0 146 };
aoqi@0 147
aoqi@0 148 friend class ClassLoaderDataGraph;
stefank@6992 149 friend class ClassLoaderDataGraphKlassIteratorAtomic;
aoqi@0 150 friend class ClassLoaderDataGraphMetaspaceIterator;
aoqi@0 151 friend class MetaDataFactory;
aoqi@0 152 friend class Method;
aoqi@0 153
aoqi@0 154 static ClassLoaderData * _the_null_class_loader_data;
aoqi@0 155
aoqi@0 156 oop _class_loader; // oop used to uniquely identify a class loader
aoqi@0 157 // class loader or a canonical class path
aoqi@0 158 Dependencies _dependencies; // holds dependencies from this class loader
aoqi@0 159 // data to others.
aoqi@0 160
aoqi@0 161 Metaspace * _metaspace; // Meta-space where meta-data defined by the
aoqi@0 162 // classes in the class loader are allocated.
aoqi@0 163 Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup.
aoqi@0 164 bool _unloading; // true if this class loader goes away
stefank@6974 165 bool _keep_alive; // if this CLD is kept alive without a keep_alive_object().
aoqi@0 166 bool _is_anonymous; // if this CLD is for an anonymous class
aoqi@0 167 volatile int _claimed; // true if claimed, for example during GC traces.
aoqi@0 168 // To avoid applying oop closure more than once.
aoqi@0 169 // Has to be an int because we cas it.
aoqi@0 170 Klass* _klasses; // The classes defined by the class loader.
aoqi@0 171
aoqi@0 172 JNIHandleBlock* _handles; // Handles to constant pool arrays
aoqi@0 173
aoqi@0 174 // These method IDs are created for the class loader and set to NULL when the
aoqi@0 175 // class loader is unloaded. They are rarely freed, only for redefine classes
aoqi@0 176 // and if they lose a data race in InstanceKlass.
aoqi@0 177 JNIMethodBlock* _jmethod_ids;
aoqi@0 178
aoqi@0 179 // Metadata to be deallocated when it's safe at class unloading, when
aoqi@0 180 // this class loader isn't unloaded itself.
aoqi@0 181 GrowableArray<Metadata*>* _deallocate_list;
aoqi@0 182
aoqi@0 183 // Support for walking class loader data objects
aoqi@0 184 ClassLoaderData* _next; /// Next loader_datas created
aoqi@0 185
aoqi@0 186 // ReadOnly and ReadWrite metaspaces (static because only on the null
aoqi@0 187 // class loader for now).
aoqi@0 188 static Metaspace* _ro_metaspace;
aoqi@0 189 static Metaspace* _rw_metaspace;
aoqi@0 190
aoqi@0 191 void set_next(ClassLoaderData* next) { _next = next; }
aoqi@0 192 ClassLoaderData* next() const { return _next; }
aoqi@0 193
aoqi@0 194 ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies);
aoqi@0 195 ~ClassLoaderData();
aoqi@0 196
aoqi@0 197 void set_metaspace(Metaspace* m) { _metaspace = m; }
aoqi@0 198
aoqi@0 199 JNIHandleBlock* handles() const;
aoqi@0 200 void set_handles(JNIHandleBlock* handles);
aoqi@0 201
aoqi@0 202 Mutex* metaspace_lock() const { return _metaspace_lock; }
aoqi@0 203
aoqi@0 204 // GC interface.
aoqi@0 205 void clear_claimed() { _claimed = 0; }
aoqi@0 206 bool claimed() const { return _claimed == 1; }
aoqi@0 207 bool claim();
aoqi@0 208
aoqi@0 209 void unload();
aoqi@0 210 bool keep_alive() const { return _keep_alive; }
aoqi@0 211 void classes_do(void f(Klass*));
aoqi@0 212 void loaded_classes_do(KlassClosure* klass_closure);
aoqi@0 213 void classes_do(void f(InstanceKlass*));
aoqi@0 214
aoqi@0 215 // Deallocate free list during class unloading.
aoqi@0 216 void free_deallocate_list();
aoqi@0 217
aoqi@0 218 // Allocate out of this class loader data
aoqi@0 219 MetaWord* allocate(size_t size);
aoqi@0 220
aoqi@0 221 public:
stefank@6992 222
stefank@6992 223 bool is_alive(BoolObjectClosure* is_alive_closure) const;
stefank@6992 224
aoqi@0 225 // Accessors
aoqi@0 226 Metaspace* metaspace_or_null() const { return _metaspace; }
aoqi@0 227
aoqi@0 228 static ClassLoaderData* the_null_class_loader_data() {
aoqi@0 229 return _the_null_class_loader_data;
aoqi@0 230 }
aoqi@0 231
aoqi@0 232 bool is_anonymous() const { return _is_anonymous; }
aoqi@0 233
aoqi@0 234 static void init_null_class_loader_data() {
aoqi@0 235 assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
aoqi@0 236 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
aoqi@0 237
aoqi@0 238 // We explicitly initialize the Dependencies object at a later phase in the initialization
aoqi@0 239 _the_null_class_loader_data = new ClassLoaderData((oop)NULL, false, Dependencies());
aoqi@0 240 ClassLoaderDataGraph::_head = _the_null_class_loader_data;
aoqi@0 241 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
aoqi@0 242 if (DumpSharedSpaces) {
aoqi@0 243 _the_null_class_loader_data->initialize_shared_metaspaces();
aoqi@0 244 }
aoqi@0 245 }
aoqi@0 246
aoqi@0 247 bool is_the_null_class_loader_data() const {
aoqi@0 248 return this == _the_null_class_loader_data;
aoqi@0 249 }
aoqi@0 250 bool is_ext_class_loader_data() const;
aoqi@0 251
aoqi@0 252 // The Metaspace is created lazily so may be NULL. This
aoqi@0 253 // method will allocate a Metaspace if needed.
aoqi@0 254 Metaspace* metaspace_non_null();
aoqi@0 255
aoqi@0 256 oop class_loader() const { return _class_loader; }
aoqi@0 257
stefank@6974 258 // The object the GC is using to keep this ClassLoaderData alive.
stefank@6974 259 oop keep_alive_object() const;
stefank@6974 260
aoqi@0 261 // Returns true if this class loader data is for a loader going away.
aoqi@0 262 bool is_unloading() const {
aoqi@0 263 assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");
aoqi@0 264 return _unloading;
aoqi@0 265 }
stefank@6974 266
stefank@6974 267 // Used to make sure that this CLD is not unloaded.
aoqi@0 268 void set_keep_alive(bool value) { _keep_alive = value; }
aoqi@0 269
aoqi@0 270 unsigned int identity_hash() {
aoqi@0 271 return _class_loader == NULL ? 0 : _class_loader->identity_hash();
aoqi@0 272 }
aoqi@0 273
aoqi@0 274 // Used when tracing from klasses.
aoqi@0 275 void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
aoqi@0 276
aoqi@0 277 void classes_do(KlassClosure* klass_closure);
aoqi@0 278
aoqi@0 279 JNIMethodBlock* jmethod_ids() const { return _jmethod_ids; }
aoqi@0 280 void set_jmethod_ids(JNIMethodBlock* new_block) { _jmethod_ids = new_block; }
aoqi@0 281
aoqi@0 282 void print_value() { print_value_on(tty); }
aoqi@0 283 void print_value_on(outputStream* out) const;
aoqi@0 284 void dump(outputStream * const out) PRODUCT_RETURN;
aoqi@0 285 void verify();
aoqi@0 286 const char* loader_name();
aoqi@0 287
aoqi@0 288 jobject add_handle(Handle h);
aoqi@0 289 void add_class(Klass* k);
aoqi@0 290 void remove_class(Klass* k);
aoqi@0 291 bool contains_klass(Klass* k);
aoqi@0 292 void record_dependency(Klass* to, TRAPS);
aoqi@0 293 void init_dependencies(TRAPS);
aoqi@0 294
aoqi@0 295 void add_to_deallocate_list(Metadata* m);
aoqi@0 296
aoqi@0 297 static ClassLoaderData* class_loader_data(oop loader);
aoqi@0 298 static ClassLoaderData* class_loader_data_or_null(oop loader);
aoqi@0 299 static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);
aoqi@0 300 static void print_loader(ClassLoaderData *loader_data, outputStream *out);
aoqi@0 301
aoqi@0 302 // CDS support
aoqi@0 303 Metaspace* ro_metaspace();
aoqi@0 304 Metaspace* rw_metaspace();
aoqi@0 305 void initialize_shared_metaspaces();
aoqi@0 306 };
aoqi@0 307
stefank@6992 308 // An iterator that distributes Klasses to parallel worker threads.
stefank@6992 309 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj {
asiebenborn@7765 310 Klass* volatile _next_klass;
stefank@6992 311 public:
stefank@6992 312 ClassLoaderDataGraphKlassIteratorAtomic();
stefank@6992 313 Klass* next_klass();
stefank@6992 314 private:
stefank@6992 315 static Klass* next_klass_in_cldg(Klass* klass);
stefank@6992 316 };
stefank@6992 317
aoqi@0 318 class ClassLoaderDataGraphMetaspaceIterator : public StackObj {
aoqi@0 319 ClassLoaderData* _data;
aoqi@0 320 public:
aoqi@0 321 ClassLoaderDataGraphMetaspaceIterator();
aoqi@0 322 ~ClassLoaderDataGraphMetaspaceIterator();
aoqi@0 323 bool repeat() { return _data != NULL; }
aoqi@0 324 Metaspace* get_next() {
aoqi@0 325 assert(_data != NULL, "Should not be NULL in call to the iterator");
aoqi@0 326 Metaspace* result = _data->metaspace_or_null();
aoqi@0 327 _data = _data->next();
aoqi@0 328 // This result might be NULL for class loaders without metaspace
aoqi@0 329 // yet. It would be nice to return only non-null results but
aoqi@0 330 // there is no guarantee that there will be a non-null result
aoqi@0 331 // down the list so the caller is going to have to check.
aoqi@0 332 return result;
aoqi@0 333 }
aoqi@0 334 };
aoqi@0 335 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP

mercurial