1 /*
 2  * Copyright (c) 2021, Huawei Technologies Co. Ltd. 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.hpp"
28 #include "gc/g1/g1EvacFailureRegions.inline.hpp"
29 #include "gc/g1/heapRegion.hpp"
30 #include "memory/allocation.hpp"
31 #include "runtime/atomic.hpp"
32 
33 G1EvacFailureRegions::G1EvacFailureRegions() :
34   _regions_failed_evacuation(mtGC),
35   _evac_failure_regions(nullptr),
36   _evac_failure_regions_cur_length(0),
37   _max_regions(0) { }
38 
39 G1EvacFailureRegions::~G1EvacFailureRegions() {
40   assert(_evac_failure_regions == nullptr, "not cleaned up");
41 }
42 
43 void G1EvacFailureRegions::pre_collection(uint max_regions) {
44   Atomic::store(&_evac_failure_regions_cur_length, 0u);
45   _max_regions = max_regions;
46   _regions_failed_evacuation.resize(_max_regions);
47   _evac_failure_regions = NEW_C_HEAP_ARRAY(uint, _max_regions, mtGC);
48 }
49 
50 void G1EvacFailureRegions::post_collection() {
51   _regions_failed_evacuation.resize(0);
52   FREE_C_HEAP_ARRAY(uint, _evac_failure_regions);
53   _evac_failure_regions = nullptr;
54   _max_regions = 0; // To have any record() attempt fail in the future.
55 }
56 
57 void G1EvacFailureRegions::par_iterate(HeapRegionClosure* closure,
58                                        HeapRegionClaimer* _hrclaimer,
59                                        uint worker_id) {
60   G1CollectedHeap::heap()->par_iterate_regions_array(closure,
61                                                      _hrclaimer,
62                                                      _evac_failure_regions,
63                                                      Atomic::load(&_evac_failure_regions_cur_length),
64                                                      worker_id);
65 }
66 
67 bool G1EvacFailureRegions::contains(uint region_idx) const {
68   assert(region_idx < _max_regions, "must be");
69   return _regions_failed_evacuation.par_at(region_idx, memory_order_relaxed);
70 }