|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 | + |
| 27 | +#include "gc/g1/g1CollectedHeap.inline.hpp" |
| 28 | +#include "gc/g1/g1YoungGCAllocationFailureInjector.inline.hpp" |
| 29 | +#include "gc/g1/g1_globals.hpp" |
| 30 | + |
| 31 | +#if ALLOCATION_FAILURE_INJECTOR |
| 32 | + |
| 33 | +class SelectAllocationFailureRegionClosure : public HeapRegionClosure { |
| 34 | + CHeapBitMap& _allocation_failure_regions; |
| 35 | + size_t _allocation_failure_regions_num; |
| 36 | + |
| 37 | +public: |
| 38 | + SelectAllocationFailureRegionClosure(CHeapBitMap& allocation_failure_regions, size_t cset_length) : |
| 39 | + _allocation_failure_regions(allocation_failure_regions), |
| 40 | + _allocation_failure_regions_num(cset_length * G1GCAllocationFailureALotCSetPercent / 100) { } |
| 41 | + |
| 42 | + bool do_heap_region(HeapRegion* r) override { |
| 43 | + assert(r->in_collection_set(), "must be"); |
| 44 | + if (_allocation_failure_regions_num > 0) { |
| 45 | + _allocation_failure_regions.set_bit(r->hrm_index()); |
| 46 | + --_allocation_failure_regions_num; |
| 47 | + } |
| 48 | + return _allocation_failure_regions_num == 0; |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +G1YoungGCAllocationFailureInjector::G1YoungGCAllocationFailureInjector() |
| 53 | + : _inject_allocation_failure_for_current_gc(), |
| 54 | + _last_collection_with_allocation_failure(), |
| 55 | + _allocation_failure_regions(mtGC) {} |
| 56 | + |
| 57 | +void G1YoungGCAllocationFailureInjector::select_allocation_failure_regions() { |
| 58 | + G1CollectedHeap* g1h = G1CollectedHeap::heap(); |
| 59 | + _allocation_failure_regions.reinitialize(g1h->max_reserved_regions()); |
| 60 | + SelectAllocationFailureRegionClosure closure(_allocation_failure_regions, g1h->collection_set()->cur_length()); |
| 61 | + g1h->collection_set_iterate_all(&closure); |
| 62 | +} |
| 63 | + |
| 64 | +bool G1YoungGCAllocationFailureInjector::arm_if_needed_for_gc_type(bool for_young_only_phase, |
| 65 | + bool during_concurrent_start, |
| 66 | + bool mark_or_rebuild_in_progress) { |
| 67 | + bool res = false; |
| 68 | + if (mark_or_rebuild_in_progress) { |
| 69 | + res |= G1GCAllocationFailureALotDuringConcMark; |
| 70 | + } |
| 71 | + if (during_concurrent_start) { |
| 72 | + res |= G1GCAllocationFailureALotDuringConcurrentStart; |
| 73 | + } |
| 74 | + if (for_young_only_phase) { |
| 75 | + res |= G1GCAllocationFailureALotDuringYoungGC; |
| 76 | + } else { |
| 77 | + // GCs are mixed |
| 78 | + res |= G1GCAllocationFailureALotDuringMixedGC; |
| 79 | + } |
| 80 | + return res; |
| 81 | +} |
| 82 | + |
| 83 | +void G1YoungGCAllocationFailureInjector::arm_if_needed() { |
| 84 | + if (G1GCAllocationFailureALot) { |
| 85 | + G1CollectedHeap* g1h = G1CollectedHeap::heap(); |
| 86 | + // Check if we have gone over the interval. |
| 87 | + const size_t gc_num = g1h->total_collections(); |
| 88 | + const size_t elapsed_gcs = gc_num - _last_collection_with_allocation_failure; |
| 89 | + |
| 90 | + _inject_allocation_failure_for_current_gc = (elapsed_gcs >= G1GCAllocationFailureALotInterval); |
| 91 | + |
| 92 | + // Now check if evacuation failure injection should be enabled for the current GC. |
| 93 | + G1CollectorState* collector_state = g1h->collector_state(); |
| 94 | + const bool in_young_only_phase = collector_state->in_young_only_phase(); |
| 95 | + const bool in_concurrent_start_gc = collector_state->in_concurrent_start_gc(); |
| 96 | + const bool mark_or_rebuild_in_progress = collector_state->mark_or_rebuild_in_progress(); |
| 97 | + |
| 98 | + _inject_allocation_failure_for_current_gc &= |
| 99 | + arm_if_needed_for_gc_type(in_young_only_phase, |
| 100 | + in_concurrent_start_gc, |
| 101 | + mark_or_rebuild_in_progress); |
| 102 | + |
| 103 | + if (_inject_allocation_failure_for_current_gc) { |
| 104 | + select_allocation_failure_regions(); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +void G1YoungGCAllocationFailureInjector::reset() { |
| 110 | + _last_collection_with_allocation_failure = G1CollectedHeap::heap()->total_collections(); |
| 111 | + _inject_allocation_failure_for_current_gc = false; |
| 112 | +} |
| 113 | + |
| 114 | +#endif // #if ALLOCATION_FAILURE_INJECTOR |
0 commit comments