1 /*
2 * Copyright (c) 2018, 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 *
175 // The MetadataOnStackMark walk during redefinition saves previous versions if it finds old methods
176 // on the stack or in the code cache, so we only have to repeat the full walk if
177 // they were found at that time.
178 // TODO: have redefinition clean old methods out of the code cache. They still exist in some places.
179 bool walk_all_metadata = InstanceKlass::has_previous_versions_and_reset();
180
181 MetadataOnStackMark md_on_stack(walk_all_metadata, /*redefinition_walk*/false);
182 clean_deallocate_lists(walk_all_metadata);
183 }
184
185 // GC root of class loader data created.
186 ClassLoaderData* volatile ClassLoaderDataGraph::_head = NULL;
187 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
188
189 bool ClassLoaderDataGraph::_should_clean_deallocate_lists = false;
190 bool ClassLoaderDataGraph::_safepoint_cleanup_needed = false;
191 bool ClassLoaderDataGraph::_metaspace_oom = false;
192
193 // Add a new class loader data node to the list. Assign the newly created
194 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
195 ClassLoaderData* ClassLoaderDataGraph::add_to_graph(Handle loader, bool is_unsafe_anonymous) {
196
197 assert_lock_strong(ClassLoaderDataGraph_lock);
198
199 ClassLoaderData* cld;
200
201 // First check if another thread beat us to creating the CLD and installing
202 // it into the loader while we were waiting for the lock.
203 if (!is_unsafe_anonymous && loader.not_null()) {
204 cld = java_lang_ClassLoader::loader_data_acquire(loader());
205 if (cld != NULL) {
206 return cld;
207 }
208 }
209
210 // We mustn't GC until we've installed the ClassLoaderData in the Graph since the CLD
211 // contains oops in _handles that must be walked. GC doesn't walk CLD from the
212 // loader oop in all collections, particularly young collections.
213 NoSafepointVerifier no_safepoints;
214
215 cld = new ClassLoaderData(loader, is_unsafe_anonymous);
216
217 // First install the new CLD to the Graph.
218 cld->set_next(_head);
219 Atomic::release_store(&_head, cld);
220
221 // Next associate with the class_loader.
222 if (!is_unsafe_anonymous) {
223 // Use OrderAccess, since readers need to get the loader_data only after
224 // it's added to the Graph
225 java_lang_ClassLoader::release_set_loader_data(loader(), cld);
226 }
227
228 // Lastly log, if requested
229 LogTarget(Trace, class, loader, data) lt;
230 if (lt.is_enabled()) {
231 ResourceMark rm;
232 LogStream ls(lt);
233 ls.print("create ");
234 cld->print_value_on(&ls);
235 ls.cr();
236 }
237 return cld;
238 }
239
240 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_unsafe_anonymous) {
241 MutexLocker ml(ClassLoaderDataGraph_lock);
242 ClassLoaderData* loader_data = add_to_graph(loader, is_unsafe_anonymous);
243 return loader_data;
244 }
245
246 void ClassLoaderDataGraph::cld_unloading_do(CLDClosure* cl) {
247 assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock);
248 for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) {
249 assert(cld->is_unloading(), "invariant");
250 cl->do_cld(cld);
251 }
252 }
253
254 // These are functions called by the GC, which require all of the CLDs, including the
255 // unloading ones.
256 void ClassLoaderDataGraph::cld_do(CLDClosure* cl) {
257 assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock);
258 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->_next) {
259 cl->do_cld(cld);
260 }
261 }
262
|
1 /*
2 * Copyright (c) 2018, 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 *
175 // The MetadataOnStackMark walk during redefinition saves previous versions if it finds old methods
176 // on the stack or in the code cache, so we only have to repeat the full walk if
177 // they were found at that time.
178 // TODO: have redefinition clean old methods out of the code cache. They still exist in some places.
179 bool walk_all_metadata = InstanceKlass::has_previous_versions_and_reset();
180
181 MetadataOnStackMark md_on_stack(walk_all_metadata, /*redefinition_walk*/false);
182 clean_deallocate_lists(walk_all_metadata);
183 }
184
185 // GC root of class loader data created.
186 ClassLoaderData* volatile ClassLoaderDataGraph::_head = NULL;
187 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
188
189 bool ClassLoaderDataGraph::_should_clean_deallocate_lists = false;
190 bool ClassLoaderDataGraph::_safepoint_cleanup_needed = false;
191 bool ClassLoaderDataGraph::_metaspace_oom = false;
192
193 // Add a new class loader data node to the list. Assign the newly created
194 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
195 ClassLoaderData* ClassLoaderDataGraph::add_to_graph(Handle loader, bool has_class_mirror_holder) {
196
197 assert_lock_strong(ClassLoaderDataGraph_lock);
198
199 ClassLoaderData* cld;
200
201 // First check if another thread beat us to creating the CLD and installing
202 // it into the loader while we were waiting for the lock.
203 if (!has_class_mirror_holder && loader.not_null()) {
204 cld = java_lang_ClassLoader::loader_data_acquire(loader());
205 if (cld != NULL) {
206 return cld;
207 }
208 }
209
210 // We mustn't GC until we've installed the ClassLoaderData in the Graph since the CLD
211 // contains oops in _handles that must be walked. GC doesn't walk CLD from the
212 // loader oop in all collections, particularly young collections.
213 NoSafepointVerifier no_safepoints;
214
215 cld = new ClassLoaderData(loader, has_class_mirror_holder);
216
217 // First install the new CLD to the Graph.
218 cld->set_next(_head);
219 Atomic::release_store(&_head, cld);
220
221 // Next associate with the class_loader.
222 if (!has_class_mirror_holder) {
223 // Use OrderAccess, since readers need to get the loader_data only after
224 // it's added to the Graph
225 java_lang_ClassLoader::release_set_loader_data(loader(), cld);
226 }
227
228 // Lastly log, if requested
229 LogTarget(Trace, class, loader, data) lt;
230 if (lt.is_enabled()) {
231 ResourceMark rm;
232 LogStream ls(lt);
233 ls.print("create ");
234 cld->print_value_on(&ls);
235 ls.cr();
236 }
237 return cld;
238 }
239
240 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool has_class_mirror_holder) {
241 MutexLocker ml(ClassLoaderDataGraph_lock);
242 ClassLoaderData* loader_data = add_to_graph(loader, has_class_mirror_holder);
243 return loader_data;
244 }
245
246 void ClassLoaderDataGraph::cld_unloading_do(CLDClosure* cl) {
247 assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock);
248 for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) {
249 assert(cld->is_unloading(), "invariant");
250 cl->do_cld(cld);
251 }
252 }
253
254 // These are functions called by the GC, which require all of the CLDs, including the
255 // unloading ones.
256 void ClassLoaderDataGraph::cld_do(CLDClosure* cl) {
257 assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock);
258 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->_next) {
259 cl->do_cld(cld);
260 }
261 }
262
|