1 /*
  2  * Copyright (c) 2015, 2023, 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 #include "precompiled.hpp"
 26 #include "code/nmethod.hpp"
 27 #include "code/dependencies.hpp"
 28 #include "code/dependencyContext.hpp"
 29 #include "logging/log.hpp"
 30 #include "logging/logStream.hpp"
 31 #include "memory/resourceArea.hpp"
 32 #include "runtime/atomic.hpp"
 33 #include "runtime/deoptimization.hpp"
 34 #include "runtime/mutexLocker.hpp"
 35 #include "runtime/orderAccess.hpp"
 36 #include "runtime/perfData.hpp"
 37 #include "utilities/exceptions.hpp"
 38 
 39 PerfCounter* DependencyContext::_perf_total_buckets_allocated_count   = nullptr;
 40 PerfCounter* DependencyContext::_perf_total_buckets_deallocated_count = nullptr;
 41 PerfCounter* DependencyContext::_perf_total_buckets_stale_count       = nullptr;
 42 PerfCounter* DependencyContext::_perf_total_buckets_stale_acc_count   = nullptr;
 43 nmethodBucket* volatile DependencyContext::_purge_list                = nullptr;
 44 volatile uint64_t DependencyContext::_cleaning_epoch                  = 0;
 45 uint64_t  DependencyContext::_cleaning_epoch_monotonic                = 0;
 46 
 47 void dependencyContext_init() {
 48   DependencyContext::init();
 49 }
 50 
 51 void DependencyContext::init() {
 52   if (UsePerfData) {
 53     EXCEPTION_MARK;
 54     _perf_total_buckets_allocated_count =
 55         PerfDataManager::create_counter(SUN_CI, "nmethodBucketsAllocated", PerfData::U_Events, CHECK);
 56     _perf_total_buckets_deallocated_count =
 57         PerfDataManager::create_counter(SUN_CI, "nmethodBucketsDeallocated", PerfData::U_Events, CHECK);
 58     _perf_total_buckets_stale_count =
 59         PerfDataManager::create_counter(SUN_CI, "nmethodBucketsStale", PerfData::U_Events, CHECK);
 60     _perf_total_buckets_stale_acc_count =
 61         PerfDataManager::create_counter(SUN_CI, "nmethodBucketsStaleAccumulated", PerfData::U_Events, CHECK);
 62   }
 63 }
 64 
 65 //
 66 // Walk the list of dependent nmethods searching for nmethods which
 67 // are dependent on the changes that were passed in and mark them for
 68 // deoptimization.
 69 //
 70 void DependencyContext::mark_dependent_nmethods(DeoptimizationScope* deopt_scope, DepChange& changes, InstanceKlass* context) {
 71   for (nmethodBucket* b = dependencies_not_unloading(); b != nullptr; b = b->next_not_unloading()) {
 72     nmethod* nm = b->get_nmethod();
 73     {
 74       LogStreamHandle(Trace, dependencies) log;
 75       if (log.is_enabled()) {
 76         log.print("Processing ");
 77         if (context != nullptr) {
 78           log.print(" ctx=");
 79           context->name()->print_value_on(&log);
 80           log.print(" ");
 81         }
 82         nm->print_value_on(&log);
 83       }
 84     }
 85     if (nm->is_marked_for_deoptimization()) {
 86       deopt_scope->dependent(nm);
 87     } else if (nm->check_dependency_on(changes)) {
 88       LogTarget(Info, dependencies) lt;
 89       if (lt.is_enabled()) {
 90         ResourceMark rm;
 91         LogStream ls(&lt);
 92         ls.print_cr("Marked for deoptimization");
 93         changes.print_on(&ls);
 94         nm->print_on(&ls);
 95         nm->print_dependencies_on(&ls);
 96       }
 97       deopt_scope->mark(nm, !changes.is_call_site_change());
 98     }
 99   }
100 }
101 
102 //
103 // Add an nmethod to the dependency context.
104 //
105 void DependencyContext::add_dependent_nmethod(nmethod* nm) {
106   assert_lock_strong(CodeCache_lock);
107   for (nmethodBucket* b = dependencies_not_unloading(); b != nullptr; b = b->next_not_unloading()) {
108     if (nm == b->get_nmethod()) {
109       return;
110     }
111   }
112   nmethodBucket* new_head = new nmethodBucket(nm, nullptr);
113   for (;;) {
114     nmethodBucket* head = Atomic::load(_dependency_context_addr);
115     new_head->set_next(head);
116     if (Atomic::cmpxchg(_dependency_context_addr, head, new_head) == head) {
117       break;
118     }
119   }
120   if (UsePerfData) {
121     _perf_total_buckets_allocated_count->inc();
122   }
123 }
124 
125 void DependencyContext::release(nmethodBucket* b) {
126   if (delete_on_release()) {
127     assert_locked_or_safepoint(CodeCache_lock);
128     delete b;
129     if (UsePerfData) {
130       _perf_total_buckets_deallocated_count->inc();
131     }
132   } else {
133     // Mark the context as having stale entries, since it is not safe to
134     // expunge the list right now.
135     for (;;) {
136       nmethodBucket* purge_list_head = Atomic::load(&_purge_list);
137       b->set_purge_list_next(purge_list_head);
138       if (Atomic::cmpxchg(&_purge_list, purge_list_head, b) == purge_list_head) {
139         break;
140       }
141     }
142     if (UsePerfData) {
143       _perf_total_buckets_stale_count->inc();
144       _perf_total_buckets_stale_acc_count->inc();
145     }
146   }
147 }
148 
149 //
150 // Reclaim all unused buckets.
151 //
152 void DependencyContext::purge_dependency_contexts() {
153   int removed = 0;
154   for (nmethodBucket* b = _purge_list; b != nullptr;) {
155     nmethodBucket* next = b->purge_list_next();
156     removed++;
157     delete b;
158     b = next;
159   }
160   if (UsePerfData && removed > 0) {
161     _perf_total_buckets_deallocated_count->inc(removed);
162   }
163   _purge_list = nullptr;
164 }
165 
166 //
167 // Cleanup a dependency context by unlinking and placing all dependents corresponding
168 // to is_unloading nmethods on a purge list, which will be deleted later when it is safe.
169 void DependencyContext::clean_unloading_dependents() {
170   if (!claim_cleanup()) {
171     // Somebody else is cleaning up this dependency context.
172     return;
173   }
174   // Walk the nmethodBuckets and move dead entries on the purge list, which will
175   // be deleted during ClassLoaderDataGraph::purge().
176   nmethodBucket* b = dependencies_not_unloading();
177   while (b != nullptr) {
178     nmethodBucket* next = b->next_not_unloading();
179     b = next;
180   }
181 }
182 
183 nmethodBucket* DependencyContext::release_and_get_next_not_unloading(nmethodBucket* b) {
184   nmethodBucket* next = b->next_not_unloading();
185   release(b);
186   return next;
187  }
188 
189 //
190 // Invalidate all dependencies in the context
191 void DependencyContext::remove_all_dependents() {
192   // Assume that the dependency is not deleted immediately but moved into the
193   // purge list when calling this.
194   assert(!delete_on_release(), "should not delete on release");
195 
196   nmethodBucket* first = Atomic::load_acquire(_dependency_context_addr);
197   if (first == nullptr) {
198     return;
199   }
200 
201   nmethodBucket* cur = first;
202   nmethodBucket* last = cur;
203   jlong count = 0;
204   for (; cur != nullptr; cur = cur->next()) {
205     assert(cur->get_nmethod()->is_unloading(), "must be");
206     last = cur;
207     count++;
208   }
209 
210   // Add the whole list to the purge list at once.
211   nmethodBucket* old_purge_list_head = Atomic::load(&_purge_list);
212   for (;;) {
213     last->set_purge_list_next(old_purge_list_head);
214     nmethodBucket* next_purge_list_head = Atomic::cmpxchg(&_purge_list, old_purge_list_head, first);
215     if (old_purge_list_head == next_purge_list_head) {
216       break;
217     }
218     old_purge_list_head = next_purge_list_head;
219   }
220 
221   if (UsePerfData) {
222     _perf_total_buckets_stale_count->inc(count);
223     _perf_total_buckets_stale_acc_count->inc(count);
224   }
225 
226   set_dependencies(nullptr);
227 }
228 
229 void DependencyContext::remove_and_mark_for_deoptimization_all_dependents(DeoptimizationScope* deopt_scope) {
230   nmethodBucket* b = dependencies_not_unloading();
231   set_dependencies(nullptr);
232   while (b != nullptr) {
233     nmethod* nm = b->get_nmethod();
234     // Also count already (concurrently) marked nmethods to make sure
235     // deoptimization is triggered before execution in this thread continues.
236     deopt_scope->mark(nm);
237     b = release_and_get_next_not_unloading(b);
238   }
239 }
240 
241 #ifndef PRODUCT
242 void DependencyContext::print_dependent_nmethods(bool verbose) {
243   int idx = 0;
244   for (nmethodBucket* b = dependencies_not_unloading(); b != nullptr; b = b->next_not_unloading()) {
245     nmethod* nm = b->get_nmethod();
246     tty->print("[%d] { ", idx++);
247     if (!verbose) {
248       nm->print_on(tty, "nmethod");
249       tty->print_cr(" } ");
250     } else {
251       nm->print();
252       nm->print_dependencies_on(tty);
253       tty->print_cr("--- } ");
254     }
255   }
256 }
257 #endif //PRODUCT
258 
259 bool DependencyContext::is_dependent_nmethod(nmethod* nm) {
260   for (nmethodBucket* b = dependencies_not_unloading(); b != nullptr; b = b->next_not_unloading()) {
261     if (nm == b->get_nmethod()) {
262       return true;
263     }
264   }
265   return false;
266 }
267 
268 // We use a monotonically increasing epoch counter to track the last epoch a given
269 // dependency context was cleaned. GC threads claim cleanup tasks by performing
270 // a CAS on this value.
271 bool DependencyContext::claim_cleanup() {
272   uint64_t cleaning_epoch = Atomic::load(&_cleaning_epoch);
273   uint64_t last_cleanup = Atomic::load(_last_cleanup_addr);
274   if (last_cleanup >= cleaning_epoch) {
275     return false;
276   }
277   return Atomic::cmpxchg(_last_cleanup_addr, last_cleanup, cleaning_epoch) == last_cleanup;
278 }
279 
280 bool DependencyContext::delete_on_release() {
281   return Atomic::load(&_cleaning_epoch) == 0;
282 }
283 
284 // Retrieve the first nmethodBucket that has a dependent that does not correspond to
285 // an is_unloading nmethod. Any nmethodBucket entries observed from the original head
286 // that is_unloading() will be unlinked and placed on the purge list.
287 nmethodBucket* DependencyContext::dependencies_not_unloading() {
288   for (;;) {
289     // Need acquire because the read value could come from a concurrent insert.
290     nmethodBucket* head = Atomic::load_acquire(_dependency_context_addr);
291     if (head == nullptr || !head->get_nmethod()->is_unloading()) {
292       return head;
293     }
294     nmethodBucket* head_next = head->next();
295     OrderAccess::loadload();
296     if (Atomic::load(_dependency_context_addr) != head) {
297       // Unstable load of head w.r.t. head->next
298       continue;
299     }
300     if (Atomic::cmpxchg(_dependency_context_addr, head, head_next) == head) {
301       // Release is_unloading entries if unlinking was claimed
302       DependencyContext::release(head);
303     }
304   }
305 }
306 
307 // Relaxed accessors
308 void DependencyContext::set_dependencies(nmethodBucket* b) {
309   Atomic::store(_dependency_context_addr, b);
310 }
311 
312 nmethodBucket* DependencyContext::dependencies() {
313   return Atomic::load(_dependency_context_addr);
314 }
315 
316 // After the gc_prologue, the dependency contexts may be claimed by the GC
317 // and releasing of nmethodBucket entries will be deferred and placed on
318 // a purge list to be deleted later.
319 void DependencyContext::cleaning_start() {
320   assert(SafepointSynchronize::is_at_safepoint(), "must be");
321   uint64_t epoch = ++_cleaning_epoch_monotonic;
322   Atomic::store(&_cleaning_epoch, epoch);
323 }
324 
325 // The epilogue marks the end of dependency context cleanup by the GC,
326 // and also makes subsequent releases of nmethodBuckets cause immediate
327 // deletion. It is okay to delay calling of cleaning_end() to a concurrent
328 // phase, subsequent to the safepoint operation in which cleaning_start()
329 // was called. That allows dependency contexts to be cleaned concurrently.
330 void DependencyContext::cleaning_end() {
331   uint64_t epoch = 0;
332   Atomic::store(&_cleaning_epoch, epoch);
333 }
334 
335 // This function skips over nmethodBuckets in the list corresponding to
336 // nmethods that are is_unloading. This allows exposing a view of the
337 // dependents as-if they were already cleaned, despite being cleaned
338 // concurrently. Any entry observed that is_unloading() will be unlinked
339 // and placed on the purge list.
340 nmethodBucket* nmethodBucket::next_not_unloading() {
341   for (;;) {
342     // Do not need acquire because the loaded entry can never be
343     // concurrently inserted.
344     nmethodBucket* next = Atomic::load(&_next);
345     if (next == nullptr || !next->get_nmethod()->is_unloading()) {
346       return next;
347     }
348     nmethodBucket* next_next = Atomic::load(&next->_next);
349     OrderAccess::loadload();
350     if (Atomic::load(&_next) != next) {
351       // Unstable load of next w.r.t. next->next
352       continue;
353     }
354     if (Atomic::cmpxchg(&_next, next, next_next) == next) {
355       // Release is_unloading entries if unlinking was claimed
356       DependencyContext::release(next);
357     }
358   }
359 }
360 
361 // Relaxed accessors
362 nmethodBucket* nmethodBucket::next() {
363   return Atomic::load(&_next);
364 }
365 
366 void nmethodBucket::set_next(nmethodBucket* b) {
367   Atomic::store(&_next, b);
368 }
369 
370 nmethodBucket* nmethodBucket::purge_list_next() {
371   return Atomic::load(&_purge_list_next);
372 }
373 
374 void nmethodBucket::set_purge_list_next(nmethodBucket* b) {
375   Atomic::store(&_purge_list_next, b);
376 }