1 /*
2 * Copyright (c) 2012, 2019, 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 *
100
101 friend class ClassLoaderDataGraph;
102 friend class ClassLoaderDataGraphIterator;
103 friend class ClassLoaderDataGraphKlassIteratorAtomic;
104 friend class ClassLoaderDataGraphKlassIteratorStatic;
105 friend class ClassLoaderDataGraphMetaspaceIterator;
106 friend class Klass;
107 friend class MetaDataFactory;
108 friend class Method;
109
110 static ClassLoaderData * _the_null_class_loader_data;
111
112 WeakHandle<vm_class_loader_data> _holder; // The oop that determines lifetime of this class loader
113 OopHandle _class_loader; // The instance of java/lang/ClassLoader associated with
114 // this ClassLoaderData
115
116 ClassLoaderMetaspace * volatile _metaspace; // Meta-space where meta-data defined by the
117 // classes in the class loader are allocated.
118 Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup.
119 bool _unloading; // true if this class loader goes away
120 bool _is_unsafe_anonymous; // CLD is dedicated to one class and that class determines the CLDs lifecycle.
121 // For example, an unsafe anonymous class.
122
123 // Remembered sets support for the oops in the class loader data.
124 bool _modified_oops; // Card Table Equivalent (YC/CMS support)
125 bool _accumulated_modified_oops; // Mod Union Equivalent (CMS support)
126
127 int _keep_alive; // if this CLD is kept alive.
128 // Used for unsafe anonymous classes and the boot class
129 // loader. _keep_alive does not need to be volatile or
130 // atomic since there is one unique CLD per unsafe anonymous class.
131
132 volatile int _claim; // non-zero if claimed, for example during GC traces.
133 // To avoid applying oop closure more than once.
134 ChunkedHandleList _handles; // Handles to constant pool arrays, Modules, etc, which
135 // have the same life cycle of the corresponding ClassLoader.
136
137 NOT_PRODUCT(volatile int _dependency_count;) // number of class loader dependencies
138
139 Klass* volatile _klasses; // The classes defined by the class loader.
140 PackageEntryTable* volatile _packages; // The packages defined by the class loader.
141 ModuleEntryTable* volatile _modules; // The modules defined by the class loader.
142 ModuleEntry* _unnamed_module; // This class loader's unnamed module.
143 Dictionary* _dictionary; // The loaded InstanceKlasses, including initiated by this class loader
144
145 // These method IDs are created for the class loader and set to NULL when the
146 // class loader is unloaded. They are rarely freed, only for redefine classes
147 // and if they lose a data race in InstanceKlass.
148 JNIMethodBlock* _jmethod_ids;
149
150 // Metadata to be deallocated when it's safe at class unloading, when
151 // this class loader isn't unloaded itself.
152 GrowableArray<Metadata*>* _deallocate_list;
153
154 // Support for walking class loader data objects
155 ClassLoaderData* _next; /// Next loader_datas created
156
157 Klass* _class_loader_klass;
158 Symbol* _name;
159 Symbol* _name_and_id;
160 JFR_ONLY(DEFINE_TRACE_ID_FIELD;)
161
162 void set_next(ClassLoaderData* next) { _next = next; }
163 ClassLoaderData* next() const { return Atomic::load(&_next); }
164
165 ClassLoaderData(Handle h_class_loader, bool is_unsafe_anonymous);
166 ~ClassLoaderData();
167
168 // The CLD are not placed in the Heap, so the Card Table or
169 // the Mod Union Table can't be used to mark when CLD have modified oops.
170 // The CT and MUT bits saves this information for the whole class loader data.
171 void clear_modified_oops() { _modified_oops = false; }
172 public:
173 void record_modified_oops() { _modified_oops = true; }
174 bool has_modified_oops() { return _modified_oops; }
175
176 void accumulate_modified_oops() { if (has_modified_oops()) _accumulated_modified_oops = true; }
177 void clear_accumulated_modified_oops() { _accumulated_modified_oops = false; }
178 bool has_accumulated_modified_oops() { return _accumulated_modified_oops; }
179 oop holder_no_keepalive() const;
180 oop holder_phantom() const;
181
182 private:
183 void unload();
184 bool keep_alive() const { return _keep_alive > 0; }
185
214 };
215 void clear_claim() { _claim = 0; }
216 void clear_claim(int claim);
217 bool claimed() const { return _claim != 0; }
218 bool claimed(int claim) const { return (_claim & claim) == claim; }
219 bool try_claim(int claim);
220
221 // Computes if the CLD is alive or not. This is safe to call in concurrent
222 // contexts.
223 bool is_alive() const;
224
225 // Accessors
226 ClassLoaderMetaspace* metaspace_or_null() const { return _metaspace; }
227
228 static ClassLoaderData* the_null_class_loader_data() {
229 return _the_null_class_loader_data;
230 }
231
232 Mutex* metaspace_lock() const { return _metaspace_lock; }
233
234 bool is_unsafe_anonymous() const { return _is_unsafe_anonymous; }
235
236 static void init_null_class_loader_data();
237
238 bool is_the_null_class_loader_data() const {
239 return this == _the_null_class_loader_data;
240 }
241
242 // Returns true if this class loader data is for the system class loader.
243 // (Note that the class loader data may be unsafe anonymous.)
244 bool is_system_class_loader_data() const;
245
246 // Returns true if this class loader data is for the platform class loader.
247 // (Note that the class loader data may be unsafe anonymous.)
248 bool is_platform_class_loader_data() const;
249
250 // Returns true if this class loader data is for the boot class loader.
251 // (Note that the class loader data may be unsafe anonymous.)
252 inline bool is_boot_class_loader_data() const;
253
254 bool is_builtin_class_loader_data() const;
255 bool is_permanent_class_loader_data() const;
256
257 // The Metaspace is created lazily so may be NULL. This
258 // method will allocate a Metaspace if needed.
259 ClassLoaderMetaspace* metaspace_non_null();
260
261 inline oop class_loader() const;
262
263 // Returns true if this class loader data is for a loader going away.
264 // Note that this is only safe after the GC has computed if the CLD is
265 // unloading or not. In concurrent contexts where there are no such
266 // guarantees, is_alive() should be used instead.
267 bool is_unloading() const {
268 assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");
269 return _unloading;
270 }
271
272 // Used to refcount an unsafe anonymous class's CLD in order to
273 // indicate their aliveness.
274 void inc_keep_alive();
275 void dec_keep_alive();
276
277 void initialize_holder(Handle holder);
278
279 void oops_do(OopClosure* f, int claim_value, bool clear_modified_oops = false);
280
281 void classes_do(KlassClosure* klass_closure);
282 Klass* klasses() { return _klasses; }
283
284 JNIMethodBlock* jmethod_ids() const { return _jmethod_ids; }
285 void set_jmethod_ids(JNIMethodBlock* new_block) { _jmethod_ids = new_block; }
286
287 void print() const;
288 void print_on(outputStream* out) const PRODUCT_RETURN;
289 void print_value() const;
290 void print_value_on(outputStream* out) const;
291 void verify();
292
296 void add_class(Klass* k, bool publicize = true);
297 void remove_class(Klass* k);
298 bool contains_klass(Klass* k);
299 void record_dependency(const Klass* to);
300 PackageEntryTable* packages() { return _packages; }
301 ModuleEntry* unnamed_module() { return _unnamed_module; }
302 ModuleEntryTable* modules();
303 bool modules_defined() { return (_modules != NULL); }
304
305 // Offsets
306 static ByteSize holder_offset() { return in_ByteSize(offset_of(ClassLoaderData, _holder)); }
307 static ByteSize keep_alive_offset() { return in_ByteSize(offset_of(ClassLoaderData, _keep_alive)); }
308
309 // Loaded class dictionary
310 Dictionary* dictionary() const { return _dictionary; }
311
312 void add_to_deallocate_list(Metadata* m);
313
314 static ClassLoaderData* class_loader_data(oop loader);
315 static ClassLoaderData* class_loader_data_or_null(oop loader);
316 static ClassLoaderData* unsafe_anonymous_class_loader_data(Handle loader);
317
318 // Returns Klass* of associated class loader, or NULL if associated loader is 'bootstrap'.
319 // Also works if unloading.
320 Klass* class_loader_klass() const { return _class_loader_klass; }
321
322 // Returns the class loader's explict name as specified during
323 // construction or the class loader's qualified class name.
324 // Works during unloading.
325 const char* loader_name() const;
326 // Returns the explicitly specified class loader name or NULL.
327 Symbol* name() const { return _name; }
328
329 // Obtain the class loader's _name_and_id, works during unloading.
330 const char* loader_name_and_id() const;
331 Symbol* name_and_id() const { return _name_and_id; }
332
333 JFR_ONLY(DEFINE_TRACE_ID_METHODS;)
334 };
335
336 #endif // SHARE_CLASSFILE_CLASSLOADERDATA_HPP
|
1 /*
2 * Copyright (c) 2012, 2020, 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 *
100
101 friend class ClassLoaderDataGraph;
102 friend class ClassLoaderDataGraphIterator;
103 friend class ClassLoaderDataGraphKlassIteratorAtomic;
104 friend class ClassLoaderDataGraphKlassIteratorStatic;
105 friend class ClassLoaderDataGraphMetaspaceIterator;
106 friend class Klass;
107 friend class MetaDataFactory;
108 friend class Method;
109
110 static ClassLoaderData * _the_null_class_loader_data;
111
112 WeakHandle<vm_class_loader_data> _holder; // The oop that determines lifetime of this class loader
113 OopHandle _class_loader; // The instance of java/lang/ClassLoader associated with
114 // this ClassLoaderData
115
116 ClassLoaderMetaspace * volatile _metaspace; // Meta-space where meta-data defined by the
117 // classes in the class loader are allocated.
118 Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup.
119 bool _unloading; // true if this class loader goes away
120 bool _has_class_mirror_holder; // If true, CLD is dedicated to one class and that class determines
121 // the CLDs lifecycle. For example, a non-strong hidden class or an
122 // unsafe anonymous class. Arrays of these classes are also assigned
123 // to these class loader datas.
124
125 // Remembered sets support for the oops in the class loader data.
126 bool _modified_oops; // Card Table Equivalent (YC/CMS support)
127 bool _accumulated_modified_oops; // Mod Union Equivalent (CMS support)
128
129 int _keep_alive; // if this CLD is kept alive.
130 // Used for weak hidden classes, unsafe anonymous classes and the
131 // boot class loader. _keep_alive does not need to be volatile or
132 // atomic since there is one unique CLD per weak hidden or unsafe anonymous class.
133
134 volatile int _claim; // non-zero if claimed, for example during GC traces.
135 // To avoid applying oop closure more than once.
136 ChunkedHandleList _handles; // Handles to constant pool arrays, Modules, etc, which
137 // have the same life cycle of the corresponding ClassLoader.
138
139 NOT_PRODUCT(volatile int _dependency_count;) // number of class loader dependencies
140
141 Klass* volatile _klasses; // The classes defined by the class loader.
142 PackageEntryTable* volatile _packages; // The packages defined by the class loader.
143 ModuleEntryTable* volatile _modules; // The modules defined by the class loader.
144 ModuleEntry* _unnamed_module; // This class loader's unnamed module.
145 Dictionary* _dictionary; // The loaded InstanceKlasses, including initiated by this class loader
146
147 // These method IDs are created for the class loader and set to NULL when the
148 // class loader is unloaded. They are rarely freed, only for redefine classes
149 // and if they lose a data race in InstanceKlass.
150 JNIMethodBlock* _jmethod_ids;
151
152 // Metadata to be deallocated when it's safe at class unloading, when
153 // this class loader isn't unloaded itself.
154 GrowableArray<Metadata*>* _deallocate_list;
155
156 // Support for walking class loader data objects
157 ClassLoaderData* _next; /// Next loader_datas created
158
159 Klass* _class_loader_klass;
160 Symbol* _name;
161 Symbol* _name_and_id;
162 JFR_ONLY(DEFINE_TRACE_ID_FIELD;)
163
164 void set_next(ClassLoaderData* next) { _next = next; }
165 ClassLoaderData* next() const { return Atomic::load(&_next); }
166
167 ClassLoaderData(Handle h_class_loader, bool has_class_mirror_holder);
168 ~ClassLoaderData();
169
170 // The CLD are not placed in the Heap, so the Card Table or
171 // the Mod Union Table can't be used to mark when CLD have modified oops.
172 // The CT and MUT bits saves this information for the whole class loader data.
173 void clear_modified_oops() { _modified_oops = false; }
174 public:
175 void record_modified_oops() { _modified_oops = true; }
176 bool has_modified_oops() { return _modified_oops; }
177
178 void accumulate_modified_oops() { if (has_modified_oops()) _accumulated_modified_oops = true; }
179 void clear_accumulated_modified_oops() { _accumulated_modified_oops = false; }
180 bool has_accumulated_modified_oops() { return _accumulated_modified_oops; }
181 oop holder_no_keepalive() const;
182 oop holder_phantom() const;
183
184 private:
185 void unload();
186 bool keep_alive() const { return _keep_alive > 0; }
187
216 };
217 void clear_claim() { _claim = 0; }
218 void clear_claim(int claim);
219 bool claimed() const { return _claim != 0; }
220 bool claimed(int claim) const { return (_claim & claim) == claim; }
221 bool try_claim(int claim);
222
223 // Computes if the CLD is alive or not. This is safe to call in concurrent
224 // contexts.
225 bool is_alive() const;
226
227 // Accessors
228 ClassLoaderMetaspace* metaspace_or_null() const { return _metaspace; }
229
230 static ClassLoaderData* the_null_class_loader_data() {
231 return _the_null_class_loader_data;
232 }
233
234 Mutex* metaspace_lock() const { return _metaspace_lock; }
235
236 bool has_class_mirror_holder() const { return _has_class_mirror_holder; }
237
238 static void init_null_class_loader_data();
239
240 bool is_the_null_class_loader_data() const {
241 return this == _the_null_class_loader_data;
242 }
243
244 // Returns true if this class loader data is for the system class loader.
245 // (Note that the class loader data may be for an weak hidden or unsafe anonymous class)
246 bool is_system_class_loader_data() const;
247
248 // Returns true if this class loader data is for the platform class loader.
249 // (Note that the class loader data may be for an weak hidden or unsafe anonymous class)
250 bool is_platform_class_loader_data() const;
251
252 // Returns true if this class loader data is for the boot class loader.
253 // (Note that the class loader data may be for an weak hidden unsafe anonymous class)
254 inline bool is_boot_class_loader_data() const;
255
256 bool is_builtin_class_loader_data() const;
257 bool is_permanent_class_loader_data() const;
258
259 // The Metaspace is created lazily so may be NULL. This
260 // method will allocate a Metaspace if needed.
261 ClassLoaderMetaspace* metaspace_non_null();
262
263 inline oop class_loader() const;
264
265 // Returns true if this class loader data is for a loader going away.
266 // Note that this is only safe after the GC has computed if the CLD is
267 // unloading or not. In concurrent contexts where there are no such
268 // guarantees, is_alive() should be used instead.
269 bool is_unloading() const {
270 assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");
271 return _unloading;
272 }
273
274 // Used to refcount an weak hidden or unsafe anonymous class's CLD in order to
275 // indicate their aliveness.
276 void inc_keep_alive();
277 void dec_keep_alive();
278
279 void initialize_holder(Handle holder);
280
281 void oops_do(OopClosure* f, int claim_value, bool clear_modified_oops = false);
282
283 void classes_do(KlassClosure* klass_closure);
284 Klass* klasses() { return _klasses; }
285
286 JNIMethodBlock* jmethod_ids() const { return _jmethod_ids; }
287 void set_jmethod_ids(JNIMethodBlock* new_block) { _jmethod_ids = new_block; }
288
289 void print() const;
290 void print_on(outputStream* out) const PRODUCT_RETURN;
291 void print_value() const;
292 void print_value_on(outputStream* out) const;
293 void verify();
294
298 void add_class(Klass* k, bool publicize = true);
299 void remove_class(Klass* k);
300 bool contains_klass(Klass* k);
301 void record_dependency(const Klass* to);
302 PackageEntryTable* packages() { return _packages; }
303 ModuleEntry* unnamed_module() { return _unnamed_module; }
304 ModuleEntryTable* modules();
305 bool modules_defined() { return (_modules != NULL); }
306
307 // Offsets
308 static ByteSize holder_offset() { return in_ByteSize(offset_of(ClassLoaderData, _holder)); }
309 static ByteSize keep_alive_offset() { return in_ByteSize(offset_of(ClassLoaderData, _keep_alive)); }
310
311 // Loaded class dictionary
312 Dictionary* dictionary() const { return _dictionary; }
313
314 void add_to_deallocate_list(Metadata* m);
315
316 static ClassLoaderData* class_loader_data(oop loader);
317 static ClassLoaderData* class_loader_data_or_null(oop loader);
318 static ClassLoaderData* has_class_mirror_holder_cld(Handle loader);
319
320 // Returns Klass* of associated class loader, or NULL if associated loader is 'bootstrap'.
321 // Also works if unloading.
322 Klass* class_loader_klass() const { return _class_loader_klass; }
323
324 // Returns the class loader's explict name as specified during
325 // construction or the class loader's qualified class name.
326 // Works during unloading.
327 const char* loader_name() const;
328 // Returns the explicitly specified class loader name or NULL.
329 Symbol* name() const { return _name; }
330
331 // Obtain the class loader's _name_and_id, works during unloading.
332 const char* loader_name_and_id() const;
333 Symbol* name_and_id() const { return _name_and_id; }
334
335 JFR_ONLY(DEFINE_TRACE_ID_METHODS;)
336 };
337
338 #endif // SHARE_CLASSFILE_CLASSLOADERDATA_HPP
|