1 /*
  2  * Copyright (c) 2001, 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 "classfile/classLoaderData.hpp"
 27 #include "classfile/vmClasses.hpp"
 28 #include "gc/shared/allocTracer.hpp"
 29 #include "gc/shared/barrierSet.hpp"
 30 #include "gc/shared/collectedHeap.hpp"
 31 #include "gc/shared/collectedHeap.inline.hpp"
 32 #include "gc/shared/gcLocker.inline.hpp"
 33 #include "gc/shared/gcHeapSummary.hpp"
 34 #include "gc/shared/stringdedup/stringDedup.hpp"
 35 #include "gc/shared/gcTrace.hpp"
 36 #include "gc/shared/gcTraceTime.inline.hpp"
 37 #include "gc/shared/gcVMOperations.hpp"
 38 #include "gc/shared/gcWhen.hpp"
 39 #include "gc/shared/gc_globals.hpp"
 40 #include "gc/shared/memAllocator.hpp"
 41 #include "gc/shared/tlab_globals.hpp"
 42 #include "logging/log.hpp"
 43 #include "logging/logStream.hpp"
 44 #include "memory/classLoaderMetaspace.hpp"
 45 #include "memory/metaspaceUtils.hpp"
 46 #include "memory/resourceArea.hpp"
 47 #include "memory/universe.hpp"
 48 #include "oops/instanceMirrorKlass.hpp"
 49 #include "oops/oop.inline.hpp"
 50 #include "runtime/handles.inline.hpp"
 51 #include "runtime/init.hpp"
 52 #include "runtime/javaThread.hpp"
 53 #include "runtime/perfData.hpp"
 54 #include "runtime/threadSMR.hpp"
 55 #include "runtime/vmThread.hpp"
 56 #include "services/heapDumper.hpp"
 57 #include "utilities/align.hpp"
 58 #include "utilities/copy.hpp"
 59 #include "utilities/events.hpp"
 60 
 61 class ClassLoaderData;
 62 
 63 size_t CollectedHeap::_lab_alignment_reserve = SIZE_MAX;
 64 Klass* CollectedHeap::_filler_object_klass = nullptr;
 65 size_t CollectedHeap::_filler_array_max_size = 0;
 66 size_t CollectedHeap::_stack_chunk_max_size = 0;
 67 
 68 class GCMessage : public FormatBuffer<1024> {
 69  public:
 70   bool is_before;
 71 };
 72 
 73 template <>
 74 void EventLogBase<GCMessage>::print(outputStream* st, GCMessage& m) {
 75   st->print_cr("GC heap %s", m.is_before ? "before" : "after");
 76   st->print_raw(m);
 77 }
 78 
 79 class GCHeapLog : public EventLogBase<GCMessage> {
 80  private:
 81   void log_heap(CollectedHeap* heap, bool before);
 82 
 83  public:
 84   GCHeapLog() : EventLogBase<GCMessage>("GC Heap History", "gc") {}
 85 
 86   void log_heap_before(CollectedHeap* heap) {
 87     log_heap(heap, true);
 88   }
 89   void log_heap_after(CollectedHeap* heap) {
 90     log_heap(heap, false);
 91   }
 92 };
 93 
 94 void GCHeapLog::log_heap(CollectedHeap* heap, bool before) {
 95   if (!should_log()) {
 96     return;
 97   }
 98 
 99   double timestamp = fetch_timestamp();
100   MutexLocker ml(&_mutex, Mutex::_no_safepoint_check_flag);
101   int index = compute_log_index();
102   _records[index].thread = nullptr; // Its the GC thread so it's not that interesting.
103   _records[index].timestamp = timestamp;
104   _records[index].data.is_before = before;
105   stringStream st(_records[index].data.buffer(), _records[index].data.size());
106 
107   st.print_cr("{Heap %s GC invocations=%u (full %u):",
108                  before ? "before" : "after",
109                  heap->total_collections(),
110                  heap->total_full_collections());
111 
112   heap->print_on(&st);
113   st.print_cr("}");
114 }
115 
116 ParallelObjectIterator::ParallelObjectIterator(uint thread_num) :
117   _impl(Universe::heap()->parallel_object_iterator(thread_num))
118 {}
119 
120 ParallelObjectIterator::~ParallelObjectIterator() {
121   delete _impl;
122 }
123 
124 void ParallelObjectIterator::object_iterate(ObjectClosure* cl, uint worker_id) {
125   _impl->object_iterate(cl, worker_id);
126 }
127 
128 size_t CollectedHeap::unused() const {
129   MutexLocker ml(Heap_lock);
130   return capacity() - used();
131 }
132 
133 VirtualSpaceSummary CollectedHeap::create_heap_space_summary() {
134   size_t capacity_in_words = capacity() / HeapWordSize;
135 
136   return VirtualSpaceSummary(
137     _reserved.start(), _reserved.start() + capacity_in_words, _reserved.end());
138 }
139 
140 GCHeapSummary CollectedHeap::create_heap_summary() {
141   VirtualSpaceSummary heap_space = create_heap_space_summary();
142   return GCHeapSummary(heap_space, used());
143 }
144 
145 MetaspaceSummary CollectedHeap::create_metaspace_summary() {
146   const MetaspaceChunkFreeListSummary& ms_chunk_free_list_summary =
147     MetaspaceUtils::chunk_free_list_summary(Metaspace::NonClassType);
148   const MetaspaceChunkFreeListSummary& class_chunk_free_list_summary =
149     MetaspaceUtils::chunk_free_list_summary(Metaspace::ClassType);
150   return MetaspaceSummary(MetaspaceGC::capacity_until_GC(),
151                           MetaspaceUtils::get_combined_statistics(),
152                           ms_chunk_free_list_summary, class_chunk_free_list_summary);
153 }
154 
155 bool CollectedHeap::contains_null(const oop* p) const {
156   return *p == nullptr;
157 }
158 
159 void CollectedHeap::print_heap_before_gc() {
160   LogTarget(Debug, gc, heap) lt;
161   if (lt.is_enabled()) {
162     LogStream ls(lt);
163     ls.print_cr("Heap before GC invocations=%u (full %u):", total_collections(), total_full_collections());
164     ResourceMark rm;
165     print_on(&ls);
166   }
167 
168   if (_gc_heap_log != nullptr) {
169     _gc_heap_log->log_heap_before(this);
170   }
171 }
172 
173 void CollectedHeap::print_heap_after_gc() {
174   LogTarget(Debug, gc, heap) lt;
175   if (lt.is_enabled()) {
176     LogStream ls(lt);
177     ls.print_cr("Heap after GC invocations=%u (full %u):", total_collections(), total_full_collections());
178     ResourceMark rm;
179     print_on(&ls);
180   }
181 
182   if (_gc_heap_log != nullptr) {
183     _gc_heap_log->log_heap_after(this);
184   }
185 }
186 
187 void CollectedHeap::print() const { print_on(tty); }
188 
189 void CollectedHeap::print_on_error(outputStream* st) const {
190   st->print_cr("Heap:");
191   print_extended_on(st);
192   st->cr();
193 
194   BarrierSet* bs = BarrierSet::barrier_set();
195   if (bs != nullptr) {
196     bs->print_on(st);
197   }
198 }
199 
200 void CollectedHeap::trace_heap(GCWhen::Type when, const GCTracer* gc_tracer) {
201   const GCHeapSummary& heap_summary = create_heap_summary();
202   gc_tracer->report_gc_heap_summary(when, heap_summary);
203 
204   const MetaspaceSummary& metaspace_summary = create_metaspace_summary();
205   gc_tracer->report_metaspace_summary(when, metaspace_summary);
206 }
207 
208 void CollectedHeap::trace_heap_before_gc(const GCTracer* gc_tracer) {
209   trace_heap(GCWhen::BeforeGC, gc_tracer);
210 }
211 
212 void CollectedHeap::trace_heap_after_gc(const GCTracer* gc_tracer) {
213   trace_heap(GCWhen::AfterGC, gc_tracer);
214 }
215 
216 // Default implementation, for collectors that don't support the feature.
217 bool CollectedHeap::supports_concurrent_gc_breakpoints() const {
218   return false;
219 }
220 
221 bool CollectedHeap::is_oop(oop object) const {
222   if (!is_object_aligned(object)) {
223     return false;
224   }
225 
226   if (!is_in(object)) {
227     return false;
228   }
229 
230   return true;
231 }
232 
233 // Memory state functions.
234 
235 
236 CollectedHeap::CollectedHeap() :
237   _capacity_at_last_gc(0),
238   _used_at_last_gc(0),
239   _is_gc_active(false),
240   _last_whole_heap_examined_time_ns(os::javaTimeNanos()),
241   _total_collections(0),
242   _total_full_collections(0),
243   _gc_cause(GCCause::_no_gc),
244   _gc_lastcause(GCCause::_no_gc)
245 {
246   // If the minimum object size is greater than MinObjAlignment, we can
247   // end up with a shard at the end of the buffer that's smaller than
248   // the smallest object.  We can't allow that because the buffer must
249   // look like it's full of objects when we retire it, so we make
250   // sure we have enough space for a filler int array object.
251   size_t min_size = min_dummy_object_size();
252   _lab_alignment_reserve = min_size > (size_t)MinObjAlignment ? align_object_size(min_size) : 0;
253 
254   const size_t max_len = size_t(arrayOopDesc::max_array_length(T_INT));
255   const size_t elements_per_word = HeapWordSize / sizeof(jint);
256   int header_size_in_bytes = arrayOopDesc::base_offset_in_bytes(T_INT);
257   assert(header_size_in_bytes % sizeof(jint) == 0, "must be aligned to int");
258   int header_size_in_ints = header_size_in_bytes / sizeof(jint);
259   _filler_array_max_size = align_object_size((header_size_in_ints + max_len) / elements_per_word);
260 
261   NOT_PRODUCT(_promotion_failure_alot_count = 0;)
262   NOT_PRODUCT(_promotion_failure_alot_gc_number = 0;)
263 
264   if (UsePerfData) {
265     EXCEPTION_MARK;
266 
267     // create the gc cause jvmstat counters
268     _perf_gc_cause = PerfDataManager::create_string_variable(SUN_GC, "cause",
269                              80, GCCause::to_string(_gc_cause), CHECK);
270 
271     _perf_gc_lastcause =
272                 PerfDataManager::create_string_variable(SUN_GC, "lastCause",
273                              80, GCCause::to_string(_gc_lastcause), CHECK);
274   }
275 
276   // Create the ring log
277   if (LogEvents) {
278     _gc_heap_log = new GCHeapLog();
279   } else {
280     _gc_heap_log = nullptr;
281   }
282 }
283 
284 // This interface assumes that it's being called by the
285 // vm thread. It collects the heap assuming that the
286 // heap lock is already held and that we are executing in
287 // the context of the vm thread.
288 void CollectedHeap::collect_as_vm_thread(GCCause::Cause cause) {
289   Thread* thread = Thread::current();
290   assert(thread->is_VM_thread(), "Precondition#1");
291   assert(Heap_lock->is_locked(), "Precondition#2");
292   GCCauseSetter gcs(this, cause);
293   switch (cause) {
294     case GCCause::_codecache_GC_threshold:
295     case GCCause::_codecache_GC_aggressive:
296     case GCCause::_heap_inspection:
297     case GCCause::_heap_dump:
298     case GCCause::_metadata_GC_threshold: {
299       HandleMark hm(thread);
300       do_full_collection(false);        // don't clear all soft refs
301       break;
302     }
303     case GCCause::_metadata_GC_clear_soft_refs: {
304       HandleMark hm(thread);
305       do_full_collection(true);         // do clear all soft refs
306       break;
307     }
308     default:
309       ShouldNotReachHere(); // Unexpected use of this function
310   }
311 }
312 
313 MetaWord* CollectedHeap::satisfy_failed_metadata_allocation(ClassLoaderData* loader_data,
314                                                             size_t word_size,
315                                                             Metaspace::MetadataType mdtype) {
316   uint loop_count = 0;
317   uint gc_count = 0;
318   uint full_gc_count = 0;
319 
320   assert(!Heap_lock->owned_by_self(), "Should not be holding the Heap_lock");
321 
322   do {
323     MetaWord* result = loader_data->metaspace_non_null()->allocate(word_size, mdtype);
324     if (result != nullptr) {
325       return result;
326     }
327 
328     if (GCLocker::is_active_and_needs_gc()) {
329       // If the GCLocker is active, just expand and allocate.
330       // If that does not succeed, wait if this thread is not
331       // in a critical section itself.
332       result = loader_data->metaspace_non_null()->expand_and_allocate(word_size, mdtype);
333       if (result != nullptr) {
334         return result;
335       }
336       JavaThread* jthr = JavaThread::current();
337       if (!jthr->in_critical()) {
338         // Wait for JNI critical section to be exited
339         GCLocker::stall_until_clear();
340         // The GC invoked by the last thread leaving the critical
341         // section will be a young collection and a full collection
342         // is (currently) needed for unloading classes so continue
343         // to the next iteration to get a full GC.
344         continue;
345       } else {
346         if (CheckJNICalls) {
347           fatal("Possible deadlock due to allocating while"
348                 " in jni critical section");
349         }
350         return nullptr;
351       }
352     }
353 
354     {  // Need lock to get self consistent gc_count's
355       MutexLocker ml(Heap_lock);
356       gc_count      = Universe::heap()->total_collections();
357       full_gc_count = Universe::heap()->total_full_collections();
358     }
359 
360     // Generate a VM operation
361     VM_CollectForMetadataAllocation op(loader_data,
362                                        word_size,
363                                        mdtype,
364                                        gc_count,
365                                        full_gc_count,
366                                        GCCause::_metadata_GC_threshold);
367     VMThread::execute(&op);
368 
369     // If GC was locked out, try again. Check before checking success because the
370     // prologue could have succeeded and the GC still have been locked out.
371     if (op.gc_locked()) {
372       continue;
373     }
374 
375     if (op.prologue_succeeded()) {
376       return op.result();
377     }
378     loop_count++;
379     if ((QueuedAllocationWarningCount > 0) &&
380         (loop_count % QueuedAllocationWarningCount == 0)) {
381       log_warning(gc, ergo)("satisfy_failed_metadata_allocation() retries %d times,"
382                             " size=" SIZE_FORMAT, loop_count, word_size);
383     }
384   } while (true);  // Until a GC is done
385 }
386 
387 MemoryUsage CollectedHeap::memory_usage() {
388   return MemoryUsage(InitialHeapSize, used(), capacity(), max_capacity());
389 }
390 
391 void CollectedHeap::set_gc_cause(GCCause::Cause v) {
392   if (UsePerfData) {
393     _gc_lastcause = _gc_cause;
394     _perf_gc_lastcause->set_value(GCCause::to_string(_gc_lastcause));
395     _perf_gc_cause->set_value(GCCause::to_string(v));
396   }
397   _gc_cause = v;
398 }
399 
400 #ifndef PRODUCT
401 void CollectedHeap::check_for_non_bad_heap_word_value(HeapWord* addr, size_t size) {
402   if (CheckMemoryInitialization && ZapUnusedHeapArea) {
403     // please note mismatch between size (in 32/64 bit words), and ju_addr that always point to a 32 bit word
404     for (juint* ju_addr = reinterpret_cast<juint*>(addr); ju_addr < reinterpret_cast<juint*>(addr + size); ++ju_addr) {
405       assert(*ju_addr == badHeapWordVal, "Found non badHeapWordValue in pre-allocation check");
406     }
407   }
408 }
409 #endif // PRODUCT
410 
411 size_t CollectedHeap::max_tlab_size() const {
412   // TLABs can't be bigger than we can fill with a int[Integer.MAX_VALUE].
413   // This restriction could be removed by enabling filling with multiple arrays.
414   // If we compute that the reasonable way as
415   //    header_size + ((sizeof(jint) * max_jint) / HeapWordSize)
416   // we'll overflow on the multiply, so we do the divide first.
417   // We actually lose a little by dividing first,
418   // but that just makes the TLAB  somewhat smaller than the biggest array,
419   // which is fine, since we'll be able to fill that.
420   int header_size_in_bytes = typeArrayOopDesc::base_offset_in_bytes(T_INT);
421   assert(header_size_in_bytes % sizeof(jint) == 0, "header size must align to int");
422   size_t max_int_size = header_size_in_bytes / HeapWordSize +
423               sizeof(jint) *
424               ((juint) max_jint / (size_t) HeapWordSize);
425   return align_down(max_int_size, MinObjAlignment);
426 }
427 
428 size_t CollectedHeap::filler_array_min_size() {
429   int aligned_header_size_words = align_up(arrayOopDesc::base_offset_in_bytes(T_INT), HeapWordSize) / HeapWordSize;
430   return align_object_size(aligned_header_size_words); // align to MinObjAlignment
431 }
432 
433 void CollectedHeap::zap_filler_array_with(HeapWord* start, size_t words, juint value) {
434   int payload_start = align_up(arrayOopDesc::base_offset_in_bytes(T_INT), HeapWordSize) / HeapWordSize;
435   Copy::fill_to_words(start + payload_start,
436                       words - payload_start, value);
437 }
438 
439 #ifdef ASSERT
440 void CollectedHeap::fill_args_check(HeapWord* start, size_t words)
441 {
442   assert(words >= min_fill_size(), "too small to fill");
443   assert(is_object_aligned(words), "unaligned size");
444 }
445 
446 void CollectedHeap::zap_filler_array(HeapWord* start, size_t words, bool zap)
447 {
448   if (ZapFillerObjects && zap) {
449     zap_filler_array_with(start, words, 0XDEAFBABE);
450   }
451 }
452 #endif // ASSERT
453 
454 void
455 CollectedHeap::fill_with_array(HeapWord* start, size_t words, bool zap)
456 {
457   assert(words >= filler_array_min_size(), "too small for an array");
458   assert(words <= filler_array_max_size(), "too big for a single object");
459 
460   const size_t payload_size_bytes = words * HeapWordSize - arrayOopDesc::base_offset_in_bytes(T_INT);
461   assert(payload_size_bytes % sizeof(jint) == 0, "must be int aligned");
462   const size_t len = payload_size_bytes / sizeof(jint);
463   assert((int)len >= 0, "size too large " SIZE_FORMAT " becomes %d", words, (int)len);
464 
465   ObjArrayAllocator allocator(Universe::fillerArrayKlassObj(), words, (int)len, /* do_zero */ false);
466   allocator.initialize(start);
467   if (DumpSharedSpaces) {
468     // This array is written into the CDS archive. Make sure it
469     // has deterministic contents.
470     zap_filler_array_with(start, words, 0);
471   } else {
472     DEBUG_ONLY(zap_filler_array(start, words, zap);)
473   }
474 }
475 
476 void
477 CollectedHeap::fill_with_object_impl(HeapWord* start, size_t words, bool zap)
478 {
479   assert(words <= filler_array_max_size(), "too big for a single object");
480 
481   if (words >= filler_array_min_size()) {
482     fill_with_array(start, words, zap);
483   } else if (words > 0) {
484     assert(words == min_fill_size(), "unaligned size");
485     ObjAllocator allocator(CollectedHeap::filler_object_klass(), words);
486     allocator.initialize(start);
487   }
488 }
489 
490 void CollectedHeap::fill_with_object(HeapWord* start, size_t words, bool zap)
491 {
492   DEBUG_ONLY(fill_args_check(start, words);)
493   HandleMark hm(Thread::current());  // Free handles before leaving.
494   fill_with_object_impl(start, words, zap);
495 }
496 
497 void CollectedHeap::fill_with_objects(HeapWord* start, size_t words, bool zap)
498 {
499   DEBUG_ONLY(fill_args_check(start, words);)
500   HandleMark hm(Thread::current());  // Free handles before leaving.
501 
502   // Multiple objects may be required depending on the filler array maximum size. Fill
503   // the range up to that with objects that are filler_array_max_size sized. The
504   // remainder is filled with a single object.
505   const size_t min = min_fill_size();
506   const size_t max = filler_array_max_size();
507   while (words > max) {
508     const size_t cur = (words - max) >= min ? max : max - min;
509     fill_with_array(start, cur, zap);
510     start += cur;
511     words -= cur;
512   }
513 
514   fill_with_object_impl(start, words, zap);
515 }
516 
517 void CollectedHeap::fill_with_dummy_object(HeapWord* start, HeapWord* end, bool zap) {
518   CollectedHeap::fill_with_object(start, end, zap);
519 }
520 
521 HeapWord* CollectedHeap::allocate_new_tlab(size_t min_size,
522                                            size_t requested_size,
523                                            size_t* actual_size) {
524   guarantee(false, "thread-local allocation buffers not supported");
525   return nullptr;
526 }
527 
528 void CollectedHeap::ensure_parsability(bool retire_tlabs) {
529   assert(SafepointSynchronize::is_at_safepoint() || !is_init_completed(),
530          "Should only be called at a safepoint or at start-up");
531 
532   ThreadLocalAllocStats stats;
533 
534   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next();) {
535     BarrierSet::barrier_set()->make_parsable(thread);
536     if (UseTLAB) {
537       if (retire_tlabs) {
538         thread->tlab().retire(&stats);
539       } else {
540         thread->tlab().make_parsable();
541       }
542     }
543   }
544 
545   stats.publish();
546 }
547 
548 void CollectedHeap::resize_all_tlabs() {
549   assert(SafepointSynchronize::is_at_safepoint() || !is_init_completed(),
550          "Should only resize tlabs at safepoint");
551 
552   if (UseTLAB && ResizeTLAB) {
553     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {
554       thread->tlab().resize();
555     }
556   }
557 }
558 
559 jlong CollectedHeap::millis_since_last_whole_heap_examined() {
560   return (os::javaTimeNanos() - _last_whole_heap_examined_time_ns) / NANOSECS_PER_MILLISEC;
561 }
562 
563 void CollectedHeap::record_whole_heap_examined_timestamp() {
564   _last_whole_heap_examined_time_ns = os::javaTimeNanos();
565 }
566 
567 void CollectedHeap::full_gc_dump(GCTimer* timer, bool before) {
568   assert(timer != nullptr, "timer is null");
569   if ((HeapDumpBeforeFullGC && before) || (HeapDumpAfterFullGC && !before)) {
570     GCTraceTime(Info, gc) tm(before ? "Heap Dump (before full gc)" : "Heap Dump (after full gc)", timer);
571     HeapDumper::dump_heap();
572   }
573 
574   LogTarget(Trace, gc, classhisto) lt;
575   if (lt.is_enabled()) {
576     GCTraceTime(Trace, gc, classhisto) tm(before ? "Class Histogram (before full gc)" : "Class Histogram (after full gc)", timer);
577     ResourceMark rm;
578     LogStream ls(lt);
579     VM_GC_HeapInspection inspector(&ls, false /* ! full gc */);
580     inspector.doit();
581   }
582 }
583 
584 void CollectedHeap::pre_full_gc_dump(GCTimer* timer) {
585   full_gc_dump(timer, true);
586 }
587 
588 void CollectedHeap::post_full_gc_dump(GCTimer* timer) {
589   full_gc_dump(timer, false);
590 }
591 
592 void CollectedHeap::initialize_reserved_region(const ReservedHeapSpace& rs) {
593   // It is important to do this in a way such that concurrent readers can't
594   // temporarily think something is in the heap.  (Seen this happen in asserts.)
595   _reserved.set_word_size(0);
596   _reserved.set_start((HeapWord*)rs.base());
597   _reserved.set_end((HeapWord*)rs.end());
598 }
599 
600 void CollectedHeap::post_initialize() {
601   StringDedup::initialize();
602   initialize_serviceability();
603 }
604 
605 #ifndef PRODUCT
606 
607 bool CollectedHeap::promotion_should_fail(volatile size_t* count) {
608   // Access to count is not atomic; the value does not have to be exact.
609   if (PromotionFailureALot) {
610     const size_t gc_num = total_collections();
611     const size_t elapsed_gcs = gc_num - _promotion_failure_alot_gc_number;
612     if (elapsed_gcs >= PromotionFailureALotInterval) {
613       // Test for unsigned arithmetic wrap-around.
614       if (++*count >= PromotionFailureALotCount) {
615         *count = 0;
616         return true;
617       }
618     }
619   }
620   return false;
621 }
622 
623 bool CollectedHeap::promotion_should_fail() {
624   return promotion_should_fail(&_promotion_failure_alot_count);
625 }
626 
627 void CollectedHeap::reset_promotion_should_fail(volatile size_t* count) {
628   if (PromotionFailureALot) {
629     _promotion_failure_alot_gc_number = total_collections();
630     *count = 0;
631   }
632 }
633 
634 void CollectedHeap::reset_promotion_should_fail() {
635   reset_promotion_should_fail(&_promotion_failure_alot_count);
636 }
637 
638 #endif  // #ifndef PRODUCT
639 
640 // It's the caller's responsibility to ensure glitch-freedom
641 // (if required).
642 void CollectedHeap::update_capacity_and_used_at_gc() {
643   _capacity_at_last_gc = capacity();
644   _used_at_last_gc     = used();
645 }