src/share/vm/classfile/classLoaderData.hpp

Mon, 12 Aug 2019 18:30:40 +0300

author
apetushkov
date
Mon, 12 Aug 2019 18:30:40 +0300
changeset 9858
b985cbb00e68
parent 8762
654eaca01d61
child 9931
fd44df5e3bc3
permissions
-rw-r--r--

8223147: JFR Backport
8199712: Flight Recorder
8203346: JFR: Inconsistent signature of jfr_add_string_constant
8195817: JFR.stop should require name of recording
8195818: JFR.start should increase autogenerated name by one
8195819: Remove recording=x from jcmd JFR.check output
8203921: JFR thread sampling is missing fixes from JDK-8194552
8203929: Limit amount of data for JFR.dump
8203664: JFR start failure after AppCDS archive created with JFR StartFlightRecording
8003209: JFR events for network utilization
8207392: [PPC64] Implement JFR profiling
8202835: jfr/event/os/TestSystemProcess.java fails on missing events
Summary: Backport JFR from JDK11. Initial integration
Reviewed-by: neugens

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

mercurial