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