1 /*
2  * Copyright (c) 2019, 2021, 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 "gc/g1/g1CollectionSetCandidates.hpp"
27 #include "gc/g1/g1CollectionSetChooser.hpp"
28 #include "gc/g1/heapRegion.inline.hpp"
29 
remove(uint num_regions)30 void G1CollectionSetCandidates::remove(uint num_regions) {
31   assert(num_regions <= num_remaining(), "Trying to remove more regions (%u) than available (%u)", num_regions, num_remaining());
32   for (uint i = 0; i < num_regions; i++) {
33     _remaining_reclaimable_bytes -= at(_front_idx)->reclaimable_bytes();
34     _front_idx++;
35   }
36 }
37 
remove_from_end(uint num_remove,size_t wasted)38 void G1CollectionSetCandidates::remove_from_end(uint num_remove, size_t wasted) {
39   assert(num_remove <= num_remaining(), "trying to remove more regions than remaining");
40 
41 #ifdef ASSERT
42   size_t reclaimable = 0;
43 
44   for (uint i = 0; i < num_remove; i++) {
45     uint cur_idx = _num_regions - i - 1;
46     reclaimable += at(cur_idx)->reclaimable_bytes();
47     // Make sure we crash if we access it.
48     _regions[cur_idx] = NULL;
49   }
50 
51   assert(reclaimable == wasted, "Recalculated reclaimable inconsistent");
52 #endif
53   _num_regions -= num_remove;
54   _remaining_reclaimable_bytes -= wasted;
55 }
56 
iterate(HeapRegionClosure * cl)57 void G1CollectionSetCandidates::iterate(HeapRegionClosure* cl) {
58   for (uint i = _front_idx; i < _num_regions; i++) {
59     HeapRegion* r = _regions[i];
60     if (cl->do_heap_region(r)) {
61       cl->set_incomplete();
62       break;
63     }
64   }
65 }
66 
iterate_backwards(HeapRegionClosure * cl)67 void G1CollectionSetCandidates::iterate_backwards(HeapRegionClosure* cl) {
68   for (uint i = _num_regions; i > _front_idx; i--) {
69     HeapRegion* r = _regions[i - 1];
70     if (cl->do_heap_region(r)) {
71       cl->set_incomplete();
72       break;
73     }
74   }
75 }
76 
77 #ifndef PRODUCT
verify() const78 void G1CollectionSetCandidates::verify() const {
79   guarantee(_front_idx <= _num_regions, "Index: %u Num_regions: %u", _front_idx, _num_regions);
80   uint idx = _front_idx;
81   size_t sum_of_reclaimable_bytes = 0;
82   HeapRegion *prev = NULL;
83   for (; idx < _num_regions; idx++) {
84     HeapRegion *cur = _regions[idx];
85     guarantee(cur != NULL, "Regions after _front_idx %u cannot be NULL but %u is", _front_idx, idx);
86     // The first disjunction filters out regions with objects that were explicitly
87     // pinned after being added to the collection set candidates. Archive regions
88     // should never have been added to the collection set though.
89     guarantee((cur->is_pinned() && !cur->is_archive()) ||
90               G1CollectionSetChooser::should_add(cur),
91               "Region %u should be eligible for addition.", cur->hrm_index());
92     if (prev != NULL) {
93       guarantee(prev->gc_efficiency() >= cur->gc_efficiency(),
94                 "GC efficiency for region %u: %1.4f smaller than for region %u: %1.4f",
95                 prev->hrm_index(), prev->gc_efficiency(), cur->hrm_index(), cur->gc_efficiency());
96     }
97     sum_of_reclaimable_bytes += cur->reclaimable_bytes();
98     prev = cur;
99   }
100   guarantee(sum_of_reclaimable_bytes == _remaining_reclaimable_bytes,
101             "Inconsistent remaining_reclaimable bytes, remaining " SIZE_FORMAT " calculated " SIZE_FORMAT,
102             _remaining_reclaimable_bytes, sum_of_reclaimable_bytes);
103 }
104 #endif // !PRODUCT
105